Elffuss BraidHub

kikocis/continue public

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