Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

feat: implement debounced save functionality in Workspace class

Muhammad Hamza Khan2597e6ab445a
diff --git a/packages/app/src/builder-ui/debugger.ts b/packages/app/src/builder-ui/debugger.ts
index 2748ae00..42803c17 100644
--- a/packages/app/src/builder-ui/debugger.ts
+++ b/packages/app/src/builder-ui/debugger.ts
@@ -347,7 +347,9 @@ export async function init() {
dbgStatus.classList.add('hidden');
}
});
- workspace.saveAgent();
+ // Use debounced save to handle rapid toggling - only the final state will be saved
+ // This prevents race conditions and reduces API calls while ensuring data consistency
+ workspace.saveAgentDebounced();
setTimeout(() => {
//workspace.redraw();
repaintDebugComponentsAfter();
diff --git a/packages/app/src/builder-ui/workspace/Workspace.class.ts b/packages/app/src/builder-ui/workspace/Workspace.class.ts
index 552c436c..0127db97 100644
--- a/packages/app/src/builder-ui/workspace/Workspace.class.ts
+++ b/packages/app/src/builder-ui/workspace/Workspace.class.ts
@@ -77,6 +77,12 @@ export class Workspace extends EventEmitter {
//private _agent: any = {};
private _loading = false;
private _saving = false;
+ /** Flag indicating a save is pending while another save is in progress */
+ private _pendingSave = false;
+ /** Timeout ID for debounced save operations */
+ private _debouncedSaveTimeout: ReturnType<typeof setTimeout> | null = null;
+ /** Default debounce delay in milliseconds for saveAgentDebounced */
+ private static readonly DEBOUNCE_SAVE_DELAY_MS = 500;
// public get agentInfo() {
// return this._agent;
// }
@@ -742,7 +748,8 @@ export class Workspace extends EventEmitter {
await delay(400); // wait for stateManager to unlock workspace
if (this._loading) return;
if (this._saving) {
- //console.log('Agent already saving ... skip');
+ // Queue pending save to ensure final state is persisted
+ this._pendingSave = true;
if (fireEvent) await this.export(fireEvent); //just export the data in order to trigger stateManager diff
return;
@@ -784,9 +791,17 @@ export class Workspace extends EventEmitter {
if (name) {
document.title = name + ' | SmythOS';
}
+
+ // Process pending save with fresh data to ensure final state is persisted
+ if (this._pendingSave) {
+ this._pendingSave = false;
+ return this.saveAgent();
+ }
+
return result;
} catch (error) {
this._saving = false;
+ this._pendingSave = false; // Clear pending on error
console.error('Error', error);
if (error?.errorCode === 'LOCKED_AGENT' || error?.errorCode === 'LOCKED_AGENT_HANDLED') {
this.updateAgentSaveStatus('View Only', 'alert');
@@ -806,17 +821,24 @@ export class Workspace extends EventEmitter {
}
}
this.updateAgentSaveStatus('Error', 'alert');
+ }
+ }
- // if (error?.errorCode === 'LOCKED_AGENT') {
- // alert(error.message, 'Agent Lock', 'OK')
- // .then((result) => {})
- // .catch((error) => {});
- // // this.locked = true;
- // } else {
- // toast(error.message ?? 'Saving Failed', 'Error', 'alert');
- // }
- // return null;
+ /**
+ * Debounced version of saveAgent - only the last call within the delay window triggers the save.
+ * @param delayMs - The debounce delay in milliseconds (default: 500ms)
+ */
+ saveAgentDebounced(delayMs: number = Workspace.DEBOUNCE_SAVE_DELAY_MS): void {
+ if (this._debouncedSaveTimeout !== null) {
+ clearTimeout(this._debouncedSaveTimeout);
}
+
+ this._debouncedSaveTimeout = setTimeout(() => {
+ this._debouncedSaveTimeout = null;
+ this.saveAgent().catch((error) => {
+ console.error('Debounced saveAgent failed:', error);
+ });
+ }, delayMs);
}
async createAgent(name, description = '', callback?) {