native-system-message-boundaries-wire.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { createOpenAI } from "@ai-sdk/openai";
  3. import { generateText } from "ai";
  4. import { describe, expect, it } from "vitest";
  5. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  6. import type { AiSdkProviderKind } from "../src/model/model-execution.js";
  7. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  8. import { createTestModelProperties } from "./test-model-format.js";
  9. const SYSTEM_CONTENTS = ["cli prefix", "\nstable context", "\n\ndynamic context"] as const;
  10. describe("native provider system message boundaries", () => {
  11. it("preserves each Anthropic system block exactly", async () => {
  12. const capture = createRequestCapture();
  13. const provider = createAnthropic({
  14. apiKey: "fake-key",
  15. baseURL: "https://api.example.test",
  16. fetch: capture.fetch,
  17. });
  18. await captureRejectedRequest({
  19. model: provider("claude-opus-4-8"),
  20. modelId: "claude-opus-4-8",
  21. providerKind: "anthropic",
  22. systemContents: SYSTEM_CONTENTS,
  23. });
  24. const system = capture.body().system as Array<{ text: string; type: string }>;
  25. expect(system).toHaveLength(SYSTEM_CONTENTS.length);
  26. expect(system.map((block) => block.text)).toEqual(SYSTEM_CONTENTS);
  27. });
  28. it("preserves each OpenAI Responses developer message exactly", async () => {
  29. const capture = createRequestCapture();
  30. const provider = createOpenAI({
  31. apiKey: "fake-key",
  32. baseURL: "https://api.example.test/v1",
  33. fetch: capture.fetch,
  34. });
  35. await captureRejectedRequest({
  36. model: provider.responses("gpt-5.2"),
  37. modelId: "gpt-5.2",
  38. providerKind: "openai",
  39. systemContents: SYSTEM_CONTENTS,
  40. });
  41. expect((capture.body().input as unknown[]).slice(0, SYSTEM_CONTENTS.length)).toEqual(
  42. SYSTEM_CONTENTS.map((content) => ({ content, role: "developer" })),
  43. );
  44. });
  45. });
  46. async function captureRejectedRequest(input: {
  47. model: ResolvedAiSdkModel["model"];
  48. modelId: string;
  49. providerKind: AiSdkProviderKind;
  50. systemContents: readonly string[];
  51. }): Promise<void> {
  52. const options = createGenerateTextOptions({
  53. includeModelIO: false,
  54. request: {
  55. messages: [
  56. ...input.systemContents.map((content) => ({ content, role: "system" as const })),
  57. { content: "question", role: "user" },
  58. ],
  59. ...(input.providerKind === "openai" ? { providerOptions: { openai: { store: false } } } : {}),
  60. },
  61. resolved: {
  62. properties: createTestModelProperties(),
  63. model: input.model,
  64. providerKind: input.providerKind,
  65. providerId: "wire-provider",
  66. modelId: input.modelId,
  67. } as ResolvedAiSdkModel,
  68. statusContext: {
  69. providerId: "wire-provider",
  70. modelId: input.modelId,
  71. providerKind: input.providerKind,
  72. requestId: "req_system_boundaries_wire",
  73. sessionId: "sess_system_boundaries_wire",
  74. traceId: "trace_system_boundaries_wire",
  75. } as never,
  76. });
  77. await expect(generateText(options)).rejects.toBeDefined();
  78. }
  79. function createRequestCapture(): {
  80. body: () => Record<string, unknown>;
  81. fetch: typeof globalThis.fetch;
  82. } {
  83. let capturedBody: Record<string, unknown> | undefined;
  84. const fetch: typeof globalThis.fetch = async (_input, init) => {
  85. if (typeof init?.body !== "string") {
  86. throw new Error("Expected a JSON request body");
  87. }
  88. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  89. return new Response(JSON.stringify({ error: { message: "wire capture", type: "test" } }), {
  90. headers: { "content-type": "application/json" },
  91. status: 400,
  92. });
  93. };
  94. return {
  95. body() {
  96. if (!capturedBody) {
  97. throw new Error("Expected the provider request body to be captured");
  98. }
  99. return capturedBody;
  100. },
  101. fetch,
  102. };
  103. }