Elffuss BraidHub

kikocis/continue public

main · 7.9 KiBRawPermalink
1import { Buffer } from "buffer";
2import { Writable } from "stream";
3import { LLMLogFormatter } from "./logFormatter";
4import { LLMLogger } from "./logger";
5
6class MemoryWritable extends Writable {
7 data: Buffer[] = [];
8 constructor() {
9 super();
10 this.data = [];
11 }
12
13 _write(
14 chunk: Buffer,
15 encoding: BufferEncoding,
16 callback: (error?: Error | null) => void,
17 ): void {
18 this.data.push(chunk);
19 callback();
20 }
21
22 getText() {
23 const decoder = new TextDecoder();
24 return decoder.decode(Buffer.concat(this.data as any));
25 }
26}
27
28describe("LLMLogFormatter", () => {
29 it("should format log items correctly", () => {
30 const logger = new LLMLogger();
31 const output = new MemoryWritable();
32 const formatter = new LLMLogFormatter(logger, output);
33
34 logger._logItem({
35 interactionId: "1",
36 timestamp: 1698765432100,
37 kind: "startChat",
38 options: {
39 model: "granite3.2-dense:8b",
40 },
41 messages: [
42 {
43 role: "system",
44 content: "You are a helpful assistant",
45 },
46 ],
47 provider: "watsonx",
48 });
49 logger._logItem({
50 interactionId: "1",
51 timestamp: 1698765433100,
52 kind: "message",
53 message: {
54 role: "assistant",
55 content: "Hello, ",
56 },
57 });
58 logger._logItem({
59 interactionId: "1",
60 timestamp: 1698765433200,
61 kind: "message",
62 message: {
63 role: "assistant",
64 content: "world!",
65 },
66 });
67 logger._logItem({
68 interactionId: "1",
69 timestamp: 1698765433300,
70 kind: "message",
71 message: {
72 role: "assistant",
73 content: "\nHow can I help you?",
74 },
75 });
76 logger._logItem({
77 interactionId: "1",
78 timestamp: 1698765434100,
79 kind: "success",
80 promptTokens: 10,
81 generatedTokens: 20,
82 thinkingTokens: 0,
83 usage: undefined,
84 });
85
86 expect(output.getText()).toBe(
87 [
88 " 15:17:12.1 [Chat]",
89 " Options: {",
90 ' "model": "granite3.2-dense:8b"',
91 " }",
92 " Role: system",
93 " | You are a helpful assistant",
94 " +1.0 Role: assistant",
95 " | Hello, world!",
96 " +1.2 | How can I help you?",
97 " +2.0 Success",
98 " Prompt Tokens: 10",
99 " Generated Tokens: 20",
100 "",
101 ].join("\n"),
102 );
103 });
104
105 it("should format completion interactions correctly", () => {
106 const logger = new LLMLogger();
107 const output = new MemoryWritable();
108 const formatter = new LLMLogFormatter(logger, output);
109
110 logger._logItem({
111 interactionId: "1",
112 timestamp: 1698765432100,
113 kind: "startComplete",
114 options: {
115 model: "granite3.2-dense:8b",
116 },
117 prompt: "A horse is a horse",
118 provider: "watsonx",
119 });
120 logger._logItem({
121 interactionId: "1",
122 timestamp: 1698765433100,
123 kind: "chunk",
124 chunk: ", of course, of course",
125 });
126
127 expect(output.getText()).toBe(
128 [
129 " 15:17:12.1 [Complete]",
130 " Options: {",
131 ' "model": "granite3.2-dense:8b"',
132 " }",
133 " Prompt:",
134 " | A horse is a horse",
135 " +1.0 Result:",
136 " | , of course, of course",
137 ].join("\n"),
138 );
139 });
140
141 it("should format FIM interactions correctly", () => {
142 const logger = new LLMLogger();
143 const output = new MemoryWritable();
144 const formatter = new LLMLogFormatter(logger, output);
145
146 logger._logItem({
147 interactionId: "1",
148 timestamp: 1698765432100,
149 kind: "startFim",
150 options: {
151 model: "granite3.2-dense:8b",
152 },
153 prefix: "A\nB",
154 suffix: "D\nE",
155 provider: "watsonx",
156 });
157 logger._logItem({
158 interactionId: "1",
159 timestamp: 1698765433100,
160 kind: "chunk",
161 chunk: "C",
162 });
163
164 expect(output.getText()).toBe(
165 [
166 " 15:17:12.1 [Fim]",
167 " Options: {",
168 ' "model": "granite3.2-dense:8b"',
169 " }",
170 " Prefix:",
171 " | A",
172 " | B",
173 " Suffix:",
174 " | D",
175 " | E",
176 " +1.0 Result:",
177 " | C",
178 ].join("\n"),
179 );
180 });
181
182 it("should interleave log items correctly", () => {
183 const logger = new LLMLogger();
184 const output = new MemoryWritable();
185 const formatter = new LLMLogFormatter(logger, output);
186
187 logger._logItem({
188 interactionId: "1",
189 timestamp: 1698765433100,
190 kind: "message",
191 message: {
192 role: "assistant",
193 content: "Hello, ",
194 },
195 });
196 logger._logItem({
197 interactionId: "2",
198 timestamp: 1698765433200,
199 kind: "message",
200 message: {
201 role: "assistant",
202 content: "Hello, ",
203 },
204 });
205 logger._logItem({
206 interactionId: "1",
207 timestamp: 1698765433300,
208 kind: "message",
209 message: {
210 role: "assistant",
211 content: "World!",
212 },
213 });
214 logger._logItem({
215 interactionId: "2",
216 timestamp: 1698765433400,
217 kind: "message",
218 message: {
219 role: "assistant",
220 content: "World!",
221 },
222 });
223 // Check if we end interaction 2 and start another interaction,
224 // it gets a different prefix marker.
225 logger._logItem({
226 interactionId: "2",
227 timestamp: 1698765434000,
228 kind: "success",
229 promptTokens: 10,
230 generatedTokens: 20,
231 thinkingTokens: 0,
232 usage: undefined,
233 });
234 logger._logItem({
235 interactionId: "3",
236 timestamp: 1698765434100,
237 kind: "message",
238 message: {
239 role: "assistant",
240 content: "Hello, World!",
241 },
242 });
243
244 expect(output.getText()).toBe(
245 [
246 " 15:17:13.1 Role: assistant",
247 " | Hello, ",
248 "|15:17:13.2 Role: assistant",
249 "| | Hello, ",
250 " +0.2 | World!",
251 "| +0.2 | World!",
252 "| +0.8 Success",
253 "| Prompt Tokens: 10",
254 "| Generated Tokens: 20",
255 "&15:17:14.1 Role: assistant",
256 "& | Hello, World!",
257 ].join("\n"),
258 );
259 });
260
261 it("should wrap long lines", () => {
262 const logger = new LLMLogger();
263 const output = new MemoryWritable();
264 const formatter = new LLMLogFormatter(logger, output, 40);
265
266 // Test a single message long enough to wrap; the input is
267 // "<xx...xx>", not "xxxx" to make sure we are writing the correct
268 // substrings.
269 logger._logItem({
270 interactionId: "1",
271 timestamp: 1698765433100,
272 kind: "message",
273 message: {
274 role: "assistant",
275 content: "<" + "x".repeat(58) + ">",
276 },
277 });
278 // Test when a first message doesn't wrap, but another message
279 // that continues the same line causes a wrap
280 logger._logItem({
281 interactionId: "1",
282 timestamp: 1698765433200,
283 kind: "message",
284 message: {
285 role: "assistant",
286 content: "\n<" + "y".repeat(29),
287 },
288 });
289 logger._logItem({
290 interactionId: "1",
291 timestamp: 1698765433300,
292 kind: "message",
293 message: {
294 role: "assistant",
295 content: "y".repeat(29) + ">",
296 },
297 });
298 logger._logItem({
299 interactionId: "1",
300 timestamp: 1698765433400,
301 kind: "message",
302 message: {
303 role: "assistant",
304 content: "\n" + "Artichokes and Beans ".repeat(3),
305 },
306 });
307
308 expect(output.getText()).toBe(
309 [
310 " 15:17:13.1 Role: assistant",
311 " | <" + "x".repeat(39),
312 " . " + "x".repeat(19) + ">",
313 " +0.1 | <" + "y".repeat(39),
314 " +0.2 . " + "y".repeat(19) + ">",
315 " +0.3 | Artichokes and Beans Artichokes and",
316 " . Beans Artichokes and Beans ",
317 ].join("\n"),
318 );
319 });
320});