dsh-plugin-opencode2api 1.2.0 → 1.2.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.
Files changed (3) hide show
  1. package/README.md +19 -6
  2. package/lib/index.js +184 -40
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,6 +2,19 @@
2
2
 
3
3
  一键把 [opencode2api](https://github.com/jasonxu114514/opencode2api) 装进 DSH:自动下载对应平台的程序、生成配置、启动并常驻,并自动把「本地免费 API」注册成 DSH 的模型商(模型列表从网关动态获取)。装好后本机就有一个专属的、兼容 OpenAI 的免费 API;DSH 关闭再打开会自动重启。
4
4
 
5
+ ## 1.2.2 更新
6
+
7
+ - 手机原生面板与桌面设置页对齐:移除「复制接入」「复制后台」,改为「复制地址」「复制 Key」;保留「打开后台」「查看日志」「项目主页」。
8
+ - 「打开后台」在 dshGo 原生面板受平台限制:`link` 只接受 https,本地后台是 `http://127.0.0.1:8081`,因此会自动复制地址并提示粘贴到浏览器;桌面 Web UI 仍可直接打开。
9
+ - 停止/重启在 Windows 之外补充 `ss` / `netstat` 兜底,Linux/macOS/Android 按端口找进程更可靠。
10
+
11
+ ## 1.2.1 更新
12
+
13
+ - 修复停止/重启:按端口反查并终止进程(Windows `netstat` + `taskkill`,macOS/Linux/Android 用 `lsof`/`fuser`/`/proc`),不再依赖可能丢失的 pid 文件;启动改为健康检查通过才算成功,并自动接管已有健康实例。
14
+ - 手机面板补齐「打开后台」「查看日志」「项目主页」,与桌面设置页功能一致。
15
+ - 推荐模型改为 `big-pickle`(`deepseek-v4.1-flash-free` 更易触发上游匿名通道限流)。
16
+ - 内置 opencode2api 版本仍固定为 v1.5.1;可用环境变量 `OPENCODE2API_VERSION` 覆盖后重装。
17
+
5
18
  ## 适用平台
6
19
 
7
20
  | 平台 | 架构 | 状态 |
@@ -25,8 +38,8 @@ dsh plugin --profile web add dsh-plugin-opencode2api
25
38
  | 方式 | 命令 | 前提 |
26
39
  |---|---|---|
27
40
  | 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` |
41
+ | 本地包 | `dsh plugin --profile web add ./dsh-plugin-opencode2api-1.2.2.tgz` | 有 17 KB 的 tgz |
42
+ | 局域网 | `dsh plugin --profile web add http://<手机IP>:8000/dsh-plugin-opencode2api-1.2.2.tgz` | 手机先 `python3 -m http.server 8000` |
30
43
 
31
44
  装好后引擎会自动下载程序(约 5 MB)、生成配置并在 `0.0.0.0:8080` 启动(本机和局域网都能连)。首次下载失败会每 30 秒重试。
32
45
 
@@ -39,13 +52,13 @@ Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1
39
52
  桌面端在 **设置 → 本地免费 API** 里操作;手机端在「本地API」面板里操作。面板会显示「模型商:已自动配置」。如果没配上,点面板里的「自动配置」重试;仍然失败时再手动接入:
40
53
 
41
54
  1. 打开「本地免费 API」面板。
42
- 2. 点「复制接入」,得到:
55
+ 2. 点「复制地址」得到本机/局域网地址,点「复制 Key」得到 API Key:
43
56
 
44
57
  ```text
45
58
  本机:http://127.0.0.1:8080/v1
46
59
  局域网:http://<你的局域网IP>:8080/v1
47
60
  API Key:<随机生成>
48
- 推荐模型:deepseek-v4.1-flash-free
61
+ 推荐模型:big-pickle
49
62
  ```
50
63
 
51
64
  3. 在 DSH 里添加 OpenAI 兼容服务商,Base URL 填上面的地址,API Key 填生成的 Key。
@@ -54,9 +67,9 @@ Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1
54
67
 
55
68
  - **启动 / 停止 / 重启 / 重装**:重装会重新下载程序。
56
69
  - **仅本机 / 开局域网**:切换监听地址(`127.0.0.1` 或 `0.0.0.0`)。
57
- - **复制地址 / 复制 Key / 复制后台信息**:桌面设置页有复制按钮;手机面板有「复制接入 / 复制后台」。
70
+ - **复制地址 / 复制 Key / 打开后台**:桌面设置页与手机面板都提供(手机原生面板的「打开后台」受 https 限制,会自动复制地址)。
58
71
  - **自动配置**:重新把本地 API 注册为模型商「本地免费 API」,并同步最新模型列表。
59
- - **运行日志**:桌面设置页可展开查看最后 40 行;手机看数据目录里的 `opencode2api.log`。
72
+ - **运行日志**:桌面设置页可展开查看最后 40 行;手机面板新增「查看日志」,也可看数据目录里的 `opencode2api.log`。
60
73
  - 状态实时显示;进程挂掉会自动拉起。
61
74
 
62
75
  ## 界面说明
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,125 @@ function alive(pid) {
575
574
  }
576
575
  }
577
576
 
577
+ function execLines(command, args, timeout = 3000) {
578
+ return new Promise((resolve) => {
579
+ const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'ignore'] })
580
+ let out = ''
581
+ let done = false
582
+ let timer
583
+ const finish = (value) => {
584
+ if (done) return
585
+ done = true
586
+ clearTimeout(timer)
587
+ resolve(value)
588
+ }
589
+ timer = setTimeout(() => {
590
+ try { child.kill() } catch {}
591
+ finish(out)
592
+ }, timeout)
593
+ child.stdout?.on('data', (chunk) => { out += chunk })
594
+ child.once('error', () => finish(''))
595
+ child.once('close', () => finish(out))
596
+ })
597
+ }
598
+
599
+ function pidsFromProc(port) {
600
+ const result = []
601
+ try {
602
+ const hex = port.toString(16).toUpperCase().padStart(4, '0')
603
+ const inodes = new Set()
604
+ for (const file of ['/proc/net/tcp', '/proc/net/tcp6']) {
605
+ for (const line of fs.readFileSync(file, 'utf8').split(/\r?\n/).slice(1)) {
606
+ const cols = line.trim().split(/\s+/)
607
+ if (cols.length < 10 || cols[3] !== '0A') continue
608
+ if (String(cols[1] || '').split(':')[1]?.toUpperCase() !== hex) continue
609
+ inodes.add(cols[9])
610
+ }
611
+ }
612
+ if (!inodes.size) return result
613
+ for (const dir of fs.readdirSync('/proc')) {
614
+ if (!/^\d+$/.test(dir)) continue
615
+ try {
616
+ for (const fd of fs.readdirSync(`/proc/${dir}/fd`)) {
617
+ let link = ''
618
+ try { link = fs.readlinkSync(`/proc/${dir}/fd/${fd}`) } catch {}
619
+ const match = /^socket:\[(\d+)\]$/.exec(link)
620
+ if (match && inodes.has(match[1])) {
621
+ result.push(Number(dir))
622
+ break
623
+ }
624
+ }
625
+ } catch {}
626
+ }
627
+ } catch {}
628
+ return result
629
+ }
630
+
631
+ async function listenerPids(port) {
632
+ const found = new Set()
633
+ if (IS_WIN) {
634
+ const text = await execLines('netstat', ['-ano', '-p', 'tcp'])
635
+ for (const line of text.split(/\r?\n/)) {
636
+ const cols = line.trim().split(/\s+/)
637
+ if (cols.length < 5 || cols[3] !== 'LISTENING') continue
638
+ if (!String(cols[1] || '').endsWith(`:${port}`)) continue
639
+ const pid = Number(cols[4])
640
+ if (pid) found.add(pid)
641
+ }
642
+ } else {
643
+ const lsof = await execLines('lsof', ['-tiTCP:' + port, '-sTCP:LISTEN'])
644
+ for (const line of lsof.split(/\r?\n/)) {
645
+ const pid = Number(line.trim())
646
+ if (pid) found.add(pid)
647
+ }
648
+ if (!found.size) {
649
+ const ss = await execLines('ss', ['-ltnp'])
650
+ for (const line of ss.split(/\r?\n/)) {
651
+ if (!new RegExp(`:${port}\\b`).test(line)) continue
652
+ for (const match of line.matchAll(/pid=(\d+)/g)) found.add(Number(match[1]))
653
+ }
654
+ }
655
+ if (!found.size) {
656
+ const netstat = await execLines('netstat', ['-ltnp'])
657
+ for (const line of netstat.split(/\r?\n/)) {
658
+ if (!/LISTEN/.test(line) || !new RegExp(`:${port}\\b`).test(line)) continue
659
+ const last = line.trim().split(/\s+/).pop() || ''
660
+ const pid = Number((last.match(/^(\d+)\//) || last.match(/^(\d+)$/) || [])[1])
661
+ if (pid) found.add(pid)
662
+ }
663
+ }
664
+ if (!found.size) {
665
+ const fuser = await execLines('fuser', ['-n', 'tcp', String(port)])
666
+ for (const token of fuser.split(/\s+/)) {
667
+ const pid = Number(token.trim())
668
+ if (pid) found.add(pid)
669
+ }
670
+ }
671
+ if (!found.size) for (const pid of pidsFromProc(port)) found.add(pid)
672
+ }
673
+ found.delete(process.pid)
674
+ return [...found]
675
+ }
676
+
677
+ async function terminatePid(pid) {
678
+ if (!pid || pid === process.pid) return
679
+ if (IS_WIN) {
680
+ await execLines('taskkill', ['/PID', String(pid), '/T', '/F'])
681
+ } else {
682
+ try { process.kill(pid, 'SIGTERM') } catch {}
683
+ for (let i = 0; i < 12 && alive(pid); i++) await sleep(250)
684
+ if (alive(pid)) {
685
+ try { process.kill(pid, 'SIGKILL') } catch {}
686
+ }
687
+ }
688
+ }
689
+
690
+ function writePid(pid) {
691
+ try {
692
+ fs.writeFileSync(PID, String(pid))
693
+ } catch {}
694
+ }
695
+
578
696
  async function health() {
579
697
  try {
580
698
  const res = await fetch(`http://127.0.0.1:${API_PORT}/healthz`, { signal: AbortSignal.timeout(1500) })
@@ -589,7 +707,12 @@ async function health() {
589
707
  async function start() {
590
708
  if (!fs.existsSync(BIN)) await ensureInstalled()
591
709
  ensureConfig()
592
- if (await portOpen()) return true
710
+ if (await health()) {
711
+ const owners = await listenerPids(API_PORT)
712
+ if (owners.length === 1) writePid(owners[0])
713
+ return true
714
+ }
715
+ if ((await portOpen()) || (await portOpen(WEB_PORT))) await killProcess()
593
716
  const fd = fs.openSync(LOG, 'a')
594
717
  let child
595
718
  try {
@@ -597,39 +720,37 @@ async function start() {
597
720
  } finally {
598
721
  fs.closeSync(fd)
599
722
  }
723
+ child.once('error', () => {})
600
724
  child.unref()
601
- try {
602
- fs.writeFileSync(PID, String(child.pid))
603
- } catch {}
604
- for (let i = 0; i < 40; i++) {
725
+ writePid(child.pid)
726
+ for (let i = 0; i < 60; i++) {
605
727
  await sleep(500)
606
- if (await portOpen()) return true
607
- if (!alive(child.pid) && i > 3) break
728
+ if (await health()) {
729
+ const owners = await listenerPids(API_PORT)
730
+ if (owners.length === 1) writePid(owners[0])
731
+ return true
732
+ }
733
+ if (i > 3 && !alive(child.pid)) break
608
734
  }
609
- return portOpen()
735
+ return Boolean(await health())
610
736
  }
611
737
 
612
738
  async function killProcess() {
739
+ const targets = new Set()
613
740
  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
- }
741
+ if (pid) targets.add(pid)
742
+ for (const port of [API_PORT, WEB_PORT]) {
743
+ for (const owner of await listenerPids(port)) targets.add(owner)
624
744
  }
745
+ for (const target of targets) await terminatePid(target)
625
746
  try {
626
747
  fs.unlinkSync(PID)
627
748
  } catch {}
628
- for (let i = 0; i < 12; i++) {
629
- if (!(await portOpen())) return true
749
+ for (let i = 0; i < 20; i++) {
750
+ if (!(await portOpen()) && !(await portOpen(WEB_PORT))) return true
630
751
  await sleep(250)
631
752
  }
632
- return !(await portOpen())
753
+ return !(await portOpen()) && !(await portOpen(WEB_PORT))
633
754
  }
634
755
 
635
756
  async function stop() {
@@ -748,12 +869,20 @@ function registerUi(ctx) {
748
869
  {
749
870
  type: 'actions',
750
871
  items: [
751
- { label: '复制接入', action: 'copyInfo' },
752
- { label: '复制后台', action: 'copyWeb' },
872
+ { label: '复制地址', action: 'copyBase' },
873
+ { label: '复制Key', action: 'copyKey' },
753
874
  { label: '自动配置', action: 'autoConfig' },
754
875
  { label: '重装', action: 'reinstall' },
755
876
  ],
756
877
  },
878
+ {
879
+ type: 'actions',
880
+ items: [
881
+ { label: '打开后台', action: 'openWeb' },
882
+ { label: '查看日志', action: 'viewLog' },
883
+ { label: '项目主页', action: 'openHome' },
884
+ ],
885
+ },
757
886
  { type: 'callout', text: { $: 'data.note' }, tone: { $: 'data.noteTone' } },
758
887
  ],
759
888
  }),
@@ -822,22 +951,19 @@ function registerUi(ctx) {
822
951
  const ok = await start()
823
952
  return { toast: ok ? '已开局域网' : '切换失败' }
824
953
  }),
825
- copyInfo: () => {
826
- const cfg = readConfig()
827
- const key = cfg.server_keys?.[0] || ''
954
+ copyBase: () => {
828
955
  const lan = lanAddress()
829
956
  const lanLine = lan ? `\n局域网:http://${lan}:${API_PORT}/v1` : ''
830
957
  return {
831
- copy: `本机:http://127.0.0.1:${API_PORT}/v1${lanLine}\nAPI Key:${key}\n推荐模型:deepseek-v4.1-flash-free`,
832
- toast: '接入信息已复制',
958
+ copy: `本机:http://127.0.0.1:${API_PORT}/v1${lanLine}`,
959
+ toast: 'API 地址已复制',
833
960
  }
834
961
  },
835
- copyWeb: () => {
962
+ copyKey: () => {
836
963
  const cfg = readConfig()
837
- const password = readPassword() || '(已在后台改过,请用新密码)'
838
964
  return {
839
- copy: `后台:http://127.0.0.1:${WEB_PORT}\n账号:${cfg.webui?.username || 'admin'}\n密码:${password}`,
840
- toast: '后台信息已复制',
965
+ copy: cfg.server_keys?.[0] || '(无 Key)',
966
+ toast: 'API Key 已复制',
841
967
  }
842
968
  },
843
969
  reinstall: () => {
@@ -857,6 +983,24 @@ function registerUi(ctx) {
857
983
  })()
858
984
  return { toast: '后台重装中…' }
859
985
  },
986
+ openWeb: () => {
987
+ const url = `http://127.0.0.1:${WEB_PORT}`
988
+ const effect = { copy: url, toast: '后台地址已复制,请粘贴到浏览器打开' }
989
+ if (/^https:\/\//i.test(url)) {
990
+ effect.link = url
991
+ effect.toast = '正在打开后台'
992
+ }
993
+ return effect
994
+ },
995
+ viewLog: () => ({
996
+ copy: tailLog(40) || '(暂无日志)',
997
+ toast: '日志已复制',
998
+ }),
999
+ openHome: () => ({
1000
+ copy: 'https://github.com/king-andthe-nice/dsh-plugin-opencode2api',
1001
+ link: 'https://github.com/king-andthe-nice/dsh-plugin-opencode2api',
1002
+ toast: '项目地址已复制',
1003
+ }),
860
1004
  autoConfig: () =>
861
1005
  pendingWork('正在配置', async () => {
862
1006
  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.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",