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,211 +1,211 @@
1
- /**
2
- * 快照与分区构建:把桌面端当前状态整理成小程序端约定的数据形状。
3
- *
4
- * 与小程序端 utils/sync.js 的缓存键契约保持一致:
5
- * stats / requests / catalog / groups / channels / gateway / checkin / balances
6
- * + health / probe / probeMeta / freeModels / providers / settings / startedAt
7
- * (批 1 起:小程序数据面与桌面管理面逐字段对齐,凭据类一律脱敏)
8
- *
9
- * 安全边界:这里的数据会离开本机(经云服务到手机),只允许出现
10
- * 展示/控制所需的最小字段 —— 渠道凭据、apiKey、密码、MCP env(可能含密钥)一律不出境。
11
- */
12
-
13
- import { runtime, loadSettings, loadGroups, loadProviders, channelState, loadBalances } from '../store.js'
14
- import { buildCatalog } from '../providers/index.js'
15
- import { CHANNELS } from '../channels/index.js'
16
- import { autoCheckinState } from '../checkin.js'
17
- import { creditsCapabilities } from '../credits.js'
18
- import * as health from '../health.js'
19
- import * as probe from '../probe.js'
20
- import { freeModelsState } from '../free-models.js'
21
-
22
- /**
23
- * 下发给小程序的最近请求条数:手机查看最近 15 条流水(桌面管理面
24
- * admin.js 仍读 runtime.recentRequests 的 500 条,与本常量无关)。
25
- */
26
- const SYNC_REQUESTS_LIMIT = 15
27
-
28
- /**
29
- * 可单独下发的分区名清单(= buildSection 的入参 = 小程序缓存键名)。
30
- * 全量快照覆盖的就是这些分区,增量帧里没出现的分区绝不能被小程序清空。
31
- */
32
- export const SECTION_NAMES = [
33
- 'stats', 'requests', 'catalog', 'groups', 'channels', 'gateway', 'settings',
34
- 'health', 'probe', 'probeMeta', 'freeModels', 'providers', 'checkin', 'balances', 'startedAt',
35
- ]
36
-
37
- /** 自定义提供商脱敏:剔除 apiKey 与槽位密钥(结构保留供小程序展示/管理)。 */
38
- function sanitizeProviders(providersDoc) {
39
- return {
40
- custom: (providersDoc.custom ?? []).map(p => ({
41
- ...p,
42
- apiKey: undefined,
43
- slots: Array.isArray(p.slots) ? p.slots.map(s => ({ ...s, apiKey: undefined })) : p.slots,
44
- })),
45
- }
46
- }
47
-
48
- /** 模型组全字段(与桌面 groups.json 展示面一致)。 */
49
- function groupsSummary() {
50
- return (loadGroups().groups ?? []).map(g => ({
51
- id: g.id,
52
- name: g.name ?? g.id,
53
- strategy: g.strategy ?? 'fallback',
54
- stickyLimit: g.stickyLimit ?? 1,
55
- enabled: g.enabled !== false,
56
- models: Array.isArray(g.models) ? g.models : [],
57
- disabled: Array.isArray(g.disabled) ? g.disabled : [],
58
- }))
59
- }
60
-
61
- /** 每个渠道的对外摘要(无凭据字段)。 */
62
- function channelSummaries() {
63
- const auto = autoCheckinState()
64
- const caps = creditsCapabilities()
65
- const checkinByProvider = new Map((auto.channels ?? []).map(c => [c.provider, c]))
66
- return CHANNELS.map(ch => {
67
- const st = channelState(ch.meta.id)
68
- const checkin = checkinByProvider.get(ch.meta.id) ?? null
69
- return {
70
- id: ch.meta.id,
71
- label: ch.meta.label,
72
- enabled: st.enabled === true,
73
- canCheckin: caps[ch.meta.id]?.checkin === true,
74
- models: st.models ?? [],
75
- modelRates: st.modelRates ?? {},
76
- accounts: (st.accounts ?? []).map(a => ({
77
- id: a.id,
78
- label: a.label,
79
- enabled: a.enabled !== false,
80
- lastError: a.lastError ?? '',
81
- expiresAt: a.expiresAt ?? 0,
82
- refreshable: a.refreshable === true,
83
- autoCheckin: a.autoCheckin === true,
84
- })),
85
- checkin: checkin ? { lastResult: checkin.text ?? '', credit: checkin.credit ?? null, claimed: checkin.claimed ?? 0, at: checkin.at ?? 0, state: checkin.state ?? '', note: checkin.note ?? '', accounts: checkin.accounts ?? [] } : null,
86
- }
87
- })
88
- }
89
-
90
- /** 网关与调度设置摘要(apiKey 永不出境;含免密渠道 kilo/opencode 摘要)。 */
91
- function settingsSummary() {
92
- const s = loadSettings()
93
- const gw = s.gateway ?? {}
94
- return {
95
- gateway: {
96
- enabled: gw.enabled !== false,
97
- host: gw.host ?? '127.0.0.1',
98
- port: gw.port ?? 18682,
99
- headerTimeoutMs: gw.headerTimeoutMs ?? 60000,
100
- idleTimeoutMs: gw.idleTimeoutMs ?? 120000,
101
- turnTimeoutMs: gw.turnTimeoutMs ?? 300000,
102
- candidateTimeoutMs: gw.candidateTimeoutMs ?? 45000,
103
- hedgeDelayMs: gw.hedgeDelayMs ?? 6000,
104
- hedgeMaxParallel: gw.hedgeMaxParallel ?? 2,
105
- },
106
- probe: {
107
- enabled: s.probe?.enabled !== false,
108
- intervalMinutes: s.probe?.intervalMinutes ?? 15,
109
- },
110
- freeModels: {
111
- // 小时扫描把各转发渠道的勾选清单整体替换为上游可用清单并刷新倍率
112
- autoSelectAvailable: s.freeModels?.autoSelectAvailable !== false,
113
- },
114
- checkin: {
115
- enabled: s.checkin?.enabled !== false,
116
- delayMs: s.checkin?.delayMs ?? 15000,
117
- },
118
- // 免密渠道(baseUrl 展示 + 启停 + 槽位摘要,密钥永不出境)
119
- kilo: {
120
- enabled: s.kilo?.enabled !== false,
121
- baseUrl: s.kilo?.baseUrl ?? '',
122
- },
123
- opencode: {
124
- enabled: s.opencode?.enabled !== false,
125
- baseUrl: s.opencode?.baseUrl ?? '',
126
- slots: (Array.isArray(s.opencode?.slots) ? s.opencode.slots : []).map(sl => ({
127
- label: sl.label ?? '',
128
- enabled: sl.enabled !== false,
129
- anonymous: sl.apiKey === 'public',
130
- })),
131
- },
132
- }
133
- }
134
-
135
- /** 全量快照(连接建立 / Client resync / 配对首帧 / 老客户端兜底时推)。 */
136
- export async function buildSnapshot() {
137
- const [catalog, providersDoc] = await Promise.all([buildCatalog(), Promise.resolve(loadProviders())])
138
- return {
139
- generatedAt: Date.now(),
140
- startedAt: runtime.startedAt,
141
- stats: { ...runtime.stats },
142
- recentRequests: runtime.recentRequests.slice(0, SYNC_REQUESTS_LIMIT),
143
- catalog: catalog.filter(r => !r.offline),
144
- groups: groupsSummary(),
145
- channels: channelSummaries(),
146
- gateway: settingsSummary().gateway,
147
- settings: settingsSummary(),
148
- health: health.snapshot(),
149
- probe: probe.probeSnapshot(),
150
- probeMeta: probe.probeMeta(),
151
- freeModels: freeModelsState(),
152
- providers: sanitizeProviders(providersDoc),
153
- checkin: autoCheckinState(),
154
- balances: loadBalances(),
155
- }
156
- }
157
-
158
- /**
159
- * 单分区增量。分区名即小程序端 `wx.setStorageSync('omr_cache_' + name)` 的缓存键
160
- * (requests 对应全量快照里的 recentRequests),未知分区返回 null。
161
- */
162
- export async function buildSection(section) {
163
- switch (section) {
164
- case 'stats':
165
- return { ...runtime.stats }
166
- case 'requests':
167
- return runtime.recentRequests.slice(0, SYNC_REQUESTS_LIMIT)
168
- case 'catalog':
169
- return (await buildCatalog()).filter(r => !r.offline)
170
- case 'groups':
171
- return groupsSummary()
172
- case 'channels':
173
- return channelSummaries()
174
- case 'gateway':
175
- return settingsSummary().gateway
176
- case 'settings':
177
- return settingsSummary()
178
- case 'health':
179
- return health.snapshot()
180
- case 'probe':
181
- return probe.probeSnapshot()
182
- case 'probeMeta':
183
- return probe.probeMeta()
184
- case 'freeModels':
185
- return freeModelsState()
186
- case 'providers':
187
- return sanitizeProviders(loadProviders())
188
- case 'checkin':
189
- return autoCheckinState()
190
- case 'balances':
191
- return loadBalances()
192
- case 'startedAt':
193
- return runtime.startedAt
194
- default:
195
- return null
196
- }
197
- }
198
-
199
- /**
200
- * 攒批增量:只构建本次变化的分区。未知分区名(buildSection 返回 null)跳过,
201
- * 跳过的分区在增量帧里不出现 → 小程序保持原缓存,绝不被清空。
202
- */
203
- export async function buildSections(names = []) {
204
- const out = {}
205
- for (const name of names) {
206
- const payload = await buildSection(name)
207
- if (payload === null || payload === undefined) continue
208
- out[name] = payload
209
- }
210
- return out
211
- }
1
+ /**
2
+ * 快照与分区构建:把桌面端当前状态整理成小程序端约定的数据形状。
3
+ *
4
+ * 与小程序端 utils/sync.js 的缓存键契约保持一致:
5
+ * stats / requests / catalog / groups / channels / gateway / checkin / balances
6
+ * + health / probe / probeMeta / freeModels / providers / settings / startedAt
7
+ * (批 1 起:小程序数据面与桌面管理面逐字段对齐,凭据类一律脱敏)
8
+ *
9
+ * 安全边界:这里的数据会离开本机(经云服务到手机),只允许出现
10
+ * 展示/控制所需的最小字段 —— 渠道凭据、apiKey、密码、MCP env(可能含密钥)一律不出境。
11
+ */
12
+
13
+ import { runtime, loadSettings, loadGroups, loadProviders, channelState, loadBalances } from '../store.js'
14
+ import { buildCatalog } from '../providers/index.js'
15
+ import { CHANNELS } from '../channels/index.js'
16
+ import { autoCheckinState } from '../checkin.js'
17
+ import { creditsCapabilities } from '../credits.js'
18
+ import * as health from '../health.js'
19
+ import * as probe from '../probe.js'
20
+ import { freeModelsState } from '../free-models.js'
21
+
22
+ /**
23
+ * 下发给小程序的最近请求条数:手机查看最近 15 条流水(桌面管理面
24
+ * admin.js 仍读 runtime.recentRequests 的 500 条,与本常量无关)。
25
+ */
26
+ const SYNC_REQUESTS_LIMIT = 15
27
+
28
+ /**
29
+ * 可单独下发的分区名清单(= buildSection 的入参 = 小程序缓存键名)。
30
+ * 全量快照覆盖的就是这些分区,增量帧里没出现的分区绝不能被小程序清空。
31
+ */
32
+ export const SECTION_NAMES = [
33
+ 'stats', 'requests', 'catalog', 'groups', 'channels', 'gateway', 'settings',
34
+ 'health', 'probe', 'probeMeta', 'freeModels', 'providers', 'checkin', 'balances', 'startedAt',
35
+ ]
36
+
37
+ /** 自定义提供商脱敏:剔除 apiKey 与槽位密钥(结构保留供小程序展示/管理)。 */
38
+ function sanitizeProviders(providersDoc) {
39
+ return {
40
+ custom: (providersDoc.custom ?? []).map(p => ({
41
+ ...p,
42
+ apiKey: undefined,
43
+ slots: Array.isArray(p.slots) ? p.slots.map(s => ({ ...s, apiKey: undefined })) : p.slots,
44
+ })),
45
+ }
46
+ }
47
+
48
+ /** 模型组全字段(与桌面 groups.json 展示面一致)。 */
49
+ function groupsSummary() {
50
+ return (loadGroups().groups ?? []).map(g => ({
51
+ id: g.id,
52
+ name: g.name ?? g.id,
53
+ strategy: g.strategy ?? 'fallback',
54
+ stickyLimit: g.stickyLimit ?? 1,
55
+ enabled: g.enabled !== false,
56
+ models: Array.isArray(g.models) ? g.models : [],
57
+ disabled: Array.isArray(g.disabled) ? g.disabled : [],
58
+ }))
59
+ }
60
+
61
+ /** 每个渠道的对外摘要(无凭据字段)。 */
62
+ function channelSummaries() {
63
+ const auto = autoCheckinState()
64
+ const caps = creditsCapabilities()
65
+ const checkinByProvider = new Map((auto.channels ?? []).map(c => [c.provider, c]))
66
+ return CHANNELS.map(ch => {
67
+ const st = channelState(ch.meta.id)
68
+ const checkin = checkinByProvider.get(ch.meta.id) ?? null
69
+ return {
70
+ id: ch.meta.id,
71
+ label: ch.meta.label,
72
+ enabled: st.enabled === true,
73
+ canCheckin: caps[ch.meta.id]?.checkin === true,
74
+ models: st.models ?? [],
75
+ modelRates: st.modelRates ?? {},
76
+ accounts: (st.accounts ?? []).map(a => ({
77
+ id: a.id,
78
+ label: a.label,
79
+ enabled: a.enabled !== false,
80
+ lastError: a.lastError ?? '',
81
+ expiresAt: a.expiresAt ?? 0,
82
+ refreshable: a.refreshable === true,
83
+ autoCheckin: a.autoCheckin === true,
84
+ })),
85
+ checkin: checkin ? { lastResult: checkin.text ?? '', credit: checkin.credit ?? null, claimed: checkin.claimed ?? 0, at: checkin.at ?? 0, state: checkin.state ?? '', note: checkin.note ?? '', accounts: checkin.accounts ?? [] } : null,
86
+ }
87
+ })
88
+ }
89
+
90
+ /** 网关与调度设置摘要(apiKey 永不出境;含免密渠道 kilo/opencode 摘要)。 */
91
+ function settingsSummary() {
92
+ const s = loadSettings()
93
+ const gw = s.gateway ?? {}
94
+ return {
95
+ gateway: {
96
+ enabled: gw.enabled !== false,
97
+ host: gw.host ?? '127.0.0.1',
98
+ port: gw.port ?? 18682,
99
+ headerTimeoutMs: gw.headerTimeoutMs ?? 60000,
100
+ idleTimeoutMs: gw.idleTimeoutMs ?? 120000,
101
+ turnTimeoutMs: gw.turnTimeoutMs ?? 300000,
102
+ candidateTimeoutMs: gw.candidateTimeoutMs ?? 45000,
103
+ hedgeDelayMs: gw.hedgeDelayMs ?? 6000,
104
+ hedgeMaxParallel: gw.hedgeMaxParallel ?? 2,
105
+ },
106
+ probe: {
107
+ enabled: s.probe?.enabled !== false,
108
+ intervalMinutes: s.probe?.intervalMinutes ?? 15,
109
+ },
110
+ freeModels: {
111
+ // 小时扫描把各转发渠道的勾选清单整体替换为上游可用清单并刷新倍率
112
+ autoSelectAvailable: s.freeModels?.autoSelectAvailable !== false,
113
+ },
114
+ checkin: {
115
+ enabled: s.checkin?.enabled !== false,
116
+ delayMs: s.checkin?.delayMs ?? 15000,
117
+ },
118
+ // 免密渠道(baseUrl 展示 + 启停 + 槽位摘要,密钥永不出境)
119
+ kilo: {
120
+ enabled: s.kilo?.enabled !== false,
121
+ baseUrl: s.kilo?.baseUrl ?? '',
122
+ },
123
+ opencode: {
124
+ enabled: s.opencode?.enabled !== false,
125
+ baseUrl: s.opencode?.baseUrl ?? '',
126
+ slots: (Array.isArray(s.opencode?.slots) ? s.opencode.slots : []).map(sl => ({
127
+ label: sl.label ?? '',
128
+ enabled: sl.enabled !== false,
129
+ anonymous: sl.apiKey === 'public',
130
+ })),
131
+ },
132
+ }
133
+ }
134
+
135
+ /** 全量快照(连接建立 / Client resync / 配对首帧 / 老客户端兜底时推)。 */
136
+ export async function buildSnapshot() {
137
+ const [catalog, providersDoc] = await Promise.all([buildCatalog(), Promise.resolve(loadProviders())])
138
+ return {
139
+ generatedAt: Date.now(),
140
+ startedAt: runtime.startedAt,
141
+ stats: { ...runtime.stats },
142
+ recentRequests: runtime.recentRequests.slice(0, SYNC_REQUESTS_LIMIT),
143
+ catalog: catalog.filter(r => !r.offline),
144
+ groups: groupsSummary(),
145
+ channels: channelSummaries(),
146
+ gateway: settingsSummary().gateway,
147
+ settings: settingsSummary(),
148
+ health: health.snapshot(),
149
+ probe: probe.probeSnapshot(),
150
+ probeMeta: probe.probeMeta(),
151
+ freeModels: freeModelsState(),
152
+ providers: sanitizeProviders(providersDoc),
153
+ checkin: autoCheckinState(),
154
+ balances: loadBalances(),
155
+ }
156
+ }
157
+
158
+ /**
159
+ * 单分区增量。分区名即小程序端 `wx.setStorageSync('omr_cache_' + name)` 的缓存键
160
+ * (requests 对应全量快照里的 recentRequests),未知分区返回 null。
161
+ */
162
+ export async function buildSection(section) {
163
+ switch (section) {
164
+ case 'stats':
165
+ return { ...runtime.stats }
166
+ case 'requests':
167
+ return runtime.recentRequests.slice(0, SYNC_REQUESTS_LIMIT)
168
+ case 'catalog':
169
+ return (await buildCatalog()).filter(r => !r.offline)
170
+ case 'groups':
171
+ return groupsSummary()
172
+ case 'channels':
173
+ return channelSummaries()
174
+ case 'gateway':
175
+ return settingsSummary().gateway
176
+ case 'settings':
177
+ return settingsSummary()
178
+ case 'health':
179
+ return health.snapshot()
180
+ case 'probe':
181
+ return probe.probeSnapshot()
182
+ case 'probeMeta':
183
+ return probe.probeMeta()
184
+ case 'freeModels':
185
+ return freeModelsState()
186
+ case 'providers':
187
+ return sanitizeProviders(loadProviders())
188
+ case 'checkin':
189
+ return autoCheckinState()
190
+ case 'balances':
191
+ return loadBalances()
192
+ case 'startedAt':
193
+ return runtime.startedAt
194
+ default:
195
+ return null
196
+ }
197
+ }
198
+
199
+ /**
200
+ * 攒批增量:只构建本次变化的分区。未知分区名(buildSection 返回 null)跳过,
201
+ * 跳过的分区在增量帧里不出现 → 小程序保持原缓存,绝不被清空。
202
+ */
203
+ export async function buildSections(names = []) {
204
+ const out = {}
205
+ for (const name of names) {
206
+ const payload = await buildSection(name)
207
+ if (payload === null || payload === undefined) continue
208
+ out[name] = payload
209
+ }
210
+ return out
211
+ }