tool-input-normalization.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. import { describe, expect, it, vi } from "vitest";
  2. import { ModelErrorCode } from "@zcode/contracts";
  3. import type { AiSdkModelRuntime } from "../src/model/runner.js";
  4. import {
  5. TestAiSdkModelAdapter as AiSdkModelAdapter,
  6. TestProviderConfigFixture,
  7. } from "./test-provider-config.js";
  8. import { normalizeModelToolInput } from "../src/model/tool-input-normalization.js";
  9. import { executeAdapterGenerateText, executeAdapterStreamText } from "./test-adapter-model.js";
  10. describe("model tool input normalization", () => {
  11. const feedbackMalformedCases = [
  12. {
  13. input:
  14. '{"file_path":"F:\\\\clash-verge-fix.bat","content":"@echo off\\nsetlocal EnableExtensions',
  15. ticket: "ZCT-2072501999367512064",
  16. toolName: "Write",
  17. },
  18. {
  19. input: '{"content":"# PenglaiAgent v0.3.5 深度审计报告\\n**High**: H1',
  20. ticket: "ZCT-2072869553480470528",
  21. toolName: "Write",
  22. },
  23. {
  24. input: '{"content":"# 第二章、第三章 投标文件组织提纲\\n| 4 | 桥梁数量 |',
  25. ticket: "ZCT-2072928903530250240",
  26. toolName: "Write",
  27. },
  28. {
  29. input: '{"content":"<!DOCTYPE html>\\n<html lang=\\"zh-CN\\">\\n<th',
  30. ticket: "ZCT-2072970699099508736",
  31. toolName: "Write",
  32. },
  33. {
  34. input:
  35. '{"questions":[{"options":[{"description":"相当于把 2.5D 当作"3D 视角"的初始预设。"}]}]}',
  36. ticket: "ZCT-2072321679810314240",
  37. toolName: "AskUserQuestion",
  38. },
  39. {
  40. input: '{"questions":[{"description":"bad\\u"}]}',
  41. ticket: "ZCT-2072582003671117824",
  42. toolName: "AskUserQuestion",
  43. },
  44. ];
  45. it("safe-parses stringified tool inputs returned from generateText", async () => {
  46. const runtime: AiSdkModelRuntime = {
  47. async generateText() {
  48. return {
  49. finishReason: "tool-calls",
  50. text: "",
  51. toolCalls: [
  52. {
  53. toolCallId: "call_bash",
  54. toolName: "Bash",
  55. input: '{"command":"python3 -c \\"print(1)\\""}',
  56. },
  57. ],
  58. totalUsage: usage(2, 1),
  59. usage: usage(2, 1),
  60. } as never;
  61. },
  62. streamText() {
  63. throw new Error("not used");
  64. },
  65. };
  66. const adapter = new AiSdkModelAdapter({
  67. registry: registry(),
  68. runtime,
  69. });
  70. const result = await executeAdapterGenerateText(adapter, {
  71. providerId: "test" as never,
  72. modelId: "model-a" as never,
  73. messages: [{ role: "user", content: "Run a command" }],
  74. });
  75. expect(result.toolCalls).toEqual([
  76. {
  77. id: "call_bash",
  78. name: "Bash",
  79. input: {
  80. command: 'python3 -c "print(1)"',
  81. },
  82. },
  83. ]);
  84. });
  85. it("accepts a byte-order mark before otherwise valid tool input JSON", async () => {
  86. const result = await generateTextWithToolInput({
  87. input: '\uFEFF{"command":"pwd"}',
  88. toolCallId: "call_bom",
  89. toolName: "Bash",
  90. });
  91. expect(result.toolCalls).toEqual([
  92. {
  93. id: "call_bom",
  94. input: { command: "pwd" },
  95. name: "Bash",
  96. },
  97. ]);
  98. });
  99. it("degrades JSON null to an empty object through the malformed input path", () => {
  100. const warn = vi.fn();
  101. expect(
  102. normalizeModelToolInput("null", {
  103. logger: testLogger(warn),
  104. source: "streamText",
  105. toolName: "Read",
  106. }),
  107. ).toEqual({});
  108. expect(warn).toHaveBeenCalledOnce();
  109. expect(warn).toHaveBeenCalledWith(
  110. "Model tool input JSON normalization failed",
  111. expect.objectContaining({
  112. inputLength: 4,
  113. parseErrorType: "TypeError",
  114. recovery: "empty_object",
  115. source: "streamText",
  116. status: "failed",
  117. toolName: "Read",
  118. }),
  119. );
  120. });
  121. it("degrades upstream-normalized null without inventing a raw input length", () => {
  122. const warn = vi.fn();
  123. expect(
  124. normalizeModelToolInput(null, {
  125. logger: testLogger(warn),
  126. source: "streamText",
  127. toolName: "Read",
  128. }),
  129. ).toEqual({});
  130. expect(warn).toHaveBeenCalledOnce();
  131. const [, context] = warn.mock.calls[0] ?? [];
  132. expect(context).toEqual(
  133. expect.objectContaining({
  134. inputType: "null",
  135. parseErrorType: "TypeError",
  136. recovery: "empty_object",
  137. source: "streamText",
  138. status: "failed",
  139. toolName: "Read",
  140. }),
  141. );
  142. expect(context).not.toHaveProperty("inputLength");
  143. });
  144. it.each([
  145. { input: undefined, name: "missing input" },
  146. { input: "", name: "empty string" },
  147. ])("degrades $name to an empty object without a parse warning", ({ input }) => {
  148. const warn = vi.fn();
  149. expect(
  150. normalizeModelToolInput(input, {
  151. logger: testLogger(warn),
  152. source: "streamText",
  153. toolName: "Read",
  154. }),
  155. ).toEqual({});
  156. expect(warn).not.toHaveBeenCalled();
  157. });
  158. it("does not let legacy args replace an explicit null generateText input", async () => {
  159. const warn = vi.fn();
  160. const runtime: AiSdkModelRuntime = {
  161. async generateText() {
  162. return {
  163. finishReason: "tool-calls",
  164. text: "",
  165. toolCalls: [
  166. {
  167. args: { file_path: "wrong.md" },
  168. input: null,
  169. toolCallId: "call_null",
  170. toolName: "Read",
  171. },
  172. ],
  173. totalUsage: usage(2, 1),
  174. usage: usage(2, 1),
  175. } as never;
  176. },
  177. streamText() {
  178. throw new Error("not used");
  179. },
  180. };
  181. const adapter = new AiSdkModelAdapter({
  182. logger: testLogger(warn),
  183. registry: registry(),
  184. runtime,
  185. });
  186. const result = await executeAdapterGenerateText(adapter, {
  187. providerId: "test" as never,
  188. modelId: "model-a" as never,
  189. messages: [{ role: "user", content: "Read a file" }],
  190. });
  191. expect(result.toolCalls).toEqual([
  192. {
  193. id: "call_null",
  194. input: {},
  195. name: "Read",
  196. },
  197. ]);
  198. expect(warn).toHaveBeenCalledOnce();
  199. });
  200. it("keeps legacy generateText args fallback when input is missing", async () => {
  201. const runtime: AiSdkModelRuntime = {
  202. async generateText() {
  203. return {
  204. finishReason: "tool-calls",
  205. text: "",
  206. toolCalls: [
  207. {
  208. args: { file_path: "README.md" },
  209. toolCallId: "call_legacy_args",
  210. toolName: "Read",
  211. },
  212. ],
  213. totalUsage: usage(2, 1),
  214. usage: usage(2, 1),
  215. } as never;
  216. },
  217. streamText() {
  218. throw new Error("not used");
  219. },
  220. };
  221. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  222. const result = await executeAdapterGenerateText(adapter, {
  223. providerId: "test" as never,
  224. modelId: "model-a" as never,
  225. messages: [{ role: "user", content: "Read a file" }],
  226. });
  227. expect(result.toolCalls).toEqual([
  228. {
  229. id: "call_legacy_args",
  230. input: { file_path: "README.md" },
  231. name: "Read",
  232. },
  233. ]);
  234. });
  235. it("degrades partial generateText JSON to an empty object without repairing it", async () => {
  236. const runtime: AiSdkModelRuntime = {
  237. async generateText() {
  238. return {
  239. finishReason: "tool-calls",
  240. text: "",
  241. toolCalls: [
  242. {
  243. toolCallId: "call_bad",
  244. toolName: "Bash",
  245. input: '{"command":',
  246. },
  247. ],
  248. totalUsage: usage(2, 1),
  249. usage: usage(2, 1),
  250. } as never;
  251. },
  252. streamText() {
  253. throw new Error("not used");
  254. },
  255. };
  256. const adapter = new AiSdkModelAdapter({
  257. registry: registry(),
  258. runtime,
  259. });
  260. const result = await executeAdapterGenerateText(adapter, {
  261. providerId: "test" as never,
  262. modelId: "model-a" as never,
  263. messages: [{ role: "user", content: "Run a command" }],
  264. });
  265. expect(result.toolCalls).toEqual([
  266. {
  267. id: "call_bad",
  268. name: "Bash",
  269. input: {},
  270. },
  271. ]);
  272. });
  273. it("recovers an unparseable generateText tool call without retaining the raw input", async () => {
  274. const malformedInput = '{"command":"bad\\u"}';
  275. const warn = vi.fn();
  276. const runtime: AiSdkModelRuntime = {
  277. async generateText() {
  278. return {
  279. finishReason: "tool-calls",
  280. text: "",
  281. toolCalls: [
  282. {
  283. toolCallId: "call_bad_escape",
  284. toolName: "Bash",
  285. input: malformedInput,
  286. },
  287. ],
  288. totalUsage: usage(2, 1),
  289. usage: usage(2, 1),
  290. } as never;
  291. },
  292. streamText() {
  293. throw new Error("not used");
  294. },
  295. };
  296. const adapter = new AiSdkModelAdapter({
  297. logger: testLogger(warn),
  298. registry: registry(),
  299. runtime,
  300. });
  301. const result = await executeAdapterGenerateText(adapter, {
  302. providerId: "test" as never,
  303. modelId: "model-a" as never,
  304. messages: [{ role: "user", content: "Run a command" }],
  305. });
  306. expect(result.toolCalls).toEqual([
  307. {
  308. id: "call_bad_escape",
  309. input: {},
  310. name: "Bash",
  311. },
  312. ]);
  313. const normalizationWarnings = warn.mock.calls.filter(
  314. ([message]) => message === "Model tool input JSON normalization failed",
  315. );
  316. expect(normalizationWarnings).toHaveLength(1);
  317. expect(normalizationWarnings[0]).toEqual([
  318. "Model tool input JSON normalization failed",
  319. expect.objectContaining({
  320. inputLength: malformedInput.length,
  321. parseErrorType: "SyntaxError",
  322. recovery: "empty_object",
  323. source: "generateText",
  324. status: "failed",
  325. toolName: "Bash",
  326. }),
  327. ]);
  328. expect(JSON.stringify(result.toolCalls)).not.toContain(malformedInput);
  329. expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
  330. });
  331. it.each(feedbackMalformedCases)(
  332. "strictly degrades malformed tool input observed in feedback $ticket",
  333. async ({ input, ticket, toolName }) => {
  334. const result = await generateTextWithToolInput({
  335. input,
  336. toolName,
  337. toolCallId: `call_${ticket}`,
  338. });
  339. expect(result.toolCalls).toEqual([
  340. {
  341. id: `call_${ticket}`,
  342. name: toolName,
  343. input: {},
  344. },
  345. ]);
  346. expect(JSON.stringify(result.toolCalls)).not.toContain(input);
  347. },
  348. );
  349. it("safe-parses stringified tool inputs in streamed tool_call events", async () => {
  350. const runtime: AiSdkModelRuntime = {
  351. async generateText() {
  352. throw new Error("not used");
  353. },
  354. streamText() {
  355. return {
  356. fullStream: stream([
  357. { type: "start" },
  358. {
  359. type: "tool-call",
  360. toolCallId: "call_read",
  361. toolName: "Read",
  362. input: '{"file_path":"README.md"}',
  363. },
  364. {
  365. type: "finish",
  366. finishReason: "tool-calls",
  367. rawFinishReason: undefined,
  368. totalUsage: usage(2, 1),
  369. },
  370. ]),
  371. } as never;
  372. },
  373. };
  374. const adapter = new AiSdkModelAdapter({
  375. registry: registry(),
  376. runtime,
  377. });
  378. const events = [];
  379. for await (const event of executeAdapterStreamText(adapter, {
  380. providerId: "test" as never,
  381. modelId: "model-a" as never,
  382. messages: [{ role: "user", content: "Read README" }],
  383. })) {
  384. events.push(event);
  385. }
  386. expect(events).toEqual([
  387. { type: "start" },
  388. {
  389. type: "tool_call",
  390. toolCall: {
  391. id: "call_read",
  392. name: "Read",
  393. input: { file_path: "README.md" },
  394. },
  395. },
  396. {
  397. type: "finish",
  398. finishReason: "tool-calls",
  399. providerMetadata: undefined,
  400. usage: {
  401. inputTokens: 2,
  402. outputTokens: 1,
  403. totalTokens: 3,
  404. cacheReadTokens: undefined,
  405. cacheWriteTokens: undefined,
  406. reasoningTokens: undefined,
  407. },
  408. },
  409. ]);
  410. });
  411. it("recovers a malformed atomic streamed tool_call as an empty object", async () => {
  412. const malformedInput =
  413. '{"questions":[{"options":[{"description":"相当于把 2.5D 当作"3D 视角"的初始预设。"}]}]}';
  414. const warn = vi.fn();
  415. const runtime: AiSdkModelRuntime = {
  416. async generateText() {
  417. throw new Error("not used");
  418. },
  419. streamText() {
  420. return {
  421. fullStream: stream([
  422. { type: "start" },
  423. {
  424. type: "tool-call",
  425. toolCallId: "call_ask_malformed",
  426. toolName: "AskUserQuestion",
  427. input: malformedInput,
  428. },
  429. {
  430. type: "finish",
  431. finishReason: "tool-calls",
  432. rawFinishReason: undefined,
  433. totalUsage: usage(2, 1),
  434. },
  435. ]),
  436. } as never;
  437. },
  438. };
  439. const adapter = new AiSdkModelAdapter({
  440. logger: testLogger(warn),
  441. registry: registry(),
  442. runtime,
  443. });
  444. const events = [];
  445. for await (const event of executeAdapterStreamText(adapter, {
  446. providerId: "test" as never,
  447. modelId: "model-a" as never,
  448. messages: [{ role: "user", content: "Read README" }],
  449. })) {
  450. events.push(event);
  451. }
  452. expect(events).toEqual([
  453. { type: "start" },
  454. {
  455. type: "tool_call",
  456. toolCall: {
  457. id: "call_ask_malformed",
  458. name: "AskUserQuestion",
  459. input: {},
  460. },
  461. },
  462. {
  463. type: "finish",
  464. finishReason: "tool-calls",
  465. providerMetadata: undefined,
  466. usage: {
  467. inputTokens: 2,
  468. outputTokens: 1,
  469. totalTokens: 3,
  470. cacheReadTokens: undefined,
  471. cacheWriteTokens: undefined,
  472. reasoningTokens: undefined,
  473. },
  474. },
  475. ]);
  476. expect(warn).toHaveBeenCalledTimes(1);
  477. expect(warn).toHaveBeenCalledWith(
  478. "Model tool input JSON normalization failed",
  479. expect.objectContaining({
  480. inputLength: malformedInput.length,
  481. source: "streamText",
  482. toolName: "AskUserQuestion",
  483. }),
  484. );
  485. expect(JSON.stringify(events)).not.toContain(malformedInput);
  486. expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
  487. });
  488. it.each(["", " "])("preserves recoverable generateText tool name %j", async (toolName) => {
  489. const runtime: AiSdkModelRuntime = {
  490. async generateText() {
  491. return {
  492. finishReason: "tool-calls",
  493. text: "",
  494. toolCalls: [
  495. {
  496. toolCallId: "call_empty",
  497. toolName,
  498. input: { offset: 48 },
  499. },
  500. ],
  501. totalUsage: usage(2, 1),
  502. usage: usage(2, 1),
  503. } as never;
  504. },
  505. streamText() {
  506. throw new Error("not used");
  507. },
  508. };
  509. const adapter = new AiSdkModelAdapter({
  510. registry: registry(),
  511. runtime,
  512. });
  513. await expect(
  514. executeAdapterGenerateText(adapter, {
  515. providerId: "test" as never,
  516. modelId: "model-a" as never,
  517. messages: [{ role: "user", content: "Read more" }],
  518. }),
  519. ).resolves.toMatchObject({
  520. toolCalls: [
  521. {
  522. id: "call_empty",
  523. input: { offset: 48 },
  524. name: toolName,
  525. },
  526. ],
  527. });
  528. });
  529. it.each([undefined, null, 42, { invalid: true }])(
  530. "rejects generateText tool name %j",
  531. async (toolName) => {
  532. const runtime: AiSdkModelRuntime = {
  533. async generateText() {
  534. return {
  535. finishReason: "tool-calls",
  536. text: "",
  537. toolCalls: [
  538. {
  539. toolCallId: "call_invalid_name",
  540. toolName,
  541. input: {},
  542. },
  543. ],
  544. totalUsage: usage(2, 1),
  545. usage: usage(2, 1),
  546. } as never;
  547. },
  548. streamText() {
  549. throw new Error("not used");
  550. },
  551. };
  552. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  553. await expect(
  554. executeAdapterGenerateText(adapter, {
  555. providerId: "test" as never,
  556. modelId: "model-a" as never,
  557. messages: [{ role: "user", content: "Read more" }],
  558. }),
  559. ).rejects.toMatchObject({
  560. code: ModelErrorCode.InvalidModelResponse,
  561. message: "Model returned an invalid tool call: tool name is empty.",
  562. });
  563. },
  564. );
  565. it("keeps the existing generated tool-call id fallback", async () => {
  566. const runtime: AiSdkModelRuntime = {
  567. async generateText() {
  568. return {
  569. finishReason: "tool-calls",
  570. text: "",
  571. toolCalls: [
  572. {
  573. toolName: "Read",
  574. input: {},
  575. },
  576. ],
  577. totalUsage: usage(2, 1),
  578. usage: usage(2, 1),
  579. } as never;
  580. },
  581. streamText() {
  582. throw new Error("not used");
  583. },
  584. };
  585. const adapter = new AiSdkModelAdapter({
  586. registry: registry(),
  587. runtime,
  588. });
  589. const result = await executeAdapterGenerateText(adapter, {
  590. providerId: "test" as never,
  591. modelId: "model-a" as never,
  592. messages: [{ role: "user", content: "Read more" }],
  593. });
  594. expect(result.toolCalls?.[0]).toMatchObject({ input: {}, name: "Read" });
  595. expect(result.toolCalls?.[0]?.id).toEqual(expect.any(String));
  596. expect(result.toolCalls?.[0]?.id.length).toBeGreaterThan(0);
  597. });
  598. it.each(["", " "])("preserves streamed tool name %j", async (toolName) => {
  599. const runtime: AiSdkModelRuntime = {
  600. async generateText() {
  601. throw new Error("not used");
  602. },
  603. streamText() {
  604. return {
  605. fullStream: stream([
  606. { type: "start" },
  607. {
  608. type: "tool-call",
  609. toolCallId: "call_empty",
  610. toolName,
  611. input: { offset: 48 },
  612. },
  613. ]),
  614. } as never;
  615. },
  616. };
  617. const adapter = new AiSdkModelAdapter({
  618. registry: registry(),
  619. runtime,
  620. });
  621. const events = [];
  622. for await (const event of executeAdapterStreamText(adapter, {
  623. providerId: "test" as never,
  624. modelId: "model-a" as never,
  625. messages: [{ role: "user", content: "Read more" }],
  626. })) {
  627. events.push(event);
  628. }
  629. expect(events).toContainEqual({
  630. type: "tool_call",
  631. toolCall: {
  632. id: "call_empty",
  633. input: { offset: 48 },
  634. name: toolName,
  635. providerExecuted: undefined,
  636. },
  637. });
  638. });
  639. it.each(["generateText", "streamText"])(
  640. "rejects provider-executed empty names from %s",
  641. async (source) => {
  642. const runtime: AiSdkModelRuntime = {
  643. async generateText() {
  644. return {
  645. finishReason: "tool-calls",
  646. text: "",
  647. toolCalls: [
  648. {
  649. providerExecuted: true,
  650. toolCallId: "call_provider_empty",
  651. toolName: "",
  652. input: {},
  653. },
  654. ],
  655. totalUsage: usage(2, 1),
  656. usage: usage(2, 1),
  657. } as never;
  658. },
  659. streamText() {
  660. return {
  661. fullStream: stream([
  662. { type: "start" },
  663. {
  664. type: "tool-call",
  665. providerExecuted: true,
  666. toolCallId: "call_provider_empty",
  667. toolName: "",
  668. input: {},
  669. },
  670. ]),
  671. } as never;
  672. },
  673. };
  674. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  675. const request = {
  676. providerId: "test" as never,
  677. modelId: "model-a" as never,
  678. messages: [{ role: "user" as const, content: "Search" }],
  679. };
  680. const call =
  681. source === "generateText"
  682. ? executeAdapterGenerateText(adapter, request)
  683. : (async () => {
  684. for await (const _event of executeAdapterStreamText(adapter, request)) {
  685. // drain
  686. }
  687. })();
  688. await expect(call).rejects.toMatchObject({
  689. code: ModelErrorCode.InvalidModelResponse,
  690. message: "Model returned an invalid tool call: tool name is empty.",
  691. });
  692. },
  693. );
  694. });
  695. function registry(): TestProviderConfigFixture {
  696. return new TestProviderConfigFixture({
  697. providers: {
  698. test: {
  699. kind: "custom",
  700. createLanguageModel: (modelId) => ({ modelId }) as never,
  701. },
  702. },
  703. });
  704. }
  705. function usage(inputTokens: number, outputTokens: number) {
  706. return {
  707. inputTokens,
  708. outputTokens,
  709. totalTokens: inputTokens + outputTokens,
  710. inputTokenDetails: {
  711. noCacheTokens: inputTokens,
  712. cacheReadTokens: undefined,
  713. cacheWriteTokens: undefined,
  714. },
  715. outputTokenDetails: {
  716. textTokens: outputTokens,
  717. reasoningTokens: undefined,
  718. },
  719. };
  720. }
  721. async function* stream(chunks: unknown[]) {
  722. for (const chunk of chunks) {
  723. yield chunk;
  724. }
  725. }
  726. async function generateTextWithToolInput(options: {
  727. input: unknown;
  728. toolCallId: string;
  729. toolName: string;
  730. }) {
  731. const runtime: AiSdkModelRuntime = {
  732. async generateText() {
  733. return {
  734. finishReason: "tool-calls",
  735. text: "",
  736. toolCalls: [
  737. {
  738. input: options.input,
  739. toolCallId: options.toolCallId,
  740. toolName: options.toolName,
  741. },
  742. ],
  743. totalUsage: usage(2, 1),
  744. usage: usage(2, 1),
  745. } as never;
  746. },
  747. streamText() {
  748. throw new Error("not used");
  749. },
  750. };
  751. const adapter = new AiSdkModelAdapter({
  752. registry: registry(),
  753. runtime,
  754. });
  755. return executeAdapterGenerateText(adapter, {
  756. providerId: "test" as never,
  757. modelId: "model-a" as never,
  758. messages: [{ role: "user", content: "Run a command" }],
  759. });
  760. }
  761. function testLogger(warn: ReturnType<typeof vi.fn>) {
  762. return {
  763. child: vi.fn(),
  764. debug: vi.fn(),
  765. error: vi.fn(),
  766. info: vi.fn(),
  767. warn,
  768. } as never;
  769. }