| 1 | import { JSONContent } from "@tiptap/core"; |
| 2 | import { InputModifiers } from "core"; |
| 3 | import { describe, expect, it, vi } from "vitest"; |
| 4 | import { createMockStore } from "../../util/test/mockStore"; |
| 5 | import { streamResponseThunk } from "./streamResponse"; |
| 6 | |
| 7 | // Mock external dependencies only - let selectors run naturally |
| 8 | // Removed: modelSupportsNativeTools - let it run naturally |
| 9 | |
| 10 | // Removed: addSystemMessageToolsToSystemMessage - let it run naturally |
| 11 | |
| 12 | // Mock system message construction to keep test readable |
| 13 | vi.mock("../util/getBaseSystemMessage", () => ({ |
| 14 | getBaseSystemMessage: vi.fn(), |
| 15 | })); |
| 16 | |
| 17 | import { getBaseSystemMessage } from "../util/getBaseSystemMessage"; |
| 18 | |
| 19 | // Removed: shouldAutoEnableSystemMessageTools - let it run naturally |
| 20 | |
| 21 | // Additional mocks for streamResponseThunk |
| 22 | vi.mock("posthog-js", () => ({ |
| 23 | default: { |
| 24 | capture: vi.fn(), |
| 25 | }, |
| 26 | })); |
| 27 | |
| 28 | vi.mock("uuid", () => ({ |
| 29 | v4: vi.fn(() => "mock-uuid-123"), |
| 30 | })); |
| 31 | |
| 32 | vi.mock( |
| 33 | "../../components/mainInput/TipTapEditor/utils/resolveEditorContent", |
| 34 | () => ({ |
| 35 | resolveEditorContent: vi.fn(), |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | import { ModelDescription } from "core"; |
| 40 | import posthog from "posthog-js"; |
| 41 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor/utils/resolveEditorContent"; |
| 42 | |
| 43 | const mockGetBaseSystemMessage = vi.mocked(getBaseSystemMessage); |
| 44 | |
| 45 | const mockPosthog = vi.mocked(posthog); |
| 46 | const mockResolveEditorContent = vi.mocked(resolveEditorContent); |
| 47 | |
| 48 | const mockClaudeModel: ModelDescription = { |
| 49 | title: "Claude 3.5 Sonnet", |
| 50 | model: "claude-3-5-sonnet-20241022", |
| 51 | provider: "anthropic", |
| 52 | underlyingProviderName: "anthropic", |
| 53 | completionOptions: { reasoningBudgetTokens: 2048 }, |
| 54 | }; |
| 55 | |
| 56 | // Mock editor state (what user types in the input) |
| 57 | const mockEditorState: JSONContent = { |
| 58 | type: "doc", |
| 59 | content: [ |
| 60 | { |
| 61 | type: "paragraph", |
| 62 | content: [{ type: "text", text: "Hello, please help me with this code" }], |
| 63 | }, |
| 64 | ], |
| 65 | }; |
| 66 | |
| 67 | // Mock input modifiers (codebase context, etc.) |
| 68 | const mockModifiers: InputModifiers = { |
| 69 | useCodebase: true, |
| 70 | noContext: false, |
| 71 | }; |
| 72 | |
| 73 | function setupTest() { |
| 74 | vi.clearAllMocks(); |
| 75 | |
| 76 | // Default mock implementations for external functions |
| 77 | |
| 78 | // Default mock for resolveEditorContent (can be overridden in individual tests) |
| 79 | mockResolveEditorContent.mockResolvedValue({ |
| 80 | selectedContextItems: [], |
| 81 | selectedCode: [], |
| 82 | content: "Hello, please help me with this code", |
| 83 | legacyCommandWithInput: undefined, |
| 84 | }); |
| 85 | |
| 86 | // Mock getBaseSystemMessage to return simple system message for readable tests |
| 87 | mockGetBaseSystemMessage.mockReturnValue("You are a helpful assistant."); |
| 88 | |
| 89 | // Create store with realistic state that selectors can work with |
| 90 | const mockStore = createMockStore({ |
| 91 | session: { |
| 92 | history: [ |
| 93 | { |
| 94 | message: { id: "1", role: "user", content: "Hello" }, |
| 95 | contextItems: [], |
| 96 | }, |
| 97 | ], |
| 98 | hasReasoningEnabled: false, |
| 99 | isStreaming: true, |
| 100 | id: "session-123", |
| 101 | mode: "chat", |
| 102 | streamAborter: new AbortController(), |
| 103 | contextPercentage: 0, |
| 104 | isPruned: false, |
| 105 | isInEdit: false, |
| 106 | title: "", |
| 107 | lastSessionId: undefined, |
| 108 | isSessionMetadataLoading: false, |
| 109 | allSessionMetadata: [], |
| 110 | symbols: {}, |
| 111 | codeBlockApplyStates: { |
| 112 | states: [], |
| 113 | curIndex: 0, |
| 114 | }, |
| 115 | newestToolbarPreviewForInput: {}, |
| 116 | compactionLoading: {}, |
| 117 | inlineErrorMessage: undefined, |
| 118 | }, |
| 119 | config: { |
| 120 | config: { |
| 121 | tools: [], |
| 122 | rules: [], |
| 123 | tabAutocompleteModel: undefined, |
| 124 | selectedModelByRole: { |
| 125 | chat: mockClaudeModel, |
| 126 | apply: null, |
| 127 | edit: null, |
| 128 | summarize: null, |
| 129 | autocomplete: null, |
| 130 | rerank: null, |
| 131 | embed: null, |
| 132 | }, |
| 133 | experimental: { |
| 134 | onlyUseSystemMessageTools: false, |
| 135 | }, |
| 136 | } as any, |
| 137 | lastSelectedModelByRole: { |
| 138 | chat: mockClaudeModel.title, |
| 139 | }, |
| 140 | } as any, |
| 141 | ui: { |
| 142 | toolSettings: {}, |
| 143 | ruleSettings: {}, |
| 144 | showDialog: false, |
| 145 | dialogMessage: undefined, |
| 146 | } as any, |
| 147 | }); |
| 148 | |
| 149 | const mockIdeMessenger = mockStore.mockIdeMessenger; |
| 150 | |
| 151 | return { mockStore, mockIdeMessenger }; |
| 152 | } |
| 153 | |
| 154 | describe("streamResponseThunk - tool calls", () => { |
| 155 | it("should execute streaming flow with tool call execution", async () => { |
| 156 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 157 | |
| 158 | // Set up auto-approved tool setting for our test tool |
| 159 | const mockStoreWithToolSettings = createMockStore({ |
| 160 | session: { |
| 161 | history: [ |
| 162 | { |
| 163 | message: { |
| 164 | id: "1", |
| 165 | role: "user", |
| 166 | content: "Please search the codebase", |
| 167 | }, |
| 168 | contextItems: [], |
| 169 | }, |
| 170 | ], |
| 171 | hasReasoningEnabled: false, |
| 172 | isStreaming: true, |
| 173 | id: "session-123", |
| 174 | mode: "chat", |
| 175 | streamAborter: new AbortController(), |
| 176 | contextPercentage: 0, |
| 177 | isPruned: false, |
| 178 | isInEdit: false, |
| 179 | title: "", |
| 180 | lastSessionId: undefined, |
| 181 | isSessionMetadataLoading: false, |
| 182 | allSessionMetadata: [], |
| 183 | symbols: {}, |
| 184 | codeBlockApplyStates: { |
| 185 | states: [], |
| 186 | curIndex: 0, |
| 187 | }, |
| 188 | newestToolbarPreviewForInput: {}, |
| 189 | compactionLoading: {}, |
| 190 | inlineErrorMessage: undefined, |
| 191 | }, |
| 192 | config: { |
| 193 | config: { |
| 194 | tools: [], |
| 195 | rules: [], |
| 196 | tabAutocompleteModel: undefined, |
| 197 | selectedModelByRole: { |
| 198 | chat: mockClaudeModel, |
| 199 | apply: null, |
| 200 | edit: null, |
| 201 | summarize: null, |
| 202 | autocomplete: null, |
| 203 | rerank: null, |
| 204 | embed: null, |
| 205 | }, |
| 206 | experimental: { |
| 207 | onlyUseSystemMessageTools: false, |
| 208 | }, |
| 209 | } as any, |
| 210 | lastSelectedModelByRole: { |
| 211 | chat: mockClaudeModel.title, |
| 212 | }, |
| 213 | } as any, |
| 214 | ui: { |
| 215 | toolSettings: { |
| 216 | search_codebase: "allowedWithoutPermission", // Auto-approve this tool |
| 217 | }, |
| 218 | ruleSettings: {}, |
| 219 | showDialog: false, |
| 220 | dialogMessage: undefined, |
| 221 | } as any, |
| 222 | }); |
| 223 | |
| 224 | const mockIdeMessengerWithTool = mockStoreWithToolSettings.mockIdeMessenger; |
| 225 | |
| 226 | // Setup successful compilation and tool responses |
| 227 | mockIdeMessengerWithTool.request.mockImplementation( |
| 228 | (endpoint: string, data: any) => { |
| 229 | if (endpoint === "llm/compileChat") { |
| 230 | return Promise.resolve({ |
| 231 | status: "success", |
| 232 | content: { |
| 233 | compiledChatMessages: [ |
| 234 | { role: "user", content: "Please search the codebase" }, |
| 235 | ], |
| 236 | didPrune: false, |
| 237 | contextPercentage: 0.9, |
| 238 | }, |
| 239 | }); |
| 240 | } else if (endpoint === "tools/call") { |
| 241 | // Mock server-side tool call response |
| 242 | return Promise.resolve({ |
| 243 | status: "success", |
| 244 | content: { |
| 245 | contextItems: [ |
| 246 | { |
| 247 | name: "Search Results", |
| 248 | description: "Found 3 matches", |
| 249 | content: "Result 1\nResult 2\nResult 3", |
| 250 | icon: "search", |
| 251 | hidden: false, |
| 252 | }, |
| 253 | ], |
| 254 | errorMessage: undefined, |
| 255 | }, |
| 256 | }); |
| 257 | } else if (endpoint === "history/save") { |
| 258 | // Mock session save |
| 259 | return Promise.resolve({ status: "success" }); |
| 260 | } else if (endpoint === "history/list") { |
| 261 | return Promise.resolve({ status: "success", content: [] }); |
| 262 | } |
| 263 | // Let other endpoints pass through or return success |
| 264 | return Promise.resolve({ status: "success", content: {} }); |
| 265 | }, |
| 266 | ); |
| 267 | |
| 268 | // Setup streaming generator with tool call |
| 269 | async function* mockStreamGeneratorWithTool() { |
| 270 | yield [ |
| 271 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 272 | ]; |
| 273 | yield [ |
| 274 | { |
| 275 | role: "assistant", |
| 276 | content: "", |
| 277 | toolCalls: [ |
| 278 | { |
| 279 | id: "tool-call-1", |
| 280 | type: "function", |
| 281 | function: { |
| 282 | name: "search_codebase", |
| 283 | arguments: JSON.stringify({ query: "test function" }), |
| 284 | }, |
| 285 | }, |
| 286 | ], |
| 287 | }, |
| 288 | ]; |
| 289 | return { |
| 290 | prompt: "Please search the codebase", |
| 291 | completion: "I'll search the codebase for you.", |
| 292 | modelProvider: "anthropic", |
| 293 | }; |
| 294 | } |
| 295 | |
| 296 | // Mock different streaming responses for multiple calls |
| 297 | let streamCallCount = 0; |
| 298 | mockIdeMessengerWithTool.llmStreamChat.mockImplementation(() => { |
| 299 | streamCallCount++; |
| 300 | if (streamCallCount === 1) { |
| 301 | // First call - main streaming with tool call |
| 302 | return mockStreamGeneratorWithTool(); |
| 303 | } else { |
| 304 | // Subsequent calls from streamResponseAfterToolCall - return minimal response |
| 305 | async function* simpleGenerator() { |
| 306 | yield [{ role: "assistant", content: "Search completed." }]; |
| 307 | return { |
| 308 | prompt: "continuing after tool", |
| 309 | completion: "Search completed.", |
| 310 | modelProvider: "anthropic", |
| 311 | }; |
| 312 | } |
| 313 | return simpleGenerator(); |
| 314 | } |
| 315 | }); |
| 316 | |
| 317 | // Track isStreaming state changes to detect UI flashing during auto-approved tool execution |
| 318 | const streamingStateChanges: boolean[] = []; |
| 319 | let lastStreamingState = |
| 320 | mockStoreWithToolSettings.getState().session.isStreaming; |
| 321 | // Record the initial state |
| 322 | streamingStateChanges.push(lastStreamingState); |
| 323 | |
| 324 | // Subscribe to store changes to catch ALL state updates |
| 325 | const unsubscribe = mockStoreWithToolSettings.subscribe(() => { |
| 326 | const currentState = |
| 327 | mockStoreWithToolSettings.getState().session.isStreaming; |
| 328 | if (currentState !== lastStreamingState) { |
| 329 | console.log( |
| 330 | `Store subscription: isStreaming changed from ${lastStreamingState} to ${currentState}`, |
| 331 | ); |
| 332 | streamingStateChanges.push(currentState); |
| 333 | lastStreamingState = currentState; |
| 334 | } |
| 335 | }); |
| 336 | |
| 337 | // Execute thunk |
| 338 | const result = await mockStoreWithToolSettings.dispatch( |
| 339 | streamResponseThunk({ |
| 340 | editorState: mockEditorState, |
| 341 | modifiers: mockModifiers, |
| 342 | }) as any, |
| 343 | ); |
| 344 | |
| 345 | // Debug: check what happened |
| 346 | const debugState = mockStoreWithToolSettings.getState(); |
| 347 | const finalStreamingState = debugState.session.isStreaming; |
| 348 | const toolCallStates = debugState.session.history.flatMap( |
| 349 | (item) => item.toolCallStates || [], |
| 350 | ); |
| 351 | |
| 352 | console.log("Initial streaming state:", lastStreamingState); |
| 353 | console.log("Final streaming state:", finalStreamingState); |
| 354 | console.log( |
| 355 | "Tool call states:", |
| 356 | toolCallStates.map((t) => ({ |
| 357 | id: t.toolCallId, |
| 358 | status: t.status, |
| 359 | toolName: t.toolCall.function.name, |
| 360 | })), |
| 361 | ); |
| 362 | console.log("Tool settings:", debugState.ui.toolSettings); |
| 363 | console.log( |
| 364 | "Auto-approved tool streaming state changes:", |
| 365 | streamingStateChanges, |
| 366 | ); |
| 367 | |
| 368 | // Unsubscribe from store updates |
| 369 | unsubscribe(); |
| 370 | |
| 371 | // Verify no UI flashing during auto-approved tool execution |
| 372 | // DESIRED: Should stay true throughout tool execution, only becoming false at the very end |
| 373 | // We expect [true, false] - streaming starts true, stays true during tool execution, then false when all done |
| 374 | expect(streamingStateChanges).toEqual([true, false]); |
| 375 | |
| 376 | // Verify key actions are dispatched (tool calls trigger a complex cascade, so we verify key actions exist) |
| 377 | const dispatchedActions = (mockStoreWithToolSettings as any).getActions(); |
| 378 | |
| 379 | // Verify exact action sequence by comparing action types |
| 380 | const actionTypes = dispatchedActions.map((action: any) => action.type); |
| 381 | expect(actionTypes).toEqual([ |
| 382 | "chat/streamResponse/pending", |
| 383 | "chat/streamWrapper/pending", |
| 384 | "session/submitEditorAndInitAtIndex", |
| 385 | "session/resetNextCodeBlockToApplyIndex", |
| 386 | "symbols/updateFromContextItems/pending", |
| 387 | "session/updateHistoryItemAtIndex", |
| 388 | "chat/streamNormalInput/pending", |
| 389 | "session/setAppliedRulesAtIndex", |
| 390 | "session/setActive", |
| 391 | "session/setInlineErrorMessage", |
| 392 | "session/setIsPruned", |
| 393 | "session/setContextPercentage", |
| 394 | "symbols/updateFromContextItems/fulfilled", |
| 395 | "session/streamUpdate", |
| 396 | "session/streamUpdate", |
| 397 | "session/addPromptCompletionPair", |
| 398 | "session/setToolGenerated", |
| 399 | "chat/callTool/pending", |
| 400 | "session/setToolCallCalling", |
| 401 | "session/updateToolCallOutput", |
| 402 | "session/acceptToolCall", |
| 403 | "chat/streamAfterToolCall/pending", |
| 404 | "chat/streamWrapper/pending", |
| 405 | "session/resetNextCodeBlockToApplyIndex", |
| 406 | "session/streamUpdate", |
| 407 | "chat/streamNormalInput/pending", |
| 408 | "session/setAppliedRulesAtIndex", |
| 409 | "session/setActive", |
| 410 | "session/setInlineErrorMessage", |
| 411 | "session/setIsPruned", |
| 412 | "session/setContextPercentage", |
| 413 | "session/streamUpdate", |
| 414 | "session/addPromptCompletionPair", |
| 415 | "session/setInactive", |
| 416 | "chat/streamNormalInput/fulfilled", |
| 417 | "session/saveCurrent/pending", |
| 418 | "session/update/pending", |
| 419 | "session/updateSessionMetadata", |
| 420 | "session/refreshMetadata/pending", |
| 421 | "session/setIsSessionMetadataLoading", |
| 422 | "session/setAllSessionMetadata", |
| 423 | "session/refreshMetadata/fulfilled", |
| 424 | "session/update/fulfilled", |
| 425 | "session/saveCurrent/fulfilled", |
| 426 | "chat/streamWrapper/fulfilled", |
| 427 | "chat/streamAfterToolCall/fulfilled", |
| 428 | "chat/callTool/fulfilled", |
| 429 | "chat/streamNormalInput/fulfilled", |
| 430 | "session/saveCurrent/pending", |
| 431 | "session/update/pending", |
| 432 | "session/updateSessionMetadata", |
| 433 | "session/refreshMetadata/pending", |
| 434 | "session/setIsSessionMetadataLoading", |
| 435 | "session/setAllSessionMetadata", |
| 436 | "session/refreshMetadata/fulfilled", |
| 437 | "session/update/fulfilled", |
| 438 | "session/saveCurrent/fulfilled", |
| 439 | "chat/streamWrapper/fulfilled", |
| 440 | "chat/streamResponse/fulfilled", |
| 441 | ]); |
| 442 | |
| 443 | // Verify key payload data for important actions |
| 444 | const setContextPercentageAction = dispatchedActions.find( |
| 445 | (a: any) => a.type === "session/setContextPercentage", |
| 446 | ); |
| 447 | expect(setContextPercentageAction.payload).toBe(0.9); |
| 448 | |
| 449 | const streamUpdates = dispatchedActions.filter( |
| 450 | (a: any) => a.type === "session/streamUpdate", |
| 451 | ); |
| 452 | expect(streamUpdates[0].payload).toEqual([ |
| 453 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 454 | ]); |
| 455 | expect(streamUpdates[1].payload).toEqual([ |
| 456 | { |
| 457 | role: "assistant", |
| 458 | content: "", |
| 459 | toolCalls: [ |
| 460 | { |
| 461 | id: "tool-call-1", |
| 462 | type: "function", |
| 463 | function: { |
| 464 | name: "search_codebase", |
| 465 | arguments: JSON.stringify({ query: "test function" }), |
| 466 | }, |
| 467 | }, |
| 468 | ], |
| 469 | }, |
| 470 | ]); |
| 471 | |
| 472 | const completionPairs = dispatchedActions.filter( |
| 473 | (a: any) => a.type === "session/addPromptCompletionPair", |
| 474 | ); |
| 475 | expect(completionPairs[0].payload).toEqual([ |
| 476 | { |
| 477 | completion: "I'll search the codebase for you.", |
| 478 | modelProvider: "anthropic", |
| 479 | prompt: "Please search the codebase", |
| 480 | }, |
| 481 | ]); |
| 482 | |
| 483 | const toolCallActions = dispatchedActions.filter( |
| 484 | (a: any) => a.type === "session/setToolCallCalling", |
| 485 | ); |
| 486 | expect(toolCallActions[0].payload).toEqual({ toolCallId: "tool-call-1" }); |
| 487 | |
| 488 | const toolOutputActions = dispatchedActions.filter( |
| 489 | (a: any) => a.type === "session/updateToolCallOutput", |
| 490 | ); |
| 491 | expect(toolOutputActions[0].payload).toEqual({ |
| 492 | toolCallId: "tool-call-1", |
| 493 | contextItems: [ |
| 494 | { |
| 495 | name: "Search Results", |
| 496 | description: "Found 3 matches", |
| 497 | content: "Result 1\nResult 2\nResult 3", |
| 498 | icon: "search", |
| 499 | hidden: false, |
| 500 | }, |
| 501 | ], |
| 502 | }); |
| 503 | |
| 504 | // Verify IDE messenger calls |
| 505 | expect(mockIdeMessengerWithTool.request).toHaveBeenCalledWith( |
| 506 | "llm/compileChat", |
| 507 | { |
| 508 | messages: [ |
| 509 | { |
| 510 | role: "system", |
| 511 | content: "You are a helpful assistant.", |
| 512 | }, |
| 513 | { |
| 514 | role: "user", |
| 515 | content: [ |
| 516 | { |
| 517 | type: "text", |
| 518 | text: "Please search the codebase", |
| 519 | }, |
| 520 | ], |
| 521 | }, |
| 522 | { |
| 523 | role: "user", |
| 524 | content: [ |
| 525 | { |
| 526 | type: "text", |
| 527 | text: "Hello, please help me with this code", |
| 528 | }, |
| 529 | ], |
| 530 | }, |
| 531 | ], |
| 532 | options: {}, |
| 533 | }, |
| 534 | ); |
| 535 | |
| 536 | expect(mockIdeMessengerWithTool.request).toHaveBeenCalledWith( |
| 537 | "tools/call", |
| 538 | { |
| 539 | toolCall: { |
| 540 | id: "tool-call-1", |
| 541 | type: "function", |
| 542 | function: { |
| 543 | name: "search_codebase", |
| 544 | arguments: JSON.stringify({ query: "test function" }), |
| 545 | }, |
| 546 | }, |
| 547 | }, |
| 548 | ); |
| 549 | |
| 550 | // Verify that multiple compilation calls were made (due to tool call continuation) |
| 551 | const compileCallsCount = |
| 552 | mockIdeMessengerWithTool.request.mock.calls.filter( |
| 553 | (call: any) => call[0] === "llm/compileChat", |
| 554 | ).length; |
| 555 | expect(compileCallsCount).toBeGreaterThanOrEqual(1); |
| 556 | |
| 557 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 558 | |
| 559 | // Verify telemetry events for auto-approved tool execution |
| 560 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_decision", { |
| 561 | decision: "auto_accept", |
| 562 | toolName: "search_codebase", |
| 563 | toolCallId: "tool-call-1", |
| 564 | }); |
| 565 | |
| 566 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_outcome", { |
| 567 | succeeded: true, |
| 568 | toolName: "search_codebase", |
| 569 | errorMessage: undefined, |
| 570 | duration_ms: expect.any(Number), |
| 571 | }); |
| 572 | |
| 573 | // Verify final state after tool call execution |
| 574 | const finalState = mockStoreWithToolSettings.getState(); |
| 575 | expect(finalState).toEqual({ |
| 576 | session: { |
| 577 | history: [ |
| 578 | { |
| 579 | contextItems: [], |
| 580 | message: { |
| 581 | id: "1", |
| 582 | role: "user", |
| 583 | content: "Please search the codebase", |
| 584 | }, |
| 585 | }, |
| 586 | { |
| 587 | appliedRules: [], |
| 588 | contextItems: [], |
| 589 | editorState: mockEditorState, |
| 590 | message: { |
| 591 | id: expect.any(String), |
| 592 | role: "user", |
| 593 | content: "Hello, please help me with this code", |
| 594 | }, |
| 595 | }, |
| 596 | { |
| 597 | contextItems: [], |
| 598 | message: { |
| 599 | content: "I'll search the codebase for you.", |
| 600 | id: expect.any(String), |
| 601 | role: "assistant", |
| 602 | toolCalls: [ |
| 603 | { |
| 604 | id: "tool-call-1", |
| 605 | type: "function", |
| 606 | function: { |
| 607 | name: "search_codebase", |
| 608 | arguments: JSON.stringify({ query: "test function" }), |
| 609 | }, |
| 610 | }, |
| 611 | ], |
| 612 | }, |
| 613 | promptLogs: [ |
| 614 | { |
| 615 | completion: "I'll search the codebase for you.", |
| 616 | modelProvider: "anthropic", |
| 617 | prompt: "Please search the codebase", |
| 618 | }, |
| 619 | ], |
| 620 | toolCallStates: [ |
| 621 | { |
| 622 | toolCallId: "tool-call-1", |
| 623 | toolCall: { |
| 624 | id: "tool-call-1", |
| 625 | type: "function", |
| 626 | function: { |
| 627 | name: "search_codebase", |
| 628 | arguments: JSON.stringify({ query: "test function" }), |
| 629 | }, |
| 630 | }, |
| 631 | parsedArgs: { query: "test function" }, |
| 632 | status: "done", |
| 633 | output: [ |
| 634 | { |
| 635 | name: "Search Results", |
| 636 | description: "Found 3 matches", |
| 637 | content: "Result 1\nResult 2\nResult 3", |
| 638 | icon: "search", |
| 639 | hidden: false, |
| 640 | }, |
| 641 | ], |
| 642 | }, |
| 643 | ], |
| 644 | }, |
| 645 | { |
| 646 | contextItems: [], |
| 647 | message: { |
| 648 | content: "Result 1\nResult 2\nResult 3", |
| 649 | id: expect.any(String), |
| 650 | role: "tool", |
| 651 | toolCallId: "tool-call-1", |
| 652 | }, |
| 653 | }, |
| 654 | { |
| 655 | contextItems: [], |
| 656 | isGatheringContext: false, |
| 657 | message: { |
| 658 | content: "Search completed.", |
| 659 | id: "mock-uuid-123", |
| 660 | role: "assistant", |
| 661 | }, |
| 662 | promptLogs: [ |
| 663 | { |
| 664 | completion: "Search completed.", |
| 665 | modelProvider: "anthropic", |
| 666 | prompt: "continuing after tool", |
| 667 | }, |
| 668 | ], |
| 669 | }, |
| 670 | ], |
| 671 | hasReasoningEnabled: false, |
| 672 | isStreaming: false, |
| 673 | id: "session-123", |
| 674 | mode: "chat", |
| 675 | streamAborter: expect.any(AbortController), |
| 676 | contextPercentage: 0.9, |
| 677 | isPruned: false, |
| 678 | isInEdit: false, |
| 679 | title: "New Session", |
| 680 | lastSessionId: undefined, |
| 681 | isSessionMetadataLoading: false, |
| 682 | allSessionMetadata: [], |
| 683 | symbols: {}, |
| 684 | codeBlockApplyStates: { |
| 685 | states: [], |
| 686 | curIndex: 0, |
| 687 | }, |
| 688 | newestToolbarPreviewForInput: {}, |
| 689 | compactionLoading: {}, |
| 690 | inlineErrorMessage: undefined, |
| 691 | }, |
| 692 | config: { |
| 693 | config: { |
| 694 | tools: [], |
| 695 | rules: [], |
| 696 | tabAutocompleteModel: undefined, |
| 697 | selectedModelByRole: { |
| 698 | chat: mockClaudeModel, |
| 699 | apply: null, |
| 700 | edit: null, |
| 701 | summarize: null, |
| 702 | autocomplete: null, |
| 703 | rerank: null, |
| 704 | embed: null, |
| 705 | }, |
| 706 | experimental: { |
| 707 | onlyUseSystemMessageTools: false, |
| 708 | }, |
| 709 | }, |
| 710 | lastSelectedModelByRole: { |
| 711 | chat: mockClaudeModel.title, |
| 712 | }, |
| 713 | loading: false, |
| 714 | configError: undefined, |
| 715 | }, |
| 716 | ui: { |
| 717 | toolSettings: { |
| 718 | search_codebase: "allowedWithoutPermission", |
| 719 | }, |
| 720 | ruleSettings: {}, |
| 721 | showDialog: false, |
| 722 | dialogMessage: undefined, |
| 723 | }, |
| 724 | editModeState: { |
| 725 | isInEdit: false, |
| 726 | returnToMode: "chat", |
| 727 | }, |
| 728 | indexing: { |
| 729 | indexingState: "disabled", |
| 730 | }, |
| 731 | tabs: { |
| 732 | tabsItems: [], |
| 733 | }, |
| 734 | profiles: { |
| 735 | profiles: [], |
| 736 | }, |
| 737 | }); |
| 738 | }); |
| 739 | |
| 740 | it("should handle tool call rejection error", async () => { |
| 741 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 742 | |
| 743 | // Create store with tool settings that will reject the tool call |
| 744 | const mockStoreWithToolReject = createMockStore({ |
| 745 | session: { |
| 746 | history: [ |
| 747 | { |
| 748 | message: { |
| 749 | id: "1", |
| 750 | role: "user", |
| 751 | content: "Please edit this file", |
| 752 | }, |
| 753 | contextItems: [], |
| 754 | }, |
| 755 | ], |
| 756 | hasReasoningEnabled: false, |
| 757 | isStreaming: true, |
| 758 | id: "session-123", |
| 759 | mode: "chat", |
| 760 | streamAborter: new AbortController(), |
| 761 | contextPercentage: 0, |
| 762 | isPruned: false, |
| 763 | isInEdit: false, |
| 764 | title: "", |
| 765 | lastSessionId: undefined, |
| 766 | isSessionMetadataLoading: false, |
| 767 | allSessionMetadata: [], |
| 768 | symbols: {}, |
| 769 | codeBlockApplyStates: { |
| 770 | states: [], |
| 771 | curIndex: 0, |
| 772 | }, |
| 773 | newestToolbarPreviewForInput: {}, |
| 774 | compactionLoading: {}, |
| 775 | inlineErrorMessage: undefined, |
| 776 | }, |
| 777 | config: { |
| 778 | config: { |
| 779 | tools: [], |
| 780 | rules: [], |
| 781 | tabAutocompleteModel: undefined, |
| 782 | selectedModelByRole: { |
| 783 | chat: mockClaudeModel, |
| 784 | apply: null, |
| 785 | edit: null, |
| 786 | summarize: null, |
| 787 | autocomplete: null, |
| 788 | rerank: null, |
| 789 | embed: null, |
| 790 | }, |
| 791 | experimental: { |
| 792 | onlyUseSystemMessageTools: false, |
| 793 | }, |
| 794 | } as any, |
| 795 | lastSelectedModelByRole: { |
| 796 | chat: mockClaudeModel.title, |
| 797 | }, |
| 798 | } as any, |
| 799 | ui: { |
| 800 | toolSettings: { |
| 801 | edit_existing_file: "disabled", // Tool is disabled/rejected |
| 802 | }, |
| 803 | ruleSettings: {}, |
| 804 | showDialog: false, |
| 805 | dialogMessage: undefined, |
| 806 | } as any, |
| 807 | }); |
| 808 | |
| 809 | const mockIdeMessengerReject = mockStoreWithToolReject.mockIdeMessenger; |
| 810 | |
| 811 | // Setup successful compilation |
| 812 | mockIdeMessengerReject.request.mockResolvedValue({ |
| 813 | status: "success", |
| 814 | content: { |
| 815 | compiledChatMessages: [ |
| 816 | { role: "user", content: "Please edit this file" }, |
| 817 | ], |
| 818 | didPrune: false, |
| 819 | contextPercentage: 0.7, |
| 820 | }, |
| 821 | }); |
| 822 | |
| 823 | // Setup streaming generator with tool call that will be rejected |
| 824 | async function* mockStreamGeneratorWithRejectedTool() { |
| 825 | yield [{ role: "assistant", content: "I'll edit the file for you." }]; |
| 826 | yield [ |
| 827 | { |
| 828 | role: "assistant", |
| 829 | content: "", |
| 830 | toolCalls: [ |
| 831 | { |
| 832 | id: "tool-reject-1", |
| 833 | type: "function", |
| 834 | function: { |
| 835 | name: "edit_existing_file", |
| 836 | arguments: JSON.stringify({ |
| 837 | filepath: "test.js", |
| 838 | changes: "const x = 1;", |
| 839 | }), |
| 840 | }, |
| 841 | }, |
| 842 | ], |
| 843 | }, |
| 844 | ]; |
| 845 | return { |
| 846 | prompt: "Please edit this file", |
| 847 | completion: "I'll edit the file for you.", |
| 848 | modelProvider: "anthropic", |
| 849 | }; |
| 850 | } |
| 851 | |
| 852 | mockIdeMessengerReject.llmStreamChat.mockReturnValue( |
| 853 | mockStreamGeneratorWithRejectedTool(), |
| 854 | ); |
| 855 | |
| 856 | // Execute thunk |
| 857 | const result = await mockStoreWithToolReject.dispatch( |
| 858 | streamResponseThunk({ |
| 859 | editorState: mockEditorState, |
| 860 | modifiers: mockModifiers, |
| 861 | }) as any, |
| 862 | ); |
| 863 | |
| 864 | // Verify thunk completed successfully (tool rejection is handled gracefully) |
| 865 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 866 | |
| 867 | // Verify exact action sequence includes tool generation but rejection handling |
| 868 | const dispatchedActions = (mockStoreWithToolReject as any).getActions(); |
| 869 | expect(dispatchedActions).toEqual([ |
| 870 | { |
| 871 | type: "chat/streamResponse/pending", |
| 872 | meta: { |
| 873 | arg: { |
| 874 | editorState: mockEditorState, |
| 875 | modifiers: mockModifiers, |
| 876 | }, |
| 877 | requestId: expect.any(String), |
| 878 | requestStatus: "pending", |
| 879 | }, |
| 880 | payload: undefined, |
| 881 | }, |
| 882 | { |
| 883 | type: "chat/streamWrapper/pending", |
| 884 | meta: { |
| 885 | arg: expect.any(Function), |
| 886 | requestId: expect.any(String), |
| 887 | requestStatus: "pending", |
| 888 | }, |
| 889 | payload: undefined, |
| 890 | }, |
| 891 | { |
| 892 | type: "session/submitEditorAndInitAtIndex", |
| 893 | payload: { |
| 894 | editorState: mockEditorState, |
| 895 | index: 1, |
| 896 | }, |
| 897 | }, |
| 898 | { |
| 899 | type: "session/resetNextCodeBlockToApplyIndex", |
| 900 | payload: undefined, |
| 901 | }, |
| 902 | { |
| 903 | type: "symbols/updateFromContextItems/pending", |
| 904 | meta: { |
| 905 | arg: [], |
| 906 | requestId: expect.any(String), |
| 907 | requestStatus: "pending", |
| 908 | }, |
| 909 | payload: undefined, |
| 910 | }, |
| 911 | { |
| 912 | type: "session/updateHistoryItemAtIndex", |
| 913 | payload: { |
| 914 | index: 1, |
| 915 | updates: { |
| 916 | contextItems: [], |
| 917 | message: { |
| 918 | content: "Hello, please help me with this code", |
| 919 | id: "mock-uuid-123", |
| 920 | role: "user", |
| 921 | }, |
| 922 | }, |
| 923 | }, |
| 924 | }, |
| 925 | { |
| 926 | type: "chat/streamNormalInput/pending", |
| 927 | meta: { |
| 928 | arg: { |
| 929 | legacySlashCommandData: undefined, |
| 930 | }, |
| 931 | requestId: expect.any(String), |
| 932 | requestStatus: "pending", |
| 933 | }, |
| 934 | payload: undefined, |
| 935 | }, |
| 936 | { |
| 937 | type: "session/setAppliedRulesAtIndex", |
| 938 | payload: { |
| 939 | appliedRules: [], |
| 940 | index: 1, |
| 941 | }, |
| 942 | }, |
| 943 | { |
| 944 | type: "session/setActive", |
| 945 | payload: undefined, |
| 946 | }, |
| 947 | { |
| 948 | type: "session/setInlineErrorMessage", |
| 949 | payload: undefined, |
| 950 | }, |
| 951 | { |
| 952 | type: "session/setIsPruned", |
| 953 | payload: false, |
| 954 | }, |
| 955 | { |
| 956 | type: "session/setContextPercentage", |
| 957 | payload: 0.7, |
| 958 | }, |
| 959 | { |
| 960 | type: "symbols/updateFromContextItems/fulfilled", |
| 961 | meta: { |
| 962 | arg: [], |
| 963 | requestId: expect.any(String), |
| 964 | requestStatus: "fulfilled", |
| 965 | }, |
| 966 | payload: undefined, |
| 967 | }, |
| 968 | { |
| 969 | type: "session/streamUpdate", |
| 970 | payload: [ |
| 971 | { |
| 972 | role: "assistant", |
| 973 | content: "I'll edit the file for you.", |
| 974 | }, |
| 975 | ], |
| 976 | }, |
| 977 | { |
| 978 | type: "session/streamUpdate", |
| 979 | payload: [ |
| 980 | { |
| 981 | role: "assistant", |
| 982 | content: "", |
| 983 | toolCalls: [ |
| 984 | { |
| 985 | id: "tool-reject-1", |
| 986 | type: "function", |
| 987 | function: { |
| 988 | name: "edit_existing_file", |
| 989 | arguments: '{"filepath":"test.js","changes":"const x = 1;"}', |
| 990 | }, |
| 991 | }, |
| 992 | ], |
| 993 | }, |
| 994 | ], |
| 995 | }, |
| 996 | { |
| 997 | type: "session/addPromptCompletionPair", |
| 998 | payload: [ |
| 999 | { |
| 1000 | completion: "I'll edit the file for you.", |
| 1001 | modelProvider: "anthropic", |
| 1002 | prompt: "Please edit this file", |
| 1003 | }, |
| 1004 | ], |
| 1005 | }, |
| 1006 | { |
| 1007 | type: "session/setInactive", |
| 1008 | payload: undefined, |
| 1009 | }, |
| 1010 | { |
| 1011 | type: "session/setToolGenerated", |
| 1012 | payload: { |
| 1013 | toolCallId: "tool-reject-1", |
| 1014 | tools: [], |
| 1015 | }, |
| 1016 | }, |
| 1017 | { |
| 1018 | type: "chat/streamNormalInput/fulfilled", |
| 1019 | meta: { |
| 1020 | arg: { |
| 1021 | legacySlashCommandData: undefined, |
| 1022 | }, |
| 1023 | requestId: expect.any(String), |
| 1024 | requestStatus: "fulfilled", |
| 1025 | }, |
| 1026 | payload: undefined, |
| 1027 | }, |
| 1028 | { |
| 1029 | type: "session/saveCurrent/pending", |
| 1030 | meta: { |
| 1031 | arg: { |
| 1032 | generateTitle: true, |
| 1033 | openNewSession: false, |
| 1034 | }, |
| 1035 | requestId: expect.any(String), |
| 1036 | requestStatus: "pending", |
| 1037 | }, |
| 1038 | payload: undefined, |
| 1039 | }, |
| 1040 | { |
| 1041 | type: "session/update/pending", |
| 1042 | meta: { |
| 1043 | arg: expect.objectContaining({ |
| 1044 | history: expect.any(Array), |
| 1045 | sessionId: "session-123", |
| 1046 | title: "New Session", |
| 1047 | workspaceDirectory: "", |
| 1048 | }), |
| 1049 | requestId: expect.any(String), |
| 1050 | requestStatus: "pending", |
| 1051 | }, |
| 1052 | payload: undefined, |
| 1053 | }, |
| 1054 | { |
| 1055 | type: "session/updateSessionMetadata", |
| 1056 | payload: { |
| 1057 | sessionId: "session-123", |
| 1058 | title: "New Session", |
| 1059 | }, |
| 1060 | }, |
| 1061 | { |
| 1062 | type: "session/refreshMetadata/pending", |
| 1063 | meta: { |
| 1064 | arg: {}, |
| 1065 | requestId: expect.any(String), |
| 1066 | requestStatus: "pending", |
| 1067 | }, |
| 1068 | payload: undefined, |
| 1069 | }, |
| 1070 | { |
| 1071 | type: "session/setIsSessionMetadataLoading", |
| 1072 | payload: false, |
| 1073 | }, |
| 1074 | { |
| 1075 | type: "session/setAllSessionMetadata", |
| 1076 | payload: { |
| 1077 | compiledChatMessages: [ |
| 1078 | { |
| 1079 | content: "Please edit this file", |
| 1080 | role: "user", |
| 1081 | }, |
| 1082 | ], |
| 1083 | contextPercentage: 0.7, |
| 1084 | didPrune: false, |
| 1085 | }, |
| 1086 | }, |
| 1087 | { |
| 1088 | type: "session/refreshMetadata/fulfilled", |
| 1089 | meta: { |
| 1090 | arg: {}, |
| 1091 | requestId: expect.any(String), |
| 1092 | requestStatus: "fulfilled", |
| 1093 | }, |
| 1094 | payload: { |
| 1095 | compiledChatMessages: [ |
| 1096 | { |
| 1097 | content: "Please edit this file", |
| 1098 | role: "user", |
| 1099 | }, |
| 1100 | ], |
| 1101 | contextPercentage: 0.7, |
| 1102 | didPrune: false, |
| 1103 | }, |
| 1104 | }, |
| 1105 | { |
| 1106 | type: "session/update/fulfilled", |
| 1107 | meta: { |
| 1108 | arg: expect.objectContaining({ |
| 1109 | history: expect.any(Array), |
| 1110 | sessionId: "session-123", |
| 1111 | title: "New Session", |
| 1112 | workspaceDirectory: "", |
| 1113 | }), |
| 1114 | requestId: expect.any(String), |
| 1115 | requestStatus: "fulfilled", |
| 1116 | }, |
| 1117 | payload: undefined, |
| 1118 | }, |
| 1119 | { |
| 1120 | type: "session/saveCurrent/fulfilled", |
| 1121 | meta: { |
| 1122 | arg: { |
| 1123 | generateTitle: true, |
| 1124 | openNewSession: false, |
| 1125 | }, |
| 1126 | requestId: expect.any(String), |
| 1127 | requestStatus: "fulfilled", |
| 1128 | }, |
| 1129 | payload: undefined, |
| 1130 | }, |
| 1131 | { |
| 1132 | type: "chat/streamWrapper/fulfilled", |
| 1133 | meta: { |
| 1134 | arg: expect.any(Function), |
| 1135 | requestId: expect.any(String), |
| 1136 | requestStatus: "fulfilled", |
| 1137 | }, |
| 1138 | payload: undefined, |
| 1139 | }, |
| 1140 | { |
| 1141 | type: "chat/streamResponse/fulfilled", |
| 1142 | meta: { |
| 1143 | arg: { |
| 1144 | editorState: mockEditorState, |
| 1145 | modifiers: mockModifiers, |
| 1146 | }, |
| 1147 | requestId: expect.any(String), |
| 1148 | requestStatus: "fulfilled", |
| 1149 | }, |
| 1150 | payload: undefined, |
| 1151 | }, |
| 1152 | ]); |
| 1153 | |
| 1154 | // Verify IDE messenger calls - compilation should succeed, streaming should happen |
| 1155 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1156 | "llm/compileChat", |
| 1157 | { |
| 1158 | messages: [ |
| 1159 | { |
| 1160 | role: "system", |
| 1161 | content: "You are a helpful assistant.", |
| 1162 | }, |
| 1163 | { |
| 1164 | role: "user", |
| 1165 | content: [ |
| 1166 | { |
| 1167 | type: "text", |
| 1168 | text: "Please edit this file", |
| 1169 | }, |
| 1170 | ], |
| 1171 | }, |
| 1172 | { |
| 1173 | role: "user", |
| 1174 | content: [ |
| 1175 | { |
| 1176 | type: "text", |
| 1177 | text: "Hello, please help me with this code", |
| 1178 | }, |
| 1179 | ], |
| 1180 | }, |
| 1181 | ], |
| 1182 | options: {}, |
| 1183 | }, |
| 1184 | ); |
| 1185 | |
| 1186 | expect(mockIdeMessengerReject.llmStreamChat).toHaveBeenCalledWith( |
| 1187 | { |
| 1188 | completionOptions: {}, |
| 1189 | legacySlashCommandData: undefined, |
| 1190 | messageOptions: { precompiled: true }, |
| 1191 | messages: [ |
| 1192 | { |
| 1193 | role: "user", |
| 1194 | content: "Please edit this file", |
| 1195 | }, |
| 1196 | ], |
| 1197 | title: "Claude 3.5 Sonnet", |
| 1198 | }, |
| 1199 | expect.any(AbortSignal), |
| 1200 | ); |
| 1201 | |
| 1202 | // Should NOT call tools/call since tool is disabled/rejected |
| 1203 | expect(mockIdeMessengerReject.request).not.toHaveBeenCalledWith( |
| 1204 | "tools/call", |
| 1205 | expect.anything(), |
| 1206 | ); |
| 1207 | |
| 1208 | // Verify session save was called |
| 1209 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1210 | "history/save", |
| 1211 | expect.anything(), |
| 1212 | ); |
| 1213 | |
| 1214 | // Verify final state contains the tool call but it's not executed |
| 1215 | const finalState = mockStoreWithToolReject.getState(); |
| 1216 | expect(finalState).toEqual({ |
| 1217 | session: { |
| 1218 | history: [ |
| 1219 | { |
| 1220 | contextItems: [], |
| 1221 | message: { |
| 1222 | id: "1", |
| 1223 | role: "user", |
| 1224 | content: "Please edit this file", |
| 1225 | }, |
| 1226 | }, |
| 1227 | { |
| 1228 | appliedRules: [], |
| 1229 | contextItems: [], |
| 1230 | editorState: mockEditorState, |
| 1231 | message: { |
| 1232 | content: "Hello, please help me with this code", |
| 1233 | id: expect.any(String), |
| 1234 | role: "user", |
| 1235 | }, |
| 1236 | }, |
| 1237 | { |
| 1238 | contextItems: [], |
| 1239 | isGatheringContext: false, |
| 1240 | message: { |
| 1241 | content: "I'll edit the file for you.", |
| 1242 | id: "mock-uuid-123", |
| 1243 | role: "assistant", |
| 1244 | toolCalls: [ |
| 1245 | { |
| 1246 | id: "tool-reject-1", |
| 1247 | type: "function", |
| 1248 | function: { |
| 1249 | name: "edit_existing_file", |
| 1250 | arguments: JSON.stringify({ |
| 1251 | filepath: "test.js", |
| 1252 | changes: "const x = 1;", |
| 1253 | }), |
| 1254 | }, |
| 1255 | }, |
| 1256 | ], |
| 1257 | }, |
| 1258 | promptLogs: [ |
| 1259 | { |
| 1260 | completion: "I'll edit the file for you.", |
| 1261 | modelProvider: "anthropic", |
| 1262 | prompt: "Please edit this file", |
| 1263 | }, |
| 1264 | ], |
| 1265 | toolCallStates: [ |
| 1266 | { |
| 1267 | toolCallId: "tool-reject-1", |
| 1268 | toolCall: { |
| 1269 | id: "tool-reject-1", |
| 1270 | type: "function", |
| 1271 | function: { |
| 1272 | name: "edit_existing_file", |
| 1273 | arguments: JSON.stringify({ |
| 1274 | filepath: "test.js", |
| 1275 | changes: "const x = 1;", |
| 1276 | }), |
| 1277 | }, |
| 1278 | }, |
| 1279 | parsedArgs: { |
| 1280 | filepath: "test.js", |
| 1281 | changes: "const x = 1;", |
| 1282 | }, |
| 1283 | status: "generated", // Tool call exists but isn't executed due to settings |
| 1284 | }, |
| 1285 | ], |
| 1286 | }, |
| 1287 | ], |
| 1288 | hasReasoningEnabled: false, |
| 1289 | isStreaming: false, |
| 1290 | id: "session-123", |
| 1291 | mode: "chat", |
| 1292 | streamAborter: expect.any(AbortController), |
| 1293 | contextPercentage: 0.7, |
| 1294 | isPruned: false, |
| 1295 | isInEdit: false, |
| 1296 | title: "New Session", |
| 1297 | lastSessionId: undefined, |
| 1298 | isSessionMetadataLoading: false, |
| 1299 | allSessionMetadata: { |
| 1300 | compiledChatMessages: [ |
| 1301 | { |
| 1302 | content: "Please edit this file", |
| 1303 | role: "user", |
| 1304 | }, |
| 1305 | ], |
| 1306 | contextPercentage: 0.7, |
| 1307 | didPrune: false, |
| 1308 | }, |
| 1309 | symbols: {}, |
| 1310 | codeBlockApplyStates: { |
| 1311 | states: [], |
| 1312 | curIndex: 0, |
| 1313 | }, |
| 1314 | newestToolbarPreviewForInput: {}, |
| 1315 | compactionLoading: {}, |
| 1316 | inlineErrorMessage: undefined, |
| 1317 | }, |
| 1318 | config: { |
| 1319 | config: { |
| 1320 | tools: [], |
| 1321 | rules: [], |
| 1322 | tabAutocompleteModel: undefined, |
| 1323 | selectedModelByRole: { |
| 1324 | chat: mockClaudeModel, |
| 1325 | apply: null, |
| 1326 | edit: null, |
| 1327 | summarize: null, |
| 1328 | autocomplete: null, |
| 1329 | rerank: null, |
| 1330 | embed: null, |
| 1331 | }, |
| 1332 | experimental: { |
| 1333 | onlyUseSystemMessageTools: false, |
| 1334 | }, |
| 1335 | }, |
| 1336 | lastSelectedModelByRole: { |
| 1337 | chat: mockClaudeModel.title, |
| 1338 | }, |
| 1339 | loading: false, |
| 1340 | configError: undefined, |
| 1341 | }, |
| 1342 | ui: { |
| 1343 | toolSettings: { |
| 1344 | edit_existing_file: "disabled", |
| 1345 | }, |
| 1346 | ruleSettings: {}, |
| 1347 | showDialog: false, |
| 1348 | dialogMessage: undefined, |
| 1349 | }, |
| 1350 | editModeState: { |
| 1351 | isInEdit: false, |
| 1352 | returnToMode: "chat", |
| 1353 | }, |
| 1354 | indexing: { |
| 1355 | indexingState: "disabled", |
| 1356 | }, |
| 1357 | tabs: { |
| 1358 | tabsItems: [], |
| 1359 | }, |
| 1360 | profiles: { |
| 1361 | profiles: [], |
| 1362 | }, |
| 1363 | }); |
| 1364 | }); |
| 1365 | |
| 1366 | it("should handle tool call requiring manual approval", async () => { |
| 1367 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 1368 | |
| 1369 | // Create store with tool settings that require manual approval (ask first) |
| 1370 | const mockStoreWithManualApproval = createMockStore({ |
| 1371 | session: { |
| 1372 | history: [ |
| 1373 | { |
| 1374 | message: { |
| 1375 | id: "1", |
| 1376 | role: "user", |
| 1377 | content: "Please search the codebase", |
| 1378 | }, |
| 1379 | contextItems: [], |
| 1380 | }, |
| 1381 | ], |
| 1382 | hasReasoningEnabled: false, |
| 1383 | isStreaming: true, |
| 1384 | id: "session-123", |
| 1385 | mode: "chat", |
| 1386 | streamAborter: new AbortController(), |
| 1387 | contextPercentage: 0, |
| 1388 | isPruned: false, |
| 1389 | isInEdit: false, |
| 1390 | title: "", |
| 1391 | lastSessionId: undefined, |
| 1392 | isSessionMetadataLoading: false, |
| 1393 | allSessionMetadata: [], |
| 1394 | symbols: {}, |
| 1395 | codeBlockApplyStates: { |
| 1396 | states: [], |
| 1397 | curIndex: 0, |
| 1398 | }, |
| 1399 | newestToolbarPreviewForInput: {}, |
| 1400 | compactionLoading: {}, |
| 1401 | inlineErrorMessage: undefined, |
| 1402 | }, |
| 1403 | config: { |
| 1404 | config: { |
| 1405 | tools: [], |
| 1406 | rules: [], |
| 1407 | tabAutocompleteModel: undefined, |
| 1408 | selectedModelByRole: { |
| 1409 | chat: mockClaudeModel, |
| 1410 | apply: null, |
| 1411 | edit: null, |
| 1412 | summarize: null, |
| 1413 | autocomplete: null, |
| 1414 | rerank: null, |
| 1415 | embed: null, |
| 1416 | }, |
| 1417 | experimental: { |
| 1418 | onlyUseSystemMessageTools: false, |
| 1419 | }, |
| 1420 | } as any, |
| 1421 | lastSelectedModelByRole: { |
| 1422 | chat: mockClaudeModel.title, |
| 1423 | }, |
| 1424 | } as any, |
| 1425 | ui: { |
| 1426 | toolSettings: { |
| 1427 | search_codebase: "askFirst", // Requires manual approval |
| 1428 | }, |
| 1429 | ruleSettings: {}, |
| 1430 | showDialog: false, |
| 1431 | dialogMessage: undefined, |
| 1432 | } as any, |
| 1433 | }); |
| 1434 | |
| 1435 | const mockIdeMessengerManual = mockStoreWithManualApproval.mockIdeMessenger; |
| 1436 | |
| 1437 | // Setup successful compilation |
| 1438 | mockIdeMessengerManual.request.mockResolvedValue({ |
| 1439 | status: "success", |
| 1440 | content: { |
| 1441 | compiledChatMessages: [ |
| 1442 | { role: "user", content: "Please search the codebase" }, |
| 1443 | ], |
| 1444 | didPrune: false, |
| 1445 | contextPercentage: 0.9, |
| 1446 | }, |
| 1447 | }); |
| 1448 | |
| 1449 | // Setup streaming generator with tool call requiring approval |
| 1450 | async function* mockStreamGeneratorWithApprovalTool() { |
| 1451 | yield [ |
| 1452 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 1453 | ]; |
| 1454 | yield [ |
| 1455 | { |
| 1456 | role: "assistant", |
| 1457 | content: "", |
| 1458 | toolCalls: [ |
| 1459 | { |
| 1460 | id: "tool-approval-1", |
| 1461 | type: "function", |
| 1462 | function: { |
| 1463 | name: "search_codebase", |
| 1464 | arguments: JSON.stringify({ query: "test function" }), |
| 1465 | }, |
| 1466 | }, |
| 1467 | ], |
| 1468 | }, |
| 1469 | ]; |
| 1470 | return { |
| 1471 | prompt: "Please search the codebase", |
| 1472 | completion: "I'll search the codebase for you.", |
| 1473 | modelProvider: "anthropic", |
| 1474 | }; |
| 1475 | } |
| 1476 | |
| 1477 | mockIdeMessengerManual.llmStreamChat.mockReturnValue( |
| 1478 | mockStreamGeneratorWithApprovalTool(), |
| 1479 | ); |
| 1480 | |
| 1481 | // Track isStreaming state changes to ensure it doesn't become false after tool generation |
| 1482 | const streamingStates: boolean[] = []; |
| 1483 | const originalDispatch = mockStoreWithManualApproval.dispatch; |
| 1484 | mockStoreWithManualApproval.dispatch = ((action: any) => { |
| 1485 | const result = originalDispatch(action); |
| 1486 | // Capture isStreaming state after each action |
| 1487 | const currentState = |
| 1488 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1489 | streamingStates.push(currentState); |
| 1490 | return result; |
| 1491 | }) as any; |
| 1492 | |
| 1493 | // Execute thunk |
| 1494 | const result = await mockStoreWithManualApproval.dispatch( |
| 1495 | streamResponseThunk({ |
| 1496 | editorState: mockEditorState, |
| 1497 | modifiers: mockModifiers, |
| 1498 | }) as any, |
| 1499 | ); |
| 1500 | |
| 1501 | // Verify thunk completed successfully (tool waits for approval) |
| 1502 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 1503 | |
| 1504 | // Since tool requires approval, isStreaming should become false after completion |
| 1505 | // but should stay true throughout the initial streaming phase |
| 1506 | const finalStreamingState = |
| 1507 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1508 | expect(finalStreamingState).toBe(false); // Final state should be false since we're waiting for approval |
| 1509 | |
| 1510 | // Verify exact action sequence includes tool generation but NO execution |
| 1511 | const dispatchedActions = (mockStoreWithManualApproval as any).getActions(); |
| 1512 | expect(dispatchedActions).toEqual([ |
| 1513 | { |
| 1514 | type: "chat/streamResponse/pending", |
| 1515 | meta: { |
| 1516 | arg: { |
| 1517 | editorState: mockEditorState, |
| 1518 | modifiers: mockModifiers, |
| 1519 | }, |
| 1520 | requestId: expect.any(String), |
| 1521 | requestStatus: "pending", |
| 1522 | }, |
| 1523 | payload: undefined, |
| 1524 | }, |
| 1525 | { |
| 1526 | type: "chat/streamWrapper/pending", |
| 1527 | meta: { |
| 1528 | arg: expect.any(Function), |
| 1529 | requestId: expect.any(String), |
| 1530 | requestStatus: "pending", |
| 1531 | }, |
| 1532 | payload: undefined, |
| 1533 | }, |
| 1534 | { |
| 1535 | type: "session/submitEditorAndInitAtIndex", |
| 1536 | payload: { |
| 1537 | editorState: mockEditorState, |
| 1538 | index: 1, |
| 1539 | }, |
| 1540 | }, |
| 1541 | { |
| 1542 | type: "session/resetNextCodeBlockToApplyIndex", |
| 1543 | payload: undefined, |
| 1544 | }, |
| 1545 | { |
| 1546 | type: "symbols/updateFromContextItems/pending", |
| 1547 | meta: { |
| 1548 | arg: [], |
| 1549 | requestId: expect.any(String), |
| 1550 | requestStatus: "pending", |
| 1551 | }, |
| 1552 | payload: undefined, |
| 1553 | }, |
| 1554 | { |
| 1555 | type: "session/updateHistoryItemAtIndex", |
| 1556 | payload: { |
| 1557 | index: 1, |
| 1558 | updates: { |
| 1559 | contextItems: [], |
| 1560 | message: { |
| 1561 | content: "Hello, please help me with this code", |
| 1562 | id: "mock-uuid-123", |
| 1563 | role: "user", |
| 1564 | }, |
| 1565 | }, |
| 1566 | }, |
| 1567 | }, |
| 1568 | { |
| 1569 | type: "chat/streamNormalInput/pending", |
| 1570 | meta: { |
| 1571 | arg: { |
| 1572 | legacySlashCommandData: undefined, |
| 1573 | }, |
| 1574 | requestId: expect.any(String), |
| 1575 | requestStatus: "pending", |
| 1576 | }, |
| 1577 | payload: undefined, |
| 1578 | }, |
| 1579 | { |
| 1580 | type: "session/setAppliedRulesAtIndex", |
| 1581 | payload: { |
| 1582 | appliedRules: [], |
| 1583 | index: 1, |
| 1584 | }, |
| 1585 | }, |
| 1586 | { |
| 1587 | type: "session/setActive", |
| 1588 | payload: undefined, |
| 1589 | }, |
| 1590 | { |
| 1591 | type: "session/setInlineErrorMessage", |
| 1592 | payload: undefined, |
| 1593 | }, |
| 1594 | { |
| 1595 | type: "session/setIsPruned", |
| 1596 | payload: false, |
| 1597 | }, |
| 1598 | { |
| 1599 | type: "session/setContextPercentage", |
| 1600 | payload: 0.9, |
| 1601 | }, |
| 1602 | { |
| 1603 | type: "symbols/updateFromContextItems/fulfilled", |
| 1604 | meta: { |
| 1605 | arg: [], |
| 1606 | requestId: expect.any(String), |
| 1607 | requestStatus: "fulfilled", |
| 1608 | }, |
| 1609 | payload: undefined, |
| 1610 | }, |
| 1611 | { |
| 1612 | type: "session/streamUpdate", |
| 1613 | payload: [ |
| 1614 | { |
| 1615 | role: "assistant", |
| 1616 | content: "I'll search the codebase for you.", |
| 1617 | }, |
| 1618 | ], |
| 1619 | }, |
| 1620 | { |
| 1621 | type: "session/streamUpdate", |
| 1622 | payload: [ |
| 1623 | { |
| 1624 | role: "assistant", |
| 1625 | content: "", |
| 1626 | toolCalls: [ |
| 1627 | { |
| 1628 | id: "tool-approval-1", |
| 1629 | type: "function", |
| 1630 | function: { |
| 1631 | name: "search_codebase", |
| 1632 | arguments: JSON.stringify({ query: "test function" }), |
| 1633 | }, |
| 1634 | }, |
| 1635 | ], |
| 1636 | }, |
| 1637 | ], |
| 1638 | }, |
| 1639 | { |
| 1640 | type: "session/addPromptCompletionPair", |
| 1641 | payload: [ |
| 1642 | { |
| 1643 | completion: "I'll search the codebase for you.", |
| 1644 | modelProvider: "anthropic", |
| 1645 | prompt: "Please search the codebase", |
| 1646 | }, |
| 1647 | ], |
| 1648 | }, |
| 1649 | { |
| 1650 | type: "session/setInactive", |
| 1651 | payload: undefined, |
| 1652 | }, |
| 1653 | { |
| 1654 | type: "session/setToolGenerated", |
| 1655 | payload: { |
| 1656 | toolCallId: "tool-approval-1", |
| 1657 | tools: [], |
| 1658 | }, |
| 1659 | }, |
| 1660 | { |
| 1661 | type: "chat/streamNormalInput/fulfilled", |
| 1662 | meta: { |
| 1663 | arg: { |
| 1664 | legacySlashCommandData: undefined, |
| 1665 | }, |
| 1666 | requestId: expect.any(String), |
| 1667 | requestStatus: "fulfilled", |
| 1668 | }, |
| 1669 | payload: undefined, |
| 1670 | }, |
| 1671 | { |
| 1672 | type: "session/saveCurrent/pending", |
| 1673 | meta: { |
| 1674 | arg: { |
| 1675 | generateTitle: true, |
| 1676 | openNewSession: false, |
| 1677 | }, |
| 1678 | requestId: expect.any(String), |
| 1679 | requestStatus: "pending", |
| 1680 | }, |
| 1681 | payload: undefined, |
| 1682 | }, |
| 1683 | { |
| 1684 | type: "session/update/pending", |
| 1685 | meta: { |
| 1686 | arg: expect.objectContaining({ |
| 1687 | history: expect.any(Array), |
| 1688 | sessionId: "session-123", |
| 1689 | title: "New Session", |
| 1690 | workspaceDirectory: "", |
| 1691 | }), |
| 1692 | requestId: expect.any(String), |
| 1693 | requestStatus: "pending", |
| 1694 | }, |
| 1695 | payload: undefined, |
| 1696 | }, |
| 1697 | { |
| 1698 | type: "session/updateSessionMetadata", |
| 1699 | payload: { |
| 1700 | sessionId: "session-123", |
| 1701 | title: "New Session", |
| 1702 | }, |
| 1703 | }, |
| 1704 | { |
| 1705 | type: "session/refreshMetadata/pending", |
| 1706 | meta: { |
| 1707 | arg: {}, |
| 1708 | requestId: expect.any(String), |
| 1709 | requestStatus: "pending", |
| 1710 | }, |
| 1711 | payload: undefined, |
| 1712 | }, |
| 1713 | { |
| 1714 | type: "session/setIsSessionMetadataLoading", |
| 1715 | payload: false, |
| 1716 | }, |
| 1717 | { |
| 1718 | type: "session/setAllSessionMetadata", |
| 1719 | payload: { |
| 1720 | compiledChatMessages: [ |
| 1721 | { |
| 1722 | content: "Please search the codebase", |
| 1723 | role: "user", |
| 1724 | }, |
| 1725 | ], |
| 1726 | contextPercentage: 0.9, |
| 1727 | didPrune: false, |
| 1728 | }, |
| 1729 | }, |
| 1730 | { |
| 1731 | type: "session/refreshMetadata/fulfilled", |
| 1732 | meta: { |
| 1733 | arg: {}, |
| 1734 | requestId: expect.any(String), |
| 1735 | requestStatus: "fulfilled", |
| 1736 | }, |
| 1737 | payload: { |
| 1738 | compiledChatMessages: [ |
| 1739 | { |
| 1740 | content: "Please search the codebase", |
| 1741 | role: "user", |
| 1742 | }, |
| 1743 | ], |
| 1744 | contextPercentage: 0.9, |
| 1745 | didPrune: false, |
| 1746 | }, |
| 1747 | }, |
| 1748 | { |
| 1749 | type: "session/update/fulfilled", |
| 1750 | meta: { |
| 1751 | arg: expect.objectContaining({ |
| 1752 | history: expect.any(Array), |
| 1753 | sessionId: "session-123", |
| 1754 | title: "New Session", |
| 1755 | workspaceDirectory: "", |
| 1756 | }), |
| 1757 | requestId: expect.any(String), |
| 1758 | requestStatus: "fulfilled", |
| 1759 | }, |
| 1760 | payload: undefined, |
| 1761 | }, |
| 1762 | { |
| 1763 | type: "session/saveCurrent/fulfilled", |
| 1764 | meta: { |
| 1765 | arg: { |
| 1766 | generateTitle: true, |
| 1767 | openNewSession: false, |
| 1768 | }, |
| 1769 | requestId: expect.any(String), |
| 1770 | requestStatus: "fulfilled", |
| 1771 | }, |
| 1772 | payload: undefined, |
| 1773 | }, |
| 1774 | { |
| 1775 | type: "chat/streamWrapper/fulfilled", |
| 1776 | meta: { |
| 1777 | arg: expect.any(Function), |
| 1778 | requestId: expect.any(String), |
| 1779 | requestStatus: "fulfilled", |
| 1780 | }, |
| 1781 | payload: undefined, |
| 1782 | }, |
| 1783 | { |
| 1784 | type: "chat/streamResponse/fulfilled", |
| 1785 | meta: { |
| 1786 | arg: { |
| 1787 | editorState: mockEditorState, |
| 1788 | modifiers: mockModifiers, |
| 1789 | }, |
| 1790 | requestId: expect.any(String), |
| 1791 | requestStatus: "fulfilled", |
| 1792 | }, |
| 1793 | payload: undefined, |
| 1794 | }, |
| 1795 | ]); |
| 1796 | |
| 1797 | // Verify IDE messenger calls - compilation should happen, streaming should happen |
| 1798 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1799 | "llm/compileChat", |
| 1800 | { |
| 1801 | messages: [ |
| 1802 | { |
| 1803 | role: "system", |
| 1804 | content: "You are a helpful assistant.", |
| 1805 | }, |
| 1806 | { |
| 1807 | role: "user", |
| 1808 | content: [ |
| 1809 | { |
| 1810 | type: "text", |
| 1811 | text: "Please search the codebase", |
| 1812 | }, |
| 1813 | ], |
| 1814 | }, |
| 1815 | { |
| 1816 | role: "user", |
| 1817 | content: [ |
| 1818 | { |
| 1819 | type: "text", |
| 1820 | text: "Hello, please help me with this code", |
| 1821 | }, |
| 1822 | ], |
| 1823 | }, |
| 1824 | ], |
| 1825 | options: {}, |
| 1826 | }, |
| 1827 | ); |
| 1828 | |
| 1829 | expect(mockIdeMessengerManual.llmStreamChat).toHaveBeenCalledWith( |
| 1830 | { |
| 1831 | completionOptions: {}, |
| 1832 | legacySlashCommandData: undefined, |
| 1833 | messageOptions: { precompiled: true }, |
| 1834 | messages: [ |
| 1835 | { |
| 1836 | role: "user", |
| 1837 | content: "Please search the codebase", |
| 1838 | }, |
| 1839 | ], |
| 1840 | title: "Claude 3.5 Sonnet", |
| 1841 | }, |
| 1842 | expect.any(AbortSignal), |
| 1843 | ); |
| 1844 | |
| 1845 | // Should NOT call tools/call since tool requires approval |
| 1846 | expect(mockIdeMessengerManual.request).not.toHaveBeenCalledWith( |
| 1847 | "tools/call", |
| 1848 | expect.anything(), |
| 1849 | ); |
| 1850 | |
| 1851 | // Verify session save was called |
| 1852 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1853 | "history/save", |
| 1854 | expect.anything(), |
| 1855 | ); |
| 1856 | |
| 1857 | // Verify final state contains the tool call in "generated" state (waiting for approval) |
| 1858 | const finalState = mockStoreWithManualApproval.getState(); |
| 1859 | expect(finalState).toEqual({ |
| 1860 | session: { |
| 1861 | history: [ |
| 1862 | { |
| 1863 | contextItems: [], |
| 1864 | message: { |
| 1865 | id: "1", |
| 1866 | role: "user", |
| 1867 | content: "Please search the codebase", |
| 1868 | }, |
| 1869 | }, |
| 1870 | { |
| 1871 | appliedRules: [], |
| 1872 | contextItems: [], |
| 1873 | editorState: mockEditorState, |
| 1874 | message: { |
| 1875 | content: "Hello, please help me with this code", |
| 1876 | id: expect.any(String), |
| 1877 | role: "user", |
| 1878 | }, |
| 1879 | }, |
| 1880 | { |
| 1881 | contextItems: [], |
| 1882 | isGatheringContext: false, |
| 1883 | message: { |
| 1884 | content: "I'll search the codebase for you.", |
| 1885 | id: "mock-uuid-123", |
| 1886 | role: "assistant", |
| 1887 | toolCalls: [ |
| 1888 | { |
| 1889 | id: "tool-approval-1", |
| 1890 | type: "function", |
| 1891 | function: { |
| 1892 | name: "search_codebase", |
| 1893 | arguments: JSON.stringify({ query: "test function" }), |
| 1894 | }, |
| 1895 | }, |
| 1896 | ], |
| 1897 | }, |
| 1898 | promptLogs: [ |
| 1899 | { |
| 1900 | completion: "I'll search the codebase for you.", |
| 1901 | modelProvider: "anthropic", |
| 1902 | prompt: "Please search the codebase", |
| 1903 | }, |
| 1904 | ], |
| 1905 | toolCallStates: [ |
| 1906 | { |
| 1907 | toolCallId: "tool-approval-1", |
| 1908 | toolCall: { |
| 1909 | id: "tool-approval-1", |
| 1910 | type: "function", |
| 1911 | function: { |
| 1912 | name: "search_codebase", |
| 1913 | arguments: JSON.stringify({ query: "test function" }), |
| 1914 | }, |
| 1915 | }, |
| 1916 | parsedArgs: { query: "test function" }, |
| 1917 | status: "generated", // Waiting for user approval |
| 1918 | }, |
| 1919 | ], |
| 1920 | }, |
| 1921 | ], |
| 1922 | hasReasoningEnabled: false, |
| 1923 | isStreaming: false, |
| 1924 | id: "session-123", |
| 1925 | mode: "chat", |
| 1926 | streamAborter: expect.any(AbortController), |
| 1927 | contextPercentage: 0.9, |
| 1928 | isPruned: false, |
| 1929 | isInEdit: false, |
| 1930 | title: "New Session", |
| 1931 | lastSessionId: undefined, |
| 1932 | isSessionMetadataLoading: false, |
| 1933 | allSessionMetadata: { |
| 1934 | compiledChatMessages: [ |
| 1935 | { |
| 1936 | content: "Please search the codebase", |
| 1937 | role: "user", |
| 1938 | }, |
| 1939 | ], |
| 1940 | contextPercentage: 0.9, |
| 1941 | didPrune: false, |
| 1942 | }, |
| 1943 | symbols: {}, |
| 1944 | codeBlockApplyStates: { |
| 1945 | states: [], |
| 1946 | curIndex: 0, |
| 1947 | }, |
| 1948 | newestToolbarPreviewForInput: {}, |
| 1949 | compactionLoading: {}, |
| 1950 | inlineErrorMessage: undefined, |
| 1951 | }, |
| 1952 | config: { |
| 1953 | config: { |
| 1954 | tools: [], |
| 1955 | rules: [], |
| 1956 | tabAutocompleteModel: undefined, |
| 1957 | selectedModelByRole: { |
| 1958 | chat: mockClaudeModel, |
| 1959 | apply: null, |
| 1960 | edit: null, |
| 1961 | summarize: null, |
| 1962 | autocomplete: null, |
| 1963 | rerank: null, |
| 1964 | embed: null, |
| 1965 | }, |
| 1966 | experimental: { |
| 1967 | onlyUseSystemMessageTools: false, |
| 1968 | }, |
| 1969 | }, |
| 1970 | lastSelectedModelByRole: { |
| 1971 | chat: mockClaudeModel.title, |
| 1972 | }, |
| 1973 | loading: false, |
| 1974 | configError: undefined, |
| 1975 | }, |
| 1976 | ui: { |
| 1977 | toolSettings: { |
| 1978 | search_codebase: "askFirst", |
| 1979 | }, |
| 1980 | ruleSettings: {}, |
| 1981 | showDialog: false, |
| 1982 | dialogMessage: undefined, |
| 1983 | }, |
| 1984 | editModeState: { |
| 1985 | isInEdit: false, |
| 1986 | returnToMode: "chat", |
| 1987 | }, |
| 1988 | indexing: { |
| 1989 | indexingState: "disabled", |
| 1990 | }, |
| 1991 | tabs: { |
| 1992 | tabsItems: [], |
| 1993 | }, |
| 1994 | profiles: { |
| 1995 | profiles: [], |
| 1996 | }, |
| 1997 | }); |
| 1998 | }); |
| 1999 | |
| 2000 | it("should handle complete user approval and tool execution flow", async () => { |
| 2001 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 2002 | |
| 2003 | // Create store with tool settings that require manual approval |
| 2004 | const mockStoreWithApproval = createMockStore({ |
| 2005 | session: { |
| 2006 | history: [ |
| 2007 | { |
| 2008 | message: { |
| 2009 | id: "1", |
| 2010 | role: "user", |
| 2011 | content: "Please search the codebase for test functions", |
| 2012 | }, |
| 2013 | contextItems: [], |
| 2014 | }, |
| 2015 | ], |
| 2016 | hasReasoningEnabled: false, |
| 2017 | isStreaming: true, |
| 2018 | id: "session-123", |
| 2019 | mode: "chat", |
| 2020 | streamAborter: new AbortController(), |
| 2021 | contextPercentage: 0, |
| 2022 | isPruned: false, |
| 2023 | isInEdit: false, |
| 2024 | title: "", |
| 2025 | lastSessionId: undefined, |
| 2026 | isSessionMetadataLoading: false, |
| 2027 | allSessionMetadata: [], |
| 2028 | symbols: {}, |
| 2029 | codeBlockApplyStates: { |
| 2030 | states: [], |
| 2031 | curIndex: 0, |
| 2032 | }, |
| 2033 | newestToolbarPreviewForInput: {}, |
| 2034 | compactionLoading: {}, |
| 2035 | inlineErrorMessage: undefined, |
| 2036 | }, |
| 2037 | config: { |
| 2038 | config: { |
| 2039 | tools: [], |
| 2040 | rules: [], |
| 2041 | tabAutocompleteModel: undefined, |
| 2042 | selectedModelByRole: { |
| 2043 | chat: mockClaudeModel, |
| 2044 | apply: null, |
| 2045 | edit: null, |
| 2046 | summarize: null, |
| 2047 | autocomplete: null, |
| 2048 | rerank: null, |
| 2049 | embed: null, |
| 2050 | }, |
| 2051 | experimental: { |
| 2052 | onlyUseSystemMessageTools: false, |
| 2053 | }, |
| 2054 | } as any, |
| 2055 | lastSelectedModelByRole: { |
| 2056 | chat: mockClaudeModel.title, |
| 2057 | }, |
| 2058 | } as any, |
| 2059 | ui: { |
| 2060 | toolSettings: { |
| 2061 | search_codebase: "askFirst", // Requires manual approval |
| 2062 | }, |
| 2063 | ruleSettings: {}, |
| 2064 | showDialog: false, |
| 2065 | dialogMessage: undefined, |
| 2066 | } as any, |
| 2067 | }); |
| 2068 | |
| 2069 | const mockIdeMessengerApproval = mockStoreWithApproval.mockIdeMessenger; |
| 2070 | |
| 2071 | // Setup successful compilation |
| 2072 | mockIdeMessengerApproval.request.mockImplementation( |
| 2073 | (endpoint: string, data: any) => { |
| 2074 | if (endpoint === "llm/compileChat") { |
| 2075 | return Promise.resolve({ |
| 2076 | status: "success", |
| 2077 | content: { |
| 2078 | compiledChatMessages: [ |
| 2079 | { |
| 2080 | role: "user", |
| 2081 | content: "Please search the codebase for test functions", |
| 2082 | }, |
| 2083 | ], |
| 2084 | didPrune: false, |
| 2085 | contextPercentage: 0.85, |
| 2086 | }, |
| 2087 | }); |
| 2088 | } else if (endpoint === "tools/call") { |
| 2089 | // Mock server-side tool call response for search_codebase |
| 2090 | return Promise.resolve({ |
| 2091 | status: "success", |
| 2092 | content: { |
| 2093 | contextItems: [ |
| 2094 | { |
| 2095 | name: "Search Results", |
| 2096 | description: "Found test functions", |
| 2097 | content: |
| 2098 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2099 | icon: "search", |
| 2100 | hidden: false, |
| 2101 | }, |
| 2102 | ], |
| 2103 | errorMessage: undefined, |
| 2104 | }, |
| 2105 | }); |
| 2106 | } else if (endpoint === "history/save") { |
| 2107 | return Promise.resolve({ status: "success" }); |
| 2108 | } else if (endpoint === "history/list") { |
| 2109 | return Promise.resolve({ status: "success", content: [] }); |
| 2110 | } |
| 2111 | return Promise.resolve({ status: "success", content: {} }); |
| 2112 | }, |
| 2113 | ); |
| 2114 | |
| 2115 | // Setup streaming generator with tool call |
| 2116 | async function* mockStreamGeneratorWithApprovalFlow() { |
| 2117 | yield [ |
| 2118 | { |
| 2119 | role: "assistant", |
| 2120 | content: "I'll search for test functions in the codebase.", |
| 2121 | }, |
| 2122 | ]; |
| 2123 | yield [ |
| 2124 | { |
| 2125 | role: "assistant", |
| 2126 | content: "", |
| 2127 | toolCalls: [ |
| 2128 | { |
| 2129 | id: "tool-approval-flow-1", |
| 2130 | type: "function", |
| 2131 | function: { |
| 2132 | name: "search_codebase", |
| 2133 | arguments: JSON.stringify({ query: "test function" }), |
| 2134 | }, |
| 2135 | }, |
| 2136 | ], |
| 2137 | }, |
| 2138 | ]; |
| 2139 | return { |
| 2140 | prompt: "Please search the codebase for test functions", |
| 2141 | completion: "I'll search for test functions in the codebase.", |
| 2142 | modelProvider: "anthropic", |
| 2143 | }; |
| 2144 | } |
| 2145 | |
| 2146 | // Mock subsequent streaming calls (after tool execution) |
| 2147 | let streamCallCount = 0; |
| 2148 | mockIdeMessengerApproval.llmStreamChat.mockImplementation(() => { |
| 2149 | streamCallCount++; |
| 2150 | if (streamCallCount === 1) { |
| 2151 | // First call - initial streaming with tool call |
| 2152 | return mockStreamGeneratorWithApprovalFlow(); |
| 2153 | } else { |
| 2154 | // Subsequent calls from streamResponseAfterToolCall |
| 2155 | async function* followupGenerator() { |
| 2156 | yield [ |
| 2157 | { |
| 2158 | role: "assistant", |
| 2159 | content: |
| 2160 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2161 | }, |
| 2162 | ]; |
| 2163 | return { |
| 2164 | prompt: "continuing after tool execution", |
| 2165 | completion: |
| 2166 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2167 | modelProvider: "anthropic", |
| 2168 | }; |
| 2169 | } |
| 2170 | return followupGenerator(); |
| 2171 | } |
| 2172 | }); |
| 2173 | |
| 2174 | // Track isStreaming state changes throughout the entire test |
| 2175 | const streamingStateChanges: boolean[] = []; |
| 2176 | let lastStreamingState = |
| 2177 | mockStoreWithApproval.getState().session.isStreaming; |
| 2178 | const originalDispatch = mockStoreWithApproval.dispatch; |
| 2179 | mockStoreWithApproval.dispatch = ((action: any) => { |
| 2180 | const result = originalDispatch(action); |
| 2181 | const currentStreamingState = |
| 2182 | mockStoreWithApproval.getState().session.isStreaming; |
| 2183 | // Only record when the state actually changes |
| 2184 | if (currentStreamingState !== lastStreamingState) { |
| 2185 | streamingStateChanges.push(currentStreamingState); |
| 2186 | lastStreamingState = currentStreamingState; |
| 2187 | } |
| 2188 | return result; |
| 2189 | }) as any; |
| 2190 | |
| 2191 | // Execute initial thunk - this should generate the tool call but not execute it |
| 2192 | const initialResult = await mockStoreWithApproval.dispatch( |
| 2193 | streamResponseThunk({ |
| 2194 | editorState: mockEditorState, |
| 2195 | modifiers: mockModifiers, |
| 2196 | }) as any, |
| 2197 | ); |
| 2198 | |
| 2199 | // Verify initial streaming completed successfully |
| 2200 | expect(initialResult.type).toBe("chat/streamResponse/fulfilled"); |
| 2201 | |
| 2202 | // Verify exact initial action sequence by comparing action types |
| 2203 | const initialActions = (mockStoreWithApproval as any).getActions(); |
| 2204 | |
| 2205 | expect(initialActions).toEqual([ |
| 2206 | { |
| 2207 | type: "chat/streamResponse/pending", |
| 2208 | meta: { |
| 2209 | arg: { |
| 2210 | editorState: mockEditorState, |
| 2211 | modifiers: mockModifiers, |
| 2212 | }, |
| 2213 | requestId: expect.any(String), |
| 2214 | requestStatus: "pending", |
| 2215 | }, |
| 2216 | payload: undefined, |
| 2217 | }, |
| 2218 | { |
| 2219 | type: "chat/streamWrapper/pending", |
| 2220 | meta: { |
| 2221 | arg: expect.any(Function), |
| 2222 | requestId: expect.any(String), |
| 2223 | requestStatus: "pending", |
| 2224 | }, |
| 2225 | payload: undefined, |
| 2226 | }, |
| 2227 | { |
| 2228 | type: "session/submitEditorAndInitAtIndex", |
| 2229 | payload: { |
| 2230 | editorState: mockEditorState, |
| 2231 | index: 1, |
| 2232 | }, |
| 2233 | }, |
| 2234 | { |
| 2235 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2236 | payload: undefined, |
| 2237 | }, |
| 2238 | { |
| 2239 | type: "symbols/updateFromContextItems/pending", |
| 2240 | meta: { |
| 2241 | arg: [], |
| 2242 | requestId: expect.any(String), |
| 2243 | requestStatus: "pending", |
| 2244 | }, |
| 2245 | payload: undefined, |
| 2246 | }, |
| 2247 | { |
| 2248 | type: "session/updateHistoryItemAtIndex", |
| 2249 | payload: { |
| 2250 | index: 1, |
| 2251 | updates: { |
| 2252 | contextItems: [], |
| 2253 | message: { |
| 2254 | content: "Hello, please help me with this code", |
| 2255 | id: "mock-uuid-123", |
| 2256 | role: "user", |
| 2257 | }, |
| 2258 | }, |
| 2259 | }, |
| 2260 | }, |
| 2261 | { |
| 2262 | type: "chat/streamNormalInput/pending", |
| 2263 | meta: { |
| 2264 | arg: { |
| 2265 | legacySlashCommandData: undefined, |
| 2266 | }, |
| 2267 | requestId: expect.any(String), |
| 2268 | requestStatus: "pending", |
| 2269 | }, |
| 2270 | payload: undefined, |
| 2271 | }, |
| 2272 | { |
| 2273 | type: "session/setAppliedRulesAtIndex", |
| 2274 | payload: { |
| 2275 | appliedRules: [], |
| 2276 | index: 1, |
| 2277 | }, |
| 2278 | }, |
| 2279 | { |
| 2280 | type: "session/setActive", |
| 2281 | payload: undefined, |
| 2282 | }, |
| 2283 | { |
| 2284 | type: "session/setInlineErrorMessage", |
| 2285 | payload: undefined, |
| 2286 | }, |
| 2287 | { |
| 2288 | type: "session/setIsPruned", |
| 2289 | payload: false, |
| 2290 | }, |
| 2291 | { |
| 2292 | type: "session/setContextPercentage", |
| 2293 | payload: 0.85, |
| 2294 | }, |
| 2295 | { |
| 2296 | type: "symbols/updateFromContextItems/fulfilled", |
| 2297 | meta: { |
| 2298 | arg: [], |
| 2299 | requestId: expect.any(String), |
| 2300 | requestStatus: "fulfilled", |
| 2301 | }, |
| 2302 | payload: undefined, |
| 2303 | }, |
| 2304 | { |
| 2305 | type: "session/streamUpdate", |
| 2306 | payload: [ |
| 2307 | { |
| 2308 | role: "assistant", |
| 2309 | content: "I'll search for test functions in the codebase.", |
| 2310 | }, |
| 2311 | ], |
| 2312 | }, |
| 2313 | { |
| 2314 | type: "session/streamUpdate", |
| 2315 | payload: [ |
| 2316 | { |
| 2317 | role: "assistant", |
| 2318 | content: "", |
| 2319 | toolCalls: [ |
| 2320 | { |
| 2321 | id: "tool-approval-flow-1", |
| 2322 | type: "function", |
| 2323 | function: { |
| 2324 | name: "search_codebase", |
| 2325 | arguments: JSON.stringify({ query: "test function" }), |
| 2326 | }, |
| 2327 | }, |
| 2328 | ], |
| 2329 | }, |
| 2330 | ], |
| 2331 | }, |
| 2332 | { |
| 2333 | type: "session/addPromptCompletionPair", |
| 2334 | payload: [ |
| 2335 | { |
| 2336 | completion: "I'll search for test functions in the codebase.", |
| 2337 | modelProvider: "anthropic", |
| 2338 | prompt: "Please search the codebase for test functions", |
| 2339 | }, |
| 2340 | ], |
| 2341 | }, |
| 2342 | { |
| 2343 | type: "session/setInactive", |
| 2344 | payload: undefined, |
| 2345 | }, |
| 2346 | { |
| 2347 | type: "session/setToolGenerated", |
| 2348 | payload: { |
| 2349 | toolCallId: "tool-approval-flow-1", |
| 2350 | tools: [], |
| 2351 | }, |
| 2352 | }, |
| 2353 | { |
| 2354 | type: "chat/streamNormalInput/fulfilled", |
| 2355 | meta: { |
| 2356 | arg: { |
| 2357 | legacySlashCommandData: undefined, |
| 2358 | }, |
| 2359 | requestId: expect.any(String), |
| 2360 | requestStatus: "fulfilled", |
| 2361 | }, |
| 2362 | payload: undefined, |
| 2363 | }, |
| 2364 | { |
| 2365 | type: "session/saveCurrent/pending", |
| 2366 | meta: { |
| 2367 | arg: { |
| 2368 | generateTitle: true, |
| 2369 | openNewSession: false, |
| 2370 | }, |
| 2371 | requestId: expect.any(String), |
| 2372 | requestStatus: "pending", |
| 2373 | }, |
| 2374 | payload: undefined, |
| 2375 | }, |
| 2376 | { |
| 2377 | type: "session/update/pending", |
| 2378 | meta: { |
| 2379 | arg: expect.objectContaining({ |
| 2380 | history: expect.any(Array), |
| 2381 | sessionId: "session-123", |
| 2382 | title: "New Session", |
| 2383 | workspaceDirectory: "", |
| 2384 | }), |
| 2385 | requestId: expect.any(String), |
| 2386 | requestStatus: "pending", |
| 2387 | }, |
| 2388 | payload: undefined, |
| 2389 | }, |
| 2390 | { |
| 2391 | type: "session/updateSessionMetadata", |
| 2392 | payload: { |
| 2393 | sessionId: "session-123", |
| 2394 | title: "New Session", |
| 2395 | }, |
| 2396 | }, |
| 2397 | { |
| 2398 | type: "session/refreshMetadata/pending", |
| 2399 | meta: { |
| 2400 | arg: {}, |
| 2401 | requestId: expect.any(String), |
| 2402 | requestStatus: "pending", |
| 2403 | }, |
| 2404 | payload: undefined, |
| 2405 | }, |
| 2406 | { |
| 2407 | type: "session/setIsSessionMetadataLoading", |
| 2408 | payload: false, |
| 2409 | }, |
| 2410 | { |
| 2411 | type: "session/setAllSessionMetadata", |
| 2412 | payload: [], |
| 2413 | }, |
| 2414 | { |
| 2415 | type: "session/refreshMetadata/fulfilled", |
| 2416 | meta: { |
| 2417 | arg: {}, |
| 2418 | requestId: expect.any(String), |
| 2419 | requestStatus: "fulfilled", |
| 2420 | }, |
| 2421 | payload: [], |
| 2422 | }, |
| 2423 | { |
| 2424 | type: "session/update/fulfilled", |
| 2425 | meta: { |
| 2426 | arg: expect.objectContaining({ |
| 2427 | history: expect.any(Array), |
| 2428 | sessionId: "session-123", |
| 2429 | title: "New Session", |
| 2430 | workspaceDirectory: "", |
| 2431 | }), |
| 2432 | requestId: expect.any(String), |
| 2433 | requestStatus: "fulfilled", |
| 2434 | }, |
| 2435 | payload: undefined, |
| 2436 | }, |
| 2437 | { |
| 2438 | type: "session/saveCurrent/fulfilled", |
| 2439 | meta: { |
| 2440 | arg: { |
| 2441 | generateTitle: true, |
| 2442 | openNewSession: false, |
| 2443 | }, |
| 2444 | requestId: expect.any(String), |
| 2445 | requestStatus: "fulfilled", |
| 2446 | }, |
| 2447 | payload: undefined, |
| 2448 | }, |
| 2449 | { |
| 2450 | type: "chat/streamWrapper/fulfilled", |
| 2451 | meta: { |
| 2452 | arg: expect.any(Function), |
| 2453 | requestId: expect.any(String), |
| 2454 | requestStatus: "fulfilled", |
| 2455 | }, |
| 2456 | payload: undefined, |
| 2457 | }, |
| 2458 | { |
| 2459 | type: "chat/streamResponse/fulfilled", |
| 2460 | meta: { |
| 2461 | arg: { |
| 2462 | editorState: mockEditorState, |
| 2463 | modifiers: mockModifiers, |
| 2464 | }, |
| 2465 | requestId: expect.any(String), |
| 2466 | requestStatus: "fulfilled", |
| 2467 | }, |
| 2468 | payload: undefined, |
| 2469 | }, |
| 2470 | ]); |
| 2471 | |
| 2472 | // Clear the actions array to track only the approval flow |
| 2473 | (mockStoreWithApproval as any).clearActions(); |
| 2474 | |
| 2475 | // Import the callToolById thunk to simulate user approval |
| 2476 | const { callToolById } = await import("./callToolById"); |
| 2477 | |
| 2478 | // Simulate user clicking "Accept" on the tool call |
| 2479 | const approvalResult = await mockStoreWithApproval.dispatch( |
| 2480 | callToolById({ toolCallId: "tool-approval-flow-1" }) as any, |
| 2481 | ); |
| 2482 | |
| 2483 | // Verify tool execution completed successfully |
| 2484 | expect(approvalResult.type).toBe("chat/callTool/fulfilled"); |
| 2485 | |
| 2486 | // Verify exact approval flow actions |
| 2487 | const approvalActions = (mockStoreWithApproval as any).getActions(); |
| 2488 | expect(approvalActions).toEqual([ |
| 2489 | { |
| 2490 | type: "chat/callTool/pending", |
| 2491 | meta: { |
| 2492 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2493 | requestId: expect.any(String), |
| 2494 | requestStatus: "pending", |
| 2495 | }, |
| 2496 | payload: undefined, |
| 2497 | }, |
| 2498 | { |
| 2499 | type: "session/setToolCallCalling", |
| 2500 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2501 | }, |
| 2502 | { |
| 2503 | type: "session/updateToolCallOutput", |
| 2504 | payload: { |
| 2505 | toolCallId: "tool-approval-flow-1", |
| 2506 | contextItems: [ |
| 2507 | { |
| 2508 | name: "Search Results", |
| 2509 | description: "Found test functions", |
| 2510 | content: |
| 2511 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2512 | icon: "search", |
| 2513 | hidden: false, |
| 2514 | }, |
| 2515 | ], |
| 2516 | }, |
| 2517 | }, |
| 2518 | { |
| 2519 | type: "session/acceptToolCall", |
| 2520 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2521 | }, |
| 2522 | { |
| 2523 | type: "chat/streamAfterToolCall/pending", |
| 2524 | meta: { |
| 2525 | arg: { |
| 2526 | toolCallId: "tool-approval-flow-1", |
| 2527 | }, |
| 2528 | requestId: expect.any(String), |
| 2529 | requestStatus: "pending", |
| 2530 | }, |
| 2531 | payload: undefined, |
| 2532 | }, |
| 2533 | { |
| 2534 | type: "chat/streamWrapper/pending", |
| 2535 | meta: { |
| 2536 | arg: expect.any(Function), |
| 2537 | requestId: expect.any(String), |
| 2538 | requestStatus: "pending", |
| 2539 | }, |
| 2540 | payload: undefined, |
| 2541 | }, |
| 2542 | { |
| 2543 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2544 | payload: undefined, |
| 2545 | }, |
| 2546 | { |
| 2547 | type: "session/streamUpdate", |
| 2548 | payload: [ |
| 2549 | { |
| 2550 | content: |
| 2551 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2552 | role: "tool", |
| 2553 | toolCallId: "tool-approval-flow-1", |
| 2554 | }, |
| 2555 | ], |
| 2556 | }, |
| 2557 | { |
| 2558 | type: "chat/streamNormalInput/pending", |
| 2559 | meta: { |
| 2560 | arg: {}, |
| 2561 | requestId: expect.any(String), |
| 2562 | requestStatus: "pending", |
| 2563 | }, |
| 2564 | payload: undefined, |
| 2565 | }, |
| 2566 | { |
| 2567 | type: "session/setAppliedRulesAtIndex", |
| 2568 | payload: { |
| 2569 | appliedRules: [], |
| 2570 | index: 1, |
| 2571 | }, |
| 2572 | }, |
| 2573 | { |
| 2574 | type: "session/setActive", |
| 2575 | payload: undefined, |
| 2576 | }, |
| 2577 | { |
| 2578 | type: "session/setInlineErrorMessage", |
| 2579 | payload: undefined, |
| 2580 | }, |
| 2581 | { |
| 2582 | type: "session/setIsPruned", |
| 2583 | payload: false, |
| 2584 | }, |
| 2585 | { |
| 2586 | type: "session/setContextPercentage", |
| 2587 | payload: 0.85, |
| 2588 | }, |
| 2589 | { |
| 2590 | type: "session/streamUpdate", |
| 2591 | payload: [ |
| 2592 | { |
| 2593 | role: "assistant", |
| 2594 | content: |
| 2595 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2596 | }, |
| 2597 | ], |
| 2598 | }, |
| 2599 | { |
| 2600 | type: "session/addPromptCompletionPair", |
| 2601 | payload: [ |
| 2602 | { |
| 2603 | completion: |
| 2604 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2605 | modelProvider: "anthropic", |
| 2606 | prompt: "continuing after tool execution", |
| 2607 | }, |
| 2608 | ], |
| 2609 | }, |
| 2610 | { |
| 2611 | type: "session/setInactive", |
| 2612 | payload: undefined, |
| 2613 | }, |
| 2614 | { |
| 2615 | type: "chat/streamNormalInput/fulfilled", |
| 2616 | meta: { |
| 2617 | arg: {}, |
| 2618 | requestId: expect.any(String), |
| 2619 | requestStatus: "fulfilled", |
| 2620 | }, |
| 2621 | payload: undefined, |
| 2622 | }, |
| 2623 | { |
| 2624 | type: "session/saveCurrent/pending", |
| 2625 | meta: { |
| 2626 | arg: { |
| 2627 | generateTitle: true, |
| 2628 | openNewSession: false, |
| 2629 | }, |
| 2630 | requestId: expect.any(String), |
| 2631 | requestStatus: "pending", |
| 2632 | }, |
| 2633 | payload: undefined, |
| 2634 | }, |
| 2635 | { |
| 2636 | type: "session/update/pending", |
| 2637 | meta: { |
| 2638 | arg: expect.objectContaining({ |
| 2639 | history: expect.any(Array), |
| 2640 | sessionId: "session-123", |
| 2641 | title: "New Session", |
| 2642 | workspaceDirectory: "", |
| 2643 | }), |
| 2644 | requestId: expect.any(String), |
| 2645 | requestStatus: "pending", |
| 2646 | }, |
| 2647 | payload: undefined, |
| 2648 | }, |
| 2649 | { |
| 2650 | type: "session/updateSessionMetadata", |
| 2651 | payload: { |
| 2652 | sessionId: "session-123", |
| 2653 | title: "New Session", |
| 2654 | }, |
| 2655 | }, |
| 2656 | { |
| 2657 | type: "session/refreshMetadata/pending", |
| 2658 | meta: { |
| 2659 | arg: {}, |
| 2660 | requestId: expect.any(String), |
| 2661 | requestStatus: "pending", |
| 2662 | }, |
| 2663 | payload: undefined, |
| 2664 | }, |
| 2665 | { |
| 2666 | type: "session/setIsSessionMetadataLoading", |
| 2667 | payload: false, |
| 2668 | }, |
| 2669 | { |
| 2670 | type: "session/setAllSessionMetadata", |
| 2671 | payload: [], |
| 2672 | }, |
| 2673 | { |
| 2674 | type: "session/refreshMetadata/fulfilled", |
| 2675 | meta: { |
| 2676 | arg: {}, |
| 2677 | requestId: expect.any(String), |
| 2678 | requestStatus: "fulfilled", |
| 2679 | }, |
| 2680 | payload: [], |
| 2681 | }, |
| 2682 | { |
| 2683 | type: "session/update/fulfilled", |
| 2684 | meta: { |
| 2685 | arg: expect.objectContaining({ |
| 2686 | history: expect.any(Array), |
| 2687 | sessionId: "session-123", |
| 2688 | title: "New Session", |
| 2689 | workspaceDirectory: "", |
| 2690 | }), |
| 2691 | requestId: expect.any(String), |
| 2692 | requestStatus: "fulfilled", |
| 2693 | }, |
| 2694 | payload: undefined, |
| 2695 | }, |
| 2696 | { |
| 2697 | type: "session/saveCurrent/fulfilled", |
| 2698 | meta: { |
| 2699 | arg: { |
| 2700 | generateTitle: true, |
| 2701 | openNewSession: false, |
| 2702 | }, |
| 2703 | requestId: expect.any(String), |
| 2704 | requestStatus: "fulfilled", |
| 2705 | }, |
| 2706 | payload: undefined, |
| 2707 | }, |
| 2708 | { |
| 2709 | type: "chat/streamWrapper/fulfilled", |
| 2710 | meta: { |
| 2711 | arg: expect.any(Function), |
| 2712 | requestId: expect.any(String), |
| 2713 | requestStatus: "fulfilled", |
| 2714 | }, |
| 2715 | payload: undefined, |
| 2716 | }, |
| 2717 | { |
| 2718 | type: "chat/streamAfterToolCall/fulfilled", |
| 2719 | meta: { |
| 2720 | arg: { |
| 2721 | toolCallId: "tool-approval-flow-1", |
| 2722 | }, |
| 2723 | requestId: expect.any(String), |
| 2724 | requestStatus: "fulfilled", |
| 2725 | }, |
| 2726 | payload: undefined, |
| 2727 | }, |
| 2728 | { |
| 2729 | type: "chat/callTool/fulfilled", |
| 2730 | meta: { |
| 2731 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2732 | requestId: expect.any(String), |
| 2733 | requestStatus: "fulfilled", |
| 2734 | }, |
| 2735 | payload: undefined, |
| 2736 | }, |
| 2737 | ]); |
| 2738 | |
| 2739 | // Verify telemetry events for manual approval flow |
| 2740 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_decision", { |
| 2741 | decision: "accept", |
| 2742 | toolName: "search_codebase", |
| 2743 | toolCallId: "tool-approval-flow-1", |
| 2744 | }); |
| 2745 | |
| 2746 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_outcome", { |
| 2747 | succeeded: true, |
| 2748 | toolName: "search_codebase", |
| 2749 | errorMessage: undefined, |
| 2750 | duration_ms: expect.any(Number), |
| 2751 | }); |
| 2752 | |
| 2753 | // Verify IDE messenger calls for tool execution |
| 2754 | expect(mockIdeMessengerApproval.request).toHaveBeenCalledWith( |
| 2755 | "tools/call", |
| 2756 | { |
| 2757 | toolCall: { |
| 2758 | id: "tool-approval-flow-1", |
| 2759 | type: "function", |
| 2760 | function: { |
| 2761 | name: "search_codebase", |
| 2762 | arguments: JSON.stringify({ query: "test function" }), |
| 2763 | }, |
| 2764 | }, |
| 2765 | }, |
| 2766 | ); |
| 2767 | |
| 2768 | // Verify second streaming call was made for continuation |
| 2769 | expect(streamCallCount).toBe(2); |
| 2770 | |
| 2771 | // Verify final state shows completed tool call and follow-up response |
| 2772 | const finalState = mockStoreWithApproval.getState(); |
| 2773 | expect(finalState).toEqual({ |
| 2774 | session: { |
| 2775 | history: [ |
| 2776 | { |
| 2777 | contextItems: [], |
| 2778 | message: { |
| 2779 | id: "1", |
| 2780 | role: "user", |
| 2781 | content: "Please search the codebase for test functions", |
| 2782 | }, |
| 2783 | }, |
| 2784 | { |
| 2785 | appliedRules: [], |
| 2786 | contextItems: [], |
| 2787 | editorState: mockEditorState, |
| 2788 | message: { |
| 2789 | content: "Hello, please help me with this code", |
| 2790 | id: expect.any(String), |
| 2791 | role: "user", |
| 2792 | }, |
| 2793 | }, |
| 2794 | { |
| 2795 | contextItems: [], |
| 2796 | isGatheringContext: false, |
| 2797 | message: { |
| 2798 | content: "I'll search for test functions in the codebase.", |
| 2799 | id: expect.any(String), |
| 2800 | role: "assistant", |
| 2801 | toolCalls: [ |
| 2802 | { |
| 2803 | id: "tool-approval-flow-1", |
| 2804 | type: "function", |
| 2805 | function: { |
| 2806 | name: "search_codebase", |
| 2807 | arguments: JSON.stringify({ query: "test function" }), |
| 2808 | }, |
| 2809 | }, |
| 2810 | ], |
| 2811 | }, |
| 2812 | promptLogs: [ |
| 2813 | { |
| 2814 | completion: "I'll search for test functions in the codebase.", |
| 2815 | modelProvider: "anthropic", |
| 2816 | prompt: "Please search the codebase for test functions", |
| 2817 | }, |
| 2818 | ], |
| 2819 | toolCallStates: [ |
| 2820 | { |
| 2821 | toolCallId: "tool-approval-flow-1", |
| 2822 | toolCall: { |
| 2823 | id: "tool-approval-flow-1", |
| 2824 | type: "function", |
| 2825 | function: { |
| 2826 | name: "search_codebase", |
| 2827 | arguments: JSON.stringify({ query: "test function" }), |
| 2828 | }, |
| 2829 | }, |
| 2830 | parsedArgs: { query: "test function" }, |
| 2831 | status: "done", // Tool call completed successfully |
| 2832 | output: [ |
| 2833 | { |
| 2834 | name: "Search Results", |
| 2835 | description: "Found test functions", |
| 2836 | content: |
| 2837 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2838 | icon: "search", |
| 2839 | hidden: false, |
| 2840 | }, |
| 2841 | ], |
| 2842 | }, |
| 2843 | ], |
| 2844 | }, |
| 2845 | { |
| 2846 | contextItems: [], |
| 2847 | message: { |
| 2848 | content: |
| 2849 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2850 | id: expect.any(String), |
| 2851 | role: "tool", |
| 2852 | toolCallId: "tool-approval-flow-1", |
| 2853 | }, |
| 2854 | }, |
| 2855 | { |
| 2856 | contextItems: [], |
| 2857 | isGatheringContext: false, |
| 2858 | message: { |
| 2859 | content: |
| 2860 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2861 | id: "mock-uuid-123", |
| 2862 | role: "assistant", |
| 2863 | }, |
| 2864 | promptLogs: [ |
| 2865 | { |
| 2866 | completion: |
| 2867 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2868 | modelProvider: "anthropic", |
| 2869 | prompt: "continuing after tool execution", |
| 2870 | }, |
| 2871 | ], |
| 2872 | }, |
| 2873 | ], |
| 2874 | hasReasoningEnabled: false, |
| 2875 | isStreaming: false, // Inactive after complete flow |
| 2876 | id: "session-123", |
| 2877 | mode: "chat", |
| 2878 | streamAborter: expect.any(AbortController), |
| 2879 | contextPercentage: 0.85, |
| 2880 | isPruned: false, |
| 2881 | isInEdit: false, |
| 2882 | title: "New Session", |
| 2883 | lastSessionId: undefined, |
| 2884 | isSessionMetadataLoading: false, |
| 2885 | allSessionMetadata: [], |
| 2886 | symbols: {}, |
| 2887 | codeBlockApplyStates: { |
| 2888 | states: [], |
| 2889 | curIndex: 0, |
| 2890 | }, |
| 2891 | newestToolbarPreviewForInput: {}, |
| 2892 | compactionLoading: {}, |
| 2893 | inlineErrorMessage: undefined, |
| 2894 | }, |
| 2895 | config: { |
| 2896 | config: { |
| 2897 | tools: [], |
| 2898 | rules: [], |
| 2899 | tabAutocompleteModel: undefined, |
| 2900 | selectedModelByRole: { |
| 2901 | chat: mockClaudeModel, |
| 2902 | apply: null, |
| 2903 | edit: null, |
| 2904 | summarize: null, |
| 2905 | autocomplete: null, |
| 2906 | rerank: null, |
| 2907 | embed: null, |
| 2908 | }, |
| 2909 | experimental: { |
| 2910 | onlyUseSystemMessageTools: false, |
| 2911 | }, |
| 2912 | }, |
| 2913 | lastSelectedModelByRole: { |
| 2914 | chat: mockClaudeModel.title, |
| 2915 | }, |
| 2916 | loading: false, |
| 2917 | configError: undefined, |
| 2918 | }, |
| 2919 | ui: { |
| 2920 | toolSettings: { |
| 2921 | search_codebase: "askFirst", |
| 2922 | }, |
| 2923 | ruleSettings: {}, |
| 2924 | showDialog: false, |
| 2925 | dialogMessage: undefined, |
| 2926 | }, |
| 2927 | editModeState: { |
| 2928 | isInEdit: false, |
| 2929 | returnToMode: "chat", |
| 2930 | }, |
| 2931 | indexing: { |
| 2932 | indexingState: "disabled", |
| 2933 | }, |
| 2934 | tabs: { |
| 2935 | tabsItems: [], |
| 2936 | }, |
| 2937 | profiles: { |
| 2938 | profiles: [], |
| 2939 | }, |
| 2940 | }); |
| 2941 | }); |
| 2942 | }); |