diff --git a/index.ts b/index.ts index 9d11179..66c8143 100644 --- a/index.ts +++ b/index.ts @@ -131,6 +131,26 @@ async function runPlugin(ctx: any, options: any, dataDir: string) { const sessionsBySessionID = new Map() + // ============================================================================= + // Parse tool calls from CodeMode JS script + // CodeMode creates nested tools: tools..(...) + // e.g. tools.duckduckgo.search({ query: "..." }) + // ============================================================================= + + function parseCodeModeTools(code: string): string[] { + const tools: string[] = [] + // Match tools..( — nested tool calls in CodeMode + const regex = /tools\.(\w+)\.(\w+)\s*\(/g + let match + while ((match = regex.exec(code)) !== null) { + const toolName = `${match[1]}_${match[2]}` + if (!tools.includes(toolName)) { + tools.push(toolName) + } + } + return tools + } + // ============================================================================= // Event subscription — collects step data and sends messages per step // ============================================================================= @@ -208,6 +228,20 @@ async function runPlugin(ctx: any, options: any, dataDir: string) { const tool = step.tools.find(t => t.toolID === toolID) if (tool) tool.args = args } + // Parse CodeMode execute tool — extract child tool calls from JS code + if (toolID && step && args) { + let parsedArgs: any + try { parsedArgs = JSON.parse(args) } catch { continue } + const code = parsedArgs?.code || "" + if (code) { + const childTools = parseCodeModeTools(code) + for (const name of childTools) { + if (!step.tools.find(t => t.name === name)) { + step.tools.push({ toolID: `${toolID}/${name}`, name, args: "", result: "", failed: false }) + } + } + } + } continue }