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