feat: complete the Homarr provider endpoint

This commit is contained in:
ajnart committed 2026-08-12 13:27:04 +02:00
1 parent 019bd11017
commit 83968be07e
10 files changed
+457 -18

No files matched your search

@@ -0,0 +1,52 @@
const enabled = process.env.HOMARR_AI_LIVE_SMOKE === "1";
if (!enabled)
throw new Error("Set HOMARR_AI_LIVE_SMOKE=1 to acknowledge that this check consumes a real provider request");
const baseUrl = process.env.WORKSHOP_LIVE_TEST_URL?.replace(/\/+$/u, "");
const token = process.env.WORKSHOP_LIVE_TEST_TOKEN;
if (!baseUrl || !token) {
throw new Error("WORKSHOP_LIVE_TEST_URL and WORKSHOP_LIVE_TEST_TOKEN are required");
}
const requestJson = async (path, init = {}) => {
const response = await fetch(`${baseUrl}${path}`, {
...init,
headers: { "content-type": "application/json", authorization: `Bearer ${token}`, ...init.headers },
signal: AbortSignal.timeout(60_000),
});
const body = await response.json().catch(() => null);
if (!response.ok) throw new Error(`${response.status} ${path}: ${JSON.stringify(body)}`);
return { body, headers: response.headers };
};
const { body: models } = await requestJson("/api/ai/v1/models");
if (models.data?.length !== 1 || models.data[0]?.id !== "homarr/model") {
throw new Error("The live Workshop endpoint did not advertise the stable Homarr model");
}
const { body: before } = await requestJson("/api/ai/usage");
if (before.remaining < 1) throw new Error("The live test user has no remaining Homarr provider allowance");
const { body: completion, headers } = await requestJson("/api/ai/v1/chat/completions", {
method: "POST",
body: JSON.stringify({
model: "homarr/model",
messages: [{ role: "user", content: "Reply with exactly OK." }],
stream: false,
}),
});
if (
completion.model !== "homarr/model" ||
typeof completion.choices?.[0]?.message?.content !== "string" ||
completion.choices[0].message.content.length === 0 ||
headers.get("x-homarr-quota-remaining") === null
) {
throw new Error("The live Homarr provider did not return an OpenAI-compatible completion with quota headers");
}
const { body: after } = await requestJson("/api/ai/usage");
if (after.used < before.used + 1 || after.remaining >= before.remaining) {
throw new Error("The live completion was not charged to the authenticated Workshop user");
}
process.stdout.write("Live Homarr provider smoke test passed\n");
+83 -2
View File
@@ -2,6 +2,7 @@ import { createServer } from "node:http";
const port = Number(process.env.PORT ?? 18091);
const expectedModel = process.env.EXPECTED_MODEL ?? "mock/team-selected-model";
const expectedApiKey = process.env.EXPECTED_API_KEY ?? "workshop-test-openrouter-key";
const json = (response, status, body) => {
response.writeHead(status, { "content-type": "application/json" });
@@ -17,10 +18,18 @@ const server = createServer(async (request, response) => {
json(response, 404, { error: { message: "Not found" } });
return;
}
if (request.headers.authorization !== "Bearer workshop-test-openrouter-key") {
if (request.headers.authorization !== `Bearer ${expectedApiKey}`) {
json(response, 401, { error: { message: "Invalid upstream credentials" } });
return;
}
if (
request.headers["http-referer"] !== "https://homarr.dev" ||
request.headers["x-openrouter-title"] !== "Homarr AI Assistant" ||
request.headers["x-openrouter-metadata"] !== "enabled"
) {
json(response, 400, { error: { message: "Homarr upstream attribution headers are missing or spoofed" } });
return;
}
const chunks = [];
for await (const chunk of request) chunks.push(chunk);
@@ -72,13 +81,42 @@ const server = createServer(async (request, response) => {
json(response, 503, { error: { message: "Simulated upstream failure" } });
return;
}
if (body.messages?.some((message) => message.content === "in-flight probe")) {
await new Promise((resolve) => setTimeout(resolve, 500));
}
const wantsToolCall = body.messages?.some((message) => message.content === "inspect my dashboard");
if (wantsToolCall && !body.tools?.some((tool) => tool.type === "function" && tool.function?.name === "board_list")) {
json(response, 400, { error: { message: "The authenticated function tool was not forwarded" } });
return;
}
const completion = {
id: "gen-workshop-test",
object: "chat.completion",
model: body.model,
choices: [
{ index: 0, message: { role: "assistant", content: "Hello from the Homarr provider" }, finish_reason: "stop" },
wantsToolCall
? {
index: 0,
message: {
role: "assistant",
content: null,
tool_calls: [
{
id: "call-board-list",
type: "function",
function: { name: "board_list", arguments: "{}" },
},
],
},
finish_reason: "tool_calls",
}
: {
index: 0,
message: { role: "assistant", content: "Hello from the Homarr provider" },
finish_reason: "stop",
},
],
usage: { prompt_tokens: 12, completion_tokens: 7, total_tokens: 19, cost: 0.00041 },
};
@@ -92,6 +130,49 @@ const server = createServer(async (request, response) => {
"cache-control": "no-cache",
"x-request-id": "mock-openrouter-request",
});
if (wantsToolCall) {
response.write(
`data: ${JSON.stringify({
id: completion.id,
object: "chat.completion.chunk",
model: body.model,
choices: [
{
index: 0,
delta: {
role: "assistant",
tool_calls: [
{
index: 0,
id: "call-board-list",
type: "function",
function: { name: "board_list", arguments: "{" },
},
],
},
finish_reason: null,
},
],
})}\n\n`,
);
response.write(
`data: ${JSON.stringify({
id: completion.id,
object: "chat.completion.chunk",
model: body.model,
choices: [
{
index: 0,
delta: { tool_calls: [{ index: 0, function: { arguments: "}" } }] },
finish_reason: "tool_calls",
},
],
usage: completion.usage,
})}\n\n`,
);
response.end("data: [DONE]\n\n");
return;
}
response.write(
`data: ${JSON.stringify({
id: completion.id,
+107 -5
View File
@@ -101,6 +101,13 @@ if (unchangedAuthor.name !== "widget-author") {
const visitorPassword = "WorkshopVisitor123!";
const visitor = await createUser("widget-visitor@example.invalid", "widget-visitor", visitorPassword);
const visitorSession = await signIn(visitor.email, visitorPassword);
const concurrencyPassword = "WorkshopConcurrency123!";
const concurrencyUser = await createUser(
"provider-concurrency@example.invalid",
"provider-concurrency",
concurrencyPassword,
);
const concurrencySession = await signIn(concurrencyUser.email, concurrencyPassword);
const modelList = await request("/api/ai/v1/models");
if (
@@ -123,6 +130,42 @@ await expectStatus(
},
401,
);
await expectStatus(
"/api/ai/v1/chat/completions",
{
method: "POST",
headers: { authorization: "Bearer forged-workshop-user-token" },
body: JSON.stringify({
model: "homarr/model",
messages: [{ role: "user", content: "forged identity" }],
}),
},
401,
);
const inFlightProbe = () =>
fetch(`${baseUrl}/api/ai/v1/chat/completions`, {
method: "POST",
headers: { "content-type": "application/json", ...concurrencySession.headers },
body: JSON.stringify({
model: "homarr/model",
messages: [{ role: "user", content: "in-flight probe" }],
stream: false,
}),
});
const inFlightResponses = await Promise.all([inFlightProbe(), inFlightProbe(), inFlightProbe()]);
if (
inFlightResponses
.map((response) => response.status)
.toSorted()
.join(",") !== "200,200,429"
) {
throw new Error("The per-user in-flight request limit was not enforced");
}
const concurrencyUsage = await request("/api/ai/usage", { headers: concurrencySession.headers });
if (concurrencyUsage.used !== 2) {
throw new Error(`Rejected in-flight requests must not consume allowance: ${JSON.stringify(concurrencyUsage)}`);
}
const initialUsage = await request("/api/ai/usage", { headers: authorSession.headers });
const initialReset = new Date(initialUsage.resetsAt);
@@ -163,7 +206,24 @@ await expectStatus(
headers: authorSession.headers,
body: JSON.stringify({
model: "homarr/model",
messages: [{ role: "user", content: "x".repeat(4 * 256 * 1024 + 1) }],
messages: [{ role: "user", content: "x".repeat(256 * 1024 + 1) }],
}),
},
413,
);
await expectStatus(
"/api/ai/v1/chat/completions",
{
method: "POST",
headers: authorSession.headers,
body: JSON.stringify({
model: "homarr/model",
messages: [
{
role: "user",
content: [{ type: "image_url", image_url: { url: "https://attacker.example/tracker.png" } }],
},
],
}),
},
413,
@@ -210,6 +270,8 @@ if (
firstChunk.done ||
!streamText.includes("Hello from ") ||
!streamText.includes("the Homarr provider") ||
streamText.includes("mock/team-selected-model") ||
!streamText.includes('"model":"homarr/model"') ||
dataFrames.length < 3 ||
dataFrames.at(-1)?.trim() !== "data: [DONE]" ||
firstChunkAt - streamStartedAt > 5_000
@@ -218,7 +280,13 @@ if (
}
const toolResponse = await fetch(`${baseUrl}/api/ai/v1/chat/completions`, {
method: "POST",
headers: { "content-type": "application/json", ...authorSession.headers },
headers: {
"content-type": "application/json",
...authorSession.headers,
"http-referer": "https://attacker.example",
"x-openrouter-title": "Attacker",
"x-openrouter-metadata": "attacker",
},
body: JSON.stringify({
model: "homarr/model",
messages: [
@@ -258,6 +326,40 @@ const toolResponse = await fetch(`${baseUrl}/api/ai/v1/chat/completions`, {
if (!toolResponse.ok || toolResponse.headers.get("x-homarr-quota-remaining") !== "48") {
throw new Error("Every upstream inference must consume one Homarr provider request unit");
}
const toolCompletion = await toolResponse.json();
if (
toolCompletion.model !== "homarr/model" ||
toolCompletion.choices?.[0]?.finish_reason !== "tool_calls" ||
toolCompletion.choices?.[0]?.message?.tool_calls?.[0]?.function?.name !== "board_list"
) {
throw new Error(
`The public model alias or function tool response was not forwarded: ${JSON.stringify(toolCompletion)}`,
);
}
const streamedToolResponse = await fetch(`${baseUrl}/api/ai/v1/chat/completions`, {
method: "POST",
headers: { "content-type": "application/json", ...concurrencySession.headers },
body: JSON.stringify({
model: "homarr/model",
messages: [{ role: "user", content: "inspect my dashboard" }],
tools: [{ type: "function", function: { name: "board_list", parameters: { type: "object" } } }],
stream: true,
}),
});
const streamedToolText = await streamedToolResponse.text();
if (
!streamedToolResponse.ok ||
streamedToolText.includes("mock/team-selected-model") ||
!streamedToolText.includes('"model":"homarr/model"') ||
!streamedToolText.includes('"name":"board_list"') ||
!streamedToolText.includes('"arguments":"{"') ||
!streamedToolText.includes('"arguments":"}"') ||
!streamedToolText.includes('"finish_reason":"tool_calls"') ||
!streamedToolText.endsWith("data: [DONE]\n\n")
) {
throw new Error(`Streamed function tool calls were not preserved safely: ${streamedToolText}`);
}
const afterToolsUsage = await request("/api/ai/usage", { headers: authorSession.headers });
if (afterToolsUsage.used !== 2 || afterToolsUsage.remaining !== 48) {
@@ -316,7 +418,7 @@ const resetQuota = (
await request(`/api/collections/assistant_quotas/records/${resetQuota.id}`, {
method: "PATCH",
headers: rootHeaders,
body: JSON.stringify({ used: 48 }),
body: JSON.stringify({ used: 49 }),
});
const allowanceProbe = () =>
@@ -329,11 +431,11 @@ const allowanceProbe = () =>
stream: false,
}),
});
const concurrentAllowanceResponses = await Promise.all([allowanceProbe(), allowanceProbe(), allowanceProbe()]);
const concurrentAllowanceResponses = await Promise.all([allowanceProbe(), allowanceProbe()]);
const concurrentStatuses = concurrentAllowanceResponses.map((response) => response.status).toSorted();
const exhaustedResponse = concurrentAllowanceResponses.find((response) => response.status === 429);
if (
concurrentStatuses.join(",") !== "200,200,429" ||
concurrentStatuses.join(",") !== "200,429" ||
exhaustedResponse?.headers.get("x-homarr-quota-remaining") !== "0" ||
!exhaustedResponse.headers.get("x-homarr-quota-reset")
) {