anthropic-mid-conversation-system-wire.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createAnthropicCompatFetch } from "../src/model/anthropic-stream-compat.js";
  3. describe("Anthropic mid-conversation system wire shape", () => {
  4. it("restores a single plain text system block to string without changing other surfaces", async () => {
  5. const baseFetch = vi.fn(async () =>
  6. new Response("{}", { headers: { "content-type": "application/json" }, status: 400 }),
  7. );
  8. const fetch = createAnthropicCompatFetch(baseFetch as typeof globalThis.fetch);
  9. const topLevelSystem = [
  10. {
  11. cache_control: { type: "ephemeral" },
  12. text: "stable system prompt",
  13. type: "text",
  14. },
  15. ];
  16. const userContent = [{ text: "user prompt", type: "text" }];
  17. const cachedSystemContent = [
  18. {
  19. cache_control: { type: "ephemeral" },
  20. text: "cached system text",
  21. type: "text",
  22. },
  23. ];
  24. await fetch("https://provider.test/v1/messages", {
  25. body: JSON.stringify({
  26. messages: [
  27. { content: userContent, role: "user" },
  28. {
  29. content: [{ text: "runtime reminder", type: "text" }],
  30. role: "system",
  31. },
  32. { content: cachedSystemContent, role: "system" },
  33. ],
  34. system: topLevelSystem,
  35. }),
  36. method: "POST",
  37. });
  38. const requestInit = baseFetch.mock.calls[0]?.[1];
  39. const body = JSON.parse(String(requestInit?.body)) as Record<string, unknown>;
  40. expect(body.system).toEqual(topLevelSystem);
  41. expect(body.messages).toEqual([
  42. { content: userContent, role: "user" },
  43. { content: "runtime reminder", role: "system" },
  44. { content: cachedSystemContent, role: "system" },
  45. ]);
  46. });
  47. });