runner-options.test.ts 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { ModelErrorCode } from "@zcode/contracts";
  3. import { AiSdkModelAdapterError } from "../src/model/errors.js";
  4. import {
  5. createGenerateTextOptions,
  6. createStreamTextOptions,
  7. shouldIncludeStreamResponseBody,
  8. } from "../src/model/runner-options.js";
  9. import { isOpenCodeGoBaseUrl } from "../src/model/opencode-session.js";
  10. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  11. function resolved(partial: Partial<ResolvedAiSdkModel>): ResolvedAiSdkModel {
  12. return partial as ResolvedAiSdkModel;
  13. }
  14. describe("shouldIncludeStreamResponseBody", () => {
  15. afterEach(() => {
  16. vi.unstubAllEnvs();
  17. vi.resetModules();
  18. });
  19. it("enables responseBody capture for zcode-plan openai-compatible providers", () => {
  20. expect(
  21. shouldIncludeStreamResponseBody(
  22. resolved({
  23. providerKind: "openai-compatible",
  24. baseURL: "https://zcode.z.ai/api/v1/zcode-plan",
  25. }),
  26. ),
  27. ).toBe(true);
  28. });
  29. it("skips responseBody capture for unrelated providers", () => {
  30. expect(
  31. shouldIncludeStreamResponseBody(
  32. resolved({
  33. providerKind: "openai-compatible",
  34. baseURL: "https://api.openai.com/v1",
  35. }),
  36. ),
  37. ).toBe(false);
  38. });
  39. });
  40. describe("isOpenCodeGoBaseUrl", () => {
  41. it.each(["https://opencode.ai/zen/go/v1", "https://api.opencode.ai/zen/go/v1/"])(
  42. "matches OpenCode Go host and path boundaries: %s",
  43. (baseURL) => {
  44. expect(isOpenCodeGoBaseUrl(baseURL)).toBe(true);
  45. },
  46. );
  47. it.each([
  48. "https://opencode.ai/zen/v1",
  49. "https://opencode.ai/zen/go/v2",
  50. "https://opencode.ai.example/zen/go/v1",
  51. "not a url",
  52. undefined,
  53. ])("rejects unrelated OpenCode endpoints: %s", (baseURL) => {
  54. expect(isOpenCodeGoBaseUrl(baseURL)).toBe(false);
  55. });
  56. });
  57. describe("createStreamTextOptions", () => {
  58. afterEach(() => {
  59. vi.unstubAllEnvs();
  60. });
  61. it("maps a JSON response schema only into generateText structured output", async () => {
  62. const responseJsonSchema = {
  63. additionalProperties: false,
  64. properties: {
  65. selected_memories: {
  66. items: { type: "string" },
  67. type: "array",
  68. },
  69. },
  70. required: ["selected_memories"],
  71. type: "object",
  72. };
  73. const input = {
  74. includeModelIO: false,
  75. request: {
  76. messages: [{ role: "user" as const, content: "select" }],
  77. responseJsonSchema,
  78. },
  79. resolved: resolved({
  80. model: {} as ResolvedAiSdkModel["model"],
  81. ref: { providerId: "test-provider", modelId: "test-model" },
  82. providerKind: "custom",
  83. }),
  84. statusContext: {
  85. model: { providerId: "test-provider", modelId: "test-model" },
  86. providerKind: "custom",
  87. requestId: "req_structured_output",
  88. sessionId: "sess_structured_output",
  89. traceId: "trace_structured_output",
  90. } as never,
  91. };
  92. const generateOptions = createGenerateTextOptions(input);
  93. const streamOptions = createStreamTextOptions(input);
  94. expect(generateOptions.output).toBeDefined();
  95. await expect(generateOptions.output?.responseFormat).resolves.toEqual({
  96. schema: responseJsonSchema,
  97. type: "json",
  98. });
  99. expect(streamOptions).not.toHaveProperty("output");
  100. });
  101. it("keeps explicit generateText schemas on the native Anthropic output format", () => {
  102. const responseJsonSchema = {
  103. additionalProperties: false,
  104. properties: {
  105. selected_memories: { items: { type: "string" }, type: "array" },
  106. },
  107. required: ["selected_memories"],
  108. type: "object",
  109. };
  110. const input = {
  111. includeModelIO: false,
  112. request: {
  113. messages: [{ role: "user" as const, content: "select" }],
  114. responseJsonSchema,
  115. },
  116. resolved: resolved({
  117. model: {} as ResolvedAiSdkModel["model"],
  118. ref: { providerId: "test-provider", modelId: "lite-model" },
  119. providerKind: "anthropic",
  120. }),
  121. statusContext: {
  122. model: { providerId: "test-provider", modelId: "lite-model" },
  123. providerKind: "anthropic",
  124. requestId: "req_native_structured_output",
  125. sessionId: "sess_native_structured_output",
  126. traceId: "trace_native_structured_output",
  127. } as never,
  128. };
  129. const generateInput = {
  130. ...input,
  131. request: {
  132. ...input.request,
  133. maxOutputTokens: 256,
  134. providerOptions: { anthropic: {} },
  135. },
  136. resolved: {
  137. ...input.resolved,
  138. providerOptions: {
  139. anthropic: {
  140. effort: "medium",
  141. thinking: { budgetTokens: 8_000, type: "enabled" },
  142. },
  143. },
  144. },
  145. };
  146. const generateOptions = createGenerateTextOptions(generateInput);
  147. expect(generateOptions.maxOutputTokens).toBe(256);
  148. expect(generateOptions.providerOptions).toEqual({
  149. anthropic: { structuredOutputMode: "outputFormat" },
  150. });
  151. expect(createStreamTextOptions(input).providerOptions).toBeUndefined();
  152. });
  153. it("applies the same empty-user wire fallback to generate and stream requests", () => {
  154. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  155. const options = createOptions({
  156. includeModelIO: false,
  157. request: {
  158. messages: [{ role: "user", content: "" }],
  159. },
  160. resolved: resolved({
  161. model: {} as ResolvedAiSdkModel["model"],
  162. ref: { providerId: "custom-compatible", modelId: "empty-user-model" },
  163. providerKind: "openai-compatible",
  164. baseURL: "https://api.example.test/v1",
  165. }),
  166. statusContext: {
  167. model: { providerId: "custom-compatible", modelId: "empty-user-model" },
  168. providerKind: "openai-compatible",
  169. requestId: "req_empty_user",
  170. sessionId: "sess_empty_user",
  171. traceId: "trace_empty_user",
  172. } as never,
  173. });
  174. expect(options.messages).toEqual([{ role: "user", content: "(no content)" }]);
  175. }
  176. });
  177. it("passes resolved image support into generateText message transforms", () => {
  178. const options = createGenerateTextOptions({
  179. includeModelIO: false,
  180. request: {
  181. messages: [
  182. {
  183. role: "user",
  184. content: [
  185. {
  186. type: "image",
  187. mediaType: "image/png",
  188. dataUrl: "data:image/png;base64,aW1hZ2U=",
  189. },
  190. ],
  191. },
  192. ],
  193. },
  194. resolved: resolved({
  195. inputMediaCapabilities: { supportsImages: false },
  196. model: {} as ResolvedAiSdkModel["model"],
  197. ref: { providerId: "zai", modelId: "glm-text" },
  198. providerKind: "openai-compatible",
  199. }),
  200. statusContext: {
  201. model: { providerId: "zai", modelId: "glm-text" },
  202. providerKind: "openai-compatible",
  203. requestId: "req_text_only",
  204. sessionId: "sess_text_only",
  205. traceId: "trace_text_only",
  206. } as never,
  207. });
  208. expect((options.messages[0] as any).content).toEqual([
  209. {
  210. type: "text",
  211. text: "[Attached image/png]\n[Media omitted from provider request because the selected model does not support image input.]",
  212. },
  213. ]);
  214. });
  215. it("strips video before the OpenAI Responses adapter sees an unsupported file part", () => {
  216. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  217. const options = createOptions({
  218. includeModelIO: false,
  219. request: {
  220. messages: [
  221. {
  222. role: "user",
  223. content: [
  224. {
  225. type: "video",
  226. mediaType: "video/mp4",
  227. dataUrl: "data:video/mp4;base64,dmlkZW8=",
  228. },
  229. ],
  230. },
  231. ],
  232. },
  233. resolved: resolved({
  234. inputMediaCapabilities: { supportsVideo: true },
  235. model: {} as ResolvedAiSdkModel["model"],
  236. ref: { providerId: "openai", modelId: "gpt-test" },
  237. providerKind: "openai",
  238. }),
  239. statusContext: {
  240. model: { providerId: "openai", modelId: "gpt-test" },
  241. providerKind: "openai",
  242. requestId: "req_openai_video",
  243. sessionId: "sess_openai_video",
  244. traceId: "trace_openai_video",
  245. } as never,
  246. });
  247. expect((options.messages[0] as any).content).toEqual([
  248. {
  249. type: "text",
  250. text: "[Attached video/mp4]\n[Media omitted from provider request because the selected model does not support video input.]",
  251. },
  252. ]);
  253. }
  254. });
  255. it("subtracts fixed Anthropic thinking from the explicit model-level generateText budget", () => {
  256. const options = createGenerateTextOptions({
  257. includeModelIO: false,
  258. request: {
  259. maxOutputTokens: 131_072,
  260. maxOutputTokensSource: "model-capability",
  261. messages: [{ role: "user", content: "hi" }],
  262. providerOptions: {
  263. anthropic: {
  264. thinking: {
  265. budgetTokens: 1_024,
  266. type: "enabled",
  267. },
  268. },
  269. },
  270. },
  271. resolved: resolved({
  272. model: {} as ResolvedAiSdkModel["model"],
  273. ref: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  274. providerKind: "anthropic",
  275. baseURL: "https://api.xiaomimimo.com/anthropic",
  276. }),
  277. statusContext: {
  278. model: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  279. providerKind: "anthropic",
  280. requestId: "req_mimo_generate",
  281. sessionId: "sess_mimo_generate",
  282. traceId: "trace_mimo_generate",
  283. } as never,
  284. });
  285. expect(options.maxOutputTokens).toBe(130_048);
  286. });
  287. it("subtracts fixed Anthropic thinking from the explicit model-level streamText budget", () => {
  288. const options = createStreamTextOptions({
  289. includeModelIO: false,
  290. request: {
  291. maxOutputTokens: 131_072,
  292. maxOutputTokensSource: "model-capability",
  293. messages: [{ role: "user", content: "hi" }],
  294. providerOptions: {
  295. anthropic: {
  296. thinking: {
  297. budgetTokens: 1_024,
  298. type: "enabled",
  299. },
  300. },
  301. },
  302. },
  303. resolved: resolved({
  304. model: {} as ResolvedAiSdkModel["model"],
  305. ref: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  306. providerKind: "anthropic",
  307. baseURL: "https://api.xiaomimimo.com/anthropic",
  308. }),
  309. statusContext: {
  310. model: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  311. providerKind: "anthropic",
  312. requestId: "req_mimo_stream",
  313. sessionId: "sess_mimo_stream",
  314. traceId: "trace_mimo_stream",
  315. } as never,
  316. });
  317. expect(options.maxOutputTokens).toBe(130_048);
  318. });
  319. it("fits oversized fixed Anthropic thinking into one immutable request budget", () => {
  320. const providerOptions = {
  321. anthropic: {
  322. effort: "high",
  323. vendorFlag: "kept",
  324. thinking: { budgetTokens: 32_000, type: "enabled" },
  325. },
  326. };
  327. const input = {
  328. includeModelIO: false,
  329. request: {
  330. maxOutputTokens: 20_000,
  331. maxOutputTokensSource: "runtime-default" as const,
  332. messages: [{ role: "user" as const, content: "compact" }],
  333. providerOptions,
  334. },
  335. resolved: resolved({
  336. model: {} as ResolvedAiSdkModel["model"],
  337. ref: { providerId: "anthropic-compatible", modelId: "fixed-thinking-model" },
  338. providerKind: "anthropic",
  339. baseURL: "https://api.example.test/anthropic",
  340. }),
  341. statusContext: {
  342. model: { providerId: "anthropic-compatible", modelId: "fixed-thinking-model" },
  343. providerKind: "anthropic",
  344. requestId: "req_compact_fixed_thinking",
  345. sessionId: "sess_compact_fixed_thinking",
  346. traceId: "trace_compact_fixed_thinking",
  347. } as never,
  348. };
  349. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  350. const options = createOptions(input);
  351. expect(options.maxOutputTokens).toBe(1);
  352. expect(options.providerOptions).toMatchObject({
  353. anthropic: {
  354. effort: "high",
  355. vendorFlag: "kept",
  356. thinking: { budgetTokens: 19_999, type: "enabled" },
  357. },
  358. });
  359. }
  360. expect(providerOptions.anthropic.thinking.budgetTokens).toBe(32_000);
  361. });
  362. it.each([0.5, 1])(
  363. "rejects Anthropic fixed thinking when the total request budget is %s",
  364. (maxOutputTokens) => {
  365. const input = {
  366. includeModelIO: false,
  367. request: {
  368. maxOutputTokens,
  369. messages: [{ role: "user" as const, content: "small budget" }],
  370. providerOptions: {
  371. anthropic: {
  372. thinking: { budgetTokens: 32_000, type: "enabled" },
  373. },
  374. },
  375. },
  376. resolved: resolved({
  377. model: {} as ResolvedAiSdkModel["model"],
  378. ref: { providerId: "anthropic-compatible", modelId: "fixed-thinking-model" },
  379. providerKind: "anthropic",
  380. baseURL: "https://api.example.test/anthropic",
  381. }),
  382. statusContext: {
  383. model: { providerId: "anthropic-compatible", modelId: "fixed-thinking-model" },
  384. providerKind: "anthropic",
  385. requestId: "req_small_fixed_thinking",
  386. sessionId: "sess_small_fixed_thinking",
  387. traceId: "trace_small_fixed_thinking",
  388. } as never,
  389. };
  390. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  391. let error: unknown;
  392. try {
  393. createOptions(input);
  394. } catch (caught) {
  395. error = caught;
  396. }
  397. expect(error).toBeInstanceOf(AiSdkModelAdapterError);
  398. expect(error).toMatchObject({
  399. code: ModelErrorCode.InvalidModelRequest,
  400. message: "Anthropic-compatible fixed thinking requires maxOutputTokens to be at least 2",
  401. } satisfies Partial<AiSdkModelAdapterError>);
  402. }
  403. },
  404. );
  405. it("keeps explicit model-level output tokens on non-Anthropic transport", () => {
  406. const options = createGenerateTextOptions({
  407. includeModelIO: false,
  408. request: {
  409. maxOutputTokens: 131_072,
  410. messages: [{ role: "user", content: "hi" }],
  411. providerOptions: {
  412. anthropic: {
  413. thinking: {
  414. budgetTokens: 1_024,
  415. type: "enabled",
  416. },
  417. },
  418. },
  419. },
  420. resolved: resolved({
  421. model: {} as ResolvedAiSdkModel["model"],
  422. ref: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  423. providerKind: "openai-compatible",
  424. baseURL: "https://api.xiaomimimo.com/v1",
  425. }),
  426. statusContext: {
  427. model: { providerId: "default-mimo", modelId: "mimo-v2.5-pro" },
  428. providerKind: "openai-compatible",
  429. requestId: "req_mimo_openai",
  430. sessionId: "sess_mimo_openai",
  431. traceId: "trace_mimo_openai",
  432. } as never,
  433. });
  434. expect(options.maxOutputTokens).toBe(131_072);
  435. });
  436. it.each([
  437. { caseId: "OTB01", modelLimit: undefined, expected: 32_000 },
  438. { caseId: "OTB02", modelLimit: 16_000, expected: 16_000 },
  439. { caseId: "OTB03", modelLimit: 64_000, expected: 64_000 },
  440. { caseId: "fractional normalization", modelLimit: 16_000.9, expected: 16_000 },
  441. { caseId: "invalid zero", modelLimit: 0, expected: 32_000 },
  442. { caseId: "invalid negative", modelLimit: -1, expected: 32_000 },
  443. { caseId: "invalid non-finite", modelLimit: Number.NaN, expected: 32_000 },
  444. ])("$caseId resolves model capability $modelLimit to $expected", ({ modelLimit, expected }) => {
  445. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  446. const options = createOptions({
  447. includeModelIO: false,
  448. request: {
  449. maxOutputTokens: modelLimit,
  450. maxOutputTokensSource: "model-capability",
  451. messages: [{ role: "user", content: "hi" }],
  452. },
  453. resolved: resolved({
  454. model: {} as ResolvedAiSdkModel["model"],
  455. ref: { providerId: "custom-compatible", modelId: "reasoning-model" },
  456. providerKind: "openai-compatible",
  457. baseURL: "https://api.example.test/v1",
  458. }),
  459. statusContext: {
  460. model: { providerId: "custom-compatible", modelId: "reasoning-model" },
  461. providerKind: "openai-compatible",
  462. requestId: "req_model_capability",
  463. sessionId: "sess_model_capability",
  464. traceId: "trace_model_capability",
  465. } as never,
  466. });
  467. expect(options.maxOutputTokens).toBe(expected);
  468. }
  469. });
  470. it.each([
  471. {
  472. expected: 32_000,
  473. name: "model-capability source",
  474. providerKind: "openai-compatible" as const,
  475. source: "model-capability" as const,
  476. },
  477. {
  478. expected: 32_000,
  479. name: "runtime-default source",
  480. providerKind: "openai-compatible" as const,
  481. source: "runtime-default" as const,
  482. },
  483. {
  484. expected: 32_000,
  485. name: "legacy Anthropic request",
  486. providerKind: "anthropic" as const,
  487. source: undefined,
  488. },
  489. {
  490. expected: undefined,
  491. name: "legacy non-Anthropic request",
  492. providerKind: "openai-compatible" as const,
  493. source: undefined,
  494. },
  495. ])(
  496. "keeps the missing-budget compatibility behavior for $name",
  497. ({ expected, providerKind, source }) => {
  498. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  499. const options = createOptions({
  500. includeModelIO: false,
  501. request: {
  502. maxOutputTokensSource: source,
  503. messages: [{ role: "user", content: "hi" }],
  504. },
  505. resolved: resolved({
  506. model: {} as ResolvedAiSdkModel["model"],
  507. ref: { providerId: "provider", modelId: "model" },
  508. providerKind,
  509. }),
  510. statusContext: {
  511. model: { providerId: "provider", modelId: "model" },
  512. providerKind,
  513. requestId: "req_missing_output_budget",
  514. sessionId: "sess_missing_output_budget",
  515. traceId: "trace_missing_output_budget",
  516. } as never,
  517. });
  518. expect(options.maxOutputTokens).toBe(expected);
  519. }
  520. },
  521. );
  522. it.each(["openai", "openai-compatible", "gateway", "custom"] as const)(
  523. "keeps normal model capability budget for %s providers",
  524. (providerKind) => {
  525. const options = createGenerateTextOptions({
  526. includeModelIO: false,
  527. request: {
  528. maxOutputTokens: 131_072,
  529. maxOutputTokensSource: "model-capability",
  530. messages: [{ role: "user", content: "hi" }],
  531. },
  532. resolved: resolved({
  533. model: {} as ResolvedAiSdkModel["model"],
  534. ref: { providerId: "provider", modelId: "model" },
  535. providerKind,
  536. }),
  537. statusContext: {
  538. model: { providerId: "provider", modelId: "model" },
  539. providerKind,
  540. requestId: `req_${providerKind}`,
  541. sessionId: `sess_${providerKind}`,
  542. traceId: `trace_${providerKind}`,
  543. } as never,
  544. });
  545. expect(options.maxOutputTokens).toBe(131_072);
  546. },
  547. );
  548. it("keeps the Compact summary budget for OpenAI-compatible runtime-default requests", () => {
  549. const generateOptions = createGenerateTextOptions({
  550. includeModelIO: false,
  551. request: {
  552. maxOutputTokens: 20_000,
  553. maxOutputTokensSource: "runtime-default",
  554. messages: [{ role: "user", content: "hi" }],
  555. },
  556. resolved: resolved({
  557. model: {} as ResolvedAiSdkModel["model"],
  558. ref: { providerId: "custom-openai-compatible", modelId: "generic-reasoning-model" },
  559. providerKind: "openai-compatible",
  560. baseURL: "https://api.example.test/v1",
  561. }),
  562. statusContext: {
  563. model: { providerId: "custom-openai-compatible", modelId: "generic-reasoning-model" },
  564. providerKind: "openai-compatible",
  565. requestId: "req_openai_compatible_default_generate",
  566. sessionId: "sess_openai_compatible_default_generate",
  567. traceId: "trace_openai_compatible_default_generate",
  568. } as never,
  569. });
  570. const streamOptions = createStreamTextOptions({
  571. includeModelIO: false,
  572. request: {
  573. maxOutputTokens: 20_000,
  574. maxOutputTokensSource: "runtime-default",
  575. messages: [{ role: "user", content: "hi" }],
  576. },
  577. resolved: resolved({
  578. model: {} as ResolvedAiSdkModel["model"],
  579. ref: { providerId: "custom-openai-compatible", modelId: "generic-reasoning-model" },
  580. providerKind: "openai-compatible",
  581. baseURL: "https://api.example.test/v1",
  582. }),
  583. statusContext: {
  584. model: { providerId: "custom-openai-compatible", modelId: "generic-reasoning-model" },
  585. providerKind: "openai-compatible",
  586. requestId: "req_openai_compatible_default_stream",
  587. sessionId: "sess_openai_compatible_default_stream",
  588. traceId: "trace_openai_compatible_default_stream",
  589. } as never,
  590. });
  591. expect(generateOptions.maxOutputTokens).toBe(20_000);
  592. expect(streamOptions.maxOutputTokens).toBe(20_000);
  593. });
  594. it("lets the canonical budget override native OpenAI-compatible output params", () => {
  595. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  596. const options = createOptions({
  597. includeModelIO: false,
  598. request: {
  599. maxOutputTokens: 64_000,
  600. maxOutputTokensSource: "runtime-default",
  601. messages: [{ role: "user", content: "hi" }],
  602. providerOptions: {
  603. openaiCompatible: {
  604. extra_body: {
  605. max_tokens: 32_768,
  606. vendor_flag: true,
  607. },
  608. },
  609. },
  610. },
  611. resolved: resolved({
  612. model: {} as ResolvedAiSdkModel["model"],
  613. ref: { providerId: "custom-openai-compatible", modelId: "large-output-model" },
  614. providerKind: "openai-compatible",
  615. baseURL: "https://api.example.test/v1",
  616. providerOptionsName: "Custom Compatible",
  617. } as Partial<ResolvedAiSdkModel> & { providerOptionsName: string }),
  618. statusContext: {
  619. model: { providerId: "custom-openai-compatible", modelId: "large-output-model" },
  620. providerKind: "openai-compatible",
  621. requestId: "req_openai_compatible_native_max_tokens",
  622. sessionId: "sess_openai_compatible_native_max_tokens",
  623. traceId: "trace_openai_compatible_native_max_tokens",
  624. } as never,
  625. });
  626. expect(options.maxOutputTokens).toBe(64_000);
  627. expect(options.providerOptions).toEqual({
  628. openaiCompatible: {
  629. extra_body: { vendor_flag: true },
  630. },
  631. "Custom Compatible": { vendor_flag: true },
  632. });
  633. }
  634. });
  635. it("keeps native output params for legacy requests without a canonical budget", () => {
  636. const options = createGenerateTextOptions({
  637. includeModelIO: false,
  638. request: {
  639. messages: [{ role: "user", content: "hi" }],
  640. providerOptions: {
  641. openaiCompatible: { extra_body: { max_tokens: 32_768 } },
  642. },
  643. },
  644. resolved: resolved({
  645. model: {} as ResolvedAiSdkModel["model"],
  646. ref: { providerId: "custom-openai-compatible", modelId: "legacy-model" },
  647. providerKind: "openai-compatible",
  648. providerOptionsName: "Custom Compatible",
  649. } as Partial<ResolvedAiSdkModel> & { providerOptionsName: string }),
  650. statusContext: {
  651. model: { providerId: "custom-openai-compatible", modelId: "legacy-model" },
  652. providerKind: "openai-compatible",
  653. requestId: "req_legacy_native_max_tokens",
  654. sessionId: "sess_legacy_native_max_tokens",
  655. traceId: "trace_legacy_native_max_tokens",
  656. } as never,
  657. });
  658. expect(options.maxOutputTokens).toBeUndefined();
  659. expect(options.providerOptions).toMatchObject({
  660. "Custom Compatible": { max_tokens: 32_768 },
  661. });
  662. });
  663. it("keeps explicit Anthropic runtime-default output tokens", () => {
  664. const options = createGenerateTextOptions({
  665. includeModelIO: false,
  666. request: {
  667. maxOutputTokens: 131_072,
  668. maxOutputTokensSource: "runtime-default",
  669. messages: [{ role: "user", content: "hi" }],
  670. },
  671. resolved: resolved({
  672. model: {} as ResolvedAiSdkModel["model"],
  673. ref: { providerId: "custom-anthropic-compatible", modelId: "generic-reasoning-model" },
  674. providerKind: "anthropic",
  675. baseURL: "https://api.example.test/anthropic",
  676. }),
  677. statusContext: {
  678. model: { providerId: "custom-anthropic-compatible", modelId: "generic-reasoning-model" },
  679. providerKind: "anthropic",
  680. requestId: "req_anthropic_runtime_default",
  681. sessionId: "sess_anthropic_runtime_default",
  682. traceId: "trace_anthropic_runtime_default",
  683. } as never,
  684. });
  685. expect(options.maxOutputTokens).toBe(131_072);
  686. });
  687. it("keeps the Compact summary budget on Anthropic requests", () => {
  688. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  689. const options = createOptions({
  690. includeModelIO: false,
  691. request: {
  692. maxOutputTokens: 20_000,
  693. maxOutputTokensSource: "runtime-default",
  694. messages: [{ role: "user", content: "hi" }],
  695. },
  696. resolved: resolved({
  697. model: {} as ResolvedAiSdkModel["model"],
  698. ref: { providerId: "custom-anthropic-compatible", modelId: "generic-reasoning-model" },
  699. providerKind: "anthropic",
  700. baseURL: "https://api.example.test/anthropic",
  701. }),
  702. statusContext: {
  703. model: { providerId: "custom-anthropic-compatible", modelId: "generic-reasoning-model" },
  704. providerKind: "anthropic",
  705. requestId: "req_anthropic_compact_independent_cap",
  706. sessionId: "sess_anthropic_compact_independent_cap",
  707. traceId: "trace_anthropic_compact_independent_cap",
  708. } as never,
  709. });
  710. expect(options.maxOutputTokens).toBe(20_000);
  711. }
  712. });
  713. it("unwraps OpenAI-compatible extra_body into the SDK provider namespace", () => {
  714. const options = createStreamTextOptions({
  715. includeModelIO: false,
  716. request: {
  717. messages: [{ role: "user", content: "hi" }],
  718. providerOptions: {
  719. openaiCompatible: {
  720. extra_body: {
  721. chat_template_kwargs: {
  722. reasoning_effort: "max",
  723. },
  724. },
  725. },
  726. },
  727. },
  728. resolved: resolved({
  729. model: {} as ResolvedAiSdkModel["model"],
  730. ref: { providerId: "custom-glm", modelId: "GLM-5.2" },
  731. providerKind: "openai-compatible",
  732. baseURL: "https://api.example.test/v1",
  733. providerOptionsName: "GLM52 E2E",
  734. } as Partial<ResolvedAiSdkModel> & { providerOptionsName: string }),
  735. statusContext: {
  736. model: { providerId: "custom-glm", modelId: "GLM-5.2" },
  737. providerKind: "openai-compatible",
  738. requestId: "req_glm52",
  739. sessionId: "sess_glm52",
  740. traceId: "trace_glm52",
  741. } as never,
  742. });
  743. expect(options.providerOptions).toEqual({
  744. openaiCompatible: {
  745. extra_body: {
  746. chat_template_kwargs: {
  747. reasoning_effort: "max",
  748. },
  749. },
  750. },
  751. "GLM52 E2E": {
  752. chat_template_kwargs: {
  753. reasoning_effort: "max",
  754. },
  755. },
  756. });
  757. });
  758. it("preserves raw and sibling namespaces while projecting canonical options", () => {
  759. const options = createStreamTextOptions({
  760. includeModelIO: false,
  761. request: {
  762. messages: [{ role: "user", content: "hi" }],
  763. providerOptions: {
  764. openaiCompatible: {
  765. extra_body: {
  766. mode: "legacy",
  767. thinking: { type: "enabled" },
  768. },
  769. mode: "direct",
  770. reasoningEffort: "max",
  771. },
  772. "foo-bar": {
  773. rawOnly: true,
  774. },
  775. fooBar: {
  776. camelOnly: true,
  777. },
  778. "openai-compatible": {
  779. legacyOnly: true,
  780. },
  781. anthropic: {
  782. siblingOnly: true,
  783. },
  784. },
  785. },
  786. resolved: resolved({
  787. model: {} as ResolvedAiSdkModel["model"],
  788. ref: { providerId: "foo-bar", modelId: "reasoning-model" },
  789. providerKind: "openai-compatible",
  790. baseURL: "https://api.example.test/v1",
  791. providerOptionsName: "foo-bar",
  792. } as Partial<ResolvedAiSdkModel> & { providerOptionsName: string }),
  793. statusContext: {
  794. model: { providerId: "foo-bar", modelId: "reasoning-model" },
  795. providerKind: "openai-compatible",
  796. requestId: "req_namespace_pass_through",
  797. sessionId: "sess_namespace_pass_through",
  798. traceId: "trace_namespace_pass_through",
  799. } as never,
  800. });
  801. expect(options.providerOptions).toEqual({
  802. openaiCompatible: {
  803. extra_body: {
  804. mode: "legacy",
  805. thinking: { type: "enabled" },
  806. },
  807. mode: "direct",
  808. reasoningEffort: "max",
  809. },
  810. "foo-bar": {
  811. mode: "direct",
  812. rawOnly: true,
  813. reasoningEffort: "max",
  814. thinking: { type: "enabled" },
  815. },
  816. fooBar: {
  817. camelOnly: true,
  818. },
  819. "openai-compatible": {
  820. legacyOnly: true,
  821. },
  822. anthropic: {
  823. siblingOnly: true,
  824. },
  825. });
  826. });
  827. it("rejects a non-object canonical extra_body", () => {
  828. let error: unknown;
  829. try {
  830. createGenerateTextOptions({
  831. includeModelIO: false,
  832. request: {
  833. messages: [{ role: "user", content: "hi" }],
  834. providerOptions: {
  835. openaiCompatible: {
  836. extra_body: [],
  837. },
  838. },
  839. },
  840. resolved: resolved({
  841. model: {} as ResolvedAiSdkModel["model"],
  842. ref: { providerId: "custom-compatible", modelId: "reasoning-model" },
  843. providerKind: "openai-compatible",
  844. baseURL: "https://api.example.test/v1",
  845. providerOptionsName: "Custom Compatible",
  846. } as Partial<ResolvedAiSdkModel> & { providerOptionsName: string }),
  847. statusContext: {
  848. model: { providerId: "custom-compatible", modelId: "reasoning-model" },
  849. providerKind: "openai-compatible",
  850. requestId: "req_invalid_extra_body",
  851. sessionId: "sess_invalid_extra_body",
  852. traceId: "trace_invalid_extra_body",
  853. } as never,
  854. });
  855. } catch (caught) {
  856. error = caught;
  857. }
  858. expect(error).toBeInstanceOf(AiSdkModelAdapterError);
  859. expect(error).toMatchObject({
  860. code: ModelErrorCode.InvalidModelRequest,
  861. message: "openaiCompatible.extra_body 必须是对象",
  862. } satisfies Partial<AiSdkModelAdapterError>);
  863. });
  864. it("passes experimental_include.responseBody for zcode-plan streams", () => {
  865. const options = createStreamTextOptions({
  866. includeModelIO: false,
  867. request: {
  868. messages: [{ role: "user", content: "hi" }],
  869. },
  870. resolved: resolved({
  871. model: {} as ResolvedAiSdkModel["model"],
  872. ref: { providerId: "builtin:zai-coding-plan", modelId: "glm-5.1" },
  873. providerKind: "openai-compatible",
  874. baseURL: "https://zcode.z.ai/api/v1/zcode-plan",
  875. }),
  876. statusContext: {
  877. model: { providerId: "builtin:zai-coding-plan", modelId: "glm-5.1" },
  878. providerKind: "openai-compatible",
  879. requestId: "req_test",
  880. sessionId: "sess_test",
  881. traceId: "trace_test",
  882. } as never,
  883. });
  884. expect(options.experimental_include).toEqual({ responseBody: true });
  885. });
  886. it("uses merged Responses provider options while transforming history", () => {
  887. const toolCallId = "call_read";
  888. const options = createGenerateTextOptions({
  889. includeModelIO: false,
  890. request: {
  891. messages: [
  892. {
  893. role: "assistant",
  894. content: [
  895. {
  896. type: "reasoning",
  897. text: "stored reasoning",
  898. providerOptions: { openai: { itemId: "rs_1" } },
  899. },
  900. { type: "text", text: "checking" },
  901. ],
  902. toolCalls: [{ id: toolCallId, name: "Read", input: { file_path: "README.md" } }],
  903. },
  904. ],
  905. },
  906. resolved: resolved({
  907. model: {} as ResolvedAiSdkModel["model"],
  908. ref: { providerId: "openai", modelId: "gpt-5" },
  909. providerKind: "openai",
  910. providerOptions: { apiFormat: "openai-responses" },
  911. }),
  912. statusContext: {
  913. model: { providerId: "openai", modelId: "gpt-5" },
  914. providerKind: "openai",
  915. requestId: "req_responses",
  916. sessionId: "sess_responses",
  917. traceId: "trace_responses",
  918. } as never,
  919. });
  920. const [message] = options.messages ?? [];
  921. expect((message as any).content).toEqual([
  922. { type: "text", text: "checking" },
  923. {
  924. type: "tool-call",
  925. toolCallId,
  926. toolName: "Read",
  927. input: { file_path: "README.md" },
  928. },
  929. ]);
  930. expect(options.providerOptions).toEqual({ apiFormat: "openai-responses" });
  931. });
  932. it("packages native OpenAI reasoning effort without dropping static OpenAI options", () => {
  933. for (const createOptions of [createGenerateTextOptions, createStreamTextOptions]) {
  934. const options = createOptions({
  935. includeModelIO: false,
  936. request: {
  937. messages: [{ role: "user", content: "Generate a title" }],
  938. providerOptions: { reasoningEffort: "none" },
  939. },
  940. resolved: resolved({
  941. model: {} as ResolvedAiSdkModel["model"],
  942. providerKind: "openai",
  943. providerOptions: { openai: { reasoningEffort: "high", store: false } },
  944. ref: { providerId: "openai", modelId: "gpt-5.2" },
  945. }),
  946. statusContext: {
  947. model: { providerId: "openai", modelId: "gpt-5.2" },
  948. providerKind: "openai",
  949. requestId: "req_openai_reasoning",
  950. sessionId: "sess_openai_reasoning",
  951. traceId: "trace_openai_reasoning",
  952. } as never,
  953. });
  954. expect(options.providerOptions).toEqual({
  955. openai: {
  956. reasoningEffort: "none",
  957. store: false,
  958. },
  959. });
  960. }
  961. });
  962. it("adds request attribution headers for generateText requests", () => {
  963. const options = createGenerateTextOptions({
  964. includeModelIO: false,
  965. request: {
  966. messages: [{ role: "user", content: "hi" }],
  967. },
  968. resolved: resolved({
  969. model: {} as ResolvedAiSdkModel["model"],
  970. ref: { providerId: "test", modelId: "model-a" },
  971. providerKind: "openai-compatible",
  972. baseURL: "https://api.example.test/v1",
  973. }),
  974. statusContext: {
  975. model: { providerId: "test", modelId: "model-a" },
  976. modelRequestSessionType: "main",
  977. providerKind: "openai-compatible",
  978. requestId: "req_generate",
  979. queryId: "query_generate",
  980. sessionId: "sess_generate",
  981. traceId: "trace_generate",
  982. } as never,
  983. });
  984. expect(options.headers).toEqual({
  985. "x-query-id": "generate",
  986. "x-request-id": "req_generate",
  987. "x-session-id": "generate",
  988. "x-zcode-session-type": "main",
  989. "x-zcode-trace-id": "trace_generate",
  990. });
  991. });
  992. it("keeps provider runtime platform headers with request attribution headers", () => {
  993. const options = createGenerateTextOptions({
  994. includeModelIO: false,
  995. request: {
  996. messages: [{ role: "user", content: "hi" }],
  997. },
  998. resolved: resolved({
  999. headers: {
  1000. "X-Os-Category": "macos",
  1001. "X-Os-Version": "25.0.0",
  1002. "X-Platform": "darwin-arm64",
  1003. "x-zcode-session-type": "spoofed",
  1004. },
  1005. model: {} as ResolvedAiSdkModel["model"],
  1006. ref: { providerId: "test", modelId: "model-a" },
  1007. providerKind: "openai-compatible",
  1008. baseURL: "https://api.example.test/v1",
  1009. }),
  1010. statusContext: {
  1011. model: { providerId: "test", modelId: "model-a" },
  1012. modelRequestSessionType: "subagent",
  1013. providerKind: "openai-compatible",
  1014. requestId: "req_platform",
  1015. sessionId: "sess_platform",
  1016. traceId: "trace_platform",
  1017. } as never,
  1018. });
  1019. expect(options.headers).toEqual({
  1020. "X-Os-Category": "macos",
  1021. "X-Os-Version": "25.0.0",
  1022. "X-Platform": "darwin-arm64",
  1023. "x-request-id": "req_platform",
  1024. "x-session-id": "platform",
  1025. "x-zcode-session-type": "subagent",
  1026. "x-zcode-trace-id": "trace_platform",
  1027. });
  1028. });
  1029. it("strips internal subagent and query prefixes from request attribution headers", () => {
  1030. const queryId = "query_ad8740cd-f843-4235-ba0d-395063e56fda";
  1031. const sessionId = "sess_subagent_agent_b3d813c3-c568-42e9-8f67-eeb814bc8ef2";
  1032. const options = createGenerateTextOptions({
  1033. includeModelIO: false,
  1034. request: {
  1035. messages: [{ role: "user", content: "hi" }],
  1036. },
  1037. resolved: resolved({
  1038. model: {} as ResolvedAiSdkModel["model"],
  1039. ref: { providerId: "test", modelId: "model-a" },
  1040. providerKind: "openai-compatible",
  1041. baseURL: "https://api.example.test/v1",
  1042. }),
  1043. statusContext: {
  1044. model: { providerId: "test", modelId: "model-a" },
  1045. modelRequestSessionType: "subagent",
  1046. providerKind: "openai-compatible",
  1047. requestId: "req_subagent",
  1048. queryId,
  1049. sessionId,
  1050. traceId: "trace_subagent",
  1051. } as never,
  1052. });
  1053. expect(options.headers).toMatchObject({
  1054. "x-query-id": "ad8740cd-f843-4235-ba0d-395063e56fda",
  1055. "x-session-id": "b3d813c3-c568-42e9-8f67-eeb814bc8ef2",
  1056. });
  1057. });
  1058. it("adds request attribution headers for streamText requests", () => {
  1059. const options = createStreamTextOptions({
  1060. includeModelIO: false,
  1061. request: {
  1062. messages: [{ role: "user", content: "hi" }],
  1063. },
  1064. resolved: resolved({
  1065. model: {} as ResolvedAiSdkModel["model"],
  1066. ref: { providerId: "test", modelId: "model-a" },
  1067. providerKind: "openai-compatible",
  1068. baseURL: "https://api.example.test/v1",
  1069. }),
  1070. statusContext: {
  1071. model: { providerId: "test", modelId: "model-a" },
  1072. modelRequestSessionType: "other",
  1073. providerKind: "openai-compatible",
  1074. requestId: "req_stream",
  1075. queryId: "query_stream",
  1076. sessionId: "sess_stream",
  1077. traceId: "trace_stream",
  1078. } as never,
  1079. });
  1080. expect(options.headers).toEqual({
  1081. "x-query-id": "stream",
  1082. "x-request-id": "req_stream",
  1083. "x-session-id": "stream",
  1084. "x-zcode-session-type": "other",
  1085. "x-zcode-trace-id": "trace_stream",
  1086. });
  1087. });
  1088. it.each([createGenerateTextOptions, createStreamTextOptions])(
  1089. "adds a stable OpenCode Go session header to %s requests",
  1090. (createOptions) => {
  1091. const options = createOptions({
  1092. includeModelIO: false,
  1093. request: {
  1094. messages: [{ role: "user", content: "hi" }],
  1095. },
  1096. resolved: resolved({
  1097. baseURL: "https://opencode.ai/zen/go/v1",
  1098. headers: { "x-opencode-session": "spoofed" },
  1099. model: {} as ResolvedAiSdkModel["model"],
  1100. providerKind: "openai-compatible",
  1101. ref: { providerId: "custom-provider", modelId: "kimi-k2.5" },
  1102. }),
  1103. statusContext: {
  1104. model: { providerId: "custom-provider", modelId: "kimi-k2.5" },
  1105. modelRequestSessionType: "main",
  1106. providerKind: "openai-compatible",
  1107. requestId: "req_opencode_go",
  1108. sessionId: "sess_conversation_stable",
  1109. traceId: "trace_opencode_go",
  1110. baseURL: "https://opencode.ai/zen/go/v1",
  1111. } as never,
  1112. });
  1113. expect(options.headers).toMatchObject({
  1114. "x-opencode-session": "conversation_stable",
  1115. });
  1116. },
  1117. );
  1118. it("does not add OpenCode Go session header for other providers on the same host", () => {
  1119. const options = createGenerateTextOptions({
  1120. includeModelIO: false,
  1121. request: { messages: [{ role: "user", content: "hi" }] },
  1122. resolved: resolved({
  1123. baseURL: "https://opencode.ai/zen/v1",
  1124. model: {} as ResolvedAiSdkModel["model"],
  1125. providerKind: "openai-compatible",
  1126. ref: { providerId: "custom-provider", modelId: "kimi-k2.5" },
  1127. }),
  1128. statusContext: {
  1129. model: { providerId: "custom-provider", modelId: "kimi-k2.5" },
  1130. modelRequestSessionType: "main",
  1131. providerKind: "openai-compatible",
  1132. requestId: "req_opencode_zen",
  1133. sessionId: "sess_conversation_stable",
  1134. traceId: "trace_opencode_zen",
  1135. } as never,
  1136. });
  1137. expect(options.headers).not.toHaveProperty("x-opencode-session");
  1138. });
  1139. });