Elffuss BraidHub

kikocis/continue public

main · 2.0 KiBRawPermalink
1import { createAsyncThunk } from "@reduxjs/toolkit";
2import posthog from "posthog-js";
3import { selectSelectedChatModel } from "../slices/configSlice";
4import {
5 cancelToolCall as cancelToolCallAction,
6 updateToolCallOutput,
7} from "../slices/sessionSlice";
8import { ThunkApiType } from "../store";
9import { findToolCallById } from "../util";
10import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
11
12const DEFAULT_USER_REJECTION_MESSAGE = `The user skipped the tool call.
13If the tool call is optional or non-critical to the main goal, skip it and continue with the next step.
14If the tool call is essential, try an alternative approach.
15If no alternatives exist, offer to pause here.`;
16
17export const cancelToolCallThunk = createAsyncThunk<
18 void,
19 { toolCallId: string },
20 ThunkApiType
21>("chat/cancelToolCall", async ({ toolCallId }, { dispatch, getState }) => {
22 const state = getState();
23 const selectedChatModel = selectSelectedChatModel(state);
24 const continueAfterToolRejection =
25 state.config.config.ui?.continueAfterToolRejection;
26 const toolCallState = findToolCallById(state.session.history, toolCallId);
27
28 if (toolCallState) {
29 // Track tool call rejection
30 posthog.capture("tool_call_decision", {
31 model: selectedChatModel,
32 decision: "reject",
33 toolName: toolCallState.toolCall.function.name,
34 toolCallId: toolCallId,
35 });
36 }
37
38 if (continueAfterToolRejection) {
39 // Update tool call output with rejection message
40 dispatch(
41 updateToolCallOutput({
42 toolCallId,
43 contextItems: [
44 {
45 icon: "problems",
46 name: "Tool Call Rejected",
47 description: "User skipped the tool call",
48 content: DEFAULT_USER_REJECTION_MESSAGE,
49 hidden: true,
50 },
51 ],
52 }),
53 );
54 }
55
56 // Dispatch the actual cancel action
57 dispatch(cancelToolCallAction({ toolCallId }));
58
59 void dispatch(streamResponseAfterToolCall({ toolCallId }));
60});