| 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 | indexing: { |
| 730 | statuses: {}, |
| 731 | hiddenChatPeekTypes: { |
| 732 | docs: false, |
| 733 | }, |
| 734 | }, |
| 735 | }, |
| 736 | tabs: { |
| 737 | tabsItems: [], |
| 738 | }, |
| 739 | profiles: { |
| 740 | organizations: [], |
| 741 | selectedProfileId: null, |
| 742 | selectedOrganizationId: null, |
| 743 | preferencesByProfileId: {}, |
| 744 | }, |
| 745 | }); |
| 746 | }); |
| 747 | |
| 748 | it("should handle tool call rejection error", async () => { |
| 749 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 750 | |
| 751 | // Create store with tool settings that will reject the tool call |
| 752 | const mockStoreWithToolReject = createMockStore({ |
| 753 | session: { |
| 754 | history: [ |
| 755 | { |
| 756 | message: { |
| 757 | id: "1", |
| 758 | role: "user", |
| 759 | content: "Please edit this file", |
| 760 | }, |
| 761 | contextItems: [], |
| 762 | }, |
| 763 | ], |
| 764 | hasReasoningEnabled: false, |
| 765 | isStreaming: true, |
| 766 | id: "session-123", |
| 767 | mode: "chat", |
| 768 | streamAborter: new AbortController(), |
| 769 | contextPercentage: 0, |
| 770 | isPruned: false, |
| 771 | isInEdit: false, |
| 772 | title: "", |
| 773 | lastSessionId: undefined, |
| 774 | isSessionMetadataLoading: false, |
| 775 | allSessionMetadata: [], |
| 776 | symbols: {}, |
| 777 | codeBlockApplyStates: { |
| 778 | states: [], |
| 779 | curIndex: 0, |
| 780 | }, |
| 781 | newestToolbarPreviewForInput: {}, |
| 782 | compactionLoading: {}, |
| 783 | inlineErrorMessage: undefined, |
| 784 | }, |
| 785 | config: { |
| 786 | config: { |
| 787 | tools: [], |
| 788 | rules: [], |
| 789 | tabAutocompleteModel: undefined, |
| 790 | selectedModelByRole: { |
| 791 | chat: mockClaudeModel, |
| 792 | apply: null, |
| 793 | edit: null, |
| 794 | summarize: null, |
| 795 | autocomplete: null, |
| 796 | rerank: null, |
| 797 | embed: null, |
| 798 | }, |
| 799 | experimental: { |
| 800 | onlyUseSystemMessageTools: false, |
| 801 | }, |
| 802 | } as any, |
| 803 | lastSelectedModelByRole: { |
| 804 | chat: mockClaudeModel.title, |
| 805 | }, |
| 806 | } as any, |
| 807 | ui: { |
| 808 | toolSettings: { |
| 809 | edit_existing_file: "disabled", // Tool is disabled/rejected |
| 810 | }, |
| 811 | ruleSettings: {}, |
| 812 | showDialog: false, |
| 813 | dialogMessage: undefined, |
| 814 | } as any, |
| 815 | }); |
| 816 | |
| 817 | const mockIdeMessengerReject = mockStoreWithToolReject.mockIdeMessenger; |
| 818 | |
| 819 | // Setup successful compilation |
| 820 | mockIdeMessengerReject.request.mockResolvedValue({ |
| 821 | status: "success", |
| 822 | content: { |
| 823 | compiledChatMessages: [ |
| 824 | { role: "user", content: "Please edit this file" }, |
| 825 | ], |
| 826 | didPrune: false, |
| 827 | contextPercentage: 0.7, |
| 828 | }, |
| 829 | }); |
| 830 | |
| 831 | // Setup streaming generator with tool call that will be rejected |
| 832 | async function* mockStreamGeneratorWithRejectedTool() { |
| 833 | yield [{ role: "assistant", content: "I'll edit the file for you." }]; |
| 834 | yield [ |
| 835 | { |
| 836 | role: "assistant", |
| 837 | content: "", |
| 838 | toolCalls: [ |
| 839 | { |
| 840 | id: "tool-reject-1", |
| 841 | type: "function", |
| 842 | function: { |
| 843 | name: "edit_existing_file", |
| 844 | arguments: JSON.stringify({ |
| 845 | filepath: "test.js", |
| 846 | changes: "const x = 1;", |
| 847 | }), |
| 848 | }, |
| 849 | }, |
| 850 | ], |
| 851 | }, |
| 852 | ]; |
| 853 | return { |
| 854 | prompt: "Please edit this file", |
| 855 | completion: "I'll edit the file for you.", |
| 856 | modelProvider: "anthropic", |
| 857 | }; |
| 858 | } |
| 859 | |
| 860 | mockIdeMessengerReject.llmStreamChat.mockReturnValue( |
| 861 | mockStreamGeneratorWithRejectedTool(), |
| 862 | ); |
| 863 | |
| 864 | // Execute thunk |
| 865 | const result = await mockStoreWithToolReject.dispatch( |
| 866 | streamResponseThunk({ |
| 867 | editorState: mockEditorState, |
| 868 | modifiers: mockModifiers, |
| 869 | }) as any, |
| 870 | ); |
| 871 | |
| 872 | // Verify thunk completed successfully (tool rejection is handled gracefully) |
| 873 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 874 | |
| 875 | // Verify exact action sequence includes tool generation but rejection handling |
| 876 | const dispatchedActions = (mockStoreWithToolReject as any).getActions(); |
| 877 | expect(dispatchedActions).toEqual([ |
| 878 | { |
| 879 | type: "chat/streamResponse/pending", |
| 880 | meta: { |
| 881 | arg: { |
| 882 | editorState: mockEditorState, |
| 883 | modifiers: mockModifiers, |
| 884 | }, |
| 885 | requestId: expect.any(String), |
| 886 | requestStatus: "pending", |
| 887 | }, |
| 888 | payload: undefined, |
| 889 | }, |
| 890 | { |
| 891 | type: "chat/streamWrapper/pending", |
| 892 | meta: { |
| 893 | arg: expect.any(Function), |
| 894 | requestId: expect.any(String), |
| 895 | requestStatus: "pending", |
| 896 | }, |
| 897 | payload: undefined, |
| 898 | }, |
| 899 | { |
| 900 | type: "session/submitEditorAndInitAtIndex", |
| 901 | payload: { |
| 902 | editorState: mockEditorState, |
| 903 | index: 1, |
| 904 | }, |
| 905 | }, |
| 906 | { |
| 907 | type: "session/resetNextCodeBlockToApplyIndex", |
| 908 | payload: undefined, |
| 909 | }, |
| 910 | { |
| 911 | type: "symbols/updateFromContextItems/pending", |
| 912 | meta: { |
| 913 | arg: [], |
| 914 | requestId: expect.any(String), |
| 915 | requestStatus: "pending", |
| 916 | }, |
| 917 | payload: undefined, |
| 918 | }, |
| 919 | { |
| 920 | type: "session/updateHistoryItemAtIndex", |
| 921 | payload: { |
| 922 | index: 1, |
| 923 | updates: { |
| 924 | contextItems: [], |
| 925 | message: { |
| 926 | content: "Hello, please help me with this code", |
| 927 | id: "mock-uuid-123", |
| 928 | role: "user", |
| 929 | }, |
| 930 | }, |
| 931 | }, |
| 932 | }, |
| 933 | { |
| 934 | type: "chat/streamNormalInput/pending", |
| 935 | meta: { |
| 936 | arg: { |
| 937 | legacySlashCommandData: undefined, |
| 938 | }, |
| 939 | requestId: expect.any(String), |
| 940 | requestStatus: "pending", |
| 941 | }, |
| 942 | payload: undefined, |
| 943 | }, |
| 944 | { |
| 945 | type: "session/setAppliedRulesAtIndex", |
| 946 | payload: { |
| 947 | appliedRules: [], |
| 948 | index: 1, |
| 949 | }, |
| 950 | }, |
| 951 | { |
| 952 | type: "session/setActive", |
| 953 | payload: undefined, |
| 954 | }, |
| 955 | { |
| 956 | type: "session/setInlineErrorMessage", |
| 957 | payload: undefined, |
| 958 | }, |
| 959 | { |
| 960 | type: "session/setIsPruned", |
| 961 | payload: false, |
| 962 | }, |
| 963 | { |
| 964 | type: "session/setContextPercentage", |
| 965 | payload: 0.7, |
| 966 | }, |
| 967 | { |
| 968 | type: "symbols/updateFromContextItems/fulfilled", |
| 969 | meta: { |
| 970 | arg: [], |
| 971 | requestId: expect.any(String), |
| 972 | requestStatus: "fulfilled", |
| 973 | }, |
| 974 | payload: undefined, |
| 975 | }, |
| 976 | { |
| 977 | type: "session/streamUpdate", |
| 978 | payload: [ |
| 979 | { |
| 980 | role: "assistant", |
| 981 | content: "I'll edit the file for you.", |
| 982 | }, |
| 983 | ], |
| 984 | }, |
| 985 | { |
| 986 | type: "session/streamUpdate", |
| 987 | payload: [ |
| 988 | { |
| 989 | role: "assistant", |
| 990 | content: "", |
| 991 | toolCalls: [ |
| 992 | { |
| 993 | id: "tool-reject-1", |
| 994 | type: "function", |
| 995 | function: { |
| 996 | name: "edit_existing_file", |
| 997 | arguments: '{"filepath":"test.js","changes":"const x = 1;"}', |
| 998 | }, |
| 999 | }, |
| 1000 | ], |
| 1001 | }, |
| 1002 | ], |
| 1003 | }, |
| 1004 | { |
| 1005 | type: "session/addPromptCompletionPair", |
| 1006 | payload: [ |
| 1007 | { |
| 1008 | completion: "I'll edit the file for you.", |
| 1009 | modelProvider: "anthropic", |
| 1010 | prompt: "Please edit this file", |
| 1011 | }, |
| 1012 | ], |
| 1013 | }, |
| 1014 | { |
| 1015 | type: "session/setInactive", |
| 1016 | payload: undefined, |
| 1017 | }, |
| 1018 | { |
| 1019 | type: "session/setToolGenerated", |
| 1020 | payload: { |
| 1021 | toolCallId: "tool-reject-1", |
| 1022 | tools: [], |
| 1023 | }, |
| 1024 | }, |
| 1025 | { |
| 1026 | type: "chat/streamNormalInput/fulfilled", |
| 1027 | meta: { |
| 1028 | arg: { |
| 1029 | legacySlashCommandData: undefined, |
| 1030 | }, |
| 1031 | requestId: expect.any(String), |
| 1032 | requestStatus: "fulfilled", |
| 1033 | }, |
| 1034 | payload: undefined, |
| 1035 | }, |
| 1036 | { |
| 1037 | type: "session/saveCurrent/pending", |
| 1038 | meta: { |
| 1039 | arg: { |
| 1040 | generateTitle: true, |
| 1041 | openNewSession: false, |
| 1042 | }, |
| 1043 | requestId: expect.any(String), |
| 1044 | requestStatus: "pending", |
| 1045 | }, |
| 1046 | payload: undefined, |
| 1047 | }, |
| 1048 | { |
| 1049 | type: "session/update/pending", |
| 1050 | meta: { |
| 1051 | arg: expect.objectContaining({ |
| 1052 | history: expect.any(Array), |
| 1053 | sessionId: "session-123", |
| 1054 | title: "New Session", |
| 1055 | workspaceDirectory: "", |
| 1056 | }), |
| 1057 | requestId: expect.any(String), |
| 1058 | requestStatus: "pending", |
| 1059 | }, |
| 1060 | payload: undefined, |
| 1061 | }, |
| 1062 | { |
| 1063 | type: "session/updateSessionMetadata", |
| 1064 | payload: { |
| 1065 | sessionId: "session-123", |
| 1066 | title: "New Session", |
| 1067 | }, |
| 1068 | }, |
| 1069 | { |
| 1070 | type: "session/refreshMetadata/pending", |
| 1071 | meta: { |
| 1072 | arg: {}, |
| 1073 | requestId: expect.any(String), |
| 1074 | requestStatus: "pending", |
| 1075 | }, |
| 1076 | payload: undefined, |
| 1077 | }, |
| 1078 | { |
| 1079 | type: "session/setIsSessionMetadataLoading", |
| 1080 | payload: false, |
| 1081 | }, |
| 1082 | { |
| 1083 | type: "session/setAllSessionMetadata", |
| 1084 | payload: { |
| 1085 | compiledChatMessages: [ |
| 1086 | { |
| 1087 | content: "Please edit this file", |
| 1088 | role: "user", |
| 1089 | }, |
| 1090 | ], |
| 1091 | contextPercentage: 0.7, |
| 1092 | didPrune: false, |
| 1093 | }, |
| 1094 | }, |
| 1095 | { |
| 1096 | type: "session/refreshMetadata/fulfilled", |
| 1097 | meta: { |
| 1098 | arg: {}, |
| 1099 | requestId: expect.any(String), |
| 1100 | requestStatus: "fulfilled", |
| 1101 | }, |
| 1102 | payload: { |
| 1103 | compiledChatMessages: [ |
| 1104 | { |
| 1105 | content: "Please edit this file", |
| 1106 | role: "user", |
| 1107 | }, |
| 1108 | ], |
| 1109 | contextPercentage: 0.7, |
| 1110 | didPrune: false, |
| 1111 | }, |
| 1112 | }, |
| 1113 | { |
| 1114 | type: "session/update/fulfilled", |
| 1115 | meta: { |
| 1116 | arg: expect.objectContaining({ |
| 1117 | history: expect.any(Array), |
| 1118 | sessionId: "session-123", |
| 1119 | title: "New Session", |
| 1120 | workspaceDirectory: "", |
| 1121 | }), |
| 1122 | requestId: expect.any(String), |
| 1123 | requestStatus: "fulfilled", |
| 1124 | }, |
| 1125 | payload: undefined, |
| 1126 | }, |
| 1127 | { |
| 1128 | type: "session/saveCurrent/fulfilled", |
| 1129 | meta: { |
| 1130 | arg: { |
| 1131 | generateTitle: true, |
| 1132 | openNewSession: false, |
| 1133 | }, |
| 1134 | requestId: expect.any(String), |
| 1135 | requestStatus: "fulfilled", |
| 1136 | }, |
| 1137 | payload: undefined, |
| 1138 | }, |
| 1139 | { |
| 1140 | type: "chat/streamWrapper/fulfilled", |
| 1141 | meta: { |
| 1142 | arg: expect.any(Function), |
| 1143 | requestId: expect.any(String), |
| 1144 | requestStatus: "fulfilled", |
| 1145 | }, |
| 1146 | payload: undefined, |
| 1147 | }, |
| 1148 | { |
| 1149 | type: "chat/streamResponse/fulfilled", |
| 1150 | meta: { |
| 1151 | arg: { |
| 1152 | editorState: mockEditorState, |
| 1153 | modifiers: mockModifiers, |
| 1154 | }, |
| 1155 | requestId: expect.any(String), |
| 1156 | requestStatus: "fulfilled", |
| 1157 | }, |
| 1158 | payload: undefined, |
| 1159 | }, |
| 1160 | ]); |
| 1161 | |
| 1162 | // Verify IDE messenger calls - compilation should succeed, streaming should happen |
| 1163 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1164 | "llm/compileChat", |
| 1165 | { |
| 1166 | messages: [ |
| 1167 | { |
| 1168 | role: "system", |
| 1169 | content: "You are a helpful assistant.", |
| 1170 | }, |
| 1171 | { |
| 1172 | role: "user", |
| 1173 | content: [ |
| 1174 | { |
| 1175 | type: "text", |
| 1176 | text: "Please edit this file", |
| 1177 | }, |
| 1178 | ], |
| 1179 | }, |
| 1180 | { |
| 1181 | role: "user", |
| 1182 | content: [ |
| 1183 | { |
| 1184 | type: "text", |
| 1185 | text: "Hello, please help me with this code", |
| 1186 | }, |
| 1187 | ], |
| 1188 | }, |
| 1189 | ], |
| 1190 | options: {}, |
| 1191 | }, |
| 1192 | ); |
| 1193 | |
| 1194 | expect(mockIdeMessengerReject.llmStreamChat).toHaveBeenCalledWith( |
| 1195 | { |
| 1196 | completionOptions: {}, |
| 1197 | legacySlashCommandData: undefined, |
| 1198 | messageOptions: { precompiled: true }, |
| 1199 | messages: [ |
| 1200 | { |
| 1201 | role: "user", |
| 1202 | content: "Please edit this file", |
| 1203 | }, |
| 1204 | ], |
| 1205 | title: "Claude 3.5 Sonnet", |
| 1206 | }, |
| 1207 | expect.any(AbortSignal), |
| 1208 | ); |
| 1209 | |
| 1210 | // Should NOT call tools/call since tool is disabled/rejected |
| 1211 | expect(mockIdeMessengerReject.request).not.toHaveBeenCalledWith( |
| 1212 | "tools/call", |
| 1213 | expect.anything(), |
| 1214 | ); |
| 1215 | |
| 1216 | // Verify session save was called |
| 1217 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1218 | "history/save", |
| 1219 | expect.anything(), |
| 1220 | ); |
| 1221 | |
| 1222 | // Verify final state contains the tool call but it's not executed |
| 1223 | const finalState = mockStoreWithToolReject.getState(); |
| 1224 | expect(finalState).toEqual({ |
| 1225 | session: { |
| 1226 | history: [ |
| 1227 | { |
| 1228 | contextItems: [], |
| 1229 | message: { |
| 1230 | id: "1", |
| 1231 | role: "user", |
| 1232 | content: "Please edit this file", |
| 1233 | }, |
| 1234 | }, |
| 1235 | { |
| 1236 | appliedRules: [], |
| 1237 | contextItems: [], |
| 1238 | editorState: mockEditorState, |
| 1239 | message: { |
| 1240 | content: "Hello, please help me with this code", |
| 1241 | id: expect.any(String), |
| 1242 | role: "user", |
| 1243 | }, |
| 1244 | }, |
| 1245 | { |
| 1246 | contextItems: [], |
| 1247 | isGatheringContext: false, |
| 1248 | message: { |
| 1249 | content: "I'll edit the file for you.", |
| 1250 | id: "mock-uuid-123", |
| 1251 | role: "assistant", |
| 1252 | toolCalls: [ |
| 1253 | { |
| 1254 | id: "tool-reject-1", |
| 1255 | type: "function", |
| 1256 | function: { |
| 1257 | name: "edit_existing_file", |
| 1258 | arguments: JSON.stringify({ |
| 1259 | filepath: "test.js", |
| 1260 | changes: "const x = 1;", |
| 1261 | }), |
| 1262 | }, |
| 1263 | }, |
| 1264 | ], |
| 1265 | }, |
| 1266 | promptLogs: [ |
| 1267 | { |
| 1268 | completion: "I'll edit the file for you.", |
| 1269 | modelProvider: "anthropic", |
| 1270 | prompt: "Please edit this file", |
| 1271 | }, |
| 1272 | ], |
| 1273 | toolCallStates: [ |
| 1274 | { |
| 1275 | toolCallId: "tool-reject-1", |
| 1276 | toolCall: { |
| 1277 | id: "tool-reject-1", |
| 1278 | type: "function", |
| 1279 | function: { |
| 1280 | name: "edit_existing_file", |
| 1281 | arguments: JSON.stringify({ |
| 1282 | filepath: "test.js", |
| 1283 | changes: "const x = 1;", |
| 1284 | }), |
| 1285 | }, |
| 1286 | }, |
| 1287 | parsedArgs: { |
| 1288 | filepath: "test.js", |
| 1289 | changes: "const x = 1;", |
| 1290 | }, |
| 1291 | status: "generated", // Tool call exists but isn't executed due to settings |
| 1292 | }, |
| 1293 | ], |
| 1294 | }, |
| 1295 | ], |
| 1296 | hasReasoningEnabled: false, |
| 1297 | isStreaming: false, |
| 1298 | id: "session-123", |
| 1299 | mode: "chat", |
| 1300 | streamAborter: expect.any(AbortController), |
| 1301 | contextPercentage: 0.7, |
| 1302 | isPruned: false, |
| 1303 | isInEdit: false, |
| 1304 | title: "New Session", |
| 1305 | lastSessionId: undefined, |
| 1306 | isSessionMetadataLoading: false, |
| 1307 | allSessionMetadata: { |
| 1308 | compiledChatMessages: [ |
| 1309 | { |
| 1310 | content: "Please edit this file", |
| 1311 | role: "user", |
| 1312 | }, |
| 1313 | ], |
| 1314 | contextPercentage: 0.7, |
| 1315 | didPrune: false, |
| 1316 | }, |
| 1317 | symbols: {}, |
| 1318 | codeBlockApplyStates: { |
| 1319 | states: [], |
| 1320 | curIndex: 0, |
| 1321 | }, |
| 1322 | newestToolbarPreviewForInput: {}, |
| 1323 | compactionLoading: {}, |
| 1324 | inlineErrorMessage: undefined, |
| 1325 | }, |
| 1326 | config: { |
| 1327 | config: { |
| 1328 | tools: [], |
| 1329 | rules: [], |
| 1330 | tabAutocompleteModel: undefined, |
| 1331 | selectedModelByRole: { |
| 1332 | chat: mockClaudeModel, |
| 1333 | apply: null, |
| 1334 | edit: null, |
| 1335 | summarize: null, |
| 1336 | autocomplete: null, |
| 1337 | rerank: null, |
| 1338 | embed: null, |
| 1339 | }, |
| 1340 | experimental: { |
| 1341 | onlyUseSystemMessageTools: false, |
| 1342 | }, |
| 1343 | }, |
| 1344 | lastSelectedModelByRole: { |
| 1345 | chat: mockClaudeModel.title, |
| 1346 | }, |
| 1347 | loading: false, |
| 1348 | configError: undefined, |
| 1349 | }, |
| 1350 | ui: { |
| 1351 | toolSettings: { |
| 1352 | edit_existing_file: "disabled", |
| 1353 | }, |
| 1354 | ruleSettings: {}, |
| 1355 | showDialog: false, |
| 1356 | dialogMessage: undefined, |
| 1357 | }, |
| 1358 | editModeState: { |
| 1359 | isInEdit: false, |
| 1360 | returnToMode: "chat", |
| 1361 | }, |
| 1362 | indexing: { |
| 1363 | indexing: { |
| 1364 | statuses: {}, |
| 1365 | hiddenChatPeekTypes: { |
| 1366 | docs: false, |
| 1367 | }, |
| 1368 | }, |
| 1369 | }, |
| 1370 | tabs: { |
| 1371 | tabsItems: [], |
| 1372 | }, |
| 1373 | profiles: { |
| 1374 | organizations: [], |
| 1375 | selectedProfileId: null, |
| 1376 | selectedOrganizationId: null, |
| 1377 | preferencesByProfileId: {}, |
| 1378 | }, |
| 1379 | }); |
| 1380 | }); |
| 1381 | |
| 1382 | it("should handle tool call requiring manual approval", async () => { |
| 1383 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 1384 | |
| 1385 | // Create store with tool settings that require manual approval (ask first) |
| 1386 | const mockStoreWithManualApproval = createMockStore({ |
| 1387 | session: { |
| 1388 | history: [ |
| 1389 | { |
| 1390 | message: { |
| 1391 | id: "1", |
| 1392 | role: "user", |
| 1393 | content: "Please search the codebase", |
| 1394 | }, |
| 1395 | contextItems: [], |
| 1396 | }, |
| 1397 | ], |
| 1398 | hasReasoningEnabled: false, |
| 1399 | isStreaming: true, |
| 1400 | id: "session-123", |
| 1401 | mode: "chat", |
| 1402 | streamAborter: new AbortController(), |
| 1403 | contextPercentage: 0, |
| 1404 | isPruned: false, |
| 1405 | isInEdit: false, |
| 1406 | title: "", |
| 1407 | lastSessionId: undefined, |
| 1408 | isSessionMetadataLoading: false, |
| 1409 | allSessionMetadata: [], |
| 1410 | symbols: {}, |
| 1411 | codeBlockApplyStates: { |
| 1412 | states: [], |
| 1413 | curIndex: 0, |
| 1414 | }, |
| 1415 | newestToolbarPreviewForInput: {}, |
| 1416 | compactionLoading: {}, |
| 1417 | inlineErrorMessage: undefined, |
| 1418 | }, |
| 1419 | config: { |
| 1420 | config: { |
| 1421 | tools: [], |
| 1422 | rules: [], |
| 1423 | tabAutocompleteModel: undefined, |
| 1424 | selectedModelByRole: { |
| 1425 | chat: mockClaudeModel, |
| 1426 | apply: null, |
| 1427 | edit: null, |
| 1428 | summarize: null, |
| 1429 | autocomplete: null, |
| 1430 | rerank: null, |
| 1431 | embed: null, |
| 1432 | }, |
| 1433 | experimental: { |
| 1434 | onlyUseSystemMessageTools: false, |
| 1435 | }, |
| 1436 | } as any, |
| 1437 | lastSelectedModelByRole: { |
| 1438 | chat: mockClaudeModel.title, |
| 1439 | }, |
| 1440 | } as any, |
| 1441 | ui: { |
| 1442 | toolSettings: { |
| 1443 | search_codebase: "askFirst", // Requires manual approval |
| 1444 | }, |
| 1445 | ruleSettings: {}, |
| 1446 | showDialog: false, |
| 1447 | dialogMessage: undefined, |
| 1448 | } as any, |
| 1449 | }); |
| 1450 | |
| 1451 | const mockIdeMessengerManual = mockStoreWithManualApproval.mockIdeMessenger; |
| 1452 | |
| 1453 | // Setup successful compilation |
| 1454 | mockIdeMessengerManual.request.mockResolvedValue({ |
| 1455 | status: "success", |
| 1456 | content: { |
| 1457 | compiledChatMessages: [ |
| 1458 | { role: "user", content: "Please search the codebase" }, |
| 1459 | ], |
| 1460 | didPrune: false, |
| 1461 | contextPercentage: 0.9, |
| 1462 | }, |
| 1463 | }); |
| 1464 | |
| 1465 | // Setup streaming generator with tool call requiring approval |
| 1466 | async function* mockStreamGeneratorWithApprovalTool() { |
| 1467 | yield [ |
| 1468 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 1469 | ]; |
| 1470 | yield [ |
| 1471 | { |
| 1472 | role: "assistant", |
| 1473 | content: "", |
| 1474 | toolCalls: [ |
| 1475 | { |
| 1476 | id: "tool-approval-1", |
| 1477 | type: "function", |
| 1478 | function: { |
| 1479 | name: "search_codebase", |
| 1480 | arguments: JSON.stringify({ query: "test function" }), |
| 1481 | }, |
| 1482 | }, |
| 1483 | ], |
| 1484 | }, |
| 1485 | ]; |
| 1486 | return { |
| 1487 | prompt: "Please search the codebase", |
| 1488 | completion: "I'll search the codebase for you.", |
| 1489 | modelProvider: "anthropic", |
| 1490 | }; |
| 1491 | } |
| 1492 | |
| 1493 | mockIdeMessengerManual.llmStreamChat.mockReturnValue( |
| 1494 | mockStreamGeneratorWithApprovalTool(), |
| 1495 | ); |
| 1496 | |
| 1497 | // Track isStreaming state changes to ensure it doesn't become false after tool generation |
| 1498 | const streamingStates: boolean[] = []; |
| 1499 | const originalDispatch = mockStoreWithManualApproval.dispatch; |
| 1500 | mockStoreWithManualApproval.dispatch = ((action: any) => { |
| 1501 | const result = originalDispatch(action); |
| 1502 | // Capture isStreaming state after each action |
| 1503 | const currentState = |
| 1504 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1505 | streamingStates.push(currentState); |
| 1506 | return result; |
| 1507 | }) as any; |
| 1508 | |
| 1509 | // Execute thunk |
| 1510 | const result = await mockStoreWithManualApproval.dispatch( |
| 1511 | streamResponseThunk({ |
| 1512 | editorState: mockEditorState, |
| 1513 | modifiers: mockModifiers, |
| 1514 | }) as any, |
| 1515 | ); |
| 1516 | |
| 1517 | // Verify thunk completed successfully (tool waits for approval) |
| 1518 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 1519 | |
| 1520 | // Since tool requires approval, isStreaming should become false after completion |
| 1521 | // but should stay true throughout the initial streaming phase |
| 1522 | const finalStreamingState = |
| 1523 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1524 | expect(finalStreamingState).toBe(false); // Final state should be false since we're waiting for approval |
| 1525 | |
| 1526 | // Verify exact action sequence includes tool generation but NO execution |
| 1527 | const dispatchedActions = (mockStoreWithManualApproval as any).getActions(); |
| 1528 | expect(dispatchedActions).toEqual([ |
| 1529 | { |
| 1530 | type: "chat/streamResponse/pending", |
| 1531 | meta: { |
| 1532 | arg: { |
| 1533 | editorState: mockEditorState, |
| 1534 | modifiers: mockModifiers, |
| 1535 | }, |
| 1536 | requestId: expect.any(String), |
| 1537 | requestStatus: "pending", |
| 1538 | }, |
| 1539 | payload: undefined, |
| 1540 | }, |
| 1541 | { |
| 1542 | type: "chat/streamWrapper/pending", |
| 1543 | meta: { |
| 1544 | arg: expect.any(Function), |
| 1545 | requestId: expect.any(String), |
| 1546 | requestStatus: "pending", |
| 1547 | }, |
| 1548 | payload: undefined, |
| 1549 | }, |
| 1550 | { |
| 1551 | type: "session/submitEditorAndInitAtIndex", |
| 1552 | payload: { |
| 1553 | editorState: mockEditorState, |
| 1554 | index: 1, |
| 1555 | }, |
| 1556 | }, |
| 1557 | { |
| 1558 | type: "session/resetNextCodeBlockToApplyIndex", |
| 1559 | payload: undefined, |
| 1560 | }, |
| 1561 | { |
| 1562 | type: "symbols/updateFromContextItems/pending", |
| 1563 | meta: { |
| 1564 | arg: [], |
| 1565 | requestId: expect.any(String), |
| 1566 | requestStatus: "pending", |
| 1567 | }, |
| 1568 | payload: undefined, |
| 1569 | }, |
| 1570 | { |
| 1571 | type: "session/updateHistoryItemAtIndex", |
| 1572 | payload: { |
| 1573 | index: 1, |
| 1574 | updates: { |
| 1575 | contextItems: [], |
| 1576 | message: { |
| 1577 | content: "Hello, please help me with this code", |
| 1578 | id: "mock-uuid-123", |
| 1579 | role: "user", |
| 1580 | }, |
| 1581 | }, |
| 1582 | }, |
| 1583 | }, |
| 1584 | { |
| 1585 | type: "chat/streamNormalInput/pending", |
| 1586 | meta: { |
| 1587 | arg: { |
| 1588 | legacySlashCommandData: undefined, |
| 1589 | }, |
| 1590 | requestId: expect.any(String), |
| 1591 | requestStatus: "pending", |
| 1592 | }, |
| 1593 | payload: undefined, |
| 1594 | }, |
| 1595 | { |
| 1596 | type: "session/setAppliedRulesAtIndex", |
| 1597 | payload: { |
| 1598 | appliedRules: [], |
| 1599 | index: 1, |
| 1600 | }, |
| 1601 | }, |
| 1602 | { |
| 1603 | type: "session/setActive", |
| 1604 | payload: undefined, |
| 1605 | }, |
| 1606 | { |
| 1607 | type: "session/setInlineErrorMessage", |
| 1608 | payload: undefined, |
| 1609 | }, |
| 1610 | { |
| 1611 | type: "session/setIsPruned", |
| 1612 | payload: false, |
| 1613 | }, |
| 1614 | { |
| 1615 | type: "session/setContextPercentage", |
| 1616 | payload: 0.9, |
| 1617 | }, |
| 1618 | { |
| 1619 | type: "symbols/updateFromContextItems/fulfilled", |
| 1620 | meta: { |
| 1621 | arg: [], |
| 1622 | requestId: expect.any(String), |
| 1623 | requestStatus: "fulfilled", |
| 1624 | }, |
| 1625 | payload: undefined, |
| 1626 | }, |
| 1627 | { |
| 1628 | type: "session/streamUpdate", |
| 1629 | payload: [ |
| 1630 | { |
| 1631 | role: "assistant", |
| 1632 | content: "I'll search the codebase for you.", |
| 1633 | }, |
| 1634 | ], |
| 1635 | }, |
| 1636 | { |
| 1637 | type: "session/streamUpdate", |
| 1638 | payload: [ |
| 1639 | { |
| 1640 | role: "assistant", |
| 1641 | content: "", |
| 1642 | toolCalls: [ |
| 1643 | { |
| 1644 | id: "tool-approval-1", |
| 1645 | type: "function", |
| 1646 | function: { |
| 1647 | name: "search_codebase", |
| 1648 | arguments: JSON.stringify({ query: "test function" }), |
| 1649 | }, |
| 1650 | }, |
| 1651 | ], |
| 1652 | }, |
| 1653 | ], |
| 1654 | }, |
| 1655 | { |
| 1656 | type: "session/addPromptCompletionPair", |
| 1657 | payload: [ |
| 1658 | { |
| 1659 | completion: "I'll search the codebase for you.", |
| 1660 | modelProvider: "anthropic", |
| 1661 | prompt: "Please search the codebase", |
| 1662 | }, |
| 1663 | ], |
| 1664 | }, |
| 1665 | { |
| 1666 | type: "session/setInactive", |
| 1667 | payload: undefined, |
| 1668 | }, |
| 1669 | { |
| 1670 | type: "session/setToolGenerated", |
| 1671 | payload: { |
| 1672 | toolCallId: "tool-approval-1", |
| 1673 | tools: [], |
| 1674 | }, |
| 1675 | }, |
| 1676 | { |
| 1677 | type: "chat/streamNormalInput/fulfilled", |
| 1678 | meta: { |
| 1679 | arg: { |
| 1680 | legacySlashCommandData: undefined, |
| 1681 | }, |
| 1682 | requestId: expect.any(String), |
| 1683 | requestStatus: "fulfilled", |
| 1684 | }, |
| 1685 | payload: undefined, |
| 1686 | }, |
| 1687 | { |
| 1688 | type: "session/saveCurrent/pending", |
| 1689 | meta: { |
| 1690 | arg: { |
| 1691 | generateTitle: true, |
| 1692 | openNewSession: false, |
| 1693 | }, |
| 1694 | requestId: expect.any(String), |
| 1695 | requestStatus: "pending", |
| 1696 | }, |
| 1697 | payload: undefined, |
| 1698 | }, |
| 1699 | { |
| 1700 | type: "session/update/pending", |
| 1701 | meta: { |
| 1702 | arg: expect.objectContaining({ |
| 1703 | history: expect.any(Array), |
| 1704 | sessionId: "session-123", |
| 1705 | title: "New Session", |
| 1706 | workspaceDirectory: "", |
| 1707 | }), |
| 1708 | requestId: expect.any(String), |
| 1709 | requestStatus: "pending", |
| 1710 | }, |
| 1711 | payload: undefined, |
| 1712 | }, |
| 1713 | { |
| 1714 | type: "session/updateSessionMetadata", |
| 1715 | payload: { |
| 1716 | sessionId: "session-123", |
| 1717 | title: "New Session", |
| 1718 | }, |
| 1719 | }, |
| 1720 | { |
| 1721 | type: "session/refreshMetadata/pending", |
| 1722 | meta: { |
| 1723 | arg: {}, |
| 1724 | requestId: expect.any(String), |
| 1725 | requestStatus: "pending", |
| 1726 | }, |
| 1727 | payload: undefined, |
| 1728 | }, |
| 1729 | { |
| 1730 | type: "session/setIsSessionMetadataLoading", |
| 1731 | payload: false, |
| 1732 | }, |
| 1733 | { |
| 1734 | type: "session/setAllSessionMetadata", |
| 1735 | payload: { |
| 1736 | compiledChatMessages: [ |
| 1737 | { |
| 1738 | content: "Please search the codebase", |
| 1739 | role: "user", |
| 1740 | }, |
| 1741 | ], |
| 1742 | contextPercentage: 0.9, |
| 1743 | didPrune: false, |
| 1744 | }, |
| 1745 | }, |
| 1746 | { |
| 1747 | type: "session/refreshMetadata/fulfilled", |
| 1748 | meta: { |
| 1749 | arg: {}, |
| 1750 | requestId: expect.any(String), |
| 1751 | requestStatus: "fulfilled", |
| 1752 | }, |
| 1753 | payload: { |
| 1754 | compiledChatMessages: [ |
| 1755 | { |
| 1756 | content: "Please search the codebase", |
| 1757 | role: "user", |
| 1758 | }, |
| 1759 | ], |
| 1760 | contextPercentage: 0.9, |
| 1761 | didPrune: false, |
| 1762 | }, |
| 1763 | }, |
| 1764 | { |
| 1765 | type: "session/update/fulfilled", |
| 1766 | meta: { |
| 1767 | arg: expect.objectContaining({ |
| 1768 | history: expect.any(Array), |
| 1769 | sessionId: "session-123", |
| 1770 | title: "New Session", |
| 1771 | workspaceDirectory: "", |
| 1772 | }), |
| 1773 | requestId: expect.any(String), |
| 1774 | requestStatus: "fulfilled", |
| 1775 | }, |
| 1776 | payload: undefined, |
| 1777 | }, |
| 1778 | { |
| 1779 | type: "session/saveCurrent/fulfilled", |
| 1780 | meta: { |
| 1781 | arg: { |
| 1782 | generateTitle: true, |
| 1783 | openNewSession: false, |
| 1784 | }, |
| 1785 | requestId: expect.any(String), |
| 1786 | requestStatus: "fulfilled", |
| 1787 | }, |
| 1788 | payload: undefined, |
| 1789 | }, |
| 1790 | { |
| 1791 | type: "chat/streamWrapper/fulfilled", |
| 1792 | meta: { |
| 1793 | arg: expect.any(Function), |
| 1794 | requestId: expect.any(String), |
| 1795 | requestStatus: "fulfilled", |
| 1796 | }, |
| 1797 | payload: undefined, |
| 1798 | }, |
| 1799 | { |
| 1800 | type: "chat/streamResponse/fulfilled", |
| 1801 | meta: { |
| 1802 | arg: { |
| 1803 | editorState: mockEditorState, |
| 1804 | modifiers: mockModifiers, |
| 1805 | }, |
| 1806 | requestId: expect.any(String), |
| 1807 | requestStatus: "fulfilled", |
| 1808 | }, |
| 1809 | payload: undefined, |
| 1810 | }, |
| 1811 | ]); |
| 1812 | |
| 1813 | // Verify IDE messenger calls - compilation should happen, streaming should happen |
| 1814 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1815 | "llm/compileChat", |
| 1816 | { |
| 1817 | messages: [ |
| 1818 | { |
| 1819 | role: "system", |
| 1820 | content: "You are a helpful assistant.", |
| 1821 | }, |
| 1822 | { |
| 1823 | role: "user", |
| 1824 | content: [ |
| 1825 | { |
| 1826 | type: "text", |
| 1827 | text: "Please search the codebase", |
| 1828 | }, |
| 1829 | ], |
| 1830 | }, |
| 1831 | { |
| 1832 | role: "user", |
| 1833 | content: [ |
| 1834 | { |
| 1835 | type: "text", |
| 1836 | text: "Hello, please help me with this code", |
| 1837 | }, |
| 1838 | ], |
| 1839 | }, |
| 1840 | ], |
| 1841 | options: {}, |
| 1842 | }, |
| 1843 | ); |
| 1844 | |
| 1845 | expect(mockIdeMessengerManual.llmStreamChat).toHaveBeenCalledWith( |
| 1846 | { |
| 1847 | completionOptions: {}, |
| 1848 | legacySlashCommandData: undefined, |
| 1849 | messageOptions: { precompiled: true }, |
| 1850 | messages: [ |
| 1851 | { |
| 1852 | role: "user", |
| 1853 | content: "Please search the codebase", |
| 1854 | }, |
| 1855 | ], |
| 1856 | title: "Claude 3.5 Sonnet", |
| 1857 | }, |
| 1858 | expect.any(AbortSignal), |
| 1859 | ); |
| 1860 | |
| 1861 | // Should NOT call tools/call since tool requires approval |
| 1862 | expect(mockIdeMessengerManual.request).not.toHaveBeenCalledWith( |
| 1863 | "tools/call", |
| 1864 | expect.anything(), |
| 1865 | ); |
| 1866 | |
| 1867 | // Verify session save was called |
| 1868 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1869 | "history/save", |
| 1870 | expect.anything(), |
| 1871 | ); |
| 1872 | |
| 1873 | // Verify final state contains the tool call in "generated" state (waiting for approval) |
| 1874 | const finalState = mockStoreWithManualApproval.getState(); |
| 1875 | expect(finalState).toEqual({ |
| 1876 | session: { |
| 1877 | history: [ |
| 1878 | { |
| 1879 | contextItems: [], |
| 1880 | message: { |
| 1881 | id: "1", |
| 1882 | role: "user", |
| 1883 | content: "Please search the codebase", |
| 1884 | }, |
| 1885 | }, |
| 1886 | { |
| 1887 | appliedRules: [], |
| 1888 | contextItems: [], |
| 1889 | editorState: mockEditorState, |
| 1890 | message: { |
| 1891 | content: "Hello, please help me with this code", |
| 1892 | id: expect.any(String), |
| 1893 | role: "user", |
| 1894 | }, |
| 1895 | }, |
| 1896 | { |
| 1897 | contextItems: [], |
| 1898 | isGatheringContext: false, |
| 1899 | message: { |
| 1900 | content: "I'll search the codebase for you.", |
| 1901 | id: "mock-uuid-123", |
| 1902 | role: "assistant", |
| 1903 | toolCalls: [ |
| 1904 | { |
| 1905 | id: "tool-approval-1", |
| 1906 | type: "function", |
| 1907 | function: { |
| 1908 | name: "search_codebase", |
| 1909 | arguments: JSON.stringify({ query: "test function" }), |
| 1910 | }, |
| 1911 | }, |
| 1912 | ], |
| 1913 | }, |
| 1914 | promptLogs: [ |
| 1915 | { |
| 1916 | completion: "I'll search the codebase for you.", |
| 1917 | modelProvider: "anthropic", |
| 1918 | prompt: "Please search the codebase", |
| 1919 | }, |
| 1920 | ], |
| 1921 | toolCallStates: [ |
| 1922 | { |
| 1923 | toolCallId: "tool-approval-1", |
| 1924 | toolCall: { |
| 1925 | id: "tool-approval-1", |
| 1926 | type: "function", |
| 1927 | function: { |
| 1928 | name: "search_codebase", |
| 1929 | arguments: JSON.stringify({ query: "test function" }), |
| 1930 | }, |
| 1931 | }, |
| 1932 | parsedArgs: { query: "test function" }, |
| 1933 | status: "generated", // Waiting for user approval |
| 1934 | }, |
| 1935 | ], |
| 1936 | }, |
| 1937 | ], |
| 1938 | hasReasoningEnabled: false, |
| 1939 | isStreaming: false, |
| 1940 | id: "session-123", |
| 1941 | mode: "chat", |
| 1942 | streamAborter: expect.any(AbortController), |
| 1943 | contextPercentage: 0.9, |
| 1944 | isPruned: false, |
| 1945 | isInEdit: false, |
| 1946 | title: "New Session", |
| 1947 | lastSessionId: undefined, |
| 1948 | isSessionMetadataLoading: false, |
| 1949 | allSessionMetadata: { |
| 1950 | compiledChatMessages: [ |
| 1951 | { |
| 1952 | content: "Please search the codebase", |
| 1953 | role: "user", |
| 1954 | }, |
| 1955 | ], |
| 1956 | contextPercentage: 0.9, |
| 1957 | didPrune: false, |
| 1958 | }, |
| 1959 | symbols: {}, |
| 1960 | codeBlockApplyStates: { |
| 1961 | states: [], |
| 1962 | curIndex: 0, |
| 1963 | }, |
| 1964 | newestToolbarPreviewForInput: {}, |
| 1965 | compactionLoading: {}, |
| 1966 | inlineErrorMessage: undefined, |
| 1967 | }, |
| 1968 | config: { |
| 1969 | config: { |
| 1970 | tools: [], |
| 1971 | rules: [], |
| 1972 | tabAutocompleteModel: undefined, |
| 1973 | selectedModelByRole: { |
| 1974 | chat: mockClaudeModel, |
| 1975 | apply: null, |
| 1976 | edit: null, |
| 1977 | summarize: null, |
| 1978 | autocomplete: null, |
| 1979 | rerank: null, |
| 1980 | embed: null, |
| 1981 | }, |
| 1982 | experimental: { |
| 1983 | onlyUseSystemMessageTools: false, |
| 1984 | }, |
| 1985 | }, |
| 1986 | lastSelectedModelByRole: { |
| 1987 | chat: mockClaudeModel.title, |
| 1988 | }, |
| 1989 | loading: false, |
| 1990 | configError: undefined, |
| 1991 | }, |
| 1992 | ui: { |
| 1993 | toolSettings: { |
| 1994 | search_codebase: "askFirst", |
| 1995 | }, |
| 1996 | ruleSettings: {}, |
| 1997 | showDialog: false, |
| 1998 | dialogMessage: undefined, |
| 1999 | }, |
| 2000 | editModeState: { |
| 2001 | isInEdit: false, |
| 2002 | returnToMode: "chat", |
| 2003 | }, |
| 2004 | indexing: { |
| 2005 | indexing: { |
| 2006 | statuses: {}, |
| 2007 | hiddenChatPeekTypes: { |
| 2008 | docs: false, |
| 2009 | }, |
| 2010 | }, |
| 2011 | }, |
| 2012 | tabs: { |
| 2013 | tabsItems: [], |
| 2014 | }, |
| 2015 | profiles: { |
| 2016 | organizations: [], |
| 2017 | selectedProfileId: null, |
| 2018 | selectedOrganizationId: null, |
| 2019 | preferencesByProfileId: {}, |
| 2020 | }, |
| 2021 | }); |
| 2022 | }); |
| 2023 | |
| 2024 | it("should handle complete user approval and tool execution flow", async () => { |
| 2025 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 2026 | |
| 2027 | // Create store with tool settings that require manual approval |
| 2028 | const mockStoreWithApproval = createMockStore({ |
| 2029 | session: { |
| 2030 | history: [ |
| 2031 | { |
| 2032 | message: { |
| 2033 | id: "1", |
| 2034 | role: "user", |
| 2035 | content: "Please search the codebase for test functions", |
| 2036 | }, |
| 2037 | contextItems: [], |
| 2038 | }, |
| 2039 | ], |
| 2040 | hasReasoningEnabled: false, |
| 2041 | isStreaming: true, |
| 2042 | id: "session-123", |
| 2043 | mode: "chat", |
| 2044 | streamAborter: new AbortController(), |
| 2045 | contextPercentage: 0, |
| 2046 | isPruned: false, |
| 2047 | isInEdit: false, |
| 2048 | title: "", |
| 2049 | lastSessionId: undefined, |
| 2050 | isSessionMetadataLoading: false, |
| 2051 | allSessionMetadata: [], |
| 2052 | symbols: {}, |
| 2053 | codeBlockApplyStates: { |
| 2054 | states: [], |
| 2055 | curIndex: 0, |
| 2056 | }, |
| 2057 | newestToolbarPreviewForInput: {}, |
| 2058 | compactionLoading: {}, |
| 2059 | inlineErrorMessage: undefined, |
| 2060 | }, |
| 2061 | config: { |
| 2062 | config: { |
| 2063 | tools: [], |
| 2064 | rules: [], |
| 2065 | tabAutocompleteModel: undefined, |
| 2066 | selectedModelByRole: { |
| 2067 | chat: mockClaudeModel, |
| 2068 | apply: null, |
| 2069 | edit: null, |
| 2070 | summarize: null, |
| 2071 | autocomplete: null, |
| 2072 | rerank: null, |
| 2073 | embed: null, |
| 2074 | }, |
| 2075 | experimental: { |
| 2076 | onlyUseSystemMessageTools: false, |
| 2077 | }, |
| 2078 | } as any, |
| 2079 | lastSelectedModelByRole: { |
| 2080 | chat: mockClaudeModel.title, |
| 2081 | }, |
| 2082 | } as any, |
| 2083 | ui: { |
| 2084 | toolSettings: { |
| 2085 | search_codebase: "askFirst", // Requires manual approval |
| 2086 | }, |
| 2087 | ruleSettings: {}, |
| 2088 | showDialog: false, |
| 2089 | dialogMessage: undefined, |
| 2090 | } as any, |
| 2091 | }); |
| 2092 | |
| 2093 | const mockIdeMessengerApproval = mockStoreWithApproval.mockIdeMessenger; |
| 2094 | |
| 2095 | // Setup successful compilation |
| 2096 | mockIdeMessengerApproval.request.mockImplementation( |
| 2097 | (endpoint: string, data: any) => { |
| 2098 | if (endpoint === "llm/compileChat") { |
| 2099 | return Promise.resolve({ |
| 2100 | status: "success", |
| 2101 | content: { |
| 2102 | compiledChatMessages: [ |
| 2103 | { |
| 2104 | role: "user", |
| 2105 | content: "Please search the codebase for test functions", |
| 2106 | }, |
| 2107 | ], |
| 2108 | didPrune: false, |
| 2109 | contextPercentage: 0.85, |
| 2110 | }, |
| 2111 | }); |
| 2112 | } else if (endpoint === "tools/call") { |
| 2113 | // Mock server-side tool call response for search_codebase |
| 2114 | return Promise.resolve({ |
| 2115 | status: "success", |
| 2116 | content: { |
| 2117 | contextItems: [ |
| 2118 | { |
| 2119 | name: "Search Results", |
| 2120 | description: "Found test functions", |
| 2121 | content: |
| 2122 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2123 | icon: "search", |
| 2124 | hidden: false, |
| 2125 | }, |
| 2126 | ], |
| 2127 | errorMessage: undefined, |
| 2128 | }, |
| 2129 | }); |
| 2130 | } else if (endpoint === "history/save") { |
| 2131 | return Promise.resolve({ status: "success" }); |
| 2132 | } else if (endpoint === "history/list") { |
| 2133 | return Promise.resolve({ status: "success", content: [] }); |
| 2134 | } |
| 2135 | return Promise.resolve({ status: "success", content: {} }); |
| 2136 | }, |
| 2137 | ); |
| 2138 | |
| 2139 | // Setup streaming generator with tool call |
| 2140 | async function* mockStreamGeneratorWithApprovalFlow() { |
| 2141 | yield [ |
| 2142 | { |
| 2143 | role: "assistant", |
| 2144 | content: "I'll search for test functions in the codebase.", |
| 2145 | }, |
| 2146 | ]; |
| 2147 | yield [ |
| 2148 | { |
| 2149 | role: "assistant", |
| 2150 | content: "", |
| 2151 | toolCalls: [ |
| 2152 | { |
| 2153 | id: "tool-approval-flow-1", |
| 2154 | type: "function", |
| 2155 | function: { |
| 2156 | name: "search_codebase", |
| 2157 | arguments: JSON.stringify({ query: "test function" }), |
| 2158 | }, |
| 2159 | }, |
| 2160 | ], |
| 2161 | }, |
| 2162 | ]; |
| 2163 | return { |
| 2164 | prompt: "Please search the codebase for test functions", |
| 2165 | completion: "I'll search for test functions in the codebase.", |
| 2166 | modelProvider: "anthropic", |
| 2167 | }; |
| 2168 | } |
| 2169 | |
| 2170 | // Mock subsequent streaming calls (after tool execution) |
| 2171 | let streamCallCount = 0; |
| 2172 | mockIdeMessengerApproval.llmStreamChat.mockImplementation(() => { |
| 2173 | streamCallCount++; |
| 2174 | if (streamCallCount === 1) { |
| 2175 | // First call - initial streaming with tool call |
| 2176 | return mockStreamGeneratorWithApprovalFlow(); |
| 2177 | } else { |
| 2178 | // Subsequent calls from streamResponseAfterToolCall |
| 2179 | async function* followupGenerator() { |
| 2180 | yield [ |
| 2181 | { |
| 2182 | role: "assistant", |
| 2183 | content: |
| 2184 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2185 | }, |
| 2186 | ]; |
| 2187 | return { |
| 2188 | prompt: "continuing after tool execution", |
| 2189 | completion: |
| 2190 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2191 | modelProvider: "anthropic", |
| 2192 | }; |
| 2193 | } |
| 2194 | return followupGenerator(); |
| 2195 | } |
| 2196 | }); |
| 2197 | |
| 2198 | // Track isStreaming state changes throughout the entire test |
| 2199 | const streamingStateChanges: boolean[] = []; |
| 2200 | let lastStreamingState = |
| 2201 | mockStoreWithApproval.getState().session.isStreaming; |
| 2202 | const originalDispatch = mockStoreWithApproval.dispatch; |
| 2203 | mockStoreWithApproval.dispatch = ((action: any) => { |
| 2204 | const result = originalDispatch(action); |
| 2205 | const currentStreamingState = |
| 2206 | mockStoreWithApproval.getState().session.isStreaming; |
| 2207 | // Only record when the state actually changes |
| 2208 | if (currentStreamingState !== lastStreamingState) { |
| 2209 | streamingStateChanges.push(currentStreamingState); |
| 2210 | lastStreamingState = currentStreamingState; |
| 2211 | } |
| 2212 | return result; |
| 2213 | }) as any; |
| 2214 | |
| 2215 | // Execute initial thunk - this should generate the tool call but not execute it |
| 2216 | const initialResult = await mockStoreWithApproval.dispatch( |
| 2217 | streamResponseThunk({ |
| 2218 | editorState: mockEditorState, |
| 2219 | modifiers: mockModifiers, |
| 2220 | }) as any, |
| 2221 | ); |
| 2222 | |
| 2223 | // Verify initial streaming completed successfully |
| 2224 | expect(initialResult.type).toBe("chat/streamResponse/fulfilled"); |
| 2225 | |
| 2226 | // Verify exact initial action sequence by comparing action types |
| 2227 | const initialActions = (mockStoreWithApproval as any).getActions(); |
| 2228 | |
| 2229 | expect(initialActions).toEqual([ |
| 2230 | { |
| 2231 | type: "chat/streamResponse/pending", |
| 2232 | meta: { |
| 2233 | arg: { |
| 2234 | editorState: mockEditorState, |
| 2235 | modifiers: mockModifiers, |
| 2236 | }, |
| 2237 | requestId: expect.any(String), |
| 2238 | requestStatus: "pending", |
| 2239 | }, |
| 2240 | payload: undefined, |
| 2241 | }, |
| 2242 | { |
| 2243 | type: "chat/streamWrapper/pending", |
| 2244 | meta: { |
| 2245 | arg: expect.any(Function), |
| 2246 | requestId: expect.any(String), |
| 2247 | requestStatus: "pending", |
| 2248 | }, |
| 2249 | payload: undefined, |
| 2250 | }, |
| 2251 | { |
| 2252 | type: "session/submitEditorAndInitAtIndex", |
| 2253 | payload: { |
| 2254 | editorState: mockEditorState, |
| 2255 | index: 1, |
| 2256 | }, |
| 2257 | }, |
| 2258 | { |
| 2259 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2260 | payload: undefined, |
| 2261 | }, |
| 2262 | { |
| 2263 | type: "symbols/updateFromContextItems/pending", |
| 2264 | meta: { |
| 2265 | arg: [], |
| 2266 | requestId: expect.any(String), |
| 2267 | requestStatus: "pending", |
| 2268 | }, |
| 2269 | payload: undefined, |
| 2270 | }, |
| 2271 | { |
| 2272 | type: "session/updateHistoryItemAtIndex", |
| 2273 | payload: { |
| 2274 | index: 1, |
| 2275 | updates: { |
| 2276 | contextItems: [], |
| 2277 | message: { |
| 2278 | content: "Hello, please help me with this code", |
| 2279 | id: "mock-uuid-123", |
| 2280 | role: "user", |
| 2281 | }, |
| 2282 | }, |
| 2283 | }, |
| 2284 | }, |
| 2285 | { |
| 2286 | type: "chat/streamNormalInput/pending", |
| 2287 | meta: { |
| 2288 | arg: { |
| 2289 | legacySlashCommandData: undefined, |
| 2290 | }, |
| 2291 | requestId: expect.any(String), |
| 2292 | requestStatus: "pending", |
| 2293 | }, |
| 2294 | payload: undefined, |
| 2295 | }, |
| 2296 | { |
| 2297 | type: "session/setAppliedRulesAtIndex", |
| 2298 | payload: { |
| 2299 | appliedRules: [], |
| 2300 | index: 1, |
| 2301 | }, |
| 2302 | }, |
| 2303 | { |
| 2304 | type: "session/setActive", |
| 2305 | payload: undefined, |
| 2306 | }, |
| 2307 | { |
| 2308 | type: "session/setInlineErrorMessage", |
| 2309 | payload: undefined, |
| 2310 | }, |
| 2311 | { |
| 2312 | type: "session/setIsPruned", |
| 2313 | payload: false, |
| 2314 | }, |
| 2315 | { |
| 2316 | type: "session/setContextPercentage", |
| 2317 | payload: 0.85, |
| 2318 | }, |
| 2319 | { |
| 2320 | type: "symbols/updateFromContextItems/fulfilled", |
| 2321 | meta: { |
| 2322 | arg: [], |
| 2323 | requestId: expect.any(String), |
| 2324 | requestStatus: "fulfilled", |
| 2325 | }, |
| 2326 | payload: undefined, |
| 2327 | }, |
| 2328 | { |
| 2329 | type: "session/streamUpdate", |
| 2330 | payload: [ |
| 2331 | { |
| 2332 | role: "assistant", |
| 2333 | content: "I'll search for test functions in the codebase.", |
| 2334 | }, |
| 2335 | ], |
| 2336 | }, |
| 2337 | { |
| 2338 | type: "session/streamUpdate", |
| 2339 | payload: [ |
| 2340 | { |
| 2341 | role: "assistant", |
| 2342 | content: "", |
| 2343 | toolCalls: [ |
| 2344 | { |
| 2345 | id: "tool-approval-flow-1", |
| 2346 | type: "function", |
| 2347 | function: { |
| 2348 | name: "search_codebase", |
| 2349 | arguments: JSON.stringify({ query: "test function" }), |
| 2350 | }, |
| 2351 | }, |
| 2352 | ], |
| 2353 | }, |
| 2354 | ], |
| 2355 | }, |
| 2356 | { |
| 2357 | type: "session/addPromptCompletionPair", |
| 2358 | payload: [ |
| 2359 | { |
| 2360 | completion: "I'll search for test functions in the codebase.", |
| 2361 | modelProvider: "anthropic", |
| 2362 | prompt: "Please search the codebase for test functions", |
| 2363 | }, |
| 2364 | ], |
| 2365 | }, |
| 2366 | { |
| 2367 | type: "session/setInactive", |
| 2368 | payload: undefined, |
| 2369 | }, |
| 2370 | { |
| 2371 | type: "session/setToolGenerated", |
| 2372 | payload: { |
| 2373 | toolCallId: "tool-approval-flow-1", |
| 2374 | tools: [], |
| 2375 | }, |
| 2376 | }, |
| 2377 | { |
| 2378 | type: "chat/streamNormalInput/fulfilled", |
| 2379 | meta: { |
| 2380 | arg: { |
| 2381 | legacySlashCommandData: undefined, |
| 2382 | }, |
| 2383 | requestId: expect.any(String), |
| 2384 | requestStatus: "fulfilled", |
| 2385 | }, |
| 2386 | payload: undefined, |
| 2387 | }, |
| 2388 | { |
| 2389 | type: "session/saveCurrent/pending", |
| 2390 | meta: { |
| 2391 | arg: { |
| 2392 | generateTitle: true, |
| 2393 | openNewSession: false, |
| 2394 | }, |
| 2395 | requestId: expect.any(String), |
| 2396 | requestStatus: "pending", |
| 2397 | }, |
| 2398 | payload: undefined, |
| 2399 | }, |
| 2400 | { |
| 2401 | type: "session/update/pending", |
| 2402 | meta: { |
| 2403 | arg: expect.objectContaining({ |
| 2404 | history: expect.any(Array), |
| 2405 | sessionId: "session-123", |
| 2406 | title: "New Session", |
| 2407 | workspaceDirectory: "", |
| 2408 | }), |
| 2409 | requestId: expect.any(String), |
| 2410 | requestStatus: "pending", |
| 2411 | }, |
| 2412 | payload: undefined, |
| 2413 | }, |
| 2414 | { |
| 2415 | type: "session/updateSessionMetadata", |
| 2416 | payload: { |
| 2417 | sessionId: "session-123", |
| 2418 | title: "New Session", |
| 2419 | }, |
| 2420 | }, |
| 2421 | { |
| 2422 | type: "session/refreshMetadata/pending", |
| 2423 | meta: { |
| 2424 | arg: {}, |
| 2425 | requestId: expect.any(String), |
| 2426 | requestStatus: "pending", |
| 2427 | }, |
| 2428 | payload: undefined, |
| 2429 | }, |
| 2430 | { |
| 2431 | type: "session/setIsSessionMetadataLoading", |
| 2432 | payload: false, |
| 2433 | }, |
| 2434 | { |
| 2435 | type: "session/setAllSessionMetadata", |
| 2436 | payload: [], |
| 2437 | }, |
| 2438 | { |
| 2439 | type: "session/refreshMetadata/fulfilled", |
| 2440 | meta: { |
| 2441 | arg: {}, |
| 2442 | requestId: expect.any(String), |
| 2443 | requestStatus: "fulfilled", |
| 2444 | }, |
| 2445 | payload: [], |
| 2446 | }, |
| 2447 | { |
| 2448 | type: "session/update/fulfilled", |
| 2449 | meta: { |
| 2450 | arg: expect.objectContaining({ |
| 2451 | history: expect.any(Array), |
| 2452 | sessionId: "session-123", |
| 2453 | title: "New Session", |
| 2454 | workspaceDirectory: "", |
| 2455 | }), |
| 2456 | requestId: expect.any(String), |
| 2457 | requestStatus: "fulfilled", |
| 2458 | }, |
| 2459 | payload: undefined, |
| 2460 | }, |
| 2461 | { |
| 2462 | type: "session/saveCurrent/fulfilled", |
| 2463 | meta: { |
| 2464 | arg: { |
| 2465 | generateTitle: true, |
| 2466 | openNewSession: false, |
| 2467 | }, |
| 2468 | requestId: expect.any(String), |
| 2469 | requestStatus: "fulfilled", |
| 2470 | }, |
| 2471 | payload: undefined, |
| 2472 | }, |
| 2473 | { |
| 2474 | type: "chat/streamWrapper/fulfilled", |
| 2475 | meta: { |
| 2476 | arg: expect.any(Function), |
| 2477 | requestId: expect.any(String), |
| 2478 | requestStatus: "fulfilled", |
| 2479 | }, |
| 2480 | payload: undefined, |
| 2481 | }, |
| 2482 | { |
| 2483 | type: "chat/streamResponse/fulfilled", |
| 2484 | meta: { |
| 2485 | arg: { |
| 2486 | editorState: mockEditorState, |
| 2487 | modifiers: mockModifiers, |
| 2488 | }, |
| 2489 | requestId: expect.any(String), |
| 2490 | requestStatus: "fulfilled", |
| 2491 | }, |
| 2492 | payload: undefined, |
| 2493 | }, |
| 2494 | ]); |
| 2495 | |
| 2496 | // Clear the actions array to track only the approval flow |
| 2497 | (mockStoreWithApproval as any).clearActions(); |
| 2498 | |
| 2499 | // Import the callToolById thunk to simulate user approval |
| 2500 | const { callToolById } = await import("./callToolById"); |
| 2501 | |
| 2502 | // Simulate user clicking "Accept" on the tool call |
| 2503 | const approvalResult = await mockStoreWithApproval.dispatch( |
| 2504 | callToolById({ toolCallId: "tool-approval-flow-1" }) as any, |
| 2505 | ); |
| 2506 | |
| 2507 | // Verify tool execution completed successfully |
| 2508 | expect(approvalResult.type).toBe("chat/callTool/fulfilled"); |
| 2509 | |
| 2510 | // Verify exact approval flow actions |
| 2511 | const approvalActions = (mockStoreWithApproval as any).getActions(); |
| 2512 | expect(approvalActions).toEqual([ |
| 2513 | { |
| 2514 | type: "chat/callTool/pending", |
| 2515 | meta: { |
| 2516 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2517 | requestId: expect.any(String), |
| 2518 | requestStatus: "pending", |
| 2519 | }, |
| 2520 | payload: undefined, |
| 2521 | }, |
| 2522 | { |
| 2523 | type: "session/setToolCallCalling", |
| 2524 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2525 | }, |
| 2526 | { |
| 2527 | type: "session/updateToolCallOutput", |
| 2528 | payload: { |
| 2529 | toolCallId: "tool-approval-flow-1", |
| 2530 | contextItems: [ |
| 2531 | { |
| 2532 | name: "Search Results", |
| 2533 | description: "Found test functions", |
| 2534 | content: |
| 2535 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2536 | icon: "search", |
| 2537 | hidden: false, |
| 2538 | }, |
| 2539 | ], |
| 2540 | }, |
| 2541 | }, |
| 2542 | { |
| 2543 | type: "session/acceptToolCall", |
| 2544 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2545 | }, |
| 2546 | { |
| 2547 | type: "chat/streamAfterToolCall/pending", |
| 2548 | meta: { |
| 2549 | arg: { |
| 2550 | toolCallId: "tool-approval-flow-1", |
| 2551 | }, |
| 2552 | requestId: expect.any(String), |
| 2553 | requestStatus: "pending", |
| 2554 | }, |
| 2555 | payload: undefined, |
| 2556 | }, |
| 2557 | { |
| 2558 | type: "chat/streamWrapper/pending", |
| 2559 | meta: { |
| 2560 | arg: expect.any(Function), |
| 2561 | requestId: expect.any(String), |
| 2562 | requestStatus: "pending", |
| 2563 | }, |
| 2564 | payload: undefined, |
| 2565 | }, |
| 2566 | { |
| 2567 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2568 | payload: undefined, |
| 2569 | }, |
| 2570 | { |
| 2571 | type: "session/streamUpdate", |
| 2572 | payload: [ |
| 2573 | { |
| 2574 | content: |
| 2575 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2576 | role: "tool", |
| 2577 | toolCallId: "tool-approval-flow-1", |
| 2578 | }, |
| 2579 | ], |
| 2580 | }, |
| 2581 | { |
| 2582 | type: "chat/streamNormalInput/pending", |
| 2583 | meta: { |
| 2584 | arg: {}, |
| 2585 | requestId: expect.any(String), |
| 2586 | requestStatus: "pending", |
| 2587 | }, |
| 2588 | payload: undefined, |
| 2589 | }, |
| 2590 | { |
| 2591 | type: "session/setAppliedRulesAtIndex", |
| 2592 | payload: { |
| 2593 | appliedRules: [], |
| 2594 | index: 1, |
| 2595 | }, |
| 2596 | }, |
| 2597 | { |
| 2598 | type: "session/setActive", |
| 2599 | payload: undefined, |
| 2600 | }, |
| 2601 | { |
| 2602 | type: "session/setInlineErrorMessage", |
| 2603 | payload: undefined, |
| 2604 | }, |
| 2605 | { |
| 2606 | type: "session/setIsPruned", |
| 2607 | payload: false, |
| 2608 | }, |
| 2609 | { |
| 2610 | type: "session/setContextPercentage", |
| 2611 | payload: 0.85, |
| 2612 | }, |
| 2613 | { |
| 2614 | type: "session/streamUpdate", |
| 2615 | payload: [ |
| 2616 | { |
| 2617 | role: "assistant", |
| 2618 | content: |
| 2619 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2620 | }, |
| 2621 | ], |
| 2622 | }, |
| 2623 | { |
| 2624 | type: "session/addPromptCompletionPair", |
| 2625 | payload: [ |
| 2626 | { |
| 2627 | completion: |
| 2628 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2629 | modelProvider: "anthropic", |
| 2630 | prompt: "continuing after tool execution", |
| 2631 | }, |
| 2632 | ], |
| 2633 | }, |
| 2634 | { |
| 2635 | type: "session/setInactive", |
| 2636 | payload: undefined, |
| 2637 | }, |
| 2638 | { |
| 2639 | type: "chat/streamNormalInput/fulfilled", |
| 2640 | meta: { |
| 2641 | arg: {}, |
| 2642 | requestId: expect.any(String), |
| 2643 | requestStatus: "fulfilled", |
| 2644 | }, |
| 2645 | payload: undefined, |
| 2646 | }, |
| 2647 | { |
| 2648 | type: "session/saveCurrent/pending", |
| 2649 | meta: { |
| 2650 | arg: { |
| 2651 | generateTitle: true, |
| 2652 | openNewSession: false, |
| 2653 | }, |
| 2654 | requestId: expect.any(String), |
| 2655 | requestStatus: "pending", |
| 2656 | }, |
| 2657 | payload: undefined, |
| 2658 | }, |
| 2659 | { |
| 2660 | type: "session/update/pending", |
| 2661 | meta: { |
| 2662 | arg: expect.objectContaining({ |
| 2663 | history: expect.any(Array), |
| 2664 | sessionId: "session-123", |
| 2665 | title: "New Session", |
| 2666 | workspaceDirectory: "", |
| 2667 | }), |
| 2668 | requestId: expect.any(String), |
| 2669 | requestStatus: "pending", |
| 2670 | }, |
| 2671 | payload: undefined, |
| 2672 | }, |
| 2673 | { |
| 2674 | type: "session/updateSessionMetadata", |
| 2675 | payload: { |
| 2676 | sessionId: "session-123", |
| 2677 | title: "New Session", |
| 2678 | }, |
| 2679 | }, |
| 2680 | { |
| 2681 | type: "session/refreshMetadata/pending", |
| 2682 | meta: { |
| 2683 | arg: {}, |
| 2684 | requestId: expect.any(String), |
| 2685 | requestStatus: "pending", |
| 2686 | }, |
| 2687 | payload: undefined, |
| 2688 | }, |
| 2689 | { |
| 2690 | type: "session/setIsSessionMetadataLoading", |
| 2691 | payload: false, |
| 2692 | }, |
| 2693 | { |
| 2694 | type: "session/setAllSessionMetadata", |
| 2695 | payload: [], |
| 2696 | }, |
| 2697 | { |
| 2698 | type: "session/refreshMetadata/fulfilled", |
| 2699 | meta: { |
| 2700 | arg: {}, |
| 2701 | requestId: expect.any(String), |
| 2702 | requestStatus: "fulfilled", |
| 2703 | }, |
| 2704 | payload: [], |
| 2705 | }, |
| 2706 | { |
| 2707 | type: "session/update/fulfilled", |
| 2708 | meta: { |
| 2709 | arg: expect.objectContaining({ |
| 2710 | history: expect.any(Array), |
| 2711 | sessionId: "session-123", |
| 2712 | title: "New Session", |
| 2713 | workspaceDirectory: "", |
| 2714 | }), |
| 2715 | requestId: expect.any(String), |
| 2716 | requestStatus: "fulfilled", |
| 2717 | }, |
| 2718 | payload: undefined, |
| 2719 | }, |
| 2720 | { |
| 2721 | type: "session/saveCurrent/fulfilled", |
| 2722 | meta: { |
| 2723 | arg: { |
| 2724 | generateTitle: true, |
| 2725 | openNewSession: false, |
| 2726 | }, |
| 2727 | requestId: expect.any(String), |
| 2728 | requestStatus: "fulfilled", |
| 2729 | }, |
| 2730 | payload: undefined, |
| 2731 | }, |
| 2732 | { |
| 2733 | type: "chat/streamWrapper/fulfilled", |
| 2734 | meta: { |
| 2735 | arg: expect.any(Function), |
| 2736 | requestId: expect.any(String), |
| 2737 | requestStatus: "fulfilled", |
| 2738 | }, |
| 2739 | payload: undefined, |
| 2740 | }, |
| 2741 | { |
| 2742 | type: "chat/streamAfterToolCall/fulfilled", |
| 2743 | meta: { |
| 2744 | arg: { |
| 2745 | toolCallId: "tool-approval-flow-1", |
| 2746 | }, |
| 2747 | requestId: expect.any(String), |
| 2748 | requestStatus: "fulfilled", |
| 2749 | }, |
| 2750 | payload: undefined, |
| 2751 | }, |
| 2752 | { |
| 2753 | type: "chat/callTool/fulfilled", |
| 2754 | meta: { |
| 2755 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2756 | requestId: expect.any(String), |
| 2757 | requestStatus: "fulfilled", |
| 2758 | }, |
| 2759 | payload: undefined, |
| 2760 | }, |
| 2761 | ]); |
| 2762 | |
| 2763 | // Verify telemetry events for manual approval flow |
| 2764 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_decision", { |
| 2765 | decision: "accept", |
| 2766 | toolName: "search_codebase", |
| 2767 | toolCallId: "tool-approval-flow-1", |
| 2768 | }); |
| 2769 | |
| 2770 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_outcome", { |
| 2771 | succeeded: true, |
| 2772 | toolName: "search_codebase", |
| 2773 | errorMessage: undefined, |
| 2774 | duration_ms: expect.any(Number), |
| 2775 | }); |
| 2776 | |
| 2777 | // Verify IDE messenger calls for tool execution |
| 2778 | expect(mockIdeMessengerApproval.request).toHaveBeenCalledWith( |
| 2779 | "tools/call", |
| 2780 | { |
| 2781 | toolCall: { |
| 2782 | id: "tool-approval-flow-1", |
| 2783 | type: "function", |
| 2784 | function: { |
| 2785 | name: "search_codebase", |
| 2786 | arguments: JSON.stringify({ query: "test function" }), |
| 2787 | }, |
| 2788 | }, |
| 2789 | }, |
| 2790 | ); |
| 2791 | |
| 2792 | // Verify second streaming call was made for continuation |
| 2793 | expect(streamCallCount).toBe(2); |
| 2794 | |
| 2795 | // Verify final state shows completed tool call and follow-up response |
| 2796 | const finalState = mockStoreWithApproval.getState(); |
| 2797 | expect(finalState).toEqual({ |
| 2798 | session: { |
| 2799 | history: [ |
| 2800 | { |
| 2801 | contextItems: [], |
| 2802 | message: { |
| 2803 | id: "1", |
| 2804 | role: "user", |
| 2805 | content: "Please search the codebase for test functions", |
| 2806 | }, |
| 2807 | }, |
| 2808 | { |
| 2809 | appliedRules: [], |
| 2810 | contextItems: [], |
| 2811 | editorState: mockEditorState, |
| 2812 | message: { |
| 2813 | content: "Hello, please help me with this code", |
| 2814 | id: expect.any(String), |
| 2815 | role: "user", |
| 2816 | }, |
| 2817 | }, |
| 2818 | { |
| 2819 | contextItems: [], |
| 2820 | isGatheringContext: false, |
| 2821 | message: { |
| 2822 | content: "I'll search for test functions in the codebase.", |
| 2823 | id: expect.any(String), |
| 2824 | role: "assistant", |
| 2825 | toolCalls: [ |
| 2826 | { |
| 2827 | id: "tool-approval-flow-1", |
| 2828 | type: "function", |
| 2829 | function: { |
| 2830 | name: "search_codebase", |
| 2831 | arguments: JSON.stringify({ query: "test function" }), |
| 2832 | }, |
| 2833 | }, |
| 2834 | ], |
| 2835 | }, |
| 2836 | promptLogs: [ |
| 2837 | { |
| 2838 | completion: "I'll search for test functions in the codebase.", |
| 2839 | modelProvider: "anthropic", |
| 2840 | prompt: "Please search the codebase for test functions", |
| 2841 | }, |
| 2842 | ], |
| 2843 | toolCallStates: [ |
| 2844 | { |
| 2845 | toolCallId: "tool-approval-flow-1", |
| 2846 | toolCall: { |
| 2847 | id: "tool-approval-flow-1", |
| 2848 | type: "function", |
| 2849 | function: { |
| 2850 | name: "search_codebase", |
| 2851 | arguments: JSON.stringify({ query: "test function" }), |
| 2852 | }, |
| 2853 | }, |
| 2854 | parsedArgs: { query: "test function" }, |
| 2855 | status: "done", // Tool call completed successfully |
| 2856 | output: [ |
| 2857 | { |
| 2858 | name: "Search Results", |
| 2859 | description: "Found test functions", |
| 2860 | content: |
| 2861 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2862 | icon: "search", |
| 2863 | hidden: false, |
| 2864 | }, |
| 2865 | ], |
| 2866 | }, |
| 2867 | ], |
| 2868 | }, |
| 2869 | { |
| 2870 | contextItems: [], |
| 2871 | message: { |
| 2872 | content: |
| 2873 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2874 | id: expect.any(String), |
| 2875 | role: "tool", |
| 2876 | toolCallId: "tool-approval-flow-1", |
| 2877 | }, |
| 2878 | }, |
| 2879 | { |
| 2880 | contextItems: [], |
| 2881 | isGatheringContext: false, |
| 2882 | message: { |
| 2883 | content: |
| 2884 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2885 | id: "mock-uuid-123", |
| 2886 | role: "assistant", |
| 2887 | }, |
| 2888 | promptLogs: [ |
| 2889 | { |
| 2890 | completion: |
| 2891 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2892 | modelProvider: "anthropic", |
| 2893 | prompt: "continuing after tool execution", |
| 2894 | }, |
| 2895 | ], |
| 2896 | }, |
| 2897 | ], |
| 2898 | hasReasoningEnabled: false, |
| 2899 | isStreaming: false, // Inactive after complete flow |
| 2900 | id: "session-123", |
| 2901 | mode: "chat", |
| 2902 | streamAborter: expect.any(AbortController), |
| 2903 | contextPercentage: 0.85, |
| 2904 | isPruned: false, |
| 2905 | isInEdit: false, |
| 2906 | title: "New Session", |
| 2907 | lastSessionId: undefined, |
| 2908 | isSessionMetadataLoading: false, |
| 2909 | allSessionMetadata: [], |
| 2910 | symbols: {}, |
| 2911 | codeBlockApplyStates: { |
| 2912 | states: [], |
| 2913 | curIndex: 0, |
| 2914 | }, |
| 2915 | newestToolbarPreviewForInput: {}, |
| 2916 | compactionLoading: {}, |
| 2917 | inlineErrorMessage: undefined, |
| 2918 | }, |
| 2919 | config: { |
| 2920 | config: { |
| 2921 | tools: [], |
| 2922 | rules: [], |
| 2923 | tabAutocompleteModel: undefined, |
| 2924 | selectedModelByRole: { |
| 2925 | chat: mockClaudeModel, |
| 2926 | apply: null, |
| 2927 | edit: null, |
| 2928 | summarize: null, |
| 2929 | autocomplete: null, |
| 2930 | rerank: null, |
| 2931 | embed: null, |
| 2932 | }, |
| 2933 | experimental: { |
| 2934 | onlyUseSystemMessageTools: false, |
| 2935 | }, |
| 2936 | }, |
| 2937 | lastSelectedModelByRole: { |
| 2938 | chat: mockClaudeModel.title, |
| 2939 | }, |
| 2940 | loading: false, |
| 2941 | configError: undefined, |
| 2942 | }, |
| 2943 | ui: { |
| 2944 | toolSettings: { |
| 2945 | search_codebase: "askFirst", |
| 2946 | }, |
| 2947 | ruleSettings: {}, |
| 2948 | showDialog: false, |
| 2949 | dialogMessage: undefined, |
| 2950 | }, |
| 2951 | editModeState: { |
| 2952 | isInEdit: false, |
| 2953 | returnToMode: "chat", |
| 2954 | }, |
| 2955 | indexing: { |
| 2956 | indexing: { |
| 2957 | statuses: {}, |
| 2958 | hiddenChatPeekTypes: { |
| 2959 | docs: false, |
| 2960 | }, |
| 2961 | }, |
| 2962 | }, |
| 2963 | tabs: { |
| 2964 | tabsItems: [], |
| 2965 | }, |
| 2966 | profiles: { |
| 2967 | organizations: [], |
| 2968 | selectedProfileId: null, |
| 2969 | selectedOrganizationId: null, |
| 2970 | preferencesByProfileId: {}, |
| 2971 | }, |
| 2972 | }); |
| 2973 | }); |
| 2974 | }); |