| 1 | import { ContextItem, ToolCallState } from "core"; |
| 2 | import { safeParseToolCallArgs } from "core/tools/parseArgs"; |
| 3 | import { IIdeMessenger } from "../../context/IdeMessenger"; |
| 4 | import { ChatHistoryItemWithMessageId } from "../slices/sessionSlice"; |
| 5 | import { RootState } from "../store"; |
| 6 | |
| 7 | export function findCurrentToolCall( |
| 8 | chatHistory: RootState["session"]["history"], |
| 9 | ): ToolCallState | undefined { |
| 10 | return chatHistory[chatHistory.length - 1]?.toolCallState; |
| 11 | } |
| 12 | |
| 13 | // TODO parallel tool calls - find tool call must search within arrays |
| 14 | export function findToolCall( |
| 15 | chatHistory: RootState["session"]["history"], |
| 16 | toolCallId: string, |
| 17 | ): ToolCallState | undefined { |
| 18 | return chatHistory.find( |
| 19 | (item) => item.toolCallState?.toolCallId === toolCallId, |
| 20 | )?.toolCallState; |
| 21 | } |
| 22 | |
| 23 | export function findToolOutput( |
| 24 | chatHistory: RootState["session"]["history"], |
| 25 | toolCallId: string, |
| 26 | ): ChatHistoryItemWithMessageId | undefined { |
| 27 | return chatHistory.find( |
| 28 | (item) => |
| 29 | item.message.role === "tool" && item.message.toolCallId === toolCallId, |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | export function logToolUsage( |
| 34 | toolCallState: ToolCallState, |
| 35 | success: boolean, |
| 36 | messenger: IIdeMessenger, |
| 37 | finalOutput?: ContextItem[], |
| 38 | ) { |
| 39 | messenger.post("devdata/log", { |
| 40 | name: "toolUsage", |
| 41 | data: { |
| 42 | toolCallId: toolCallState.toolCallId, |
| 43 | functionName: toolCallState.toolCall?.function?.name, |
| 44 | functionArgs: toolCallState.toolCall?.function?.arguments, |
| 45 | toolCallArgs: safeParseToolCallArgs(toolCallState.toolCall), |
| 46 | output: finalOutput || toolCallState.output || [], |
| 47 | succeeded: success, |
| 48 | }, |
| 49 | }); |
| 50 | } |