mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
feat(headroom): add proxy lifecycle management + dashboard UI
Build on the optional Headroom Token Saver from Carmelo Campos
(PR: feat: add optional Headroom token saver). Add managed start/stop
of the local headroom proxy from the dashboard, install detection,
status probing, and a simplified Token Saver UI.
- detect headroom CLI + python>=3.10, probe proxy /health
- spawn/stop proxy as a detached, pid-tracked process
- /api/headroom/{status,start,stop} routes, gated local-only in dashboardGuard
- one-click Start/Stop Headroom modal, no manual config needed
- claude<->openai shape conversion for /v1/compress via 9router translators
Thanks to Carmelo Campos (@carmelogunsroses) for the original Headroom integration.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { describe, it, expect, vi, afterEach } from "vitest";
|
||||
import { compressWithHeadroom, formatHeadroomLog } from "../../open-sse/rtk/headroom.js";
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("compressWithHeadroom", () => {
|
||||
it("no-ops when disabled", async () => {
|
||||
global.fetch = vi.fn();
|
||||
const body = { messages: [{ role: "user", content: "hello" }] };
|
||||
|
||||
const stats = await compressWithHeadroom(body, { enabled: false, url: "http://localhost:8787" });
|
||||
|
||||
expect(stats).toBeNull();
|
||||
expect(global.fetch).not.toHaveBeenCalled();
|
||||
expect(body.messages[0].content).toBe("hello");
|
||||
});
|
||||
|
||||
it("compresses messages in-place", async () => {
|
||||
global.fetch = vi.fn(async () => new Response(JSON.stringify({
|
||||
messages: [{ role: "user", content: "short" }],
|
||||
tokens_before: 100,
|
||||
tokens_after: 20,
|
||||
tokens_saved: 80,
|
||||
}), { status: 200 }));
|
||||
const body = { messages: [{ role: "user", content: "long" }] };
|
||||
|
||||
const stats = await compressWithHeadroom(body, { enabled: true, url: "http://headroom:8787/", model: "gpt-4o" });
|
||||
|
||||
expect(body.messages[0].content).toBe("short");
|
||||
expect(stats.tokens_saved).toBe(80);
|
||||
expect(global.fetch).toHaveBeenCalledWith("http://headroom:8787/v1/compress", expect.objectContaining({ method: "POST" }));
|
||||
});
|
||||
|
||||
it("compresses responses input in-place", async () => {
|
||||
global.fetch = vi.fn(async () => new Response(JSON.stringify({
|
||||
messages: [{ role: "user", content: "short" }],
|
||||
}), { status: 200 }));
|
||||
const body = { input: [{ role: "user", content: "long" }] };
|
||||
|
||||
await compressWithHeadroom(body, { enabled: true, url: "http://localhost:8787" });
|
||||
|
||||
expect(body.input[0].content).toBe("short");
|
||||
});
|
||||
|
||||
it("fails open on bad response", async () => {
|
||||
global.fetch = vi.fn(async () => new Response(JSON.stringify({ error: "bad" }), { status: 500 }));
|
||||
const body = { messages: [{ role: "user", content: "long" }] };
|
||||
|
||||
const stats = await compressWithHeadroom(body, { enabled: true, url: "http://localhost:8787" });
|
||||
|
||||
expect(stats).toBeNull();
|
||||
expect(body.messages[0].content).toBe("long");
|
||||
});
|
||||
|
||||
it("skips unknown shapes", async () => {
|
||||
global.fetch = vi.fn();
|
||||
const body = { contents: [{ parts: [{ text: "long" }] }] };
|
||||
|
||||
const stats = await compressWithHeadroom(body, { enabled: true, url: "http://localhost:8787" });
|
||||
|
||||
expect(stats).toBeNull();
|
||||
expect(global.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatHeadroomLog", () => {
|
||||
it("formats savings", () => {
|
||||
expect(formatHeadroomLog({ tokens_before: 100, tokens_after: 25, tokens_saved: 75 }))
|
||||
.toBe("saved 75 tokens / 100 (75.0%) after=25");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user