@lyra_wang/dsh-pending-queue 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +60 -2
- package/client.js +624 -0
- package/cordis.patch.yml +5 -0
- package/index.js +306 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wang Ying
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 待发送队列(pending-queue)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
右侧边栏里的一块草稿区:把还没准备发的想法先存下来,等手上这一件事做完,再挑一条推进当前会话。
|
|
4
|
+
|
|
5
|
+
## 它解决什么
|
|
6
|
+
|
|
7
|
+
DSH 自己的「排队」解决的是**同一个任务里**的插话:agent 跑着的时候你打的字按顺序排着,这一轮结束自动发出去。它解决不了另一种情况——
|
|
8
|
+
|
|
9
|
+
> 我又想起一件**独立**的事,但现在这个功能还没开发完,不能让它插进来;先记下来,等这轮收尾了再发。
|
|
10
|
+
|
|
11
|
+
这两件事长得像,其实不是一件事:前者是「排队发送」,后者是「攒着稍后发」。混在一起做,用户会发现关掉会话就丢了。所以这个插件单独存一份队列,什么时候推由你决定。
|
|
12
|
+
|
|
13
|
+
## 安装
|
|
14
|
+
|
|
15
|
+
插件以 npm 包形式分发,装进一个 DSH profile:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# 从 npm(发布后)
|
|
19
|
+
dsh plugin --profile desktop add @lyra_wang/dsh-pending-queue
|
|
20
|
+
|
|
21
|
+
# 本地开发:先打包,再从 tarball 装
|
|
22
|
+
npm pack
|
|
23
|
+
dsh plugin --profile desktop add ./lyra_wang-dsh-pending-queue-0.1.0.tgz
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
装完重启 DSH,右侧栏的「添加 tab」里会出现「待发送」入口。
|
|
27
|
+
|
|
28
|
+
## 用法
|
|
29
|
+
|
|
30
|
+
1. 在右侧栏打开「待发送」。
|
|
31
|
+
2. 在顶部输入框里记一条,`Cmd/Ctrl + Enter` 或点「存下」。
|
|
32
|
+
3. 每一条可以:
|
|
33
|
+
- **发送**:内容进输入框并立刻提交(agent 正在跑时会排进队列,不会打断当前任务)
|
|
34
|
+
- **填入**:只放进输入框,你自己改两笔再回车
|
|
35
|
+
- **编辑 / 删除 / ↑ ↓**:改内容、删掉、调整顺序
|
|
36
|
+
4. 发过的行标「已发送」,可以「取回」重新排队,或一键「清空已发送」。
|
|
37
|
+
|
|
38
|
+
## 实现要点
|
|
39
|
+
|
|
40
|
+
- **不写会话事件**。宿主半只有一份按会话拆开的 JSON,和两条同源路由(`GET /plugins/pending-queue/state`、`POST /plugins/pending-queue/action`),走 DSH 自己的连接鉴权。
|
|
41
|
+
- **发送走输入框自己的动作**:`inputActions.setDraft(text)` + `inputActions.submit()`。所以推送出去的消息和你手打的一模一样——同样的排队语义、同样的回显、同样的失败处理,插件不另造一条发送通道。
|
|
42
|
+
- **按会话存**:`<DSH_HOME>/storages/pending-queue/<sessionId>.json`。两个会话不会共用一条队列;删掉这个文件就是清空。
|
|
43
|
+
- 面板每 10 秒静默同步一次,切回窗口也会同步;写入按读-改-写串行,两个窗口同时开着不会互相覆盖。
|
|
44
|
+
|
|
45
|
+
## 已知限制
|
|
46
|
+
|
|
47
|
+
- 拿不到输入框动作(`inputActions`)的位置只提示「填入失败」,不会退化成剪贴板或 DOM 注入——那种兜底会绕开 DSH 自己的提交路径。
|
|
48
|
+
- 队列只在当前机器上,不跨设备同步。
|
|
49
|
+
- 「已发送」的行是留痕,不自动清理;不想看就点「清空已发送」。
|
|
50
|
+
|
|
51
|
+
## 验证
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
node --test test/routes.test.mjs
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
覆盖:路由注册、新增与落盘、编辑、排序、标记已发送、清空已发送、删除、空文本 / 不存在的行 / 非法会话 id 的拒绝。
|
|
58
|
+
|
|
59
|
+
## 版本
|
|
60
|
+
|
|
61
|
+
0.1.0 — 首个版本:新增、编辑、删除、排序、发送、填入、已发送留痕。
|
package/client.js
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of the pending-send queue.
|
|
3
|
+
*
|
|
4
|
+
* One tab type in the right sidebar. Rows live in this plugin's own host store
|
|
5
|
+
* (/plugins/pending-queue/*), keyed by the session the tab belongs to, so two
|
|
6
|
+
* conversations never share one queue.
|
|
7
|
+
*
|
|
8
|
+
* Pushing a row into the chat uses the composer's public input actions:
|
|
9
|
+
* setDraft() puts the text in the input box exactly as if it had been typed,
|
|
10
|
+
* and submit() sends it. While the agent is running, submit() queues the
|
|
11
|
+
* message the same way a typed message is queued, so a push never interrupts
|
|
12
|
+
* the current task.
|
|
13
|
+
*/
|
|
14
|
+
window.__ModuleLoader__.load({
|
|
15
|
+
id: '@lyra_wang/dsh-pending-queue',
|
|
16
|
+
factory(require) {
|
|
17
|
+
const React = require('react')
|
|
18
|
+
const h = React.createElement
|
|
19
|
+
const { useCallback, useEffect, useRef, useState } = React
|
|
20
|
+
|
|
21
|
+
const PKG = '@lyra_wang/dsh-pending-queue'
|
|
22
|
+
/** The tab kind this plugin owns; unique in the tab registry. */
|
|
23
|
+
const KIND = 'pending-queue'
|
|
24
|
+
/** Locale namespace for every string this tab draws. */
|
|
25
|
+
const NS = 'pendingQueue'
|
|
26
|
+
/** The plugin's own browser routes, registered by the host half. */
|
|
27
|
+
const STATE_URL = '/plugins/pending-queue/state'
|
|
28
|
+
const ACTION_URL = '/plugins/pending-queue/action'
|
|
29
|
+
const ROOT = 'dsh-pending-queue'
|
|
30
|
+
|
|
31
|
+
/* --------------------------------- copy --------------------------------- */
|
|
32
|
+
|
|
33
|
+
const ZH = {
|
|
34
|
+
'tab.title': '待发送',
|
|
35
|
+
'tab.description': '先攒着,等上一件事做完再推',
|
|
36
|
+
'queue.title': '待发送队列',
|
|
37
|
+
'queue.pending': '{n} 条待发',
|
|
38
|
+
'queue.sent': '{n} 条已发',
|
|
39
|
+
'queue.refresh': '刷新',
|
|
40
|
+
'queue.clearSent': '清空已发送',
|
|
41
|
+
'empty.title': '队列是空的',
|
|
42
|
+
'empty.body': '把脑子里冒出来的想法先记在这里。等手上的活告一段落,再决定推哪一条。',
|
|
43
|
+
'input.placeholder': '记一条想法…',
|
|
44
|
+
'input.hint': 'Cmd/Ctrl + Enter 存下。发送时内容进输入框并直接提交;运行中的会话会把它排进队列。',
|
|
45
|
+
'action.save': '存下',
|
|
46
|
+
'action.send': '发送',
|
|
47
|
+
'action.fill': '填入',
|
|
48
|
+
'action.edit': '编辑',
|
|
49
|
+
'action.remove': '删除',
|
|
50
|
+
'action.up': '上移',
|
|
51
|
+
'action.down': '下移',
|
|
52
|
+
'action.restore': '取回',
|
|
53
|
+
'action.cancel': '取消',
|
|
54
|
+
'action.saveEdit': '保存',
|
|
55
|
+
'status.sent': '已发送',
|
|
56
|
+
'notice.filled': '已填入输入框',
|
|
57
|
+
'notice.sent': '已推送',
|
|
58
|
+
'error.network': '连不上插件接口',
|
|
59
|
+
'error.noComposer': '这个位置还拿不到会话输入框,换到聊天页面再试',
|
|
60
|
+
'error.empty': '先写点什么',
|
|
61
|
+
'time.justNow': '刚刚',
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const EN = {
|
|
65
|
+
'tab.title': 'Pending',
|
|
66
|
+
'tab.description': 'Hold ideas, push them when the last task is done',
|
|
67
|
+
'queue.title': 'Pending queue',
|
|
68
|
+
'queue.pending': '{n} to send',
|
|
69
|
+
'queue.sent': '{n} sent',
|
|
70
|
+
'queue.refresh': 'Refresh',
|
|
71
|
+
'queue.clearSent': 'Clear sent',
|
|
72
|
+
'empty.title': 'Nothing queued',
|
|
73
|
+
'empty.body': 'Jot ideas down here as they arrive. When the current task is finished, decide which one to push.',
|
|
74
|
+
'input.placeholder': 'Jot one idea…',
|
|
75
|
+
'input.hint': 'Cmd/Ctrl + Enter saves. Sending puts the text in the composer and submits it; a running session queues it.',
|
|
76
|
+
'action.save': 'Save',
|
|
77
|
+
'action.send': 'Send',
|
|
78
|
+
'action.fill': 'Fill',
|
|
79
|
+
'action.edit': 'Edit',
|
|
80
|
+
'action.remove': 'Delete',
|
|
81
|
+
'action.up': 'Move up',
|
|
82
|
+
'action.down': 'Move down',
|
|
83
|
+
'action.restore': 'Restore',
|
|
84
|
+
'action.cancel': 'Cancel',
|
|
85
|
+
'action.saveEdit': 'Save',
|
|
86
|
+
'status.sent': 'Sent',
|
|
87
|
+
'notice.filled': 'Filled into the composer',
|
|
88
|
+
'notice.sent': 'Pushed',
|
|
89
|
+
'error.network': 'Cannot reach the plugin route',
|
|
90
|
+
'error.noComposer': 'The composer is not reachable from here; open the chat view and retry',
|
|
91
|
+
'error.empty': 'Write something first',
|
|
92
|
+
'time.justNow': 'just now',
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* -------------------------------- helpers -------------------------------- */
|
|
96
|
+
|
|
97
|
+
function fallbackCopy(key) {
|
|
98
|
+
return ZH[key] !== undefined ? ZH[key] : key
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function translator(props) {
|
|
102
|
+
return typeof props.t === 'function' ? props.t : fallbackCopy
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function count(t, key, n) {
|
|
106
|
+
return t(key).replace('{n}', String(n))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let relativeFormatter
|
|
110
|
+
function relativeTime(iso, t) {
|
|
111
|
+
const at = Date.parse(iso || '')
|
|
112
|
+
if (Number.isNaN(at)) return ''
|
|
113
|
+
const diff = Date.now() - at
|
|
114
|
+
if (diff < 60_000) return t('time.justNow')
|
|
115
|
+
if (relativeFormatter === undefined) {
|
|
116
|
+
try {
|
|
117
|
+
relativeFormatter = new Intl.RelativeTimeFormat(undefined, { numeric: 'auto' })
|
|
118
|
+
} catch {
|
|
119
|
+
relativeFormatter = null
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const units = [['day', 86400000], ['hour', 3600000], ['minute', 60000]]
|
|
123
|
+
for (const pair of units) {
|
|
124
|
+
if (diff >= pair[1]) {
|
|
125
|
+
const value = Math.floor(diff / pair[1])
|
|
126
|
+
if (relativeFormatter !== null) {
|
|
127
|
+
try {
|
|
128
|
+
return relativeFormatter.format(-value, pair[0])
|
|
129
|
+
} catch {
|
|
130
|
+
/* fall through to a plain label */
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return String(value) + ' ' + pair[0]
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return t('time.justNow')
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function messageOf(error) {
|
|
140
|
+
return error instanceof Error ? error.message : String(error)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function readJson(response) {
|
|
144
|
+
try {
|
|
145
|
+
return await response.json()
|
|
146
|
+
} catch {
|
|
147
|
+
return {}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function fetchQueue(sessionId) {
|
|
152
|
+
const response = await fetch(STATE_URL + '?sessionId=' + encodeURIComponent(sessionId), {
|
|
153
|
+
headers: { accept: 'application/json' },
|
|
154
|
+
})
|
|
155
|
+
const payload = await readJson(response)
|
|
156
|
+
if (!response.ok || payload.ok !== true) {
|
|
157
|
+
throw new Error(payload.message || ('HTTP ' + response.status))
|
|
158
|
+
}
|
|
159
|
+
return payload.queue || { items: [] }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function postAction(sessionId, body) {
|
|
163
|
+
const response = await fetch(ACTION_URL, {
|
|
164
|
+
method: 'POST',
|
|
165
|
+
headers: { 'content-type': 'application/json' },
|
|
166
|
+
body: JSON.stringify({ sessionId, ...body }),
|
|
167
|
+
})
|
|
168
|
+
const payload = await readJson(response)
|
|
169
|
+
if (!response.ok || payload.ok !== true) {
|
|
170
|
+
throw new Error(payload.message || ('HTTP ' + response.status))
|
|
171
|
+
}
|
|
172
|
+
return payload.queue || { items: [] }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/* -------------------------------- artwork -------------------------------- */
|
|
176
|
+
|
|
177
|
+
/** One glyph for the guide card and the tab chip. */
|
|
178
|
+
function QueueGlyph(props) {
|
|
179
|
+
const size = props && props.size ? props.size : 16
|
|
180
|
+
return h('svg', {
|
|
181
|
+
width: size,
|
|
182
|
+
height: size,
|
|
183
|
+
viewBox: '0 0 16 16',
|
|
184
|
+
fill: 'none',
|
|
185
|
+
'aria-hidden': 'true',
|
|
186
|
+
}, [
|
|
187
|
+
h('rect', { key: 'a', x: 1.6, y: 2.2, width: 9.6, height: 9.2, rx: 2.4, stroke: 'currentColor', strokeWidth: 1.3 }),
|
|
188
|
+
h('path', { key: 'b', d: 'M4 5.4h4.8M4 8.4h3.2', stroke: 'currentColor', strokeWidth: 1.3, strokeLinecap: 'round' }),
|
|
189
|
+
h('path', { key: 'c', d: 'M11.2 12.6l2.4-2.2-2.4-2.2', stroke: 'currentColor', strokeWidth: 1.3, strokeLinecap: 'round', strokeLinejoin: 'round' }),
|
|
190
|
+
])
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/* --------------------------------- styles -------------------------------- */
|
|
194
|
+
|
|
195
|
+
const CSS = [
|
|
196
|
+
'.dsh-pending-queue{box-sizing:border-box;height:100%;min-height:0;display:flex;flex-direction:column;',
|
|
197
|
+
' color:var(--dsw-alias-label-primary,#e8e8ea);',
|
|
198
|
+
' font:13px/1.5 -apple-system,BlinkMacSystemFont,"PingFang SC","Segoe UI",sans-serif}',
|
|
199
|
+
'.dsh-pending-queue-head{flex:none;display:flex;align-items:center;gap:8px;padding:10px 14px 8px}',
|
|
200
|
+
'.dsh-pending-queue-title{flex:none;font-size:14px;font-weight:600}',
|
|
201
|
+
'.dsh-pending-queue-counts{flex:1;min-width:0;font-size:11px;color:var(--dsw-alias-label-tertiary,#9a9aa2);',
|
|
202
|
+
' white-space:nowrap;overflow:hidden;text-overflow:ellipsis}',
|
|
203
|
+
'.dsh-pending-queue-ghost{all:unset;cursor:pointer;flex:none;display:inline-flex;align-items:center;gap:4px;',
|
|
204
|
+
' height:24px;padding:0 8px;border-radius:var(--dsw-radius-sm,8px);font-size:12px;',
|
|
205
|
+
' color:var(--dsw-alias-label-secondary,#b9b9c0)}',
|
|
206
|
+
'.dsh-pending-queue-ghost:hover{background:var(--dsw-alias-interactive-bg-hover,#ffffff14);color:var(--dsw-alias-label-primary,#e8e8ea)}',
|
|
207
|
+
'.dsh-pending-queue-ghost[disabled]{opacity:.45;cursor:default}',
|
|
208
|
+
'.dsh-pending-queue-compose{flex:none;margin:0 10px 10px;padding:8px 10px;border-radius:var(--dsw-radius-md,12px);',
|
|
209
|
+
' background:var(--dsw-alias-bg-layer-1,#1f1f22);border:0.5px solid var(--dsw-alias-border-l3,#ffffff1a)}',
|
|
210
|
+
'.dsh-pending-queue-compose:focus-within{border-color:var(--dsw-alias-brand-primary-new-colorprimary-new-color,#4d8dff)}',
|
|
211
|
+
'.dsh-pending-queue-input{box-sizing:border-box;width:100%;min-height:52px;max-height:160px;resize:vertical;',
|
|
212
|
+
' border:0;outline:0;background:transparent;color:inherit;font:inherit;padding:0}',
|
|
213
|
+
'.dsh-pending-queue-input::placeholder{color:var(--dsw-alias-label-tertiary,#9a9aa2)}',
|
|
214
|
+
'.dsh-pending-queue-composeFoot{display:flex;align-items:center;gap:8px;margin-top:6px}',
|
|
215
|
+
'.dsh-pending-queue-hint{flex:1;min-width:0;font-size:11px;line-height:1.45;color:var(--dsw-alias-label-tertiary,#9a9aa2)}',
|
|
216
|
+
'.dsh-pending-queue-primary{all:unset;cursor:pointer;flex:none;height:24px;padding:0 10px;border-radius:var(--dsw-radius-sm,8px);',
|
|
217
|
+
' font-size:12px;background:var(--dsw-alias-brand-primary-new-colorprimary-new-color,#4d8dff);color:#fff}',
|
|
218
|
+
'.dsh-pending-queue-primary[disabled]{opacity:.45;cursor:default}',
|
|
219
|
+
'.dsh-pending-queue-body{flex:1;min-height:0;overflow:auto;padding:0 10px 16px;overscroll-behavior:contain}',
|
|
220
|
+
'.dsh-pending-queue-hintBlock{padding:28px 12px;text-align:center;color:var(--dsw-alias-label-tertiary,#9a9aa2);',
|
|
221
|
+
' font-size:12.5px;line-height:1.7}',
|
|
222
|
+
'.dsh-pending-queue-hintTitle{display:block;font-size:13.5px;color:var(--dsw-alias-label-secondary,#b9b9c0);margin-bottom:6px}',
|
|
223
|
+
'.dsh-pending-queue-err{margin:8px 12px;padding:8px 10px;border-radius:var(--dsw-radius-sm,8px);',
|
|
224
|
+
' border:0.5px solid var(--dsw-alias-state-error-primary,#f56c6c);',
|
|
225
|
+
' color:var(--dsw-alias-state-error-primary,#f56c6c);font-size:12px}',
|
|
226
|
+
'.dsh-pending-queue-card{position:relative;margin:0 0 10px;padding:10px 12px 8px;border-radius:var(--dsw-radius-md,12px);',
|
|
227
|
+
' background:var(--dsw-alias-bg-layer-1,#1f1f22);border:0.5px solid var(--dsw-alias-border-l3,#ffffff1a)}',
|
|
228
|
+
'.dsh-pending-queue-card[data-sent="true"]{opacity:.6}',
|
|
229
|
+
'.dsh-pending-queue-text{white-space:pre-wrap;word-break:break-word;font-size:13px;line-height:1.55;',
|
|
230
|
+
' display:-webkit-box;-webkit-line-clamp:6;-webkit-box-orient:vertical;overflow:hidden}',
|
|
231
|
+
'.dsh-pending-queue-meta{display:flex;align-items:center;gap:6px;margin-top:6px;font-size:11px;',
|
|
232
|
+
' color:var(--dsw-alias-label-tertiary,#9a9aa2)}',
|
|
233
|
+
'.dsh-pending-queue-badge{flex:none;border:0.5px solid var(--dsw-alias-border-l3,#ffffff1f);border-radius:999px;',
|
|
234
|
+
' padding:0 6px;line-height:15px}',
|
|
235
|
+
'.dsh-pending-queue-acts{display:flex;align-items:center;gap:2px;margin-top:4px;flex-wrap:wrap}',
|
|
236
|
+
'.dsh-pending-queue-act{all:unset;cursor:pointer;height:24px;padding:0 7px;border-radius:var(--dsw-radius-sm,8px);',
|
|
237
|
+
' font-size:12px;color:var(--dsw-alias-label-secondary,#b9b9c0)}',
|
|
238
|
+
'.dsh-pending-queue-act:hover{background:var(--dsw-alias-interactive-bg-hover,#ffffff14);color:var(--dsw-alias-label-primary,#e8e8ea)}',
|
|
239
|
+
'.dsh-pending-queue-act[disabled]{opacity:.4;cursor:default}',
|
|
240
|
+
'.dsh-pending-queue-act[data-tone="send"]{color:var(--dsw-alias-brand-primary-new-colorprimary-new-color,#4d8dff)}',
|
|
241
|
+
'.dsh-pending-queue-edit{box-sizing:border-box;width:100%;min-height:64px;resize:vertical;border-radius:var(--dsw-radius-sm,8px);',
|
|
242
|
+
' border:0.5px solid var(--dsw-alias-border-l3,#ffffff1a);background:transparent;color:inherit;font:inherit;padding:6px 8px}',
|
|
243
|
+
'.dsh-pending-queue-notice{flex:none;margin:0 12px 6px;font-size:11.5px;color:var(--dsw-alias-label-tertiary,#9a9aa2)}',
|
|
244
|
+
].join('\n')
|
|
245
|
+
|
|
246
|
+
/* -------------------------------- component ------------------------------- */
|
|
247
|
+
|
|
248
|
+
function QueueBody(props) {
|
|
249
|
+
const t = translator(props)
|
|
250
|
+
const sessionId = props.sessionId
|
|
251
|
+
const inputActions = props.inputActions
|
|
252
|
+
|
|
253
|
+
const [queue, setQueue] = useState(null)
|
|
254
|
+
const [loading, setLoading] = useState(true)
|
|
255
|
+
const [error, setError] = useState('')
|
|
256
|
+
const [draft, setDraft] = useState('')
|
|
257
|
+
const [editing, setEditing] = useState(null)
|
|
258
|
+
const [busy, setBusy] = useState('')
|
|
259
|
+
const [notice, setNotice] = useState('')
|
|
260
|
+
const live = useRef(true)
|
|
261
|
+
|
|
262
|
+
useEffect(() => {
|
|
263
|
+
live.current = true
|
|
264
|
+
return () => {
|
|
265
|
+
live.current = false
|
|
266
|
+
}
|
|
267
|
+
}, [])
|
|
268
|
+
|
|
269
|
+
const load = useCallback(async (quiet) => {
|
|
270
|
+
if (!sessionId) {
|
|
271
|
+
setLoading(false)
|
|
272
|
+
return
|
|
273
|
+
}
|
|
274
|
+
if (!quiet) setLoading(true)
|
|
275
|
+
try {
|
|
276
|
+
const next = await fetchQueue(sessionId)
|
|
277
|
+
if (!live.current) return
|
|
278
|
+
setQueue(next)
|
|
279
|
+
setError('')
|
|
280
|
+
} catch (failure) {
|
|
281
|
+
if (!live.current) return
|
|
282
|
+
setError(messageOf(failure))
|
|
283
|
+
} finally {
|
|
284
|
+
if (live.current) setLoading(false)
|
|
285
|
+
}
|
|
286
|
+
}, [sessionId])
|
|
287
|
+
|
|
288
|
+
useEffect(() => {
|
|
289
|
+
load(false)
|
|
290
|
+
const timer = setInterval(() => load(true), 10000)
|
|
291
|
+
const refresh = () => {
|
|
292
|
+
if (typeof document === 'undefined' || document.visibilityState === 'visible') load(true)
|
|
293
|
+
}
|
|
294
|
+
if (typeof document !== 'undefined') document.addEventListener('visibilitychange', refresh)
|
|
295
|
+
return () => {
|
|
296
|
+
clearInterval(timer)
|
|
297
|
+
if (typeof document !== 'undefined') document.removeEventListener('visibilitychange', refresh)
|
|
298
|
+
}
|
|
299
|
+
}, [load])
|
|
300
|
+
|
|
301
|
+
const act = useCallback(async (body) => {
|
|
302
|
+
if (!sessionId) return undefined
|
|
303
|
+
try {
|
|
304
|
+
const next = await postAction(sessionId, body)
|
|
305
|
+
if (live.current) {
|
|
306
|
+
setQueue(next)
|
|
307
|
+
setError('')
|
|
308
|
+
}
|
|
309
|
+
return next
|
|
310
|
+
} catch (failure) {
|
|
311
|
+
if (live.current) setError(messageOf(failure))
|
|
312
|
+
return undefined
|
|
313
|
+
}
|
|
314
|
+
}, [sessionId])
|
|
315
|
+
|
|
316
|
+
const flash = useCallback((text) => {
|
|
317
|
+
setNotice(text)
|
|
318
|
+
setTimeout(() => {
|
|
319
|
+
if (live.current) setNotice((current) => (current === text ? '' : current))
|
|
320
|
+
}, 2200)
|
|
321
|
+
}, [])
|
|
322
|
+
|
|
323
|
+
const saveDraft = useCallback(async () => {
|
|
324
|
+
const text = draft.trim()
|
|
325
|
+
if (text === '') {
|
|
326
|
+
setError(t('error.empty'))
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
setBusy('add')
|
|
330
|
+
const next = await act({ action: 'add', text })
|
|
331
|
+
if (live.current) {
|
|
332
|
+
setBusy('')
|
|
333
|
+
if (next) setDraft('')
|
|
334
|
+
}
|
|
335
|
+
}, [draft, act, t])
|
|
336
|
+
|
|
337
|
+
/** Push one row into the composer, standing in for the user's own typing. */
|
|
338
|
+
const push = useCallback(async (item, mode) => {
|
|
339
|
+
if (!inputActions || typeof inputActions.setDraft !== 'function') {
|
|
340
|
+
setError(t('error.noComposer'))
|
|
341
|
+
return
|
|
342
|
+
}
|
|
343
|
+
setBusy(item.id)
|
|
344
|
+
setError('')
|
|
345
|
+
try {
|
|
346
|
+
inputActions.setDraft(item.text)
|
|
347
|
+
if (mode === 'send') {
|
|
348
|
+
if (typeof inputActions.submit !== 'function') {
|
|
349
|
+
setError(t('error.noComposer'))
|
|
350
|
+
return
|
|
351
|
+
}
|
|
352
|
+
inputActions.submit()
|
|
353
|
+
flash(t('notice.sent'))
|
|
354
|
+
await act({ action: 'markSent', id: item.id })
|
|
355
|
+
} else {
|
|
356
|
+
flash(t('notice.filled'))
|
|
357
|
+
}
|
|
358
|
+
} finally {
|
|
359
|
+
if (live.current) setBusy('')
|
|
360
|
+
}
|
|
361
|
+
}, [inputActions, act, flash, t])
|
|
362
|
+
|
|
363
|
+
const startEdit = useCallback((item) => {
|
|
364
|
+
setEditing({ id: item.id, text: item.text })
|
|
365
|
+
}, [])
|
|
366
|
+
|
|
367
|
+
const saveEdit = useCallback(async () => {
|
|
368
|
+
if (editing === null) return
|
|
369
|
+
const text = editing.text.trim()
|
|
370
|
+
if (text === '') {
|
|
371
|
+
setError(t('error.empty'))
|
|
372
|
+
return
|
|
373
|
+
}
|
|
374
|
+
setBusy(editing.id)
|
|
375
|
+
const next = await act({ action: 'update', id: editing.id, text })
|
|
376
|
+
if (live.current) {
|
|
377
|
+
setBusy('')
|
|
378
|
+
if (next) setEditing(null)
|
|
379
|
+
}
|
|
380
|
+
}, [editing, act, t])
|
|
381
|
+
|
|
382
|
+
const items = queue && Array.isArray(queue.items) ? queue.items : []
|
|
383
|
+
const pending = items.filter((item) => item.sentAt === null || item.sentAt === undefined).length
|
|
384
|
+
const sent = items.length - pending
|
|
385
|
+
|
|
386
|
+
const onComposeKey = useCallback((event) => {
|
|
387
|
+
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
|
|
388
|
+
event.preventDefault()
|
|
389
|
+
void saveDraft()
|
|
390
|
+
}
|
|
391
|
+
}, [saveDraft])
|
|
392
|
+
|
|
393
|
+
const countLabel = [count(t, 'queue.pending', pending), sent > 0 ? count(t, 'queue.sent', sent) : ''].filter(Boolean).join(' · ')
|
|
394
|
+
|
|
395
|
+
const children = [
|
|
396
|
+
h('style', { key: 'css' }, CSS),
|
|
397
|
+
h('div', { key: 'head', className: ROOT + '-head' }, [
|
|
398
|
+
h('span', { key: 'title', className: ROOT + '-title' }, t('queue.title')),
|
|
399
|
+
h('span', { key: 'counts', className: ROOT + '-counts' }, countLabel),
|
|
400
|
+
sent > 0
|
|
401
|
+
? h('button', {
|
|
402
|
+
key: 'clear',
|
|
403
|
+
type: 'button',
|
|
404
|
+
className: ROOT + '-ghost',
|
|
405
|
+
onClick: () => { void act({ action: 'clearSent' }) },
|
|
406
|
+
}, t('queue.clearSent'))
|
|
407
|
+
: null,
|
|
408
|
+
h('button', {
|
|
409
|
+
key: 'refresh',
|
|
410
|
+
type: 'button',
|
|
411
|
+
className: ROOT + '-ghost',
|
|
412
|
+
onClick: () => { void load(true) },
|
|
413
|
+
}, t('queue.refresh')),
|
|
414
|
+
]),
|
|
415
|
+
h('div', { key: 'compose', className: ROOT + '-compose' }, [
|
|
416
|
+
h('textarea', {
|
|
417
|
+
key: 'input',
|
|
418
|
+
className: ROOT + '-input',
|
|
419
|
+
value: draft,
|
|
420
|
+
placeholder: t('input.placeholder'),
|
|
421
|
+
rows: 2,
|
|
422
|
+
onChange: (event) => setDraft(event.target.value),
|
|
423
|
+
onKeyDown: onComposeKey,
|
|
424
|
+
}),
|
|
425
|
+
h('div', { key: 'foot', className: ROOT + '-composeFoot' }, [
|
|
426
|
+
h('span', { key: 'hint', className: ROOT + '-hint' }, t('input.hint')),
|
|
427
|
+
h('button', {
|
|
428
|
+
key: 'save',
|
|
429
|
+
type: 'button',
|
|
430
|
+
className: ROOT + '-primary',
|
|
431
|
+
disabled: busy === 'add' || draft.trim() === '',
|
|
432
|
+
onClick: () => { void saveDraft() },
|
|
433
|
+
}, t('action.save')),
|
|
434
|
+
]),
|
|
435
|
+
]),
|
|
436
|
+
notice !== '' ? h('div', { key: 'notice', className: ROOT + '-notice', role: 'status' }, notice) : null,
|
|
437
|
+
error !== '' ? h('div', { key: 'err', className: ROOT + '-err', role: 'alert' }, error) : null,
|
|
438
|
+
h('div', { key: 'body', className: ROOT + '-body' },
|
|
439
|
+
loading && items.length === 0
|
|
440
|
+
? [h('div', { key: 'loading', className: ROOT + '-hintBlock' }, '…')]
|
|
441
|
+
: items.length === 0
|
|
442
|
+
? [h('div', { key: 'empty', className: ROOT + '-hintBlock' }, [
|
|
443
|
+
h('span', { key: 'title', className: ROOT + '-hintTitle' }, t('empty.title')),
|
|
444
|
+
h('span', { key: 'body' }, t('empty.body')),
|
|
445
|
+
])]
|
|
446
|
+
: items.map((item, index) => renderRow(item, index))),
|
|
447
|
+
]
|
|
448
|
+
|
|
449
|
+
function renderRow(item, index) {
|
|
450
|
+
const isEditing = editing !== null && editing.id === item.id
|
|
451
|
+
const isSent = item.sentAt !== null && item.sentAt !== undefined
|
|
452
|
+
const isBusy = busy === item.id
|
|
453
|
+
const actions = []
|
|
454
|
+
if (!isEditing && !isSent) {
|
|
455
|
+
actions.push(h('button', {
|
|
456
|
+
key: 'send',
|
|
457
|
+
type: 'button',
|
|
458
|
+
className: ROOT + '-act',
|
|
459
|
+
'data-tone': 'send',
|
|
460
|
+
disabled: isBusy,
|
|
461
|
+
onClick: () => { void push(item, 'send') },
|
|
462
|
+
}, t('action.send')))
|
|
463
|
+
actions.push(h('button', {
|
|
464
|
+
key: 'fill',
|
|
465
|
+
type: 'button',
|
|
466
|
+
className: ROOT + '-act',
|
|
467
|
+
disabled: isBusy,
|
|
468
|
+
onClick: () => { void push(item, 'fill') },
|
|
469
|
+
}, t('action.fill')))
|
|
470
|
+
actions.push(h('button', {
|
|
471
|
+
key: 'edit',
|
|
472
|
+
type: 'button',
|
|
473
|
+
className: ROOT + '-act',
|
|
474
|
+
onClick: () => startEdit(item),
|
|
475
|
+
}, t('action.edit')))
|
|
476
|
+
}
|
|
477
|
+
if (isSent) {
|
|
478
|
+
actions.push(h('button', {
|
|
479
|
+
key: 'restore',
|
|
480
|
+
type: 'button',
|
|
481
|
+
className: ROOT + '-act',
|
|
482
|
+
onClick: () => { void act({ action: 'restore', id: item.id }) },
|
|
483
|
+
}, t('action.restore')))
|
|
484
|
+
}
|
|
485
|
+
if (!isEditing) {
|
|
486
|
+
actions.push(h('button', {
|
|
487
|
+
key: 'up',
|
|
488
|
+
type: 'button',
|
|
489
|
+
className: ROOT + '-act',
|
|
490
|
+
disabled: index === 0,
|
|
491
|
+
'aria-label': t('action.up'),
|
|
492
|
+
onClick: () => { void act({ action: 'move', id: item.id, direction: 'up' }) },
|
|
493
|
+
}, '↑'))
|
|
494
|
+
actions.push(h('button', {
|
|
495
|
+
key: 'down',
|
|
496
|
+
type: 'button',
|
|
497
|
+
className: ROOT + '-act',
|
|
498
|
+
disabled: index === items.length - 1,
|
|
499
|
+
'aria-label': t('action.down'),
|
|
500
|
+
onClick: () => { void act({ action: 'move', id: item.id, direction: 'down' }) },
|
|
501
|
+
}, '↓'))
|
|
502
|
+
actions.push(h('button', {
|
|
503
|
+
key: 'remove',
|
|
504
|
+
type: 'button',
|
|
505
|
+
className: ROOT + '-act',
|
|
506
|
+
onClick: () => { void act({ action: 'remove', id: item.id }) },
|
|
507
|
+
}, t('action.remove')))
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return h('div', {
|
|
511
|
+
key: item.id,
|
|
512
|
+
className: ROOT + '-card',
|
|
513
|
+
'data-sent': isSent ? 'true' : 'false',
|
|
514
|
+
}, [
|
|
515
|
+
isEditing
|
|
516
|
+
? h('div', { key: 'editing' }, [
|
|
517
|
+
h('textarea', {
|
|
518
|
+
key: 'ta',
|
|
519
|
+
className: ROOT + '-edit',
|
|
520
|
+
value: editing.text,
|
|
521
|
+
rows: 3,
|
|
522
|
+
onChange: (event) => setEditing({ id: item.id, text: event.target.value }),
|
|
523
|
+
onKeyDown: (event) => {
|
|
524
|
+
if (event.key === 'Enter' && (event.metaKey || event.ctrlKey)) {
|
|
525
|
+
event.preventDefault()
|
|
526
|
+
void saveEdit()
|
|
527
|
+
}
|
|
528
|
+
if (event.key === 'Escape') {
|
|
529
|
+
event.preventDefault()
|
|
530
|
+
setEditing(null)
|
|
531
|
+
}
|
|
532
|
+
},
|
|
533
|
+
}),
|
|
534
|
+
h('div', { key: 'acts', className: ROOT + '-acts' }, [
|
|
535
|
+
h('button', {
|
|
536
|
+
key: 'save',
|
|
537
|
+
type: 'button',
|
|
538
|
+
className: ROOT + '-act',
|
|
539
|
+
'data-tone': 'send',
|
|
540
|
+
disabled: isBusy,
|
|
541
|
+
onClick: () => { void saveEdit() },
|
|
542
|
+
}, t('action.saveEdit')),
|
|
543
|
+
h('button', {
|
|
544
|
+
key: 'cancel',
|
|
545
|
+
type: 'button',
|
|
546
|
+
className: ROOT + '-act',
|
|
547
|
+
onClick: () => setEditing(null),
|
|
548
|
+
}, t('action.cancel')),
|
|
549
|
+
]),
|
|
550
|
+
])
|
|
551
|
+
: h('div', { key: 'text', className: ROOT + '-text' }, item.text),
|
|
552
|
+
h('div', { key: 'meta', className: ROOT + '-meta' }, [
|
|
553
|
+
h('span', { key: 'time' }, relativeTime(item.createdAt, t)),
|
|
554
|
+
isSent ? h('span', { key: 'sent', className: ROOT + '-badge' }, t('status.sent')) : null,
|
|
555
|
+
]),
|
|
556
|
+
h('div', { key: 'acts', className: ROOT + '-acts' }, actions),
|
|
557
|
+
])
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return h('div', { className: ROOT }, children)
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
/** The tab chip: one glyph and the localized type name. */
|
|
564
|
+
function QueueTitle(props) {
|
|
565
|
+
const t = translator(props)
|
|
566
|
+
return h(React.Fragment, null, [
|
|
567
|
+
h('span', { key: 'icon', style: { display: 'inline-flex', marginInlineEnd: 6 } }, h(QueueGlyph, { size: 14 })),
|
|
568
|
+
h('span', { key: 'label' }, t('tab.title')),
|
|
569
|
+
])
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/* ---------------------------------- apply --------------------------------- */
|
|
573
|
+
|
|
574
|
+
return {
|
|
575
|
+
inject: ['slots', 'sidebarRightTabs', 'sidebarRight', 'locale'],
|
|
576
|
+
apply(ctx) {
|
|
577
|
+
const tr = ctx.locale.bind(NS)
|
|
578
|
+
|
|
579
|
+
ctx.effect(() => ctx.locale.register(NS, { zh: ZH, en: EN }), 'pending-queue: dictionaries')
|
|
580
|
+
|
|
581
|
+
const definition = {
|
|
582
|
+
id: PKG,
|
|
583
|
+
kind: KIND,
|
|
584
|
+
priority: 'extension',
|
|
585
|
+
title: () => tr('tab.title'),
|
|
586
|
+
guide: [{
|
|
587
|
+
id: 'queue',
|
|
588
|
+
order: 45,
|
|
589
|
+
title: () => tr('tab.title'),
|
|
590
|
+
description: () => tr('tab.description'),
|
|
591
|
+
icon: QueueGlyph,
|
|
592
|
+
}],
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
ctx.effect(() => {
|
|
596
|
+
const disposers = []
|
|
597
|
+
|
|
598
|
+
disposers.push(ctx.sidebarRightTabs.register(definition))
|
|
599
|
+
disposers.push(ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({
|
|
600
|
+
name: 'sidebar.right.pane.tab',
|
|
601
|
+
key: PKG,
|
|
602
|
+
locale: NS,
|
|
603
|
+
inject: (sessionId) => ({ sessionId }),
|
|
604
|
+
}, QueueBody)))
|
|
605
|
+
disposers.push(ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({
|
|
606
|
+
name: 'sidebar.right.pane.tab.title',
|
|
607
|
+
key: PKG,
|
|
608
|
+
locale: NS,
|
|
609
|
+
}, QueueTitle)))
|
|
610
|
+
|
|
611
|
+
return () => {
|
|
612
|
+
for (let index = disposers.length - 1; index >= 0; index -= 1) {
|
|
613
|
+
try {
|
|
614
|
+
disposers[index]()
|
|
615
|
+
} catch {
|
|
616
|
+
/* one failed disposer must not keep the others registered */
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}, 'pending-queue: tab')
|
|
621
|
+
},
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
})
|
package/cordis.patch.yml
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host half of the pending-send queue.
|
|
3
|
+
*
|
|
4
|
+
* The panel is a scratchpad for messages the user is not ready to send yet:
|
|
5
|
+
* one JSON document per session under <DSH_HOME>/storages/pending-queue, read
|
|
6
|
+
* and written through this plugin's own browser routes. Nothing here appends a
|
|
7
|
+
* session event, and nothing here sends anything: pushing a row into the chat
|
|
8
|
+
* happens in the client half through the composer's own input actions, so a
|
|
9
|
+
* queued row behaves exactly like text the user typed themselves.
|
|
10
|
+
*
|
|
11
|
+
* Storage: <DSH_HOME>/storages/pending-queue/<sessionId>.json.
|
|
12
|
+
* DSH_HOME defaults to ~/.dsh.
|
|
13
|
+
*/
|
|
14
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
15
|
+
import { homedir } from 'node:os'
|
|
16
|
+
import { join } from 'node:path'
|
|
17
|
+
|
|
18
|
+
/** The routes this plugin owns on the browser HTTP carrier. */
|
|
19
|
+
const ROUTE_BASE = '/plugins/pending-queue'
|
|
20
|
+
|
|
21
|
+
/** One file per session; the id is validated before it reaches a path. */
|
|
22
|
+
const STORE_DIR = join(
|
|
23
|
+
process.env.DSH_HOME || join(homedir(), '.dsh'),
|
|
24
|
+
'storages',
|
|
25
|
+
'pending-queue',
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
/** Session ids are opaque but filesystem-safe in every shipped deployment. */
|
|
29
|
+
const SESSION_ID = /^[A-Za-z0-9._-]{1,128}$/
|
|
30
|
+
|
|
31
|
+
/** Bounded so one runaway session cannot grow the document without limit. */
|
|
32
|
+
const MAX_ITEMS = 200
|
|
33
|
+
const MAX_TEXT = 4000
|
|
34
|
+
|
|
35
|
+
function nowIso() {
|
|
36
|
+
return new Date().toISOString()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** One row's text: newlines survive, edges do not. */
|
|
40
|
+
function textOf(value) {
|
|
41
|
+
if (typeof value !== 'string') return ''
|
|
42
|
+
return value.replace(/\r\n/g, '\n').trim().slice(0, MAX_TEXT)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function createDoc(sessionId) {
|
|
46
|
+
return {
|
|
47
|
+
version: 1,
|
|
48
|
+
sessionId,
|
|
49
|
+
seq: 1,
|
|
50
|
+
items: [],
|
|
51
|
+
updatedAt: nowIso(),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function fileFor(sessionId) {
|
|
56
|
+
if (!SESSION_ID.test(sessionId)) throw new Error('invalid session id')
|
|
57
|
+
return join(STORE_DIR, sessionId + '.json')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* ------------------------------- persistence ------------------------------ */
|
|
61
|
+
|
|
62
|
+
let queue = Promise.resolve()
|
|
63
|
+
|
|
64
|
+
/** Serialize read-modify-write cycles so two windows cannot lose one edit. */
|
|
65
|
+
function withLock(task) {
|
|
66
|
+
const run = queue.then(() => task(), () => task())
|
|
67
|
+
queue = run.then(() => undefined, () => undefined)
|
|
68
|
+
return run
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function readDoc(sessionId) {
|
|
72
|
+
try {
|
|
73
|
+
const raw = await readFile(fileFor(sessionId), 'utf8')
|
|
74
|
+
const parsed = JSON.parse(raw)
|
|
75
|
+
if (parsed !== null && typeof parsed === 'object' && parsed.version === 1 && Array.isArray(parsed.items)) {
|
|
76
|
+
return normalizeDoc(parsed, sessionId)
|
|
77
|
+
}
|
|
78
|
+
return createDoc(sessionId)
|
|
79
|
+
} catch (error) {
|
|
80
|
+
// A missing or unreadable document is an empty queue, not a failure: the
|
|
81
|
+
// tab must still open in a session that never had one.
|
|
82
|
+
if (error && (error.code === 'ENOENT' || error instanceof SyntaxError)) return createDoc(sessionId)
|
|
83
|
+
throw error
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Fill fields an older document may lack, so one shape reaches every reader. */
|
|
88
|
+
function normalizeDoc(doc, sessionId) {
|
|
89
|
+
doc.sessionId = sessionId
|
|
90
|
+
doc.items = doc.items.filter((item) => item && typeof item.id === 'string')
|
|
91
|
+
if (!Number.isInteger(doc.seq) || doc.seq < 1) doc.seq = doc.items.length + 1
|
|
92
|
+
for (const item of doc.items) {
|
|
93
|
+
if (typeof item.text !== 'string') item.text = ''
|
|
94
|
+
if (typeof item.createdAt !== 'string') item.createdAt = nowIso()
|
|
95
|
+
if (typeof item.updatedAt !== 'string') item.updatedAt = item.createdAt
|
|
96
|
+
if (typeof item.sentAt !== 'string') delete item.sentAt
|
|
97
|
+
}
|
|
98
|
+
return doc
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function writeDoc(doc) {
|
|
102
|
+
await mkdir(STORE_DIR, { recursive: true })
|
|
103
|
+
const target = fileFor(doc.sessionId)
|
|
104
|
+
const temporary = target + '.' + process.pid + '.tmp'
|
|
105
|
+
await writeFile(temporary, JSON.stringify(doc, null, 2) + '\n', 'utf8')
|
|
106
|
+
await rename(temporary, target)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Read, apply one change, persist. */
|
|
110
|
+
function mutateDoc(sessionId, change) {
|
|
111
|
+
return withLock(async () => {
|
|
112
|
+
const doc = await readDoc(sessionId)
|
|
113
|
+
change(doc)
|
|
114
|
+
doc.updatedAt = nowIso()
|
|
115
|
+
await writeDoc(doc)
|
|
116
|
+
return doc
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* -------------------------------- the model ------------------------------- */
|
|
121
|
+
|
|
122
|
+
function findItem(doc, id) {
|
|
123
|
+
return doc.items.find((item) => item.id === id)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function requireItem(doc, id) {
|
|
127
|
+
if (typeof id !== 'string' || id.trim() === '') throw new Error('itemId is required')
|
|
128
|
+
const item = findItem(doc, id.trim())
|
|
129
|
+
if (item === undefined) throw new Error('unknown item: ' + id.trim())
|
|
130
|
+
return item
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Apply one panel write; every action is validated before it mutates. */
|
|
134
|
+
function applyAction(doc, body) {
|
|
135
|
+
const action = String((body && body.action) || '')
|
|
136
|
+
if (action === 'add') {
|
|
137
|
+
const text = textOf(body.text)
|
|
138
|
+
if (text === '') throw new Error('text is required')
|
|
139
|
+
if (doc.items.length >= MAX_ITEMS) {
|
|
140
|
+
throw new Error('this queue already holds ' + MAX_ITEMS + ' rows; send or delete some first')
|
|
141
|
+
}
|
|
142
|
+
const at = nowIso()
|
|
143
|
+
const id = 'q' + doc.seq
|
|
144
|
+
doc.seq += 1
|
|
145
|
+
doc.items.push({ id, text, createdAt: at, updatedAt: at })
|
|
146
|
+
return { id }
|
|
147
|
+
}
|
|
148
|
+
if (action === 'update') {
|
|
149
|
+
const item = requireItem(doc, body.id)
|
|
150
|
+
const text = textOf(body.text)
|
|
151
|
+
if (text === '') throw new Error('text is required')
|
|
152
|
+
item.text = text
|
|
153
|
+
item.updatedAt = nowIso()
|
|
154
|
+
return { id: item.id }
|
|
155
|
+
}
|
|
156
|
+
if (action === 'remove') {
|
|
157
|
+
const item = requireItem(doc, body.id)
|
|
158
|
+
doc.items = doc.items.filter((entry) => entry.id !== item.id)
|
|
159
|
+
return { id: item.id }
|
|
160
|
+
}
|
|
161
|
+
if (action === 'move') {
|
|
162
|
+
const item = requireItem(doc, body.id)
|
|
163
|
+
const direction = body.direction === 'up' ? -1 : body.direction === 'down' ? 1 : 0
|
|
164
|
+
if (direction === 0) throw new Error('direction must be up or down')
|
|
165
|
+
const index = doc.items.indexOf(item)
|
|
166
|
+
const target = index + direction
|
|
167
|
+
if (target < 0 || target >= doc.items.length) return { id: item.id }
|
|
168
|
+
doc.items.splice(index, 1)
|
|
169
|
+
doc.items.splice(target, 0, item)
|
|
170
|
+
return { id: item.id }
|
|
171
|
+
}
|
|
172
|
+
if (action === 'markSent' || action === 'restore') {
|
|
173
|
+
const item = requireItem(doc, body.id)
|
|
174
|
+
if (action === 'markSent') item.sentAt = nowIso()
|
|
175
|
+
else delete item.sentAt
|
|
176
|
+
item.updatedAt = nowIso()
|
|
177
|
+
return { id: item.id }
|
|
178
|
+
}
|
|
179
|
+
if (action === 'clearSent') {
|
|
180
|
+
doc.items = doc.items.filter((item) => typeof item.sentAt !== 'string')
|
|
181
|
+
return {}
|
|
182
|
+
}
|
|
183
|
+
throw new Error('unknown action: ' + (action || '(none)'))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The compact shape both the browser and the tool result read. */
|
|
187
|
+
function viewDoc(doc) {
|
|
188
|
+
return {
|
|
189
|
+
updatedAt: doc.updatedAt,
|
|
190
|
+
items: doc.items.map((item) => ({
|
|
191
|
+
id: item.id,
|
|
192
|
+
text: item.text,
|
|
193
|
+
createdAt: item.createdAt,
|
|
194
|
+
updatedAt: item.updatedAt,
|
|
195
|
+
sentAt: typeof item.sentAt === 'string' ? item.sentAt : null,
|
|
196
|
+
})),
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/* ------------------------------ browser API ------------------------------- */
|
|
201
|
+
|
|
202
|
+
function sendJson(res, status, payload) {
|
|
203
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
204
|
+
res.writeHead(status)
|
|
205
|
+
res.end(JSON.stringify(payload))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function readJsonBody(req, limit = 64 * 1024) {
|
|
209
|
+
return new Promise((resolve, reject) => {
|
|
210
|
+
const chunks = []
|
|
211
|
+
let size = 0
|
|
212
|
+
req.on('data', (chunk) => {
|
|
213
|
+
size += chunk.length
|
|
214
|
+
if (size > limit) {
|
|
215
|
+
reject(new Error('request body too large'))
|
|
216
|
+
req.destroy()
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
chunks.push(chunk)
|
|
220
|
+
})
|
|
221
|
+
req.on('end', () => {
|
|
222
|
+
try {
|
|
223
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
|
|
224
|
+
} catch {
|
|
225
|
+
reject(new Error('invalid JSON body'))
|
|
226
|
+
}
|
|
227
|
+
})
|
|
228
|
+
req.on('error', reject)
|
|
229
|
+
})
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Reuse the host's own browser authentication for this plugin's routes. */
|
|
233
|
+
function connectionRejection(ctx, req) {
|
|
234
|
+
const connection = typeof ctx.get === 'function' ? ctx.get('connection') : undefined
|
|
235
|
+
if (!connection || typeof connection.requestRejection !== 'function') return undefined
|
|
236
|
+
try {
|
|
237
|
+
return connection.requestRejection({ headers: req.headers })
|
|
238
|
+
} catch {
|
|
239
|
+
return undefined
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function sessionIdOf(value) {
|
|
244
|
+
const sessionId = typeof value === 'string' ? value.trim() : ''
|
|
245
|
+
if (!SESSION_ID.test(sessionId)) throw new Error('missing or invalid sessionId')
|
|
246
|
+
return sessionId
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* --------------------------------- plugin --------------------------------- */
|
|
250
|
+
|
|
251
|
+
export const inject = ['webServer']
|
|
252
|
+
|
|
253
|
+
export function apply(ctx) {
|
|
254
|
+
const stateHandler = async (req, res) => {
|
|
255
|
+
if (req.method !== 'GET') {
|
|
256
|
+
sendJson(res, 405, { ok: false, message: 'method not allowed' })
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
const rejection = connectionRejection(ctx, req)
|
|
260
|
+
if (rejection !== undefined) {
|
|
261
|
+
sendJson(res, rejection, { ok: false, message: 'not authorized' })
|
|
262
|
+
return
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
const url = new URL(req.url || '/', 'http://127.0.0.1')
|
|
266
|
+
const sessionId = sessionIdOf(url.searchParams.get('sessionId'))
|
|
267
|
+
const doc = await readDoc(sessionId)
|
|
268
|
+
sendJson(res, 200, { ok: true, queue: viewDoc(doc) })
|
|
269
|
+
} catch (error) {
|
|
270
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
271
|
+
ctx.logger?.warn?.('pending-queue: read failed: ' + message)
|
|
272
|
+
sendJson(res, 400, { ok: false, message })
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const actionHandler = async (req, res) => {
|
|
277
|
+
if (req.method !== 'POST') {
|
|
278
|
+
sendJson(res, 405, { ok: false, message: 'method not allowed' })
|
|
279
|
+
return
|
|
280
|
+
}
|
|
281
|
+
const rejection = connectionRejection(ctx, req)
|
|
282
|
+
if (rejection !== undefined) {
|
|
283
|
+
sendJson(res, rejection, { ok: false, message: 'not authorized' })
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
const body = await readJsonBody(req)
|
|
288
|
+
const sessionId = sessionIdOf(body && body.sessionId)
|
|
289
|
+
const doc = await mutateDoc(sessionId, (current) => applyAction(current, body))
|
|
290
|
+
sendJson(res, 200, { ok: true, queue: viewDoc(doc) })
|
|
291
|
+
} catch (error) {
|
|
292
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
293
|
+
ctx.logger?.warn?.('pending-queue: action failed: ' + message)
|
|
294
|
+
sendJson(res, 400, { ok: false, message })
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
ctx.effect(
|
|
299
|
+
() => ctx.webServer.register({ kind: 'exact', path: ROUTE_BASE + '/state', handler: stateHandler }),
|
|
300
|
+
'pending-queue: state route',
|
|
301
|
+
)
|
|
302
|
+
ctx.effect(
|
|
303
|
+
() => ctx.webServer.register({ kind: 'exact', path: ROUTE_BASE + '/action', handler: actionHandler }),
|
|
304
|
+
'pending-queue: action route',
|
|
305
|
+
)
|
|
306
|
+
}
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lyra_wang/dsh-pending-queue",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Right-sidebar draft queue for DeepSeek Harness: jot ideas down, then push one into the session when the previous task is finished.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"icon": "./icon.svg",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js",
|
|
11
|
+
"./client": "./client.js",
|
|
12
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
13
|
+
"./locale/*.json": "./locale/*.json",
|
|
14
|
+
"./icon": "./icon.svg",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"index.js",
|
|
19
|
+
"client.js",
|
|
20
|
+
"cordis.patch.yml",
|
|
21
|
+
"locale/*.json",
|
|
22
|
+
"icon.svg",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"dsh",
|
|
30
|
+
"dsh-plugin",
|
|
31
|
+
"client-plugin",
|
|
32
|
+
"queue",
|
|
33
|
+
"draft"
|
|
34
|
+
],
|
|
35
|
+
"dsh": {
|
|
36
|
+
"manifestVersion": 1,
|
|
37
|
+
"bundle": {
|
|
38
|
+
"patch": "./cordis.patch.yml"
|
|
39
|
+
},
|
|
40
|
+
"client": {
|
|
41
|
+
"platform": "web",
|
|
42
|
+
"immediately": true,
|
|
43
|
+
"inject": [
|
|
44
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right",
|
|
45
|
+
"@deepseek-ai/dsh-client-locale"
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/LyraWang6688/ai-capability-store.git",
|
|
52
|
+
"directory": "external-capabilities/plugin/dsh/pending-queue"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|