model-option-map-fetch.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { describe, expect, it, vi } from "vitest";
  2. import { compileModelOptionMaps } from "@zcode/model-option-map";
  3. import { createModelOptionMapFetch } from "../src/model/model-option-map-fetch.js";
  4. describe("createModelOptionMapFetch", () => {
  5. it.each([
  6. {
  7. name: "Anthropic Messages",
  8. reasoningMap:
  9. "reasoningLevel == 'off' ? {'thinking': {'type': 'disabled'}} : {'thinking': {'type': 'enabled'}}",
  10. maxMap: "{'max_tokens': maxOutputTokens}",
  11. expected: { thinking: { type: "enabled" }, max_tokens: 32000 },
  12. },
  13. {
  14. name: "OpenAI Chat Completions",
  15. reasoningMap: "{'reasoning_effort': reasoningLevel}",
  16. maxMap: "{'max_completion_tokens': maxOutputTokens}",
  17. expected: { reasoning_effort: "high", max_completion_tokens: 32000 },
  18. },
  19. {
  20. name: "OpenAI Responses",
  21. reasoningMap: "{'reasoning': {'effort': reasoningLevel}}",
  22. maxMap: "{'max_output_tokens': maxOutputTokens}",
  23. expected: { reasoning: { effort: "high" }, max_output_tokens: 32000 },
  24. },
  25. ])("patches the final $name raw JSON body", async ({ reasoningMap, maxMap, expected }) => {
  26. let sentBody: unknown;
  27. const transport = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => {
  28. sentBody = JSON.parse(String(init?.body));
  29. return new Response("{}", { status: 200 });
  30. });
  31. const capture: { body?: Record<string, unknown> } = {};
  32. const fetch = createModelOptionMapFetch({
  33. capture,
  34. fetch: transport,
  35. maps: compileModelOptionMaps({
  36. reasoningLevel: { map: reasoningMap },
  37. maxOutputTokens: { map: maxMap },
  38. }),
  39. values: { reasoningLevel: "high", maxOutputTokens: 32000 },
  40. });
  41. await fetch("https://example.com", {
  42. method: "POST",
  43. headers: { "content-type": "application/json" },
  44. body: JSON.stringify({ model: "example", untouched: true }),
  45. });
  46. expect(sentBody).toEqual({ model: "example", untouched: true, ...expected });
  47. expect(capture.body).toEqual(sentBody);
  48. });
  49. it("does not mutate or reuse a previous request body", async () => {
  50. const bodies: unknown[] = [];
  51. const fetch = createModelOptionMapFetch({
  52. fetch: async (_input, init) => {
  53. bodies.push(JSON.parse(String(init?.body)));
  54. return new Response("{}");
  55. },
  56. maps: compileModelOptionMaps({
  57. reasoningLevel: { map: "{}" },
  58. maxOutputTokens: { map: "{'max_tokens': maxOutputTokens}" },
  59. }),
  60. values: { reasoningLevel: "disabled", maxOutputTokens: 7 },
  61. });
  62. await fetch("https://example.com", { method: "POST", body: '{"model":"a"}' });
  63. await fetch("https://example.com", { method: "POST", body: '{"model":"b"}' });
  64. expect(bodies).toEqual([
  65. { model: "a", max_tokens: 7 },
  66. { model: "b", max_tokens: 7 },
  67. ]);
  68. });
  69. it("makes the option map authoritative over SDK-produced option fields", async () => {
  70. let sentBody: unknown;
  71. const fetch = createModelOptionMapFetch({
  72. fetch: async (_input, init) => {
  73. sentBody = JSON.parse(String(init?.body));
  74. return new Response("{}");
  75. },
  76. maps: compileModelOptionMaps({
  77. reasoningLevel: { map: "{'reasoning_effort': reasoningLevel}" },
  78. maxOutputTokens: { map: "{'max_completion_tokens': maxOutputTokens}" },
  79. }),
  80. values: { reasoningLevel: "low", maxOutputTokens: 32000 },
  81. });
  82. await fetch("https://example.com", {
  83. method: "POST",
  84. body: JSON.stringify({
  85. model: "example",
  86. reasoning_effort: "high",
  87. max_completion_tokens: 1,
  88. }),
  89. });
  90. expect(sentBody).toEqual({
  91. model: "example",
  92. reasoning_effort: "low",
  93. max_completion_tokens: 32000,
  94. });
  95. });
  96. it("fails closed when a model request body is not JSON text", async () => {
  97. const transport = vi.fn(async () => new Response("{}"));
  98. const fetch = createModelOptionMapFetch({
  99. fetch: transport,
  100. maps: compileModelOptionMaps({
  101. reasoningLevel: { map: "{}" },
  102. maxOutputTokens: { map: "{'max_tokens': maxOutputTokens}" },
  103. }),
  104. values: { reasoningLevel: "disabled", maxOutputTokens: 7 },
  105. });
  106. await expect(
  107. fetch("https://example.com", {
  108. method: "POST",
  109. body: new Uint8Array([123, 125]),
  110. }),
  111. ).rejects.toThrow("JSON text request body");
  112. expect(transport).not.toHaveBeenCalled();
  113. });
  114. });