feat: expand execute tool into child tool spoilers when script has tool calls

- Если execute скрипт содержит tools.<server>.<tool>(...) — показываем
  отдельные спойлеры для каждого найденного инструмента
- Если нет вызовов инструментов — показываем execute как обычно
- Args для дочерних инструментов извлекаются из кода regex
This commit is contained in:
Бородин Роман 2026-09-26 11:53:51 +03:00
parent 729dee2c11
commit 6dc6cf41c4
1 changed files with 50 additions and 6 deletions

View File

@ -367,6 +367,27 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
// Tool calls // Tool calls
if (step.tools.length > 0) { if (step.tools.length > 0) {
for (const tool of step.tools) { for (const tool of step.tools) {
// Check if this is an execute tool with child tool calls
// Child tools have toolID like "parentID/toolName"
const isExecuteWithChildren = tool.name === "execute" && tool.toolID.includes("/")
const childTools = isExecuteWithChildren ? step.tools.filter(t => t.toolID.startsWith(tool.toolID + "/")) : []
if (isExecuteWithChildren && childTools.length > 0) {
// Expand execute → individual spoilers for each child tool
for (const child of childTools) {
// Extract args from the code for this child tool
const childArgs = extractChildToolArgs(tool.args, child.name)
parts.push(`<details>\n<summary>🔧 ${child.name}</summary>\n\n`)
parts.push(`<b>Args:</b>\n\`\`\`\n${childArgs}\n\`\`\`\n\n`)
if (child.failed) {
parts.push(`<b>Failed:</b> ${child.result}\n\n`)
} else if (child.result) {
parts.push(`<b>Result:</b>\n\`\`\`\n${child.result}\n\`\`\`\n\n`)
}
parts.push(`</details>`)
}
} else {
// Normal tool (or execute without children)
parts.push(`<details>\n<summary>🔧 ${tool.name}</summary>\n\n`) parts.push(`<details>\n<summary>🔧 ${tool.name}</summary>\n\n`)
parts.push(`<b>Args:</b>\n\`\`\`\n${tool.args}\n\`\`\`\n\n`) parts.push(`<b>Args:</b>\n\`\`\`\n${tool.args}\n\`\`\`\n\n`)
if (tool.failed) { if (tool.failed) {
@ -377,6 +398,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
parts.push(`</details>`) parts.push(`</details>`)
} }
} }
}
// Separator before text // Separator before text
if (step.text) { if (step.text) {
@ -387,6 +409,28 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
return parts.join("\n") return parts.join("\n")
} }
// Extract args for a child tool from the execute code
function extractChildToolArgs(code: string, toolName: string): string {
try {
const args = JSON.parse(code)
const codeStr = args?.code || ""
// Match tools.<server>.<toolName>(...) and extract the arguments
const regex = new RegExp(`tools\\.\\w+\\.${escapeRegex(toolName.replace("_", "."))}\\s*\\(([^)]*)\\)`, "g")
const matches: string[] = []
let match
while ((match = regex.exec(codeStr)) !== null) {
matches.push(match[1].trim())
}
return matches.join("\n") || "(parsed from code)"
} catch {
return "(parsed from code)"
}
}
function escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
}
// ============================================================================= // =============================================================================
// Send message to Matrix (reuses existing sendReply/sendNotice) // Send message to Matrix (reuses existing sendReply/sendNotice)
// ============================================================================= // =============================================================================