@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.
Files changed (56) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +131 -0
  3. package/client/openrouter-mirror.css +131 -0
  4. package/client/openrouter-mirror.tsx +221 -0
  5. package/dist/client/openrouter-mirror.bundle.js +235 -0
  6. package/dist/client/openrouter-mirror.css +131 -0
  7. package/dist/client/openrouter-mirror.d.ts +1 -0
  8. package/dist/client/openrouter-mirror.js +95 -0
  9. package/dist/client/openrouter-mirror.tsx +221 -0
  10. package/dist/src/cli.d.ts +2 -0
  11. package/dist/src/cli.js +31 -0
  12. package/dist/src/index.d.ts +15 -0
  13. package/dist/src/index.js +66 -0
  14. package/dist/src/openrouter-budget.d.ts +58 -0
  15. package/dist/src/openrouter-budget.js +133 -0
  16. package/dist/src/openrouter-capabilities.d.ts +3 -0
  17. package/dist/src/openrouter-capabilities.js +720 -0
  18. package/dist/src/openrouter-conformance.d.ts +11 -0
  19. package/dist/src/openrouter-conformance.js +53 -0
  20. package/dist/src/openrouter-connector.d.ts +90 -0
  21. package/dist/src/openrouter-connector.js +245 -0
  22. package/dist/src/openrouter-local-generation.d.ts +17 -0
  23. package/dist/src/openrouter-local-generation.js +271 -0
  24. package/dist/src/openrouter-mirror-ui.d.ts +12 -0
  25. package/dist/src/openrouter-mirror-ui.js +78 -0
  26. package/dist/src/openrouter-models.d.ts +39 -0
  27. package/dist/src/openrouter-models.js +78 -0
  28. package/dist/src/openrouter-scenario.d.ts +36 -0
  29. package/dist/src/openrouter-scenario.js +145 -0
  30. package/dist/src/openrouter-server.d.ts +20 -0
  31. package/dist/src/openrouter-server.js +144 -0
  32. package/dist/src/openrouter-stub.d.ts +8 -0
  33. package/dist/src/openrouter-stub.js +58 -0
  34. package/dist/src/openrouter-twin.d.ts +4 -0
  35. package/dist/src/openrouter-twin.js +1506 -0
  36. package/dist/src/openrouter-types.d.ts +50 -0
  37. package/dist/src/openrouter-types.js +1 -0
  38. package/dist/test-fixtures/openrouter-openapi-operations.SOURCE.md +16 -0
  39. package/dist/test-fixtures/openrouter-openapi-operations.json +1041 -0
  40. package/package.json +71 -0
  41. package/src/cli.ts +29 -0
  42. package/src/index.ts +108 -0
  43. package/src/openrouter-budget.ts +159 -0
  44. package/src/openrouter-capabilities.ts +861 -0
  45. package/src/openrouter-conformance.ts +60 -0
  46. package/src/openrouter-connector.ts +264 -0
  47. package/src/openrouter-local-generation.ts +207 -0
  48. package/src/openrouter-mirror-ui.ts +84 -0
  49. package/src/openrouter-models.ts +118 -0
  50. package/src/openrouter-scenario.ts +156 -0
  51. package/src/openrouter-server.ts +158 -0
  52. package/src/openrouter-stub.ts +60 -0
  53. package/src/openrouter-twin.ts +1441 -0
  54. package/src/openrouter-types.ts +49 -0
  55. package/test-fixtures/openrouter-openapi-operations.SOURCE.md +16 -0
  56. package/test-fixtures/openrouter-openapi-operations.json +1041 -0
@@ -0,0 +1,133 @@
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 { declareRateBudget, rateBudgetPath, rateBudgetWeight, RateBudget, } from '@volter/world-core';
45
+ const VENDOR = 'openrouter';
46
+ /** Rolling window, in ms. Matches the per-MINUTE shape of every figure OpenRouter publishes. */
47
+ export const OPENROUTER_BUDGET_WINDOW_MS = 60_000;
48
+ /**
49
+ * Weighted units allowed inside one window. 20 calls/60s — the tightest per-minute figure OpenRouter
50
+ * documents anywhere, adopted wholesale because none of its published figures governs this connector.
51
+ */
52
+ export const OPENROUTER_BUDGET_CEILING = 20;
53
+ /** Seconds. A `Retry-After` above this means the key is throttled hard — fail loudly. */
54
+ export const OPENROUTER_BUDGET_MAX_RETRY_AFTER_S = 300;
55
+ /** Per-call cost, keyed by the `<METHOD> <path>` this connector is about to request. See the header. */
56
+ export const OPENROUTER_CALL_WEIGHTS = {
57
+ /** `GET /api/v1/generation?id=…` — one per pending action, so unbounded in the caller's backlog. */
58
+ generation: 2,
59
+ /** Every other modeled call, including the `/api/v1/models` catalog pull. */
60
+ other: 1,
61
+ };
62
+ /**
63
+ * THE PACK'S DECLARATION — pure data, the only OpenRouter-specific thing in the whole budget. Rules
64
+ * are ordered and first-match-wins, and they are keyed on `"<METHOD> <pathname>"`.
65
+ *
66
+ * Also exported as `pack.rateBudget` (see index.ts), so `registerPack` arms it too.
67
+ */
68
+ export const OPENROUTER_RATE_BUDGET = {
69
+ windowMs: OPENROUTER_BUDGET_WINDOW_MS,
70
+ ceiling: OPENROUTER_BUDGET_CEILING,
71
+ defaultWeight: OPENROUTER_CALL_WEIGHTS.other,
72
+ maxRetryAfterSeconds: OPENROUTER_BUDGET_MAX_RETRY_AFTER_S,
73
+ rules: [
74
+ { match: '^[A-Z]+ /api/v1/generation', weight: OPENROUTER_CALL_WEIGHTS.generation },
75
+ ],
76
+ reason: 'OpenRouter documents a 200 requests/SECOND account surge cap, 20 requests/minute for :free model ' +
77
+ 'variants (plus a credit-dependent daily cap, whose published figure has moved and is deliberately not ' +
78
+ 'restated here), and 30 requests/minute per key on its data API endpoints. NONE governs this ' +
79
+ 'connector exactly — it calls no inference endpoint and not the dataset endpoint, and OpenRouter ' +
80
+ 'publishes no per-minute figure for /api/v1/models or /api/v1/generation. Rather than invent one, this ' +
81
+ 'adopts the tightest documented per-minute figure wholesale: 20 units / 60s at 1 per call, under both ' +
82
+ 'published per-minute limits. /api/v1/generation costs 2 because push issues one per pending action, so ' +
83
+ 'its call count is unbounded in the caller\'s backlog — a judgement call, not a published cost.',
84
+ };
85
+ // Declared at module load, so merely importing this module (which `openrouter-connector.ts` does) is
86
+ // enough to arm the real ceiling. A budget constructed BEFORE this runs falls back to the kernel's
87
+ // DEFAULT_RATE_BUDGET, whose ceiling (60) is HIGHER than this one — so here the fallback is genuinely
88
+ // looser, not merely differently-priced. `RateBudget` reads its policy live precisely so this
89
+ // declaration takes effect the moment it lands, and constructing through the subclass below (which
90
+ // imports this module) is what makes the ordering a non-issue in practice.
91
+ declareRateBudget(VENDOR, OPENROUTER_RATE_BUDGET);
92
+ /** Split a connector path (which carries its own query string) into pathname + parsed query. */
93
+ export function splitOpenRouterPath(path) {
94
+ const q = path.indexOf('?');
95
+ if (q < 0)
96
+ return { pathname: path, query: {} };
97
+ const query = {};
98
+ for (const [k, v] of new URLSearchParams(path.slice(q + 1)))
99
+ query[k] = v;
100
+ return { pathname: path.slice(0, q), query };
101
+ }
102
+ /**
103
+ * Price one call. Keyed off the request the connector is ABOUT to make, so an unclassified endpoint
104
+ * still costs `defaultWeight` — an unknown endpoint must never be free.
105
+ */
106
+ export function openRouterCallWeight(method, path) {
107
+ const { pathname, query } = splitOpenRouterPath(path);
108
+ return rateBudgetWeight(VENDOR, `${(method || 'GET').toUpperCase()} ${pathname}`, query);
109
+ }
110
+ /** Where OpenRouter's ledger lives. Key-keyed and cwd-independent by default (limits are per key /
111
+ * per account, so a cwd-scoped ledger would hand the same key a fresh allowance in every checkout,
112
+ * worktree and CI matrix leg); pass `root` to opt into world-scoped accounting instead. */
113
+ export function openRouterBudgetPath(opts = {}) {
114
+ const o = typeof opts === 'string' ? { root: opts } : opts;
115
+ // VENDOR spread LAST: a loosely-typed `{ vendor: 'other', … }` slipping through (TypeScript's
116
+ // excess-property check only catches object literals) must not redirect this pack's ledger to
117
+ // another vendor's file.
118
+ return rateBudgetPath({ ...o, vendor: VENDOR });
119
+ }
120
+ /**
121
+ * OpenRouter's budget — the shared kernel guard bound to this vendor's declaration. A real subclass,
122
+ * not an alias, so `budget instanceof OpenRouterBudget` in `liveOpenRouterExecute` still means "a
123
+ * budget that accounts against OPENROUTER's ledger under OPENROUTER's ceiling": another vendor's
124
+ * `RateBudget` (with its own, almost certainly larger, ceiling) is NOT assignable there.
125
+ */
126
+ export class OpenRouterBudget extends RateBudget {
127
+ constructor(opts = {}) {
128
+ super({ ...opts, vendor: VENDOR });
129
+ }
130
+ }
131
+ /** The typed refusal. One error class shared with every other vendor's budget; `err.vendor` says
132
+ * which one refused, and `err.kind` says why. */
133
+ export { RateBudgetError as OpenRouterBudgetError } from '@volter/world-core';
@@ -0,0 +1,3 @@
1
+ import { type CapabilityReport, type CapabilitySpec } from '@volter/world-tooling';
2
+ export declare const OPENROUTER_CAPABILITIES: CapabilitySpec[];
3
+ export declare function openrouterCapabilities(): Promise<CapabilityReport>;