foliko 1.0.75 → 1.0.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +159 -157
- package/cli/bin/foliko.js +12 -12
- package/cli/src/commands/chat.js +143 -143
- package/cli/src/commands/list.js +93 -93
- package/cli/src/index.js +75 -75
- package/cli/src/ui/chat-ui.js +201 -201
- package/cli/src/utils/ansi.js +40 -40
- package/cli/src/utils/markdown.js +292 -292
- package/examples/ambient-example.js +194 -194
- package/examples/basic.js +115 -115
- package/examples/bootstrap.js +121 -121
- package/examples/mcp-example.js +56 -56
- package/examples/skill-example.js +49 -49
- package/examples/test-chat.js +137 -137
- package/examples/test-mcp.js +85 -85
- package/examples/test-reload.js +59 -59
- package/examples/test-telegram.js +50 -50
- package/examples/test-tg-bot.js +45 -45
- package/examples/test-tg-simple.js +47 -47
- package/examples/test-tg.js +62 -62
- package/examples/test-think.js +43 -43
- package/examples/test-web-plugin.js +103 -103
- package/examples/test-weixin-feishu.js +103 -103
- package/examples/workflow.js +158 -158
- package/package.json +1 -1
- package/plugins/ai-plugin.js +102 -102
- package/plugins/ambient-agent/EventWatcher.js +113 -113
- package/plugins/ambient-agent/ExplorerLoop.js +640 -640
- package/plugins/ambient-agent/GoalManager.js +197 -197
- package/plugins/ambient-agent/Reflector.js +95 -95
- package/plugins/ambient-agent/StateStore.js +90 -90
- package/plugins/ambient-agent/constants.js +101 -101
- package/plugins/ambient-agent/index.js +579 -579
- package/plugins/audit-plugin.js +187 -187
- package/plugins/default-plugins.js +662 -662
- package/plugins/email/constants.js +64 -64
- package/plugins/email/handlers.js +461 -461
- package/plugins/email/index.js +278 -278
- package/plugins/email/monitor.js +269 -269
- package/plugins/email/parser.js +138 -138
- package/plugins/email/reply.js +151 -151
- package/plugins/email/utils.js +124 -124
- package/plugins/feishu-plugin.js +481 -481
- package/plugins/file-system-plugin.js +826 -826
- package/plugins/install-plugin.js +199 -199
- package/plugins/python-executor-plugin.js +367 -367
- package/plugins/python-plugin-loader.js +481 -481
- package/plugins/rules-plugin.js +294 -294
- package/plugins/scheduler-plugin.js +691 -691
- package/plugins/session-plugin.js +369 -369
- package/plugins/shell-executor-plugin.js +197 -197
- package/plugins/storage-plugin.js +240 -240
- package/plugins/subagent-plugin.js +845 -845
- package/plugins/telegram-plugin.js +482 -482
- package/plugins/think-plugin.js +345 -345
- package/plugins/tools-plugin.js +196 -196
- package/plugins/web-plugin.js +606 -606
- package/plugins/weixin-plugin.js +545 -545
- package/src/capabilities/index.js +11 -11
- package/src/capabilities/skill-manager.js +609 -609
- package/src/capabilities/workflow-engine.js +1109 -1109
- package/src/core/agent-chat.js +882 -882
- package/src/core/agent.js +892 -892
- package/src/core/framework.js +465 -465
- package/src/core/index.js +19 -19
- package/src/core/plugin-base.js +219 -219
- package/src/core/plugin-manager.js +863 -863
- package/src/core/provider.js +114 -114
- package/src/core/sub-agent-config.js +264 -264
- package/src/core/system-prompt-builder.js +120 -120
- package/src/core/tool-registry.js +517 -517
- package/src/core/tool-router.js +297 -297
- package/src/executors/executor-base.js +58 -58
- package/src/executors/mcp-executor.js +741 -741
- package/src/index.js +25 -25
- package/src/utils/circuit-breaker.js +301 -301
- package/src/utils/error-boundary.js +363 -363
- package/src/utils/error.js +374 -374
- package/src/utils/event-emitter.js +97 -97
- package/src/utils/id.js +133 -133
- package/src/utils/index.js +217 -217
- package/src/utils/logger.js +181 -181
- package/src/utils/plugin-helpers.js +90 -90
- package/src/utils/retry.js +122 -122
- package/src/utils/sandbox.js +292 -292
- package/test/tool-registry-validation.test.js +218 -218
- package/website/script.js +136 -136
- package/foliko-1.0.75.tgz +0 -0
|
@@ -1,368 +1,368 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Python 执行器插件
|
|
3
|
-
* 使用 child_process 执行 Python 代码和脚本
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const { Plugin } = require('../src/core/plugin-base')
|
|
7
|
-
const { logger } = require('../src/utils/logger')
|
|
8
|
-
const log = logger.child('PythonExecutor')
|
|
9
|
-
const { spawn } = require('child_process')
|
|
10
|
-
const fs = require('fs')
|
|
11
|
-
const path = require('path')
|
|
12
|
-
const os = require('os')
|
|
13
|
-
const { z } = require('zod')
|
|
14
|
-
|
|
15
|
-
class PythonExecutorPlugin extends Plugin {
|
|
16
|
-
constructor(config = {}) {
|
|
17
|
-
super()
|
|
18
|
-
this.name = 'python-executor'
|
|
19
|
-
this.version = '1.0.0'
|
|
20
|
-
this.description = 'Python 执行器,用于运行 Python 代码和脚本,禁止传入无用的emoji'
|
|
21
|
-
this.priority = 15
|
|
22
|
-
|
|
23
|
-
this.config = {
|
|
24
|
-
pythonPath: config.pythonPath || 'python', // python 或 python3
|
|
25
|
-
timeout: config.timeout || 120000, // 2分钟超时
|
|
26
|
-
baseDir: config.baseDir || '.agent/python-scripts',
|
|
27
|
-
pipPath: config.pipPath || null // 可选,自定义 pip 路径
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
this.system = true
|
|
31
|
-
|
|
32
|
-
this._framework = null
|
|
33
|
-
this._tempDir = null
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
install(framework) {
|
|
37
|
-
this._framework = framework
|
|
38
|
-
|
|
39
|
-
// 确保临时目录存在
|
|
40
|
-
this._tempDir = path.join(os.tmpdir(), `vb-python-${process.pid}`)
|
|
41
|
-
if (!fs.existsSync(this._tempDir)) {
|
|
42
|
-
fs.mkdirSync(this._tempDir, { recursive: true })
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return this
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
start(framework) {
|
|
49
|
-
// 注册 python-execute 工具
|
|
50
|
-
framework.registerTool({
|
|
51
|
-
name: 'python-execute',
|
|
52
|
-
description: '执行 Python 代码,禁止传入无用的emoji',
|
|
53
|
-
inputSchema: z.object({
|
|
54
|
-
code: z.string().describe('Python 代码'),
|
|
55
|
-
timeout: z.number().optional().describe('超时时间(毫秒),默认 120000'),
|
|
56
|
-
input_data: z.record(z.any()).optional().describe('输入数据对象,可在代码中通过 input_data.get("key") 访问')
|
|
57
|
-
}),
|
|
58
|
-
execute: async (args) => {
|
|
59
|
-
return this._executePython(args.code, args.timeout || this.config.timeout, args.input_data)
|
|
60
|
-
}
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
// 注册 python_script 工具
|
|
64
|
-
framework.registerTool({
|
|
65
|
-
name: 'python_script',
|
|
66
|
-
description: '执行 Python 脚本文件',
|
|
67
|
-
inputSchema: z.object({
|
|
68
|
-
scriptPath: z.string().describe('Python 脚本文件路径'),
|
|
69
|
-
args: z.string().optional().describe('传递给脚本的命令行参数'),
|
|
70
|
-
timeout: z.number().optional().describe('超时时间(毫秒)')
|
|
71
|
-
}),
|
|
72
|
-
execute: async (args) => {
|
|
73
|
-
return this._executeScript(args.scriptPath, args.args, args.timeout || this.config.timeout)
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
// 注册 pip_install 工具
|
|
78
|
-
framework.registerTool({
|
|
79
|
-
name: 'pip_install',
|
|
80
|
-
description: '安装 Python 包',
|
|
81
|
-
inputSchema: z.object({
|
|
82
|
-
package: z.string().describe('包名 (例如: numpy, pandas>=1.0)'),
|
|
83
|
-
upgrade: z.boolean().optional().describe('是否升级已存在的包'),
|
|
84
|
-
timeout: z.number().optional().describe('超时时间(毫秒)')
|
|
85
|
-
}),
|
|
86
|
-
execute: async (args) => {
|
|
87
|
-
return this._pipInstall(args.package, args.upgrade, args.timeout || this.config.timeout)
|
|
88
|
-
}
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
return this
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 执行 Python 代码
|
|
96
|
-
* @private
|
|
97
|
-
*/
|
|
98
|
-
_executePython(code, timeout, inputData) {
|
|
99
|
-
return new Promise((resolve) => {
|
|
100
|
-
const startTime = Date.now()
|
|
101
|
-
let output = ''
|
|
102
|
-
let errorOutput = ''
|
|
103
|
-
|
|
104
|
-
// 创建临时脚本文件
|
|
105
|
-
const tempFile = path.join(this._tempDir, `temp_${Date.now()}.py`)
|
|
106
|
-
|
|
107
|
-
// 注入 input_data 变量
|
|
108
|
-
let inputDataSetup = ''
|
|
109
|
-
if (inputData) {
|
|
110
|
-
const inputDataJson = JSON.stringify(inputData).replace(/'/g, "\\'")
|
|
111
|
-
inputDataSetup = `import json\ninput_data = json.loads('${inputDataJson}')\n`
|
|
112
|
-
} else {
|
|
113
|
-
inputDataSetup = `input_data = {}\n`
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// 包装代码,添加错误处理
|
|
117
|
-
const wrappedCode = `
|
|
118
|
-
import sys
|
|
119
|
-
import traceback
|
|
120
|
-
${inputDataSetup}
|
|
121
|
-
try:
|
|
122
|
-
${code.split('\n').map(line => ' ' + line).join('\n')}
|
|
123
|
-
except SystemExit:
|
|
124
|
-
pass
|
|
125
|
-
except Exception:
|
|
126
|
-
traceback.print_exc()
|
|
127
|
-
sys.exit(1)
|
|
128
|
-
`
|
|
129
|
-
|
|
130
|
-
fs.writeFileSync(tempFile, wrappedCode, 'utf-8')
|
|
131
|
-
|
|
132
|
-
const proc = spawn(this.config.pythonPath, [tempFile], {
|
|
133
|
-
timeout,
|
|
134
|
-
env: { ...process.env }
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
proc.stdout.on('data', (data) => {
|
|
138
|
-
output += data.toString()
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
proc.stderr.on('data', (data) => {
|
|
142
|
-
errorOutput += data.toString()
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
proc.on('close', (code) => {
|
|
146
|
-
// 清理临时文件
|
|
147
|
-
try {
|
|
148
|
-
fs.unlinkSync(tempFile)
|
|
149
|
-
} catch (e) { }
|
|
150
|
-
|
|
151
|
-
const elapsed = Date.now() - startTime
|
|
152
|
-
|
|
153
|
-
// 限制返回数据大小,避免上下文超限(限制为 5000 字符)
|
|
154
|
-
const MAX_OUTPUT_LENGTH = 5000
|
|
155
|
-
let truncatedOutput = output
|
|
156
|
-
let truncated = false
|
|
157
|
-
if (output.length > MAX_OUTPUT_LENGTH) {
|
|
158
|
-
truncatedOutput = output.substring(0, MAX_OUTPUT_LENGTH) + `\n... [输出已截断,总长度 ${output.length} 字符]`
|
|
159
|
-
truncated = true
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
resolve({
|
|
163
|
-
success: code === 0,
|
|
164
|
-
exitCode: code,
|
|
165
|
-
stdout: truncatedOutput,
|
|
166
|
-
stderr: errorOutput.length > MAX_OUTPUT_LENGTH
|
|
167
|
-
? errorOutput.substring(0, MAX_OUTPUT_LENGTH) + `\n... [错误输出已截断]`
|
|
168
|
-
: errorOutput,
|
|
169
|
-
elapsed,
|
|
170
|
-
timedOut: code === null,
|
|
171
|
-
truncated
|
|
172
|
-
})
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
proc.on('error', (err) => {
|
|
176
|
-
// 清理临时文件
|
|
177
|
-
try {
|
|
178
|
-
fs.unlinkSync(tempFile)
|
|
179
|
-
} catch (e) { }
|
|
180
|
-
|
|
181
|
-
resolve({
|
|
182
|
-
success: false,
|
|
183
|
-
error: err.message
|
|
184
|
-
})
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
setTimeout(() => {
|
|
188
|
-
if (!proc.killed) {
|
|
189
|
-
proc.kill('SIGTERM')
|
|
190
|
-
try {
|
|
191
|
-
fs.unlinkSync(tempFile)
|
|
192
|
-
} catch (e) { }
|
|
193
|
-
|
|
194
|
-
// 限制返回数据大小
|
|
195
|
-
const MAX_OUTPUT_LENGTH = 5000
|
|
196
|
-
const truncatedOutput = output.length > MAX_OUTPUT_LENGTH
|
|
197
|
-
? output.substring(0, MAX_OUTPUT_LENGTH) + `\n... [输出已截断,总长度 ${output.length} 字符]`
|
|
198
|
-
: output
|
|
199
|
-
|
|
200
|
-
resolve({
|
|
201
|
-
success: false,
|
|
202
|
-
error: `Python execution timed out after ${timeout}ms`,
|
|
203
|
-
stdout: truncatedOutput,
|
|
204
|
-
stderr: errorOutput.length > MAX_OUTPUT_LENGTH
|
|
205
|
-
? errorOutput.substring(0, MAX_OUTPUT_LENGTH) + `\n... [错误输出已截断]`
|
|
206
|
-
: errorOutput,
|
|
207
|
-
timedOut: true
|
|
208
|
-
})
|
|
209
|
-
}
|
|
210
|
-
}, timeout)
|
|
211
|
-
})
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 执行 Python 脚本文件
|
|
216
|
-
* @private
|
|
217
|
-
*/
|
|
218
|
-
_executeScript(scriptPath, args, timeout) {
|
|
219
|
-
return new Promise((resolve) => {
|
|
220
|
-
const startTime = Date.now()
|
|
221
|
-
let output = ''
|
|
222
|
-
let errorOutput = ''
|
|
223
|
-
|
|
224
|
-
// 检查文件是否存在
|
|
225
|
-
if (!fs.existsSync(scriptPath)) {
|
|
226
|
-
resolve({
|
|
227
|
-
success: false,
|
|
228
|
-
error: `Script file not found: ${scriptPath}`
|
|
229
|
-
})
|
|
230
|
-
return
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const procArgs = [scriptPath]
|
|
234
|
-
if (args) {
|
|
235
|
-
procArgs.push(...args.split(' '))
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const proc = spawn(this.config.pythonPath, procArgs, {
|
|
239
|
-
timeout,
|
|
240
|
-
env: { ...process.env }
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
proc.stdout.on('data', (data) => {
|
|
244
|
-
output += data.toString()
|
|
245
|
-
})
|
|
246
|
-
|
|
247
|
-
proc.stderr.on('data', (data) => {
|
|
248
|
-
errorOutput += data.toString()
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
proc.on('close', (code) => {
|
|
252
|
-
const elapsed = Date.now() - startTime
|
|
253
|
-
resolve({
|
|
254
|
-
success: code === 0,
|
|
255
|
-
exitCode: code,
|
|
256
|
-
stdout: output,
|
|
257
|
-
stderr: errorOutput,
|
|
258
|
-
elapsed,
|
|
259
|
-
timedOut: code === null
|
|
260
|
-
})
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
proc.on('error', (err) => {
|
|
264
|
-
resolve({
|
|
265
|
-
success: false,
|
|
266
|
-
error: err.message
|
|
267
|
-
})
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
setTimeout(() => {
|
|
271
|
-
if (!proc.killed) {
|
|
272
|
-
proc.kill('SIGTERM')
|
|
273
|
-
resolve({
|
|
274
|
-
success: false,
|
|
275
|
-
error: `Script execution timed out after ${timeout}ms`,
|
|
276
|
-
stdout: output,
|
|
277
|
-
stderr: errorOutput,
|
|
278
|
-
timedOut: true
|
|
279
|
-
})
|
|
280
|
-
}
|
|
281
|
-
}, timeout)
|
|
282
|
-
})
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* 安装 Python 包
|
|
287
|
-
* @private
|
|
288
|
-
*/
|
|
289
|
-
_pipInstall(packageName, upgrade, timeout) {
|
|
290
|
-
return new Promise((resolve) => {
|
|
291
|
-
const startTime = Date.now()
|
|
292
|
-
let output = ''
|
|
293
|
-
let errorOutput = ''
|
|
294
|
-
|
|
295
|
-
const pipPath = this.config.pipPath || this.config.pythonPath.replace('python', 'pip')
|
|
296
|
-
const args = [packageName]
|
|
297
|
-
if (upgrade) args.unshift('-U')
|
|
298
|
-
if (upgrade === undefined && packageName.includes('>=')) args.unshift('-U')
|
|
299
|
-
|
|
300
|
-
const proc = spawn(pipPath, ['install', ...args], {
|
|
301
|
-
timeout,
|
|
302
|
-
env: { ...process.env }
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
proc.stdout.on('data', (data) => {
|
|
306
|
-
output += data.toString()
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
proc.stderr.on('data', (data) => {
|
|
310
|
-
errorOutput += data.toString()
|
|
311
|
-
})
|
|
312
|
-
|
|
313
|
-
proc.on('close', (code) => {
|
|
314
|
-
const elapsed = Date.now() - startTime
|
|
315
|
-
resolve({
|
|
316
|
-
success: code === 0,
|
|
317
|
-
exitCode: code,
|
|
318
|
-
stdout: output,
|
|
319
|
-
stderr: errorOutput,
|
|
320
|
-
elapsed,
|
|
321
|
-
package: packageName,
|
|
322
|
-
upgraded: upgrade || packageName.includes('>=')
|
|
323
|
-
})
|
|
324
|
-
})
|
|
325
|
-
|
|
326
|
-
proc.on('error', (err) => {
|
|
327
|
-
resolve({
|
|
328
|
-
success: false,
|
|
329
|
-
error: err.message,
|
|
330
|
-
package: packageName
|
|
331
|
-
})
|
|
332
|
-
})
|
|
333
|
-
|
|
334
|
-
setTimeout(() => {
|
|
335
|
-
if (!proc.killed) {
|
|
336
|
-
proc.kill('SIGTERM')
|
|
337
|
-
resolve({
|
|
338
|
-
success: false,
|
|
339
|
-
error: `pip install timed out after ${timeout}ms`,
|
|
340
|
-
stdout: output,
|
|
341
|
-
stderr: errorOutput,
|
|
342
|
-
timedOut: true,
|
|
343
|
-
package: packageName
|
|
344
|
-
})
|
|
345
|
-
}
|
|
346
|
-
}, timeout)
|
|
347
|
-
})
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
reload(framework) {
|
|
351
|
-
this._framework = framework
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
uninstall(framework) {
|
|
355
|
-
// 清理临时目录
|
|
356
|
-
if (this._tempDir && fs.existsSync(this._tempDir)) {
|
|
357
|
-
try {
|
|
358
|
-
fs.rmSync(this._tempDir, { recursive: true, force: true })
|
|
359
|
-
} catch (e) {
|
|
360
|
-
log.warn(` Failed to cleanup temp dir: ${e.message}`)
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
this._framework = null
|
|
364
|
-
this._tempDir = null
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Python 执行器插件
|
|
3
|
+
* 使用 child_process 执行 Python 代码和脚本
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { Plugin } = require('../src/core/plugin-base')
|
|
7
|
+
const { logger } = require('../src/utils/logger')
|
|
8
|
+
const log = logger.child('PythonExecutor')
|
|
9
|
+
const { spawn } = require('child_process')
|
|
10
|
+
const fs = require('fs')
|
|
11
|
+
const path = require('path')
|
|
12
|
+
const os = require('os')
|
|
13
|
+
const { z } = require('zod')
|
|
14
|
+
|
|
15
|
+
class PythonExecutorPlugin extends Plugin {
|
|
16
|
+
constructor(config = {}) {
|
|
17
|
+
super()
|
|
18
|
+
this.name = 'python-executor'
|
|
19
|
+
this.version = '1.0.0'
|
|
20
|
+
this.description = 'Python 执行器,用于运行 Python 代码和脚本,禁止传入无用的emoji'
|
|
21
|
+
this.priority = 15
|
|
22
|
+
|
|
23
|
+
this.config = {
|
|
24
|
+
pythonPath: config.pythonPath || 'python', // python 或 python3
|
|
25
|
+
timeout: config.timeout || 120000, // 2分钟超时
|
|
26
|
+
baseDir: config.baseDir || '.agent/python-scripts',
|
|
27
|
+
pipPath: config.pipPath || null // 可选,自定义 pip 路径
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.system = true
|
|
31
|
+
|
|
32
|
+
this._framework = null
|
|
33
|
+
this._tempDir = null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
install(framework) {
|
|
37
|
+
this._framework = framework
|
|
38
|
+
|
|
39
|
+
// 确保临时目录存在
|
|
40
|
+
this._tempDir = path.join(os.tmpdir(), `vb-python-${process.pid}`)
|
|
41
|
+
if (!fs.existsSync(this._tempDir)) {
|
|
42
|
+
fs.mkdirSync(this._tempDir, { recursive: true })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return this
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
start(framework) {
|
|
49
|
+
// 注册 python-execute 工具
|
|
50
|
+
framework.registerTool({
|
|
51
|
+
name: 'python-execute',
|
|
52
|
+
description: '执行 Python 代码,禁止传入无用的emoji',
|
|
53
|
+
inputSchema: z.object({
|
|
54
|
+
code: z.string().describe('Python 代码'),
|
|
55
|
+
timeout: z.number().optional().describe('超时时间(毫秒),默认 120000'),
|
|
56
|
+
input_data: z.record(z.any()).optional().describe('输入数据对象,可在代码中通过 input_data.get("key") 访问')
|
|
57
|
+
}),
|
|
58
|
+
execute: async (args) => {
|
|
59
|
+
return this._executePython(args.code, args.timeout || this.config.timeout, args.input_data)
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// 注册 python_script 工具
|
|
64
|
+
framework.registerTool({
|
|
65
|
+
name: 'python_script',
|
|
66
|
+
description: '执行 Python 脚本文件',
|
|
67
|
+
inputSchema: z.object({
|
|
68
|
+
scriptPath: z.string().describe('Python 脚本文件路径'),
|
|
69
|
+
args: z.string().optional().describe('传递给脚本的命令行参数'),
|
|
70
|
+
timeout: z.number().optional().describe('超时时间(毫秒)')
|
|
71
|
+
}),
|
|
72
|
+
execute: async (args) => {
|
|
73
|
+
return this._executeScript(args.scriptPath, args.args, args.timeout || this.config.timeout)
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// 注册 pip_install 工具
|
|
78
|
+
framework.registerTool({
|
|
79
|
+
name: 'pip_install',
|
|
80
|
+
description: '安装 Python 包',
|
|
81
|
+
inputSchema: z.object({
|
|
82
|
+
package: z.string().describe('包名 (例如: numpy, pandas>=1.0)'),
|
|
83
|
+
upgrade: z.boolean().optional().describe('是否升级已存在的包'),
|
|
84
|
+
timeout: z.number().optional().describe('超时时间(毫秒)')
|
|
85
|
+
}),
|
|
86
|
+
execute: async (args) => {
|
|
87
|
+
return this._pipInstall(args.package, args.upgrade, args.timeout || this.config.timeout)
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
return this
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 执行 Python 代码
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
_executePython(code, timeout, inputData) {
|
|
99
|
+
return new Promise((resolve) => {
|
|
100
|
+
const startTime = Date.now()
|
|
101
|
+
let output = ''
|
|
102
|
+
let errorOutput = ''
|
|
103
|
+
|
|
104
|
+
// 创建临时脚本文件
|
|
105
|
+
const tempFile = path.join(this._tempDir, `temp_${Date.now()}.py`)
|
|
106
|
+
|
|
107
|
+
// 注入 input_data 变量
|
|
108
|
+
let inputDataSetup = ''
|
|
109
|
+
if (inputData) {
|
|
110
|
+
const inputDataJson = JSON.stringify(inputData).replace(/'/g, "\\'")
|
|
111
|
+
inputDataSetup = `import json\ninput_data = json.loads('${inputDataJson}')\n`
|
|
112
|
+
} else {
|
|
113
|
+
inputDataSetup = `input_data = {}\n`
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 包装代码,添加错误处理
|
|
117
|
+
const wrappedCode = `
|
|
118
|
+
import sys
|
|
119
|
+
import traceback
|
|
120
|
+
${inputDataSetup}
|
|
121
|
+
try:
|
|
122
|
+
${code.split('\n').map(line => ' ' + line).join('\n')}
|
|
123
|
+
except SystemExit:
|
|
124
|
+
pass
|
|
125
|
+
except Exception:
|
|
126
|
+
traceback.print_exc()
|
|
127
|
+
sys.exit(1)
|
|
128
|
+
`
|
|
129
|
+
|
|
130
|
+
fs.writeFileSync(tempFile, wrappedCode, 'utf-8')
|
|
131
|
+
|
|
132
|
+
const proc = spawn(this.config.pythonPath, [tempFile], {
|
|
133
|
+
timeout,
|
|
134
|
+
env: { ...process.env }
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
proc.stdout.on('data', (data) => {
|
|
138
|
+
output += data.toString()
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
proc.stderr.on('data', (data) => {
|
|
142
|
+
errorOutput += data.toString()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
proc.on('close', (code) => {
|
|
146
|
+
// 清理临时文件
|
|
147
|
+
try {
|
|
148
|
+
fs.unlinkSync(tempFile)
|
|
149
|
+
} catch (e) { }
|
|
150
|
+
|
|
151
|
+
const elapsed = Date.now() - startTime
|
|
152
|
+
|
|
153
|
+
// 限制返回数据大小,避免上下文超限(限制为 5000 字符)
|
|
154
|
+
const MAX_OUTPUT_LENGTH = 5000
|
|
155
|
+
let truncatedOutput = output
|
|
156
|
+
let truncated = false
|
|
157
|
+
if (output.length > MAX_OUTPUT_LENGTH) {
|
|
158
|
+
truncatedOutput = output.substring(0, MAX_OUTPUT_LENGTH) + `\n... [输出已截断,总长度 ${output.length} 字符]`
|
|
159
|
+
truncated = true
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
resolve({
|
|
163
|
+
success: code === 0,
|
|
164
|
+
exitCode: code,
|
|
165
|
+
stdout: truncatedOutput,
|
|
166
|
+
stderr: errorOutput.length > MAX_OUTPUT_LENGTH
|
|
167
|
+
? errorOutput.substring(0, MAX_OUTPUT_LENGTH) + `\n... [错误输出已截断]`
|
|
168
|
+
: errorOutput,
|
|
169
|
+
elapsed,
|
|
170
|
+
timedOut: code === null,
|
|
171
|
+
truncated
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
proc.on('error', (err) => {
|
|
176
|
+
// 清理临时文件
|
|
177
|
+
try {
|
|
178
|
+
fs.unlinkSync(tempFile)
|
|
179
|
+
} catch (e) { }
|
|
180
|
+
|
|
181
|
+
resolve({
|
|
182
|
+
success: false,
|
|
183
|
+
error: err.message
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
if (!proc.killed) {
|
|
189
|
+
proc.kill('SIGTERM')
|
|
190
|
+
try {
|
|
191
|
+
fs.unlinkSync(tempFile)
|
|
192
|
+
} catch (e) { }
|
|
193
|
+
|
|
194
|
+
// 限制返回数据大小
|
|
195
|
+
const MAX_OUTPUT_LENGTH = 5000
|
|
196
|
+
const truncatedOutput = output.length > MAX_OUTPUT_LENGTH
|
|
197
|
+
? output.substring(0, MAX_OUTPUT_LENGTH) + `\n... [输出已截断,总长度 ${output.length} 字符]`
|
|
198
|
+
: output
|
|
199
|
+
|
|
200
|
+
resolve({
|
|
201
|
+
success: false,
|
|
202
|
+
error: `Python execution timed out after ${timeout}ms`,
|
|
203
|
+
stdout: truncatedOutput,
|
|
204
|
+
stderr: errorOutput.length > MAX_OUTPUT_LENGTH
|
|
205
|
+
? errorOutput.substring(0, MAX_OUTPUT_LENGTH) + `\n... [错误输出已截断]`
|
|
206
|
+
: errorOutput,
|
|
207
|
+
timedOut: true
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
}, timeout)
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 执行 Python 脚本文件
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
_executeScript(scriptPath, args, timeout) {
|
|
219
|
+
return new Promise((resolve) => {
|
|
220
|
+
const startTime = Date.now()
|
|
221
|
+
let output = ''
|
|
222
|
+
let errorOutput = ''
|
|
223
|
+
|
|
224
|
+
// 检查文件是否存在
|
|
225
|
+
if (!fs.existsSync(scriptPath)) {
|
|
226
|
+
resolve({
|
|
227
|
+
success: false,
|
|
228
|
+
error: `Script file not found: ${scriptPath}`
|
|
229
|
+
})
|
|
230
|
+
return
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const procArgs = [scriptPath]
|
|
234
|
+
if (args) {
|
|
235
|
+
procArgs.push(...args.split(' '))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const proc = spawn(this.config.pythonPath, procArgs, {
|
|
239
|
+
timeout,
|
|
240
|
+
env: { ...process.env }
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
proc.stdout.on('data', (data) => {
|
|
244
|
+
output += data.toString()
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
proc.stderr.on('data', (data) => {
|
|
248
|
+
errorOutput += data.toString()
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
proc.on('close', (code) => {
|
|
252
|
+
const elapsed = Date.now() - startTime
|
|
253
|
+
resolve({
|
|
254
|
+
success: code === 0,
|
|
255
|
+
exitCode: code,
|
|
256
|
+
stdout: output,
|
|
257
|
+
stderr: errorOutput,
|
|
258
|
+
elapsed,
|
|
259
|
+
timedOut: code === null
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
proc.on('error', (err) => {
|
|
264
|
+
resolve({
|
|
265
|
+
success: false,
|
|
266
|
+
error: err.message
|
|
267
|
+
})
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
setTimeout(() => {
|
|
271
|
+
if (!proc.killed) {
|
|
272
|
+
proc.kill('SIGTERM')
|
|
273
|
+
resolve({
|
|
274
|
+
success: false,
|
|
275
|
+
error: `Script execution timed out after ${timeout}ms`,
|
|
276
|
+
stdout: output,
|
|
277
|
+
stderr: errorOutput,
|
|
278
|
+
timedOut: true
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
}, timeout)
|
|
282
|
+
})
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 安装 Python 包
|
|
287
|
+
* @private
|
|
288
|
+
*/
|
|
289
|
+
_pipInstall(packageName, upgrade, timeout) {
|
|
290
|
+
return new Promise((resolve) => {
|
|
291
|
+
const startTime = Date.now()
|
|
292
|
+
let output = ''
|
|
293
|
+
let errorOutput = ''
|
|
294
|
+
|
|
295
|
+
const pipPath = this.config.pipPath || this.config.pythonPath.replace('python', 'pip')
|
|
296
|
+
const args = [packageName]
|
|
297
|
+
if (upgrade) args.unshift('-U')
|
|
298
|
+
if (upgrade === undefined && packageName.includes('>=')) args.unshift('-U')
|
|
299
|
+
|
|
300
|
+
const proc = spawn(pipPath, ['install', ...args], {
|
|
301
|
+
timeout,
|
|
302
|
+
env: { ...process.env }
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
proc.stdout.on('data', (data) => {
|
|
306
|
+
output += data.toString()
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
proc.stderr.on('data', (data) => {
|
|
310
|
+
errorOutput += data.toString()
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
proc.on('close', (code) => {
|
|
314
|
+
const elapsed = Date.now() - startTime
|
|
315
|
+
resolve({
|
|
316
|
+
success: code === 0,
|
|
317
|
+
exitCode: code,
|
|
318
|
+
stdout: output,
|
|
319
|
+
stderr: errorOutput,
|
|
320
|
+
elapsed,
|
|
321
|
+
package: packageName,
|
|
322
|
+
upgraded: upgrade || packageName.includes('>=')
|
|
323
|
+
})
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
proc.on('error', (err) => {
|
|
327
|
+
resolve({
|
|
328
|
+
success: false,
|
|
329
|
+
error: err.message,
|
|
330
|
+
package: packageName
|
|
331
|
+
})
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
setTimeout(() => {
|
|
335
|
+
if (!proc.killed) {
|
|
336
|
+
proc.kill('SIGTERM')
|
|
337
|
+
resolve({
|
|
338
|
+
success: false,
|
|
339
|
+
error: `pip install timed out after ${timeout}ms`,
|
|
340
|
+
stdout: output,
|
|
341
|
+
stderr: errorOutput,
|
|
342
|
+
timedOut: true,
|
|
343
|
+
package: packageName
|
|
344
|
+
})
|
|
345
|
+
}
|
|
346
|
+
}, timeout)
|
|
347
|
+
})
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
reload(framework) {
|
|
351
|
+
this._framework = framework
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
uninstall(framework) {
|
|
355
|
+
// 清理临时目录
|
|
356
|
+
if (this._tempDir && fs.existsSync(this._tempDir)) {
|
|
357
|
+
try {
|
|
358
|
+
fs.rmSync(this._tempDir, { recursive: true, force: true })
|
|
359
|
+
} catch (e) {
|
|
360
|
+
log.warn(` Failed to cleanup temp dir: ${e.message}`)
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
this._framework = null
|
|
364
|
+
this._tempDir = null
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
368
|
module.exports = { PythonExecutorPlugin }
|