@volter/twin-xai 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 +246 -0
  3. package/client/xai-device-auth.css +246 -0
  4. package/client/xai-device-auth.tsx +138 -0
  5. package/dist/client/xai-device-auth.bundle.js +18 -0
  6. package/dist/client/xai-device-auth.css +246 -0
  7. package/dist/client/xai-device-auth.d.ts +19 -0
  8. package/dist/client/xai-device-auth.js +50 -0
  9. package/dist/client/xai-device-auth.tsx +138 -0
  10. package/dist/src/cli.d.ts +2 -0
  11. package/dist/src/cli.js +28 -0
  12. package/dist/src/index.d.ts +17 -0
  13. package/dist/src/index.js +70 -0
  14. package/dist/src/xai-budget.d.ts +60 -0
  15. package/dist/src/xai-budget.js +139 -0
  16. package/dist/src/xai-capabilities.d.ts +4 -0
  17. package/dist/src/xai-capabilities.js +1072 -0
  18. package/dist/src/xai-conformance.d.ts +13 -0
  19. package/dist/src/xai-conformance.js +148 -0
  20. package/dist/src/xai-connector.d.ts +82 -0
  21. package/dist/src/xai-connector.js +174 -0
  22. package/dist/src/xai-device-auth-css.gen.d.ts +1 -0
  23. package/dist/src/xai-device-auth-css.gen.js +6 -0
  24. package/dist/src/xai-device-auth-ui.d.ts +13 -0
  25. package/dist/src/xai-device-auth-ui.js +72 -0
  26. package/dist/src/xai-models.d.ts +57 -0
  27. package/dist/src/xai-models.js +102 -0
  28. package/dist/src/xai-oauth.d.ts +30 -0
  29. package/dist/src/xai-oauth.js +279 -0
  30. package/dist/src/xai-scenario.d.ts +33 -0
  31. package/dist/src/xai-scenario.js +139 -0
  32. package/dist/src/xai-server.d.ts +36 -0
  33. package/dist/src/xai-server.js +232 -0
  34. package/dist/src/xai-stub.d.ts +69 -0
  35. package/dist/src/xai-stub.js +210 -0
  36. package/dist/src/xai-twin.d.ts +89 -0
  37. package/dist/src/xai-twin.js +883 -0
  38. package/dist/src/xai-types.d.ts +118 -0
  39. package/dist/src/xai-types.js +6 -0
  40. package/package.json +76 -0
  41. package/src/cli.ts +27 -0
  42. package/src/index.ts +120 -0
  43. package/src/xai-budget.ts +165 -0
  44. package/src/xai-capabilities.ts +1046 -0
  45. package/src/xai-conformance.ts +136 -0
  46. package/src/xai-connector.ts +212 -0
  47. package/src/xai-device-auth-css.gen.ts +6 -0
  48. package/src/xai-device-auth-ui.ts +90 -0
  49. package/src/xai-journey.uitest.ts +155 -0
  50. package/src/xai-models.ts +154 -0
  51. package/src/xai-oauth.ts +301 -0
  52. package/src/xai-scenario.ts +148 -0
  53. package/src/xai-server.ts +258 -0
  54. package/src/xai-stub.ts +213 -0
  55. package/src/xai-twin.ts +960 -0
  56. package/src/xai-types.ts +111 -0
@@ -0,0 +1,139 @@
1
+ // xAI's CLIENT-SIDE RATE BUDGET — the pack's DECLARATION (the numbers) plus the thin typed bindings
2
+ // `liveXaiExecute` uses. The MECHANISM — the durable token-keyed ledger, the rolling window,
3
+ // reserve-under-lock, the `Retry-After`/429 cooldown, fail-CLOSED on a corrupt ledger — lives ONCE
4
+ // in the vendor-agnostic kernel (`@volter/world-core` → `rateBudget.ts`). Read that module's header for
5
+ // the full rationale AND for the honest list of what the guard does not guarantee (an injected clock
6
+ // or ledger path still defeats it — it guards carelessness, not malice).
7
+ //
8
+ // ── WHY THIS EXISTS ──────────────────────────────────────────────────────────────────────────
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. `liveXaiExecute` was exactly that shape: this pack's one construction site for a
11
+ // real client, and a BARE `fetch` with the credential attached as `Authorization: Bearer` and
12
+ // NOTHING in front of it — no ceiling, no cooldown, no ledger. `scripts/rate-budget-coverage.test.ts`
13
+ // is the gate that found it; this module is the fix.
14
+ //
15
+ // ── HOW THE CEILING WAS CHOSEN: xAI PUBLISHES NO SCALAR LIMIT AT ALL ─────────────────────────
16
+ // This is a DISCLAIMER, not a model of a limit, and it is a stronger disclaimer than gemini's.
17
+ //
18
+ // xAI's public documentation states no rate limit anywhere that could bind this connector: neither
19
+ // the API reference (docs.x.ai/docs/api-reference), nor the models page (docs.x.ai/docs/models,
20
+ // which carries pricing and capability tables but no RPM/RPS/TPM figures), nor the overview or
21
+ // quickstart pages publish a requests-per-minute, requests-per-second or tokens-per-minute number,
22
+ // and none of them even direct the reader to a dashboard where one could be read. (The URL commonly
23
+ // cited for this, docs.x.ai/docs/consumption-and-rate-limits, returns 404.)
24
+ //
25
+ // So there is no vendor figure to size against, and the rule for that case is to SAY SO and pin to
26
+ // the kernel's austere fallback rather than borrow an adjacent-but-different number. Copying, say,
27
+ // OpenAI's tier limits because xAI's API is OpenAI-chat-compatible would be exactly the fabrication
28
+ // this rule exists to prevent — protocol compatibility is not quota compatibility.
29
+ //
30
+ // Therefore: 60 weighted units per 60s at a default weight of 2 — EXACTLY `DEFAULT_RATE_BUDGET`.
31
+ // 30 calls a minute, one every two seconds. Comfortably above any real pull (the whole pull surface
32
+ // is TWO calls: the flat catalog and the rich one) and far below the shape that causes lockouts.
33
+ // Nothing here is more permissive than the fallback.
34
+ //
35
+ // ── HOW THE WEIGHTS WERE CHOSEN: THEY ARE FLAT, DELIBERATELY ─────────────────────────────────
36
+ // There are NO pricing rules, and their absence is a decision rather than an omission.
37
+ //
38
+ // `XaiExecute` is typed `method: 'GET'` — the pull surface is read-only catalogs
39
+ // (`/v1/models`, `/v1/language-models`) and the push surface REFUSES LOUDLY for every operation,
40
+ // because the xAI API has no client-writable resources. So no caller of this execute can reach the
41
+ // token-billed inference endpoints (`POST /v1/chat/completions`, `/v1/messages`), and there is no
42
+ // endpoint in reach that is credibly dearer than another.
43
+ //
44
+ // Inventing a ratio anyway — pricing some GET at 4 "to be safe" — would be a made-up number wearing
45
+ // the costume of a vendor fact, which is precisely what the anti-fabrication rule forbids. Every
46
+ // call therefore costs the same 2, and nothing is free.
47
+ import { declareRateBudget, rateBudgetPath, rateBudgetWeight, RateBudget, } from '@volter/world-core';
48
+ const VENDOR = 'xai';
49
+ /** Rolling window, in ms. The fallback's, because xAI publishes no per-minute figure to match. */
50
+ export const XAI_BUDGET_WINDOW_MS = 60_000;
51
+ /**
52
+ * Weighted units allowed inside one window. 60/60s at the default weight of 2 = 30 calls a minute —
53
+ * the kernel's austere fallback exactly, adopted deliberately because xAI publishes no scalar limit.
54
+ */
55
+ export const XAI_BUDGET_CEILING = 60;
56
+ /** Seconds. A `Retry-After` above this means the key is throttled hard — fail loudly, don't sleep. */
57
+ export const XAI_BUDGET_MAX_RETRY_AFTER_S = 300;
58
+ /**
59
+ * Per-call cost. FLAT by design — see the header: the execute is GET-only over read-only catalogs,
60
+ * so no reachable endpoint is credibly dearer than another, and inventing a ratio would be a
61
+ * fabricated vendor fact. `other` is the only weight, and it is never zero.
62
+ */
63
+ export const XAI_CALL_WEIGHTS = {
64
+ /** Every call. xAI publishes no per-endpoint cost, and this execute reaches only catalog reads. */
65
+ other: 2,
66
+ };
67
+ /**
68
+ * THE PACK'S DECLARATION — pure data, the only xAI-specific thing in the whole budget.
69
+ *
70
+ * `rules` is EMPTY on purpose (see the header). Also exported as `pack.rateBudget` (see index.ts),
71
+ * so `registerPack` arms it too.
72
+ */
73
+ export const XAI_RATE_BUDGET = {
74
+ windowMs: XAI_BUDGET_WINDOW_MS,
75
+ ceiling: XAI_BUDGET_CEILING,
76
+ defaultWeight: XAI_CALL_WEIGHTS.other,
77
+ maxRetryAfterSeconds: XAI_BUDGET_MAX_RETRY_AFTER_S,
78
+ rules: [],
79
+ reason: 'xAI publishes NO scalar rate limit anywhere in its public documentation that could bind this ' +
80
+ 'connector: neither the API reference (docs.x.ai/docs/api-reference), nor the models page (which ' +
81
+ 'carries pricing and capability tables but no RPM/RPS/TPM figures), nor the overview or quickstart ' +
82
+ 'pages state a requests-per-minute, requests-per-second or tokens-per-minute number, and none even ' +
83
+ 'points at a dashboard where one could be read (the commonly-cited consumption-and-rate-limits URL ' +
84
+ '404s). With no vendor figure to model, this pins to the kernel\'s austere fallback rather than ' +
85
+ 'borrowing OpenAI\'s tier limits on the strength of xAI\'s OpenAI-compatible protocol — protocol ' +
86
+ 'compatibility is not quota compatibility. 60 weighted units / 60s at 2 per call = 30 calls a ' +
87
+ 'minute, identical to DEFAULT_RATE_BUDGET and no more permissive than it. Pricing is FLAT and that ' +
88
+ 'is deliberate: XaiExecute is typed GET-only over read-only catalogs and the push surface refuses ' +
89
+ 'loudly, so no caller can reach a token-billed inference endpoint and no reachable endpoint is ' +
90
+ 'credibly dearer than another; inventing a ratio would be a made-up number dressed as vendor fact.',
91
+ };
92
+ // Declared at module load, so merely importing this module (which `xai-connector.ts` does) is enough
93
+ // to arm the real ceiling. Here the declaration and the kernel's DEFAULT_RATE_BUDGET happen to be
94
+ // numerically identical, so the usual "the fallback is not uniformly tighter" hazard does not bite —
95
+ // but `RateBudget` still reads its policy live, and constructing through the subclass below (which
96
+ // imports this module) is what keeps the ordering a non-issue in general.
97
+ declareRateBudget(VENDOR, XAI_RATE_BUDGET);
98
+ /** Split a connector path (which may carry its own query string) into pathname + parsed query. */
99
+ export function splitXaiPath(path) {
100
+ const q = path.indexOf('?');
101
+ if (q < 0)
102
+ return { pathname: path, query: {} };
103
+ const query = {};
104
+ for (const [k, v] of new URLSearchParams(path.slice(q + 1)))
105
+ query[k] = v;
106
+ return { pathname: path.slice(0, q), query };
107
+ }
108
+ /**
109
+ * Price one call. Keyed off the request the connector is ABOUT to make. With no rules declared every
110
+ * call takes `defaultWeight` — which is the point: an unclassified endpoint must never be free.
111
+ */
112
+ export function xaiCallWeight(method, path) {
113
+ const { pathname, query } = splitXaiPath(path);
114
+ return rateBudgetWeight(VENDOR, `${(method || 'GET').toUpperCase()} ${pathname}`, query);
115
+ }
116
+ /** Where xAI's ledger lives. API-key-keyed and cwd-independent by default (the limit, whatever it
117
+ * is, is per key, so a cwd-scoped ledger would hand the same key a fresh allowance in every
118
+ * checkout, worktree and CI matrix leg); pass `root` to opt into world-scoped accounting instead. */
119
+ export function xaiBudgetPath(opts = {}) {
120
+ const o = typeof opts === 'string' ? { root: opts } : opts;
121
+ // VENDOR spread LAST: a loosely-typed `{ vendor: 'other', … }` slipping through (TypeScript's
122
+ // excess-property check only catches object literals) must not redirect this pack's ledger to
123
+ // another vendor's file.
124
+ return rateBudgetPath({ ...o, vendor: VENDOR });
125
+ }
126
+ /**
127
+ * xAI's budget — the shared kernel guard bound to this vendor's declaration. A real subclass, not an
128
+ * alias, so the guard check in `liveXaiExecute` still means "a budget that accounts against XAI's
129
+ * ledger under XAI's ceiling": another vendor's `RateBudget` (with its own, possibly larger,
130
+ * ceiling) is NOT assignable there.
131
+ */
132
+ export class XaiBudget extends RateBudget {
133
+ constructor(opts = {}) {
134
+ super({ ...opts, vendor: VENDOR });
135
+ }
136
+ }
137
+ /** The typed refusal. One error class shared with every other vendor's budget; `err.vendor` says
138
+ * which one refused, and `err.kind` says why. */
139
+ export { RateBudgetError as XaiBudgetError } from '@volter/world-core';
@@ -0,0 +1,4 @@
1
+ import { type CapabilityReport, type CapabilitySpec } from '@volter/world-tooling';
2
+ export declare const XAI_CAPABILITIES: CapabilitySpec[];
3
+ export declare const XAI_AREAS: readonly ["api_key", "chat", "cli_proxy", "completions_legacy", "conformance", "connector", "deferred", "errors", "images", "messages_compat", "models", "oauth", "responses", "search", "streaming", "tokenize", "tools", "ui"];
4
+ export declare function xaiCapabilities(): Promise<CapabilityReport>;