| 1 | import { ContextItem, ToolCallState } from "core"; |
| 2 | import { IIdeMessenger } from "../../context/IdeMessenger"; |
| 3 | import { ChatHistoryItemWithMessageId } from "../slices/sessionSlice"; |
| 4 | import { RootState } from "../store"; |
| 5 | |
| 6 | export function findCurrentToolCall( |
| 7 | chatHistory: RootState["session"]["history"], |
| 8 | ): ToolCallState | undefined { |
| 9 | return chatHistory[chatHistory.length - 1]?.toolCallState; |
| 10 | } |
| 11 | |
| 12 | // TODO parallel tool calls - find tool call must search within arrays |
| 13 | export function findToolCall( |
| 14 | chatHistory: RootState["session"]["history"], |
| 15 | toolCallId: string, |
| 16 | ): ToolCallState | undefined { |
| 17 | return chatHistory.find( |
| 18 | (item) => item.toolCallState?.toolCallId === toolCallId, |
| 19 | )?.toolCallState; |
| 20 | } |
| 21 | |
| 22 | export function findToolOutput( |
| 23 | chatHistory: RootState["session"]["history"], |
| 24 | toolCallId: string, |
| 25 | ): ChatHistoryItemWithMessageId | undefined { |
| 26 | return chatHistory.find( |
| 27 | (item) => |
| 28 | item.message.role === "tool" && item.message.toolCallId === toolCallId, |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | export function logToolUsage( |
| 33 | toolCallState: ToolCallState, |
| 34 | accepted: boolean, |
| 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 | |
| 44 | functionName: |
| 45 | toolCallState.tool?.function?.name || |
| 46 | toolCallState.toolCall.function.name, |
| 47 | |
| 48 | functionParams: toolCallState.tool?.function.parameters, |
| 49 | |
| 50 | toolCallArgs: toolCallState.toolCall.function.arguments, |
| 51 | accepted: accepted, |
| 52 | output: finalOutput || toolCallState.output || [], |
| 53 | succeeded: success, |
| 54 | }, |
| 55 | }); |
| 56 | } |