Changesets · Branches · Tags · Compare
| Changeset | 6ae8fdd230bd31761d83405803a0f2f435fa482a169f6e158cf2f8d561dcb295 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-06-26 15:59 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-06-26 15:59 -07:00 |
| Parents | 04e94bf72239, b4865f2e59c8 |
22 files changed
modified gui/src/components/StyledMarkdownPreview/StepContainerPreToolbar/index.tsx +15 −13
| @@ -11,8 +11,10 @@ | ||
| 11 | 11 | import { useIdeMessengerRequest } from "../../../hooks/useIdeMessengerRequest"; |
| 12 | 12 | import { useWebviewListener } from "../../../hooks/useWebviewListener"; |
| 13 | 13 | import { useAppSelector } from "../../../redux/hooks"; |
| 14 | -import { selectCurrentToolCallApplyState } from "../../../redux/selectors/selectCurrentToolCall"; | |
| 15 | -import { selectApplyStateByStreamId } from "../../../redux/slices/sessionSlice"; | |
| 14 | +import { | |
| 15 | + selectApplyStateByStreamId, | |
| 16 | + selectApplyStateByToolCallId, | |
| 17 | +} from "../../../redux/slices/sessionSlice"; | |
| 16 | 18 | import { getFontSize } from "../../../util"; |
| 17 | 19 | import Spinner from "../../gui/Spinner"; |
| 18 | 20 | import { isTerminalCodeBlock } from "../utils"; |
| @@ -54,6 +56,7 @@ | ||
| 54 | 56 | codeBlockIndex: number; // To track which codeblock we are applying |
| 55 | 57 | isLastCodeblock: boolean; |
| 56 | 58 | codeBlockStreamId: string; |
| 59 | + forceToolCallId?: string; // If this is defined, we will use this streamId instead of the one from the codeBlock | |
| 57 | 60 | range?: string; |
| 58 | 61 | children: any; |
| 59 | 62 | expanded?: boolean; |
| @@ -68,6 +71,7 @@ | ||
| 68 | 71 | codeBlockIndex, |
| 69 | 72 | isLastCodeblock, |
| 70 | 73 | codeBlockStreamId, |
| 74 | + forceToolCallId, | |
| 71 | 75 | range, |
| 72 | 76 | children, |
| 73 | 77 | expanded, |
| @@ -99,8 +103,8 @@ | ||
| 99 | 103 | const applyState = useAppSelector((state) => |
| 100 | 104 | selectApplyStateByStreamId(state, codeBlockStreamId), |
| 101 | 105 | ); |
| 102 | - const currentToolCallApplyState = useAppSelector( | |
| 103 | - selectCurrentToolCallApplyState, | |
| 106 | + const toolCallApplyState = useAppSelector((state) => | |
| 107 | + selectApplyStateByToolCallId(state, forceToolCallId), | |
| 104 | 108 | ); |
| 105 | 109 | |
| 106 | 110 | /** |
| @@ -147,7 +151,7 @@ | ||
| 147 | 151 | setRelativeFilepathUri(resolvedUri); |
| 148 | 152 | } |
| 149 | 153 | }; |
| 150 | - getRelativeFilepathUri(); | |
| 154 | + void getRelativeFilepathUri(); | |
| 151 | 155 | }, [relativeFilepath, ideMessenger.ide]); |
| 152 | 156 | |
| 153 | 157 | async function getFileUriToApplyTo() { |
| @@ -173,7 +177,7 @@ | ||
| 173 | 177 | async function onClickApply() { |
| 174 | 178 | const fileUri = await getFileUriToApplyTo(); |
| 175 | 179 | if (!fileUri) { |
| 176 | - ideMessenger.ide.showToast( | |
| 180 | + void ideMessenger.ide.showToast( | |
| 177 | 181 | "error", |
| 178 | 182 | "Could not resolve filepath to apply changes", |
| 179 | 183 | ); |
| @@ -188,7 +192,7 @@ | ||
| 188 | 192 | }); |
| 189 | 193 | |
| 190 | 194 | setAppliedFileUri(fileUri); |
| 191 | - refreshFileExists(); | |
| 195 | + void refreshFileExists(); | |
| 192 | 196 | } |
| 193 | 197 | |
| 194 | 198 | function onClickInsertAtCursor() { |
| @@ -198,7 +202,7 @@ | ||
| 198 | 202 | async function handleDiffAction(action: "accept" | "reject") { |
| 199 | 203 | const filepath = await getFileUriToApplyTo(); |
| 200 | 204 | if (!filepath) { |
| 201 | - ideMessenger.ide.showToast( | |
| 205 | + void ideMessenger.ide.showToast( | |
| 202 | 206 | "error", |
| 203 | 207 | `Could not resolve filepath to ${action} changes`, |
| 204 | 208 | ); |
| @@ -233,10 +237,7 @@ | ||
| 233 | 237 | } |
| 234 | 238 | |
| 235 | 239 | const renderActionButtons = () => { |
| 236 | - const isPendingToolCall = | |
| 237 | - currentToolCallApplyState && | |
| 238 | - currentToolCallApplyState.streamId === applyState?.streamId && | |
| 239 | - currentToolCallApplyState.status === "not-started"; | |
| 240 | + const isPendingToolCall = toolCallApplyState?.status === "not-started"; | |
| 240 | 241 | |
| 241 | 242 | if (isGeneratingCodeBlock || isPendingToolCall) { |
| 242 | 243 | const numLines = codeBlockContent.split("\n").length; |
| @@ -271,7 +272,7 @@ | ||
| 271 | 272 | return ( |
| 272 | 273 | <ApplyActions |
| 273 | 274 | disableManualApply={disableManualApply} |
| 274 | - applyState={applyState} | |
| 275 | + applyState={toolCallApplyState ?? applyState} | |
| 275 | 276 | onClickApply={onClickApply} |
| 276 | 277 | onClickAccept={() => handleDiffAction("accept")} |
| 277 | 278 | onClickReject={() => handleDiffAction("reject")} |
| @@ -293,6 +294,7 @@ | ||
| 293 | 294 | <ToolbarDiv isExpanded={isExpanded} className="find-widget-skip gap-3"> |
| 294 | 295 | <div className="max-w-[50% flex flex-row items-center"> |
| 295 | 296 | <ChevronDownIcon |
| 297 | + data-testid="toggle-codeblock" | |
| 296 | 298 | onClick={() => setIsExpanded(!isExpanded)} |
| 297 | 299 | className={`text-lightgray h-3.5 w-3.5 flex-shrink-0 cursor-pointer hover:brightness-125 ${ |
| 298 | 300 | isExpanded ? "rotate-0" : "-rotate-90" |
modified gui/src/components/StyledMarkdownPreview/index.tsx +4 −9
| @@ -126,7 +126,7 @@ | ||
| 126 | 126 | itemIndex?: number; |
| 127 | 127 | useParentBackgroundColor?: boolean; |
| 128 | 128 | disableManualApply?: boolean; |
| 129 | - forceStreamId?: string; | |
| 129 | + toolCallId?: string; | |
| 130 | 130 | expandCodeblocks?: boolean; |
| 131 | 131 | } |
| 132 | 132 | |
| @@ -196,11 +196,6 @@ | ||
| 196 | 196 | const itemIndexRef = useUpdatingRef(props.itemIndex); |
| 197 | 197 | |
| 198 | 198 | const codeblockStreamIds = useRef<string[]>([]); |
| 199 | - useEffect(() => { | |
| 200 | - if (props.forceStreamId) { | |
| 201 | - codeblockStreamIds.current = [props.forceStreamId]; | |
| 202 | - } | |
| 203 | - }, [props.forceStreamId, codeblockStreamIds]); | |
| 204 | 199 | |
| 205 | 200 | const [reactContent, setMarkdownSource] = useRemark({ |
| 206 | 201 | remarkPlugins: [ |
| @@ -295,8 +290,7 @@ | ||
| 295 | 290 | const isLastCodeblock = preChildProps["data-islastcodeblock"]; |
| 296 | 291 | |
| 297 | 292 | if (codeblockStreamIds.current[codeBlockIndex] === undefined) { |
| 298 | - codeblockStreamIds.current[codeBlockIndex] = | |
| 299 | - props.forceStreamId ?? uuidv4(); | |
| 293 | + codeblockStreamIds.current[codeBlockIndex] = uuidv4(); | |
| 300 | 294 | } |
| 301 | 295 | |
| 302 | 296 | return ( |
| @@ -308,7 +302,8 @@ | ||
| 308 | 302 | relativeFilepath={relativeFilePath} |
| 309 | 303 | isLastCodeblock={isLastCodeblock} |
| 310 | 304 | range={range} |
| 311 | - codeBlockStreamId={codeblockStreamIds.current[codeBlockIndex]} | |
| 305 | + codeBlockStreamId={codeblockStreamIds.current[codeBlockIndex]} // ignored if toolCallId stream state is found | |
| 306 | + forceToolCallId={props.toolCallId} | |
| 312 | 307 | expanded={props.expandCodeblocks} |
| 313 | 308 | disableManualApply={props.disableManualApply} |
| 314 | 309 | > |
modified gui/src/components/mainInput/TipTapEditor/utils/editorConfig.ts +4 −1
| @@ -330,7 +330,10 @@ | ||
| 330 | 330 | ], |
| 331 | 331 | editorProps: { |
| 332 | 332 | attributes: { |
| 333 | - class: "outline-none overflow-hidden", | |
| 333 | + "data-testid": props.isMainInput | |
| 334 | + ? "editor-input-main" | |
| 335 | + : `editor-input-${props.inputId}`, | |
| 336 | + class: "ProseMirror outline-none overflow-hidden", | |
| 334 | 337 | style: `font-size: ${getFontSize()}px;`, |
| 335 | 338 | }, |
| 336 | 339 | }, |
modified gui/src/context/MockIdeMessenger.ts +111 −70
| @@ -1,7 +1,6 @@ | ||
| 1 | -import { IDE, PromptLog } from "core"; | |
| 1 | +import { ChatMessage, IDE, PromptLog } from "core"; | |
| 2 | 2 | import { AuthType } from "core/control-plane/AuthTypes"; |
| 3 | 3 | import { |
| 4 | - FromIdeProtocol, | |
| 5 | 4 | FromWebviewProtocol, |
| 6 | 5 | ToCoreProtocol, |
| 7 | 6 | ToWebviewProtocol, |
| @@ -13,73 +12,83 @@ | ||
| 13 | 12 | GeneratorYieldType, |
| 14 | 13 | WebviewSingleProtocolMessage, |
| 15 | 14 | } from "core/protocol/util"; |
| 16 | -import { ChatMessage } from "../redux/store"; | |
| 17 | 15 | import { IIdeMessenger } from "./IdeMessenger"; |
| 18 | 16 | |
| 19 | -async function defaultMockHandleMessage<T extends keyof FromWebviewProtocol>( | |
| 20 | - messageType: T, | |
| 21 | - data: FromWebviewProtocol[T][0], | |
| 22 | -): Promise<FromWebviewProtocol[T][1]> { | |
| 23 | - function returnFor<K extends keyof FromWebviewProtocol>( | |
| 24 | - _: K, | |
| 25 | - value: FromWebviewProtocol[K][1], | |
| 26 | - ): FromWebviewProtocol[T][1] { | |
| 27 | - return value as unknown as FromWebviewProtocol[T][1]; | |
| 28 | - } | |
| 17 | +type MockResponses = Partial<{ | |
| 18 | + [K in keyof FromWebviewProtocol]: FromWebviewProtocol[K][1]; | |
| 19 | +}>; | |
| 29 | 20 | |
| 30 | - switch (messageType) { | |
| 31 | - case "history/list": | |
| 32 | - return returnFor("history/list", [ | |
| 33 | - { | |
| 34 | - title: "Session 1", | |
| 35 | - sessionId: "session-1", | |
| 36 | - dateCreated: new Date().toString(), | |
| 37 | - workspaceDirectory: "/tmp", | |
| 38 | - }, | |
| 39 | - ]); | |
| 40 | - case "getControlPlaneSessionInfo": | |
| 41 | - return returnFor("getControlPlaneSessionInfo", { | |
| 42 | - AUTH_TYPE: AuthType.WorkOsStaging, | |
| 43 | - accessToken: "", | |
| 44 | - account: { | |
| 45 | - label: "", | |
| 46 | - id: "", | |
| 47 | - }, | |
| 48 | - }); | |
| 49 | - case "config/getSerializedProfileInfo": | |
| 50 | - return returnFor("config/getSerializedProfileInfo", { | |
| 51 | - organizations: [], | |
| 52 | - profileId: "test-profile", | |
| 53 | - result: { | |
| 54 | - config: undefined, | |
| 55 | - errors: [], | |
| 56 | - configLoadInterrupted: false, | |
| 57 | - }, | |
| 58 | - selectedOrgId: "local", | |
| 59 | - }); | |
| 60 | - default: | |
| 61 | - throw new Error(`Unknown message type ${messageType}`); | |
| 62 | - } | |
| 63 | -} | |
| 21 | +const DEFAULT_MOCK_CORE_RESPONSES: MockResponses = { | |
| 22 | + fileExists: true, | |
| 23 | + getCurrentFile: { | |
| 24 | + isUntitled: false, | |
| 25 | + contents: "Current file contents", | |
| 26 | + path: "file:///Users/user/workspace1/current_file.py", | |
| 27 | + }, | |
| 28 | + "controlPlane/getFreeTrialStatus": { | |
| 29 | + autocompleteLimit: 1000, | |
| 30 | + optedInToFreeTrial: false, | |
| 31 | + chatLimit: 1000, | |
| 32 | + autocompleteCount: 0, | |
| 33 | + chatCount: 0, | |
| 34 | + }, | |
| 35 | + getWorkspaceDirs: [ | |
| 36 | + "file:///Users/user/workspace1", | |
| 37 | + "file:///Users/user/workspace2", | |
| 38 | + ], | |
| 39 | + "history/list": [ | |
| 40 | + { | |
| 41 | + title: "Session 1", | |
| 42 | + sessionId: "session-1", | |
| 43 | + dateCreated: new Date().toString(), | |
| 44 | + workspaceDirectory: "/tmp", | |
| 45 | + }, | |
| 46 | + ], | |
| 47 | + "history/save": undefined, | |
| 48 | + getControlPlaneSessionInfo: { | |
| 49 | + AUTH_TYPE: AuthType.WorkOsStaging, | |
| 50 | + accessToken: "", | |
| 51 | + account: { | |
| 52 | + label: "", | |
| 53 | + id: "", | |
| 54 | + }, | |
| 55 | + }, | |
| 56 | + "config/getSerializedProfileInfo": { | |
| 57 | + organizations: [], | |
| 58 | + profileId: "test-profile", | |
| 59 | + result: { | |
| 60 | + config: undefined, | |
| 61 | + errors: [], | |
| 62 | + configLoadInterrupted: false, | |
| 63 | + }, | |
| 64 | + selectedOrgId: "local", | |
| 65 | + }, | |
| 66 | + "chatDescriber/describe": "Session summary", | |
| 67 | + applyToFile: undefined, | |
| 68 | + acceptDiff: undefined, | |
| 69 | +}; | |
| 64 | 70 | |
| 71 | +const DEFAULT_CHAT_RESPONSE: ChatMessage[] = [ | |
| 72 | + { | |
| 73 | + role: "assistant", | |
| 74 | + content: "This is a test", | |
| 75 | + }, | |
| 76 | +]; | |
| 65 | 77 | export class MockIdeMessenger implements IIdeMessenger { |
| 66 | 78 | ide: IDE; |
| 67 | - private messageHandlers: Map< | |
| 68 | - keyof FromIdeProtocol, | |
| 69 | - Array<(data: any) => void> | |
| 70 | - > = new Map(); | |
| 71 | 79 | |
| 72 | 80 | constructor() { |
| 73 | 81 | this.ide = new MessageIde( |
| 74 | - (messageType, data) => { | |
| 75 | - throw new Error("Not implemented"); | |
| 82 | + async (messageType, data) => { | |
| 83 | + const response = await this.request.bind(this)(messageType, data); | |
| 84 | + if (response.status === "error") { | |
| 85 | + throw new Error(response.error); | |
| 86 | + } else { | |
| 87 | + return response.content; | |
| 88 | + } | |
| 76 | 89 | }, |
| 77 | 90 | (messageType, callback) => { |
| 78 | - // Store the callback in our handlers map | |
| 79 | - if (!this.messageHandlers.has(messageType)) { | |
| 80 | - this.messageHandlers.set(messageType, []); | |
| 81 | - } | |
| 82 | - this.messageHandlers.get(messageType)?.push(callback); | |
| 91 | + return; | |
| 83 | 92 | }, |
| 84 | 93 | ); |
| 85 | 94 | } |
| @@ -109,16 +118,38 @@ | ||
| 109 | 118 | ); |
| 110 | 119 | } |
| 111 | 120 | |
| 112 | - async *llmStreamChat( | |
| 113 | - msg: ToCoreProtocol["llm/streamChat"][0], | |
| 114 | - cancelToken: AbortSignal, | |
| 115 | - ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { | |
| 116 | - yield [ | |
| 121 | + responses: MockResponses = { ...DEFAULT_MOCK_CORE_RESPONSES }; | |
| 122 | + chatResponse: ChatMessage[] = DEFAULT_CHAT_RESPONSE; | |
| 123 | + chatStreamDelay: number = 0; | |
| 124 | + setChatResponseText(text: string): void { | |
| 125 | + this.chatResponse = [ | |
| 117 | 126 | { |
| 118 | 127 | role: "assistant", |
| 119 | - content: "This is a test", | |
| 128 | + content: text, | |
| 120 | 129 | }, |
| 121 | 130 | ]; |
| 131 | + } | |
| 132 | + | |
| 133 | + async *llmStreamChat( | |
| 134 | + msg: ToCoreProtocol["llm/streamChat"][0], | |
| 135 | + cancelToken: AbortSignal, | |
| 136 | + ): AsyncGenerator<ChatMessage[], PromptLog | undefined> { | |
| 137 | + for (const response of this.chatResponse) { | |
| 138 | + if (cancelToken.aborted) { | |
| 139 | + console.log("MockIdeMessenger: Stream aborted"); | |
| 140 | + return undefined; | |
| 141 | + } | |
| 142 | + console.log( | |
| 143 | + "MockIdeMessenger: Yielding chunk", | |
| 144 | + JSON.stringify(response, null, 2), | |
| 145 | + ); | |
| 146 | + yield [response]; | |
| 147 | + if (this.chatStreamDelay > 0) { | |
| 148 | + await new Promise((resolve) => | |
| 149 | + setTimeout(resolve, this.chatStreamDelay), | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + } | |
| 122 | 153 | return undefined; |
| 123 | 154 | } |
| 124 | 155 | |
| @@ -133,12 +164,16 @@ | ||
| 133 | 164 | messageType: T, |
| 134 | 165 | data: FromWebviewProtocol[T][0], |
| 135 | 166 | ): Promise<WebviewSingleProtocolMessage<T>> { |
| 136 | - const content = await defaultMockHandleMessage(messageType, data); | |
| 137 | - return { | |
| 138 | - status: "success", | |
| 139 | - content, | |
| 140 | - done: true, | |
| 141 | - }; | |
| 167 | + if (messageType in this.responses) { | |
| 168 | + const content = this.responses[messageType]; | |
| 169 | + return { | |
| 170 | + status: "success", | |
| 171 | + content, | |
| 172 | + done: true, | |
| 173 | + }; | |
| 174 | + } | |
| 175 | + console.error(messageType); | |
| 176 | + throw new Error("MockIdeMessenger: No response defined for " + messageType); | |
| 142 | 177 | } |
| 143 | 178 | |
| 144 | 179 | respond<T extends keyof ToWebviewProtocol>( |
| @@ -157,4 +192,10 @@ | ||
| 157 | 192 | > { |
| 158 | 193 | return undefined; |
| 159 | 194 | } |
| 195 | + | |
| 196 | + resetMocks(): void { | |
| 197 | + this.responses = { ...DEFAULT_MOCK_CORE_RESPONSES }; | |
| 198 | + this.chatResponse = DEFAULT_CHAT_RESPONSE; | |
| 199 | + this.chatStreamDelay = 0; | |
| 200 | + } | |
| 160 | 201 | } |
modified gui/src/hooks/ParallelListeners.tsx +38 −18
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import { |
| 21 | 21 | acceptToolCall, |
| 22 | 22 | addContextItemsAtIndex, |
| 23 | + selectCurrentToolCall, | |
| 23 | 24 | setHasReasoningEnabled, |
| 24 | 25 | updateApplyState, |
| 25 | 26 | } from "../redux/slices/sessionSlice"; |
| @@ -244,6 +245,10 @@ | ||
| 244 | 245 | dispatch(updateIndexingStatus(data)); |
| 245 | 246 | }); |
| 246 | 247 | |
| 248 | + const autoAcceptEditToolDiffs = useAppSelector( | |
| 249 | + (store) => store.config.config.ui?.autoAcceptEditToolDiffs, | |
| 250 | + ); | |
| 251 | + const currentToolCall = useAppSelector(selectCurrentToolCall); | |
| 247 | 252 | useWebviewListener( |
| 248 | 253 | "updateApplyState", |
| 249 | 254 | async (state) => { |
| @@ -259,30 +264,45 @@ | ||
| 259 | 264 | |
| 260 | 265 | // Handle apply status updates that are associated with current tool call |
| 261 | 266 | if ( |
| 262 | - state.status === "closed" && | |
| 263 | 267 | currentToolCallApplyState && |
| 264 | 268 | currentToolCallApplyState.streamId === state.streamId |
| 265 | 269 | ) { |
| 266 | - // const output: ContextItem = { | |
| 267 | - // name: "Edit tool output", | |
| 268 | - // content: "Completed edit", | |
| 269 | - // description: "", | |
| 270 | - // }; | |
| 271 | - dispatch( | |
| 272 | - acceptToolCall({ | |
| 273 | - toolCallId: currentToolCallApplyState.toolCallId!, | |
| 274 | - }), | |
| 275 | - ); | |
| 276 | - // dispatch(setToolCallOutput([])); | |
| 277 | - void dispatch( | |
| 278 | - streamResponseAfterToolCall({ | |
| 279 | - toolCallId: currentToolCallApplyState.toolCallId!, | |
| 280 | - }), | |
| 281 | - ); | |
| 270 | + if (state.status === "done" && autoAcceptEditToolDiffs) { | |
| 271 | + console.log("AUTO ACCEPTED"); | |
| 272 | + ideMessenger.post("acceptDiff", { | |
| 273 | + streamId: state.streamId, | |
| 274 | + filepath: state.filepath, | |
| 275 | + }); | |
| 276 | + } | |
| 277 | + if (state.status === "closed") { | |
| 278 | + if (currentToolCall?.status !== "canceled") { | |
| 279 | + dispatch( | |
| 280 | + acceptToolCall({ | |
| 281 | + toolCallId: currentToolCallApplyState.toolCallId!, | |
| 282 | + }), | |
| 283 | + ); | |
| 284 | + } | |
| 285 | + // const output: ContextItem = { | |
| 286 | + // name: "Edit tool output", | |
| 287 | + // content: "Completed edit", | |
| 288 | + // description: "", | |
| 289 | + // }; | |
| 290 | + // dispatch(setToolCallOutput([])); | |
| 291 | + void dispatch( | |
| 292 | + streamResponseAfterToolCall({ | |
| 293 | + toolCallId: currentToolCallApplyState.toolCallId!, | |
| 294 | + }), | |
| 295 | + ); | |
| 296 | + } | |
| 282 | 297 | } |
| 283 | 298 | } |
| 284 | 299 | }, |
| 285 | - [currentToolCallApplyState, history], | |
| 300 | + [ | |
| 301 | + currentToolCall, | |
| 302 | + currentToolCallApplyState, | |
| 303 | + autoAcceptEditToolDiffs, | |
| 304 | + ideMessenger, | |
| 305 | + ], | |
| 286 | 306 | ); |
| 287 | 307 | |
| 288 | 308 | useEffect(() => { |
deleted gui/src/pages/gui/Chat.test.tsx +0 −161
| @@ -1,161 +0,0 @@ | ||
| 1 | -import { screen, waitFor } from "@testing-library/dom"; | |
| 2 | -import { act } from "@testing-library/react"; | |
| 3 | -import { renderWithProviders } from "../../util/test/render"; | |
| 4 | -import { Chat } from "./Chat"; | |
| 5 | - | |
| 6 | -describe("Chat page test", () => { | |
| 7 | - it("should render input box", async () => { | |
| 8 | - await renderWithProviders(<Chat />); | |
| 9 | - expect(await screen.findByTestId("continue-input-box")).toBeInTheDocument(); | |
| 10 | - }); | |
| 11 | - | |
| 12 | - it("should be able to toggle modes", async () => { | |
| 13 | - await renderWithProviders(<Chat />); | |
| 14 | - expect(screen.getByText("Chat")).toBeInTheDocument(); | |
| 15 | - | |
| 16 | - // Simulate cmd+. keyboard shortcut to toggle modes | |
| 17 | - act(() => { | |
| 18 | - document.dispatchEvent( | |
| 19 | - new KeyboardEvent("keydown", { | |
| 20 | - key: ".", | |
| 21 | - metaKey: true, // cmd key on Mac | |
| 22 | - }), | |
| 23 | - ); | |
| 24 | - }); | |
| 25 | - | |
| 26 | - // Check that it switched to Agent mode | |
| 27 | - expect(await screen.findByText("Agent")).toBeInTheDocument(); | |
| 28 | - | |
| 29 | - act(() => { | |
| 30 | - document.dispatchEvent( | |
| 31 | - new KeyboardEvent("keydown", { | |
| 32 | - key: ".", | |
| 33 | - metaKey: true, // cmd key on Mac | |
| 34 | - }), | |
| 35 | - ); | |
| 36 | - }); | |
| 37 | - | |
| 38 | - // Check that it switched back to Chat mode | |
| 39 | - expect(await screen.findByText("Chat")).toBeInTheDocument(); | |
| 40 | - }); | |
| 41 | - | |
| 42 | - it.skip("should send a message and receive a response", async () => { | |
| 43 | - const { user, container } = await renderWithProviders(<Chat />); | |
| 44 | - const inputBox = await waitFor(() => | |
| 45 | - container.querySelector(".ProseMirror")!.querySelector("p"), | |
| 46 | - ); | |
| 47 | - expect(inputBox).toBeDefined(); | |
| 48 | - | |
| 49 | - const sendButton = await screen.findByTestId("submit-input-button"); | |
| 50 | - | |
| 51 | - await act(async () => { | |
| 52 | - // Focus input box | |
| 53 | - inputBox!.focus(); | |
| 54 | - | |
| 55 | - // Type message | |
| 56 | - await user.type(inputBox!, "Hello, world!"); | |
| 57 | - | |
| 58 | - sendButton.click(); | |
| 59 | - }); | |
| 60 | - expect(await screen.findByText("Hello, world!")).toBeInTheDocument(); | |
| 61 | - }); | |
| 62 | - | |
| 63 | - it("should handle apply updates and display the accept / reject all buttons", async () => { | |
| 64 | - const { ideMessenger } = await renderWithProviders(<Chat />); | |
| 65 | - | |
| 66 | - // Use queryByText which returns null when element isn't found | |
| 67 | - const acceptAllButton = screen.queryByText("Accept All"); | |
| 68 | - const rejectAllButton = screen.queryByText("Reject All"); | |
| 69 | - | |
| 70 | - // Assert that the buttons don't exist | |
| 71 | - expect(acceptAllButton).not.toBeInTheDocument(); | |
| 72 | - expect(rejectAllButton).not.toBeInTheDocument(); | |
| 73 | - | |
| 74 | - for (let i = 0; i < 5; i++) { | |
| 75 | - ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 76 | - status: "streaming", | |
| 77 | - streamId: `12345`, | |
| 78 | - }); | |
| 79 | - } | |
| 80 | - | |
| 81 | - ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 82 | - status: "done", | |
| 83 | - streamId: "12345", | |
| 84 | - }); | |
| 85 | - | |
| 86 | - // Wait for the buttons to appear | |
| 87 | - await waitFor(() => { | |
| 88 | - expect( | |
| 89 | - screen.getByTestId("accept-reject-all-buttons"), | |
| 90 | - ).toBeInTheDocument(); | |
| 91 | - }); | |
| 92 | - | |
| 93 | - // IDE sends back message that it is done | |
| 94 | - ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 95 | - status: "closed", | |
| 96 | - streamId: "12345", | |
| 97 | - }); | |
| 98 | - | |
| 99 | - // Wait for the buttons to disappear | |
| 100 | - await waitFor(() => { | |
| 101 | - expect( | |
| 102 | - screen.queryByTestId("edit-accept-button"), | |
| 103 | - ).not.toBeInTheDocument(); | |
| 104 | - expect( | |
| 105 | - screen.queryByTestId("edit-reject-button"), | |
| 106 | - ).not.toBeInTheDocument(); | |
| 107 | - }); | |
| 108 | - }); | |
| 109 | - | |
| 110 | - it("should show apply cancellation", async () => { | |
| 111 | - const { ideMessenger } = await renderWithProviders(<Chat />); | |
| 112 | - | |
| 113 | - // Spy on the request method of ideMessenger | |
| 114 | - const messengerPostSpy = vi.spyOn(ideMessenger, "post"); | |
| 115 | - | |
| 116 | - for (let i = 0; i < 5; i++) { | |
| 117 | - ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 118 | - status: "streaming", | |
| 119 | - streamId: `12345`, | |
| 120 | - }); | |
| 121 | - } | |
| 122 | - | |
| 123 | - // Wait for applying toolbar to appear | |
| 124 | - await waitFor(() => { | |
| 125 | - const cancelApplyButton = screen.getByTestId( | |
| 126 | - "notch-applying-cancel-button", | |
| 127 | - ); | |
| 128 | - const applyingText = screen.getByTestId("notch-applying-text"); | |
| 129 | - | |
| 130 | - expect(cancelApplyButton).toBeInTheDocument(); | |
| 131 | - expect(applyingText).toBeInTheDocument(); | |
| 132 | - }); | |
| 133 | - | |
| 134 | - const cancelApplyButton = screen.getByTestId( | |
| 135 | - "notch-applying-cancel-button", | |
| 136 | - ); | |
| 137 | - cancelApplyButton.click(); | |
| 138 | - | |
| 139 | - // Verify that rejectDiff message has been posted to ideMessenger | |
| 140 | - expect(messengerPostSpy).toHaveBeenCalledWith("rejectDiff", {}); | |
| 141 | - | |
| 142 | - // Now simulate the IDE sending back a message that the apply state is closed | |
| 143 | - ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 144 | - status: "closed", | |
| 145 | - streamId: "12345", | |
| 146 | - }); | |
| 147 | - | |
| 148 | - await waitFor(() => { | |
| 149 | - const cancelApplyButton = screen.queryByTestId( | |
| 150 | - "notch-applying-cancel-button", | |
| 151 | - ); | |
| 152 | - const applyingText = screen.queryByTestId("notch-applying-text"); | |
| 153 | - | |
| 154 | - expect(cancelApplyButton).not.toBeInTheDocument(); | |
| 155 | - expect(applyingText).not.toBeInTheDocument(); | |
| 156 | - }); | |
| 157 | - | |
| 158 | - // Cleanup spy | |
| 159 | - messengerPostSpy.mockRestore(); | |
| 160 | - }); | |
| 161 | -}); |
modified gui/src/pages/gui/ToolCallDiv/EditFile.tsx +1 −26
| @@ -1,12 +1,5 @@ | ||
| 1 | 1 | import { getMarkdownLanguageTagForFile } from "core/util"; |
| 2 | -import { useEffect } from "react"; | |
| 3 | -import { v4 as uuidv4 } from "uuid"; | |
| 4 | 2 | import StyledMarkdownPreview from "../../../components/StyledMarkdownPreview"; |
| 5 | -import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; | |
| 6 | -import { | |
| 7 | - selectApplyStateByToolCallId, | |
| 8 | - updateApplyState, | |
| 9 | -} from "../../../redux/slices/sessionSlice"; | |
| 10 | 3 | |
| 11 | 4 | type EditToolCallProps = { |
| 12 | 5 | relativeFilePath: string; |
| @@ -16,24 +9,6 @@ | ||
| 16 | 9 | }; |
| 17 | 10 | |
| 18 | 11 | export function EditFile(props: EditToolCallProps) { |
| 19 | - const dispatch = useAppDispatch(); | |
| 20 | - | |
| 21 | - const applyState = useAppSelector((state) => | |
| 22 | - selectApplyStateByToolCallId(state, props.toolCallId), | |
| 23 | - ); | |
| 24 | - | |
| 25 | - useEffect(() => { | |
| 26 | - if (!applyState) { | |
| 27 | - dispatch( | |
| 28 | - updateApplyState({ | |
| 29 | - streamId: uuidv4(), | |
| 30 | - toolCallId: props.toolCallId, | |
| 31 | - status: "not-started", | |
| 32 | - }), | |
| 33 | - ); | |
| 34 | - } | |
| 35 | - }, [applyState, props.toolCallId]); | |
| 36 | - | |
| 37 | 12 | if (!props.relativeFilePath || !props.changes) { |
| 38 | 13 | return null; |
| 39 | 14 | } |
| @@ -45,7 +20,7 @@ | ||
| 45 | 20 | isRenderingInStepContainer |
| 46 | 21 | disableManualApply |
| 47 | 22 | source={src} |
| 48 | - forceStreamId={applyState?.streamId} | |
| 23 | + toolCallId={props.toolCallId} | |
| 49 | 24 | expandCodeblocks={false} |
| 50 | 25 | itemIndex={props.historyIndex} |
| 51 | 26 | /> |
added gui/src/pages/gui/chat-tests/ApplyScenarios.test.tsx +80 −0
| @@ -0,0 +1,80 @@ | ||
| 1 | +import { act } from "@testing-library/react"; | |
| 2 | +import { renderWithProviders } from "../../../util/test/render"; | |
| 3 | +import { | |
| 4 | + getElementByTestId, | |
| 5 | + verifyNotPresentByTestId, | |
| 6 | + verifyNotPresentByText, | |
| 7 | +} from "../../../util/test/utils"; | |
| 8 | +import { Chat } from "../Chat"; | |
| 9 | + | |
| 10 | +test("Chat apply scenarios: handle apply updates and display the accept / reject all buttons", async () => { | |
| 11 | + const { ideMessenger } = await renderWithProviders(<Chat />); | |
| 12 | + | |
| 13 | + // Use queryByText which returns null when element isn't found | |
| 14 | + await verifyNotPresentByText("Accept All"); | |
| 15 | + await verifyNotPresentByText("Reject All"); | |
| 16 | + | |
| 17 | + for (let i = 0; i < 5; i++) { | |
| 18 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 19 | + status: "streaming", | |
| 20 | + streamId: `12345`, | |
| 21 | + }); | |
| 22 | + } | |
| 23 | + | |
| 24 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 25 | + status: "done", | |
| 26 | + streamId: "12345", | |
| 27 | + }); | |
| 28 | + | |
| 29 | + // Wait for the buttons to appear | |
| 30 | + await getElementByTestId("accept-reject-all-buttons"); | |
| 31 | + | |
| 32 | + // IDE sends back message that it is done | |
| 33 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 34 | + status: "closed", | |
| 35 | + streamId: "12345", | |
| 36 | + }); | |
| 37 | + | |
| 38 | + // Wait for the buttons to disappear | |
| 39 | + await verifyNotPresentByTestId("edit-accept-button"); | |
| 40 | + await verifyNotPresentByTestId("edit-reject-button"); | |
| 41 | +}); | |
| 42 | + | |
| 43 | +test("Chat apply scenarios: show apply cancellation", async () => { | |
| 44 | + const { ideMessenger } = await renderWithProviders(<Chat />); | |
| 45 | + | |
| 46 | + // Spy on the request method of ideMessenger | |
| 47 | + const messengerPostSpy = vi.spyOn(ideMessenger, "post"); | |
| 48 | + | |
| 49 | + for (let i = 0; i < 5; i++) { | |
| 50 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 51 | + status: "streaming", | |
| 52 | + streamId: `12345`, | |
| 53 | + }); | |
| 54 | + } | |
| 55 | + | |
| 56 | + // Wait for applying toolbar to appear | |
| 57 | + await getElementByTestId("notch-applying-text"); | |
| 58 | + | |
| 59 | + await act(async () => { | |
| 60 | + const cancelApplyButton = await getElementByTestId( | |
| 61 | + "notch-applying-cancel-button", | |
| 62 | + ); | |
| 63 | + cancelApplyButton.click(); | |
| 64 | + }); | |
| 65 | + | |
| 66 | + // Verify that rejectDiff message has been posted to ideMessenger | |
| 67 | + expect(messengerPostSpy).toHaveBeenCalledWith("rejectDiff", {}); | |
| 68 | + | |
| 69 | + // Now simulate the IDE sending back a message that the apply state is closed | |
| 70 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 71 | + status: "closed", | |
| 72 | + streamId: "12345", | |
| 73 | + }); | |
| 74 | + | |
| 75 | + await verifyNotPresentByTestId("notch-applying-text"); | |
| 76 | + await verifyNotPresentByTestId("notch-applying-cancel-button"); | |
| 77 | + | |
| 78 | + // Cleanup spy | |
| 79 | + messengerPostSpy.mockRestore(); | |
| 80 | +}); |
added gui/src/pages/gui/chat-tests/Chat.test.tsx +61 −0
| @@ -0,0 +1,61 @@ | ||
| 1 | +import { act } from "@testing-library/react"; | |
| 2 | +import { addAndSelectMockLlm } from "../../../util/test/config"; | |
| 3 | +import { renderWithProviders } from "../../../util/test/render"; | |
| 4 | +import { | |
| 5 | + getElementByTestId, | |
| 6 | + getElementByText, | |
| 7 | + sendInputWithMockedResponse, | |
| 8 | +} from "../../../util/test/utils"; | |
| 9 | +import { Chat } from "../Chat"; | |
| 10 | + | |
| 11 | +test("should render input box", async () => { | |
| 12 | + await renderWithProviders(<Chat />); | |
| 13 | + await getElementByTestId("continue-input-box"); | |
| 14 | +}); | |
| 15 | + | |
| 16 | +test("should be able to toggle modes", async () => { | |
| 17 | + await renderWithProviders(<Chat />); | |
| 18 | + await getElementByText("Chat"); | |
| 19 | + | |
| 20 | + // Simulate cmd+. keyboard shortcut to toggle modes | |
| 21 | + act(() => { | |
| 22 | + document.dispatchEvent( | |
| 23 | + new KeyboardEvent("keydown", { | |
| 24 | + key: ".", | |
| 25 | + metaKey: true, // cmd key on Mac | |
| 26 | + }), | |
| 27 | + ); | |
| 28 | + }); | |
| 29 | + | |
| 30 | + // Check that it switched to Agent mode | |
| 31 | + await getElementByText("Agent"); | |
| 32 | + | |
| 33 | + act(() => { | |
| 34 | + document.dispatchEvent( | |
| 35 | + new KeyboardEvent("keydown", { | |
| 36 | + key: ".", | |
| 37 | + metaKey: true, // cmd key on Mac | |
| 38 | + }), | |
| 39 | + ); | |
| 40 | + }); | |
| 41 | + | |
| 42 | + await getElementByText("Chat"); | |
| 43 | +}); | |
| 44 | + | |
| 45 | +test("should send a message and receive a response", async () => { | |
| 46 | + const { ideMessenger, store } = await renderWithProviders(<Chat />); | |
| 47 | + | |
| 48 | + // First add and select the mock LLM | |
| 49 | + await act(async () => { | |
| 50 | + addAndSelectMockLlm(store, ideMessenger); | |
| 51 | + }); | |
| 52 | + | |
| 53 | + const CONTENT = "Expected response"; | |
| 54 | + const INPUT = "User input"; | |
| 55 | + | |
| 56 | + await sendInputWithMockedResponse(ideMessenger, INPUT, [ | |
| 57 | + { role: "assistant", content: CONTENT }, | |
| 58 | + ]); | |
| 59 | + | |
| 60 | + await getElementByText(CONTENT); | |
| 61 | +}); |
added gui/src/pages/gui/chat-tests/EditToolScenarios.test.tsx +231 −0
| @@ -0,0 +1,231 @@ | ||
| 1 | +import { BuiltInToolNames } from "core/tools/builtIn"; | |
| 2 | +import { | |
| 3 | + addAndSelectMockLlm, | |
| 4 | + triggerConfigUpdate, | |
| 5 | +} from "../../../util/test/config"; | |
| 6 | +import { renderWithProviders } from "../../../util/test/render"; | |
| 7 | +import { Chat } from "../Chat"; | |
| 8 | + | |
| 9 | +import { waitFor } from "@testing-library/dom"; | |
| 10 | +import { ChatMessage } from "core"; | |
| 11 | +import { toggleToolSetting } from "../../../redux/slices/uiSlice"; | |
| 12 | +import { | |
| 13 | + getElementByTestId, | |
| 14 | + getElementByText, | |
| 15 | + sendInputWithMockedResponse, | |
| 16 | + verifyNotPresentByTestId, | |
| 17 | +} from "../../../util/test/utils"; | |
| 18 | + | |
| 19 | +const EDIT_WORKSPACE_DIR = "file:///workspace"; | |
| 20 | +const EDIT_FILEPATH = "test.txt"; | |
| 21 | +const EDIT_FILE_URI = `${EDIT_WORKSPACE_DIR}/${EDIT_FILEPATH}`; | |
| 22 | +const EDIT_CHANGES = "New content"; | |
| 23 | +const EDIT_TOOL_CALL_ID = "known-id"; | |
| 24 | +const EDIT_MESSAGES: ChatMessage[] = [ | |
| 25 | + { | |
| 26 | + role: "assistant", | |
| 27 | + content: "", | |
| 28 | + toolCalls: [ | |
| 29 | + { | |
| 30 | + id: EDIT_TOOL_CALL_ID, | |
| 31 | + function: { | |
| 32 | + name: BuiltInToolNames.EditExistingFile, | |
| 33 | + arguments: JSON.stringify({ | |
| 34 | + filepath: EDIT_FILEPATH, | |
| 35 | + changes: EDIT_CHANGES, | |
| 36 | + }), | |
| 37 | + }, | |
| 38 | + }, | |
| 39 | + ], | |
| 40 | + }, | |
| 41 | +]; | |
| 42 | + | |
| 43 | +const POST_EDIT_RESPONSE = "Edit applied successfully"; | |
| 44 | +beforeEach(() => { | |
| 45 | + vi.clearAllMocks(); | |
| 46 | +}); | |
| 47 | + | |
| 48 | +test("Edit run with ask first policy and no auto apply", async () => { | |
| 49 | + // Setup | |
| 50 | + const { ideMessenger, store, user } = await renderWithProviders(<Chat />); | |
| 51 | + | |
| 52 | + ideMessenger.responses["getWorkspaceDirs"] = [EDIT_WORKSPACE_DIR]; | |
| 53 | + const messengerPostSpy = vi.spyOn(ideMessenger, "post"); | |
| 54 | + | |
| 55 | + addAndSelectMockLlm(store, ideMessenger); | |
| 56 | + | |
| 57 | + // Send the input that will respond with an edit tool call | |
| 58 | + await sendInputWithMockedResponse( | |
| 59 | + ideMessenger, | |
| 60 | + "Edit this file", | |
| 61 | + EDIT_MESSAGES, | |
| 62 | + ); | |
| 63 | + | |
| 64 | + // Toggle the codeblock and make sure the changes show | |
| 65 | + const toggleCodeblockChevron = await getElementByTestId("toggle-codeblock"); | |
| 66 | + | |
| 67 | + await user.click(toggleCodeblockChevron); | |
| 68 | + await getElementByText(EDIT_CHANGES); | |
| 69 | + | |
| 70 | + // Pending tool call - find and click the accept button | |
| 71 | + const acceptToolCallButton = await getElementByTestId( | |
| 72 | + "accept-tool-call-button", | |
| 73 | + ); | |
| 74 | + await user.click(acceptToolCallButton); | |
| 75 | + | |
| 76 | + // Tool call, check that applyToFile was called for edit | |
| 77 | + await waitFor(() => { | |
| 78 | + expect(messengerPostSpy).toHaveBeenCalledWith("applyToFile", { | |
| 79 | + streamId: expect.any(String), | |
| 80 | + filepath: EDIT_FILE_URI, | |
| 81 | + text: EDIT_CHANGES, | |
| 82 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 83 | + }); | |
| 84 | + }); | |
| 85 | + | |
| 86 | + // Extract stream ID and initiate mock streaming | |
| 87 | + const streamId = messengerPostSpy.mock.calls.find( | |
| 88 | + (call) => call[0] === "applyToFile", | |
| 89 | + )?.[1]?.streamId; | |
| 90 | + expect(streamId).toBeDefined(); | |
| 91 | + | |
| 92 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 93 | + status: "streaming", | |
| 94 | + streamId, | |
| 95 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 96 | + filepath: EDIT_FILE_URI, | |
| 97 | + }); | |
| 98 | + | |
| 99 | + // Mid stream, should show applying in notch | |
| 100 | + await getElementByTestId("notch-applying-text"); | |
| 101 | + await getElementByTestId("notch-applying-cancel-button"); | |
| 102 | + | |
| 103 | + // Close the stream | |
| 104 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 105 | + status: "done", | |
| 106 | + streamId, | |
| 107 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 108 | + filepath: EDIT_FILE_URI, | |
| 109 | + }); | |
| 110 | + | |
| 111 | + // Verify accept/reject buttons are present | |
| 112 | + const acceptButton = await getElementByTestId("edit-accept-button"); | |
| 113 | + await getElementByTestId("edit-reject-button"); | |
| 114 | + | |
| 115 | + // Accept the changes, which should trigger a response after the tool call | |
| 116 | + await user.click(acceptButton); | |
| 117 | + | |
| 118 | + expect(messengerPostSpy).toHaveBeenCalledWith("acceptDiff", { | |
| 119 | + streamId, | |
| 120 | + filepath: EDIT_FILE_URI, | |
| 121 | + }); | |
| 122 | + | |
| 123 | + ideMessenger.setChatResponseText(POST_EDIT_RESPONSE); | |
| 124 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 125 | + status: "closed", | |
| 126 | + streamId, | |
| 127 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 128 | + filepath: EDIT_FILE_URI, | |
| 129 | + }); | |
| 130 | + | |
| 131 | + await verifyNotPresentByTestId("edit-accept-button"); | |
| 132 | + await verifyNotPresentByTestId("edit-reject-button"); | |
| 133 | + | |
| 134 | + await getElementByText(POST_EDIT_RESPONSE); | |
| 135 | +}); | |
| 136 | + | |
| 137 | +test("Edit run with no policy and yolo mode", async () => { | |
| 138 | + // Setup | |
| 139 | + const { ideMessenger, store, user } = await renderWithProviders(<Chat />); | |
| 140 | + | |
| 141 | + ideMessenger.responses["getWorkspaceDirs"] = [EDIT_WORKSPACE_DIR]; | |
| 142 | + const messengerPostSpy = vi.spyOn(ideMessenger, "post"); | |
| 143 | + | |
| 144 | + addAndSelectMockLlm(store, ideMessenger); | |
| 145 | + | |
| 146 | + // Enable automatic edit and yolo mode | |
| 147 | + store.dispatch(toggleToolSetting(BuiltInToolNames.EditExistingFile)); | |
| 148 | + triggerConfigUpdate({ | |
| 149 | + store, | |
| 150 | + ideMessenger, | |
| 151 | + editConfig: (current) => { | |
| 152 | + current.ui = { | |
| 153 | + ...current.ui, | |
| 154 | + autoAcceptEditToolDiffs: true, // Enable auto-accept for edit tool diffs | |
| 155 | + }; | |
| 156 | + return current; | |
| 157 | + }, | |
| 158 | + }); | |
| 159 | + expect(store.getState().config.config.ui?.autoAcceptEditToolDiffs).toBe(true); | |
| 160 | + | |
| 161 | + // Send the input that will respond with an edit tool call | |
| 162 | + await sendInputWithMockedResponse( | |
| 163 | + ideMessenger, | |
| 164 | + "Edit this file", | |
| 165 | + EDIT_MESSAGES, | |
| 166 | + ); | |
| 167 | + | |
| 168 | + // Toggle the codeblock and make sure the changes show | |
| 169 | + const toggleCodeblockChevron = await getElementByTestId("toggle-codeblock"); | |
| 170 | + | |
| 171 | + await user.click(toggleCodeblockChevron); | |
| 172 | + await getElementByText(EDIT_CHANGES); | |
| 173 | + | |
| 174 | + // Make sure there's no pending tool call | |
| 175 | + verifyNotPresentByTestId("accept-tool-call-button"); | |
| 176 | + // Tool call, check that applyToFile was called for edit | |
| 177 | + await waitFor(() => { | |
| 178 | + expect(messengerPostSpy).toHaveBeenCalledWith("applyToFile", { | |
| 179 | + streamId: expect.any(String), | |
| 180 | + filepath: EDIT_FILE_URI, | |
| 181 | + text: EDIT_CHANGES, | |
| 182 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 183 | + }); | |
| 184 | + }); | |
| 185 | + | |
| 186 | + // Extract stream ID and initiate mock streaming | |
| 187 | + const streamId = messengerPostSpy.mock.calls.find( | |
| 188 | + (call) => call[0] === "applyToFile", | |
| 189 | + )?.[1]?.streamId; | |
| 190 | + expect(streamId).toBeDefined(); | |
| 191 | + | |
| 192 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 193 | + status: "streaming", | |
| 194 | + streamId, | |
| 195 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 196 | + filepath: EDIT_FILE_URI, | |
| 197 | + }); | |
| 198 | + | |
| 199 | + // Mid stream, should show applying in notch | |
| 200 | + await getElementByTestId("notch-applying-text"); | |
| 201 | + await getElementByTestId("notch-applying-cancel-button"); | |
| 202 | + | |
| 203 | + // Close the stream | |
| 204 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 205 | + status: "done", | |
| 206 | + streamId, | |
| 207 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 208 | + filepath: EDIT_FILE_URI, | |
| 209 | + }); | |
| 210 | + | |
| 211 | + await waitFor(() => { | |
| 212 | + expect(messengerPostSpy).toHaveBeenCalledWith("acceptDiff", { | |
| 213 | + streamId, | |
| 214 | + filepath: EDIT_FILE_URI, | |
| 215 | + }); | |
| 216 | + }); | |
| 217 | + | |
| 218 | + // Close the stream, ensure response is shown and diff buttons are not present | |
| 219 | + ideMessenger.setChatResponseText(POST_EDIT_RESPONSE); | |
| 220 | + ideMessenger.mockMessageToWebview("updateApplyState", { | |
| 221 | + status: "closed", | |
| 222 | + streamId, | |
| 223 | + toolCallId: EDIT_TOOL_CALL_ID, | |
| 224 | + filepath: EDIT_FILE_URI, | |
| 225 | + }); | |
| 226 | + | |
| 227 | + await verifyNotPresentByTestId("edit-accept-button"); | |
| 228 | + await verifyNotPresentByTestId("edit-reject-button"); | |
| 229 | + | |
| 230 | + await getElementByText(POST_EDIT_RESPONSE); | |
| 231 | +}); |
modified gui/src/redux/slices/sessionSlice.ts +3 −1
| @@ -657,7 +657,9 @@ | ||
| 657 | 657 | (state: RootState, toolCallId?: string) => toolCallId, |
| 658 | 658 | ], |
| 659 | 659 | (states, toolCallId) => { |
| 660 | - return states.find((state) => state.toolCallId === toolCallId); | |
| 660 | + if (toolCallId) { | |
| 661 | + return states.find((state) => state.toolCallId === toolCallId); | |
| 662 | + } | |
| 661 | 663 | }, |
| 662 | 664 | ); |
| 663 | 665 |
modified gui/src/redux/store.ts +0 −5
| @@ -23,11 +23,6 @@ | ||
| 23 | 23 | import tabsReducer from "./slices/tabsSlice"; |
| 24 | 24 | import uiReducer from "./slices/uiSlice"; |
| 25 | 25 | |
| 26 | -export interface ChatMessage { | |
| 27 | - role: "system" | "user" | "assistant"; | |
| 28 | - content: string; | |
| 29 | -} | |
| 30 | - | |
| 31 | 26 | const rootReducer = combineReducers({ |
| 32 | 27 | session: sessionReducer, |
| 33 | 28 | ui: uiReducer, |
modified gui/src/redux/thunks/callCurrentTool.ts +2 −7
| @@ -19,7 +19,6 @@ | ||
| 19 | 19 | async (_, { dispatch, extra, getState }) => { |
| 20 | 20 | const state = getState(); |
| 21 | 21 | const toolCallState = selectCurrentToolCall(state); |
| 22 | - | |
| 23 | 22 | if (!toolCallState) { |
| 24 | 23 | return; |
| 25 | 24 | } |
| @@ -61,19 +60,15 @@ | ||
| 61 | 60 | ) { |
| 62 | 61 | // Tool is called on client side |
| 63 | 62 | const { |
| 64 | - output: clientToolOuput, | |
| 63 | + output: clientToolOutput, | |
| 65 | 64 | respondImmediately, |
| 66 | 65 | errorMessage: clientToolError, |
| 67 | 66 | } = await callClientTool(toolCallState, { |
| 68 | 67 | dispatch, |
| 69 | 68 | ideMessenger: extra.ideMessenger, |
| 70 | - streamId: state.session.codeBlockApplyStates.states.find( | |
| 71 | - (state) => | |
| 72 | - state.toolCallId && state.toolCallId === toolCallState.toolCallId, | |
| 73 | - )?.streamId, | |
| 74 | 69 | getState, |
| 75 | 70 | }); |
| 76 | - output = clientToolOuput; | |
| 71 | + output = clientToolOutput; | |
| 77 | 72 | errorMessage = clientToolError; |
| 78 | 73 | streamResponse = respondImmediately; |
| 79 | 74 | } else { |
modified gui/src/redux/thunks/edit.ts +3 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | import { |
| 18 | 18 | newSession, |
| 19 | 19 | setActive, |
| 20 | + setInactive, | |
| 20 | 21 | setIsInEdit, |
| 21 | 22 | setMainEditorContentTrigger, |
| 22 | 23 | setMode, |
| @@ -61,6 +62,8 @@ | ||
| 61 | 62 | range: codeToEdit[0] as RangeInFileWithContents, |
| 62 | 63 | }); |
| 63 | 64 | |
| 65 | + dispatch(setInactive()); | |
| 66 | + | |
| 64 | 67 | if (response.status === "error") { |
| 65 | 68 | throw new Error(response.error); |
| 66 | 69 | } |
modified gui/src/redux/thunks/streamNormalInput.ts +6 −0
| @@ -8,6 +8,8 @@ | ||
| 8 | 8 | import { |
| 9 | 9 | abortStream, |
| 10 | 10 | addPromptCompletionPair, |
| 11 | + setActive, | |
| 12 | + setInactive, | |
| 11 | 13 | setToolGenerated, |
| 12 | 14 | streamUpdate, |
| 13 | 15 | } from "../slices/sessionSlice"; |
| @@ -53,6 +55,8 @@ | ||
| 53 | 55 | }; |
| 54 | 56 | } |
| 55 | 57 | |
| 58 | + dispatch(setActive()); | |
| 59 | + | |
| 56 | 60 | // Send request |
| 57 | 61 | const gen = extra.ideMessenger.llmStreamChat( |
| 58 | 62 | { |
| @@ -76,6 +80,8 @@ | ||
| 76 | 80 | next = await gen.next(); |
| 77 | 81 | } |
| 78 | 82 | |
| 83 | + dispatch(setInactive()); | |
| 84 | + | |
| 79 | 85 | // Attach prompt log and end thinking for reasoning models |
| 80 | 86 | if (next.done && next.value) { |
| 81 | 87 | dispatch(addPromptCompletionPair([next.value])); |
modified gui/src/redux/thunks/streamResponseAfterToolCall.ts +1 −7
| @@ -4,11 +4,7 @@ | ||
| 4 | 4 | import { renderContextItems } from "core/util/messageContent"; |
| 5 | 5 | import { getBaseSystemMessage } from "../../util"; |
| 6 | 6 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 7 | -import { | |
| 8 | - addContextItemsAtIndex, | |
| 9 | - setActive, | |
| 10 | - streamUpdate, | |
| 11 | -} from "../slices/sessionSlice"; | |
| 7 | +import { addContextItemsAtIndex, streamUpdate } from "../slices/sessionSlice"; | |
| 12 | 8 | import { ThunkApiType } from "../store"; |
| 13 | 9 | import { findToolCall } from "../util"; |
| 14 | 10 | import { resetStateForNewMessage } from "./resetStateForNewMessage"; |
| @@ -63,8 +59,6 @@ | ||
| 63 | 59 | }), |
| 64 | 60 | ); |
| 65 | 61 | |
| 66 | - dispatch(setActive()); | |
| 67 | - | |
| 68 | 62 | const updatedHistory = getState().session.history; |
| 69 | 63 | const messageMode = getState().session.mode; |
| 70 | 64 |
modified gui/src/redux/thunks/streamThunkWrapper.tsx +0 −2
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | 2 | import StreamErrorDialog from "../../pages/gui/StreamError"; |
| 3 | -import { setInactive } from "../slices/sessionSlice"; | |
| 4 | 3 | import { setDialogMessage, setShowDialog } from "../slices/uiSlice"; |
| 5 | 4 | import { ThunkApiType } from "../store"; |
| 6 | 5 | import { cancelStream } from "./cancelStream"; |
| @@ -22,7 +21,6 @@ | ||
| 22 | 21 | }), |
| 23 | 22 | ); |
| 24 | 23 | } |
| 25 | - dispatch(setInactive()); | |
| 26 | 24 | } catch (e) { |
| 27 | 25 | dispatch(cancelStream()); |
| 28 | 26 | dispatch(setDialogMessage(<StreamErrorDialog error={e} />)); |
modified gui/src/util/clientTools/callClientTool.ts +0 −1
| @@ -8,7 +8,6 @@ | ||
| 8 | 8 | getState: () => RootState; |
| 9 | 9 | dispatch: AppThunkDispatch; |
| 10 | 10 | ideMessenger: IIdeMessenger; |
| 11 | - streamId?: string; | |
| 12 | 11 | } |
| 13 | 12 | |
| 14 | 13 | export interface ClientToolOutput { |
modified gui/src/util/clientTools/editImpl.ts +5 −24
| @@ -1,14 +1,11 @@ | ||
| 1 | 1 | import { resolveRelativePathInDir } from "core/util/ideUtils"; |
| 2 | +import { v4 as uuid } from "uuid"; | |
| 2 | 3 | import { ClientToolImpl } from "./callClientTool"; |
| 3 | - | |
| 4 | 4 | export const editToolImpl: ClientToolImpl = async ( |
| 5 | 5 | args, |
| 6 | 6 | toolCallId, |
| 7 | 7 | extras, |
| 8 | 8 | ) => { |
| 9 | - if (!extras.streamId) { | |
| 10 | - throw new Error("Invalid apply state"); | |
| 11 | - } | |
| 12 | 9 | const firstUriMatch = await resolveRelativePathInDir( |
| 13 | 10 | args.filepath, |
| 14 | 11 | extras.ideMessenger.ide, |
| @@ -16,30 +13,14 @@ | ||
| 16 | 13 | if (!firstUriMatch) { |
| 17 | 14 | throw new Error(`${args.filepath} does not exist`); |
| 18 | 15 | } |
| 19 | - const apply = await extras.ideMessenger.request("applyToFile", { | |
| 20 | - streamId: extras.streamId, | |
| 16 | + const streamId = uuid(); | |
| 17 | + extras.ideMessenger.post("applyToFile", { | |
| 18 | + streamId, | |
| 21 | 19 | text: args.changes, |
| 22 | 20 | toolCallId, |
| 23 | 21 | filepath: firstUriMatch, |
| 24 | 22 | }); |
| 25 | - if (apply.status === "error") { | |
| 26 | - throw new Error(apply.error); | |
| 27 | - } | |
| 28 | - const state = extras.getState(); | |
| 29 | - const autoAccept = !!state.config.config.ui?.autoAcceptEditToolDiffs; | |
| 30 | - if (autoAccept) { | |
| 31 | - const out = await extras.ideMessenger.request("acceptDiff", { | |
| 32 | - streamId: extras.streamId, | |
| 33 | - filepath: firstUriMatch, | |
| 34 | - }); | |
| 35 | - if (out.status === "error") { | |
| 36 | - throw new Error(out.error); | |
| 37 | - } | |
| 38 | - return { | |
| 39 | - respondImmediately: true, | |
| 40 | - output: undefined, // TODO - feed edit results back to model (also in parallel listeners) | |
| 41 | - }; | |
| 42 | - } | |
| 23 | + | |
| 43 | 24 | return { |
| 44 | 25 | respondImmediately: false, |
| 45 | 26 | output: undefined, // No immediate output. |
added gui/src/util/test/config.ts +67 −0
| @@ -0,0 +1,67 @@ | ||
| 1 | +import { EnhancedStore } from "@reduxjs/toolkit"; | |
| 2 | +import { BrowserSerializedContinueConfig, ModelDescription } from "core"; | |
| 3 | +import { SerializedOrgWithProfiles } from "core/config/ProfileLifecycleManager"; | |
| 4 | +import { copyOf } from "core/util"; | |
| 5 | +import { MockIdeMessenger } from "../../context/MockIdeMessenger"; | |
| 6 | + | |
| 7 | +interface TestConfigUpdateParams { | |
| 8 | + store: EnhancedStore; | |
| 9 | + ideMessenger: MockIdeMessenger; | |
| 10 | + newProfileId?: string; | |
| 11 | + newOrgId?: string; | |
| 12 | + newOrgs?: SerializedOrgWithProfiles[]; | |
| 13 | + editConfig?: ( | |
| 14 | + current: BrowserSerializedContinueConfig, | |
| 15 | + ) => BrowserSerializedContinueConfig; | |
| 16 | +} | |
| 17 | + | |
| 18 | +export function triggerConfigUpdate({ | |
| 19 | + store, | |
| 20 | + ideMessenger, | |
| 21 | + editConfig, | |
| 22 | + newOrgs, | |
| 23 | + newOrgId, | |
| 24 | + newProfileId, | |
| 25 | +}: TestConfigUpdateParams) { | |
| 26 | + const state = store.getState(); | |
| 27 | + ideMessenger.mockMessageToWebview("configUpdate", { | |
| 28 | + organizations: newOrgs ?? state.profiles.organizations, | |
| 29 | + selectedOrgId: newOrgId ?? state.profiles.selectedOrganizationId, | |
| 30 | + profileId: newProfileId ?? state.profiles.selectedProfileId, | |
| 31 | + result: { | |
| 32 | + config: editConfig | |
| 33 | + ? editConfig(copyOf(state.config.config)) | |
| 34 | + : state.config.config, | |
| 35 | + configLoadInterrupted: false, | |
| 36 | + errors: [], | |
| 37 | + }, | |
| 38 | + }); | |
| 39 | +} | |
| 40 | + | |
| 41 | +export function addAndSelectChatModel( | |
| 42 | + store: EnhancedStore, | |
| 43 | + ideMessenger: MockIdeMessenger, | |
| 44 | + llmDesc: ModelDescription, | |
| 45 | +) { | |
| 46 | + triggerConfigUpdate({ | |
| 47 | + store, | |
| 48 | + ideMessenger, | |
| 49 | + editConfig(current) { | |
| 50 | + current.modelsByRole.chat.push(llmDesc); | |
| 51 | + current.selectedModelByRole.chat = llmDesc; | |
| 52 | + return current; | |
| 53 | + }, | |
| 54 | + }); | |
| 55 | +} | |
| 56 | + | |
| 57 | +export function addAndSelectMockLlm( | |
| 58 | + store: EnhancedStore, | |
| 59 | + ideMessenger: MockIdeMessenger, | |
| 60 | +) { | |
| 61 | + addAndSelectChatModel(store, ideMessenger, { | |
| 62 | + model: "mock", | |
| 63 | + provider: "mock", | |
| 64 | + title: "Mock LLM", | |
| 65 | + underlyingProviderName: "mock", | |
| 66 | + }); | |
| 67 | +} |
added gui/src/util/test/utils.ts +102 −0
| @@ -0,0 +1,102 @@ | ||
| 1 | +import { screen, waitFor } from "@testing-library/dom"; | |
| 2 | +import { act } from "@testing-library/react"; | |
| 3 | +import { Editor } from "@tiptap/core"; | |
| 4 | +import { ChatMessage } from "core"; | |
| 5 | +import { MockIdeMessenger } from "../../context/MockIdeMessenger"; | |
| 6 | + | |
| 7 | +function logDomText() { | |
| 8 | + const bodyText = document.body.innerHTML; | |
| 9 | + const parser = new DOMParser(); | |
| 10 | + const doc = parser.parseFromString(bodyText, "text/html"); | |
| 11 | + let cleaned = doc.body.textContent ?? ""; | |
| 12 | + cleaned = cleaned?.replace( | |
| 13 | + "No resultsAaModelsRulesDocsPromptsToolsMCPCreate your first assistant", | |
| 14 | + "", | |
| 15 | + ); | |
| 16 | + cleaned = cleaned?.replace( | |
| 17 | + "Alt⏎ Active file⏎ Enter⏎Log in to access a free trial of theModels Add-OnLog in to Continue HubOr, configure your own models", | |
| 18 | + "", | |
| 19 | + ); | |
| 20 | + cleaned = cleaned.replaceAll("Select model", ""); | |
| 21 | + cleaned = cleaned.replaceAll("Mock LLM", ""); | |
| 22 | + cleaned = cleaned.replaceAll(/^Chat|Chat$/g, ""); | |
| 23 | + console.log(cleaned || "No body content"); | |
| 24 | +} | |
| 25 | + | |
| 26 | +export function logAllTestIds() { | |
| 27 | + const elements = document.querySelectorAll("*"); | |
| 28 | + | |
| 29 | + const testIds = Array.from(elements) | |
| 30 | + .map((el) => Array.from(el.attributes)) | |
| 31 | + .flat() | |
| 32 | + .filter((a) => a.name === "data-testid") | |
| 33 | + .map((a) => a.value); | |
| 34 | + console.log("Available test ids", testIds); | |
| 35 | +} | |
| 36 | +export async function getElementByTestId(testId: string): Promise<HTMLElement> { | |
| 37 | + try { | |
| 38 | + const testIdElement = await waitFor(() => screen.findByTestId(testId)); | |
| 39 | + expect(testIdElement).toBeInTheDocument(); | |
| 40 | + return testIdElement; | |
| 41 | + } catch (e) { | |
| 42 | + logAllTestIds(); | |
| 43 | + throw new Error(`Element with testId "${testId}" not found`); | |
| 44 | + } | |
| 45 | +} | |
| 46 | + | |
| 47 | +export async function verifyNotPresentByTestId(testId: string): Promise<void> { | |
| 48 | + const element = await waitFor(() => screen.queryByTestId(testId)); | |
| 49 | + expect(element).not.toBeInTheDocument(); | |
| 50 | +} | |
| 51 | + | |
| 52 | +export async function verifyNotPresentByText(text: string): Promise<void> { | |
| 53 | + const element = screen.queryByText(text); | |
| 54 | + expect(element).not.toBeInTheDocument(); | |
| 55 | +} | |
| 56 | + | |
| 57 | +export async function getElementByText(text: string): Promise<HTMLElement> { | |
| 58 | + try { | |
| 59 | + const textElement = await waitFor(() => screen.findByText(text)); | |
| 60 | + expect(textElement).toBeInTheDocument(); | |
| 61 | + return textElement; | |
| 62 | + } catch (e) { | |
| 63 | + logDomText(); | |
| 64 | + throw new Error(`Element with text "${text}" not found.`); | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +export async function getMainEditor(): Promise<Editor> { | |
| 69 | + const editorElement = await getElementByTestId("editor-input-main"); | |
| 70 | + if ("editor" in editorElement) { | |
| 71 | + return editorElement.editor as Editor; | |
| 72 | + } else { | |
| 73 | + throw new Error("Main editor not found within editor container"); | |
| 74 | + } | |
| 75 | +} | |
| 76 | + | |
| 77 | +export async function sendInputWithMockedResponse( | |
| 78 | + ideMessenger: MockIdeMessenger, | |
| 79 | + input: string, | |
| 80 | + response: ChatMessage[], | |
| 81 | +): Promise<void> { | |
| 82 | + // Setup the mock response before any actions | |
| 83 | + ideMessenger.chatResponse = response; | |
| 84 | + | |
| 85 | + // Wait for the editor to be in the document | |
| 86 | + const editor = await getMainEditor(); | |
| 87 | + | |
| 88 | + // Find the submit button | |
| 89 | + const sendButton = await getElementByTestId("submit-input-button"); | |
| 90 | + | |
| 91 | + // Insert content into the editor | |
| 92 | + await act(async () => { | |
| 93 | + editor.commands.insertContent(input); | |
| 94 | + }); | |
| 95 | + | |
| 96 | + await getElementByText(input); | |
| 97 | + | |
| 98 | + // Click the send button | |
| 99 | + await act(async () => { | |
| 100 | + sendButton.click(); | |
| 101 | + }); | |
| 102 | +} |
modified gui/vite.config.ts +13 −0
| @@ -32,5 +32,18 @@ | ||
| 32 | 32 | globals: true, |
| 33 | 33 | environment: "jsdom", |
| 34 | 34 | setupFiles: "./src/util/test/setupTests.ts", |
| 35 | + onConsoleLog(log, type) { | |
| 36 | + if (type === "stderr") { | |
| 37 | + if ( | |
| 38 | + [ | |
| 39 | + "contentEditable", | |
| 40 | + "An update to Chat inside a test was not wrapped in act", | |
| 41 | + ].some((text) => log.includes(text)) | |
| 42 | + ) { | |
| 43 | + return false; | |
| 44 | + } | |
| 45 | + } | |
| 46 | + return true; | |
| 47 | + }, | |
| 35 | 48 | }, |
| 36 | 49 | }); |