omnigateway 0.1.4 → 0.1.6
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 +35 -1
- package/bin/omni.js +263 -105
- package/gateway.js +983 -423
- package/package.json +1 -1
- package/public/assets/{CopyValue-BhGx7Gy0.js → CopyValue-cAwgC1sU.js} +1 -1
- package/public/assets/{Modal-DOQGdhoM.js → Modal-Dqir9wBz.js} +1 -1
- package/public/assets/{Rack-DnbGnBvz.js → Rack-XtfiZ6GC.js} +1 -1
- package/public/assets/{Toggle-CP3RvAAB.js → Toggle-BhViDMm9.js} +1 -1
- package/public/assets/{_app-D83powx4.js → _app-B8lqq_ks.js} +1 -1
- package/public/assets/{_app.accounts-DR-2t9-7.js → _app.accounts-DSMHSYBF.js} +1 -1
- package/public/assets/{_app.index-DimCB0Bo.js → _app.index-DXH5sH41.js} +1 -1
- package/public/assets/{_app.keys-B7kl3CSe.js → _app.keys-DdEyVy_b.js} +1 -1
- package/public/assets/_app.logs-Camxditb.js +33 -0
- package/public/assets/_app.models-CuD-JtGS.js +144 -0
- package/public/assets/{_app.settings-DtDIhAAr.js → _app.settings-DA1aYcR4.js} +1 -1
- package/public/assets/{_app.usage-BChnSBbS.js → _app.usage-DAyYUfIi.js} +1 -1
- package/public/assets/catalog-C_OQ0icG.js +1 -0
- package/public/assets/{index-C53PiRfb.js → index-BtjyVE1i.js} +2 -2
- package/public/assets/{login-BuvJY1Qd.js → login-DXHM-Noj.js} +1 -1
- package/public/assets/{trash-2-Cq7ldH9D.js → trash-2-JF_uX2gE.js} +1 -1
- package/public/index.html +2 -2
- package/public/assets/_app.logs-BjtdzcDY.js +0 -18
- package/public/assets/_app.models-BEECjcLM.js +0 -144
- package/public/assets/catalog-DVRPlBJJ.js +0 -1
package/gateway.js
CHANGED
|
@@ -5295,35 +5295,6 @@ function createAdminAuth(store, opts) {
|
|
|
5295
5295
|
}
|
|
5296
5296
|
};
|
|
5297
5297
|
}
|
|
5298
|
-
// packages/control/src/config.ts
|
|
5299
|
-
var MIN_KEY_LENGTH = 16;
|
|
5300
|
-
var DECIMAL_INTEGER = /^\d+$/;
|
|
5301
|
-
function optionalText(value, fallback) {
|
|
5302
|
-
return value?.trim() || fallback;
|
|
5303
|
-
}
|
|
5304
|
-
function loadConfig(env) {
|
|
5305
|
-
const encryptionKey = env.OMNI_ENCRYPTION_KEY;
|
|
5306
|
-
if (typeof encryptionKey !== "string" || encryptionKey.length < MIN_KEY_LENGTH) {
|
|
5307
|
-
throw new Error(`OMNI_ENCRYPTION_KEY must be set to at least ${MIN_KEY_LENGTH} characters. ` + "Generate one with: openssl rand -base64 32");
|
|
5308
|
-
}
|
|
5309
|
-
const host = optionalText(env.OMNI_HOST, "127.0.0.1");
|
|
5310
|
-
const rawPort = env.OMNI_PORT ?? "9000";
|
|
5311
|
-
const port = Number(rawPort);
|
|
5312
|
-
if (!DECIMAL_INTEGER.test(rawPort) || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
5313
|
-
throw new Error(`OMNI_PORT must be an integer between 1 and 65535, got "${rawPort}"`);
|
|
5314
|
-
}
|
|
5315
|
-
const derivedBaseUrl = `http://${host}:${port}`;
|
|
5316
|
-
const baseUrl = optionalText(env.OMNI_BASE_URL, derivedBaseUrl).replace(/\/+$/, "") || derivedBaseUrl;
|
|
5317
|
-
const staticDir = env.OMNI_STATIC_DIR?.trim();
|
|
5318
|
-
return {
|
|
5319
|
-
port,
|
|
5320
|
-
host,
|
|
5321
|
-
databasePath: optionalText(env.OMNI_DB_PATH, "./omnigateway.db"),
|
|
5322
|
-
encryptionKey,
|
|
5323
|
-
baseUrl,
|
|
5324
|
-
staticDir: staticDir === undefined || staticDir.length === 0 ? null : staticDir
|
|
5325
|
-
};
|
|
5326
|
-
}
|
|
5327
5298
|
// packages/ir/src/capabilities.ts
|
|
5328
5299
|
var PROVIDER_CAPABILITIES = {
|
|
5329
5300
|
anthropic: { tools: true, images: true, reasoning: true },
|
|
@@ -5380,7 +5351,117 @@ class GatewayError extends Error {
|
|
|
5380
5351
|
this.retryAfterMs = opts?.retryAfterMs;
|
|
5381
5352
|
}
|
|
5382
5353
|
}
|
|
5354
|
+
// packages/ir/src/logger.ts
|
|
5355
|
+
var LEVELS = { debug: 10, info: 20, warn: 30, error: 40 };
|
|
5356
|
+
var FIELD_ORDER = [
|
|
5357
|
+
"requestId",
|
|
5358
|
+
"surface",
|
|
5359
|
+
"status",
|
|
5360
|
+
"provider",
|
|
5361
|
+
"model",
|
|
5362
|
+
"requestedModel",
|
|
5363
|
+
"credentialId",
|
|
5364
|
+
"apiKeyId",
|
|
5365
|
+
"attempt",
|
|
5366
|
+
"attempts",
|
|
5367
|
+
"code",
|
|
5368
|
+
"retryable",
|
|
5369
|
+
"stream",
|
|
5370
|
+
"inputTokens",
|
|
5371
|
+
"outputTokens",
|
|
5372
|
+
"cacheReadTokens",
|
|
5373
|
+
"cacheWriteTokens",
|
|
5374
|
+
"costUsd",
|
|
5375
|
+
"ttftMs",
|
|
5376
|
+
"durationMs",
|
|
5377
|
+
"retryAfterMs",
|
|
5378
|
+
"count",
|
|
5379
|
+
"rawCount",
|
|
5380
|
+
"dailyCount",
|
|
5381
|
+
"host",
|
|
5382
|
+
"port",
|
|
5383
|
+
"path",
|
|
5384
|
+
"reason"
|
|
5385
|
+
];
|
|
5386
|
+
var REASON_MAX = 200;
|
|
5387
|
+
var LEVEL_COLOR = {
|
|
5388
|
+
debug: "\x1B[2;37m",
|
|
5389
|
+
info: "\x1B[36m",
|
|
5390
|
+
warn: "\x1B[33m",
|
|
5391
|
+
error: "\x1B[31m"
|
|
5392
|
+
};
|
|
5393
|
+
var RESET = "\x1B[0m";
|
|
5394
|
+
var NEEDS_QUOTING = /[\s="]/;
|
|
5395
|
+
function renderValue(value) {
|
|
5396
|
+
if (value === null)
|
|
5397
|
+
return "null";
|
|
5398
|
+
if (typeof value !== "string")
|
|
5399
|
+
return String(value);
|
|
5400
|
+
return NEEDS_QUOTING.test(value) ? JSON.stringify(value) : value;
|
|
5401
|
+
}
|
|
5402
|
+
function formatLine(level, at, msg, fields, color) {
|
|
5403
|
+
const label = level.toUpperCase().padEnd(5, " ");
|
|
5404
|
+
const head = `${new Date(at).toISOString()} ${color ? `${LEVEL_COLOR[level]}${label}${RESET}` : label} ${msg}`;
|
|
5405
|
+
if (fields === undefined)
|
|
5406
|
+
return head;
|
|
5407
|
+
const parts = [];
|
|
5408
|
+
for (const key of FIELD_ORDER) {
|
|
5409
|
+
const value = fields[key];
|
|
5410
|
+
if (value === undefined)
|
|
5411
|
+
continue;
|
|
5412
|
+
const capped = key === "reason" && typeof value === "string" && value.length > REASON_MAX ? `${value.slice(0, REASON_MAX)}\u2026` : value;
|
|
5413
|
+
parts.push(`${key}=${renderValue(capped)}`);
|
|
5414
|
+
}
|
|
5415
|
+
return parts.length === 0 ? head : `${head} ${parts.join(" ")}`;
|
|
5416
|
+
}
|
|
5417
|
+
function createLogger(opts) {
|
|
5418
|
+
const threshold = LEVELS[opts.level];
|
|
5419
|
+
const now = opts.now ?? (() => Date.now());
|
|
5420
|
+
const color = opts.color ?? false;
|
|
5421
|
+
const emit = (level, msg, fields) => {
|
|
5422
|
+
if (LEVELS[level] < threshold)
|
|
5423
|
+
return;
|
|
5424
|
+
try {
|
|
5425
|
+
opts.write(formatLine(level, now(), msg, fields, color));
|
|
5426
|
+
} catch {}
|
|
5427
|
+
};
|
|
5428
|
+
return {
|
|
5429
|
+
debug: (msg, fields) => emit("debug", msg, fields),
|
|
5430
|
+
info: (msg, fields) => emit("info", msg, fields),
|
|
5431
|
+
warn: (msg, fields) => emit("warn", msg, fields),
|
|
5432
|
+
error: (msg, fields) => emit("error", msg, fields),
|
|
5433
|
+
enabled: (level) => LEVELS[level] >= threshold
|
|
5434
|
+
};
|
|
5435
|
+
}
|
|
5436
|
+
var noopLogger = {
|
|
5437
|
+
debug: () => {},
|
|
5438
|
+
info: () => {},
|
|
5439
|
+
warn: () => {},
|
|
5440
|
+
error: () => {},
|
|
5441
|
+
enabled: () => false
|
|
5442
|
+
};
|
|
5443
|
+
function parseLogLevel(value) {
|
|
5444
|
+
const normalized = value?.trim().toLowerCase();
|
|
5445
|
+
if (normalized === undefined || normalized.length === 0)
|
|
5446
|
+
return null;
|
|
5447
|
+
return Object.hasOwn(LEVELS, normalized) ? normalized : null;
|
|
5448
|
+
}
|
|
5449
|
+
// packages/ir/src/request.ts
|
|
5450
|
+
function cacheControlOf(block) {
|
|
5451
|
+
return block.type === "thinking" ? undefined : block.cacheControl;
|
|
5452
|
+
}
|
|
5383
5453
|
// packages/ir/src/stream.ts
|
|
5454
|
+
function usageFromPromptTotal(promptTokens, outputTokens, cacheReadTokens, cacheWriteTokens = 0) {
|
|
5455
|
+
return {
|
|
5456
|
+
inputTokens: Math.max(0, promptTokens - cacheReadTokens - cacheWriteTokens),
|
|
5457
|
+
outputTokens,
|
|
5458
|
+
cacheReadTokens,
|
|
5459
|
+
cacheWriteTokens
|
|
5460
|
+
};
|
|
5461
|
+
}
|
|
5462
|
+
function promptTokens(usage) {
|
|
5463
|
+
return usage.inputTokens + usage.cacheReadTokens + usage.cacheWriteTokens;
|
|
5464
|
+
}
|
|
5384
5465
|
function collect(events) {
|
|
5385
5466
|
let id = "";
|
|
5386
5467
|
let model = "";
|
|
@@ -5473,6 +5554,39 @@ function validateRequest(req) {
|
|
|
5473
5554
|
}
|
|
5474
5555
|
return { ...req, messages: cleaned };
|
|
5475
5556
|
}
|
|
5557
|
+
// packages/control/src/config.ts
|
|
5558
|
+
var MIN_KEY_LENGTH = 16;
|
|
5559
|
+
var DECIMAL_INTEGER = /^\d+$/;
|
|
5560
|
+
function optionalText(value, fallback) {
|
|
5561
|
+
return value?.trim() || fallback;
|
|
5562
|
+
}
|
|
5563
|
+
function loadConfig(env) {
|
|
5564
|
+
const encryptionKey = env.OMNI_ENCRYPTION_KEY;
|
|
5565
|
+
if (typeof encryptionKey !== "string" || encryptionKey.length < MIN_KEY_LENGTH) {
|
|
5566
|
+
throw new Error(`OMNI_ENCRYPTION_KEY must be set to at least ${MIN_KEY_LENGTH} characters. ` + "Generate one with: openssl rand -base64 32");
|
|
5567
|
+
}
|
|
5568
|
+
const host = optionalText(env.OMNI_HOST, "127.0.0.1");
|
|
5569
|
+
const rawPort = env.OMNI_PORT ?? "9000";
|
|
5570
|
+
const port = Number(rawPort);
|
|
5571
|
+
if (!DECIMAL_INTEGER.test(rawPort) || !Number.isInteger(port) || port < 1 || port > 65535) {
|
|
5572
|
+
throw new Error(`OMNI_PORT must be an integer between 1 and 65535, got "${rawPort}"`);
|
|
5573
|
+
}
|
|
5574
|
+
const derivedBaseUrl = `http://${host}:${port}`;
|
|
5575
|
+
const baseUrl = optionalText(env.OMNI_BASE_URL, derivedBaseUrl).replace(/\/+$/, "") || derivedBaseUrl;
|
|
5576
|
+
const staticDir = env.OMNI_STATIC_DIR?.trim();
|
|
5577
|
+
const rawLogLevel = env.OMNI_LOG_LEVEL?.trim();
|
|
5578
|
+
const logLevel = parseLogLevel(rawLogLevel);
|
|
5579
|
+
return {
|
|
5580
|
+
logLevel: logLevel ?? "info",
|
|
5581
|
+
logLevelFallbackFrom: logLevel === null && rawLogLevel ? rawLogLevel : null,
|
|
5582
|
+
port,
|
|
5583
|
+
host,
|
|
5584
|
+
databasePath: optionalText(env.OMNI_DB_PATH, "./omnigateway.db"),
|
|
5585
|
+
encryptionKey,
|
|
5586
|
+
baseUrl,
|
|
5587
|
+
staticDir: staticDir === undefined || staticDir.length === 0 ? null : staticDir
|
|
5588
|
+
};
|
|
5589
|
+
}
|
|
5476
5590
|
// packages/providers/src/body.ts
|
|
5477
5591
|
var BODY_ORDER = {
|
|
5478
5592
|
anthropic: [
|
|
@@ -5559,8 +5673,13 @@ function applyAnthropicSystem(system) {
|
|
|
5559
5673
|
let rewritten = text;
|
|
5560
5674
|
for (const [from, to] of REWRITES)
|
|
5561
5675
|
rewritten = rewritten.replaceAll(from, to);
|
|
5562
|
-
if (rewritten.trim().length > 0)
|
|
5563
|
-
kept.push({
|
|
5676
|
+
if (rewritten.trim().length > 0) {
|
|
5677
|
+
kept.push({
|
|
5678
|
+
type: "text",
|
|
5679
|
+
text: rewritten,
|
|
5680
|
+
...block.cache_control === undefined ? {} : { cache_control: block.cache_control }
|
|
5681
|
+
});
|
|
5682
|
+
}
|
|
5564
5683
|
}
|
|
5565
5684
|
return [{ type: "text", text: billingBlock() }, { type: "text", text: AGENT_PREAMBLE }, ...kept];
|
|
5566
5685
|
}
|
|
@@ -5864,6 +5983,8 @@ async function* decodeAnthropic(messages) {
|
|
|
5864
5983
|
let inputTokens = 0;
|
|
5865
5984
|
let cacheReadTokens = 0;
|
|
5866
5985
|
let cacheWriteTokens = 0;
|
|
5986
|
+
let cacheWrite5mTokens;
|
|
5987
|
+
let cacheWrite1hTokens;
|
|
5867
5988
|
let outputTokens = 0;
|
|
5868
5989
|
let stopReason = "endTurn";
|
|
5869
5990
|
let terminal = false;
|
|
@@ -5877,6 +5998,8 @@ async function* decodeAnthropic(messages) {
|
|
|
5877
5998
|
inputTokens = m.usage?.input_tokens ?? 0;
|
|
5878
5999
|
cacheReadTokens = m.usage?.cache_read_input_tokens ?? 0;
|
|
5879
6000
|
cacheWriteTokens = m.usage?.cache_creation_input_tokens ?? 0;
|
|
6001
|
+
cacheWrite5mTokens = m.usage?.cache_creation?.ephemeral_5m_input_tokens;
|
|
6002
|
+
cacheWrite1hTokens = m.usage?.cache_creation?.ephemeral_1h_input_tokens;
|
|
5880
6003
|
yield { type: "start", id: String(m.id ?? ""), model: String(m.model ?? "") };
|
|
5881
6004
|
break;
|
|
5882
6005
|
}
|
|
@@ -5936,7 +6059,14 @@ async function* decodeAnthropic(messages) {
|
|
|
5936
6059
|
yield {
|
|
5937
6060
|
type: "end",
|
|
5938
6061
|
stopReason,
|
|
5939
|
-
usage: {
|
|
6062
|
+
usage: {
|
|
6063
|
+
inputTokens,
|
|
6064
|
+
outputTokens,
|
|
6065
|
+
cacheReadTokens,
|
|
6066
|
+
cacheWriteTokens,
|
|
6067
|
+
...cacheWrite5mTokens === undefined ? {} : { cacheWrite5mTokens },
|
|
6068
|
+
...cacheWrite1hTokens === undefined ? {} : { cacheWrite1hTokens }
|
|
6069
|
+
}
|
|
5940
6070
|
};
|
|
5941
6071
|
break;
|
|
5942
6072
|
case "error": {
|
|
@@ -5966,25 +6096,33 @@ async function* decodeAnthropic(messages) {
|
|
|
5966
6096
|
|
|
5967
6097
|
// packages/providers/src/anthropic/wire.ts
|
|
5968
6098
|
var OAUTH_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude.";
|
|
6099
|
+
function wireCacheControl(c) {
|
|
6100
|
+
if (c === undefined)
|
|
6101
|
+
return {};
|
|
6102
|
+
return { cache_control: { type: c.type, ...c.ttl === undefined ? {} : { ttl: c.ttl } } };
|
|
6103
|
+
}
|
|
5969
6104
|
function encodeBlock(b) {
|
|
6105
|
+
const cache = wireCacheControl(cacheControlOf(b));
|
|
5970
6106
|
switch (b.type) {
|
|
5971
6107
|
case "text":
|
|
5972
|
-
return { type: "text", text: b.text };
|
|
6108
|
+
return { type: "text", text: b.text, ...cache };
|
|
5973
6109
|
case "image":
|
|
5974
6110
|
return {
|
|
5975
6111
|
type: "image",
|
|
5976
|
-
source: { type: "base64", media_type: b.mediaType, data: b.data }
|
|
6112
|
+
source: { type: "base64", media_type: b.mediaType, data: b.data },
|
|
6113
|
+
...cache
|
|
5977
6114
|
};
|
|
5978
6115
|
case "thinking":
|
|
5979
6116
|
return { type: "thinking", thinking: b.text, signature: b.signature };
|
|
5980
6117
|
case "toolUse":
|
|
5981
|
-
return { type: "tool_use", id: b.id, name: b.name, input: b.input };
|
|
6118
|
+
return { type: "tool_use", id: b.id, name: b.name, input: b.input, ...cache };
|
|
5982
6119
|
case "toolResult":
|
|
5983
6120
|
return {
|
|
5984
6121
|
type: "tool_result",
|
|
5985
6122
|
tool_use_id: b.toolUseId,
|
|
5986
6123
|
content: b.content,
|
|
5987
|
-
is_error: b.isError
|
|
6124
|
+
is_error: b.isError,
|
|
6125
|
+
...cache
|
|
5988
6126
|
};
|
|
5989
6127
|
}
|
|
5990
6128
|
}
|
|
@@ -6006,14 +6144,25 @@ function encodeToolChoice(c) {
|
|
|
6006
6144
|
}
|
|
6007
6145
|
function toWire(req, model, opts) {
|
|
6008
6146
|
const degradations = [];
|
|
6009
|
-
|
|
6147
|
+
const note = (d) => {
|
|
6148
|
+
if (!degradations.includes(d))
|
|
6149
|
+
degradations.push(d);
|
|
6150
|
+
};
|
|
6151
|
+
let system = req.system?.flatMap((b) => b.type === "text" ? [{ type: "text", text: b.text, ...wireCacheControl(b.cacheControl) }] : []);
|
|
6010
6152
|
if (opts.oauth && system?.[0]?.text !== OAUTH_IDENTITY) {
|
|
6011
6153
|
system = [{ type: "text", text: OAUTH_IDENTITY }, ...system ?? []];
|
|
6012
|
-
|
|
6154
|
+
note("anthropic:oauth-system-prefix");
|
|
6013
6155
|
}
|
|
6014
6156
|
const body = {
|
|
6015
6157
|
model,
|
|
6016
|
-
messages: req.messages.map((m) =>
|
|
6158
|
+
messages: req.messages.map((m) => {
|
|
6159
|
+
if (m.role !== "system")
|
|
6160
|
+
return { role: m.role, content: m.content.map(encodeBlock) };
|
|
6161
|
+
if (m.content.some((b) => cacheControlOf(b) !== undefined)) {
|
|
6162
|
+
note("anthropic:system-turn-cache-control-dropped");
|
|
6163
|
+
}
|
|
6164
|
+
return { role: m.role, content: encodeSystemTurn(m.content) };
|
|
6165
|
+
}),
|
|
6017
6166
|
max_tokens: req.maxTokens ?? 4096,
|
|
6018
6167
|
stream: req.stream
|
|
6019
6168
|
};
|
|
@@ -6027,7 +6176,8 @@ function toWire(req, model, opts) {
|
|
|
6027
6176
|
body.tools = req.tools.map((t) => ({
|
|
6028
6177
|
name: t.name,
|
|
6029
6178
|
...t.description === undefined ? {} : { description: t.description },
|
|
6030
|
-
input_schema: t.inputSchema
|
|
6179
|
+
input_schema: t.inputSchema,
|
|
6180
|
+
...wireCacheControl(t.cacheControl)
|
|
6031
6181
|
}));
|
|
6032
6182
|
}
|
|
6033
6183
|
if (req.toolChoice !== undefined)
|
|
@@ -6067,12 +6217,13 @@ var anthropicAdapter = {
|
|
|
6067
6217
|
const { body, degradations } = toWire(req.request, req.model, { oauth });
|
|
6068
6218
|
const withSystem = {
|
|
6069
6219
|
...body,
|
|
6070
|
-
system: applyAnthropicSystem(body.system ?? [])
|
|
6220
|
+
system: applyAnthropicSystem(body.system ?? []),
|
|
6221
|
+
stream: true
|
|
6071
6222
|
};
|
|
6072
6223
|
const protocol = [
|
|
6073
6224
|
["Content-Type", "application/json"],
|
|
6074
6225
|
["anthropic-version", API_VERSION],
|
|
6075
|
-
["Accept",
|
|
6226
|
+
["Accept", "text/event-stream"]
|
|
6076
6227
|
];
|
|
6077
6228
|
const betas = new Set(req.request.betas ?? []);
|
|
6078
6229
|
if (oauth) {
|
|
@@ -6091,6 +6242,7 @@ var anthropicAdapter = {
|
|
|
6091
6242
|
const headers = orderHeaders(mergeHeaders(profile.headers, protocol), profile.order);
|
|
6092
6243
|
const bodyString = signAnthropicBody(JSON.stringify(orderFields(withSystem, BODY_ORDER.anthropic)));
|
|
6093
6244
|
const res = await req.http({
|
|
6245
|
+
provider: "anthropic",
|
|
6094
6246
|
url: BASE_URL,
|
|
6095
6247
|
method: "POST",
|
|
6096
6248
|
headers,
|
|
@@ -6111,22 +6263,26 @@ var ANTHROPIC_MODELS = {
|
|
|
6111
6263
|
{
|
|
6112
6264
|
id: "claude-fable-5",
|
|
6113
6265
|
label: "Claude Fable 5",
|
|
6114
|
-
pricing: { input: 10, output: 50, cacheRead: 1 }
|
|
6266
|
+
pricing: { input: 10, output: 50, cacheRead: 1, cacheWrite5m: 12.5, cacheWrite1h: 20 },
|
|
6267
|
+
limits: { contextWindow: 1e6, maxOutputTokens: 128000 }
|
|
6115
6268
|
},
|
|
6116
6269
|
{
|
|
6117
6270
|
id: "claude-opus-5",
|
|
6118
6271
|
label: "Claude Opus 5",
|
|
6119
|
-
pricing: { input: 5, output: 25, cacheRead: 0.5 }
|
|
6272
|
+
pricing: { input: 5, output: 25, cacheRead: 0.5, cacheWrite5m: 6.25, cacheWrite1h: 10 },
|
|
6273
|
+
limits: { contextWindow: 1e6, maxOutputTokens: 128000 }
|
|
6120
6274
|
},
|
|
6121
6275
|
{
|
|
6122
6276
|
id: "claude-sonnet-5",
|
|
6123
6277
|
label: "Claude Sonnet 5",
|
|
6124
|
-
pricing: { input: 3, output: 15, cacheRead: 0.3 }
|
|
6278
|
+
pricing: { input: 3, output: 15, cacheRead: 0.3, cacheWrite5m: 3.75, cacheWrite1h: 6 },
|
|
6279
|
+
limits: { contextWindow: 1e6, maxOutputTokens: 128000 }
|
|
6125
6280
|
},
|
|
6126
6281
|
{
|
|
6127
6282
|
id: "claude-haiku-4-5",
|
|
6128
6283
|
label: "Claude Haiku 4.5",
|
|
6129
|
-
pricing: { input: 1, output: 5, cacheRead: 0.1 }
|
|
6284
|
+
pricing: { input: 1, output: 5, cacheRead: 0.1, cacheWrite5m: 1.25, cacheWrite1h: 2 },
|
|
6285
|
+
limits: { contextWindow: 200000, maxOutputTokens: 64000 }
|
|
6130
6286
|
}
|
|
6131
6287
|
]
|
|
6132
6288
|
};
|
|
@@ -6138,22 +6294,26 @@ var KIMI_MODELS = {
|
|
|
6138
6294
|
{
|
|
6139
6295
|
id: "k3-256k",
|
|
6140
6296
|
label: "Kimi K3 \u2014 256K",
|
|
6141
|
-
pricing: { input: 3, output: 15, cacheRead: 0.3 }
|
|
6297
|
+
pricing: { input: 3, output: 15, cacheRead: 0.3, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6298
|
+
limits: { contextWindow: 262144, maxOutputTokens: 131072 }
|
|
6142
6299
|
},
|
|
6143
6300
|
{
|
|
6144
6301
|
id: "k3",
|
|
6145
6302
|
label: "Kimi K3 \u2014 up to 1M",
|
|
6146
|
-
pricing: { input: 3, output: 15, cacheRead: 0.3 }
|
|
6303
|
+
pricing: { input: 3, output: 15, cacheRead: 0.3, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6304
|
+
limits: { contextWindow: 1048576, maxOutputTokens: 131072 }
|
|
6147
6305
|
},
|
|
6148
6306
|
{
|
|
6149
6307
|
id: "kimi-for-coding",
|
|
6150
6308
|
label: "Kimi K2.7 Code",
|
|
6151
|
-
pricing: { input: 0.95, output: 4, cacheRead: 0.19 }
|
|
6309
|
+
pricing: { input: 0.95, output: 4, cacheRead: 0.19, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6310
|
+
limits: { contextWindow: 262144, maxOutputTokens: 131072 }
|
|
6152
6311
|
},
|
|
6153
6312
|
{
|
|
6154
6313
|
id: "kimi-for-coding-highspeed",
|
|
6155
6314
|
label: "Kimi K2.7 Code \u2014 High Speed",
|
|
6156
|
-
pricing: { input: 0.95, output: 8, cacheRead: 0.19 }
|
|
6315
|
+
pricing: { input: 0.95, output: 8, cacheRead: 0.19, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6316
|
+
limits: { contextWindow: 262144, maxOutputTokens: 131072 }
|
|
6157
6317
|
}
|
|
6158
6318
|
]
|
|
6159
6319
|
};
|
|
@@ -6165,22 +6325,30 @@ var OPENAI_MODELS = {
|
|
|
6165
6325
|
{
|
|
6166
6326
|
id: "gpt-5.6",
|
|
6167
6327
|
label: "GPT-5.6 \u2014 routes to Sol",
|
|
6168
|
-
pricing: { input: 5, output: 30, cacheRead: 0.5 }
|
|
6328
|
+
pricing: { input: 5, output: 30, cacheRead: 0.5, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6329
|
+
limits: { contextWindow: 922000, maxOutputTokens: 128000 },
|
|
6330
|
+
oauthLimits: { contextWindow: 272000, maxOutputTokens: 128000 }
|
|
6169
6331
|
},
|
|
6170
6332
|
{
|
|
6171
6333
|
id: "gpt-5.6-sol",
|
|
6172
6334
|
label: "GPT-5.6 Sol \u2014 deepest reasoning",
|
|
6173
|
-
pricing: { input: 5, output: 30, cacheRead: 0.5 }
|
|
6335
|
+
pricing: { input: 5, output: 30, cacheRead: 0.5, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6336
|
+
limits: { contextWindow: 922000, maxOutputTokens: 128000 },
|
|
6337
|
+
oauthLimits: { contextWindow: 272000, maxOutputTokens: 128000 }
|
|
6174
6338
|
},
|
|
6175
6339
|
{
|
|
6176
6340
|
id: "gpt-5.6-terra",
|
|
6177
6341
|
label: "GPT-5.6 Terra \u2014 balanced",
|
|
6178
|
-
pricing: { input: 2, output: 12, cacheRead: 0.2 }
|
|
6342
|
+
pricing: { input: 2, output: 12, cacheRead: 0.2, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6343
|
+
limits: { contextWindow: 922000, maxOutputTokens: 128000 },
|
|
6344
|
+
oauthLimits: { contextWindow: 272000, maxOutputTokens: 128000 }
|
|
6179
6345
|
},
|
|
6180
6346
|
{
|
|
6181
6347
|
id: "gpt-5.6-luna",
|
|
6182
6348
|
label: "GPT-5.6 Luna \u2014 fastest",
|
|
6183
|
-
pricing: { input: 0.2, output: 1.2, cacheRead: 0.02 }
|
|
6349
|
+
pricing: { input: 0.2, output: 1.2, cacheRead: 0.02, cacheWrite5m: 0, cacheWrite1h: 0 },
|
|
6350
|
+
limits: { contextWindow: 922000, maxOutputTokens: 128000 },
|
|
6351
|
+
oauthLimits: { contextWindow: 272000, maxOutputTokens: 128000 }
|
|
6184
6352
|
}
|
|
6185
6353
|
]
|
|
6186
6354
|
};
|
|
@@ -6192,15 +6360,38 @@ var PROVIDER_MODEL_CATALOG = {
|
|
|
6192
6360
|
kimi: KIMI_MODELS
|
|
6193
6361
|
};
|
|
6194
6362
|
function catalogPricing(provider, model) {
|
|
6195
|
-
return PROVIDER_MODEL_CATALOG[provider]
|
|
6363
|
+
return PROVIDER_MODEL_CATALOG[provider]?.models.find((entry) => entry.id === model)?.pricing ?? null;
|
|
6364
|
+
}
|
|
6365
|
+
function catalogLimits(provider, model, auth = "apiKey") {
|
|
6366
|
+
const entry = PROVIDER_MODEL_CATALOG[provider]?.models.find((choice) => choice.id === model);
|
|
6367
|
+
if (entry === undefined)
|
|
6368
|
+
return null;
|
|
6369
|
+
return (auth === "oauth" ? entry.oauthLimits : undefined) ?? entry.limits;
|
|
6196
6370
|
}
|
|
6197
6371
|
// packages/providers/src/http-client.ts
|
|
6198
6372
|
import { request as httpRequest } from "http";
|
|
6199
6373
|
import { request as httpsRequest } from "https";
|
|
6200
6374
|
import { Readable } from "stream";
|
|
6201
|
-
function nodeHttpClient() {
|
|
6375
|
+
function nodeHttpClient(options = {}) {
|
|
6376
|
+
const logger2 = options.logger ?? noopLogger;
|
|
6377
|
+
const now = options.now ?? (() => Date.now());
|
|
6202
6378
|
return (req) => new Promise((resolve, reject) => {
|
|
6203
6379
|
const url = new URL(req.url);
|
|
6380
|
+
const startedAt = now();
|
|
6381
|
+
let traced = false;
|
|
6382
|
+
const trace = (status, failed = false) => {
|
|
6383
|
+
if (traced || !logger2.enabled("debug"))
|
|
6384
|
+
return;
|
|
6385
|
+
traced = true;
|
|
6386
|
+
logger2.debug("upstream http", {
|
|
6387
|
+
provider: req.provider,
|
|
6388
|
+
status,
|
|
6389
|
+
host: url.host,
|
|
6390
|
+
path: url.pathname,
|
|
6391
|
+
durationMs: now() - startedAt,
|
|
6392
|
+
reason: failed ? "transport error" : undefined
|
|
6393
|
+
});
|
|
6394
|
+
};
|
|
6204
6395
|
const send = url.protocol === "https:" ? httpsRequest : httpRequest;
|
|
6205
6396
|
const bodyBytes = Buffer.from(req.body, "utf8");
|
|
6206
6397
|
const headers = {};
|
|
@@ -6228,6 +6419,7 @@ function nodeHttpClient() {
|
|
|
6228
6419
|
else if (typeof v === "string")
|
|
6229
6420
|
responseHeaders.set(k, v);
|
|
6230
6421
|
}
|
|
6422
|
+
trace(incoming.statusCode);
|
|
6231
6423
|
resolve({
|
|
6232
6424
|
status: incoming.statusCode ?? 0,
|
|
6233
6425
|
headers: responseHeaders,
|
|
@@ -6243,17 +6435,17 @@ function nodeHttpClient() {
|
|
|
6243
6435
|
});
|
|
6244
6436
|
});
|
|
6245
6437
|
const onAbort = () => outgoing.destroy(new Error("aborted"));
|
|
6246
|
-
if (req.signal.aborted) {
|
|
6247
|
-
outgoing.destroy(new Error("aborted"));
|
|
6248
|
-
reject(new Error("aborted"));
|
|
6249
|
-
return;
|
|
6250
|
-
}
|
|
6251
|
-
req.signal.addEventListener("abort", onAbort, { once: true });
|
|
6252
6438
|
outgoing.on("error", (err) => {
|
|
6253
6439
|
req.signal.removeEventListener("abort", onAbort);
|
|
6440
|
+
trace(undefined, true);
|
|
6254
6441
|
reject(err);
|
|
6255
6442
|
});
|
|
6256
6443
|
outgoing.on("close", () => req.signal.removeEventListener("abort", onAbort));
|
|
6444
|
+
if (req.signal.aborted) {
|
|
6445
|
+
outgoing.destroy(new Error("aborted"));
|
|
6446
|
+
return;
|
|
6447
|
+
}
|
|
6448
|
+
req.signal.addEventListener("abort", onAbort, { once: true });
|
|
6257
6449
|
if (bodyBytes.byteLength > 0)
|
|
6258
6450
|
outgoing.write(bodyBytes);
|
|
6259
6451
|
outgoing.end();
|
|
@@ -6322,12 +6514,8 @@ async function* decodeChat(messages) {
|
|
|
6322
6514
|
yield { type: "start", id: String(d.id ?? ""), model: String(d.model ?? "") };
|
|
6323
6515
|
}
|
|
6324
6516
|
if (d.usage) {
|
|
6325
|
-
|
|
6326
|
-
|
|
6327
|
-
outputTokens: d.usage.completion_tokens ?? 0,
|
|
6328
|
-
cacheReadTokens: d.usage.prompt_cache_hit_tokens ?? 0,
|
|
6329
|
-
cacheWriteTokens: 0
|
|
6330
|
-
};
|
|
6517
|
+
const details = d.usage.prompt_tokens_details;
|
|
6518
|
+
usage = usageFromPromptTotal(d.usage.prompt_tokens ?? 0, d.usage.completion_tokens ?? 0, details?.cached_tokens ?? d.usage.prompt_cache_hit_tokens ?? 0, details?.cache_creation_tokens ?? details?.cache_write_tokens ?? 0);
|
|
6331
6519
|
}
|
|
6332
6520
|
const choice = d.choices?.[0];
|
|
6333
6521
|
if (!choice)
|
|
@@ -6453,7 +6641,12 @@ function toChatWire(req, model) {
|
|
|
6453
6641
|
`) });
|
|
6454
6642
|
}
|
|
6455
6643
|
}
|
|
6456
|
-
const body = {
|
|
6644
|
+
const body = {
|
|
6645
|
+
model,
|
|
6646
|
+
messages,
|
|
6647
|
+
stream: req.stream,
|
|
6648
|
+
stream_options: { include_usage: true }
|
|
6649
|
+
};
|
|
6457
6650
|
if (req.maxTokens !== undefined)
|
|
6458
6651
|
body.max_tokens = req.maxTokens;
|
|
6459
6652
|
if (req.temperature !== undefined)
|
|
@@ -6494,6 +6687,7 @@ var kimiAdapter = {
|
|
|
6494
6687
|
const profile = PROFILES.kimi;
|
|
6495
6688
|
const headers = orderHeaders(mergeHeaders(profile.headers, protocol), profile.order);
|
|
6496
6689
|
const res = await req.http({
|
|
6690
|
+
provider: "kimi",
|
|
6497
6691
|
url: BASE_URL2,
|
|
6498
6692
|
method: "POST",
|
|
6499
6693
|
headers,
|
|
@@ -6624,12 +6818,7 @@ async function* decodeResponses(messages) {
|
|
|
6624
6818
|
yield {
|
|
6625
6819
|
type: "end",
|
|
6626
6820
|
stopReason,
|
|
6627
|
-
usage:
|
|
6628
|
-
inputTokens: r.usage?.input_tokens ?? 0,
|
|
6629
|
-
outputTokens: r.usage?.output_tokens ?? 0,
|
|
6630
|
-
cacheReadTokens: r.usage?.input_tokens_details?.cached_tokens ?? 0,
|
|
6631
|
-
cacheWriteTokens: 0
|
|
6632
|
-
}
|
|
6821
|
+
usage: usageFromPromptTotal(r.usage?.input_tokens ?? 0, r.usage?.output_tokens ?? 0, r.usage?.input_tokens_details?.cached_tokens ?? r.usage?.prompt_tokens_details?.cached_tokens ?? 0)
|
|
6633
6822
|
};
|
|
6634
6823
|
break;
|
|
6635
6824
|
}
|
|
@@ -6802,6 +6991,7 @@ var openaiAdapter = {
|
|
|
6802
6991
|
const profile = PROFILES.openai;
|
|
6803
6992
|
const headers = orderHeaders(mergeHeaders(profile.headers, protocol), profile.order);
|
|
6804
6993
|
const res = await req.http({
|
|
6994
|
+
provider: "openai",
|
|
6805
6995
|
url: oauth ? OAUTH_URL : API_URL,
|
|
6806
6996
|
method: "POST",
|
|
6807
6997
|
headers,
|
|
@@ -6822,13 +7012,14 @@ var ADAPTERS = {
|
|
|
6822
7012
|
kimi: kimiAdapter
|
|
6823
7013
|
};
|
|
6824
7014
|
// packages/control/src/oauth/types.ts
|
|
6825
|
-
async function postJson(deps, url, profile2, opts) {
|
|
7015
|
+
async function postJson(deps, provider, url, profile2, opts) {
|
|
6826
7016
|
const headers = orderHeaders(mergeHeaders(profile2.headers, [
|
|
6827
7017
|
["Content-Type", opts.contentType],
|
|
6828
7018
|
["Accept", "application/json"],
|
|
6829
7019
|
...opts.extraHeaders ?? []
|
|
6830
7020
|
]), profile2.order);
|
|
6831
7021
|
const res = await deps.http({
|
|
7022
|
+
provider,
|
|
6832
7023
|
url,
|
|
6833
7024
|
method: "POST",
|
|
6834
7025
|
headers,
|
|
@@ -6842,13 +7033,14 @@ async function postJson(deps, url, profile2, opts) {
|
|
|
6842
7033
|
} catch {}
|
|
6843
7034
|
return { status: res.status, parsed };
|
|
6844
7035
|
}
|
|
6845
|
-
async function getJson(deps, url, profile2, opts) {
|
|
7036
|
+
async function getJson(deps, provider, url, profile2, opts) {
|
|
6846
7037
|
const headers = orderHeaders(mergeHeaders(profile2.headers, [
|
|
6847
7038
|
["Authorization", `Bearer ${opts.accessToken}`],
|
|
6848
7039
|
["Accept", "application/json"],
|
|
6849
7040
|
...opts.extraHeaders ?? []
|
|
6850
7041
|
]), profile2.order);
|
|
6851
7042
|
const res = await deps.http({
|
|
7043
|
+
provider,
|
|
6852
7044
|
url,
|
|
6853
7045
|
method: "GET",
|
|
6854
7046
|
headers,
|
|
@@ -7042,7 +7234,7 @@ function deviceCodeFrom(value) {
|
|
|
7042
7234
|
};
|
|
7043
7235
|
}
|
|
7044
7236
|
async function post(url, body2, device, deps) {
|
|
7045
|
-
const { status, parsed } = await postJson(deps, url, PROFILES.kimi, {
|
|
7237
|
+
const { status, parsed } = await postJson(deps, "kimi", url, PROFILES.kimi, {
|
|
7046
7238
|
contentType: "application/x-www-form-urlencoded",
|
|
7047
7239
|
body: new URLSearchParams({ ...body2, client_id: CLIENT_ID }).toString(),
|
|
7048
7240
|
extraHeaders: kimiDeviceHeaders(device)
|
|
@@ -7145,7 +7337,7 @@ var kimiOAuth = {
|
|
|
7145
7337
|
if (secrets.accessToken === null)
|
|
7146
7338
|
return null;
|
|
7147
7339
|
const device = deviceFrom(providerData);
|
|
7148
|
-
const { status, parsed } = await getJson(deps, USAGE_URL, PROFILES.kimi, {
|
|
7340
|
+
const { status, parsed } = await getJson(deps, "kimi", USAGE_URL, PROFILES.kimi, {
|
|
7149
7341
|
accessToken: secrets.accessToken,
|
|
7150
7342
|
extraHeaders: kimiDeviceHeaders(device)
|
|
7151
7343
|
});
|
|
@@ -7214,6 +7406,7 @@ function deviceIdFrom(start) {
|
|
|
7214
7406
|
return deviceId;
|
|
7215
7407
|
}
|
|
7216
7408
|
function createConnectFlows(deps) {
|
|
7409
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
7217
7410
|
const flows = createPendingFlows({ now: deps.now, ttlMs: FLOW_TTL_MS });
|
|
7218
7411
|
const pollsInFlight = new Map;
|
|
7219
7412
|
const callbackUri = (provider) => provider === "openai" ? "http://localhost:1455/auth/callback" : "";
|
|
@@ -7258,6 +7451,10 @@ function createConnectFlows(deps) {
|
|
|
7258
7451
|
disabledAt: null,
|
|
7259
7452
|
...result.secrets
|
|
7260
7453
|
});
|
|
7454
|
+
logger2.info("oauth connect completed", {
|
|
7455
|
+
provider: flow.provider,
|
|
7456
|
+
credentialId: id
|
|
7457
|
+
});
|
|
7261
7458
|
return { id };
|
|
7262
7459
|
}
|
|
7263
7460
|
async function pollOutcome(flowId, poll) {
|
|
@@ -21633,28 +21830,18 @@ var modelSchema = exports_external.object({
|
|
|
21633
21830
|
costPerMTok: exports_external.object({
|
|
21634
21831
|
input: exports_external.number().min(0),
|
|
21635
21832
|
output: exports_external.number().min(0),
|
|
21636
|
-
cacheRead: exports_external.number().min(0)
|
|
21833
|
+
cacheRead: exports_external.number().min(0).optional(),
|
|
21834
|
+
cacheWrite5m: exports_external.number().min(0).optional(),
|
|
21835
|
+
cacheWrite1h: exports_external.number().min(0).optional()
|
|
21637
21836
|
}),
|
|
21837
|
+
contextWindow: exports_external.number().int().positive().optional(),
|
|
21838
|
+
maxOutputTokens: exports_external.number().int().positive().optional(),
|
|
21638
21839
|
capabilities: exports_external.object({
|
|
21639
21840
|
tools: exports_external.boolean(),
|
|
21640
21841
|
images: exports_external.boolean(),
|
|
21641
21842
|
reasoning: exports_external.boolean()
|
|
21642
21843
|
})
|
|
21643
|
-
}).
|
|
21644
|
-
provider: providerIdSchema,
|
|
21645
|
-
model: exports_external.string().min(1),
|
|
21646
|
-
tier: exports_external.number().int().min(1),
|
|
21647
|
-
weight: exports_external.number().positive(),
|
|
21648
|
-
costPerMTok: exports_external.object({
|
|
21649
|
-
input: exports_external.number().min(0),
|
|
21650
|
-
output: exports_external.number().min(0)
|
|
21651
|
-
}),
|
|
21652
|
-
capabilities: exports_external.object({
|
|
21653
|
-
tools: exports_external.boolean(),
|
|
21654
|
-
images: exports_external.boolean(),
|
|
21655
|
-
reasoning: exports_external.boolean()
|
|
21656
|
-
})
|
|
21657
|
-
}))).min(1, "a virtual model needs at least one target")
|
|
21844
|
+
})).min(1, "a virtual model needs at least one target")
|
|
21658
21845
|
});
|
|
21659
21846
|
var keyCreateSchema = exports_external.object({
|
|
21660
21847
|
label: exports_external.string().min(1).default("api key"),
|
|
@@ -21748,6 +21935,12 @@ async function patchCredential(deps, id, input) {
|
|
|
21748
21935
|
...patch.tier === undefined ? {} : { tier: patch.tier },
|
|
21749
21936
|
...patch.weight === undefined ? {} : { weight: patch.weight }
|
|
21750
21937
|
});
|
|
21938
|
+
if (patch.enabled !== undefined && patch.enabled !== existing.enabled) {
|
|
21939
|
+
(deps.logger ?? noopLogger).info(patch.enabled ? "credential enabled" : "credential disabled", {
|
|
21940
|
+
credentialId: id,
|
|
21941
|
+
provider: existing.provider
|
|
21942
|
+
});
|
|
21943
|
+
}
|
|
21751
21944
|
}
|
|
21752
21945
|
async function removeCredential(store, id) {
|
|
21753
21946
|
await store.credentials.remove(id);
|
|
@@ -22009,12 +22202,20 @@ var PREFIX_PROVIDER = [
|
|
|
22009
22202
|
];
|
|
22010
22203
|
function synthesize(provider, model) {
|
|
22011
22204
|
const listed = catalogPricing(provider, model);
|
|
22205
|
+
const limits = catalogLimits(provider, model);
|
|
22012
22206
|
const target = {
|
|
22013
22207
|
provider,
|
|
22014
22208
|
model,
|
|
22015
22209
|
tier: 1,
|
|
22016
22210
|
weight: 1,
|
|
22017
|
-
costPerMTok: listed === null ? { input: 0, output: 0 } : {
|
|
22211
|
+
costPerMTok: listed === null ? { input: 0, output: 0 } : {
|
|
22212
|
+
input: listed.input,
|
|
22213
|
+
output: listed.output,
|
|
22214
|
+
cacheRead: listed.cacheRead,
|
|
22215
|
+
cacheWrite5m: listed.cacheWrite5m,
|
|
22216
|
+
cacheWrite1h: listed.cacheWrite1h
|
|
22217
|
+
},
|
|
22218
|
+
...limits === null ? {} : { contextWindow: limits.contextWindow, maxOutputTokens: limits.maxOutputTokens },
|
|
22018
22219
|
capabilities: PROVIDER_CAPABILITIES[provider]
|
|
22019
22220
|
};
|
|
22020
22221
|
return { id: `${provider}/${model}`, targets: [target], strategy: "score", isAlias: true };
|
|
@@ -23017,7 +23218,9 @@ function createUsageRepo(db) {
|
|
|
23017
23218
|
|
|
23018
23219
|
// packages/store/src/sqlite/store.ts
|
|
23019
23220
|
async function createStore(opts) {
|
|
23221
|
+
const logger2 = opts.logger ?? noopLogger;
|
|
23020
23222
|
const db = openDb(opts.path);
|
|
23223
|
+
logger2.debug("store opened", { path: opts.path });
|
|
23021
23224
|
const listeners = new Set;
|
|
23022
23225
|
const emit = (change) => {
|
|
23023
23226
|
for (const listener of listeners) {
|
|
@@ -23104,7 +23307,7 @@ var REDIRECT_URI = "https://platform.claude.com/oauth/code/callback";
|
|
|
23104
23307
|
var SCOPES = "org:create_api_key user:profile user:inference";
|
|
23105
23308
|
var USAGE_URL2 = "https://api.anthropic.com/api/oauth/usage";
|
|
23106
23309
|
async function postToken(body2, deps) {
|
|
23107
|
-
const { status, parsed } = await postJson(deps, TOKEN_URL2, PROFILES.anthropic, {
|
|
23310
|
+
const { status, parsed } = await postJson(deps, "anthropic", TOKEN_URL2, PROFILES.anthropic, {
|
|
23108
23311
|
contentType: "application/json",
|
|
23109
23312
|
body: JSON.stringify({ ...body2, client_id: CLIENT_ID2 })
|
|
23110
23313
|
});
|
|
@@ -23182,7 +23385,7 @@ var anthropicOAuth = {
|
|
|
23182
23385
|
async usage(secrets, deps) {
|
|
23183
23386
|
if (secrets.accessToken === null)
|
|
23184
23387
|
return null;
|
|
23185
|
-
const { status, parsed } = await getJson(deps, USAGE_URL2, PROFILES.anthropic, {
|
|
23388
|
+
const { status, parsed } = await getJson(deps, "anthropic", USAGE_URL2, PROFILES.anthropic, {
|
|
23186
23389
|
accessToken: secrets.accessToken,
|
|
23187
23390
|
extraHeaders: [
|
|
23188
23391
|
["anthropic-beta", "oauth-2025-04-20"],
|
|
@@ -23240,7 +23443,7 @@ function parseTokenResponse(value) {
|
|
|
23240
23443
|
};
|
|
23241
23444
|
}
|
|
23242
23445
|
async function postToken2(body2, deps) {
|
|
23243
|
-
const { status, parsed } = await postJson(deps, TOKEN_URL3, PROFILES.openai, {
|
|
23446
|
+
const { status, parsed } = await postJson(deps, "openai", TOKEN_URL3, PROFILES.openai, {
|
|
23244
23447
|
contentType: "application/x-www-form-urlencoded",
|
|
23245
23448
|
body: new URLSearchParams({ ...body2, client_id: CLIENT_ID3 }).toString()
|
|
23246
23449
|
});
|
|
@@ -23339,7 +23542,7 @@ var openaiOAuth = {
|
|
|
23339
23542
|
if (secrets.accessToken === null)
|
|
23340
23543
|
return null;
|
|
23341
23544
|
const accountId = accountIdFromProviderData(providerData);
|
|
23342
|
-
const { status, parsed } = await getJson(deps, USAGE_URL3, PROFILES.openai, {
|
|
23545
|
+
const { status, parsed } = await getJson(deps, "openai", USAGE_URL3, PROFILES.openai, {
|
|
23343
23546
|
accessToken: secrets.accessToken,
|
|
23344
23547
|
...accountId === null ? {} : { extraHeaders: [["chatgpt-account-id", accountId]] }
|
|
23345
23548
|
});
|
|
@@ -23360,6 +23563,7 @@ var DISPATCH_REFRESH_LEAD_MS = 120000;
|
|
|
23360
23563
|
var SCHEDULER_REFRESH_LEAD_MS = 300000;
|
|
23361
23564
|
// packages/control/src/oauth/refresh.ts
|
|
23362
23565
|
function createRefresher(deps) {
|
|
23566
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
23363
23567
|
const inFlight = new Map;
|
|
23364
23568
|
async function run(credential) {
|
|
23365
23569
|
const secrets = await credential.openForRefresh();
|
|
@@ -23367,11 +23571,20 @@ function createRefresher(deps) {
|
|
|
23367
23571
|
throw new GatewayError("AUTH", `credential ${credential.id} has no refresh token`);
|
|
23368
23572
|
}
|
|
23369
23573
|
const provider = deps.providers[credential.provider];
|
|
23574
|
+
logger2.debug("refreshing credential", {
|
|
23575
|
+
provider: credential.provider,
|
|
23576
|
+
credentialId: credential.id
|
|
23577
|
+
});
|
|
23370
23578
|
let result;
|
|
23371
23579
|
try {
|
|
23372
23580
|
result = await provider.refresh(secrets.refreshToken, { http: deps.http, now: deps.now }, credential.providerData);
|
|
23373
23581
|
} catch (error51) {
|
|
23374
23582
|
if (error51 instanceof GatewayError && error51.code === "AUTH") {
|
|
23583
|
+
logger2.warn("credential disabled: refresh token rejected", {
|
|
23584
|
+
provider: credential.provider,
|
|
23585
|
+
credentialId: credential.id,
|
|
23586
|
+
code: "AUTH"
|
|
23587
|
+
});
|
|
23375
23588
|
await deps.store.credentials.update(credential.id, {
|
|
23376
23589
|
enabled: false,
|
|
23377
23590
|
disabledReason: "tokenRejected",
|
|
@@ -23385,6 +23598,10 @@ function createRefresher(deps) {
|
|
|
23385
23598
|
accountEmail: result.accountEmail ?? credential.accountEmail,
|
|
23386
23599
|
providerData: { ...credential.providerData, ...result.providerData }
|
|
23387
23600
|
});
|
|
23601
|
+
logger2.debug("credential refreshed", {
|
|
23602
|
+
provider: credential.provider,
|
|
23603
|
+
credentialId: credential.id
|
|
23604
|
+
});
|
|
23388
23605
|
return result.secrets;
|
|
23389
23606
|
}
|
|
23390
23607
|
return function refresh(credential) {
|
|
@@ -23424,9 +23641,15 @@ async function probe(deps, credential) {
|
|
|
23424
23641
|
observedAt
|
|
23425
23642
|
}));
|
|
23426
23643
|
await deps.store.credentials.saveQuota(rows);
|
|
23644
|
+
(deps.logger ?? noopLogger).debug("quota snapshot written", {
|
|
23645
|
+
provider: credential.provider,
|
|
23646
|
+
credentialId: credential.id,
|
|
23647
|
+
count: rows.length
|
|
23648
|
+
});
|
|
23427
23649
|
return rows;
|
|
23428
23650
|
}
|
|
23429
23651
|
async function poll(deps) {
|
|
23652
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
23430
23653
|
const now = deps.now();
|
|
23431
23654
|
const credentials = (await deps.store.credentials.list()).filter((c) => c.enabled && c.authType === "oauth" && deps.providers[c.provider].usage !== undefined && (cooldowns.get(c.id) ?? 0) <= now);
|
|
23432
23655
|
let written = 0;
|
|
@@ -23440,12 +23663,15 @@ async function poll(deps) {
|
|
|
23440
23663
|
if (await probe(deps, credential) !== null)
|
|
23441
23664
|
written += 1;
|
|
23442
23665
|
} catch (error51) {
|
|
23443
|
-
|
|
23666
|
+
const rateLimited = error51 instanceof GatewayError && error51.code === "RATE_LIMIT";
|
|
23667
|
+
if (rateLimited) {
|
|
23444
23668
|
cooldowns.set(credential.id, deps.now() + RATE_LIMIT_COOLDOWN_MS);
|
|
23445
23669
|
}
|
|
23446
|
-
|
|
23447
|
-
credentialId: credential.id,
|
|
23670
|
+
logger2.warn(rateLimited ? "quota probe rate limited" : "quota probe failed", {
|
|
23448
23671
|
provider: credential.provider,
|
|
23672
|
+
credentialId: credential.id,
|
|
23673
|
+
code: error51 instanceof GatewayError ? error51.code : "INTERNAL",
|
|
23674
|
+
...rateLimited ? { retryAfterMs: RATE_LIMIT_COOLDOWN_MS } : {},
|
|
23449
23675
|
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
23450
23676
|
});
|
|
23451
23677
|
}
|
|
@@ -37903,7 +38129,7 @@ class ApiKeyRateLimiter {
|
|
|
37903
38129
|
}
|
|
37904
38130
|
|
|
37905
38131
|
// apps/gateway/src/dispatch/snapshotCache.ts
|
|
37906
|
-
function createRoutingSnapshotCache(store) {
|
|
38132
|
+
function createRoutingSnapshotCache(store, logger2 = noopLogger) {
|
|
37907
38133
|
let snapshot = null;
|
|
37908
38134
|
let stale = true;
|
|
37909
38135
|
let version2 = store.routing.version();
|
|
@@ -37942,6 +38168,7 @@ function createRoutingSnapshotCache(store) {
|
|
|
37942
38168
|
return inFlight;
|
|
37943
38169
|
const buildGeneration = generation;
|
|
37944
38170
|
const buildVersion = currentVersion;
|
|
38171
|
+
logger2.debug("routing snapshot cache miss");
|
|
37945
38172
|
inFlight = buildSnapshot(store, now).then(async (next) => {
|
|
37946
38173
|
const completedVersion = store.routing.version();
|
|
37947
38174
|
if (generation !== buildGeneration || completedVersion !== buildVersion) {
|
|
@@ -37961,6 +38188,28 @@ function createRoutingSnapshotCache(store) {
|
|
|
37961
38188
|
}
|
|
37962
38189
|
|
|
37963
38190
|
// apps/gateway/src/ingress/schemas.ts
|
|
38191
|
+
var cacheControlSchema = exports_external.object({
|
|
38192
|
+
type: exports_external.literal("ephemeral"),
|
|
38193
|
+
ttl: exports_external.enum(["5m", "1h"]).optional()
|
|
38194
|
+
});
|
|
38195
|
+
function looseCacheControl(value) {
|
|
38196
|
+
const parsed = cacheControlSchema.safeParse(value);
|
|
38197
|
+
return parsed.success ? irCacheControl(parsed.data) : {};
|
|
38198
|
+
}
|
|
38199
|
+
function irCacheControl(c) {
|
|
38200
|
+
if (c === undefined)
|
|
38201
|
+
return {};
|
|
38202
|
+
return { cacheControl: { type: c.type, ...c.ttl === undefined ? {} : { ttl: c.ttl } } };
|
|
38203
|
+
}
|
|
38204
|
+
function applyMessageCacheControl(blocks, value) {
|
|
38205
|
+
const control = looseCacheControl(value);
|
|
38206
|
+
if (control.cacheControl === undefined)
|
|
38207
|
+
return;
|
|
38208
|
+
const last = blocks.at(-1);
|
|
38209
|
+
if (last === undefined || last.type === "thinking" || cacheControlOf(last) !== undefined)
|
|
38210
|
+
return;
|
|
38211
|
+
Object.assign(last, control);
|
|
38212
|
+
}
|
|
37964
38213
|
function parseOrThrow2(schema, body2) {
|
|
37965
38214
|
const result = schema.safeParse(body2);
|
|
37966
38215
|
if (result.success)
|
|
@@ -37988,7 +38237,18 @@ function parseDataUrl(url2) {
|
|
|
37988
38237
|
return { mediaType: match[1], data: match[2] };
|
|
37989
38238
|
}
|
|
37990
38239
|
|
|
37991
|
-
// apps/gateway/src/routes/
|
|
38240
|
+
// apps/gateway/src/routes/http.ts
|
|
38241
|
+
function readCookie(request2, name) {
|
|
38242
|
+
const header = request2.headers.get("cookie");
|
|
38243
|
+
if (header === null)
|
|
38244
|
+
return null;
|
|
38245
|
+
for (const part of header.split(";")) {
|
|
38246
|
+
const [key, ...rest] = part.trim().split("=");
|
|
38247
|
+
if (key === name)
|
|
38248
|
+
return rest.join("=");
|
|
38249
|
+
}
|
|
38250
|
+
return null;
|
|
38251
|
+
}
|
|
37992
38252
|
function sessionCookie(request2, token, maxAge) {
|
|
37993
38253
|
const secure = new URL(request2.url).protocol === "https:" ? ["Secure"] : [];
|
|
37994
38254
|
return [
|
|
@@ -38000,38 +38260,37 @@ function sessionCookie(request2, token, maxAge) {
|
|
|
38000
38260
|
`Max-Age=${maxAge}`
|
|
38001
38261
|
].join("; ");
|
|
38002
38262
|
}
|
|
38003
|
-
function
|
|
38004
|
-
const
|
|
38005
|
-
if (
|
|
38006
|
-
|
|
38007
|
-
for (const part of header.split(";")) {
|
|
38008
|
-
const [key, ...rest] = part.trim().split("=");
|
|
38009
|
-
if (key === name)
|
|
38010
|
-
return rest.join("=");
|
|
38263
|
+
async function requireAdmin(request2, admin) {
|
|
38264
|
+
const token = readCookie(request2, ADMIN_COOKIE);
|
|
38265
|
+
if (token === null || !await admin.verify(token)) {
|
|
38266
|
+
throw new GatewayError("AUTH", "admin session required");
|
|
38011
38267
|
}
|
|
38012
|
-
return null;
|
|
38013
38268
|
}
|
|
38014
|
-
async function
|
|
38269
|
+
async function readJson(request2) {
|
|
38015
38270
|
try {
|
|
38016
|
-
|
|
38017
|
-
|
|
38018
|
-
|
|
38019
|
-
|
|
38271
|
+
return await request2.json();
|
|
38272
|
+
} catch (error51) {
|
|
38273
|
+
if (!(error51 instanceof SyntaxError))
|
|
38274
|
+
throw error51;
|
|
38275
|
+
throw new GatewayError("BAD_REQUEST", "invalid JSON body");
|
|
38020
38276
|
}
|
|
38021
38277
|
}
|
|
38278
|
+
async function readJsonRecord(request2) {
|
|
38279
|
+
const body2 = await readJson(request2);
|
|
38280
|
+
return isRecord3(body2) ? body2 : null;
|
|
38281
|
+
}
|
|
38282
|
+
function apiErrorResponse(error51) {
|
|
38283
|
+
const gatewayError = error51 instanceof GatewayError ? error51 : new GatewayError("INTERNAL", "internal error");
|
|
38284
|
+
return Response.json({ error: { code: gatewayError.code, message: gatewayError.message } }, { status: HTTP_STATUS[gatewayError.code] });
|
|
38285
|
+
}
|
|
38286
|
+
function apiErrorHandler(input) {
|
|
38287
|
+
return input.code === "NOT_FOUND" ? undefined : apiErrorResponse(input.error);
|
|
38288
|
+
}
|
|
38289
|
+
|
|
38290
|
+
// apps/gateway/src/routes/admin.ts
|
|
38022
38291
|
function adminRoutes(deps) {
|
|
38023
|
-
const
|
|
38024
|
-
|
|
38025
|
-
set2.status = HTTP_STATUS[gatewayError.code];
|
|
38026
|
-
return { error: { code: gatewayError.code, message: gatewayError.message } };
|
|
38027
|
-
});
|
|
38028
|
-
async function requireAdmin(request2) {
|
|
38029
|
-
const token = readCookie(request2, ADMIN_COOKIE);
|
|
38030
|
-
if (token === null || !await deps.admin.verify(token)) {
|
|
38031
|
-
throw new GatewayError("AUTH", "admin session required");
|
|
38032
|
-
}
|
|
38033
|
-
}
|
|
38034
|
-
return app.get("/api/status", async ({ request: request2 }) => {
|
|
38292
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
38293
|
+
return new Elysia().onError(apiErrorHandler).get("/api/status", async ({ request: request2 }) => {
|
|
38035
38294
|
const token = readCookie(request2, ADMIN_COOKIE);
|
|
38036
38295
|
return {
|
|
38037
38296
|
configured: await deps.admin.isConfigured(),
|
|
@@ -38042,7 +38301,7 @@ function adminRoutes(deps) {
|
|
|
38042
38301
|
set2.status = 409;
|
|
38043
38302
|
return { error: { code: "CONFLICT", message: "an admin password is already configured" } };
|
|
38044
38303
|
}
|
|
38045
|
-
const body2 = await
|
|
38304
|
+
const body2 = await readJsonRecord(request2);
|
|
38046
38305
|
if (typeof body2?.password !== "string") {
|
|
38047
38306
|
throw new GatewayError("BAD_REQUEST", "password is required");
|
|
38048
38307
|
}
|
|
@@ -38059,13 +38318,17 @@ function adminRoutes(deps) {
|
|
|
38059
38318
|
const token = await deps.admin.login(body2.password);
|
|
38060
38319
|
if (token === null)
|
|
38061
38320
|
throw new GatewayError("INTERNAL", "could not create admin session");
|
|
38321
|
+
logger2.info("admin setup completed");
|
|
38062
38322
|
set2.headers["set-cookie"] = sessionCookie(request2, token, Math.floor(deps.sessionTtlMs / 1000));
|
|
38063
38323
|
return { ok: true };
|
|
38064
38324
|
}).post("/api/login", async ({ request: request2, set: set2 }) => {
|
|
38065
|
-
const body2 = await
|
|
38325
|
+
const body2 = await readJsonRecord(request2);
|
|
38066
38326
|
const token = typeof body2?.password === "string" ? await deps.admin.login(body2.password) : null;
|
|
38067
|
-
if (token === null)
|
|
38327
|
+
if (token === null) {
|
|
38328
|
+
logger2.info("admin login failed", { reason: "invalid credentials" });
|
|
38068
38329
|
throw new GatewayError("AUTH", "invalid password");
|
|
38330
|
+
}
|
|
38331
|
+
logger2.info("admin login succeeded");
|
|
38069
38332
|
set2.headers["set-cookie"] = sessionCookie(request2, token, Math.floor(deps.sessionTtlMs / 1000));
|
|
38070
38333
|
return { ok: true };
|
|
38071
38334
|
}).post("/api/logout", async ({ request: request2, set: set2 }) => {
|
|
@@ -38075,52 +38338,52 @@ function adminRoutes(deps) {
|
|
|
38075
38338
|
set2.headers["set-cookie"] = sessionCookie(request2, "", 0);
|
|
38076
38339
|
return { ok: true };
|
|
38077
38340
|
}).get("/api/credentials", async ({ request: request2 }) => {
|
|
38078
|
-
await requireAdmin(request2);
|
|
38341
|
+
await requireAdmin(request2, deps.admin);
|
|
38079
38342
|
return { credentials: await listCredentials(deps.store) };
|
|
38080
38343
|
}).get("/api/credentials/health", async ({ request: request2 }) => {
|
|
38081
|
-
await requireAdmin(request2);
|
|
38344
|
+
await requireAdmin(request2, deps.admin);
|
|
38082
38345
|
return credentialHealth(deps.store);
|
|
38083
38346
|
}).patch("/api/credentials/:id", async ({ request: request2, params }) => {
|
|
38084
|
-
await requireAdmin(request2);
|
|
38085
|
-
await patchCredential(deps, params.id, await request2
|
|
38347
|
+
await requireAdmin(request2, deps.admin);
|
|
38348
|
+
await patchCredential(deps, params.id, await readJson(request2));
|
|
38086
38349
|
return { ok: true };
|
|
38087
38350
|
}).delete("/api/credentials/:id", async ({ request: request2, params }) => {
|
|
38088
|
-
await requireAdmin(request2);
|
|
38351
|
+
await requireAdmin(request2, deps.admin);
|
|
38089
38352
|
await removeCredential(deps.store, params.id);
|
|
38090
38353
|
return { ok: true };
|
|
38091
38354
|
}).get("/api/models", async ({ request: request2 }) => {
|
|
38092
|
-
await requireAdmin(request2);
|
|
38355
|
+
await requireAdmin(request2, deps.admin);
|
|
38093
38356
|
return { models: await listModels(deps.store) };
|
|
38094
38357
|
}).put("/api/models/:id", async ({ request: request2, params }) => {
|
|
38095
|
-
await requireAdmin(request2);
|
|
38096
|
-
await putModel(deps.store, params.id, await request2
|
|
38358
|
+
await requireAdmin(request2, deps.admin);
|
|
38359
|
+
await putModel(deps.store, params.id, await readJson(request2));
|
|
38097
38360
|
return { ok: true };
|
|
38098
38361
|
}).delete("/api/models/:id", async ({ request: request2, params }) => {
|
|
38099
|
-
await requireAdmin(request2);
|
|
38362
|
+
await requireAdmin(request2, deps.admin);
|
|
38100
38363
|
await removeModel(deps.store, params.id);
|
|
38101
38364
|
return { ok: true };
|
|
38102
38365
|
}).get("/api/keys", async ({ request: request2 }) => {
|
|
38103
|
-
await requireAdmin(request2);
|
|
38366
|
+
await requireAdmin(request2, deps.admin);
|
|
38104
38367
|
return { keys: await listKeys(deps.store) };
|
|
38105
38368
|
}).post("/api/keys", async ({ request: request2 }) => {
|
|
38106
|
-
await requireAdmin(request2);
|
|
38107
|
-
return createKey(deps.store, await request2
|
|
38369
|
+
await requireAdmin(request2, deps.admin);
|
|
38370
|
+
return createKey(deps.store, await readJson(request2));
|
|
38108
38371
|
}).delete("/api/keys/:id", async ({ request: request2, params }) => {
|
|
38109
|
-
await requireAdmin(request2);
|
|
38372
|
+
await requireAdmin(request2, deps.admin);
|
|
38110
38373
|
await revokeKey(deps.store, params.id);
|
|
38111
38374
|
return { ok: true };
|
|
38112
38375
|
}).get("/api/settings", async ({ request: request2 }) => {
|
|
38113
|
-
await requireAdmin(request2);
|
|
38376
|
+
await requireAdmin(request2, deps.admin);
|
|
38114
38377
|
return { settings: await getSettings(deps.store) };
|
|
38115
38378
|
}).put("/api/settings", async ({ request: request2 }) => {
|
|
38116
|
-
await requireAdmin(request2);
|
|
38117
|
-
await putSettings(deps.store, await request2
|
|
38379
|
+
await requireAdmin(request2, deps.admin);
|
|
38380
|
+
await putSettings(deps.store, await readJson(request2));
|
|
38118
38381
|
return { ok: true };
|
|
38119
38382
|
}).post("/api/models/:id/dry-run", async ({ request: request2, params }) => {
|
|
38120
|
-
await requireAdmin(request2);
|
|
38121
|
-
return dryRun(deps, params.id, await request2
|
|
38383
|
+
await requireAdmin(request2, deps.admin);
|
|
38384
|
+
return dryRun(deps, params.id, await readJson(request2));
|
|
38122
38385
|
}).get("/api/usage", async ({ request: request2, query }) => {
|
|
38123
|
-
await requireAdmin(request2);
|
|
38386
|
+
await requireAdmin(request2, deps.admin);
|
|
38124
38387
|
return {
|
|
38125
38388
|
rows: await queryUsage(deps, {
|
|
38126
38389
|
grain: query.grain,
|
|
@@ -38131,70 +38394,29 @@ function adminRoutes(deps) {
|
|
|
38131
38394
|
})
|
|
38132
38395
|
};
|
|
38133
38396
|
}).get("/api/logs", async ({ request: request2, query }) => {
|
|
38134
|
-
await requireAdmin(request2);
|
|
38397
|
+
await requireAdmin(request2, deps.admin);
|
|
38135
38398
|
return { logs: await recentLogs(deps.store, query.limit) };
|
|
38136
38399
|
});
|
|
38137
38400
|
}
|
|
38138
38401
|
|
|
38139
38402
|
// apps/gateway/src/routes/connect.ts
|
|
38140
|
-
function asGatewayError(error51) {
|
|
38141
|
-
if (error51 instanceof GatewayError)
|
|
38142
|
-
return error51;
|
|
38143
|
-
return new GatewayError("INTERNAL", error51 instanceof Error ? error51.message : "internal error");
|
|
38144
|
-
}
|
|
38145
|
-
function errorResponse(error51) {
|
|
38146
|
-
const gatewayError = asGatewayError(error51);
|
|
38147
|
-
return new Response(JSON.stringify({ error: { code: gatewayError.code, message: gatewayError.message } }), {
|
|
38148
|
-
status: HTTP_STATUS[gatewayError.code],
|
|
38149
|
-
headers: { "content-type": "application/json" }
|
|
38150
|
-
});
|
|
38151
|
-
}
|
|
38152
|
-
function readCookie2(request2, name) {
|
|
38153
|
-
const header = request2.headers.get("cookie");
|
|
38154
|
-
if (header === null)
|
|
38155
|
-
return null;
|
|
38156
|
-
for (const part of header.split(";")) {
|
|
38157
|
-
const [key, ...rest] = part.trim().split("=");
|
|
38158
|
-
if (key === name)
|
|
38159
|
-
return rest.join("=");
|
|
38160
|
-
}
|
|
38161
|
-
return null;
|
|
38162
|
-
}
|
|
38163
38403
|
function connectRoutes(deps) {
|
|
38164
38404
|
const flows = createConnectFlows(deps);
|
|
38165
|
-
async
|
|
38166
|
-
|
|
38167
|
-
|
|
38168
|
-
|
|
38169
|
-
}
|
|
38170
|
-
}
|
|
38171
|
-
return new Elysia().post("/api/connect/start", async ({ request: request2 }) => {
|
|
38172
|
-
try {
|
|
38173
|
-
await requireAdmin(request2);
|
|
38174
|
-
const body2 = await request2.json();
|
|
38175
|
-
return await flows.start(isRecord3(body2) ? body2.provider : undefined, isRecord3(body2) ? body2.label : undefined);
|
|
38176
|
-
} catch (error51) {
|
|
38177
|
-
return errorResponse(error51);
|
|
38178
|
-
}
|
|
38405
|
+
return new Elysia().onError(apiErrorHandler).post("/api/connect/start", async ({ request: request2 }) => {
|
|
38406
|
+
await requireAdmin(request2, deps.admin);
|
|
38407
|
+
const body2 = await readJsonRecord(request2);
|
|
38408
|
+
return flows.start(body2?.provider, body2?.label);
|
|
38179
38409
|
}).post("/api/connect/finish", async ({ request: request2 }) => {
|
|
38180
|
-
|
|
38181
|
-
|
|
38182
|
-
|
|
38183
|
-
return await flows.finish(isRecord3(body2) ? body2.flowId : undefined, isRecord3(body2) ? body2.code : undefined);
|
|
38184
|
-
} catch (error51) {
|
|
38185
|
-
return errorResponse(error51);
|
|
38186
|
-
}
|
|
38410
|
+
await requireAdmin(request2, deps.admin);
|
|
38411
|
+
const body2 = await readJsonRecord(request2);
|
|
38412
|
+
return flows.finish(body2?.flowId, body2?.code);
|
|
38187
38413
|
}).post("/api/connect/poll", async ({ request: request2, set: set2 }) => {
|
|
38188
|
-
|
|
38189
|
-
|
|
38190
|
-
|
|
38191
|
-
|
|
38192
|
-
|
|
38193
|
-
|
|
38194
|
-
return outcome;
|
|
38195
|
-
} catch (error51) {
|
|
38196
|
-
return errorResponse(error51);
|
|
38197
|
-
}
|
|
38414
|
+
await requireAdmin(request2, deps.admin);
|
|
38415
|
+
const body2 = await readJsonRecord(request2);
|
|
38416
|
+
const outcome = await flows.poll(body2?.flowId);
|
|
38417
|
+
if (outcome.status === "pending")
|
|
38418
|
+
set2.status = 202;
|
|
38419
|
+
return outcome;
|
|
38198
38420
|
});
|
|
38199
38421
|
}
|
|
38200
38422
|
|
|
@@ -38233,13 +38455,86 @@ function extractToken(header) {
|
|
|
38233
38455
|
return match === null ? value : match[1].trim();
|
|
38234
38456
|
}
|
|
38235
38457
|
|
|
38458
|
+
// apps/gateway/src/logging.ts
|
|
38459
|
+
function requestLogDefaults(id, at) {
|
|
38460
|
+
return {
|
|
38461
|
+
id,
|
|
38462
|
+
state: "done",
|
|
38463
|
+
at,
|
|
38464
|
+
apiKeyId: null,
|
|
38465
|
+
requestedModel: "",
|
|
38466
|
+
resolvedProvider: null,
|
|
38467
|
+
resolvedModel: null,
|
|
38468
|
+
credentialId: null,
|
|
38469
|
+
attempts: 0,
|
|
38470
|
+
status: 0,
|
|
38471
|
+
errorCode: null,
|
|
38472
|
+
inputTokens: 0,
|
|
38473
|
+
outputTokens: 0,
|
|
38474
|
+
cacheReadTokens: 0,
|
|
38475
|
+
cacheWriteTokens: 0,
|
|
38476
|
+
ttftMs: null,
|
|
38477
|
+
durationMs: 0,
|
|
38478
|
+
costUsd: 0,
|
|
38479
|
+
degradations: []
|
|
38480
|
+
};
|
|
38481
|
+
}
|
|
38482
|
+
function newCompletedRequestLog(id, at, overrides) {
|
|
38483
|
+
return { ...requestLogDefaults(id, at), ...overrides, id, state: "done", at };
|
|
38484
|
+
}
|
|
38485
|
+
function newPendingRequestLog(input) {
|
|
38486
|
+
const { id, at, requestedModel, resolvedProvider, resolvedModel, credentialId } = input;
|
|
38487
|
+
return {
|
|
38488
|
+
...requestLogDefaults(id, at),
|
|
38489
|
+
requestedModel,
|
|
38490
|
+
resolvedProvider,
|
|
38491
|
+
resolvedModel,
|
|
38492
|
+
credentialId,
|
|
38493
|
+
state: "pending",
|
|
38494
|
+
status: 0
|
|
38495
|
+
};
|
|
38496
|
+
}
|
|
38497
|
+
function report(logger2, what, requestId, error51) {
|
|
38498
|
+
logger2.warn(what, {
|
|
38499
|
+
requestId,
|
|
38500
|
+
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
38501
|
+
});
|
|
38502
|
+
}
|
|
38503
|
+
async function beginLog(store, log, keyId, logger2 = noopLogger) {
|
|
38504
|
+
try {
|
|
38505
|
+
await store.usage.begin({ ...log, state: "pending", apiKeyId: keyId });
|
|
38506
|
+
} catch (error51) {
|
|
38507
|
+
report(logger2, "failed to record request start", log.id, error51);
|
|
38508
|
+
}
|
|
38509
|
+
}
|
|
38510
|
+
async function routeLog(store, requestId, target, logger2 = noopLogger) {
|
|
38511
|
+
try {
|
|
38512
|
+
await store.usage.route(requestId, target);
|
|
38513
|
+
} catch (error51) {
|
|
38514
|
+
report(logger2, "failed to record request route", requestId, error51);
|
|
38515
|
+
}
|
|
38516
|
+
}
|
|
38517
|
+
async function finishLog(store, log, keyId, logger2 = noopLogger) {
|
|
38518
|
+
try {
|
|
38519
|
+
await store.usage.append({ ...log, apiKeyId: keyId });
|
|
38520
|
+
} catch (error51) {
|
|
38521
|
+
report(logger2, "failed to persist request log", log.id, error51);
|
|
38522
|
+
}
|
|
38523
|
+
}
|
|
38524
|
+
|
|
38236
38525
|
// apps/gateway/src/dispatch/attempt.ts
|
|
38237
38526
|
async function attempt(opts) {
|
|
38238
38527
|
const { candidate, adapter, http, now, signal, refresh, refreshLeadMs } = opts;
|
|
38528
|
+
const logger2 = opts.logger ?? noopLogger;
|
|
38239
38529
|
const credential = candidate.credential;
|
|
38240
38530
|
let secrets = opts.secrets ?? await credential.openForInference();
|
|
38241
38531
|
const stale = opts.secrets === undefined && credential.authType === "oauth" && credential.expiresAt !== null && credential.expiresAt - refreshLeadMs <= now;
|
|
38242
38532
|
if (stale) {
|
|
38533
|
+
logger2.debug("preemptive credential refresh", {
|
|
38534
|
+
requestId: opts.requestId,
|
|
38535
|
+
provider: credential.provider,
|
|
38536
|
+
credentialId: credential.id
|
|
38537
|
+
});
|
|
38243
38538
|
if (!credential.hasRefreshToken) {
|
|
38244
38539
|
throw new GatewayError("AUTH", "credential expired with no refresh token", {
|
|
38245
38540
|
provider: credential.provider
|
|
@@ -38285,32 +38580,42 @@ function classify(error51) {
|
|
|
38285
38580
|
return { code: "INTERNAL" };
|
|
38286
38581
|
}
|
|
38287
38582
|
|
|
38583
|
+
// apps/gateway/src/dispatch/price.ts
|
|
38584
|
+
var WRITE_OVER_INPUT = {
|
|
38585
|
+
anthropic: { fiveMinute: 1.25, oneHour: 2 },
|
|
38586
|
+
openai: { fiveMinute: 0, oneHour: 0 },
|
|
38587
|
+
kimi: { fiveMinute: 0, oneHour: 0 }
|
|
38588
|
+
};
|
|
38589
|
+
var READ_OVER_INPUT = 0.1;
|
|
38590
|
+
function splitWrites(usage) {
|
|
38591
|
+
const { cacheWrite5mTokens: five, cacheWrite1hTokens: hour, cacheWriteTokens: total } = usage;
|
|
38592
|
+
if (five !== undefined && hour !== undefined)
|
|
38593
|
+
return { fiveMinute: five, oneHour: hour };
|
|
38594
|
+
if (five !== undefined)
|
|
38595
|
+
return { fiveMinute: five, oneHour: Math.max(0, total - five) };
|
|
38596
|
+
if (hour !== undefined)
|
|
38597
|
+
return { fiveMinute: Math.max(0, total - hour), oneHour: hour };
|
|
38598
|
+
return { fiveMinute: total, oneHour: 0 };
|
|
38599
|
+
}
|
|
38600
|
+
function priceOf(prices, usage, provider) {
|
|
38601
|
+
const fallback = WRITE_OVER_INPUT[provider];
|
|
38602
|
+
const readRate = prices.cacheRead ?? prices.input * READ_OVER_INPUT;
|
|
38603
|
+
const write5mRate = prices.cacheWrite5m ?? prices.input * fallback.fiveMinute;
|
|
38604
|
+
const write1hRate = prices.cacheWrite1h ?? prices.input * fallback.oneHour;
|
|
38605
|
+
const writes = splitWrites(usage);
|
|
38606
|
+
return (usage.inputTokens * prices.input + usage.outputTokens * prices.output + usage.cacheReadTokens * readRate + writes.fiveMinute * write5mRate + writes.oneHour * write1hRate) / 1e6;
|
|
38607
|
+
}
|
|
38608
|
+
|
|
38288
38609
|
// apps/gateway/src/dispatch/index.ts
|
|
38289
|
-
async function dispatch(request2, deps, signal) {
|
|
38610
|
+
async function dispatch(request2, deps, signal, requestId) {
|
|
38611
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
38290
38612
|
const startedAt = deps.now();
|
|
38291
38613
|
const snapshot = await deps.snapshots.get(startedAt);
|
|
38292
38614
|
const deadlineAt = startedAt + snapshot.settings.requestDeadlineMs;
|
|
38293
|
-
const log = {
|
|
38294
|
-
id: crypto.randomUUID(),
|
|
38295
|
-
state: "done",
|
|
38296
|
-
at: startedAt,
|
|
38297
|
-
apiKeyId: null,
|
|
38615
|
+
const log = newCompletedRequestLog(requestId, startedAt, {
|
|
38298
38616
|
requestedModel: request2.model,
|
|
38299
|
-
|
|
38300
|
-
|
|
38301
|
-
credentialId: null,
|
|
38302
|
-
attempts: 0,
|
|
38303
|
-
status: 200,
|
|
38304
|
-
errorCode: null,
|
|
38305
|
-
inputTokens: 0,
|
|
38306
|
-
outputTokens: 0,
|
|
38307
|
-
cacheReadTokens: 0,
|
|
38308
|
-
cacheWriteTokens: 0,
|
|
38309
|
-
ttftMs: null,
|
|
38310
|
-
durationMs: 0,
|
|
38311
|
-
costUsd: 0,
|
|
38312
|
-
degradations: []
|
|
38313
|
-
};
|
|
38617
|
+
status: 0
|
|
38618
|
+
});
|
|
38314
38619
|
const fail = (code, message) => {
|
|
38315
38620
|
log.errorCode = code;
|
|
38316
38621
|
log.status = HTTP_STATUS[code];
|
|
@@ -38357,8 +38662,20 @@ async function dispatch(request2, deps, signal) {
|
|
|
38357
38662
|
now: startedAt,
|
|
38358
38663
|
rand: deps.rand()
|
|
38359
38664
|
});
|
|
38665
|
+
logger2.debug("routing candidates ranked", {
|
|
38666
|
+
requestId,
|
|
38667
|
+
requestedModel: request2.model,
|
|
38668
|
+
model: candidates[0]?.target.model,
|
|
38669
|
+
credentialId: candidates[0]?.credential.id,
|
|
38670
|
+
count: candidates.length
|
|
38671
|
+
});
|
|
38360
38672
|
for (const e of excluded) {
|
|
38361
38673
|
log.degradations.push(`excluded:${e.credentialId}:${e.reason}`);
|
|
38674
|
+
logger2.debug("routing candidate excluded", {
|
|
38675
|
+
requestId,
|
|
38676
|
+
credentialId: e.credentialId,
|
|
38677
|
+
reason: e.reason
|
|
38678
|
+
});
|
|
38362
38679
|
}
|
|
38363
38680
|
if (candidates.length === 0) {
|
|
38364
38681
|
clearDeadline();
|
|
@@ -38389,6 +38706,13 @@ async function dispatch(request2, deps, signal) {
|
|
|
38389
38706
|
model: candidate.target.model,
|
|
38390
38707
|
credentialId: candidate.credential.id
|
|
38391
38708
|
});
|
|
38709
|
+
logger2.debug("attempt started", {
|
|
38710
|
+
requestId,
|
|
38711
|
+
provider: candidate.target.provider,
|
|
38712
|
+
model: candidate.target.model,
|
|
38713
|
+
credentialId: candidate.credential.id,
|
|
38714
|
+
attempt: i + 1
|
|
38715
|
+
});
|
|
38392
38716
|
log.inputTokens = 0;
|
|
38393
38717
|
log.outputTokens = 0;
|
|
38394
38718
|
log.cacheReadTokens = 0;
|
|
@@ -38410,7 +38734,9 @@ async function dispatch(request2, deps, signal) {
|
|
|
38410
38734
|
signal: dispatchSignal,
|
|
38411
38735
|
refresh: (credential) => waitForCancellation(deps.refresh(credential), dispatchSignal),
|
|
38412
38736
|
refreshLeadMs: DISPATCH_REFRESH_LEAD_MS,
|
|
38413
|
-
...retrySecrets === undefined ? {} : { secrets: retrySecrets }
|
|
38737
|
+
...retrySecrets === undefined ? {} : { secrets: retrySecrets },
|
|
38738
|
+
logger: logger2,
|
|
38739
|
+
requestId
|
|
38414
38740
|
}), dispatchSignal);
|
|
38415
38741
|
for (const d of result.degradations)
|
|
38416
38742
|
log.degradations.push(d);
|
|
@@ -38420,6 +38746,14 @@ async function dispatch(request2, deps, signal) {
|
|
|
38420
38746
|
if (event.type === "blockDelta" && !committed) {
|
|
38421
38747
|
committed = true;
|
|
38422
38748
|
log.ttftMs = deps.now() - startedAt;
|
|
38749
|
+
logger2.debug("stream committed", {
|
|
38750
|
+
requestId,
|
|
38751
|
+
provider: candidate.target.provider,
|
|
38752
|
+
model: candidate.target.model,
|
|
38753
|
+
credentialId: candidate.credential.id,
|
|
38754
|
+
attempt: i + 1,
|
|
38755
|
+
ttftMs: log.ttftMs
|
|
38756
|
+
});
|
|
38423
38757
|
for (const buffered of pending)
|
|
38424
38758
|
yield buffered;
|
|
38425
38759
|
pending.length = 0;
|
|
@@ -38430,7 +38764,7 @@ async function dispatch(request2, deps, signal) {
|
|
|
38430
38764
|
log.outputTokens = event.usage.outputTokens;
|
|
38431
38765
|
log.cacheReadTokens = event.usage.cacheReadTokens;
|
|
38432
38766
|
log.cacheWriteTokens = event.usage.cacheWriteTokens;
|
|
38433
|
-
log.costUsd = priceOf(candidate, event.usage);
|
|
38767
|
+
log.costUsd = priceOf(candidate.target.costPerMTok, event.usage, candidate.target.provider);
|
|
38434
38768
|
}
|
|
38435
38769
|
if (event.type === "error") {
|
|
38436
38770
|
terminal = true;
|
|
@@ -38484,6 +38818,14 @@ async function dispatch(request2, deps, signal) {
|
|
|
38484
38818
|
lastError = retryAfterMs === undefined ? new GatewayError(code2, message) : new GatewayError(code2, message, { retryAfterMs });
|
|
38485
38819
|
if (code2 === "AUTH" && !committed && !authRefreshRetried && !preemptiveRefreshRequired && candidate.credential.authType === "oauth" && candidate.credential.hasRefreshToken) {
|
|
38486
38820
|
authRefreshRetried = true;
|
|
38821
|
+
logger2.warn("attempt authentication failed; refreshing credential", {
|
|
38822
|
+
requestId,
|
|
38823
|
+
provider: candidate.target.provider,
|
|
38824
|
+
model: candidate.target.model,
|
|
38825
|
+
credentialId: candidate.credential.id,
|
|
38826
|
+
attempt: i + 1,
|
|
38827
|
+
code: code2
|
|
38828
|
+
});
|
|
38487
38829
|
try {
|
|
38488
38830
|
retrySecrets = await waitForCancellation(deps.refresh(candidate.credential), dispatchSignal);
|
|
38489
38831
|
continue;
|
|
@@ -38505,6 +38847,18 @@ async function dispatch(request2, deps, signal) {
|
|
|
38505
38847
|
...failure.retryAfterMs === undefined ? {} : { retryAfterMs: failure.retryAfterMs },
|
|
38506
38848
|
jitter: deps.rand()
|
|
38507
38849
|
}));
|
|
38850
|
+
if (!committed && RETRYABLE[failure.code] && i + 1 < maxAttempts) {
|
|
38851
|
+
logger2.warn("attempt failed; retrying", {
|
|
38852
|
+
requestId,
|
|
38853
|
+
provider: candidate.target.provider,
|
|
38854
|
+
model: candidate.target.model,
|
|
38855
|
+
credentialId: candidate.credential.id,
|
|
38856
|
+
attempt: i + 1,
|
|
38857
|
+
code: failure.code,
|
|
38858
|
+
retryable: true,
|
|
38859
|
+
retryAfterMs: failure.retryAfterMs
|
|
38860
|
+
});
|
|
38861
|
+
}
|
|
38508
38862
|
if (committed) {
|
|
38509
38863
|
log.status = HTTP_STATUS[failure.code];
|
|
38510
38864
|
log.errorCode = failure.code;
|
|
@@ -38541,11 +38895,6 @@ async function dispatch(request2, deps, signal) {
|
|
|
38541
38895
|
}
|
|
38542
38896
|
return { events: run(), log: () => log };
|
|
38543
38897
|
}
|
|
38544
|
-
function priceOf(candidate, usage) {
|
|
38545
|
-
const p = candidate.target.costPerMTok;
|
|
38546
|
-
const cacheRate = p.cacheRead ?? p.input * 0.1;
|
|
38547
|
-
return (usage.inputTokens * p.input + usage.outputTokens * p.output + usage.cacheReadTokens * cacheRate) / 1e6;
|
|
38548
|
-
}
|
|
38549
38898
|
function waitForCancellation(promise2, signal) {
|
|
38550
38899
|
if (signal.aborted)
|
|
38551
38900
|
return Promise.reject(signal.reason);
|
|
@@ -38606,7 +38955,12 @@ async function* anthropicStream(events, requestId) {
|
|
|
38606
38955
|
content: [],
|
|
38607
38956
|
stop_reason: null,
|
|
38608
38957
|
stop_sequence: null,
|
|
38609
|
-
usage: {
|
|
38958
|
+
usage: {
|
|
38959
|
+
input_tokens: 0,
|
|
38960
|
+
output_tokens: 0,
|
|
38961
|
+
cache_read_input_tokens: 0,
|
|
38962
|
+
cache_creation_input_tokens: 0
|
|
38963
|
+
}
|
|
38610
38964
|
}
|
|
38611
38965
|
});
|
|
38612
38966
|
break;
|
|
@@ -38639,7 +38993,9 @@ async function* anthropicStream(events, requestId) {
|
|
|
38639
38993
|
delta: { stop_reason: STOP_REASON2[event.stopReason], stop_sequence: null },
|
|
38640
38994
|
usage: {
|
|
38641
38995
|
input_tokens: event.usage.inputTokens,
|
|
38642
|
-
output_tokens: event.usage.outputTokens
|
|
38996
|
+
output_tokens: event.usage.outputTokens,
|
|
38997
|
+
cache_read_input_tokens: event.usage.cacheReadTokens,
|
|
38998
|
+
cache_creation_input_tokens: event.usage.cacheWriteTokens
|
|
38643
38999
|
}
|
|
38644
39000
|
});
|
|
38645
39001
|
yield frame("message_stop", { type: "message_stop" });
|
|
@@ -38675,7 +39031,9 @@ function anthropicResponse(collected, requestId) {
|
|
|
38675
39031
|
stop_sequence: null,
|
|
38676
39032
|
usage: {
|
|
38677
39033
|
input_tokens: collected.usage.inputTokens,
|
|
38678
|
-
output_tokens: collected.usage.outputTokens
|
|
39034
|
+
output_tokens: collected.usage.outputTokens,
|
|
39035
|
+
cache_read_input_tokens: collected.usage.cacheReadTokens,
|
|
39036
|
+
cache_creation_input_tokens: collected.usage.cacheWriteTokens
|
|
38679
39037
|
}
|
|
38680
39038
|
};
|
|
38681
39039
|
}
|
|
@@ -38684,6 +39042,15 @@ function anthropicErrorBody(code, message) {
|
|
|
38684
39042
|
}
|
|
38685
39043
|
|
|
38686
39044
|
// apps/gateway/src/egress/openai.ts
|
|
39045
|
+
function usageBody(usage) {
|
|
39046
|
+
const prompt = promptTokens(usage);
|
|
39047
|
+
return {
|
|
39048
|
+
prompt_tokens: prompt,
|
|
39049
|
+
completion_tokens: usage.outputTokens,
|
|
39050
|
+
total_tokens: prompt + usage.outputTokens,
|
|
39051
|
+
prompt_tokens_details: { cached_tokens: usage.cacheReadTokens }
|
|
39052
|
+
};
|
|
39053
|
+
}
|
|
38687
39054
|
var FINISH2 = {
|
|
38688
39055
|
endTurn: "stop",
|
|
38689
39056
|
maxTokens: "length",
|
|
@@ -38781,11 +39148,7 @@ async function* openaiStream(events, requestId, created) {
|
|
|
38781
39148
|
break;
|
|
38782
39149
|
case "end":
|
|
38783
39150
|
completed = true;
|
|
38784
|
-
yield emit(chunk(requestId, created, model, { index: 0, delta: {}, finish_reason: FINISH2[event.stopReason] },
|
|
38785
|
-
prompt_tokens: event.usage.inputTokens,
|
|
38786
|
-
completion_tokens: event.usage.outputTokens,
|
|
38787
|
-
total_tokens: event.usage.inputTokens + event.usage.outputTokens
|
|
38788
|
-
}));
|
|
39151
|
+
yield emit(chunk(requestId, created, model, { index: 0, delta: {}, finish_reason: FINISH2[event.stopReason] }, usageBody(event.usage)));
|
|
38789
39152
|
break;
|
|
38790
39153
|
case "error": {
|
|
38791
39154
|
const e = ERROR_TYPE3[event.code];
|
|
@@ -38822,11 +39185,7 @@ function openaiResponse(collected, requestId, created) {
|
|
|
38822
39185
|
finish_reason: FINISH2[collected.stopReason]
|
|
38823
39186
|
}
|
|
38824
39187
|
],
|
|
38825
|
-
usage:
|
|
38826
|
-
prompt_tokens: collected.usage.inputTokens,
|
|
38827
|
-
completion_tokens: collected.usage.outputTokens,
|
|
38828
|
-
total_tokens: collected.usage.inputTokens + collected.usage.outputTokens
|
|
38829
|
-
}
|
|
39188
|
+
usage: usageBody(collected.usage)
|
|
38830
39189
|
};
|
|
38831
39190
|
}
|
|
38832
39191
|
function openaiErrorBody(code, message) {
|
|
@@ -38835,14 +39194,19 @@ function openaiErrorBody(code, message) {
|
|
|
38835
39194
|
}
|
|
38836
39195
|
|
|
38837
39196
|
// apps/gateway/src/ingress/anthropic.ts
|
|
38838
|
-
var textBlock = exports_external.object({
|
|
39197
|
+
var textBlock = exports_external.object({
|
|
39198
|
+
type: exports_external.literal("text"),
|
|
39199
|
+
text: exports_external.string(),
|
|
39200
|
+
cache_control: cacheControlSchema.optional()
|
|
39201
|
+
});
|
|
38839
39202
|
var imageBlock = exports_external.object({
|
|
38840
39203
|
type: exports_external.literal("image"),
|
|
38841
39204
|
source: exports_external.object({
|
|
38842
39205
|
type: exports_external.literal("base64"),
|
|
38843
39206
|
media_type: exports_external.string(),
|
|
38844
39207
|
data: exports_external.string()
|
|
38845
|
-
})
|
|
39208
|
+
}),
|
|
39209
|
+
cache_control: cacheControlSchema.optional()
|
|
38846
39210
|
});
|
|
38847
39211
|
var thinkingBlock = exports_external.object({
|
|
38848
39212
|
type: exports_external.literal("thinking"),
|
|
@@ -38853,13 +39217,15 @@ var toolUseBlock = exports_external.object({
|
|
|
38853
39217
|
type: exports_external.literal("tool_use"),
|
|
38854
39218
|
id: exports_external.string(),
|
|
38855
39219
|
name: exports_external.string(),
|
|
38856
|
-
input: exports_external.record(exports_external.string(), exports_external.unknown()).default({})
|
|
39220
|
+
input: exports_external.record(exports_external.string(), exports_external.unknown()).default({}),
|
|
39221
|
+
cache_control: cacheControlSchema.optional()
|
|
38857
39222
|
});
|
|
38858
39223
|
var toolResultBlock = exports_external.object({
|
|
38859
39224
|
type: exports_external.literal("tool_result"),
|
|
38860
39225
|
tool_use_id: exports_external.string(),
|
|
38861
39226
|
content: exports_external.union([exports_external.string(), exports_external.array(exports_external.unknown())]).optional(),
|
|
38862
|
-
is_error: exports_external.boolean().optional()
|
|
39227
|
+
is_error: exports_external.boolean().optional(),
|
|
39228
|
+
cache_control: cacheControlSchema.optional()
|
|
38863
39229
|
});
|
|
38864
39230
|
var block = exports_external.discriminatedUnion("type", [
|
|
38865
39231
|
textBlock,
|
|
@@ -38883,7 +39249,8 @@ var schema = exports_external.object({
|
|
|
38883
39249
|
tools: exports_external.array(exports_external.object({
|
|
38884
39250
|
name: exports_external.string(),
|
|
38885
39251
|
description: exports_external.string().optional(),
|
|
38886
|
-
input_schema: exports_external.record(exports_external.string(), exports_external.unknown())
|
|
39252
|
+
input_schema: exports_external.record(exports_external.string(), exports_external.unknown()),
|
|
39253
|
+
cache_control: cacheControlSchema.optional()
|
|
38887
39254
|
})).optional(),
|
|
38888
39255
|
tool_choice: exports_external.union([
|
|
38889
39256
|
exports_external.object({ type: exports_external.enum(["auto", "any", "none"]) }),
|
|
@@ -38927,9 +39294,14 @@ function flattenToolResult(content) {
|
|
|
38927
39294
|
function toIrBlock(b) {
|
|
38928
39295
|
switch (b.type) {
|
|
38929
39296
|
case "text":
|
|
38930
|
-
return { type: "text", text: b.text };
|
|
39297
|
+
return { type: "text", text: b.text, ...irCacheControl(b.cache_control) };
|
|
38931
39298
|
case "image":
|
|
38932
|
-
return {
|
|
39299
|
+
return {
|
|
39300
|
+
type: "image",
|
|
39301
|
+
mediaType: b.source.media_type,
|
|
39302
|
+
data: b.source.data,
|
|
39303
|
+
...irCacheControl(b.cache_control)
|
|
39304
|
+
};
|
|
38933
39305
|
case "thinking":
|
|
38934
39306
|
return {
|
|
38935
39307
|
type: "thinking",
|
|
@@ -38937,13 +39309,20 @@ function toIrBlock(b) {
|
|
|
38937
39309
|
...b.signature !== undefined && { signature: b.signature }
|
|
38938
39310
|
};
|
|
38939
39311
|
case "tool_use":
|
|
38940
|
-
return {
|
|
39312
|
+
return {
|
|
39313
|
+
type: "toolUse",
|
|
39314
|
+
id: b.id,
|
|
39315
|
+
name: b.name,
|
|
39316
|
+
input: b.input,
|
|
39317
|
+
...irCacheControl(b.cache_control)
|
|
39318
|
+
};
|
|
38941
39319
|
case "tool_result":
|
|
38942
39320
|
return {
|
|
38943
39321
|
type: "toolResult",
|
|
38944
39322
|
toolUseId: b.tool_use_id,
|
|
38945
39323
|
content: flattenToolResult(b.content),
|
|
38946
|
-
isError: b.is_error ?? false
|
|
39324
|
+
isError: b.is_error ?? false,
|
|
39325
|
+
...irCacheControl(b.cache_control)
|
|
38947
39326
|
};
|
|
38948
39327
|
}
|
|
38949
39328
|
}
|
|
@@ -38990,7 +39369,11 @@ function parseAnthropicRequest(body2, headers) {
|
|
|
38990
39369
|
role: m.role,
|
|
38991
39370
|
content: typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content.map(toIrBlock)
|
|
38992
39371
|
}));
|
|
38993
|
-
const system = parsed.system === undefined ? undefined : typeof parsed.system === "string" ? [{ type: "text", text: parsed.system }] : parsed.system.map((b) => ({
|
|
39372
|
+
const system = parsed.system === undefined ? undefined : typeof parsed.system === "string" ? [{ type: "text", text: parsed.system }] : parsed.system.map((b) => ({
|
|
39373
|
+
type: "text",
|
|
39374
|
+
text: b.text,
|
|
39375
|
+
...irCacheControl(b.cache_control)
|
|
39376
|
+
}));
|
|
38994
39377
|
const request2 = {
|
|
38995
39378
|
model: parsed.model,
|
|
38996
39379
|
messages,
|
|
@@ -39008,7 +39391,8 @@ function parseAnthropicRequest(body2, headers) {
|
|
|
39008
39391
|
request2.tools = parsed.tools.map((t2) => ({
|
|
39009
39392
|
name: t2.name,
|
|
39010
39393
|
...t2.description !== undefined && { description: t2.description },
|
|
39011
|
-
inputSchema: t2.input_schema
|
|
39394
|
+
inputSchema: t2.input_schema,
|
|
39395
|
+
...irCacheControl(t2.cache_control)
|
|
39012
39396
|
}));
|
|
39013
39397
|
}
|
|
39014
39398
|
if (parsed.tool_choice !== undefined)
|
|
@@ -39030,8 +39414,16 @@ function parseAnthropicRequest(body2, headers) {
|
|
|
39030
39414
|
|
|
39031
39415
|
// apps/gateway/src/ingress/openai.ts
|
|
39032
39416
|
var part = exports_external.discriminatedUnion("type", [
|
|
39033
|
-
exports_external.object({
|
|
39034
|
-
|
|
39417
|
+
exports_external.object({
|
|
39418
|
+
type: exports_external.literal("text"),
|
|
39419
|
+
text: exports_external.string(),
|
|
39420
|
+
cache_control: exports_external.unknown().optional()
|
|
39421
|
+
}),
|
|
39422
|
+
exports_external.object({
|
|
39423
|
+
type: exports_external.literal("image_url"),
|
|
39424
|
+
image_url: exports_external.object({ url: exports_external.string() }),
|
|
39425
|
+
cache_control: exports_external.unknown().optional()
|
|
39426
|
+
})
|
|
39035
39427
|
]);
|
|
39036
39428
|
var toolCall = exports_external.object({
|
|
39037
39429
|
id: exports_external.string(),
|
|
@@ -39042,7 +39434,8 @@ var message2 = exports_external.object({
|
|
|
39042
39434
|
role: exports_external.enum(["system", "developer", "user", "assistant", "tool"]),
|
|
39043
39435
|
content: exports_external.union([exports_external.string(), exports_external.array(part), exports_external.null()]).optional(),
|
|
39044
39436
|
tool_calls: exports_external.array(toolCall).optional(),
|
|
39045
|
-
tool_call_id: exports_external.string().optional()
|
|
39437
|
+
tool_call_id: exports_external.string().optional(),
|
|
39438
|
+
cache_control: exports_external.unknown().optional()
|
|
39046
39439
|
});
|
|
39047
39440
|
var schema2 = exports_external.object({
|
|
39048
39441
|
model: exports_external.string().min(1),
|
|
@@ -39057,8 +39450,10 @@ var schema2 = exports_external.object({
|
|
|
39057
39450
|
function: exports_external.object({
|
|
39058
39451
|
name: exports_external.string(),
|
|
39059
39452
|
description: exports_external.string().optional(),
|
|
39060
|
-
parameters: exports_external.record(exports_external.string(), exports_external.unknown()).optional()
|
|
39061
|
-
|
|
39453
|
+
parameters: exports_external.record(exports_external.string(), exports_external.unknown()).optional(),
|
|
39454
|
+
cache_control: exports_external.unknown().optional()
|
|
39455
|
+
}),
|
|
39456
|
+
cache_control: exports_external.unknown().optional()
|
|
39062
39457
|
})).optional(),
|
|
39063
39458
|
tool_choice: exports_external.union([
|
|
39064
39459
|
exports_external.enum(["auto", "none", "required"]),
|
|
@@ -39096,11 +39491,15 @@ function contentBlocks(content) {
|
|
|
39096
39491
|
return [];
|
|
39097
39492
|
return content.map((p) => {
|
|
39098
39493
|
if (p.type === "text")
|
|
39099
|
-
return { type: "text", text: p.text };
|
|
39494
|
+
return { type: "text", text: p.text, ...looseCacheControl(p.cache_control) };
|
|
39100
39495
|
const { mediaType, data } = parseDataUrl(p.image_url.url);
|
|
39101
|
-
return { type: "image", mediaType, data };
|
|
39496
|
+
return { type: "image", mediaType, data, ...looseCacheControl(p.cache_control) };
|
|
39102
39497
|
});
|
|
39103
39498
|
}
|
|
39499
|
+
function toolCacheControl(outer, inner) {
|
|
39500
|
+
const parsed = looseCacheControl(outer);
|
|
39501
|
+
return parsed.cacheControl === undefined ? looseCacheControl(inner) : parsed;
|
|
39502
|
+
}
|
|
39104
39503
|
function toIrToolChoice2(c) {
|
|
39105
39504
|
if (typeof c === "string") {
|
|
39106
39505
|
if (c === "required")
|
|
@@ -39118,7 +39517,9 @@ function parseOpenAIRequest(body2) {
|
|
|
39118
39517
|
const messages = [];
|
|
39119
39518
|
for (const m of parsed.messages) {
|
|
39120
39519
|
if (m.role === "system" || m.role === "developer") {
|
|
39121
|
-
|
|
39520
|
+
const blocks = contentBlocks(m.content);
|
|
39521
|
+
applyMessageCacheControl(blocks, m.cache_control);
|
|
39522
|
+
system.push(...blocks);
|
|
39122
39523
|
continue;
|
|
39123
39524
|
}
|
|
39124
39525
|
if (m.role === "tool") {
|
|
@@ -39132,7 +39533,8 @@ function parseOpenAIRequest(body2) {
|
|
|
39132
39533
|
type: "toolResult",
|
|
39133
39534
|
toolUseId: m.tool_call_id,
|
|
39134
39535
|
content: typeof m.content === "string" ? m.content : "",
|
|
39135
|
-
isError: false
|
|
39536
|
+
isError: false,
|
|
39537
|
+
...looseCacheControl(m.cache_control)
|
|
39136
39538
|
}
|
|
39137
39539
|
]
|
|
39138
39540
|
});
|
|
@@ -39147,6 +39549,7 @@ function parseOpenAIRequest(body2) {
|
|
|
39147
39549
|
input: parseArguments(call.function.arguments)
|
|
39148
39550
|
});
|
|
39149
39551
|
}
|
|
39552
|
+
applyMessageCacheControl(content, m.cache_control);
|
|
39150
39553
|
if (content.length > 0)
|
|
39151
39554
|
messages.push({ role: m.role, content });
|
|
39152
39555
|
}
|
|
@@ -39172,7 +39575,8 @@ function parseOpenAIRequest(body2) {
|
|
|
39172
39575
|
request2.tools = parsed.tools.map((t2) => ({
|
|
39173
39576
|
name: t2.function.name,
|
|
39174
39577
|
...t2.function.description !== undefined && { description: t2.function.description },
|
|
39175
|
-
inputSchema: t2.function.parameters ?? { type: "object" }
|
|
39578
|
+
inputSchema: t2.function.parameters ?? { type: "object" },
|
|
39579
|
+
...toolCacheControl(t2.cache_control, t2.function.cache_control)
|
|
39176
39580
|
}));
|
|
39177
39581
|
}
|
|
39178
39582
|
if (parsed.tool_choice !== undefined)
|
|
@@ -39185,33 +39589,83 @@ function parseOpenAIRequest(body2) {
|
|
|
39185
39589
|
return validateRequest(request2);
|
|
39186
39590
|
}
|
|
39187
39591
|
|
|
39188
|
-
// apps/gateway/src/
|
|
39189
|
-
|
|
39190
|
-
|
|
39191
|
-
|
|
39192
|
-
|
|
39193
|
-
|
|
39592
|
+
// apps/gateway/src/routes/models.ts
|
|
39593
|
+
var CREATED_AT = new Date(0).toISOString();
|
|
39594
|
+
function narrower(a, b) {
|
|
39595
|
+
const context = [a.contextWindow, b.contextWindow].filter((n) => n !== undefined);
|
|
39596
|
+
const output = [a.maxOutputTokens, b.maxOutputTokens].filter((n) => n !== undefined);
|
|
39597
|
+
return {
|
|
39598
|
+
...context.length === 0 ? {} : { contextWindow: Math.min(...context) },
|
|
39599
|
+
...output.length === 0 ? {} : { maxOutputTokens: Math.min(...output) }
|
|
39600
|
+
};
|
|
39194
39601
|
}
|
|
39195
|
-
|
|
39196
|
-
|
|
39197
|
-
|
|
39198
|
-
|
|
39199
|
-
|
|
39602
|
+
function targetLimits(target, auths) {
|
|
39603
|
+
const ways = auths.size === 0 ? ["apiKey"] : [...auths];
|
|
39604
|
+
let listed = {};
|
|
39605
|
+
for (const auth of ways) {
|
|
39606
|
+
const entry = catalogLimits(target.provider, target.model, auth);
|
|
39607
|
+
if (entry === null)
|
|
39608
|
+
continue;
|
|
39609
|
+
listed = narrower(listed, {
|
|
39610
|
+
contextWindow: entry.contextWindow,
|
|
39611
|
+
maxOutputTokens: entry.maxOutputTokens
|
|
39612
|
+
});
|
|
39200
39613
|
}
|
|
39614
|
+
const contextWindow = target.contextWindow ?? listed.contextWindow;
|
|
39615
|
+
const maxOutputTokens = target.maxOutputTokens ?? listed.maxOutputTokens;
|
|
39616
|
+
return {
|
|
39617
|
+
...contextWindow === undefined ? {} : { contextWindow },
|
|
39618
|
+
...maxOutputTokens === undefined ? {} : { maxOutputTokens }
|
|
39619
|
+
};
|
|
39201
39620
|
}
|
|
39202
|
-
|
|
39203
|
-
|
|
39204
|
-
|
|
39205
|
-
|
|
39206
|
-
report("failed to record request route", requestId, error51);
|
|
39621
|
+
function limitsOf(model, authsByProvider) {
|
|
39622
|
+
let limits = {};
|
|
39623
|
+
for (const target of model.targets) {
|
|
39624
|
+
limits = narrower(limits, targetLimits(target, authsByProvider.get(target.provider) ?? new Set));
|
|
39207
39625
|
}
|
|
39626
|
+
return limits;
|
|
39208
39627
|
}
|
|
39209
|
-
|
|
39210
|
-
|
|
39211
|
-
|
|
39212
|
-
|
|
39213
|
-
|
|
39628
|
+
function displayName(model) {
|
|
39629
|
+
const only = model.targets.length === 1 ? model.targets[0] : undefined;
|
|
39630
|
+
if (only === undefined)
|
|
39631
|
+
return model.id;
|
|
39632
|
+
const labelled = PROVIDER_MODEL_CATALOG[only.provider]?.models.find((choice) => choice.id === only.model);
|
|
39633
|
+
return labelled?.label ?? model.id;
|
|
39634
|
+
}
|
|
39635
|
+
function servingAuths(credentials) {
|
|
39636
|
+
const byProvider = new Map;
|
|
39637
|
+
for (const credential of credentials) {
|
|
39638
|
+
if (!credential.enabled)
|
|
39639
|
+
continue;
|
|
39640
|
+
const ways = byProvider.get(credential.provider) ?? new Set;
|
|
39641
|
+
ways.add(credential.authType);
|
|
39642
|
+
byProvider.set(credential.provider, ways);
|
|
39214
39643
|
}
|
|
39644
|
+
return byProvider;
|
|
39645
|
+
}
|
|
39646
|
+
function describeModel(model, credentials) {
|
|
39647
|
+
const limits = limitsOf(model, servingAuths(credentials));
|
|
39648
|
+
return {
|
|
39649
|
+
id: model.id,
|
|
39650
|
+
object: "model",
|
|
39651
|
+
type: "model",
|
|
39652
|
+
display_name: displayName(model),
|
|
39653
|
+
created: 0,
|
|
39654
|
+
created_at: CREATED_AT,
|
|
39655
|
+
owned_by: "omnigateway",
|
|
39656
|
+
...limits.contextWindow === undefined ? {} : { max_input_tokens: limits.contextWindow },
|
|
39657
|
+
...limits.maxOutputTokens === undefined ? {} : { max_tokens: limits.maxOutputTokens }
|
|
39658
|
+
};
|
|
39659
|
+
}
|
|
39660
|
+
function modelListBody(models, credentials) {
|
|
39661
|
+
const data = models.map((model) => describeModel(model, credentials));
|
|
39662
|
+
return {
|
|
39663
|
+
object: "list",
|
|
39664
|
+
data,
|
|
39665
|
+
has_more: false,
|
|
39666
|
+
first_id: data[0]?.id ?? null,
|
|
39667
|
+
last_id: data[data.length - 1]?.id ?? null
|
|
39668
|
+
};
|
|
39215
39669
|
}
|
|
39216
39670
|
|
|
39217
39671
|
// apps/gateway/src/routes/proxy.ts
|
|
@@ -39230,14 +39684,14 @@ var SSE_HEADERS = {
|
|
|
39230
39684
|
connection: "keep-alive",
|
|
39231
39685
|
"x-accel-buffering": "no"
|
|
39232
39686
|
};
|
|
39233
|
-
function
|
|
39687
|
+
function errorResponse(surface, code, message3) {
|
|
39234
39688
|
const body2 = surface === "anthropic" ? anthropicErrorBody(code, message3) : openaiErrorBody(code, message3);
|
|
39235
39689
|
return new Response(JSON.stringify(body2), {
|
|
39236
39690
|
status: HTTP_STATUS[code],
|
|
39237
39691
|
headers: { "content-type": "application/json" }
|
|
39238
39692
|
});
|
|
39239
39693
|
}
|
|
39240
|
-
function
|
|
39694
|
+
function asGatewayError(error51) {
|
|
39241
39695
|
if (error51 instanceof GatewayError)
|
|
39242
39696
|
return error51;
|
|
39243
39697
|
return new GatewayError("INTERNAL", error51 instanceof Error ? error51.message : "internal error");
|
|
@@ -39245,9 +39699,9 @@ function asGatewayError2(error51) {
|
|
|
39245
39699
|
function sseResponse(frames, onDone, keepaliveMs) {
|
|
39246
39700
|
const encoder2 = new TextEncoder;
|
|
39247
39701
|
let done = null;
|
|
39248
|
-
const runOnce = () => {
|
|
39702
|
+
const runOnce = (cancelled = false) => {
|
|
39249
39703
|
if (done === null)
|
|
39250
|
-
done = onDone();
|
|
39704
|
+
done = onDone(cancelled);
|
|
39251
39705
|
return done;
|
|
39252
39706
|
};
|
|
39253
39707
|
let inflight = null;
|
|
@@ -39280,7 +39734,7 @@ data: ${data}
|
|
|
39280
39734
|
},
|
|
39281
39735
|
async cancel() {
|
|
39282
39736
|
await frames.return(undefined);
|
|
39283
|
-
await runOnce();
|
|
39737
|
+
await runOnce(true);
|
|
39284
39738
|
}
|
|
39285
39739
|
});
|
|
39286
39740
|
return new Response(stream2, { headers: SSE_HEADERS });
|
|
@@ -39291,9 +39745,34 @@ async function handle(deps, rateLimiter, surface, request2) {
|
|
|
39291
39745
|
let keyId = null;
|
|
39292
39746
|
let requestedModel = "";
|
|
39293
39747
|
try {
|
|
39294
|
-
|
|
39748
|
+
let key;
|
|
39749
|
+
try {
|
|
39750
|
+
key = await authenticateApiKey(deps.store, apiKeyHeader(request2.headers));
|
|
39751
|
+
} catch (error51) {
|
|
39752
|
+
if (error51 instanceof GatewayError && error51.code === "AUTH") {
|
|
39753
|
+
deps.logger.warn("authentication rejected", {
|
|
39754
|
+
requestId,
|
|
39755
|
+
surface,
|
|
39756
|
+
reason: "invalid credentials"
|
|
39757
|
+
});
|
|
39758
|
+
}
|
|
39759
|
+
throw error51;
|
|
39760
|
+
}
|
|
39295
39761
|
keyId = key.id;
|
|
39296
|
-
|
|
39762
|
+
try {
|
|
39763
|
+
rateLimiter.consume(key.id, key.rateLimitPerMin);
|
|
39764
|
+
} catch (error51) {
|
|
39765
|
+
if (error51 instanceof GatewayError && error51.code === "RATE_LIMIT") {
|
|
39766
|
+
deps.logger.warn("rate limit rejected", {
|
|
39767
|
+
requestId,
|
|
39768
|
+
surface,
|
|
39769
|
+
apiKeyId: key.id,
|
|
39770
|
+
code: error51.code,
|
|
39771
|
+
retryAfterMs: error51.retryAfterMs
|
|
39772
|
+
});
|
|
39773
|
+
}
|
|
39774
|
+
throw error51;
|
|
39775
|
+
}
|
|
39297
39776
|
const body2 = await request2.json();
|
|
39298
39777
|
const chatRequest = surface === "anthropic" ? parseAnthropicRequest(body2, request2.headers) : parseOpenAIRequest(body2);
|
|
39299
39778
|
if (key.modelAllowlist !== null && !key.modelAllowlist.includes(chatRequest.model)) {
|
|
@@ -39305,33 +39784,57 @@ async function handle(deps, rateLimiter, surface, request2) {
|
|
|
39305
39784
|
...deps,
|
|
39306
39785
|
async onRoute(target) {
|
|
39307
39786
|
if (began) {
|
|
39308
|
-
await routeLog(deps.store, requestId, target);
|
|
39787
|
+
await routeLog(deps.store, requestId, target, deps.logger);
|
|
39309
39788
|
return;
|
|
39310
39789
|
}
|
|
39311
39790
|
began = true;
|
|
39312
|
-
|
|
39791
|
+
const log2 = newPendingRequestLog({
|
|
39313
39792
|
id: requestId,
|
|
39314
39793
|
at: startedAt,
|
|
39315
|
-
apiKeyId: keyId,
|
|
39316
39794
|
requestedModel,
|
|
39317
39795
|
resolvedProvider: target.provider,
|
|
39318
39796
|
resolvedModel: target.model,
|
|
39319
|
-
credentialId: target.credentialId
|
|
39320
|
-
|
|
39321
|
-
|
|
39322
|
-
|
|
39323
|
-
|
|
39324
|
-
|
|
39325
|
-
|
|
39326
|
-
|
|
39327
|
-
|
|
39328
|
-
|
|
39329
|
-
|
|
39330
|
-
|
|
39331
|
-
|
|
39332
|
-
|
|
39333
|
-
|
|
39334
|
-
|
|
39797
|
+
credentialId: target.credentialId
|
|
39798
|
+
});
|
|
39799
|
+
await beginLog(deps.store, log2, keyId, deps.logger);
|
|
39800
|
+
}
|
|
39801
|
+
}, request2.signal, requestId);
|
|
39802
|
+
const log = async (cancelled = false) => {
|
|
39803
|
+
const completed = outcome.log();
|
|
39804
|
+
const wasCancelled = cancelled || request2.signal.aborted;
|
|
39805
|
+
if (wasCancelled && completed.status === 0) {
|
|
39806
|
+
completed.status = 499;
|
|
39807
|
+
completed.errorCode = "interrupted";
|
|
39808
|
+
completed.durationMs = deps.now() - startedAt;
|
|
39809
|
+
}
|
|
39810
|
+
await finishLog(deps.store, completed, keyId, deps.logger);
|
|
39811
|
+
const fields = {
|
|
39812
|
+
requestId,
|
|
39813
|
+
surface,
|
|
39814
|
+
status: completed.status,
|
|
39815
|
+
provider: completed.resolvedProvider ?? undefined,
|
|
39816
|
+
model: completed.resolvedModel ?? undefined,
|
|
39817
|
+
requestedModel: completed.requestedModel,
|
|
39818
|
+
credentialId: completed.credentialId ?? undefined,
|
|
39819
|
+
apiKeyId: keyId ?? undefined,
|
|
39820
|
+
attempts: completed.attempts,
|
|
39821
|
+
code: completed.errorCode ?? undefined,
|
|
39822
|
+
stream: chatRequest.stream,
|
|
39823
|
+
inputTokens: completed.inputTokens,
|
|
39824
|
+
outputTokens: completed.outputTokens,
|
|
39825
|
+
cacheReadTokens: completed.cacheReadTokens,
|
|
39826
|
+
cacheWriteTokens: completed.cacheWriteTokens,
|
|
39827
|
+
costUsd: completed.costUsd,
|
|
39828
|
+
ttftMs: completed.ttftMs,
|
|
39829
|
+
durationMs: completed.durationMs
|
|
39830
|
+
};
|
|
39831
|
+
if (wasCancelled)
|
|
39832
|
+
deps.logger.debug("request cancelled", fields);
|
|
39833
|
+
else if (completed.status >= 400)
|
|
39834
|
+
deps.logger.error("request failed", fields);
|
|
39835
|
+
else
|
|
39836
|
+
deps.logger.info("request done", fields);
|
|
39837
|
+
};
|
|
39335
39838
|
if (chatRequest.stream) {
|
|
39336
39839
|
const frames = surface === "anthropic" ? anthropicStream(outcome.events, requestId) : openaiStream(outcome.events, requestId, Math.floor(deps.now() / 1000));
|
|
39337
39840
|
return sseResponse(frames, log, deps.keepaliveMs);
|
|
@@ -39342,7 +39845,7 @@ async function handle(deps, rateLimiter, surface, request2) {
|
|
|
39342
39845
|
await log();
|
|
39343
39846
|
const failure = events.find((e) => e.type === "error");
|
|
39344
39847
|
if (failure !== undefined)
|
|
39345
|
-
return
|
|
39848
|
+
return errorResponse(surface, failure.code, failure.message);
|
|
39346
39849
|
const collected = collect(events);
|
|
39347
39850
|
const responseBody = surface === "anthropic" ? anthropicResponse(collected, requestId) : openaiResponse(collected, requestId, Math.floor(deps.now() / 1000));
|
|
39348
39851
|
return new Response(JSON.stringify(responseBody), {
|
|
@@ -39350,59 +39853,61 @@ async function handle(deps, rateLimiter, surface, request2) {
|
|
|
39350
39853
|
headers: { "content-type": "application/json" }
|
|
39351
39854
|
});
|
|
39352
39855
|
} catch (error51) {
|
|
39353
|
-
const gatewayError =
|
|
39354
|
-
|
|
39355
|
-
id: requestId,
|
|
39356
|
-
state: "done",
|
|
39357
|
-
at: startedAt,
|
|
39358
|
-
apiKeyId: keyId,
|
|
39856
|
+
const gatewayError = asGatewayError(error51);
|
|
39857
|
+
const completed = newCompletedRequestLog(requestId, startedAt, {
|
|
39359
39858
|
requestedModel,
|
|
39360
|
-
resolvedProvider: null,
|
|
39361
|
-
resolvedModel: null,
|
|
39362
|
-
credentialId: null,
|
|
39363
|
-
attempts: 0,
|
|
39364
39859
|
status: HTTP_STATUS[gatewayError.code],
|
|
39365
39860
|
errorCode: gatewayError.code,
|
|
39366
|
-
|
|
39367
|
-
|
|
39368
|
-
|
|
39369
|
-
|
|
39370
|
-
|
|
39371
|
-
|
|
39372
|
-
|
|
39373
|
-
|
|
39374
|
-
|
|
39375
|
-
|
|
39861
|
+
durationMs: deps.now() - startedAt
|
|
39862
|
+
});
|
|
39863
|
+
await finishLog(deps.store, completed, keyId, deps.logger);
|
|
39864
|
+
deps.logger.error("request failed", {
|
|
39865
|
+
requestId,
|
|
39866
|
+
surface,
|
|
39867
|
+
status: completed.status,
|
|
39868
|
+
requestedModel,
|
|
39869
|
+
apiKeyId: keyId ?? undefined,
|
|
39870
|
+
code: gatewayError.code,
|
|
39871
|
+
attempts: completed.attempts,
|
|
39872
|
+
durationMs: completed.durationMs,
|
|
39873
|
+
reason: gatewayError.message
|
|
39874
|
+
});
|
|
39875
|
+
return errorResponse(surface, gatewayError.code, gatewayError.message);
|
|
39376
39876
|
}
|
|
39377
39877
|
}
|
|
39378
39878
|
function proxyRoutes(deps) {
|
|
39379
39879
|
const rateLimiter = deps.rateLimiter ?? new ApiKeyRateLimiter(deps.now);
|
|
39880
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39380
39881
|
const dispatchDeps = {
|
|
39381
39882
|
...deps,
|
|
39382
|
-
|
|
39883
|
+
logger: logger2,
|
|
39884
|
+
snapshots: deps.snapshots ?? createRoutingSnapshotCache(deps.store, logger2),
|
|
39383
39885
|
keepaliveMs: deps.keepaliveMs ?? KEEPALIVE_MS
|
|
39384
39886
|
};
|
|
39385
39887
|
return new Elysia().post("/v1/messages", ({ request: request2 }) => handle(dispatchDeps, rateLimiter, "anthropic", request2)).post("/v1/chat/completions", ({ request: request2 }) => handle(dispatchDeps, rateLimiter, "openai", request2)).get("/v1/models", async ({ request: request2 }) => {
|
|
39386
39888
|
try {
|
|
39387
39889
|
const key = await authenticateApiKey(deps.store, apiKeyHeader(request2.headers));
|
|
39388
|
-
const
|
|
39890
|
+
const snapshot = await dispatchDeps.snapshots.get(deps.now());
|
|
39891
|
+
const models = [...snapshot.models.values()];
|
|
39389
39892
|
const visibleModels = key.modelAllowlist === null ? models : models.filter((model) => key.modelAllowlist?.includes(model.id));
|
|
39390
|
-
return Response.json(
|
|
39391
|
-
object: "list",
|
|
39392
|
-
data: visibleModels.map((m) => ({
|
|
39393
|
-
id: m.id,
|
|
39394
|
-
object: "model",
|
|
39395
|
-
created: 0,
|
|
39396
|
-
owned_by: "omnigateway"
|
|
39397
|
-
}))
|
|
39398
|
-
});
|
|
39893
|
+
return Response.json(modelListBody(visibleModels, snapshot.credentials));
|
|
39399
39894
|
} catch (error51) {
|
|
39400
|
-
const gatewayError =
|
|
39401
|
-
|
|
39895
|
+
const gatewayError = asGatewayError(error51);
|
|
39896
|
+
logger2.error("model listing failed", {
|
|
39897
|
+
status: HTTP_STATUS[gatewayError.code],
|
|
39898
|
+
code: gatewayError.code,
|
|
39899
|
+
reason: gatewayError.message
|
|
39900
|
+
});
|
|
39901
|
+
return errorResponse("anthropic", gatewayError.code, gatewayError.message);
|
|
39402
39902
|
}
|
|
39403
39903
|
}).onError(({ error: error51 }) => {
|
|
39404
|
-
const gatewayError =
|
|
39405
|
-
|
|
39904
|
+
const gatewayError = asGatewayError(error51);
|
|
39905
|
+
logger2.error("unhandled proxy route error", {
|
|
39906
|
+
status: HTTP_STATUS[gatewayError.code],
|
|
39907
|
+
code: gatewayError.code,
|
|
39908
|
+
reason: gatewayError.message
|
|
39909
|
+
});
|
|
39910
|
+
return errorResponse("anthropic", gatewayError.code, gatewayError.message);
|
|
39406
39911
|
});
|
|
39407
39912
|
}
|
|
39408
39913
|
|
|
@@ -39410,18 +39915,20 @@ function proxyRoutes(deps) {
|
|
|
39410
39915
|
var ADMIN_SESSION_TTL_MS = 12 * 60 * 60 * 1000;
|
|
39411
39916
|
function createApp(deps) {
|
|
39412
39917
|
const now = deps.now ?? (() => Date.now());
|
|
39918
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39413
39919
|
const rand = deps.rand ?? Math.random;
|
|
39414
|
-
const http = deps.http ?? nodeHttpClient();
|
|
39920
|
+
const http = deps.http ?? nodeHttpClient({ logger: logger2, now });
|
|
39415
39921
|
const adapters = deps.adapters ?? ADAPTERS;
|
|
39416
39922
|
const requestId = deps.requestId ?? (() => `req_${crypto.randomUUID()}`);
|
|
39417
39923
|
const rateLimiter = new ApiKeyRateLimiter(now);
|
|
39418
|
-
const snapshots = createRoutingSnapshotCache(deps.store);
|
|
39924
|
+
const snapshots = createRoutingSnapshotCache(deps.store, logger2);
|
|
39419
39925
|
const admin = createAdminAuth(deps.store, { now, sessionTtlMs: ADMIN_SESSION_TTL_MS });
|
|
39420
39926
|
const refresh = deps.refresh ?? createRefresher({
|
|
39421
39927
|
store: deps.store,
|
|
39422
39928
|
providers: OAUTH_PROVIDERS,
|
|
39423
39929
|
http,
|
|
39424
|
-
now
|
|
39930
|
+
now,
|
|
39931
|
+
logger: logger2
|
|
39425
39932
|
});
|
|
39426
39933
|
const staticDir = deps.staticDir ? resolve(deps.staticDir) : undefined;
|
|
39427
39934
|
let staticRoot;
|
|
@@ -39439,13 +39946,21 @@ function createApp(deps) {
|
|
|
39439
39946
|
rand,
|
|
39440
39947
|
refresh,
|
|
39441
39948
|
requestId,
|
|
39442
|
-
rateLimiter
|
|
39443
|
-
|
|
39949
|
+
rateLimiter,
|
|
39950
|
+
logger: logger2
|
|
39951
|
+
})).use(adminRoutes({
|
|
39952
|
+
store: deps.store,
|
|
39953
|
+
admin,
|
|
39954
|
+
now,
|
|
39955
|
+
sessionTtlMs: ADMIN_SESSION_TTL_MS,
|
|
39956
|
+
logger: logger2
|
|
39957
|
+
})).use(connectRoutes({
|
|
39444
39958
|
store: deps.store,
|
|
39445
39959
|
admin,
|
|
39446
39960
|
providers: OAUTH_PROVIDERS,
|
|
39447
39961
|
http,
|
|
39448
|
-
now
|
|
39962
|
+
now,
|
|
39963
|
+
logger: logger2
|
|
39449
39964
|
})).get("/*", ({ path, request: request2, set: set2 }) => {
|
|
39450
39965
|
const rawPath = new URL(request2.url).pathname;
|
|
39451
39966
|
const error51 = () => {
|
|
@@ -39503,13 +40018,15 @@ var DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
39503
40018
|
var ROLLUP_RETENTION_DAYS = 400;
|
|
39504
40019
|
async function pruneLogs(store, now) {
|
|
39505
40020
|
const settings = await store.config.getSettings();
|
|
39506
|
-
await store.usage.prune(now - settings.logRetentionDays * DAY_MS);
|
|
39507
|
-
await store.usage.pruneDaily(now - ROLLUP_RETENTION_DAYS * DAY_MS);
|
|
40021
|
+
const raw = await store.usage.prune(now - settings.logRetentionDays * DAY_MS);
|
|
40022
|
+
const daily = await store.usage.pruneDaily(now - ROLLUP_RETENTION_DAYS * DAY_MS);
|
|
40023
|
+
return { raw, daily };
|
|
39508
40024
|
}
|
|
39509
40025
|
function startMaintenance(deps) {
|
|
40026
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39510
40027
|
const timer = setInterval(() => {
|
|
39511
|
-
pruneLogs(deps.store, deps.now()).catch((error51) => {
|
|
39512
|
-
|
|
40028
|
+
pruneLogs(deps.store, deps.now()).then(({ raw, daily }) => logger2.debug("request logs pruned", { rawCount: raw, dailyCount: daily })).catch((error51) => {
|
|
40029
|
+
logger2.error("log pruning failed", {
|
|
39513
40030
|
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
39514
40031
|
});
|
|
39515
40032
|
});
|
|
@@ -39524,6 +40041,7 @@ function due(credentials, now) {
|
|
|
39524
40041
|
return credentials.filter((c) => c.enabled && c.authType === "oauth" && c.expiresAt !== null && c.expiresAt - SCHEDULER_REFRESH_LEAD_MS <= now);
|
|
39525
40042
|
}
|
|
39526
40043
|
async function sweep(deps) {
|
|
40044
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39527
40045
|
const credentials = await deps.store.credentials.list();
|
|
39528
40046
|
let refreshed = 0;
|
|
39529
40047
|
for (const credential of due(credentials, deps.now())) {
|
|
@@ -39542,23 +40060,25 @@ async function sweep(deps) {
|
|
|
39542
40060
|
refreshed += 1;
|
|
39543
40061
|
} catch (error51) {
|
|
39544
40062
|
const code = error51 instanceof GatewayError ? error51.code : "INTERNAL";
|
|
39545
|
-
|
|
39546
|
-
credentialId: credential.id,
|
|
40063
|
+
logger2.warn("scheduled token refresh failed", {
|
|
39547
40064
|
provider: credential.provider,
|
|
39548
|
-
|
|
40065
|
+
credentialId: credential.id,
|
|
40066
|
+
code,
|
|
40067
|
+
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
39549
40068
|
});
|
|
39550
40069
|
}
|
|
39551
40070
|
}
|
|
39552
40071
|
return refreshed;
|
|
39553
40072
|
}
|
|
39554
40073
|
function startRefreshScheduler(deps) {
|
|
40074
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39555
40075
|
let running = false;
|
|
39556
40076
|
const timer = setInterval(() => {
|
|
39557
40077
|
if (running)
|
|
39558
40078
|
return;
|
|
39559
40079
|
running = true;
|
|
39560
40080
|
sweep(deps).catch((error51) => {
|
|
39561
|
-
|
|
40081
|
+
logger2.error("token refresh sweep failed", {
|
|
39562
40082
|
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
39563
40083
|
});
|
|
39564
40084
|
}).finally(() => {
|
|
@@ -39571,6 +40091,7 @@ function startRefreshScheduler(deps) {
|
|
|
39571
40091
|
|
|
39572
40092
|
// apps/gateway/src/quota/poller.ts
|
|
39573
40093
|
async function startQuotaPoller(deps) {
|
|
40094
|
+
const logger2 = deps.logger ?? noopLogger;
|
|
39574
40095
|
const { quotaPollIntervalMs } = await deps.store.config.getSettings();
|
|
39575
40096
|
if (quotaPollIntervalMs <= 0)
|
|
39576
40097
|
return () => {};
|
|
@@ -39580,7 +40101,7 @@ async function startQuotaPoller(deps) {
|
|
|
39580
40101
|
return;
|
|
39581
40102
|
running = true;
|
|
39582
40103
|
poll(deps).catch((error51) => {
|
|
39583
|
-
|
|
40104
|
+
logger2.error("quota poll failed", {
|
|
39584
40105
|
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
39585
40106
|
});
|
|
39586
40107
|
}).finally(() => {
|
|
@@ -39594,62 +40115,101 @@ async function startQuotaPoller(deps) {
|
|
|
39594
40115
|
}
|
|
39595
40116
|
|
|
39596
40117
|
// apps/gateway/src/index.ts
|
|
39597
|
-
|
|
39598
|
-
|
|
39599
|
-
|
|
39600
|
-
|
|
39601
|
-
|
|
39602
|
-
|
|
39603
|
-
|
|
39604
|
-
|
|
39605
|
-
|
|
39606
|
-
}
|
|
39607
|
-
var
|
|
39608
|
-
|
|
39609
|
-
|
|
39610
|
-
|
|
39611
|
-
|
|
39612
|
-
|
|
39613
|
-
|
|
39614
|
-
|
|
39615
|
-
|
|
39616
|
-
|
|
39617
|
-
|
|
39618
|
-
|
|
39619
|
-
|
|
39620
|
-
|
|
39621
|
-
|
|
39622
|
-
|
|
39623
|
-
|
|
39624
|
-
|
|
39625
|
-
|
|
39626
|
-
|
|
39627
|
-
|
|
39628
|
-
|
|
39629
|
-
|
|
39630
|
-
|
|
39631
|
-
|
|
39632
|
-
|
|
39633
|
-
now
|
|
39634
|
-
});
|
|
39635
|
-
|
|
39636
|
-
|
|
39637
|
-
|
|
39638
|
-
|
|
39639
|
-
|
|
39640
|
-
|
|
39641
|
-
|
|
39642
|
-
|
|
40118
|
+
function stdoutLogger(level) {
|
|
40119
|
+
return createLogger({
|
|
40120
|
+
level,
|
|
40121
|
+
color: Boolean(process.stdout.isTTY),
|
|
40122
|
+
write: (line) => {
|
|
40123
|
+
process.stdout.write(`${line}
|
|
40124
|
+
`);
|
|
40125
|
+
}
|
|
40126
|
+
});
|
|
40127
|
+
}
|
|
40128
|
+
var logger2 = stdoutLogger("info");
|
|
40129
|
+
async function main() {
|
|
40130
|
+
const config2 = loadConfig(process.env);
|
|
40131
|
+
logger2 = stdoutLogger(config2.logLevel);
|
|
40132
|
+
if (config2.logLevelFallbackFrom !== null) {
|
|
40133
|
+
logger2.info("invalid log level; using info", { reason: config2.logLevelFallbackFrom });
|
|
40134
|
+
}
|
|
40135
|
+
logger2.info("omnigateway booting", { host: config2.host, port: config2.port });
|
|
40136
|
+
function dashboardDir() {
|
|
40137
|
+
if (config2.staticDir !== null)
|
|
40138
|
+
return config2.staticDir;
|
|
40139
|
+
const candidates = [
|
|
40140
|
+
resolve2(import.meta.dir, "./public"),
|
|
40141
|
+
resolve2(import.meta.dir, "../../dashboard/dist")
|
|
40142
|
+
];
|
|
40143
|
+
return candidates.find((path) => existsSync(path)) ?? candidates[1];
|
|
40144
|
+
}
|
|
40145
|
+
const encryptionKey = await deriveKey(config2.encryptionKey);
|
|
40146
|
+
const store = await createStore({
|
|
40147
|
+
path: config2.databasePath,
|
|
40148
|
+
encryptionKey,
|
|
40149
|
+
logger: logger2
|
|
40150
|
+
});
|
|
40151
|
+
const swept = await store.usage.sweepPending();
|
|
40152
|
+
if (swept > 0)
|
|
40153
|
+
logger2.info("retired interrupted requests", { count: swept });
|
|
40154
|
+
const now = () => Date.now();
|
|
40155
|
+
const http = nodeHttpClient({ logger: logger2, now });
|
|
40156
|
+
const refresh = createRefresher({ store, providers: OAUTH_PROVIDERS, http, now, logger: logger2 });
|
|
40157
|
+
const staticDir = dashboardDir();
|
|
40158
|
+
logger2.info(existsSync(staticDir) ? "dashboard directory resolved" : "dashboard directory absent", {
|
|
40159
|
+
path: staticDir
|
|
40160
|
+
});
|
|
40161
|
+
const app = createApp({
|
|
40162
|
+
store,
|
|
40163
|
+
baseUrl: config2.baseUrl,
|
|
40164
|
+
http,
|
|
40165
|
+
now,
|
|
40166
|
+
refresh,
|
|
40167
|
+
staticDir,
|
|
40168
|
+
logger: logger2
|
|
40169
|
+
});
|
|
40170
|
+
const stopMaintenance = startMaintenance({ store, now, logger: logger2 });
|
|
40171
|
+
const stopRefreshScheduler = startRefreshScheduler({ store, refresh, now, logger: logger2 });
|
|
40172
|
+
const stopQuotaPoller = await startQuotaPoller({
|
|
40173
|
+
store,
|
|
40174
|
+
providers: OAUTH_PROVIDERS,
|
|
40175
|
+
http,
|
|
40176
|
+
refresh,
|
|
40177
|
+
now,
|
|
40178
|
+
logger: logger2
|
|
40179
|
+
});
|
|
40180
|
+
app.listen({ port: config2.port, hostname: config2.host, idleTimeout: 255 });
|
|
40181
|
+
logger2.info("omnigateway listening", { host: config2.host, port: config2.port });
|
|
40182
|
+
let shuttingDown = false;
|
|
40183
|
+
function exitAfterClosingStore(code) {
|
|
40184
|
+
try {
|
|
40185
|
+
store.close();
|
|
40186
|
+
} finally {
|
|
40187
|
+
process.exit(code);
|
|
40188
|
+
}
|
|
40189
|
+
}
|
|
40190
|
+
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
40191
|
+
process.on(signal, () => {
|
|
40192
|
+
if (shuttingDown)
|
|
40193
|
+
exitAfterClosingStore(1);
|
|
40194
|
+
logger2.info("shutdown requested", { reason: signal });
|
|
40195
|
+
shuttingDown = true;
|
|
40196
|
+
stopMaintenance();
|
|
40197
|
+
stopRefreshScheduler();
|
|
40198
|
+
stopQuotaPoller();
|
|
40199
|
+
app.stop().then(() => exitAfterClosingStore(0), (error51) => {
|
|
40200
|
+
logger2.error("shutdown failed", {
|
|
40201
|
+
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
40202
|
+
});
|
|
40203
|
+
return exitAfterClosingStore(1);
|
|
40204
|
+
});
|
|
40205
|
+
});
|
|
39643
40206
|
}
|
|
39644
40207
|
}
|
|
39645
|
-
|
|
39646
|
-
|
|
39647
|
-
|
|
39648
|
-
|
|
39649
|
-
|
|
39650
|
-
stopMaintenance();
|
|
39651
|
-
stopRefreshScheduler();
|
|
39652
|
-
stopQuotaPoller();
|
|
39653
|
-
app.stop().then(() => exitAfterClosingStore(0), () => exitAfterClosingStore(1));
|
|
40208
|
+
try {
|
|
40209
|
+
await main();
|
|
40210
|
+
} catch (error51) {
|
|
40211
|
+
logger2.error("gateway boot failed", {
|
|
40212
|
+
reason: error51 instanceof Error ? error51.message : "unknown"
|
|
39654
40213
|
});
|
|
40214
|
+
process.exit(1);
|
|
39655
40215
|
}
|