272 lines · 16.6 KB · TypeScript
RawDownload
/** * Build the ES module source that runs inside the dynamic Worker. * * The module exports a `WorkerEntrypoint` subclass with an `evaluate` * method that: * 1. Captures console output into `__logs`. * 2. Accumulates user-visible output from `emit`, `text`, `file`, `image`, *    `audio`, `resource`, and `notify`. * 3. Creates a recursive `tools` Proxy that dispatches calls via RPC. * 4. Executes the normalised user code with a `Promise.race` timeout. * 5. Returns `{ result, output?, error?, logs }`. * * Tool args cross the dispatcher boundary via Workers RPC, not JSON, so * `Uint8Array` / `ArrayBuffer` survive intact. `Blob` / `File` aren't on * RPC's structured-clone allow-list, so we encode them to a tagged * envelope (`__encodeBinary`) and the host rehydrates them. This is * required for any tool whose upstream wants `multipart/form-data` * (OpenAI Files API, etc.). */export const buildExecutorModule = (body: string, timeoutMs: number): string =>  [    'import { WorkerEntrypoint } from "cloudflare:workers";',    "",    "export default class CodeExecutor extends WorkerEntrypoint {",    "  async evaluate(__dispatcher) {",    "    const __logs = [];",    "    const __outputs = [];",    "    let __pendingOutput = Promise.resolve();",    "    const __recordOutput = (item) => {",    "      __outputs.push(item);",    "      if (__dispatcher && typeof __dispatcher.output === 'function') {",    "        __pendingOutput = __pendingOutput.then(() => __dispatcher.output(item)).catch(() => undefined);",    "      }",    "    };",    "    const yield_control = async () => {",    "      await __pendingOutput;",    "      if (__dispatcher && typeof __dispatcher.yieldControl === 'function') await __dispatcher.yieldControl();",    "    };",    "    const yieldControl = yield_control;",    '    console.log = (...a) => { __logs.push(a.map(String).join(" ")); };',    '    console.warn = (...a) => { __logs.push("[warn] " + a.map(String).join(" ")); };',    '    console.error = (...a) => { __logs.push("[error] " + a.map(String).join(" ")); };',    "    const __serializeErrorValue = (value) => {",    "      if (value instanceof Error) {",    "        return {",    "          __type: 'Error',",    "          name: value.name,",    "          message: value.message,",    "        };",    "      }",    "      if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {",    "        return value;",    "      }",    "      try {",    "        return JSON.parse(JSON.stringify(value));",    "      } catch {",    "        return String(value);",    "      }",    "    };",    "    const __renderMessage = (value) => {",    "      if (typeof value === 'string') return value;",    "      if (value && typeof value === 'object' && value.__type === 'Error' && typeof value.message === 'string') {",    "        return value.message;",    "      }",    "      if (value instanceof Error) return value.message;",    "      if (value && typeof value === 'object' && typeof value.message === 'string') return value.message;",    "      if (typeof value === 'undefined') return 'Unknown error';",    "      if (value && typeof value === 'object') {",    "        try {",    "          return JSON.stringify(value);",    "        } catch {",    "          return String(value);",    "        }",    "      }",    "      return String(value);",    "    };",    "    const __formatOutputText = (value) => {",    "      if (typeof value === 'undefined') return 'undefined';",    "      if (value === null) return 'null';",    "      if (typeof value === 'string') return value;",    "      try {",    "        return JSON.stringify(value);",    "      } catch {",    "        return String(value);",    "      }",    "    };",    "    const __IMAGE_DETAIL_META_KEY = 'codex/imageDetail';",    "    const __DEFAULT_IMAGE_DETAIL = 'high';",    "    const __validImageDetails = new Set(['auto', 'low', 'high', 'original']);",    "    const __normalizeImageDetail = (detail) => {",    "      if (detail === null || typeof detail === 'undefined') return undefined;",    "      if (typeof detail !== 'string') throw new TypeError('image detail must be a string when provided');",    "      const normalized = detail.toLowerCase();",    "      if (!__validImageDetails.has(normalized)) throw new TypeError('image detail must be one of: auto, low, high, original');",    "      return normalized;",    "    };",    "    const __isToolFile = (value) => value && typeof value === 'object' && value._tag === 'ToolFile' && typeof value.mimeType === 'string' && value.encoding === 'base64' && typeof value.data === 'string' && typeof value.byteLength === 'number';",    "    const __isMcpTextContentBlock = (value) => value && typeof value === 'object' && value.type === 'text' && typeof value.text === 'string';",    "    const __isMcpImageContentBlock = (value) => value && typeof value === 'object' && value.type === 'image' && typeof value.data === 'string' && (typeof value.mimeType === 'string' || typeof value.mime_type === 'string' || value.data.toLowerCase().startsWith('data:'));",    "    const __isMcpAudioContentBlock = (value) => value && typeof value === 'object' && value.type === 'audio' && typeof value.data === 'string' && typeof value.mimeType === 'string';",    "    const __isMcpResourceContentBlock = (value) => value && typeof value === 'object' && value.type === 'resource' && value.resource && typeof value.resource === 'object' && typeof value.resource.uri === 'string' && (typeof value.resource.text === 'string' || typeof value.resource.blob === 'string');",    "    const __isMcpResourceLinkContentBlock = (value) => value && typeof value === 'object' && value.type === 'resource_link' && typeof value.uri === 'string' && typeof value.name === 'string';",    "    const __isMcpContentBlock = (value) => __isMcpTextContentBlock(value) || __isMcpImageContentBlock(value) || __isMcpAudioContentBlock(value) || __isMcpResourceContentBlock(value) || __isMcpResourceLinkContentBlock(value);",    "    const __parseDataImageUrl = (imageUrl) => {",    "      if (typeof imageUrl !== 'string' || imageUrl.length === 0) throw new TypeError('image expects a non-empty data URI, an object with image_url and optional detail, or a raw MCP image block');",    "      const lower = imageUrl.toLowerCase();",    "      if (lower.startsWith('http://') || lower.startsWith('https://')) throw new TypeError('remote image URLs are not supported in code output; pass a base64 data URI or MCP image block');",    "      const match = /^data:([^;,]+);base64,(.*)$/i.exec(imageUrl);",    "      if (!match) throw new TypeError('image expects a base64 data URI or MCP image block');",    "      return { mimeType: match[1], data: match[2] };",    "    };",    "    const __imageDetailFromMeta = (value) => {",    "      const meta = value && typeof value === 'object' ? value._meta : undefined;",    "      const detail = meta && typeof meta === 'object' ? meta[__IMAGE_DETAIL_META_KEY] : undefined;",    "      return typeof detail === 'string' && __validImageDetails.has(detail) ? detail : undefined;",    "    };",    "    const __imageWithDetail = (block, detail) => ({ ...block, _meta: { ...(block._meta && typeof block._meta === 'object' ? block._meta : {}), [__IMAGE_DETAIL_META_KEY]: detail ?? __DEFAULT_IMAGE_DETAIL } });",    "    const __normalizeImageBlock = (value, detailOverride) => {",    "      const override = __normalizeImageDetail(detailOverride);",    "      if (typeof value === 'string') return __imageWithDetail({ type: 'image', ...__parseDataImageUrl(value) }, override);",    "      if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('image expects a non-empty data URI, an object with image_url and optional detail, or a raw MCP image block');",    "      if (typeof value.image_url === 'string') {",    "        return __imageWithDetail({ type: 'image', ...__parseDataImageUrl(value.image_url) }, override ?? __normalizeImageDetail(value.detail));",    "      }",    "      if (value.type === 'image' && typeof value.data === 'string') {",    "        const parsed = value.data.toLowerCase().startsWith('data:') ? __parseDataImageUrl(value.data) : { data: value.data, mimeType: typeof value.mimeType === 'string' ? value.mimeType : typeof value.mime_type === 'string' ? value.mime_type : 'application/octet-stream' };",    "        return __imageWithDetail({ ...value, type: 'image', data: parsed.data, mimeType: parsed.mimeType }, override ?? __imageDetailFromMeta(value));",    "      }",    "      throw new TypeError('image expects a non-empty data URI, an object with image_url and optional detail, or a raw MCP image block');",    "    };",    "    const text = (value) => { __recordOutput({ type: 'content', content: { type: 'text', text: __formatOutputText(value) } }); };",    "    const image = (value, detail) => { __recordOutput({ type: 'content', content: __normalizeImageBlock(value, detail) }); };",    "    const audio = (value) => { if (!__isMcpAudioContentBlock(value)) throw new TypeError('audio expects an MCP audio content block'); __recordOutput({ type: 'content', content: value }); };",    "    const file = (value) => { if (!__isToolFile(value)) throw new TypeError('file expects a ToolFile value'); __recordOutput({ type: 'file', file: value }); };",    "    const resource = (value) => { if (!__isMcpResourceContentBlock(value) && !__isMcpResourceLinkContentBlock(value)) throw new TypeError('resource expects an MCP resource or resource_link content block'); __recordOutput({ type: 'content', content: value }); };",    "    const notify = (value) => { const notification = value && typeof value === 'object' && typeof value.message === 'string' ? { message: value.message, ...(Object.prototype.hasOwnProperty.call(value, 'data') ? { data: value.data } : {}) } : { message: __formatOutputText(value) }; __recordOutput({ type: 'notification', notification }); };",    "    const emit = (value) => {",    "      if (__isToolFile(value)) { file(value); return; }",    "      if (__isMcpContentBlock(value)) { __recordOutput({ type: 'content', content: value }); return; }",    "      text(value);",    "    };",    "    const __serializeThrownError = (err) => {",    "      const primary = __serializeErrorValue(err);",    "      return {",    "        kind: 'unknown',",    "        message: __renderMessage(primary),",    "        primary,",    "        failures: [],",    "        defects: [],",    "        interrupted: false,",    "      };",    "    };",    "",    // Blob/File aren't on Workers RPC's structured-clone allow-list, so    // we encode them to a tagged ArrayBuffer envelope before the call and    // the host rehydrates them — and symmetrically the host encodes    // Blob/File results which we rehydrate here. ArrayBuffer / typed    // arrays cross natively.    "    const __isPlainObject = (v) => {",    "      const p = Object.getPrototypeOf(v);",    "      return p === Object.prototype || p === null;",    "    };",    "    const __isBinaryEnvelope = (v) =>",    "      v && typeof v === 'object' && v.__executorBinary === 1 &&",    "      v.buffer instanceof ArrayBuffer && typeof v.type === 'string';",    "    const __encodeBinary = async (value, seen = new WeakSet()) => {",    "      if (value === null || typeof value !== 'object') return value;",    "      if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return value;",    "      if (seen.has(value)) throw new Error('Tool RPC payload contains a circular reference');",    "      seen.add(value);",    "      if (typeof File !== 'undefined' && value instanceof File) {",    "        const out = {",    "          __executorBinary: 1,",    "          kind: 'file',",    "          type: value.type,",    "          name: value.name,",    "          lastModified: value.lastModified,",    "          buffer: await value.arrayBuffer(),",    "        };",    "        seen.delete(value);",    "        return out;",    "      }",    "      if (value instanceof Blob) {",    "        const out = { __executorBinary: 1, kind: 'blob', type: value.type, buffer: await value.arrayBuffer() };",    "        seen.delete(value);",    "        return out;",    "      }",    "      if (Array.isArray(value)) {",    "        const out = await Promise.all(value.map((item) => __encodeBinary(item, seen)));",    "        seen.delete(value);",    "        return out;",    "      }",    "      if (!__isPlainObject(value)) { seen.delete(value); return value; }",    "      const out = {};",    "      for (const [k, v] of Object.entries(value)) out[k] = await __encodeBinary(v, seen);",    "      seen.delete(value);",    "      return out;",    "    };",    "    const __decodeBinary = (value, seen = new WeakSet()) => {",    "      if (value === null || typeof value !== 'object') return value;",    "      if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return value;",    "      if (seen.has(value)) throw new Error('Tool RPC payload contains a circular reference');",    "      seen.add(value);",    "      if (__isBinaryEnvelope(value)) {",    "        seen.delete(value);",    "        if (value.kind === 'file' && typeof value.name === 'string') {",    "          return new File([value.buffer], value.name, {",    "            type: value.type,",    "            ...(typeof value.lastModified === 'number' ? { lastModified: value.lastModified } : {}),",    "          });",    "        }",    "        return new Blob([value.buffer], { type: value.type });",    "      }",    "      if (Array.isArray(value)) { const out = value.map((item) => __decodeBinary(item, seen)); seen.delete(value); return out; }",    "      if (!__isPlainObject(value)) { seen.delete(value); return value; }",    "      const out = {};",    "      for (const [k, v] of Object.entries(value)) out[k] = __decodeBinary(v, seen);",    "      seen.delete(value);",    "      return out;",    "    };",    "    const __publicToolErrorMessage = (error) => {",    "      const values = [error && error.primary, ...(Array.isArray(error && error.failures) ? error.failures : [])];",    "      for (const value of values) {",    "        if (value && value.__type === 'Error' && value.name === 'ExecutionToolError' && typeof value.message === 'string') return value.message;",    "      }",    "      if (error && typeof error.message === 'string' && error.message.startsWith('Internal tool error')) return error.message;",    "      return null;",    "    };",    "    const __builtinToolKeys = { '': ['search', 'describe', 'executor'], 'describe': ['tool'], 'executor': ['sources'], 'executor.sources': ['list'] };",    "    const __toolKeysForPath = (path) => __builtinToolKeys[path.join('.')] ?? [];",    "    const __makeToolsProxy = (path = []) => new Proxy(() => undefined, {",    "      get(_target, prop) {",    "        if (prop === 'then' || typeof prop === 'symbol') return undefined;",    "        return __makeToolsProxy([...path, String(prop)]);",    "      },",    "      ownKeys() {",    "        return __toolKeysForPath(path);",    "      },",    "      getOwnPropertyDescriptor(_target, prop) {",    "        return typeof prop === 'string' && __toolKeysForPath(path).includes(prop) ? { enumerable: true, configurable: true } : undefined;",    "      },",    "      apply(_target, _thisArg, args) {",    "        const toolPath = path.join('.');",    "        if (!toolPath) throw new Error('Tool path missing in invocation');",    "        return (async () => {",    "          const encoded = await __encodeBinary(args[0]);",    "          const data = await __dispatcher.call(toolPath, encoded);",    "          if (!data.ok) throw new Error(__publicToolErrorMessage(data.error) || 'Internal tool error');",    "          return __decodeBinary(data.result);",    "        })();",    "      },",    "    });",    "    const tools = __makeToolsProxy();",    "",    "    try {",    "      const result = await Promise.race([",    "        (async () => {",    body,    "        })(),",    "        new Promise((_, reject) =>",    `          setTimeout(() => reject(new Error("Execution timed out after ${timeoutMs}ms")), ${timeoutMs})`,    "        ),",    "      ]);",    "      await __pendingOutput;",    "      return { result, output: __outputs.length > 0 ? __outputs : undefined, logs: __logs };",    "    } catch (err) {",    "      await __pendingOutput;",    "      return { result: undefined, output: __outputs.length > 0 ? __outputs : undefined, error: __serializeThrownError(err), logs: __logs };",    "    }",    "  }",    "}",  ].join("\n");