@kaperkunde/tokens 0.0.0-stage → 0.1.1

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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +425 -2
  3. package/dist/browser-api.d.mts +56 -0
  4. package/dist/browser-api.mjs +75 -0
  5. package/dist/errors.d.mts +47 -0
  6. package/dist/errors.mjs +105 -0
  7. package/dist/index.d.mts +5 -0
  8. package/dist/index.mjs +5 -0
  9. package/dist/react/autorefill.d.mts +21 -0
  10. package/dist/react/autorefill.mjs +269 -0
  11. package/dist/react/balance.d.mts +23 -0
  12. package/dist/react/balance.mjs +116 -0
  13. package/dist/react/buy.d.mts +27 -0
  14. package/dist/react/buy.mjs +175 -0
  15. package/dist/react/history.d.mts +23 -0
  16. package/dist/react/history.mjs +102 -0
  17. package/dist/react/hooks.d.mts +52 -0
  18. package/dist/react/hooks.mjs +150 -0
  19. package/dist/react/index.d.mts +11 -0
  20. package/dist/react/index.mjs +10 -0
  21. package/dist/react/labels.d.mts +114 -0
  22. package/dist/react/labels.mjs +125 -0
  23. package/dist/react/own-model.d.mts +31 -0
  24. package/dist/react/own-model.mjs +358 -0
  25. package/dist/react/ui.d.mts +61 -0
  26. package/dist/react/ui.mjs +128 -0
  27. package/dist/react/usage.d.mts +21 -0
  28. package/dist/react/usage.mjs +137 -0
  29. package/dist/server/client.d.mts +78 -0
  30. package/dist/server/client.mjs +138 -0
  31. package/dist/server/error-of.d.mts +12 -0
  32. package/dist/server/error-of.mjs +63 -0
  33. package/dist/server/gateway.d.mts +64 -0
  34. package/dist/server/gateway.mjs +114 -0
  35. package/dist/server/index.d.mts +7 -0
  36. package/dist/server/index.mjs +7 -0
  37. package/dist/server/node.d.mts +23 -0
  38. package/dist/server/node.mjs +81 -0
  39. package/dist/server/routes.d.mts +56 -0
  40. package/dist/server/routes.mjs +257 -0
  41. package/dist/server/webhook.d.mts +47 -0
  42. package/dist/server/webhook.mjs +99 -0
  43. package/dist/styles.css +380 -0
  44. package/dist/types.d.mts +645 -0
  45. package/dist/types.mjs +162 -0
  46. package/dist/units.d.mts +37 -0
  47. package/dist/units.mjs +76 -0
  48. package/package.json +72 -3
  49. package/stub/cli.mjs +8 -0
  50. package/stub/server.d.mts +38 -0
  51. package/stub/server.mjs +1465 -0
@@ -0,0 +1,1465 @@
1
+ // Token Center contract stub (contract v1). Plain Node >= 22, no dependencies.
2
+ // In-memory wallet; canned or forwarded provider answers; signed webhooks. Not the center.
3
+ // `startStub(options)` starts one; `tokens-stub` (cli.mjs) is the command line. Kept in sync with
4
+ // the contract (types.ts / openapi.yaml): see the package README, "Testing against the stub".
5
+ import { createServer } from "node:http"
6
+ import { createHmac, randomBytes } from "node:crypto"
7
+
8
+ /**
9
+ * Start a stand-in Token Center. Options map onto the `TOKENS_STUB_*` environment variables
10
+ * (an option wins over its variable): port (0 = any free port), key, webhookUrl, webhookSecret,
11
+ * upstream {anthropic, openai, own}, extraModels, startMicros, feeBps, unitOne, unitMany, reply.
12
+ * Resolves to {url, port, close(), reset()}.
13
+ */
14
+ export function startStub(options = {}) {
15
+ const set = (name, value) =>
16
+ value === undefined || value === null
17
+ ? {}
18
+ : { [name]: Array.isArray(value) ? value.join(",") : String(value) }
19
+ const env = {
20
+ ...process.env,
21
+ ...set("TOKENS_STUB_PORT", options.port),
22
+ ...set("TOKENS_STUB_KEY", options.key),
23
+ ...set("TOKENS_STUB_WEBHOOK_URL", options.webhookUrl),
24
+ ...set("TOKENS_STUB_WEBHOOK_SECRET", options.webhookSecret),
25
+ ...set("TOKENS_STUB_ANTHROPIC_UPSTREAM", options.upstream?.anthropic),
26
+ ...set("TOKENS_STUB_OPENAI_UPSTREAM", options.upstream?.openai),
27
+ ...set("TOKENS_STUB_OWN_UPSTREAM", options.upstream?.own),
28
+ ...set("TOKENS_STUB_EXTRA_MODELS", options.extraModels),
29
+ ...set("TOKENS_STUB_START_MICROS", options.startMicros),
30
+ ...set("TOKENS_STUB_FEE_BPS", options.feeBps),
31
+ ...set("TOKENS_STUB_UNIT_ONE", options.unitOne),
32
+ ...set("TOKENS_STUB_UNIT_MANY", options.unitMany),
33
+ ...set("TOKENS_STUB_REPLY", options.reply),
34
+ ...set("TOKENS_STUB_PACKS", options.packsCents),
35
+ ...set("TOKENS_STUB_PACKS_ONLY", options.packsOnly),
36
+ ...set("TOKENS_STUB_FAIL", options.fail),
37
+ }
38
+ const timers = new Set()
39
+ let PORT = Number(env.TOKENS_STUB_PORT ?? 4100)
40
+ const KEY = env.TOKENS_STUB_KEY || null // unset: any non-empty key is accepted
41
+ const HOOK_URL = env.TOKENS_STUB_WEBHOOK_URL || null
42
+ const HOOK_SECRET = env.TOKENS_STUB_WEBHOOK_SECRET ?? "whsec_stub"
43
+ const UP = {
44
+ anthropic: env.TOKENS_STUB_ANTHROPIC_UPSTREAM,
45
+ openai: env.TOKENS_STUB_OPENAI_UPSTREAM,
46
+ }
47
+ const START = Number(env.TOKENS_STUB_START_MICROS ?? 5_000_000)
48
+ const FEE_BPS = Number(env.TOKENS_STUB_FEE_BPS ?? 2000)
49
+ const UNIT = {
50
+ one: env.TOKENS_STUB_UNIT_ONE ?? "token",
51
+ many: env.TOKENS_STUB_UNIT_MANY ?? "tokens",
52
+ perDollar: 1_000_000,
53
+ }
54
+ const PRICE = {
55
+ inputUsdPerM: "3",
56
+ outputUsdPerM: "15",
57
+ cacheReadUsdPerM: "0.3",
58
+ cacheWriteUsdPerM: "3.75",
59
+ cacheWrite1hUsdPerM: "6",
60
+ longPromptTokens: null,
61
+ longPromptFactor: null,
62
+ fastFactor: null,
63
+ effectiveFrom: "2026-01-01T00:00:00.000Z",
64
+ }
65
+ const MODELS = [
66
+ {
67
+ id: "claude-sonnet-5-5",
68
+ displayName: "Claude Sonnet 5.5",
69
+ provider: "anthropic",
70
+ protocol: "anthropic",
71
+ },
72
+ {
73
+ id: "claude-haiku-5-5",
74
+ displayName: "Claude Haiku 5.5",
75
+ provider: "anthropic",
76
+ protocol: "anthropic",
77
+ },
78
+ {
79
+ id: "gpt-5.5-mini",
80
+ displayName: "GPT-5.5 mini",
81
+ provider: "openai",
82
+ protocol: "openai",
83
+ },
84
+ ...(env.TOKENS_STUB_EXTRA_MODELS ?? "")
85
+ .split(",")
86
+ .filter(Boolean)
87
+ .map((id) => ({
88
+ id,
89
+ displayName: id,
90
+ provider: "openai",
91
+ protocol: "openai",
92
+ })),
93
+ ].map((m) => ({
94
+ ...m,
95
+ capabilities: ["tools", "streaming", "json"],
96
+ contextTokens: 200000,
97
+ price: PRICE,
98
+ }))
99
+ const VOICES = [
100
+ {
101
+ id: "stub-voice",
102
+ name: "Stub voice",
103
+ provider: "openai",
104
+ usdPerMChars: "15",
105
+ isDefault: true,
106
+ },
107
+ {
108
+ id: "stub-voice-eleven",
109
+ name: "Stub voice (ElevenLabs)",
110
+ provider: "elevenlabs",
111
+ usdPerMChars: "30",
112
+ isDefault: false,
113
+ },
114
+ ]
115
+ const SALE = [
116
+ {
117
+ currency: "eur",
118
+ centsPerDollar: 121,
119
+ taxBehavior: "inclusive",
120
+ minimumCents: 500,
121
+ effectiveFrom: PRICE.effectiveFrom,
122
+ },
123
+ {
124
+ currency: "usd",
125
+ centsPerDollar: 100,
126
+ taxBehavior: "exclusive",
127
+ minimumCents: 500,
128
+ effectiveFrom: PRICE.effectiveFrom,
129
+ },
130
+ ]
131
+ // Purchase options (contract PurchaseOptions): TOKENS_STUB_PACKS (cents, comma-separated, default
132
+ // AJ's 500,1000,2500,5000) and TOKENS_STUB_PACKS_ONLY=true to allow only those.
133
+ const OPTIONS = {
134
+ packsCents: (env.TOKENS_STUB_PACKS ?? "500,1000,2500,5000")
135
+ .split(",")
136
+ .filter(Boolean)
137
+ .map(Number),
138
+ packsOnly: env.TOKENS_STUB_PACKS_ONLY === "true",
139
+ minimumCents: 500,
140
+ maximumCents: 100000,
141
+ }
142
+ const STATUS = {
143
+ unauthenticated: 401,
144
+ forbidden: 403,
145
+ invalid_request: 400,
146
+ unknown_user: 404,
147
+ not_found: 404,
148
+ unknown_model: 404,
149
+ model_not_priced: 409,
150
+ wrong_protocol: 400,
151
+ insufficient_balance: 402,
152
+ capability_unavailable: 402,
153
+ byo_not_configured: 409,
154
+ byo_failed: 502,
155
+ byo_limit_reached: 402,
156
+ provider_error: 502,
157
+ provider_timeout: 504,
158
+ provider_out_of_credit: 503,
159
+ provider_rate_limited: 429,
160
+ provider_overloaded: 503,
161
+ rate_limited: 429,
162
+ payments_unavailable: 503,
163
+ purchase_too_small: 400,
164
+ no_card: 409,
165
+ conflict: 409,
166
+ center_unavailable: 503,
167
+ internal_error: 500,
168
+ }
169
+
170
+ // A provider failure the test asked for: TOKENS_STUB_FAIL, POST /stub/mode {"fail"} or the request
171
+ // header `x-stub-fail`: out_of_credit, rate_limited or overloaded (see the README).
172
+ const FAIL_FROM_ENV = [
173
+ "out_of_credit",
174
+ "rate_limited",
175
+ "overloaded",
176
+ ].includes(env.TOKENS_STUB_FAIL)
177
+ ? env.TOKENS_STUB_FAIL
178
+ : null
179
+ const FAILS = {
180
+ out_of_credit: "provider_out_of_credit",
181
+ rate_limited: "provider_rate_limited",
182
+ overloaded: "provider_overloaded",
183
+ }
184
+ const PROVIDER_NAME = {
185
+ anthropic: "Anthropic",
186
+ openai: "OpenAI",
187
+ elevenlabs: "ElevenLabs",
188
+ }
189
+
190
+ let state
191
+ const reset = () => {
192
+ state = {
193
+ users: new Map(),
194
+ calls: [],
195
+ purchases: new Map(),
196
+ mode: {
197
+ down: false,
198
+ providerError: false,
199
+ fail: FAIL_FROM_ENV,
200
+ },
201
+ }
202
+ }
203
+ reset()
204
+ const now = () => new Date().toISOString()
205
+ const id = (p) => `${p}_${randomBytes(12).toString("hex")}`
206
+
207
+ // ------------------------------------------------------------------ wallet
208
+ const user = (userId, email) => {
209
+ let u = state.users.get(userId)
210
+ if (!u) {
211
+ u = {
212
+ userId,
213
+ email: null,
214
+ displayName: null,
215
+ createdAt: now(),
216
+ lastSeenAt: null,
217
+ purchased: START,
218
+ lots: [],
219
+ own: null,
220
+ order: "tokens_only",
221
+ autoRefill: {
222
+ enabled: false,
223
+ thresholdMicros: 1_000_000,
224
+ amountCents: 1000,
225
+ currency: "eur",
226
+ monthlyMaxCents: 5000,
227
+ card: null,
228
+ pending: false,
229
+ failedAt: null,
230
+ },
231
+ history: [],
232
+ low: false,
233
+ empty: false,
234
+ }
235
+ state.users.set(userId, u)
236
+ }
237
+ if (email) u.email = email
238
+ u.lastSeenAt = now()
239
+ return u
240
+ }
241
+ const live = (lot) =>
242
+ lot.remainingMicros > 0 && (!lot.expiresAt || lot.expiresAt > now())
243
+ // Every lot is locked to one provider; soonest expiry first, none last, then by id.
244
+ const lotsFor = (u, provider) =>
245
+ u.lots
246
+ .filter((l) => live(l) && l.provider === provider)
247
+ .sort(
248
+ (a, b) =>
249
+ (a.expiresAt ?? "9").localeCompare(b.expiresAt ?? "9") ||
250
+ a.id.localeCompare(b.id),
251
+ )
252
+ const grantsOf = (u) =>
253
+ u.lots.filter(live).reduce((s, l) => s + l.remainingMicros, 0)
254
+ const summary = (u) => ({
255
+ availableMicros: u.purchased + grantsOf(u),
256
+ purchasedMicros: u.purchased,
257
+ grantsMicros: grantsOf(u),
258
+ })
259
+ const available = (u, provider) =>
260
+ u.purchased +
261
+ lotsFor(u, provider).reduce((s, l) => s + l.remainingMicros, 0)
262
+ const balance = (u, known = true) => {
263
+ const lots = u.lots.filter(live)
264
+ const next = lots
265
+ .filter((l) => l.expiresAt)
266
+ .sort((a, b) => a.expiresAt.localeCompare(b.expiresAt))[0]
267
+ return {
268
+ userId: u.userId,
269
+ known,
270
+ ...summary(u),
271
+ byProvider: Object.fromEntries(
272
+ ["anthropic", "openai", "elevenlabs"].map((p) => [p, available(u, p)]),
273
+ ),
274
+ lots,
275
+ nextExpiry: next
276
+ ? { at: next.expiresAt, micros: next.remainingMicros }
277
+ : null,
278
+ unit: UNIT,
279
+ feeBps: FEE_BPS,
280
+ autoRefill: { enabled: u.autoRefill.enabled, pending: false },
281
+ }
282
+ }
283
+ const crossings = (u) => {
284
+ const a = summary(u).availableMicros
285
+ if (a < 500_000 && !u.low) {
286
+ u.low = true
287
+ emit("balance.low", {
288
+ userId: u.userId,
289
+ thresholdMicros: 500_000,
290
+ balance: summary(u),
291
+ })
292
+ } else if (a >= 500_000) u.low = false
293
+ if (a <= 0 && !u.empty) {
294
+ u.empty = true
295
+ emit("balance.empty", { userId: u.userId, balance: summary(u) })
296
+ } else if (a > 0) u.empty = false
297
+ }
298
+ const charge = (u, call, provider, costMicros) => {
299
+ let rest = costMicros,
300
+ fromGrants = 0
301
+ for (const lot of call.source === "own" ? [] : lotsFor(u, provider)) {
302
+ const take = Math.min(lot.remainingMicros, rest)
303
+ lot.remainingMicros -= take
304
+ lot.spentMicros += take
305
+ fromGrants += take
306
+ rest -= take
307
+ if (!rest) break
308
+ }
309
+ const fee = Math.ceil((rest * FEE_BPS) / 10000)
310
+ // Shortfall: absorbed by the center; purchased never goes below zero.
311
+ const shortfall = Math.max(0, rest + fee - u.purchased)
312
+ u.purchased = Math.max(0, u.purchased - rest - fee)
313
+ Object.assign(call, {
314
+ status: "charged",
315
+ costMicros,
316
+ feeMicros: fee,
317
+ chargedMicros: fromGrants + rest + fee,
318
+ fromGrantsMicros: fromGrants,
319
+ fromPurchasedMicros: rest + fee,
320
+ shortfallMicros: shortfall,
321
+ balanceAfterMicros: summary(u).availableMicros,
322
+ finishedAt: now(),
323
+ latencyMs: Date.now() - call.t0,
324
+ })
325
+ crossings(u)
326
+ return call
327
+ }
328
+ const usd = (usage) =>
329
+ (usage.inputTokens - usage.cacheReadTokens - usage.cacheWriteTokens) * 3 +
330
+ usage.cacheReadTokens * 0.3 +
331
+ (usage.cacheWriteTokens - usage.cacheWrite1hTokens) * 3.75 +
332
+ usage.cacheWrite1hTokens * 6 +
333
+ usage.outputTokens * 15 +
334
+ (usage.units?.tts_chars ?? 0) * 15
335
+ const costOf = (usage) => Math.ceil(usd(usage)) // $/M × tokens = micros
336
+ // Calls are keyed `callId` on the wire (CallCharge / CallRecord), never `id`.
337
+ const publicCall = ({ t0: _t0, id: callId, ...c }) => ({ callId, ...c })
338
+
339
+ // ------------------------------------------------------------------ webhooks
340
+ const sign = (body, t = Math.floor(Date.now() / 1000)) =>
341
+ `t=${t},v1=${createHmac("sha256", HOOK_SECRET).update(`${t}.${body}`).digest("hex")}`
342
+ function emit(type, data) {
343
+ const event = {
344
+ id: id("evt"),
345
+ type,
346
+ createdAt: now(),
347
+ appId: "app_stub",
348
+ livemode: false,
349
+ data,
350
+ }
351
+ if (!HOOK_URL) return event
352
+ const body = JSON.stringify(event)
353
+ const send = (attempt) =>
354
+ fetch(HOOK_URL, {
355
+ method: "POST",
356
+ headers: {
357
+ "content-type": "application/json",
358
+ "token-signature": sign(body),
359
+ "token-event-id": event.id,
360
+ },
361
+ body,
362
+ signal: AbortSignal.timeout(10_000),
363
+ })
364
+ .then((r) => {
365
+ if (!r.ok) throw new Error(String(r.status))
366
+ })
367
+ .catch((e) => {
368
+ if (attempt < 3) {
369
+ const t = setTimeout(
370
+ () => {
371
+ timers.delete(t)
372
+ send(attempt + 1)
373
+ },
374
+ 1000 * 2 ** attempt,
375
+ )
376
+ t.unref?.()
377
+ timers.add(t)
378
+ } else console.error(`[stub] webhook ${type} dead: ${e.message}`)
379
+ })
380
+ send(0)
381
+ return event
382
+ }
383
+
384
+ // ------------------------------------------------------------------ http helpers
385
+ const readBody = (req) =>
386
+ new Promise((ok, no) => {
387
+ const b = []
388
+ req.on("data", (c) => b.push(c))
389
+ req.on("end", () => ok(Buffer.concat(b).toString("utf8")))
390
+ req.on("error", no)
391
+ })
392
+ const json = (res, status, body, headers = {}) => {
393
+ res.writeHead(status, { "content-type": "application/json", ...headers })
394
+ res.end(body === undefined ? "" : JSON.stringify(body))
395
+ }
396
+ const fail = (res, code, message, shape = "api", extra = {}) => {
397
+ const detail = { code, message, ...extra }
398
+ const body =
399
+ shape === "anthropic"
400
+ ? {
401
+ type: "error",
402
+ error: {
403
+ type:
404
+ code === "insufficient_balance"
405
+ ? "permission_error"
406
+ : "api_error",
407
+ message,
408
+ },
409
+ token_center: detail,
410
+ }
411
+ : shape === "openai"
412
+ ? { error: { message, type: code, code }, token_center: detail }
413
+ : { error: detail }
414
+ json(res, STATUS[code], body, {
415
+ "x-token-error": code,
416
+ ...(extra.callId ? { "x-token-call-id": extra.callId } : {}),
417
+ ...(code === "provider_out_of_credit"
418
+ ? { "x-should-retry": "false" }
419
+ : {}),
420
+ ...(extra.retryAfterSeconds !== undefined &&
421
+ ["rate_limited", "provider_rate_limited", "provider_overloaded"].includes(
422
+ code,
423
+ )
424
+ ? { "retry-after": String(extra.retryAfterSeconds) }
425
+ : {}),
426
+ })
427
+ }
428
+ const attribution = (req) =>
429
+ Object.fromEntries(
430
+ ["purpose", "activity", "scope", "job", "subject", "correlation"]
431
+ .map((k) => [k, req.headers[`x-token-${k}`]])
432
+ .filter(([, v]) => v)
433
+ .map(([k, v]) => [k, decodeURIComponent(v).slice(0, 200)]),
434
+ )
435
+ const chargeHeaders = (c) => ({
436
+ "x-token-call-id": c.id,
437
+ "x-token-source": c.source,
438
+ "x-token-model": c.model,
439
+ "x-token-cost-micros": String(c.costMicros),
440
+ "x-token-fee-micros": String(c.feeMicros),
441
+ "x-token-charged-micros": String(c.chargedMicros),
442
+ "x-token-balance-micros": String(c.balanceAfterMicros),
443
+ })
444
+ const zeroUsage = () => ({
445
+ inputTokens: 0,
446
+ outputTokens: 0,
447
+ cacheReadTokens: 0,
448
+ cacheWriteTokens: 0,
449
+ cacheWrite1hTokens: 0,
450
+ cacheWrite5mTokens: 0,
451
+ totalTokens: 0,
452
+ })
453
+ const fromAnthropic = (u, x = {}) => {
454
+ const read = x.cache_read_input_tokens ?? u.cacheReadTokens,
455
+ write = x.cache_creation_input_tokens ?? u.cacheWriteTokens
456
+ const input =
457
+ x.input_tokens !== undefined
458
+ ? x.input_tokens + read + write
459
+ : u.inputTokens
460
+ const output = x.output_tokens ?? u.outputTokens
461
+ const write1h =
462
+ x.cache_creation?.ephemeral_1h_input_tokens ?? u.cacheWrite1hTokens
463
+ return {
464
+ inputTokens: input,
465
+ outputTokens: output,
466
+ cacheReadTokens: read,
467
+ cacheWriteTokens: write,
468
+ cacheWrite1hTokens: write1h,
469
+ cacheWrite5mTokens: write - write1h,
470
+ totalTokens: input + output,
471
+ }
472
+ }
473
+ const fromOpenAI = (x) => ({
474
+ inputTokens: x.prompt_tokens ?? 0,
475
+ outputTokens: x.completion_tokens ?? 0,
476
+ cacheReadTokens: x.prompt_tokens_details?.cached_tokens ?? 0,
477
+ cacheWriteTokens: 0,
478
+ cacheWrite1hTokens: 0,
479
+ cacheWrite5mTokens: 0,
480
+ totalTokens: x.total_tokens ?? 0,
481
+ })
482
+
483
+ // ------------------------------------------------------------------ canned provider
484
+ const sse = (res, events) => {
485
+ for (const [event, data] of events)
486
+ res.write(
487
+ `${event ? `event: ${event}\n` : ""}data: ${typeof data === "string" ? data : JSON.stringify(data)}\n\n`,
488
+ )
489
+ }
490
+ const TEXT = env.TOKENS_STUB_REPLY ?? "Stub reply."
491
+ const cannedAnthropic = (body) => ({
492
+ id: id("msg"),
493
+ type: "message",
494
+ role: "assistant",
495
+ model: body.model,
496
+ content: [{ type: "text", text: TEXT }],
497
+ stop_reason: "end_turn",
498
+ stop_sequence: null,
499
+ usage: {
500
+ input_tokens: 100,
501
+ output_tokens: 20,
502
+ cache_read_input_tokens: 0,
503
+ cache_creation_input_tokens: 0,
504
+ },
505
+ })
506
+ const cannedAnthropicStream = (m) => [
507
+ [
508
+ "message_start",
509
+ {
510
+ type: "message_start",
511
+ message: {
512
+ ...m,
513
+ content: [],
514
+ stop_reason: null,
515
+ usage: { ...m.usage, output_tokens: 1 },
516
+ },
517
+ },
518
+ ],
519
+ [
520
+ "content_block_start",
521
+ {
522
+ type: "content_block_start",
523
+ index: 0,
524
+ content_block: { type: "text", text: "" },
525
+ },
526
+ ],
527
+ [
528
+ "content_block_delta",
529
+ {
530
+ type: "content_block_delta",
531
+ index: 0,
532
+ delta: { type: "text_delta", text: TEXT },
533
+ },
534
+ ],
535
+ ["content_block_stop", { type: "content_block_stop", index: 0 }],
536
+ [
537
+ "message_delta",
538
+ {
539
+ type: "message_delta",
540
+ delta: { stop_reason: "end_turn", stop_sequence: null },
541
+ usage: { output_tokens: 20 },
542
+ },
543
+ ],
544
+ ["message_stop", { type: "message_stop" }],
545
+ ]
546
+ const cannedOpenAI = (body) => ({
547
+ id: id("chatcmpl"),
548
+ object: "chat.completion",
549
+ created: Math.floor(Date.now() / 1000),
550
+ model: body.model,
551
+ choices: [
552
+ {
553
+ index: 0,
554
+ message: { role: "assistant", content: TEXT },
555
+ finish_reason: "stop",
556
+ },
557
+ ],
558
+ usage: { prompt_tokens: 100, completion_tokens: 20, total_tokens: 120 },
559
+ })
560
+ const cannedOpenAIStream = (c) => {
561
+ const base = {
562
+ id: c.id,
563
+ object: "chat.completion.chunk",
564
+ created: c.created,
565
+ model: c.model,
566
+ }
567
+ return [
568
+ [
569
+ null,
570
+ {
571
+ ...base,
572
+ choices: [
573
+ {
574
+ index: 0,
575
+ delta: { role: "assistant", content: TEXT },
576
+ finish_reason: null,
577
+ },
578
+ ],
579
+ },
580
+ ],
581
+ [
582
+ null,
583
+ { ...base, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] },
584
+ ],
585
+ [null, { ...base, choices: [], usage: c.usage }],
586
+ [null, "[DONE]"],
587
+ ]
588
+ }
589
+
590
+ // Forward to an upstream fake (or real) provider; meter what it reports. Returns usage.
591
+ async function forward(
592
+ req,
593
+ res,
594
+ upstream,
595
+ path,
596
+ raw,
597
+ protocol,
598
+ stream,
599
+ headers,
600
+ ) {
601
+ const up = await fetch(upstream.replace(/\/$/, "") + path, {
602
+ method: "POST",
603
+ headers: {
604
+ "content-type": "application/json",
605
+ authorization: "Bearer stub",
606
+ "x-api-key": "stub",
607
+ "anthropic-version": req.headers["anthropic-version"] ?? "2023-06-01",
608
+ },
609
+ body: raw,
610
+ })
611
+ if (!stream || !up.ok) {
612
+ const text = await up.text()
613
+ let usage = zeroUsage()
614
+ try {
615
+ const j = JSON.parse(text)
616
+ usage =
617
+ protocol === "anthropic"
618
+ ? fromAnthropic(usage, j.usage)
619
+ : fromOpenAI(j.usage ?? {})
620
+ } catch {
621
+ /* not JSON or no usage: stays zero */
622
+ }
623
+ return { status: up.status, text, usage, ok: up.ok }
624
+ }
625
+ res.writeHead(200, {
626
+ "content-type": "text/event-stream",
627
+ "cache-control": "no-cache",
628
+ ...headers,
629
+ })
630
+ let usage = zeroUsage(),
631
+ buf = ""
632
+ for await (const chunk of up.body) {
633
+ res.write(chunk)
634
+ buf += Buffer.from(chunk).toString()
635
+ const lines = buf.split("\n")
636
+ buf = lines.pop()
637
+ for (const line of lines)
638
+ if (line.startsWith("data:")) {
639
+ try {
640
+ const j = JSON.parse(line.slice(5))
641
+ if (protocol === "anthropic")
642
+ usage = fromAnthropic(usage, j.message?.usage ?? j.usage)
643
+ else if (j.usage) usage = fromOpenAI(j.usage)
644
+ } catch {
645
+ /* not JSON or no usage: stays zero */
646
+ }
647
+ }
648
+ }
649
+ return { status: 200, usage, ok: true, streamed: true }
650
+ }
651
+
652
+ // Real, playable audio: 12 MPEG-1 Layer III frames (128 kbps, 44.1 kHz, stereo, all-zero payload =
653
+ // silence), about 0.3 s. Every decoder plays it; the length does not follow the text.
654
+ const silentMp3 = () =>
655
+ Buffer.concat(
656
+ Array.from({ length: 12 }, () =>
657
+ Buffer.concat([
658
+ Buffer.from([0xff, 0xfb, 0x90, 0x00]),
659
+ Buffer.alloc(413),
660
+ ]),
661
+ ),
662
+ )
663
+
664
+ // ------------------------------------------------------------------ gateway
665
+ async function gateway(req, res, url, raw) {
666
+ const shape = url.pathname === "/v1/messages" ? "anthropic" : "openai"
667
+ const userId = req.headers["x-token-user"]
668
+ if (!userId)
669
+ return fail(res, "invalid_request", "x-token-user is required.", shape, {
670
+ path: "x-token-user",
671
+ })
672
+ const u = user(
673
+ decodeURIComponent(userId),
674
+ req.headers["x-token-email"] &&
675
+ decodeURIComponent(req.headers["x-token-email"]),
676
+ )
677
+ let body
678
+ try {
679
+ body = JSON.parse(raw || "{}")
680
+ } catch {
681
+ return fail(res, "invalid_request", "Body is not JSON.", shape)
682
+ }
683
+ const speech = url.pathname === "/v1/audio/speech"
684
+ const backend = req.headers["x-token-backend"] === "own" ? "own" : "tokens"
685
+ let model = speech
686
+ ? VOICES.find((v) => v.id === (body.voice ?? "stub-voice")) &&
687
+ "tts-stub-voice"
688
+ : body.model
689
+ const known = MODELS.find((m) => m.id === model)
690
+ if (speech && !model) return fail(res, "not_found", "No such voice.", shape)
691
+ if (!speech && backend === "tokens") {
692
+ if (/unpriced/.test(model ?? ""))
693
+ return fail(res, "model_not_priced", `${model} has no price.`, shape)
694
+ if (!known)
695
+ return fail(res, "unknown_model", `${model} is not offered.`, shape)
696
+ if (known.protocol !== shape)
697
+ return fail(
698
+ res,
699
+ "wrong_protocol",
700
+ `${model} is served on the other endpoint.`,
701
+ shape,
702
+ )
703
+ }
704
+ if (backend === "own") {
705
+ if (shape !== "openai" || speech)
706
+ return fail(
707
+ res,
708
+ "wrong_protocol",
709
+ "Own endpoints speak chat completions.",
710
+ shape,
711
+ )
712
+ if (!u.own)
713
+ return fail(
714
+ res,
715
+ "byo_not_configured",
716
+ "No own endpoint saved.",
717
+ shape,
718
+ { action: "configure_own" },
719
+ )
720
+ if (!u.own.models.includes(model)) model = u.own.defaultModel
721
+ }
722
+ const provider = speech
723
+ ? "openai"
724
+ : backend === "own"
725
+ ? "own"
726
+ : known.provider
727
+ if (backend === "tokens" && available(u, provider) <= 0)
728
+ return fail(res, "insufficient_balance", "Out of tokens.", shape, {
729
+ action: "buy_tokens",
730
+ provider,
731
+ })
732
+ const endpoint = speech
733
+ ? "speech"
734
+ : shape === "anthropic"
735
+ ? "messages"
736
+ : "chat_completions"
737
+ const call = {
738
+ id: id("call"),
739
+ t0: Date.now(),
740
+ userId: u.userId,
741
+ status: "admitted",
742
+ endpoint,
743
+ provider,
744
+ source: backend,
745
+ requestedModel: body.model ?? model,
746
+ model,
747
+ stream: !!body.stream,
748
+ attribution: attribution(req),
749
+ error: null,
750
+ createdAt: now(),
751
+ finishedAt: null,
752
+ latencyMs: null,
753
+ usage: zeroUsage(),
754
+ costMicros: 0,
755
+ feeMicros: 0,
756
+ chargedMicros: 0,
757
+ fromGrantsMicros: 0,
758
+ fromPurchasedMicros: 0,
759
+ shortfallMicros: 0,
760
+ balanceAfterMicros: 0,
761
+ }
762
+ state.calls.push(call)
763
+ if (state.mode.providerError) {
764
+ Object.assign(call, {
765
+ status: "failed",
766
+ finishedAt: now(),
767
+ error: {
768
+ code: "provider_error",
769
+ message: "Stub provider error.",
770
+ status: 529,
771
+ },
772
+ })
773
+ return fail(res, "provider_error", "The provider failed.", shape, {
774
+ status: 529,
775
+ callId: call.id,
776
+ })
777
+ }
778
+ const failKind = FAILS[req.headers["x-stub-fail"]]
779
+ ? req.headers["x-stub-fail"]
780
+ : state.mode.fail
781
+ const failing =
782
+ backend === "tokens" &&
783
+ failKind &&
784
+ (!speech || (body.voice ?? "stub-voice") !== "stub-voice-eleven")
785
+ if (failing && !speech) {
786
+ const code = FAILS[failKind]
787
+ const name = PROVIDER_NAME[provider]
788
+ const message =
789
+ failKind === "out_of_credit"
790
+ ? `The ${name} account behind the token center is out of credit; nothing was charged.`
791
+ : failKind === "overloaded"
792
+ ? `${name} is overloaded; nothing was charged. Try again shortly.`
793
+ : `${name} is rate limiting the token center; nothing was charged. Try again shortly.`
794
+ Object.assign(call, {
795
+ status: "failed",
796
+ finishedAt: now(),
797
+ error: { code, message, provider, model },
798
+ })
799
+ return fail(res, code, message, shape, {
800
+ provider,
801
+ model,
802
+ callId: call.id,
803
+ ...(failKind === "out_of_credit" ? {} : { retryAfterSeconds: 7 }),
804
+ })
805
+ }
806
+ // Speech does not fail: it falls back to the app's other voice, on a provider that is not failing.
807
+ let fallbackVoice = null
808
+ if (failing && speech) {
809
+ fallbackVoice = "stub-voice-eleven"
810
+ call.model = fallbackVoice
811
+ call.provider = "elevenlabs"
812
+ }
813
+ if (speech) {
814
+ const chars = [...String(body.input ?? "")].length
815
+ if (!chars || chars > 4096) {
816
+ call.status = "failed"
817
+ return fail(
818
+ res,
819
+ "invalid_request",
820
+ "input must be 1..4096 characters.",
821
+ shape,
822
+ { path: "input" },
823
+ )
824
+ }
825
+ call.usage = { ...zeroUsage(), units: { tts_chars: chars } }
826
+ charge(u, call, call.provider, costOf(call.usage))
827
+ const audio = silentMp3()
828
+ res.writeHead(200, {
829
+ "content-type": "audio/mpeg",
830
+ "x-token-chars": String(chars),
831
+ ...(fallbackVoice ? { "x-token-voice-fallback": fallbackVoice } : {}),
832
+ ...chargeHeaders(call),
833
+ })
834
+ return res.end(audio)
835
+ }
836
+ if (shape === "openai" && body.stream) {
837
+ body.stream_options = { ...body.stream_options, include_usage: true }
838
+ raw = JSON.stringify(body)
839
+ }
840
+ const upstream =
841
+ backend === "own" ? (u.own.upstream ?? UP.openai) : UP[shape]
842
+ const head = {
843
+ "x-token-call-id": call.id,
844
+ "x-token-source": call.source,
845
+ "x-token-model": call.model,
846
+ }
847
+ const trailer = () => {
848
+ if (req.headers["x-token-trailer"] === "event")
849
+ sse(res, [["token_center", publicCall(call)]])
850
+ }
851
+ if (upstream) {
852
+ const out = await forward(
853
+ req,
854
+ res,
855
+ upstream,
856
+ url.pathname.replace(/^\/v1/, ""),
857
+ raw,
858
+ shape,
859
+ !!body.stream,
860
+ head,
861
+ )
862
+ call.usage = out.usage
863
+ if (!out.ok) {
864
+ charge(u, call, provider, backend === "own" ? 0 : costOf(out.usage))
865
+ call.status = "failed"
866
+ return fail(
867
+ res,
868
+ backend === "own" ? "byo_failed" : "provider_error",
869
+ `Upstream answered ${out.status}.`,
870
+ shape,
871
+ { status: out.status, callId: call.id },
872
+ )
873
+ }
874
+ charge(u, call, provider, backend === "own" ? 0 : costOf(out.usage))
875
+ if (out.streamed) {
876
+ trailer()
877
+ return res.end()
878
+ }
879
+ res.writeHead(200, {
880
+ "content-type": "application/json",
881
+ ...chargeHeaders(call),
882
+ })
883
+ return res.end(out.text)
884
+ }
885
+ const message =
886
+ shape === "anthropic"
887
+ ? cannedAnthropic({ ...body, model })
888
+ : cannedOpenAI({ ...body, model })
889
+ call.usage =
890
+ shape === "anthropic"
891
+ ? fromAnthropic(zeroUsage(), message.usage)
892
+ : fromOpenAI(message.usage)
893
+ if (!body.stream) {
894
+ charge(u, call, provider, backend === "own" ? 0 : costOf(call.usage))
895
+ return json(res, 200, message, chargeHeaders(call))
896
+ }
897
+ res.writeHead(200, {
898
+ "content-type": "text/event-stream",
899
+ "cache-control": "no-cache",
900
+ ...head,
901
+ })
902
+ sse(
903
+ res,
904
+ shape === "anthropic"
905
+ ? cannedAnthropicStream(message)
906
+ : cannedOpenAIStream(message),
907
+ )
908
+ charge(u, call, provider, backend === "own" ? 0 : costOf(call.usage))
909
+ trailer()
910
+ res.end()
911
+ }
912
+
913
+ // ------------------------------------------------------------------ app API
914
+ const page = (calls, q) => {
915
+ const f = calls
916
+ .filter(
917
+ (c) =>
918
+ (!q.get("userId") || c.userId === q.get("userId")) &&
919
+ ["scope", "job", "correlation", "purpose"].every(
920
+ (k) => !q.get(k) || c.attribution[k] === q.get(k),
921
+ ) &&
922
+ (!q.get("source") || c.source === q.get("source")) &&
923
+ (!q.get("since") || c.createdAt >= q.get("since")),
924
+ )
925
+ .reverse()
926
+ const start = Number(q.get("cursor") ?? 0),
927
+ limit = Math.min(Number(q.get("limit") ?? 50), 200)
928
+ const sum = (k) => f.reduce((s, c) => s + (c[k] ?? c.usage[k] ?? 0), 0)
929
+ return {
930
+ calls: f.slice(start, start + limit).map(publicCall),
931
+ nextCursor: start + limit < f.length ? String(start + limit) : null,
932
+ totals: {
933
+ count: f.length,
934
+ ...Object.fromEntries(
935
+ [
936
+ "costMicros",
937
+ "feeMicros",
938
+ "chargedMicros",
939
+ "inputTokens",
940
+ "outputTokens",
941
+ "cacheReadTokens",
942
+ "cacheWriteTokens",
943
+ "cacheWrite5mTokens",
944
+ "cacheWrite1hTokens",
945
+ "totalTokens",
946
+ ].map((k) => [k, sum(k)]),
947
+ ),
948
+ },
949
+ }
950
+ }
951
+ const ownView = (u) =>
952
+ u.own && {
953
+ ...u.own,
954
+ apiKey: undefined,
955
+ upstream: undefined,
956
+ order: u.order,
957
+ }
958
+ async function api(req, res, url, raw) {
959
+ const p = url.pathname,
960
+ m = req.method,
961
+ q = url.searchParams
962
+ let body = {}
963
+ try {
964
+ body = raw ? JSON.parse(raw) : {}
965
+ } catch {
966
+ return fail(res, "invalid_request", "Body is not JSON.")
967
+ }
968
+ if (p === "/api/v1/app")
969
+ return json(res, 200, {
970
+ id: "app_stub",
971
+ name: "Stub app",
972
+ unit: UNIT,
973
+ purchaseOptions: OPTIONS,
974
+ feeBps: FEE_BPS,
975
+ lowBalanceMicros: 500_000,
976
+ maxInFlightPerUser: 4,
977
+ livemode: false,
978
+ returnOrigins: ["http://localhost", "http://127.0.0.1"],
979
+ })
980
+ if (p === "/api/v1/models") return json(res, 200, { models: MODELS })
981
+ if (p === "/api/v1/voices") return json(res, 200, { voices: VOICES })
982
+ if (p === "/api/v1/sale-prices")
983
+ return json(res, 200, {
984
+ prices: SALE,
985
+ feeBps: FEE_BPS,
986
+ paymentsAvailable: true,
987
+ options: OPTIONS,
988
+ })
989
+ if (p === "/api/v1/calls") return json(res, 200, page(state.calls, q))
990
+ let r = p.match(/^\/api\/v1\/calls\/([^/]+)$/)
991
+ if (r) {
992
+ const c = state.calls.find((x) => x.id === r[1])
993
+ return c
994
+ ? json(res, 200, publicCall(c))
995
+ : fail(res, "not_found", "No such call.")
996
+ }
997
+ if (p === "/api/v1/route") {
998
+ if (!body.userId || !body.model)
999
+ return fail(
1000
+ res,
1001
+ "invalid_request",
1002
+ "userId and model are needed.",
1003
+ "api",
1004
+ { path: body.userId ? "model" : "userId" },
1005
+ )
1006
+ const u = user(body.userId),
1007
+ needs = body.needs ?? [],
1008
+ own =
1009
+ u.own &&
1010
+ u.order !== "tokens_only" &&
1011
+ needs.every((n) => n !== "voice" && u.own.capabilities.includes(n))
1012
+ const ownFirst =
1013
+ own &&
1014
+ (u.order === "own_only" ||
1015
+ u.order === "own_then_tokens" ||
1016
+ (u.order === "tokens_then_own" &&
1017
+ available(
1018
+ u,
1019
+ MODELS.find((x) => x.id === body.model)?.provider ?? "anthropic",
1020
+ ) <= 0))
1021
+ const known = MODELS.find((x) => x.id === body.model)
1022
+ if (ownFirst)
1023
+ return json(res, 200, {
1024
+ source: "own",
1025
+ protocol: "openai",
1026
+ model: u.own.models.includes(body.model)
1027
+ ? body.model
1028
+ : u.own.defaultModel,
1029
+ backend: "own",
1030
+ fallback: null,
1031
+ availableMicros: summary(u).availableMicros,
1032
+ })
1033
+ if (!known)
1034
+ return fail(
1035
+ res,
1036
+ /unpriced/.test(body.model ?? "")
1037
+ ? "model_not_priced"
1038
+ : "unknown_model",
1039
+ `${body.model} is not offered.`,
1040
+ )
1041
+ const missing =
1042
+ u.own &&
1043
+ u.order !== "tokens_only" &&
1044
+ needs.find((n) => n === "voice" || !u.own.capabilities.includes(n))
1045
+ if (available(u, known.provider) <= 0)
1046
+ return fail(
1047
+ res,
1048
+ missing ? "capability_unavailable" : "insufficient_balance",
1049
+ missing ? `Your own service can't do ${missing}.` : "Out of tokens.",
1050
+ "api",
1051
+ { action: "buy_tokens", ...(missing ? { capability: missing } : {}) },
1052
+ )
1053
+ return json(res, 200, {
1054
+ source: "tokens",
1055
+ protocol: known.protocol,
1056
+ model: known.id,
1057
+ backend: "tokens",
1058
+ fallback:
1059
+ missing && u.order !== "tokens_then_own"
1060
+ ? { reason: "capability", capability: missing }
1061
+ : null,
1062
+ availableMicros: available(u, known.provider),
1063
+ })
1064
+ }
1065
+ r = p.match(/^\/api\/v1\/users\/([^/]+)(\/.*)?$/)
1066
+ if (!r) return fail(res, "not_found", "No such endpoint.")
1067
+ const userId = decodeURIComponent(r[1]),
1068
+ rest = r[2] ?? "",
1069
+ seen = state.users.has(userId),
1070
+ u = user(userId)
1071
+ if (rest === "" && m === "PUT") {
1072
+ if (body.email !== undefined) u.email = body.email
1073
+ if (body.displayName !== undefined) u.displayName = body.displayName
1074
+ const { userId: i, email, displayName, createdAt, lastSeenAt } = u
1075
+ return json(res, 200, {
1076
+ userId: i,
1077
+ email,
1078
+ displayName,
1079
+ createdAt,
1080
+ lastSeenAt,
1081
+ })
1082
+ }
1083
+ if (rest === "/balance") return json(res, 200, balance(u, seen))
1084
+ if (rest === "/calls") {
1085
+ q.set("userId", userId)
1086
+ return json(res, 200, page(state.calls, q))
1087
+ }
1088
+ if (rest === "/history")
1089
+ return json(res, 200, {
1090
+ entries: [...u.history].reverse(),
1091
+ nextCursor: null,
1092
+ })
1093
+ if (rest === "/checkout" && m === "POST") {
1094
+ const price = SALE.find((s) => s.currency === body.currency)
1095
+ if (!price)
1096
+ return fail(res, "invalid_request", "Unknown currency.", "api", {
1097
+ path: "currency",
1098
+ })
1099
+ if (!(body.amountCents >= OPTIONS.minimumCents))
1100
+ return fail(
1101
+ res,
1102
+ "purchase_too_small",
1103
+ "The smallest amount is 5.",
1104
+ "api",
1105
+ { minimumCents: OPTIONS.minimumCents },
1106
+ )
1107
+ if (
1108
+ body.amountCents > OPTIONS.maximumCents ||
1109
+ (OPTIONS.packsOnly && !OPTIONS.packsCents.includes(body.amountCents))
1110
+ )
1111
+ return fail(
1112
+ res,
1113
+ "invalid_request",
1114
+ "That amount is not offered.",
1115
+ "api",
1116
+ { path: "amountCents" },
1117
+ )
1118
+ const purchase = {
1119
+ id: id("pur"),
1120
+ kind: "checkout",
1121
+ status: "open",
1122
+ currency: body.currency,
1123
+ amountCents: body.amountCents,
1124
+ taxCents: null,
1125
+ totalCents: null,
1126
+ creditMicros: Math.floor(
1127
+ (body.amountCents * 1_000_000) / price.centsPerDollar,
1128
+ ),
1129
+ failure: null,
1130
+ createdAt: now(),
1131
+ paidAt: null,
1132
+ }
1133
+ state.purchases.set(purchase.id, {
1134
+ purchase,
1135
+ userId,
1136
+ successUrl: body.successUrl,
1137
+ saveCard: !!body.saveCard,
1138
+ })
1139
+ return json(res, 201, {
1140
+ purchaseId: purchase.id,
1141
+ url: `http://localhost:${PORT}/stub/checkout/${purchase.id}`,
1142
+ expiresAt: new Date(Date.now() + 86400e3).toISOString(),
1143
+ creditMicros: purchase.creditMicros,
1144
+ })
1145
+ }
1146
+ r = rest.match(/^\/purchases\/([^/]+)$/)
1147
+ if (r) {
1148
+ const x = state.purchases.get(r[1])
1149
+ return x && x.userId === userId
1150
+ ? json(res, 200, x.purchase)
1151
+ : fail(res, "not_found", "No such purchase.")
1152
+ }
1153
+ if (rest === "/autorefill") {
1154
+ if (m === "PUT") {
1155
+ // Only the settings: a card or payment method is never accepted here.
1156
+ const fields = [
1157
+ "enabled",
1158
+ "thresholdMicros",
1159
+ "amountCents",
1160
+ "currency",
1161
+ "monthlyMaxCents",
1162
+ ]
1163
+ const unknown = Object.keys(body).filter((k) => !fields.includes(k))
1164
+ if (unknown.length)
1165
+ return fail(
1166
+ res,
1167
+ "invalid_request",
1168
+ `Auto-refill takes only its settings; ${unknown.join(", ")} is not one.`,
1169
+ "api",
1170
+ { path: unknown[0] },
1171
+ )
1172
+ if (body.enabled && !u.autoRefill.card)
1173
+ return fail(res, "no_card", "Save a card first.")
1174
+ for (const k of fields)
1175
+ if (body[k] !== undefined) u.autoRefill[k] = body[k]
1176
+ }
1177
+ return json(res, 200, u.autoRefill)
1178
+ }
1179
+ if (rest === "/card" && m === "POST")
1180
+ return json(res, 201, {
1181
+ url: `http://localhost:${PORT}/stub/card/${encodeURIComponent(userId)}?to=${encodeURIComponent(body.successUrl)}`,
1182
+ expiresAt: new Date(Date.now() + 86400e3).toISOString(),
1183
+ })
1184
+ if (rest === "/card" && m === "DELETE") {
1185
+ u.autoRefill.card = null
1186
+ u.autoRefill.enabled = false
1187
+ return json(res, 204)
1188
+ }
1189
+ if (rest === "/own-endpoint") {
1190
+ if (m === "GET") return json(res, 200, u.own ? ownView(u) : null)
1191
+ if (m === "DELETE") {
1192
+ u.own = null
1193
+ u.order = "tokens_only"
1194
+ return json(res, 204)
1195
+ }
1196
+ if (!/^https?:\/\//.test(body.baseUrl ?? "") || !body.models?.length)
1197
+ return fail(
1198
+ res,
1199
+ "invalid_request",
1200
+ "baseUrl and models are needed.",
1201
+ "api",
1202
+ { path: "baseUrl" },
1203
+ )
1204
+ const key = body.apiKey === undefined ? u.own?.apiKey : body.apiKey
1205
+ u.own = {
1206
+ baseUrl: body.baseUrl,
1207
+ apiKey: key,
1208
+ hasKey: !!key,
1209
+ keyHint: key ? key.slice(-4) : null,
1210
+ models: body.models,
1211
+ defaultModel: body.defaultModel ?? body.models[0],
1212
+ capabilities: body.capabilities ?? [],
1213
+ toolChoice: body.toolChoice === "required" ? "required" : "auto",
1214
+ monthlyTokenLimit: body.monthlyTokenLimit ?? null,
1215
+ usedThisMonth: 0,
1216
+ periodEndsAt: new Date(
1217
+ Date.UTC(new Date().getUTCFullYear(), new Date().getUTCMonth() + 1),
1218
+ ).toISOString(),
1219
+ lastTest: null,
1220
+ upstream: env.TOKENS_STUB_OWN_UPSTREAM || null,
1221
+ }
1222
+ u.order = body.order ?? "own_then_tokens"
1223
+ return json(res, 200, ownView(u))
1224
+ }
1225
+ if (rest === "/own-endpoint/test") {
1226
+ if (!u.own)
1227
+ return fail(res, "byo_not_configured", "No own endpoint saved.")
1228
+ u.own.lastTest = {
1229
+ ok: !/fail/.test(u.own.baseUrl),
1230
+ at: now(),
1231
+ latencyMs: 42,
1232
+ models: u.own.models,
1233
+ error: /fail/.test(u.own.baseUrl)
1234
+ ? {
1235
+ code: "byo_failed",
1236
+ message: "The endpoint answered 401.",
1237
+ status: 401,
1238
+ }
1239
+ : null,
1240
+ }
1241
+ return json(res, 200, u.own.lastTest)
1242
+ }
1243
+ if (rest === "/backend-order" && m === "PUT") {
1244
+ if (!u.own && body.order !== "tokens_only")
1245
+ return fail(res, "byo_not_configured", "Save an own endpoint first.")
1246
+ u.order = body.order
1247
+ return json(res, 200, { order: u.order })
1248
+ }
1249
+ return fail(res, "not_found", "No such endpoint.")
1250
+ }
1251
+
1252
+ // ------------------------------------------------------------------ stub controls (no auth)
1253
+ async function control(req, res, url, raw) {
1254
+ const body = raw ? JSON.parse(raw) : {}
1255
+ if (url.pathname === "/stub/reset") {
1256
+ reset()
1257
+ return json(res, 200, { ok: true })
1258
+ }
1259
+ if (url.pathname === "/stub/mode") {
1260
+ Object.assign(state.mode, body)
1261
+ return json(res, 200, state.mode)
1262
+ }
1263
+ if (url.pathname === "/stub/state")
1264
+ return json(res, 200, {
1265
+ users: [...state.users.values()].map((u) => balance(u)),
1266
+ calls: state.calls.map(publicCall),
1267
+ mode: state.mode,
1268
+ })
1269
+ if (url.pathname === "/stub/balance") {
1270
+ const u = user(body.userId)
1271
+ u.purchased = body.purchasedMicros
1272
+ crossings(u)
1273
+ return json(res, 200, balance(u))
1274
+ }
1275
+ if (url.pathname === "/stub/grant") {
1276
+ const u = user(body.userId)
1277
+ if (body.provider === null)
1278
+ return fail(
1279
+ res,
1280
+ "invalid_request",
1281
+ "Every lot is locked to one provider.",
1282
+ "api",
1283
+ { path: "provider" },
1284
+ )
1285
+ const lot = {
1286
+ id: id("lot"),
1287
+ kind: body.kind ?? "airdrop",
1288
+ provider: body.provider ?? "anthropic",
1289
+ grantedMicros: body.micros,
1290
+ spentMicros: 0,
1291
+ remainingMicros: body.micros,
1292
+ expiresAt: body.expiresAt ?? null,
1293
+ reason: body.reason ?? null,
1294
+ createdAt: now(),
1295
+ }
1296
+ u.lots.push(lot)
1297
+ u.history.push({ type: "grant", at: lot.createdAt, lot })
1298
+ crossings(u)
1299
+ return json(
1300
+ res,
1301
+ 200,
1302
+ emit("grant.received", {
1303
+ userId: u.userId,
1304
+ source: lot.kind,
1305
+ lot,
1306
+ airdropId: lot.kind === "airdrop" ? id("drop") : null,
1307
+ balance: summary(u),
1308
+ }),
1309
+ )
1310
+ }
1311
+ if (url.pathname === "/stub/expire") {
1312
+ for (const u of state.users.values())
1313
+ for (const lot of u.lots)
1314
+ if (lot.id === body.lotId && lot.remainingMicros > 0) {
1315
+ const expiredMicros = lot.remainingMicros
1316
+ lot.remainingMicros = 0
1317
+ lot.expiresAt = now()
1318
+ u.history.push({
1319
+ type: "expiry",
1320
+ at: now(),
1321
+ lotId: lot.id,
1322
+ expiredMicros,
1323
+ })
1324
+ crossings(u)
1325
+ return json(
1326
+ res,
1327
+ 200,
1328
+ emit("grant.expired", {
1329
+ userId: u.userId,
1330
+ lot,
1331
+ expiredMicros,
1332
+ balance: summary(u),
1333
+ }),
1334
+ )
1335
+ }
1336
+ return fail(res, "not_found", "No such live lot.")
1337
+ }
1338
+ if (url.pathname === "/stub/autorefill-fail") {
1339
+ const u = user(body.userId)
1340
+ u.autoRefill.enabled = false
1341
+ u.autoRefill.failedAt = now()
1342
+ return json(
1343
+ res,
1344
+ 200,
1345
+ emit("autorefill.failed", {
1346
+ userId: u.userId,
1347
+ purchaseId: null,
1348
+ reason: "Your card was declined.",
1349
+ }),
1350
+ )
1351
+ }
1352
+ let r = url.pathname.match(/^\/stub\/checkout\/([^/]+)$/)
1353
+ if (r) {
1354
+ const x = state.purchases.get(r[1])
1355
+ if (!x) return fail(res, "not_found", "No such purchase.")
1356
+ if (x.purchase.status === "open") {
1357
+ const u = user(x.userId)
1358
+ Object.assign(x.purchase, {
1359
+ status: "paid",
1360
+ paidAt: now(),
1361
+ taxCents: 0,
1362
+ totalCents: x.purchase.amountCents,
1363
+ })
1364
+ u.purchased += x.purchase.creditMicros
1365
+ if (x.saveCard) u.autoRefill.card = { label: "Visa ending 4242" }
1366
+ u.history.push({ type: "purchase", at: now(), purchase: x.purchase })
1367
+ crossings(u)
1368
+ emit("purchase.completed", {
1369
+ userId: u.userId,
1370
+ purchase: x.purchase,
1371
+ balance: summary(u),
1372
+ })
1373
+ }
1374
+ res.writeHead(303, {
1375
+ location: (x.successUrl ?? "/").replace("{PURCHASE_ID}", x.purchase.id),
1376
+ })
1377
+ return res.end()
1378
+ }
1379
+ r = url.pathname.match(/^\/stub\/card\/([^/]+)$/)
1380
+ if (r) {
1381
+ user(decodeURIComponent(r[1])).autoRefill.card = {
1382
+ label: "Visa ending 4242",
1383
+ }
1384
+ res.writeHead(303, { location: url.searchParams.get("to") ?? "/" })
1385
+ return res.end()
1386
+ }
1387
+ return fail(res, "not_found", "No such stub control.")
1388
+ }
1389
+
1390
+ // ------------------------------------------------------------------ server
1391
+ const server = createServer(async (req, res) => {
1392
+ const url = new URL(req.url, `http://localhost:${PORT}`)
1393
+ try {
1394
+ const raw = await readBody(req)
1395
+ if (url.pathname.startsWith("/stub/"))
1396
+ return await control(req, res, url, raw)
1397
+ const shape =
1398
+ url.pathname === "/v1/messages"
1399
+ ? "anthropic"
1400
+ : url.pathname.startsWith("/v1/")
1401
+ ? "openai"
1402
+ : "api"
1403
+ if (state.mode.down)
1404
+ return fail(
1405
+ res,
1406
+ "center_unavailable",
1407
+ "The token center is unavailable.",
1408
+ shape,
1409
+ )
1410
+ const key =
1411
+ req.headers["x-api-key"] ??
1412
+ (req.headers.authorization ?? "").replace(/^Bearer\s+/i, "")
1413
+ if (!key || (KEY && key !== KEY))
1414
+ return fail(res, "unauthenticated", "Unknown app key.", shape)
1415
+ if (url.pathname === "/v1/models")
1416
+ return json(res, 200, {
1417
+ object: "list",
1418
+ data: MODELS.map((x) => ({
1419
+ id: x.id,
1420
+ object: "model",
1421
+ type: "model",
1422
+ display_name: x.displayName,
1423
+ created: 1767225600,
1424
+ created_at: PRICE.effectiveFrom,
1425
+ owned_by: x.provider,
1426
+ })),
1427
+ has_more: false,
1428
+ first_id: MODELS[0].id,
1429
+ last_id: MODELS.at(-1).id,
1430
+ })
1431
+ if (
1432
+ req.method === "POST" &&
1433
+ ["/v1/messages", "/v1/chat/completions", "/v1/audio/speech"].includes(
1434
+ url.pathname,
1435
+ )
1436
+ )
1437
+ return await gateway(req, res, url, raw)
1438
+ if (url.pathname.startsWith("/api/v1/"))
1439
+ return await api(req, res, url, raw)
1440
+ fail(res, "not_found", "No such endpoint.", shape)
1441
+ } catch (e) {
1442
+ console.error("[stub]", e)
1443
+ if (!res.headersSent) fail(res, "internal_error", "Stub error.")
1444
+ else res.end()
1445
+ }
1446
+ })
1447
+ return new Promise((resolve, reject) => {
1448
+ server.once("error", reject)
1449
+ server.listen(PORT, () => {
1450
+ PORT = server.address().port
1451
+ resolve({
1452
+ url: `http://localhost:${PORT}`,
1453
+ port: PORT,
1454
+ reset,
1455
+ close: () =>
1456
+ new Promise((done) => {
1457
+ for (const t of timers) clearTimeout(t)
1458
+ timers.clear()
1459
+ server.closeAllConnections?.()
1460
+ server.close(() => done())
1461
+ }),
1462
+ })
1463
+ })
1464
+ })
1465
+ }