one-model-router 0.0.10 → 0.0.11
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.
- package/LICENSE +21 -21
- package/package.json +1 -1
- package/src/admin.js +941 -941
- package/src/balances.js +61 -61
- package/src/boot.js +117 -117
- package/src/channels/buddy.js +2031 -2031
- package/src/channels/cline.js +753 -753
- package/src/channels/codearts.js +2051 -2051
- package/src/channels/comate.js +830 -830
- package/src/channels/doubaowork.js +1784 -1784
- package/src/channels/freebuff.js +1316 -1316
- package/src/channels/gemini.js +1721 -1721
- package/src/channels/index.js +270 -270
- package/src/channels/joycode.js +936 -936
- package/src/channels/kimi.js +572 -572
- package/src/channels/lobsterai.js +1226 -1226
- package/src/channels/loomy.js +1032 -1032
- package/src/channels/miaoshou.js +1396 -1396
- package/src/channels/mimo.js +474 -474
- package/src/channels/minimax.js +1451 -1451
- package/src/channels/qoder-wasm.js +434 -434
- package/src/channels/qoder.js +1619 -1619
- package/src/channels/raccoon.js +1842 -1842
- package/src/channels/tabbit.js +1457 -1457
- package/src/channels/trae.js +1405 -1405
- package/src/channels/zcode-global.js +35 -35
- package/src/channels/zcode.js +2085 -2085
- package/src/checkin.js +496 -496
- package/src/credits.js +464 -464
- package/src/crypto.js +132 -132
- package/src/free-models.js +683 -683
- package/src/gateway.js +596 -578
- package/src/health.js +86 -86
- package/src/model-key.js +35 -35
- package/src/paths.js +96 -96
- package/src/probe.js +318 -318
- package/src/providers/builtin.js +626 -626
- package/src/providers/index.js +483 -457
- package/src/providers/kilo.js +111 -111
- package/src/providers/last-good.js +69 -69
- package/src/providers/modelsdev.js +242 -242
- package/src/providers/openai-compat.js +312 -312
- package/src/providers/opencode/bun-fetch.js +226 -226
- package/src/providers/opencode/lane.js +186 -186
- package/src/providers/opencode/o2a/LICENSE.dsh-our-free-model +21 -21
- package/src/providers/opencode/o2a/asset-source.js +43 -43
- package/src/providers/opencode/o2a/assets/opencode_prompt_v2.txt +96 -96
- package/src/providers/opencode/o2a/assets/opencode_tools.json +327 -327
- package/src/providers/opencode/o2a/entry.js +17 -17
- package/src/providers/opencode/o2a/gateway-body.js +42 -42
- package/src/providers/opencode/o2a/headers.js +133 -133
- package/src/providers/opencode/o2a/identity.js +148 -148
- package/src/providers/opencode/o2a/official-prompt.js +191 -191
- package/src/providers/opencode/o2a/official-tools.js +110 -110
- package/src/providers/opencode/o2a/tool-renames.js +217 -217
- package/src/providers/opencode/o2a/toolshape.js +418 -418
- package/src/providers/opencode/o2a/version.js +37 -37
- package/src/providers/opencode/upstream.js +403 -403
- package/src/providers/opencode/wire.js +496 -496
- package/src/providers/opencode.js +652 -652
- package/src/router.js +629 -629
- package/src/server.js +33 -33
- package/src/store.js +467 -467
- package/src/sync/index.js +1529 -1529
- package/src/sync/login-preload.cjs +52 -52
- package/src/sync/noise.js +141 -141
- package/src/sync/relay-http.js +176 -176
- package/src/sync/remote-host.js +818 -818
- package/src/sync/remote-http.js +112 -112
- package/src/sync/remote-login.js +657 -657
- package/src/sync/remote-pairing.js +204 -204
- package/src/sync/remote-protocol.js +321 -321
- package/src/sync/snapshot.js +211 -211
- package/src/util.js +334 -334
- package/web/app.js +3972 -3972
- package/web/index.html +32 -32
- package/web/style.css +971 -971
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenCode 免密车道的车道级熔断器。
|
|
3
|
-
*
|
|
4
|
-
* ## 为什么需要它
|
|
5
|
-
*
|
|
6
|
-
* 上游按 **出口 IP × project id** 记免费额度,一个烧尽的出口对**任何**模型、
|
|
7
|
-
* **任何** session 都回同一句 `429 FreeUsageLimitError` + 数小时的 `retry-after`
|
|
8
|
-
* (实测:换模型、换会话、换 UA 全都无效)。熔断前
|
|
9
|
-
* 车道只能靠"再发一条真实请求"来发现这堵墙,于是每次撞墙都在继续烧配额;
|
|
10
|
-
* 熔断后这堵墙由真实流量记录一次,窗口内后续请求**本地直接拒绝**。
|
|
11
|
-
*
|
|
12
|
-
* ## 状态
|
|
13
|
-
*
|
|
14
|
-
* - `ok` — 当前出口没有不利判定。
|
|
15
|
-
* - `quota` — 真实 429;`retryAt` 是窗口结束时刻(缺 `retry-after` 时按默认窗口估)。
|
|
16
|
-
* - `locked` — 客户端伪装被拒(`FreeTierError`):等待无用,只有整形层跟上上游、
|
|
17
|
-
* 或换出口才能清;本地拒绝直到状态被清。
|
|
18
|
-
*
|
|
19
|
-
* 出口 IP 变化一律清闸(新出口 = 新配额桶)。出口探测只在被拒后按需做(TTL 内
|
|
20
|
-
* 复用),正常路径不出网探测。
|
|
21
|
-
*
|
|
22
|
-
* 落盘:`{DATA_DIR}/opencode-lane.json`,重启后仍然有效(额度窗口是按小时的)。
|
|
23
|
-
*
|
|
24
|
-
* @module src/providers/opencode/lane
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import { createHash } from 'node:crypto'
|
|
28
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
29
|
-
import { join } from 'node:path'
|
|
30
|
-
import { DATA_DIR, emitRuntimeEvent } from '../../store.js'
|
|
31
|
-
import { httpFetch, readBodyText } from '../../util.js'
|
|
32
|
-
|
|
33
|
-
/** 响应没带可用 `retry-after` 时假设的额度窗口。 */
|
|
34
|
-
const LANE_QUOTA_DEFAULT_MS = 6 * 60 * 60 * 1000
|
|
35
|
-
|
|
36
|
-
/** 被拒后复查出口 IP 的最小间隔(正常流量不再重复出网探测)。 */
|
|
37
|
-
const EGRESS_CHECK_MS = 5 * 60 * 1000
|
|
38
|
-
|
|
39
|
-
/** 公开出口探测源,按序尝试,任一应答即可(只读公网 IP,不带任何凭据)。 */
|
|
40
|
-
const ECHO_SOURCES = [
|
|
41
|
-
{ url: 'https://api.ipify.org?format=json', pick: payload => payload?.ip },
|
|
42
|
-
{ url: 'https://ipinfo.io/json', pick: payload => payload?.ip },
|
|
43
|
-
{ url: 'https://ipapi.co/json/', pick: payload => payload?.ip },
|
|
44
|
-
]
|
|
45
|
-
|
|
46
|
-
const LANE_FILE = join(DATA_DIR, 'opencode-lane.json')
|
|
47
|
-
|
|
48
|
-
let cached = null
|
|
49
|
-
|
|
50
|
-
function read() {
|
|
51
|
-
if (cached !== null) return cached
|
|
52
|
-
try {
|
|
53
|
-
if (!existsSync(LANE_FILE)) { cached = { state: 'ok', at: 0, retryAt: 0, message: '', egress: '' }; return cached }
|
|
54
|
-
const doc = JSON.parse(readFileSync(LANE_FILE, 'utf8'))
|
|
55
|
-
cached = {
|
|
56
|
-
state: doc.state === 'quota' || doc.state === 'locked' ? doc.state : 'ok',
|
|
57
|
-
at: Number(doc.at) || 0,
|
|
58
|
-
retryAt: Number(doc.retryAt) || 0,
|
|
59
|
-
message: typeof doc.message === 'string' ? doc.message.slice(0, 300) : '',
|
|
60
|
-
egress: typeof doc.egress === 'string' ? doc.egress : '',
|
|
61
|
-
}
|
|
62
|
-
} catch {
|
|
63
|
-
cached = { state: 'ok', at: 0, retryAt: 0, message: '', egress: '' }
|
|
64
|
-
}
|
|
65
|
-
return cached
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function write(next) {
|
|
69
|
-
cached = next
|
|
70
|
-
try {
|
|
71
|
-
mkdirSync(DATA_DIR, { recursive: true })
|
|
72
|
-
const temp = `${LANE_FILE}.tmp`
|
|
73
|
-
writeFileSync(temp, JSON.stringify({ version: 1, ...next }, undefined, 2), 'utf8')
|
|
74
|
-
renameSync(temp, LANE_FILE)
|
|
75
|
-
} catch { /* 只读目录 / 磁盘满:状态仍留在内存里 */ }
|
|
76
|
-
emitRuntimeEvent('opencode-lane', next)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** 出口指纹:公网 IP 的 sha256 前 12 位(不落明文 IP)。 */
|
|
80
|
-
function fingerprint(ip) {
|
|
81
|
-
const trimmed = String(ip ?? '').trim()
|
|
82
|
-
if (trimmed === '') return ''
|
|
83
|
-
return createHash('sha256').update(`opencode-egress\0${trimmed}`).digest('hex').slice(0, 12)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** 探测本机出口 IP(失败返回空串——探测失败绝不影响请求本身)。 */
|
|
87
|
-
async function detectEgress() {
|
|
88
|
-
for (const source of ECHO_SOURCES) {
|
|
89
|
-
try {
|
|
90
|
-
const response = await httpFetch(source.url, { headers: { accept: 'application/json' } }, 4000)
|
|
91
|
-
const text = await readBodyText(response, 4000)
|
|
92
|
-
if (!response.ok) continue
|
|
93
|
-
const ip = source.pick(JSON.parse(text))
|
|
94
|
-
if (typeof ip === 'string' && ip.trim() !== '') return ip.trim()
|
|
95
|
-
} catch { /* 换下一个源 */ }
|
|
96
|
-
}
|
|
97
|
-
return ''
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 被拒状态下的出口复查:探测到出口变了就清闸。
|
|
102
|
-
* 只有确实拿到新指纹时才更新 `checkedAt`,避免探测失败导致每次请求都出网。
|
|
103
|
-
*/
|
|
104
|
-
let egressCheckedAt = 0
|
|
105
|
-
async function recheckEgress() {
|
|
106
|
-
const now = Date.now()
|
|
107
|
-
if (egressCheckedAt !== 0 && now - egressCheckedAt < EGRESS_CHECK_MS) return false
|
|
108
|
-
egressCheckedAt = now
|
|
109
|
-
const current = fingerprint(await detectEgress())
|
|
110
|
-
if (current === '') return false
|
|
111
|
-
const lane = read()
|
|
112
|
-
if (lane.egress !== '' && lane.egress !== current) {
|
|
113
|
-
write({ ...lane, state: 'ok', at: now, retryAt: 0, message: '', egress: current })
|
|
114
|
-
return true
|
|
115
|
-
}
|
|
116
|
-
if (lane.egress === '') write({ ...lane, egress: current })
|
|
117
|
-
return false
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** 车道当前状态(面板展示用;含窗口剩余时间与出口复查提示)。 */
|
|
121
|
-
export function laneStatus() {
|
|
122
|
-
const lane = read()
|
|
123
|
-
const now = Date.now()
|
|
124
|
-
if (lane.state === 'quota' && lane.retryAt <= now) return { state: 'ok', at: lane.at, retryAt: 0, message: '', remainingMs: 0 }
|
|
125
|
-
return {
|
|
126
|
-
state: lane.state,
|
|
127
|
-
at: lane.at,
|
|
128
|
-
retryAt: lane.retryAt,
|
|
129
|
-
message: lane.message,
|
|
130
|
-
remainingMs: lane.state === 'quota' ? Math.max(0, lane.retryAt - now) : 0,
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 本地闸门:`ok:false` 时调用方应直接拒绝这一轮请求(不再出网),
|
|
136
|
-
* 并把 `retryAt` / 「换出口」作为唯一有效动作回给用户。
|
|
137
|
-
*/
|
|
138
|
-
export function laneGate() {
|
|
139
|
-
const lane = read()
|
|
140
|
-
if (lane.state === 'ok' || lane.state === undefined) return { ok: true }
|
|
141
|
-
if (lane.state === 'quota') {
|
|
142
|
-
if (!Number.isFinite(lane.retryAt) || lane.retryAt <= Date.now()) return { ok: true }
|
|
143
|
-
return { ok: false, state: 'quota', retryAt: lane.retryAt, message: lane.message }
|
|
144
|
-
}
|
|
145
|
-
if (lane.state === 'locked') return { ok: false, state: 'locked', message: lane.message }
|
|
146
|
-
return { ok: true }
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/** 手动清闸(面板「重置车道」;换代理/换网络后不想等窗口时用它)。 */
|
|
150
|
-
export async function resetLane({ probe = false } = {}) {
|
|
151
|
-
const lane = read()
|
|
152
|
-
egressCheckedAt = 0
|
|
153
|
-
const egress = probe ? fingerprint(await detectEgress()) : lane.egress
|
|
154
|
-
write({ state: 'ok', at: Date.now(), retryAt: 0, message: '', egress })
|
|
155
|
-
return laneStatus()
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* 用一次真实流量的结果记账。
|
|
160
|
-
* 成功是最强的开放信号:清掉任何判定。
|
|
161
|
-
*/
|
|
162
|
-
export function noteLaneOutcome({ ok, error } = {}) {
|
|
163
|
-
const lane = read()
|
|
164
|
-
if (ok === true) {
|
|
165
|
-
if (lane.state !== 'ok') write({ ...lane, state: 'ok', at: Date.now(), retryAt: 0, message: '' })
|
|
166
|
-
return
|
|
167
|
-
}
|
|
168
|
-
const kind = error?.kind
|
|
169
|
-
if (kind === 'quota') {
|
|
170
|
-
const after = Number(error?.retryAfterMs ?? error?.cooldownMs)
|
|
171
|
-
const retryAt = Number.isFinite(after) && after > 0 ? Date.now() + after : Date.now() + LANE_QUOTA_DEFAULT_MS
|
|
172
|
-
write({ ...lane, state: 'quota', at: Date.now(), retryAt, message: String(error?.message ?? 'free usage limit reached').slice(0, 300) })
|
|
173
|
-
return
|
|
174
|
-
}
|
|
175
|
-
if (kind === 'free_tier') {
|
|
176
|
-
write({ ...lane, state: 'locked', at: Date.now(), retryAt: 0, message: String(error?.message ?? 'the free tier answers this client only').slice(0, 300) })
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 闸门拒绝后的一次异步复查:若出口已变则清闸,让下一条请求放行。
|
|
182
|
-
* 由调用方在拒绝路径上 fire-and-forget(不阻塞、不抛出)。
|
|
183
|
-
*/
|
|
184
|
-
export function recheckEgressAsync() {
|
|
185
|
-
recheckEgress().catch(() => false)
|
|
186
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* OpenCode 免密车道的车道级熔断器。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么需要它
|
|
5
|
+
*
|
|
6
|
+
* 上游按 **出口 IP × project id** 记免费额度,一个烧尽的出口对**任何**模型、
|
|
7
|
+
* **任何** session 都回同一句 `429 FreeUsageLimitError` + 数小时的 `retry-after`
|
|
8
|
+
* (实测:换模型、换会话、换 UA 全都无效)。熔断前
|
|
9
|
+
* 车道只能靠"再发一条真实请求"来发现这堵墙,于是每次撞墙都在继续烧配额;
|
|
10
|
+
* 熔断后这堵墙由真实流量记录一次,窗口内后续请求**本地直接拒绝**。
|
|
11
|
+
*
|
|
12
|
+
* ## 状态
|
|
13
|
+
*
|
|
14
|
+
* - `ok` — 当前出口没有不利判定。
|
|
15
|
+
* - `quota` — 真实 429;`retryAt` 是窗口结束时刻(缺 `retry-after` 时按默认窗口估)。
|
|
16
|
+
* - `locked` — 客户端伪装被拒(`FreeTierError`):等待无用,只有整形层跟上上游、
|
|
17
|
+
* 或换出口才能清;本地拒绝直到状态被清。
|
|
18
|
+
*
|
|
19
|
+
* 出口 IP 变化一律清闸(新出口 = 新配额桶)。出口探测只在被拒后按需做(TTL 内
|
|
20
|
+
* 复用),正常路径不出网探测。
|
|
21
|
+
*
|
|
22
|
+
* 落盘:`{DATA_DIR}/opencode-lane.json`,重启后仍然有效(额度窗口是按小时的)。
|
|
23
|
+
*
|
|
24
|
+
* @module src/providers/opencode/lane
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { createHash } from 'node:crypto'
|
|
28
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
29
|
+
import { join } from 'node:path'
|
|
30
|
+
import { DATA_DIR, emitRuntimeEvent } from '../../store.js'
|
|
31
|
+
import { httpFetch, readBodyText } from '../../util.js'
|
|
32
|
+
|
|
33
|
+
/** 响应没带可用 `retry-after` 时假设的额度窗口。 */
|
|
34
|
+
const LANE_QUOTA_DEFAULT_MS = 6 * 60 * 60 * 1000
|
|
35
|
+
|
|
36
|
+
/** 被拒后复查出口 IP 的最小间隔(正常流量不再重复出网探测)。 */
|
|
37
|
+
const EGRESS_CHECK_MS = 5 * 60 * 1000
|
|
38
|
+
|
|
39
|
+
/** 公开出口探测源,按序尝试,任一应答即可(只读公网 IP,不带任何凭据)。 */
|
|
40
|
+
const ECHO_SOURCES = [
|
|
41
|
+
{ url: 'https://api.ipify.org?format=json', pick: payload => payload?.ip },
|
|
42
|
+
{ url: 'https://ipinfo.io/json', pick: payload => payload?.ip },
|
|
43
|
+
{ url: 'https://ipapi.co/json/', pick: payload => payload?.ip },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
const LANE_FILE = join(DATA_DIR, 'opencode-lane.json')
|
|
47
|
+
|
|
48
|
+
let cached = null
|
|
49
|
+
|
|
50
|
+
function read() {
|
|
51
|
+
if (cached !== null) return cached
|
|
52
|
+
try {
|
|
53
|
+
if (!existsSync(LANE_FILE)) { cached = { state: 'ok', at: 0, retryAt: 0, message: '', egress: '' }; return cached }
|
|
54
|
+
const doc = JSON.parse(readFileSync(LANE_FILE, 'utf8'))
|
|
55
|
+
cached = {
|
|
56
|
+
state: doc.state === 'quota' || doc.state === 'locked' ? doc.state : 'ok',
|
|
57
|
+
at: Number(doc.at) || 0,
|
|
58
|
+
retryAt: Number(doc.retryAt) || 0,
|
|
59
|
+
message: typeof doc.message === 'string' ? doc.message.slice(0, 300) : '',
|
|
60
|
+
egress: typeof doc.egress === 'string' ? doc.egress : '',
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
cached = { state: 'ok', at: 0, retryAt: 0, message: '', egress: '' }
|
|
64
|
+
}
|
|
65
|
+
return cached
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function write(next) {
|
|
69
|
+
cached = next
|
|
70
|
+
try {
|
|
71
|
+
mkdirSync(DATA_DIR, { recursive: true })
|
|
72
|
+
const temp = `${LANE_FILE}.tmp`
|
|
73
|
+
writeFileSync(temp, JSON.stringify({ version: 1, ...next }, undefined, 2), 'utf8')
|
|
74
|
+
renameSync(temp, LANE_FILE)
|
|
75
|
+
} catch { /* 只读目录 / 磁盘满:状态仍留在内存里 */ }
|
|
76
|
+
emitRuntimeEvent('opencode-lane', next)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 出口指纹:公网 IP 的 sha256 前 12 位(不落明文 IP)。 */
|
|
80
|
+
function fingerprint(ip) {
|
|
81
|
+
const trimmed = String(ip ?? '').trim()
|
|
82
|
+
if (trimmed === '') return ''
|
|
83
|
+
return createHash('sha256').update(`opencode-egress\0${trimmed}`).digest('hex').slice(0, 12)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 探测本机出口 IP(失败返回空串——探测失败绝不影响请求本身)。 */
|
|
87
|
+
async function detectEgress() {
|
|
88
|
+
for (const source of ECHO_SOURCES) {
|
|
89
|
+
try {
|
|
90
|
+
const response = await httpFetch(source.url, { headers: { accept: 'application/json' } }, 4000)
|
|
91
|
+
const text = await readBodyText(response, 4000)
|
|
92
|
+
if (!response.ok) continue
|
|
93
|
+
const ip = source.pick(JSON.parse(text))
|
|
94
|
+
if (typeof ip === 'string' && ip.trim() !== '') return ip.trim()
|
|
95
|
+
} catch { /* 换下一个源 */ }
|
|
96
|
+
}
|
|
97
|
+
return ''
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 被拒状态下的出口复查:探测到出口变了就清闸。
|
|
102
|
+
* 只有确实拿到新指纹时才更新 `checkedAt`,避免探测失败导致每次请求都出网。
|
|
103
|
+
*/
|
|
104
|
+
let egressCheckedAt = 0
|
|
105
|
+
async function recheckEgress() {
|
|
106
|
+
const now = Date.now()
|
|
107
|
+
if (egressCheckedAt !== 0 && now - egressCheckedAt < EGRESS_CHECK_MS) return false
|
|
108
|
+
egressCheckedAt = now
|
|
109
|
+
const current = fingerprint(await detectEgress())
|
|
110
|
+
if (current === '') return false
|
|
111
|
+
const lane = read()
|
|
112
|
+
if (lane.egress !== '' && lane.egress !== current) {
|
|
113
|
+
write({ ...lane, state: 'ok', at: now, retryAt: 0, message: '', egress: current })
|
|
114
|
+
return true
|
|
115
|
+
}
|
|
116
|
+
if (lane.egress === '') write({ ...lane, egress: current })
|
|
117
|
+
return false
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 车道当前状态(面板展示用;含窗口剩余时间与出口复查提示)。 */
|
|
121
|
+
export function laneStatus() {
|
|
122
|
+
const lane = read()
|
|
123
|
+
const now = Date.now()
|
|
124
|
+
if (lane.state === 'quota' && lane.retryAt <= now) return { state: 'ok', at: lane.at, retryAt: 0, message: '', remainingMs: 0 }
|
|
125
|
+
return {
|
|
126
|
+
state: lane.state,
|
|
127
|
+
at: lane.at,
|
|
128
|
+
retryAt: lane.retryAt,
|
|
129
|
+
message: lane.message,
|
|
130
|
+
remainingMs: lane.state === 'quota' ? Math.max(0, lane.retryAt - now) : 0,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 本地闸门:`ok:false` 时调用方应直接拒绝这一轮请求(不再出网),
|
|
136
|
+
* 并把 `retryAt` / 「换出口」作为唯一有效动作回给用户。
|
|
137
|
+
*/
|
|
138
|
+
export function laneGate() {
|
|
139
|
+
const lane = read()
|
|
140
|
+
if (lane.state === 'ok' || lane.state === undefined) return { ok: true }
|
|
141
|
+
if (lane.state === 'quota') {
|
|
142
|
+
if (!Number.isFinite(lane.retryAt) || lane.retryAt <= Date.now()) return { ok: true }
|
|
143
|
+
return { ok: false, state: 'quota', retryAt: lane.retryAt, message: lane.message }
|
|
144
|
+
}
|
|
145
|
+
if (lane.state === 'locked') return { ok: false, state: 'locked', message: lane.message }
|
|
146
|
+
return { ok: true }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 手动清闸(面板「重置车道」;换代理/换网络后不想等窗口时用它)。 */
|
|
150
|
+
export async function resetLane({ probe = false } = {}) {
|
|
151
|
+
const lane = read()
|
|
152
|
+
egressCheckedAt = 0
|
|
153
|
+
const egress = probe ? fingerprint(await detectEgress()) : lane.egress
|
|
154
|
+
write({ state: 'ok', at: Date.now(), retryAt: 0, message: '', egress })
|
|
155
|
+
return laneStatus()
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 用一次真实流量的结果记账。
|
|
160
|
+
* 成功是最强的开放信号:清掉任何判定。
|
|
161
|
+
*/
|
|
162
|
+
export function noteLaneOutcome({ ok, error } = {}) {
|
|
163
|
+
const lane = read()
|
|
164
|
+
if (ok === true) {
|
|
165
|
+
if (lane.state !== 'ok') write({ ...lane, state: 'ok', at: Date.now(), retryAt: 0, message: '' })
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
const kind = error?.kind
|
|
169
|
+
if (kind === 'quota') {
|
|
170
|
+
const after = Number(error?.retryAfterMs ?? error?.cooldownMs)
|
|
171
|
+
const retryAt = Number.isFinite(after) && after > 0 ? Date.now() + after : Date.now() + LANE_QUOTA_DEFAULT_MS
|
|
172
|
+
write({ ...lane, state: 'quota', at: Date.now(), retryAt, message: String(error?.message ?? 'free usage limit reached').slice(0, 300) })
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
if (kind === 'free_tier') {
|
|
176
|
+
write({ ...lane, state: 'locked', at: Date.now(), retryAt: 0, message: String(error?.message ?? 'the free tier answers this client only').slice(0, 300) })
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 闸门拒绝后的一次异步复查:若出口已变则清闸,让下一条请求放行。
|
|
182
|
+
* 由调用方在拒绝路径上 fire-and-forget(不阻塞、不抛出)。
|
|
183
|
+
*/
|
|
184
|
+
export function recheckEgressAsync() {
|
|
185
|
+
recheckEgress().catch(() => false)
|
|
186
|
+
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Ebony-Vinyl
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ebony-Vinyl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The two captured official assets the shaping layer loads lazily.
|
|
3
|
-
*
|
|
4
|
-
* Development form: read from `assets/` beside this module, degrading exactly
|
|
5
|
-
* like the previous inline reads (missing files → empty prompt / no
|
|
6
|
-
* definitions). The release bundle replaces this module with inlined
|
|
7
|
-
* constants — `scripts/build-legacy-payload.mjs` swaps it during the
|
|
8
|
-
* `src/o2a.js` build, because the 80-file legacy-upgrader budget cannot afford
|
|
9
|
-
* shipping `src/o2a/assets/` alongside the bundle.
|
|
10
|
-
*
|
|
11
|
-
* @module src/o2a/asset-source.js
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import fs from 'node:fs'
|
|
15
|
-
|
|
16
|
-
const PROMPT_ASSET = new URL('./assets/opencode_prompt_v2.txt', import.meta.url)
|
|
17
|
-
const TOOLS_ASSET = new URL('./assets/opencode_tools.json', import.meta.url)
|
|
18
|
-
|
|
19
|
-
let promptCache
|
|
20
|
-
|
|
21
|
-
/** The official CLI base prompt, verbatim; `''` when the asset is unreadable. */
|
|
22
|
-
export function officialPromptText() {
|
|
23
|
-
if (promptCache !== undefined) return promptCache
|
|
24
|
-
try {
|
|
25
|
-
promptCache = fs.readFileSync(PROMPT_ASSET, 'utf8')
|
|
26
|
-
} catch {
|
|
27
|
-
promptCache = ''
|
|
28
|
-
}
|
|
29
|
-
return promptCache
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
let toolsCache
|
|
33
|
-
|
|
34
|
-
/** The captured official tool definitions as raw JSON text; `''` when unreadable. */
|
|
35
|
-
export function officialToolsText() {
|
|
36
|
-
if (toolsCache !== undefined) return toolsCache
|
|
37
|
-
try {
|
|
38
|
-
toolsCache = fs.readFileSync(TOOLS_ASSET, 'utf8')
|
|
39
|
-
} catch {
|
|
40
|
-
toolsCache = ''
|
|
41
|
-
}
|
|
42
|
-
return toolsCache
|
|
43
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* The two captured official assets the shaping layer loads lazily.
|
|
3
|
+
*
|
|
4
|
+
* Development form: read from `assets/` beside this module, degrading exactly
|
|
5
|
+
* like the previous inline reads (missing files → empty prompt / no
|
|
6
|
+
* definitions). The release bundle replaces this module with inlined
|
|
7
|
+
* constants — `scripts/build-legacy-payload.mjs` swaps it during the
|
|
8
|
+
* `src/o2a.js` build, because the 80-file legacy-upgrader budget cannot afford
|
|
9
|
+
* shipping `src/o2a/assets/` alongside the bundle.
|
|
10
|
+
*
|
|
11
|
+
* @module src/o2a/asset-source.js
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs'
|
|
15
|
+
|
|
16
|
+
const PROMPT_ASSET = new URL('./assets/opencode_prompt_v2.txt', import.meta.url)
|
|
17
|
+
const TOOLS_ASSET = new URL('./assets/opencode_tools.json', import.meta.url)
|
|
18
|
+
|
|
19
|
+
let promptCache
|
|
20
|
+
|
|
21
|
+
/** The official CLI base prompt, verbatim; `''` when the asset is unreadable. */
|
|
22
|
+
export function officialPromptText() {
|
|
23
|
+
if (promptCache !== undefined) return promptCache
|
|
24
|
+
try {
|
|
25
|
+
promptCache = fs.readFileSync(PROMPT_ASSET, 'utf8')
|
|
26
|
+
} catch {
|
|
27
|
+
promptCache = ''
|
|
28
|
+
}
|
|
29
|
+
return promptCache
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
let toolsCache
|
|
33
|
+
|
|
34
|
+
/** The captured official tool definitions as raw JSON text; `''` when unreadable. */
|
|
35
|
+
export function officialToolsText() {
|
|
36
|
+
if (toolsCache !== undefined) return toolsCache
|
|
37
|
+
try {
|
|
38
|
+
toolsCache = fs.readFileSync(TOOLS_ASSET, 'utf8')
|
|
39
|
+
} catch {
|
|
40
|
+
toolsCache = ''
|
|
41
|
+
}
|
|
42
|
+
return toolsCache
|
|
43
|
+
}
|