Elffuss BraidHub

kikocis/continue public

35c0ed0a3308 · 2.5 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 "Move to background" link in the UI
14 *
15 * It preserves all existing terminal output, marks the command as
16 * visually complete, and stops listening to further output from
17 * the already running process
18 */
19export const moveTerminalProcessToBackground = createAsyncThunk<
20 void,
21 { toolCallId: string },
22 ThunkApiType
23>(
24 "chat/moveTerminalProcessToBackground",
25 async ({ toolCallId }, { dispatch, getState, extra }) => {
26 // Find the current tool call
27 const state = getState();
28 const toolCalls = state.session.history.filter(
29 (item) => item.toolCallState?.toolCallId === toolCallId,
30 );
31
32 if (toolCalls.length === 0) {
33 console.error("Could not find tool call with ID:", toolCallId);
34 return;
35 }
36
37 const toolCall = toolCalls[0].toolCallState;
38 if (!toolCall) {
39 console.error("Tool call state is missing");
40 return;
41 }
42
43 // Find existing terminal output to preserve it
44 const existingOutput = toolCall.output?.find(
45 (item) => item.name === "Terminal",
46 );
47 const existingContent = existingOutput?.content || "";
48
49 const status =
50 "Command moved to background. Further output will be ignored.";
51
52 const contextItems: ContextItem[] = [
53 {
54 name: "Terminal",
55 description: "Terminal command output",
56 content: existingContent + (existingContent ? "\n\n" : "") + status,
57 status: status,
58 },
59 ];
60
61 // Abort any existing stream for this tool call
62 dispatch(abortStream());
63
64 // Update the tool call output
65 dispatch(
66 updateToolCallOutput({
67 toolCallId,
68 contextItems,
69 }),
70 );
71
72 // Mark the process as backgrounded so we ignore future events
73 await extra.ideMessenger.request("process/markAsBackgrounded", {
74 toolCallId,
75 });
76
77 // Mark the tool call as "done" in the UI
78 // This will set isRunning to false in RunTerminalCommand.tsx
79 dispatch(acceptToolCall({ toolCallId }));
80
81 // Trigger an LLM response about the command being moved to background
82 dispatch(updateToolCallOutput({ toolCallId, contextItems }));
83 dispatch(
84 streamResponseAfterToolCall({
85 toolCallId,
86 }),
87 );
88 },
89);