mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-12 00:49:14 +08:00
🐛 fix: inline hub worker source
This commit is contained in:
parent
41699a1afd
commit
2b2f18dd17
@ -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) {
|
||||
|
||||
@ -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
|
||||
}
|
||||
})
|
||||
`
|
||||
Loading…
Reference in New Issue
Block a user