@volter/twin-openrouter 0.1.0
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/LICENSE +202 -0
- package/README.md +131 -0
- package/client/openrouter-mirror.css +131 -0
- package/client/openrouter-mirror.tsx +221 -0
- package/dist/client/openrouter-mirror.bundle.js +235 -0
- package/dist/client/openrouter-mirror.css +131 -0
- package/dist/client/openrouter-mirror.d.ts +1 -0
- package/dist/client/openrouter-mirror.js +95 -0
- package/dist/client/openrouter-mirror.tsx +221 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +31 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.js +66 -0
- package/dist/src/openrouter-budget.d.ts +58 -0
- package/dist/src/openrouter-budget.js +133 -0
- package/dist/src/openrouter-capabilities.d.ts +3 -0
- package/dist/src/openrouter-capabilities.js +720 -0
- package/dist/src/openrouter-conformance.d.ts +11 -0
- package/dist/src/openrouter-conformance.js +53 -0
- package/dist/src/openrouter-connector.d.ts +90 -0
- package/dist/src/openrouter-connector.js +245 -0
- package/dist/src/openrouter-local-generation.d.ts +17 -0
- package/dist/src/openrouter-local-generation.js +271 -0
- package/dist/src/openrouter-mirror-ui.d.ts +12 -0
- package/dist/src/openrouter-mirror-ui.js +78 -0
- package/dist/src/openrouter-models.d.ts +39 -0
- package/dist/src/openrouter-models.js +78 -0
- package/dist/src/openrouter-scenario.d.ts +36 -0
- package/dist/src/openrouter-scenario.js +145 -0
- package/dist/src/openrouter-server.d.ts +20 -0
- package/dist/src/openrouter-server.js +144 -0
- package/dist/src/openrouter-stub.d.ts +8 -0
- package/dist/src/openrouter-stub.js +58 -0
- package/dist/src/openrouter-twin.d.ts +4 -0
- package/dist/src/openrouter-twin.js +1506 -0
- package/dist/src/openrouter-types.d.ts +50 -0
- package/dist/src/openrouter-types.js +1 -0
- package/dist/test-fixtures/openrouter-openapi-operations.SOURCE.md +16 -0
- package/dist/test-fixtures/openrouter-openapi-operations.json +1041 -0
- package/package.json +71 -0
- package/src/cli.ts +29 -0
- package/src/index.ts +108 -0
- package/src/openrouter-budget.ts +159 -0
- package/src/openrouter-capabilities.ts +861 -0
- package/src/openrouter-conformance.ts +60 -0
- package/src/openrouter-connector.ts +264 -0
- package/src/openrouter-local-generation.ts +207 -0
- package/src/openrouter-mirror-ui.ts +84 -0
- package/src/openrouter-models.ts +118 -0
- package/src/openrouter-scenario.ts +156 -0
- package/src/openrouter-server.ts +158 -0
- package/src/openrouter-stub.ts +60 -0
- package/src/openrouter-twin.ts +1441 -0
- package/src/openrouter-types.ts +49 -0
- package/test-fixtures/openrouter-openapi-operations.SOURCE.md +16 -0
- package/test-fixtures/openrouter-openapi-operations.json +1041 -0
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@volter/twin-openrouter",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local OpenRouter twin — a faithful, stateful local OpenRouter API your OpenAI-compatible SDK talks to unmodified. Chat completions and streaming return deterministic labeled stubs, generation stats and key/credits/model surfaces are local. Built on @volter/world-core.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"twin",
|
|
7
|
+
"local",
|
|
8
|
+
"mock",
|
|
9
|
+
"mirror",
|
|
10
|
+
"simulator",
|
|
11
|
+
"sdk",
|
|
12
|
+
"api",
|
|
13
|
+
"openrouter",
|
|
14
|
+
"llm"
|
|
15
|
+
],
|
|
16
|
+
"author": "Volter (https://github.com/volter-ai)",
|
|
17
|
+
"license": "Apache-2.0",
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"client",
|
|
21
|
+
"test-fixtures",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"!**/*.test.ts",
|
|
25
|
+
"!**/*.test.tsx",
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/volter-ai/twin.git",
|
|
31
|
+
"directory": "packages/twin/openrouter"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/volter-ai/twin/tree/main/packages/twin/openrouter#readme",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/src/index.d.ts",
|
|
38
|
+
"default": "./dist/src/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"bin": {
|
|
42
|
+
"world-openrouter": "dist/src/cli.js"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "bun test src/*.test.ts",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"build": "node ../../../scripts/publish/build.mjs",
|
|
48
|
+
"prepack": "node ../../../scripts/publish/prepare-publish.mjs prepack",
|
|
49
|
+
"postpack": "node ../../../scripts/publish/prepare-publish.mjs postpack"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"react": "^19.2.7",
|
|
53
|
+
"react-dom": "^19.2.7"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@volter/world-core": "2.0.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@volter/world-core": "2.0.0",
|
|
60
|
+
"@volter/world-tooling": "0.1.0",
|
|
61
|
+
"openai": "^6.16.0",
|
|
62
|
+
"@types/bun": "^1.2.20",
|
|
63
|
+
"@types/node": "^24.0.0",
|
|
64
|
+
"@types/react": "^19.2.17",
|
|
65
|
+
"@types/react-dom": "^19.2.3",
|
|
66
|
+
"typescript": "^5.9.0"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=22.3"
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { keepProcessAlive } from '@volter/world-core/lifecycle';
|
|
3
|
+
import { hasFlag, optionValue } from '@volter/world-core/args';
|
|
4
|
+
import { createOpenRouterTwinServer } from './openrouter-server.ts';
|
|
5
|
+
import { createOpenRouterMirrorServer } from './openrouter-mirror-ui.ts';
|
|
6
|
+
|
|
7
|
+
const [cmd, ...rest] = process.argv.slice(2);
|
|
8
|
+
const port = Number(optionValue(rest, '--port', '0')) || undefined;
|
|
9
|
+
const root = optionValue(rest, '--root') || undefined;
|
|
10
|
+
const scenario = optionValue(rest, '--scenario') || undefined;
|
|
11
|
+
const localGenerationUrl = hasFlag(rest, '--local-generation-url') ? optionValue(rest, '--local-generation-url') : undefined;
|
|
12
|
+
const readOnly = hasFlag(rest, '--read-only');
|
|
13
|
+
|
|
14
|
+
if (cmd === 'serve') {
|
|
15
|
+
const server = await createOpenRouterTwinServer({ readOnly, ...(root ? { root } : {}), ...(port ? { port } : {}), ...(scenario ? { scenarioPath: scenario } : {}), ...(localGenerationUrl !== undefined ? { localGenerationUrl } : {}) });
|
|
16
|
+
process.stdout.write(`openrouter twin (${localGenerationUrl !== undefined || process.env.TWIN_OPENROUTER_LOCAL_GENERATION_URL !== undefined ? 'optional local generation' : 'deterministic stub output'})${readOnly ? ' [read-only]' : ''}${scenario ? ' [scenario]' : ''} at http://127.0.0.1:${server.port}\n`);
|
|
17
|
+
await keepProcessAlive();
|
|
18
|
+
} else if (cmd === 'mirror') {
|
|
19
|
+
const server = await createOpenRouterMirrorServer({ ...(root ? { root } : {}), ...(port ? { port } : {}) });
|
|
20
|
+
process.stdout.write(`openrouter mirror UI at http://127.0.0.1:${server.port}\n`);
|
|
21
|
+
await keepProcessAlive();
|
|
22
|
+
} else if (cmd === 'conformance') {
|
|
23
|
+
const { checkOpenRouterConformance } = await import('./openrouter-conformance.ts');
|
|
24
|
+
const report = await checkOpenRouterConformance({ ...(root ? { root } : {}) });
|
|
25
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
26
|
+
if (!report.ok) process.exitCode = 1;
|
|
27
|
+
} else {
|
|
28
|
+
process.stdout.write('Usage: world-openrouter serve|mirror|conformance [--port N] [--root DIR] [--read-only] [--scenario FILE] [--local-generation-url http://127.0.0.1:PORT]\n');
|
|
29
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
export { handleOpenRouterTwinRequest } from './openrouter-twin.ts';
|
|
2
|
+
export type { OpenRouterRequest, OpenRouterResponse, SseEvent, SseSink, GenerationStat } from './openrouter-types.ts';
|
|
3
|
+
export { createOpenRouterTwinFetch, createOpenRouterTwinServer, type OpenRouterTwinFetchOptions } from './openrouter-server.ts';
|
|
4
|
+
export { OPENROUTER_MODELS, OPENROUTER_PROVIDERS, findOpenRouterModel } from './openrouter-models.ts';
|
|
5
|
+
export type { OpenRouterModel, OpenRouterProvider } from './openrouter-models.ts';
|
|
6
|
+
export { contentToText, countPromptTokens, estimateTokens, lastUserText, stableHash, stubAssistantText } from './openrouter-stub.ts';
|
|
7
|
+
// The pack's HALF of the scenario system (the grammar is the kernel's ONE engine). Exported so a
|
|
8
|
+
// test can build an engine and `use()` handlers in-process, and so the fetch adapter can mount it.
|
|
9
|
+
export {
|
|
10
|
+
createOpenRouterScenarioEngine,
|
|
11
|
+
loadOpenRouterScenarioDocument,
|
|
12
|
+
openrouterScenarioAdapter,
|
|
13
|
+
realizeOpenRouterRespond,
|
|
14
|
+
} from './openrouter-scenario.ts';
|
|
15
|
+
export type {
|
|
16
|
+
OpenRouterScenarioEngine,
|
|
17
|
+
OpenRouterScenarioRequest,
|
|
18
|
+
OpenRouterScenarioRespond,
|
|
19
|
+
ScenarioToolCall,
|
|
20
|
+
} from './openrouter-scenario.ts';
|
|
21
|
+
export { buildOpenRouterMirrorClient, createOpenRouterMirrorServer, openrouterMirrorHtml } from './openrouter-mirror-ui.ts';
|
|
22
|
+
export {
|
|
23
|
+
OPENROUTER_API_BASE,
|
|
24
|
+
liveOpenRouterExecute,
|
|
25
|
+
openRouterRequestForAction,
|
|
26
|
+
pullOpenRouterState,
|
|
27
|
+
pushOpenRouterAction,
|
|
28
|
+
pushPendingOpenRouterActions,
|
|
29
|
+
syncOpenRouterFromReal,
|
|
30
|
+
} from './openrouter-connector.ts';
|
|
31
|
+
export type { OpenRouterExecute } from './openrouter-connector.ts';
|
|
32
|
+
// The client-side rate budget — the fail-closed backstop `liveOpenRouterExecute` routes every live
|
|
33
|
+
// request through. The mechanism is the kernel's (`@volter/world-core` -> rateBudget.ts); these are this
|
|
34
|
+
// vendor's numbers and the typed bindings around them.
|
|
35
|
+
export {
|
|
36
|
+
OPENROUTER_BUDGET_CEILING,
|
|
37
|
+
OPENROUTER_BUDGET_MAX_RETRY_AFTER_S,
|
|
38
|
+
OPENROUTER_BUDGET_WINDOW_MS,
|
|
39
|
+
OPENROUTER_CALL_WEIGHTS,
|
|
40
|
+
OPENROUTER_RATE_BUDGET,
|
|
41
|
+
OpenRouterBudget,
|
|
42
|
+
OpenRouterBudgetError,
|
|
43
|
+
openRouterBudgetPath,
|
|
44
|
+
openRouterCallWeight,
|
|
45
|
+
splitOpenRouterPath,
|
|
46
|
+
} from './openrouter-budget.ts';
|
|
47
|
+
export type {
|
|
48
|
+
OpenRouterBudgetErrorKind,
|
|
49
|
+
OpenRouterBudgetOptions,
|
|
50
|
+
OpenRouterBudgetReservation,
|
|
51
|
+
OpenRouterBudgetSnapshot,
|
|
52
|
+
} from './openrouter-budget.ts';
|
|
53
|
+
|
|
54
|
+
import { registerPack, type TwinPack } from '@volter/world-core';
|
|
55
|
+
import { performOpenRouterAction, syncOpenRouterFromRemote } from './openrouter-connector.ts';
|
|
56
|
+
import { OPENROUTER_RATE_BUDGET as RATE_BUDGET } from './openrouter-budget.ts';
|
|
57
|
+
|
|
58
|
+
export const pack: TwinPack = {
|
|
59
|
+
// PROTOCOL 2 (docs/contributing/architecture.md#protocol-2-the-pack-is-a-plugin): the pack is a plugin — its wire, its tree, and its half of
|
|
60
|
+
// the real state system. Moved 2026-09-08.
|
|
61
|
+
protocol: '2',
|
|
62
|
+
// OpenRouter publishes no webhooks for the provisioning surface, so the refresh is a poll. Five
|
|
63
|
+
// minutes is the catalog's cadence, and the pull is two calls (the model catalog, the account's
|
|
64
|
+
// keys) which the rate budget counts as two.
|
|
65
|
+
refresh: { every: '5m', webhook: false, onDemand: { atMost: '30s' } },
|
|
66
|
+
stateSystem: { perform: performOpenRouterAction, refresh: syncOpenRouterFromRemote },
|
|
67
|
+
// The round trip is a provisioning-key create — OpenRouter's own write, and the ONE part of an
|
|
68
|
+
// account this twin both writes and reads back (`GET /api/v1/keys` is the pull's second domain).
|
|
69
|
+
// The key's hash is minted from the name AND the write's instant, so a second send on a branch
|
|
70
|
+
// is a second key rather than a refused duplicate.
|
|
71
|
+
roundTrip: { method: 'POST', path: '/api/v1/keys', body: { name: 'round trip' }, headers: { authorization: 'Bearer round-trip' } },
|
|
72
|
+
parityOrigin: 'http://twin',
|
|
73
|
+
vendor: 'openrouter',
|
|
74
|
+
// The SAME object openrouter-budget.ts declares at module load — one source of truth, so
|
|
75
|
+
// registering the pack and importing the connector arm identical numbers.
|
|
76
|
+
rateBudget: RATE_BUDGET,
|
|
77
|
+
transport: 'rest',
|
|
78
|
+
archetype: 'generative',
|
|
79
|
+
bin: 'world-openrouter',
|
|
80
|
+
// The subject types the twin SERVES from its own state — the R2 resource-level claim.
|
|
81
|
+
// `model` is deliberately NOT here: it is a connector-side projection that only
|
|
82
|
+
// `pullOpenRouterState` folds in from a real OpenRouter through an injected client, and no
|
|
83
|
+
// route reads it back — `GET /v1/models`, `/v1/models/count`, `/v1/models/:id` and
|
|
84
|
+
// `/:id/endpoints` all serve the static OPENROUTER_MODELS catalog (openrouter-models.ts),
|
|
85
|
+
// never a stored subject (the ai-gateway `model`/`credits` precedent).
|
|
86
|
+
resources: ['generation', 'api_key', 'byok', 'preset'],
|
|
87
|
+
specSource: 'OpenRouter API reference (OpenAI-compatible chat completions + API Reference surfaces; model output is a deterministic stub)',
|
|
88
|
+
description: 'OpenRouter API twin — OpenAI-compatible chat completions/streaming, models, key/credits/generation stats; generative output is a labeled deterministic stub.',
|
|
89
|
+
// Adoption + interception, moved off the central maps unchanged (descriptor-first back-migration, adding-a-twin.md §3,
|
|
90
|
+
// 2026-08-31).
|
|
91
|
+
adoption: {
|
|
92
|
+
// OpenRouter's official Python SDK (OpenRouterTeam/python-sdk). An `openai` client pointed at
|
|
93
|
+
// openrouter.ai is NOT claimed here - that distribution is the openai pack's, and
|
|
94
|
+
// OPENROUTER_BASE_URL is what routes it.
|
|
95
|
+
pypi: ['openrouter'],
|
|
96
|
+
sdks: ['@openrouter/ai-sdk-provider'],
|
|
97
|
+
scopes: ['@openrouter/'],
|
|
98
|
+
// 'OR' — OpenRouter's own documented short prefix, the trio LiteLLM ships in its .env.example
|
|
99
|
+
// under a literal `# OpenRouter` heading (OR_SITE_URL / OR_APP_NAME / OR_API_KEY). Only a var
|
|
100
|
+
// whose WHOLE prefix is `OR` reaches this stem, the same narrowness that lets the github pack
|
|
101
|
+
// claim 'GH' (ladder classification 2026-09-02: litellm detected no openrouter without it).
|
|
102
|
+
envStems: ['OPENROUTER', 'OR'],
|
|
103
|
+
},
|
|
104
|
+
hosts: [{ host: 'openrouter.ai' }],
|
|
105
|
+
browserRouting: { apiPathPrefix: '/api/v1/', loaderHost: 'https://openrouter.ai' },
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
registerPack(pack);
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// OpenRouter's CLIENT-SIDE RATE BUDGET — the pack's DECLARATION (the numbers) plus the thin typed
|
|
2
|
+
// bindings `liveOpenRouterExecute` uses. The MECHANISM — the durable token-keyed ledger, the rolling
|
|
3
|
+
// window, reserve-under-lock, the `Retry-After`/429 cooldown, fail-CLOSED on a corrupt ledger — lives
|
|
4
|
+
// ONCE in the vendor-agnostic kernel (`@volter/world-core` → `rateBudget.ts`). Read that module's header
|
|
5
|
+
// for the full rationale AND for the honest list of what the guard does not guarantee (an injected
|
|
6
|
+
// clock or ledger path still defeats it — it guards carelessness, not malice).
|
|
7
|
+
//
|
|
8
|
+
// ── WHY THIS EXISTS: THE MISSING CHOKE POINT ─────────────────────────────────────────────────
|
|
9
|
+
// A ~4.5-day Figma token lockout (2026-07-25) came from a burst of raw vendor calls made OUTSIDE any
|
|
10
|
+
// guarded client. The lesson generalized: a pack with no single construction site for its live client
|
|
11
|
+
// is a pack that CANNOT be protected, because there is nowhere for a guard to sit.
|
|
12
|
+
//
|
|
13
|
+
// This connector took an INJECTED `OpenRouterExecute` and nothing more. Excellent for testing — every
|
|
14
|
+
// verify runs offline against a fake — but it meant the pack never built a network-calling execute, so
|
|
15
|
+
// a rate budget could only ever be something the CALLER opted into. `liveOpenRouterExecute` (in
|
|
16
|
+
// `openrouter-connector.ts`) is the choke point that fixes that: the ONE place in this pack that turns
|
|
17
|
+
// a credential into a function that really talks to openrouter.ai, with this budget charged BEFORE
|
|
18
|
+
// every request goes out. The injected-function contract is untouched — the factory is an ADDITION.
|
|
19
|
+
//
|
|
20
|
+
// ── HOW THE CEILING WAS CHOSEN, AND WHAT IS HONESTLY UNKNOWN ──────────────────────────────────
|
|
21
|
+
// OpenRouter publishes three figures (openrouter.ai/docs/api_reference/limits and the endpoint docs):
|
|
22
|
+
// • a 200 requests/SECOND account surge cap, for every user, as DoS defence;
|
|
23
|
+
// • 20 requests/minute for `:free` model variants, plus a daily cap that depends on how much credit
|
|
24
|
+
// has been purchased (the published daily figure has moved; only the 20/minute is used below);
|
|
25
|
+
// • 30 requests/minute per key + 500/day per account on data API endpoints (e.g. app rankings).
|
|
26
|
+
//
|
|
27
|
+
// NONE of them governs this connector exactly. It calls no inference endpoint (so the `:free` limit is
|
|
28
|
+
// not its limit) and not the dataset endpoint (so the 30/minute figure is not its limit either), and
|
|
29
|
+
// OpenRouter publishes no per-minute figure for `/api/v1/models` or `/api/v1/generation`. That is
|
|
30
|
+
// stated plainly rather than papered over.
|
|
31
|
+
//
|
|
32
|
+
// Given no governing figure, the budget adopts the TIGHTEST documented per-minute number wholesale:
|
|
33
|
+
// 20 weighted units per 60s at 1 per call. That is under both published per-minute limits, four
|
|
34
|
+
// orders of magnitude under the surge cap, and far more than the connector's real workload (a catalog
|
|
35
|
+
// pull is ONE call; a push is one call per pending action). If a future caller needs more, the pack
|
|
36
|
+
// declares more — with the figure that justifies it.
|
|
37
|
+
//
|
|
38
|
+
// ── HOW THE WEIGHTS WERE CHOSEN ──────────────────────────────────────────────────────────────
|
|
39
|
+
// OpenRouter counts REQUESTS, so weight 1 is the faithful price and the catalog pull gets it.
|
|
40
|
+
// `/api/v1/generation` costs 2 — not because OpenRouter charges more (it does not) but because it is
|
|
41
|
+
// the only endpoint here whose call count is UNBOUNDED IN THE CALLER'S DATA: `pushPendingOpenRouterActions`
|
|
42
|
+
// issues one per pending `generation.record` action, so a backlog turns into a burst. Doubling its
|
|
43
|
+
// price halves the depth at which that runaway is stopped. A judgement call, not a published cost.
|
|
44
|
+
import {
|
|
45
|
+
declareRateBudget,
|
|
46
|
+
rateBudgetPath,
|
|
47
|
+
rateBudgetWeight,
|
|
48
|
+
RateBudget,
|
|
49
|
+
type RateBudgetDeclaration,
|
|
50
|
+
type RateBudgetOptions,
|
|
51
|
+
type RateBudgetReservation,
|
|
52
|
+
type RateBudgetSnapshot,
|
|
53
|
+
} from '@volter/world-core';
|
|
54
|
+
|
|
55
|
+
const VENDOR = 'openrouter';
|
|
56
|
+
|
|
57
|
+
/** Rolling window, in ms. Matches the per-MINUTE shape of every figure OpenRouter publishes. */
|
|
58
|
+
export const OPENROUTER_BUDGET_WINDOW_MS = 60_000;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Weighted units allowed inside one window. 20 calls/60s — the tightest per-minute figure OpenRouter
|
|
62
|
+
* documents anywhere, adopted wholesale because none of its published figures governs this connector.
|
|
63
|
+
*/
|
|
64
|
+
export const OPENROUTER_BUDGET_CEILING = 20;
|
|
65
|
+
|
|
66
|
+
/** Seconds. A `Retry-After` above this means the key is throttled hard — fail loudly. */
|
|
67
|
+
export const OPENROUTER_BUDGET_MAX_RETRY_AFTER_S = 300;
|
|
68
|
+
|
|
69
|
+
/** Per-call cost, keyed by the `<METHOD> <path>` this connector is about to request. See the header. */
|
|
70
|
+
export const OPENROUTER_CALL_WEIGHTS = {
|
|
71
|
+
/** `GET /api/v1/generation?id=…` — one per pending action, so unbounded in the caller's backlog. */
|
|
72
|
+
generation: 2,
|
|
73
|
+
/** Every other modeled call, including the `/api/v1/models` catalog pull. */
|
|
74
|
+
other: 1,
|
|
75
|
+
} as const;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* THE PACK'S DECLARATION — pure data, the only OpenRouter-specific thing in the whole budget. Rules
|
|
79
|
+
* are ordered and first-match-wins, and they are keyed on `"<METHOD> <pathname>"`.
|
|
80
|
+
*
|
|
81
|
+
* Also exported as `pack.rateBudget` (see index.ts), so `registerPack` arms it too.
|
|
82
|
+
*/
|
|
83
|
+
export const OPENROUTER_RATE_BUDGET: RateBudgetDeclaration = {
|
|
84
|
+
windowMs: OPENROUTER_BUDGET_WINDOW_MS,
|
|
85
|
+
ceiling: OPENROUTER_BUDGET_CEILING,
|
|
86
|
+
defaultWeight: OPENROUTER_CALL_WEIGHTS.other,
|
|
87
|
+
maxRetryAfterSeconds: OPENROUTER_BUDGET_MAX_RETRY_AFTER_S,
|
|
88
|
+
rules: [
|
|
89
|
+
{ match: '^[A-Z]+ /api/v1/generation', weight: OPENROUTER_CALL_WEIGHTS.generation },
|
|
90
|
+
],
|
|
91
|
+
reason:
|
|
92
|
+
'OpenRouter documents a 200 requests/SECOND account surge cap, 20 requests/minute for :free model ' +
|
|
93
|
+
'variants (plus a credit-dependent daily cap, whose published figure has moved and is deliberately not ' +
|
|
94
|
+
'restated here), and 30 requests/minute per key on its data API endpoints. NONE governs this ' +
|
|
95
|
+
'connector exactly — it calls no inference endpoint and not the dataset endpoint, and OpenRouter ' +
|
|
96
|
+
'publishes no per-minute figure for /api/v1/models or /api/v1/generation. Rather than invent one, this ' +
|
|
97
|
+
'adopts the tightest documented per-minute figure wholesale: 20 units / 60s at 1 per call, under both ' +
|
|
98
|
+
'published per-minute limits. /api/v1/generation costs 2 because push issues one per pending action, so ' +
|
|
99
|
+
'its call count is unbounded in the caller\'s backlog — a judgement call, not a published cost.',
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Declared at module load, so merely importing this module (which `openrouter-connector.ts` does) is
|
|
103
|
+
// enough to arm the real ceiling. A budget constructed BEFORE this runs falls back to the kernel's
|
|
104
|
+
// DEFAULT_RATE_BUDGET, whose ceiling (60) is HIGHER than this one — so here the fallback is genuinely
|
|
105
|
+
// looser, not merely differently-priced. `RateBudget` reads its policy live precisely so this
|
|
106
|
+
// declaration takes effect the moment it lands, and constructing through the subclass below (which
|
|
107
|
+
// imports this module) is what makes the ordering a non-issue in practice.
|
|
108
|
+
declareRateBudget(VENDOR, OPENROUTER_RATE_BUDGET);
|
|
109
|
+
|
|
110
|
+
/** Split a connector path (which carries its own query string) into pathname + parsed query. */
|
|
111
|
+
export function splitOpenRouterPath(path: string): { pathname: string; query: Record<string, string> } {
|
|
112
|
+
const q = path.indexOf('?');
|
|
113
|
+
if (q < 0) return { pathname: path, query: {} };
|
|
114
|
+
const query: Record<string, string> = {};
|
|
115
|
+
for (const [k, v] of new URLSearchParams(path.slice(q + 1))) query[k] = v;
|
|
116
|
+
return { pathname: path.slice(0, q), query };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Price one call. Keyed off the request the connector is ABOUT to make, so an unclassified endpoint
|
|
121
|
+
* still costs `defaultWeight` — an unknown endpoint must never be free.
|
|
122
|
+
*/
|
|
123
|
+
export function openRouterCallWeight(method: string, path: string): number {
|
|
124
|
+
const { pathname, query } = splitOpenRouterPath(path);
|
|
125
|
+
return rateBudgetWeight(VENDOR, `${(method || 'GET').toUpperCase()} ${pathname}`, query);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Where OpenRouter's ledger lives. Key-keyed and cwd-independent by default (limits are per key /
|
|
129
|
+
* per account, so a cwd-scoped ledger would hand the same key a fresh allowance in every checkout,
|
|
130
|
+
* worktree and CI matrix leg); pass `root` to opt into world-scoped accounting instead. */
|
|
131
|
+
export function openRouterBudgetPath(opts: { root?: string; token?: string } | string = {}): string {
|
|
132
|
+
const o = typeof opts === 'string' ? { root: opts } : opts;
|
|
133
|
+
// VENDOR spread LAST: a loosely-typed `{ vendor: 'other', … }` slipping through (TypeScript's
|
|
134
|
+
// excess-property check only catches object literals) must not redirect this pack's ledger to
|
|
135
|
+
// another vendor's file.
|
|
136
|
+
return rateBudgetPath({ ...o, vendor: VENDOR });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Construction options for OpenRouter's budget. The vendor is fixed; everything else may only TIGHTEN. */
|
|
140
|
+
export type OpenRouterBudgetOptions = Omit<RateBudgetOptions, 'vendor'>;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* OpenRouter's budget — the shared kernel guard bound to this vendor's declaration. A real subclass,
|
|
144
|
+
* not an alias, so `budget instanceof OpenRouterBudget` in `liveOpenRouterExecute` still means "a
|
|
145
|
+
* budget that accounts against OPENROUTER's ledger under OPENROUTER's ceiling": another vendor's
|
|
146
|
+
* `RateBudget` (with its own, almost certainly larger, ceiling) is NOT assignable there.
|
|
147
|
+
*/
|
|
148
|
+
export class OpenRouterBudget extends RateBudget {
|
|
149
|
+
constructor(opts: OpenRouterBudgetOptions = {}) {
|
|
150
|
+
super({ ...opts, vendor: VENDOR });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The typed refusal. One error class shared with every other vendor's budget; `err.vendor` says
|
|
155
|
+
* which one refused, and `err.kind` says why. */
|
|
156
|
+
export { RateBudgetError as OpenRouterBudgetError } from '@volter/world-core';
|
|
157
|
+
export type { RateBudgetErrorKind as OpenRouterBudgetErrorKind } from '@volter/world-core';
|
|
158
|
+
export type OpenRouterBudgetReservation = RateBudgetReservation;
|
|
159
|
+
export type OpenRouterBudgetSnapshot = RateBudgetSnapshot;
|