kimi-code/packages/agent-core/test/loop/tool-args-parse.test.ts
Kai 063538744f
Some checks are pending
CI / test-windows (push) Waiting to run
CI / lint (push) Waiting to run
CI / build (push) Waiting to run
CI / test (push) Waiting to run
CI / typecheck (push) Waiting to run
Nix Build / Check flake.nix workspace sync (push) Waiting to run
Nix Build / nix build .#kimi-code (push) Blocked by required conditions
Release / Native release artifact (push) Blocked by required conditions
Release / Release (push) Waiting to run
Release / Deploy docs (push) Blocked by required conditions
Release / Publish native release assets (push) Blocked by required conditions
refactor(agent-core): align malformed tool args with schema validation (#1209)
* feat(agent-core): repair malformed tool args JSON

Attempt jsonrepair when tool call arguments fail JSON.parse, then continue schema validation. Return malformed JSON errors with a concise expected schema hint so the model can retry with corrected arguments.

* chore(nix): update pnpm deps hash

Update the fixed-output pnpmDeps hash after adding jsonrepair so the Nix build can fetch dependencies.

* refactor(agent-core): drop tool args JSON repair

Stop repairing malformed tool call arguments. Fall back to an empty object on JSON parse failure and let schema validation produce the retry error, while preserving valid arguments for unknown tools in the transcript.

* chore: add changeset for tool args validation
2026-06-29 23:24:03 +08:00

45 lines
1.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { parseToolCallArguments } from '../../src/loop/tool-args-parse';
describe('parseToolCallArguments', () => {
it('treats null or empty arguments as an empty object', () => {
expect(parseToolCallArguments(null)).toEqual({
success: true,
data: {},
parseFailed: false,
});
expect(parseToolCallArguments('')).toEqual({
success: true,
data: {},
parseFailed: false,
});
});
it('parses valid JSON', () => {
expect(parseToolCallArguments('{"text":"hi"}')).toEqual({
success: true,
data: { text: 'hi' },
parseFailed: false,
});
});
it('falls back to an empty object when JSON is malformed', () => {
expect(parseToolCallArguments('{"text":"hi",}')).toEqual({
success: true,
data: {},
parseFailed: true,
error: expect.any(String),
});
});
it('falls back to an empty object for unrecoverable JSON', () => {
const result = parseToolCallArguments('{}{');
expect(result).toEqual({
success: true,
data: {},
parseFailed: true,
error: expect.any(String),
});
});
});