fix(native-agent): recover textual tool_use JSON calls

This commit is contained in:
William Valentin
2026-02-19 08:55:41 -08:00
parent dcbb4649a2
commit b6c8d8ddf4
2 changed files with 181 additions and 14 deletions
+38
View File
@@ -337,6 +337,44 @@ describe('NativeAgent tool loop', () => {
expect(history[history.length - 1]).toEqual({ role: 'assistant', content: response });
});
it('recovers and executes valid textual tool_use JSON for registered tools', async () => {
let callCount = 0;
const mockClient: ModelClient = {
chat: vi.fn().mockImplementation(() => {
callCount++;
if (callCount === 1) {
return {
content: 'Running tool now: {"type":"tool_use","id":"call_123","name":"test_echo","input":{"text":"hello"}}',
stopReason: 'end_turn',
usage: { inputTokens: 10, outputTokens: 5 },
};
}
return {
content: 'The tool returned: hello',
stopReason: 'end_turn',
usage: { inputTokens: 10, outputTokens: 5 },
};
}),
};
const registry = new ToolRegistry();
registry.register(echoTool);
const hooks = new HookEngine({ confirm: [], log: [], silent: [] });
const executor = new ToolExecutor(registry, hooks);
const agent = new NativeAgent({
modelClient: mockClient,
systemPrompt: 'You are helpful.',
toolRegistry: registry,
toolExecutor: executor,
});
const response = await agent.process('echo hello');
expect(response).toBe('The tool returned: hello');
expect(mockClient.chat).toHaveBeenCalledTimes(2);
});
it('works without tools (backward compatible)', async () => {
const mockClient: ModelClient = {
chat: vi.fn().mockResolvedValue({