| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817 |
- import { describe, expect, it, vi } from "vitest";
- import { ModelErrorCode } from "@zcode/contracts";
- import type { AiSdkModelRuntime } from "../src/model/runner.js";
- import {
- TestAiSdkModelAdapter as AiSdkModelAdapter,
- TestProviderConfigFixture,
- } from "./test-provider-config.js";
- import { normalizeModelToolInput } from "../src/model/tool-input-normalization.js";
- import { executeAdapterGenerateText, executeAdapterStreamText } from "./test-adapter-model.js";
- describe("model tool input normalization", () => {
- const feedbackMalformedCases = [
- {
- input:
- '{"file_path":"F:\\\\clash-verge-fix.bat","content":"@echo off\\nsetlocal EnableExtensions',
- ticket: "ZCT-2072501999367512064",
- toolName: "Write",
- },
- {
- input: '{"content":"# PenglaiAgent v0.3.5 深度审计报告\\n**High**: H1',
- ticket: "ZCT-2072869553480470528",
- toolName: "Write",
- },
- {
- input: '{"content":"# 第二章、第三章 投标文件组织提纲\\n| 4 | 桥梁数量 |',
- ticket: "ZCT-2072928903530250240",
- toolName: "Write",
- },
- {
- input: '{"content":"<!DOCTYPE html>\\n<html lang=\\"zh-CN\\">\\n<th',
- ticket: "ZCT-2072970699099508736",
- toolName: "Write",
- },
- {
- input:
- '{"questions":[{"options":[{"description":"相当于把 2.5D 当作"3D 视角"的初始预设。"}]}]}',
- ticket: "ZCT-2072321679810314240",
- toolName: "AskUserQuestion",
- },
- {
- input: '{"questions":[{"description":"bad\\u"}]}',
- ticket: "ZCT-2072582003671117824",
- toolName: "AskUserQuestion",
- },
- ];
- it("safe-parses stringified tool inputs returned from generateText", async () => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolCallId: "call_bash",
- toolName: "Bash",
- input: '{"command":"python3 -c \\"print(1)\\""}',
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Run a command" }],
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_bash",
- name: "Bash",
- input: {
- command: 'python3 -c "print(1)"',
- },
- },
- ]);
- });
- it("accepts a byte-order mark before otherwise valid tool input JSON", async () => {
- const result = await generateTextWithToolInput({
- input: '\uFEFF{"command":"pwd"}',
- toolCallId: "call_bom",
- toolName: "Bash",
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_bom",
- input: { command: "pwd" },
- name: "Bash",
- },
- ]);
- });
- it("degrades JSON null to an empty object through the malformed input path", () => {
- const warn = vi.fn();
- expect(
- normalizeModelToolInput("null", {
- logger: testLogger(warn),
- source: "streamText",
- toolName: "Read",
- }),
- ).toEqual({});
- expect(warn).toHaveBeenCalledOnce();
- expect(warn).toHaveBeenCalledWith(
- "Model tool input JSON normalization failed",
- expect.objectContaining({
- inputLength: 4,
- parseErrorType: "TypeError",
- recovery: "empty_object",
- source: "streamText",
- status: "failed",
- toolName: "Read",
- }),
- );
- });
- it("degrades upstream-normalized null without inventing a raw input length", () => {
- const warn = vi.fn();
- expect(
- normalizeModelToolInput(null, {
- logger: testLogger(warn),
- source: "streamText",
- toolName: "Read",
- }),
- ).toEqual({});
- expect(warn).toHaveBeenCalledOnce();
- const [, context] = warn.mock.calls[0] ?? [];
- expect(context).toEqual(
- expect.objectContaining({
- inputType: "null",
- parseErrorType: "TypeError",
- recovery: "empty_object",
- source: "streamText",
- status: "failed",
- toolName: "Read",
- }),
- );
- expect(context).not.toHaveProperty("inputLength");
- });
- it.each([
- { input: undefined, name: "missing input" },
- { input: "", name: "empty string" },
- ])("degrades $name to an empty object without a parse warning", ({ input }) => {
- const warn = vi.fn();
- expect(
- normalizeModelToolInput(input, {
- logger: testLogger(warn),
- source: "streamText",
- toolName: "Read",
- }),
- ).toEqual({});
- expect(warn).not.toHaveBeenCalled();
- });
- it("does not let legacy args replace an explicit null generateText input", async () => {
- const warn = vi.fn();
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- args: { file_path: "wrong.md" },
- input: null,
- toolCallId: "call_null",
- toolName: "Read",
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- logger: testLogger(warn),
- registry: registry(),
- runtime,
- });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read a file" }],
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_null",
- input: {},
- name: "Read",
- },
- ]);
- expect(warn).toHaveBeenCalledOnce();
- });
- it("keeps legacy generateText args fallback when input is missing", async () => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- args: { file_path: "README.md" },
- toolCallId: "call_legacy_args",
- toolName: "Read",
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read a file" }],
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_legacy_args",
- input: { file_path: "README.md" },
- name: "Read",
- },
- ]);
- });
- it("degrades partial generateText JSON to an empty object without repairing it", async () => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolCallId: "call_bad",
- toolName: "Bash",
- input: '{"command":',
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Run a command" }],
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_bad",
- name: "Bash",
- input: {},
- },
- ]);
- });
- it("recovers an unparseable generateText tool call without retaining the raw input", async () => {
- const malformedInput = '{"command":"bad\\u"}';
- const warn = vi.fn();
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolCallId: "call_bad_escape",
- toolName: "Bash",
- input: malformedInput,
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- logger: testLogger(warn),
- registry: registry(),
- runtime,
- });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Run a command" }],
- });
- expect(result.toolCalls).toEqual([
- {
- id: "call_bad_escape",
- input: {},
- name: "Bash",
- },
- ]);
- const normalizationWarnings = warn.mock.calls.filter(
- ([message]) => message === "Model tool input JSON normalization failed",
- );
- expect(normalizationWarnings).toHaveLength(1);
- expect(normalizationWarnings[0]).toEqual([
- "Model tool input JSON normalization failed",
- expect.objectContaining({
- inputLength: malformedInput.length,
- parseErrorType: "SyntaxError",
- recovery: "empty_object",
- source: "generateText",
- status: "failed",
- toolName: "Bash",
- }),
- ]);
- expect(JSON.stringify(result.toolCalls)).not.toContain(malformedInput);
- expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
- });
- it.each(feedbackMalformedCases)(
- "strictly degrades malformed tool input observed in feedback $ticket",
- async ({ input, ticket, toolName }) => {
- const result = await generateTextWithToolInput({
- input,
- toolName,
- toolCallId: `call_${ticket}`,
- });
- expect(result.toolCalls).toEqual([
- {
- id: `call_${ticket}`,
- name: toolName,
- input: {},
- },
- ]);
- expect(JSON.stringify(result.toolCalls)).not.toContain(input);
- },
- );
- it("safe-parses stringified tool inputs in streamed tool_call events", async () => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- throw new Error("not used");
- },
- streamText() {
- return {
- fullStream: stream([
- { type: "start" },
- {
- type: "tool-call",
- toolCallId: "call_read",
- toolName: "Read",
- input: '{"file_path":"README.md"}',
- },
- {
- type: "finish",
- finishReason: "tool-calls",
- rawFinishReason: undefined,
- totalUsage: usage(2, 1),
- },
- ]),
- } as never;
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- const events = [];
- for await (const event of executeAdapterStreamText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read README" }],
- })) {
- events.push(event);
- }
- expect(events).toEqual([
- { type: "start" },
- {
- type: "tool_call",
- toolCall: {
- id: "call_read",
- name: "Read",
- input: { file_path: "README.md" },
- },
- },
- {
- type: "finish",
- finishReason: "tool-calls",
- providerMetadata: undefined,
- usage: {
- inputTokens: 2,
- outputTokens: 1,
- totalTokens: 3,
- cacheReadTokens: undefined,
- cacheWriteTokens: undefined,
- reasoningTokens: undefined,
- },
- },
- ]);
- });
- it("recovers a malformed atomic streamed tool_call as an empty object", async () => {
- const malformedInput =
- '{"questions":[{"options":[{"description":"相当于把 2.5D 当作"3D 视角"的初始预设。"}]}]}';
- const warn = vi.fn();
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- throw new Error("not used");
- },
- streamText() {
- return {
- fullStream: stream([
- { type: "start" },
- {
- type: "tool-call",
- toolCallId: "call_ask_malformed",
- toolName: "AskUserQuestion",
- input: malformedInput,
- },
- {
- type: "finish",
- finishReason: "tool-calls",
- rawFinishReason: undefined,
- totalUsage: usage(2, 1),
- },
- ]),
- } as never;
- },
- };
- const adapter = new AiSdkModelAdapter({
- logger: testLogger(warn),
- registry: registry(),
- runtime,
- });
- const events = [];
- for await (const event of executeAdapterStreamText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read README" }],
- })) {
- events.push(event);
- }
- expect(events).toEqual([
- { type: "start" },
- {
- type: "tool_call",
- toolCall: {
- id: "call_ask_malformed",
- name: "AskUserQuestion",
- input: {},
- },
- },
- {
- type: "finish",
- finishReason: "tool-calls",
- providerMetadata: undefined,
- usage: {
- inputTokens: 2,
- outputTokens: 1,
- totalTokens: 3,
- cacheReadTokens: undefined,
- cacheWriteTokens: undefined,
- reasoningTokens: undefined,
- },
- },
- ]);
- expect(warn).toHaveBeenCalledTimes(1);
- expect(warn).toHaveBeenCalledWith(
- "Model tool input JSON normalization failed",
- expect.objectContaining({
- inputLength: malformedInput.length,
- source: "streamText",
- toolName: "AskUserQuestion",
- }),
- );
- expect(JSON.stringify(events)).not.toContain(malformedInput);
- expect(JSON.stringify(warn.mock.calls)).not.toContain(malformedInput);
- });
- it.each(["", " "])("preserves recoverable generateText tool name %j", async (toolName) => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolCallId: "call_empty",
- toolName,
- input: { offset: 48 },
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- await expect(
- executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read more" }],
- }),
- ).resolves.toMatchObject({
- toolCalls: [
- {
- id: "call_empty",
- input: { offset: 48 },
- name: toolName,
- },
- ],
- });
- });
- it.each([undefined, null, 42, { invalid: true }])(
- "rejects generateText tool name %j",
- async (toolName) => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolCallId: "call_invalid_name",
- toolName,
- input: {},
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
- await expect(
- executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read more" }],
- }),
- ).rejects.toMatchObject({
- code: ModelErrorCode.InvalidModelResponse,
- message: "Model returned an invalid tool call: tool name is empty.",
- });
- },
- );
- it("keeps the existing generated tool-call id fallback", async () => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- toolName: "Read",
- input: {},
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- const result = await executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read more" }],
- });
- expect(result.toolCalls?.[0]).toMatchObject({ input: {}, name: "Read" });
- expect(result.toolCalls?.[0]?.id).toEqual(expect.any(String));
- expect(result.toolCalls?.[0]?.id.length).toBeGreaterThan(0);
- });
- it.each(["", " "])("preserves streamed tool name %j", async (toolName) => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- throw new Error("not used");
- },
- streamText() {
- return {
- fullStream: stream([
- { type: "start" },
- {
- type: "tool-call",
- toolCallId: "call_empty",
- toolName,
- input: { offset: 48 },
- },
- ]),
- } as never;
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- const events = [];
- for await (const event of executeAdapterStreamText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Read more" }],
- })) {
- events.push(event);
- }
- expect(events).toContainEqual({
- type: "tool_call",
- toolCall: {
- id: "call_empty",
- input: { offset: 48 },
- name: toolName,
- providerExecuted: undefined,
- },
- });
- });
- it.each(["generateText", "streamText"])(
- "rejects provider-executed empty names from %s",
- async (source) => {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- providerExecuted: true,
- toolCallId: "call_provider_empty",
- toolName: "",
- input: {},
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- return {
- fullStream: stream([
- { type: "start" },
- {
- type: "tool-call",
- providerExecuted: true,
- toolCallId: "call_provider_empty",
- toolName: "",
- input: {},
- },
- ]),
- } as never;
- },
- };
- const adapter = new AiSdkModelAdapter({ registry: registry(), runtime });
- const request = {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user" as const, content: "Search" }],
- };
- const call =
- source === "generateText"
- ? executeAdapterGenerateText(adapter, request)
- : (async () => {
- for await (const _event of executeAdapterStreamText(adapter, request)) {
- // drain
- }
- })();
- await expect(call).rejects.toMatchObject({
- code: ModelErrorCode.InvalidModelResponse,
- message: "Model returned an invalid tool call: tool name is empty.",
- });
- },
- );
- });
- function registry(): TestProviderConfigFixture {
- return new TestProviderConfigFixture({
- providers: {
- test: {
- kind: "custom",
- createLanguageModel: (modelId) => ({ modelId }) as never,
- },
- },
- });
- }
- function usage(inputTokens: number, outputTokens: number) {
- return {
- inputTokens,
- outputTokens,
- totalTokens: inputTokens + outputTokens,
- inputTokenDetails: {
- noCacheTokens: inputTokens,
- cacheReadTokens: undefined,
- cacheWriteTokens: undefined,
- },
- outputTokenDetails: {
- textTokens: outputTokens,
- reasoningTokens: undefined,
- },
- };
- }
- async function* stream(chunks: unknown[]) {
- for (const chunk of chunks) {
- yield chunk;
- }
- }
- async function generateTextWithToolInput(options: {
- input: unknown;
- toolCallId: string;
- toolName: string;
- }) {
- const runtime: AiSdkModelRuntime = {
- async generateText() {
- return {
- finishReason: "tool-calls",
- text: "",
- toolCalls: [
- {
- input: options.input,
- toolCallId: options.toolCallId,
- toolName: options.toolName,
- },
- ],
- totalUsage: usage(2, 1),
- usage: usage(2, 1),
- } as never;
- },
- streamText() {
- throw new Error("not used");
- },
- };
- const adapter = new AiSdkModelAdapter({
- registry: registry(),
- runtime,
- });
- return executeAdapterGenerateText(adapter, {
- providerId: "test" as never,
- modelId: "model-a" as never,
- messages: [{ role: "user", content: "Run a command" }],
- });
- }
- function testLogger(warn: ReturnType<typeof vi.fn>) {
- return {
- child: vi.fn(),
- debug: vi.fn(),
- error: vi.fn(),
- info: vi.fn(),
- warn,
- } as never;
- }
|