one-model-router 0.0.10 → 0.0.12

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 (80) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +32 -5
  3. package/bin/omr.js +46 -0
  4. package/package.json +3 -2
  5. package/src/admin.js +965 -941
  6. package/src/balances.js +61 -61
  7. package/src/boot.js +117 -117
  8. package/src/channels/buddy.js +2031 -2031
  9. package/src/channels/cline.js +753 -753
  10. package/src/channels/codearts.js +2051 -2051
  11. package/src/channels/comate.js +830 -830
  12. package/src/channels/doubaowork.js +1784 -1784
  13. package/src/channels/freebuff.js +1316 -1316
  14. package/src/channels/gemini.js +1721 -1721
  15. package/src/channels/index.js +270 -270
  16. package/src/channels/joycode.js +936 -936
  17. package/src/channels/kimi.js +572 -572
  18. package/src/channels/lobsterai.js +1226 -1226
  19. package/src/channels/loomy.js +1032 -1032
  20. package/src/channels/miaoshou.js +1396 -1396
  21. package/src/channels/mimo.js +474 -474
  22. package/src/channels/minimax.js +1451 -1451
  23. package/src/channels/qoder-wasm.js +434 -434
  24. package/src/channels/qoder.js +1619 -1619
  25. package/src/channels/raccoon.js +1842 -1842
  26. package/src/channels/tabbit.js +1457 -1457
  27. package/src/channels/trae.js +1405 -1405
  28. package/src/channels/zcode-global.js +35 -35
  29. package/src/channels/zcode.js +2085 -2085
  30. package/src/checkin.js +496 -496
  31. package/src/credits.js +464 -464
  32. package/src/crypto.js +132 -132
  33. package/src/free-models.js +683 -683
  34. package/src/gateway.js +596 -578
  35. package/src/health.js +86 -86
  36. package/src/model-key.js +35 -35
  37. package/src/paths.js +96 -96
  38. package/src/probe.js +318 -318
  39. package/src/providers/builtin.js +626 -626
  40. package/src/providers/index.js +483 -457
  41. package/src/providers/kilo.js +111 -111
  42. package/src/providers/last-good.js +69 -69
  43. package/src/providers/modelsdev.js +242 -242
  44. package/src/providers/openai-compat.js +312 -312
  45. package/src/providers/opencode/bun-fetch.js +226 -226
  46. package/src/providers/opencode/lane.js +186 -186
  47. package/src/providers/opencode/o2a/LICENSE.dsh-our-free-model +21 -21
  48. package/src/providers/opencode/o2a/asset-source.js +43 -43
  49. package/src/providers/opencode/o2a/assets/opencode_prompt_v2.txt +96 -96
  50. package/src/providers/opencode/o2a/assets/opencode_tools.json +327 -327
  51. package/src/providers/opencode/o2a/entry.js +17 -17
  52. package/src/providers/opencode/o2a/gateway-body.js +42 -42
  53. package/src/providers/opencode/o2a/headers.js +133 -133
  54. package/src/providers/opencode/o2a/identity.js +148 -148
  55. package/src/providers/opencode/o2a/official-prompt.js +191 -191
  56. package/src/providers/opencode/o2a/official-tools.js +110 -110
  57. package/src/providers/opencode/o2a/tool-renames.js +217 -217
  58. package/src/providers/opencode/o2a/toolshape.js +418 -418
  59. package/src/providers/opencode/o2a/version.js +37 -37
  60. package/src/providers/opencode/upstream.js +403 -403
  61. package/src/providers/opencode/wire.js +496 -496
  62. package/src/providers/opencode.js +652 -652
  63. package/src/router.js +629 -629
  64. package/src/server.js +49 -33
  65. package/src/store.js +467 -467
  66. package/src/sync/index.js +1529 -1529
  67. package/src/sync/login-preload.cjs +52 -52
  68. package/src/sync/noise.js +141 -141
  69. package/src/sync/relay-http.js +176 -176
  70. package/src/sync/remote-host.js +818 -818
  71. package/src/sync/remote-http.js +112 -112
  72. package/src/sync/remote-login.js +657 -657
  73. package/src/sync/remote-pairing.js +204 -204
  74. package/src/sync/remote-protocol.js +321 -321
  75. package/src/sync/snapshot.js +211 -211
  76. package/src/update.js +458 -0
  77. package/src/util.js +334 -334
  78. package/web/app.js +4010 -3972
  79. package/web/index.html +32 -32
  80. package/web/style.css +971 -971
@@ -1,148 +1,148 @@
1
- /**
2
- * The session and project identity the upstream free tier inspects.
3
- *
4
- * Upstream facts (each verified against the live upstream):
5
- *
6
- * - The free tier validates the session id's **encoding**, not just the `ses_`
7
- * shape: the twelve hex digits must be the descending `~(ts_ms << 12 | counter)`
8
- * value with counter 1–63 (low 12 bits `0xfc0–0xfff`); anything else 403s.
9
- * - The timestamp inside a session must also stay plausible. A session dated
10
- * more than about a year from now, in either direction, is answered with HTTP
11
- * 429 "Free usage limit reached" — the same message a genuinely exhausted
12
- * egress receives — so a purely hash-derived timestamp (which lands in 1970)
13
- * fails every request. The mint below draws the date from a slow clock
14
- * instead: today's UTC midnight minus a hash-derived offset of at most
15
- * {@link SESSION_TIMESTAMP_WINDOW_MS}.
16
- * - `bootNonce` separates the sessions one process derives from those its
17
- * earlier runs derived, so a restart re-mints while a single run keeps one
18
- * stable id per conversation signal.
19
- * - Free-tier quota is ledged per **project id × egress**, so a shared project
20
- * id is one shared bucket. When the caller names no project, one is minted
21
- * per request ({@link randomProjectID}); a named `global` or 40-hex git
22
- * commit is kept verbatim (`headers.projectID`).
23
- *
24
- * {@link sessionPhaseMs} / {@link phaseAgeMs} decode
25
- * the time phase back out of a session id. The gateway never needs them (it
26
- * only mints); the lane uses them to re-mint persisted sessions whose phase
27
- * left the accepted window — e.g. ids carried over from an earlier era —
28
- * instead of replaying a dead id forever.
29
- *
30
- * @module src/o2a/identity.js
31
- */
32
-
33
- import crypto from 'node:crypto'
34
-
35
- /** `ses_` + 12 hex + 14 base62 — the shape the free tier accepts since 2026-09-16. */
36
- export const CANONICAL_SESSION_RE = /^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$/
37
-
38
- /**
39
- * The descending time part: 9 free hex digits, then `f[c-f]`, then one free
40
- * digit — exactly the counter window 1–63 (`0xfff` down to `0xfc0`).
41
- */
42
- export const DESCENDING_TIME_RE = /^[0-9a-f]{9}f[c-f][0-9a-f]$/
43
-
44
- export const BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
45
-
46
- /** How far before today a synthesized session's timestamp may fall (`sessionTimestampWindow`). */
47
- export const SESSION_TIMESTAMP_WINDOW_MS = 30 * 24 * 60 * 60 * 1000
48
-
49
- /**
50
- * The per-process session salt (`bootNonce`). Mixed into every synthesized
51
- * session: stable within one process run, fresh after a restart.
52
- */
53
- const BOOT_NONCE = crypto.randomBytes(16)
54
-
55
- /** Whether `signal` is a session the official client minted (descending encoding). */
56
- export function isOfficialSessionID(signal) {
57
- return typeof signal === 'string'
58
- && CANONICAL_SESSION_RE.test(signal)
59
- && DESCENDING_TIME_RE.test(signal.slice(4, 16))
60
- }
61
-
62
- /**
63
- * The canonical session for `signal`, stable for the life of the process.
64
- * An official client-minted session is kept verbatim to
65
- * preserve upstream prompt-cache affinity; every other signal is re-encoded.
66
- */
67
- export function canonicalSessionID(signal, now = Date.now()) {
68
- if (isOfficialSessionID(signal)) return signal
69
- const digest = crypto.createHash('sha256')
70
- .update(Buffer.concat([Buffer.from('ses\0'), BOOT_NONCE, Buffer.from([0]), Buffer.from(String(signal))]))
71
- .digest()
72
- const timePart = descendingTimePart(now, digest)
73
- const randomPart = base62Fixed(digest.subarray(6, 16), 14)
74
- return `ses_${timePart}${randomPart}`
75
- }
76
-
77
- /**
78
- * The width of the time phase a session id carries. The wire's time part is 12
79
- * hex characters = 48 bits = `(ms << 12 | counter)` truncated to 48 bits
80
- * (`^current & 0xFFFFFFFFFFFF`) — so the
81
- * decodable time is the low 36 bits of the millisecond clock, a phase that
82
- * repeats every {@link SESSION_PHASE_MS} (~795 days), not an absolute date.
83
- */
84
- export const SESSION_PHASE_MS = 2n ** 36n
85
-
86
- /**
87
- * Decode the millisecond time phase a canonical session id carries, or null
88
- * when the id does not carry the descending encoding. Compare it against
89
- * {@link currentTimePhaseMs}, never
90
- * against the absolute clock.
91
- */
92
- export function sessionPhaseMs(id) {
93
- if (!isOfficialSessionID(id)) return null
94
- const inverted = BigInt(`0x${id.slice(4, 16)}`)
95
- const current = ~inverted & 0xffffffffffffn
96
- return Number(current >> 12n)
97
- }
98
-
99
- /** The current clock's 36-bit millisecond phase — the value ids are dated in. */
100
- export function currentTimePhaseMs(now = Date.now()) {
101
- return now % Number(SESSION_PHASE_MS)
102
- }
103
-
104
- /** How long ago, within the phase cycle, `phase` sits relative to now's phase. */
105
- export function phaseAgeMs(phase, now = Date.now()) {
106
- const nowPhase = BigInt(now % Number(SESSION_PHASE_MS))
107
- const age = (nowPhase - BigInt(phase)) % SESSION_PHASE_MS
108
- return Number((age + SESSION_PHASE_MS) % SESSION_PHASE_MS)
109
- }
110
-
111
- /**
112
- * Render a plausible recent timestamp in the descending layout:
113
- * today's UTC midnight minus a hash-derived offset of
114
- * at most {@link SESSION_TIMESTAMP_WINDOW_MS}, shifted `<< 12 | 1`, inverted.
115
- * The hash only picks the offset; it never supplies the date.
116
- */
117
- export function descendingTimePart(now = Date.now(), hash) {
118
- const offset = (((hash[0] << 24) | (hash[1] << 16) | (hash[2] << 8) | hash[3]) >>> 0) % SESSION_TIMESTAMP_WINDOW_MS
119
- const midnight = Math.floor(now / 86400000) * 86400000
120
- const base = midnight - offset
121
- const inverted = ~((BigInt(base) << 12n) | 1n) & 0xffffffffffffn
122
- return inverted.toString(16).padStart(12, '0')
123
- }
124
-
125
- /** Render `bytes` as exactly `width` base62 characters, big-endian. */
126
- export function base62Fixed(bytes, width) {
127
- let n = 0n
128
- for (const byte of bytes) n = (n << 8n) | BigInt(byte)
129
- const out = new Array(width)
130
- for (let i = width - 1; i >= 0; i -= 1) {
131
- out[i] = BASE62_ALPHABET[Number(n % 62n)]
132
- n /= 62n
133
- }
134
- return out.join('')
135
- }
136
-
137
- /** `prefix` + "_" + `size` random bytes in hex. */
138
- export function randomID(prefix, size) {
139
- return `${prefix}_${crypto.randomBytes(size).toString('hex')}`
140
- }
141
-
142
- /**
143
- * A project id in the shape the official CLI sends — the 40-character hex of a
144
- * git root commit — minted per request when the client names no project.
145
- */
146
- export function randomProjectID() {
147
- return crypto.randomBytes(20).toString('hex')
148
- }
1
+ /**
2
+ * The session and project identity the upstream free tier inspects.
3
+ *
4
+ * Upstream facts (each verified against the live upstream):
5
+ *
6
+ * - The free tier validates the session id's **encoding**, not just the `ses_`
7
+ * shape: the twelve hex digits must be the descending `~(ts_ms << 12 | counter)`
8
+ * value with counter 1–63 (low 12 bits `0xfc0–0xfff`); anything else 403s.
9
+ * - The timestamp inside a session must also stay plausible. A session dated
10
+ * more than about a year from now, in either direction, is answered with HTTP
11
+ * 429 "Free usage limit reached" — the same message a genuinely exhausted
12
+ * egress receives — so a purely hash-derived timestamp (which lands in 1970)
13
+ * fails every request. The mint below draws the date from a slow clock
14
+ * instead: today's UTC midnight minus a hash-derived offset of at most
15
+ * {@link SESSION_TIMESTAMP_WINDOW_MS}.
16
+ * - `bootNonce` separates the sessions one process derives from those its
17
+ * earlier runs derived, so a restart re-mints while a single run keeps one
18
+ * stable id per conversation signal.
19
+ * - Free-tier quota is ledged per **project id × egress**, so a shared project
20
+ * id is one shared bucket. When the caller names no project, one is minted
21
+ * per request ({@link randomProjectID}); a named `global` or 40-hex git
22
+ * commit is kept verbatim (`headers.projectID`).
23
+ *
24
+ * {@link sessionPhaseMs} / {@link phaseAgeMs} decode
25
+ * the time phase back out of a session id. The gateway never needs them (it
26
+ * only mints); the lane uses them to re-mint persisted sessions whose phase
27
+ * left the accepted window — e.g. ids carried over from an earlier era —
28
+ * instead of replaying a dead id forever.
29
+ *
30
+ * @module src/o2a/identity.js
31
+ */
32
+
33
+ import crypto from 'node:crypto'
34
+
35
+ /** `ses_` + 12 hex + 14 base62 — the shape the free tier accepts since 2026-09-16. */
36
+ export const CANONICAL_SESSION_RE = /^ses_[0-9a-f]{12}[0-9A-Za-z]{14}$/
37
+
38
+ /**
39
+ * The descending time part: 9 free hex digits, then `f[c-f]`, then one free
40
+ * digit — exactly the counter window 1–63 (`0xfff` down to `0xfc0`).
41
+ */
42
+ export const DESCENDING_TIME_RE = /^[0-9a-f]{9}f[c-f][0-9a-f]$/
43
+
44
+ export const BASE62_ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
45
+
46
+ /** How far before today a synthesized session's timestamp may fall (`sessionTimestampWindow`). */
47
+ export const SESSION_TIMESTAMP_WINDOW_MS = 30 * 24 * 60 * 60 * 1000
48
+
49
+ /**
50
+ * The per-process session salt (`bootNonce`). Mixed into every synthesized
51
+ * session: stable within one process run, fresh after a restart.
52
+ */
53
+ const BOOT_NONCE = crypto.randomBytes(16)
54
+
55
+ /** Whether `signal` is a session the official client minted (descending encoding). */
56
+ export function isOfficialSessionID(signal) {
57
+ return typeof signal === 'string'
58
+ && CANONICAL_SESSION_RE.test(signal)
59
+ && DESCENDING_TIME_RE.test(signal.slice(4, 16))
60
+ }
61
+
62
+ /**
63
+ * The canonical session for `signal`, stable for the life of the process.
64
+ * An official client-minted session is kept verbatim to
65
+ * preserve upstream prompt-cache affinity; every other signal is re-encoded.
66
+ */
67
+ export function canonicalSessionID(signal, now = Date.now()) {
68
+ if (isOfficialSessionID(signal)) return signal
69
+ const digest = crypto.createHash('sha256')
70
+ .update(Buffer.concat([Buffer.from('ses\0'), BOOT_NONCE, Buffer.from([0]), Buffer.from(String(signal))]))
71
+ .digest()
72
+ const timePart = descendingTimePart(now, digest)
73
+ const randomPart = base62Fixed(digest.subarray(6, 16), 14)
74
+ return `ses_${timePart}${randomPart}`
75
+ }
76
+
77
+ /**
78
+ * The width of the time phase a session id carries. The wire's time part is 12
79
+ * hex characters = 48 bits = `(ms << 12 | counter)` truncated to 48 bits
80
+ * (`^current & 0xFFFFFFFFFFFF`) — so the
81
+ * decodable time is the low 36 bits of the millisecond clock, a phase that
82
+ * repeats every {@link SESSION_PHASE_MS} (~795 days), not an absolute date.
83
+ */
84
+ export const SESSION_PHASE_MS = 2n ** 36n
85
+
86
+ /**
87
+ * Decode the millisecond time phase a canonical session id carries, or null
88
+ * when the id does not carry the descending encoding. Compare it against
89
+ * {@link currentTimePhaseMs}, never
90
+ * against the absolute clock.
91
+ */
92
+ export function sessionPhaseMs(id) {
93
+ if (!isOfficialSessionID(id)) return null
94
+ const inverted = BigInt(`0x${id.slice(4, 16)}`)
95
+ const current = ~inverted & 0xffffffffffffn
96
+ return Number(current >> 12n)
97
+ }
98
+
99
+ /** The current clock's 36-bit millisecond phase — the value ids are dated in. */
100
+ export function currentTimePhaseMs(now = Date.now()) {
101
+ return now % Number(SESSION_PHASE_MS)
102
+ }
103
+
104
+ /** How long ago, within the phase cycle, `phase` sits relative to now's phase. */
105
+ export function phaseAgeMs(phase, now = Date.now()) {
106
+ const nowPhase = BigInt(now % Number(SESSION_PHASE_MS))
107
+ const age = (nowPhase - BigInt(phase)) % SESSION_PHASE_MS
108
+ return Number((age + SESSION_PHASE_MS) % SESSION_PHASE_MS)
109
+ }
110
+
111
+ /**
112
+ * Render a plausible recent timestamp in the descending layout:
113
+ * today's UTC midnight minus a hash-derived offset of
114
+ * at most {@link SESSION_TIMESTAMP_WINDOW_MS}, shifted `<< 12 | 1`, inverted.
115
+ * The hash only picks the offset; it never supplies the date.
116
+ */
117
+ export function descendingTimePart(now = Date.now(), hash) {
118
+ const offset = (((hash[0] << 24) | (hash[1] << 16) | (hash[2] << 8) | hash[3]) >>> 0) % SESSION_TIMESTAMP_WINDOW_MS
119
+ const midnight = Math.floor(now / 86400000) * 86400000
120
+ const base = midnight - offset
121
+ const inverted = ~((BigInt(base) << 12n) | 1n) & 0xffffffffffffn
122
+ return inverted.toString(16).padStart(12, '0')
123
+ }
124
+
125
+ /** Render `bytes` as exactly `width` base62 characters, big-endian. */
126
+ export function base62Fixed(bytes, width) {
127
+ let n = 0n
128
+ for (const byte of bytes) n = (n << 8n) | BigInt(byte)
129
+ const out = new Array(width)
130
+ for (let i = width - 1; i >= 0; i -= 1) {
131
+ out[i] = BASE62_ALPHABET[Number(n % 62n)]
132
+ n /= 62n
133
+ }
134
+ return out.join('')
135
+ }
136
+
137
+ /** `prefix` + "_" + `size` random bytes in hex. */
138
+ export function randomID(prefix, size) {
139
+ return `${prefix}_${crypto.randomBytes(size).toString('hex')}`
140
+ }
141
+
142
+ /**
143
+ * A project id in the shape the official CLI sends — the 40-character hex of a
144
+ * git root commit — minted per request when the client names no project.
145
+ */
146
+ export function randomProjectID() {
147
+ return crypto.randomBytes(20).toString('hex')
148
+ }