feat: parse CodeMode execute tool calls from JS script

Парсим JS код из аргумента execute инструмента и извлекаем
вызовы tools.<server>.<tool>(...) — как это делает CodeMode.
Дочерние инструменты показываются в спойлере вместе с обычными.
This commit is contained in:
Бородин Роман 2026-09-26 11:46:44 +03:00
parent 53e77e0608
commit 729dee2c11
1 changed files with 34 additions and 0 deletions

View File

@ -131,6 +131,26 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
const sessionsBySessionID = new Map<string, SessionState>()
// =============================================================================
// Parse tool calls from CodeMode JS script
// CodeMode creates nested tools: tools.<server>.<tool>(...)
// e.g. tools.duckduckgo.search({ query: "..." })
// =============================================================================
function parseCodeModeTools(code: string): string[] {
const tools: string[] = []
// Match tools.<word>.<word>( — 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
}