@warmdrift/kgauto-compiler 2.0.0-alpha.7 → 2.0.0-alpha.70
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 +176 -46
- package/dist/brain-proxy.d.mts +113 -0
- package/dist/brain-proxy.d.ts +113 -0
- package/dist/brain-proxy.js +193 -0
- package/dist/brain-proxy.mjs +6 -0
- package/dist/chunk-4UO4CCSP.mjs +1620 -0
- package/dist/chunk-65ZMX5OT.mjs +169 -0
- package/dist/{chunk-5TI6PNSK.mjs → chunk-BVEXV5KC.mjs} +11 -0
- package/dist/chunk-FR4DNGLW.mjs +203 -0
- package/dist/chunk-NBO4R5PC.mjs +313 -0
- package/dist/chunk-P3TOAEG4.mjs +56 -0
- package/dist/chunk-RO22VFIF.mjs +29 -0
- package/dist/chunk-SBFSYCQG.mjs +719 -0
- package/dist/dialect.d.mts +41 -3
- package/dist/dialect.d.ts +41 -3
- package/dist/dialect.js +14 -2
- package/dist/dialect.mjs +5 -3
- package/dist/glassbox/index.d.mts +59 -0
- package/dist/glassbox/index.d.ts +59 -0
- package/dist/glassbox/index.js +312 -0
- package/dist/glassbox/index.mjs +12 -0
- package/dist/glassbox-routes/format.d.mts +24 -0
- package/dist/glassbox-routes/format.d.ts +24 -0
- package/dist/glassbox-routes/format.js +86 -0
- package/dist/glassbox-routes/format.mjs +18 -0
- package/dist/glassbox-routes/index.d.mts +191 -0
- package/dist/glassbox-routes/index.d.ts +191 -0
- package/dist/glassbox-routes/index.js +2888 -0
- package/dist/glassbox-routes/index.mjs +667 -0
- package/dist/glassbox-routes/react/index.d.mts +74 -0
- package/dist/glassbox-routes/react/index.d.ts +74 -0
- package/dist/glassbox-routes/react/index.js +819 -0
- package/dist/glassbox-routes/react/index.mjs +754 -0
- package/dist/index.d.mts +2739 -17
- package/dist/index.d.ts +2739 -17
- package/dist/index.js +8989 -1659
- package/dist/index.mjs +5078 -367
- package/dist/ir-Bqn1RVdV.d.mts +1583 -0
- package/dist/ir-Bvlkw5ja.d.ts +1583 -0
- package/dist/key-health.d.mts +131 -0
- package/dist/key-health.d.ts +131 -0
- package/dist/key-health.js +228 -0
- package/dist/key-health.mjs +6 -0
- package/dist/profiles.d.mts +292 -2
- package/dist/profiles.d.ts +292 -2
- package/dist/profiles.js +1231 -16
- package/dist/profiles.mjs +9 -1
- package/dist/types-Bon96eyc.d.ts +131 -0
- package/dist/types-CwGhacGT.d.mts +149 -0
- package/dist/types-DIxRZ3Dj.d.ts +149 -0
- package/dist/types-DsEq35WY.d.mts +131 -0
- package/package.json +54 -8
- package/dist/chunk-MBEI5UOM.mjs +0 -409
- package/dist/profiles-B3eNQ2py.d.ts +0 -619
- package/dist/profiles-Py8c7zjJ.d.mts +0 -619
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@warmdrift/kgauto-compiler/key-health` — per-consumer provider-key health
|
|
3
|
+
* endpoint factory (alpha.58, KG-fleet-v1 phase 1).
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* Consumers (playbacksam, inspire-central, tt-intelligence, global-expansion)
|
|
8
|
+
* hold their own provider API keys in their own Vercel envs — the library
|
|
9
|
+
* never sees keys centrally, by design. That means the operator dashboard has
|
|
10
|
+
* no way to answer "is PB's DeepSeek key still valid?" without a per-consumer
|
|
11
|
+
* probe surface. This factory is that surface: the consumer mounts ONE route,
|
|
12
|
+
* the dashboard polls it with the consumer's existing `KGAUTO_INGEST_SECRET`
|
|
13
|
+
* bearer, and gets back a validity grid for every canonical provider key.
|
|
14
|
+
*
|
|
15
|
+
* Probes are the cheapest authenticated GETs each provider offers (model
|
|
16
|
+
* listings; DeepSeek's balance endpoint) — never a completion call, zero
|
|
17
|
+
* token cost. Keys themselves NEVER appear in any response: only presence,
|
|
18
|
+
* validity, HTTP status, latency, and (DeepSeek only) account balance.
|
|
19
|
+
*
|
|
20
|
+
* Same design contract as `createBrainForwardRoutes` (brain-proxy.ts):
|
|
21
|
+
* web-standard Request→Response, never throws (every path returns a
|
|
22
|
+
* `Response`), bearer gate, runs on Edge/Workers/Node.
|
|
23
|
+
*
|
|
24
|
+
* ## Mounting recipes
|
|
25
|
+
*
|
|
26
|
+
* ### (a) Next.js app-router route handler — ONE file
|
|
27
|
+
*
|
|
28
|
+
* // app/api/kgauto/v2/keyhealth/route.ts
|
|
29
|
+
* import { createKeyHealthRoute } from '@warmdrift/kgauto-compiler/key-health';
|
|
30
|
+
* const routes = createKeyHealthRoute({
|
|
31
|
+
* appId: 'playbacksam',
|
|
32
|
+
* ingestSecret: process.env.KGAUTO_INGEST_SECRET!,
|
|
33
|
+
* });
|
|
34
|
+
* export const GET = (req: Request) => routes.handle(req);
|
|
35
|
+
*
|
|
36
|
+
* // middleware.ts — if the app gates routes behind auth middleware, the
|
|
37
|
+
* // existing brain-proxy public-path prefix ('/api/kgauto/v2/') already
|
|
38
|
+
* // covers this route. If the allowlist is per-path, add
|
|
39
|
+
* // '/api/kgauto/v2/keyhealth' or server-to-server GETs 307 to /login
|
|
40
|
+
* // (the tt-intel middleware-drift shape — see brain-proxy.ts).
|
|
41
|
+
*
|
|
42
|
+
* ### (b) Plain per-file Vercel function
|
|
43
|
+
*
|
|
44
|
+
* // api/kgauto/keyhealth.ts
|
|
45
|
+
* export const config = { runtime: 'edge' };
|
|
46
|
+
* import { createKeyHealthRoute } from '@warmdrift/kgauto-compiler/key-health';
|
|
47
|
+
* const routes = createKeyHealthRoute({
|
|
48
|
+
* appId: 'inspire-central',
|
|
49
|
+
* ingestSecret: process.env.KGAUTO_INGEST_SECRET!,
|
|
50
|
+
* });
|
|
51
|
+
* export default (req: Request) => routes.handle(req);
|
|
52
|
+
*
|
|
53
|
+
* ## Wire shape (LOCKED CONTRACT with the operator dashboard — do not rename)
|
|
54
|
+
*
|
|
55
|
+
* 200 {
|
|
56
|
+
* app_id: string,
|
|
57
|
+
* checked_at: string, // ISO timestamp
|
|
58
|
+
* library_version: string, // installed @warmdrift/kgauto-compiler
|
|
59
|
+
* // version (alpha.60+) — the dashboard's
|
|
60
|
+
* // live vendor-drift signal; absent on
|
|
61
|
+
* // pre-alpha.60 mounts, render "—"
|
|
62
|
+
* keys: [ // sorted by provider name
|
|
63
|
+
* {
|
|
64
|
+
* provider: 'anthropic' | 'deepseek' | 'google' | 'openai',
|
|
65
|
+
* env: string, // canonical env var name probed
|
|
66
|
+
* present: boolean, // key present (non-empty after trim)
|
|
67
|
+
* valid: boolean | null, // true 2xx / false 401·403 / null unknown
|
|
68
|
+
* status?: number, // probe HTTP status when a response landed
|
|
69
|
+
* latency_ms?: number, // probe round-trip
|
|
70
|
+
* balance_usd?: number, // DeepSeek only, when currency is USD
|
|
71
|
+
* detail?: string, // e.g. 'timeout', 'http_500', 'key_absent'
|
|
72
|
+
* }
|
|
73
|
+
* ]
|
|
74
|
+
* }
|
|
75
|
+
*/
|
|
76
|
+
type KeyHealthProvider = 'anthropic' | 'deepseek' | 'google' | 'moonshot' | 'openai';
|
|
77
|
+
interface KeyHealthConfig {
|
|
78
|
+
/** Consumer app id echoed as `app_id` in the response (e.g. 'playbacksam'). */
|
|
79
|
+
appId: string;
|
|
80
|
+
/** Bearer token the poller must present (consumer's existing KGAUTO_INGEST_SECRET). */
|
|
81
|
+
ingestSecret: string;
|
|
82
|
+
/** Optional fetch impl for tests. */
|
|
83
|
+
fetchImpl?: typeof fetch;
|
|
84
|
+
/** Env source; defaults to process.env. Test injection point. */
|
|
85
|
+
env?: Record<string, string | undefined>;
|
|
86
|
+
/** Per-provider probe timeout in ms. Default 3000. */
|
|
87
|
+
timeoutMs?: number;
|
|
88
|
+
}
|
|
89
|
+
interface KeyHealthResult {
|
|
90
|
+
provider: KeyHealthProvider;
|
|
91
|
+
/** Canonical env var name probed (the resolved one for Google). */
|
|
92
|
+
env: string;
|
|
93
|
+
/** Key present (non-empty after trim) in env. */
|
|
94
|
+
present: boolean;
|
|
95
|
+
/** true = provider accepted the key (2xx); false = rejected (401/403); null = unknown (absent / timeout / 5xx / 429 / network). */
|
|
96
|
+
valid: boolean | null;
|
|
97
|
+
/** HTTP status of the probe, when a response landed. */
|
|
98
|
+
status?: number;
|
|
99
|
+
/** Probe round-trip in ms, when a probe ran. */
|
|
100
|
+
latency_ms?: number;
|
|
101
|
+
/** DeepSeek only: account balance in USD when the balance API reports USD. */
|
|
102
|
+
balance_usd?: number;
|
|
103
|
+
/** Failure class when valid is null with a probe attempted (e.g. 'timeout', 'http_500') or 'key_absent'. */
|
|
104
|
+
detail?: string;
|
|
105
|
+
}
|
|
106
|
+
interface KeyHealthResponseBody {
|
|
107
|
+
app_id: string;
|
|
108
|
+
checked_at: string;
|
|
109
|
+
/**
|
|
110
|
+
* alpha.60 — installed library version (`LIBRARY_VERSION`). Lets the
|
|
111
|
+
* operator dashboard render per-consumer vendor drift from the same poll
|
|
112
|
+
* it already makes; absent on pre-alpha.60 mounts.
|
|
113
|
+
*/
|
|
114
|
+
library_version: string;
|
|
115
|
+
keys: KeyHealthResult[];
|
|
116
|
+
}
|
|
117
|
+
interface KeyHealthRoute {
|
|
118
|
+
/**
|
|
119
|
+
* Web-standard handler. GET only. Never throws — every error path returns
|
|
120
|
+
* a `Response`.
|
|
121
|
+
*/
|
|
122
|
+
handle(req: Request): Promise<Response>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Create the per-consumer key-health route. See module docstring for the
|
|
126
|
+
* mounting recipes ((a) Next.js route handler, (b) per-file Vercel function)
|
|
127
|
+
* and the locked wire shape.
|
|
128
|
+
*/
|
|
129
|
+
declare function createKeyHealthRoute(config: KeyHealthConfig): KeyHealthRoute;
|
|
130
|
+
|
|
131
|
+
export { type KeyHealthConfig, type KeyHealthProvider, type KeyHealthResponseBody, type KeyHealthResult, type KeyHealthRoute, createKeyHealthRoute };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@warmdrift/kgauto-compiler/key-health` — per-consumer provider-key health
|
|
3
|
+
* endpoint factory (alpha.58, KG-fleet-v1 phase 1).
|
|
4
|
+
*
|
|
5
|
+
* ## Why this exists
|
|
6
|
+
*
|
|
7
|
+
* Consumers (playbacksam, inspire-central, tt-intelligence, global-expansion)
|
|
8
|
+
* hold their own provider API keys in their own Vercel envs — the library
|
|
9
|
+
* never sees keys centrally, by design. That means the operator dashboard has
|
|
10
|
+
* no way to answer "is PB's DeepSeek key still valid?" without a per-consumer
|
|
11
|
+
* probe surface. This factory is that surface: the consumer mounts ONE route,
|
|
12
|
+
* the dashboard polls it with the consumer's existing `KGAUTO_INGEST_SECRET`
|
|
13
|
+
* bearer, and gets back a validity grid for every canonical provider key.
|
|
14
|
+
*
|
|
15
|
+
* Probes are the cheapest authenticated GETs each provider offers (model
|
|
16
|
+
* listings; DeepSeek's balance endpoint) — never a completion call, zero
|
|
17
|
+
* token cost. Keys themselves NEVER appear in any response: only presence,
|
|
18
|
+
* validity, HTTP status, latency, and (DeepSeek only) account balance.
|
|
19
|
+
*
|
|
20
|
+
* Same design contract as `createBrainForwardRoutes` (brain-proxy.ts):
|
|
21
|
+
* web-standard Request→Response, never throws (every path returns a
|
|
22
|
+
* `Response`), bearer gate, runs on Edge/Workers/Node.
|
|
23
|
+
*
|
|
24
|
+
* ## Mounting recipes
|
|
25
|
+
*
|
|
26
|
+
* ### (a) Next.js app-router route handler — ONE file
|
|
27
|
+
*
|
|
28
|
+
* // app/api/kgauto/v2/keyhealth/route.ts
|
|
29
|
+
* import { createKeyHealthRoute } from '@warmdrift/kgauto-compiler/key-health';
|
|
30
|
+
* const routes = createKeyHealthRoute({
|
|
31
|
+
* appId: 'playbacksam',
|
|
32
|
+
* ingestSecret: process.env.KGAUTO_INGEST_SECRET!,
|
|
33
|
+
* });
|
|
34
|
+
* export const GET = (req: Request) => routes.handle(req);
|
|
35
|
+
*
|
|
36
|
+
* // middleware.ts — if the app gates routes behind auth middleware, the
|
|
37
|
+
* // existing brain-proxy public-path prefix ('/api/kgauto/v2/') already
|
|
38
|
+
* // covers this route. If the allowlist is per-path, add
|
|
39
|
+
* // '/api/kgauto/v2/keyhealth' or server-to-server GETs 307 to /login
|
|
40
|
+
* // (the tt-intel middleware-drift shape — see brain-proxy.ts).
|
|
41
|
+
*
|
|
42
|
+
* ### (b) Plain per-file Vercel function
|
|
43
|
+
*
|
|
44
|
+
* // api/kgauto/keyhealth.ts
|
|
45
|
+
* export const config = { runtime: 'edge' };
|
|
46
|
+
* import { createKeyHealthRoute } from '@warmdrift/kgauto-compiler/key-health';
|
|
47
|
+
* const routes = createKeyHealthRoute({
|
|
48
|
+
* appId: 'inspire-central',
|
|
49
|
+
* ingestSecret: process.env.KGAUTO_INGEST_SECRET!,
|
|
50
|
+
* });
|
|
51
|
+
* export default (req: Request) => routes.handle(req);
|
|
52
|
+
*
|
|
53
|
+
* ## Wire shape (LOCKED CONTRACT with the operator dashboard — do not rename)
|
|
54
|
+
*
|
|
55
|
+
* 200 {
|
|
56
|
+
* app_id: string,
|
|
57
|
+
* checked_at: string, // ISO timestamp
|
|
58
|
+
* library_version: string, // installed @warmdrift/kgauto-compiler
|
|
59
|
+
* // version (alpha.60+) — the dashboard's
|
|
60
|
+
* // live vendor-drift signal; absent on
|
|
61
|
+
* // pre-alpha.60 mounts, render "—"
|
|
62
|
+
* keys: [ // sorted by provider name
|
|
63
|
+
* {
|
|
64
|
+
* provider: 'anthropic' | 'deepseek' | 'google' | 'openai',
|
|
65
|
+
* env: string, // canonical env var name probed
|
|
66
|
+
* present: boolean, // key present (non-empty after trim)
|
|
67
|
+
* valid: boolean | null, // true 2xx / false 401·403 / null unknown
|
|
68
|
+
* status?: number, // probe HTTP status when a response landed
|
|
69
|
+
* latency_ms?: number, // probe round-trip
|
|
70
|
+
* balance_usd?: number, // DeepSeek only, when currency is USD
|
|
71
|
+
* detail?: string, // e.g. 'timeout', 'http_500', 'key_absent'
|
|
72
|
+
* }
|
|
73
|
+
* ]
|
|
74
|
+
* }
|
|
75
|
+
*/
|
|
76
|
+
type KeyHealthProvider = 'anthropic' | 'deepseek' | 'google' | 'moonshot' | 'openai';
|
|
77
|
+
interface KeyHealthConfig {
|
|
78
|
+
/** Consumer app id echoed as `app_id` in the response (e.g. 'playbacksam'). */
|
|
79
|
+
appId: string;
|
|
80
|
+
/** Bearer token the poller must present (consumer's existing KGAUTO_INGEST_SECRET). */
|
|
81
|
+
ingestSecret: string;
|
|
82
|
+
/** Optional fetch impl for tests. */
|
|
83
|
+
fetchImpl?: typeof fetch;
|
|
84
|
+
/** Env source; defaults to process.env. Test injection point. */
|
|
85
|
+
env?: Record<string, string | undefined>;
|
|
86
|
+
/** Per-provider probe timeout in ms. Default 3000. */
|
|
87
|
+
timeoutMs?: number;
|
|
88
|
+
}
|
|
89
|
+
interface KeyHealthResult {
|
|
90
|
+
provider: KeyHealthProvider;
|
|
91
|
+
/** Canonical env var name probed (the resolved one for Google). */
|
|
92
|
+
env: string;
|
|
93
|
+
/** Key present (non-empty after trim) in env. */
|
|
94
|
+
present: boolean;
|
|
95
|
+
/** true = provider accepted the key (2xx); false = rejected (401/403); null = unknown (absent / timeout / 5xx / 429 / network). */
|
|
96
|
+
valid: boolean | null;
|
|
97
|
+
/** HTTP status of the probe, when a response landed. */
|
|
98
|
+
status?: number;
|
|
99
|
+
/** Probe round-trip in ms, when a probe ran. */
|
|
100
|
+
latency_ms?: number;
|
|
101
|
+
/** DeepSeek only: account balance in USD when the balance API reports USD. */
|
|
102
|
+
balance_usd?: number;
|
|
103
|
+
/** Failure class when valid is null with a probe attempted (e.g. 'timeout', 'http_500') or 'key_absent'. */
|
|
104
|
+
detail?: string;
|
|
105
|
+
}
|
|
106
|
+
interface KeyHealthResponseBody {
|
|
107
|
+
app_id: string;
|
|
108
|
+
checked_at: string;
|
|
109
|
+
/**
|
|
110
|
+
* alpha.60 — installed library version (`LIBRARY_VERSION`). Lets the
|
|
111
|
+
* operator dashboard render per-consumer vendor drift from the same poll
|
|
112
|
+
* it already makes; absent on pre-alpha.60 mounts.
|
|
113
|
+
*/
|
|
114
|
+
library_version: string;
|
|
115
|
+
keys: KeyHealthResult[];
|
|
116
|
+
}
|
|
117
|
+
interface KeyHealthRoute {
|
|
118
|
+
/**
|
|
119
|
+
* Web-standard handler. GET only. Never throws — every error path returns
|
|
120
|
+
* a `Response`.
|
|
121
|
+
*/
|
|
122
|
+
handle(req: Request): Promise<Response>;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Create the per-consumer key-health route. See module docstring for the
|
|
126
|
+
* mounting recipes ((a) Next.js route handler, (b) per-file Vercel function)
|
|
127
|
+
* and the locked wire shape.
|
|
128
|
+
*/
|
|
129
|
+
declare function createKeyHealthRoute(config: KeyHealthConfig): KeyHealthRoute;
|
|
130
|
+
|
|
131
|
+
export { type KeyHealthConfig, type KeyHealthProvider, type KeyHealthResponseBody, type KeyHealthResult, type KeyHealthRoute, createKeyHealthRoute };
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/key-health.ts
|
|
21
|
+
var key_health_exports = {};
|
|
22
|
+
__export(key_health_exports, {
|
|
23
|
+
createKeyHealthRoute: () => createKeyHealthRoute
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(key_health_exports);
|
|
26
|
+
|
|
27
|
+
// src/version.ts
|
|
28
|
+
var LIBRARY_VERSION = "2.0.0-alpha.70";
|
|
29
|
+
|
|
30
|
+
// src/key-health.ts
|
|
31
|
+
var JSON_HEADERS = { "Content-Type": "application/json" };
|
|
32
|
+
function jsonResponse(status, body) {
|
|
33
|
+
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS });
|
|
34
|
+
}
|
|
35
|
+
function bearerOf(req) {
|
|
36
|
+
const header = req.headers.get("Authorization") ?? "";
|
|
37
|
+
const match = /^Bearer\s+(.+)$/i.exec(header);
|
|
38
|
+
return match?.[1]?.trim() ?? "";
|
|
39
|
+
}
|
|
40
|
+
function requireString(name, value) {
|
|
41
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
42
|
+
throw new Error(`createKeyHealthRoute: ${name} is required`);
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
function envKey(env, name) {
|
|
47
|
+
const v = env[name];
|
|
48
|
+
if (typeof v !== "string") return void 0;
|
|
49
|
+
const trimmed = v.trim();
|
|
50
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
51
|
+
}
|
|
52
|
+
var PROBE_SPECS = [
|
|
53
|
+
{
|
|
54
|
+
provider: "anthropic",
|
|
55
|
+
canonicalEnvName: "ANTHROPIC_API_KEY",
|
|
56
|
+
buildRequest: (key) => ({
|
|
57
|
+
url: "https://api.anthropic.com/v1/models?limit=1",
|
|
58
|
+
headers: { "x-api-key": key, "anthropic-version": "2023-06-01" }
|
|
59
|
+
})
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
provider: "deepseek",
|
|
63
|
+
canonicalEnvName: "DEEPSEEK_API_KEY",
|
|
64
|
+
buildRequest: (key) => ({
|
|
65
|
+
url: "https://api.deepseek.com/user/balance",
|
|
66
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
67
|
+
}),
|
|
68
|
+
parseBalanceUsd: (body) => {
|
|
69
|
+
const infos = body?.balance_infos;
|
|
70
|
+
if (!Array.isArray(infos) || infos.length === 0) return void 0;
|
|
71
|
+
const first = infos[0];
|
|
72
|
+
if (first?.currency !== "USD") return void 0;
|
|
73
|
+
const n = Number(first.total_balance);
|
|
74
|
+
return Number.isFinite(n) ? n : void 0;
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
provider: "moonshot",
|
|
79
|
+
canonicalEnvName: "MOONSHOT_API_KEY",
|
|
80
|
+
buildRequest: (key) => ({
|
|
81
|
+
url: "https://api.moonshot.ai/v1/models",
|
|
82
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
83
|
+
})
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
provider: "google",
|
|
87
|
+
canonicalEnvName: "GEMINI_API_KEY",
|
|
88
|
+
buildRequest: (key) => ({
|
|
89
|
+
url: `https://generativelanguage.googleapis.com/v1beta/models?pageSize=1&key=${encodeURIComponent(key)}`,
|
|
90
|
+
headers: {}
|
|
91
|
+
})
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
provider: "openai",
|
|
95
|
+
canonicalEnvName: "OPENAI_API_KEY",
|
|
96
|
+
buildRequest: (key) => ({
|
|
97
|
+
url: "https://api.openai.com/v1/models",
|
|
98
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
];
|
|
102
|
+
function createKeyHealthRoute(config) {
|
|
103
|
+
const appId = requireString("appId", config.appId);
|
|
104
|
+
const ingestSecret = requireString("ingestSecret", config.ingestSecret);
|
|
105
|
+
const fetchFn = config.fetchImpl ?? fetch;
|
|
106
|
+
const timeoutMs = config.timeoutMs ?? 3e3;
|
|
107
|
+
async function probeProvider(spec, env) {
|
|
108
|
+
let key;
|
|
109
|
+
let envName = spec.canonicalEnvName;
|
|
110
|
+
if (spec.provider === "google") {
|
|
111
|
+
const gemini = envKey(env, "GEMINI_API_KEY");
|
|
112
|
+
const google = envKey(env, "GOOGLE_API_KEY");
|
|
113
|
+
const aiSdk = envKey(env, "GOOGLE_GENERATIVE_AI_API_KEY");
|
|
114
|
+
key = gemini ?? google ?? aiSdk;
|
|
115
|
+
envName = gemini ? "GEMINI_API_KEY" : google ? "GOOGLE_API_KEY" : aiSdk ? "GOOGLE_GENERATIVE_AI_API_KEY" : "GEMINI_API_KEY";
|
|
116
|
+
} else if (spec.provider === "moonshot") {
|
|
117
|
+
const moonshot = envKey(env, "MOONSHOT_API_KEY");
|
|
118
|
+
const kimi = envKey(env, "KIMI_API_KEY");
|
|
119
|
+
key = moonshot ?? kimi;
|
|
120
|
+
envName = moonshot ? "MOONSHOT_API_KEY" : kimi ? "KIMI_API_KEY" : "MOONSHOT_API_KEY";
|
|
121
|
+
} else {
|
|
122
|
+
key = envKey(env, spec.canonicalEnvName);
|
|
123
|
+
}
|
|
124
|
+
if (!key) {
|
|
125
|
+
return {
|
|
126
|
+
provider: spec.provider,
|
|
127
|
+
env: envName,
|
|
128
|
+
present: false,
|
|
129
|
+
valid: null,
|
|
130
|
+
detail: "key_absent"
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
const base = {
|
|
134
|
+
provider: spec.provider,
|
|
135
|
+
env: envName,
|
|
136
|
+
present: true,
|
|
137
|
+
valid: null
|
|
138
|
+
};
|
|
139
|
+
const { url, headers } = spec.buildRequest(key);
|
|
140
|
+
const started = Date.now();
|
|
141
|
+
let res;
|
|
142
|
+
try {
|
|
143
|
+
res = await fetchFn(url, {
|
|
144
|
+
method: "GET",
|
|
145
|
+
headers,
|
|
146
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
147
|
+
});
|
|
148
|
+
} catch (err) {
|
|
149
|
+
const isTimeout = err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError");
|
|
150
|
+
return {
|
|
151
|
+
...base,
|
|
152
|
+
latency_ms: Date.now() - started,
|
|
153
|
+
// NEVER echo err.message — provider errors could theoretically carry
|
|
154
|
+
// request context; a fixed vocabulary keeps key material impossible.
|
|
155
|
+
detail: isTimeout ? "timeout" : "network_error"
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
const latencyMs = Date.now() - started;
|
|
159
|
+
if (res.ok) {
|
|
160
|
+
const result = {
|
|
161
|
+
...base,
|
|
162
|
+
valid: true,
|
|
163
|
+
status: res.status,
|
|
164
|
+
latency_ms: latencyMs
|
|
165
|
+
};
|
|
166
|
+
if (spec.parseBalanceUsd) {
|
|
167
|
+
try {
|
|
168
|
+
const body = await res.json();
|
|
169
|
+
const balance = spec.parseBalanceUsd(body);
|
|
170
|
+
if (balance !== void 0) result.balance_usd = balance;
|
|
171
|
+
} catch {
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
if (res.status === 401 || res.status === 403) {
|
|
177
|
+
return { ...base, valid: false, status: res.status, latency_ms: latencyMs };
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
...base,
|
|
181
|
+
valid: null,
|
|
182
|
+
status: res.status,
|
|
183
|
+
latency_ms: latencyMs,
|
|
184
|
+
detail: `http_${res.status}`
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
async function handle(req) {
|
|
188
|
+
try {
|
|
189
|
+
if (req.method !== "GET") {
|
|
190
|
+
return jsonResponse(405, { error: "method_not_allowed" });
|
|
191
|
+
}
|
|
192
|
+
if (bearerOf(req) !== ingestSecret) {
|
|
193
|
+
return jsonResponse(401, { error: "unauthorized" });
|
|
194
|
+
}
|
|
195
|
+
const env = config.env ?? process.env;
|
|
196
|
+
const settled = await Promise.allSettled(
|
|
197
|
+
PROBE_SPECS.map((spec) => probeProvider(spec, env))
|
|
198
|
+
);
|
|
199
|
+
const keys = settled.map((s, i) => {
|
|
200
|
+
if (s.status === "fulfilled") return s.value;
|
|
201
|
+
const spec = PROBE_SPECS[i];
|
|
202
|
+
return {
|
|
203
|
+
provider: spec.provider,
|
|
204
|
+
env: spec.canonicalEnvName,
|
|
205
|
+
present: true,
|
|
206
|
+
valid: null,
|
|
207
|
+
detail: "probe_failed"
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
keys.sort((a, b) => a.provider.localeCompare(b.provider));
|
|
211
|
+
const body = {
|
|
212
|
+
app_id: appId,
|
|
213
|
+
checked_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
214
|
+
library_version: LIBRARY_VERSION,
|
|
215
|
+
keys
|
|
216
|
+
};
|
|
217
|
+
return jsonResponse(200, body);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
void err;
|
|
220
|
+
return jsonResponse(500, { error: "key_health_internal_error" });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return { handle };
|
|
224
|
+
}
|
|
225
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
226
|
+
0 && (module.exports = {
|
|
227
|
+
createKeyHealthRoute
|
|
228
|
+
});
|