| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- /**
- * OpenCode Tool Wrapper - 供 Python 调用的命令行接口
- *
- * 用法:
- * bun run tool-wrapper.ts <tool_name> <args_json>
- *
- * 示例:
- * bun run tool-wrapper.ts read '{"filePath": "config.py"}'
- *
- * 支持的工具:
- * - read: 读取文件
- * - edit: 编辑文件
- * - write: 写入文件
- * - bash: 执行命令
- * - glob: 文件匹配
- * - grep: 内容搜索
- * - webfetch: 抓取网页
- * - lsp: LSP 诊断
- */
- import { resolve } from 'path'
- // 动态导入工具(避免编译时依赖)
- async function loadTool(toolName: string) {
- // 从 agent/tools/adapters/ 定位到 vendor/opencode
- const toolPath = resolve(__dirname, '../../../../vendor/opencode/packages/opencode/src/tool')
- switch (toolName) {
- case 'read':
- return (await import(`${toolPath}/read.ts`)).ReadTool
- case 'edit':
- return (await import(`${toolPath}/edit.ts`)).EditTool
- case 'write':
- return (await import(`${toolPath}/write.ts`)).WriteTool
- case 'bash':
- return (await import(`${toolPath}/bash.ts`)).BashTool
- case 'glob':
- return (await import(`${toolPath}/glob.ts`)).GlobTool
- case 'grep':
- return (await import(`${toolPath}/grep.ts`)).GrepTool
- case 'webfetch':
- return (await import(`${toolPath}/webfetch.ts`)).WebFetchTool
- case 'lsp':
- return (await import(`${toolPath}/lsp.ts`)).LspTool
- default:
- throw new Error(`Unknown tool: ${toolName}`)
- }
- }
- async function main() {
- const toolName = process.argv[2]
- const argsJson = process.argv[3]
- if (!toolName || !argsJson) {
- console.error('Usage: bun run tool-wrapper.ts <tool_name> <args_json>')
- console.error('Example: bun run tool-wrapper.ts read \'{"filePath": "test.py"}\'')
- process.exit(1)
- }
- try {
- // 解析参数
- const args = JSON.parse(argsJson)
- // 加载工具
- const Tool = await loadTool(toolName)
- // 构造最小化的 context(Python 调用不需要完整 context)
- const context = {
- sessionID: 'python-adapter',
- messageID: 'python-adapter',
- agent: 'python',
- abort: new AbortController().signal,
- messages: [],
- metadata: () => {},
- ask: async () => {}, // 跳过权限检查(由 Python 层处理)
- }
- // 初始化工具
- const toolInfo = await Tool.init()
- // 执行工具
- const result = await toolInfo.execute(args, context)
- // 输出 JSON 结果
- const output = {
- title: result.title,
- output: result.output,
- metadata: result.metadata || {},
- attachments: result.attachments || []
- }
- console.log(JSON.stringify(output))
- } catch (error: any) {
- // 输出错误信息
- const errorOutput = {
- title: 'Error',
- output: `Tool execution failed: ${error.message}`,
- metadata: {
- error: error.message,
- stack: error.stack
- }
- }
- console.error(JSON.stringify(errorOutput))
- process.exit(1)
- }
- }
- main().catch((error) => {
- console.error(JSON.stringify({
- title: 'Fatal Error',
- output: `Fatal error: ${error.message}`,
- metadata: {
- error: error.message,
- stack: error.stack
- }
- }))
- process.exit(1)
- })
|