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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.each([256, 20_000, 64_000])(
  50. "preserves the final request budget %s without subtracting thinking",
  51. async (budget) => {
  52. const send = vi.fn(
  53. async (_input: RequestInfo | URL, _init?: RequestInit) => new Response("{}"),
  54. );
  55. const fetch = createModelOptionMapFetch({
  56. fetch: send,
  57. maps: compileModelOptionMaps({
  58. reasoningLevel: { map: "{}" },
  59. maxOutputTokens: { map: "{'max_tokens': maxOutputTokens}" },
  60. }),
  61. values: { reasoningLevel: "high", maxOutputTokens: budget },
  62. });
  63. await fetch("https://example.com", {
  64. method: "POST",
  65. body: JSON.stringify({ max_tokens: 1, thinking: { type: "enabled", budget_tokens: 8000 } }),
  66. });
  67. expect(JSON.parse(String(send.mock.calls[0]?.[1]?.body))).toEqual({
  68. max_tokens: budget,
  69. thinking: { type: "enabled", budget_tokens: 8000 },
  70. });
  71. },
  72. );
  73. it("does not mutate or reuse a previous request body", async () => {
  74. const bodies: unknown[] = [];
  75. const fetch = createModelOptionMapFetch({
  76. fetch: async (_input, init) => {
  77. bodies.push(JSON.parse(String(init?.body)));
  78. return new Response("{}");
  79. },
  80. maps: compileModelOptionMaps({
  81. reasoningLevel: { map: "{}" },
  82. maxOutputTokens: { map: "{'max_tokens': maxOutputTokens}" },
  83. }),
  84. values: { reasoningLevel: "disabled", maxOutputTokens: 7 },
  85. });
  86. await fetch("https://example.com", { method: "POST", body: '{"model":"a"}' });
  87. await fetch("https://example.com", { method: "POST", body: '{"model":"b"}' });
  88. expect(bodies).toEqual([
  89. { model: "a", max_tokens: 7 },
  90. { model: "b", max_tokens: 7 },
  91. ]);
  92. });
  93. it("makes the option map authoritative over SDK-produced option fields", async () => {
  94. let sentBody: unknown;
  95. const fetch = createModelOptionMapFetch({
  96. fetch: async (_input, init) => {
  97. sentBody = JSON.parse(String(init?.body));
  98. return new Response("{}");
  99. },
  100. maps: compileModelOptionMaps({
  101. reasoningLevel: { map: "{'reasoning_effort': reasoningLevel}" },
  102. maxOutputTokens: { map: "{'max_completion_tokens': maxOutputTokens}" },
  103. }),
  104. values: { reasoningLevel: "low", maxOutputTokens: 32000 },
  105. });
  106. await fetch("https://example.com", {
  107. method: "POST",
  108. body: JSON.stringify({
  109. model: "example",
  110. reasoning_effort: "high",
  111. max_completion_tokens: 1,
  112. }),
  113. });
  114. expect(sentBody).toEqual({
  115. model: "example",
  116. reasoning_effort: "low",
  117. max_completion_tokens: 32000,
  118. });
  119. });
  120. it("fails closed when a model request body is not JSON text", async () => {
  121. const transport = vi.fn(async () => new Response("{}"));
  122. const fetch = createModelOptionMapFetch({
  123. fetch: transport,
  124. maps: compileModelOptionMaps({
  125. reasoningLevel: { map: "{}" },
  126. maxOutputTokens: { map: "{'max_tokens': maxOutputTokens}" },
  127. }),
  128. values: { reasoningLevel: "disabled", maxOutputTokens: 7 },
  129. });
  130. await expect(
  131. fetch("https://example.com", {
  132. method: "POST",
  133. body: new Uint8Array([123, 125]),
  134. }),
  135. ).rejects.toThrow("JSON text request body");
  136. expect(transport).not.toHaveBeenCalled();
  137. });
  138. });