diff --git a/src/main/mcpServers/hub/runtime.ts b/src/main/mcpServers/hub/runtime.ts index 8a3495560b..c266157a09 100644 --- a/src/main/mcpServers/hub/runtime.ts +++ b/src/main/mcpServers/hub/runtime.ts @@ -12,12 +12,12 @@ import type { HubWorkerMessage, HubWorkerResultMessage } from './types' +import { hubWorkerSource } from './worker' const logger = loggerService.withContext('MCPServer:Hub:Runtime') const MAX_LOGS = 1000 const EXECUTION_TIMEOUT = 60000 -const WORKER_URL = new URL('./worker.js', import.meta.url) export class Runtime { async execute(code: string, tools: GeneratedTool[]): Promise { @@ -28,7 +28,7 @@ export class Runtime { let timedOut = false let timeoutId: NodeJS.Timeout | null = null - const worker = new Worker(WORKER_URL) + const worker = new Worker(hubWorkerSource, { eval: true }) const addLog = (entry: string) => { if (logs.length >= MAX_LOGS) { diff --git a/src/main/mcpServers/hub/worker.js b/src/main/mcpServers/hub/worker.ts similarity index 96% rename from src/main/mcpServers/hub/worker.js rename to src/main/mcpServers/hub/worker.ts index a8706f0704..88dcbc6858 100644 --- a/src/main/mcpServers/hub/worker.js +++ b/src/main/mcpServers/hub/worker.ts @@ -1,3 +1,4 @@ +export const hubWorkerSource = ` const crypto = require('node:crypto') const { parentPort } = require('node:worker_threads') @@ -26,7 +27,7 @@ const pushLog = (level, args) => { return } const message = args.map((arg) => stringify(arg)).join(' ') - const entry = `[${level}] ${message}` + const entry = \`[\${level}] \${message}\` logs.push(entry) parentPort?.postMessage({ type: 'log', entry }) } @@ -65,11 +66,11 @@ const runCode = async (code, context) => { const contextKeys = Object.keys(context) const contextValues = contextKeys.map((key) => context[key]) - const wrappedCode = ` + const wrappedCode = \` return (async () => { - ${code} + \${code} })() - ` + \` const fn = new Function(...contextKeys, wrappedCode) return await fn(...contextValues) @@ -129,3 +130,4 @@ parentPort?.on('message', (message) => { break } }) +`