@openhoo/hoopilot 2.1.8 → 2.1.10
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/README.md +17 -3
- package/dist/{chunk-2GLKVNAA.js → chunk-2GIR4W4A.js} +39 -3
- package/dist/chunk-2GIR4W4A.js.map +1 -0
- package/dist/cli.js +294 -108
- package/dist/cli.js.map +1 -1
- package/dist/codexx.js +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +311 -109
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-2GLKVNAA.js.map +0 -1
package/README.md
CHANGED
|
@@ -369,7 +369,7 @@ docker run --rm -v hoopilot-data:/data ghcr.io/openhoo/hoopilot login --print-ke
|
|
|
369
369
|
|
|
370
370
|
## Logging
|
|
371
371
|
|
|
372
|
-
Hoopilot uses Pino for structured logs. Server startup, request
|
|
372
|
+
Hoopilot uses Pino for structured logs. Server startup, request errors, upstream Copilot failures, model-list fallback, auth failures, and update-check diagnostics are logged with stable event names and request IDs. Successful request completion logs are disabled by default; pass `--access-log` or set `HOOPILOT_ACCESS_LOG=1` to enable them.
|
|
373
373
|
|
|
374
374
|
Logs never include request bodies, prompt text, completions, stream chunks, OAuth tokens, API keys, authorization headers, cookies, or auth-file contents.
|
|
375
375
|
|
|
@@ -379,7 +379,7 @@ Console logs default to pretty output at `info` level:
|
|
|
379
379
|
hoopilot --log-level info --log-format pretty
|
|
380
380
|
```
|
|
381
381
|
|
|
382
|
-
|
|
382
|
+
When access logs are enabled, pretty logs keep common request and diagnostic fields inline for terminal use:
|
|
383
383
|
|
|
384
384
|
```text
|
|
385
385
|
INFO [16:40:14]: request completed component=server event=http.request.completed method=POST path=/v1/chat/completions status=200 duration=42.37ms stream=true requestId=req-test
|
|
@@ -401,7 +401,9 @@ Hoopilot tracks token usage, request counts, and latency in memory while the ser
|
|
|
401
401
|
- `GET /v1/usage` returns JSON combining the proxy metrics snapshot with live Copilot quota fetched from GitHub and cached for 60 seconds. If quota cannot be read, `copilot` is `null` and `copilot_error` explains why. The snapshot's `proxy.githubRateLimit` field reports the most recent GitHub REST rate-limit budget per resource (`limit`, `remaining`, `used`, `resetAt`, `retryAfterSeconds`, `observedAt`).
|
|
402
402
|
- `hoopilot usage` prints your Copilot plan and quota — and, when GitHub returns them, your GitHub API rate-limit budget — from the command line.
|
|
403
403
|
|
|
404
|
-
Token usage is read from the upstream `usage` object. For streaming chat completions, usage is only available when the client sends `stream_options: {"include_usage": true}`; Hoopilot does not inject that flag.
|
|
404
|
+
Token usage is read from the upstream `usage` object when Hoopilot is already parsing a compatibility response, or when full token accounting is enabled. For streaming chat completions, usage is only available when the client sends `stream_options: {"include_usage": true}`; Hoopilot does not inject that flag.
|
|
405
|
+
|
|
406
|
+
The default `HOOPILOT_USAGE_ACCOUNTING=basic` mode skips token extraction from pass-through response bodies and SSE streams while still recording usage from compatibility responses Hoopilot already has to parse. Set `HOOPILOT_USAGE_ACCOUNTING=full` when exact pass-through stream/body token accounting matters more than CPU use. Set `HOOPILOT_USAGE_ACCOUNTING=off` to skip token accounting entirely. Request counts, upstream counts, latency, in-flight metrics, `/metrics`, and `/v1/usage` still work in every mode. The `hoopilot_token_extraction_total{outcome="extracted"|"missing"}` counter (mirrored in `/v1/usage` as `proxy.tokens.extraction`) tracks how often a completion reported usage versus not.
|
|
405
407
|
|
|
406
408
|
GitHub API usage is read from the `x-ratelimit-*` response headers that `api.github.com` returns on the `copilot_internal/user` quota call Hoopilot already makes, so it costs no extra request. (The Copilot completion host `api.githubcopilot.com` does not currently emit these headers, so per-completion rate-limit data is not yet available there.)
|
|
407
409
|
|
|
@@ -452,6 +454,7 @@ Server and local-client settings:
|
|
|
452
454
|
| `HOOPILOT_ALLOWED_ORIGINS` | Comma-separated browser origins allowed to make cross-origin requests. Loopback origins are always allowed; every other origin is blocked. |
|
|
453
455
|
| `HOOPILOT_ALLOW_UNAUTHENTICATED` / `--allow-unauthenticated` | Allow non-loopback binds without a local API key. |
|
|
454
456
|
| `HOOPILOT_STREAM_MODE` / `--stream-mode` | `auto`, `live`, or `buffer`. `auto` buffers streams for Windows standalone binaries. `HOOPILOT_STREAMING_PROXY_MODE` is accepted as an alias. |
|
|
457
|
+
| `HOOPILOT_USAGE_ACCOUNTING` / `--usage-accounting` | `basic`, `full`, or `off`. Default: `basic`. `basic` avoids token extraction from pass-through bodies/streams; `full` tracks all reported usage; `off` skips token accounting. |
|
|
455
458
|
|
|
456
459
|
Copilot and GitHub settings:
|
|
457
460
|
|
|
@@ -470,10 +473,21 @@ Logging and update settings:
|
|
|
470
473
|
| --- | --- |
|
|
471
474
|
| `HOOPILOT_LOG_LEVEL` / `--log-level` | `trace`, `debug`, `info`, `warn`, `error`, `fatal`, or `silent`. Default: `info`. |
|
|
472
475
|
| `HOOPILOT_LOG_FORMAT` / `--log-format` | `pretty` or `json`. Default: `pretty`. |
|
|
476
|
+
| `HOOPILOT_ACCESS_LOG` / `--access-log` / `--no-access-log` | Successful request logs are disabled by default. Set `1`/`true` or pass `--access-log` to enable them. Client and server errors are still logged. |
|
|
473
477
|
| `HOOPILOT_UPSTREAM_TIMEOUT_MS` | Time to wait for Copilot response headers before returning `504 copilot_timeout`. Default: `120000`; set `0` to disable. |
|
|
474
478
|
| `HOOPILOT_UPSTREAM_STREAM_IDLE_TIMEOUT_MS` | Time to wait for bytes on a Copilot response body before failing the stream so clients can retry. Default: `120000`; set `0` to disable. |
|
|
475
479
|
| `HOOPILOT_NO_UPDATE_CHECK` / `--no-update-check` | Disable background update checks. `NO_UPDATE_NOTIFIER` is also honored. |
|
|
476
480
|
|
|
481
|
+
The default service profile is low-resource:
|
|
482
|
+
|
|
483
|
+
```sh
|
|
484
|
+
hoopilot
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
For the quietest local service, also use `HOOPILOT_LOG_LEVEL=warn HOOPILOT_NO_UPDATE_CHECK=1`.
|
|
488
|
+
|
|
489
|
+
If you prefer fewer stream timers and can tolerate clients handling stalled upstream streams themselves, also set `HOOPILOT_UPSTREAM_STREAM_IDLE_TIMEOUT_MS=0`.
|
|
490
|
+
|
|
477
491
|
`codexx` settings:
|
|
478
492
|
|
|
479
493
|
| Setting | Description |
|
|
@@ -6,6 +6,7 @@ import { constants as osConstants } from "os";
|
|
|
6
6
|
var DEFAULT_MODEL = "gpt-5.5";
|
|
7
7
|
|
|
8
8
|
// src/util.ts
|
|
9
|
+
import { isIP } from "net";
|
|
9
10
|
function trimTrailingSlash(value) {
|
|
10
11
|
return value.replace(/\/+$/, "");
|
|
11
12
|
}
|
|
@@ -42,9 +43,16 @@ function parseUrl(rawUrl) {
|
|
|
42
43
|
}
|
|
43
44
|
return url;
|
|
44
45
|
}
|
|
45
|
-
var LOOPBACK_HOSTNAMES = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
|
46
46
|
function isLoopbackHostname(host) {
|
|
47
|
-
|
|
47
|
+
const normalized = host.trim().toLowerCase();
|
|
48
|
+
const address = normalized.startsWith("[") && normalized.endsWith("]") ? normalized.slice(1, -1) : normalized;
|
|
49
|
+
if (address === "localhost") {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
if (isIP(address) === 4) {
|
|
53
|
+
return address.startsWith("127.");
|
|
54
|
+
}
|
|
55
|
+
return isIP(address) === 6 && (address === "::1" || address === "0:0:0:0:0:0:0:1");
|
|
48
56
|
}
|
|
49
57
|
function isLoopbackHttpUrl(url) {
|
|
50
58
|
return url.protocol === "http:" && isLoopbackHostname(url.hostname);
|
|
@@ -107,12 +115,38 @@ var STREAMING_PROXY_MODES = [
|
|
|
107
115
|
"buffer",
|
|
108
116
|
"live"
|
|
109
117
|
];
|
|
118
|
+
var USAGE_ACCOUNTING_MODES = [
|
|
119
|
+
"basic",
|
|
120
|
+
"full",
|
|
121
|
+
"off"
|
|
122
|
+
];
|
|
110
123
|
function parseStreamingProxyMode(value) {
|
|
111
124
|
if (STREAMING_PROXY_MODES.includes(value)) {
|
|
112
125
|
return value;
|
|
113
126
|
}
|
|
114
127
|
throw new Error(`Invalid stream mode: ${value}. Expected ${STREAMING_PROXY_MODES.join(", ")}.`);
|
|
115
128
|
}
|
|
129
|
+
function parseUsageAccountingMode(value) {
|
|
130
|
+
if (USAGE_ACCOUNTING_MODES.includes(value)) {
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
throw new Error(
|
|
134
|
+
`Invalid usage accounting mode: ${value}. Expected ${USAGE_ACCOUNTING_MODES.join(", ")}.`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
function parseBooleanEnv(value, name) {
|
|
138
|
+
const raw = envValue(value)?.toLowerCase();
|
|
139
|
+
if (raw === void 0) {
|
|
140
|
+
return void 0;
|
|
141
|
+
}
|
|
142
|
+
if (raw === "1" || raw === "true" || raw === "yes" || raw === "on") {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
if (raw === "0" || raw === "false" || raw === "no" || raw === "off") {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
throw new Error(`${name} must be one of: 1, 0, true, false, yes, no, on, off.`);
|
|
149
|
+
}
|
|
116
150
|
|
|
117
151
|
// src/codexx.ts
|
|
118
152
|
var DEFAULT_BASE_URL = "http://127.0.0.1:4141/v1";
|
|
@@ -313,9 +347,11 @@ export {
|
|
|
313
347
|
parseJsonObject,
|
|
314
348
|
modelIdsFromResponse,
|
|
315
349
|
parseStreamingProxyMode,
|
|
350
|
+
parseUsageAccountingMode,
|
|
351
|
+
parseBooleanEnv,
|
|
316
352
|
DEFAULT_MODEL,
|
|
317
353
|
buildCodexxInvocation,
|
|
318
354
|
main,
|
|
319
355
|
verifyCodexxModel
|
|
320
356
|
};
|
|
321
|
-
//# sourceMappingURL=chunk-
|
|
357
|
+
//# sourceMappingURL=chunk-2GIR4W4A.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/codexx.ts","../src/defaults.ts","../src/util.ts"],"sourcesContent":["#!/usr/bin/env bun\n\nimport { spawn } from \"node:child_process\";\nimport { constants as osConstants } from \"node:os\";\nimport { DEFAULT_MODEL } from \"./defaults\";\nimport type { FetchLike } from \"./types\";\nimport {\n envValue,\n errorMessage,\n modelIdsFromResponse,\n trimTrailingSlash,\n truncatedResponseText,\n} from \"./util\";\n\nconst DEFAULT_BASE_URL = \"http://127.0.0.1:4141/v1\";\nconst DEFAULT_CODEX_BIN = \"codex\";\nconst DEFAULT_REASONING_EFFORT = \"xhigh\";\nconst DEFAULT_STREAM_IDLE_TIMEOUT_MS = 120_000;\nconst PROXY_ENV_KEYS = [\n \"ALL_PROXY\",\n \"HTTPS_PROXY\",\n \"HTTP_PROXY\",\n \"NO_PROXY\",\n \"all_proxy\",\n \"https_proxy\",\n \"http_proxy\",\n \"no_proxy\",\n];\n\nexport interface CodexxInvocation {\n args: string[];\n baseUrl: string;\n command: string;\n env: NodeJS.ProcessEnv;\n model: string;\n}\n\nexport function buildCodexxInvocation(\n argv: string[],\n env: NodeJS.ProcessEnv = process.env,\n): CodexxInvocation {\n const baseUrl = envValue(env.CODEXX_BASE_URL) ?? DEFAULT_BASE_URL;\n // Never fall back to a public, predictable key: a shared constant like the old\n // \"local-key\" default is also a credential a malicious local/browser client\n // could guess. When no key is configured the local server is expected to run\n // unauthenticated, which accepts any value, so a random throwaway key is safe.\n const apiKey =\n envValue(env.CODEXX_API_KEY) ?? envValue(env.HOOPILOT_API_KEY) ?? generateEphemeralApiKey();\n const command = envValue(env.CODEXX_CODEX_BIN) ?? DEFAULT_CODEX_BIN;\n const model = envValue(env.CODEXX_MODEL) ?? DEFAULT_MODEL;\n const reasoningEffort = envValue(env.CODEXX_MODEL_REASONING_EFFORT) ?? DEFAULT_REASONING_EFFORT;\n const streamIdleTimeoutMs = parseStreamIdleTimeoutMs(env.CODEXX_STREAM_IDLE_TIMEOUT_MS);\n const providerConfigParts = [\n '{ name = \"Hoopilot\"',\n `base_url = ${JSON.stringify(baseUrl)}`,\n 'env_key = \"OPENAI_API_KEY\"',\n 'wire_api = \"responses\"',\n \"supports_websockets = false\",\n ];\n if (streamIdleTimeoutMs > 0) {\n providerConfigParts.push(`stream_idle_timeout_ms = ${streamIdleTimeoutMs}`);\n }\n const providerConfig = `${providerConfigParts.join(\", \")} }`;\n\n return {\n args: [\n // Codex ships a managed network proxy (codex-rs/network-proxy) that routes the\n // agent's traffic through a local proxy on :3128 and enforces a domain allowlist.\n // A host that is not allowlisted — like the local Hoopilot server — gets an instant\n // 403 (the Squid error page) and never reaches Hoopilot. It has two independent\n // gates: the `network_proxy` feature flag and the `permissions.workspace.network`\n // config. Disabling only the feature (`--disable network_proxy`) does not reliably\n // turn it off when the proxy is enabled through the permissions config, so set both\n // off: the feature flag via `--disable`, and the proxy itself via the config key\n // (when `enabled` is false the proxy no-ops and binds no listeners).\n \"--disable\",\n \"network_proxy\",\n \"-c\",\n \"permissions.workspace.network.enabled=false\",\n \"-c\",\n 'model_provider=\"hoopilot\"',\n \"-c\",\n `model_providers.hoopilot=${providerConfig}`,\n \"-m\",\n model,\n \"-c\",\n `model_reasoning_effort=${JSON.stringify(reasoningEffort)}`,\n ...argv,\n ],\n baseUrl,\n command,\n env: withoutProxyEnv({\n ...env,\n OPENAI_API_KEY: apiKey,\n }),\n model,\n };\n}\n\n// A random, non-guessable placeholder key for when neither CODEXX_API_KEY nor\n// HOOPILOT_API_KEY is set. An unauthenticated local Hoopilot accepts any value;\n// a keyed server rejects it with a 401, which the model preflight surfaces.\nfunction generateEphemeralApiKey(): string {\n return `codexx-${crypto.randomUUID()}`;\n}\n\nfunction parseStreamIdleTimeoutMs(rawValue: string | undefined): number {\n const raw = envValue(rawValue);\n if (raw === undefined) {\n return DEFAULT_STREAM_IDLE_TIMEOUT_MS;\n }\n const value = Number(raw);\n if (!Number.isInteger(value) || value < 0) {\n throw new Error(\"CODEXX_STREAM_IDLE_TIMEOUT_MS must be a non-negative integer.\");\n }\n return value;\n}\n\nfunction withoutProxyEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {\n const next = { ...env };\n for (const key of PROXY_ENV_KEYS) {\n delete next[key];\n }\n return next;\n}\n\nexport async function main(argv = Bun.argv.slice(2), env = process.env): Promise<void> {\n if (argv.length === 1 && (argv[0] === \"--help\" || argv[0] === \"-h\")) {\n console.log(helpText());\n return;\n }\n\n const invocation = buildCodexxInvocation(argv, env);\n if (env.CODEXX_SKIP_MODEL_PREFLIGHT !== \"1\") {\n await verifyCodexxModel(invocation);\n }\n const child = spawn(invocation.command, invocation.args, {\n env: invocation.env,\n shell: process.platform === \"win32\",\n stdio: \"inherit\",\n });\n\n const exitCode = await new Promise<number>((resolve, reject) => {\n child.once(\"error\", reject);\n child.once(\"exit\", (code, signal) => {\n if (typeof code === \"number\") {\n resolve(code);\n return;\n }\n resolve(signal ? 128 + signalNumber(signal) : 1);\n });\n });\n\n process.exitCode = exitCode;\n}\n\nexport async function verifyCodexxModel(\n invocation: Pick<CodexxInvocation, \"baseUrl\" | \"env\" | \"model\">,\n fetcher: FetchLike = fetch,\n): Promise<void> {\n const modelsUrl = `${trimTrailingSlash(invocation.baseUrl)}/models`;\n const apiKey = invocation.env.OPENAI_API_KEY;\n if (apiKey === undefined) {\n throw new Error(\n \"verifyCodexxModel requires invocation.env.OPENAI_API_KEY; build the invocation with buildCodexxInvocation.\",\n );\n }\n let response: Response;\n try {\n response = await fetcher(modelsUrl, {\n headers: {\n accept: \"application/json\",\n authorization: `Bearer ${apiKey}`,\n },\n method: \"GET\",\n });\n } catch (error) {\n throw new Error(\n `Could not reach Hoopilot at ${modelsUrl}. Start Hoopilot first, or set CODEXX_SKIP_MODEL_PREFLIGHT=1 to skip this check. ${errorMessage(error)}`,\n );\n }\n\n if (!response.ok) {\n throw new Error(\n `Could not verify model ${JSON.stringify(invocation.model)} because ${modelsUrl} returned ${response.status}: ${await truncatedResponseText(response)}`,\n );\n }\n\n const models = modelIdsFromResponse(await response.json().catch(() => undefined));\n if (models.length > 0 && !models.includes(invocation.model)) {\n throw new Error(\n `The logged-in Copilot account does not advertise model ${JSON.stringify(invocation.model)} at ${modelsUrl}. Available models: ${models.join(\", \")}. After upgrading Hoopilot, rerun \"hoopilot login\" to refresh the Copilot OAuth token, or set CODEXX_MODEL to one of the advertised model IDs.`,\n );\n }\n}\n\nfunction helpText(): string {\n return `codexx\n\nRun Codex against an already-running local Hoopilot server.\n\nUsage:\n codexx [codex options] [prompt]\n\nEnvironment:\n CODEXX_BASE_URL OpenAI-compatible base URL. Default: ${DEFAULT_BASE_URL}\n CODEXX_API_KEY API key sent to the local Hoopilot server.\n HOOPILOT_API_KEY Used as the API key when CODEXX_API_KEY is unset. When\n neither is set, a random throwaway key is generated for\n an unauthenticated local server.\n CODEXX_CODEX_BIN Codex executable to run. Default: ${DEFAULT_CODEX_BIN}\n CODEXX_MODEL Codex model to use. Default: ${DEFAULT_MODEL}\n CODEXX_MODEL_REASONING_EFFORT\n Codex reasoning effort. Default: ${DEFAULT_REASONING_EFFORT}\n CODEXX_STREAM_IDLE_TIMEOUT_MS\n Codex Responses stream idle timeout in milliseconds. Default:\n ${DEFAULT_STREAM_IDLE_TIMEOUT_MS}; set 0 to use Codex's own default.\n CODEXX_SKIP_MODEL_PREFLIGHT\n Set to 1 to skip checking /v1/models before starting Codex.\n\ncodexx does not start Hoopilot and does not change your shell environment. It selects a temporary Hoopilot model provider with Responses WebSockets disabled, uses ${DEFAULT_MODEL} with ${DEFAULT_REASONING_EFFORT} reasoning by default, disables Codex's managed network proxy (permissions.workspace.network.enabled=false) so requests reach the local server instead of being blocked by its allowlist, and removes proxy variables only from the spawned Codex process.`;\n}\n\nfunction signalNumber(signal: NodeJS.Signals): number {\n return osConstants.signals[signal] ?? 1;\n}\n\nif (import.meta.main) {\n main().catch((error: unknown) => {\n console.error(errorMessage(error));\n process.exit(1);\n });\n}\n","/** Default model Hoopilot uses when a client does not supply one. */\nexport const DEFAULT_MODEL = \"gpt-5.5\";\n","import { isIP } from \"node:net\";\nimport type { JsonObject, StreamingProxyMode, UsageAccountingMode } from \"./types\";\n\n/** Remove any trailing slashes from a URL or path string. */\nexport function trimTrailingSlash(value: string): string {\n return value.replace(/\\/+$/, \"\");\n}\n\n/** Treat blank environment variables as unset while preserving nonblank values. */\nexport function envValue(value: string | undefined): string | undefined {\n const trimmed = value?.trim();\n return trimmed ? trimmed : undefined;\n}\n\n/** True for HTTPS URLs, or HTTP only on loopback hosts used by local tests/dev. */\nexport function isHttpsOrLoopbackUrl(rawUrl: string): boolean {\n const url = parseUrl(rawUrl);\n if (!url) {\n return false;\n }\n return url.protocol === \"https:\" || isLoopbackHttpUrl(url);\n}\n\n/** Validate a base URL before sending a bearer/OAuth token to it. */\nexport function isTrustedTokenBaseUrl(\n rawUrl: string,\n allowedHttpsHosts: readonly string[],\n allowUnsafeHttps = false,\n): boolean {\n const url = parseUrl(rawUrl);\n if (!url) {\n return false;\n }\n if (url.username || url.password || url.search || url.hash) {\n return false;\n }\n if (url.pathname !== \"\" && url.pathname !== \"/\") {\n return false;\n }\n if (isLoopbackHttpUrl(url)) {\n return true;\n }\n if (url.protocol !== \"https:\") {\n return false;\n }\n const host = url.hostname.toLowerCase();\n return allowedHttpsHosts.includes(host) || allowUnsafeHttps;\n}\n\nfunction parseUrl(rawUrl: string): URL | undefined {\n let url: URL;\n try {\n url = new URL(rawUrl);\n } catch {\n return undefined;\n }\n return url;\n}\n\n/** True for hostnames that always resolve to the local machine. */\nexport function isLoopbackHostname(host: string): boolean {\n const normalized = host.trim().toLowerCase();\n const address =\n normalized.startsWith(\"[\") && normalized.endsWith(\"]\") ? normalized.slice(1, -1) : normalized;\n if (address === \"localhost\") {\n return true;\n }\n if (isIP(address) === 4) {\n return address.startsWith(\"127.\");\n }\n return isIP(address) === 6 && (address === \"::1\" || address === \"0:0:0:0:0:0:0:1\");\n}\n\nfunction isLoopbackHttpUrl(url: URL): boolean {\n return url.protocol === \"http:\" && isLoopbackHostname(url.hostname);\n}\n\n/** Read a response body as text, truncated to keep error messages bounded. */\nexport async function truncatedResponseText(response: Response, max = 500): Promise<string> {\n const text = await response.text();\n return text.slice(0, max);\n}\n\n/** Narrow an unknown value to a plain object, returning {} for arrays/primitives/null. */\nexport function asRecord(value: unknown): JsonObject {\n return value && typeof value === \"object\" && !Array.isArray(value) ? (value as JsonObject) : {};\n}\n\n/** Extract a human-readable message from an unknown thrown value. */\nexport function errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/** Return the first finite number among the candidates, else undefined. */\nexport function firstNumber(...values: unknown[]): number | undefined {\n for (const value of values) {\n if (typeof value === \"number\" && Number.isFinite(value)) {\n return value;\n }\n }\n return undefined;\n}\n\n/** Generate a dash-free random identifier for synthesized response/message ids. */\nexport function randomId(): string {\n return crypto.randomUUID().replaceAll(\"-\", \"\");\n}\n\n/** Drop keys whose value is undefined so they are omitted from JSON output. */\nexport function removeUndefined<T extends object>(value: T): T {\n return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined)) as T;\n}\n\n/** Parse JSON, returning undefined instead of throwing on malformed input. */\nexport function safeJsonParse(text: string): unknown {\n try {\n return JSON.parse(text);\n } catch {\n return undefined;\n }\n}\n\n/** Parse JSON into a plain object, returning undefined on malformed or non-object input. */\nexport function parseJsonObject(text: string): JsonObject | undefined {\n try {\n return asRecord(JSON.parse(text));\n } catch {\n return undefined;\n }\n}\n\n/**\n * Extract de-duplicated model IDs from an OpenAI-style `/models` response (an\n * object carrying a `data` array, or a bare array of model objects).\n */\nexport function modelIdsFromResponse(body: unknown): string[] {\n const record = asRecord(body);\n const data = Array.isArray(record.data) ? record.data : Array.isArray(body) ? body : [];\n const seen = new Set<string>();\n const ids: string[] = [];\n for (const model of data) {\n const id = asRecord(model).id;\n if (typeof id !== \"string\" || id.length === 0 || seen.has(id)) {\n continue;\n }\n seen.add(id);\n ids.push(id);\n }\n return ids;\n}\n\n/** Canonical set of accepted streaming-proxy modes, kept in sync with {@link StreamingProxyMode}. */\nexport const STREAMING_PROXY_MODES = [\n \"auto\",\n \"buffer\",\n \"live\",\n] as const satisfies readonly StreamingProxyMode[];\n\n/** Canonical set of accepted token/accounting modes, kept in sync with {@link UsageAccountingMode}. */\nexport const USAGE_ACCOUNTING_MODES = [\n \"basic\",\n \"full\",\n \"off\",\n] as const satisfies readonly UsageAccountingMode[];\n\n/** Validate a stream-mode string against the allowed {@link StreamingProxyMode} values. */\nexport function parseStreamingProxyMode(value: string): StreamingProxyMode {\n if ((STREAMING_PROXY_MODES as readonly string[]).includes(value)) {\n return value as StreamingProxyMode;\n }\n throw new Error(`Invalid stream mode: ${value}. Expected ${STREAMING_PROXY_MODES.join(\", \")}.`);\n}\n\n/** Validate a usage-accounting string against the allowed {@link UsageAccountingMode} values. */\nexport function parseUsageAccountingMode(value: string): UsageAccountingMode {\n if ((USAGE_ACCOUNTING_MODES as readonly string[]).includes(value)) {\n return value as UsageAccountingMode;\n }\n throw new Error(\n `Invalid usage accounting mode: ${value}. Expected ${USAGE_ACCOUNTING_MODES.join(\", \")}.`,\n );\n}\n\n/** Parse common environment boolean spellings. */\nexport function parseBooleanEnv(value: string | undefined, name: string): boolean | undefined {\n const raw = envValue(value)?.toLowerCase();\n if (raw === undefined) {\n return undefined;\n }\n if (raw === \"1\" || raw === \"true\" || raw === \"yes\" || raw === \"on\") {\n return true;\n }\n if (raw === \"0\" || raw === \"false\" || raw === \"no\" || raw === \"off\") {\n return false;\n }\n throw new Error(`${name} must be one of: 1, 0, true, false, yes, no, on, off.`);\n}\n"],"mappings":";AAEA,SAAS,aAAa;AACtB,SAAS,aAAa,mBAAmB;;;ACFlC,IAAM,gBAAgB;;;ACD7B,SAAS,YAAY;AAId,SAAS,kBAAkB,OAAuB;AACvD,SAAO,MAAM,QAAQ,QAAQ,EAAE;AACjC;AAGO,SAAS,SAAS,OAA+C;AACtE,QAAM,UAAU,OAAO,KAAK;AAC5B,SAAO,UAAU,UAAU;AAC7B;AAYO,SAAS,sBACd,QACA,mBACA,mBAAmB,OACV;AACT,QAA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