anthropic-opus-4-8-smoke.test.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { streamSimple } from "../src/stream.ts";
  4. import type { Context } from "../src/types.ts";
  5. interface AnthropicThinkingPayload {
  6. thinking?: { type: string };
  7. output_config?: { effort?: string };
  8. }
  9. function makeContext(): Context {
  10. return {
  11. systemPrompt: "You are a precise assistant. Follow the user's instructions exactly.",
  12. messages: [
  13. {
  14. role: "user",
  15. content:
  16. "Compute 48291 * 7317 and 90844 - 17729, add the results, and determine whether the sum is divisible by 11. Reply with exactly this format and nothing else: sum=<sum>; divisibleBy11=<yes|no>",
  17. timestamp: Date.now(),
  18. },
  19. ],
  20. };
  21. }
  22. describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic Opus 4.8 smoke", () => {
  23. it("streams Claude Opus 4.8 with reasoning enabled", { retry: 2, timeout: 30000 }, async () => {
  24. const model = getModel("anthropic", "claude-opus-4-8");
  25. let capturedPayload: AnthropicThinkingPayload | undefined;
  26. const s = streamSimple(model, makeContext(), {
  27. reasoning: "high",
  28. maxTokens: 1024,
  29. onPayload: (payload) => {
  30. capturedPayload = payload as AnthropicThinkingPayload;
  31. return payload;
  32. },
  33. });
  34. let sawThinking = false;
  35. for await (const event of s) {
  36. if (event.type === "thinking_start" || event.type === "thinking_delta" || event.type === "thinking_end") {
  37. sawThinking = true;
  38. }
  39. }
  40. const response = await s.result();
  41. expect(response.stopReason, response.errorMessage).toBe("stop");
  42. expect(response.errorMessage).toBeFalsy();
  43. expect(capturedPayload?.thinking).toEqual({ type: "adaptive" });
  44. expect(capturedPayload?.output_config).toEqual({ effort: "high" });
  45. expect(sawThinking).toBe(true);
  46. const thinkingBlock = response.content.find((block) => block.type === "thinking");
  47. expect(thinkingBlock?.type).toBe("thinking");
  48. if (!thinkingBlock || thinkingBlock.type !== "thinking") {
  49. throw new Error("Expected thinking block from Claude Opus 4.8");
  50. }
  51. expect(typeof thinkingBlock.thinkingSignature).toBe("string");
  52. const thinkingSignature = thinkingBlock.thinkingSignature;
  53. if (!thinkingSignature) {
  54. throw new Error("Expected thinking signature from Claude Opus 4.8");
  55. }
  56. expect(thinkingSignature.length).toBeGreaterThan(0);
  57. const text = response.content
  58. .filter((block) => block.type === "text")
  59. .map((block) => block.text)
  60. .join("")
  61. .trim();
  62. expect(text).toBe("sum=353418362; divisibleBy11=yes");
  63. });
  64. });