mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-20 22:22:57 +03:00
- Remove `new Request('http://localhost/api/keys')` default arg from GET handler in src/app/api/keys/route.ts (line 26)
- Fix api-key-reveal-route.test.mjs to pass explicit Request instead of calling GET() with no args
- Add --output-dir coverage to all c8 scripts in package.json
- Add coverage.reportsDirectory: 'coverage' to vitest.config.ts and vitest.mcp.config.ts
- Fix CHANGELOG.md structure (# Changelog + [Unreleased] to top)
- Remove 30+ stale coverage-* directories from project root
- Coverage: Statements 78.76% | Branches 72.75% | Functions 80.93% | Lines 78.76% (all thresholds passed)
267 lines
7.3 KiB
JavaScript
267 lines
7.3 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
const { openaiResponsesToOpenAIRequest, openaiToOpenAIResponsesRequest } =
|
|
await import("../../open-sse/translator/request/openai-responses.ts");
|
|
|
|
test("Responses -> Chat converts instructions, inputs, function calls, outputs, tools and tool_choice", () => {
|
|
const result = openaiResponsesToOpenAIRequest(
|
|
"gpt-4o",
|
|
{
|
|
instructions: "Rules",
|
|
input: [
|
|
{
|
|
type: "message",
|
|
role: "user",
|
|
content: [
|
|
{ type: "input_text", text: "Hello" },
|
|
{ type: "input_image", image_url: "https://example.com/cat.png", detail: "high" },
|
|
{ type: "input_file", file_data: "abc", filename: "doc.txt" },
|
|
],
|
|
},
|
|
{
|
|
type: "function_call",
|
|
call_id: "call_1",
|
|
name: "read_file",
|
|
arguments: { path: "/tmp/a" },
|
|
},
|
|
{
|
|
type: "function_call_output",
|
|
call_id: "call_1",
|
|
output: { ok: true },
|
|
},
|
|
],
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
name: "read_file",
|
|
description: "Read",
|
|
parameters: { type: "object" },
|
|
},
|
|
],
|
|
tool_choice: { type: "function", name: "read_file" },
|
|
},
|
|
false,
|
|
null
|
|
);
|
|
|
|
assert.deepEqual(result.messages, [
|
|
{ role: "system", content: "Rules" },
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{ type: "text", text: "Hello" },
|
|
{ type: "image_url", image_url: { url: "https://example.com/cat.png", detail: "high" } },
|
|
{ type: "file", file: { file_data: "abc", filename: "doc.txt" } },
|
|
],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: null,
|
|
tool_calls: [
|
|
{
|
|
id: "call_1",
|
|
type: "function",
|
|
function: { name: "read_file", arguments: '{"path":"/tmp/a"}' },
|
|
},
|
|
],
|
|
},
|
|
{ role: "tool", tool_call_id: "call_1", content: '{"ok":true}' },
|
|
]);
|
|
assert.deepEqual(result.tools, [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "read_file",
|
|
description: "Read",
|
|
parameters: { type: "object" },
|
|
strict: undefined,
|
|
},
|
|
},
|
|
]);
|
|
assert.deepEqual(result.tool_choice, { type: "function", function: { name: "read_file" } });
|
|
});
|
|
|
|
test("Responses -> Chat filters orphan tool outputs and supports role-based message items", () => {
|
|
const result = openaiResponsesToOpenAIRequest(
|
|
"gpt-4o",
|
|
{
|
|
input: [
|
|
{ role: "user", content: [{ type: "input_text", text: "Hello" }] },
|
|
{ type: "function_call_output", call_id: "orphan", output: "skip" },
|
|
{ type: "function_call", call_id: "call_2", name: "search", arguments: "{}" },
|
|
{ type: "function_call_output", call_id: "call_2", output: "found" },
|
|
],
|
|
},
|
|
false,
|
|
null
|
|
);
|
|
|
|
assert.equal(result.messages.length, 3);
|
|
assert.equal(result.messages[0].role, "user");
|
|
assert.equal(result.messages[1].tool_calls[0].id, "call_2");
|
|
assert.deepEqual(result.messages[2], {
|
|
role: "tool",
|
|
tool_call_id: "call_2",
|
|
content: "found",
|
|
});
|
|
});
|
|
|
|
test("Responses -> Chat rejects unsupported built-in tools and background mode", () => {
|
|
assert.throws(
|
|
() =>
|
|
openaiResponsesToOpenAIRequest(
|
|
"gpt-4o",
|
|
{
|
|
input: [],
|
|
tools: [{ type: "web_search_preview", name: "search" }],
|
|
},
|
|
false,
|
|
null
|
|
),
|
|
(error) => error.statusCode === 400 && error.errorType === "unsupported_feature"
|
|
);
|
|
|
|
assert.throws(
|
|
() =>
|
|
openaiResponsesToOpenAIRequest(
|
|
"gpt-4o",
|
|
{
|
|
input: [],
|
|
background: true,
|
|
},
|
|
false,
|
|
null
|
|
),
|
|
(error) => error.statusCode === 400 && error.errorType === "unsupported_feature"
|
|
);
|
|
});
|
|
|
|
test("Chat -> Responses converts messages, tool calls, tool outputs, tools and pass-through params", () => {
|
|
const result = openaiToOpenAIResponsesRequest(
|
|
"gpt-4o",
|
|
{
|
|
messages: [
|
|
{ role: "system", content: "Rules" },
|
|
{
|
|
role: "user",
|
|
content: [
|
|
{ type: "text", text: "Hello" },
|
|
{
|
|
type: "image_url",
|
|
image_url: { url: "https://example.com/cat.png", detail: "high" },
|
|
},
|
|
{ type: "file", file: { file_data: "abc", filename: "doc.txt" } },
|
|
],
|
|
},
|
|
{
|
|
role: "assistant",
|
|
content: "Done",
|
|
tool_calls: [
|
|
{
|
|
id: "call_1",
|
|
type: "function",
|
|
function: { name: "read_file", arguments: '{"path":"/tmp/a"}' },
|
|
},
|
|
],
|
|
},
|
|
{ role: "tool", tool_call_id: "call_1", content: [{ type: "text", text: "ok" }] },
|
|
],
|
|
tools: [
|
|
{
|
|
type: "function",
|
|
function: {
|
|
name: "read_file",
|
|
description: "Read",
|
|
parameters: { type: "object" },
|
|
strict: true,
|
|
},
|
|
},
|
|
],
|
|
tool_choice: { type: "function", function: { name: "read_file" } },
|
|
temperature: 0.2,
|
|
max_tokens: 100,
|
|
top_p: 0.9,
|
|
},
|
|
false,
|
|
null
|
|
);
|
|
|
|
assert.equal(result.instructions, "Rules");
|
|
assert.equal(result.stream, true);
|
|
assert.equal(result.store, false);
|
|
assert.deepEqual(result.input, [
|
|
{
|
|
type: "message",
|
|
role: "user",
|
|
content: [
|
|
{ type: "input_text", text: "Hello" },
|
|
{ type: "input_image", image_url: "https://example.com/cat.png", detail: "high" },
|
|
{ type: "input_file", file_data: "abc", filename: "doc.txt" },
|
|
],
|
|
},
|
|
{
|
|
type: "message",
|
|
role: "assistant",
|
|
content: [{ type: "output_text", text: "Done" }],
|
|
},
|
|
{
|
|
type: "function_call",
|
|
call_id: "call_1",
|
|
name: "read_file",
|
|
arguments: '{"path":"/tmp/a"}',
|
|
},
|
|
{
|
|
type: "function_call_output",
|
|
call_id: "call_1",
|
|
output: [{ type: "input_text", text: "ok" }],
|
|
},
|
|
]);
|
|
assert.deepEqual(result.tools, [
|
|
{
|
|
type: "function",
|
|
name: "read_file",
|
|
description: "Read",
|
|
parameters: { type: "object" },
|
|
strict: true,
|
|
},
|
|
]);
|
|
assert.deepEqual(result.tool_choice, { type: "function", name: "read_file" });
|
|
assert.equal(result.temperature, 0.2);
|
|
assert.equal(result.max_tokens, 100);
|
|
assert.equal(result.top_p, 0.9);
|
|
});
|
|
|
|
test("Chat -> Responses filters orphan function_call_output items and leaves empty instructions when absent", () => {
|
|
const result = openaiToOpenAIResponsesRequest(
|
|
"gpt-4o",
|
|
{
|
|
messages: [
|
|
{ role: "user", content: "Hello" },
|
|
{ role: "tool", tool_call_id: "orphan", content: "skip" },
|
|
{
|
|
role: "assistant",
|
|
tool_calls: [
|
|
{
|
|
id: "call_2",
|
|
type: "function",
|
|
function: { name: "search", arguments: "{}" },
|
|
},
|
|
],
|
|
},
|
|
{ role: "tool", tool_call_id: "call_2", content: "found" },
|
|
],
|
|
},
|
|
false,
|
|
null
|
|
);
|
|
|
|
assert.equal(result.instructions, "");
|
|
assert.equal(
|
|
result.input.some((item) => item.call_id === "orphan"),
|
|
false
|
|
);
|
|
assert.equal(result.input.filter((item) => item.type === "function_call_output").length, 1);
|
|
assert.equal(result.input.find((item) => item.type === "function_call_output").call_id, "call_2");
|
|
});
|