ui: add opt-in run_javascript frontend tool (#24244)

* ui: add opt-in run_javascript frontend tool

Expose a run_javascript tool to the model, executed entirely in the
browser through the existing agentic loop. Code runs in a Web Worker
inside a sandboxed iframe with an opaque origin, isolated from the
WebUI and its API. Console output, errors and the return value are
fed back as the tool result. The parent enforces a hard timeout by
removing the iframe, which terminates the worker.

Disabled by default, toggle in Settings > Developer.

* ui: address review feedback from allozaur

Use the JsonSchemaType enum for the tool definition parameter types
instead of raw string literals, extending it with STRING and NUMBER.

Move the worker shim and the iframe harness html into their own files
so the service no longer carries inline source blobs.

Replace the remaining magic strings with constants: SANDBOX_EMPTY_OUTPUT
and SANDBOX_TRUNCATION_NOTICE, and reuse NEWLINE_SEPARATOR for joins.

* ui: move sandbox worker shim to a raw imported file

Replace the inline worker template string with a real sandbox-worker.js
imported as raw text, and build the iframe harness from it in
sandbox-harness.ts. The raw worker ships as a string, not a module, so
it is excluded from eslint and the typecheck program.
This commit is contained in:
Pascal
2026-06-09 18:02:31 +02:00
committed by GitHub
parent 49f3542190
commit 483609509d
15 changed files with 283 additions and 6 deletions
+20
View File
@@ -261,6 +261,26 @@ export { ParameterSyncService } from './parameter-sync.service';
*/
export { MCPService } from './mcp.service';
/**
* **SandboxService** - Frontend JavaScript execution in a browser sandbox
*
* Stateless executor for the run_javascript frontend tool. Model generated
* code runs in a Web Worker spawned inside a sandboxed iframe with an opaque
* origin: no access to the app origin, its storage or its API, and outgoing
* requests carry a null origin. The code never touches a main thread, so the
* parent enforces the timeout by removing the iframe, which terminates the
* worker at the browser level.
*
* **Architecture & Relationships:**
* - **SandboxService** (this class): Stateless sandbox execution
* - **toolsStore**: Exposes the tool definition when the sandbox is enabled
* - **agenticStore**: Dispatches ToolSource.FRONTEND calls here
*
* @see SANDBOX_TOOL_DEFINITION in constants/sandbox.ts - tool schema sent to the LLM
* @see agenticStore in stores/agentic.svelte.ts - tool dispatch
*/
export { SandboxService } from './sandbox.service';
/**
* **RouterService** — Dynamic route URL construction utility
*
@@ -0,0 +1,25 @@
import WORKER_SHIM from './sandbox-worker.js?raw';
/**
* Harness loaded as srcdoc into a sandboxed iframe (allow-scripts only).
* The opaque origin is the security boundary: no access to the app origin,
* its storage or its API. The harness spawns a worker so model code never
* runs on a main thread, which makes the parent timeout enforceable by
* removing the iframe.
*/
export const SANDBOX_HARNESS_HTML = `<!doctype html><script>
const SHIM = ${JSON.stringify(WORKER_SHIM)};
addEventListener('message', (event) => {
const respond = (payload) => parent.postMessage(payload, '*');
let worker;
try {
worker = new Worker(URL.createObjectURL(new Blob([SHIM], { type: 'text/javascript' })));
} catch (err) {
respond({ logs: [], result: null, error: 'Worker creation failed: ' + err });
return;
}
worker.onmessage = (msg) => respond(msg.data);
worker.onerror = (err) => respond({ logs: [], result: null, error: String(err.message || err) });
worker.postMessage({ code: event.data.code });
});
</script>`;
@@ -0,0 +1,30 @@
const logs = [];
const fmt = (value) => {
if (typeof value === 'string') return value;
try {
return JSON.stringify(value);
} catch {
return String(value);
}
};
const capture =
(level, prefix) =>
(...args) => {
logs.push(prefix + args.map(fmt).join(' '));
};
console.log = capture('log', '');
console.info = capture('info', '');
console.debug = capture('debug', '');
console.warn = capture('warn', 'warn: ');
console.error = capture('error', 'error: ');
self.onmessage = async (event) => {
const reply = { logs, result: null, error: null };
try {
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const value = await new AsyncFunction(event.data.code)();
if (value !== undefined) reply.result = fmt(value);
} catch (err) {
reply.error = err instanceof Error ? err.stack || err.message : String(err);
}
self.postMessage(reply);
};
@@ -0,0 +1,112 @@
import {
NEWLINE_SEPARATOR,
SANDBOX_EMPTY_OUTPUT,
SANDBOX_OUTPUT_MAX_CHARS,
SANDBOX_TIMEOUT_MS_DEFAULT,
SANDBOX_TIMEOUT_MS_MAX,
SANDBOX_TOOL_NAME,
SANDBOX_TRUNCATION_NOTICE
} from '$lib/constants';
import { SANDBOX_HARNESS_HTML } from './sandbox-harness';
import type { ToolExecutionResult } from '$lib/types';
interface SandboxReply {
logs?: unknown;
result?: unknown;
error?: unknown;
}
function formatReply(reply: SandboxReply): ToolExecutionResult {
const lines: string[] = [];
if (Array.isArray(reply.logs)) {
for (const line of reply.logs) lines.push(String(line));
}
if (reply.error != null) {
lines.push(`Error: ${String(reply.error)}`);
} else if (reply.result != null) {
lines.push(`=> ${String(reply.result)}`);
}
let content = lines.join(NEWLINE_SEPARATOR);
if (!content) content = SANDBOX_EMPTY_OUTPUT;
if (content.length > SANDBOX_OUTPUT_MAX_CHARS) {
content = `${content.slice(0, SANDBOX_OUTPUT_MAX_CHARS)}${NEWLINE_SEPARATOR}${SANDBOX_TRUNCATION_NOTICE}`;
}
return { content, isError: reply.error != null };
}
export class SandboxService {
/**
* Execute a frontend sandbox tool call and return its output.
* One disposable iframe per execution, removed on completion,
* timeout or abort. Removing the iframe terminates the worker
* at the browser level, so runaway code cannot outlive it.
*/
static executeTool(
toolName: string,
params: Record<string, unknown>,
signal?: AbortSignal
): Promise<ToolExecutionResult> {
if (toolName !== SANDBOX_TOOL_NAME) {
return Promise.resolve({ content: `Unknown frontend tool: ${toolName}`, isError: true });
}
const code = typeof params.code === 'string' ? params.code : '';
if (!code) {
return Promise.resolve({ content: 'Missing required parameter: code', isError: true });
}
const requested = Number(params.timeout_ms);
const timeoutMs =
Number.isFinite(requested) && requested > 0
? Math.min(requested, SANDBOX_TIMEOUT_MS_MAX)
: SANDBOX_TIMEOUT_MS_DEFAULT;
return new Promise<ToolExecutionResult>((resolve, reject) => {
const iframe = document.createElement('iframe');
iframe.setAttribute('sandbox', 'allow-scripts');
iframe.style.display = 'none';
iframe.srcdoc = SANDBOX_HARNESS_HTML;
let settled = false;
const cleanup = () => {
settled = true;
clearTimeout(timer);
window.removeEventListener('message', onMessage);
signal?.removeEventListener('abort', onAbort);
iframe.remove();
};
const finish = (result: ToolExecutionResult) => {
if (settled) return;
cleanup();
resolve(result);
};
const onAbort = () => {
if (settled) return;
cleanup();
reject(new DOMException('Sandbox execution aborted', 'AbortError'));
};
const onMessage = (event: MessageEvent) => {
if (event.source !== iframe.contentWindow) return;
finish(formatReply((event.data ?? {}) as SandboxReply));
};
const timer = setTimeout(
() => finish({ content: `Execution timed out after ${timeoutMs} ms`, isError: true }),
timeoutMs
);
window.addEventListener('message', onMessage);
signal?.addEventListener('abort', onAbort);
iframe.onload = () => iframe.contentWindow?.postMessage({ code }, '*');
document.body.appendChild(iframe);
});
}
}