| 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/evaluatePolicy") { |
| 241 | // Mock dynamic policy evaluation - return auto-approve for this test |
| 242 | return Promise.resolve({ |
| 243 | status: "success", |
| 244 | content: { policy: "allowedWithoutPermission" }, |
| 245 | }); |
| 246 | } else if (endpoint === "tools/call") { |
| 247 | // Mock server-side tool call response |
| 248 | return Promise.resolve({ |
| 249 | status: "success", |
| 250 | content: { |
| 251 | contextItems: [ |
| 252 | { |
| 253 | name: "Search Results", |
| 254 | description: "Found 3 matches", |
| 255 | content: "Result 1\nResult 2\nResult 3", |
| 256 | icon: "search", |
| 257 | hidden: false, |
| 258 | }, |
| 259 | ], |
| 260 | errorMessage: undefined, |
| 261 | }, |
| 262 | }); |
| 263 | } else if (endpoint === "history/save") { |
| 264 | // Mock session save |
| 265 | return Promise.resolve({ status: "success" }); |
| 266 | } else if (endpoint === "history/list") { |
| 267 | return Promise.resolve({ status: "success", content: [] }); |
| 268 | } |
| 269 | // Let other endpoints pass through or return success |
| 270 | return Promise.resolve({ status: "success", content: {} }); |
| 271 | }, |
| 272 | ); |
| 273 | |
| 274 | // Setup streaming generator with tool call |
| 275 | async function* mockStreamGeneratorWithTool() { |
| 276 | yield [ |
| 277 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 278 | ]; |
| 279 | yield [ |
| 280 | { |
| 281 | role: "assistant", |
| 282 | content: "", |
| 283 | toolCalls: [ |
| 284 | { |
| 285 | id: "tool-call-1", |
| 286 | type: "function", |
| 287 | function: { |
| 288 | name: "search_codebase", |
| 289 | arguments: JSON.stringify({ query: "test function" }), |
| 290 | }, |
| 291 | }, |
| 292 | ], |
| 293 | }, |
| 294 | ]; |
| 295 | return { |
| 296 | prompt: "Please search the codebase", |
| 297 | completion: "I'll search the codebase for you.", |
| 298 | modelProvider: "anthropic", |
| 299 | }; |
| 300 | } |
| 301 | |
| 302 | // Mock different streaming responses for multiple calls |
| 303 | let streamCallCount = 0; |
| 304 | mockIdeMessengerWithTool.llmStreamChat.mockImplementation(() => { |
| 305 | streamCallCount++; |
| 306 | if (streamCallCount === 1) { |
| 307 | // First call - main streaming with tool call |
| 308 | return mockStreamGeneratorWithTool(); |
| 309 | } else { |
| 310 | // Subsequent calls from streamResponseAfterToolCall - return minimal response |
| 311 | async function* simpleGenerator() { |
| 312 | yield [{ role: "assistant", content: "Search completed." }]; |
| 313 | return { |
| 314 | prompt: "continuing after tool", |
| 315 | completion: "Search completed.", |
| 316 | modelProvider: "anthropic", |
| 317 | }; |
| 318 | } |
| 319 | return simpleGenerator(); |
| 320 | } |
| 321 | }); |
| 322 | |
| 323 | // Track isStreaming state changes to detect UI flashing during auto-approved tool execution |
| 324 | const streamingStateChanges: boolean[] = []; |
| 325 | let lastStreamingState = |
| 326 | mockStoreWithToolSettings.getState().session.isStreaming; |
| 327 | // Record the initial state |
| 328 | streamingStateChanges.push(lastStreamingState); |
| 329 | |
| 330 | // Subscribe to store changes to catch ALL state updates |
| 331 | const unsubscribe = mockStoreWithToolSettings.subscribe(() => { |
| 332 | const currentState = |
| 333 | mockStoreWithToolSettings.getState().session.isStreaming; |
| 334 | if (currentState !== lastStreamingState) { |
| 335 | console.log( |
| 336 | `Store subscription: isStreaming changed from ${lastStreamingState} to ${currentState}`, |
| 337 | ); |
| 338 | streamingStateChanges.push(currentState); |
| 339 | lastStreamingState = currentState; |
| 340 | } |
| 341 | }); |
| 342 | |
| 343 | // Execute thunk |
| 344 | const result = await mockStoreWithToolSettings.dispatch( |
| 345 | streamResponseThunk({ |
| 346 | editorState: mockEditorState, |
| 347 | modifiers: mockModifiers, |
| 348 | }) as any, |
| 349 | ); |
| 350 | |
| 351 | // Debug: check what happened |
| 352 | const debugState = mockStoreWithToolSettings.getState(); |
| 353 | const finalStreamingState = debugState.session.isStreaming; |
| 354 | const toolCallStates = debugState.session.history.flatMap( |
| 355 | (item) => item.toolCallStates || [], |
| 356 | ); |
| 357 | |
| 358 | console.log("Initial streaming state:", lastStreamingState); |
| 359 | console.log("Final streaming state:", finalStreamingState); |
| 360 | console.log( |
| 361 | "Tool call states:", |
| 362 | toolCallStates.map((t) => ({ |
| 363 | id: t.toolCallId, |
| 364 | status: t.status, |
| 365 | toolName: t.toolCall.function.name, |
| 366 | })), |
| 367 | ); |
| 368 | console.log("Tool settings:", debugState.ui.toolSettings); |
| 369 | console.log( |
| 370 | "Auto-approved tool streaming state changes:", |
| 371 | streamingStateChanges, |
| 372 | ); |
| 373 | |
| 374 | // Unsubscribe from store updates |
| 375 | unsubscribe(); |
| 376 | |
| 377 | // Verify no UI flashing during auto-approved tool execution |
| 378 | // DESIRED: Should stay true throughout tool execution, only becoming false at the very end |
| 379 | // We expect [true, false] - streaming starts true, stays true during tool execution, then false when all done |
| 380 | expect(streamingStateChanges).toEqual([true, false]); |
| 381 | |
| 382 | // Verify key actions are dispatched (tool calls trigger a complex cascade, so we verify key actions exist) |
| 383 | const dispatchedActions = (mockStoreWithToolSettings as any).getActions(); |
| 384 | |
| 385 | // Verify exact action sequence |
| 386 | const actionTypes = dispatchedActions.map((action: any) => action.type); |
| 387 | expect(actionTypes).toEqual([ |
| 388 | "chat/streamResponse/pending", |
| 389 | "chat/streamWrapper/pending", |
| 390 | "session/submitEditorAndInitAtIndex", |
| 391 | "session/resetNextCodeBlockToApplyIndex", |
| 392 | "symbols/updateFromContextItems/pending", |
| 393 | "session/updateHistoryItemAtIndex", |
| 394 | "chat/streamNormalInput/pending", |
| 395 | "session/setAppliedRulesAtIndex", |
| 396 | "session/setActive", |
| 397 | "session/setInlineErrorMessage", |
| 398 | "session/setIsPruned", |
| 399 | "session/setContextPercentage", |
| 400 | "symbols/updateFromContextItems/fulfilled", |
| 401 | "session/streamUpdate", |
| 402 | "session/streamUpdate", |
| 403 | "session/addPromptCompletionPair", |
| 404 | "session/setToolGenerated", |
| 405 | "chat/callTool/pending", |
| 406 | "session/setToolCallCalling", |
| 407 | "session/updateToolCallOutput", |
| 408 | "session/acceptToolCall", |
| 409 | "chat/streamAfterToolCall/pending", |
| 410 | "chat/streamWrapper/pending", |
| 411 | "session/resetNextCodeBlockToApplyIndex", |
| 412 | "session/streamUpdate", |
| 413 | "chat/streamNormalInput/pending", |
| 414 | "session/setAppliedRulesAtIndex", |
| 415 | "session/setActive", |
| 416 | "session/setInlineErrorMessage", |
| 417 | "session/setIsPruned", |
| 418 | "session/setContextPercentage", |
| 419 | "session/streamUpdate", |
| 420 | "session/addPromptCompletionPair", |
| 421 | "session/setInactive", |
| 422 | "chat/streamNormalInput/fulfilled", |
| 423 | "session/saveCurrent/pending", |
| 424 | "session/update/pending", |
| 425 | "session/updateSessionMetadata", |
| 426 | "session/refreshMetadata/pending", |
| 427 | "session/setIsSessionMetadataLoading", |
| 428 | "session/setAllSessionMetadata", |
| 429 | "session/refreshMetadata/fulfilled", |
| 430 | "session/update/fulfilled", |
| 431 | "session/saveCurrent/fulfilled", |
| 432 | "chat/streamWrapper/fulfilled", |
| 433 | "chat/streamAfterToolCall/fulfilled", |
| 434 | "chat/callTool/fulfilled", |
| 435 | "chat/streamNormalInput/fulfilled", |
| 436 | "session/saveCurrent/pending", |
| 437 | "session/update/pending", |
| 438 | "session/updateSessionMetadata", |
| 439 | "session/refreshMetadata/pending", |
| 440 | "session/setIsSessionMetadataLoading", |
| 441 | "session/setAllSessionMetadata", |
| 442 | "session/refreshMetadata/fulfilled", |
| 443 | "session/update/fulfilled", |
| 444 | "session/saveCurrent/fulfilled", |
| 445 | "chat/streamWrapper/fulfilled", |
| 446 | "chat/streamResponse/fulfilled", |
| 447 | ]); |
| 448 | |
| 449 | // Verify key payload data for important actions |
| 450 | const setContextPercentageAction = dispatchedActions.find( |
| 451 | (a: any) => a.type === "session/setContextPercentage", |
| 452 | ); |
| 453 | expect(setContextPercentageAction.payload).toBe(0.9); |
| 454 | |
| 455 | const streamUpdates = dispatchedActions.filter( |
| 456 | (a: any) => a.type === "session/streamUpdate", |
| 457 | ); |
| 458 | expect(streamUpdates[0].payload).toEqual([ |
| 459 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 460 | ]); |
| 461 | expect(streamUpdates[1].payload).toEqual([ |
| 462 | { |
| 463 | role: "assistant", |
| 464 | content: "", |
| 465 | toolCalls: [ |
| 466 | { |
| 467 | id: "tool-call-1", |
| 468 | type: "function", |
| 469 | function: { |
| 470 | name: "search_codebase", |
| 471 | arguments: JSON.stringify({ query: "test function" }), |
| 472 | }, |
| 473 | }, |
| 474 | ], |
| 475 | }, |
| 476 | ]); |
| 477 | |
| 478 | const completionPairs = dispatchedActions.filter( |
| 479 | (a: any) => a.type === "session/addPromptCompletionPair", |
| 480 | ); |
| 481 | expect(completionPairs[0].payload).toEqual([ |
| 482 | { |
| 483 | completion: "I'll search the codebase for you.", |
| 484 | modelProvider: "anthropic", |
| 485 | prompt: "Please search the codebase", |
| 486 | }, |
| 487 | ]); |
| 488 | |
| 489 | const toolCallActions = dispatchedActions.filter( |
| 490 | (a: any) => a.type === "session/setToolCallCalling", |
| 491 | ); |
| 492 | expect(toolCallActions[0].payload).toEqual({ toolCallId: "tool-call-1" }); |
| 493 | |
| 494 | const toolOutputActions = dispatchedActions.filter( |
| 495 | (a: any) => a.type === "session/updateToolCallOutput", |
| 496 | ); |
| 497 | expect(toolOutputActions[0].payload).toEqual({ |
| 498 | toolCallId: "tool-call-1", |
| 499 | contextItems: [ |
| 500 | { |
| 501 | name: "Search Results", |
| 502 | description: "Found 3 matches", |
| 503 | content: "Result 1\nResult 2\nResult 3", |
| 504 | icon: "search", |
| 505 | hidden: false, |
| 506 | }, |
| 507 | ], |
| 508 | }); |
| 509 | |
| 510 | // Verify IDE messenger calls |
| 511 | expect(mockIdeMessengerWithTool.request).toHaveBeenCalledWith( |
| 512 | "llm/compileChat", |
| 513 | { |
| 514 | messages: [ |
| 515 | { |
| 516 | role: "system", |
| 517 | content: "You are a helpful assistant.", |
| 518 | }, |
| 519 | { |
| 520 | role: "user", |
| 521 | content: [ |
| 522 | { |
| 523 | type: "text", |
| 524 | text: "Please search the codebase", |
| 525 | }, |
| 526 | ], |
| 527 | }, |
| 528 | { |
| 529 | role: "user", |
| 530 | content: [ |
| 531 | { |
| 532 | type: "text", |
| 533 | text: "Hello, please help me with this code", |
| 534 | }, |
| 535 | ], |
| 536 | }, |
| 537 | ], |
| 538 | options: { |
| 539 | reasoning: false, |
| 540 | }, |
| 541 | }, |
| 542 | ); |
| 543 | |
| 544 | expect(mockIdeMessengerWithTool.request).toHaveBeenCalledWith( |
| 545 | "tools/call", |
| 546 | { |
| 547 | toolCall: { |
| 548 | id: "tool-call-1", |
| 549 | type: "function", |
| 550 | function: { |
| 551 | name: "search_codebase", |
| 552 | arguments: JSON.stringify({ query: "test function" }), |
| 553 | }, |
| 554 | }, |
| 555 | }, |
| 556 | ); |
| 557 | |
| 558 | // Verify that multiple compilation calls were made (due to tool call continuation) |
| 559 | const compileCallsCount = |
| 560 | mockIdeMessengerWithTool.request.mock.calls.filter( |
| 561 | (call: any) => call[0] === "llm/compileChat", |
| 562 | ).length; |
| 563 | expect(compileCallsCount).toBeGreaterThanOrEqual(1); |
| 564 | |
| 565 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 566 | |
| 567 | // Verify telemetry events for auto-approved tool execution |
| 568 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_decision", { |
| 569 | decision: "auto_accept", |
| 570 | toolName: "search_codebase", |
| 571 | toolCallId: "tool-call-1", |
| 572 | }); |
| 573 | |
| 574 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_outcome", { |
| 575 | succeeded: true, |
| 576 | toolName: "search_codebase", |
| 577 | errorMessage: undefined, |
| 578 | duration_ms: expect.any(Number), |
| 579 | }); |
| 580 | |
| 581 | // Verify final state after tool call execution |
| 582 | const finalState = mockStoreWithToolSettings.getState(); |
| 583 | |
| 584 | // Check that the tool was executed (status should be 'done' if auto-approved and executed) |
| 585 | const toolCallState = finalState.session.history.find( |
| 586 | (item) => item.toolCallStates && item.toolCallStates.length > 0, |
| 587 | )?.toolCallStates?.[0]; |
| 588 | |
| 589 | // With proper mocking, the tool should be auto-executed |
| 590 | expect(toolCallState?.status).toBe("done"); |
| 591 | expect(toolCallState?.output).toBeDefined(); |
| 592 | |
| 593 | expect(finalState).toEqual({ |
| 594 | session: { |
| 595 | history: [ |
| 596 | { |
| 597 | contextItems: [], |
| 598 | message: { |
| 599 | id: "1", |
| 600 | role: "user", |
| 601 | content: "Please search the codebase", |
| 602 | }, |
| 603 | }, |
| 604 | { |
| 605 | appliedRules: [], |
| 606 | contextItems: [], |
| 607 | editorState: mockEditorState, |
| 608 | message: { |
| 609 | id: expect.any(String), |
| 610 | role: "user", |
| 611 | content: "Hello, please help me with this code", |
| 612 | }, |
| 613 | }, |
| 614 | { |
| 615 | contextItems: [], |
| 616 | message: { |
| 617 | content: "I'll search the codebase for you.", |
| 618 | id: expect.any(String), |
| 619 | role: "assistant", |
| 620 | toolCalls: [ |
| 621 | { |
| 622 | id: "tool-call-1", |
| 623 | type: "function", |
| 624 | function: { |
| 625 | name: "search_codebase", |
| 626 | arguments: JSON.stringify({ query: "test function" }), |
| 627 | }, |
| 628 | }, |
| 629 | ], |
| 630 | }, |
| 631 | promptLogs: [ |
| 632 | { |
| 633 | completion: "I'll search the codebase for you.", |
| 634 | modelProvider: "anthropic", |
| 635 | prompt: "Please search the codebase", |
| 636 | }, |
| 637 | ], |
| 638 | toolCallStates: [ |
| 639 | { |
| 640 | toolCallId: "tool-call-1", |
| 641 | toolCall: { |
| 642 | id: "tool-call-1", |
| 643 | type: "function", |
| 644 | function: { |
| 645 | name: "search_codebase", |
| 646 | arguments: JSON.stringify({ query: "test function" }), |
| 647 | }, |
| 648 | }, |
| 649 | parsedArgs: { query: "test function" }, |
| 650 | status: "done", |
| 651 | output: [ |
| 652 | { |
| 653 | name: "Search Results", |
| 654 | description: "Found 3 matches", |
| 655 | content: "Result 1\nResult 2\nResult 3", |
| 656 | icon: "search", |
| 657 | hidden: false, |
| 658 | }, |
| 659 | ], |
| 660 | }, |
| 661 | ], |
| 662 | }, |
| 663 | { |
| 664 | contextItems: [], |
| 665 | message: { |
| 666 | content: "Result 1\nResult 2\nResult 3", |
| 667 | id: expect.any(String), |
| 668 | role: "tool", |
| 669 | toolCallId: "tool-call-1", |
| 670 | }, |
| 671 | }, |
| 672 | { |
| 673 | contextItems: [], |
| 674 | isGatheringContext: false, |
| 675 | message: { |
| 676 | content: "Search completed.", |
| 677 | id: "mock-uuid-123", |
| 678 | role: "assistant", |
| 679 | }, |
| 680 | promptLogs: [ |
| 681 | { |
| 682 | completion: "Search completed.", |
| 683 | modelProvider: "anthropic", |
| 684 | prompt: "continuing after tool", |
| 685 | }, |
| 686 | ], |
| 687 | }, |
| 688 | ], |
| 689 | hasReasoningEnabled: false, |
| 690 | isStreaming: false, |
| 691 | id: "session-123", |
| 692 | mode: "chat", |
| 693 | streamAborter: expect.any(AbortController), |
| 694 | contextPercentage: 0.9, |
| 695 | isPruned: false, |
| 696 | isInEdit: false, |
| 697 | title: "New Session", |
| 698 | lastSessionId: undefined, |
| 699 | isSessionMetadataLoading: false, |
| 700 | allSessionMetadata: [], |
| 701 | symbols: {}, |
| 702 | codeBlockApplyStates: { |
| 703 | states: [], |
| 704 | curIndex: 0, |
| 705 | }, |
| 706 | newestToolbarPreviewForInput: {}, |
| 707 | compactionLoading: {}, |
| 708 | inlineErrorMessage: undefined, |
| 709 | }, |
| 710 | config: { |
| 711 | config: { |
| 712 | tools: [], |
| 713 | rules: [], |
| 714 | tabAutocompleteModel: undefined, |
| 715 | selectedModelByRole: { |
| 716 | chat: mockClaudeModel, |
| 717 | apply: null, |
| 718 | edit: null, |
| 719 | summarize: null, |
| 720 | autocomplete: null, |
| 721 | rerank: null, |
| 722 | embed: null, |
| 723 | }, |
| 724 | experimental: { |
| 725 | onlyUseSystemMessageTools: false, |
| 726 | }, |
| 727 | }, |
| 728 | lastSelectedModelByRole: { |
| 729 | chat: mockClaudeModel.title, |
| 730 | }, |
| 731 | loading: false, |
| 732 | configError: undefined, |
| 733 | }, |
| 734 | ui: { |
| 735 | toolSettings: { |
| 736 | search_codebase: "allowedWithoutPermission", |
| 737 | }, |
| 738 | ruleSettings: {}, |
| 739 | showDialog: false, |
| 740 | dialogMessage: undefined, |
| 741 | }, |
| 742 | editModeState: { |
| 743 | isInEdit: false, |
| 744 | returnToMode: "chat", |
| 745 | }, |
| 746 | indexing: { |
| 747 | indexing: { |
| 748 | statuses: {}, |
| 749 | hiddenChatPeekTypes: { |
| 750 | docs: false, |
| 751 | }, |
| 752 | }, |
| 753 | }, |
| 754 | tabs: { |
| 755 | tabsItems: [], |
| 756 | }, |
| 757 | profiles: { |
| 758 | organizations: [], |
| 759 | selectedProfileId: null, |
| 760 | selectedOrganizationId: null, |
| 761 | preferencesByProfileId: {}, |
| 762 | }, |
| 763 | }); |
| 764 | }); |
| 765 | |
| 766 | it("should handle tool call rejection error", async () => { |
| 767 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 768 | |
| 769 | // Create store with tool settings that will reject the tool call |
| 770 | const mockStoreWithToolReject = createMockStore({ |
| 771 | session: { |
| 772 | history: [ |
| 773 | { |
| 774 | message: { |
| 775 | id: "1", |
| 776 | role: "user", |
| 777 | content: "Please edit this file", |
| 778 | }, |
| 779 | contextItems: [], |
| 780 | }, |
| 781 | ], |
| 782 | hasReasoningEnabled: false, |
| 783 | isStreaming: true, |
| 784 | id: "session-123", |
| 785 | mode: "chat", |
| 786 | streamAborter: new AbortController(), |
| 787 | contextPercentage: 0, |
| 788 | isPruned: false, |
| 789 | isInEdit: false, |
| 790 | title: "", |
| 791 | lastSessionId: undefined, |
| 792 | isSessionMetadataLoading: false, |
| 793 | allSessionMetadata: [], |
| 794 | symbols: {}, |
| 795 | codeBlockApplyStates: { |
| 796 | states: [], |
| 797 | curIndex: 0, |
| 798 | }, |
| 799 | newestToolbarPreviewForInput: {}, |
| 800 | compactionLoading: {}, |
| 801 | inlineErrorMessage: undefined, |
| 802 | }, |
| 803 | config: { |
| 804 | config: { |
| 805 | tools: [], |
| 806 | rules: [], |
| 807 | tabAutocompleteModel: undefined, |
| 808 | selectedModelByRole: { |
| 809 | chat: mockClaudeModel, |
| 810 | apply: null, |
| 811 | edit: null, |
| 812 | summarize: null, |
| 813 | autocomplete: null, |
| 814 | rerank: null, |
| 815 | embed: null, |
| 816 | }, |
| 817 | experimental: { |
| 818 | onlyUseSystemMessageTools: false, |
| 819 | }, |
| 820 | } as any, |
| 821 | lastSelectedModelByRole: { |
| 822 | chat: mockClaudeModel.title, |
| 823 | }, |
| 824 | } as any, |
| 825 | ui: { |
| 826 | toolSettings: { |
| 827 | edit_existing_file: "disabled", // Tool is disabled/rejected |
| 828 | }, |
| 829 | ruleSettings: {}, |
| 830 | showDialog: false, |
| 831 | dialogMessage: undefined, |
| 832 | } as any, |
| 833 | }); |
| 834 | |
| 835 | const mockIdeMessengerReject = mockStoreWithToolReject.mockIdeMessenger; |
| 836 | |
| 837 | // Setup successful compilation |
| 838 | mockIdeMessengerReject.request.mockResolvedValue({ |
| 839 | status: "success", |
| 840 | content: { |
| 841 | compiledChatMessages: [ |
| 842 | { role: "user", content: "Please edit this file" }, |
| 843 | ], |
| 844 | didPrune: false, |
| 845 | contextPercentage: 0.7, |
| 846 | }, |
| 847 | }); |
| 848 | |
| 849 | // Setup streaming generator with tool call that will be rejected |
| 850 | async function* mockStreamGeneratorWithRejectedTool() { |
| 851 | yield [{ role: "assistant", content: "I'll edit the file for you." }]; |
| 852 | yield [ |
| 853 | { |
| 854 | role: "assistant", |
| 855 | content: "", |
| 856 | toolCalls: [ |
| 857 | { |
| 858 | id: "tool-reject-1", |
| 859 | type: "function", |
| 860 | function: { |
| 861 | name: "edit_existing_file", |
| 862 | arguments: JSON.stringify({ |
| 863 | filepath: "test.js", |
| 864 | changes: "const x = 1;", |
| 865 | }), |
| 866 | }, |
| 867 | }, |
| 868 | ], |
| 869 | }, |
| 870 | ]; |
| 871 | return { |
| 872 | prompt: "Please edit this file", |
| 873 | completion: "I'll edit the file for you.", |
| 874 | modelProvider: "anthropic", |
| 875 | }; |
| 876 | } |
| 877 | |
| 878 | mockIdeMessengerReject.llmStreamChat.mockReturnValue( |
| 879 | mockStreamGeneratorWithRejectedTool(), |
| 880 | ); |
| 881 | |
| 882 | // Execute thunk |
| 883 | const result = await mockStoreWithToolReject.dispatch( |
| 884 | streamResponseThunk({ |
| 885 | editorState: mockEditorState, |
| 886 | modifiers: mockModifiers, |
| 887 | }) as any, |
| 888 | ); |
| 889 | |
| 890 | // Verify thunk completed successfully (tool rejection is handled gracefully) |
| 891 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 892 | |
| 893 | // Verify exact action sequence includes tool generation but rejection handling |
| 894 | const dispatchedActions = (mockStoreWithToolReject as any).getActions(); |
| 895 | expect(dispatchedActions).toEqual([ |
| 896 | { |
| 897 | type: "chat/streamResponse/pending", |
| 898 | meta: { |
| 899 | arg: { |
| 900 | editorState: mockEditorState, |
| 901 | modifiers: mockModifiers, |
| 902 | }, |
| 903 | requestId: expect.any(String), |
| 904 | requestStatus: "pending", |
| 905 | }, |
| 906 | payload: undefined, |
| 907 | }, |
| 908 | { |
| 909 | type: "chat/streamWrapper/pending", |
| 910 | meta: { |
| 911 | arg: expect.any(Function), |
| 912 | requestId: expect.any(String), |
| 913 | requestStatus: "pending", |
| 914 | }, |
| 915 | payload: undefined, |
| 916 | }, |
| 917 | { |
| 918 | type: "session/submitEditorAndInitAtIndex", |
| 919 | payload: { |
| 920 | editorState: mockEditorState, |
| 921 | index: 1, |
| 922 | }, |
| 923 | }, |
| 924 | { |
| 925 | type: "session/resetNextCodeBlockToApplyIndex", |
| 926 | payload: undefined, |
| 927 | }, |
| 928 | { |
| 929 | type: "symbols/updateFromContextItems/pending", |
| 930 | meta: { |
| 931 | arg: [], |
| 932 | requestId: expect.any(String), |
| 933 | requestStatus: "pending", |
| 934 | }, |
| 935 | payload: undefined, |
| 936 | }, |
| 937 | { |
| 938 | type: "session/updateHistoryItemAtIndex", |
| 939 | payload: { |
| 940 | index: 1, |
| 941 | updates: { |
| 942 | contextItems: [], |
| 943 | message: { |
| 944 | content: "Hello, please help me with this code", |
| 945 | id: "mock-uuid-123", |
| 946 | role: "user", |
| 947 | }, |
| 948 | }, |
| 949 | }, |
| 950 | }, |
| 951 | { |
| 952 | type: "chat/streamNormalInput/pending", |
| 953 | meta: { |
| 954 | arg: { |
| 955 | legacySlashCommandData: undefined, |
| 956 | }, |
| 957 | requestId: expect.any(String), |
| 958 | requestStatus: "pending", |
| 959 | }, |
| 960 | payload: undefined, |
| 961 | }, |
| 962 | { |
| 963 | type: "session/setAppliedRulesAtIndex", |
| 964 | payload: { |
| 965 | appliedRules: [], |
| 966 | index: 1, |
| 967 | }, |
| 968 | }, |
| 969 | { |
| 970 | type: "session/setActive", |
| 971 | payload: undefined, |
| 972 | }, |
| 973 | { |
| 974 | type: "session/setInlineErrorMessage", |
| 975 | payload: undefined, |
| 976 | }, |
| 977 | { |
| 978 | type: "session/setIsPruned", |
| 979 | payload: false, |
| 980 | }, |
| 981 | { |
| 982 | type: "session/setContextPercentage", |
| 983 | payload: 0.7, |
| 984 | }, |
| 985 | { |
| 986 | type: "symbols/updateFromContextItems/fulfilled", |
| 987 | meta: { |
| 988 | arg: [], |
| 989 | requestId: expect.any(String), |
| 990 | requestStatus: "fulfilled", |
| 991 | }, |
| 992 | payload: undefined, |
| 993 | }, |
| 994 | { |
| 995 | type: "session/streamUpdate", |
| 996 | payload: [ |
| 997 | { |
| 998 | role: "assistant", |
| 999 | content: "I'll edit the file for you.", |
| 1000 | }, |
| 1001 | ], |
| 1002 | }, |
| 1003 | { |
| 1004 | type: "session/streamUpdate", |
| 1005 | payload: [ |
| 1006 | { |
| 1007 | role: "assistant", |
| 1008 | content: "", |
| 1009 | toolCalls: [ |
| 1010 | { |
| 1011 | id: "tool-reject-1", |
| 1012 | type: "function", |
| 1013 | function: { |
| 1014 | name: "edit_existing_file", |
| 1015 | arguments: '{"filepath":"test.js","changes":"const x = 1;"}', |
| 1016 | }, |
| 1017 | }, |
| 1018 | ], |
| 1019 | }, |
| 1020 | ], |
| 1021 | }, |
| 1022 | { |
| 1023 | type: "session/addPromptCompletionPair", |
| 1024 | payload: [ |
| 1025 | { |
| 1026 | completion: "I'll edit the file for you.", |
| 1027 | modelProvider: "anthropic", |
| 1028 | prompt: "Please edit this file", |
| 1029 | }, |
| 1030 | ], |
| 1031 | }, |
| 1032 | { |
| 1033 | type: "session/errorToolCall", |
| 1034 | payload: { |
| 1035 | toolCallId: "tool-reject-1", |
| 1036 | }, |
| 1037 | }, |
| 1038 | { |
| 1039 | type: "session/updateToolCallOutput", |
| 1040 | payload: { |
| 1041 | toolCallId: "tool-reject-1", |
| 1042 | contextItems: [ |
| 1043 | { |
| 1044 | icon: "problems", |
| 1045 | name: "Security Policy Violation", |
| 1046 | description: "Command Disabled", |
| 1047 | content: `This command has been disabled by security policy:\n\nedit_existing_file\n\nThis command cannot be executed as it may pose a security risk.`, |
| 1048 | hidden: false, |
| 1049 | }, |
| 1050 | ], |
| 1051 | }, |
| 1052 | }, |
| 1053 | { |
| 1054 | type: "session/setInactive", |
| 1055 | payload: undefined, |
| 1056 | }, |
| 1057 | { |
| 1058 | type: "chat/streamNormalInput/fulfilled", |
| 1059 | meta: { |
| 1060 | arg: { |
| 1061 | legacySlashCommandData: undefined, |
| 1062 | }, |
| 1063 | requestId: expect.any(String), |
| 1064 | requestStatus: "fulfilled", |
| 1065 | }, |
| 1066 | payload: undefined, |
| 1067 | }, |
| 1068 | { |
| 1069 | type: "session/saveCurrent/pending", |
| 1070 | meta: { |
| 1071 | arg: { |
| 1072 | generateTitle: true, |
| 1073 | openNewSession: false, |
| 1074 | }, |
| 1075 | requestId: expect.any(String), |
| 1076 | requestStatus: "pending", |
| 1077 | }, |
| 1078 | payload: undefined, |
| 1079 | }, |
| 1080 | { |
| 1081 | type: "session/update/pending", |
| 1082 | meta: { |
| 1083 | arg: expect.objectContaining({ |
| 1084 | history: expect.any(Array), |
| 1085 | sessionId: "session-123", |
| 1086 | title: "New Session", |
| 1087 | workspaceDirectory: "", |
| 1088 | }), |
| 1089 | requestId: expect.any(String), |
| 1090 | requestStatus: "pending", |
| 1091 | }, |
| 1092 | payload: undefined, |
| 1093 | }, |
| 1094 | { |
| 1095 | type: "session/updateSessionMetadata", |
| 1096 | payload: { |
| 1097 | sessionId: "session-123", |
| 1098 | title: "New Session", |
| 1099 | }, |
| 1100 | }, |
| 1101 | { |
| 1102 | type: "session/refreshMetadata/pending", |
| 1103 | meta: { |
| 1104 | arg: {}, |
| 1105 | requestId: expect.any(String), |
| 1106 | requestStatus: "pending", |
| 1107 | }, |
| 1108 | payload: undefined, |
| 1109 | }, |
| 1110 | { |
| 1111 | type: "session/setIsSessionMetadataLoading", |
| 1112 | payload: false, |
| 1113 | }, |
| 1114 | { |
| 1115 | type: "session/setAllSessionMetadata", |
| 1116 | payload: { |
| 1117 | compiledChatMessages: [ |
| 1118 | { |
| 1119 | content: "Please edit this file", |
| 1120 | role: "user", |
| 1121 | }, |
| 1122 | ], |
| 1123 | contextPercentage: 0.7, |
| 1124 | didPrune: false, |
| 1125 | }, |
| 1126 | }, |
| 1127 | { |
| 1128 | type: "session/refreshMetadata/fulfilled", |
| 1129 | meta: { |
| 1130 | arg: {}, |
| 1131 | requestId: expect.any(String), |
| 1132 | requestStatus: "fulfilled", |
| 1133 | }, |
| 1134 | payload: { |
| 1135 | compiledChatMessages: [ |
| 1136 | { |
| 1137 | content: "Please edit this file", |
| 1138 | role: "user", |
| 1139 | }, |
| 1140 | ], |
| 1141 | contextPercentage: 0.7, |
| 1142 | didPrune: false, |
| 1143 | }, |
| 1144 | }, |
| 1145 | { |
| 1146 | type: "session/update/fulfilled", |
| 1147 | meta: { |
| 1148 | arg: expect.objectContaining({ |
| 1149 | history: expect.any(Array), |
| 1150 | sessionId: "session-123", |
| 1151 | title: "New Session", |
| 1152 | workspaceDirectory: "", |
| 1153 | }), |
| 1154 | requestId: expect.any(String), |
| 1155 | requestStatus: "fulfilled", |
| 1156 | }, |
| 1157 | payload: undefined, |
| 1158 | }, |
| 1159 | { |
| 1160 | type: "session/saveCurrent/fulfilled", |
| 1161 | meta: { |
| 1162 | arg: { |
| 1163 | generateTitle: true, |
| 1164 | openNewSession: false, |
| 1165 | }, |
| 1166 | requestId: expect.any(String), |
| 1167 | requestStatus: "fulfilled", |
| 1168 | }, |
| 1169 | payload: undefined, |
| 1170 | }, |
| 1171 | { |
| 1172 | type: "chat/streamWrapper/fulfilled", |
| 1173 | meta: { |
| 1174 | arg: expect.any(Function), |
| 1175 | requestId: expect.any(String), |
| 1176 | requestStatus: "fulfilled", |
| 1177 | }, |
| 1178 | payload: undefined, |
| 1179 | }, |
| 1180 | { |
| 1181 | type: "chat/streamResponse/fulfilled", |
| 1182 | meta: { |
| 1183 | arg: { |
| 1184 | editorState: mockEditorState, |
| 1185 | modifiers: mockModifiers, |
| 1186 | }, |
| 1187 | requestId: expect.any(String), |
| 1188 | requestStatus: "fulfilled", |
| 1189 | }, |
| 1190 | payload: undefined, |
| 1191 | }, |
| 1192 | ]); |
| 1193 | |
| 1194 | // Verify IDE messenger calls - compilation should succeed, streaming should happen |
| 1195 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1196 | "llm/compileChat", |
| 1197 | { |
| 1198 | messages: [ |
| 1199 | { |
| 1200 | role: "system", |
| 1201 | content: "You are a helpful assistant.", |
| 1202 | }, |
| 1203 | { |
| 1204 | role: "user", |
| 1205 | content: [ |
| 1206 | { |
| 1207 | type: "text", |
| 1208 | text: "Please edit this file", |
| 1209 | }, |
| 1210 | ], |
| 1211 | }, |
| 1212 | { |
| 1213 | role: "user", |
| 1214 | content: [ |
| 1215 | { |
| 1216 | type: "text", |
| 1217 | text: "Hello, please help me with this code", |
| 1218 | }, |
| 1219 | ], |
| 1220 | }, |
| 1221 | ], |
| 1222 | options: { |
| 1223 | reasoning: false, |
| 1224 | }, |
| 1225 | }, |
| 1226 | ); |
| 1227 | |
| 1228 | expect(mockIdeMessengerReject.llmStreamChat).toHaveBeenCalledWith( |
| 1229 | { |
| 1230 | completionOptions: { |
| 1231 | reasoning: false, |
| 1232 | }, |
| 1233 | legacySlashCommandData: undefined, |
| 1234 | messageOptions: { precompiled: true }, |
| 1235 | messages: [ |
| 1236 | { |
| 1237 | role: "user", |
| 1238 | content: "Please edit this file", |
| 1239 | }, |
| 1240 | ], |
| 1241 | title: "Claude 3.5 Sonnet", |
| 1242 | }, |
| 1243 | expect.any(AbortSignal), |
| 1244 | ); |
| 1245 | |
| 1246 | // Should NOT call tools/call since tool is disabled/rejected |
| 1247 | expect(mockIdeMessengerReject.request).not.toHaveBeenCalledWith( |
| 1248 | "tools/call", |
| 1249 | expect.anything(), |
| 1250 | ); |
| 1251 | |
| 1252 | // Verify session save was called |
| 1253 | expect(mockIdeMessengerReject.request).toHaveBeenCalledWith( |
| 1254 | "history/save", |
| 1255 | expect.anything(), |
| 1256 | ); |
| 1257 | |
| 1258 | // Verify final state contains the tool call but it's not executed |
| 1259 | const finalState = mockStoreWithToolReject.getState(); |
| 1260 | expect(finalState).toEqual({ |
| 1261 | session: { |
| 1262 | history: [ |
| 1263 | { |
| 1264 | contextItems: [], |
| 1265 | message: { |
| 1266 | id: "1", |
| 1267 | role: "user", |
| 1268 | content: "Please edit this file", |
| 1269 | }, |
| 1270 | }, |
| 1271 | { |
| 1272 | appliedRules: [], |
| 1273 | contextItems: [], |
| 1274 | editorState: mockEditorState, |
| 1275 | message: { |
| 1276 | content: "Hello, please help me with this code", |
| 1277 | id: expect.any(String), |
| 1278 | role: "user", |
| 1279 | }, |
| 1280 | }, |
| 1281 | { |
| 1282 | contextItems: [], |
| 1283 | isGatheringContext: false, |
| 1284 | message: { |
| 1285 | content: "I'll edit the file for you.", |
| 1286 | id: "mock-uuid-123", |
| 1287 | role: "assistant", |
| 1288 | toolCalls: [ |
| 1289 | { |
| 1290 | id: "tool-reject-1", |
| 1291 | type: "function", |
| 1292 | function: { |
| 1293 | name: "edit_existing_file", |
| 1294 | arguments: JSON.stringify({ |
| 1295 | filepath: "test.js", |
| 1296 | changes: "const x = 1;", |
| 1297 | }), |
| 1298 | }, |
| 1299 | }, |
| 1300 | ], |
| 1301 | }, |
| 1302 | promptLogs: [ |
| 1303 | { |
| 1304 | completion: "I'll edit the file for you.", |
| 1305 | modelProvider: "anthropic", |
| 1306 | prompt: "Please edit this file", |
| 1307 | }, |
| 1308 | ], |
| 1309 | toolCallStates: [ |
| 1310 | { |
| 1311 | toolCallId: "tool-reject-1", |
| 1312 | toolCall: { |
| 1313 | id: "tool-reject-1", |
| 1314 | type: "function", |
| 1315 | function: { |
| 1316 | name: "edit_existing_file", |
| 1317 | arguments: JSON.stringify({ |
| 1318 | filepath: "test.js", |
| 1319 | changes: "const x = 1;", |
| 1320 | }), |
| 1321 | }, |
| 1322 | }, |
| 1323 | parsedArgs: { |
| 1324 | filepath: "test.js", |
| 1325 | changes: "const x = 1;", |
| 1326 | }, |
| 1327 | status: "errored", // Tool call is marked as errored due to being disabled |
| 1328 | output: [ |
| 1329 | { |
| 1330 | icon: "problems", |
| 1331 | name: "Security Policy Violation", |
| 1332 | description: "Command Disabled", |
| 1333 | content: `This command has been disabled by security policy:\n\nedit_existing_file\n\nThis command cannot be executed as it may pose a security risk.`, |
| 1334 | hidden: false, |
| 1335 | }, |
| 1336 | ], |
| 1337 | }, |
| 1338 | ], |
| 1339 | }, |
| 1340 | ], |
| 1341 | hasReasoningEnabled: false, |
| 1342 | isStreaming: false, |
| 1343 | id: "session-123", |
| 1344 | mode: "chat", |
| 1345 | streamAborter: expect.any(AbortController), |
| 1346 | contextPercentage: 0.7, |
| 1347 | isPruned: false, |
| 1348 | isInEdit: false, |
| 1349 | title: "New Session", |
| 1350 | lastSessionId: undefined, |
| 1351 | isSessionMetadataLoading: false, |
| 1352 | allSessionMetadata: { |
| 1353 | compiledChatMessages: [ |
| 1354 | { |
| 1355 | content: "Please edit this file", |
| 1356 | role: "user", |
| 1357 | }, |
| 1358 | ], |
| 1359 | contextPercentage: 0.7, |
| 1360 | didPrune: false, |
| 1361 | }, |
| 1362 | symbols: {}, |
| 1363 | codeBlockApplyStates: { |
| 1364 | states: [], |
| 1365 | curIndex: 0, |
| 1366 | }, |
| 1367 | newestToolbarPreviewForInput: {}, |
| 1368 | compactionLoading: {}, |
| 1369 | inlineErrorMessage: undefined, |
| 1370 | }, |
| 1371 | config: { |
| 1372 | config: { |
| 1373 | tools: [], |
| 1374 | rules: [], |
| 1375 | tabAutocompleteModel: undefined, |
| 1376 | selectedModelByRole: { |
| 1377 | chat: mockClaudeModel, |
| 1378 | apply: null, |
| 1379 | edit: null, |
| 1380 | summarize: null, |
| 1381 | autocomplete: null, |
| 1382 | rerank: null, |
| 1383 | embed: null, |
| 1384 | }, |
| 1385 | experimental: { |
| 1386 | onlyUseSystemMessageTools: false, |
| 1387 | }, |
| 1388 | }, |
| 1389 | lastSelectedModelByRole: { |
| 1390 | chat: mockClaudeModel.title, |
| 1391 | }, |
| 1392 | loading: false, |
| 1393 | configError: undefined, |
| 1394 | }, |
| 1395 | ui: { |
| 1396 | toolSettings: { |
| 1397 | edit_existing_file: "disabled", |
| 1398 | }, |
| 1399 | ruleSettings: {}, |
| 1400 | showDialog: false, |
| 1401 | dialogMessage: undefined, |
| 1402 | }, |
| 1403 | editModeState: { |
| 1404 | isInEdit: false, |
| 1405 | returnToMode: "chat", |
| 1406 | }, |
| 1407 | indexing: { |
| 1408 | indexing: { |
| 1409 | statuses: {}, |
| 1410 | hiddenChatPeekTypes: { |
| 1411 | docs: false, |
| 1412 | }, |
| 1413 | }, |
| 1414 | }, |
| 1415 | tabs: { |
| 1416 | tabsItems: [], |
| 1417 | }, |
| 1418 | profiles: { |
| 1419 | organizations: [], |
| 1420 | selectedProfileId: null, |
| 1421 | selectedOrganizationId: null, |
| 1422 | preferencesByProfileId: {}, |
| 1423 | }, |
| 1424 | }); |
| 1425 | }); |
| 1426 | |
| 1427 | it("should handle tool call requiring manual approval", async () => { |
| 1428 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 1429 | |
| 1430 | // Create store with tool settings that require manual approval (ask first) |
| 1431 | const mockStoreWithManualApproval = createMockStore({ |
| 1432 | session: { |
| 1433 | history: [ |
| 1434 | { |
| 1435 | message: { |
| 1436 | id: "1", |
| 1437 | role: "user", |
| 1438 | content: "Please search the codebase", |
| 1439 | }, |
| 1440 | contextItems: [], |
| 1441 | }, |
| 1442 | ], |
| 1443 | hasReasoningEnabled: false, |
| 1444 | isStreaming: true, |
| 1445 | id: "session-123", |
| 1446 | mode: "chat", |
| 1447 | streamAborter: new AbortController(), |
| 1448 | contextPercentage: 0, |
| 1449 | isPruned: false, |
| 1450 | isInEdit: false, |
| 1451 | title: "", |
| 1452 | lastSessionId: undefined, |
| 1453 | isSessionMetadataLoading: false, |
| 1454 | allSessionMetadata: [], |
| 1455 | symbols: {}, |
| 1456 | codeBlockApplyStates: { |
| 1457 | states: [], |
| 1458 | curIndex: 0, |
| 1459 | }, |
| 1460 | newestToolbarPreviewForInput: {}, |
| 1461 | compactionLoading: {}, |
| 1462 | inlineErrorMessage: undefined, |
| 1463 | }, |
| 1464 | config: { |
| 1465 | config: { |
| 1466 | tools: [], |
| 1467 | rules: [], |
| 1468 | tabAutocompleteModel: undefined, |
| 1469 | selectedModelByRole: { |
| 1470 | chat: mockClaudeModel, |
| 1471 | apply: null, |
| 1472 | edit: null, |
| 1473 | summarize: null, |
| 1474 | autocomplete: null, |
| 1475 | rerank: null, |
| 1476 | embed: null, |
| 1477 | }, |
| 1478 | experimental: { |
| 1479 | onlyUseSystemMessageTools: false, |
| 1480 | }, |
| 1481 | } as any, |
| 1482 | lastSelectedModelByRole: { |
| 1483 | chat: mockClaudeModel.title, |
| 1484 | }, |
| 1485 | } as any, |
| 1486 | ui: { |
| 1487 | toolSettings: { |
| 1488 | search_codebase: "askFirst", // Requires manual approval |
| 1489 | }, |
| 1490 | ruleSettings: {}, |
| 1491 | showDialog: false, |
| 1492 | dialogMessage: undefined, |
| 1493 | } as any, |
| 1494 | }); |
| 1495 | |
| 1496 | const mockIdeMessengerManual = mockStoreWithManualApproval.mockIdeMessenger; |
| 1497 | |
| 1498 | // Setup successful compilation |
| 1499 | mockIdeMessengerManual.request.mockResolvedValue({ |
| 1500 | status: "success", |
| 1501 | content: { |
| 1502 | compiledChatMessages: [ |
| 1503 | { role: "user", content: "Please search the codebase" }, |
| 1504 | ], |
| 1505 | didPrune: false, |
| 1506 | contextPercentage: 0.9, |
| 1507 | }, |
| 1508 | }); |
| 1509 | |
| 1510 | // Setup streaming generator with tool call requiring approval |
| 1511 | async function* mockStreamGeneratorWithApprovalTool() { |
| 1512 | yield [ |
| 1513 | { role: "assistant", content: "I'll search the codebase for you." }, |
| 1514 | ]; |
| 1515 | yield [ |
| 1516 | { |
| 1517 | role: "assistant", |
| 1518 | content: "", |
| 1519 | toolCalls: [ |
| 1520 | { |
| 1521 | id: "tool-approval-1", |
| 1522 | type: "function", |
| 1523 | function: { |
| 1524 | name: "search_codebase", |
| 1525 | arguments: JSON.stringify({ query: "test function" }), |
| 1526 | }, |
| 1527 | }, |
| 1528 | ], |
| 1529 | }, |
| 1530 | ]; |
| 1531 | return { |
| 1532 | prompt: "Please search the codebase", |
| 1533 | completion: "I'll search the codebase for you.", |
| 1534 | modelProvider: "anthropic", |
| 1535 | }; |
| 1536 | } |
| 1537 | |
| 1538 | mockIdeMessengerManual.llmStreamChat.mockReturnValue( |
| 1539 | mockStreamGeneratorWithApprovalTool(), |
| 1540 | ); |
| 1541 | |
| 1542 | // Track isStreaming state changes to ensure it doesn't become false after tool generation |
| 1543 | const streamingStates: boolean[] = []; |
| 1544 | const originalDispatch = mockStoreWithManualApproval.dispatch; |
| 1545 | mockStoreWithManualApproval.dispatch = ((action: any) => { |
| 1546 | const result = originalDispatch(action); |
| 1547 | // Capture isStreaming state after each action |
| 1548 | const currentState = |
| 1549 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1550 | streamingStates.push(currentState); |
| 1551 | return result; |
| 1552 | }) as any; |
| 1553 | |
| 1554 | // Execute thunk |
| 1555 | const result = await mockStoreWithManualApproval.dispatch( |
| 1556 | streamResponseThunk({ |
| 1557 | editorState: mockEditorState, |
| 1558 | modifiers: mockModifiers, |
| 1559 | }) as any, |
| 1560 | ); |
| 1561 | |
| 1562 | // Verify thunk completed successfully (tool waits for approval) |
| 1563 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 1564 | |
| 1565 | // Since tool requires approval, isStreaming should become false after completion |
| 1566 | // but should stay true throughout the initial streaming phase |
| 1567 | const finalStreamingState = |
| 1568 | mockStoreWithManualApproval.getState().session.isStreaming; |
| 1569 | expect(finalStreamingState).toBe(false); // Final state should be false since we're waiting for approval |
| 1570 | |
| 1571 | // Verify exact action sequence includes tool generation but NO execution |
| 1572 | const dispatchedActions = (mockStoreWithManualApproval as any).getActions(); |
| 1573 | expect(dispatchedActions).toEqual([ |
| 1574 | { |
| 1575 | type: "chat/streamResponse/pending", |
| 1576 | meta: { |
| 1577 | arg: { |
| 1578 | editorState: mockEditorState, |
| 1579 | modifiers: mockModifiers, |
| 1580 | }, |
| 1581 | requestId: expect.any(String), |
| 1582 | requestStatus: "pending", |
| 1583 | }, |
| 1584 | payload: undefined, |
| 1585 | }, |
| 1586 | { |
| 1587 | type: "chat/streamWrapper/pending", |
| 1588 | meta: { |
| 1589 | arg: expect.any(Function), |
| 1590 | requestId: expect.any(String), |
| 1591 | requestStatus: "pending", |
| 1592 | }, |
| 1593 | payload: undefined, |
| 1594 | }, |
| 1595 | { |
| 1596 | type: "session/submitEditorAndInitAtIndex", |
| 1597 | payload: { |
| 1598 | editorState: mockEditorState, |
| 1599 | index: 1, |
| 1600 | }, |
| 1601 | }, |
| 1602 | { |
| 1603 | type: "session/resetNextCodeBlockToApplyIndex", |
| 1604 | payload: undefined, |
| 1605 | }, |
| 1606 | { |
| 1607 | type: "symbols/updateFromContextItems/pending", |
| 1608 | meta: { |
| 1609 | arg: [], |
| 1610 | requestId: expect.any(String), |
| 1611 | requestStatus: "pending", |
| 1612 | }, |
| 1613 | payload: undefined, |
| 1614 | }, |
| 1615 | { |
| 1616 | type: "session/updateHistoryItemAtIndex", |
| 1617 | payload: { |
| 1618 | index: 1, |
| 1619 | updates: { |
| 1620 | contextItems: [], |
| 1621 | message: { |
| 1622 | content: "Hello, please help me with this code", |
| 1623 | id: "mock-uuid-123", |
| 1624 | role: "user", |
| 1625 | }, |
| 1626 | }, |
| 1627 | }, |
| 1628 | }, |
| 1629 | { |
| 1630 | type: "chat/streamNormalInput/pending", |
| 1631 | meta: { |
| 1632 | arg: { |
| 1633 | legacySlashCommandData: undefined, |
| 1634 | }, |
| 1635 | requestId: expect.any(String), |
| 1636 | requestStatus: "pending", |
| 1637 | }, |
| 1638 | payload: undefined, |
| 1639 | }, |
| 1640 | { |
| 1641 | type: "session/setAppliedRulesAtIndex", |
| 1642 | payload: { |
| 1643 | appliedRules: [], |
| 1644 | index: 1, |
| 1645 | }, |
| 1646 | }, |
| 1647 | { |
| 1648 | type: "session/setActive", |
| 1649 | payload: undefined, |
| 1650 | }, |
| 1651 | { |
| 1652 | type: "session/setInlineErrorMessage", |
| 1653 | payload: undefined, |
| 1654 | }, |
| 1655 | { |
| 1656 | type: "session/setIsPruned", |
| 1657 | payload: false, |
| 1658 | }, |
| 1659 | { |
| 1660 | type: "session/setContextPercentage", |
| 1661 | payload: 0.9, |
| 1662 | }, |
| 1663 | { |
| 1664 | type: "symbols/updateFromContextItems/fulfilled", |
| 1665 | meta: { |
| 1666 | arg: [], |
| 1667 | requestId: expect.any(String), |
| 1668 | requestStatus: "fulfilled", |
| 1669 | }, |
| 1670 | payload: undefined, |
| 1671 | }, |
| 1672 | { |
| 1673 | type: "session/streamUpdate", |
| 1674 | payload: [ |
| 1675 | { |
| 1676 | role: "assistant", |
| 1677 | content: "I'll search the codebase for you.", |
| 1678 | }, |
| 1679 | ], |
| 1680 | }, |
| 1681 | { |
| 1682 | type: "session/streamUpdate", |
| 1683 | payload: [ |
| 1684 | { |
| 1685 | role: "assistant", |
| 1686 | content: "", |
| 1687 | toolCalls: [ |
| 1688 | { |
| 1689 | id: "tool-approval-1", |
| 1690 | type: "function", |
| 1691 | function: { |
| 1692 | name: "search_codebase", |
| 1693 | arguments: JSON.stringify({ query: "test function" }), |
| 1694 | }, |
| 1695 | }, |
| 1696 | ], |
| 1697 | }, |
| 1698 | ], |
| 1699 | }, |
| 1700 | { |
| 1701 | type: "session/addPromptCompletionPair", |
| 1702 | payload: [ |
| 1703 | { |
| 1704 | completion: "I'll search the codebase for you.", |
| 1705 | modelProvider: "anthropic", |
| 1706 | prompt: "Please search the codebase", |
| 1707 | }, |
| 1708 | ], |
| 1709 | }, |
| 1710 | { |
| 1711 | type: "session/setToolGenerated", |
| 1712 | payload: { |
| 1713 | toolCallId: "tool-approval-1", |
| 1714 | tools: [], |
| 1715 | }, |
| 1716 | }, |
| 1717 | { |
| 1718 | type: "session/setInactive", |
| 1719 | payload: undefined, |
| 1720 | }, |
| 1721 | { |
| 1722 | type: "chat/streamNormalInput/fulfilled", |
| 1723 | meta: { |
| 1724 | arg: { |
| 1725 | legacySlashCommandData: undefined, |
| 1726 | }, |
| 1727 | requestId: expect.any(String), |
| 1728 | requestStatus: "fulfilled", |
| 1729 | }, |
| 1730 | payload: undefined, |
| 1731 | }, |
| 1732 | { |
| 1733 | type: "session/saveCurrent/pending", |
| 1734 | meta: { |
| 1735 | arg: { |
| 1736 | generateTitle: true, |
| 1737 | openNewSession: false, |
| 1738 | }, |
| 1739 | requestId: expect.any(String), |
| 1740 | requestStatus: "pending", |
| 1741 | }, |
| 1742 | payload: undefined, |
| 1743 | }, |
| 1744 | { |
| 1745 | type: "session/update/pending", |
| 1746 | meta: { |
| 1747 | arg: expect.objectContaining({ |
| 1748 | history: expect.any(Array), |
| 1749 | sessionId: "session-123", |
| 1750 | title: "New Session", |
| 1751 | workspaceDirectory: "", |
| 1752 | }), |
| 1753 | requestId: expect.any(String), |
| 1754 | requestStatus: "pending", |
| 1755 | }, |
| 1756 | payload: undefined, |
| 1757 | }, |
| 1758 | { |
| 1759 | type: "session/updateSessionMetadata", |
| 1760 | payload: { |
| 1761 | sessionId: "session-123", |
| 1762 | title: "New Session", |
| 1763 | }, |
| 1764 | }, |
| 1765 | { |
| 1766 | type: "session/refreshMetadata/pending", |
| 1767 | meta: { |
| 1768 | arg: {}, |
| 1769 | requestId: expect.any(String), |
| 1770 | requestStatus: "pending", |
| 1771 | }, |
| 1772 | payload: undefined, |
| 1773 | }, |
| 1774 | { |
| 1775 | type: "session/setIsSessionMetadataLoading", |
| 1776 | payload: false, |
| 1777 | }, |
| 1778 | { |
| 1779 | type: "session/setAllSessionMetadata", |
| 1780 | payload: { |
| 1781 | compiledChatMessages: [ |
| 1782 | { |
| 1783 | content: "Please search the codebase", |
| 1784 | role: "user", |
| 1785 | }, |
| 1786 | ], |
| 1787 | contextPercentage: 0.9, |
| 1788 | didPrune: false, |
| 1789 | }, |
| 1790 | }, |
| 1791 | { |
| 1792 | type: "session/refreshMetadata/fulfilled", |
| 1793 | meta: { |
| 1794 | arg: {}, |
| 1795 | requestId: expect.any(String), |
| 1796 | requestStatus: "fulfilled", |
| 1797 | }, |
| 1798 | payload: { |
| 1799 | compiledChatMessages: [ |
| 1800 | { |
| 1801 | content: "Please search the codebase", |
| 1802 | role: "user", |
| 1803 | }, |
| 1804 | ], |
| 1805 | contextPercentage: 0.9, |
| 1806 | didPrune: false, |
| 1807 | }, |
| 1808 | }, |
| 1809 | { |
| 1810 | type: "session/update/fulfilled", |
| 1811 | meta: { |
| 1812 | arg: expect.objectContaining({ |
| 1813 | history: expect.any(Array), |
| 1814 | sessionId: "session-123", |
| 1815 | title: "New Session", |
| 1816 | workspaceDirectory: "", |
| 1817 | }), |
| 1818 | requestId: expect.any(String), |
| 1819 | requestStatus: "fulfilled", |
| 1820 | }, |
| 1821 | payload: undefined, |
| 1822 | }, |
| 1823 | { |
| 1824 | type: "session/saveCurrent/fulfilled", |
| 1825 | meta: { |
| 1826 | arg: { |
| 1827 | generateTitle: true, |
| 1828 | openNewSession: false, |
| 1829 | }, |
| 1830 | requestId: expect.any(String), |
| 1831 | requestStatus: "fulfilled", |
| 1832 | }, |
| 1833 | payload: undefined, |
| 1834 | }, |
| 1835 | { |
| 1836 | type: "chat/streamWrapper/fulfilled", |
| 1837 | meta: { |
| 1838 | arg: expect.any(Function), |
| 1839 | requestId: expect.any(String), |
| 1840 | requestStatus: "fulfilled", |
| 1841 | }, |
| 1842 | payload: undefined, |
| 1843 | }, |
| 1844 | { |
| 1845 | type: "chat/streamResponse/fulfilled", |
| 1846 | meta: { |
| 1847 | arg: { |
| 1848 | editorState: mockEditorState, |
| 1849 | modifiers: mockModifiers, |
| 1850 | }, |
| 1851 | requestId: expect.any(String), |
| 1852 | requestStatus: "fulfilled", |
| 1853 | }, |
| 1854 | payload: undefined, |
| 1855 | }, |
| 1856 | ]); |
| 1857 | |
| 1858 | // Verify IDE messenger calls - compilation should happen, streaming should happen |
| 1859 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1860 | "llm/compileChat", |
| 1861 | { |
| 1862 | messages: [ |
| 1863 | { |
| 1864 | role: "system", |
| 1865 | content: "You are a helpful assistant.", |
| 1866 | }, |
| 1867 | { |
| 1868 | role: "user", |
| 1869 | content: [ |
| 1870 | { |
| 1871 | type: "text", |
| 1872 | text: "Please search the codebase", |
| 1873 | }, |
| 1874 | ], |
| 1875 | }, |
| 1876 | { |
| 1877 | role: "user", |
| 1878 | content: [ |
| 1879 | { |
| 1880 | type: "text", |
| 1881 | text: "Hello, please help me with this code", |
| 1882 | }, |
| 1883 | ], |
| 1884 | }, |
| 1885 | ], |
| 1886 | options: { |
| 1887 | reasoning: false, |
| 1888 | }, |
| 1889 | }, |
| 1890 | ); |
| 1891 | |
| 1892 | expect(mockIdeMessengerManual.llmStreamChat).toHaveBeenCalledWith( |
| 1893 | { |
| 1894 | completionOptions: { |
| 1895 | reasoning: false, |
| 1896 | }, |
| 1897 | legacySlashCommandData: undefined, |
| 1898 | messageOptions: { precompiled: true }, |
| 1899 | messages: [ |
| 1900 | { |
| 1901 | role: "user", |
| 1902 | content: "Please search the codebase", |
| 1903 | }, |
| 1904 | ], |
| 1905 | title: "Claude 3.5 Sonnet", |
| 1906 | }, |
| 1907 | expect.any(AbortSignal), |
| 1908 | ); |
| 1909 | |
| 1910 | // Should NOT call tools/call since tool requires approval |
| 1911 | expect(mockIdeMessengerManual.request).not.toHaveBeenCalledWith( |
| 1912 | "tools/call", |
| 1913 | expect.anything(), |
| 1914 | ); |
| 1915 | |
| 1916 | // Verify session save was called |
| 1917 | expect(mockIdeMessengerManual.request).toHaveBeenCalledWith( |
| 1918 | "history/save", |
| 1919 | expect.anything(), |
| 1920 | ); |
| 1921 | |
| 1922 | // Verify final state contains the tool call in "generated" state (waiting for approval) |
| 1923 | const finalState = mockStoreWithManualApproval.getState(); |
| 1924 | expect(finalState).toEqual({ |
| 1925 | session: { |
| 1926 | history: [ |
| 1927 | { |
| 1928 | contextItems: [], |
| 1929 | message: { |
| 1930 | id: "1", |
| 1931 | role: "user", |
| 1932 | content: "Please search the codebase", |
| 1933 | }, |
| 1934 | }, |
| 1935 | { |
| 1936 | appliedRules: [], |
| 1937 | contextItems: [], |
| 1938 | editorState: mockEditorState, |
| 1939 | message: { |
| 1940 | content: "Hello, please help me with this code", |
| 1941 | id: expect.any(String), |
| 1942 | role: "user", |
| 1943 | }, |
| 1944 | }, |
| 1945 | { |
| 1946 | contextItems: [], |
| 1947 | isGatheringContext: false, |
| 1948 | message: { |
| 1949 | content: "I'll search the codebase for you.", |
| 1950 | id: "mock-uuid-123", |
| 1951 | role: "assistant", |
| 1952 | toolCalls: [ |
| 1953 | { |
| 1954 | id: "tool-approval-1", |
| 1955 | type: "function", |
| 1956 | function: { |
| 1957 | name: "search_codebase", |
| 1958 | arguments: JSON.stringify({ query: "test function" }), |
| 1959 | }, |
| 1960 | }, |
| 1961 | ], |
| 1962 | }, |
| 1963 | promptLogs: [ |
| 1964 | { |
| 1965 | completion: "I'll search the codebase for you.", |
| 1966 | modelProvider: "anthropic", |
| 1967 | prompt: "Please search the codebase", |
| 1968 | }, |
| 1969 | ], |
| 1970 | toolCallStates: [ |
| 1971 | { |
| 1972 | toolCallId: "tool-approval-1", |
| 1973 | toolCall: { |
| 1974 | id: "tool-approval-1", |
| 1975 | type: "function", |
| 1976 | function: { |
| 1977 | name: "search_codebase", |
| 1978 | arguments: JSON.stringify({ query: "test function" }), |
| 1979 | }, |
| 1980 | }, |
| 1981 | parsedArgs: { query: "test function" }, |
| 1982 | status: "generated", // Waiting for user approval |
| 1983 | }, |
| 1984 | ], |
| 1985 | }, |
| 1986 | ], |
| 1987 | hasReasoningEnabled: false, |
| 1988 | isStreaming: false, |
| 1989 | id: "session-123", |
| 1990 | mode: "chat", |
| 1991 | streamAborter: expect.any(AbortController), |
| 1992 | contextPercentage: 0.9, |
| 1993 | isPruned: false, |
| 1994 | isInEdit: false, |
| 1995 | title: "New Session", |
| 1996 | lastSessionId: undefined, |
| 1997 | isSessionMetadataLoading: false, |
| 1998 | allSessionMetadata: { |
| 1999 | compiledChatMessages: [ |
| 2000 | { |
| 2001 | content: "Please search the codebase", |
| 2002 | role: "user", |
| 2003 | }, |
| 2004 | ], |
| 2005 | contextPercentage: 0.9, |
| 2006 | didPrune: false, |
| 2007 | }, |
| 2008 | symbols: {}, |
| 2009 | codeBlockApplyStates: { |
| 2010 | states: [], |
| 2011 | curIndex: 0, |
| 2012 | }, |
| 2013 | newestToolbarPreviewForInput: {}, |
| 2014 | compactionLoading: {}, |
| 2015 | inlineErrorMessage: undefined, |
| 2016 | }, |
| 2017 | config: { |
| 2018 | config: { |
| 2019 | tools: [], |
| 2020 | rules: [], |
| 2021 | tabAutocompleteModel: undefined, |
| 2022 | selectedModelByRole: { |
| 2023 | chat: mockClaudeModel, |
| 2024 | apply: null, |
| 2025 | edit: null, |
| 2026 | summarize: null, |
| 2027 | autocomplete: null, |
| 2028 | rerank: null, |
| 2029 | embed: null, |
| 2030 | }, |
| 2031 | experimental: { |
| 2032 | onlyUseSystemMessageTools: false, |
| 2033 | }, |
| 2034 | }, |
| 2035 | lastSelectedModelByRole: { |
| 2036 | chat: mockClaudeModel.title, |
| 2037 | }, |
| 2038 | loading: false, |
| 2039 | configError: undefined, |
| 2040 | }, |
| 2041 | ui: { |
| 2042 | toolSettings: { |
| 2043 | search_codebase: "askFirst", |
| 2044 | }, |
| 2045 | ruleSettings: {}, |
| 2046 | showDialog: false, |
| 2047 | dialogMessage: undefined, |
| 2048 | }, |
| 2049 | editModeState: { |
| 2050 | isInEdit: false, |
| 2051 | returnToMode: "chat", |
| 2052 | }, |
| 2053 | indexing: { |
| 2054 | indexing: { |
| 2055 | statuses: {}, |
| 2056 | hiddenChatPeekTypes: { |
| 2057 | docs: false, |
| 2058 | }, |
| 2059 | }, |
| 2060 | }, |
| 2061 | tabs: { |
| 2062 | tabsItems: [], |
| 2063 | }, |
| 2064 | profiles: { |
| 2065 | organizations: [], |
| 2066 | selectedProfileId: null, |
| 2067 | selectedOrganizationId: null, |
| 2068 | preferencesByProfileId: {}, |
| 2069 | }, |
| 2070 | }); |
| 2071 | }); |
| 2072 | |
| 2073 | it("should handle complete user approval and tool execution flow", async () => { |
| 2074 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 2075 | |
| 2076 | // Create store with tool settings that require manual approval |
| 2077 | const mockStoreWithApproval = createMockStore({ |
| 2078 | session: { |
| 2079 | history: [ |
| 2080 | { |
| 2081 | message: { |
| 2082 | id: "1", |
| 2083 | role: "user", |
| 2084 | content: "Please search the codebase for test functions", |
| 2085 | }, |
| 2086 | contextItems: [], |
| 2087 | }, |
| 2088 | ], |
| 2089 | hasReasoningEnabled: false, |
| 2090 | isStreaming: true, |
| 2091 | id: "session-123", |
| 2092 | mode: "chat", |
| 2093 | streamAborter: new AbortController(), |
| 2094 | contextPercentage: 0, |
| 2095 | isPruned: false, |
| 2096 | isInEdit: false, |
| 2097 | title: "", |
| 2098 | lastSessionId: undefined, |
| 2099 | isSessionMetadataLoading: false, |
| 2100 | allSessionMetadata: [], |
| 2101 | symbols: {}, |
| 2102 | codeBlockApplyStates: { |
| 2103 | states: [], |
| 2104 | curIndex: 0, |
| 2105 | }, |
| 2106 | newestToolbarPreviewForInput: {}, |
| 2107 | compactionLoading: {}, |
| 2108 | inlineErrorMessage: undefined, |
| 2109 | }, |
| 2110 | config: { |
| 2111 | config: { |
| 2112 | tools: [], |
| 2113 | rules: [], |
| 2114 | tabAutocompleteModel: undefined, |
| 2115 | selectedModelByRole: { |
| 2116 | chat: mockClaudeModel, |
| 2117 | apply: null, |
| 2118 | edit: null, |
| 2119 | summarize: null, |
| 2120 | autocomplete: null, |
| 2121 | rerank: null, |
| 2122 | embed: null, |
| 2123 | }, |
| 2124 | experimental: { |
| 2125 | onlyUseSystemMessageTools: false, |
| 2126 | }, |
| 2127 | } as any, |
| 2128 | lastSelectedModelByRole: { |
| 2129 | chat: mockClaudeModel.title, |
| 2130 | }, |
| 2131 | } as any, |
| 2132 | ui: { |
| 2133 | toolSettings: { |
| 2134 | search_codebase: "askFirst", // Requires manual approval |
| 2135 | }, |
| 2136 | ruleSettings: {}, |
| 2137 | showDialog: false, |
| 2138 | dialogMessage: undefined, |
| 2139 | } as any, |
| 2140 | }); |
| 2141 | |
| 2142 | const mockIdeMessengerApproval = mockStoreWithApproval.mockIdeMessenger; |
| 2143 | |
| 2144 | // Setup successful compilation |
| 2145 | mockIdeMessengerApproval.request.mockImplementation( |
| 2146 | (endpoint: string, data: any) => { |
| 2147 | if (endpoint === "llm/compileChat") { |
| 2148 | return Promise.resolve({ |
| 2149 | status: "success", |
| 2150 | content: { |
| 2151 | compiledChatMessages: [ |
| 2152 | { |
| 2153 | role: "user", |
| 2154 | content: "Please search the codebase for test functions", |
| 2155 | }, |
| 2156 | ], |
| 2157 | didPrune: false, |
| 2158 | contextPercentage: 0.85, |
| 2159 | }, |
| 2160 | }); |
| 2161 | } else if (endpoint === "tools/call") { |
| 2162 | // Mock server-side tool call response for search_codebase |
| 2163 | return Promise.resolve({ |
| 2164 | status: "success", |
| 2165 | content: { |
| 2166 | contextItems: [ |
| 2167 | { |
| 2168 | name: "Search Results", |
| 2169 | description: "Found test functions", |
| 2170 | content: |
| 2171 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2172 | icon: "search", |
| 2173 | hidden: false, |
| 2174 | }, |
| 2175 | ], |
| 2176 | errorMessage: undefined, |
| 2177 | }, |
| 2178 | }); |
| 2179 | } else if (endpoint === "history/save") { |
| 2180 | return Promise.resolve({ status: "success" }); |
| 2181 | } else if (endpoint === "history/list") { |
| 2182 | return Promise.resolve({ status: "success", content: [] }); |
| 2183 | } |
| 2184 | return Promise.resolve({ status: "success", content: {} }); |
| 2185 | }, |
| 2186 | ); |
| 2187 | |
| 2188 | // Setup streaming generator with tool call |
| 2189 | async function* mockStreamGeneratorWithApprovalFlow() { |
| 2190 | yield [ |
| 2191 | { |
| 2192 | role: "assistant", |
| 2193 | content: "I'll search for test functions in the codebase.", |
| 2194 | }, |
| 2195 | ]; |
| 2196 | yield [ |
| 2197 | { |
| 2198 | role: "assistant", |
| 2199 | content: "", |
| 2200 | toolCalls: [ |
| 2201 | { |
| 2202 | id: "tool-approval-flow-1", |
| 2203 | type: "function", |
| 2204 | function: { |
| 2205 | name: "search_codebase", |
| 2206 | arguments: JSON.stringify({ query: "test function" }), |
| 2207 | }, |
| 2208 | }, |
| 2209 | ], |
| 2210 | }, |
| 2211 | ]; |
| 2212 | return { |
| 2213 | prompt: "Please search the codebase for test functions", |
| 2214 | completion: "I'll search for test functions in the codebase.", |
| 2215 | modelProvider: "anthropic", |
| 2216 | }; |
| 2217 | } |
| 2218 | |
| 2219 | // Mock subsequent streaming calls (after tool execution) |
| 2220 | let streamCallCount = 0; |
| 2221 | mockIdeMessengerApproval.llmStreamChat.mockImplementation(() => { |
| 2222 | streamCallCount++; |
| 2223 | if (streamCallCount === 1) { |
| 2224 | // First call - initial streaming with tool call |
| 2225 | return mockStreamGeneratorWithApprovalFlow(); |
| 2226 | } else { |
| 2227 | // Subsequent calls from streamResponseAfterToolCall |
| 2228 | async function* followupGenerator() { |
| 2229 | yield [ |
| 2230 | { |
| 2231 | role: "assistant", |
| 2232 | content: |
| 2233 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2234 | }, |
| 2235 | ]; |
| 2236 | return { |
| 2237 | prompt: "continuing after tool execution", |
| 2238 | completion: |
| 2239 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2240 | modelProvider: "anthropic", |
| 2241 | }; |
| 2242 | } |
| 2243 | return followupGenerator(); |
| 2244 | } |
| 2245 | }); |
| 2246 | |
| 2247 | // Track isStreaming state changes throughout the entire test |
| 2248 | const streamingStateChanges: boolean[] = []; |
| 2249 | let lastStreamingState = |
| 2250 | mockStoreWithApproval.getState().session.isStreaming; |
| 2251 | const originalDispatch = mockStoreWithApproval.dispatch; |
| 2252 | mockStoreWithApproval.dispatch = ((action: any) => { |
| 2253 | const result = originalDispatch(action); |
| 2254 | const currentStreamingState = |
| 2255 | mockStoreWithApproval.getState().session.isStreaming; |
| 2256 | // Only record when the state actually changes |
| 2257 | if (currentStreamingState !== lastStreamingState) { |
| 2258 | streamingStateChanges.push(currentStreamingState); |
| 2259 | lastStreamingState = currentStreamingState; |
| 2260 | } |
| 2261 | return result; |
| 2262 | }) as any; |
| 2263 | |
| 2264 | // Execute initial thunk - this should generate the tool call but not execute it |
| 2265 | const initialResult = await mockStoreWithApproval.dispatch( |
| 2266 | streamResponseThunk({ |
| 2267 | editorState: mockEditorState, |
| 2268 | modifiers: mockModifiers, |
| 2269 | }) as any, |
| 2270 | ); |
| 2271 | |
| 2272 | // Verify initial streaming completed successfully |
| 2273 | expect(initialResult.type).toBe("chat/streamResponse/fulfilled"); |
| 2274 | |
| 2275 | // Verify exact initial action sequence by comparing action types |
| 2276 | const initialActions = (mockStoreWithApproval as any).getActions(); |
| 2277 | |
| 2278 | expect(initialActions).toEqual([ |
| 2279 | { |
| 2280 | type: "chat/streamResponse/pending", |
| 2281 | meta: { |
| 2282 | arg: { |
| 2283 | editorState: mockEditorState, |
| 2284 | modifiers: mockModifiers, |
| 2285 | }, |
| 2286 | requestId: expect.any(String), |
| 2287 | requestStatus: "pending", |
| 2288 | }, |
| 2289 | payload: undefined, |
| 2290 | }, |
| 2291 | { |
| 2292 | type: "chat/streamWrapper/pending", |
| 2293 | meta: { |
| 2294 | arg: expect.any(Function), |
| 2295 | requestId: expect.any(String), |
| 2296 | requestStatus: "pending", |
| 2297 | }, |
| 2298 | payload: undefined, |
| 2299 | }, |
| 2300 | { |
| 2301 | type: "session/submitEditorAndInitAtIndex", |
| 2302 | payload: { |
| 2303 | editorState: mockEditorState, |
| 2304 | index: 1, |
| 2305 | }, |
| 2306 | }, |
| 2307 | { |
| 2308 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2309 | payload: undefined, |
| 2310 | }, |
| 2311 | { |
| 2312 | type: "symbols/updateFromContextItems/pending", |
| 2313 | meta: { |
| 2314 | arg: [], |
| 2315 | requestId: expect.any(String), |
| 2316 | requestStatus: "pending", |
| 2317 | }, |
| 2318 | payload: undefined, |
| 2319 | }, |
| 2320 | { |
| 2321 | type: "session/updateHistoryItemAtIndex", |
| 2322 | payload: { |
| 2323 | index: 1, |
| 2324 | updates: { |
| 2325 | contextItems: [], |
| 2326 | message: { |
| 2327 | content: "Hello, please help me with this code", |
| 2328 | id: "mock-uuid-123", |
| 2329 | role: "user", |
| 2330 | }, |
| 2331 | }, |
| 2332 | }, |
| 2333 | }, |
| 2334 | { |
| 2335 | type: "chat/streamNormalInput/pending", |
| 2336 | meta: { |
| 2337 | arg: { |
| 2338 | legacySlashCommandData: undefined, |
| 2339 | }, |
| 2340 | requestId: expect.any(String), |
| 2341 | requestStatus: "pending", |
| 2342 | }, |
| 2343 | payload: undefined, |
| 2344 | }, |
| 2345 | { |
| 2346 | type: "session/setAppliedRulesAtIndex", |
| 2347 | payload: { |
| 2348 | appliedRules: [], |
| 2349 | index: 1, |
| 2350 | }, |
| 2351 | }, |
| 2352 | { |
| 2353 | type: "session/setActive", |
| 2354 | payload: undefined, |
| 2355 | }, |
| 2356 | { |
| 2357 | type: "session/setInlineErrorMessage", |
| 2358 | payload: undefined, |
| 2359 | }, |
| 2360 | { |
| 2361 | type: "session/setIsPruned", |
| 2362 | payload: false, |
| 2363 | }, |
| 2364 | { |
| 2365 | type: "session/setContextPercentage", |
| 2366 | payload: 0.85, |
| 2367 | }, |
| 2368 | { |
| 2369 | type: "symbols/updateFromContextItems/fulfilled", |
| 2370 | meta: { |
| 2371 | arg: [], |
| 2372 | requestId: expect.any(String), |
| 2373 | requestStatus: "fulfilled", |
| 2374 | }, |
| 2375 | payload: undefined, |
| 2376 | }, |
| 2377 | { |
| 2378 | type: "session/streamUpdate", |
| 2379 | payload: [ |
| 2380 | { |
| 2381 | role: "assistant", |
| 2382 | content: "I'll search for test functions in the codebase.", |
| 2383 | }, |
| 2384 | ], |
| 2385 | }, |
| 2386 | { |
| 2387 | type: "session/streamUpdate", |
| 2388 | payload: [ |
| 2389 | { |
| 2390 | role: "assistant", |
| 2391 | content: "", |
| 2392 | toolCalls: [ |
| 2393 | { |
| 2394 | id: "tool-approval-flow-1", |
| 2395 | type: "function", |
| 2396 | function: { |
| 2397 | name: "search_codebase", |
| 2398 | arguments: JSON.stringify({ query: "test function" }), |
| 2399 | }, |
| 2400 | }, |
| 2401 | ], |
| 2402 | }, |
| 2403 | ], |
| 2404 | }, |
| 2405 | { |
| 2406 | type: "session/addPromptCompletionPair", |
| 2407 | payload: [ |
| 2408 | { |
| 2409 | completion: "I'll search for test functions in the codebase.", |
| 2410 | modelProvider: "anthropic", |
| 2411 | prompt: "Please search the codebase for test functions", |
| 2412 | }, |
| 2413 | ], |
| 2414 | }, |
| 2415 | { |
| 2416 | type: "session/setToolGenerated", |
| 2417 | payload: { |
| 2418 | toolCallId: "tool-approval-flow-1", |
| 2419 | tools: [], |
| 2420 | }, |
| 2421 | }, |
| 2422 | { |
| 2423 | type: "session/setInactive", |
| 2424 | payload: undefined, |
| 2425 | }, |
| 2426 | { |
| 2427 | type: "chat/streamNormalInput/fulfilled", |
| 2428 | meta: { |
| 2429 | arg: { |
| 2430 | legacySlashCommandData: undefined, |
| 2431 | }, |
| 2432 | requestId: expect.any(String), |
| 2433 | requestStatus: "fulfilled", |
| 2434 | }, |
| 2435 | payload: undefined, |
| 2436 | }, |
| 2437 | { |
| 2438 | type: "session/saveCurrent/pending", |
| 2439 | meta: { |
| 2440 | arg: { |
| 2441 | generateTitle: true, |
| 2442 | openNewSession: false, |
| 2443 | }, |
| 2444 | requestId: expect.any(String), |
| 2445 | requestStatus: "pending", |
| 2446 | }, |
| 2447 | payload: undefined, |
| 2448 | }, |
| 2449 | { |
| 2450 | type: "session/update/pending", |
| 2451 | meta: { |
| 2452 | arg: expect.objectContaining({ |
| 2453 | history: expect.any(Array), |
| 2454 | sessionId: "session-123", |
| 2455 | title: "New Session", |
| 2456 | workspaceDirectory: "", |
| 2457 | }), |
| 2458 | requestId: expect.any(String), |
| 2459 | requestStatus: "pending", |
| 2460 | }, |
| 2461 | payload: undefined, |
| 2462 | }, |
| 2463 | { |
| 2464 | type: "session/updateSessionMetadata", |
| 2465 | payload: { |
| 2466 | sessionId: "session-123", |
| 2467 | title: "New Session", |
| 2468 | }, |
| 2469 | }, |
| 2470 | { |
| 2471 | type: "session/refreshMetadata/pending", |
| 2472 | meta: { |
| 2473 | arg: {}, |
| 2474 | requestId: expect.any(String), |
| 2475 | requestStatus: "pending", |
| 2476 | }, |
| 2477 | payload: undefined, |
| 2478 | }, |
| 2479 | { |
| 2480 | type: "session/setIsSessionMetadataLoading", |
| 2481 | payload: false, |
| 2482 | }, |
| 2483 | { |
| 2484 | type: "session/setAllSessionMetadata", |
| 2485 | payload: [], |
| 2486 | }, |
| 2487 | { |
| 2488 | type: "session/refreshMetadata/fulfilled", |
| 2489 | meta: { |
| 2490 | arg: {}, |
| 2491 | requestId: expect.any(String), |
| 2492 | requestStatus: "fulfilled", |
| 2493 | }, |
| 2494 | payload: [], |
| 2495 | }, |
| 2496 | { |
| 2497 | type: "session/update/fulfilled", |
| 2498 | meta: { |
| 2499 | arg: expect.objectContaining({ |
| 2500 | history: expect.any(Array), |
| 2501 | sessionId: "session-123", |
| 2502 | title: "New Session", |
| 2503 | workspaceDirectory: "", |
| 2504 | }), |
| 2505 | requestId: expect.any(String), |
| 2506 | requestStatus: "fulfilled", |
| 2507 | }, |
| 2508 | payload: undefined, |
| 2509 | }, |
| 2510 | { |
| 2511 | type: "session/saveCurrent/fulfilled", |
| 2512 | meta: { |
| 2513 | arg: { |
| 2514 | generateTitle: true, |
| 2515 | openNewSession: false, |
| 2516 | }, |
| 2517 | requestId: expect.any(String), |
| 2518 | requestStatus: "fulfilled", |
| 2519 | }, |
| 2520 | payload: undefined, |
| 2521 | }, |
| 2522 | { |
| 2523 | type: "chat/streamWrapper/fulfilled", |
| 2524 | meta: { |
| 2525 | arg: expect.any(Function), |
| 2526 | requestId: expect.any(String), |
| 2527 | requestStatus: "fulfilled", |
| 2528 | }, |
| 2529 | payload: undefined, |
| 2530 | }, |
| 2531 | { |
| 2532 | type: "chat/streamResponse/fulfilled", |
| 2533 | meta: { |
| 2534 | arg: { |
| 2535 | editorState: mockEditorState, |
| 2536 | modifiers: mockModifiers, |
| 2537 | }, |
| 2538 | requestId: expect.any(String), |
| 2539 | requestStatus: "fulfilled", |
| 2540 | }, |
| 2541 | payload: undefined, |
| 2542 | }, |
| 2543 | ]); |
| 2544 | |
| 2545 | // Clear the actions array to track only the approval flow |
| 2546 | (mockStoreWithApproval as any).clearActions(); |
| 2547 | |
| 2548 | // Import the callToolById thunk to simulate user approval |
| 2549 | const { callToolById } = await import("./callToolById"); |
| 2550 | |
| 2551 | // Simulate user clicking "Accept" on the tool call |
| 2552 | const approvalResult = await mockStoreWithApproval.dispatch( |
| 2553 | callToolById({ toolCallId: "tool-approval-flow-1" }) as any, |
| 2554 | ); |
| 2555 | |
| 2556 | // Verify tool execution completed successfully |
| 2557 | expect(approvalResult.type).toBe("chat/callTool/fulfilled"); |
| 2558 | |
| 2559 | // Verify exact approval flow actions |
| 2560 | const approvalActions = (mockStoreWithApproval as any).getActions(); |
| 2561 | expect(approvalActions).toEqual([ |
| 2562 | { |
| 2563 | type: "chat/callTool/pending", |
| 2564 | meta: { |
| 2565 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2566 | requestId: expect.any(String), |
| 2567 | requestStatus: "pending", |
| 2568 | }, |
| 2569 | payload: undefined, |
| 2570 | }, |
| 2571 | { |
| 2572 | type: "session/setToolCallCalling", |
| 2573 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2574 | }, |
| 2575 | { |
| 2576 | type: "session/updateToolCallOutput", |
| 2577 | payload: { |
| 2578 | toolCallId: "tool-approval-flow-1", |
| 2579 | contextItems: [ |
| 2580 | { |
| 2581 | name: "Search Results", |
| 2582 | description: "Found test functions", |
| 2583 | content: |
| 2584 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2585 | icon: "search", |
| 2586 | hidden: false, |
| 2587 | }, |
| 2588 | ], |
| 2589 | }, |
| 2590 | }, |
| 2591 | { |
| 2592 | type: "session/acceptToolCall", |
| 2593 | payload: { toolCallId: "tool-approval-flow-1" }, |
| 2594 | }, |
| 2595 | { |
| 2596 | type: "chat/streamAfterToolCall/pending", |
| 2597 | meta: { |
| 2598 | arg: { |
| 2599 | toolCallId: "tool-approval-flow-1", |
| 2600 | }, |
| 2601 | requestId: expect.any(String), |
| 2602 | requestStatus: "pending", |
| 2603 | }, |
| 2604 | payload: undefined, |
| 2605 | }, |
| 2606 | { |
| 2607 | type: "chat/streamWrapper/pending", |
| 2608 | meta: { |
| 2609 | arg: expect.any(Function), |
| 2610 | requestId: expect.any(String), |
| 2611 | requestStatus: "pending", |
| 2612 | }, |
| 2613 | payload: undefined, |
| 2614 | }, |
| 2615 | { |
| 2616 | type: "session/resetNextCodeBlockToApplyIndex", |
| 2617 | payload: undefined, |
| 2618 | }, |
| 2619 | { |
| 2620 | type: "session/streamUpdate", |
| 2621 | payload: [ |
| 2622 | { |
| 2623 | content: |
| 2624 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2625 | role: "tool", |
| 2626 | toolCallId: "tool-approval-flow-1", |
| 2627 | }, |
| 2628 | ], |
| 2629 | }, |
| 2630 | { |
| 2631 | type: "chat/streamNormalInput/pending", |
| 2632 | meta: { |
| 2633 | arg: {}, |
| 2634 | requestId: expect.any(String), |
| 2635 | requestStatus: "pending", |
| 2636 | }, |
| 2637 | payload: undefined, |
| 2638 | }, |
| 2639 | { |
| 2640 | type: "session/setAppliedRulesAtIndex", |
| 2641 | payload: { |
| 2642 | appliedRules: [], |
| 2643 | index: 1, |
| 2644 | }, |
| 2645 | }, |
| 2646 | { |
| 2647 | type: "session/setActive", |
| 2648 | payload: undefined, |
| 2649 | }, |
| 2650 | { |
| 2651 | type: "session/setInlineErrorMessage", |
| 2652 | payload: undefined, |
| 2653 | }, |
| 2654 | { |
| 2655 | type: "session/setIsPruned", |
| 2656 | payload: false, |
| 2657 | }, |
| 2658 | { |
| 2659 | type: "session/setContextPercentage", |
| 2660 | payload: 0.85, |
| 2661 | }, |
| 2662 | { |
| 2663 | type: "session/streamUpdate", |
| 2664 | payload: [ |
| 2665 | { |
| 2666 | role: "assistant", |
| 2667 | content: |
| 2668 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2669 | }, |
| 2670 | ], |
| 2671 | }, |
| 2672 | { |
| 2673 | type: "session/addPromptCompletionPair", |
| 2674 | payload: [ |
| 2675 | { |
| 2676 | completion: |
| 2677 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2678 | modelProvider: "anthropic", |
| 2679 | prompt: "continuing after tool execution", |
| 2680 | }, |
| 2681 | ], |
| 2682 | }, |
| 2683 | { |
| 2684 | type: "session/setInactive", |
| 2685 | payload: undefined, |
| 2686 | }, |
| 2687 | { |
| 2688 | type: "chat/streamNormalInput/fulfilled", |
| 2689 | meta: { |
| 2690 | arg: {}, |
| 2691 | requestId: expect.any(String), |
| 2692 | requestStatus: "fulfilled", |
| 2693 | }, |
| 2694 | payload: undefined, |
| 2695 | }, |
| 2696 | { |
| 2697 | type: "session/saveCurrent/pending", |
| 2698 | meta: { |
| 2699 | arg: { |
| 2700 | generateTitle: true, |
| 2701 | openNewSession: false, |
| 2702 | }, |
| 2703 | requestId: expect.any(String), |
| 2704 | requestStatus: "pending", |
| 2705 | }, |
| 2706 | payload: undefined, |
| 2707 | }, |
| 2708 | { |
| 2709 | type: "session/update/pending", |
| 2710 | meta: { |
| 2711 | arg: expect.objectContaining({ |
| 2712 | history: expect.any(Array), |
| 2713 | sessionId: "session-123", |
| 2714 | title: "New Session", |
| 2715 | workspaceDirectory: "", |
| 2716 | }), |
| 2717 | requestId: expect.any(String), |
| 2718 | requestStatus: "pending", |
| 2719 | }, |
| 2720 | payload: undefined, |
| 2721 | }, |
| 2722 | { |
| 2723 | type: "session/updateSessionMetadata", |
| 2724 | payload: { |
| 2725 | sessionId: "session-123", |
| 2726 | title: "New Session", |
| 2727 | }, |
| 2728 | }, |
| 2729 | { |
| 2730 | type: "session/refreshMetadata/pending", |
| 2731 | meta: { |
| 2732 | arg: {}, |
| 2733 | requestId: expect.any(String), |
| 2734 | requestStatus: "pending", |
| 2735 | }, |
| 2736 | payload: undefined, |
| 2737 | }, |
| 2738 | { |
| 2739 | type: "session/setIsSessionMetadataLoading", |
| 2740 | payload: false, |
| 2741 | }, |
| 2742 | { |
| 2743 | type: "session/setAllSessionMetadata", |
| 2744 | payload: [], |
| 2745 | }, |
| 2746 | { |
| 2747 | type: "session/refreshMetadata/fulfilled", |
| 2748 | meta: { |
| 2749 | arg: {}, |
| 2750 | requestId: expect.any(String), |
| 2751 | requestStatus: "fulfilled", |
| 2752 | }, |
| 2753 | payload: [], |
| 2754 | }, |
| 2755 | { |
| 2756 | type: "session/update/fulfilled", |
| 2757 | meta: { |
| 2758 | arg: expect.objectContaining({ |
| 2759 | history: expect.any(Array), |
| 2760 | sessionId: "session-123", |
| 2761 | title: "New Session", |
| 2762 | workspaceDirectory: "", |
| 2763 | }), |
| 2764 | requestId: expect.any(String), |
| 2765 | requestStatus: "fulfilled", |
| 2766 | }, |
| 2767 | payload: undefined, |
| 2768 | }, |
| 2769 | { |
| 2770 | type: "session/saveCurrent/fulfilled", |
| 2771 | meta: { |
| 2772 | arg: { |
| 2773 | generateTitle: true, |
| 2774 | openNewSession: false, |
| 2775 | }, |
| 2776 | requestId: expect.any(String), |
| 2777 | requestStatus: "fulfilled", |
| 2778 | }, |
| 2779 | payload: undefined, |
| 2780 | }, |
| 2781 | { |
| 2782 | type: "chat/streamWrapper/fulfilled", |
| 2783 | meta: { |
| 2784 | arg: expect.any(Function), |
| 2785 | requestId: expect.any(String), |
| 2786 | requestStatus: "fulfilled", |
| 2787 | }, |
| 2788 | payload: undefined, |
| 2789 | }, |
| 2790 | { |
| 2791 | type: "chat/streamAfterToolCall/fulfilled", |
| 2792 | meta: { |
| 2793 | arg: { |
| 2794 | toolCallId: "tool-approval-flow-1", |
| 2795 | }, |
| 2796 | requestId: expect.any(String), |
| 2797 | requestStatus: "fulfilled", |
| 2798 | }, |
| 2799 | payload: undefined, |
| 2800 | }, |
| 2801 | { |
| 2802 | type: "chat/callTool/fulfilled", |
| 2803 | meta: { |
| 2804 | arg: { toolCallId: "tool-approval-flow-1" }, |
| 2805 | requestId: expect.any(String), |
| 2806 | requestStatus: "fulfilled", |
| 2807 | }, |
| 2808 | payload: undefined, |
| 2809 | }, |
| 2810 | ]); |
| 2811 | |
| 2812 | // Verify telemetry events for manual approval flow |
| 2813 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_decision", { |
| 2814 | decision: "accept", |
| 2815 | toolName: "search_codebase", |
| 2816 | toolCallId: "tool-approval-flow-1", |
| 2817 | }); |
| 2818 | |
| 2819 | expect(mockPosthog.capture).toHaveBeenCalledWith("gui_tool_call_outcome", { |
| 2820 | succeeded: true, |
| 2821 | toolName: "search_codebase", |
| 2822 | errorMessage: undefined, |
| 2823 | duration_ms: expect.any(Number), |
| 2824 | }); |
| 2825 | |
| 2826 | // Verify IDE messenger calls for tool execution |
| 2827 | expect(mockIdeMessengerApproval.request).toHaveBeenCalledWith( |
| 2828 | "tools/call", |
| 2829 | { |
| 2830 | toolCall: { |
| 2831 | id: "tool-approval-flow-1", |
| 2832 | type: "function", |
| 2833 | function: { |
| 2834 | name: "search_codebase", |
| 2835 | arguments: JSON.stringify({ query: "test function" }), |
| 2836 | }, |
| 2837 | }, |
| 2838 | }, |
| 2839 | ); |
| 2840 | |
| 2841 | // Verify second streaming call was made for continuation |
| 2842 | expect(streamCallCount).toBe(2); |
| 2843 | |
| 2844 | // Verify final state shows completed tool call and follow-up response |
| 2845 | const finalState = mockStoreWithApproval.getState(); |
| 2846 | expect(finalState).toEqual({ |
| 2847 | session: { |
| 2848 | history: [ |
| 2849 | { |
| 2850 | contextItems: [], |
| 2851 | message: { |
| 2852 | id: "1", |
| 2853 | role: "user", |
| 2854 | content: "Please search the codebase for test functions", |
| 2855 | }, |
| 2856 | }, |
| 2857 | { |
| 2858 | appliedRules: [], |
| 2859 | contextItems: [], |
| 2860 | editorState: mockEditorState, |
| 2861 | message: { |
| 2862 | content: "Hello, please help me with this code", |
| 2863 | id: expect.any(String), |
| 2864 | role: "user", |
| 2865 | }, |
| 2866 | }, |
| 2867 | { |
| 2868 | contextItems: [], |
| 2869 | isGatheringContext: false, |
| 2870 | message: { |
| 2871 | content: "I'll search for test functions in the codebase.", |
| 2872 | id: expect.any(String), |
| 2873 | role: "assistant", |
| 2874 | toolCalls: [ |
| 2875 | { |
| 2876 | id: "tool-approval-flow-1", |
| 2877 | type: "function", |
| 2878 | function: { |
| 2879 | name: "search_codebase", |
| 2880 | arguments: JSON.stringify({ query: "test function" }), |
| 2881 | }, |
| 2882 | }, |
| 2883 | ], |
| 2884 | }, |
| 2885 | promptLogs: [ |
| 2886 | { |
| 2887 | completion: "I'll search for test functions in the codebase.", |
| 2888 | modelProvider: "anthropic", |
| 2889 | prompt: "Please search the codebase for test functions", |
| 2890 | }, |
| 2891 | ], |
| 2892 | toolCallStates: [ |
| 2893 | { |
| 2894 | toolCallId: "tool-approval-flow-1", |
| 2895 | toolCall: { |
| 2896 | id: "tool-approval-flow-1", |
| 2897 | type: "function", |
| 2898 | function: { |
| 2899 | name: "search_codebase", |
| 2900 | arguments: JSON.stringify({ query: "test function" }), |
| 2901 | }, |
| 2902 | }, |
| 2903 | parsedArgs: { query: "test function" }, |
| 2904 | status: "done", // Tool call completed successfully |
| 2905 | output: [ |
| 2906 | { |
| 2907 | name: "Search Results", |
| 2908 | description: "Found test functions", |
| 2909 | content: |
| 2910 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2911 | icon: "search", |
| 2912 | hidden: false, |
| 2913 | }, |
| 2914 | ], |
| 2915 | }, |
| 2916 | ], |
| 2917 | }, |
| 2918 | { |
| 2919 | contextItems: [], |
| 2920 | message: { |
| 2921 | content: |
| 2922 | "function testUserLogin() {...}\\nfunction testDataValidation() {...}", |
| 2923 | id: expect.any(String), |
| 2924 | role: "tool", |
| 2925 | toolCallId: "tool-approval-flow-1", |
| 2926 | }, |
| 2927 | }, |
| 2928 | { |
| 2929 | contextItems: [], |
| 2930 | isGatheringContext: false, |
| 2931 | message: { |
| 2932 | content: |
| 2933 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2934 | id: "mock-uuid-123", |
| 2935 | role: "assistant", |
| 2936 | }, |
| 2937 | promptLogs: [ |
| 2938 | { |
| 2939 | completion: |
| 2940 | "I found several test functions in your codebase. Here are the main ones I discovered...", |
| 2941 | modelProvider: "anthropic", |
| 2942 | prompt: "continuing after tool execution", |
| 2943 | }, |
| 2944 | ], |
| 2945 | }, |
| 2946 | ], |
| 2947 | hasReasoningEnabled: false, |
| 2948 | isStreaming: false, // Inactive after complete flow |
| 2949 | id: "session-123", |
| 2950 | mode: "chat", |
| 2951 | streamAborter: expect.any(AbortController), |
| 2952 | contextPercentage: 0.85, |
| 2953 | isPruned: false, |
| 2954 | isInEdit: false, |
| 2955 | title: "New Session", |
| 2956 | lastSessionId: undefined, |
| 2957 | isSessionMetadataLoading: false, |
| 2958 | allSessionMetadata: [], |
| 2959 | symbols: {}, |
| 2960 | codeBlockApplyStates: { |
| 2961 | states: [], |
| 2962 | curIndex: 0, |
| 2963 | }, |
| 2964 | newestToolbarPreviewForInput: {}, |
| 2965 | compactionLoading: {}, |
| 2966 | inlineErrorMessage: undefined, |
| 2967 | }, |
| 2968 | config: { |
| 2969 | config: { |
| 2970 | tools: [], |
| 2971 | rules: [], |
| 2972 | tabAutocompleteModel: undefined, |
| 2973 | selectedModelByRole: { |
| 2974 | chat: mockClaudeModel, |
| 2975 | apply: null, |
| 2976 | edit: null, |
| 2977 | summarize: null, |
| 2978 | autocomplete: null, |
| 2979 | rerank: null, |
| 2980 | embed: null, |
| 2981 | }, |
| 2982 | experimental: { |
| 2983 | onlyUseSystemMessageTools: false, |
| 2984 | }, |
| 2985 | }, |
| 2986 | lastSelectedModelByRole: { |
| 2987 | chat: mockClaudeModel.title, |
| 2988 | }, |
| 2989 | loading: false, |
| 2990 | configError: undefined, |
| 2991 | }, |
| 2992 | ui: { |
| 2993 | toolSettings: { |
| 2994 | search_codebase: "askFirst", |
| 2995 | }, |
| 2996 | ruleSettings: {}, |
| 2997 | showDialog: false, |
| 2998 | dialogMessage: undefined, |
| 2999 | }, |
| 3000 | editModeState: { |
| 3001 | isInEdit: false, |
| 3002 | returnToMode: "chat", |
| 3003 | }, |
| 3004 | indexing: { |
| 3005 | indexing: { |
| 3006 | statuses: {}, |
| 3007 | hiddenChatPeekTypes: { |
| 3008 | docs: false, |
| 3009 | }, |
| 3010 | }, |
| 3011 | }, |
| 3012 | tabs: { |
| 3013 | tabsItems: [], |
| 3014 | }, |
| 3015 | profiles: { |
| 3016 | organizations: [], |
| 3017 | selectedProfileId: null, |
| 3018 | selectedOrganizationId: null, |
| 3019 | preferencesByProfileId: {}, |
| 3020 | }, |
| 3021 | }); |
| 3022 | }); |
| 3023 | |
| 3024 | describe("dynamic policy evaluation", () => { |
| 3025 | it("should call tools/evaluatePolicy with correct parameters", async () => { |
| 3026 | const mockStore = createMockStore({ |
| 3027 | session: { |
| 3028 | history: [ |
| 3029 | { |
| 3030 | message: { id: "1", role: "user", content: "Run echo hello" }, |
| 3031 | contextItems: [], |
| 3032 | }, |
| 3033 | ], |
| 3034 | hasReasoningEnabled: false, |
| 3035 | isStreaming: true, |
| 3036 | id: "session-123", |
| 3037 | mode: "chat", |
| 3038 | streamAborter: new AbortController(), |
| 3039 | contextPercentage: 0, |
| 3040 | isPruned: false, |
| 3041 | isInEdit: false, |
| 3042 | title: "", |
| 3043 | lastSessionId: undefined, |
| 3044 | isSessionMetadataLoading: false, |
| 3045 | allSessionMetadata: [], |
| 3046 | symbols: {}, |
| 3047 | codeBlockApplyStates: { |
| 3048 | states: [], |
| 3049 | curIndex: 0, |
| 3050 | }, |
| 3051 | newestToolbarPreviewForInput: {}, |
| 3052 | compactionLoading: {}, |
| 3053 | inlineErrorMessage: undefined, |
| 3054 | }, |
| 3055 | config: { |
| 3056 | config: { |
| 3057 | tools: [], |
| 3058 | rules: [], |
| 3059 | tabAutocompleteModel: undefined, |
| 3060 | selectedModelByRole: { |
| 3061 | chat: mockClaudeModel, |
| 3062 | apply: null, |
| 3063 | edit: null, |
| 3064 | summarize: null, |
| 3065 | autocomplete: null, |
| 3066 | rerank: null, |
| 3067 | embed: null, |
| 3068 | }, |
| 3069 | experimental: { |
| 3070 | onlyUseSystemMessageTools: false, |
| 3071 | }, |
| 3072 | } as any, |
| 3073 | lastSelectedModelByRole: { |
| 3074 | chat: mockClaudeModel.title, |
| 3075 | }, |
| 3076 | } as any, |
| 3077 | ui: { |
| 3078 | toolSettings: { |
| 3079 | run_terminal_command: "allowedWithoutPermission", |
| 3080 | }, |
| 3081 | ruleSettings: {}, |
| 3082 | showDialog: false, |
| 3083 | dialogMessage: undefined, |
| 3084 | } as any, |
| 3085 | } as any); |
| 3086 | |
| 3087 | const mockIdeMessenger = mockStore.mockIdeMessenger; |
| 3088 | |
| 3089 | // Simple mock - just return a different policy for testing |
| 3090 | mockIdeMessenger.request.mockImplementation( |
| 3091 | (endpoint: string, data: any) => { |
| 3092 | if (endpoint === "tools/evaluatePolicy") { |
| 3093 | // For this test, simulate that echo commands require permission |
| 3094 | if (data.args.command?.toLowerCase().startsWith("echo")) { |
| 3095 | return Promise.resolve({ |
| 3096 | status: "success", |
| 3097 | content: { policy: "allowedWithPermission" }, |
| 3098 | }); |
| 3099 | } |
| 3100 | return Promise.resolve({ |
| 3101 | status: "success", |
| 3102 | content: { policy: data.basePolicy }, |
| 3103 | }); |
| 3104 | } else if (endpoint === "llm/compileChat") { |
| 3105 | return Promise.resolve({ |
| 3106 | status: "success", |
| 3107 | content: { |
| 3108 | compiledChatMessages: [ |
| 3109 | { role: "user", content: "Run echo hello" }, |
| 3110 | ], |
| 3111 | didPrune: false, |
| 3112 | contextPercentage: 0.5, |
| 3113 | }, |
| 3114 | }); |
| 3115 | } else if (endpoint === "history/save") { |
| 3116 | return Promise.resolve({ status: "success" }); |
| 3117 | } else if (endpoint === "history/list") { |
| 3118 | return Promise.resolve({ status: "success", content: [] }); |
| 3119 | } |
| 3120 | return Promise.resolve({ status: "success", content: {} }); |
| 3121 | }, |
| 3122 | ); |
| 3123 | |
| 3124 | // Setup streaming with echo command (should require permission despite auto-approve base) |
| 3125 | async function* mockStreamWithEcho() { |
| 3126 | yield [{ role: "assistant", content: "I'll run the echo command." }]; |
| 3127 | yield [ |
| 3128 | { |
| 3129 | role: "assistant", |
| 3130 | content: "", |
| 3131 | toolCalls: [ |
| 3132 | { |
| 3133 | id: "tool-echo-1", |
| 3134 | type: "function", |
| 3135 | function: { |
| 3136 | name: "run_terminal_command", |
| 3137 | arguments: JSON.stringify({ command: "echo hello" }), |
| 3138 | }, |
| 3139 | }, |
| 3140 | ], |
| 3141 | }, |
| 3142 | ]; |
| 3143 | return { |
| 3144 | prompt: "Run echo hello", |
| 3145 | completion: "I'll run the echo command.", |
| 3146 | modelProvider: "anthropic", |
| 3147 | }; |
| 3148 | } |
| 3149 | |
| 3150 | mockIdeMessenger.llmStreamChat.mockReturnValue(mockStreamWithEcho()); |
| 3151 | |
| 3152 | // Execute thunk |
| 3153 | await mockStore.dispatch( |
| 3154 | streamResponseThunk({ |
| 3155 | editorState: mockEditorState, |
| 3156 | modifiers: mockModifiers, |
| 3157 | }) as any, |
| 3158 | ); |
| 3159 | |
| 3160 | // Just verify the call was made with correct params |
| 3161 | expect(mockIdeMessenger.request).toHaveBeenCalledWith( |
| 3162 | "tools/evaluatePolicy", |
| 3163 | expect.objectContaining({ |
| 3164 | toolName: "run_terminal_command", |
| 3165 | basePolicy: "allowedWithoutPermission", |
| 3166 | args: { command: "echo hello" }, |
| 3167 | }), |
| 3168 | ); |
| 3169 | |
| 3170 | // Verify tool wasn't auto-executed (policy changed to require permission) |
| 3171 | expect(mockIdeMessenger.request).not.toHaveBeenCalledWith( |
| 3172 | "tools/call", |
| 3173 | expect.any(Object), |
| 3174 | ); |
| 3175 | }); |
| 3176 | |
| 3177 | it("should respect disabled policy", async () => { |
| 3178 | const mockStore = createMockStore({ |
| 3179 | session: { |
| 3180 | history: [ |
| 3181 | { |
| 3182 | message: { id: "1", role: "user", content: "Run ls" }, |
| 3183 | contextItems: [], |
| 3184 | }, |
| 3185 | ], |
| 3186 | hasReasoningEnabled: false, |
| 3187 | isStreaming: true, |
| 3188 | id: "session-123", |
| 3189 | mode: "chat", |
| 3190 | streamAborter: new AbortController(), |
| 3191 | contextPercentage: 0, |
| 3192 | isPruned: false, |
| 3193 | isInEdit: false, |
| 3194 | title: "", |
| 3195 | lastSessionId: undefined, |
| 3196 | isSessionMetadataLoading: false, |
| 3197 | allSessionMetadata: [], |
| 3198 | symbols: {}, |
| 3199 | codeBlockApplyStates: { |
| 3200 | states: [], |
| 3201 | curIndex: 0, |
| 3202 | }, |
| 3203 | newestToolbarPreviewForInput: {}, |
| 3204 | compactionLoading: {}, |
| 3205 | inlineErrorMessage: undefined, |
| 3206 | }, |
| 3207 | config: { |
| 3208 | config: { |
| 3209 | tools: [], |
| 3210 | rules: [], |
| 3211 | tabAutocompleteModel: undefined, |
| 3212 | selectedModelByRole: { |
| 3213 | chat: mockClaudeModel, |
| 3214 | apply: null, |
| 3215 | edit: null, |
| 3216 | summarize: null, |
| 3217 | autocomplete: null, |
| 3218 | rerank: null, |
| 3219 | embed: null, |
| 3220 | }, |
| 3221 | experimental: { |
| 3222 | onlyUseSystemMessageTools: false, |
| 3223 | }, |
| 3224 | } as any, |
| 3225 | lastSelectedModelByRole: { |
| 3226 | chat: mockClaudeModel.title, |
| 3227 | }, |
| 3228 | } as any, |
| 3229 | ui: { |
| 3230 | toolSettings: { |
| 3231 | run_terminal_command: "disabled", // Tool is disabled |
| 3232 | }, |
| 3233 | ruleSettings: {}, |
| 3234 | showDialog: false, |
| 3235 | dialogMessage: undefined, |
| 3236 | } as any, |
| 3237 | } as any); |
| 3238 | |
| 3239 | const mockIdeMessenger = mockStore.mockIdeMessenger; |
| 3240 | |
| 3241 | // Simple mock - just return disabled policy |
| 3242 | mockIdeMessenger.request.mockImplementation( |
| 3243 | (endpoint: string, data: any) => { |
| 3244 | if (endpoint === "tools/evaluatePolicy") { |
| 3245 | // Backend should respect disabled state |
| 3246 | return Promise.resolve({ |
| 3247 | status: "success", |
| 3248 | content: { policy: "disabled" }, |
| 3249 | }); |
| 3250 | } else if (endpoint === "llm/compileChat") { |
| 3251 | return Promise.resolve({ |
| 3252 | status: "success", |
| 3253 | content: { |
| 3254 | compiledChatMessages: [{ role: "user", content: "Run ls" }], |
| 3255 | didPrune: false, |
| 3256 | contextPercentage: 0.5, |
| 3257 | }, |
| 3258 | }); |
| 3259 | } else if (endpoint === "history/save") { |
| 3260 | return Promise.resolve({ status: "success" }); |
| 3261 | } else if (endpoint === "history/list") { |
| 3262 | return Promise.resolve({ status: "success", content: [] }); |
| 3263 | } |
| 3264 | return Promise.resolve({ status: "success", content: {} }); |
| 3265 | }, |
| 3266 | ); |
| 3267 | |
| 3268 | // Setup streaming with regular command |
| 3269 | async function* mockStreamWithLs() { |
| 3270 | yield [{ role: "assistant", content: "I'll list the files." }]; |
| 3271 | yield [ |
| 3272 | { |
| 3273 | role: "assistant", |
| 3274 | content: "", |
| 3275 | toolCalls: [ |
| 3276 | { |
| 3277 | id: "tool-ls-1", |
| 3278 | type: "function", |
| 3279 | function: { |
| 3280 | name: "run_terminal_command", |
| 3281 | arguments: JSON.stringify({ command: "ls" }), |
| 3282 | }, |
| 3283 | }, |
| 3284 | ], |
| 3285 | }, |
| 3286 | ]; |
| 3287 | return { |
| 3288 | prompt: "Run ls", |
| 3289 | completion: "I'll list the files.", |
| 3290 | modelProvider: "anthropic", |
| 3291 | }; |
| 3292 | } |
| 3293 | |
| 3294 | mockIdeMessenger.llmStreamChat.mockReturnValue(mockStreamWithLs()); |
| 3295 | |
| 3296 | // Execute thunk |
| 3297 | await mockStore.dispatch( |
| 3298 | streamResponseThunk({ |
| 3299 | editorState: mockEditorState, |
| 3300 | modifiers: mockModifiers, |
| 3301 | }) as any, |
| 3302 | ); |
| 3303 | |
| 3304 | // Verify tools/evaluatePolicy was called |
| 3305 | expect(mockIdeMessenger.request).toHaveBeenCalledWith( |
| 3306 | "tools/evaluatePolicy", |
| 3307 | expect.objectContaining({ |
| 3308 | toolName: "run_terminal_command", |
| 3309 | basePolicy: "disabled", |
| 3310 | args: { command: "ls" }, |
| 3311 | }), |
| 3312 | ); |
| 3313 | |
| 3314 | // Tool should NOT be executed since it's disabled |
| 3315 | expect(mockIdeMessenger.request).not.toHaveBeenCalledWith( |
| 3316 | "tools/call", |
| 3317 | expect.any(Object), |
| 3318 | ); |
| 3319 | }); |
| 3320 | |
| 3321 | it("should handle evaluation errors gracefully", async () => { |
| 3322 | const mockStore = createMockStore({ |
| 3323 | session: { |
| 3324 | history: [ |
| 3325 | { |
| 3326 | message: { id: "1", role: "user", content: "Do something" }, |
| 3327 | contextItems: [], |
| 3328 | }, |
| 3329 | ], |
| 3330 | hasReasoningEnabled: false, |
| 3331 | isStreaming: true, |
| 3332 | id: "session-123", |
| 3333 | mode: "chat", |
| 3334 | streamAborter: new AbortController(), |
| 3335 | contextPercentage: 0, |
| 3336 | isPruned: false, |
| 3337 | isInEdit: false, |
| 3338 | title: "", |
| 3339 | lastSessionId: undefined, |
| 3340 | isSessionMetadataLoading: false, |
| 3341 | allSessionMetadata: [], |
| 3342 | symbols: {}, |
| 3343 | codeBlockApplyStates: { |
| 3344 | states: [], |
| 3345 | curIndex: 0, |
| 3346 | }, |
| 3347 | newestToolbarPreviewForInput: {}, |
| 3348 | compactionLoading: {}, |
| 3349 | inlineErrorMessage: undefined, |
| 3350 | }, |
| 3351 | config: { |
| 3352 | config: { |
| 3353 | tools: [], |
| 3354 | rules: [], |
| 3355 | tabAutocompleteModel: undefined, |
| 3356 | selectedModelByRole: { |
| 3357 | chat: mockClaudeModel, |
| 3358 | apply: null, |
| 3359 | edit: null, |
| 3360 | summarize: null, |
| 3361 | autocomplete: null, |
| 3362 | rerank: null, |
| 3363 | embed: null, |
| 3364 | }, |
| 3365 | experimental: { |
| 3366 | onlyUseSystemMessageTools: false, |
| 3367 | }, |
| 3368 | } as any, |
| 3369 | lastSelectedModelByRole: { |
| 3370 | chat: mockClaudeModel.title, |
| 3371 | }, |
| 3372 | } as any, |
| 3373 | ui: { |
| 3374 | toolSettings: { |
| 3375 | some_tool: "allowedWithoutPermission", |
| 3376 | }, |
| 3377 | ruleSettings: {}, |
| 3378 | showDialog: false, |
| 3379 | dialogMessage: undefined, |
| 3380 | } as any, |
| 3381 | } as any); |
| 3382 | |
| 3383 | const mockIdeMessenger = mockStore.mockIdeMessenger; |
| 3384 | |
| 3385 | // Mock evaluation failure |
| 3386 | mockIdeMessenger.request.mockImplementation((endpoint: string) => { |
| 3387 | if (endpoint === "tools/evaluatePolicy") { |
| 3388 | // Simulate evaluation error |
| 3389 | return Promise.resolve({ |
| 3390 | status: "error", |
| 3391 | error: "Failed to evaluate policy", |
| 3392 | }); |
| 3393 | } else if (endpoint === "llm/compileChat") { |
| 3394 | return Promise.resolve({ |
| 3395 | status: "success", |
| 3396 | content: { |
| 3397 | compiledChatMessages: [{ role: "user", content: "Do something" }], |
| 3398 | didPrune: false, |
| 3399 | contextPercentage: 0.5, |
| 3400 | }, |
| 3401 | }); |
| 3402 | } else if (endpoint === "history/save") { |
| 3403 | return Promise.resolve({ status: "success" }); |
| 3404 | } else if (endpoint === "history/list") { |
| 3405 | return Promise.resolve({ status: "success", content: [] }); |
| 3406 | } |
| 3407 | return Promise.resolve({ status: "success", content: {} }); |
| 3408 | }); |
| 3409 | |
| 3410 | // Setup streaming with tool call |
| 3411 | async function* mockStreamWithTool() { |
| 3412 | yield [{ role: "assistant", content: "I'll help you." }]; |
| 3413 | yield [ |
| 3414 | { |
| 3415 | role: "assistant", |
| 3416 | content: "", |
| 3417 | toolCalls: [ |
| 3418 | { |
| 3419 | id: "tool-1", |
| 3420 | type: "function", |
| 3421 | function: { |
| 3422 | name: "some_tool", |
| 3423 | arguments: JSON.stringify({ arg: "value" }), |
| 3424 | }, |
| 3425 | }, |
| 3426 | ], |
| 3427 | }, |
| 3428 | ]; |
| 3429 | return { |
| 3430 | prompt: "Do something", |
| 3431 | completion: "I'll help you.", |
| 3432 | modelProvider: "anthropic", |
| 3433 | }; |
| 3434 | } |
| 3435 | |
| 3436 | mockIdeMessenger.llmStreamChat.mockReturnValue(mockStreamWithTool()); |
| 3437 | |
| 3438 | // Execute thunk - should handle error gracefully |
| 3439 | const result = await mockStore.dispatch( |
| 3440 | streamResponseThunk({ |
| 3441 | editorState: mockEditorState, |
| 3442 | modifiers: mockModifiers, |
| 3443 | }) as any, |
| 3444 | ); |
| 3445 | |
| 3446 | // Should complete successfully despite evaluation error |
| 3447 | expect(result.type).toBe("chat/streamResponse/fulfilled"); |
| 3448 | |
| 3449 | // Tool should be treated as disabled due to evaluation error |
| 3450 | expect(mockIdeMessenger.request).not.toHaveBeenCalledWith( |
| 3451 | "tools/call", |
| 3452 | expect.any(Object), |
| 3453 | ); |
| 3454 | }); |
| 3455 | |
| 3456 | it("should properly handle disabled commands and show error status", async () => { |
| 3457 | const { mockStore, mockIdeMessenger } = setupTest(); |
| 3458 | |
| 3459 | // Setup store with runTerminalCommand tool |
| 3460 | const mockStoreWithTerminalTool = createMockStore({ |
| 3461 | ...mockStore.getState(), |
| 3462 | config: { |
| 3463 | ...mockStore.getState().config, |
| 3464 | config: { |
| 3465 | ...mockStore.getState().config.config, |
| 3466 | tools: [ |
| 3467 | { |
| 3468 | type: "function", |
| 3469 | displayTitle: "Run Terminal Command", |
| 3470 | function: { |
| 3471 | name: "runTerminalCommand", |
| 3472 | description: "Execute a terminal command", |
| 3473 | parameters: { |
| 3474 | type: "object", |
| 3475 | properties: { |
| 3476 | command: { |
| 3477 | type: "string", |
| 3478 | description: "The command to execute", |
| 3479 | }, |
| 3480 | }, |
| 3481 | required: ["command"], |
| 3482 | }, |
| 3483 | }, |
| 3484 | defaultToolPolicy: "allowedWithPermission", |
| 3485 | readonly: false, |
| 3486 | group: "code", |
| 3487 | }, |
| 3488 | ], |
| 3489 | }, |
| 3490 | }, |
| 3491 | ui: { |
| 3492 | ...mockStore.getState().ui, |
| 3493 | toolSettings: { |
| 3494 | runTerminalCommand: "allowedWithPermission", |
| 3495 | }, |
| 3496 | }, |
| 3497 | }); |
| 3498 | |
| 3499 | const mockTerminalIdeMessenger = |
| 3500 | mockStoreWithTerminalTool.mockIdeMessenger; |
| 3501 | |
| 3502 | // Setup compilation and policy evaluation responses |
| 3503 | mockTerminalIdeMessenger.request.mockImplementation( |
| 3504 | (endpoint: string, data: any) => { |
| 3505 | if (endpoint === "llm/compileChat") { |
| 3506 | return Promise.resolve({ |
| 3507 | status: "success", |
| 3508 | content: { |
| 3509 | compiledChatMessages: [ |
| 3510 | { role: "user", content: "Run eval command" }, |
| 3511 | ], |
| 3512 | didPrune: false, |
| 3513 | contextPercentage: 0.9, |
| 3514 | }, |
| 3515 | }); |
| 3516 | } else if (endpoint === "tools/evaluatePolicy") { |
| 3517 | // Return disabled for eval command |
| 3518 | const args = data.args || {}; |
| 3519 | if (args.command && args.command.includes("eval")) { |
| 3520 | return Promise.resolve({ |
| 3521 | status: "success", |
| 3522 | content: { policy: "disabled" }, |
| 3523 | }); |
| 3524 | } |
| 3525 | return Promise.resolve({ |
| 3526 | status: "success", |
| 3527 | content: { policy: "allowedWithPermission" }, |
| 3528 | }); |
| 3529 | } else if (endpoint === "history/save") { |
| 3530 | return Promise.resolve({ status: "success" }); |
| 3531 | } else if (endpoint === "history/list") { |
| 3532 | return Promise.resolve({ status: "success", content: [] }); |
| 3533 | } |
| 3534 | return Promise.resolve({ status: "success", content: {} }); |
| 3535 | }, |
| 3536 | ); |
| 3537 | |
| 3538 | // Setup streaming with eval command tool call |
| 3539 | async function* mockStreamWithEvalCommand() { |
| 3540 | yield [ |
| 3541 | { role: "assistant", content: "I'll run the eval command for you." }, |
| 3542 | ]; |
| 3543 | yield [ |
| 3544 | { |
| 3545 | role: "assistant", |
| 3546 | content: "", |
| 3547 | toolCalls: [ |
| 3548 | { |
| 3549 | id: "tool-call-eval", |
| 3550 | type: "function", |
| 3551 | function: { |
| 3552 | name: "runTerminalCommand", |
| 3553 | arguments: JSON.stringify({ command: 'eval "echo hello"' }), |
| 3554 | }, |
| 3555 | }, |
| 3556 | ], |
| 3557 | }, |
| 3558 | ]; |
| 3559 | return { |
| 3560 | prompt: "Run eval command", |
| 3561 | completion: "I'll run the eval command for you.", |
| 3562 | modelProvider: "anthropic", |
| 3563 | }; |
| 3564 | } |
| 3565 | |
| 3566 | mockTerminalIdeMessenger.llmStreamChat.mockReturnValue( |
| 3567 | mockStreamWithEvalCommand(), |
| 3568 | ); |
| 3569 | |
| 3570 | // Execute thunk |
| 3571 | await mockStoreWithTerminalTool.dispatch( |
| 3572 | streamResponseThunk({ |
| 3573 | editorState: mockEditorState, |
| 3574 | modifiers: mockModifiers, |
| 3575 | }) as any, |
| 3576 | ); |
| 3577 | |
| 3578 | // Get final state |
| 3579 | const finalState = mockStoreWithTerminalTool.getState(); |
| 3580 | const toolCallStates = finalState.session.history.flatMap( |
| 3581 | (item) => item.toolCallStates || [], |
| 3582 | ); |
| 3583 | |
| 3584 | // Find the eval command tool call |
| 3585 | const evalToolCall = toolCallStates.find( |
| 3586 | (t) => t.toolCallId === "tool-call-eval", |
| 3587 | ); |
| 3588 | |
| 3589 | expect(evalToolCall).toBeDefined(); |
| 3590 | |
| 3591 | // The tool call should have an errored status (not "generated") |
| 3592 | expect(evalToolCall?.status).toBe("errored"); |
| 3593 | |
| 3594 | // The tool call should have an error message explaining it's disabled |
| 3595 | // Errors are stored as ContextItems with the error in the content |
| 3596 | const errorOutput = evalToolCall?.output?.[0]; |
| 3597 | expect(errorOutput?.content).toContain("disabled"); |
| 3598 | |
| 3599 | // Verify the command was NOT executed (no tool/call request) |
| 3600 | const toolCallRequests = |
| 3601 | mockTerminalIdeMessenger.request.mock.calls.filter( |
| 3602 | (call) => call[0] === "tools/call", |
| 3603 | ); |
| 3604 | expect(toolCallRequests).toHaveLength(0); |
| 3605 | }); |
| 3606 | }); |
| 3607 | }); |