Elffuss BraidHub

kikocis/continue public

9875eaa07e45 · 3.4 KiBRawPermalink
1import { ContextItem, ToolCallState, ToolStatus } from "core";
2import { IIdeMessenger } from "../../context/IdeMessenger";
3import { ChatHistoryItemWithMessageId } from "../slices/sessionSlice";
4import { RootState } from "../store";
5
6/**
7 * Checks if there are any current tool calls in the most recent assistant message.
8 *
9 * @param chatHistory - The chat history array from Redux state
10 * @returns True if there are any current tool calls, false otherwise
11 */
12export function hasCurrentToolCalls(
13 chatHistory: RootState["session"]["history"],
14): boolean {
15 return findAllCurToolCalls(chatHistory).length > 0;
16}
17
18/**
19 * Finds current tool calls with a specific status.
20 *
21 * @param chatHistory - The chat history array from Redux state
22 * @param status - The tool call status to filter by
23 * @returns Array of tool call states with the specified status
24 */
25export function findAllCurToolCallsByStatus(
26 chatHistory: RootState["session"]["history"],
27 status: ToolStatus,
28): ToolCallState[] {
29 return findAllCurToolCalls(chatHistory).filter(
30 (toolCall) => toolCall.status === status,
31 );
32}
33
34/**
35 * Finds all current tool calls from the most recent assistant message with tool call states.
36 *
37 * @param chatHistory - The chat history array from Redux state
38 * @returns An array of all current tool call states, or empty array if none found
39 */
40export function findAllCurToolCalls(
41 chatHistory: RootState["session"]["history"],
42): ToolCallState[] {
43 // Find the most recent assistant message that has tool call states
44 for (let i = chatHistory.length - 1; i >= 0; i--) {
45 const item = chatHistory[i];
46 if (
47 item.message.role === "assistant" &&
48 item.toolCallStates &&
49 item.toolCallStates.length > 0
50 ) {
51 return item.toolCallStates;
52 }
53 }
54
55 return [];
56}
57
58/**
59 * Finds a specific tool call by its ID anywhere in the chat history.
60 *
61 * @param chatHistory - The chat history array from Redux state
62 * @param toolCallId - The unique identifier of the tool call to find
63 * @returns The tool call state with the matching ID, or undefined if not found
64 */
65export function findToolCallById(
66 chatHistory: RootState["session"]["history"],
67 toolCallId: string,
68): ToolCallState | undefined {
69 for (const item of chatHistory) {
70 if (item.toolCallStates) {
71 const toolCallState = item.toolCallStates.find(
72 (state) => state.toolCallId === toolCallId,
73 );
74 if (toolCallState) {
75 return toolCallState;
76 }
77 }
78 }
79
80 return undefined;
81}
82
83export function findChatHistoryItemByToolCallId(
84 chatHistory: RootState["session"]["history"],
85 toolCallId: string,
86): ChatHistoryItemWithMessageId | undefined {
87 return chatHistory.find(
88 (item) =>
89 item.message.role === "tool" && item.message.toolCallId === toolCallId,
90 );
91}
92
93export function logToolUsage(
94 toolCallState: ToolCallState,
95 accepted: boolean,
96 success: boolean,
97 messenger: IIdeMessenger,
98 finalOutput?: ContextItem[],
99) {
100 messenger.post("devdata/log", {
101 name: "toolUsage",
102 data: {
103 toolCallId: toolCallState.toolCallId,
104
105 functionName:
106 toolCallState.tool?.function?.name ||
107 toolCallState.toolCall.function.name,
108
109 functionParams: toolCallState.tool?.function.parameters,
110
111 toolCallArgs: toolCallState.toolCall.function.arguments,
112 accepted: accepted,
113 output: finalOutput || toolCallState.output || [],
114 succeeded: success,
115 },
116 });
117}