@@ -31,6 +31,7 @@ export const BatchTool = Tool.define("batch", async () => { |
31 | 31 | return `Invalid parameters for tool 'batch':\n${formattedErrors}\n\nExpected payload format:\n [{"tool": "tool_name", "parameters": {...}}, {...}]` |
32 | 32 | }, |
33 | 33 | async execute(params, ctx) { |
| 34 | + const { Session } = await import("../session") |
34 | 35 | const { Identifier } = await import("../id/id") |
35 | 36 |
|
36 | 37 | const toolCalls = params.tool_calls |
@@ -39,26 +40,36 @@ export const BatchTool = Tool.define("batch", async () => { |
39 | 40 | const availableTools = await ToolRegistry.tools("", "") |
40 | 41 | const toolMap = new Map(availableTools.map((t) => [t.id, t])) |
41 | 42 |
|
| 43 | + const partIDs = new Map<(typeof toolCalls)[0], string>() |
42 | 44 | for (const call of toolCalls) { |
43 | | - if (DISALLOWED.has(call.tool)) { |
44 | | - throw new Error( |
45 | | - `tool '${call.tool}' is not allowed in batch. Disallowed tools: ${Array.from(DISALLOWED).join(", ")}`, |
46 | | - ) |
47 | | - } |
48 | | - if (!toolMap.has(call.tool)) { |
49 | | - const allowed = Array.from(toolMap.keys()).filter((name) => !FILTERED_FROM_SUGGESTIONS.has(name)) |
50 | | - throw new Error(`tool '${call.tool}' is not available. Available tools: ${allowed.join(", ")}`) |
51 | | - } |
| 45 | + const partID = Identifier.ascending("part") |
| 46 | + partIDs.set(call, partID) |
| 47 | + Session.updatePart({ |
| 48 | + id: partID, |
| 49 | + messageID: ctx.messageID, |
| 50 | + sessionID: ctx.sessionID, |
| 51 | + type: "tool", |
| 52 | + tool: call.tool, |
| 53 | + callID: partID, |
| 54 | + state: { |
| 55 | + status: "pending", |
| 56 | + input: call.parameters, |
| 57 | + raw: JSON.stringify(call), |
| 58 | + }, |
| 59 | + }) |
52 | 60 | } |
53 | 61 |
|
54 | 62 | const executeCall = async (call: (typeof toolCalls)[0]) => { |
55 | | - if (ctx.abort.aborted) { |
56 | | - return { success: false as const, tool: call.tool, error: new Error("Aborted") } |
57 | | - } |
58 | | - |
59 | | - const partID = Identifier.ascending("part") |
| 63 | + const callStartTime = Date.now() |
| 64 | + const partID = partIDs.get(call)! |
60 | 65 |
|
61 | 66 | try { |
| 67 | + if (DISALLOWED.has(call.tool)) { |
| 68 | + throw new Error( |
| 69 | + `Tool '${call.tool}' is not allowed in batch. Disallowed tools: ${Array.from(DISALLOWED).join(", ")}`, |
| 70 | + ) |
| 71 | + } |
| 72 | + |
62 | 73 | const tool = toolMap.get(call.tool) |
63 | 74 | if (!tool) { |
64 | 75 | const availableToolsList = Array.from(toolMap.keys()).filter((name) => !FILTERED_FROM_SUGGESTIONS.has(name)) |
@@ -68,13 +79,53 @@ export const BatchTool = Tool.define("batch", async () => { |
68 | 79 |
|
69 | 80 | const result = await tool.execute(validatedParams, { ...ctx, callID: partID }) |
70 | 81 |
|
| 82 | + await Session.updatePart({ |
| 83 | + id: partID, |
| 84 | + messageID: ctx.messageID, |
| 85 | + sessionID: ctx.sessionID, |
| 86 | + type: "tool", |
| 87 | + tool: call.tool, |
| 88 | + callID: partID, |
| 89 | + state: { |
| 90 | + status: "completed", |
| 91 | + input: call.parameters, |
| 92 | + output: result.output, |
| 93 | + title: result.title, |
| 94 | + metadata: result.metadata, |
| 95 | + attachments: result.attachments, |
| 96 | + time: { |
| 97 | + start: callStartTime, |
| 98 | + end: Date.now(), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }) |
| 102 | + |
71 | 103 | return { success: true as const, tool: call.tool, result } |
72 | 104 | } catch (error) { |
| 105 | + await Session.updatePart({ |
| 106 | + id: partID, |
| 107 | + messageID: ctx.messageID, |
| 108 | + sessionID: ctx.sessionID, |
| 109 | + type: "tool", |
| 110 | + tool: call.tool, |
| 111 | + callID: partID, |
| 112 | + state: { |
| 113 | + status: "error", |
| 114 | + input: call.parameters, |
| 115 | + error: error instanceof Error ? error.message : String(error), |
| 116 | + time: { |
| 117 | + start: callStartTime, |
| 118 | + end: Date.now(), |
| 119 | + }, |
| 120 | + }, |
| 121 | + }) |
| 122 | + |
73 | 123 | return { success: false as const, tool: call.tool, error } |
74 | 124 | } |
75 | 125 | } |
76 | 126 |
|
77 | | - const results = await Promise.all(toolCalls.flatMap((call) => executeCall(call))) |
| 127 | + const results = await Promise.all(toolCalls.map((call) => executeCall(call))) |
| 128 | + |
78 | 129 | const successfulCalls = results.filter((r) => r.success).length |
79 | 130 | const failedCalls = toolCalls.length - successfulCalls |
80 | 131 |
|
|