openai-compatible-system-messages-wire.test.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
  2. import { generateText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import { createGenerateTextOptions } from "../src/model/runner-options.js";
  5. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  6. import { createTestModelProperties } from "./test-model-format.js";
  7. describe("OpenAI-compatible system message wire shape", () => {
  8. it.each([
  9. {
  10. expectedSystem: "cli prefix\nstable context\n\ndynamic context",
  11. name: "concatenates self-delimited Main blocks without adding separators",
  12. systemMessages: [
  13. { role: "system", content: "cli prefix" },
  14. { role: "system", content: "\nstable context" },
  15. { role: "system", content: "\n\ndynamic context" },
  16. ],
  17. },
  18. ] as const)("$name", async ({ expectedSystem, systemMessages }) => {
  19. let capturedBody: Record<string, unknown> | undefined;
  20. const provider = createOpenAICompatible({
  21. apiKey: "fake-ak",
  22. baseURL: "https://api.example.test/v1",
  23. name: "Compatible System E2E",
  24. fetch: async (_input, init) => {
  25. if (typeof init?.body !== "string") {
  26. throw new Error("Expected a JSON request body");
  27. }
  28. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  29. return new Response(
  30. JSON.stringify({
  31. choices: [{ finish_reason: "stop", message: { content: "ok", role: "assistant" } }],
  32. id: "chatcmpl-system-merge",
  33. model: "legacy-chat-template",
  34. }),
  35. { headers: { "content-type": "application/json" } },
  36. );
  37. },
  38. });
  39. const options = createGenerateTextOptions({
  40. includeModelIO: false,
  41. request: {
  42. messages: [
  43. ...systemMessages,
  44. { role: "user", content: "first question" },
  45. { role: "assistant", content: "first answer" },
  46. { role: "user", content: "follow-up" },
  47. ],
  48. },
  49. resolved: {
  50. properties: createTestModelProperties(),
  51. baseURL: "https://api.example.test/v1",
  52. model: provider("legacy-chat-template"),
  53. providerKind: "openai-compatible",
  54. providerId: "legacy-compatible",
  55. modelId: "legacy-chat-template",
  56. } as ResolvedAiSdkModel,
  57. statusContext: {
  58. providerId: "legacy-compatible",
  59. modelId: "legacy-chat-template",
  60. providerKind: "openai-compatible",
  61. requestId: "req_system_merge_wire",
  62. sessionId: "sess_system_merge_wire",
  63. traceId: "trace_system_merge_wire",
  64. } as never,
  65. });
  66. await generateText(options);
  67. expect(capturedBody?.messages).toEqual([
  68. {
  69. role: "system",
  70. content: expectedSystem,
  71. },
  72. { role: "user", content: "first question" },
  73. { role: "assistant", content: "first answer" },
  74. { role: "user", content: "follow-up" },
  75. ]);
  76. });
  77. });