| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { BaseLlmApi } from "@continuedev/openai-adapters"; |
| 3 | import { ChatCompletionMessageParam } from "openai/resources.mjs"; |
| 4 | import { describe, it, expect, vi } from "vitest"; |
| 5 | |
| 6 | import { |
| 7 | compactChatHistory, |
| 8 | findCompactionIndex, |
| 9 | getHistoryForLLM, |
| 10 | COMPACTION_MARKER, |
| 11 | } from "./compaction.js"; |
| 12 | import { streamChatResponse } from "./streamChatResponse.js"; |
| 13 | |
| 14 | // Mock the streamChatResponse function |
| 15 | vi.mock("./streamChatResponse.js", () => ({ |
| 16 | streamChatResponse: vi.fn(), |
| 17 | })); |
| 18 | |
| 19 | describe("compaction", () => { |
| 20 | const mockModel: ModelConfig = { |
| 21 | name: "test-model", |
| 22 | provider: "test", |
| 23 | model: "test-model", |
| 24 | } as ModelConfig; |
| 25 | |
| 26 | const mockLlmApi = {} as BaseLlmApi; |
| 27 | |
| 28 | describe("findCompactionIndex", () => { |
| 29 | it("should find compaction marker in chat history", () => { |
| 30 | const history: ChatCompletionMessageParam[] = [ |
| 31 | { role: "system", content: "System message" }, |
| 32 | { role: "user", content: "Hello" }, |
| 33 | { role: "assistant", content: "Hi there" }, |
| 34 | { |
| 35 | role: "assistant", |
| 36 | content: `${COMPACTION_MARKER}\nThis is a summary`, |
| 37 | }, |
| 38 | { role: "user", content: "Another message" }, |
| 39 | ]; |
| 40 | |
| 41 | const index = findCompactionIndex(history); |
| 42 | expect(index).toBe(3); |
| 43 | }); |
| 44 | |
| 45 | it("should return null if no compaction marker found", () => { |
| 46 | const history: ChatCompletionMessageParam[] = [ |
| 47 | { role: "system", content: "System message" }, |
| 48 | { role: "user", content: "Hello" }, |
| 49 | { role: "assistant", content: "Hi there" }, |
| 50 | ]; |
| 51 | |
| 52 | const index = findCompactionIndex(history); |
| 53 | expect(index).toBeNull(); |
| 54 | }); |
| 55 | |
| 56 | it("should return first compaction marker when multiple exist", () => { |
| 57 | const history: ChatCompletionMessageParam[] = [ |
| 58 | { role: "system", content: "System message" }, |
| 59 | { role: "assistant", content: `${COMPACTION_MARKER}\nFirst summary` }, |
| 60 | { role: "user", content: "Hello" }, |
| 61 | { role: "assistant", content: `${COMPACTION_MARKER}\nSecond summary` }, |
| 62 | ]; |
| 63 | |
| 64 | const index = findCompactionIndex(history); |
| 65 | expect(index).toBe(1); |
| 66 | }); |
| 67 | |
| 68 | it("should return null for empty chat history", () => { |
| 69 | const history: ChatCompletionMessageParam[] = []; |
| 70 | const index = findCompactionIndex(history); |
| 71 | expect(index).toBeNull(); |
| 72 | }); |
| 73 | |
| 74 | it("should not match marker in middle of content", () => { |
| 75 | const history: ChatCompletionMessageParam[] = [ |
| 76 | { role: "system", content: "System message" }, |
| 77 | { |
| 78 | role: "assistant", |
| 79 | content: `Some text ${COMPACTION_MARKER} in the middle`, |
| 80 | }, |
| 81 | { role: "user", content: "Hello" }, |
| 82 | ]; |
| 83 | |
| 84 | const index = findCompactionIndex(history); |
| 85 | expect(index).toBeNull(); |
| 86 | }); |
| 87 | |
| 88 | it("should handle non-string content", () => { |
| 89 | const history: ChatCompletionMessageParam[] = [ |
| 90 | { role: "system", content: "System message" }, |
| 91 | { role: "assistant", content: null as any }, |
| 92 | { role: "assistant", content: undefined as any }, |
| 93 | { role: "assistant", content: 123 as any }, |
| 94 | ]; |
| 95 | |
| 96 | const index = findCompactionIndex(history); |
| 97 | expect(index).toBeNull(); |
| 98 | }); |
| 99 | |
| 100 | it("should only match assistant role messages", () => { |
| 101 | const history: ChatCompletionMessageParam[] = [ |
| 102 | { role: "system", content: `${COMPACTION_MARKER}\nSystem compaction?` }, |
| 103 | { role: "user", content: `${COMPACTION_MARKER}\nUser compaction?` }, |
| 104 | { role: "assistant", content: `${COMPACTION_MARKER}\nReal compaction` }, |
| 105 | ]; |
| 106 | |
| 107 | const index = findCompactionIndex(history); |
| 108 | expect(index).toBe(2); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | describe("getHistoryForLLM", () => { |
| 113 | it("should return full history when no compaction index", () => { |
| 114 | const history: ChatCompletionMessageParam[] = [ |
| 115 | { role: "system", content: "System message" }, |
| 116 | { role: "user", content: "Hello" }, |
| 117 | { role: "assistant", content: "Hi there" }, |
| 118 | ]; |
| 119 | |
| 120 | const result = getHistoryForLLM(history, null); |
| 121 | expect(result).toEqual(history); |
| 122 | }); |
| 123 | |
| 124 | it("should return compacted history with system message", () => { |
| 125 | const history: ChatCompletionMessageParam[] = [ |
| 126 | { role: "system", content: "System message" }, |
| 127 | { role: "user", content: "Hello" }, |
| 128 | { role: "assistant", content: "Hi there" }, |
| 129 | { |
| 130 | role: "assistant", |
| 131 | content: `${COMPACTION_MARKER}\nThis is a summary`, |
| 132 | }, |
| 133 | { role: "user", content: "Another message" }, |
| 134 | ]; |
| 135 | |
| 136 | const result = getHistoryForLLM(history, 3); |
| 137 | expect(result).toEqual([ |
| 138 | { role: "system", content: "System message" }, |
| 139 | { |
| 140 | role: "assistant", |
| 141 | content: `${COMPACTION_MARKER}\nThis is a summary`, |
| 142 | }, |
| 143 | { role: "user", content: "Another message" }, |
| 144 | ]); |
| 145 | }); |
| 146 | |
| 147 | it("should return compacted history without system message when compaction is at index 0", () => { |
| 148 | const history: ChatCompletionMessageParam[] = [ |
| 149 | { |
| 150 | role: "assistant", |
| 151 | content: `${COMPACTION_MARKER}\nThis is a summary`, |
| 152 | }, |
| 153 | { role: "user", content: "Another message" }, |
| 154 | ]; |
| 155 | |
| 156 | const result = getHistoryForLLM(history, 0); |
| 157 | expect(result).toEqual(history); |
| 158 | }); |
| 159 | |
| 160 | it("should return full history when compactionIndex is out of bounds", () => { |
| 161 | const history: ChatCompletionMessageParam[] = [ |
| 162 | { role: "system", content: "System message" }, |
| 163 | { role: "user", content: "Hello" }, |
| 164 | ]; |
| 165 | |
| 166 | const result = getHistoryForLLM(history, 10); |
| 167 | expect(result).toEqual(history); |
| 168 | }); |
| 169 | |
| 170 | it("should handle negative compactionIndex", () => { |
| 171 | const history: ChatCompletionMessageParam[] = [ |
| 172 | { role: "system", content: "System message" }, |
| 173 | { role: "user", content: "Hello" }, |
| 174 | ]; |
| 175 | |
| 176 | const result = getHistoryForLLM(history, -1); |
| 177 | // Negative index with slice means "from the end", so -1 gives us only the last message |
| 178 | // Since compactionIndex is not > 0, system message is not included |
| 179 | expect(result).toEqual([{ role: "user", content: "Hello" }]); |
| 180 | }); |
| 181 | |
| 182 | it("should handle empty history", () => { |
| 183 | const history: ChatCompletionMessageParam[] = []; |
| 184 | const result = getHistoryForLLM(history, 0); |
| 185 | expect(result).toEqual([]); |
| 186 | }); |
| 187 | |
| 188 | it("should handle history with only system message", () => { |
| 189 | const history: ChatCompletionMessageParam[] = [ |
| 190 | { role: "system", content: "System message" }, |
| 191 | ]; |
| 192 | |
| 193 | const result = getHistoryForLLM(history, 0); |
| 194 | expect(result).toEqual(history); |
| 195 | }); |
| 196 | |
| 197 | it("should handle history without system message but with compaction", () => { |
| 198 | const history: ChatCompletionMessageParam[] = [ |
| 199 | { role: "user", content: "First message" }, |
| 200 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 201 | { role: "user", content: "New message" }, |
| 202 | ]; |
| 203 | |
| 204 | const result = getHistoryForLLM(history, 1); |
| 205 | expect(result).toEqual([ |
| 206 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 207 | { role: "user", content: "New message" }, |
| 208 | ]); |
| 209 | }); |
| 210 | |
| 211 | it("should include system message when first message is not system", () => { |
| 212 | const history: ChatCompletionMessageParam[] = [ |
| 213 | { role: "user", content: "First user message" }, |
| 214 | { role: "system", content: "System message in wrong position" }, |
| 215 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 216 | { role: "user", content: "New message" }, |
| 217 | ]; |
| 218 | |
| 219 | // Since system message is not at index 0, it's not included |
| 220 | const result = getHistoryForLLM(history, 2); |
| 221 | expect(result).toEqual([ |
| 222 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 223 | { role: "user", content: "New message" }, |
| 224 | ]); |
| 225 | }); |
| 226 | |
| 227 | it("should preserve message order from compaction point", () => { |
| 228 | const history: ChatCompletionMessageParam[] = [ |
| 229 | { role: "system", content: "System" }, |
| 230 | { role: "user", content: "Old 1" }, |
| 231 | { role: "assistant", content: "Old 2" }, |
| 232 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 233 | { role: "user", content: "New 1" }, |
| 234 | { role: "assistant", content: "New 2" }, |
| 235 | { role: "user", content: "New 3" }, |
| 236 | ]; |
| 237 | |
| 238 | const result = getHistoryForLLM(history, 3); |
| 239 | expect(result).toEqual([ |
| 240 | { role: "system", content: "System" }, |
| 241 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 242 | { role: "user", content: "New 1" }, |
| 243 | { role: "assistant", content: "New 2" }, |
| 244 | { role: "user", content: "New 3" }, |
| 245 | ]); |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | describe("compactChatHistory", () => { |
| 250 | it("should compact chat history successfully", async () => { |
| 251 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 252 | const mockContent = "This is a summary of the conversation"; |
| 253 | |
| 254 | mockStreamResponse.mockImplementation( |
| 255 | async (history, model, api, controller, callbacks) => { |
| 256 | callbacks?.onContent?.(mockContent); |
| 257 | callbacks?.onContentComplete?.(mockContent); |
| 258 | return mockContent; |
| 259 | }, |
| 260 | ); |
| 261 | |
| 262 | const history: ChatCompletionMessageParam[] = [ |
| 263 | { role: "system", content: "System message" }, |
| 264 | { role: "user", content: "Hello" }, |
| 265 | { role: "assistant", content: "Hi there" }, |
| 266 | ]; |
| 267 | |
| 268 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 269 | |
| 270 | expect(result.compactedHistory).toHaveLength(2); |
| 271 | expect(result.compactedHistory[0]).toEqual({ |
| 272 | role: "system", |
| 273 | content: "System message", |
| 274 | }); |
| 275 | expect(result.compactedHistory[1]).toEqual({ |
| 276 | role: "assistant", |
| 277 | content: `${COMPACTION_MARKER}\n${mockContent}`, |
| 278 | }); |
| 279 | expect(result.compactionIndex).toBe(1); |
| 280 | expect(result.compactionContent).toBe(mockContent); |
| 281 | }); |
| 282 | |
| 283 | it("should handle callbacks correctly", async () => { |
| 284 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 285 | const mockContent = "Summary content"; |
| 286 | const onStreamContent = vi.fn(); |
| 287 | const onStreamComplete = vi.fn(); |
| 288 | |
| 289 | mockStreamResponse.mockImplementation( |
| 290 | async (history, model, api, controller, callbacks) => { |
| 291 | callbacks?.onContent?.("Summary "); |
| 292 | callbacks?.onContent?.("content"); |
| 293 | callbacks?.onContentComplete?.(mockContent); |
| 294 | return mockContent; |
| 295 | }, |
| 296 | ); |
| 297 | |
| 298 | const history: ChatCompletionMessageParam[] = [ |
| 299 | { role: "user", content: "Hello" }, |
| 300 | ]; |
| 301 | |
| 302 | await compactChatHistory(history, mockModel, mockLlmApi, { |
| 303 | onStreamContent, |
| 304 | onStreamComplete, |
| 305 | }); |
| 306 | |
| 307 | expect(onStreamContent).toHaveBeenCalledWith("Summary "); |
| 308 | expect(onStreamContent).toHaveBeenCalledWith("content"); |
| 309 | expect(onStreamComplete).toHaveBeenCalled(); |
| 310 | }); |
| 311 | |
| 312 | it("should handle errors correctly", async () => { |
| 313 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 314 | const mockError = new Error("Stream failed"); |
| 315 | const onError = vi.fn(); |
| 316 | |
| 317 | mockStreamResponse.mockRejectedValue(mockError); |
| 318 | |
| 319 | const history: ChatCompletionMessageParam[] = [ |
| 320 | { role: "user", content: "Hello" }, |
| 321 | ]; |
| 322 | |
| 323 | await expect( |
| 324 | compactChatHistory(history, mockModel, mockLlmApi, { onError }), |
| 325 | ).rejects.toThrow("Stream failed"); |
| 326 | |
| 327 | expect(onError).toHaveBeenCalledWith(mockError); |
| 328 | }); |
| 329 | |
| 330 | it("should handle history with only system message", async () => { |
| 331 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 332 | const mockContent = "Summary of system setup"; |
| 333 | |
| 334 | mockStreamResponse.mockImplementation( |
| 335 | async (history, model, api, controller, callbacks) => { |
| 336 | callbacks?.onContent?.(mockContent); |
| 337 | callbacks?.onContentComplete?.(mockContent); |
| 338 | return mockContent; |
| 339 | }, |
| 340 | ); |
| 341 | |
| 342 | const history: ChatCompletionMessageParam[] = [ |
| 343 | { role: "system", content: "You are a helpful assistant" }, |
| 344 | ]; |
| 345 | |
| 346 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 347 | |
| 348 | expect(result.compactedHistory).toHaveLength(2); |
| 349 | expect(result.compactedHistory[0]).toEqual({ |
| 350 | role: "system", |
| 351 | content: "You are a helpful assistant", |
| 352 | }); |
| 353 | expect(result.compactedHistory[1].content).toContain(COMPACTION_MARKER); |
| 354 | }); |
| 355 | |
| 356 | it("should handle empty content from stream", async () => { |
| 357 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 358 | |
| 359 | mockStreamResponse.mockImplementation( |
| 360 | async (history, model, api, controller, callbacks) => { |
| 361 | callbacks?.onContent?.(""); |
| 362 | callbacks?.onContentComplete?.(""); |
| 363 | return ""; |
| 364 | }, |
| 365 | ); |
| 366 | |
| 367 | const history: ChatCompletionMessageParam[] = [ |
| 368 | { role: "user", content: "Hello" }, |
| 369 | ]; |
| 370 | |
| 371 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 372 | |
| 373 | expect(result.compactionContent).toBe(""); |
| 374 | expect(result.compactedHistory[0].content).toBe(`${COMPACTION_MARKER}\n`); |
| 375 | }); |
| 376 | |
| 377 | it("should correctly construct prompt for compaction", async () => { |
| 378 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 379 | let capturedHistory: ChatCompletionMessageParam[] = []; |
| 380 | |
| 381 | mockStreamResponse.mockImplementation( |
| 382 | async (history, model, api, controller, callbacks) => { |
| 383 | capturedHistory = [...history]; |
| 384 | callbacks?.onContent?.("Summary"); |
| 385 | callbacks?.onContentComplete?.("Summary"); |
| 386 | return "Summary"; |
| 387 | }, |
| 388 | ); |
| 389 | |
| 390 | const history: ChatCompletionMessageParam[] = [ |
| 391 | { role: "system", content: "System" }, |
| 392 | { role: "user", content: "Hello" }, |
| 393 | { role: "assistant", content: "Hi" }, |
| 394 | ]; |
| 395 | |
| 396 | await compactChatHistory(history, mockModel, mockLlmApi); |
| 397 | |
| 398 | // Should have original history plus the compaction prompt |
| 399 | expect(capturedHistory).toHaveLength(4); |
| 400 | expect(capturedHistory[3]).toEqual({ |
| 401 | role: "user", |
| 402 | content: expect.stringContaining("provide a concise summary"), |
| 403 | }); |
| 404 | }); |
| 405 | |
| 406 | it("should handle history with tool calls and mixed message types", async () => { |
| 407 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 408 | const mockContent = "Summary including tool usage"; |
| 409 | |
| 410 | mockStreamResponse.mockImplementation( |
| 411 | async (history, model, api, controller, callbacks) => { |
| 412 | callbacks?.onContent?.(mockContent); |
| 413 | callbacks?.onContentComplete?.(mockContent); |
| 414 | return mockContent; |
| 415 | }, |
| 416 | ); |
| 417 | |
| 418 | const history: ChatCompletionMessageParam[] = [ |
| 419 | { role: "system", content: "System" }, |
| 420 | { role: "user", content: "Do something" }, |
| 421 | { role: "assistant", content: "I'll help", tool_calls: [{} as any] }, |
| 422 | { role: "tool", content: "Tool result", tool_call_id: "123" }, |
| 423 | { role: "assistant", content: "Done" }, |
| 424 | ]; |
| 425 | |
| 426 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 427 | |
| 428 | expect(result.compactedHistory).toHaveLength(2); |
| 429 | expect(result.compactionContent).toBe(mockContent); |
| 430 | }); |
| 431 | |
| 432 | it("should handle very long chat histories", async () => { |
| 433 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 434 | |
| 435 | mockStreamResponse.mockImplementation( |
| 436 | async (history, model, api, controller, callbacks) => { |
| 437 | callbacks?.onContent?.("Long summary"); |
| 438 | callbacks?.onContentComplete?.("Long summary"); |
| 439 | return "Long summary"; |
| 440 | }, |
| 441 | ); |
| 442 | |
| 443 | // Create a very long history |
| 444 | const history: ChatCompletionMessageParam[] = [ |
| 445 | { role: "system", content: "System" }, |
| 446 | ]; |
| 447 | |
| 448 | for (let i = 0; i < 100; i++) { |
| 449 | history.push({ role: "user", content: `User message ${i}` }); |
| 450 | history.push({ role: "assistant", content: `Assistant response ${i}` }); |
| 451 | } |
| 452 | |
| 453 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 454 | |
| 455 | expect(result.compactedHistory).toHaveLength(2); // System + compaction |
| 456 | expect(result.compactionIndex).toBe(1); |
| 457 | }); |
| 458 | |
| 459 | it("should handle history without system message", async () => { |
| 460 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 461 | const mockContent = "Summary without system"; |
| 462 | |
| 463 | mockStreamResponse.mockImplementation( |
| 464 | async (history, model, api, controller, callbacks) => { |
| 465 | callbacks?.onContent?.(mockContent); |
| 466 | callbacks?.onContentComplete?.(mockContent); |
| 467 | return mockContent; |
| 468 | }, |
| 469 | ); |
| 470 | |
| 471 | const history: ChatCompletionMessageParam[] = [ |
| 472 | { role: "user", content: "Hello" }, |
| 473 | { role: "assistant", content: "Hi" }, |
| 474 | ]; |
| 475 | |
| 476 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 477 | |
| 478 | expect(result.compactedHistory).toHaveLength(1); // Only compaction |
| 479 | expect(result.compactedHistory[0].role).toBe("assistant"); |
| 480 | expect(result.compactionIndex).toBe(0); |
| 481 | }); |
| 482 | }); |
| 483 | |
| 484 | describe("property-based tests", () => { |
| 485 | it("getHistoryForLLM should always return a subset of the original history", () => { |
| 486 | const testCases: Array<{ |
| 487 | history: ChatCompletionMessageParam[]; |
| 488 | compactionIndex: number | null; |
| 489 | }> = [ |
| 490 | { |
| 491 | history: [ |
| 492 | { role: "system", content: "System" }, |
| 493 | { role: "user", content: "User 1" }, |
| 494 | { role: "assistant", content: "Assistant 1" }, |
| 495 | ], |
| 496 | compactionIndex: 1, |
| 497 | }, |
| 498 | { |
| 499 | history: [ |
| 500 | { role: "user", content: "User 1" }, |
| 501 | { role: "assistant", content: "Assistant 1" }, |
| 502 | { role: "user", content: "User 2" }, |
| 503 | ], |
| 504 | compactionIndex: 2, |
| 505 | }, |
| 506 | { |
| 507 | history: [], |
| 508 | compactionIndex: null, |
| 509 | }, |
| 510 | ]; |
| 511 | |
| 512 | testCases.forEach(({ history, compactionIndex }) => { |
| 513 | const result = getHistoryForLLM(history, compactionIndex); |
| 514 | |
| 515 | // Every message in result should exist in original history |
| 516 | result.forEach((msg) => { |
| 517 | expect(history).toContainEqual(msg); |
| 518 | }); |
| 519 | |
| 520 | // Result length should be <= original length |
| 521 | expect(result.length).toBeLessThanOrEqual(history.length); |
| 522 | }); |
| 523 | }); |
| 524 | |
| 525 | it("getHistoryForLLM should always include system message if it exists at index 0", () => { |
| 526 | const systemMessage: ChatCompletionMessageParam = { |
| 527 | role: "system", |
| 528 | content: "System", |
| 529 | }; |
| 530 | const testCases: Array<{ |
| 531 | history: ChatCompletionMessageParam[]; |
| 532 | compactionIndex: number | null; |
| 533 | }> = [ |
| 534 | { |
| 535 | history: [ |
| 536 | systemMessage, |
| 537 | { role: "user", content: "User 1" }, |
| 538 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 539 | ], |
| 540 | compactionIndex: 2, |
| 541 | }, |
| 542 | { |
| 543 | history: [ |
| 544 | systemMessage, |
| 545 | { role: "assistant", content: `${COMPACTION_MARKER}\nSummary` }, |
| 546 | ], |
| 547 | compactionIndex: 1, |
| 548 | }, |
| 549 | ]; |
| 550 | |
| 551 | testCases.forEach(({ history, compactionIndex }) => { |
| 552 | const result = getHistoryForLLM(history, compactionIndex); |
| 553 | |
| 554 | if ( |
| 555 | history[0]?.role === "system" && |
| 556 | compactionIndex !== null && |
| 557 | compactionIndex > 0 |
| 558 | ) { |
| 559 | expect(result[0]).toEqual(systemMessage); |
| 560 | } |
| 561 | }); |
| 562 | }); |
| 563 | |
| 564 | it("compaction should always reduce message count for non-trivial histories", async () => { |
| 565 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 566 | |
| 567 | mockStreamResponse.mockImplementation( |
| 568 | async (history, model, api, controller, callbacks) => { |
| 569 | callbacks?.onContent?.("Summary"); |
| 570 | callbacks?.onContentComplete?.("Summary"); |
| 571 | return "Summary"; |
| 572 | }, |
| 573 | ); |
| 574 | |
| 575 | const testHistories: ChatCompletionMessageParam[][] = [ |
| 576 | [ |
| 577 | { role: "system", content: "System" }, |
| 578 | { role: "user", content: "User 1" }, |
| 579 | { role: "assistant", content: "Assistant 1" }, |
| 580 | { role: "user", content: "User 2" }, |
| 581 | { role: "assistant", content: "Assistant 2" }, |
| 582 | ], |
| 583 | [ |
| 584 | { role: "user", content: "User 1" }, |
| 585 | { role: "assistant", content: "Assistant 1" }, |
| 586 | { role: "user", content: "User 2" }, |
| 587 | ], |
| 588 | ]; |
| 589 | |
| 590 | for (const history of testHistories) { |
| 591 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 592 | |
| 593 | // Compacted history should be shorter than original |
| 594 | // (system + compaction) or just compaction |
| 595 | expect(result.compactedHistory.length).toBeLessThanOrEqual(2); |
| 596 | expect(result.compactedHistory.length).toBeLessThan(history.length); |
| 597 | } |
| 598 | }); |
| 599 | }); |
| 600 | |
| 601 | describe("invariant tests", () => { |
| 602 | it("compactionIndex should always point to a message with COMPACTION_MARKER", async () => { |
| 603 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 604 | |
| 605 | mockStreamResponse.mockImplementation( |
| 606 | async (history, model, api, controller, callbacks) => { |
| 607 | callbacks?.onContent?.("Summary content"); |
| 608 | callbacks?.onContentComplete?.("Summary content"); |
| 609 | return "Summary content"; |
| 610 | }, |
| 611 | ); |
| 612 | |
| 613 | const histories: ChatCompletionMessageParam[][] = [ |
| 614 | [ |
| 615 | { role: "system", content: "System" }, |
| 616 | { role: "user", content: "Hello" }, |
| 617 | ], |
| 618 | [ |
| 619 | { role: "user", content: "Hello" }, |
| 620 | { role: "assistant", content: "Hi" }, |
| 621 | ], |
| 622 | [{ role: "system", content: "System" }], |
| 623 | ]; |
| 624 | |
| 625 | for (const history of histories) { |
| 626 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 627 | |
| 628 | // The message at compactionIndex should contain the marker |
| 629 | const compactionMessage = |
| 630 | result.compactedHistory[result.compactionIndex]; |
| 631 | expect(compactionMessage.role).toBe("assistant"); |
| 632 | expect( |
| 633 | typeof compactionMessage.content === "string" && |
| 634 | compactionMessage.content.startsWith(COMPACTION_MARKER), |
| 635 | ).toBe(true); |
| 636 | } |
| 637 | }); |
| 638 | |
| 639 | it("system message should always be preserved in the same position", async () => { |
| 640 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 641 | |
| 642 | mockStreamResponse.mockImplementation( |
| 643 | async (history, model, api, controller, callbacks) => { |
| 644 | callbacks?.onContent?.("Summary"); |
| 645 | callbacks?.onContentComplete?.("Summary"); |
| 646 | return "Summary"; |
| 647 | }, |
| 648 | ); |
| 649 | |
| 650 | const systemMessage: ChatCompletionMessageParam = { |
| 651 | role: "system", |
| 652 | content: "You are helpful", |
| 653 | }; |
| 654 | const history: ChatCompletionMessageParam[] = [ |
| 655 | systemMessage, |
| 656 | { role: "user", content: "Hello" }, |
| 657 | { role: "assistant", content: "Hi" }, |
| 658 | ]; |
| 659 | |
| 660 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 661 | |
| 662 | // System message should still be at index 0 |
| 663 | expect(result.compactedHistory[0]).toEqual(systemMessage); |
| 664 | }); |
| 665 | |
| 666 | it("findCompactionIndex should be consistent with compactChatHistory result", async () => { |
| 667 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 668 | |
| 669 | mockStreamResponse.mockImplementation( |
| 670 | async (history, model, api, controller, callbacks) => { |
| 671 | callbacks?.onContent?.("Summary"); |
| 672 | callbacks?.onContentComplete?.("Summary"); |
| 673 | return "Summary"; |
| 674 | }, |
| 675 | ); |
| 676 | |
| 677 | const history: ChatCompletionMessageParam[] = [ |
| 678 | { role: "user", content: "Hello" }, |
| 679 | { role: "assistant", content: "Hi" }, |
| 680 | ]; |
| 681 | |
| 682 | const result = await compactChatHistory(history, mockModel, mockLlmApi); |
| 683 | |
| 684 | // findCompactionIndex should find the same index |
| 685 | const foundIndex = findCompactionIndex(result.compactedHistory); |
| 686 | expect(foundIndex).toBe(result.compactionIndex); |
| 687 | }); |
| 688 | }); |
| 689 | }); |