dsh-collab-pair 1.5.1 → 1.5.3
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 +22 -2
- package/client.js +79 -19
- package/hub.js +6 -0
- package/index.js +132 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -112,6 +112,15 @@ Security: the token is a shared secret for the room; anyone holding it can deliv
|
|
|
112
112
|
|
|
113
113
|
If a tunnel is inconvenient, choose **Free public relay (ntfy.sh)** in the Settings card: no account and no deployment. The topic is derived from room + token; messages go through ntfy.sh in 4 KB chunks (the free tier allows about 250 publishes per day per IP). Use a long random token; this is not end-to-end encrypted.
|
|
114
114
|
|
|
115
|
+
**`127.0.0.1` / `localhost` only works on the machine running the hub.** On every other device use the hub's LAN IP (same network, with the firewall port open) or the HTTPS tunnel URL. `hub.js` prints its LAN addresses at startup. Before relying on a relay, open `<hub-url>/v1/health` from the other device and press **Test connection** in the Settings card.
|
|
116
|
+
|
|
117
|
+
Cross-device checklist:
|
|
118
|
+
|
|
119
|
+
- the other device can open `<hub-url>/v1/health`;
|
|
120
|
+
- both devices use the same `hubRoom` and `hubToken`, with **distinct** `deviceName`s;
|
|
121
|
+
- the worker device registered its alias, and the manager linked that device + alias;
|
|
122
|
+
- remote workers run on their own machine, so files are **not** shared: use a common git remote or network drive, or ask the worker to return patches/text in its report.
|
|
123
|
+
|
|
115
124
|
## Stage queue
|
|
116
125
|
|
|
117
126
|
A human can pre-write several large stages for one worker; finishing one dispatches the next automatically.
|
|
@@ -127,7 +136,8 @@ Any conversation, not only a manager, can have its own prompt queue:
|
|
|
127
136
|
|
|
128
137
|
- Add items in the **Conversation to-do** card, or `/collab queue {"action":"add","title":"…","prompt":"…"}`.
|
|
129
138
|
- `start` sends the first pending prompt as a normal user message; when that turn ends, the next pending prompt is sent automatically.
|
|
130
|
-
- `pause` stops auto-advance after the current turn, `resume` re-enables it, and `clear` removes pending items.
|
|
139
|
+
- `pause` stops auto-advance after the current turn, `resume` re-enables it and sends the next pending prompt when the queue is idle, and `clear` removes pending items.
|
|
140
|
+
- If a turn ends with an error/abort, that item is marked **failed** and auto-advance pauses; press **Reset queue** to return a stuck `sent` item to pending and retry.
|
|
131
141
|
- The queue is stored per conversation and survives plugin reloads.
|
|
132
142
|
|
|
133
143
|
## Projects
|
|
@@ -372,6 +382,15 @@ collab_dispatch({ worker: "前端", task: "…" })
|
|
|
372
382
|
|
|
373
383
|
如果内网穿透不方便,可在设置卡片里选 **免费公共中继(ntfy.sh)**:不用账号、不用部署。topic 由 room + token 派生;消息以 4 KB 分片经过 ntfy.sh(免费额度约每 IP 每天 250 次发布)。请用足够长的随机 token;这不是端到端加密。
|
|
374
384
|
|
|
385
|
+
**`127.0.0.1` / `localhost` 只对运行 hub 的那台机器有效。** 其他设备要填 hub 所在机器的局域网 IP(同一网络、防火墙放行端口)或 HTTPS 隧道地址;`hub.js` 启动时会打印局域网地址。正式使用前,从另一台设备打开 `<hub 地址>/v1/health`,并在设置卡片里点「测试连接」。
|
|
386
|
+
|
|
387
|
+
跨设备检查清单:
|
|
388
|
+
|
|
389
|
+
- 另一台设备能打开 `<hub 地址>/v1/health`;
|
|
390
|
+
- 两台设备的 `hubRoom`、`hubToken` 相同,`deviceName` **不同**;
|
|
391
|
+
- 员工设备已注册别名,经理设备关联了该设备 + 别名;
|
|
392
|
+
- 远程员工在各自机器上执行,**文件不共享**:用共同的 git remote 或网络盘,或让员工在回报里给出 patch/文本。
|
|
393
|
+
|
|
375
394
|
## 阶段队列
|
|
376
395
|
|
|
377
396
|
人可以给同一个员工预先写好多个大阶段;完成一个阶段后自动派发下一个。
|
|
@@ -387,7 +406,8 @@ collab_dispatch({ worker: "前端", task: "…" })
|
|
|
387
406
|
|
|
388
407
|
- 在「对话待办」卡片里添加,或 `/collab queue {"action":"add","title":"…","prompt":"…"}`。
|
|
389
408
|
- `start` 把第一条待发提示词作为普通用户消息发进去;该回合结束后,自动发送下一条。
|
|
390
|
-
- `pause` 在当前回合后停止自动连发,`resume`
|
|
409
|
+
- `pause` 在当前回合后停止自动连发,`resume` 恢复并在队列空闲时发送下一条,`clear` 清空待发。
|
|
410
|
+
- 如果某轮以错误/中止结束,该条会标记为 **失败** 并暂停自动连发;点「重置队列」可把卡在 `sent` 的条目退回待发并重试。
|
|
391
411
|
- 队列按对话保存,插件重载后仍在。
|
|
392
412
|
|
|
393
413
|
## 项目
|
package/client.js
CHANGED
|
@@ -77,6 +77,7 @@ window.__ModuleLoader__.load({
|
|
|
77
77
|
relayNtfy: '免费公共中继 ntfy.sh',
|
|
78
78
|
relayOffHint: '关闭后不再轮询中继,令牌会保留,切回时无需重填。',
|
|
79
79
|
relayHubUrl: 'Hub 地址',
|
|
80
|
+
relayLocalhostHint: '127.0.0.1 / localhost 只在本机可访问;另一台设备请填这台机器的局域网 IP 或 HTTPS 隧道地址。',
|
|
80
81
|
relayRoom: '房间',
|
|
81
82
|
relayToken: '令牌',
|
|
82
83
|
relayTokenKeep: '留空保留已保存的令牌',
|
|
@@ -143,6 +144,10 @@ window.__ModuleLoader__.load({
|
|
|
143
144
|
queueUntitled: '未命名任务',
|
|
144
145
|
queueAuto: '自动连发',
|
|
145
146
|
queuePaused: '已暂停',
|
|
147
|
+
queueFailed: '失败',
|
|
148
|
+
queueReset: '重置队列',
|
|
149
|
+
queueLoadFailed: '读取队列失败',
|
|
150
|
+
queueLastError: '最近错误',
|
|
146
151
|
stageAdded: '阶段已添加。',
|
|
147
152
|
stageStarted: '阶段已开始。',
|
|
148
153
|
stageFinished: '阶段已完成。',
|
|
@@ -230,6 +235,7 @@ window.__ModuleLoader__.load({
|
|
|
230
235
|
relayNtfy: 'Free public relay ntfy.sh',
|
|
231
236
|
relayOffHint: 'Turning it off stops polling and keeps the token for next time.',
|
|
232
237
|
relayHubUrl: 'Hub URL',
|
|
238
|
+
relayLocalhostHint: '127.0.0.1 / localhost only works on this machine; on another device use this machine’s LAN address or an HTTPS tunnel URL.',
|
|
233
239
|
relayRoom: 'Room',
|
|
234
240
|
relayToken: 'Token',
|
|
235
241
|
relayTokenKeep: 'Leave blank to keep the saved token',
|
|
@@ -296,6 +302,10 @@ window.__ModuleLoader__.load({
|
|
|
296
302
|
queueUntitled: 'Untitled task',
|
|
297
303
|
queueAuto: 'Auto-advance',
|
|
298
304
|
queuePaused: 'Paused',
|
|
305
|
+
queueFailed: 'Failed',
|
|
306
|
+
queueReset: 'Reset queue',
|
|
307
|
+
queueLoadFailed: 'Failed to load the queue',
|
|
308
|
+
queueLastError: 'Last error',
|
|
299
309
|
stageAdded: 'Stage added.',
|
|
300
310
|
stageStarted: 'Stage started.',
|
|
301
311
|
stageFinished: 'Stage finished.',
|
|
@@ -423,8 +433,12 @@ window.__ModuleLoader__.load({
|
|
|
423
433
|
const [queueTitle, setQueueTitle] = React.useState('')
|
|
424
434
|
const [queuePrompt, setQueuePrompt] = React.useState('')
|
|
425
435
|
const [queueBusy, setQueueBusy] = React.useState(false)
|
|
436
|
+
const [queueError, setQueueError] = React.useState(null)
|
|
426
437
|
const commandsRef = React.useRef(commands)
|
|
427
438
|
commandsRef.current = commands
|
|
439
|
+
const workIdRef = React.useRef(workId)
|
|
440
|
+
workIdRef.current = workId
|
|
441
|
+
const queueReadSeq = React.useRef(0)
|
|
428
442
|
|
|
429
443
|
React.useEffect(() => {
|
|
430
444
|
if (workId !== undefined) return
|
|
@@ -509,6 +523,7 @@ window.__ModuleLoader__.load({
|
|
|
509
523
|
setError(null)
|
|
510
524
|
try {
|
|
511
525
|
await loadBoard(workId)
|
|
526
|
+
if (!queueBusy) await readQueue(workId)
|
|
512
527
|
const relay = await readRelay(workId).catch(() => null)
|
|
513
528
|
if (relay !== null && relay.mode !== 'off') await loadAliases(workId).catch(() => undefined)
|
|
514
529
|
} catch (cause) {
|
|
@@ -582,32 +597,51 @@ window.__ModuleLoader__.load({
|
|
|
582
597
|
return value
|
|
583
598
|
}
|
|
584
599
|
|
|
585
|
-
/**
|
|
586
|
-
const readQueue = async (sessionId) => {
|
|
600
|
+
/** Read one session's queue; stale session responses are discarded. */
|
|
601
|
+
const readQueue = async (sessionId, isCancelled) => {
|
|
602
|
+
const seq = ++queueReadSeq.current
|
|
603
|
+
const stale = () => (typeof isCancelled === 'function' && isCancelled())
|
|
604
|
+
|| workIdRef.current !== sessionId || seq !== queueReadSeq.current
|
|
587
605
|
try {
|
|
588
606
|
const value = await runCommand(commandsRef.current, sessionId, '/collab queue {"action":"list"}')
|
|
589
|
-
if (value !== null && typeof value === 'object' && Array.isArray(value.items)
|
|
607
|
+
if (value !== null && typeof value === 'object' && Array.isArray(value.items)
|
|
608
|
+
&& value.sessionId === sessionId && !stale()) {
|
|
609
|
+
setQueue(value)
|
|
610
|
+
setQueueError(null)
|
|
611
|
+
}
|
|
590
612
|
return value
|
|
591
|
-
} catch {
|
|
592
|
-
|
|
613
|
+
} catch (cause) {
|
|
614
|
+
if (!stale()) {
|
|
615
|
+
setQueue(null)
|
|
616
|
+
setQueueError(cause instanceof Error ? cause.message : String(cause))
|
|
617
|
+
}
|
|
593
618
|
return null
|
|
594
619
|
}
|
|
595
620
|
}
|
|
596
621
|
|
|
597
622
|
const queueAction = async (action, extra = {}) => {
|
|
598
623
|
if (workId === undefined) return
|
|
624
|
+
const sessionId = workId
|
|
625
|
+
const seq = ++queueReadSeq.current
|
|
599
626
|
setQueueBusy(true)
|
|
600
627
|
setError(null)
|
|
601
628
|
setNotice(null)
|
|
629
|
+
setQueueError(null)
|
|
602
630
|
try {
|
|
603
|
-
const value = await runCommand(commandsRef.current,
|
|
604
|
-
|
|
605
|
-
if (
|
|
631
|
+
const value = await runCommand(commandsRef.current, sessionId, `/collab queue ${JSON.stringify({ action, ...extra })}`)
|
|
632
|
+
const current = workIdRef.current === sessionId && seq === queueReadSeq.current
|
|
633
|
+
if (current && value !== null && typeof value === 'object' && Array.isArray(value.items) && value.sessionId === sessionId) {
|
|
634
|
+
setQueue(value)
|
|
635
|
+
setQueueError(null)
|
|
636
|
+
}
|
|
637
|
+
if (action === 'add' && current) {
|
|
606
638
|
setQueueTitle('')
|
|
607
639
|
setQueuePrompt('')
|
|
608
640
|
}
|
|
609
641
|
} catch (cause) {
|
|
610
|
-
|
|
642
|
+
if (workIdRef.current === sessionId && seq === queueReadSeq.current) {
|
|
643
|
+
setError(cause instanceof Error ? cause.message : String(cause))
|
|
644
|
+
}
|
|
611
645
|
} finally {
|
|
612
646
|
setQueueBusy(false)
|
|
613
647
|
}
|
|
@@ -616,9 +650,16 @@ window.__ModuleLoader__.load({
|
|
|
616
650
|
React.useEffect(() => {
|
|
617
651
|
if (workId === undefined) {
|
|
618
652
|
setQueue(null)
|
|
619
|
-
|
|
653
|
+
setQueueError(null)
|
|
654
|
+
return undefined
|
|
655
|
+
}
|
|
656
|
+
let cancelled = false
|
|
657
|
+
setQueue(null)
|
|
658
|
+
setQueueError(null)
|
|
659
|
+
void readQueue(workId, () => cancelled)
|
|
660
|
+
return () => {
|
|
661
|
+
cancelled = true
|
|
620
662
|
}
|
|
621
|
-
void readQueue(workId)
|
|
622
663
|
}, [workId])
|
|
623
664
|
|
|
624
665
|
const runAction = async (line, successText) => {
|
|
@@ -866,7 +907,7 @@ window.__ModuleLoader__.load({
|
|
|
866
907
|
h('option', { value: 'en' }, t('langEn'))),
|
|
867
908
|
),
|
|
868
909
|
h('div', { style: S.row },
|
|
869
|
-
h('button', { type: 'button', style: S.secondary, disabled: busy || workId === undefined, onClick: () => void refresh() }, t('refresh')),
|
|
910
|
+
h('button', { type: 'button', style: S.secondary, disabled: busy || queueBusy || workId === undefined, onClick: () => void refresh() }, t('refresh')),
|
|
870
911
|
busy ? h('span', { style: S.muted }, t('busy')) : null,
|
|
871
912
|
),
|
|
872
913
|
),
|
|
@@ -960,6 +1001,9 @@ window.__ModuleLoader__.load({
|
|
|
960
1001
|
onChange: (event) => setRelayHubUrl(event.target.value),
|
|
961
1002
|
}))
|
|
962
1003
|
: null,
|
|
1004
|
+
relayMode === 'hub' && /^https?:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/i.test(relayHubUrl.trim())
|
|
1005
|
+
? h('p', { style: S.hint }, t('relayLocalhostHint'))
|
|
1006
|
+
: null,
|
|
963
1007
|
relayMode === 'off'
|
|
964
1008
|
? null
|
|
965
1009
|
: h('div', { style: { display: 'flex', gap: 8, flexWrap: 'wrap' } },
|
|
@@ -1166,6 +1210,9 @@ window.__ModuleLoader__.load({
|
|
|
1166
1210
|
h('span', { style: S.badge }, queue !== null && queue.auto === false ? t('queuePaused') : t('queueAuto')),
|
|
1167
1211
|
),
|
|
1168
1212
|
h('p', { style: S.intro }, t('queueIntro')),
|
|
1213
|
+
queue !== null && typeof queue.lastError === 'string' && queue.lastError !== ''
|
|
1214
|
+
? h('p', { style: { ...S.notice, color: 'var(--dsw-alias-state-error-primary)' } }, t('queueLastError') + ':' + queue.lastError)
|
|
1215
|
+
: null,
|
|
1169
1216
|
h('label', { style: S.label }, t('queueAddTitle'),
|
|
1170
1217
|
h('input', {
|
|
1171
1218
|
style: S.input,
|
|
@@ -1207,16 +1254,29 @@ window.__ModuleLoader__.load({
|
|
|
1207
1254
|
disabled: queueBusy || queue === null || !queueItems.some((item) => item.status === 'pending'),
|
|
1208
1255
|
onClick: () => void queueAction('clear'),
|
|
1209
1256
|
}, t('queueClear')),
|
|
1257
|
+
queue !== null && ((queue.active !== null && queue.active !== undefined) || (typeof queue.lastError === 'string' && queue.lastError !== ''))
|
|
1258
|
+
? h('button', {
|
|
1259
|
+
type: 'button',
|
|
1260
|
+
style: S.secondary,
|
|
1261
|
+
disabled: queueBusy,
|
|
1262
|
+
onClick: () => void queueAction('reset'),
|
|
1263
|
+
}, t('queueReset'))
|
|
1264
|
+
: null,
|
|
1210
1265
|
),
|
|
1211
|
-
|
|
1212
|
-
? h('p', { style: S.
|
|
1213
|
-
:
|
|
1214
|
-
? h('p', { style: S.intro }, t('
|
|
1215
|
-
:
|
|
1216
|
-
|
|
1266
|
+
queueError !== null
|
|
1267
|
+
? h('p', { style: { ...S.notice, color: 'var(--dsw-alias-state-error-primary)' } }, t('queueLoadFailed') + ':' + queueError)
|
|
1268
|
+
: queue === null
|
|
1269
|
+
? h('p', { style: S.intro }, t('loading'))
|
|
1270
|
+
: queueItems.length === 0
|
|
1271
|
+
? h('p', { style: S.intro }, t('queueEmpty'))
|
|
1272
|
+
: h('div', { style: { display: 'flex', flexDirection: 'column', gap: 8 } },
|
|
1273
|
+
queueItems.map((item, index) => h('div', { key: item.id, style: S.worker },
|
|
1217
1274
|
h('div', { style: S.workerMain },
|
|
1218
1275
|
h('div', { style: S.workerName }, `${index + 1}. ${item.title || t('queueUntitled')}`),
|
|
1219
|
-
|
|
1276
|
+
item.status === 'failed'
|
|
1277
|
+
? h('span', { style: { ...S.badge, alignSelf: 'flex-start', color: 'var(--dsw-alias-state-error-primary)' } }, t('queueFailed'))
|
|
1278
|
+
: null,
|
|
1279
|
+
h('div', { style: S.muted }, `${item.status === 'pending' ? t('queuePending') : item.status === 'sent' ? t('queueSent') : item.status === 'failed' ? t('queueFailed') : t('queueDone')} · ${item.prompt}`),
|
|
1220
1280
|
),
|
|
1221
1281
|
item.status === 'pending'
|
|
1222
1282
|
? h('button', {
|
package/hub.js
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
// for dispatch/report round trips; it is deliberately not a durable queue.
|
|
18
18
|
|
|
19
19
|
import { createHash, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
20
|
+
import { networkInterfaces } from 'node:os'
|
|
20
21
|
import { createServer } from 'node:http'
|
|
21
22
|
|
|
22
23
|
const args = new Map()
|
|
@@ -192,6 +193,11 @@ const server = createServer(async (request, response) => {
|
|
|
192
193
|
|
|
193
194
|
server.listen(port, host, () => {
|
|
194
195
|
console.log(`collab-pair hub listening on http://${host}:${port}`)
|
|
196
|
+
const lan = Object.values(networkInterfaces()).flat()
|
|
197
|
+
.filter((entry) => entry !== undefined && entry.family === 'IPv4' && entry.internal !== true)
|
|
198
|
+
.map((entry) => `http://${entry.address}:${port}`)
|
|
199
|
+
if (lan.length > 0) console.log(`LAN addresses: ${lan.join(' ')}`)
|
|
195
200
|
console.log(`token: ${token}`)
|
|
201
|
+
console.log('127.0.0.1/localhost only works on this machine; other devices must use a LAN address or an HTTPS tunnel')
|
|
196
202
|
console.log('put the hub behind HTTPS before using it across untrusted networks')
|
|
197
203
|
})
|
package/index.js
CHANGED
|
@@ -41,7 +41,9 @@ const DONE = 'done'
|
|
|
41
41
|
const STOPPED = 'stopped'
|
|
42
42
|
const REPORTS = new Set(['done', 'partial', 'blocked'])
|
|
43
43
|
const PROMPT_TIMEOUT_MS = 180000
|
|
44
|
-
const
|
|
44
|
+
const QUEUE_LIMIT = 100
|
|
45
|
+
const QUEUE_DONE_LIMIT = 20
|
|
46
|
+
const ENGINE = 'collab-pair/1.5.3'
|
|
45
47
|
const PREFIX_DEFAULTS = {
|
|
46
48
|
zh: { manager: '【项目经理】', worker: '【员工】' },
|
|
47
49
|
en: { manager: '[Manager]', worker: '[Worker]' },
|
|
@@ -289,6 +291,7 @@ const LANGUAGES = {
|
|
|
289
291
|
'',
|
|
290
292
|
'规则:',
|
|
291
293
|
'- 默认自主创建员工:需要人干活时,先用 `collab_worker_create` 建立合适的员工(可按角色建多个),再派发;不要等用户手动新建。',
|
|
294
|
+
'- 远端设备上的员工用 `collab_remote` 关联(先在设置页的「跨设备协作」里配置中继);本机员工才用 `collab_worker_create`,不要重复创建。',
|
|
292
295
|
'- 不要自己承担员工的具体执行工作。你负责拆解、派发、验收和决定下一步;自己动手只限于规划、审阅和必要协调,避免因长时间执行而错过员工回报。',
|
|
293
296
|
'- 用 `collab_dispatch` 派发任务;有多个员工时用 `worker` 指定员工(sessionId 或标题),只有一个时可省略。',
|
|
294
297
|
'- 阶段队列:用 `collab_stage` 添加/查看;当前阶段验收通过后调用 `collab_stage({action:"complete"})`,会自动派发下一阶段。',
|
|
@@ -429,6 +432,7 @@ const LANGUAGES = {
|
|
|
429
432
|
'',
|
|
430
433
|
'Rules:',
|
|
431
434
|
'- Create workers by default: when work needs doing, use `collab_worker_create` to make the right worker(s) (multiple roles if needed), then dispatch. Do not wait for the user to create them manually.',
|
|
435
|
+
'- For a worker on another device use `collab_remote` (configure the relay in the Settings cross-device card first); only create a local worker with `collab_worker_create`. Do not duplicate it.',
|
|
432
436
|
'- Do not do the workers\' execution work yourself. You break down, dispatch, review and decide; your own hands-on work should be limited to planning, review and necessary coordination, so a long execution does not make you miss worker reports.',
|
|
433
437
|
'- Dispatch a round with `collab_dispatch`; when several workers exist, select one with `worker` (session id or title). With a single worker you may omit it.',
|
|
434
438
|
'- Stage queue: manage it with `collab_stage`; when the current stage passes acceptance, call `collab_stage({action:"complete"})` to dispatch the next stage automatically.',
|
|
@@ -590,6 +594,7 @@ export function apply(ctx, rawConfig) {
|
|
|
590
594
|
setTimeout(() => { void recoverPendingLinks() }, 5000)
|
|
591
595
|
setTimeout(() => { void backfillProjects() }, 3000)
|
|
592
596
|
setTimeout(() => { void relocalizePrefixes() }, 4000)
|
|
597
|
+
setTimeout(() => { if (alive) void recoverQueues() }, 6000)
|
|
593
598
|
setTimeout(() => { if (alive) void remoteLoop() }, 2000)
|
|
594
599
|
|
|
595
600
|
registerTools()
|
|
@@ -808,11 +813,11 @@ export function apply(ctx, rawConfig) {
|
|
|
808
813
|
|
|
809
814
|
function normalizeQueue(value, sessionId) {
|
|
810
815
|
const items = []
|
|
811
|
-
for (const item of Array.isArray(value?.items) ? value.items
|
|
816
|
+
for (const item of Array.isArray(value?.items) ? value.items : []) {
|
|
812
817
|
if (item === null || typeof item !== 'object') continue
|
|
813
818
|
const prompt = typeof item.prompt === 'string' ? item.prompt.trim() : ''
|
|
814
819
|
if (prompt === '') continue
|
|
815
|
-
const status = item.status === 'sent' || item.status === 'done' ? item.status : 'pending'
|
|
820
|
+
const status = item.status === 'sent' || item.status === 'done' || item.status === 'failed' ? item.status : 'pending'
|
|
816
821
|
items.push({
|
|
817
822
|
id: typeof item.id === 'string' ? item.id : `q-${randomUUID().slice(0, 8)}`,
|
|
818
823
|
title: typeof item.title === 'string' ? item.title : '',
|
|
@@ -824,17 +829,26 @@ export function apply(ctx, rawConfig) {
|
|
|
824
829
|
...(typeof item.marker === 'string' ? { marker: item.marker } : {}),
|
|
825
830
|
})
|
|
826
831
|
}
|
|
832
|
+
// Keep every pending/sent/failed item; only trim the oldest done history.
|
|
833
|
+
const doneIndexes = []
|
|
834
|
+
for (let index = 0; index < items.length; index++) {
|
|
835
|
+
if (items[index].status === 'done') doneIndexes.push(index)
|
|
836
|
+
}
|
|
837
|
+
const keepDone = new Set(doneIndexes.slice(-QUEUE_DONE_LIMIT))
|
|
838
|
+
const kept = items.filter((item, index) => item.status !== 'done' || keepDone.has(index))
|
|
827
839
|
let activeId = typeof value?.activeId === 'string' ? value.activeId : null
|
|
828
|
-
if (activeId !== null && !
|
|
840
|
+
if (activeId !== null && !kept.some((item) => item.id === activeId && item.status === 'sent')) activeId = null
|
|
829
841
|
if (activeId === null) {
|
|
830
|
-
const sent =
|
|
842
|
+
const sent = kept.find((item) => item.status === 'sent')
|
|
831
843
|
if (sent !== undefined) activeId = sent.id
|
|
832
844
|
}
|
|
833
845
|
return {
|
|
834
846
|
sessionId,
|
|
835
|
-
items,
|
|
847
|
+
items: kept,
|
|
836
848
|
activeId,
|
|
837
849
|
auto: value?.auto !== false,
|
|
850
|
+
started: value?.started === true || kept.some((item) => item.status === 'sent' || item.status === 'done' || item.status === 'failed'),
|
|
851
|
+
lastError: typeof value?.lastError === 'string' && value.lastError !== '' ? value.lastError : null,
|
|
838
852
|
updatedAt: Number.isFinite(value?.updatedAt) ? value.updatedAt : Date.now(),
|
|
839
853
|
}
|
|
840
854
|
}
|
|
@@ -872,6 +886,7 @@ export function apply(ctx, rawConfig) {
|
|
|
872
886
|
sessionId: queue.sessionId,
|
|
873
887
|
auto: queue.auto,
|
|
874
888
|
active: queue.activeId,
|
|
889
|
+
lastError: queue.lastError ?? null,
|
|
875
890
|
items: queue.items.map((item) => ({
|
|
876
891
|
id: item.id,
|
|
877
892
|
title: item.title,
|
|
@@ -897,6 +912,8 @@ export function apply(ctx, rawConfig) {
|
|
|
897
912
|
next.requestId = requestId
|
|
898
913
|
next.marker = marker
|
|
899
914
|
queue.activeId = next.id
|
|
915
|
+
queue.started = true
|
|
916
|
+
queue.lastError = null
|
|
900
917
|
queue.updatedAt = Date.now()
|
|
901
918
|
await persistQueue(queue)
|
|
902
919
|
const text = [
|
|
@@ -916,6 +933,7 @@ export function apply(ctx, rawConfig) {
|
|
|
916
933
|
delete next.requestId
|
|
917
934
|
delete next.marker
|
|
918
935
|
queue.activeId = null
|
|
936
|
+
queue.lastError = clip(error instanceof Error ? error.message : String(error), 500)
|
|
919
937
|
queue.updatedAt = Date.now()
|
|
920
938
|
await persistQueue(queue)
|
|
921
939
|
throw error
|
|
@@ -939,30 +957,113 @@ export function apply(ctx, rawConfig) {
|
|
|
939
957
|
return
|
|
940
958
|
}
|
|
941
959
|
if (events === undefined || !claimedQueue(events, active)) return
|
|
960
|
+
const reason = event.data?.reason
|
|
961
|
+
const kind = typeof reason?.kind === 'string' ? reason.kind : (typeof reason === 'string' && reason !== '' ? reason : 'unknown')
|
|
962
|
+
if (kind !== 'completed' && kind !== 'max-tokens') {
|
|
963
|
+
active.status = 'failed'
|
|
964
|
+
queue.activeId = null
|
|
965
|
+
queue.auto = false
|
|
966
|
+
queue.lastError = '本轮结束原因:' + kind + ';未自动续发。'
|
|
967
|
+
queue.updatedAt = Date.now()
|
|
968
|
+
await persistQueue(queue)
|
|
969
|
+
return
|
|
970
|
+
}
|
|
942
971
|
active.status = 'done'
|
|
943
972
|
active.completedAt = Date.now()
|
|
944
973
|
queue.activeId = null
|
|
974
|
+
queue.lastError = null
|
|
945
975
|
queue.updatedAt = Date.now()
|
|
976
|
+
trimQueue(queue)
|
|
946
977
|
await persistQueue(queue)
|
|
947
978
|
if (!queue.auto) return
|
|
948
979
|
if (queue.items.some((item) => item.status === 'pending')) {
|
|
949
980
|
try {
|
|
950
981
|
await startQueueItem(session.id, AbortSignal.timeout(PROMPT_TIMEOUT_MS))
|
|
951
982
|
} catch (error) {
|
|
983
|
+
queue.lastError = clip(error instanceof Error ? error.message : String(error), 500)
|
|
984
|
+
queue.updatedAt = Date.now()
|
|
985
|
+
await persistQueue(queue)
|
|
952
986
|
ctx.logger.warn(`collab-pair: sending the next queue item for ${session.id} failed: ${String(error)}`)
|
|
953
987
|
}
|
|
954
988
|
}
|
|
955
989
|
}
|
|
956
990
|
|
|
957
991
|
function claimedQueue(events, item) {
|
|
992
|
+
if (typeof item.requestId !== 'string' || item.requestId === '') return false
|
|
958
993
|
for (const event of events) {
|
|
959
994
|
if (event.type !== 'user/message') continue
|
|
960
|
-
if (
|
|
961
|
-
if (typeof item.marker === 'string' && messageText(event.data).includes(item.marker)) return true
|
|
995
|
+
if (event.data?.source?.rpcId === item.requestId) return true
|
|
962
996
|
}
|
|
963
997
|
return false
|
|
964
998
|
}
|
|
965
999
|
|
|
1000
|
+
/** Keep all live items but only the newest done history. */
|
|
1001
|
+
function trimQueue(queue) {
|
|
1002
|
+
const done = queue.items.filter((item) => item.status === 'done')
|
|
1003
|
+
if (done.length <= QUEUE_DONE_LIMIT) return
|
|
1004
|
+
const keep = new Set(done.slice(-QUEUE_DONE_LIMIT).map((item) => item.id))
|
|
1005
|
+
queue.items = queue.items.filter((item) => item.status !== 'done' || keep.has(item.id))
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/** Return every sent item to pending and clear the active/failure state. */
|
|
1009
|
+
async function resetQueue(sessionId) {
|
|
1010
|
+
const queue = await readQueue(sessionId)
|
|
1011
|
+
for (const item of queue.items) {
|
|
1012
|
+
if (item.status !== 'sent') continue
|
|
1013
|
+
item.status = 'pending'
|
|
1014
|
+
delete item.sentAt
|
|
1015
|
+
delete item.requestId
|
|
1016
|
+
delete item.marker
|
|
1017
|
+
}
|
|
1018
|
+
queue.activeId = null
|
|
1019
|
+
queue.lastError = null
|
|
1020
|
+
queue.updatedAt = Date.now()
|
|
1021
|
+
await persistQueue(queue)
|
|
1022
|
+
return queue
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
/** Resume idle auto-advance queues after activation. */
|
|
1026
|
+
async function recoverQueues() {
|
|
1027
|
+
try {
|
|
1028
|
+
const candidates = new Map(queues)
|
|
1029
|
+
const domain = await remoteReady.catch(() => undefined)
|
|
1030
|
+
if (domain !== undefined) {
|
|
1031
|
+
try {
|
|
1032
|
+
for (const [key, value] of domain.table('state').entries()) {
|
|
1033
|
+
if (typeof key !== 'string' || !key.startsWith('queue:')) continue
|
|
1034
|
+
const sessionId = key.slice('queue:'.length)
|
|
1035
|
+
if (sessionId === '') continue
|
|
1036
|
+
if (!queues.has(sessionId)) queues.set(sessionId, normalizeQueue(value, sessionId))
|
|
1037
|
+
candidates.set(sessionId, queues.get(sessionId))
|
|
1038
|
+
}
|
|
1039
|
+
} catch (error) {
|
|
1040
|
+
ctx.logger.warn('collab-pair: scanning queues for recovery failed: ' + String(error))
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
for (const [sessionId, queue] of candidates) {
|
|
1044
|
+
if (!alive) return
|
|
1045
|
+
if (queue === undefined || queue.auto !== true || queue.started !== true || queue.activeId !== null) continue
|
|
1046
|
+
if (!queue.items.some((item) => item.status === 'pending')) continue
|
|
1047
|
+
try {
|
|
1048
|
+
const current = await readQueue(sessionId)
|
|
1049
|
+
if (current.auto !== true || current.started !== true || current.activeId !== null) continue
|
|
1050
|
+
if (!current.items.some((item) => item.status === 'pending')) continue
|
|
1051
|
+
await startQueueItem(sessionId, AbortSignal.timeout(PROMPT_TIMEOUT_MS))
|
|
1052
|
+
} catch (error) {
|
|
1053
|
+
const current = await readQueue(sessionId).catch(() => undefined)
|
|
1054
|
+
if (current !== undefined && current.activeId === null) {
|
|
1055
|
+
current.lastError = clip(error instanceof Error ? error.message : String(error), 500)
|
|
1056
|
+
current.updatedAt = Date.now()
|
|
1057
|
+
await persistQueue(current)
|
|
1058
|
+
}
|
|
1059
|
+
ctx.logger.warn('collab-pair: recovering the queue for ' + sessionId + ' failed: ' + String(error))
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
} catch (error) {
|
|
1063
|
+
ctx.logger.warn('collab-pair: queue recovery failed: ' + String(error))
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
|
|
966
1067
|
function hubConfigured() {
|
|
967
1068
|
return cfg.relayMode === 'hub' && cfg.hubUrl !== '' && cfg.hubToken !== ''
|
|
968
1069
|
}
|
|
@@ -2366,7 +2467,7 @@ export function apply(ctx, rawConfig) {
|
|
|
2366
2467
|
toolExecutors.remote = register({
|
|
2367
2468
|
name: 'collab_remote',
|
|
2368
2469
|
description:
|
|
2369
|
-
'
|
|
2470
|
+
'跨设备协作:在设置页的「跨设备协作」里选择中继并保存(自建 hub 或免费 ntfy.sh)。action=alias 在员工设备上把当前对话注册成别名;action=link 在项目经理设备上关联远端设备上的别名;action=aliases / remove-alias / status 查看或维护。',
|
|
2370
2471
|
parameters: {
|
|
2371
2472
|
type: 'object',
|
|
2372
2473
|
properties: {
|
|
@@ -3121,6 +3222,9 @@ export function apply(ctx, rawConfig) {
|
|
|
3121
3222
|
if (sub === 'add') {
|
|
3122
3223
|
const prompt = typeof args?.prompt === 'string' ? args.prompt.trim() : ''
|
|
3123
3224
|
if (prompt === '') return { kind: 'error', text: 'prompt 不能为空。' }
|
|
3225
|
+
if (queue.items.length >= QUEUE_LIMIT) {
|
|
3226
|
+
return { kind: 'error', text: '队列最多 ' + QUEUE_LIMIT + ' 条;请先清理待发或已完成条目。' }
|
|
3227
|
+
}
|
|
3124
3228
|
queue.items.push({
|
|
3125
3229
|
id: `q-${randomUUID().slice(0, 8)}`,
|
|
3126
3230
|
title: typeof args?.title === 'string' ? args.title.trim() : '',
|
|
@@ -3139,11 +3243,22 @@ export function apply(ctx, rawConfig) {
|
|
|
3139
3243
|
queue.auto = sub === 'resume'
|
|
3140
3244
|
queue.updatedAt = Date.now()
|
|
3141
3245
|
await persistQueue(queue)
|
|
3246
|
+
if (sub === 'resume' && queue.activeId === null && queue.items.some((item) => item.status === 'pending')) {
|
|
3247
|
+
const { queue: started, item } = await startQueueItem(agent.id, invocation.signal)
|
|
3248
|
+
return { kind: 'success', text: JSON.stringify({ ...queueView(started), started: item.id }) }
|
|
3249
|
+
}
|
|
3142
3250
|
return { kind: 'success', text: JSON.stringify(queueView(queue)) }
|
|
3143
3251
|
}
|
|
3252
|
+
if (sub === 'reset') {
|
|
3253
|
+
const reset = await resetQueue(agent.id)
|
|
3254
|
+
return { kind: 'success', text: JSON.stringify(queueView(reset)) }
|
|
3255
|
+
}
|
|
3144
3256
|
if (sub === 'remove') {
|
|
3145
|
-
const index =
|
|
3146
|
-
|
|
3257
|
+
const index = Number(args?.index)
|
|
3258
|
+
if (!Number.isInteger(index) || index < 1 || index > queue.items.length) {
|
|
3259
|
+
return { kind: 'error', text: 'index 必须是 1 到当前条目数之间的整数。' }
|
|
3260
|
+
}
|
|
3261
|
+
const item = queue.items[index - 1]
|
|
3147
3262
|
if (item === undefined) return { kind: 'error', text: 'index 超出范围。' }
|
|
3148
3263
|
if (item.status !== 'pending') return { kind: 'error', text: '只能删除尚未发送的条目。' }
|
|
3149
3264
|
queue.items.splice(index - 1, 1)
|
|
@@ -3157,7 +3272,7 @@ export function apply(ctx, rawConfig) {
|
|
|
3157
3272
|
await persistQueue(queue)
|
|
3158
3273
|
return { kind: 'success', text: JSON.stringify(queueView(queue)) }
|
|
3159
3274
|
}
|
|
3160
|
-
return { kind: 'error', text: 'action 必须是 list | add | start | pause | resume | remove | clear。' }
|
|
3275
|
+
return { kind: 'error', text: 'action 必须是 list | add | start | pause | resume | reset | remove | clear。' }
|
|
3161
3276
|
}
|
|
3162
3277
|
if (action === 'manage') {
|
|
3163
3278
|
const agent = invocation.agent
|
|
@@ -3205,14 +3320,18 @@ export function apply(ctx, rawConfig) {
|
|
|
3205
3320
|
if (sub === 'test') {
|
|
3206
3321
|
if (!relayConfigured()) return { kind: 'error', text: '中继未启用:先选择模式并保存。' }
|
|
3207
3322
|
try {
|
|
3323
|
+
let message = ''
|
|
3208
3324
|
if (cfg.relayMode === 'hub') {
|
|
3209
3325
|
await hubFetch(`/v1/head?room=${encodeURIComponent(cfg.hubRoom)}`)
|
|
3326
|
+
if (/^https?:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/i.test(cfg.hubUrl)) {
|
|
3327
|
+
message = '连接成功,但这个地址只在本机可访问;跨设备请改用局域网 IP 或 HTTPS 隧道地址。'
|
|
3328
|
+
}
|
|
3210
3329
|
} else {
|
|
3211
3330
|
const response = await fetch(`https://ntfy.sh/${encodeURIComponent(ntfyTopic())}/json?poll=1&since=10m`, { signal: AbortSignal.timeout(15000) })
|
|
3212
3331
|
if (!response.ok) throw new Error(`HTTP ${response.status}`)
|
|
3213
3332
|
await response.text()
|
|
3214
3333
|
}
|
|
3215
|
-
return { kind: 'success', text: JSON.stringify({ ok: true, mode: cfg.relayMode, device: cfg.deviceName, room: cfg.hubRoom }) }
|
|
3334
|
+
return { kind: 'success', text: JSON.stringify({ ok: true, mode: cfg.relayMode, device: cfg.deviceName, room: cfg.hubRoom, ...(message === '' ? {} : { message }) }) }
|
|
3216
3335
|
} catch (error) {
|
|
3217
3336
|
return { kind: 'error', text: `连接测试失败:${error instanceof Error ? error.message : String(error)}` }
|
|
3218
3337
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-collab-pair",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "DSH collaboration plugin: link a project-manager conversation with local or remote workers across devices, dispatch rounds, relay results and manage projects/stages automatically. Also ships a prompt to-do queue for any conversation: pre-write a series of prompts and the next one is sent when a turn ends. Includes a relay hub and a free ntfy.sh transport.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|