Elffuss BraidHub

kikocis/continue public

main · 5.9 KiBRawPermalink
1import type { ChatHistoryItem } from "core/index.js";
2import { describe, expect, it, vi } from "vitest";
3
4import { createStreamCallbacks } from "./useChat.stream.helpers.js";
5
6describe("useChat streaming", () => {
7 it("should handle content completion", () => {
8 let chatHistory: ChatHistoryItem[] = [];
9
10 const setChatHistory = vi.fn((updater: any) => {
11 if (typeof updater === "function") {
12 chatHistory = updater(chatHistory);
13 } else {
14 chatHistory = updater;
15 }
16 });
17
18 const callbacks = createStreamCallbacks({
19 setChatHistory,
20 setActivePermissionRequest: vi.fn(),
21 });
22
23 // onContent is now empty and doesn't update history
24 callbacks.onContent("Hello");
25 expect(setChatHistory).toHaveBeenCalledTimes(0);
26 expect(chatHistory).toHaveLength(0);
27
28 callbacks.onContent(" world");
29 expect(setChatHistory).toHaveBeenCalledTimes(0);
30 expect(chatHistory).toHaveLength(0);
31
32 callbacks.onContent("!");
33 expect(setChatHistory).toHaveBeenCalledTimes(0);
34 expect(chatHistory).toHaveLength(0);
35
36 // Complete the streaming - this adds the message
37 callbacks.onContentComplete("Hello world!");
38 expect(setChatHistory).toHaveBeenCalledTimes(1);
39 expect(chatHistory).toHaveLength(1);
40 expect(chatHistory[0].message.content).toBe("Hello world!");
41 });
42
43 it("should handle tool calls during streaming", () => {
44 let chatHistory: ChatHistoryItem[] = [];
45
46 const setChatHistory = vi.fn((updater: any) => {
47 if (typeof updater === "function") {
48 chatHistory = updater(chatHistory);
49 } else {
50 chatHistory = updater;
51 }
52 });
53
54 const callbacks = createStreamCallbacks({
55 setChatHistory,
56 setActivePermissionRequest: vi.fn(),
57 });
58
59 // Start with some content
60 callbacks.onContent("Let me help you with that.");
61 expect(chatHistory).toHaveLength(0); // onContent is empty, doesn't add messages
62
63 // Complete content before tool call
64 callbacks.onContentComplete("Let me help you with that.");
65 expect(chatHistory).toHaveLength(1);
66 expect(chatHistory[0].message.content).toBe("Let me help you with that.");
67
68 // Now a tool call - creates a new message
69 callbacks.onToolStart("Read", { file_path: "/test.txt" });
70 expect(chatHistory).toHaveLength(2); // New message for tool call
71 expect(chatHistory[1].toolCallStates).toHaveLength(1);
72 expect(chatHistory[1].toolCallStates![0].status).toBe("calling");
73
74 // Tool result
75 callbacks.onToolResult("File contents", "Read", "done");
76 expect(chatHistory[1].toolCallStates![0].status).toBe("done");
77
78 // More content after tool creates a new message
79 callbacks.onContent("Based on the file");
80 expect(chatHistory).toHaveLength(2); // onContent is empty, doesn't add
81
82 callbacks.onContentComplete("Based on the file");
83 expect(chatHistory).toHaveLength(3); // New message from onContentComplete
84 expect(chatHistory[2].message.role).toBe("assistant");
85 expect(chatHistory[2].message.content).toBe("Based on the file");
86 });
87
88 it("should create new message if last message is not assistant", () => {
89 let chatHistory: ChatHistoryItem[] = [
90 {
91 message: { role: "user", content: "Hello" },
92 contextItems: [],
93 },
94 ];
95
96 const setChatHistory = vi.fn((updater: any) => {
97 if (typeof updater === "function") {
98 chatHistory = updater(chatHistory);
99 } else {
100 chatHistory = updater;
101 }
102 });
103
104 const callbacks = createStreamCallbacks({
105 setChatHistory,
106 setActivePermissionRequest: vi.fn(),
107 });
108
109 callbacks.onContent("Hi there");
110 expect(chatHistory).toHaveLength(1); // onContent is empty, doesn't add
111
112 callbacks.onContentComplete("Hi there");
113 expect(chatHistory).toHaveLength(2);
114 expect(chatHistory[1].message.role).toBe("assistant");
115 expect(chatHistory[1].message.content).toBe("Hi there");
116 });
117
118 it("should create separate messages for each tool call", () => {
119 let chatHistory: ChatHistoryItem[] = [];
120
121 const setChatHistory = vi.fn((updater: any) => {
122 if (typeof updater === "function") {
123 chatHistory = updater(chatHistory);
124 } else {
125 chatHistory = updater;
126 }
127 });
128
129 const callbacks = createStreamCallbacks({
130 setChatHistory,
131 setActivePermissionRequest: vi.fn(),
132 });
133
134 // Add initial content
135 callbacks.onContentComplete("Let me check multiple things");
136 expect(chatHistory).toHaveLength(1);
137
138 // First tool call - creates new message
139 callbacks.onToolStart("Read", { file_path: "/first.txt" });
140 expect(chatHistory).toHaveLength(2);
141 expect(chatHistory[1].toolCallStates).toHaveLength(1);
142 expect(chatHistory[1].toolCallStates![0].toolCall.function.name).toBe(
143 "Read",
144 );
145
146 // First tool result
147 callbacks.onToolResult("First file contents", "Read", "done");
148 expect(chatHistory[1].toolCallStates![0].status).toBe("done");
149
150 // Second tool call - creates another new message
151 callbacks.onToolStart("Write", {
152 file_path: "/second.txt",
153 content: "test",
154 });
155 expect(chatHistory).toHaveLength(3);
156 expect(chatHistory[2].toolCallStates).toHaveLength(1);
157 expect(chatHistory[2].toolCallStates![0].toolCall.function.name).toBe(
158 "Write",
159 );
160
161 // Second tool result
162 callbacks.onToolResult("Write successful", "Write", "done");
163 expect(chatHistory[2].toolCallStates![0].status).toBe("done");
164
165 // Third tool call - creates yet another new message
166 callbacks.onToolStart("Read", { file_path: "/third.txt" });
167 expect(chatHistory).toHaveLength(4);
168 expect(chatHistory[3].toolCallStates).toHaveLength(1);
169 expect(chatHistory[3].toolCallStates![0].toolCall.function.name).toBe(
170 "Read",
171 );
172
173 // Final content after all tools
174 callbacks.onContentComplete("All operations completed");
175 expect(chatHistory).toHaveLength(5);
176 expect(chatHistory[4].message.content).toBe("All operations completed");
177 });
178});