@ouro.bot/cli 0.1.0-alpha.954 → 0.1.0-alpha.956

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.json CHANGED
@@ -1,6 +1,20 @@
1
1
  {
2
2
  "_note": "This changelog is maintained as part of the PR/version-bump workflow. Agent-curated, not auto-generated. Agents read this file directly via read_file to understand what changed between versions.",
3
3
  "versions": [
4
+ {
5
+ "version": "0.1.0-alpha.956",
6
+ "changes": [
7
+ "A new credit gateway (ouro gateway serve) lets agents use one master key while it spends several Anthropic API keys in a fixed order, moving to the next key only when one runs out of credits, with a health page and a runbook for running it next to the Butler on Unraid.",
8
+ "The Anthropic provider can now use a plain API key and a custom base URL (ouro auth --provider anthropic --api-key-file <path> --base-url <url>, which reads the key from a file or standard input and never from the command line), so an agent can run on API credits through the credit gateway; the existing setup-token login works as before.",
9
+ "The credit gateway refuses any request path that would send a key to a host other than the Anthropic API."
10
+ ]
11
+ },
12
+ {
13
+ "version": "0.1.0-alpha.955",
14
+ "changes": [
15
+ "iMessage no longer loses a reply that fails to send. When Slugger's reply times out or otherwise fails at BlueBubbles, your message stays pending; about ten minutes later a recovery pass checks the chat, and if the reply did not arrive after all, Slugger answers again. The message is marked handled only once a reply goes through. After three failed sends it is given up on with an error in the logs, so one stuck message cannot use up the day's recovery budget. If Slugger had already said something partway through that turn, the message is not retried, so you never see that part twice."
16
+ ]
17
+ },
4
18
  {
5
19
  "version": "0.1.0-alpha.954",
6
20
  "changes": [
@@ -0,0 +1,95 @@
1
+ # Credit gateway runbook
2
+
3
+ The credit gateway lets the Butler run on the API credits of several Claude Max plans through one key. Agents present a single master key; the gateway holds the real Anthropic API keys and spends them in a fixed order: it uses the first key until the API says that key's credit balance is too low, then the second, then the third. It never spreads load across keys.
4
+
5
+ It runs from the same image as the Butler (no separate image), as a second container named `ouro-credit-gateway`. Both containers use host networking, so the gateway listens on `127.0.0.1:8787` and the Butler reaches it at `http://127.0.0.1:8787`. Nothing is published to the network.
6
+
7
+ ## How it behaves
8
+
9
+ - `master.key` in the keys folder is the key clients must send, as `x-api-key` or `Authorization: Bearer`. A wrong or missing key gets HTTP 401 `invalid x-api-key`.
10
+ - Every other `*.key` file is an upstream Anthropic API key. The gateway uses them in file-name order (`acct1.key`, `acct2.key`, `acct3.key`). The label in logs and health is the file name without `.key`. Key contents are never logged.
11
+ - The gateway reads the keys once at start. Restart the container after changing a key file.
12
+ - When the API answers HTTP 400, 402 or 403 with "credit balance is too low", the gateway skips that key for the cooldown (60 minutes by default), logs `gateway: <label> out of credits; moving on`, and retries the same request with the next key. After the cooldown it tries the key again, which is how a key comes back once its org's credits refresh. A rejected credit check is not billed.
13
+ - The skip list is saved in `gateway-state.json` in the keys folder (mode 0600), so a restart remembers it. Delete that file to retry every key at once.
14
+ - When every key is out of credits, clients get HTTP 400 with the API's last credit-balance error.
15
+ - If the API cannot be reached, clients get HTTP 502 and the gateway does not move to the next key.
16
+ - Every request logs one line: `gateway: <label> <method> <path> <status> <ms>ms`. Read them with `docker logs ouro-credit-gateway`.
17
+
18
+ ## One-time setup (as root on the Unraid host)
19
+
20
+ 1. Create the keys folder, owned by the image's user (UID/GID 10001) and closed to everyone else:
21
+
22
+ ```sh
23
+ install -d -o 10001 -g 10001 -m 0700 /mnt/user/appdata/credit-gateway /mnt/user/appdata/credit-gateway/keys
24
+ ```
25
+
26
+ 2. Put one upstream API key per account in the folder, one key per file, each file mode 0600. Copy the key files in rather than pasting keys on the command line, so they stay out of shell history:
27
+
28
+ ```sh
29
+ cd /mnt/user/appdata/credit-gateway/keys
30
+ install -o 10001 -g 10001 -m 0600 /path/to/acct1.key acct1.key
31
+ install -o 10001 -g 10001 -m 0600 /path/to/acct2.key acct2.key
32
+ install -o 10001 -g 10001 -m 0600 /path/to/acct3.key acct3.key
33
+ ```
34
+
35
+ 3. Generate the master key:
36
+
37
+ ```sh
38
+ cd /mnt/user/appdata/credit-gateway/keys
39
+ (umask 077; printf 'ouro-gw-%s\n' "$(openssl rand -hex 32)" > master.key)
40
+ chown 10001:10001 master.key
41
+ ```
42
+
43
+ 4. Check the result. Every file should be `-rw------- 10001 10001`:
44
+
45
+ ```sh
46
+ ls -ln /mnt/user/appdata/credit-gateway/keys
47
+ ```
48
+
49
+ The folder must stay writable by UID 10001, because the gateway saves `gateway-state.json` there.
50
+
51
+ ## Run the gateway
52
+
53
+ Use the same image tag the Butler runs (the `<Repository>` in `sanctuary.xml`). The image's entrypoint starts the agent daemon, so override it with `--entrypoint node`. The image's built-in health check probes the agent, so replace it with a check of the gateway's own health URL:
54
+
55
+ ```sh
56
+ docker run -d --name ouro-credit-gateway \
57
+ --network host \
58
+ --restart unless-stopped \
59
+ --entrypoint node \
60
+ -v /mnt/user/appdata/credit-gateway/keys:/keys \
61
+ --health-cmd 'curl -fsS http://127.0.0.1:8787/health || exit 1' \
62
+ --health-interval 30s --health-timeout 5s --health-retries 3 \
63
+ ghcr.io/ourostack/ouroboros-butler:<same tag as ouro-butler> \
64
+ /opt/ouro/dist/heart/daemon/ouro-entry.js gateway serve --keys-dir /keys --host 127.0.0.1 --port 8787
65
+ ```
66
+
67
+ Port 8787 is also the default port of Readarr and its forks (such as Bookshelf). If `ss -ltn | grep 8787` shows it in use, pick a free port (for example 8788) and use it in `--port`, the health check and the Butler's `--base-url`.
68
+
69
+ Optional flags after `gateway serve`: `--cooldown-minutes <n>` (default 60), `--state <path>` (default `<keys-dir>/gateway-state.json`), `--upstream <url>` (default `https://api.anthropic.com`).
70
+
71
+ ## Check it
72
+
73
+ ```sh
74
+ docker inspect --format '{{.State.Status}} {{.State.Health.Status}}' ouro-credit-gateway
75
+ curl -fsS http://127.0.0.1:8787/health
76
+ docker logs --tail 20 ouro-credit-gateway
77
+ ```
78
+
79
+ `/health` needs no key and returns, for example, `{"ok":true,"keys":[{"label":"acct1","available":true},{"label":"acct2","available":false,"skipUntil":"2026-11-01T10:00:00.000Z"},{"label":"acct3","available":true}]}`.
80
+
81
+ ## Point the Butler at it
82
+
83
+ Store the master key and the gateway URL as the Butler's Anthropic credentials (API-key mode). The key is read from a file or standard input, never from the command line:
84
+
85
+ ```sh
86
+ docker exec -i -u 10001 ouro-butler node /opt/ouro/dist/heart/daemon/ouro-entry.js \
87
+ auth --agent sanctuary --provider anthropic --api-key-file /dev/stdin --base-url http://127.0.0.1:8787 \
88
+ < /mnt/user/appdata/credit-gateway/keys/master.key
89
+ ```
90
+
91
+ Then select an Anthropic model for the Butler's lanes with `ouro use`. In API-key mode the agent sends a plain API key, with no Claude Code identity headers, no Claude Code system preamble and no token refresh.
92
+
93
+ ## Change or rotate keys
94
+
95
+ Replace the file (keep owner 10001 and mode 0600), then `docker restart ouro-credit-gateway`. After rotating `master.key`, run the `auth` step above again so the Butler sends the new master key.
@@ -1,5 +1,5 @@
1
1
  {
2
- "runtimeVersion": "0.1.0-alpha.954",
2
+ "runtimeVersion": "0.1.0-alpha.956",
3
3
  "bundleSchemaVersion": 3,
4
4
  "lastUpdated": "2026-09-03T00:00:00.000Z"
5
5
  }
@@ -1,7 +1,7 @@
1
1
  <?xml version="1.0"?>
2
2
  <Container version="2">
3
3
  <Name>ouro-butler</Name>
4
- <Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.954</Repository>
4
+ <Repository>ghcr.io/ourostack/ouroboros-butler:0.1.0-alpha.956</Repository>
5
5
  <Registry>https://github.com/ourostack/ouroboros/pkgs/container/ouroboros-butler</Registry>
6
6
  <Network>host</Network>
7
7
  <Shell>sh</Shell>
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.loadGatewayKeys = loadGatewayKeys;
37
+ exports.loadGatewayState = loadGatewayState;
38
+ exports.saveGatewayState = saveGatewayState;
39
+ const fs = __importStar(require("node:fs"));
40
+ const path = __importStar(require("node:path"));
41
+ const runtime_1 = require("../nerves/runtime");
42
+ const MASTER_KEY_FILE = "master.key";
43
+ const KEY_SUFFIX = ".key";
44
+ function readTrimmed(filePath) {
45
+ try {
46
+ return fs.readFileSync(filePath, "utf8").trim();
47
+ }
48
+ catch {
49
+ return "";
50
+ }
51
+ }
52
+ /**
53
+ * Reads the master key and the upstream keys from a directory, once. Every `*.key` file other than
54
+ * `master.key` is an upstream key; the gateway drains them in file-name order.
55
+ */
56
+ function loadGatewayKeys(keysDir) {
57
+ let entries;
58
+ try {
59
+ entries = fs.readdirSync(keysDir);
60
+ }
61
+ catch (error) {
62
+ /* v8 ignore next -- defensive: fs always throws Error instances @preserve */
63
+ const reason = error instanceof Error ? error.message : String(error);
64
+ throw new Error(`gateway: cannot read keys directory ${keysDir}: ${reason}`);
65
+ }
66
+ const masterKey = readTrimmed(path.join(keysDir, MASTER_KEY_FILE));
67
+ if (!masterKey)
68
+ throw new Error(`gateway: ${path.join(keysDir, MASTER_KEY_FILE)} is missing or empty`);
69
+ const upstream = [];
70
+ for (const name of entries.filter((entry) => entry.endsWith(KEY_SUFFIX) && entry !== MASTER_KEY_FILE).sort()) {
71
+ const key = readTrimmed(path.join(keysDir, name));
72
+ if (!key)
73
+ throw new Error(`gateway: upstream key file ${path.join(keysDir, name)} is empty`);
74
+ upstream.push({ label: name.slice(0, -KEY_SUFFIX.length), key });
75
+ }
76
+ if (upstream.length === 0)
77
+ throw new Error(`gateway: no upstream *.key files in ${keysDir} (besides master.key)`);
78
+ (0, runtime_1.emitNervesEvent)({
79
+ component: "gateway",
80
+ event: "gateway.keys_loaded",
81
+ message: "credit gateway keys loaded",
82
+ meta: { keysDir, labels: upstream.map((entry) => entry.label) },
83
+ });
84
+ return { masterKey, upstream };
85
+ }
86
+ /** Loads the skip list. A missing or corrupt file starts empty; entries that are not valid times are dropped. */
87
+ function loadGatewayState(statePath) {
88
+ const exhausted = {};
89
+ let parsed;
90
+ try {
91
+ parsed = JSON.parse(fs.readFileSync(statePath, "utf8"));
92
+ }
93
+ catch {
94
+ return { exhausted };
95
+ }
96
+ const raw = parsed && typeof parsed === "object" ? parsed.exhausted : undefined;
97
+ if (raw && typeof raw === "object" && !Array.isArray(raw)) {
98
+ for (const [label, until] of Object.entries(raw)) {
99
+ if (typeof until === "string" && Number.isFinite(Date.parse(until)))
100
+ exhausted[label] = until;
101
+ }
102
+ }
103
+ return { exhausted };
104
+ }
105
+ /** Writes the skip list with mode 0600, through a temporary file so a crash never leaves half a file. */
106
+ function saveGatewayState(statePath, state) {
107
+ const tmpPath = `${statePath}.${process.pid}.tmp`;
108
+ fs.writeFileSync(tmpPath, `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
109
+ fs.chmodSync(tmpPath, 0o600);
110
+ fs.renameSync(tmpPath, statePath);
111
+ (0, runtime_1.emitNervesEvent)({
112
+ component: "gateway",
113
+ event: "gateway.state_saved",
114
+ message: "credit gateway state saved",
115
+ meta: { statePath, exhausted: Object.keys(state.exhausted) },
116
+ });
117
+ }
@@ -0,0 +1,429 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.GATEWAY_MAX_REQUEST_BYTES = exports.GATEWAY_DEFAULT_COOLDOWN_MS = exports.GATEWAY_DEFAULT_UPSTREAM = exports.GATEWAY_DEFAULT_PORT = exports.GATEWAY_DEFAULT_HOST = void 0;
37
+ exports.isCreditExhaustedBody = isCreditExhaustedBody;
38
+ exports.creditGatewayOptionsFromCli = creditGatewayOptionsFromCli;
39
+ exports.startCreditGateway = startCreditGateway;
40
+ exports.waitForShutdownSignal = waitForShutdownSignal;
41
+ exports.runCreditGatewayCommand = runCreditGatewayCommand;
42
+ const http = __importStar(require("node:http"));
43
+ const https = __importStar(require("node:https"));
44
+ const path = __importStar(require("node:path"));
45
+ const zlib = __importStar(require("node:zlib"));
46
+ const node_crypto_1 = require("node:crypto");
47
+ const node_stream_1 = require("node:stream");
48
+ const runtime_1 = require("../nerves/runtime");
49
+ const keys_1 = require("./keys");
50
+ exports.GATEWAY_DEFAULT_HOST = "127.0.0.1";
51
+ exports.GATEWAY_DEFAULT_PORT = 8787;
52
+ exports.GATEWAY_DEFAULT_UPSTREAM = "https://api.anthropic.com";
53
+ exports.GATEWAY_DEFAULT_COOLDOWN_MS = 60 * 60 * 1000;
54
+ exports.GATEWAY_MAX_REQUEST_BYTES = 32 * 1024 * 1024;
55
+ const CREDIT_LOW_PHRASE = "credit balance is too low";
56
+ const CREDIT_CHECK_STATUSES = new Set([400, 402, 403]);
57
+ const HOP_BY_HOP_HEADERS = new Set([
58
+ "connection",
59
+ "keep-alive",
60
+ "proxy-authenticate",
61
+ "proxy-authorization",
62
+ "proxy-connection",
63
+ "te",
64
+ "trailer",
65
+ "transfer-encoding",
66
+ "upgrade",
67
+ ]);
68
+ class RequestTooLargeError extends Error {
69
+ }
70
+ function anthropicError(type, message) {
71
+ return JSON.stringify({ type: "error", error: { type, message } });
72
+ }
73
+ /* v8 ignore start -- defensive: Node and the HTTP stack always throw Error instances @preserve */
74
+ function errorMessage(error) {
75
+ return error instanceof Error ? error.message : String(error);
76
+ }
77
+ /* v8 ignore stop */
78
+ function digest(value) {
79
+ return (0, node_crypto_1.createHash)("sha256").update(value).digest();
80
+ }
81
+ /** Compares digests so the comparison takes the same time whatever the presented key's length. */
82
+ function matchesMasterKey(presented, masterDigest) {
83
+ if (!presented)
84
+ return false;
85
+ return (0, node_crypto_1.timingSafeEqual)(digest(presented), masterDigest);
86
+ }
87
+ function presentedKey(req) {
88
+ const apiKey = req.headers["x-api-key"];
89
+ if (typeof apiKey === "string" && apiKey.trim())
90
+ return apiKey.trim();
91
+ const bearer = /^Bearer\s+(.+)$/i.exec(req.headers.authorization ?? "");
92
+ return bearer ? bearer[1].trim() : undefined;
93
+ }
94
+ function readRequestBody(req, maxBytes) {
95
+ return new Promise((resolve, reject) => {
96
+ const chunks = [];
97
+ let size = 0;
98
+ req.on("data", (chunk) => {
99
+ size += chunk.length;
100
+ if (size > maxBytes) {
101
+ reject(new RequestTooLargeError());
102
+ req.pause();
103
+ return;
104
+ }
105
+ chunks.push(chunk);
106
+ });
107
+ req.on("end", () => resolve(Buffer.concat(chunks)));
108
+ req.on("error", reject);
109
+ });
110
+ }
111
+ function readAll(stream) {
112
+ return new Promise((resolve, reject) => {
113
+ const chunks = [];
114
+ stream.on("data", (chunk) => chunks.push(chunk));
115
+ stream.on("end", () => resolve(Buffer.concat(chunks)));
116
+ stream.on("error", reject);
117
+ });
118
+ }
119
+ function forwardedRequestHeaders(req, upstreamKey, bodyLength) {
120
+ const headers = {};
121
+ for (const [name, value] of Object.entries(req.headers)) {
122
+ if (HOP_BY_HOP_HEADERS.has(name) || name === "host" || name === "content-length" || name === "x-api-key" || name === "authorization")
123
+ continue;
124
+ headers[name] = value;
125
+ }
126
+ headers["x-api-key"] = upstreamKey;
127
+ if (bodyLength > 0)
128
+ headers["content-length"] = String(bodyLength);
129
+ return headers;
130
+ }
131
+ function responseHeaders(headers) {
132
+ const result = {};
133
+ for (const [name, value] of Object.entries(headers)) {
134
+ if (HOP_BY_HOP_HEADERS.has(name))
135
+ continue;
136
+ result[name] = value;
137
+ }
138
+ return result;
139
+ }
140
+ /** Decodes a compressed body for inspection only; the client still gets the original bytes. */
141
+ function decodeForInspection(body, encoding) {
142
+ try {
143
+ const normalized = (encoding ?? "").trim().toLowerCase();
144
+ if (normalized === "gzip" || normalized === "x-gzip")
145
+ return zlib.gunzipSync(body).toString("utf8");
146
+ if (normalized === "deflate")
147
+ return zlib.inflateSync(body).toString("utf8");
148
+ if (normalized === "br")
149
+ return zlib.brotliDecompressSync(body).toString("utf8");
150
+ return body.toString("utf8");
151
+ }
152
+ catch {
153
+ return body.toString("utf8");
154
+ }
155
+ }
156
+ function isCreditExhaustedBody(body, encoding) {
157
+ return decodeForInspection(body, encoding).toLowerCase().includes(CREDIT_LOW_PHRASE);
158
+ }
159
+ function upstreamUrl(base, requestUrl) {
160
+ const prefix = base.pathname.replace(/\/+$/, "");
161
+ return new URL(`${prefix}${requestUrl}`, base.origin);
162
+ }
163
+ function sendUpstream(target, method, headers, body) {
164
+ return new Promise((resolve, reject) => {
165
+ const transport = target.protocol === "https:" ? https : http;
166
+ const request = transport.request(target, { method, headers }, (response) => resolve({ response, request }));
167
+ request.on("error", reject);
168
+ request.end(body.length > 0 ? body : undefined);
169
+ });
170
+ }
171
+ function writeBuffered(res, buffered) {
172
+ res.writeHead(buffered.status, { ...buffered.headers, "content-length": String(buffered.body.length) });
173
+ res.end(buffered.body);
174
+ }
175
+ function writeJsonError(res, status, type, message) {
176
+ const body = Buffer.from(anthropicError(type, message));
177
+ writeBuffered(res, { status, headers: { "content-type": "application/json" }, body });
178
+ }
179
+ /** Maps `ouro gateway serve` flags to gateway options. */
180
+ function creditGatewayOptionsFromCli(command) {
181
+ return {
182
+ keysDir: command.keysDir,
183
+ ...(command.statePath ? { statePath: command.statePath } : {}),
184
+ ...(command.host ? { host: command.host } : {}),
185
+ ...(command.port !== undefined ? { port: command.port } : {}),
186
+ ...(command.upstream ? { upstream: command.upstream } : {}),
187
+ ...(command.cooldownMinutes !== undefined ? { cooldownMs: command.cooldownMinutes * 60 * 1000 } : {}),
188
+ };
189
+ }
190
+ /* v8 ignore start -- the default log writes to the real stderr; tests inject log @preserve */
191
+ function defaultLog(line) {
192
+ process.stderr.write(`${line}\n`);
193
+ }
194
+ /* v8 ignore stop */
195
+ /**
196
+ * Starts the credit gateway: a byte-passthrough proxy to the Anthropic API that accepts one master key
197
+ * and spends the upstream keys in a fixed order, moving to the next key only when the current one
198
+ * reports that its credit balance is too low.
199
+ */
200
+ async function startCreditGateway(options) {
201
+ const keys = (0, keys_1.loadGatewayKeys)(options.keysDir);
202
+ const statePath = options.statePath ?? path.join(options.keysDir, "gateway-state.json");
203
+ const state = (0, keys_1.loadGatewayState)(statePath);
204
+ const host = options.host ?? exports.GATEWAY_DEFAULT_HOST;
205
+ const upstreamBase = new URL(options.upstream ?? exports.GATEWAY_DEFAULT_UPSTREAM);
206
+ const cooldownMs = options.cooldownMs ?? exports.GATEWAY_DEFAULT_COOLDOWN_MS;
207
+ const maxRequestBytes = options.maxRequestBytes ?? exports.GATEWAY_MAX_REQUEST_BYTES;
208
+ const now = options.now ?? Date.now;
209
+ const log = options.log ?? defaultLog;
210
+ const masterDigest = digest(keys.masterKey);
211
+ function persist() {
212
+ try {
213
+ (0, keys_1.saveGatewayState)(statePath, state);
214
+ }
215
+ catch (error) {
216
+ (0, runtime_1.emitNervesEvent)({
217
+ level: "warn",
218
+ component: "gateway",
219
+ event: "gateway.state_save_error",
220
+ message: "credit gateway could not save its state",
221
+ meta: { statePath, error: errorMessage(error) },
222
+ });
223
+ }
224
+ }
225
+ /** Returns the skip-until time for a label, or undefined when the key is usable. Expired entries are dropped. */
226
+ function skipUntil(label) {
227
+ const until = state.exhausted[label];
228
+ if (!until)
229
+ return undefined;
230
+ if (Date.parse(until) > now())
231
+ return until;
232
+ delete state.exhausted[label];
233
+ persist();
234
+ return undefined;
235
+ }
236
+ function markExhausted(entry) {
237
+ state.exhausted[entry.label] = new Date(now() + cooldownMs).toISOString();
238
+ persist();
239
+ log(`gateway: ${entry.label} out of credits; moving on`);
240
+ (0, runtime_1.emitNervesEvent)({
241
+ level: "warn",
242
+ component: "gateway",
243
+ event: "gateway.key_exhausted",
244
+ message: "credit gateway key is out of credits",
245
+ meta: { label: entry.label, skipUntil: state.exhausted[entry.label] },
246
+ });
247
+ }
248
+ function logRequest(label, method, pathname, status, startedAt) {
249
+ const ms = now() - startedAt;
250
+ log(`gateway: ${label} ${method} ${pathname} ${status} ${ms}ms`);
251
+ (0, runtime_1.emitNervesEvent)({
252
+ component: "gateway",
253
+ event: "gateway.request",
254
+ message: "credit gateway request",
255
+ meta: { label, method, path: pathname, status, ms },
256
+ });
257
+ }
258
+ async function handle(req, res) {
259
+ const startedAt = now();
260
+ /* v8 ignore start -- Node always supplies url and method for accepted HTTP requests @preserve */
261
+ const requestUrl = req.url ?? "/";
262
+ const method = req.method ?? "GET";
263
+ /* v8 ignore stop */
264
+ const pathname = new URL(requestUrl, "http://gateway.local").pathname;
265
+ if (method === "GET" && pathname === "/health") {
266
+ const body = JSON.stringify({
267
+ ok: true,
268
+ keys: keys.upstream.map((entry) => {
269
+ const until = skipUntil(entry.label);
270
+ return until ? { label: entry.label, available: false, skipUntil: until } : { label: entry.label, available: true };
271
+ }),
272
+ });
273
+ writeBuffered(res, { status: 200, headers: { "content-type": "application/json" }, body: Buffer.from(body) });
274
+ logRequest("-", method, pathname, 200, startedAt);
275
+ return;
276
+ }
277
+ if (!matchesMasterKey(presentedKey(req), masterDigest)) {
278
+ writeJsonError(res, 401, "authentication_error", "invalid x-api-key");
279
+ logRequest("-", method, pathname, 401, startedAt);
280
+ return;
281
+ }
282
+ const target = upstreamUrl(upstreamBase, requestUrl);
283
+ if (target.origin !== upstreamBase.origin) {
284
+ writeJsonError(res, 400, "invalid_request_error", "invalid request path");
285
+ logRequest("-", method, pathname, 400, startedAt);
286
+ return;
287
+ }
288
+ let body;
289
+ try {
290
+ body = await readRequestBody(req, maxRequestBytes);
291
+ }
292
+ catch (error) {
293
+ /* v8 ignore start -- the client went away while sending its body; there is no one to answer @preserve */
294
+ if (!(error instanceof RequestTooLargeError)) {
295
+ res.destroy();
296
+ return;
297
+ }
298
+ /* v8 ignore stop */
299
+ res.setHeader("connection", "close");
300
+ writeJsonError(res, 413, "request_too_large", `request body exceeds ${maxRequestBytes} bytes`);
301
+ logRequest("-", method, pathname, 413, startedAt);
302
+ return;
303
+ }
304
+ let lastCredit;
305
+ let lastCreditLabel = "-";
306
+ for (const entry of keys.upstream) {
307
+ if (skipUntil(entry.label))
308
+ continue;
309
+ let upstream;
310
+ try {
311
+ upstream = await sendUpstream(target, method, forwardedRequestHeaders(req, entry.key, body.length), body);
312
+ }
313
+ catch (error) {
314
+ (0, runtime_1.emitNervesEvent)({
315
+ level: "error",
316
+ component: "gateway",
317
+ event: "gateway.upstream_error",
318
+ message: "credit gateway could not reach the upstream API",
319
+ meta: { label: entry.label, error: errorMessage(error) },
320
+ });
321
+ writeJsonError(res, 502, "api_error", "credit gateway could not reach the upstream API");
322
+ logRequest(entry.label, method, pathname, 502, startedAt);
323
+ return;
324
+ }
325
+ /* v8 ignore next -- Node always sets statusCode on an upstream response @preserve */
326
+ const status = upstream.response.statusCode ?? 502;
327
+ if (CREDIT_CHECK_STATUSES.has(status)) {
328
+ const buffered = {
329
+ status,
330
+ headers: responseHeaders(upstream.response.headers),
331
+ body: await readAll(upstream.response),
332
+ };
333
+ if (isCreditExhaustedBody(buffered.body, upstream.response.headers["content-encoding"])) {
334
+ markExhausted(entry);
335
+ lastCredit = buffered;
336
+ lastCreditLabel = entry.label;
337
+ continue;
338
+ }
339
+ writeBuffered(res, buffered);
340
+ logRequest(entry.label, method, pathname, status, startedAt);
341
+ return;
342
+ }
343
+ res.writeHead(status, responseHeaders(upstream.response.headers));
344
+ res.flushHeaders();
345
+ (0, node_stream_1.pipeline)(upstream.response, res, (error) => {
346
+ if (error)
347
+ upstream.request.destroy();
348
+ logRequest(entry.label, method, pathname, status, startedAt);
349
+ });
350
+ return;
351
+ }
352
+ if (lastCredit) {
353
+ writeBuffered(res, { ...lastCredit, status: 400 });
354
+ }
355
+ else {
356
+ writeJsonError(res, 400, "invalid_request_error", "Your credit balance is too low on every gateway key");
357
+ }
358
+ logRequest(lastCreditLabel, method, pathname, 400, startedAt);
359
+ }
360
+ const server = http.createServer((req, res) => {
361
+ handle(req, res).catch((error) => {
362
+ (0, runtime_1.emitNervesEvent)({
363
+ level: "error",
364
+ component: "gateway",
365
+ event: "gateway.request_error",
366
+ message: "credit gateway request failed",
367
+ meta: { error: errorMessage(error) },
368
+ });
369
+ /* v8 ignore next 4 -- failures after the headers went out are reported by the stream pipeline instead @preserve */
370
+ if (res.headersSent) {
371
+ res.destroy();
372
+ return;
373
+ }
374
+ writeJsonError(res, 502, "api_error", "credit gateway request failed");
375
+ });
376
+ });
377
+ await new Promise((resolve, reject) => {
378
+ server.once("error", reject);
379
+ /* v8 ignore next -- tests always bind port 0; the default is for production @preserve */
380
+ const port = options.port ?? exports.GATEWAY_DEFAULT_PORT;
381
+ server.listen(port, host, () => {
382
+ server.off("error", reject);
383
+ resolve();
384
+ });
385
+ });
386
+ const address = server.address();
387
+ const url = `http://${host}:${address.port}`;
388
+ (0, runtime_1.emitNervesEvent)({
389
+ component: "gateway",
390
+ event: "gateway.server_started",
391
+ message: "credit gateway started",
392
+ meta: { url, upstream: upstreamBase.origin, labels: keys.upstream.map((entry) => entry.label), statePath },
393
+ });
394
+ return {
395
+ server,
396
+ url,
397
+ port: address.port,
398
+ close: () => new Promise((resolve) => {
399
+ server.closeAllConnections();
400
+ server.close(() => {
401
+ (0, runtime_1.emitNervesEvent)({
402
+ component: "gateway",
403
+ event: "gateway.server_stopped",
404
+ message: "credit gateway stopped",
405
+ meta: { url },
406
+ });
407
+ resolve();
408
+ });
409
+ }),
410
+ };
411
+ }
412
+ /* v8 ignore start -- waits for real process signals; tests inject waitForStop @preserve */
413
+ function waitForShutdownSignal() {
414
+ return new Promise((resolve) => {
415
+ process.once("SIGINT", () => resolve());
416
+ process.once("SIGTERM", () => resolve());
417
+ });
418
+ }
419
+ /* v8 ignore stop */
420
+ /** Runs `ouro gateway serve` in the foreground until `waitForStop` resolves. */
421
+ async function runCreditGatewayCommand(command, deps) {
422
+ const handle = await startCreditGateway(creditGatewayOptionsFromCli(command));
423
+ const message = `credit gateway listening on ${handle.url}\nhealth: ${handle.url}/health`;
424
+ deps.writeStdout(message);
425
+ /* v8 ignore next -- production waits for SIGINT/SIGTERM; tests inject waitForStop @preserve */
426
+ await (deps.waitForStop ?? waitForShutdownSignal)();
427
+ await handle.close();
428
+ return message;
429
+ }