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,111 +1,111 @@
1
- /**
2
- * Kilo 通道(免凭据免费池的 OpenAI 兼容接入)。
3
- *
4
- * Kilo AI (https://kilo.ai) 的 OpenAI 兼容网关有一个**完全免凭据**的免费池:
5
- * 无 key、无登录、无指纹门禁。GET /models 拿清单(只保留 isFree: true),
6
- * POST /chat/completions 直接对话(SSE 流式)。
7
- * 免费池的请求可能被上游记录,仅适合非敏感用途。
8
- */
9
-
10
- import { httpFetch, readSseData, readBodyText, classifyFailure, classifyThrownError, retryAfterMs } from '../util.js'
11
-
12
- const LISTING_TIMEOUT_MS = 15000
13
- const TURN_TIMEOUT_MS = 300000
14
-
15
- /**
16
- * GET {base}/models —— 拉实时模型清单,只保留免费条目(`isFree: true`),
17
- * 返回 [{ id, name, contextWindow, vision, reasoning, free: true }]。
18
- *
19
- * 清单只认上游实时响应(无内置兜底表):拉取失败直接抛错,不做任何回落。
20
- * 免费条目 pricing 全 0,故不再产出价格字段。
21
- */
22
- export async function fetchKiloModels(settings, { signal } = {}) {
23
- const base = String(settings.baseUrl ?? 'https://api.kilo.ai/api/gateway').replace(/\/+$/, '')
24
- const response = await httpFetch(`${base}/models`, {
25
- headers: { accept: 'application/json', 'user-agent': settings.userAgent ?? 'OneModelRouter/1.0' },
26
- redirect: 'error',
27
- signal,
28
- }, LISTING_TIMEOUT_MS)
29
- const text = await readBodyText(response, LISTING_TIMEOUT_MS)
30
- if (!response.ok) throw classifyFailure(response.status, text)
31
- let payload
32
- try { payload = JSON.parse(text) } catch { throw classifyFailure(response.status, text) }
33
- const rows = Array.isArray(payload) ? payload : Array.isArray(payload.data) ? payload.data : Array.isArray(payload.models) ? payload.models : []
34
- const models = []
35
- for (const row of rows) {
36
- if (row === null || typeof row !== 'object') continue
37
- if (row.isFree !== true) continue
38
- const id = typeof row.id === 'string' ? row.id.trim() : ''
39
- if (id === '') continue
40
- models.push({
41
- id,
42
- name: typeof row.name === 'string' && row.name !== '' ? row.name : id,
43
- contextWindow: positiveNumber(
44
- row.context_length ?? row.top_provider?.context_length ?? row.contextLength ?? row.context_window
45
- ),
46
- vision: typeof row.vision === 'boolean' ? row.vision : null,
47
- reasoning: typeof row.reasoning === 'boolean' ? row.reasoning : null,
48
- free: true,
49
- })
50
- }
51
- return models
52
- }
53
-
54
- function positiveNumber(value) {
55
- return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.trunc(value) : null
56
- }
57
-
58
- /**
59
- * 获取可用模型(fetchKiloModels 的别名)。
60
- */
61
- export async function fetchModels(settings, options) {
62
- return fetchKiloModels(settings, options)
63
- }
64
-
65
- /**
66
- * POST {base}/chat/completions —— 免 key 转发一轮对话。
67
- * body 为 OpenAI chat 格式;stream=true 时 onData 逐帧收到 SSE `data:` 文本,
68
- * 返回 { status, stream: true };否则返回 { status, json }。
69
- */
70
- export async function kiloCompletion(settings, body, { signal, onData, headerTimeoutMs = 60000, idleTimeoutMs = 120000 } = {}) {
71
- const base = String(settings.baseUrl ?? 'https://api.kilo.ai/api/gateway').replace(/\/+$/, '')
72
- const stream = body.stream === true
73
- const response = await httpFetch(`${base}/chat/completions`, {
74
- method: 'POST',
75
- headers: {
76
- 'content-type': 'application/json',
77
- accept: stream ? 'text/event-stream' : 'application/json',
78
- 'user-agent': settings.userAgent ?? 'OneModelRouter/1.0',
79
- },
80
- body: JSON.stringify(body),
81
- redirect: 'error',
82
- signal,
83
- }, headerTimeoutMs)
84
-
85
- if (!response.ok) {
86
- const text = await readBodyText(response, 15000).catch(() => '')
87
- const failure = classifyFailure(response.status, text)
88
- const ra = retryAfterMs(response.headers)
89
- if (ra) failure.cooldownMs = ra
90
- throw failure
91
- }
92
- if (!response.body) throw { kind: 'server', retryable: true, cooldownMs: 30000, message: '上游返回空响应体' }
93
-
94
- if (!stream) {
95
- const text = await readBodyText(response, TURN_TIMEOUT_MS, signal)
96
- let payload
97
- try { payload = JSON.parse(text) } catch {
98
- throw { kind: 'server', retryable: true, cooldownMs: 30000, message: `非 JSON 响应: ${text.slice(0, 200)}` }
99
- }
100
- if (payload.error) throw classifyFailure(response.status, text)
101
- return { status: response.status, json: payload }
102
- }
103
-
104
- // 上游中途断流/空闲超时:归类成可冷却的失败,避免裸 TypeError 直接冒到路由层
105
- try {
106
- await readSseData(response.body, onData, signal, { idleTimeoutMs })
107
- } catch (error) {
108
- throw classifyThrownError(error)
109
- }
110
- return { status: response.status, stream: true }
111
- }
1
+ /**
2
+ * Kilo 通道(免凭据免费池的 OpenAI 兼容接入)。
3
+ *
4
+ * Kilo AI (https://kilo.ai) 的 OpenAI 兼容网关有一个**完全免凭据**的免费池:
5
+ * 无 key、无登录、无指纹门禁。GET /models 拿清单(只保留 isFree: true),
6
+ * POST /chat/completions 直接对话(SSE 流式)。
7
+ * 免费池的请求可能被上游记录,仅适合非敏感用途。
8
+ */
9
+
10
+ import { httpFetch, readSseData, readBodyText, classifyFailure, classifyThrownError, retryAfterMs } from '../util.js'
11
+
12
+ const LISTING_TIMEOUT_MS = 15000
13
+ const TURN_TIMEOUT_MS = 300000
14
+
15
+ /**
16
+ * GET {base}/models —— 拉实时模型清单,只保留免费条目(`isFree: true`),
17
+ * 返回 [{ id, name, contextWindow, vision, reasoning, free: true }]。
18
+ *
19
+ * 清单只认上游实时响应(无内置兜底表):拉取失败直接抛错,不做任何回落。
20
+ * 免费条目 pricing 全 0,故不再产出价格字段。
21
+ */
22
+ export async function fetchKiloModels(settings, { signal } = {}) {
23
+ const base = String(settings.baseUrl ?? 'https://api.kilo.ai/api/gateway').replace(/\/+$/, '')
24
+ const response = await httpFetch(`${base}/models`, {
25
+ headers: { accept: 'application/json', 'user-agent': settings.userAgent ?? 'OneModelRouter/1.0' },
26
+ redirect: 'error',
27
+ signal,
28
+ }, LISTING_TIMEOUT_MS)
29
+ const text = await readBodyText(response, LISTING_TIMEOUT_MS)
30
+ if (!response.ok) throw classifyFailure(response.status, text)
31
+ let payload
32
+ try { payload = JSON.parse(text) } catch { throw classifyFailure(response.status, text) }
33
+ const rows = Array.isArray(payload) ? payload : Array.isArray(payload.data) ? payload.data : Array.isArray(payload.models) ? payload.models : []
34
+ const models = []
35
+ for (const row of rows) {
36
+ if (row === null || typeof row !== 'object') continue
37
+ if (row.isFree !== true) continue
38
+ const id = typeof row.id === 'string' ? row.id.trim() : ''
39
+ if (id === '') continue
40
+ models.push({
41
+ id,
42
+ name: typeof row.name === 'string' && row.name !== '' ? row.name : id,
43
+ contextWindow: positiveNumber(
44
+ row.context_length ?? row.top_provider?.context_length ?? row.contextLength ?? row.context_window
45
+ ),
46
+ vision: typeof row.vision === 'boolean' ? row.vision : null,
47
+ reasoning: typeof row.reasoning === 'boolean' ? row.reasoning : null,
48
+ free: true,
49
+ })
50
+ }
51
+ return models
52
+ }
53
+
54
+ function positiveNumber(value) {
55
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.trunc(value) : null
56
+ }
57
+
58
+ /**
59
+ * 获取可用模型(fetchKiloModels 的别名)。
60
+ */
61
+ export async function fetchModels(settings, options) {
62
+ return fetchKiloModels(settings, options)
63
+ }
64
+
65
+ /**
66
+ * POST {base}/chat/completions —— 免 key 转发一轮对话。
67
+ * body 为 OpenAI chat 格式;stream=true 时 onData 逐帧收到 SSE `data:` 文本,
68
+ * 返回 { status, stream: true };否则返回 { status, json }。
69
+ */
70
+ export async function kiloCompletion(settings, body, { signal, onData, headerTimeoutMs = 60000, idleTimeoutMs = 120000 } = {}) {
71
+ const base = String(settings.baseUrl ?? 'https://api.kilo.ai/api/gateway').replace(/\/+$/, '')
72
+ const stream = body.stream === true
73
+ const response = await httpFetch(`${base}/chat/completions`, {
74
+ method: 'POST',
75
+ headers: {
76
+ 'content-type': 'application/json',
77
+ accept: stream ? 'text/event-stream' : 'application/json',
78
+ 'user-agent': settings.userAgent ?? 'OneModelRouter/1.0',
79
+ },
80
+ body: JSON.stringify(body),
81
+ redirect: 'error',
82
+ signal,
83
+ }, headerTimeoutMs)
84
+
85
+ if (!response.ok) {
86
+ const text = await readBodyText(response, 15000).catch(() => '')
87
+ const failure = classifyFailure(response.status, text)
88
+ const ra = retryAfterMs(response.headers)
89
+ if (ra) failure.cooldownMs = ra
90
+ throw failure
91
+ }
92
+ if (!response.body) throw { kind: 'server', retryable: true, cooldownMs: 30000, message: '上游返回空响应体' }
93
+
94
+ if (!stream) {
95
+ const text = await readBodyText(response, TURN_TIMEOUT_MS, signal)
96
+ let payload
97
+ try { payload = JSON.parse(text) } catch {
98
+ throw { kind: 'server', retryable: true, cooldownMs: 30000, message: `非 JSON 响应: ${text.slice(0, 200)}` }
99
+ }
100
+ if (payload.error) throw classifyFailure(response.status, text)
101
+ return { status: response.status, json: payload }
102
+ }
103
+
104
+ // 上游中途断流/空闲超时:归类成可冷却的失败,避免裸 TypeError 直接冒到路由层
105
+ try {
106
+ await readSseData(response.body, onData, signal, { idleTimeoutMs })
107
+ } catch (error) {
108
+ throw classifyThrownError(error)
109
+ }
110
+ return { status: response.status, stream: true }
111
+ }
@@ -1,69 +1,69 @@
1
- /**
2
- * 各拉取源「最近一次成功清单」快照:网络不通或上游异常时的兜底来源。
3
- *
4
- * 覆盖免密渠道(kilo / opencode)、转发渠道与自定义提供商:
5
- * - 拉取成功 → 原始模型对象整表落盘(每源一个文件,数据目录内);
6
- * - 拉取失败 → 回放上次快照,调用方按 stale 标记提示「非实时」。
7
- * 快照存的是 fetchModels 的原始返回对象(id/name/contextWindow/rate/free…),
8
- * 回放后仍走调用方既有的过滤/选用逻辑,不另设口径。
9
- * 成功但返回空清单不落盘(空多半是权限/过滤问题,保留上一份更有价值)。
10
- */
11
-
12
- import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
13
- import { join } from 'node:path'
14
- import { DATA_DIR } from '../store.js'
15
-
16
- const KEY_RE = /[^A-Za-z0-9._-]/g
17
- const SNAPSHOT_MAX_AGE_MS = 30 * 24 * 60 * 60_000
18
-
19
- function snapshotFile(key) {
20
- return join(DATA_DIR, `last-good-${String(key).replace(KEY_RE, '-')}.json`)
21
- }
22
-
23
- /** 成功清单落盘;写失败静默(只读目录等),下次兜底退回「无快照」。 */
24
- export function saveLastGood(key, models) {
25
- const rows = (Array.isArray(models) ? models : [])
26
- .filter(m => m && typeof m === 'object' && typeof m.id === 'string' && m.id !== '')
27
- if (rows.length === 0) return
28
- try {
29
- mkdirSync(DATA_DIR, { recursive: true })
30
- const file = snapshotFile(key)
31
- const temp = `${file}.tmp`
32
- writeFileSync(temp, JSON.stringify({ at: Date.now(), models: rows }), 'utf8')
33
- renameSync(temp, file)
34
- } catch { /* 纯内存运行环境:忽略 */ }
35
- }
36
-
37
- /** 读取快照;缺失、损坏或超过 30 天视为无快照。 */
38
- export function loadLastGood(key) {
39
- try {
40
- const doc = JSON.parse(readFileSync(snapshotFile(key), 'utf8'))
41
- const at = Number(doc?.at) || 0
42
- if (at === 0 || Date.now() - at > SNAPSHOT_MAX_AGE_MS) return null
43
- const models = Array.isArray(doc?.models)
44
- ? doc.models.filter(m => m && typeof m === 'object' && typeof m.id === 'string' && m.id !== '')
45
- : []
46
- return models.length > 0 ? { at, models } : null
47
- } catch {
48
- return null
49
- }
50
- }
51
-
52
- /**
53
- * 带兜底的拉取包装:成功即落盘并原样返回;失败回放上次快照。
54
- * 返回 { models, stale, fetchedAt, error }:
55
- * - stale=false:本轮实时拉到;
56
- * - stale=true:实时失败(error 为失败原因)、models 为上次快照;
57
- * - 失败且无快照:models=null 并抛出原错误(调用方保留原报错路径)。
58
- */
59
- export async function withLastGoodFallback(key, fetchFn) {
60
- try {
61
- const models = await fetchFn()
62
- saveLastGood(key, models)
63
- return { models, stale: false, fetchedAt: Date.now() }
64
- } catch (error) {
65
- const snapshot = loadLastGood(key)
66
- if (snapshot) return { models: snapshot.models, stale: true, fetchedAt: snapshot.at, error: String(error?.message ?? error) }
67
- throw error
68
- }
69
- }
1
+ /**
2
+ * 各拉取源「最近一次成功清单」快照:网络不通或上游异常时的兜底来源。
3
+ *
4
+ * 覆盖免密渠道(kilo / opencode)、转发渠道与自定义提供商:
5
+ * - 拉取成功 → 原始模型对象整表落盘(每源一个文件,数据目录内);
6
+ * - 拉取失败 → 回放上次快照,调用方按 stale 标记提示「非实时」。
7
+ * 快照存的是 fetchModels 的原始返回对象(id/name/contextWindow/rate/free…),
8
+ * 回放后仍走调用方既有的过滤/选用逻辑,不另设口径。
9
+ * 成功但返回空清单不落盘(空多半是权限/过滤问题,保留上一份更有价值)。
10
+ */
11
+
12
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
13
+ import { join } from 'node:path'
14
+ import { DATA_DIR } from '../store.js'
15
+
16
+ const KEY_RE = /[^A-Za-z0-9._-]/g
17
+ const SNAPSHOT_MAX_AGE_MS = 30 * 24 * 60 * 60_000
18
+
19
+ function snapshotFile(key) {
20
+ return join(DATA_DIR, `last-good-${String(key).replace(KEY_RE, '-')}.json`)
21
+ }
22
+
23
+ /** 成功清单落盘;写失败静默(只读目录等),下次兜底退回「无快照」。 */
24
+ export function saveLastGood(key, models) {
25
+ const rows = (Array.isArray(models) ? models : [])
26
+ .filter(m => m && typeof m === 'object' && typeof m.id === 'string' && m.id !== '')
27
+ if (rows.length === 0) return
28
+ try {
29
+ mkdirSync(DATA_DIR, { recursive: true })
30
+ const file = snapshotFile(key)
31
+ const temp = `${file}.tmp`
32
+ writeFileSync(temp, JSON.stringify({ at: Date.now(), models: rows }), 'utf8')
33
+ renameSync(temp, file)
34
+ } catch { /* 纯内存运行环境:忽略 */ }
35
+ }
36
+
37
+ /** 读取快照;缺失、损坏或超过 30 天视为无快照。 */
38
+ export function loadLastGood(key) {
39
+ try {
40
+ const doc = JSON.parse(readFileSync(snapshotFile(key), 'utf8'))
41
+ const at = Number(doc?.at) || 0
42
+ if (at === 0 || Date.now() - at > SNAPSHOT_MAX_AGE_MS) return null
43
+ const models = Array.isArray(doc?.models)
44
+ ? doc.models.filter(m => m && typeof m === 'object' && typeof m.id === 'string' && m.id !== '')
45
+ : []
46
+ return models.length > 0 ? { at, models } : null
47
+ } catch {
48
+ return null
49
+ }
50
+ }
51
+
52
+ /**
53
+ * 带兜底的拉取包装:成功即落盘并原样返回;失败回放上次快照。
54
+ * 返回 { models, stale, fetchedAt, error }:
55
+ * - stale=false:本轮实时拉到;
56
+ * - stale=true:实时失败(error 为失败原因)、models 为上次快照;
57
+ * - 失败且无快照:models=null 并抛出原错误(调用方保留原报错路径)。
58
+ */
59
+ export async function withLastGoodFallback(key, fetchFn) {
60
+ try {
61
+ const models = await fetchFn()
62
+ saveLastGood(key, models)
63
+ return { models, stale: false, fetchedAt: Date.now() }
64
+ } catch (error) {
65
+ const snapshot = loadLastGood(key)
66
+ if (snapshot) return { models: snapshot.models, stale: true, fetchedAt: snapshot.at, error: String(error?.message ?? error) }
67
+ throw error
68
+ }
69
+ }