Elffuss BraidHub

kikocis/continue public

main · 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 (let i = chatHistory.length - 1; i >= 0; i--) {
70 const item = chatHistory[i];
71 if (item.toolCallStates) {
72 const toolCallState = item.toolCallStates.find(
73 (state) => state.toolCallId === toolCallId,
74 );
75 if (toolCallState) {
76 return toolCallState;
77 }
78 }
79 }
80
81 return undefined;
82}
83
84export function findChatHistoryItemByToolCallId(
85 chatHistory: RootState["session"]["history"],
86 toolCallId: string,
87): ChatHistoryItemWithMessageId | undefined {
88 return chatHistory.findLast(
89 (item) =>
90 item.message.role === "tool" && item.message.toolCallId === toolCallId,
91 );
92}
93
94export function logToolUsage(
95 toolCallState: ToolCallState,
96 accepted: boolean,
97 success: boolean,
98 messenger: IIdeMessenger,
99 finalOutput?: ContextItem[],
100) {
101 messenger.post("devdata/log", {
102 name: "toolUsage",
103 data: {
104 toolCallId: toolCallState.toolCallId,
105
106 functionName:
107 toolCallState.tool?.function?.name ||
108 toolCallState.toolCall.function.name,
109
110 functionParams: toolCallState.tool?.function.parameters,
111
112 toolCallArgs: toolCallState.toolCall.function.arguments,
113 accepted: accepted,
114 output: finalOutput || toolCallState.output || [],
115 succeeded: success,
116 },
117 });
118}