74 lines
2.4 KiB
TypeScript
Executable File
74 lines
2.4 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);
|
|
});
|
|
|
|
it('rewrites tool call found in thinking into structured tool_calls', () => {
|
|
const inputResponse: OllamaChatResponse = {
|
|
model: "test-model",
|
|
done: true,
|
|
message: {
|
|
role: "assistant",
|
|
content: "",
|
|
thinking: "<tool_call>\n{\"name\":\"read\",\"arguments\":{\"path\":\"/tmp/test.txt\"}}\n</tool_call>"
|
|
}
|
|
};
|
|
|
|
const result = rewriteResponse(inputResponse);
|
|
|
|
expect(result.message.content).toBe("");
|
|
expect(result.message.tool_calls).toBeDefined();
|
|
expect(result.message.tool_calls).toHaveLength(1);
|
|
expect(result.message.tool_calls![0].function.name).toBe("read");
|
|
expect(result.message.tool_calls![0].function.arguments).toEqual({ path: "/tmp/test.txt" });
|
|
expect(result.message.thinking).toBe("");
|
|
});
|
|
});
|