dsh-plugin-opencode2api 1.1.0 → 1.2.0
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 +34 -8
- package/lib/client.js +468 -0
- package/lib/index.js +152 -0
- package/package.json +14 -3
package/README.md
CHANGED
|
@@ -14,21 +14,29 @@
|
|
|
14
14
|
|
|
15
15
|
## 安装
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
最方便的是 npm(已发布,国内镜像已同步):
|
|
18
18
|
|
|
19
19
|
```sh
|
|
20
|
-
dsh plugin --profile web add
|
|
20
|
+
dsh plugin --profile web add dsh-plugin-opencode2api
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
其他方式:
|
|
24
|
+
|
|
25
|
+
| 方式 | 命令 | 前提 |
|
|
26
|
+
|---|---|---|
|
|
27
|
+
| 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` |
|
|
24
30
|
|
|
25
31
|
装好后引擎会自动下载程序(约 5 MB)、生成配置并在 `0.0.0.0:8080` 启动(本机和局域网都能连)。首次下载失败会每 30 秒重试。
|
|
26
32
|
|
|
33
|
+
Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1:3080`)→ **设置 → 本地免费 API**,就能看到状态和操作按钮;手机 App 则在「本地API」胶囊/面板里。
|
|
34
|
+
|
|
27
35
|
## 使用
|
|
28
36
|
|
|
29
37
|
装好后插件会自动把本地 API 注册成模型商「本地免费 API」,模型列表从 `/v1/models` 动态获取。直接在 DSH 的模型选择器里选它下面的免费模型即可,不需要手动填 URL 和 Key。
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
桌面端在 **设置 → 本地免费 API** 里操作;手机端在「本地API」面板里操作。面板会显示「模型商:已自动配置」。如果没配上,点面板里的「自动配置」重试;仍然失败时再手动接入:
|
|
32
40
|
|
|
33
41
|
1. 打开「本地免费 API」面板。
|
|
34
42
|
2. 点「复制接入」,得到:
|
|
@@ -42,13 +50,31 @@ dsh plugin --profile web add github:king-andthe-nice/dsh-plugin-opencode2api
|
|
|
42
50
|
|
|
43
51
|
3. 在 DSH 里添加 OpenAI 兼容服务商,Base URL 填上面的地址,API Key 填生成的 Key。
|
|
44
52
|
|
|
45
|
-
##
|
|
53
|
+
## 功能(桌面设置页 / 手机面板)
|
|
46
54
|
|
|
47
55
|
- **启动 / 停止 / 重启 / 重装**:重装会重新下载程序。
|
|
48
56
|
- **仅本机 / 开局域网**:切换监听地址(`127.0.0.1` 或 `0.0.0.0`)。
|
|
49
|
-
-
|
|
57
|
+
- **复制地址 / 复制 Key / 复制后台信息**:桌面设置页有复制按钮;手机面板有「复制接入 / 复制后台」。
|
|
50
58
|
- **自动配置**:重新把本地 API 注册为模型商「本地免费 API」,并同步最新模型列表。
|
|
51
|
-
-
|
|
59
|
+
- **运行日志**:桌面设置页可展开查看最后 40 行;手机看数据目录里的 `opencode2api.log`。
|
|
60
|
+
- 状态实时显示;进程挂掉会自动拉起。
|
|
61
|
+
|
|
62
|
+
## 界面说明
|
|
63
|
+
|
|
64
|
+
- **Windows / macOS / Linux(DSH Web UI)**:**设置 → 本地免费 API**,有状态、启动/停止/重启、监听切换、复制地址/Key、自动配置、重装和日志。
|
|
65
|
+
- **dsh-go 手机 App**:有「本地API」胶囊、状态条和原生面板。
|
|
66
|
+
- dsh-go 原生界面(胶囊/状态条/面板)只存在于手机 App;桌面端用的是 DSH Web UI 的设置页,不是 dsh-go 原生面板。
|
|
67
|
+
|
|
68
|
+
### Windows 装完看不到界面?
|
|
69
|
+
|
|
70
|
+
1. 先确认插件已装:DSH Web UI → 插件 → **已安装**,应看到 `dsh-plugin-opencode2api`。
|
|
71
|
+
2. 打开 **设置 → 本地免费 API**(1.2.0 起提供桌面设置页)。1.1.0 只有手机原生面板,桌面没有界面。
|
|
72
|
+
3. 如果插件市场里标着「本地开发」,说明装的是本地链接;想用 npm 版先移除再装,然后重启 DSH:
|
|
73
|
+
|
|
74
|
+
```powershell
|
|
75
|
+
dsh plugin --profile web remove dsh-plugin-opencode2api
|
|
76
|
+
dsh plugin --profile web add dsh-plugin-opencode2api
|
|
77
|
+
```
|
|
52
78
|
|
|
53
79
|
## 自动配置
|
|
54
80
|
|
|
@@ -63,7 +89,7 @@ dsh plugin --profile web add github:king-andthe-nice/dsh-plugin-opencode2api
|
|
|
63
89
|
|
|
64
90
|
## 管理后台
|
|
65
91
|
|
|
66
|
-
浏览器打开 `http://127.0.0.1:8081`(或局域网地址),账号 `admin
|
|
92
|
+
浏览器打开 `http://127.0.0.1:8081`(或局域网地址),账号 `admin`,初始密码见桌面设置页(仅本机显示)或 `webui-password.txt`;后台改过后以新密码为准。后台可以改密码、查看请求、管理上游 Key 等。
|
|
67
93
|
|
|
68
94
|
## 数据目录
|
|
69
95
|
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-plugin-opencode2api',
|
|
3
|
+
factory: require => {
|
|
4
|
+
const module = { exports: {} }
|
|
5
|
+
const exports = module.exports
|
|
6
|
+
const React = require('react')
|
|
7
|
+
const { createElement: h, useCallback, useEffect, useState } = React
|
|
8
|
+
const inject = ['slots']
|
|
9
|
+
|
|
10
|
+
const TONE = {
|
|
11
|
+
good: 'var(--dsw-alias-state-success-primary)',
|
|
12
|
+
warn: 'var(--dsw-alias-state-warning-primary)',
|
|
13
|
+
bad: 'var(--dsw-alias-state-error-primary)',
|
|
14
|
+
info: 'var(--dsw-alias-state-business-primary)',
|
|
15
|
+
muted: 'var(--dsw-alias-label-tertiary)',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const CSS = `
|
|
19
|
+
.oc2a button:hover:not(:disabled) { filter: brightness(1.08); }
|
|
20
|
+
.oc2a code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; background: var(--dsw-alias-bg-layer-2); padding: 1px 4px; border-radius: 4px; }
|
|
21
|
+
.oc2a summary { user-select: none; }
|
|
22
|
+
`
|
|
23
|
+
|
|
24
|
+
const calloutStyle = {
|
|
25
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
26
|
+
borderRadius: 9,
|
|
27
|
+
padding: '9px 11px',
|
|
28
|
+
margin: '8px 0',
|
|
29
|
+
fontSize: 12,
|
|
30
|
+
lineHeight: 1.6,
|
|
31
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function request(path, init) {
|
|
35
|
+
const res = await fetch(`/api/opencode2api${path}`, init)
|
|
36
|
+
const text = await res.text()
|
|
37
|
+
let data = null
|
|
38
|
+
try {
|
|
39
|
+
data = text ? JSON.parse(text) : null
|
|
40
|
+
} catch {
|
|
41
|
+
data = null
|
|
42
|
+
}
|
|
43
|
+
if (!res.ok) throw new Error(data?.error || text || `HTTP ${res.status}`)
|
|
44
|
+
return data
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function useCopy() {
|
|
48
|
+
const [copied, setCopied] = useState('')
|
|
49
|
+
const copy = useCallback(async (label, value) => {
|
|
50
|
+
try {
|
|
51
|
+
await navigator.clipboard.writeText(value)
|
|
52
|
+
} catch {
|
|
53
|
+
const area = document.createElement('textarea')
|
|
54
|
+
area.value = value
|
|
55
|
+
document.body.appendChild(area)
|
|
56
|
+
area.select()
|
|
57
|
+
try {
|
|
58
|
+
document.execCommand('copy')
|
|
59
|
+
} catch {}
|
|
60
|
+
area.remove()
|
|
61
|
+
}
|
|
62
|
+
setCopied(label)
|
|
63
|
+
setTimeout(() => setCopied(''), 1500)
|
|
64
|
+
}, [])
|
|
65
|
+
return [copied, copy]
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function Button({ children, onClick, disabled, primary }) {
|
|
69
|
+
return h(
|
|
70
|
+
'button',
|
|
71
|
+
{
|
|
72
|
+
type: 'button',
|
|
73
|
+
onClick,
|
|
74
|
+
disabled,
|
|
75
|
+
style: {
|
|
76
|
+
appearance: 'none',
|
|
77
|
+
border: primary ? 'none' : '1px solid var(--dsw-alias-border-l1)',
|
|
78
|
+
background: primary
|
|
79
|
+
? 'var(--dsw-alias-state-business-primary)'
|
|
80
|
+
: 'var(--dsw-alias-bg-layer-2)',
|
|
81
|
+
color: primary
|
|
82
|
+
? 'var(--dsw-alias-label-on-accent)'
|
|
83
|
+
: 'var(--dsw-alias-label-primary)',
|
|
84
|
+
borderRadius: 8,
|
|
85
|
+
padding: '7px 12px',
|
|
86
|
+
font: 'inherit',
|
|
87
|
+
fontSize: 13,
|
|
88
|
+
cursor: disabled ? 'not-allowed' : 'pointer',
|
|
89
|
+
opacity: disabled ? 0.55 : 1,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
children,
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function Metric({ label, value, tone }) {
|
|
97
|
+
return h(
|
|
98
|
+
'div',
|
|
99
|
+
{
|
|
100
|
+
style: {
|
|
101
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
102
|
+
borderRadius: 10,
|
|
103
|
+
padding: '10px 12px',
|
|
104
|
+
background: 'var(--dsw-alias-bg-layer-1)',
|
|
105
|
+
minWidth: 0,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
h(
|
|
109
|
+
'div',
|
|
110
|
+
{
|
|
111
|
+
style: {
|
|
112
|
+
fontSize: 11,
|
|
113
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
114
|
+
marginBottom: 4,
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
label,
|
|
118
|
+
),
|
|
119
|
+
h(
|
|
120
|
+
'div',
|
|
121
|
+
{
|
|
122
|
+
style: {
|
|
123
|
+
fontSize: 13,
|
|
124
|
+
fontWeight: 600,
|
|
125
|
+
color: tone || 'var(--dsw-alias-label-primary)',
|
|
126
|
+
overflowWrap: 'anywhere',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
value,
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function Page() {
|
|
135
|
+
const [state, setState] = useState(null)
|
|
136
|
+
const [error, setError] = useState('')
|
|
137
|
+
const [busy, setBusy] = useState('')
|
|
138
|
+
const [copied, copy] = useCopy()
|
|
139
|
+
|
|
140
|
+
const load = useCallback(async () => {
|
|
141
|
+
try {
|
|
142
|
+
setState(await request('/state'))
|
|
143
|
+
setError('')
|
|
144
|
+
} catch (err) {
|
|
145
|
+
setError(String(err?.message || err))
|
|
146
|
+
}
|
|
147
|
+
}, [])
|
|
148
|
+
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
void load()
|
|
151
|
+
const timer = setInterval(load, 5000)
|
|
152
|
+
return () => clearInterval(timer)
|
|
153
|
+
}, [load])
|
|
154
|
+
|
|
155
|
+
const act = async action => {
|
|
156
|
+
setBusy(action)
|
|
157
|
+
setError('')
|
|
158
|
+
try {
|
|
159
|
+
const result = await request('/action', {
|
|
160
|
+
method: 'POST',
|
|
161
|
+
headers: { 'content-type': 'application/json' },
|
|
162
|
+
body: JSON.stringify({ action }),
|
|
163
|
+
})
|
|
164
|
+
if (result?.state) setState(result.state)
|
|
165
|
+
else void load()
|
|
166
|
+
} catch (err) {
|
|
167
|
+
setError(String(err?.message || err))
|
|
168
|
+
} finally {
|
|
169
|
+
setBusy('')
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const tone = state
|
|
174
|
+
? TONE[state.tone] || 'var(--dsw-alias-label-secondary)'
|
|
175
|
+
: 'var(--dsw-alias-label-secondary)'
|
|
176
|
+
const running = state?.running === true
|
|
177
|
+
const installed = state?.installed === true
|
|
178
|
+
const statusText = state
|
|
179
|
+
? state.state || (running ? '运行中' : installed ? '已停止' : '未安装')
|
|
180
|
+
: '读取中…'
|
|
181
|
+
const disabled = busy !== ''
|
|
182
|
+
const secretHidden = state?.keyHidden === true
|
|
183
|
+
|
|
184
|
+
return h(
|
|
185
|
+
'div',
|
|
186
|
+
{
|
|
187
|
+
className: 'oc2a',
|
|
188
|
+
style: {
|
|
189
|
+
padding: '18px 20px 28px',
|
|
190
|
+
maxWidth: 820,
|
|
191
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
h('style', null, CSS),
|
|
195
|
+
h(
|
|
196
|
+
'div',
|
|
197
|
+
{
|
|
198
|
+
style: {
|
|
199
|
+
display: 'flex',
|
|
200
|
+
alignItems: 'center',
|
|
201
|
+
gap: 10,
|
|
202
|
+
flexWrap: 'wrap',
|
|
203
|
+
marginBottom: 4,
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
h('div', { style: { fontSize: 18, fontWeight: 700 } }, '本地免费 API'),
|
|
207
|
+
h(
|
|
208
|
+
'span',
|
|
209
|
+
{
|
|
210
|
+
style: {
|
|
211
|
+
display: 'inline-flex',
|
|
212
|
+
alignItems: 'center',
|
|
213
|
+
gap: 6,
|
|
214
|
+
fontSize: 12,
|
|
215
|
+
color: tone,
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
h('span', {
|
|
219
|
+
style: {
|
|
220
|
+
width: 7,
|
|
221
|
+
height: 7,
|
|
222
|
+
borderRadius: 99,
|
|
223
|
+
background: tone,
|
|
224
|
+
},
|
|
225
|
+
}),
|
|
226
|
+
statusText,
|
|
227
|
+
),
|
|
228
|
+
),
|
|
229
|
+
h(
|
|
230
|
+
'div',
|
|
231
|
+
{
|
|
232
|
+
style: {
|
|
233
|
+
fontSize: 12,
|
|
234
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
235
|
+
marginBottom: 14,
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
'opencode2api · 自动下载、启动、掉线拉起;模型商自动配置,无需手动填 URL 和 Key。',
|
|
239
|
+
),
|
|
240
|
+
error
|
|
241
|
+
? h(
|
|
242
|
+
'div',
|
|
243
|
+
{
|
|
244
|
+
style: {
|
|
245
|
+
...calloutStyle,
|
|
246
|
+
borderColor: 'var(--dsw-alias-state-error-primary)',
|
|
247
|
+
color: 'var(--dsw-alias-state-error-primary)',
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
error,
|
|
251
|
+
)
|
|
252
|
+
: null,
|
|
253
|
+
state?.note
|
|
254
|
+
? h(
|
|
255
|
+
'div',
|
|
256
|
+
{
|
|
257
|
+
style: {
|
|
258
|
+
...calloutStyle,
|
|
259
|
+
borderColor:
|
|
260
|
+
state.noteTone === 'warn'
|
|
261
|
+
? 'var(--dsw-alias-state-warning-primary)'
|
|
262
|
+
: 'var(--dsw-alias-border-l1)',
|
|
263
|
+
color:
|
|
264
|
+
state.noteTone === 'warn'
|
|
265
|
+
? 'var(--dsw-alias-state-warning-primary)'
|
|
266
|
+
: 'var(--dsw-alias-label-secondary)',
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
state.note,
|
|
270
|
+
)
|
|
271
|
+
: null,
|
|
272
|
+
h(
|
|
273
|
+
'div',
|
|
274
|
+
{
|
|
275
|
+
style: {
|
|
276
|
+
display: 'grid',
|
|
277
|
+
gridTemplateColumns:
|
|
278
|
+
'repeat(auto-fit, minmax(170px, 1fr))',
|
|
279
|
+
gap: 10,
|
|
280
|
+
margin: '14px 0',
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
h(Metric, { label: '本机地址', value: state?.baseUrl || '—' }),
|
|
284
|
+
h(Metric, { label: '局域网', value: state?.lanUrl || '—' }),
|
|
285
|
+
h(Metric, { label: '模型数', value: state?.models || '—' }),
|
|
286
|
+
h(Metric, {
|
|
287
|
+
label: '模型商',
|
|
288
|
+
value: state?.provider || '—',
|
|
289
|
+
tone: TONE[state?.providerTone] || undefined,
|
|
290
|
+
}),
|
|
291
|
+
h(Metric, { label: '监听', value: state?.listenMode || '—' }),
|
|
292
|
+
h(Metric, { label: '平台', value: state?.platform || '—' }),
|
|
293
|
+
),
|
|
294
|
+
h(
|
|
295
|
+
'div',
|
|
296
|
+
{
|
|
297
|
+
style: {
|
|
298
|
+
display: 'flex',
|
|
299
|
+
flexWrap: 'wrap',
|
|
300
|
+
gap: 8,
|
|
301
|
+
marginBottom: 10,
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
running
|
|
305
|
+
? h(Button, { onClick: () => act('stop'), disabled }, '停止')
|
|
306
|
+
: h(
|
|
307
|
+
Button,
|
|
308
|
+
{
|
|
309
|
+
primary: true,
|
|
310
|
+
onClick: () => act('start'),
|
|
311
|
+
disabled,
|
|
312
|
+
},
|
|
313
|
+
busy === 'start' ? '启动中…' : '启动',
|
|
314
|
+
),
|
|
315
|
+
h(Button, { onClick: () => act('restart'), disabled }, '重启'),
|
|
316
|
+
h(Button, { onClick: () => act('listenLocal'), disabled }, '仅本机'),
|
|
317
|
+
h(Button, { onClick: () => act('listenLan'), disabled }, '开局域网'),
|
|
318
|
+
h(
|
|
319
|
+
Button,
|
|
320
|
+
{ onClick: () => act('autoConfig'), disabled },
|
|
321
|
+
busy === 'autoConfig' ? '配置中…' : '自动配置',
|
|
322
|
+
),
|
|
323
|
+
h(Button, { onClick: () => act('reinstall'), disabled }, '重装'),
|
|
324
|
+
),
|
|
325
|
+
h(
|
|
326
|
+
'div',
|
|
327
|
+
{
|
|
328
|
+
style: {
|
|
329
|
+
display: 'flex',
|
|
330
|
+
flexWrap: 'wrap',
|
|
331
|
+
gap: 8,
|
|
332
|
+
marginBottom: 14,
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
h(
|
|
336
|
+
Button,
|
|
337
|
+
{
|
|
338
|
+
onClick: () => void copy('本机地址', state?.baseUrl || ''),
|
|
339
|
+
disabled: !state?.baseUrl,
|
|
340
|
+
},
|
|
341
|
+
copied === '本机地址' ? '已复制地址' : '复制地址',
|
|
342
|
+
),
|
|
343
|
+
h(
|
|
344
|
+
Button,
|
|
345
|
+
{
|
|
346
|
+
onClick: () => void copy('Key', state?.key || ''),
|
|
347
|
+
disabled: !state?.key,
|
|
348
|
+
},
|
|
349
|
+
secretHidden
|
|
350
|
+
? 'Key 仅本机可见'
|
|
351
|
+
: copied === 'Key'
|
|
352
|
+
? '已复制 Key'
|
|
353
|
+
: '复制 Key',
|
|
354
|
+
),
|
|
355
|
+
h(
|
|
356
|
+
Button,
|
|
357
|
+
{
|
|
358
|
+
onClick: () =>
|
|
359
|
+
window.open(
|
|
360
|
+
state?.webUrl || 'http://127.0.0.1:8081',
|
|
361
|
+
'_blank',
|
|
362
|
+
'noopener',
|
|
363
|
+
),
|
|
364
|
+
},
|
|
365
|
+
'打开后台',
|
|
366
|
+
),
|
|
367
|
+
h(
|
|
368
|
+
Button,
|
|
369
|
+
{
|
|
370
|
+
onClick: () =>
|
|
371
|
+
window.open(
|
|
372
|
+
'https://github.com/king-andthe-nice/dsh-plugin-opencode2api',
|
|
373
|
+
'_blank',
|
|
374
|
+
'noopener',
|
|
375
|
+
),
|
|
376
|
+
},
|
|
377
|
+
'项目主页',
|
|
378
|
+
),
|
|
379
|
+
),
|
|
380
|
+
h(
|
|
381
|
+
'div',
|
|
382
|
+
{
|
|
383
|
+
style: {
|
|
384
|
+
fontSize: 12,
|
|
385
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
386
|
+
lineHeight: 1.7,
|
|
387
|
+
marginBottom: 12,
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
'DSH 模型选择器里应出现「本地免费 API」。后台账号 admin,密码见 ',
|
|
391
|
+
h('code', null, state?.webPasswordPath || 'webui-password.txt'),
|
|
392
|
+
secretHidden || !state?.password
|
|
393
|
+
? null
|
|
394
|
+
: h('span', null, ';当前密码:', h('code', null, state.password)),
|
|
395
|
+
),
|
|
396
|
+
h(
|
|
397
|
+
'details',
|
|
398
|
+
{
|
|
399
|
+
style: {
|
|
400
|
+
borderTop: '1px solid var(--dsw-alias-border-l1)',
|
|
401
|
+
paddingTop: 10,
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
h(
|
|
405
|
+
'summary',
|
|
406
|
+
{
|
|
407
|
+
style: {
|
|
408
|
+
cursor: 'pointer',
|
|
409
|
+
fontSize: 12,
|
|
410
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
411
|
+
},
|
|
412
|
+
},
|
|
413
|
+
'运行日志',
|
|
414
|
+
),
|
|
415
|
+
h(
|
|
416
|
+
'pre',
|
|
417
|
+
{
|
|
418
|
+
style: {
|
|
419
|
+
margin: '8px 0 0',
|
|
420
|
+
padding: 10,
|
|
421
|
+
borderRadius: 8,
|
|
422
|
+
background: 'var(--dsw-alias-bg-layer-2)',
|
|
423
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
424
|
+
fontSize: 11,
|
|
425
|
+
lineHeight: 1.55,
|
|
426
|
+
whiteSpace: 'pre-wrap',
|
|
427
|
+
wordBreak: 'break-all',
|
|
428
|
+
maxHeight: 220,
|
|
429
|
+
overflow: 'auto',
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
state?.log || '暂无日志',
|
|
433
|
+
),
|
|
434
|
+
),
|
|
435
|
+
h(
|
|
436
|
+
'div',
|
|
437
|
+
{
|
|
438
|
+
style: {
|
|
439
|
+
marginTop: 12,
|
|
440
|
+
fontSize: 11,
|
|
441
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
442
|
+
},
|
|
443
|
+
},
|
|
444
|
+
'网关来自 opencode2api(GPL-3.0-or-later)官方 Release,本插件不打包、不修改它。',
|
|
445
|
+
),
|
|
446
|
+
)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function apply(ctx) {
|
|
450
|
+
ctx.slots.inject('settings.section', () =>
|
|
451
|
+
ctx.slots.register(
|
|
452
|
+
{
|
|
453
|
+
name: 'settings.section',
|
|
454
|
+
id: 'opencode2api',
|
|
455
|
+
order: 36,
|
|
456
|
+
label: () => '本地免费 API',
|
|
457
|
+
},
|
|
458
|
+
() => h(Page),
|
|
459
|
+
),
|
|
460
|
+
)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
exports.apply = apply
|
|
464
|
+
exports.inject = inject
|
|
465
|
+
exports.name = 'opencode2api'
|
|
466
|
+
return module.exports
|
|
467
|
+
},
|
|
468
|
+
})
|
package/lib/index.js
CHANGED
|
@@ -270,6 +270,152 @@ async function autoConfigure(force = false) {
|
|
|
270
270
|
autoInfo = { status: '已自动配置', tone: 'good' }
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
+
function tailLog(lines = 40) {
|
|
274
|
+
try {
|
|
275
|
+
const stat = fs.statSync(LOG)
|
|
276
|
+
const start = Math.max(0, stat.size - 32 * 1024)
|
|
277
|
+
const fd = fs.openSync(LOG, 'r')
|
|
278
|
+
const buffer = Buffer.alloc(stat.size - start)
|
|
279
|
+
try {
|
|
280
|
+
fs.readSync(fd, buffer, 0, buffer.length, start)
|
|
281
|
+
} finally {
|
|
282
|
+
fs.closeSync(fd)
|
|
283
|
+
}
|
|
284
|
+
return buffer.toString('utf8').split('\n').slice(-lines).join('\n')
|
|
285
|
+
} catch {
|
|
286
|
+
return ''
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function readJson(req) {
|
|
291
|
+
return new Promise((resolve, reject) => {
|
|
292
|
+
let body = ''
|
|
293
|
+
req.on('data', (chunk) => {
|
|
294
|
+
body += chunk
|
|
295
|
+
if (body.length > 1024 * 1024) {
|
|
296
|
+
req.destroy()
|
|
297
|
+
reject(new Error('请求过大'))
|
|
298
|
+
}
|
|
299
|
+
})
|
|
300
|
+
req.on('end', () => {
|
|
301
|
+
try {
|
|
302
|
+
resolve(body ? JSON.parse(body) : {})
|
|
303
|
+
} catch (error) {
|
|
304
|
+
reject(error)
|
|
305
|
+
}
|
|
306
|
+
})
|
|
307
|
+
req.on('error', reject)
|
|
308
|
+
})
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
async function buildState() {
|
|
312
|
+
const snap = await snapshot()
|
|
313
|
+
const cfg = fs.existsSync(CFG) ? readConfig() : {}
|
|
314
|
+
return {
|
|
315
|
+
...snap,
|
|
316
|
+
running: Boolean(await health()),
|
|
317
|
+
installed: fs.existsSync(BIN),
|
|
318
|
+
key: cfg.server_keys?.[0] || '',
|
|
319
|
+
password: readPassword(),
|
|
320
|
+
webPasswordPath: PASS,
|
|
321
|
+
apiPort: API_PORT,
|
|
322
|
+
webPort: WEB_PORT,
|
|
323
|
+
listen: cfg.listen || '',
|
|
324
|
+
log: tailLog(40),
|
|
325
|
+
autoError: lastAutoError,
|
|
326
|
+
version: VERSION,
|
|
327
|
+
time: Date.now(),
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async function runAction(action) {
|
|
332
|
+
switch (action) {
|
|
333
|
+
case 'start':
|
|
334
|
+
desired = true
|
|
335
|
+
await boot()
|
|
336
|
+
return '已启动'
|
|
337
|
+
case 'stop':
|
|
338
|
+
await stop()
|
|
339
|
+
return '已停止'
|
|
340
|
+
case 'restart':
|
|
341
|
+
await restart()
|
|
342
|
+
void autoConfigure().catch(() => {})
|
|
343
|
+
return '已重启'
|
|
344
|
+
case 'listenLocal':
|
|
345
|
+
case 'listenLan': {
|
|
346
|
+
setListen(action === 'listenLocal' ? '127.0.0.1' : '0.0.0.0')
|
|
347
|
+
desired = true
|
|
348
|
+
await killProcess()
|
|
349
|
+
const ok = await start()
|
|
350
|
+
return ok ? (action === 'listenLocal' ? '已仅本机监听' : '已开局域网') : '切换失败'
|
|
351
|
+
}
|
|
352
|
+
case 'autoConfig':
|
|
353
|
+
await autoConfigure(true)
|
|
354
|
+
return autoInfo.status
|
|
355
|
+
case 'reinstall':
|
|
356
|
+
void (async () => {
|
|
357
|
+
busy = true
|
|
358
|
+
try {
|
|
359
|
+
desired = true
|
|
360
|
+
await killProcess()
|
|
361
|
+
await ensureInstalled(true)
|
|
362
|
+
await start()
|
|
363
|
+
lastError = ''
|
|
364
|
+
void autoConfigure().catch(() => {})
|
|
365
|
+
} catch (error) {
|
|
366
|
+
lastError = human(error)
|
|
367
|
+
} finally {
|
|
368
|
+
busy = false
|
|
369
|
+
}
|
|
370
|
+
})()
|
|
371
|
+
return '后台重装中…'
|
|
372
|
+
default:
|
|
373
|
+
throw new Error('未知操作')
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async function api(req, res) {
|
|
378
|
+
const url = new URL(req.url ?? '/', 'http://localhost')
|
|
379
|
+
const route = url.pathname.replace(/^\/api\/opencode2api/, '').replace(/\/+$/, '') || '/'
|
|
380
|
+
const method = String(req.method ?? 'GET').toUpperCase()
|
|
381
|
+
const send = (status, payload) => {
|
|
382
|
+
res.writeHead(status, {
|
|
383
|
+
'content-type': 'application/json; charset=utf-8',
|
|
384
|
+
'cache-control': 'no-store',
|
|
385
|
+
})
|
|
386
|
+
res.end(JSON.stringify(payload))
|
|
387
|
+
}
|
|
388
|
+
const address = req.socket?.remoteAddress ?? ''
|
|
389
|
+
const local = address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1'
|
|
390
|
+
const origin = String(req.headers.origin || '')
|
|
391
|
+
if (origin && !/^https?:\/\/(127\.0\.0\.1|localhost)(:\d+)?$/i.test(origin)) {
|
|
392
|
+
return send(403, { error: 'forbidden' })
|
|
393
|
+
}
|
|
394
|
+
try {
|
|
395
|
+
if (method === 'GET' && route === '/state') {
|
|
396
|
+
const state = await buildState()
|
|
397
|
+
if (!local) {
|
|
398
|
+
state.key = ''
|
|
399
|
+
state.password = ''
|
|
400
|
+
state.keyHidden = true
|
|
401
|
+
}
|
|
402
|
+
return send(200, state)
|
|
403
|
+
}
|
|
404
|
+
if (method === 'GET' && route === '/log') {
|
|
405
|
+
return send(200, { log: tailLog(Number(url.searchParams.get('lines')) || 120) })
|
|
406
|
+
}
|
|
407
|
+
if (method === 'POST' && route === '/action') {
|
|
408
|
+
if (!local) return send(403, { error: '仅本机可操作' })
|
|
409
|
+
const body = await readJson(req)
|
|
410
|
+
const toast = await runAction(String(body.action || ''))
|
|
411
|
+
return send(200, { ok: true, toast, state: await buildState() })
|
|
412
|
+
}
|
|
413
|
+
return send(404, { error: 'not found' })
|
|
414
|
+
} catch (error) {
|
|
415
|
+
return send(500, { error: human(error) })
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
273
419
|
function setListen(host) {
|
|
274
420
|
const cfg = readConfig()
|
|
275
421
|
cfg.listen = `${host}:${API_PORT}`
|
|
@@ -760,4 +906,10 @@ export function apply(ctx) {
|
|
|
760
906
|
ctx.inject(['dshGo'], (ctx) => {
|
|
761
907
|
registerUi(ctx)
|
|
762
908
|
})
|
|
909
|
+
ctx.inject(['webServer'], (scoped) => {
|
|
910
|
+
scoped.effect(
|
|
911
|
+
() => scoped.webServer.register({ kind: 'prefix', path: '/api/opencode2api', handler: api }),
|
|
912
|
+
'opencode2api: web api',
|
|
913
|
+
)
|
|
914
|
+
})
|
|
763
915
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-opencode2api",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Unofficial one-click installer and auto-runner for opencode2api — a local free OpenAI-compatible API gateway — inside DSH (Windows, macOS, Linux, Android).",
|
|
3
|
+
"version": "1.2.0",
|
|
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",
|
|
7
7
|
"url": "git+https://github.com/king-andthe-nice/dsh-plugin-opencode2api.git"
|
|
@@ -9,7 +9,13 @@
|
|
|
9
9
|
"homepage": "https://github.com/king-andthe-nice/dsh-plugin-opencode2api#readme",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"type": "module",
|
|
12
|
-
"main": "lib/index.js",
|
|
12
|
+
"main": "./lib/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./lib/index.js",
|
|
15
|
+
"./client": "./lib/client.js",
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
"./cordis.patch.yml": "./cordis.patch.yml"
|
|
18
|
+
},
|
|
13
19
|
"files": [
|
|
14
20
|
"lib",
|
|
15
21
|
"cordis.patch.yml",
|
|
@@ -33,6 +39,11 @@
|
|
|
33
39
|
"dsh": {
|
|
34
40
|
"bundle": {
|
|
35
41
|
"patch": "./cordis.patch.yml"
|
|
42
|
+
},
|
|
43
|
+
"client": {
|
|
44
|
+
"platform": "web",
|
|
45
|
+
"immediately": true,
|
|
46
|
+
"inject": []
|
|
36
47
|
}
|
|
37
48
|
},
|
|
38
49
|
"engines": {
|