@warmdrift/kgauto-compiler 2.0.0-alpha.6 → 2.0.0-alpha.61
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 +158 -46
- package/dist/brain-proxy.d.mts +113 -0
- package/dist/brain-proxy.d.ts +113 -0
- package/dist/brain-proxy.js +189 -0
- package/dist/brain-proxy.mjs +6 -0
- package/dist/chunk-3RMLZCUK.mjs +190 -0
- package/dist/chunk-IIMPJZNH.mjs +694 -0
- package/dist/chunk-IUWFML6Z.mjs +165 -0
- package/dist/chunk-NBO4R5PC.mjs +313 -0
- package/dist/chunk-P3TOAEG4.mjs +56 -0
- package/dist/chunk-QKXTMVCT.mjs +1470 -0
- package/dist/chunk-RO22VFIF.mjs +29 -0
- 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 +2714 -0
- package/dist/glassbox-routes/index.mjs +663 -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 +2236 -17
- package/dist/index.d.ts +2236 -17
- package/dist/index.js +7402 -1596
- package/dist/index.mjs +3580 -164
- package/dist/ir-DAKlQsVb.d.mts +1386 -0
- package/dist/ir-DmUuJsWc.d.ts +1386 -0
- package/dist/key-health.d.mts +131 -0
- package/dist/key-health.d.ts +131 -0
- package/dist/key-health.js +215 -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 +1081 -16
- package/dist/profiles.mjs +9 -1
- package/dist/types-B8X1Pyhx.d.ts +149 -0
- package/dist/types-CssWqd0X.d.mts +131 -0
- package/dist/types-DR62iPcO.d.ts +131 -0
- package/dist/types-MRMBUqzY.d.mts +149 -0
- package/package.json +54 -8
- package/dist/chunk-MBEI5UOM.mjs +0 -409
- package/dist/profiles-CQnLkQ7b.d.ts +0 -611
- package/dist/profiles-zm6diETo.d.mts +0 -611
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// src/version.ts
|
|
2
|
+
var LIBRARY_VERSION = "2.0.0-alpha.61";
|
|
3
|
+
|
|
4
|
+
// src/key-health.ts
|
|
5
|
+
var JSON_HEADERS = { "Content-Type": "application/json" };
|
|
6
|
+
function jsonResponse(status, body) {
|
|
7
|
+
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS });
|
|
8
|
+
}
|
|
9
|
+
function bearerOf(req) {
|
|
10
|
+
const header = req.headers.get("Authorization") ?? "";
|
|
11
|
+
const match = /^Bearer\s+(.+)$/i.exec(header);
|
|
12
|
+
return match?.[1]?.trim() ?? "";
|
|
13
|
+
}
|
|
14
|
+
function requireString(name, value) {
|
|
15
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
16
|
+
throw new Error(`createKeyHealthRoute: ${name} is required`);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function envKey(env, name) {
|
|
21
|
+
const v = env[name];
|
|
22
|
+
if (typeof v !== "string") return void 0;
|
|
23
|
+
const trimmed = v.trim();
|
|
24
|
+
return trimmed.length > 0 ? trimmed : void 0;
|
|
25
|
+
}
|
|
26
|
+
var PROBE_SPECS = [
|
|
27
|
+
{
|
|
28
|
+
provider: "anthropic",
|
|
29
|
+
canonicalEnvName: "ANTHROPIC_API_KEY",
|
|
30
|
+
buildRequest: (key) => ({
|
|
31
|
+
url: "https://api.anthropic.com/v1/models?limit=1",
|
|
32
|
+
headers: { "x-api-key": key, "anthropic-version": "2023-06-01" }
|
|
33
|
+
})
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
provider: "deepseek",
|
|
37
|
+
canonicalEnvName: "DEEPSEEK_API_KEY",
|
|
38
|
+
buildRequest: (key) => ({
|
|
39
|
+
url: "https://api.deepseek.com/user/balance",
|
|
40
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
41
|
+
}),
|
|
42
|
+
parseBalanceUsd: (body) => {
|
|
43
|
+
const infos = body?.balance_infos;
|
|
44
|
+
if (!Array.isArray(infos) || infos.length === 0) return void 0;
|
|
45
|
+
const first = infos[0];
|
|
46
|
+
if (first?.currency !== "USD") return void 0;
|
|
47
|
+
const n = Number(first.total_balance);
|
|
48
|
+
return Number.isFinite(n) ? n : void 0;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
provider: "google",
|
|
53
|
+
canonicalEnvName: "GEMINI_API_KEY",
|
|
54
|
+
buildRequest: (key) => ({
|
|
55
|
+
url: `https://generativelanguage.googleapis.com/v1beta/models?pageSize=1&key=${encodeURIComponent(key)}`,
|
|
56
|
+
headers: {}
|
|
57
|
+
})
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
provider: "openai",
|
|
61
|
+
canonicalEnvName: "OPENAI_API_KEY",
|
|
62
|
+
buildRequest: (key) => ({
|
|
63
|
+
url: "https://api.openai.com/v1/models",
|
|
64
|
+
headers: { Authorization: `Bearer ${key}` }
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
function createKeyHealthRoute(config) {
|
|
69
|
+
const appId = requireString("appId", config.appId);
|
|
70
|
+
const ingestSecret = requireString("ingestSecret", config.ingestSecret);
|
|
71
|
+
const fetchFn = config.fetchImpl ?? fetch;
|
|
72
|
+
const timeoutMs = config.timeoutMs ?? 3e3;
|
|
73
|
+
async function probeProvider(spec, env) {
|
|
74
|
+
let key;
|
|
75
|
+
let envName = spec.canonicalEnvName;
|
|
76
|
+
if (spec.provider === "google") {
|
|
77
|
+
const gemini = envKey(env, "GEMINI_API_KEY");
|
|
78
|
+
const google = envKey(env, "GOOGLE_API_KEY");
|
|
79
|
+
const aiSdk = envKey(env, "GOOGLE_GENERATIVE_AI_API_KEY");
|
|
80
|
+
key = gemini ?? google ?? aiSdk;
|
|
81
|
+
envName = gemini ? "GEMINI_API_KEY" : google ? "GOOGLE_API_KEY" : aiSdk ? "GOOGLE_GENERATIVE_AI_API_KEY" : "GEMINI_API_KEY";
|
|
82
|
+
} else {
|
|
83
|
+
key = envKey(env, spec.canonicalEnvName);
|
|
84
|
+
}
|
|
85
|
+
if (!key) {
|
|
86
|
+
return {
|
|
87
|
+
provider: spec.provider,
|
|
88
|
+
env: envName,
|
|
89
|
+
present: false,
|
|
90
|
+
valid: null,
|
|
91
|
+
detail: "key_absent"
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const base = {
|
|
95
|
+
provider: spec.provider,
|
|
96
|
+
env: envName,
|
|
97
|
+
present: true,
|
|
98
|
+
valid: null
|
|
99
|
+
};
|
|
100
|
+
const { url, headers } = spec.buildRequest(key);
|
|
101
|
+
const started = Date.now();
|
|
102
|
+
let res;
|
|
103
|
+
try {
|
|
104
|
+
res = await fetchFn(url, {
|
|
105
|
+
method: "GET",
|
|
106
|
+
headers,
|
|
107
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
108
|
+
});
|
|
109
|
+
} catch (err) {
|
|
110
|
+
const isTimeout = err instanceof Error && (err.name === "TimeoutError" || err.name === "AbortError");
|
|
111
|
+
return {
|
|
112
|
+
...base,
|
|
113
|
+
latency_ms: Date.now() - started,
|
|
114
|
+
// NEVER echo err.message — provider errors could theoretically carry
|
|
115
|
+
// request context; a fixed vocabulary keeps key material impossible.
|
|
116
|
+
detail: isTimeout ? "timeout" : "network_error"
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const latencyMs = Date.now() - started;
|
|
120
|
+
if (res.ok) {
|
|
121
|
+
const result = {
|
|
122
|
+
...base,
|
|
123
|
+
valid: true,
|
|
124
|
+
status: res.status,
|
|
125
|
+
latency_ms: latencyMs
|
|
126
|
+
};
|
|
127
|
+
if (spec.parseBalanceUsd) {
|
|
128
|
+
try {
|
|
129
|
+
const body = await res.json();
|
|
130
|
+
const balance = spec.parseBalanceUsd(body);
|
|
131
|
+
if (balance !== void 0) result.balance_usd = balance;
|
|
132
|
+
} catch {
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
if (res.status === 401 || res.status === 403) {
|
|
138
|
+
return { ...base, valid: false, status: res.status, latency_ms: latencyMs };
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
...base,
|
|
142
|
+
valid: null,
|
|
143
|
+
status: res.status,
|
|
144
|
+
latency_ms: latencyMs,
|
|
145
|
+
detail: `http_${res.status}`
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async function handle(req) {
|
|
149
|
+
try {
|
|
150
|
+
if (req.method !== "GET") {
|
|
151
|
+
return jsonResponse(405, { error: "method_not_allowed" });
|
|
152
|
+
}
|
|
153
|
+
if (bearerOf(req) !== ingestSecret) {
|
|
154
|
+
return jsonResponse(401, { error: "unauthorized" });
|
|
155
|
+
}
|
|
156
|
+
const env = config.env ?? process.env;
|
|
157
|
+
const settled = await Promise.allSettled(
|
|
158
|
+
PROBE_SPECS.map((spec) => probeProvider(spec, env))
|
|
159
|
+
);
|
|
160
|
+
const keys = settled.map((s, i) => {
|
|
161
|
+
if (s.status === "fulfilled") return s.value;
|
|
162
|
+
const spec = PROBE_SPECS[i];
|
|
163
|
+
return {
|
|
164
|
+
provider: spec.provider,
|
|
165
|
+
env: spec.canonicalEnvName,
|
|
166
|
+
present: true,
|
|
167
|
+
valid: null,
|
|
168
|
+
detail: "probe_failed"
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
keys.sort((a, b) => a.provider.localeCompare(b.provider));
|
|
172
|
+
const body = {
|
|
173
|
+
app_id: appId,
|
|
174
|
+
checked_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
175
|
+
library_version: LIBRARY_VERSION,
|
|
176
|
+
keys
|
|
177
|
+
};
|
|
178
|
+
return jsonResponse(200, body);
|
|
179
|
+
} catch (err) {
|
|
180
|
+
void err;
|
|
181
|
+
return jsonResponse(500, { error: "key_health_internal_error" });
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { handle };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export {
|
|
188
|
+
LIBRARY_VERSION,
|
|
189
|
+
createKeyHealthRoute
|
|
190
|
+
};
|