|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +import { prepareApiConversationMessage } from "../apiConversationHistory.js" |
| 4 | + |
| 5 | +describe("prepareApiConversationMessage", () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers() |
| 8 | + vi.setSystemTime(new Date("2026-01-02T03:04:05.000Z")) |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + vi.useRealTimers() |
| 13 | + }) |
| 14 | + |
| 15 | + it("prepends Anthropic thinking blocks with thought signatures", () => { |
| 16 | + const result = prepareApiConversationMessage({ |
| 17 | + message: { role: "assistant", content: "answer" }, |
| 18 | + reasoning: "private reasoning", |
| 19 | + api: { |
| 20 | + getResponseId: () => "response-1", |
| 21 | + getThoughtSignature: () => "signature-1", |
| 22 | + } as any, |
| 23 | + apiConfiguration: { apiProvider: "anthropic", apiModelId: "claude-3-5-sonnet" } as any, |
| 24 | + apiConversationHistory: [], |
| 25 | + }) as any |
| 26 | + |
| 27 | + expect(result).toMatchObject({ |
| 28 | + role: "assistant", |
| 29 | + id: "response-1", |
| 30 | + ts: Date.now(), |
| 31 | + }) |
| 32 | + expect(result.content).toEqual([ |
| 33 | + { type: "thinking", thinking: "private reasoning", signature: "signature-1" }, |
| 34 | + { type: "text", text: "answer" }, |
| 35 | + ]) |
| 36 | + }) |
| 37 | + |
| 38 | + it("prepends non-Anthropic reasoning blocks without dead summary fallback", () => { |
| 39 | + const result = prepareApiConversationMessage({ |
| 40 | + message: { role: "assistant", content: "answer" }, |
| 41 | + reasoning: "visible reasoning", |
| 42 | + api: {} as any, |
| 43 | + apiConfiguration: { apiProvider: "openrouter", openRouterModelId: "openai/gpt-4" } as any, |
| 44 | + apiConversationHistory: [], |
| 45 | + }) as any |
| 46 | + |
| 47 | + expect(result.content).toEqual([ |
| 48 | + { type: "reasoning", text: "visible reasoning" }, |
| 49 | + { type: "text", text: "answer" }, |
| 50 | + ]) |
| 51 | + expect(result.content[0]).not.toHaveProperty("summary") |
| 52 | + }) |
| 53 | + |
| 54 | + it("preserves encrypted reasoning content", () => { |
| 55 | + const result = prepareApiConversationMessage({ |
| 56 | + message: { role: "assistant", content: [{ type: "text", text: "answer" }] }, |
| 57 | + api: { |
| 58 | + getEncryptedContent: () => ({ encrypted_content: "encrypted", id: "reasoning-1" }), |
| 59 | + } as any, |
| 60 | + apiConfiguration: { apiProvider: "openrouter", openRouterModelId: "openai/gpt-4" } as any, |
| 61 | + apiConversationHistory: [], |
| 62 | + }) as any |
| 63 | + |
| 64 | + expect(result.content).toEqual([ |
| 65 | + { type: "reasoning", summary: [], encrypted_content: "encrypted", id: "reasoning-1" }, |
| 66 | + { type: "text", text: "answer" }, |
| 67 | + ]) |
| 68 | + }) |
| 69 | + |
| 70 | + it("appends thought signatures for non-Anthropic protocols", () => { |
| 71 | + const result = prepareApiConversationMessage({ |
| 72 | + message: { role: "assistant", content: "answer" }, |
| 73 | + api: { |
| 74 | + getThoughtSignature: () => "signature-1", |
| 75 | + getReasoningDetails: () => [{ type: "reasoning", text: "detail" }], |
| 76 | + } as any, |
| 77 | + apiConfiguration: { apiProvider: "openrouter", openRouterModelId: "openai/gpt-4" } as any, |
| 78 | + apiConversationHistory: [], |
| 79 | + }) as any |
| 80 | + |
| 81 | + expect(result.reasoning_details).toEqual([{ type: "reasoning", text: "detail" }]) |
| 82 | + expect(result.content).toEqual([ |
| 83 | + { type: "text", text: "answer" }, |
| 84 | + { type: "thoughtSignature", thoughtSignature: "signature-1" }, |
| 85 | + ]) |
| 86 | + }) |
| 87 | + |
| 88 | + it("validates user tool_result blocks against the last effective assistant message", () => { |
| 89 | + const result = prepareApiConversationMessage({ |
| 90 | + message: { |
| 91 | + role: "user", |
| 92 | + content: [{ type: "tool_result", tool_use_id: "wrong-id", content: "done" }], |
| 93 | + }, |
| 94 | + api: {} as any, |
| 95 | + apiConfiguration: { apiProvider: "openrouter", openRouterModelId: "openai/gpt-4" } as any, |
| 96 | + apiConversationHistory: [ |
| 97 | + { |
| 98 | + role: "assistant", |
| 99 | + content: [{ type: "tool_use", id: "tool-1", name: "read_file", input: {} }], |
| 100 | + } as any, |
| 101 | + ], |
| 102 | + }) as any |
| 103 | + |
| 104 | + expect(result.content).toEqual([{ type: "tool_result", tool_use_id: "tool-1", content: "done" }]) |
| 105 | + expect(result.ts).toBe(Date.now()) |
| 106 | + }) |
| 107 | +}) |
0 commit comments