tool-input-normalization.test.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. import { describe, expect, it, vi } from "vitest";
  2. import { markUnparsedToolInput, ModelErrorCode, readUnparsedToolInput } 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("marks unparseable JSON on an empty object that serializes as {}", () => {
  145. // 缺少结尾的 `}`,JSON.parse 失败;第 200 个码元(下标 199)是高位代理。
  146. const raw = `{"content":"${"x".repeat(187)}\u{1F600}"`;
  147. const input = normalizeModelToolInput(raw, { source: "streamText", toolName: "Write" });
  148. // 只有 symbol 键:provider 历史、持久化与 model-io 序列化后仍是 {}。
  149. expect(JSON.stringify(input)).toBe("{}");
  150. expect(Object.keys(input as object)).toEqual([]);
  151. // 前 200 个码元的末尾是高位代理时去掉它;长度是完整原串的 UTF-16 长度。
  152. expect(readUnparsedToolInput(input)).toEqual({
  153. preview: raw.slice(0, 199),
  154. length: raw.length,
  155. });
  156. expect(readUnparsedToolInput({})).toBeUndefined();
  157. expect(readUnparsedToolInput(normalizeModelToolInput("null", { source: "streamText" }))).toBe(
  158. undefined,
  159. );
  160. });
  161. it.each([
  162. { input: undefined, name: "missing input" },
  163. { input: "", name: "empty string" },
  164. // 只含空白与空串同样是“没给参数”,不打解析失败标记(toEqual 会比较 symbol 键)。
  165. { input: " \n\t", name: "whitespace-only string" },
  166. ])("degrades $name to an empty object without a parse warning", ({ input }) => {
  167. const warn = vi.fn();
  168. expect(
  169. normalizeModelToolInput(input, {
  170. logger: testLogger(warn),
  171. source: "streamText",
  172. toolName: "Read",
  173. }),
  174. ).toEqual({});
  175. expect(warn).not.toHaveBeenCalled();
  176. });
  177. it("keeps legacy generateText args fallback when input is missing", async () => {
  178. const runtime: AiSdkModelRuntime = {
  179. async generateText() {
  180. return {
  181. finishReason: "tool-calls",
  182. text: "",
  183. toolCalls: [
  184. {
  185. args: { file_path: "README.md" },
  186. toolCallId: "call_legacy_args",
  187. toolName: "Read",
  188. },
  189. ],
  190. totalUsage: usage(2, 1),
  191. usage: usage(2, 1),
  192. } as never;
  193. },
  194. streamText() {
  195. throw new Error("not used");
  196. },
  197. };
  198. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  199. const result = await executeAdapterGenerateText(adapter, {
  200. providerId: "test" as never,
  201. modelId: "model-a" as never,
  202. messages: [{ role: "user", content: "Read a file" }],
  203. });
  204. expect(result.toolCalls).toEqual([
  205. {
  206. id: "call_legacy_args",
  207. input: { file_path: "README.md" },
  208. name: "Read",
  209. },
  210. ]);
  211. });
  212. it("degrades partial generateText JSON to an empty object without repairing it", async () => {
  213. const runtime: AiSdkModelRuntime = {
  214. async generateText() {
  215. return {
  216. finishReason: "tool-calls",
  217. text: "",
  218. toolCalls: [
  219. {
  220. toolCallId: "call_bad",
  221. toolName: "Bash",
  222. input: '{"command":',
  223. },
  224. ],
  225. totalUsage: usage(2, 1),
  226. usage: usage(2, 1),
  227. } as never;
  228. },
  229. streamText() {
  230. throw new Error("not used");
  231. },
  232. };
  233. const adapter = new AiSdkModelAdapter({
  234. registry: registry(),
  235. runtime,
  236. });
  237. const result = await executeAdapterGenerateText(adapter, {
  238. providerId: "test" as never,
  239. modelId: "model-a" as never,
  240. messages: [{ role: "user", content: "Run a command" }],
  241. });
  242. expect(result.toolCalls).toEqual([
  243. {
  244. id: "call_bad",
  245. name: "Bash",
  246. input: markUnparsedToolInput('{"command":'),
  247. },
  248. ]);
  249. });
  250. it("recovers an unparseable generateText tool call without retaining the raw input", async () => {
  251. const malformedInput = '{"command":"bad\\u"}';
  252. const warn = vi.fn();
  253. const runtime: AiSdkModelRuntime = {
  254. async generateText() {
  255. return {
  256. finishReason: "tool-calls",
  257. text: "",
  258. toolCalls: [
  259. {
  260. toolCallId: "call_bad_escape",
  261. toolName: "Bash",
  262. input: malformedInput,
  263. },
  264. ],
  265. totalUsage: usage(2, 1),
  266. usage: usage(2, 1),
  267. } as never;
  268. },
  269. streamText() {
  270. throw new Error("not used");
  271. },
  272. };
  273. const adapter = new AiSdkModelAdapter({
  274. logger: testLogger(warn),
  275. registry: registry(),
  276. runtime,
  277. });
  278. const result = await executeAdapterGenerateText(adapter, {
  279. providerId: "test" as never,
  280. modelId: "model-a" as never,
  281. messages: [{ role: "user", content: "Run a command" }],
  282. });
  283. expect(result.toolCalls).toEqual([
  284. {
  285. id: "call_bad_escape",
  286. input: markUnparsedToolInput(malformedInput),
  287. name: "Bash",
  288. },
  289. ]);
  290. const normalizationWarnings = warn.mock.calls.filter(
  291. ([message]) => message === "Model tool input JSON normalization failed",
  292. );
  293. expect(normalizationWarnings).toHaveLength(1);
  294. expect(normalizationWarnings[0]).toEqual([
  295. "Model tool input JSON normalization failed",
  296. expect.objectContaining({
  297. inputLength: malformedInput.length,
  298. parseErrorType: "SyntaxError",
  299. recovery: "unparsed_marker",
  300. source: "generateText",
  301. status: "failed",
  302. toolName: "Bash",
  303. }),
  304. ]);
  305. expect(JSON.stringify(result.toolCalls)).not.toContain(malformedInput);
  306. expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
  307. });
  308. it.each(feedbackMalformedCases)(
  309. "strictly degrades malformed tool input observed in feedback $ticket",
  310. async ({ input, ticket, toolName }) => {
  311. const result = await generateTextWithToolInput({
  312. input,
  313. toolName,
  314. toolCallId: `call_${ticket}`,
  315. });
  316. expect(result.toolCalls).toEqual([
  317. {
  318. id: `call_${ticket}`,
  319. name: toolName,
  320. input: markUnparsedToolInput(input),
  321. },
  322. ]);
  323. expect(JSON.stringify(result.toolCalls)).not.toContain(input);
  324. },
  325. );
  326. it("safe-parses stringified tool inputs in streamed tool_call events", async () => {
  327. const runtime: AiSdkModelRuntime = {
  328. async generateText() {
  329. throw new Error("not used");
  330. },
  331. streamText() {
  332. return {
  333. fullStream: stream([
  334. { type: "start" },
  335. {
  336. type: "tool-call",
  337. toolCallId: "call_read",
  338. toolName: "Read",
  339. input: '{"file_path":"README.md"}',
  340. },
  341. {
  342. type: "finish",
  343. finishReason: "tool-calls",
  344. rawFinishReason: undefined,
  345. totalUsage: usage(2, 1),
  346. },
  347. ]),
  348. } as never;
  349. },
  350. };
  351. const adapter = new AiSdkModelAdapter({
  352. registry: registry(),
  353. runtime,
  354. });
  355. const events = [];
  356. for await (const event of executeAdapterStreamText(adapter, {
  357. providerId: "test" as never,
  358. modelId: "model-a" as never,
  359. messages: [{ role: "user", content: "Read README" }],
  360. })) {
  361. events.push(event);
  362. }
  363. expect(events).toEqual([
  364. { type: "start" },
  365. {
  366. type: "tool_call",
  367. toolCall: {
  368. id: "call_read",
  369. name: "Read",
  370. input: { file_path: "README.md" },
  371. },
  372. },
  373. {
  374. type: "finish",
  375. finishReason: "tool-calls",
  376. providerMetadata: undefined,
  377. usage: {
  378. inputTokens: 2,
  379. outputTokens: 1,
  380. totalTokens: 3,
  381. cacheReadTokens: undefined,
  382. cacheWriteTokens: undefined,
  383. reasoningTokens: undefined,
  384. },
  385. },
  386. ]);
  387. });
  388. it("recovers a malformed atomic streamed tool_call as an empty object", async () => {
  389. const malformedInput =
  390. '{"questions":[{"options":[{"description":"相当于把 2.5D 当作"3D 视角"的初始预设。"}]}]}';
  391. const warn = vi.fn();
  392. const runtime: AiSdkModelRuntime = {
  393. async generateText() {
  394. throw new Error("not used");
  395. },
  396. streamText() {
  397. return {
  398. fullStream: stream([
  399. { type: "start" },
  400. {
  401. type: "tool-call",
  402. toolCallId: "call_ask_malformed",
  403. toolName: "AskUserQuestion",
  404. input: malformedInput,
  405. },
  406. {
  407. type: "finish",
  408. finishReason: "tool-calls",
  409. rawFinishReason: undefined,
  410. totalUsage: usage(2, 1),
  411. },
  412. ]),
  413. } as never;
  414. },
  415. };
  416. const adapter = new AiSdkModelAdapter({
  417. logger: testLogger(warn),
  418. registry: registry(),
  419. runtime,
  420. });
  421. const events = [];
  422. for await (const event of executeAdapterStreamText(adapter, {
  423. providerId: "test" as never,
  424. modelId: "model-a" as never,
  425. messages: [{ role: "user", content: "Read README" }],
  426. })) {
  427. events.push(event);
  428. }
  429. expect(events).toEqual([
  430. { type: "start" },
  431. {
  432. type: "tool_call",
  433. toolCall: {
  434. id: "call_ask_malformed",
  435. name: "AskUserQuestion",
  436. input: markUnparsedToolInput(malformedInput),
  437. },
  438. },
  439. {
  440. type: "finish",
  441. finishReason: "tool-calls",
  442. providerMetadata: undefined,
  443. usage: {
  444. inputTokens: 2,
  445. outputTokens: 1,
  446. totalTokens: 3,
  447. cacheReadTokens: undefined,
  448. cacheWriteTokens: undefined,
  449. reasoningTokens: undefined,
  450. },
  451. },
  452. ]);
  453. expect(warn).toHaveBeenCalledTimes(1);
  454. expect(warn).toHaveBeenCalledWith(
  455. "Model tool input JSON normalization failed",
  456. expect.objectContaining({
  457. inputLength: malformedInput.length,
  458. source: "streamText",
  459. toolName: "AskUserQuestion",
  460. }),
  461. );
  462. expect(JSON.stringify(events)).not.toContain(malformedInput);
  463. expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
  464. });
  465. it.each(["", " "])("preserves recoverable generateText tool name %j", async (toolName) => {
  466. const runtime: AiSdkModelRuntime = {
  467. async generateText() {
  468. return {
  469. finishReason: "tool-calls",
  470. text: "",
  471. toolCalls: [
  472. {
  473. toolCallId: "call_empty",
  474. toolName,
  475. input: { offset: 48 },
  476. },
  477. ],
  478. totalUsage: usage(2, 1),
  479. usage: usage(2, 1),
  480. } as never;
  481. },
  482. streamText() {
  483. throw new Error("not used");
  484. },
  485. };
  486. const adapter = new AiSdkModelAdapter({
  487. registry: registry(),
  488. runtime,
  489. });
  490. await expect(
  491. executeAdapterGenerateText(adapter, {
  492. providerId: "test" as never,
  493. modelId: "model-a" as never,
  494. messages: [{ role: "user", content: "Read more" }],
  495. }),
  496. ).resolves.toMatchObject({
  497. toolCalls: [
  498. {
  499. id: "call_empty",
  500. input: { offset: 48 },
  501. name: toolName,
  502. },
  503. ],
  504. });
  505. });
  506. it.each([undefined, null, 42, { invalid: true }])(
  507. "rejects generateText tool name %j",
  508. async (toolName) => {
  509. const runtime: AiSdkModelRuntime = {
  510. async generateText() {
  511. return {
  512. finishReason: "tool-calls",
  513. text: "",
  514. toolCalls: [
  515. {
  516. toolCallId: "call_invalid_name",
  517. toolName,
  518. input: {},
  519. },
  520. ],
  521. totalUsage: usage(2, 1),
  522. usage: usage(2, 1),
  523. } as never;
  524. },
  525. streamText() {
  526. throw new Error("not used");
  527. },
  528. };
  529. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  530. await expect(
  531. executeAdapterGenerateText(adapter, {
  532. providerId: "test" as never,
  533. modelId: "model-a" as never,
  534. messages: [{ role: "user", content: "Read more" }],
  535. }),
  536. ).rejects.toMatchObject({
  537. code: ModelErrorCode.InvalidModelResponse,
  538. message: "Model returned an invalid tool call: tool name is empty.",
  539. });
  540. },
  541. );
  542. it("keeps the existing generated tool-call id fallback", async () => {
  543. const runtime: AiSdkModelRuntime = {
  544. async generateText() {
  545. return {
  546. finishReason: "tool-calls",
  547. text: "",
  548. toolCalls: [
  549. {
  550. toolName: "Read",
  551. input: {},
  552. },
  553. ],
  554. totalUsage: usage(2, 1),
  555. usage: usage(2, 1),
  556. } as never;
  557. },
  558. streamText() {
  559. throw new Error("not used");
  560. },
  561. };
  562. const adapter = new AiSdkModelAdapter({
  563. registry: registry(),
  564. runtime,
  565. });
  566. const result = await executeAdapterGenerateText(adapter, {
  567. providerId: "test" as never,
  568. modelId: "model-a" as never,
  569. messages: [{ role: "user", content: "Read more" }],
  570. });
  571. expect(result.toolCalls?.[0]).toMatchObject({ input: {}, name: "Read" });
  572. expect(result.toolCalls?.[0]?.id).toEqual(expect.any(String));
  573. expect(result.toolCalls?.[0]?.id.length).toBeGreaterThan(0);
  574. });
  575. it.each(["", " "])("preserves streamed tool name %j", async (toolName) => {
  576. const runtime: AiSdkModelRuntime = {
  577. async generateText() {
  578. throw new Error("not used");
  579. },
  580. streamText() {
  581. return {
  582. fullStream: stream([
  583. { type: "start" },
  584. {
  585. type: "tool-call",
  586. toolCallId: "call_empty",
  587. toolName,
  588. input: { offset: 48 },
  589. },
  590. ]),
  591. } as never;
  592. },
  593. };
  594. const adapter = new AiSdkModelAdapter({
  595. registry: registry(),
  596. runtime,
  597. });
  598. const events = [];
  599. for await (const event of executeAdapterStreamText(adapter, {
  600. providerId: "test" as never,
  601. modelId: "model-a" as never,
  602. messages: [{ role: "user", content: "Read more" }],
  603. })) {
  604. events.push(event);
  605. }
  606. expect(events).toContainEqual({
  607. type: "tool_call",
  608. toolCall: {
  609. id: "call_empty",
  610. input: { offset: 48 },
  611. name: toolName,
  612. providerExecuted: undefined,
  613. },
  614. });
  615. });
  616. it.each(["generateText", "streamText"])(
  617. "rejects provider-executed empty names from %s",
  618. async (source) => {
  619. const runtime: AiSdkModelRuntime = {
  620. async generateText() {
  621. return {
  622. finishReason: "tool-calls",
  623. text: "",
  624. toolCalls: [
  625. {
  626. providerExecuted: true,
  627. toolCallId: "call_provider_empty",
  628. toolName: "",
  629. input: {},
  630. },
  631. ],
  632. totalUsage: usage(2, 1),
  633. usage: usage(2, 1),
  634. } as never;
  635. },
  636. streamText() {
  637. return {
  638. fullStream: stream([
  639. { type: "start" },
  640. {
  641. type: "tool-call",
  642. providerExecuted: true,
  643. toolCallId: "call_provider_empty",
  644. toolName: "",
  645. input: {},
  646. },
  647. ]),
  648. } as never;
  649. },
  650. };
  651. const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
  652. const request = {
  653. providerId: "test" as never,
  654. modelId: "model-a" as never,
  655. messages: [{ role: "user" as const, content: "Search" }],
  656. };
  657. const call =
  658. source === "generateText"
  659. ? executeAdapterGenerateText(adapter, request)
  660. : (async () => {
  661. for await (const _event of executeAdapterStreamText(adapter, request)) {
  662. // drain
  663. }
  664. })();
  665. await expect(call).rejects.toMatchObject({
  666. code: ModelErrorCode.InvalidModelResponse,
  667. message: "Model returned an invalid tool call: tool name is empty.",
  668. });
  669. },
  670. );
  671. });
  672. function registry(): TestProviderConfigFixture {
  673. return new TestProviderConfigFixture({
  674. providers: {
  675. test: {
  676. kind: "custom",
  677. createLanguageModel: (modelId) => ({ modelId }) as never,
  678. },
  679. },
  680. });
  681. }
  682. function usage(inputTokens: number, outputTokens: number) {
  683. return {
  684. inputTokens,
  685. outputTokens,
  686. totalTokens: inputTokens + outputTokens,
  687. inputTokenDetails: {
  688. noCacheTokens: inputTokens,
  689. cacheReadTokens: undefined,
  690. cacheWriteTokens: undefined,
  691. },
  692. outputTokenDetails: {
  693. textTokens: outputTokens,
  694. reasoningTokens: undefined,
  695. },
  696. };
  697. }
  698. async function* stream(chunks: unknown[]) {
  699. for (const chunk of chunks) {
  700. yield chunk;
  701. }
  702. }
  703. async function generateTextWithToolInput(options: {
  704. input: unknown;
  705. toolCallId: string;
  706. toolName: string;
  707. }) {
  708. const runtime: AiSdkModelRuntime = {
  709. async generateText() {
  710. return {
  711. finishReason: "tool-calls",
  712. text: "",
  713. toolCalls: [
  714. {
  715. input: options.input,
  716. toolCallId: options.toolCallId,
  717. toolName: options.toolName,
  718. },
  719. ],
  720. totalUsage: usage(2, 1),
  721. usage: usage(2, 1),
  722. } as never;
  723. },
  724. streamText() {
  725. throw new Error("not used");
  726. },
  727. };
  728. const adapter = new AiSdkModelAdapter({
  729. registry: registry(),
  730. runtime,
  731. });
  732. return executeAdapterGenerateText(adapter, {
  733. providerId: "test" as never,
  734. modelId: "model-a" as never,
  735. messages: [{ role: "user", content: "Run a command" }],
  736. });
  737. }
  738. function testLogger(warn: ReturnType<typeof vi.fn>) {
  739. return {
  740. child: vi.fn(),
  741. debug: vi.fn(),
  742. error: vi.fn(),
  743. info: vi.fn(),
  744. warn,
  745. } as never;
  746. }