Files
openclaw-ollama-toolcall-proxy/test/vllm-rewriter.test.ts
2026-03-22 20:49:44 +08:00

81 lines
2.6 KiB
TypeScript
Executable File

import { describe, it, expect } from 'vitest';
import { rewriteVllmResponse } from '../src/proxy/vllm-response-rewriter';
describe('vLLM Response Rewriter', () => {
it('rewrites XML tool call in OpenAI choices content into structured tool_calls', () => {
const inputResponse = {
id: "chatcmpl-123",
choices: [{
index: 0,
message: {
role: "assistant",
content: "<function=read>\n<parameter=path>\n/tmp/test.txt\n</parameter>\n</function>"
}
}]
};
const result = rewriteVllmResponse(inputResponse);
expect(result.choices[0].message.content).toBe("");
expect(result.choices[0].message.tool_calls).toBeDefined();
expect(result.choices[0].message.tool_calls).toHaveLength(1);
const toolCall = result.choices[0].message.tool_calls![0];
expect(toolCall.type).toBe('function');
expect(toolCall.function.name).toBe('read');
const argsObject = JSON.parse(toolCall.function.arguments);
expect(argsObject).toEqual({ path: '/tmp/test.txt' });
});
it('normalizes response that already has tool_calls into a tool-first shape', () => {
const inputResponse = {
choices: [{
message: {
role: "assistant",
content: "Here are the calls",
thinking: "<think>internal</think>",
tool_calls: [
{
id: "123",
type: "function",
function: { name: "read", arguments: "{}" }
}
]
}
}]
};
const result = rewriteVllmResponse(inputResponse);
expect(result.choices[0].message.content).toBe("");
expect(result.choices[0].message.thinking).toBe("");
expect(result.choices[0].message.tool_calls).toHaveLength(1);
});
it('rewrites tool call found in reasoning_content into structured tool_calls', () => {
const inputResponse = {
id: "chatcmpl-123",
choices: [{
index: 0,
message: {
role: "assistant",
content: "",
reasoning_content: "<tool_call>\n{\"name\":\"read\",\"arguments\":{\"path\":\"/tmp/test.txt\"}}\n</tool_call>"
}
}]
};
const result = rewriteVllmResponse(inputResponse);
expect(result.choices[0].message.content).toBe("");
expect(result.choices[0].message.tool_calls).toBeDefined();
expect(result.choices[0].message.tool_calls).toHaveLength(1);
expect(result.choices[0].message.tool_calls[0].function.name).toBe("read");
expect(JSON.parse(result.choices[0].message.tool_calls[0].function.arguments)).toEqual({
path: "/tmp/test.txt"
});
expect(result.choices[0].message.reasoning_content).toBe("");
});
});