Files
openclaw-ollama-toolcall-proxy/test/response-rewriter.test.ts

53 lines
1.6 KiB
TypeScript
Executable File

import { describe, it, expect } from 'vitest';
import { rewriteResponse } from '../src/proxy/response-rewriter';
import { OllamaChatResponse } from '../src/types/ollama';
describe('Response Rewriter', () => {
it('rewrites XML tool call in content into structured tool_calls', () => {
const inputResponse: OllamaChatResponse = {
model: "test-model",
done: true,
message: {
role: "assistant",
content: "<function=read>\n<parameter=path>\n/tmp/test.txt\n</parameter>\n</function>"
}
};
const result = rewriteResponse(inputResponse);
expect(result.message.content).toBe("");
expect(result.message.tool_calls).toBeDefined();
expect(result.message.tool_calls).toHaveLength(1);
const toolCall = result.message.tool_calls![0];
expect(toolCall.type).toBe('function');
expect(toolCall.function.name).toBe('read');
const argsObject = toolCall.function.arguments;
expect(argsObject).toEqual({ path: '/tmp/test.txt' });
});
it('does not touch response that already has tool_calls', () => {
const inputResponse: OllamaChatResponse = {
model: "test-model",
done: true,
message: {
role: "assistant",
content: "Here are the calls",
tool_calls: [
{
id: "123",
type: "function",
function: { name: "read", arguments: "{}" }
}
]
}
};
const result = rewriteResponse(inputResponse);
expect(result.message.content).toBe("Here are the calls"); // not cleared
expect(result.message.tool_calls).toHaveLength(1);
});
});