| 1 | import { createAsyncThunk, unwrapResult } from "@reduxjs/toolkit"; |
| 2 | import { selectCurrentToolCall } from "../selectors/selectCurrentToolCall"; |
| 3 | import { cancelToolCall, updateToolCallOutput } from "../slices/sessionSlice"; |
| 4 | import { ThunkApiType } from "../store"; |
| 5 | import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; |
| 6 | |
| 7 | export const cancelCurrentToolCall = createAsyncThunk< |
| 8 | void, |
| 9 | undefined, |
| 10 | ThunkApiType |
| 11 | >("chat/cancelTool", async (_, { dispatch, extra, getState }) => { |
| 12 | const state = getState(); |
| 13 | const toolCallState = selectCurrentToolCall(state); |
| 14 | |
| 15 | if (!toolCallState) { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | if (toolCallState.status !== "generated") { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | const { toolCallId } = toolCallState; |
| 24 | dispatch( |
| 25 | cancelToolCall({ |
| 26 | toolCallId, |
| 27 | }), |
| 28 | ); |
| 29 | |
| 30 | dispatch( |
| 31 | updateToolCallOutput({ |
| 32 | toolCallId, |
| 33 | contextItems: [ |
| 34 | { |
| 35 | name: "Tool Cancelled", |
| 36 | description: "Tool Cancelled", |
| 37 | content: |
| 38 | "The tool call was cancelled by the user. Please try something else or request further instructions.", |
| 39 | hidden: true, |
| 40 | }, |
| 41 | ], |
| 42 | }), |
| 43 | ); |
| 44 | const output = await dispatch( |
| 45 | streamResponseAfterToolCall({ |
| 46 | toolCallId, |
| 47 | }), |
| 48 | ); |
| 49 | unwrapResult(output); |
| 50 | }); |