dsh-plugin-opencode2api 1.2.0 → 1.2.1

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 (3) hide show
  1. package/README.md +12 -5
  2. package/lib/index.js +145 -30
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  一键把 [opencode2api](https://github.com/jasonxu114514/opencode2api) 装进 DSH:自动下载对应平台的程序、生成配置、启动并常驻,并自动把「本地免费 API」注册成 DSH 的模型商(模型列表从网关动态获取)。装好后本机就有一个专属的、兼容 OpenAI 的免费 API;DSH 关闭再打开会自动重启。
4
4
 
5
+ ## 1.2.1 更新
6
+
7
+ - 修复停止/重启:按端口反查并终止进程(Windows `netstat` + `taskkill`,macOS/Linux/Android 用 `lsof`/`fuser`/`/proc`),不再依赖可能丢失的 pid 文件;启动改为健康检查通过才算成功,并自动接管已有健康实例。
8
+ - 手机面板补齐「打开后台」「查看日志」「项目主页」,与桌面设置页功能一致。
9
+ - 推荐模型改为 `big-pickle`(`deepseek-v4.1-flash-free` 更易触发上游匿名通道限流)。
10
+ - 内置 opencode2api 版本仍固定为 v1.5.1;可用环境变量 `OPENCODE2API_VERSION` 覆盖后重装。
11
+
5
12
  ## 适用平台
6
13
 
7
14
  | 平台 | 架构 | 状态 |
@@ -25,8 +32,8 @@ dsh plugin --profile web add dsh-plugin-opencode2api
25
32
  | 方式 | 命令 | 前提 |
26
33
  |---|---|---|
27
34
  | GitHub | `dsh plugin --profile web add github:king-andthe-nice/dsh-plugin-opencode2api` | 能访问 GitHub |
28
- | 本地包 | `dsh plugin --profile web add ./dsh-plugin-opencode2api-1.2.0.tgz` | 有 17 KB 的 tgz |
29
- | 局域网 | `dsh plugin --profile web add http://<手机IP>:8000/dsh-plugin-opencode2api-1.2.0.tgz` | 手机先 `python3 -m http.server 8000` |
35
+ | 本地包 | `dsh plugin --profile web add ./dsh-plugin-opencode2api-1.2.1.tgz` | 有 17 KB 的 tgz |
36
+ | 局域网 | `dsh plugin --profile web add http://<手机IP>:8000/dsh-plugin-opencode2api-1.2.1.tgz` | 手机先 `python3 -m http.server 8000` |
30
37
 
31
38
  装好后引擎会自动下载程序(约 5 MB)、生成配置并在 `0.0.0.0:8080` 启动(本机和局域网都能连)。首次下载失败会每 30 秒重试。
32
39
 
@@ -45,7 +52,7 @@ Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1
45
52
  本机:http://127.0.0.1:8080/v1
46
53
  局域网:http://<你的局域网IP>:8080/v1
47
54
  API Key:<随机生成>
48
- 推荐模型:deepseek-v4.1-flash-free
55
+ 推荐模型:big-pickle
49
56
  ```
50
57
 
51
58
  3. 在 DSH 里添加 OpenAI 兼容服务商,Base URL 填上面的地址,API Key 填生成的 Key。
@@ -54,9 +61,9 @@ Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1
54
61
 
55
62
  - **启动 / 停止 / 重启 / 重装**:重装会重新下载程序。
56
63
  - **仅本机 / 开局域网**:切换监听地址(`127.0.0.1` 或 `0.0.0.0`)。
57
- - **复制地址 / 复制 Key / 复制后台信息**:桌面设置页有复制按钮;手机面板有「复制接入 / 复制后台」。
64
+ - **复制地址 / 复制 Key / 复制后台信息 / 打开后台**:桌面设置页与手机面板都提供。
58
65
  - **自动配置**:重新把本地 API 注册为模型商「本地免费 API」,并同步最新模型列表。
59
- - **运行日志**:桌面设置页可展开查看最后 40 行;手机看数据目录里的 `opencode2api.log`。
66
+ - **运行日志**:桌面设置页可展开查看最后 40 行;手机面板新增「查看日志」,也可看数据目录里的 `opencode2api.log`。
60
67
  - 状态实时显示;进程挂掉会自动拉起。
61
68
 
62
69
  ## 界面说明
package/lib/index.js CHANGED
@@ -332,15 +332,14 @@ async function runAction(action) {
332
332
  switch (action) {
333
333
  case 'start':
334
334
  desired = true
335
- await boot()
336
- return '已启动'
335
+ return (await start()) ? '已启动' : '启动失败,看日志'
337
336
  case 'stop':
338
- await stop()
339
- return '已停止'
340
- case 'restart':
341
- await restart()
342
- void autoConfigure().catch(() => {})
343
- return '已重启'
337
+ return (await stop()) ? '已停止' : '停止失败'
338
+ case 'restart': {
339
+ const ok = await restart()
340
+ if (ok) void autoConfigure().catch(() => {})
341
+ return ok ? '已重启' : '重启失败,看日志'
342
+ }
344
343
  case 'listenLocal':
345
344
  case 'listenLan': {
346
345
  setListen(action === 'listenLocal' ? '127.0.0.1' : '0.0.0.0')
@@ -575,6 +574,97 @@ function alive(pid) {
575
574
  }
576
575
  }
577
576
 
577
+ function execLines(command, args) {
578
+ return new Promise((resolve) => {
579
+ const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'ignore'] })
580
+ let out = ''
581
+ child.stdout?.on('data', (chunk) => { out += chunk })
582
+ child.once('error', () => resolve(''))
583
+ child.once('close', () => resolve(out))
584
+ })
585
+ }
586
+
587
+ function pidsFromProc(port) {
588
+ const result = []
589
+ try {
590
+ const hex = port.toString(16).toUpperCase().padStart(4, '0')
591
+ const inodes = new Set()
592
+ for (const file of ['/proc/net/tcp', '/proc/net/tcp6']) {
593
+ for (const line of fs.readFileSync(file, 'utf8').split(/\r?\n/).slice(1)) {
594
+ const cols = line.trim().split(/\s+/)
595
+ if (cols.length < 10 || cols[3] !== '0A') continue
596
+ if (String(cols[1] || '').split(':')[1]?.toUpperCase() !== hex) continue
597
+ inodes.add(cols[9])
598
+ }
599
+ }
600
+ if (!inodes.size) return result
601
+ for (const dir of fs.readdirSync('/proc')) {
602
+ if (!/^\d+$/.test(dir)) continue
603
+ try {
604
+ for (const fd of fs.readdirSync(`/proc/${dir}/fd`)) {
605
+ let link = ''
606
+ try { link = fs.readlinkSync(`/proc/${dir}/fd/${fd}`) } catch {}
607
+ const match = /^socket:\[(\d+)\]$/.exec(link)
608
+ if (match && inodes.has(match[1])) {
609
+ result.push(Number(dir))
610
+ break
611
+ }
612
+ }
613
+ } catch {}
614
+ }
615
+ } catch {}
616
+ return result
617
+ }
618
+
619
+ async function listenerPids(port) {
620
+ const found = new Set()
621
+ if (IS_WIN) {
622
+ const text = await execLines('netstat', ['-ano', '-p', 'tcp'])
623
+ for (const line of text.split(/\r?\n/)) {
624
+ const cols = line.trim().split(/\s+/)
625
+ if (cols.length < 5 || cols[3] !== 'LISTENING') continue
626
+ if (!String(cols[1] || '').endsWith(`:${port}`)) continue
627
+ const pid = Number(cols[4])
628
+ if (pid) found.add(pid)
629
+ }
630
+ } else {
631
+ const lsof = await execLines('lsof', ['-tiTCP:' + port, '-sTCP:LISTEN'])
632
+ for (const line of lsof.split(/\r?\n/)) {
633
+ const pid = Number(line.trim())
634
+ if (pid) found.add(pid)
635
+ }
636
+ if (!found.size) {
637
+ const fuser = await execLines('fuser', ['-n', 'tcp', String(port)])
638
+ for (const token of fuser.split(/\s+/)) {
639
+ const pid = Number(token.trim())
640
+ if (pid) found.add(pid)
641
+ }
642
+ }
643
+ if (!found.size) for (const pid of pidsFromProc(port)) found.add(pid)
644
+ }
645
+ found.delete(process.pid)
646
+ return [...found]
647
+ }
648
+
649
+ async function terminatePid(pid) {
650
+ if (!pid || pid === process.pid) return
651
+ if (IS_WIN) {
652
+ await execLines('taskkill', ['/PID', String(pid), '/T', '/F'])
653
+ } else {
654
+ try { process.kill(pid, 'SIGTERM') } catch {}
655
+ for (let i = 0; i < 12 && alive(pid); i++) await sleep(250)
656
+ if (alive(pid)) {
657
+ try { process.kill(pid, 'SIGKILL') } catch {}
658
+ }
659
+ }
660
+ }
661
+
662
+ function writePid(pid) {
663
+ try {
664
+ fs.writeFileSync(PID, String(pid))
665
+ } catch {}
666
+ }
667
+
578
668
  async function health() {
579
669
  try {
580
670
  const res = await fetch(`http://127.0.0.1:${API_PORT}/healthz`, { signal: AbortSignal.timeout(1500) })
@@ -589,7 +679,12 @@ async function health() {
589
679
  async function start() {
590
680
  if (!fs.existsSync(BIN)) await ensureInstalled()
591
681
  ensureConfig()
592
- if (await portOpen()) return true
682
+ if (await health()) {
683
+ const owners = await listenerPids(API_PORT)
684
+ if (owners.length === 1) writePid(owners[0])
685
+ return true
686
+ }
687
+ if ((await portOpen()) || (await portOpen(WEB_PORT))) await killProcess()
593
688
  const fd = fs.openSync(LOG, 'a')
594
689
  let child
595
690
  try {
@@ -597,39 +692,37 @@ async function start() {
597
692
  } finally {
598
693
  fs.closeSync(fd)
599
694
  }
695
+ child.once('error', () => {})
600
696
  child.unref()
601
- try {
602
- fs.writeFileSync(PID, String(child.pid))
603
- } catch {}
604
- for (let i = 0; i < 40; i++) {
697
+ writePid(child.pid)
698
+ for (let i = 0; i < 60; i++) {
605
699
  await sleep(500)
606
- if (await portOpen()) return true
607
- if (!alive(child.pid) && i > 3) break
700
+ if (await health()) {
701
+ const owners = await listenerPids(API_PORT)
702
+ if (owners.length === 1) writePid(owners[0])
703
+ return true
704
+ }
705
+ if (i > 3 && !alive(child.pid)) break
608
706
  }
609
- return portOpen()
707
+ return Boolean(await health())
610
708
  }
611
709
 
612
710
  async function killProcess() {
711
+ const targets = new Set()
613
712
  const pid = readPid()
614
- if (pid && alive(pid)) {
615
- try {
616
- process.kill(pid, 'SIGTERM')
617
- } catch {}
618
- for (let i = 0; i < 24 && alive(pid); i++) await sleep(250)
619
- if (alive(pid)) {
620
- try {
621
- process.kill(pid, 'SIGKILL')
622
- } catch {}
623
- }
713
+ if (pid) targets.add(pid)
714
+ for (const port of [API_PORT, WEB_PORT]) {
715
+ for (const owner of await listenerPids(port)) targets.add(owner)
624
716
  }
717
+ for (const target of targets) await terminatePid(target)
625
718
  try {
626
719
  fs.unlinkSync(PID)
627
720
  } catch {}
628
- for (let i = 0; i < 12; i++) {
629
- if (!(await portOpen())) return true
721
+ for (let i = 0; i < 20; i++) {
722
+ if (!(await portOpen()) && !(await portOpen(WEB_PORT))) return true
630
723
  await sleep(250)
631
724
  }
632
- return !(await portOpen())
725
+ return !(await portOpen()) && !(await portOpen(WEB_PORT))
633
726
  }
634
727
 
635
728
  async function stop() {
@@ -754,6 +847,14 @@ function registerUi(ctx) {
754
847
  { label: '重装', action: 'reinstall' },
755
848
  ],
756
849
  },
850
+ {
851
+ type: 'actions',
852
+ items: [
853
+ { label: '打开后台', action: 'openWeb', style: 'primary' },
854
+ { label: '查看日志', action: 'viewLog' },
855
+ { label: '项目主页', action: 'openHome' },
856
+ ],
857
+ },
757
858
  { type: 'callout', text: { $: 'data.note' }, tone: { $: 'data.noteTone' } },
758
859
  ],
759
860
  }),
@@ -828,7 +929,7 @@ function registerUi(ctx) {
828
929
  const lan = lanAddress()
829
930
  const lanLine = lan ? `\n局域网:http://${lan}:${API_PORT}/v1` : ''
830
931
  return {
831
- copy: `本机:http://127.0.0.1:${API_PORT}/v1${lanLine}\nAPI Key:${key}\n推荐模型:deepseek-v4.1-flash-free`,
932
+ copy: `本机:http://127.0.0.1:${API_PORT}/v1${lanLine}\nAPI Key:${key}\n推荐模型:big-pickle`,
832
933
  toast: '接入信息已复制',
833
934
  }
834
935
  },
@@ -857,6 +958,20 @@ function registerUi(ctx) {
857
958
  })()
858
959
  return { toast: '后台重装中…' }
859
960
  },
961
+ openWeb: () => ({
962
+ copy: `http://127.0.0.1:${WEB_PORT}`,
963
+ open: `http://127.0.0.1:${WEB_PORT}`,
964
+ toast: '后台地址已复制,可粘贴到浏览器打开',
965
+ }),
966
+ viewLog: () => ({
967
+ copy: tailLog(40) || '(暂无日志)',
968
+ toast: '日志已复制',
969
+ }),
970
+ openHome: () => ({
971
+ copy: 'https://github.com/king-andthe-nice/dsh-plugin-opencode2api',
972
+ open: 'https://github.com/king-andthe-nice/dsh-plugin-opencode2api',
973
+ toast: '项目地址已复制',
974
+ }),
860
975
  autoConfig: () =>
861
976
  pendingWork('正在配置', async () => {
862
977
  await autoConfigure(true)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-opencode2api",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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",