Elffuss BraidHub

kikocis/continue public

fc89067dc2f1 · 4.5 KiBRawPermalink
1import { createAsyncThunk } from "@reduxjs/toolkit";
2import { ApplyState } 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 if (toolCallState.status !== "errored") {
87 dispatch(
88 acceptToolCall({
89 toolCallId: applyState.toolCallId,
90 }),
91 );
92 }
93
94 void dispatch(
95 streamResponseAfterToolCall({
96 toolCallId: applyState.toolCallId,
97 }),
98 );
99 }
100 }
101 // TODO return output from edit tools so the model knows the result
102 }
103 }
104 }
105 },
106);
107
108export const handleEditToolApplyError = createAsyncThunk<
109 void,
110 { toolCallId: string },
111 ThunkApiType
112>(
113 "apply/handleEditError",
114 async ({ toolCallId }, { dispatch, getState, extra }) => {
115 const state = getState();
116
117 const toolCallState = selectToolCallById(state, toolCallId);
118 const applyState = selectApplyStateByToolCallId(state, toolCallId);
119 if (
120 toolCallState &&
121 applyState &&
122 applyState.status !== "closed" &&
123 toolCallState.status === "calling"
124 ) {
125 dispatch(
126 errorToolCall({
127 toolCallId,
128 }),
129 );
130 dispatch(
131 updateToolCallOutput({
132 toolCallId,
133 contextItems: [
134 {
135 icon: "problems",
136 name: "Apply Error",
137 description: "Failed to apply changes",
138 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.`,
139 hidden: false,
140 },
141 ],
142 }),
143 );
144 void dispatch(
145 handleApplyStateUpdate({
146 status: "closed",
147 streamId: applyState.streamId,
148 toolCallId,
149 }),
150 );
151 }
152 },
153);