- slow_down now sleeps (and waits longer) instead of re-polling at once; the interval cap that could lower a server interval is gone. The deadline already bounds the loop. - Non-JSON error bodies from gateways and the Vercel checkpoint no longer surface as SyntaxError; the HTTP status is preserved. - Drop the dead process.env.NOUS_API_KEY fallback in fetchModels: envApiKeyAuth already resolves it into the credential upstream. - apply() persists the catalog flag before mutating memory, so a read-only agent dir cannot leave the two out of step. - sleep() replaced with node:timers/promises; three unused exports removed; stale tags.user comment corrected; hermes filter explained. - Tests: 8 (was 4). Pin tierOf, the tier header, and withoutTierHeader on both stream entry points; t.mock.method replaces hand-rolled fetch swaps; stub gains getAgentDir. - Docs: README covers install lockfile flag, hermes row, browser-UA posture, and typecheck path pinning; AGENTS.md allowlist includes pi-coding-agent; tests/AGENTS.md contracts updated.
103 lines
5.3 KiB
JavaScript
103 lines
5.3 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import { createJiti } from "jiti";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
// Host packages are not installed here (pi injects them at load time), so import index.ts through jiti —
|
|
// the same TS loader pi uses — with the host imports aliased to a minimal stub.
|
|
const stub = fileURLToPath(new URL("./stubs/pi-ai.mjs", import.meta.url));
|
|
const jiti = createJiti(import.meta.url, {
|
|
// Longest-prefix first: pi-ai/compat must precede pi-ai or the bare key swallows the subpath.
|
|
alias: {
|
|
"@earendil-works/pi-ai/compat": stub,
|
|
"@earendil-works/pi-ai": stub,
|
|
"@earendil-works/pi-coding-agent": stub,
|
|
},
|
|
});
|
|
const { toModel, toCredential, fetchModels, tierOf, withoutTierHeader } = await jiti.import("../index.ts");
|
|
|
|
test("toModel: per-token price becomes per-million, image input from architecture, reasoning by id", () => {
|
|
const m = toModel({ id: "OpenAI/GPT-5.4-Nano", context_length: 400000, architecture: { input_modalities: ["text", "image"] }, pricing: { prompt: "0.00000016", completion: "0.000001" } });
|
|
assert.equal(m.cost.input, 0.16);
|
|
assert.equal(m.cost.output, 1);
|
|
assert.deepEqual(m.input, ["text", "image"]);
|
|
assert.equal(m.reasoning, true);
|
|
assert.equal(m.baseUrl, "https://inference-api.nousresearch.com/v1");
|
|
assert.equal(m.headers, undefined, "untiered models carry no header");
|
|
});
|
|
|
|
test("tierOf: free by synthesizedFreeVariant or :free suffix, promo only when price dropped, else undefined", () => {
|
|
assert.equal(tierOf({ id: "tencent/hy4-flash:free" }), "free");
|
|
assert.equal(tierOf({ id: "x", synthesizedFreeVariant: true }), "free");
|
|
assert.equal(tierOf({ id: "google/gemini-4-carbon", pricing: { prompt: "0.000002", original: { prompt: "0.000004" } } }), "promo");
|
|
assert.equal(tierOf({ id: "x", pricing: { prompt: "0.000002", original: { prompt: "0.000002" } } }), undefined, "no discount");
|
|
assert.equal(tierOf({ id: "x", pricing: { prompt: "0.000004", original: { prompt: "0.000002" } } }), undefined, "price raised");
|
|
assert.equal(tierOf({ id: "x" }), undefined);
|
|
});
|
|
|
|
test("toModel: the catalog tier rides on the model as the tier header (pi persists and restores model objects)", () => {
|
|
const free = toModel({ id: "upstage/solar-mini4:free" });
|
|
assert.equal(free.headers["x-nous-catalog-tier"], "free");
|
|
assert.deepEqual(free.input, ["text"]);
|
|
assert.equal(free.reasoning, false);
|
|
assert.equal(free.cost.input, 0);
|
|
assert.equal(free.contextWindow, 131072);
|
|
});
|
|
|
|
test("withoutTierHeader: strips the tier header on both stream entry points and leaves everything else untouched", async () => {
|
|
const calls = [];
|
|
const api = {
|
|
stream: (m, c, o) => (calls.push(["stream", m, c, o]), "s"),
|
|
streamSimple: (m, c, o) => (calls.push(["streamSimple", m, c, o]), "ss"),
|
|
};
|
|
const wrapped = withoutTierHeader(api);
|
|
const tiered = { id: "m", headers: { "x-nous-catalog-tier": "free", "x-keep": "1" } };
|
|
const ctx = { messages: [] };
|
|
const opts = { apiKey: "k" };
|
|
assert.equal(await wrapped.stream(tiered, ctx, opts), "s");
|
|
assert.equal(await wrapped.streamSimple(tiered, ctx, opts), "ss");
|
|
for (const [entry, model, , options] of calls) {
|
|
assert.equal(model.headers["x-nous-catalog-tier"], undefined, entry);
|
|
assert.equal(model.headers["x-keep"], "1", "other headers survive");
|
|
assert.notEqual(model, tiered, "original model object is not mutated");
|
|
assert.equal(options, opts, "options pass through untouched");
|
|
assert.equal(tiered.headers["x-nous-catalog-tier"], "free", "caller's model is not mutated");
|
|
}
|
|
// Untiered models take the zero-copy fast path.
|
|
const plain = { id: "p" };
|
|
wrapped.stream(plain, ctx, opts);
|
|
assert.equal(calls[2][1], plain);
|
|
});
|
|
|
|
test("toCredential: keeps the previous refresh token when the server rotates only the access token", () => {
|
|
const prev = { type: "oauth", access: "old", refresh: "rt_keep", expires: 0 };
|
|
const rotated = toCredential({ access_token: "new", expires_in: 100 });
|
|
assert.equal(rotated.refresh, "");
|
|
const kept = toCredential({ access_token: "new", expires_in: 100 }, prev);
|
|
assert.equal(kept.refresh, "rt_keep");
|
|
assert.equal(kept.access, "new");
|
|
assert.ok(kept.expires > Date.now());
|
|
});
|
|
|
|
test("fetchModels: returns [] and never touches the network with no credential", async (t) => {
|
|
const mocked = t.mock.method(globalThis, "fetch", () => { throw new Error("must not fetch"); });
|
|
assert.deepEqual(await fetchModels(undefined, new AbortController().signal), []);
|
|
assert.equal(mocked.mock.callCount(), 0);
|
|
});
|
|
|
|
test("fetchModels: an OAuth credential is used as the bearer; hermes entries are dropped; the rest are mapped", async (t) => {
|
|
let auth;
|
|
t.mock.method(globalThis, "fetch", async (url, init) => {
|
|
auth = init.headers.Authorization;
|
|
return { ok: true, json: async () => ({ data: [{ id: "hermes/agent" }, { id: "qwen/qwen3.5-35b-a3b" }] }) };
|
|
});
|
|
const models = await fetchModels({ type: "oauth", access: "tok" }, new AbortController().signal);
|
|
assert.equal(auth, "Bearer tok");
|
|
assert.deepEqual(models.map((m) => m.id), ["qwen/qwen3.5-35b-a3b"]);
|
|
});
|
|
|
|
test("fetchModels: a non-OK response throws with the status", async (t) => {
|
|
t.mock.method(globalThis, "fetch", async () => ({ ok: false, status: 503 }));
|
|
await assert.rejects(fetchModels({ type: "api_key", key: "k" }, new AbortController().signal), /503/);
|
|
});
|