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,176 +1,176 @@
1
- /**
2
- * 云总线传输层(桌面端 ↔ WorkBuddy 云服务中继)。
3
- *
4
- * WorkBuddy 云服务是 DB/Auth 数据面,无自定义 WebSocket 服务,因此实时通道为:
5
- * 下行:桌面端把快照/增量写入云 DB(转发缓存,TTL 10 分钟),小程序轮询拉取;
6
- * 上行:小程序把指令写入云 DB,桌面端每 3s 轮询取走执行后删除。
7
- * 持久化的只有设备注册表(omr_devices,属用户登录信息);其余秒级停留。
8
- *
9
- * 鉴权两层:
10
- * - 账号层:设备专用 WorkBuddy 账号(settings.sync.email/password,密封落盘);
11
- * 认证数据面要求 Origin 头 —— 小程序类应用的白名单是 https://servicewechat.com
12
- * (微信小程序 wx.request 的自动来源),桌面端按同值补发。
13
- * - 设备层:settings.sync.apiKey(omr_relay RPC 内 sha256 校验)。
14
- */
15
-
16
- const POLL_INTERVAL_MS = 3_000
17
- const POLL_ERROR_BACKOFF_MS = 15_000
18
- const SIGNIN_ORIGIN = 'https://servicewechat.com'
19
-
20
- export function createCloudRelay({ config, onCommands, onStatus, log = () => {} }) {
21
- const state = {
22
- started: false,
23
- cloud: null,
24
- status: 'offline', // offline | connecting | online
25
- failures: 0,
26
- pollTimer: null,
27
- polling: false,
28
- lastConnectedAt: 0,
29
- lastError: '',
30
- }
31
-
32
- const setStatus = s => {
33
- if (state.status !== s) {
34
- state.status = s
35
- try { onStatus?.(s) } catch { /* 回调异常不影响 */ }
36
- }
37
- }
38
-
39
- /** 仅对中继数据面的请求补 Origin(不影响进程内其他出网请求)。 */
40
- function patchFetchForOrigin() {
41
- const realFetch = globalThis.fetch
42
- if (realFetch.__omrOriginPatched) return
43
- const endpointHost = new URL(config.endpoint).host
44
- const patched = (url, init = {}) => {
45
- try {
46
- if (new URL(String(url)).host === endpointHost) {
47
- const headers = new Headers(init.headers ?? {})
48
- if (!headers.has('origin')) headers.set('origin', SIGNIN_ORIGIN)
49
- return realFetch(url, { ...init, headers })
50
- }
51
- } catch { /* 非 http URL 交给原 fetch */ }
52
- return realFetch(url, init)
53
- }
54
- patched.__omrOriginPatched = true
55
- globalThis.fetch = patched
56
- }
57
-
58
- const rpc = async (action, extra = {}) => {
59
- const { data, error } = await state.cloud.database.rpc('omr_relay', {
60
- p_args: { action, deviceId: config.deviceId, deviceKey: config.deviceKey, ...extra },
61
- })
62
- if (error) throw normalizeError(error)
63
- return data
64
- }
65
-
66
- function normalizeError(error) {
67
- const err = error ?? {}
68
- const e = new Error(String(err.message ?? err.description ?? JSON.stringify(err)))
69
- e.code = err.code ?? err.kind ?? ''
70
- return e
71
- }
72
-
73
- async function signIn() {
74
- const { data, error } = await state.cloud.auth.signInWithPassword({ email: config.email, password: config.password })
75
- if (error) throw normalizeError(error)
76
- return data
77
- }
78
-
79
- const isAuthError = e => /invalid_grant|unauthenticated|session/i.test(e.code + ' ' + e.message)
80
-
81
- async function runPoll() {
82
- if (!state.started || state.polling) return
83
- state.polling = true
84
- try {
85
- const result = await rpc('poll')
86
- state.failures = 0
87
- state.lastError = ''
88
- if (state.status !== 'online') {
89
- state.lastConnectedAt = Date.now()
90
- setStatus('online')
91
- }
92
- const commands = Array.isArray(result?.commands) ? result.commands : []
93
- if (commands.length > 0 && typeof onCommands === 'function') {
94
- await onCommands(commands)
95
- // 执行完毕统一删除(转发缓存语义:取走即删)
96
- await rpc('ackCommands', { ids: commands.map(c => c.id) }).catch(e => log(`指令回执失败: ${e.message}`))
97
- }
98
- schedulePoll(POLL_INTERVAL_MS)
99
- } catch (error) {
100
- state.failures += 1
101
- state.lastError = error.message
102
- if (isAuthError(error)) {
103
- setStatus('offline')
104
- log(`会话失效,重新登录…(${error.message})`)
105
- try {
106
- await signIn()
107
- schedulePoll(1000)
108
- } catch (e2) {
109
- state.lastError = e2.message
110
- log(`重新登录失败: ${e2.message}`)
111
- schedulePoll(POLL_ERROR_BACKOFF_MS)
112
- }
113
- } else {
114
- if (state.status !== 'offline') setStatus('offline')
115
- schedulePoll(state.failures > 3 ? POLL_ERROR_BACKOFF_MS : POLL_INTERVAL_MS)
116
- }
117
- } finally {
118
- state.polling = false
119
- }
120
- }
121
-
122
- function schedulePoll(delay) {
123
- if (!state.started) return
124
- if (state.pollTimer) clearTimeout(state.pollTimer)
125
- state.pollTimer = setTimeout(() => { state.pollTimer = null; void runPoll() }, delay)
126
- state.pollTimer.unref?.()
127
- }
128
-
129
- return {
130
- async start() {
131
- if (state.started) return
132
- state.started = true
133
- setStatus('connecting')
134
- try {
135
- patchFetchForOrigin()
136
- const lib = await import('@tencent-ai/workbuddy-cloud-sdk')
137
- state.cloud = lib.createWorkBuddyCloud({ endpoint: config.endpoint, publishableKey: config.publishableKey })
138
- await signIn()
139
- await rpc('register', { label: config.deviceLabel ?? 'OneModelRouter' })
140
- log(`设备已注册到中继(${config.deviceId})`)
141
- setStatus('online')
142
- state.lastConnectedAt = Date.now()
143
- schedulePoll(POLL_INTERVAL_MS)
144
- } catch (error) {
145
- state.lastError = error.message
146
- setStatus('offline')
147
- log(`同步启动失败: ${error.message},将持续重试`)
148
- // 启动失败不放弃:退避重试(账号/网络恢复后自动接上)
149
- if (state.started) {
150
- state.pollTimer = setTimeout(() => {
151
- state.pollTimer = null
152
- state.started = false
153
- void this.start()
154
- }, POLL_ERROR_BACKOFF_MS)
155
- state.pollTimer.unref?.()
156
- }
157
- }
158
- },
159
- stop() {
160
- state.started = false
161
- if (state.pollTimer) { clearTimeout(state.pollTimer); state.pollTimer = null }
162
- setStatus('offline')
163
- },
164
- /** 推全量快照(转发缓存:覆盖设备唯一快照行,10 分钟 TTL)。 */
165
- async pushSnapshot(payload) {
166
- return rpc('pushSnapshot', { payload })
167
- },
168
- /** 推送一次性配对码(10 分钟有效,绑定成功即删)。 */
169
- async pushPairCode(code) {
170
- return rpc('pushPairCode', { code })
171
- },
172
- get status() { return state.status },
173
- get lastConnectedAt() { return state.lastConnectedAt },
174
- get lastError() { return state.lastError },
175
- }
176
- }
1
+ /**
2
+ * 云总线传输层(桌面端 ↔ WorkBuddy 云服务中继)。
3
+ *
4
+ * WorkBuddy 云服务是 DB/Auth 数据面,无自定义 WebSocket 服务,因此实时通道为:
5
+ * 下行:桌面端把快照/增量写入云 DB(转发缓存,TTL 10 分钟),小程序轮询拉取;
6
+ * 上行:小程序把指令写入云 DB,桌面端每 3s 轮询取走执行后删除。
7
+ * 持久化的只有设备注册表(omr_devices,属用户登录信息);其余秒级停留。
8
+ *
9
+ * 鉴权两层:
10
+ * - 账号层:设备专用 WorkBuddy 账号(settings.sync.email/password,密封落盘);
11
+ * 认证数据面要求 Origin 头 —— 小程序类应用的白名单是 https://servicewechat.com
12
+ * (微信小程序 wx.request 的自动来源),桌面端按同值补发。
13
+ * - 设备层:settings.sync.apiKey(omr_relay RPC 内 sha256 校验)。
14
+ */
15
+
16
+ const POLL_INTERVAL_MS = 3_000
17
+ const POLL_ERROR_BACKOFF_MS = 15_000
18
+ const SIGNIN_ORIGIN = 'https://servicewechat.com'
19
+
20
+ export function createCloudRelay({ config, onCommands, onStatus, log = () => {} }) {
21
+ const state = {
22
+ started: false,
23
+ cloud: null,
24
+ status: 'offline', // offline | connecting | online
25
+ failures: 0,
26
+ pollTimer: null,
27
+ polling: false,
28
+ lastConnectedAt: 0,
29
+ lastError: '',
30
+ }
31
+
32
+ const setStatus = s => {
33
+ if (state.status !== s) {
34
+ state.status = s
35
+ try { onStatus?.(s) } catch { /* 回调异常不影响 */ }
36
+ }
37
+ }
38
+
39
+ /** 仅对中继数据面的请求补 Origin(不影响进程内其他出网请求)。 */
40
+ function patchFetchForOrigin() {
41
+ const realFetch = globalThis.fetch
42
+ if (realFetch.__omrOriginPatched) return
43
+ const endpointHost = new URL(config.endpoint).host
44
+ const patched = (url, init = {}) => {
45
+ try {
46
+ if (new URL(String(url)).host === endpointHost) {
47
+ const headers = new Headers(init.headers ?? {})
48
+ if (!headers.has('origin')) headers.set('origin', SIGNIN_ORIGIN)
49
+ return realFetch(url, { ...init, headers })
50
+ }
51
+ } catch { /* 非 http URL 交给原 fetch */ }
52
+ return realFetch(url, init)
53
+ }
54
+ patched.__omrOriginPatched = true
55
+ globalThis.fetch = patched
56
+ }
57
+
58
+ const rpc = async (action, extra = {}) => {
59
+ const { data, error } = await state.cloud.database.rpc('omr_relay', {
60
+ p_args: { action, deviceId: config.deviceId, deviceKey: config.deviceKey, ...extra },
61
+ })
62
+ if (error) throw normalizeError(error)
63
+ return data
64
+ }
65
+
66
+ function normalizeError(error) {
67
+ const err = error ?? {}
68
+ const e = new Error(String(err.message ?? err.description ?? JSON.stringify(err)))
69
+ e.code = err.code ?? err.kind ?? ''
70
+ return e
71
+ }
72
+
73
+ async function signIn() {
74
+ const { data, error } = await state.cloud.auth.signInWithPassword({ email: config.email, password: config.password })
75
+ if (error) throw normalizeError(error)
76
+ return data
77
+ }
78
+
79
+ const isAuthError = e => /invalid_grant|unauthenticated|session/i.test(e.code + ' ' + e.message)
80
+
81
+ async function runPoll() {
82
+ if (!state.started || state.polling) return
83
+ state.polling = true
84
+ try {
85
+ const result = await rpc('poll')
86
+ state.failures = 0
87
+ state.lastError = ''
88
+ if (state.status !== 'online') {
89
+ state.lastConnectedAt = Date.now()
90
+ setStatus('online')
91
+ }
92
+ const commands = Array.isArray(result?.commands) ? result.commands : []
93
+ if (commands.length > 0 && typeof onCommands === 'function') {
94
+ await onCommands(commands)
95
+ // 执行完毕统一删除(转发缓存语义:取走即删)
96
+ await rpc('ackCommands', { ids: commands.map(c => c.id) }).catch(e => log(`指令回执失败: ${e.message}`))
97
+ }
98
+ schedulePoll(POLL_INTERVAL_MS)
99
+ } catch (error) {
100
+ state.failures += 1
101
+ state.lastError = error.message
102
+ if (isAuthError(error)) {
103
+ setStatus('offline')
104
+ log(`会话失效,重新登录…(${error.message})`)
105
+ try {
106
+ await signIn()
107
+ schedulePoll(1000)
108
+ } catch (e2) {
109
+ state.lastError = e2.message
110
+ log(`重新登录失败: ${e2.message}`)
111
+ schedulePoll(POLL_ERROR_BACKOFF_MS)
112
+ }
113
+ } else {
114
+ if (state.status !== 'offline') setStatus('offline')
115
+ schedulePoll(state.failures > 3 ? POLL_ERROR_BACKOFF_MS : POLL_INTERVAL_MS)
116
+ }
117
+ } finally {
118
+ state.polling = false
119
+ }
120
+ }
121
+
122
+ function schedulePoll(delay) {
123
+ if (!state.started) return
124
+ if (state.pollTimer) clearTimeout(state.pollTimer)
125
+ state.pollTimer = setTimeout(() => { state.pollTimer = null; void runPoll() }, delay)
126
+ state.pollTimer.unref?.()
127
+ }
128
+
129
+ return {
130
+ async start() {
131
+ if (state.started) return
132
+ state.started = true
133
+ setStatus('connecting')
134
+ try {
135
+ patchFetchForOrigin()
136
+ const lib = await import('@tencent-ai/workbuddy-cloud-sdk')
137
+ state.cloud = lib.createWorkBuddyCloud({ endpoint: config.endpoint, publishableKey: config.publishableKey })
138
+ await signIn()
139
+ await rpc('register', { label: config.deviceLabel ?? 'OneModelRouter' })
140
+ log(`设备已注册到中继(${config.deviceId})`)
141
+ setStatus('online')
142
+ state.lastConnectedAt = Date.now()
143
+ schedulePoll(POLL_INTERVAL_MS)
144
+ } catch (error) {
145
+ state.lastError = error.message
146
+ setStatus('offline')
147
+ log(`同步启动失败: ${error.message},将持续重试`)
148
+ // 启动失败不放弃:退避重试(账号/网络恢复后自动接上)
149
+ if (state.started) {
150
+ state.pollTimer = setTimeout(() => {
151
+ state.pollTimer = null
152
+ state.started = false
153
+ void this.start()
154
+ }, POLL_ERROR_BACKOFF_MS)
155
+ state.pollTimer.unref?.()
156
+ }
157
+ }
158
+ },
159
+ stop() {
160
+ state.started = false
161
+ if (state.pollTimer) { clearTimeout(state.pollTimer); state.pollTimer = null }
162
+ setStatus('offline')
163
+ },
164
+ /** 推全量快照(转发缓存:覆盖设备唯一快照行,10 分钟 TTL)。 */
165
+ async pushSnapshot(payload) {
166
+ return rpc('pushSnapshot', { payload })
167
+ },
168
+ /** 推送一次性配对码(10 分钟有效,绑定成功即删)。 */
169
+ async pushPairCode(code) {
170
+ return rpc('pushPairCode', { code })
171
+ },
172
+ get status() { return state.status },
173
+ get lastConnectedAt() { return state.lastConnectedAt },
174
+ get lastError() { return state.lastError },
175
+ }
176
+ }