| 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 | |
| 93 | // Add autoformatting diff to tool output if present |
| 94 | if (applyState.autoFormattingDiff) { |
| 95 | dispatch( |
| 96 | updateToolCallOutput({ |
| 97 | toolCallId: applyState.toolCallId, |
| 98 | contextItems: [ |
| 99 | { |
| 100 | icon: "info", |
| 101 | name: "Auto-formatting Applied", |
| 102 | description: "Editor auto-formatting changes", |
| 103 | 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.)`, |
| 104 | hidden: false, |
| 105 | }, |
| 106 | ], |
| 107 | }), |
| 108 | ); |
| 109 | } else { |
| 110 | dispatch( |
| 111 | updateToolCallOutput({ |
| 112 | toolCallId: applyState.toolCallId, |
| 113 | contextItems: [ |
| 114 | { |
| 115 | name: "Edit Success", |
| 116 | content: `Successfully edited ${applyState.filepath}`, |
| 117 | description: "", |
| 118 | hidden: true, |
| 119 | }, |
| 120 | ], |
| 121 | }), |
| 122 | ); |
| 123 | } |
| 124 | } else { |
| 125 | dispatch( |
| 126 | updateToolCallOutput({ |
| 127 | toolCallId: applyState.toolCallId, |
| 128 | contextItems: [ |
| 129 | { |
| 130 | name: "Edit Failed", |
| 131 | content: `Failed to edit ${applyState.filepath}. To continue working with the file, read it again to see the most up-to-date contents`, |
| 132 | description: "", |
| 133 | hidden: true, |
| 134 | }, |
| 135 | ], |
| 136 | }), |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | void dispatch( |
| 141 | streamResponseAfterToolCall({ |
| 142 | toolCallId: applyState.toolCallId, |
| 143 | }), |
| 144 | ); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | }, |
| 151 | ); |
| 152 | |
| 153 | export const applyForEditTool = createAsyncThunk< |
| 154 | void, |
| 155 | ApplyToFilePayload & { toolCallId: string }, |
| 156 | ThunkApiType |
| 157 | >("apply/editTool", async (payload, { dispatch, getState, extra }) => { |
| 158 | const { toolCallId, streamId } = payload; |
| 159 | dispatch( |
| 160 | updateApplyState({ |
| 161 | streamId, |
| 162 | toolCallId, |
| 163 | status: "not-started", |
| 164 | }), |
| 165 | ); |
| 166 | |
| 167 | let didError = false; |
| 168 | try { |
| 169 | const response = await extra.ideMessenger.request("applyToFile", payload); |
| 170 | if (response.status === "error") { |
| 171 | didError = true; |
| 172 | } |
| 173 | } catch (e) { |
| 174 | didError = true; |
| 175 | } |
| 176 | if (didError) { |
| 177 | const state = getState(); |
| 178 | |
| 179 | const toolCallState = selectToolCallById(state, toolCallId); |
| 180 | const applyState = selectApplyStateByToolCallId(state, toolCallId); |
| 181 | if ( |
| 182 | toolCallState && |
| 183 | applyState && |
| 184 | applyState.status !== "closed" && |
| 185 | toolCallState.status === "calling" |
| 186 | ) { |
| 187 | dispatch( |
| 188 | errorToolCall({ |
| 189 | toolCallId, |
| 190 | }), |
| 191 | ); |
| 192 | dispatch( |
| 193 | updateToolCallOutput({ |
| 194 | toolCallId, |
| 195 | contextItems: [ |
| 196 | { |
| 197 | icon: "problems", |
| 198 | name: "Apply Error", |
| 199 | description: "Failed to apply changes", |
| 200 | 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.`, |
| 201 | hidden: false, |
| 202 | }, |
| 203 | ], |
| 204 | }), |
| 205 | ); |
| 206 | void dispatch( |
| 207 | handleApplyStateUpdate({ |
| 208 | status: "closed", |
| 209 | streamId: applyState.streamId, |
| 210 | toolCallId, |
| 211 | }), |
| 212 | ); |
| 213 | } |
| 214 | } |
| 215 | }); |