🐛 fix: inline hub worker source

This commit is contained in:
Vaayne 2025-12-30 12:02:28 +08:00
parent 41699a1afd
commit 2b2f18dd17
2 changed files with 8 additions and 6 deletions

View File

@ -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<ExecOutput> {
@ -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) {

View File

@ -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
}
})
`