Files
plugins/packages/agentcompass/src/quota-loader.js
T
Akiiwa d951674164 agentcompass 0.1.0: intent, capability and quota routing as a gateway middleware (#670)
New package @magpie-community/gateway-agentcompass 0.1.0 (akiiwa-games, #670): a gateway middleware that profiles a request and routes `agentcompass` / `agentcompass/*` to a model and effort within the user's own candidateModels. Only `model` and the effort field change; nothing is added to the prompt; no network unless the user sets allowExternalScorer: true plus a URL.

Reviewed at head 3bdea54 (rebased on 7ea6119), after three review rounds.

Run on mote (macOS), at 3bdea54:
- `bun scripts/check.mjs agentcompass`: ok.
- packages/agentcompass `node --test test/router.test.mjs`: 33 pass, 0 fail.
- Every network test passes a mock fetch; the one that doesn't (9.5) replaces globalThis.fetch with a thrower.

Broken by hand, each turned a test red:
- carrier deletes in middleware.js removed: 9.3 and 9.6 red.
- the 400 reject for a virtual model with no candidates replaced by `return undefined`: 9.2 red.
- MAX_NOISE_STRIP_CHARS / MAX_USER_TURN_CHARS set to 1e12: 9.9 red.
- an unclaimed request that carries the key made to return a mutated body: 9.6 red.

Read: the carrier is no longer read from the body (useRouteCarrier, carrierFromBody and the profile branch are gone); DEFAULT_GATEWAY_CANDIDATES is gone; no process.env, no default URL, quotas.json only from an explicit path.

Not tried: the plugin in magpie's real moejs runtime at this head (the review of 9d22371 did that; the changes since are the ones above), and a real gateway with real accounts. Leftover header comments in scorer-live.js, quota.js, catalog.js and allocator.js still say `magpie.hooks`; harmless, to tidy in a later version.
2026-11-04 04:09:20 +08:00

95 lines
2.7 KiB
JavaScript

/**
* Explicit Quota Snapshot Loader for Bun (`magpie.hooks` & `/v1/systemone`)
*
* By default, performs zero disk reads and never walks parent directories or probes
* Magpie's internal configuration directory.
* Only reads from:
* 1. Explicit `options.quotas` in-memory object/array (returned immediately)
* 2. Explicit `options.quotasPath` / `options.quotaFile` / `options.magpieConfigDir` when explicitly configured by the user
*/
import { existsSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
const CACHE_TTL_MS = 5000;
let cachedPath = "";
let cachedMtimeMs = 0;
let cachedReadAt = 0;
let cachedQuotas = null;
/**
* Resolve an explicitly configured quotas file path if provided in `options`.
* Never walks parent directories or probes `$HOME` / `$XDG_CONFIG_HOME`.
*/
export function resolveMagpieQuotasFile(options = {}) {
const explicitFile = options.quotasPath || options.quotaFile;
if (typeof explicitFile === "string" && explicitFile.trim()) {
return explicitFile.trim();
}
const explicitDir = options.magpieConfigDir;
if (typeof explicitDir === "string" && explicitDir.trim()) {
const dir = explicitDir.trim();
const candidate = join(dir, "quotas.json");
if (existsSync(candidate)) return candidate;
const nested = join(dir, "magpie", "quotas.json");
if (existsSync(nested)) return nested;
return candidate;
}
return "";
}
/**
* Load live subscription quotas from `options.quotas` or an explicitly configured `options.quotasPath` / `options.quotaFile`.
*/
export function loadLiveQuotas(options = {}, nowMs = Date.now()) {
if (options.quotas && typeof options.quotas === "object") {
return options.quotas;
}
if (options.disableQuotas === true) {
return null;
}
const qPath = resolveMagpieQuotasFile(options);
if (!qPath) return null;
if (
cachedQuotas &&
cachedPath === qPath &&
nowMs - cachedReadAt < CACHE_TTL_MS
) {
return cachedQuotas;
}
try {
if (!existsSync(qPath)) return null;
const st = statSync(qPath);
if (cachedQuotas && cachedPath === qPath && st.mtimeMs === cachedMtimeMs) {
cachedReadAt = nowMs;
return cachedQuotas;
}
const rawText = readFileSync(qPath, "utf8");
const parsed = JSON.parse(rawText);
if (parsed && typeof parsed === "object") {
cachedPath = qPath;
cachedMtimeMs = st.mtimeMs;
cachedReadAt = nowMs;
cachedQuotas = parsed;
return parsed;
}
} catch {
// Non-fatal if quotas file is unavailable or being written concurrently
}
return null;
}
export function clearQuotasCache() {
cachedPath = "";
cachedMtimeMs = 0;
cachedReadAt = 0;
cachedQuotas = null;
}