Elffuss BraidHub

kikocis/continue public

d40a7b9638b1 · 2.2 KiBRawPermalink
1import { createAsyncThunk } from "@reduxjs/toolkit";
2import { ContextItem } from "core";
3import {
4 abortStream,
5 acceptToolCall,
6 updateToolCallOutput,
7} from "../slices/sessionSlice";
8import { ThunkApiType } from "../store";
9import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
10
11/**
12 * This thunk is used to move a terminal command to the background
13 * when the user clicks the "Continue" button in the UI
14 *
15 * It marks the command as visually complete without stopping
16 * the already running process
17 */
18export const moveTerminalProcessToBackground = createAsyncThunk<
19 void,
20 { toolCallId: string },
21 ThunkApiType
22>(
23 "chat/moveTerminalProcessToBackground",
24 async ({ toolCallId }, { dispatch, getState, extra }) => {
25 // Find the current tool call
26 const state = getState();
27 const toolCalls = state.session.history.filter(
28 (item) => item.toolCallState?.toolCallId === toolCallId,
29 );
30
31 if (toolCalls.length === 0) {
32 console.error("Could not find tool call with ID:", toolCallId);
33 return;
34 }
35
36 const toolCall = toolCalls[0].toolCallState;
37 if (!toolCall) {
38 console.error("Tool call state is missing");
39 return;
40 }
41
42 const status =
43 "Command moved to background. Further output will be ignored.";
44
45 const contextItems: ContextItem[] = [
46 {
47 name: "Terminal",
48 description: "Terminal command output",
49 content: "\n" + status,
50 status: status,
51 },
52 ];
53
54 // Abort any existing stream for this tool call
55 dispatch(abortStream());
56
57 // Update the tool call output
58 dispatch(
59 updateToolCallOutput({
60 toolCallId,
61 contextItems,
62 }),
63 );
64
65 // Mark the process as backgrounded so we ignore future events
66 await extra.ideMessenger.request("process/markAsBackgrounded", {
67 toolCallId,
68 });
69
70 // Mark the tool call as "done" in the UI
71 // This will set isRunning to false in RunTerminalCommand.tsx
72 dispatch(acceptToolCall({ toolCallId }));
73
74 // Trigger an LLM response about the command being moved to background
75 dispatch(updateToolCallOutput({ toolCallId, contextItems }));
76 dispatch(
77 streamResponseAfterToolCall({
78 toolCallId,
79 }),
80 );
81 },
82);