@kir_sleeping/dsh-relay 0.3.1 → 0.5.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 +9 -5
- package/client.js +6 -4
- package/docs/demo.html +90 -17
- package/docs/integration.md +368 -116
- package/docs/reference.md +4 -3
- package/index.js +309 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,19 +64,21 @@ dsh plugin add @kir_sleeping/dsh-relay
|
|
|
64
64
|
|
|
65
65
|
```bash
|
|
66
66
|
# 1. 申请凭证。这一步会在主机上弹系统确认框,用户点「允许」才继续
|
|
67
|
-
curl -s -X POST http://127.0.0.1:3080/x/relay/
|
|
67
|
+
curl -s -X POST http://127.0.0.1:3080/x/relay/enroll \
|
|
68
68
|
-H 'Content-Type: application/json' -d '{"source":"my-app"}'
|
|
69
|
-
# → {"source":"my-app","
|
|
69
|
+
# → {"source":"my-app","token":"…","level":1}
|
|
70
70
|
|
|
71
|
-
# 2.
|
|
71
|
+
# 2. 提交。token 放头里,source 在 body
|
|
72
72
|
curl -s -X POST http://127.0.0.1:3080/x/relay/submit \
|
|
73
73
|
-H 'Content-Type: application/json' \
|
|
74
|
-
-
|
|
74
|
+
-H 'Authorization: Bearer <token>' \
|
|
75
|
+
-d '{"content":"请只回复 OK","source":"my-app","trigger":"user","wait":true,"timeout":60}'
|
|
75
76
|
|
|
76
77
|
# 3. 或者流式
|
|
77
78
|
curl -N -X POST http://127.0.0.1:3080/x/relay/submit \
|
|
78
79
|
-H 'Content-Type: application/json' \
|
|
79
|
-
-
|
|
80
|
+
-H 'Authorization: Bearer <token>' \
|
|
81
|
+
-d '{"content":"介绍一下这个项目","source":"my-app","trigger":"user","stream":true,"timeout":120}'
|
|
80
82
|
```
|
|
81
83
|
|
|
82
84
|
## 文档
|
|
@@ -90,6 +92,8 @@ curl -N -X POST http://127.0.0.1:3080/x/relay/submit \
|
|
|
90
92
|
|
|
91
93
|
这份包已经装在本机的话,同样的文件就在包的 `docs/` 目录下,**直接读本地文件,不用联网**。
|
|
92
94
|
|
|
95
|
+
可运行的示例页面:`http://127.0.0.1:3080/x/relay/demo`(由插件自己提供,和接口同源;用 `file://` 打开会被 DSH 的请求来源检查拒绝)。
|
|
96
|
+
|
|
93
97
|
## AGENT
|
|
94
98
|
|
|
95
99
|
如果你是 agent,建议先看 [usage.md](https://github.com/kir-sleeping/DSH-relay/blob/main/docs/usage.md):教用户怎么用、为什么会出现那些弹窗、不想反复弹窗该怎么做。
|
package/client.js
CHANGED
|
@@ -115,16 +115,16 @@ window.__ModuleLoader__.load({
|
|
|
115
115
|
h('select', {
|
|
116
116
|
style: styles.select,
|
|
117
117
|
value: String(row.level),
|
|
118
|
-
disabled: busy || !row.
|
|
118
|
+
disabled: busy || !row.hasToken,
|
|
119
119
|
onChange: (event) => {
|
|
120
120
|
const level = Number(event.currentTarget.value)
|
|
121
121
|
if (level !== row.level) run('/sources/level', { source: row.source, level })
|
|
122
122
|
},
|
|
123
123
|
}, LEVELS.map((option) => h('option', { key: option.value, value: String(option.value) }, option.label)))),
|
|
124
|
-
h('td', { style: styles.td }, row.
|
|
124
|
+
h('td', { style: styles.td }, row.hasToken ? '已持有' : '—'),
|
|
125
125
|
h('td', { style: styles.td },
|
|
126
126
|
h('div', { style: styles.actions },
|
|
127
|
-
row.
|
|
127
|
+
row.hasToken
|
|
128
128
|
? h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/reset', { source: row.source }) }, '重置凭证')
|
|
129
129
|
: h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/clear', { source: row.source }) }, '解除禁止'),
|
|
130
130
|
h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/delete', { source: row.source }) }, '删除')))))
|
|
@@ -156,7 +156,9 @@ window.__ModuleLoader__.load({
|
|
|
156
156
|
h('li', { style: styles.helpItem },
|
|
157
157
|
'放宽权限(提高级别、重置凭证)会弹一次系统确认框;收紧(降低级别、禁止)立即生效,不弹窗。'),
|
|
158
158
|
h('li', { style: styles.helpItem },
|
|
159
|
-
'拿不准就留在 1 级,先看着它跑几次再决定。')
|
|
159
|
+
'拿不准就留在 1 级,先看着它跑几次再决定。'),
|
|
160
|
+
h('li', { style: styles.helpItem },
|
|
161
|
+
'应用可以把消息投递到你的**任意会话**(比如你正在聊的那个)。弹窗里会写明投递到哪个会话;3 级不弹窗。')))
|
|
160
162
|
|
|
161
163
|
return h('div', null,
|
|
162
164
|
h('div', { style: styles.header },
|
package/docs/demo.html
CHANGED
|
@@ -34,11 +34,12 @@
|
|
|
34
34
|
|
|
35
35
|
── 后端契约(只列本页用到的) ──────────────────────────────────────────────
|
|
36
36
|
|
|
37
|
-
POST /
|
|
37
|
+
POST /enroll {source} → 返回 token
|
|
38
38
|
首次接入。会在主机上弹出系统确认框,用户点「允许」才返回凭证。
|
|
39
39
|
阻塞最长 60 秒,所以这个请求的 fetch 不能设置太短的超时。
|
|
40
40
|
|
|
41
|
-
POST /submit {content, source,
|
|
41
|
+
POST /submit {content, source, session, trigger, wait, timeout}
|
|
42
|
+
头:Authorization: Bearer <token>
|
|
42
43
|
wait=true → 200 {type:"final", content} | 202 {id,status:"pending"} | 410
|
|
43
44
|
stream=true → text/event-stream,事件见下面 streamAsk()
|
|
44
45
|
失败 → 4xx/5xx + {code, message}
|
|
@@ -156,6 +157,8 @@
|
|
|
156
157
|
background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
|
|
157
158
|
}
|
|
158
159
|
.chat .body { white-space: pre-wrap; min-height: 22px; }
|
|
160
|
+
.chat .session-row { align-items: center; gap: 8px; margin-bottom: 10px; }
|
|
161
|
+
.chat .session-row select { flex: 1; min-width: 0; }
|
|
159
162
|
.chat .preview { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
|
|
160
163
|
.chat .thumb { position: relative; }
|
|
161
164
|
.chat .thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); display: block; }
|
|
@@ -205,6 +208,11 @@
|
|
|
205
208
|
<div class="chat">
|
|
206
209
|
<h2>随手问</h2>
|
|
207
210
|
<div class="dim">这条走流式:回答边生成边显示,不用轮询。</div>
|
|
211
|
+
<div class="row session-row">
|
|
212
|
+
<label class="dim" for="sessionPick">投递到</label>
|
|
213
|
+
<select id="sessionPick"></select>
|
|
214
|
+
<button id="sessionRefresh" title="重新读取会话列表">刷新</button>
|
|
215
|
+
</div>
|
|
208
216
|
<div class="row">
|
|
209
217
|
<input id="chatInput" placeholder="问点什么…回车或点发送">
|
|
210
218
|
<button id="chatPick">图片</button>
|
|
@@ -257,12 +265,17 @@ function classify(status, body) {
|
|
|
257
265
|
return 'request';
|
|
258
266
|
}
|
|
259
267
|
|
|
260
|
-
async function call(path, payload) {
|
|
268
|
+
async function call(path, payload, token) {
|
|
269
|
+
// 凭证走 Authorization 头,source 留在 body —— 前者是密钥,后者是给人看的标签。
|
|
270
|
+
const headers = {};
|
|
271
|
+
if (payload !== undefined) headers['Content-Type'] = 'application/json';
|
|
272
|
+
if (typeof token === 'string' && token !== '') headers.Authorization = `Bearer ${token}`;
|
|
273
|
+
|
|
261
274
|
let response;
|
|
262
275
|
try {
|
|
263
276
|
response = await fetch(CONFIG.base + path, {
|
|
264
277
|
method: payload === undefined ? 'GET' : 'POST',
|
|
265
|
-
headers:
|
|
278
|
+
headers: Object.keys(headers).length > 0 ? headers : undefined,
|
|
266
279
|
body: payload === undefined ? undefined : JSON.stringify(payload),
|
|
267
280
|
});
|
|
268
281
|
} catch {
|
|
@@ -286,18 +299,18 @@ async function call(path, payload) {
|
|
|
286
299
|
// 注意:申请接口会阻塞到用户点完(最长 60 秒),所以这里不能套用短超时。
|
|
287
300
|
// ============================================================================
|
|
288
301
|
async function enroll() {
|
|
289
|
-
const { status, body } = await call('/
|
|
302
|
+
const { status, body } = await call('/enroll', { source: CONFIG.source });
|
|
290
303
|
if (status !== 200) {
|
|
291
|
-
// ERR_SOURCE_EXISTS
|
|
292
|
-
//
|
|
304
|
+
// ERR_SOURCE_EXISTS:来源已注册,但本页没有它的凭证(换了地址或清过缓存)。
|
|
305
|
+
// 页面无法自救——重置凭证不会把新凭证交给本页,只能在设置里删掉来源再重新授权。
|
|
293
306
|
throw new RelayError(classify(status, body), status, body);
|
|
294
307
|
}
|
|
295
|
-
localStorage.setItem(CONFIG.storageKey, body.
|
|
308
|
+
localStorage.setItem(CONFIG.storageKey, body.token);
|
|
296
309
|
await refreshStatus();
|
|
297
|
-
return body.
|
|
310
|
+
return body.token;
|
|
298
311
|
}
|
|
299
312
|
|
|
300
|
-
async function
|
|
313
|
+
async function getToken() {
|
|
301
314
|
const cached = localStorage.getItem(CONFIG.storageKey);
|
|
302
315
|
return cached || await enroll();
|
|
303
316
|
}
|
|
@@ -332,17 +345,16 @@ function dropCredential() {
|
|
|
332
345
|
// streamAsk() 走 SSE,适合用户主动请求:边生成边显示,用户在看。
|
|
333
346
|
// ============================================================================
|
|
334
347
|
async function submit({ content, trigger, wait, stream, timeoutSec }) {
|
|
335
|
-
const
|
|
348
|
+
const token = await getToken();
|
|
336
349
|
const payload = {
|
|
337
350
|
content,
|
|
338
351
|
source: CONFIG.source,
|
|
339
|
-
fingerprint: print,
|
|
340
352
|
session: CONFIG.session,
|
|
341
353
|
trigger, // "user" | "app"
|
|
342
354
|
timeout: timeoutSec,
|
|
343
355
|
...(stream ? { stream: true } : { wait: true }),
|
|
344
356
|
};
|
|
345
|
-
return call('/submit', payload);
|
|
357
|
+
return call('/submit', payload, token);
|
|
346
358
|
}
|
|
347
359
|
|
|
348
360
|
async function pollResult(id) {
|
|
@@ -389,15 +401,16 @@ async function ask({ content, trigger, timeoutSec = 120, retryCredential = true
|
|
|
389
401
|
}
|
|
390
402
|
|
|
391
403
|
async function streamAsk({ content, trigger, timeoutSec = 120, onDelta, images, retryCredential = true }) {
|
|
392
|
-
const
|
|
404
|
+
const token = await getToken();
|
|
393
405
|
let response;
|
|
394
406
|
try {
|
|
395
407
|
response = await fetch(CONFIG.base + '/submit', {
|
|
396
408
|
method: 'POST',
|
|
397
|
-
headers: { 'Content-Type': 'application/json' },
|
|
409
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
|
398
410
|
body: JSON.stringify({
|
|
399
|
-
content, source: CONFIG.source,
|
|
400
|
-
session: CONFIG.session,
|
|
411
|
+
content, source: CONFIG.source,
|
|
412
|
+
session: sessionPick.value || CONFIG.session,
|
|
413
|
+
trigger, stream: true, timeout: timeoutSec,
|
|
401
414
|
...(images && images.length > 0 ? { images } : {}),
|
|
402
415
|
}),
|
|
403
416
|
});
|
|
@@ -756,6 +769,64 @@ const chatOut = document.getElementById('chatOut');
|
|
|
756
769
|
const chatFile = document.getElementById('chatFile');
|
|
757
770
|
const chatPreview = document.getElementById('chatPreview');
|
|
758
771
|
|
|
772
|
+
// ----------------------------------------------------------------------------
|
|
773
|
+
// 投递目标:列出用户的全部会话,按工作区分组,按最近活动排序。
|
|
774
|
+
// 会话 id 是唯一标识,标题只用来显示——标题会重名。
|
|
775
|
+
// ----------------------------------------------------------------------------
|
|
776
|
+
const sessionPick = document.getElementById('sessionPick');
|
|
777
|
+
|
|
778
|
+
function ago(ms) {
|
|
779
|
+
if (!Number.isFinite(ms) || ms <= 0) return '';
|
|
780
|
+
const diff = Date.now() - ms;
|
|
781
|
+
if (diff < 60_000) return '刚刚';
|
|
782
|
+
if (diff < 3_600_000) return `${Math.floor(diff / 60_000)} 分钟前`;
|
|
783
|
+
if (diff < 86_400_000) return `${Math.floor(diff / 3_600_000)} 小时前`;
|
|
784
|
+
return `${Math.floor(diff / 86_400_000)} 天前`;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
async function loadSessions() {
|
|
788
|
+
try {
|
|
789
|
+
const token = await getToken();
|
|
790
|
+
// 这个接口返回的是"工作区数组",每个工作区下面挂它的会话。
|
|
791
|
+
// 注意 call() 回的是 { status, body },body 才是数组。
|
|
792
|
+
const { status, body: groups } = await call(
|
|
793
|
+
`/sessions?source=${encodeURIComponent(CONFIG.source)}`, undefined, token,
|
|
794
|
+
);
|
|
795
|
+
if (status !== 200 || !Array.isArray(groups)) {
|
|
796
|
+
throw new RelayError('list', status, { code: 'ERR_SESSION_LIST', message: `读取会话列表失败(${status})` });
|
|
797
|
+
}
|
|
798
|
+
sessionPick.innerHTML = '';
|
|
799
|
+
const previous = sessionPick.value;
|
|
800
|
+
for (const group of groups) {
|
|
801
|
+
const box = document.createElement('optgroup');
|
|
802
|
+
box.label = group.title || '未归类';
|
|
803
|
+
for (const session of group.sessions) {
|
|
804
|
+
const option = document.createElement('option');
|
|
805
|
+
option.value = session.id;
|
|
806
|
+
const name = session.title || session.id.slice(0, 12);
|
|
807
|
+
option.textContent = `${name} · ${ago(session.updatedAt)}${session.live ? ' · 进行中' : ''}`;
|
|
808
|
+
box.append(option);
|
|
809
|
+
}
|
|
810
|
+
if (box.children.length > 0) sessionPick.append(box);
|
|
811
|
+
}
|
|
812
|
+
if (previous) sessionPick.value = previous;
|
|
813
|
+
if (!sessionPick.value) sessionPick.value = CONFIG.session;
|
|
814
|
+
} catch (error) {
|
|
815
|
+
console.error('[relay] 读取会话列表失败', error);
|
|
816
|
+
sessionPick.innerHTML = `<option value="${CONFIG.session}">${CONFIG.session}</option>`;
|
|
817
|
+
|
|
818
|
+
// 最常见的一种:页面换了地址(比如从 file:// 改成 http://),
|
|
819
|
+
// localStorage 是按来源隔离的,旧凭证读不到;而这个来源在插件那边已经注册过,
|
|
820
|
+
// 又不能重复申请。给出能照做的步骤,别让人对着一个空的列表猜。
|
|
821
|
+
const code = error && error.body && error.body.code;
|
|
822
|
+
document.getElementById('chatError').textContent = code === 'ERR_SOURCE_EXISTS'
|
|
823
|
+
? `本页没有凭证,而来源「${CONFIG.source}」已经接入过。请到 设置 → 应用权限 里删除它,然后刷新本页重新授权。`
|
|
824
|
+
: `读取会话列表失败:${error && error.message ? error.message : error}`;
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
document.getElementById('sessionRefresh').onclick = () => loadSessions();
|
|
829
|
+
|
|
759
830
|
// 最多 4 张,和 Host 的限制一致;本地先拦,别让用户白等一个注定失败的请求。
|
|
760
831
|
let chatImages = [];
|
|
761
832
|
const MAX_IMAGES = 4;
|
|
@@ -864,6 +935,8 @@ chatInput.addEventListener('keydown', (event) => {
|
|
|
864
935
|
renderList();
|
|
865
936
|
renderReader();
|
|
866
937
|
refreshStatus();
|
|
938
|
+
// 会话列表要等凭证就绪,失败了也不影响主流程
|
|
939
|
+
loadSessions();
|
|
867
940
|
</script>
|
|
868
941
|
</body>
|
|
869
942
|
</html>
|