Elffuss BraidHub

kikocis/continue public

main · 5.6 KiBRawPermalink
1import { beforeEach, describe, expect, test, vi } from "vitest";
2
3import {
4 SERVICE_NAMES,
5 initializeServices,
6 serviceContainer,
7} from "../services/index.js";
8import { modeService } from "../services/ModeService.js";
9
10import { getAllTools } from "./handleToolCalls.js";
11
12describe("streamChatResponse - Mode Switch During Streaming", () => {
13 beforeEach(async () => {
14 // Clean up service container state before each test
15 const services = [
16 SERVICE_NAMES.TOOL_PERMISSIONS,
17 SERVICE_NAMES.AUTH,
18 SERVICE_NAMES.API_CLIENT,
19 SERVICE_NAMES.CONFIG,
20 SERVICE_NAMES.MODEL,
21 SERVICE_NAMES.MCP,
22 ];
23
24 services.forEach((service) => {
25 (serviceContainer as any).services.delete(service);
26 (serviceContainer as any).factories.delete(service);
27 (serviceContainer as any).dependencies.delete(service);
28 });
29
30 // Initialize services in normal mode
31 await initializeServices({
32 headless: true,
33 toolPermissionOverrides: {
34 mode: "normal",
35 },
36 });
37 });
38
39 test("should recompute tools on each iteration to handle mode switches", async () => {
40 // Start in normal mode
41 let tools = await getAllTools();
42 let toolNames = tools.map((t) => t.function.name);
43
44 // Should include write tools in normal mode
45 expect(toolNames).toContain("Write");
46 expect(toolNames).toContain("Edit");
47
48 // Switch to plan mode (simulating Shift+Tab during streaming)
49 modeService.switchMode("plan");
50
51 // Update the service container (this is what UserInput.tsx does)
52 const updatedState = modeService.getToolPermissionService().getState();
53 serviceContainer.set(SERVICE_NAMES.TOOL_PERMISSIONS, updatedState);
54
55 // Recompute tools - should now exclude write tools
56 tools = await getAllTools();
57 toolNames = tools.map((t) => t.function.name);
58
59 // Should exclude write tools in plan mode
60 expect(toolNames).not.toContain("Write");
61 expect(toolNames).not.toContain("Edit");
62
63 // Should still include read-only tools
64 expect(toolNames).toContain("Read");
65 expect(toolNames).toContain("Bash");
66 expect(toolNames).toContain("List");
67 });
68
69 test("getAllTools reflects current mode immediately", async () => {
70 // Start in normal mode
71 expect(modeService.getCurrentMode()).toBe("normal");
72 let tools = await getAllTools();
73 expect(tools.map((t) => t.function.name)).toContain("Write");
74
75 // Switch to plan mode
76 modeService.switchMode("plan");
77 let updatedState = modeService.getToolPermissionService().getState();
78 serviceContainer.set(SERVICE_NAMES.TOOL_PERMISSIONS, updatedState);
79 expect(modeService.getCurrentMode()).toBe("plan");
80
81 // getAllTools should immediately reflect the new mode
82 tools = await getAllTools();
83 expect(tools.map((t) => t.function.name)).not.toContain("Write");
84
85 // Switch to auto mode
86 modeService.switchMode("auto");
87 updatedState = modeService.getToolPermissionService().getState();
88 serviceContainer.set(SERVICE_NAMES.TOOL_PERMISSIONS, updatedState);
89 expect(modeService.getCurrentMode()).toBe("auto");
90
91 // getAllTools should immediately reflect auto mode (all tools allowed)
92 tools = await getAllTools();
93 expect(tools.map((t) => t.function.name)).toContain("Write");
94 expect(tools.map((t) => t.function.name)).toContain("Edit");
95 expect(tools.map((t) => t.function.name)).toContain("Read");
96 });
97
98 test("demonstrates the fix: no more stale tool lists", async () => {
99 // This test demonstrates that the fix prevents the original race condition
100
101 // Mock a multi-turn conversation scenario
102 const mockLlmApi = {
103 chatCompletions: {
104 create: vi.fn().mockImplementation(() => {
105 // Return a mock stream that would trigger multiple iterations
106 return {
107 [Symbol.asyncIterator]: async function* () {
108 // First response - content only
109 yield {
110 choices: [
111 {
112 delta: { content: "I'll help you with that." },
113 finish_reason: null,
114 },
115 ],
116 };
117
118 // Second response - indicates tool calls coming
119 yield {
120 choices: [
121 {
122 delta: {
123 tool_calls: [
124 {
125 index: 0,
126 id: "call_1",
127 function: {
128 name: "Read",
129 arguments: '{"filepath": "test.txt"}',
130 },
131 },
132 ],
133 },
134 finish_reason: null,
135 },
136 ],
137 };
138
139 // Final response
140 yield {
141 choices: [
142 {
143 delta: {},
144 finish_reason: "tool_calls",
145 },
146 ],
147 };
148 },
149 };
150 }),
151 },
152 };
153
154 const mockModel = {
155 model: "gpt-4",
156 defaultCompletionOptions: {},
157 };
158
159 const abortController = new AbortController();
160
161 // Start in normal mode - tools should include Write
162 const initialTools = await getAllTools();
163 expect(initialTools.map((t) => t.function.name)).toContain("Write");
164
165 // During streaming, if mode switches, subsequent iterations should use new tools
166 // This is now handled by recomputing tools on each iteration
167
168 // The fix ensures that each call to processStreamingResponse gets fresh tools
169 // based on the current mode, preventing the race condition
170 });
171});