mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Progressively rebuild when files change
This commit is contained in:
8 files changed
+225
-6
No files matched your search
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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)`;
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user