From 3932c5e04cd2c6fe3e8091d8b423d32f20cfe2a5 Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Sun, 16 Nov 2025 00:56:20 +0100 Subject: [PATCH] Add lifecycle callbacks to SandboxRuntimeProvider for abort signal support - Added onExecutionStart(sandboxId, signal) - called when sandbox execution begins - Added onExecutionEnd(sandboxId) - called when sandbox execution ends - Integrated callbacks in SandboxedIframe.execute() - Enables providers to track and cancel async operations (e.g., userScript executions) --- .../web-ui/src/components/SandboxedIframe.ts | 10 ++++++++++ .../sandbox/SandboxRuntimeProvider.ts | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/web-ui/src/components/SandboxedIframe.ts b/packages/web-ui/src/components/SandboxedIframe.ts index 211570f6..f1e8cb1f 100644 --- a/packages/web-ui/src/components/SandboxedIframe.ts +++ b/packages/web-ui/src/components/SandboxedIframe.ts @@ -254,6 +254,11 @@ export class SandboxIframe extends LitElement { providers = [consoleProvider, ...providers]; RUNTIME_MESSAGE_ROUTER.registerSandbox(sandboxId, providers, consumers); + // Notify providers that execution is starting + for (const provider of providers) { + provider.onExecutionStart?.(sandboxId, signal); + } + const files: SandboxFile[] = []; let completed = false; @@ -287,6 +292,11 @@ export class SandboxIframe extends LitElement { RUNTIME_MESSAGE_ROUTER.addConsumer(sandboxId, executionConsumer); const cleanup = () => { + // Notify providers that execution has ended + for (const provider of providers) { + provider.onExecutionEnd?.(sandboxId); + } + RUNTIME_MESSAGE_ROUTER.unregisterSandbox(sandboxId); signal?.removeEventListener("abort", abortHandler); clearTimeout(timeoutId); diff --git a/packages/web-ui/src/components/sandbox/SandboxRuntimeProvider.ts b/packages/web-ui/src/components/sandbox/SandboxRuntimeProvider.ts index 09cf6b03..90cb5df4 100644 --- a/packages/web-ui/src/components/sandbox/SandboxRuntimeProvider.ts +++ b/packages/web-ui/src/components/sandbox/SandboxRuntimeProvider.ts @@ -32,4 +32,21 @@ export interface SandboxRuntimeProvider { * This will be appended to tool descriptions dynamically so the LLM knows what's available. */ getDescription(): string; + + /** + * Optional lifecycle callback invoked when sandbox execution starts. + * Providers can use this to track abort signals for cancellation of async operations. + * + * @param sandboxId - The unique identifier for this sandbox execution + * @param signal - Optional AbortSignal that will be triggered if execution is cancelled + */ + onExecutionStart?(sandboxId: string, signal?: AbortSignal): void; + + /** + * Optional lifecycle callback invoked when sandbox execution ends (success, error, or abort). + * Providers can use this to clean up any resources associated with the sandbox. + * + * @param sandboxId - The unique identifier for this sandbox execution + */ + onExecutionEnd?(sandboxId: string): void; }