Progressively rebuild when files change

This commit is contained in:
Alex Gleason committed 2025-11-04 17:22:12 -06:00
1 parent adc4dd19b7
commit ef6962d7c7
8 files changed
+225 -6

No files matched your search

+102
View File
@@ -0,0 +1,102 @@
/**
* BuildDebouncer manages automatic builds triggered by file changes.
* It debounces build requests to avoid excessive builds while the AI is working.
*/
export class BuildDebouncer {
private debounceTimers = new Map<string, NodeJS.Timeout>();
private pendingBuilds = new Map<string, Set<string>>();
private readonly debounceMs: number;
private readonly onBuild: (projectId: string, changedFiles: string[]) => void;
constructor(
debounceMs: number = 2000,
onBuild: (projectId: string, changedFiles: string[]) => void
) {
this.debounceMs = debounceMs;
this.onBuild = onBuild;
}
/**
* Register a file change for a project.
* This will trigger a debounced build after the configured delay.
*/
registerFileChange(projectId: string, filePath: string): void {
// Track the changed file
if (!this.pendingBuilds.has(projectId)) {
this.pendingBuilds.set(projectId, new Set());
}
this.pendingBuilds.get(projectId)!.add(filePath);
// Clear existing timer
const existingTimer = this.debounceTimers.get(projectId);
if (existingTimer) {
clearTimeout(existingTimer);
}
// Set new timer
const timer = setTimeout(() => {
this.triggerBuild(projectId);
}, this.debounceMs);
this.debounceTimers.set(projectId, timer);
}
/**
* Trigger an immediate build for a project, bypassing the debounce.
*/
triggerBuild(projectId: string): void {
const changedFiles = Array.from(this.pendingBuilds.get(projectId) || []);
if (changedFiles.length === 0) {
return;
}
// Clear the pending builds and timer
this.pendingBuilds.delete(projectId);
const timer = this.debounceTimers.get(projectId);
if (timer) {
clearTimeout(timer);
this.debounceTimers.delete(projectId);
}
// Trigger the build
this.onBuild(projectId, changedFiles);
}
/**
* Cancel any pending builds for a project.
*/
cancel(projectId: string): void {
const timer = this.debounceTimers.get(projectId);
if (timer) {
clearTimeout(timer);
this.debounceTimers.delete(projectId);
}
this.pendingBuilds.delete(projectId);
}
/**
* Check if a project has pending file changes.
*/
hasPendingChanges(projectId: string): boolean {
return (this.pendingBuilds.get(projectId)?.size || 0) > 0;
}
/**
* Get the list of pending changed files for a project.
*/
getPendingFiles(projectId: string): string[] {
return Array.from(this.pendingBuilds.get(projectId) || []);
}
/**
* Cleanup all timers (call on unmount/cleanup).
*/
cleanup(): void {
for (const timer of this.debounceTimers.values()) {
clearTimeout(timer);
}
this.debounceTimers.clear();
this.pendingBuilds.clear();
}
}
+1
View File
@@ -47,6 +47,7 @@ export interface SessionManagerEvents {
loadingChanged: (projectId: string, isLoading: boolean) => void;
costUpdated: (projectId: string, totalCost: number) => void;
contextUsageUpdated: (projectId: string, inputTokens: number) => void;
fileChanged: (projectId: string, filePath: string) => void;
}
/**
+8 -1
View File
@@ -17,6 +17,7 @@ export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams
private cwd: string;
private projectsPath: string;
private tmpPath: string;
private onFileChanged?: (filePath: string) => void;
readonly description = "Replace a string in a file with a new string";
@@ -29,11 +30,12 @@ export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams
),
});
constructor(fs: JSRuntimeFS, cwd: string, options?: { projectsPath?: string; tmpPath?: string }) {
constructor(fs: JSRuntimeFS, cwd: string, options?: { projectsPath?: string; tmpPath?: string; onFileChanged?: (filePath: string) => void }) {
this.fs = fs;
this.cwd = cwd;
this.projectsPath = options?.projectsPath || '/projects';
this.tmpPath = options?.tmpPath || '/tmp';
this.onFileChanged = options?.onFileChanged;
}
async execute(args: TextEditorStrReplaceParams): Promise<string> {
@@ -172,6 +174,11 @@ export class TextEditorStrReplaceTool implements Tool<TextEditorStrReplaceParams
await this.fs.writeFile(absolutePath, newContent, "utf8");
// Notify about file change (for auto-build)
if (this.onFileChanged) {
this.onFileChanged(path);
}
let successMessage = `String successfully replaced in ${path}`;
if (occurrences > 1) {
successMessage += ` (${occurrences} occurrences found, only first occurrence replaced)`;
+9 -1
View File
@@ -15,6 +15,7 @@ export class TextEditorWriteTool implements Tool<TextEditorWriteParams> {
private cwd: string;
private projectsPath: string;
private tmpPath: string;
private onFileChanged?: (filePath: string) => void;
readonly description = "Create or overwrite a file with new content";
@@ -25,11 +26,12 @@ export class TextEditorWriteTool implements Tool<TextEditorWriteParams> {
file_text: z.string().describe("Content to write to the file"),
});
constructor(fs: JSRuntimeFS, cwd: string, options?: { projectsPath?: string; tmpPath?: string }) {
constructor(fs: JSRuntimeFS, cwd: string, options?: { projectsPath?: string; tmpPath?: string; onFileChanged?: (filePath: string) => void }) {
this.fs = fs;
this.cwd = cwd;
this.projectsPath = options?.projectsPath || '/projects';
this.tmpPath = options?.tmpPath || '/tmp';
this.onFileChanged = options?.onFileChanged;
}
async execute(args: TextEditorWriteParams): Promise<string> {
@@ -58,6 +60,12 @@ export class TextEditorWriteTool implements Tool<TextEditorWriteParams> {
await this.fs.mkdir(dirname(absolutePath), { recursive: true });
await this.fs.writeFile(absolutePath, file_text, "utf8");
// Notify about file change (for auto-build)
if (this.onFileChanged) {
this.onFileChanged(path);
}
return `File successfully written to ${path}`;
} catch (error) {
throw new Error(String(error));