|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { Context, Model } from "../types.js"; |
| 3 | +import { isContextOverflow } from "../utils/overflow.js"; |
| 4 | +import { streamAnthropic, streamSimpleAnthropic } from "./anthropic.js"; |
| 5 | + |
| 6 | +const apiKey = process.env.ANTHROPIC_API_KEY?.trim() ?? ""; |
| 7 | +const live = process.env.OPENCLAW_LIVE_TEST === "1" && apiKey.length > 0; |
| 8 | +const describeLive = live ? describe : describe.skip; |
| 9 | +const timeoutMs = 120_000; |
| 10 | + |
| 11 | +const model: Model<"anthropic-messages"> = { |
| 12 | + id: "claude-haiku-4-5", |
| 13 | + name: "Claude Haiku 4.5", |
| 14 | + api: "anthropic-messages", |
| 15 | + provider: "anthropic", |
| 16 | + baseUrl: "https://api.anthropic.com", |
| 17 | + reasoning: true, |
| 18 | + input: ["text"], |
| 19 | + cost: { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 }, |
| 20 | + contextWindow: 200_000, |
| 21 | + maxTokens: 8_192, |
| 22 | +}; |
| 23 | + |
| 24 | +describeLive("Anthropic provider live", () => { |
| 25 | + it( |
| 26 | + "streams a basic response with usage", |
| 27 | + async () => { |
| 28 | + const result = await streamSimpleAnthropic( |
| 29 | + model, |
| 30 | + { messages: [{ role: "user", content: "Reply with the single word ok.", timestamp: 0 }] }, |
| 31 | + { apiKey, maxTokens: 32, reasoning: "off" }, |
| 32 | + ).result(); |
| 33 | + |
| 34 | + expect(result.stopReason).toBe("stop"); |
| 35 | + expect(result.usage.output).toBeGreaterThan(0); |
| 36 | + }, |
| 37 | + timeoutMs, |
| 38 | + ); |
| 39 | + |
| 40 | + it( |
| 41 | + "parses long-retention cache-write usage", |
| 42 | + async () => { |
| 43 | + const context: Context = { |
| 44 | + systemPrompt: "Stable cacheable provider context. ".repeat(2_000), |
| 45 | + messages: [{ role: "user", content: "Reply briefly with ok.", timestamp: 0 }], |
| 46 | + }; |
| 47 | + const result = await streamSimpleAnthropic(model, context, { |
| 48 | + apiKey, |
| 49 | + cacheRetention: "long", |
| 50 | + maxTokens: 32, |
| 51 | + reasoning: "off", |
| 52 | + }).result(); |
| 53 | + |
| 54 | + expect(result.stopReason).toBe("stop"); |
| 55 | + expect(result.usage.cacheWrite).toBeGreaterThanOrEqual(0); |
| 56 | + if (result.usage.cacheWrite1h !== undefined) { |
| 57 | + expect(result.usage.cacheWrite1h).toBeGreaterThanOrEqual(0); |
| 58 | + expect(Number.isFinite(result.usage.cost.cacheWrite)).toBe(true); |
| 59 | + } |
| 60 | + }, |
| 61 | + timeoutMs, |
| 62 | + ); |
| 63 | + |
| 64 | + it( |
| 65 | + "keeps the signature on a streamed thinking block", |
| 66 | + async () => { |
| 67 | + const result = await streamSimpleAnthropic( |
| 68 | + model, |
| 69 | + { |
| 70 | + messages: [ |
| 71 | + { |
| 72 | + role: "user", |
| 73 | + content: "Think through whether 17 is prime, then answer in one sentence.", |
| 74 | + timestamp: 0, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + { apiKey, maxTokens: 128, reasoning: "low" }, |
| 79 | + ).result(); |
| 80 | + |
| 81 | + expect(result.stopReason).toBe("stop"); |
| 82 | + const thinking = result.content.find((block) => block.type === "thinking"); |
| 83 | + expect(thinking?.thinkingSignature?.length).toBeGreaterThan(0); |
| 84 | + }, |
| 85 | + timeoutMs, |
| 86 | + ); |
| 87 | + |
| 88 | + it( |
| 89 | + "classifies the provider's current context-overflow error", |
| 90 | + async () => { |
| 91 | + const result = await streamAnthropic( |
| 92 | + model, |
| 93 | + { |
| 94 | + messages: [ |
| 95 | + { |
| 96 | + role: "user", |
| 97 | + content: "x ".repeat(Math.ceil(model.contextWindow * 1.1)), |
| 98 | + timestamp: 0, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }, |
| 102 | + { apiKey, maxTokens: 1, maxRetries: 0 }, |
| 103 | + ).result(); |
| 104 | + |
| 105 | + expect(result.stopReason).toBe("error"); |
| 106 | + expect(isContextOverflow(result)).toBe(true); |
| 107 | + }, |
| 108 | + timeoutMs, |
| 109 | + ); |
| 110 | + |
| 111 | + it( |
| 112 | + "clamps an excessive output request to the model limit", |
| 113 | + async () => { |
| 114 | + let sentMaxTokens: number | undefined; |
| 115 | + const context: Context = { |
| 116 | + messages: [{ role: "user", content: "Reply briefly with ok.", timestamp: 0 }], |
| 117 | + }; |
| 118 | + const requestedMaxTokens = model.maxTokens * 100; |
| 119 | + |
| 120 | + const result = await streamSimpleAnthropic(model, context, { |
| 121 | + apiKey, |
| 122 | + maxTokens: requestedMaxTokens, |
| 123 | + maxRetries: 0, |
| 124 | + reasoning: "off", |
| 125 | + onPayload: (payload) => { |
| 126 | + sentMaxTokens = (payload as { max_tokens?: number }).max_tokens; |
| 127 | + }, |
| 128 | + }).result(); |
| 129 | + |
| 130 | + expect(sentMaxTokens).toBe(model.maxTokens); |
| 131 | + expect(result.errorMessage).toBeUndefined(); |
| 132 | + expect(["stop", "length"]).toContain(result.stopReason); |
| 133 | + }, |
| 134 | + timeoutMs, |
| 135 | + ); |
| 136 | +}); |
0 commit comments