feat: native tool calling message normalization for Ollama and llama.cpp
- ollama.ts: add normalizeMessagesForOllama() converting Anthropic-style tool_use/tool_result blocks to Ollama's native tool_calls + role:tool format - llamacpp.ts: add normalizeMessagesForLlamaCpp() with hybrid approach — assistant tool_calls in native format, but tool results as structured user messages (many GGUF templates silently drop role:tool messages) - llamacpp.ts: add configurable requestTimeout with AbortController (default 3min) - Both use fast-path when no tool blocks are present (zero overhead) - Full test coverage for both normalizers: plain text passthrough, tool_use conversion, tool_result mapping, multi-tool round trips, error results
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { LlamaCppClient } from './llamacpp.js';
|
||||
import type { ChatStreamEvent } from '../types.js';
|
||||
import { LlamaCppClient, normalizeMessagesForLlamaCpp } from './llamacpp.js';
|
||||
import type { ChatStreamEvent, Message } from '../types.js';
|
||||
|
||||
describe('LlamaCppClient', () => {
|
||||
const mockFetch = vi.fn();
|
||||
@@ -341,3 +341,172 @@ describe('LlamaCppClient', () => {
|
||||
expect(requestBody.stream).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeMessagesForLlamaCpp', () => {
|
||||
it('passes plain text messages through', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'Hello' },
|
||||
{ role: 'assistant', content: 'Hi there' },
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp('System prompt', messages);
|
||||
|
||||
expect(result).toEqual([
|
||||
{ role: 'system', content: 'System prompt' },
|
||||
{ role: 'user', content: 'Hello' },
|
||||
{ role: 'assistant', content: 'Hi there' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('converts assistant tool_use blocks to OpenAI tool_calls format', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'Search for news' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: 'Searching...' },
|
||||
{ type: 'tool_use', id: 'call_1', name: 'web.search', input: { query: 'news' } },
|
||||
] as any,
|
||||
},
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp(undefined, messages);
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[1]).toEqual({
|
||||
role: 'assistant',
|
||||
content: 'Searching...',
|
||||
tool_calls: [{
|
||||
id: 'call_1',
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'web.search',
|
||||
arguments: '{"query":"news"}',
|
||||
},
|
||||
}],
|
||||
});
|
||||
});
|
||||
|
||||
it('converts user tool_result blocks to user messages with text formatting', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'Search' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'tool_use', id: 'call_1', name: 'web.search', input: { query: 'news' } },
|
||||
] as any,
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'tool_result', tool_use_id: 'call_1', content: 'Results here', is_error: false },
|
||||
] as any,
|
||||
},
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp(undefined, messages);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[2]).toEqual({
|
||||
role: 'user',
|
||||
content: '[Tool "web.search" result]\nResults here',
|
||||
});
|
||||
});
|
||||
|
||||
it('handles multiple tool results in a single user message', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'Do two things' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'tool_use', id: 'call_a', name: 'tool.a', input: {} },
|
||||
{ type: 'tool_use', id: 'call_b', name: 'tool.b', input: { x: 1 } },
|
||||
] as any,
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'tool_result', tool_use_id: 'call_a', content: 'A result' },
|
||||
{ type: 'tool_result', tool_use_id: 'call_b', content: 'B result' },
|
||||
] as any,
|
||||
},
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp(undefined, messages);
|
||||
|
||||
expect(result[1].tool_calls).toHaveLength(2);
|
||||
expect(result[1].tool_calls![0].id).toBe('call_a');
|
||||
expect(result[1].tool_calls![1].function.arguments).toBe('{"x":1}');
|
||||
// Multiple results merged into one user message
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[2]).toEqual({
|
||||
role: 'user',
|
||||
content: '[Tool "tool.a" result]\nA result\n\n[Tool "tool.b" result]\nB result',
|
||||
});
|
||||
});
|
||||
|
||||
it('marks error results in text formatting', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'Do it' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'tool_use', id: 'call_1', name: 'file.read', input: { path: '/tmp/x' } },
|
||||
] as any,
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'tool_result', tool_use_id: 'call_1', content: 'File not found', is_error: true },
|
||||
] as any,
|
||||
},
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp(undefined, messages);
|
||||
|
||||
expect(result[2]).toEqual({
|
||||
role: 'user',
|
||||
content: '[Tool "file.read" result (error)]\nFile not found',
|
||||
});
|
||||
});
|
||||
|
||||
it('handles full tool round-trip conversation', () => {
|
||||
const messages: Message[] = [
|
||||
{ role: 'user', content: 'What is the weather?' },
|
||||
{
|
||||
role: 'assistant',
|
||||
content: [
|
||||
{ type: 'text', text: 'Checking...' },
|
||||
{ type: 'tool_use', id: 'tc_0', name: 'weather.get', input: { city: 'NYC' } },
|
||||
] as any,
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'tool_result', tool_use_id: 'tc_0', content: 'Sunny, 72F' },
|
||||
] as any,
|
||||
},
|
||||
{ role: 'assistant', content: 'The weather in NYC is sunny, 72F.' },
|
||||
];
|
||||
|
||||
const result = normalizeMessagesForLlamaCpp('You are helpful.', messages);
|
||||
|
||||
expect(result).toHaveLength(5);
|
||||
expect(result[0]).toEqual({ role: 'system', content: 'You are helpful.' });
|
||||
expect(result[1]).toEqual({ role: 'user', content: 'What is the weather?' });
|
||||
expect(result[2]).toEqual({
|
||||
role: 'assistant',
|
||||
content: 'Checking...',
|
||||
tool_calls: [{
|
||||
id: 'tc_0',
|
||||
type: 'function',
|
||||
function: { name: 'weather.get', arguments: '{"city":"NYC"}' },
|
||||
}],
|
||||
});
|
||||
expect(result[3]).toEqual({
|
||||
role: 'user',
|
||||
content: '[Tool "weather.get" result]\nSunny, 72F',
|
||||
});
|
||||
expect(result[4]).toEqual({ role: 'assistant', content: 'The weather in NYC is sunny, 72F.' });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user