Elffuss BraidHub

kikocis/continue public

9c134d8c1202 · 5.9 KiBRawPermalink
1import { createAsyncThunk } from "@reduxjs/toolkit";
2import { ApplyState, ApplyToFilePayload } from "core";
3import { EDIT_MODE_STREAM_ID } from "core/edit/constants";
4import { logAgentModeEditOutcome } from "../../util/editOutcomeLogger";
5import {
6 selectApplyStateByToolCallId,
7 selectToolCallById,
8} from "../selectors/selectToolCalls";
9import { updateEditStateApplyState } from "../slices/editState";
10import {
11 acceptToolCall,
12 errorToolCall,
13 updateApplyState,
14 updateToolCallOutput,
15} from "../slices/sessionSlice";
16import { ThunkApiType } from "../store";
17import { findToolCallById, logToolUsage } from "../util";
18import { exitEdit } from "./edit";
19import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall";
20
21export const handleApplyStateUpdate = createAsyncThunk<
22 void,
23 ApplyState,
24 ThunkApiType
25>(
26 "apply/handleStateUpdate",
27 async (applyState, { dispatch, getState, extra }) => {
28 if (applyState.streamId === EDIT_MODE_STREAM_ID) {
29 dispatch(updateEditStateApplyState(applyState));
30
31 if (applyState.status === "closed") {
32 const toolCallState = findToolCallById(
33 getState().session.history,
34 applyState.toolCallId!,
35 );
36 if (toolCallState) {
37 logToolUsage(toolCallState, true, true, extra.ideMessenger);
38 }
39 void dispatch(exitEdit({}));
40 }
41 } else {
42 // chat or agent
43 dispatch(updateApplyState(applyState));
44
45 // Handle apply status updates - use toolCallId from event payload
46 if (applyState.toolCallId) {
47 if (
48 applyState.status === "done" &&
49 getState().config.config?.ui?.autoAcceptEditToolDiffs
50 ) {
51 extra.ideMessenger.post("acceptDiff", {
52 streamId: applyState.streamId,
53 filepath: applyState.filepath,
54 });
55 }
56 if (applyState.status === "closed") {
57 // Find the tool call to check if it was canceled
58 const toolCallState = findToolCallById(
59 getState().session.history,
60 applyState.toolCallId,
61 );
62
63 if (toolCallState) {
64 const accepted = toolCallState.status !== "canceled";
65
66 logToolUsage(toolCallState, accepted, true, extra.ideMessenger);
67
68 // Log edit outcome for Agent Mode
69 const newApplyState =
70 getState().session.codeBlockApplyStates.states.find(
71 (s) => s.streamId === applyState.streamId,
72 );
73 const newState = getState();
74 if (newApplyState) {
75 void logAgentModeEditOutcome(
76 newState.session.history,
77 newState.config.config,
78 toolCallState,
79 newApplyState,
80 accepted,
81 extra.ideMessenger,
82 );
83 }
84
85 if (accepted) {
86 // Add autoformatting diff to tool output if present
87 if (applyState.autoFormattingDiff) {
88 dispatch(
89 updateToolCallOutput({
90 toolCallId: applyState.toolCallId,
91 contextItems: [
92 {
93 icon: "info",
94 name: "Auto-formatting Applied",
95 description: "Editor auto-formatting changes",
96 content: `Along with your edits, the editor applied the following auto-formatting:\n\n${applyState.autoFormattingDiff}\n\n(Note: Pay close attention to changes such as single quotes being converted to double quotes, semicolons being removed or added, long lines being broken into multiple lines, adjusting indentation style, adding/removing trailing commas, etc. This will help you ensure future SEARCH/REPLACE operations to this file are accurate.)`,
97 hidden: false,
98 },
99 ],
100 }),
101 );
102 }
103
104 if (toolCallState.status !== "errored") {
105 dispatch(
106 acceptToolCall({
107 toolCallId: applyState.toolCallId,
108 }),
109 );
110 }
111
112 void dispatch(
113 streamResponseAfterToolCall({
114 toolCallId: applyState.toolCallId,
115 }),
116 );
117 }
118 }
119 }
120 }
121 }
122 },
123);
124
125export const applyForEditTool = createAsyncThunk<
126 void,
127 ApplyToFilePayload & { toolCallId: string },
128 ThunkApiType
129>("apply/editTool", async (payload, { dispatch, getState, extra }) => {
130 const { toolCallId, streamId } = payload;
131 dispatch(
132 updateApplyState({
133 streamId,
134 toolCallId,
135 status: "not-started",
136 }),
137 );
138
139 let didError = false;
140 try {
141 const response = await extra.ideMessenger.request("applyToFile", payload);
142 if (response.status === "error") {
143 didError = true;
144 }
145 } catch (e) {
146 didError = true;
147 }
148 if (didError) {
149 const state = getState();
150
151 const toolCallState = selectToolCallById(state, toolCallId);
152 const applyState = selectApplyStateByToolCallId(state, toolCallId);
153 if (
154 toolCallState &&
155 applyState &&
156 applyState.status !== "closed" &&
157 toolCallState.status === "calling"
158 ) {
159 dispatch(
160 errorToolCall({
161 toolCallId,
162 }),
163 );
164 dispatch(
165 updateToolCallOutput({
166 toolCallId,
167 contextItems: [
168 {
169 icon: "problems",
170 name: "Apply Error",
171 description: "Failed to apply changes",
172 content: `Error editing file: failed to apply changes to file.\n\nPlease try again with correct args or notify the user and request further instructions.`,
173 hidden: false,
174 },
175 ],
176 }),
177 );
178 void dispatch(
179 handleApplyStateUpdate({
180 status: "closed",
181 streamId: applyState.streamId,
182 toolCallId,
183 }),
184 );
185 }
186 }
187});