dsh-plugin-opencode2api 1.2.2 → 1.3.2
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/README.md +14 -0
- package/lib/client.js +65 -2
- package/lib/index.js +323 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,6 +45,20 @@ dsh plugin --profile web add dsh-plugin-opencode2api
|
|
|
45
45
|
|
|
46
46
|
Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1:3080`)→ **设置 → 本地免费 API**,就能看到状态和操作按钮;手机 App 则在「本地API」胶囊/面板里。
|
|
47
47
|
|
|
48
|
+
## 接入云端设备池(1.3.2)
|
|
49
|
+
|
|
50
|
+
如果你有一个自己的中转站(例如本项目作者的 oc-proxy),可以把这台设备变成它的一个后端,让中转站把请求分过来。
|
|
51
|
+
|
|
52
|
+
1. 中转站后台 → **管理 → 设备** → **生成接入码**(15 分钟有效,只能用一次);
|
|
53
|
+
2. 在这台设备上打开插件:
|
|
54
|
+
- 手机:输入框上方「本地API」→ 面板里填前端地址和接入码 → 加入设备池;
|
|
55
|
+
- 桌面:设置 → 本地免费 API → **云端设备池** → 填地址和接入码 → 加入设备池;
|
|
56
|
+
3. 插件会自动创建 Cloudflare 隧道、下载并启动 cloudflared、每 60 秒上报心跳。
|
|
57
|
+
|
|
58
|
+
管理端看到设备「在线」后就会把请求分配过来。心跳超过 3 分钟没到会自动标记离线,重新上线自动恢复;长期不用时在管理端点「删除」会同时清理隧道和 DNS。
|
|
59
|
+
|
|
60
|
+
退出:面板里点「退出设备池」,会同时从中转站删除这台设备。
|
|
61
|
+
|
|
48
62
|
## 使用
|
|
49
63
|
|
|
50
64
|
装好后插件会自动把本地 API 注册成模型商「本地免费 API」,模型列表从 `/v1/models` 动态获取。直接在 DSH 的模型选择器里选它下面的免费模型即可,不需要手动填 URL 和 Key。
|
package/lib/client.js
CHANGED
|
@@ -21,6 +21,17 @@ window.__ModuleLoader__.load({
|
|
|
21
21
|
.oc2a summary { user-select: none; }
|
|
22
22
|
`
|
|
23
23
|
|
|
24
|
+
const inputStyle = {
|
|
25
|
+
flex: '1 1 200px',
|
|
26
|
+
minWidth: 140,
|
|
27
|
+
padding: '6px 8px',
|
|
28
|
+
borderRadius: 7,
|
|
29
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
30
|
+
background: 'var(--dsw-alias-bg-layer-2)',
|
|
31
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
32
|
+
fontSize: 12,
|
|
33
|
+
}
|
|
34
|
+
|
|
24
35
|
const calloutStyle = {
|
|
25
36
|
border: '1px solid var(--dsw-alias-border-l1)',
|
|
26
37
|
borderRadius: 9,
|
|
@@ -136,6 +147,8 @@ window.__ModuleLoader__.load({
|
|
|
136
147
|
const [error, setError] = useState('')
|
|
137
148
|
const [busy, setBusy] = useState('')
|
|
138
149
|
const [copied, copy] = useCopy()
|
|
150
|
+
const [poolUrl, setPoolUrl] = useState('https://')
|
|
151
|
+
const [poolCode, setPoolCode] = useState('')
|
|
139
152
|
|
|
140
153
|
const load = useCallback(async () => {
|
|
141
154
|
try {
|
|
@@ -152,14 +165,14 @@ window.__ModuleLoader__.load({
|
|
|
152
165
|
return () => clearInterval(timer)
|
|
153
166
|
}, [load])
|
|
154
167
|
|
|
155
|
-
const act = async action => {
|
|
168
|
+
const act = async (action, extra = {}) => {
|
|
156
169
|
setBusy(action)
|
|
157
170
|
setError('')
|
|
158
171
|
try {
|
|
159
172
|
const result = await request('/action', {
|
|
160
173
|
method: 'POST',
|
|
161
174
|
headers: { 'content-type': 'application/json' },
|
|
162
|
-
body: JSON.stringify({ action }),
|
|
175
|
+
body: JSON.stringify({ action, ...extra }),
|
|
163
176
|
})
|
|
164
177
|
if (result?.state) setState(result.state)
|
|
165
178
|
else void load()
|
|
@@ -377,6 +390,56 @@ window.__ModuleLoader__.load({
|
|
|
377
390
|
'项目主页',
|
|
378
391
|
),
|
|
379
392
|
),
|
|
393
|
+
h(
|
|
394
|
+
'div',
|
|
395
|
+
{ style: calloutStyle },
|
|
396
|
+
h(
|
|
397
|
+
'div',
|
|
398
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 6 } },
|
|
399
|
+
h('strong', null, '云端设备池'),
|
|
400
|
+
h(
|
|
401
|
+
'span',
|
|
402
|
+
{ style: { fontSize: 12, color: TONE[state?.poolTone] || TONE.muted } },
|
|
403
|
+
state?.poolStatus || '未接入',
|
|
404
|
+
),
|
|
405
|
+
),
|
|
406
|
+
state?.poolHost
|
|
407
|
+
? h('div', { style: { fontSize: 12, marginBottom: 6 } }, state.poolHost)
|
|
408
|
+
: null,
|
|
409
|
+
h(
|
|
410
|
+
'div',
|
|
411
|
+
{ style: { display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 6 } },
|
|
412
|
+
h('input', {
|
|
413
|
+
value: poolUrl,
|
|
414
|
+
onChange: e => setPoolUrl(e.target.value),
|
|
415
|
+
placeholder: 'https://oc.techpanda.qzz.io',
|
|
416
|
+
style: inputStyle,
|
|
417
|
+
}),
|
|
418
|
+
h('input', {
|
|
419
|
+
value: poolCode,
|
|
420
|
+
onChange: e => setPoolCode(e.target.value),
|
|
421
|
+
placeholder: '接入码 ocp_…',
|
|
422
|
+
style: inputStyle,
|
|
423
|
+
}),
|
|
424
|
+
h(
|
|
425
|
+
Button,
|
|
426
|
+
{
|
|
427
|
+
primary: true,
|
|
428
|
+
disabled: disabled || !poolCode.trim(),
|
|
429
|
+
onClick: () => act('joinPool', { url: poolUrl.trim(), code: poolCode.trim() }),
|
|
430
|
+
},
|
|
431
|
+
busy === 'joinPool' ? '接入中…' : '加入设备池',
|
|
432
|
+
),
|
|
433
|
+
state?.poolHost
|
|
434
|
+
? h(Button, { onClick: () => act('leavePool'), disabled }, '退出')
|
|
435
|
+
: null,
|
|
436
|
+
),
|
|
437
|
+
h(
|
|
438
|
+
'div',
|
|
439
|
+
{ style: { fontSize: 11, color: TONE.muted } },
|
|
440
|
+
'在这台设备上自动建隧道并上报心跳;掉线后管理端会自动标记离线。',
|
|
441
|
+
),
|
|
442
|
+
),
|
|
380
443
|
h(
|
|
381
444
|
'div',
|
|
382
445
|
{
|
package/lib/index.js
CHANGED
|
@@ -308,9 +308,300 @@ function readJson(req) {
|
|
|
308
308
|
})
|
|
309
309
|
}
|
|
310
310
|
|
|
311
|
+
|
|
312
|
+
// ---- 云端设备池 ----
|
|
313
|
+
const PLUGIN_VERSION = '1.3.0'
|
|
314
|
+
const POOL = path.join(DATA, 'pool.json')
|
|
315
|
+
const CLOUDFLARED = path.join(DATA, IS_WIN ? 'cloudflared.exe' : 'cloudflared')
|
|
316
|
+
const TUNNEL_LOG = path.join(DATA, 'cloudflared.log')
|
|
317
|
+
const TUNNEL_PID = path.join(DATA, 'cloudflared.pid')
|
|
318
|
+
|
|
319
|
+
let poolInfo = { status: '未接入', tone: 'muted', hostname: '', lastError: '' }
|
|
320
|
+
let tunnelChild = null
|
|
321
|
+
let heartbeatTimer = null
|
|
322
|
+
let tunnelTimer = null
|
|
323
|
+
let joining = null
|
|
324
|
+
|
|
325
|
+
function readPool() {
|
|
326
|
+
try {
|
|
327
|
+
return JSON.parse(fs.readFileSync(POOL, 'utf8'))
|
|
328
|
+
} catch {
|
|
329
|
+
return null
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function writePool(data) {
|
|
334
|
+
fs.mkdirSync(DATA, { recursive: true })
|
|
335
|
+
fs.writeFileSync(POOL, JSON.stringify(data, null, 2), { mode: 0o600 })
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function localApiKey() {
|
|
339
|
+
const cfg = readConfig()
|
|
340
|
+
return cfg.server_keys?.[0] || ''
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function poolSnapshot() {
|
|
344
|
+
const pool = readPool()
|
|
345
|
+
if (!pool?.deviceId) return { status: '未接入', tone: 'muted', hostname: '', lastError: '' }
|
|
346
|
+
const age = pool.lastHeartbeat ? Date.now() - pool.lastHeartbeat : null
|
|
347
|
+
if (pool.lastError) return { status: '心跳失败', tone: 'warn', hostname: pool.hostname || '', lastError: pool.lastError }
|
|
348
|
+
if (age === null) return { status: '等待心跳', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
349
|
+
if (age < 180000) return { status: '在线', tone: 'good', hostname: pool.hostname || '', lastError: '' }
|
|
350
|
+
return { status: '离线', tone: 'bad', hostname: pool.hostname || '', lastError: '' }
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function cloudflaredTarget() {
|
|
354
|
+
const arch = process.arch === 'x64' ? 'amd64' : process.arch === 'arm64' ? 'arm64' : null
|
|
355
|
+
if (!arch) throw new Error('当前 CPU 架构不支持 cloudflared')
|
|
356
|
+
if (process.platform === 'win32') return { file: `cloudflared-windows-${arch}.exe`, archive: false }
|
|
357
|
+
if (process.platform === 'darwin') return { file: `cloudflared-darwin-${arch}.tgz`, archive: true }
|
|
358
|
+
return { file: `cloudflared-linux-${arch}`, archive: false }
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async function ensureCloudflared() {
|
|
362
|
+
if (fs.existsSync(CLOUDFLARED)) return
|
|
363
|
+
const t = cloudflaredTarget()
|
|
364
|
+
let lastFailure = null
|
|
365
|
+
for (const prefix of MIRRORS) {
|
|
366
|
+
try {
|
|
367
|
+
const body = await fetchBuffer(`${prefix}https://github.com/cloudflare/cloudflared/releases/latest/download/${t.file}`)
|
|
368
|
+
if (t.archive) {
|
|
369
|
+
const temp = path.join(DATA, `.cf-${Date.now()}`)
|
|
370
|
+
fs.mkdirSync(temp, { recursive: true })
|
|
371
|
+
const archive = path.join(temp, t.file)
|
|
372
|
+
await fsp.writeFile(archive, body)
|
|
373
|
+
await extract(archive, temp)
|
|
374
|
+
const found = findFile(temp, 'cloudflared')
|
|
375
|
+
if (!found) throw new Error('cloudflared 解压失败')
|
|
376
|
+
await fsp.copyFile(found, CLOUDFLARED)
|
|
377
|
+
await fsp.rm(temp, { recursive: true, force: true }).catch(() => {})
|
|
378
|
+
} else {
|
|
379
|
+
await fsp.writeFile(CLOUDFLARED, body)
|
|
380
|
+
}
|
|
381
|
+
if (!IS_WIN) fs.chmodSync(CLOUDFLARED, 0o755)
|
|
382
|
+
return
|
|
383
|
+
} catch (error) {
|
|
384
|
+
lastFailure = error
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
throw lastFailure || new Error('cloudflared 下载失败')
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function tunnelRunning() {
|
|
391
|
+
if (tunnelChild && tunnelChild.exitCode === null) return true
|
|
392
|
+
try {
|
|
393
|
+
const pid = Number(fs.readFileSync(TUNNEL_PID, 'utf8').trim())
|
|
394
|
+
if (pid && alive(pid)) return true
|
|
395
|
+
} catch {}
|
|
396
|
+
return false
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async function startTunnel() {
|
|
400
|
+
const pool = readPool()
|
|
401
|
+
if (!pool?.tunnelToken) return false
|
|
402
|
+
if (tunnelRunning()) return true
|
|
403
|
+
if (!fs.existsSync(CLOUDFLARED)) await ensureCloudflared()
|
|
404
|
+
const fd = fs.openSync(TUNNEL_LOG, 'a')
|
|
405
|
+
try {
|
|
406
|
+
tunnelChild = spawn(
|
|
407
|
+
CLOUDFLARED,
|
|
408
|
+
['tunnel', '--no-autoupdate', 'run', '--token', pool.tunnelToken],
|
|
409
|
+
{ stdio: ['ignore', fd, fd], detached: false },
|
|
410
|
+
)
|
|
411
|
+
} finally {
|
|
412
|
+
fs.closeSync(fd)
|
|
413
|
+
}
|
|
414
|
+
tunnelChild.once('error', () => {})
|
|
415
|
+
tunnelChild.unref()
|
|
416
|
+
try {
|
|
417
|
+
fs.writeFileSync(TUNNEL_PID, String(tunnelChild.pid))
|
|
418
|
+
} catch {}
|
|
419
|
+
return true
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function stopTunnel() {
|
|
423
|
+
const pids = new Set()
|
|
424
|
+
if (tunnelChild?.pid) pids.add(tunnelChild.pid)
|
|
425
|
+
try {
|
|
426
|
+
const filePid = Number(fs.readFileSync(TUNNEL_PID, 'utf8').trim())
|
|
427
|
+
if (filePid) pids.add(filePid)
|
|
428
|
+
} catch {}
|
|
429
|
+
for (const pid of pids) {
|
|
430
|
+
try {
|
|
431
|
+
process.kill(pid)
|
|
432
|
+
} catch {}
|
|
433
|
+
}
|
|
434
|
+
tunnelChild = null
|
|
435
|
+
try {
|
|
436
|
+
fs.unlinkSync(TUNNEL_PID)
|
|
437
|
+
} catch {}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export async function heartbeat() {
|
|
441
|
+
const pool = readPool()
|
|
442
|
+
if (!pool?.deviceId || !pool?.deviceSecret) return
|
|
443
|
+
try {
|
|
444
|
+
const res = await fetch(`${pool.url}/api/device/heartbeat`, {
|
|
445
|
+
method: 'POST',
|
|
446
|
+
headers: {
|
|
447
|
+
'content-type': 'application/json',
|
|
448
|
+
'x-device-id': pool.deviceId,
|
|
449
|
+
'x-device-secret': pool.deviceSecret,
|
|
450
|
+
},
|
|
451
|
+
body: JSON.stringify({
|
|
452
|
+
version: PLUGIN_VERSION,
|
|
453
|
+
platform: target() ? `${target().os}/${target().arch}` : process.platform,
|
|
454
|
+
}),
|
|
455
|
+
signal: AbortSignal.timeout(15000),
|
|
456
|
+
})
|
|
457
|
+
const data = await res.json().catch(() => ({}))
|
|
458
|
+
if (res.status === 401) {
|
|
459
|
+
resetPoolLocally(data.error || '设备已被移除')
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`)
|
|
463
|
+
pool.lastHeartbeat = Date.now()
|
|
464
|
+
pool.lastError = ''
|
|
465
|
+
writePool(pool)
|
|
466
|
+
if (data.enabled === false) {
|
|
467
|
+
stopTunnel()
|
|
468
|
+
poolInfo = { status: '已被管理端停用', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
469
|
+
} else {
|
|
470
|
+
void startTunnel()
|
|
471
|
+
poolInfo = { status: '在线', tone: 'good', hostname: pool.hostname || '', lastError: '' }
|
|
472
|
+
}
|
|
473
|
+
} catch (error) {
|
|
474
|
+
pool.lastError = human(error)
|
|
475
|
+
writePool(pool)
|
|
476
|
+
poolInfo = { status: '心跳失败', tone: 'warn', hostname: pool.hostname || '', lastError: pool.lastError }
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function startPoolTimers() {
|
|
481
|
+
if (!heartbeatTimer) {
|
|
482
|
+
heartbeatTimer = setInterval(() => {
|
|
483
|
+
void heartbeat()
|
|
484
|
+
}, 60_000)
|
|
485
|
+
heartbeatTimer.unref?.()
|
|
486
|
+
}
|
|
487
|
+
if (!tunnelTimer) {
|
|
488
|
+
tunnelTimer = setInterval(() => {
|
|
489
|
+
const pool = readPool()
|
|
490
|
+
if (pool?.tunnelToken && !tunnelRunning()) void startTunnel().catch(() => {})
|
|
491
|
+
}, 45_000)
|
|
492
|
+
tunnelTimer.unref?.()
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function stopPoolTimers() {
|
|
497
|
+
if (heartbeatTimer) clearInterval(heartbeatTimer)
|
|
498
|
+
if (tunnelTimer) clearInterval(tunnelTimer)
|
|
499
|
+
heartbeatTimer = null
|
|
500
|
+
tunnelTimer = null
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export async function joinPool(input = {}) {
|
|
504
|
+
if (joining) return joining
|
|
505
|
+
joining = (async () => {
|
|
506
|
+
const url = String(input.url || '').trim().replace(/\/+$/, '')
|
|
507
|
+
const code = String(input.code || '').trim()
|
|
508
|
+
if (!/^https?:\/\//i.test(url)) throw new Error('前端地址要以 http:// 或 https:// 开头')
|
|
509
|
+
if (!code) throw new Error('缺少接入码')
|
|
510
|
+
|
|
511
|
+
if (!fs.existsSync(CFG)) ensureConfig()
|
|
512
|
+
if (!fs.existsSync(BIN)) await ensureInstalled()
|
|
513
|
+
if (!(await health())) await start()
|
|
514
|
+
const key = localApiKey()
|
|
515
|
+
if (!key) throw new Error('本机 API Key 还没生成,先点一次启动')
|
|
516
|
+
|
|
517
|
+
const res = await fetch(`${url}/api/device/enroll`, {
|
|
518
|
+
method: 'POST',
|
|
519
|
+
headers: { 'content-type': 'application/json' },
|
|
520
|
+
body: JSON.stringify({
|
|
521
|
+
code,
|
|
522
|
+
apiKey: key,
|
|
523
|
+
name: os.hostname().slice(0, 40) || 'device',
|
|
524
|
+
platform: target() ? `${target().os}/${target().arch}` : process.platform,
|
|
525
|
+
version: PLUGIN_VERSION,
|
|
526
|
+
}),
|
|
527
|
+
signal: AbortSignal.timeout(30000),
|
|
528
|
+
})
|
|
529
|
+
const data = await res.json().catch(() => ({}))
|
|
530
|
+
if (!res.ok || !data.device_id) throw new Error(data.error || `接入失败 HTTP ${res.status}`)
|
|
531
|
+
|
|
532
|
+
writePool({
|
|
533
|
+
url,
|
|
534
|
+
deviceId: data.device_id,
|
|
535
|
+
deviceSecret: data.device_secret,
|
|
536
|
+
tunnelToken: data.tunnel_token,
|
|
537
|
+
hostname: data.hostname || '',
|
|
538
|
+
joinedAt: Date.now(),
|
|
539
|
+
lastHeartbeat: 0,
|
|
540
|
+
lastError: '',
|
|
541
|
+
})
|
|
542
|
+
await ensureCloudflared()
|
|
543
|
+
await startTunnel()
|
|
544
|
+
startPoolTimers()
|
|
545
|
+
await heartbeat()
|
|
546
|
+
poolInfo = { status: '在线', tone: 'good', hostname: data.hostname || '', lastError: '' }
|
|
547
|
+
return `已接入设备池:${data.hostname || data.device_id}`
|
|
548
|
+
})()
|
|
549
|
+
try {
|
|
550
|
+
return await joining
|
|
551
|
+
} finally {
|
|
552
|
+
joining = null
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export function resetPoolLocally(reason) {
|
|
557
|
+
stopPoolTimers()
|
|
558
|
+
stopTunnel()
|
|
559
|
+
try {
|
|
560
|
+
fs.unlinkSync(POOL)
|
|
561
|
+
} catch {}
|
|
562
|
+
poolInfo = { status: String(reason || '已退出').slice(0, 40), tone: 'warn', hostname: '', lastError: '' }
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
async function leavePool() {
|
|
566
|
+
const pool = readPool()
|
|
567
|
+
stopPoolTimers()
|
|
568
|
+
stopTunnel()
|
|
569
|
+
if (pool?.deviceId && pool?.deviceSecret && pool?.url) {
|
|
570
|
+
await fetch(`${pool.url}/api/device/leave`, {
|
|
571
|
+
method: 'POST',
|
|
572
|
+
headers: { 'x-device-id': pool.deviceId, 'x-device-secret': pool.deviceSecret },
|
|
573
|
+
signal: AbortSignal.timeout(15000),
|
|
574
|
+
}).catch(() => {})
|
|
575
|
+
}
|
|
576
|
+
try {
|
|
577
|
+
fs.unlinkSync(POOL)
|
|
578
|
+
} catch {}
|
|
579
|
+
poolInfo = { status: '未接入', tone: 'muted', hostname: '', lastError: '' }
|
|
580
|
+
return '已退出设备池'
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function bootPool() {
|
|
584
|
+
const pool = readPool()
|
|
585
|
+
if (!pool?.deviceId) {
|
|
586
|
+
poolInfo = { status: '未接入', tone: 'muted', hostname: '', lastError: '' }
|
|
587
|
+
return
|
|
588
|
+
}
|
|
589
|
+
void (async () => {
|
|
590
|
+
try {
|
|
591
|
+
await ensureCloudflared()
|
|
592
|
+
await startTunnel()
|
|
593
|
+
startPoolTimers()
|
|
594
|
+
await heartbeat()
|
|
595
|
+
} catch (error) {
|
|
596
|
+
poolInfo = { status: '接入异常', tone: 'bad', hostname: pool.hostname || '', lastError: human(error) }
|
|
597
|
+
}
|
|
598
|
+
})()
|
|
599
|
+
}
|
|
600
|
+
|
|
311
601
|
async function buildState() {
|
|
312
602
|
const snap = await snapshot()
|
|
313
603
|
const cfg = fs.existsSync(CFG) ? readConfig() : {}
|
|
604
|
+
const pool = poolSnapshot()
|
|
314
605
|
return {
|
|
315
606
|
...snap,
|
|
316
607
|
running: Boolean(await health()),
|
|
@@ -325,10 +616,14 @@ async function buildState() {
|
|
|
325
616
|
autoError: lastAutoError,
|
|
326
617
|
version: VERSION,
|
|
327
618
|
time: Date.now(),
|
|
619
|
+
poolStatus: pool.status,
|
|
620
|
+
poolTone: pool.tone,
|
|
621
|
+
poolHost: pool.hostname,
|
|
622
|
+
poolError: pool.lastError,
|
|
328
623
|
}
|
|
329
624
|
}
|
|
330
625
|
|
|
331
|
-
async function runAction(action) {
|
|
626
|
+
async function runAction(action, body = {}) {
|
|
332
627
|
switch (action) {
|
|
333
628
|
case 'start':
|
|
334
629
|
desired = true
|
|
@@ -348,6 +643,13 @@ async function runAction(action) {
|
|
|
348
643
|
const ok = await start()
|
|
349
644
|
return ok ? (action === 'listenLocal' ? '已仅本机监听' : '已开局域网') : '切换失败'
|
|
350
645
|
}
|
|
646
|
+
case 'joinPool':
|
|
647
|
+
return await joinPool(body)
|
|
648
|
+
case 'leavePool':
|
|
649
|
+
return await leavePool()
|
|
650
|
+
case 'poolSync':
|
|
651
|
+
await heartbeat()
|
|
652
|
+
return poolInfo.status
|
|
351
653
|
case 'autoConfig':
|
|
352
654
|
await autoConfigure(true)
|
|
353
655
|
return autoInfo.status
|
|
@@ -406,7 +708,7 @@ async function api(req, res) {
|
|
|
406
708
|
if (method === 'POST' && route === '/action') {
|
|
407
709
|
if (!local) return send(403, { error: '仅本机可操作' })
|
|
408
710
|
const body = await readJson(req)
|
|
409
|
-
const toast = await runAction(String(body.action || ''))
|
|
711
|
+
const toast = await runAction(String(body.action || ''), body)
|
|
410
712
|
return send(200, { ok: true, toast, state: await buildState() })
|
|
411
713
|
}
|
|
412
714
|
return send(404, { error: 'not found' })
|
|
@@ -848,6 +1150,8 @@ function registerUi(ctx) {
|
|
|
848
1150
|
{ label: '局域网', value: { $: 'data.lanUrl' } },
|
|
849
1151
|
{ label: '模型', value: { $: 'data.models' } },
|
|
850
1152
|
{ label: '模型商', value: { $: 'data.provider' }, tone: { $: 'data.providerTone' } },
|
|
1153
|
+
{ label: '设备池', value: { $: 'data.poolStatus' }, tone: { $: 'data.poolTone' } },
|
|
1154
|
+
{ label: '池地址', value: { $: 'data.poolHost' } },
|
|
851
1155
|
{ label: '平台', value: { $: 'data.platform' } },
|
|
852
1156
|
],
|
|
853
1157
|
},
|
|
@@ -875,6 +1179,13 @@ function registerUi(ctx) {
|
|
|
875
1179
|
{ label: '重装', action: 'reinstall' },
|
|
876
1180
|
],
|
|
877
1181
|
},
|
|
1182
|
+
{
|
|
1183
|
+
type: 'actions',
|
|
1184
|
+
items: [
|
|
1185
|
+
{ label: '退出设备池', action: 'leavePool' },
|
|
1186
|
+
{ label: '立即心跳', action: 'poolSync' },
|
|
1187
|
+
],
|
|
1188
|
+
},
|
|
878
1189
|
{
|
|
879
1190
|
type: 'actions',
|
|
880
1191
|
items: [
|
|
@@ -1006,6 +1317,12 @@ function registerUi(ctx) {
|
|
|
1006
1317
|
await autoConfigure(true)
|
|
1007
1318
|
return { toast: autoInfo.tone === 'good' ? '模型商已配置' : autoInfo.status }
|
|
1008
1319
|
}),
|
|
1320
|
+
leavePool: () => pendingWork('正在退出', async () => ({ toast: await leavePool() })),
|
|
1321
|
+
poolSync: () =>
|
|
1322
|
+
pendingWork('正在同步', async () => {
|
|
1323
|
+
await heartbeat()
|
|
1324
|
+
return { toast: poolInfo.status }
|
|
1325
|
+
}),
|
|
1009
1326
|
},
|
|
1010
1327
|
})
|
|
1011
1328
|
|
|
@@ -1038,11 +1355,15 @@ export function apply(ctx) {
|
|
|
1038
1355
|
const bootTimer = setTimeout(() => {
|
|
1039
1356
|
void boot()
|
|
1040
1357
|
}, 1500)
|
|
1358
|
+
const poolTimer = setTimeout(() => {
|
|
1359
|
+
bootPool()
|
|
1360
|
+
}, 2500)
|
|
1041
1361
|
const watchdog = setInterval(() => {
|
|
1042
1362
|
if (desired && !busy) void boot()
|
|
1043
1363
|
}, 30000)
|
|
1044
1364
|
return () => {
|
|
1045
1365
|
clearTimeout(bootTimer)
|
|
1366
|
+
clearTimeout(poolTimer)
|
|
1046
1367
|
clearInterval(watchdog)
|
|
1047
1368
|
}
|
|
1048
1369
|
}, 'opencode2api autostart')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-opencode2api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Unofficial one-click installer and auto-runner for opencode2api — a local free OpenAI-compatible API gateway — inside DSH, with a desktop settings UI and a dsh-go native panel (Windows, macOS, Linux, Android).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|