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:
parent
729dee2c11
commit
6dc6cf41c4
44
index.ts
44
index.ts
|
|
@ -367,6 +367,27 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
|||
// Tool calls
|
||||
if (step.tools.length > 0) {
|
||||
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(`<b>Args:</b>\n\`\`\`\n${tool.args}\n\`\`\`\n\n`)
|
||||
if (tool.failed) {
|
||||
|
|
@ -377,6 +398,7 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
|||
parts.push(`</details>`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Separator before text
|
||||
if (step.text) {
|
||||
|
|
@ -387,6 +409,28 @@ async function runPlugin(ctx: any, options: any, dataDir: string) {
|
|||
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)
|
||||
// =============================================================================
|
||||
|
|
|
|||
Loading…
Reference in New Issue