| 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | import { ApplyState, ApplyToFilePayload } from "core"; |
| 3 | import { EDIT_MODE_STREAM_ID } from "core/edit/constants"; |
| 4 | import { logAgentModeEditOutcome } from "../../util/editOutcomeLogger"; |
| 5 | import { |
| 6 | selectApplyStateByToolCallId, |
| 7 | selectToolCallById, |
| 8 | } from "../selectors/selectToolCalls"; |
| 9 | import { updateEditStateApplyState } from "../slices/editState"; |
| 10 | import { |
| 11 | acceptToolCall, |
| 12 | errorToolCall, |
| 13 | updateApplyState, |
| 14 | updateToolCallOutput, |
| 15 | } from "../slices/sessionSlice"; |
| 16 | import { ThunkApiType } from "../store"; |
| 17 | import { findToolCallById, logToolUsage } from "../util"; |
| 18 | import { exitEdit } from "./edit"; |
| 19 | import { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; |
| 20 | |
| 21 | export 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 | dispatch( |
| 93 | updateToolCallOutput({ |
| 94 | toolCallId: applyState.toolCallId, |
| 95 | contextItems: [ |
| 96 | { |
| 97 | name: "Edit Success", |
| 98 | content: `Successfully edited ${applyState.filepath}`, |
| 99 | description: "", |
| 100 | hidden: true, |
| 101 | }, |
| 102 | ], |
| 103 | }), |
| 104 | ); |
| 105 | } else { |
| 106 | dispatch( |
| 107 | updateToolCallOutput({ |
| 108 | toolCallId: applyState.toolCallId, |
| 109 | contextItems: [ |
| 110 | { |
| 111 | name: "Edit Failed", |
| 112 | content: `Failed to edit ${applyState.filepath}. To continue working with the file, read it again to see the most up-to-date contents`, |
| 113 | description: "", |
| 114 | hidden: true, |
| 115 | }, |
| 116 | ], |
| 117 | }), |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | void dispatch( |
| 122 | streamResponseAfterToolCall({ |
| 123 | toolCallId: applyState.toolCallId, |
| 124 | }), |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | // TODO return output from edit tools so the model knows the result |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | }, |
| 133 | ); |
| 134 | |
| 135 | export const applyForEditTool = createAsyncThunk< |
| 136 | void, |
| 137 | ApplyToFilePayload & { toolCallId: string }, |
| 138 | ThunkApiType |
| 139 | >("apply/editTool", async (payload, { dispatch, getState, extra }) => { |
| 140 | const { toolCallId, streamId } = payload; |
| 141 | dispatch( |
| 142 | updateApplyState({ |
| 143 | streamId, |
| 144 | toolCallId, |
| 145 | status: "not-started", |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | let didError = false; |
| 150 | try { |
| 151 | const response = await extra.ideMessenger.request("applyToFile", payload); |
| 152 | if (response.status === "error") { |
| 153 | didError = true; |
| 154 | } |
| 155 | } catch (e) { |
| 156 | didError = true; |
| 157 | } |
| 158 | if (didError) { |
| 159 | const state = getState(); |
| 160 | |
| 161 | const toolCallState = selectToolCallById(state, toolCallId); |
| 162 | const applyState = selectApplyStateByToolCallId(state, toolCallId); |
| 163 | if ( |
| 164 | toolCallState && |
| 165 | applyState && |
| 166 | applyState.status !== "closed" && |
| 167 | toolCallState.status === "calling" |
| 168 | ) { |
| 169 | dispatch( |
| 170 | errorToolCall({ |
| 171 | toolCallId, |
| 172 | }), |
| 173 | ); |
| 174 | dispatch( |
| 175 | updateToolCallOutput({ |
| 176 | toolCallId, |
| 177 | contextItems: [ |
| 178 | { |
| 179 | icon: "problems", |
| 180 | name: "Apply Error", |
| 181 | description: "Failed to apply changes", |
| 182 | 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.`, |
| 183 | hidden: false, |
| 184 | }, |
| 185 | ], |
| 186 | }), |
| 187 | ); |
| 188 | void dispatch( |
| 189 | handleApplyStateUpdate({ |
| 190 | status: "closed", |
| 191 | streamId: applyState.streamId, |
| 192 | toolCallId, |
| 193 | }), |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 | }); |