🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { describe, expect, it } from 'vitest';
import { ToolArgumentsRepairer } from '../ToolArgumentsRepairer';
import type { LobeToolManifest } from '../types';
describe('ToolArgumentsRepairer', () => {
describe('repair - basic functionality', () => {
it('should return original parsed data when no schema provided', () => {
const repairer = new ToolArgumentsRepairer();
const parsed = { foo: 'bar' };
const result = repairer.repair(parsed);
expect(result).toEqual(parsed);
});
it('should return original parsed data when schema has no required fields', () => {
const repairer = new ToolArgumentsRepairer();
const parsed = { foo: 'bar' };
const schema = { type: 'object', properties: { foo: { type: 'string' } } };
const result = repairer.repair(parsed, schema);
expect(result).toEqual(parsed);
});
it('should return original parsed data when all required fields are present', () => {
const repairer = new ToolArgumentsRepairer();
const parsed = { description: 'test', instruction: 'do something' };
const schema = {
type: 'object',
required: ['description', 'instruction'],
properties: {
description: { type: 'string' },
instruction: { type: 'string' },
},
};
const result = repairer.repair(parsed, schema);
expect(result).toEqual(parsed);
});
});
describe('repair - malformed JSON from LLM', () => {
it('should repair malformed JSON with escaped string issue', () => {
const repairer = new ToolArgumentsRepairer();
// This is the malformed data from haiku-4.5 model
// The entire JSON got stuffed into the "description" field with escaped quotes
const malformedParsed = {
description:
'Synthesize all 10 batch analyses into 10 most important themes for product builders", "instruction": "You have access to 10 batch analysis files", "runInClient": true, "timeout": 120000}',
};
const schema = {
type: 'object',
required: ['description', 'instruction'],
properties: {
description: { type: 'string' },
instruction: { type: 'string' },
runInClient: { type: 'boolean' },
timeout: { type: 'number' },
},
};
const result = repairer.repair(malformedParsed, schema);
expect(result).toHaveProperty('description');
expect(result).toHaveProperty('instruction');
expect(result).toHaveProperty('runInClient', true);
expect(result).toHaveProperty('timeout', 120000);
expect(result.description).toBe(
'Synthesize all 10 batch analyses into 10 most important themes for product builders',
);
expect(result.instruction).toBe('You have access to 10 batch analysis files');
});
it('should return original data if repair fails', () => {
const repairer = new ToolArgumentsRepairer();
// Invalid malformed data that cannot be repaired
const malformedParsed = {
description: 'some text without proper escape pattern',
};
const schema = {
type: 'object',
required: ['description', 'instruction'],
properties: {
description: { type: 'string' },
instruction: { type: 'string' },
},
};
const result = repairer.repair(malformedParsed, schema);
// Should return original since pattern doesn't match
expect(result).toEqual(malformedParsed);
});
});
describe('parse - integrated parsing and repair', () => {
it('should parse and repair arguments using manifest schema', () => {
const manifest: LobeToolManifest = {
identifier: 'lobe-agent',
api: [
{
name: 'callSubAgent',
description: 'Dispatch a sub-agent',
parameters: {
type: 'object',
required: ['description', 'instruction'],
properties: {
description: { type: 'string' },
instruction: { type: 'string' },
runInClient: { type: 'boolean' },
timeout: { type: 'number' },
},
},
},
],
type: 'builtin',
} as unknown as LobeToolManifest;
const repairer = new ToolArgumentsRepairer(manifest);
// Malformed arguments string
const malformedArguments = JSON.stringify({
description:
'Test task", "instruction": "Do something important", "runInClient": true, "timeout": 60000}',
});
const result = repairer.parse('callSubAgent', malformedArguments);
expect(result).toHaveProperty('description', 'Test task');
expect(result).toHaveProperty('instruction', 'Do something important');
expect(result).toHaveProperty('runInClient', true);
expect(result).toHaveProperty('timeout', 60000);
});
it('should handle normal arguments without repair needed', () => {
const manifest: LobeToolManifest = {
identifier: 'test-tool',
api: [
{
name: 'testApi',
description: 'Test API',
parameters: {
type: 'object',
required: ['name'],
properties: {
name: { type: 'string' },
},
},
},
],
type: 'default',
} as unknown as LobeToolManifest;
const repairer = new ToolArgumentsRepairer(manifest);
const normalArguments = JSON.stringify({ name: 'test value' });
const result = repairer.parse('testApi', normalArguments);
expect(result).toEqual({ name: 'test value' });
});
it('should handle no manifest gracefully', () => {
const repairer = new ToolArgumentsRepairer();
const arguments_ = JSON.stringify({ foo: 'bar' });
const result = repairer.parse('unknownApi', arguments_);
expect(result).toEqual({ foo: 'bar' });
});
it('should handle invalid JSON gracefully', () => {
const repairer = new ToolArgumentsRepairer();
const result = repairer.parse('test', 'invalid json');
expect(result).toEqual({});
});
});
describe('parse - partial JSON recovery (stream interruption)', () => {
it('should recover fields from incomplete JSON when stream is interrupted', () => {
const repairer = new ToolArgumentsRepairer();
// Simulates stream dropping after title, description, type were sent but before content
const incompleteArgs =
'{"title": "My Document", "description": "A brief summary", "type": "report"';
const result = repairer.parse('createDocument', incompleteArgs);
expect(result).toEqual({
title: 'My Document',
description: 'A brief summary',
type: 'report',
});
});
it('should recover fields when stream drops mid-value', () => {
const repairer = new ToolArgumentsRepairer();
// Stream drops in the middle of the content value
const incompleteArgs = '{"title": "My Document", "content": "This is the beginning of';
const result = repairer.parse('createDocument', incompleteArgs);
expect(result.title).toBe('My Document');
expect(result.content).toBe('This is the beginning of');
});
it('should recover when stream drops after first field', () => {
const repairer = new ToolArgumentsRepairer();
const incompleteArgs = '{"title": "My Document"';
const result = repairer.parse('createDocument', incompleteArgs);
expect(result).toEqual({ title: 'My Document' });
});
it('should return empty object when stream drops before any field value', () => {
const repairer = new ToolArgumentsRepairer();
const result = repairer.parse('createDocument', '{');
expect(result).toEqual({});
});
it('should recover partial JSON and still apply repair if needed', () => {
const manifest: LobeToolManifest = {
identifier: 'lobe-notebook',
api: [
{
name: 'createDocument',
description: 'Create a document',
parameters: {
type: 'object',
required: ['title', 'description', 'content'],
properties: {
title: { type: 'string' },
description: { type: 'string' },
content: { type: 'string' },
},
},
},
],
type: 'builtin',
} as unknown as LobeToolManifest;
const repairer = new ToolArgumentsRepairer(manifest);
// Stream interrupted - has title and description but no content
const incompleteArgs = '{"title": "Test", "description": "Summary"';
const result = repairer.parse('createDocument', incompleteArgs);
// Should recover available fields instead of returning {}
expect(result.title).toBe('Test');
expect(result.description).toBe('Summary');
});
});
});