openai-completions-tool-choice.test.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. import { Type } from "typebox";
  2. import { beforeEach, describe, expect, it, vi } from "vitest";
  3. import { getModel } from "../src/models.ts";
  4. import { convertMessages } from "../src/providers/openai-completions.ts";
  5. import { stream, streamSimple } from "../src/stream.ts";
  6. import type { AssistantMessage, Model, Tool, ToolResultMessage } from "../src/types.ts";
  7. const mockState = vi.hoisted(() => ({
  8. lastParams: undefined as unknown,
  9. chunks: undefined as
  10. | Array<null | {
  11. id?: string;
  12. choices?: Array<{ delta: Record<string, unknown>; finish_reason: string | null; usage?: unknown }>;
  13. usage?: {
  14. prompt_tokens: number;
  15. completion_tokens: number;
  16. prompt_tokens_details: { cached_tokens: number; cache_write_tokens?: number };
  17. completion_tokens_details: { reasoning_tokens: number };
  18. };
  19. }>
  20. | undefined,
  21. }));
  22. vi.mock("openai", () => {
  23. class FakeOpenAI {
  24. chat = {
  25. completions: {
  26. create: (params: unknown) => {
  27. mockState.lastParams = params;
  28. const stream = {
  29. async *[Symbol.asyncIterator]() {
  30. const chunks = mockState.chunks ?? [
  31. {
  32. choices: [{ delta: {}, finish_reason: "stop" }],
  33. usage: {
  34. prompt_tokens: 1,
  35. completion_tokens: 1,
  36. prompt_tokens_details: { cached_tokens: 0 },
  37. completion_tokens_details: { reasoning_tokens: 0 },
  38. },
  39. },
  40. ];
  41. for (const chunk of chunks) {
  42. yield chunk;
  43. }
  44. },
  45. };
  46. const promise = Promise.resolve(stream) as Promise<typeof stream> & {
  47. withResponse: () => Promise<{
  48. data: typeof stream;
  49. response: { status: number; headers: Headers };
  50. }>;
  51. };
  52. promise.withResponse = async () => ({
  53. data: stream,
  54. response: { status: 200, headers: new Headers() },
  55. });
  56. return promise;
  57. },
  58. },
  59. };
  60. }
  61. return { default: FakeOpenAI };
  62. });
  63. describe("openai-completions tool_choice", () => {
  64. beforeEach(() => {
  65. mockState.lastParams = undefined;
  66. mockState.chunks = undefined;
  67. });
  68. it("forwards toolChoice from simple options to payload", async () => {
  69. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  70. const model = { ...baseModel, api: "openai-completions" } as const;
  71. const tools: Tool[] = [
  72. {
  73. name: "ping",
  74. description: "Ping tool",
  75. parameters: Type.Object({
  76. ok: Type.Boolean(),
  77. }),
  78. },
  79. ];
  80. let payload: unknown;
  81. await streamSimple(
  82. model,
  83. {
  84. messages: [
  85. {
  86. role: "user",
  87. content: "Call ping with ok=true",
  88. timestamp: Date.now(),
  89. },
  90. ],
  91. tools,
  92. },
  93. {
  94. apiKey: "test",
  95. toolChoice: "required",
  96. onPayload: (params: unknown) => {
  97. payload = params;
  98. },
  99. } as unknown as Parameters<typeof streamSimple>[2],
  100. ).result();
  101. const params = (payload ?? mockState.lastParams) as { tool_choice?: string; tools?: unknown[] };
  102. expect(params.tool_choice).toBe("required");
  103. expect(Array.isArray(params.tools)).toBe(true);
  104. expect(params.tools?.length ?? 0).toBeGreaterThan(0);
  105. });
  106. it("omits strict when compat disables strict mode", async () => {
  107. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  108. const model = {
  109. ...baseModel,
  110. api: "openai-completions",
  111. compat: { supportsStrictMode: false },
  112. } as const;
  113. const tools: Tool[] = [
  114. {
  115. name: "ping",
  116. description: "Ping tool",
  117. parameters: Type.Object({
  118. ok: Type.Boolean(),
  119. }),
  120. },
  121. ];
  122. let payload: unknown;
  123. await streamSimple(
  124. model,
  125. {
  126. messages: [
  127. {
  128. role: "user",
  129. content: "Call ping with ok=true",
  130. timestamp: Date.now(),
  131. },
  132. ],
  133. tools,
  134. },
  135. {
  136. apiKey: "test",
  137. onPayload: (params: unknown) => {
  138. payload = params;
  139. },
  140. } as unknown as Parameters<typeof streamSimple>[2],
  141. ).result();
  142. const params = (payload ?? mockState.lastParams) as { tools?: Array<{ function?: Record<string, unknown> }> };
  143. const tool = params.tools?.[0]?.function;
  144. expect(tool).toBeTruthy();
  145. expect(tool?.strict).toBeUndefined();
  146. expect("strict" in (tool ?? {})).toBe(false);
  147. });
  148. it("maps groq qwen3 reasoning levels to default reasoning_effort", async () => {
  149. const model = getModel("groq", "qwen/qwen3-32b")!;
  150. let payload: unknown;
  151. await streamSimple(
  152. model,
  153. {
  154. messages: [
  155. {
  156. role: "user",
  157. content: "Hi",
  158. timestamp: Date.now(),
  159. },
  160. ],
  161. },
  162. {
  163. apiKey: "test",
  164. reasoning: "medium",
  165. onPayload: (params: unknown) => {
  166. payload = params;
  167. },
  168. },
  169. ).result();
  170. const params = (payload ?? mockState.lastParams) as { reasoning_effort?: string };
  171. expect(params.reasoning_effort).toBe("default");
  172. });
  173. it("keeps normal reasoning_effort for groq models without compat mapping", async () => {
  174. const model = getModel("groq", "openai/gpt-oss-20b")!;
  175. let payload: unknown;
  176. await streamSimple(
  177. model,
  178. {
  179. messages: [
  180. {
  181. role: "user",
  182. content: "Hi",
  183. timestamp: Date.now(),
  184. },
  185. ],
  186. },
  187. {
  188. apiKey: "test",
  189. reasoning: "medium",
  190. onPayload: (params: unknown) => {
  191. payload = params;
  192. },
  193. },
  194. ).result();
  195. const params = (payload ?? mockState.lastParams) as { reasoning_effort?: string };
  196. expect(params.reasoning_effort).toBe("medium");
  197. });
  198. it("enables tool_stream for supported z.ai models with tools", async () => {
  199. const model = getModel("zai", "glm-5.1")!;
  200. const tools: Tool[] = [
  201. {
  202. name: "ping",
  203. description: "Ping tool",
  204. parameters: Type.Object({
  205. ok: Type.Boolean(),
  206. }),
  207. },
  208. ];
  209. let payload: unknown;
  210. await streamSimple(
  211. model,
  212. {
  213. messages: [
  214. {
  215. role: "user",
  216. content: "Call ping with ok=true",
  217. timestamp: Date.now(),
  218. },
  219. ],
  220. tools,
  221. },
  222. {
  223. apiKey: "test",
  224. onPayload: (params: unknown) => {
  225. payload = params;
  226. },
  227. },
  228. ).result();
  229. const params = (payload ?? mockState.lastParams) as { tool_stream?: boolean };
  230. expect(params.tool_stream).toBe(true);
  231. });
  232. it("stores z.ai tool_stream support in model compat metadata", () => {
  233. expect(getModel("zai", "glm-5.1")?.compat?.zaiToolStream).toBe(true);
  234. expect(getModel("zai", "glm-4.7")?.compat?.zaiToolStream).toBe(true);
  235. expect(getModel("zai", "glm-4.7")?.compat?.zaiToolStream).toBe(true);
  236. expect(getModel("zai", "glm-5-turbo")?.compat?.zaiToolStream).toBe(true);
  237. expect(getModel("zai", "glm-4.5-air")?.compat?.zaiToolStream).toBeUndefined();
  238. });
  239. it("omits tool_stream for unsupported z.ai models", async () => {
  240. const model = getModel("zai", "glm-4.5-air")!;
  241. const tools: Tool[] = [
  242. {
  243. name: "ping",
  244. description: "Ping tool",
  245. parameters: Type.Object({
  246. ok: Type.Boolean(),
  247. }),
  248. },
  249. ];
  250. let payload: unknown;
  251. await streamSimple(
  252. model,
  253. {
  254. messages: [
  255. {
  256. role: "user",
  257. content: "Call ping with ok=true",
  258. timestamp: Date.now(),
  259. },
  260. ],
  261. tools,
  262. },
  263. {
  264. apiKey: "test",
  265. onPayload: (params: unknown) => {
  266. payload = params;
  267. },
  268. },
  269. ).result();
  270. const params = (payload ?? mockState.lastParams) as { tool_stream?: boolean };
  271. expect(params.tool_stream).toBeUndefined();
  272. });
  273. it("respects explicit z.ai tool_stream compat override", async () => {
  274. const baseModel = getModel("zai", "glm-4.5-air")!;
  275. const model = {
  276. ...baseModel,
  277. compat: {
  278. ...baseModel.compat,
  279. zaiToolStream: true,
  280. },
  281. } as const;
  282. const tools: Tool[] = [
  283. {
  284. name: "ping",
  285. description: "Ping tool",
  286. parameters: Type.Object({
  287. ok: Type.Boolean(),
  288. }),
  289. },
  290. ];
  291. let payload: unknown;
  292. await streamSimple(
  293. model,
  294. {
  295. messages: [
  296. {
  297. role: "user",
  298. content: "Call ping with ok=true",
  299. timestamp: Date.now(),
  300. },
  301. ],
  302. tools,
  303. },
  304. {
  305. apiKey: "test",
  306. onPayload: (params: unknown) => {
  307. payload = params;
  308. },
  309. },
  310. ).result();
  311. const params = (payload ?? mockState.lastParams) as { tool_stream?: boolean };
  312. expect(params.tool_stream).toBe(true);
  313. });
  314. it("omits tool_stream when no tools are provided", async () => {
  315. const model = getModel("zai", "glm-5.1")!;
  316. let payload: unknown;
  317. await streamSimple(
  318. model,
  319. {
  320. messages: [
  321. {
  322. role: "user",
  323. content: "Hi",
  324. timestamp: Date.now(),
  325. },
  326. ],
  327. },
  328. {
  329. apiKey: "test",
  330. onPayload: (params: unknown) => {
  331. payload = params;
  332. },
  333. },
  334. ).result();
  335. const params = (payload ?? mockState.lastParams) as { tool_stream?: boolean };
  336. expect(params.tool_stream).toBeUndefined();
  337. });
  338. it("maps non-standard provider finish_reason values to stopReason error", async () => {
  339. mockState.chunks = [
  340. {
  341. choices: [{ delta: { content: "partial" }, finish_reason: null }],
  342. },
  343. {
  344. choices: [{ delta: {}, finish_reason: "network_error" }],
  345. usage: {
  346. prompt_tokens: 1,
  347. completion_tokens: 1,
  348. prompt_tokens_details: { cached_tokens: 0 },
  349. completion_tokens_details: { reasoning_tokens: 0 },
  350. },
  351. },
  352. ];
  353. const model = getModel("zai", "glm-5.1")!;
  354. const response = await streamSimple(
  355. model,
  356. {
  357. messages: [
  358. {
  359. role: "user",
  360. content: "Hi",
  361. timestamp: Date.now(),
  362. },
  363. ],
  364. },
  365. { apiKey: "test" },
  366. ).result();
  367. expect(response.stopReason).toBe("error");
  368. expect(response.errorMessage).toBe("Provider finish_reason: network_error");
  369. });
  370. it("ignores null stream chunks from openai-compatible providers", async () => {
  371. mockState.chunks = [
  372. null,
  373. {
  374. id: "chatcmpl-test",
  375. choices: [{ delta: { content: "OK" }, finish_reason: null }],
  376. },
  377. {
  378. id: "chatcmpl-test",
  379. choices: [{ delta: {}, finish_reason: "stop" }],
  380. usage: {
  381. prompt_tokens: 3,
  382. completion_tokens: 1,
  383. prompt_tokens_details: { cached_tokens: 0 },
  384. completion_tokens_details: { reasoning_tokens: 0 },
  385. },
  386. },
  387. ];
  388. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  389. const model = { ...baseModel, api: "openai-completions" } as const;
  390. const response = await streamSimple(
  391. model,
  392. {
  393. messages: [
  394. {
  395. role: "user",
  396. content: "Reply with exactly OK",
  397. timestamp: Date.now(),
  398. },
  399. ],
  400. },
  401. { apiKey: "test" },
  402. ).result();
  403. expect(response.stopReason).toBe("stop");
  404. expect(response.errorMessage).toBeUndefined();
  405. expect(response.responseId).toBe("chatcmpl-test");
  406. expect(response.usage.totalTokens).toBe(4);
  407. expect(response.content).toEqual([{ type: "text", text: "OK" }]);
  408. });
  409. it("errors when a stream ends after only null finish_reason chunks", async () => {
  410. mockState.chunks = [
  411. {
  412. id: "chatcmpl-truncated",
  413. choices: [{ delta: { content: "partial answer" }, finish_reason: null }],
  414. },
  415. {
  416. id: "chatcmpl-truncated",
  417. choices: [{ delta: { content: "partial answer" }, finish_reason: null }],
  418. },
  419. ];
  420. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  421. const model = { ...baseModel, api: "openai-completions" } as const;
  422. const response = await streamSimple(
  423. model,
  424. {
  425. messages: [
  426. {
  427. role: "user",
  428. content: "Reply with a longer sentence",
  429. timestamp: Date.now(),
  430. },
  431. ],
  432. },
  433. { apiKey: "test" },
  434. ).result();
  435. expect(response.stopReason).toBe("error");
  436. expect(response.errorMessage).toBe("Stream ended without finish_reason");
  437. });
  438. it("coalesces tool call deltas by stable index when provider mutates ids mid-stream", async () => {
  439. mockState.chunks = [
  440. {
  441. id: "chatcmpl-kimi-bad-stream",
  442. choices: [
  443. {
  444. delta: {
  445. tool_calls: [
  446. {
  447. index: 0,
  448. id: "functions.read:0",
  449. type: "function",
  450. function: { name: "read", arguments: "" },
  451. },
  452. ],
  453. },
  454. finish_reason: null,
  455. },
  456. ],
  457. },
  458. {
  459. id: "chatcmpl-kimi-bad-stream",
  460. choices: [
  461. {
  462. delta: {
  463. tool_calls: [
  464. {
  465. index: 0,
  466. id: "chatcmpl-tool-a",
  467. type: "function",
  468. function: { name: null, arguments: '{"path":"README' },
  469. },
  470. ],
  471. },
  472. finish_reason: null,
  473. },
  474. ],
  475. },
  476. {
  477. id: "chatcmpl-kimi-bad-stream",
  478. choices: [
  479. {
  480. delta: {
  481. tool_calls: [
  482. {
  483. index: 0,
  484. id: "chatcmpl-tool-b",
  485. type: "function",
  486. function: { name: null, arguments: '.md"}' },
  487. },
  488. ],
  489. },
  490. finish_reason: "tool_calls",
  491. },
  492. ],
  493. usage: {
  494. prompt_tokens: 10,
  495. completion_tokens: 5,
  496. prompt_tokens_details: { cached_tokens: 0 },
  497. completion_tokens_details: { reasoning_tokens: 0 },
  498. },
  499. },
  500. ];
  501. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  502. const model = { ...baseModel, api: "openai-completions" } as const;
  503. const tool: Tool = {
  504. name: "read",
  505. description: "Read a file",
  506. parameters: Type.Object({
  507. path: Type.String(),
  508. }),
  509. };
  510. const s = streamSimple(
  511. model,
  512. {
  513. messages: [
  514. {
  515. role: "user",
  516. content: "Read README.md",
  517. timestamp: Date.now(),
  518. },
  519. ],
  520. tools: [tool],
  521. },
  522. { apiKey: "test" },
  523. );
  524. const toolCallContentIndexes: number[] = [];
  525. for await (const event of s) {
  526. if (event.type === "toolcall_start" || event.type === "toolcall_delta" || event.type === "toolcall_end") {
  527. toolCallContentIndexes.push(event.contentIndex);
  528. }
  529. }
  530. const response = await s.result();
  531. expect(response.stopReason).toBe("toolUse");
  532. expect(toolCallContentIndexes).toEqual([0, 0, 0, 0, 0]);
  533. expect(response.content).toHaveLength(1);
  534. const toolCall = response.content[0];
  535. expect(toolCall.type).toBe("toolCall");
  536. if (toolCall.type !== "toolCall") {
  537. throw new Error("Expected toolCall content");
  538. }
  539. expect(toolCall.id).toBe("functions.read:0");
  540. expect(toolCall.name).toBe("read");
  541. expect(toolCall.arguments).toEqual({ path: "README.md" });
  542. expect(toolCall).not.toHaveProperty("streamIndex");
  543. expect(toolCall).not.toHaveProperty("partialArgs");
  544. });
  545. it("accumulates mixed content, reasoning, and parallel tool call deltas independently", async () => {
  546. mockState.chunks = [
  547. {
  548. id: "chatcmpl-mixed-deltas",
  549. choices: [
  550. {
  551. delta: {
  552. content: "answer 1",
  553. reasoning_content: "think 1",
  554. tool_calls: [
  555. {
  556. index: 0,
  557. id: "tc_read_initial",
  558. type: "function",
  559. function: { name: "read", arguments: '{"path":"README' },
  560. },
  561. {
  562. index: 1,
  563. id: "tc_grep_initial",
  564. type: "function",
  565. function: { name: "grep", arguments: '{"pattern":"TODO' },
  566. },
  567. {
  568. id: "tc_list_no_index",
  569. type: "function",
  570. function: { name: "list", arguments: '{"path":"packages' },
  571. },
  572. {
  573. id: "tc_write_no_index",
  574. type: "function",
  575. function: { name: "write", arguments: '{"path":"out' },
  576. },
  577. ],
  578. },
  579. finish_reason: null,
  580. },
  581. ],
  582. },
  583. {
  584. id: "chatcmpl-mixed-deltas",
  585. choices: [
  586. {
  587. delta: {
  588. content: " answer 2",
  589. tool_calls: [
  590. {
  591. index: 1,
  592. id: "tc_grep_changed",
  593. type: "function",
  594. function: { arguments: '","path":"src' },
  595. },
  596. {
  597. id: "tc_write_no_index",
  598. type: "function",
  599. function: { arguments: '.txt","content":"ok"}' },
  600. },
  601. {
  602. id: "tc_list_no_index",
  603. type: "function",
  604. function: { arguments: '/ai"}' },
  605. },
  606. ],
  607. },
  608. finish_reason: null,
  609. },
  610. ],
  611. },
  612. {
  613. id: "chatcmpl-mixed-deltas",
  614. choices: [
  615. {
  616. delta: {
  617. content: "\n",
  618. reasoning_content: " think 2",
  619. tool_calls: [
  620. {
  621. index: 0,
  622. id: "tc_read_changed",
  623. type: "function",
  624. function: { arguments: '.md"}' },
  625. },
  626. {
  627. index: 1,
  628. type: "function",
  629. function: { arguments: '"}' },
  630. },
  631. ],
  632. },
  633. finish_reason: "tool_calls",
  634. },
  635. ],
  636. usage: {
  637. prompt_tokens: 10,
  638. completion_tokens: 8,
  639. prompt_tokens_details: { cached_tokens: 0 },
  640. completion_tokens_details: { reasoning_tokens: 2 },
  641. },
  642. },
  643. ];
  644. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  645. const model = { ...baseModel, api: "openai-completions" } as const;
  646. const tools: Tool[] = [
  647. {
  648. name: "read",
  649. description: "Read a file",
  650. parameters: Type.Object({ path: Type.String() }),
  651. },
  652. {
  653. name: "grep",
  654. description: "Search a file",
  655. parameters: Type.Object({ pattern: Type.String(), path: Type.String() }),
  656. },
  657. {
  658. name: "list",
  659. description: "List a directory",
  660. parameters: Type.Object({ path: Type.String() }),
  661. },
  662. {
  663. name: "write",
  664. description: "Write a file",
  665. parameters: Type.Object({ path: Type.String(), content: Type.String() }),
  666. },
  667. ];
  668. const s = streamSimple(
  669. model,
  670. {
  671. messages: [
  672. {
  673. role: "user",
  674. content: "Think, answer, and use tools.",
  675. timestamp: Date.now(),
  676. },
  677. ],
  678. tools,
  679. },
  680. { apiKey: "test" },
  681. );
  682. const eventTypes: string[] = [];
  683. const toolEventsByContentIndex = new Map<number, string[]>();
  684. for await (const event of s) {
  685. eventTypes.push(event.type);
  686. if (event.type === "toolcall_start" || event.type === "toolcall_delta" || event.type === "toolcall_end") {
  687. const events = toolEventsByContentIndex.get(event.contentIndex) ?? [];
  688. events.push(event.type);
  689. toolEventsByContentIndex.set(event.contentIndex, events);
  690. }
  691. }
  692. const response = await s.result();
  693. expect(response.stopReason).toBe("toolUse");
  694. expect(eventTypes.filter((type) => type === "text_start")).toHaveLength(1);
  695. expect(eventTypes.filter((type) => type === "text_delta")).toHaveLength(3);
  696. expect(eventTypes.filter((type) => type === "text_end")).toHaveLength(1);
  697. expect(eventTypes.filter((type) => type === "thinking_start")).toHaveLength(1);
  698. expect(eventTypes.filter((type) => type === "thinking_delta")).toHaveLength(2);
  699. expect(eventTypes.filter((type) => type === "thinking_end")).toHaveLength(1);
  700. expect(eventTypes.filter((type) => type === "toolcall_start")).toHaveLength(4);
  701. expect(eventTypes.filter((type) => type === "toolcall_delta")).toHaveLength(9);
  702. expect(eventTypes.filter((type) => type === "toolcall_end")).toHaveLength(4);
  703. expect(toolEventsByContentIndex.get(2)).toEqual([
  704. "toolcall_start",
  705. "toolcall_delta",
  706. "toolcall_delta",
  707. "toolcall_end",
  708. ]);
  709. expect(toolEventsByContentIndex.get(3)).toEqual([
  710. "toolcall_start",
  711. "toolcall_delta",
  712. "toolcall_delta",
  713. "toolcall_delta",
  714. "toolcall_end",
  715. ]);
  716. expect(toolEventsByContentIndex.get(4)).toEqual([
  717. "toolcall_start",
  718. "toolcall_delta",
  719. "toolcall_delta",
  720. "toolcall_end",
  721. ]);
  722. expect(toolEventsByContentIndex.get(5)).toEqual([
  723. "toolcall_start",
  724. "toolcall_delta",
  725. "toolcall_delta",
  726. "toolcall_end",
  727. ]);
  728. expect(response.content).toHaveLength(6);
  729. expect(response.content[0]).toEqual({ type: "text", text: "answer 1 answer 2\n" });
  730. expect(response.content[1]).toEqual({
  731. type: "thinking",
  732. thinking: "think 1 think 2",
  733. thinkingSignature: "reasoning_content",
  734. });
  735. const readCall = response.content[2];
  736. const grepCall = response.content[3];
  737. const listCall = response.content[4];
  738. const writeCall = response.content[5];
  739. expect(readCall.type).toBe("toolCall");
  740. expect(grepCall.type).toBe("toolCall");
  741. expect(listCall.type).toBe("toolCall");
  742. expect(writeCall.type).toBe("toolCall");
  743. if (
  744. readCall.type !== "toolCall" ||
  745. grepCall.type !== "toolCall" ||
  746. listCall.type !== "toolCall" ||
  747. writeCall.type !== "toolCall"
  748. ) {
  749. throw new Error("Expected toolCall content");
  750. }
  751. expect(readCall.id).toBe("tc_read_initial");
  752. expect(readCall.name).toBe("read");
  753. expect(readCall.arguments).toEqual({ path: "README.md" });
  754. expect(readCall).not.toHaveProperty("streamIndex");
  755. expect(readCall).not.toHaveProperty("partialArgs");
  756. expect(grepCall.id).toBe("tc_grep_initial");
  757. expect(grepCall.name).toBe("grep");
  758. expect(grepCall.arguments).toEqual({ pattern: "TODO", path: "src" });
  759. expect(grepCall).not.toHaveProperty("streamIndex");
  760. expect(grepCall).not.toHaveProperty("partialArgs");
  761. expect(listCall.id).toBe("tc_list_no_index");
  762. expect(listCall.name).toBe("list");
  763. expect(listCall.arguments).toEqual({ path: "packages/ai" });
  764. expect(listCall).not.toHaveProperty("streamIndex");
  765. expect(listCall).not.toHaveProperty("partialArgs");
  766. expect(writeCall.id).toBe("tc_write_no_index");
  767. expect(writeCall.name).toBe("write");
  768. expect(writeCall.arguments).toEqual({ path: "out.txt", content: "ok" });
  769. expect(writeCall).not.toHaveProperty("streamIndex");
  770. expect(writeCall).not.toHaveProperty("partialArgs");
  771. });
  772. it("uses system messages for non-OpenAI/Anthropic OpenRouter reasoning model instructions", async () => {
  773. const model = getModel("openrouter", "deepseek/deepseek-v4-pro")!;
  774. let payload: unknown;
  775. await streamSimple(
  776. model,
  777. {
  778. systemPrompt: "Follow instructions.",
  779. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  780. },
  781. {
  782. apiKey: "test",
  783. onPayload: (params: unknown) => {
  784. payload = params;
  785. },
  786. },
  787. ).result();
  788. const params = payload as { messages?: Array<{ role?: string }> };
  789. expect(params.messages?.[0]?.role).toBe("system");
  790. });
  791. it("keeps developer messages for OpenAI and Anthropic OpenRouter reasoning model instructions", async () => {
  792. for (const model of [
  793. getModel("openrouter", "openai/gpt-5.2-codex"),
  794. getModel("openrouter", "anthropic/claude-sonnet-4.5"),
  795. ]) {
  796. expect(model).toBeDefined();
  797. let payload: unknown;
  798. await streamSimple(
  799. model!,
  800. {
  801. systemPrompt: "Follow instructions.",
  802. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  803. },
  804. {
  805. apiKey: "test",
  806. onPayload: (params: unknown) => {
  807. payload = params;
  808. },
  809. },
  810. ).result();
  811. const params = payload as { messages?: Array<{ role?: string }> };
  812. expect(params.messages?.[0]?.role).toBe("developer");
  813. }
  814. });
  815. it("keeps developer messages for OpenAI reasoning model instructions", async () => {
  816. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-5.5")!;
  817. const model = { ...baseModel, api: "openai-completions" } as const;
  818. let payload: unknown;
  819. await streamSimple(
  820. model,
  821. {
  822. systemPrompt: "Follow instructions.",
  823. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  824. },
  825. {
  826. apiKey: "test",
  827. onPayload: (params: unknown) => {
  828. payload = params;
  829. },
  830. },
  831. ).result();
  832. const params = payload as { messages?: Array<{ role?: string }> };
  833. expect(params.messages?.[0]?.role).toBe("developer");
  834. });
  835. it("stores OpenRouter Kimi K2.6 reasoning replay compat in built-in metadata", () => {
  836. for (const modelId of ["moonshotai/kimi-k2.6", "moonshotai/kimi-k2.6:free"] as const) {
  837. const model = getModel("openrouter", modelId)!;
  838. expect(model.compat?.supportsDeveloperRole).toBe(false);
  839. expect(model.compat?.requiresReasoningContentOnAssistantMessages).toBe(true);
  840. }
  841. });
  842. it("stores Xiaomi MiMo reasoning replay compat in built-in metadata", () => {
  843. const providers = ["xiaomi", "xiaomi-token-plan-cn", "xiaomi-token-plan-ams", "xiaomi-token-plan-sgp"] as const;
  844. for (const provider of providers) {
  845. const model = getModel(provider, "mimo-v2.5-pro")!;
  846. expect(model.compat?.requiresReasoningContentOnAssistantMessages).toBe(true);
  847. expect(model.compat?.thinkingFormat).toBe("deepseek");
  848. expect(model.compat?.maxTokensField).toBeUndefined();
  849. expect(model.compat?.supportsDeveloperRole).toBeUndefined();
  850. }
  851. });
  852. it("replays Xiaomi MiMo assistant tool calls with empty reasoning_content when thinking is missing", async () => {
  853. const model = getModel("xiaomi", "mimo-v2.5-pro")!;
  854. const assistantMessage: AssistantMessage = {
  855. role: "assistant",
  856. api: "openai-completions",
  857. provider: "xiaomi",
  858. model: "mimo-v2.5-pro",
  859. content: [{ type: "toolCall", id: "call_1", name: "read", arguments: { path: "README.md" } }],
  860. usage: {
  861. input: 0,
  862. output: 0,
  863. cacheRead: 0,
  864. cacheWrite: 0,
  865. totalTokens: 0,
  866. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  867. },
  868. stopReason: "toolUse",
  869. timestamp: Date.now(),
  870. };
  871. const toolResult: ToolResultMessage = {
  872. role: "toolResult",
  873. toolCallId: "call_1",
  874. toolName: "read",
  875. content: [{ type: "text", text: "contents" }],
  876. isError: false,
  877. timestamp: Date.now(),
  878. };
  879. let payload: unknown;
  880. await streamSimple(
  881. model,
  882. {
  883. messages: [
  884. { role: "user", content: "Read README.md", timestamp: Date.now() },
  885. assistantMessage,
  886. toolResult,
  887. ],
  888. },
  889. {
  890. apiKey: "test",
  891. reasoning: "high",
  892. onPayload: (params: unknown) => {
  893. payload = params;
  894. },
  895. },
  896. ).result();
  897. const params = (payload ?? mockState.lastParams) as {
  898. messages?: Array<Record<string, unknown>>;
  899. thinking?: { type?: string };
  900. reasoning_effort?: string;
  901. };
  902. const replayedAssistant = params.messages?.find((message) => message.role === "assistant");
  903. expect(replayedAssistant).toMatchObject({ role: "assistant", reasoning_content: "" });
  904. expect(params.thinking).toEqual({ type: "enabled" });
  905. expect(params.reasoning_effort).toBe("high");
  906. });
  907. it("normalizes OpenCode Go reasoning deltas to reasoning_content for replay", async () => {
  908. mockState.chunks = [
  909. {
  910. id: "chatcmpl-opencode-go-reasoning",
  911. choices: [{ delta: { reasoning: "think" }, finish_reason: "stop" }],
  912. },
  913. ];
  914. const { compat: _compat, ...baseModel } = getModel("opencode-go", "kimi-k2.6")!;
  915. const model = { ...baseModel, api: "openai-completions" } as const;
  916. const response = await streamSimple(
  917. model,
  918. {
  919. messages: [{ role: "user", content: "Use reasoning.", timestamp: Date.now() }],
  920. },
  921. { apiKey: "test" },
  922. ).result();
  923. expect(response.content).toEqual([
  924. {
  925. type: "thinking",
  926. thinking: "think",
  927. thinkingSignature: "reasoning_content",
  928. },
  929. ]);
  930. });
  931. it("keeps non-OpenCode Go reasoning deltas on the original reasoning field", async () => {
  932. mockState.chunks = [
  933. {
  934. id: "chatcmpl-reasoning",
  935. choices: [{ delta: { reasoning: "think" }, finish_reason: "stop" }],
  936. },
  937. ];
  938. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  939. const model = { ...baseModel, api: "openai-completions" } as const;
  940. const response = await streamSimple(
  941. model,
  942. {
  943. messages: [{ role: "user", content: "Use reasoning.", timestamp: Date.now() }],
  944. },
  945. { apiKey: "test" },
  946. ).result();
  947. expect(response.content).toEqual([
  948. {
  949. type: "thinking",
  950. thinking: "think",
  951. thinkingSignature: "reasoning",
  952. },
  953. ]);
  954. });
  955. it("replays OpenCode Go reasoning thinking blocks as reasoning_content", () => {
  956. const { compat: _compat, ...baseModel } = getModel("opencode-go", "kimi-k2.6")!;
  957. const model = { ...baseModel, api: "openai-completions" } as Model<"openai-completions">;
  958. const messages = convertMessages(
  959. model,
  960. {
  961. messages: [
  962. {
  963. role: "assistant",
  964. api: "openai-completions",
  965. provider: "opencode-go",
  966. model: "kimi-k2.6",
  967. content: [
  968. { type: "thinking", thinking: "think", thinkingSignature: "reasoning" },
  969. { type: "toolCall", id: "call_1", name: "read", arguments: { path: "README.md" } },
  970. ],
  971. usage: {
  972. input: 0,
  973. output: 0,
  974. cacheRead: 0,
  975. cacheWrite: 0,
  976. totalTokens: 0,
  977. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  978. },
  979. stopReason: "stop",
  980. timestamp: Date.now(),
  981. },
  982. ],
  983. },
  984. {
  985. ...model.compat,
  986. supportsStore: false,
  987. supportsDeveloperRole: false,
  988. supportsReasoningEffort: true,
  989. supportsUsageInStreaming: true,
  990. maxTokensField: "max_completion_tokens",
  991. requiresToolResultName: false,
  992. requiresAssistantAfterToolResult: false,
  993. requiresThinkingAsText: false,
  994. requiresReasoningContentOnAssistantMessages: false,
  995. thinkingFormat: "openai",
  996. openRouterRouting: {},
  997. vercelGatewayRouting: {},
  998. zaiToolStream: false,
  999. supportsStrictMode: true,
  1000. sendSessionAffinityHeaders: false,
  1001. supportsLongCacheRetention: true,
  1002. },
  1003. );
  1004. expect(messages[0]).toMatchObject({ role: "assistant", reasoning_content: "think" });
  1005. expect(messages[0]).not.toHaveProperty("reasoning");
  1006. });
  1007. it("sends thinking disabled for OpenCode Go Kimi K2.6 when thinking is off", async () => {
  1008. const model = getModel("opencode-go", "kimi-k2.6")!;
  1009. let payload: unknown;
  1010. await streamSimple(
  1011. model,
  1012. {
  1013. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1014. },
  1015. {
  1016. apiKey: "test",
  1017. onPayload: (params: unknown) => {
  1018. payload = params;
  1019. },
  1020. },
  1021. ).result();
  1022. const params = (payload ?? mockState.lastParams) as { thinking?: unknown; reasoning_effort?: string };
  1023. expect(params.thinking).toEqual({ type: "disabled" });
  1024. expect(params.reasoning_effort).toBeUndefined();
  1025. });
  1026. it("sends thinking enabled for OpenCode Go Kimi K2.6 when thinking is enabled", async () => {
  1027. const model = getModel("opencode-go", "kimi-k2.6")!;
  1028. let payload: unknown;
  1029. await streamSimple(
  1030. model,
  1031. {
  1032. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1033. },
  1034. {
  1035. apiKey: "test",
  1036. reasoning: "high",
  1037. onPayload: (params: unknown) => {
  1038. payload = params;
  1039. },
  1040. },
  1041. ).result();
  1042. const params = (payload ?? mockState.lastParams) as { thinking?: unknown; reasoning_effort?: string };
  1043. expect(params.thinking).toEqual({ type: "enabled" });
  1044. expect(params.reasoning_effort).toBeUndefined();
  1045. });
  1046. it("sends max_tokens for OpenCode completions models", async () => {
  1047. const cases = [getModel("opencode-go", "kimi-k2.6")!, getModel("opencode", "grok-build-0.1")!] as const;
  1048. for (const model of cases) {
  1049. let payload: unknown;
  1050. expect(model.compat?.maxTokensField).toBe("max_tokens");
  1051. await streamSimple(
  1052. model,
  1053. {
  1054. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1055. },
  1056. {
  1057. apiKey: "test",
  1058. maxTokens: 123,
  1059. onPayload: (params: unknown) => {
  1060. payload = params;
  1061. },
  1062. },
  1063. ).result();
  1064. const params = (payload ?? mockState.lastParams) as { max_tokens?: number; max_completion_tokens?: number };
  1065. expect(params.max_tokens).toBe(123);
  1066. expect(params.max_completion_tokens).toBeUndefined();
  1067. }
  1068. });
  1069. it("omits reasoning effort for OpenCode Grok Build", async () => {
  1070. const model = getModel("opencode", "grok-build-0.1")!;
  1071. let payload: unknown;
  1072. await streamSimple(
  1073. model,
  1074. {
  1075. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1076. },
  1077. {
  1078. apiKey: "test",
  1079. reasoning: "high",
  1080. onPayload: (params: unknown) => {
  1081. payload = params;
  1082. },
  1083. },
  1084. ).result();
  1085. const params = (payload ?? mockState.lastParams) as { reasoning_effort?: string };
  1086. expect(params.reasoning_effort).toBeUndefined();
  1087. });
  1088. it("does not double-count reasoning tokens in completion usage", async () => {
  1089. mockState.chunks = [
  1090. {
  1091. id: "chatcmpl-reasoning-usage",
  1092. choices: [{ delta: {}, finish_reason: "stop" }],
  1093. usage: {
  1094. prompt_tokens: 10,
  1095. completion_tokens: 33,
  1096. prompt_tokens_details: { cached_tokens: 0 },
  1097. completion_tokens_details: { reasoning_tokens: 21 },
  1098. },
  1099. },
  1100. ];
  1101. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  1102. const model = { ...baseModel, api: "openai-completions" } as const;
  1103. const response = await streamSimple(
  1104. model,
  1105. {
  1106. messages: [
  1107. {
  1108. role: "user",
  1109. content: "Use reasoning.",
  1110. timestamp: Date.now(),
  1111. },
  1112. ],
  1113. },
  1114. { apiKey: "test" },
  1115. ).result();
  1116. expect(response.usage.input).toBe(10);
  1117. expect(response.usage.output).toBe(33);
  1118. expect(response.usage.totalTokens).toBe(43);
  1119. });
  1120. it("preserves prompt_tokens_details cache read/write fields from chunk usage", async () => {
  1121. mockState.chunks = [
  1122. {
  1123. id: "chatcmpl-cache-write",
  1124. choices: [{ delta: { content: "OK" }, finish_reason: null }],
  1125. },
  1126. {
  1127. id: "chatcmpl-cache-write",
  1128. choices: [{ delta: {}, finish_reason: "stop" }],
  1129. usage: {
  1130. prompt_tokens: 100,
  1131. completion_tokens: 5,
  1132. prompt_tokens_details: { cached_tokens: 50, cache_write_tokens: 30 },
  1133. completion_tokens_details: { reasoning_tokens: 0 },
  1134. },
  1135. },
  1136. ];
  1137. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  1138. const model = { ...baseModel, api: "openai-completions" } as const;
  1139. const response = await streamSimple(
  1140. model,
  1141. {
  1142. messages: [
  1143. {
  1144. role: "user",
  1145. content: "Reply with exactly OK",
  1146. timestamp: Date.now(),
  1147. },
  1148. ],
  1149. },
  1150. { apiKey: "test" },
  1151. ).result();
  1152. // cached_tokens is documented as cache reads; cache_write_tokens is separate.
  1153. expect(response.usage.input).toBe(20);
  1154. expect(response.usage.cacheRead).toBe(50);
  1155. expect(response.usage.cacheWrite).toBe(30);
  1156. expect(response.usage.totalTokens).toBe(105);
  1157. });
  1158. it("preserves prompt_tokens_details cache read/write fields from choice usage fallback", async () => {
  1159. mockState.chunks = [
  1160. {
  1161. id: "chatcmpl-cache-write-choice",
  1162. choices: [{ delta: { content: "OK" }, finish_reason: null }],
  1163. },
  1164. {
  1165. id: "chatcmpl-cache-write-choice",
  1166. choices: [
  1167. {
  1168. delta: {},
  1169. finish_reason: "stop",
  1170. usage: {
  1171. prompt_tokens: 100,
  1172. completion_tokens: 5,
  1173. prompt_tokens_details: { cached_tokens: 50, cache_write_tokens: 30 },
  1174. completion_tokens_details: { reasoning_tokens: 0 },
  1175. },
  1176. },
  1177. ],
  1178. },
  1179. ];
  1180. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini")!;
  1181. const model = { ...baseModel, api: "openai-completions" } as const;
  1182. const response = await streamSimple(
  1183. model,
  1184. {
  1185. messages: [
  1186. {
  1187. role: "user",
  1188. content: "Reply with exactly OK",
  1189. timestamp: Date.now(),
  1190. },
  1191. ],
  1192. },
  1193. { apiKey: "test" },
  1194. ).result();
  1195. // cached_tokens is documented as cache reads; cache_write_tokens is separate.
  1196. expect(response.usage.input).toBe(20);
  1197. expect(response.usage.cacheRead).toBe(50);
  1198. expect(response.usage.cacheWrite).toBe(30);
  1199. expect(response.usage.totalTokens).toBe(105);
  1200. });
  1201. it("uses OpenRouter reasoning object instead of reasoning_effort", async () => {
  1202. const model = getModel("openrouter", "deepseek/deepseek-r1")!;
  1203. let payload: unknown;
  1204. await streamSimple(
  1205. model,
  1206. {
  1207. messages: [
  1208. {
  1209. role: "user",
  1210. content: "Hi",
  1211. timestamp: Date.now(),
  1212. },
  1213. ],
  1214. },
  1215. {
  1216. apiKey: "test",
  1217. reasoning: "high",
  1218. onPayload: (params: unknown) => {
  1219. payload = params;
  1220. },
  1221. },
  1222. ).result();
  1223. const params = (payload ?? mockState.lastParams) as {
  1224. reasoning?: { effort?: string };
  1225. reasoning_effort?: string;
  1226. };
  1227. expect(params.reasoning).toEqual({ effort: "high" });
  1228. expect(params.reasoning_effort).toBeUndefined();
  1229. });
  1230. it("uses Ant Ling compatibility metadata", async () => {
  1231. const model = getModel("ant-ling", "Ring-2.6-1T")!;
  1232. let payload: unknown;
  1233. expect(model.compat).toMatchObject({
  1234. supportsStore: false,
  1235. supportsDeveloperRole: false,
  1236. supportsReasoningEffort: false,
  1237. maxTokensField: "max_tokens",
  1238. thinkingFormat: "ant-ling",
  1239. supportsLongCacheRetention: false,
  1240. });
  1241. expect(model.compat?.supportsStrictMode).toBeUndefined();
  1242. expect(model.compat?.requiresReasoningContentOnAssistantMessages).toBeUndefined();
  1243. await streamSimple(
  1244. model,
  1245. {
  1246. systemPrompt: "Follow instructions.",
  1247. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1248. },
  1249. {
  1250. apiKey: "test",
  1251. maxTokens: 123,
  1252. reasoning: "high",
  1253. cacheRetention: "long",
  1254. sessionId: "ant-ling-session",
  1255. onPayload: (params: unknown) => {
  1256. payload = params;
  1257. },
  1258. },
  1259. ).result();
  1260. const params = (payload ?? mockState.lastParams) as {
  1261. max_tokens?: number;
  1262. max_completion_tokens?: number;
  1263. messages?: Array<{ role?: string }>;
  1264. reasoning?: { effort?: string };
  1265. reasoning_effort?: string;
  1266. store?: boolean;
  1267. prompt_cache_key?: string;
  1268. prompt_cache_retention?: string;
  1269. };
  1270. expect(params.max_tokens).toBe(123);
  1271. expect(params.max_completion_tokens).toBeUndefined();
  1272. expect(params.messages?.[0]?.role).toBe("system");
  1273. expect(params.reasoning).toEqual({ effort: "high" });
  1274. expect(params.reasoning_effort).toBeUndefined();
  1275. expect(params.store).toBeUndefined();
  1276. expect(params.prompt_cache_key).toBeUndefined();
  1277. expect(params.prompt_cache_retention).toBeUndefined();
  1278. });
  1279. it("omits Ant Ling reasoning for unmapped direct reasoning efforts and non-reasoning models", async () => {
  1280. const ring = getModel("ant-ling", "Ring-2.6-1T")!;
  1281. let payload: unknown;
  1282. await stream(
  1283. ring,
  1284. {
  1285. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1286. },
  1287. {
  1288. apiKey: "test",
  1289. reasoningEffort: "medium",
  1290. onPayload: (params: unknown) => {
  1291. payload = params;
  1292. },
  1293. },
  1294. ).result();
  1295. expect((payload ?? mockState.lastParams) as { reasoning?: unknown }).not.toHaveProperty("reasoning");
  1296. const ling = getModel("ant-ling", "Ling-2.6-flash")!;
  1297. await streamSimple(
  1298. ling,
  1299. {
  1300. messages: [{ role: "user", content: "Hi", timestamp: Date.now() }],
  1301. },
  1302. {
  1303. apiKey: "test",
  1304. reasoning: "high",
  1305. onPayload: (params: unknown) => {
  1306. payload = params;
  1307. },
  1308. },
  1309. ).result();
  1310. expect((payload ?? mockState.lastParams) as { reasoning?: unknown }).not.toHaveProperty("reasoning");
  1311. });
  1312. });