dsh-context-switch 0.0.0-stage → 1.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 +225 -2
- package/contexts/example/NOW.md +25 -0
- package/contexts/example/RULES.md +8 -0
- package/contexts/example/TODO.md +5 -0
- package/contexts/example/WHEN.md +4 -0
- package/contexts/example/index.md +7 -0
- package/cordis.patch.yml +100 -0
- package/lib/index.js +793 -0
- package/lib/now-card.mjs +407 -0
- package/lib/schedule.mjs +150 -0
- package/lib/switch-count.mjs +50 -0
- package/lib/ui.js +642 -0
- package/package.json +48 -4
package/lib/ui.js
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-context-switch 的浏览器端 UI:右下角一个小胶囊按钮,点开是上下文列表。
|
|
3
|
+
*
|
|
4
|
+
* 为什么走这个路子:DSH 的 Web 端只暴露 `webServer.register`(注册端点)和
|
|
5
|
+
* `webServer.tapIndex`(往 index.html 里塞标签)两个口子,没有正式的客户端插件 API。
|
|
6
|
+
* 所以这里就是一个自包含的原生 JS,不依赖框架,注入即用。
|
|
7
|
+
*
|
|
8
|
+
* 获取数据 / 切换都走 `/dsh-context-switch/api/*`,由插件在服务端实现。
|
|
9
|
+
*/
|
|
10
|
+
;(() => {
|
|
11
|
+
'use strict'
|
|
12
|
+
if (window.__dshContextSwitchMounted) return
|
|
13
|
+
window.__dshContextSwitchMounted = true
|
|
14
|
+
|
|
15
|
+
const BASE = '/dsh-context-switch'
|
|
16
|
+
|
|
17
|
+
// 整个挂载过程包一层:以前它一旦抛错就静默死掉,页面上什么都没有,
|
|
18
|
+
// 只能靠翻服务端日志猜「到底挂没挂」。现在失败会在页面角落留一条红字。
|
|
19
|
+
try {
|
|
20
|
+
|
|
21
|
+
// ── 样式 ──────────────────────────────────────────────────
|
|
22
|
+
const style = document.createElement('style')
|
|
23
|
+
style.textContent = `
|
|
24
|
+
/* 按钮:默认按「输入框工具栏」的样子长——28×28 的圆,跟旁边那个 + 一样大。
|
|
25
|
+
数值是照 DSH 自己的 + 按钮量出来的(28×28 / radius:999px / rgb(245,246,247)),
|
|
26
|
+
不是为了好看瞎定的;DSH 改版后这里要跟着重新量。 */
|
|
27
|
+
#dcs-btn {
|
|
28
|
+
position: relative;
|
|
29
|
+
display: grid; place-items: center;
|
|
30
|
+
width: 28px; height: 28px; flex: 0 0 auto;
|
|
31
|
+
padding: 0; border: 0; border-radius: 999px;
|
|
32
|
+
background: rgb(245, 246, 247); color: rgb(15, 17, 21);
|
|
33
|
+
cursor: pointer; user-select: none;
|
|
34
|
+
transition: background .12s ease, transform .12s ease;
|
|
35
|
+
}
|
|
36
|
+
#dcs-btn:hover { background: rgb(233, 236, 239); }
|
|
37
|
+
#dcs-btn:active { transform: scale(.94); }
|
|
38
|
+
@media (prefers-color-scheme: dark) {
|
|
39
|
+
#dcs-btn { background: rgba(255,255,255,.10); color: #e5e7eb; }
|
|
40
|
+
#dcs-btn:hover { background: rgba(255,255,255,.17); }
|
|
41
|
+
}
|
|
42
|
+
#dcs-btn > svg { width: 14px; height: 14px; display: block; }
|
|
43
|
+
|
|
44
|
+
/* 小圆点:贴在按钮右下角。绿=有上下文在注入,灰=关着。 */
|
|
45
|
+
#dcs-dot {
|
|
46
|
+
position: absolute; right: 1px; bottom: 1px;
|
|
47
|
+
width: 6px; height: 6px; border-radius: 50%;
|
|
48
|
+
background: #22c55e;
|
|
49
|
+
box-shadow: 0 0 0 1.5px rgb(245, 246, 247);
|
|
50
|
+
}
|
|
51
|
+
@media (prefers-color-scheme: dark) {
|
|
52
|
+
#dcs-dot { box-shadow: 0 0 0 1.5px #2b2c30; }
|
|
53
|
+
}
|
|
54
|
+
#dcs-btn[data-off="1"] #dcs-dot { background: #9ca3af; }
|
|
55
|
+
|
|
56
|
+
/* 文字标签:塞进输入框时藏起来(28px 放不下),只在降级浮窗里露脸 */
|
|
57
|
+
#dcs-label {
|
|
58
|
+
display: none;
|
|
59
|
+
max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
|
60
|
+
font: 12px/1.2 system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* 降级:找不到输入框那排(DSH 改版了)就退回右下角浮着——旧样子原样保留,
|
|
64
|
+
最坏情况也不会比之前差。 */
|
|
65
|
+
#dcs-btn[data-dcs-place="floating"] {
|
|
66
|
+
position: fixed; right: 18px; bottom: 96px; z-index: 2147483000;
|
|
67
|
+
display: flex; align-items: center; gap: 6px;
|
|
68
|
+
width: auto; height: auto; padding: 7px 12px;
|
|
69
|
+
background: rgba(255,255,255,.92);
|
|
70
|
+
border: 1px solid rgba(0,0,0,.10);
|
|
71
|
+
box-shadow: 0 2px 10px rgba(0,0,0,.10);
|
|
72
|
+
backdrop-filter: blur(6px);
|
|
73
|
+
transition: transform .12s ease, box-shadow .12s ease;
|
|
74
|
+
}
|
|
75
|
+
#dcs-btn[data-dcs-place="floating"]:hover {
|
|
76
|
+
transform: translateY(-1px); box-shadow: 0 4px 14px rgba(0,0,0,.14);
|
|
77
|
+
}
|
|
78
|
+
#dcs-btn[data-dcs-place="floating"]:active { transform: translateY(0); }
|
|
79
|
+
@media (prefers-color-scheme: dark) {
|
|
80
|
+
#dcs-btn[data-dcs-place="floating"] {
|
|
81
|
+
background: rgba(32,33,36,.92); border-color: rgba(255,255,255,.12);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
#dcs-btn[data-dcs-place="floating"] #dcs-dot {
|
|
85
|
+
position: static; width: 7px; height: 7px; box-shadow: none;
|
|
86
|
+
}
|
|
87
|
+
#dcs-btn[data-dcs-place="floating"] #dcs-label { display: inline; }
|
|
88
|
+
|
|
89
|
+
/* 提示气泡:按钮只有 28px 塞不下字,切换结果改成旁边冒一句就走 */
|
|
90
|
+
/* 切换记录 + 文件位置:一行「已换 N 轮」,一行「文件在哪」 */
|
|
91
|
+
.dcs-meta {
|
|
92
|
+
padding: 6px 12px;
|
|
93
|
+
font: 11px/1.6 ui-monospace, Menlo, Consolas, monospace;
|
|
94
|
+
color: #6b7280;
|
|
95
|
+
display: flex;
|
|
96
|
+
flex-direction: column;
|
|
97
|
+
gap: 2px;
|
|
98
|
+
border-top: 1px solid rgba(0, 0, 0, 0.06);
|
|
99
|
+
}
|
|
100
|
+
.dcs-meta span {
|
|
101
|
+
white-space: nowrap;
|
|
102
|
+
overflow: hidden;
|
|
103
|
+
text-overflow: ellipsis;
|
|
104
|
+
}
|
|
105
|
+
.dcs-meta code {
|
|
106
|
+
font-size: 10px;
|
|
107
|
+
}
|
|
108
|
+
#dcs-flash {
|
|
109
|
+
position: fixed; z-index: 2147483001;
|
|
110
|
+
display: none; padding: 4px 9px; border-radius: 8px;
|
|
111
|
+
font: 11.5px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
112
|
+
color: #fff; background: rgba(24, 26, 30, .88);
|
|
113
|
+
box-shadow: 0 4px 14px rgba(0,0,0,.18);
|
|
114
|
+
pointer-events: none; white-space: nowrap;
|
|
115
|
+
}
|
|
116
|
+
#dcs-flash[data-show="1"] { display: block; }
|
|
117
|
+
|
|
118
|
+
/* 面板:位置由 JS 算(placePanel)。它挂在 body 上、用 fixed 定位,
|
|
119
|
+
所以不受卡片/滚动容器的 overflow 和 transform 影响——比塞进按钮里稳。 */
|
|
120
|
+
#dcs-panel {
|
|
121
|
+
position: fixed; left: 0; top: 0; z-index: 2147483000;
|
|
122
|
+
width: 220px; max-height: 60vh; overflow: auto;
|
|
123
|
+
box-sizing: border-box;
|
|
124
|
+
border-radius: 12px; padding: 8px;
|
|
125
|
+
font: 12px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
126
|
+
color: #1f2937; background: rgba(255,255,255,.97);
|
|
127
|
+
border: 1px solid rgba(0,0,0,.10);
|
|
128
|
+
box-shadow: 0 8px 28px rgba(0,0,0,.18);
|
|
129
|
+
backdrop-filter: blur(10px);
|
|
130
|
+
display: none;
|
|
131
|
+
}
|
|
132
|
+
#dcs-panel[data-open="1"] { display: block; }
|
|
133
|
+
@media (prefers-color-scheme: dark) {
|
|
134
|
+
#dcs-panel { color: #e5e7eb; background: rgba(32,33,36,.97); border-color: rgba(255,255,255,.12); }
|
|
135
|
+
}
|
|
136
|
+
.dcs-head {
|
|
137
|
+
font-size: 11px; opacity: .55; padding: 4px 8px 6px;
|
|
138
|
+
display: flex; justify-content: space-between; align-items: center;
|
|
139
|
+
}
|
|
140
|
+
.dcs-item {
|
|
141
|
+
display: flex; align-items: center; justify-content: space-between; gap: 6px;
|
|
142
|
+
padding: 7px 8px; border-radius: 8px; cursor: pointer;
|
|
143
|
+
}
|
|
144
|
+
.dcs-item:hover { background: rgba(0,0,0,.06); }
|
|
145
|
+
@media (prefers-color-scheme: dark) { .dcs-item:hover { background: rgba(255,255,255,.08); } }
|
|
146
|
+
.dcs-item[data-active="1"] { background: rgba(34,197,94,.14); font-weight: 600; }
|
|
147
|
+
.dcs-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
148
|
+
.dcs-tag { font-size: 10px; opacity: .5; flex: 0 0 auto; }
|
|
149
|
+
.dcs-empty { padding: 10px 8px; opacity: .55; }
|
|
150
|
+
.dcs-sep { height: 1px; margin: 6px 4px; background: rgba(127,127,127,.22); }
|
|
151
|
+
.dcs-save {
|
|
152
|
+
padding: 7px 8px; border-radius: 8px; cursor: pointer;
|
|
153
|
+
display: flex; align-items: center; gap: 6px; opacity: .85;
|
|
154
|
+
}
|
|
155
|
+
.dcs-save:hover { background: rgba(0,0,0,.06); }
|
|
156
|
+
@media (prefers-color-scheme: dark) { .dcs-save:hover { background: rgba(255,255,255,.08); } }
|
|
157
|
+
|
|
158
|
+
/* ── 自动挡 ──────────────────────────────────────────── */
|
|
159
|
+
/* 每套上下文右边的小时段,比如「22:00-06:00」 */
|
|
160
|
+
.dcs-when { font-size: 9.5px; opacity: .42; flex: 0 0 auto; letter-spacing: .01em; }
|
|
161
|
+
/* 手动/自动的小标记,跟在标题后面 */
|
|
162
|
+
.dcs-badge {
|
|
163
|
+
font-size: 9px; padding: 0 5px; border-radius: 8px; line-height: 14px;
|
|
164
|
+
background: rgba(59,130,246,.16); color: #2563eb; flex: 0 0 auto;
|
|
165
|
+
}
|
|
166
|
+
@media (prefers-color-scheme: dark) {
|
|
167
|
+
.dcs-badge { background: rgba(96,165,250,.20); color: #93c5fd; }
|
|
168
|
+
}
|
|
169
|
+
.dcs-badge[data-kind="manual"] { background: rgba(245,158,11,.16); color: #d97706; }
|
|
170
|
+
@media (prefers-color-scheme: dark) {
|
|
171
|
+
.dcs-badge[data-kind="manual"] { background: rgba(251,191,36,.20); color: #fbbf24; }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/* ── 进度卡 ─────────────────────────────────────────── */
|
|
175
|
+
.dcs-now { margin-top: 4px; border-top: 1px solid rgba(127,127,127,.22); padding-top: 6px; }
|
|
176
|
+
.dcs-now-hint {
|
|
177
|
+
font-size: 9.5px; opacity: .4; padding: 1px 6px 5px;
|
|
178
|
+
letter-spacing: .02em;
|
|
179
|
+
}
|
|
180
|
+
.dcs-now-sec { padding: 4px 2px 6px; }
|
|
181
|
+
.dcs-now-h {
|
|
182
|
+
display: flex; align-items: center; justify-content: space-between;
|
|
183
|
+
font-size: 10px; letter-spacing: .04em; opacity: .5; padding: 2px 6px 4px;
|
|
184
|
+
}
|
|
185
|
+
.dcs-now-n {
|
|
186
|
+
font-size: 9px; padding: 0 5px; border-radius: 8px;
|
|
187
|
+
background: rgba(127,127,127,.22); line-height: 14px;
|
|
188
|
+
}
|
|
189
|
+
.dcs-now-b { font-size: 11.5px; line-height: 1.5; padding: 0 6px; }
|
|
190
|
+
.dcs-now-li { display: flex; gap: 5px; align-items: flex-start; }
|
|
191
|
+
.dcs-now-m { opacity: .45; flex: 0 0 auto; }
|
|
192
|
+
.dcs-now-meta { font-size: 10px; opacity: .45; padding-bottom: 3px; }
|
|
193
|
+
.dcs-now-msg { opacity: .8; padding: 1px 0; }
|
|
194
|
+
.dcs-now-who {
|
|
195
|
+
font-size: 9px; opacity: .6; margin-right: 5px;
|
|
196
|
+
padding: 0 4px; border-radius: 6px; background: rgba(127,127,127,.18);
|
|
197
|
+
}
|
|
198
|
+
`
|
|
199
|
+
document.head.appendChild(style)
|
|
200
|
+
|
|
201
|
+
// ── 控件 ──────────────────────────────────────────────────
|
|
202
|
+
const btn = document.createElement('button')
|
|
203
|
+
btn.id = 'dcs-btn'
|
|
204
|
+
btn.type = 'button'
|
|
205
|
+
btn.title = '切换上下文(点击选择)'
|
|
206
|
+
// 图标走 DSH 自带的图标系统(<use href="#dsw-icon-…">),跟旁边的 + 是同一套:
|
|
207
|
+
// 尺寸/明暗/描边都跟着主题走,不用自己画。context-injection =「上下文注入」,字面就对。
|
|
208
|
+
btn.innerHTML =
|
|
209
|
+
'<svg aria-hidden="true"><use href="#dsw-icon-context-injection-outline-medium"></use></svg>' +
|
|
210
|
+
'<span id="dcs-dot"></span>' +
|
|
211
|
+
'<span id="dcs-label">上下文</span>'
|
|
212
|
+
|
|
213
|
+
const panel = document.createElement('div')
|
|
214
|
+
panel.id = 'dcs-panel'
|
|
215
|
+
|
|
216
|
+
document.body.appendChild(panel)
|
|
217
|
+
// 按钮先挂 body 兜底,再由 mount() 挪进输入框那排
|
|
218
|
+
document.body.appendChild(btn)
|
|
219
|
+
|
|
220
|
+
/* ── 落点:输入框工具栏 ──────────────────────────────────────
|
|
221
|
+
找 DSH 那个「+」按钮,它的爹就是工具栏(显式 gap:12px 的 flex 行)。
|
|
222
|
+
为什么不用 class 定位:`GRgSLq_` 是构建期 hash,DSH 一升级就变;
|
|
223
|
+
aria-label 是给人读的,稳得多。
|
|
224
|
+
两条路都不通 → 返回 null → 降级成右下角浮窗,功能不断。 */
|
|
225
|
+
function findToolsRow() {
|
|
226
|
+
// 主路:+ 按钮(中文 aria-label)
|
|
227
|
+
const plus = document.querySelector('button[aria-label*="添加文件或调用指令"]')
|
|
228
|
+
if (plus && plus.parentElement) return { row: plus.parentElement, plus }
|
|
229
|
+
// 备路:从输入框本体往上找它旁边的工具栏
|
|
230
|
+
const ed = document.querySelector('[contenteditable="true"]')
|
|
231
|
+
const card = ed && ed.closest('div[class*="card"]')
|
|
232
|
+
if (card) {
|
|
233
|
+
const rows = card.querySelectorAll('div[class*="tools"], div[class*="row"]')
|
|
234
|
+
for (const r of rows) {
|
|
235
|
+
if (r.querySelector('button')) return { row: r, plus: null }
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** 把按钮放进输入框那排;返回落点名字,写进 data-dcs-place 方便自证。 */
|
|
242
|
+
function mount() {
|
|
243
|
+
const hit = findToolsRow()
|
|
244
|
+
if (!hit) {
|
|
245
|
+
// 降级:留在 body 上,走旧的浮动样式
|
|
246
|
+
if (btn.parentElement !== document.body) document.body.appendChild(btn)
|
|
247
|
+
btn.dataset.dcsPlace = 'floating'
|
|
248
|
+
return 'floating'
|
|
249
|
+
}
|
|
250
|
+
if (btn.parentElement === hit.row) {
|
|
251
|
+
btn.dataset.dcsPlace = 'composer'
|
|
252
|
+
return 'composer' // 已经在位就别动(幂等)
|
|
253
|
+
}
|
|
254
|
+
// 插在 + 后面;没有 + 就插最前
|
|
255
|
+
if (hit.plus) {
|
|
256
|
+
if (hit.plus.nextSibling) hit.row.insertBefore(btn, hit.plus.nextSibling)
|
|
257
|
+
else hit.row.appendChild(btn)
|
|
258
|
+
} else {
|
|
259
|
+
hit.row.insertBefore(btn, hit.row.firstChild)
|
|
260
|
+
}
|
|
261
|
+
btn.dataset.dcsPlace = 'composer'
|
|
262
|
+
return 'composer'
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** 把面板摆到按钮上方(上方放不下就落下方),并按可用空间限高。
|
|
266
|
+
输入框本来就贴着屏幕底部,所以「先量空间再决定往哪弹」是必须的——
|
|
267
|
+
不然面板会有一半钻到视口外面去。 */
|
|
268
|
+
function placePanel() {
|
|
269
|
+
if (panel.dataset.open !== '1') return
|
|
270
|
+
const r = btn.getBoundingClientRect()
|
|
271
|
+
const vw = window.innerWidth
|
|
272
|
+
const vh = window.innerHeight
|
|
273
|
+
const pw = panel.offsetWidth || 220
|
|
274
|
+
const pad = 8
|
|
275
|
+
|
|
276
|
+
// 横向:贴按钮左边;右边出界就往左推,但别越过左边缘
|
|
277
|
+
let x = r.left
|
|
278
|
+
if (x + pw > vw - pad) x = vw - pw - pad
|
|
279
|
+
if (x < pad) x = pad
|
|
280
|
+
|
|
281
|
+
// 纵向:算两头各剩多少,挑大的那头,再把面板限到能装下
|
|
282
|
+
const spaceAbove = r.top - pad * 2
|
|
283
|
+
const spaceBelow = vh - r.bottom - pad * 2
|
|
284
|
+
const above = spaceAbove >= spaceBelow
|
|
285
|
+
const room = Math.max(spaceAbove, spaceBelow)
|
|
286
|
+
panel.style.maxHeight = Math.max(120, Math.min(Math.round(vh * 0.6), room)) + 'px'
|
|
287
|
+
|
|
288
|
+
// 限高之后高度会变,得重新量一次才知道真正要占多少
|
|
289
|
+
const ph = panel.offsetHeight || 200
|
|
290
|
+
let y = above ? r.top - ph - pad : r.bottom + pad
|
|
291
|
+
if (y < pad) y = pad
|
|
292
|
+
if (y + ph > vh - pad) y = Math.max(pad, vh - ph - pad)
|
|
293
|
+
|
|
294
|
+
panel.style.left = Math.round(x) + 'px'
|
|
295
|
+
panel.style.top = Math.round(y) + 'px'
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 自证:排查「按钮不见了」时先看 <html data-dcs-mounted>——
|
|
299
|
+
// 有 "1" = 挂上了(那就是被挡住 / 看不见,别再查注入)
|
|
300
|
+
// "error" = 脚本抛错了(页面上有红条 #dcs-err)
|
|
301
|
+
// 没有 = 脚本压根没加载(问题在注入那环)
|
|
302
|
+
// 落点另记一笔:data-dcs-place = composer(住进输入框)/ floating(降级浮窗)
|
|
303
|
+
document.documentElement.dataset.dcsMounted = '1'
|
|
304
|
+
|
|
305
|
+
let state = {
|
|
306
|
+
active: '',
|
|
307
|
+
available: [],
|
|
308
|
+
drag: true,
|
|
309
|
+
/** 这颗胶囊现在归谁管:config / auto / manual(服务端 /api/state 给的) */
|
|
310
|
+
source: 'config',
|
|
311
|
+
/** 每套上下文声明的时段:[{ name, slots: [{start,end,text}], hasWhen }] */
|
|
312
|
+
schedule: [],
|
|
313
|
+
/** 换了几轮(服务端 /api/state 给的) */
|
|
314
|
+
switchCount: 0,
|
|
315
|
+
/** 上下文文件放在哪(服务端 /api/state 给的绝对路径) */
|
|
316
|
+
contextDir: '',
|
|
317
|
+
}
|
|
318
|
+
/** NOW 卡:{ now: {doing,todo,done,board}|null, tail: {messages,sessionId,mtime,error} } */
|
|
319
|
+
let nowCard = { now: null, tail: { messages: [], sessionId: null, mtime: null, error: null } }
|
|
320
|
+
|
|
321
|
+
async function api(path, init) {
|
|
322
|
+
const res = await fetch(BASE + path, {
|
|
323
|
+
credentials: 'same-origin',
|
|
324
|
+
headers: { 'content-type': 'application/json' },
|
|
325
|
+
...init,
|
|
326
|
+
})
|
|
327
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`)
|
|
328
|
+
return res.json()
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** 这套上下文的时段文字(没声明就返回空串) */
|
|
332
|
+
function whenText(name) {
|
|
333
|
+
const s = (state.schedule || []).find((x) => x.name === name)
|
|
334
|
+
if (!s || !s.slots?.length) return ''
|
|
335
|
+
return s.slots.map((x) => x.text).join(' ')
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** 现在这颗胶囊归谁管 —— 手动/自动的小标 */
|
|
339
|
+
function sourceBadge() {
|
|
340
|
+
if (state.source === 'manual') return { text: '手动', kind: 'manual' }
|
|
341
|
+
if (state.source === 'auto') return { text: '自动', kind: 'auto' }
|
|
342
|
+
return null
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function render() {
|
|
346
|
+
panel.innerHTML = ''
|
|
347
|
+
const badge = sourceBadge()
|
|
348
|
+
const head = document.createElement('div')
|
|
349
|
+
head.className = 'dcs-head'
|
|
350
|
+
head.innerHTML =
|
|
351
|
+
`<span>上下文${
|
|
352
|
+
badge ? ` <span class="dcs-badge" data-kind="${badge.kind}">${badge.text}</span>` : ''
|
|
353
|
+
}</span><span>${state.available.length} 套</span>`
|
|
354
|
+
panel.appendChild(head)
|
|
355
|
+
|
|
356
|
+
if (!state.available.length) {
|
|
357
|
+
const empty = document.createElement('div')
|
|
358
|
+
empty.className = 'dcs-empty'
|
|
359
|
+
empty.textContent = '这个目录下还没有上下文'
|
|
360
|
+
panel.appendChild(empty)
|
|
361
|
+
return
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
for (const name of state.available) {
|
|
365
|
+
const item = document.createElement('div')
|
|
366
|
+
item.className = 'dcs-item'
|
|
367
|
+
item.dataset.active = name === state.active ? '1' : '0'
|
|
368
|
+
item.dataset.name = name
|
|
369
|
+
const when = whenText(name)
|
|
370
|
+
item.innerHTML =
|
|
371
|
+
`<span class="dcs-name">${escapeHtml(name)}</span>` +
|
|
372
|
+
`<span style="display:flex;gap:5px;align-items:center;flex:0 0 auto">` +
|
|
373
|
+
(when ? `<span class="dcs-when">${escapeHtml(when)}</span>` : '') +
|
|
374
|
+
(name === state.active ? '<span class="dcs-tag">当前</span>' : '') +
|
|
375
|
+
`</span>`
|
|
376
|
+
item.addEventListener('click', () => switchTo(name, false))
|
|
377
|
+
panel.appendChild(item)
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const sep = document.createElement('div')
|
|
381
|
+
sep.className = 'dcs-sep'
|
|
382
|
+
panel.appendChild(sep)
|
|
383
|
+
|
|
384
|
+
const off = document.createElement('div')
|
|
385
|
+
off.className = 'dcs-save'
|
|
386
|
+
off.textContent = state.active ? '关闭注入' : '已关闭'
|
|
387
|
+
off.addEventListener('click', () => switchTo('', true))
|
|
388
|
+
panel.appendChild(off)
|
|
389
|
+
|
|
390
|
+
const save = document.createElement('div')
|
|
391
|
+
save.className = 'dcs-save'
|
|
392
|
+
save.innerHTML = '<span>💾</span><span>固定当前(写回配置)</span>'
|
|
393
|
+
save.addEventListener('click', async () => {
|
|
394
|
+
if (!state.active) return
|
|
395
|
+
try {
|
|
396
|
+
await api('/api/switch', { method: 'POST', body: JSON.stringify({ name: state.active, persist: true }) })
|
|
397
|
+
flash(`已固定 ${state.active}`)
|
|
398
|
+
} catch (e) {
|
|
399
|
+
flash('固定失败:' + e.message)
|
|
400
|
+
}
|
|
401
|
+
})
|
|
402
|
+
panel.appendChild(save)
|
|
403
|
+
|
|
404
|
+
// ── 恢复自动:只在手动切开过之后出现 ──────────────────────
|
|
405
|
+
// 你不点它,时间表就一直哑着——这是刻意的,为了不让你刚切过去就被切回来。
|
|
406
|
+
if (state.source === 'manual') {
|
|
407
|
+
const auto = document.createElement('div')
|
|
408
|
+
auto.className = 'dcs-save'
|
|
409
|
+
auto.innerHTML = '<span>🔄</span><span>恢复自动(按时间表)</span>'
|
|
410
|
+
auto.addEventListener('click', async () => {
|
|
411
|
+
try {
|
|
412
|
+
const r = await api('/api/switch', { method: 'POST', body: JSON.stringify({ auto: true }) })
|
|
413
|
+
state.active = r.active || ''
|
|
414
|
+
state.source = r.source || 'auto'
|
|
415
|
+
paintButton()
|
|
416
|
+
render()
|
|
417
|
+
flash(r.matched ? '已交给时间表' : '时间表暂无匹配,保持现状')
|
|
418
|
+
} catch (e) {
|
|
419
|
+
flash('失败:' + e.message)
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
panel.appendChild(auto)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ── 切换记录 + 文件位置:让用户看得见「换了几轮」「文件在哪」 ──
|
|
426
|
+
const meta = document.createElement('div')
|
|
427
|
+
meta.className = 'dcs-meta'
|
|
428
|
+
meta.innerHTML =
|
|
429
|
+
`<span>↻ 已换 <b>${state.switchCount || 0}</b> 轮</span>` +
|
|
430
|
+
`<span>📁 文件在 <code>${escapeHtml(state.contextDir || '')}</code></span>`
|
|
431
|
+
panel.appendChild(meta)
|
|
432
|
+
|
|
433
|
+
// ── NOW 卡:正在做 / 未做完 / 已做完 / 上个会话的尾巴 ──────────
|
|
434
|
+
renderNowCard()
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** 把进度卡渲染进面板 */
|
|
438
|
+
function renderNowCard() {
|
|
439
|
+
const n = nowCard.now
|
|
440
|
+
const t = nowCard.tail || {}
|
|
441
|
+
const hasAnything =
|
|
442
|
+
n && (n.doing?.length || n.todo?.length || n.done?.length || n.board) || (t.messages?.length)
|
|
443
|
+
if (!hasAnything) return
|
|
444
|
+
|
|
445
|
+
const box = document.createElement('div')
|
|
446
|
+
box.className = 'dcs-now'
|
|
447
|
+
|
|
448
|
+
// 一行小字:讲清这张卡是什么——它不是附加功能,是「连续会话记忆不断片」
|
|
449
|
+
const hint = document.createElement('div')
|
|
450
|
+
hint.className = 'dcs-now-hint'
|
|
451
|
+
hint.textContent = '记忆卡[主要功能是.连续会话记忆不断片]'
|
|
452
|
+
box.appendChild(hint)
|
|
453
|
+
|
|
454
|
+
const section = (title, count, bodyHtml) => {
|
|
455
|
+
const sec = document.createElement('div')
|
|
456
|
+
sec.className = 'dcs-now-sec'
|
|
457
|
+
const h = document.createElement('div')
|
|
458
|
+
h.className = 'dcs-now-h'
|
|
459
|
+
h.innerHTML = `<span>${escapeHtml(title)}</span>${count != null ? `<span class="dcs-now-n">${count}</span>` : ''}`
|
|
460
|
+
sec.appendChild(h)
|
|
461
|
+
const b = document.createElement('div')
|
|
462
|
+
b.className = 'dcs-now-b'
|
|
463
|
+
b.innerHTML = bodyHtml
|
|
464
|
+
sec.appendChild(b)
|
|
465
|
+
box.appendChild(sec)
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const listHtml = (items, mark) =>
|
|
469
|
+
items.map((x) => `<div class="dcs-now-li"><span class="dcs-now-m">${mark}</span><span>${escapeHtml(x)}</span></div>`).join('')
|
|
470
|
+
|
|
471
|
+
if (n?.doing?.length) section('正在做', null, escapeHtml(n.doing.join('\n')).replace(/\n/g, '<br>'))
|
|
472
|
+
if (n?.todo?.length) section('未做完', n.todo.length, listHtml(n.todo, '☐'))
|
|
473
|
+
if (n?.done?.length) section('已做完', n.done.length, listHtml(n.done, '☑'))
|
|
474
|
+
if (n?.board) section('黑板', null, escapeHtml(n.board).replace(/\n/g, '<br>'))
|
|
475
|
+
|
|
476
|
+
if (t.messages?.length) {
|
|
477
|
+
const when = t.mtime ? new Date(t.mtime).toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }) : ''
|
|
478
|
+
const head = when ? `${when} · ${t.messages.length} 条` : `${t.messages.length} 条`
|
|
479
|
+
section(
|
|
480
|
+
'上个会话的尾巴',
|
|
481
|
+
null,
|
|
482
|
+
`<div class="dcs-now-meta">${escapeHtml(head)}</div>` +
|
|
483
|
+
t.messages
|
|
484
|
+
.map((m) => {
|
|
485
|
+
const who = m.role === 'user' ? '你' : '我'
|
|
486
|
+
return `<div class="dcs-now-msg"><span class="dcs-now-who">${who}</span>${escapeHtml((m.text || '').replace(/\s+/g, ' ').slice(0, 160))}</div>`
|
|
487
|
+
})
|
|
488
|
+
.join(''),
|
|
489
|
+
)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
panel.appendChild(box)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
let flashTimer = null
|
|
496
|
+
let flashEl = null
|
|
497
|
+
/** 按钮旁边冒一句短话,1.6 秒后自己消失(按钮只有 28px,写不下文字)。 */
|
|
498
|
+
function flash(text) {
|
|
499
|
+
if (!flashEl) {
|
|
500
|
+
flashEl = document.createElement('div')
|
|
501
|
+
flashEl.id = 'dcs-flash'
|
|
502
|
+
document.body.appendChild(flashEl)
|
|
503
|
+
}
|
|
504
|
+
flashEl.textContent = text
|
|
505
|
+
const r = btn.getBoundingClientRect()
|
|
506
|
+
flashEl.style.left = Math.round(r.left) + 'px'
|
|
507
|
+
flashEl.style.top = Math.round(r.top - 28) + 'px'
|
|
508
|
+
flashEl.dataset.show = '1'
|
|
509
|
+
clearTimeout(flashTimer)
|
|
510
|
+
flashTimer = setTimeout(() => {
|
|
511
|
+
flashEl.dataset.show = '0'
|
|
512
|
+
}, 1600)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function paintButton() {
|
|
516
|
+
const label = btn.querySelector('#dcs-label')
|
|
517
|
+
const b = sourceBadge()
|
|
518
|
+
// 标签现在只在降级浮窗里显示(输入框那排 28px 塞不下),但 title 一直有用。
|
|
519
|
+
// brief 里已经带了「· 自动/手动」,就别再拼一次来源——之前拼重了。
|
|
520
|
+
const brief = state.active ? state.active + (b ? ` · ${b.text}` : '') : '关'
|
|
521
|
+
if (label) label.textContent = brief
|
|
522
|
+
btn.dataset.off = state.active ? '0' : '1'
|
|
523
|
+
btn.title = state.active ? `上下文:${brief} 点击切换` : '上下文注入已关闭,点击选择'
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
async function refresh() {
|
|
527
|
+
try {
|
|
528
|
+
const s = await api('/api/state')
|
|
529
|
+
state.active = s.active || ''
|
|
530
|
+
state.available = s.available || []
|
|
531
|
+
state.source = s.source || 'config'
|
|
532
|
+
state.schedule = Array.isArray(s.schedule) ? s.schedule : []
|
|
533
|
+
// 换了几轮 + 文件放哪(面板底部那两行靠它们)
|
|
534
|
+
state.switchCount = Number(s.switchCount) || 0
|
|
535
|
+
state.contextDir = s.contextDir || ''
|
|
536
|
+
} catch {
|
|
537
|
+
state.active = ''
|
|
538
|
+
state.available = []
|
|
539
|
+
state.source = 'config'
|
|
540
|
+
state.schedule = []
|
|
541
|
+
state.switchCount = 0
|
|
542
|
+
state.contextDir = ''
|
|
543
|
+
}
|
|
544
|
+
// NOW 卡单独拉:失败不影响切换功能
|
|
545
|
+
try {
|
|
546
|
+
const n = await api('/api/now')
|
|
547
|
+
nowCard = { now: n.now || null, tail: n.tail || { messages: [] } }
|
|
548
|
+
} catch {
|
|
549
|
+
nowCard = { now: null, tail: { messages: [] } }
|
|
550
|
+
}
|
|
551
|
+
paintButton()
|
|
552
|
+
if (panel.dataset.open === '1') render()
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
async function switchTo(name, persist) {
|
|
556
|
+
try {
|
|
557
|
+
const r = await api('/api/switch', {
|
|
558
|
+
method: 'POST',
|
|
559
|
+
body: JSON.stringify({ name, persist: Boolean(persist) }),
|
|
560
|
+
})
|
|
561
|
+
state.active = r.active || ''
|
|
562
|
+
state.available = r.available || state.available
|
|
563
|
+
// 服务端会说这次是谁定的(手动点 → 'manual',自动挡 → 'auto')
|
|
564
|
+
if (r.source) state.source = r.source
|
|
565
|
+
paintButton()
|
|
566
|
+
render()
|
|
567
|
+
flash(name ? `→ ${name}` : '已关闭')
|
|
568
|
+
} catch (e) {
|
|
569
|
+
flash('失败:' + e.message)
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function escapeHtml(s) {
|
|
574
|
+
return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c])
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// ── 交互 ──────────────────────────────────────────────────
|
|
578
|
+
btn.addEventListener('click', (e) => {
|
|
579
|
+
e.stopPropagation()
|
|
580
|
+
const open = panel.dataset.open === '1'
|
|
581
|
+
panel.dataset.open = open ? '0' : '1'
|
|
582
|
+
if (!open) {
|
|
583
|
+
render() // 先填内容,量得到真实高度
|
|
584
|
+
placePanel() // 再摆位置
|
|
585
|
+
refresh()
|
|
586
|
+
}
|
|
587
|
+
})
|
|
588
|
+
|
|
589
|
+
panel.addEventListener('click', (e) => e.stopPropagation())
|
|
590
|
+
document.addEventListener('click', () => {
|
|
591
|
+
panel.dataset.open = '0'
|
|
592
|
+
})
|
|
593
|
+
document.addEventListener('keydown', (e) => {
|
|
594
|
+
if (e.key === 'Escape') panel.dataset.open = '0'
|
|
595
|
+
})
|
|
596
|
+
// 窗口尺寸变了,开着就重新摆一次
|
|
597
|
+
window.addEventListener('resize', placePanel)
|
|
598
|
+
|
|
599
|
+
// ── 启动 ──────────────────────────────────────────────────
|
|
600
|
+
mount()
|
|
601
|
+
|
|
602
|
+
/* 兜底:输入框那块是 React 管的,重渲染偶尔会把它不认识的节点冲掉。
|
|
603
|
+
实测(2026-11-27)往工具栏塞节点、打字触发重渲染后都还在,但仍然留一道保险:
|
|
604
|
+
守着 body 子树,发现按钮不在工具栏里就塞回去;连着三次塞不回去就认输(转浮窗)。 */
|
|
605
|
+
let missCount = 0
|
|
606
|
+
let obsTimer = null
|
|
607
|
+
const observer = new MutationObserver(() => {
|
|
608
|
+
if (obsTimer) return
|
|
609
|
+
obsTimer = setTimeout(() => {
|
|
610
|
+
obsTimer = null
|
|
611
|
+
const seated = btn.isConnected && btn.parentElement && btn.parentElement !== document.body
|
|
612
|
+
if (seated) return // 好好待在工具栏里,什么都不用做
|
|
613
|
+
if (mount() === 'composer') missCount = 0
|
|
614
|
+
else if (++missCount >= 3) observer.disconnect() // 连着三次都只能浮窗,就别再空跑了
|
|
615
|
+
}, 300)
|
|
616
|
+
})
|
|
617
|
+
try {
|
|
618
|
+
observer.observe(document.body, { childList: true, subtree: true })
|
|
619
|
+
} catch {}
|
|
620
|
+
|
|
621
|
+
refresh()
|
|
622
|
+
// 轻量轮询,别的入口(斜杠命令 / 工具)切了之后按钮能跟上
|
|
623
|
+
setInterval(refresh, 5000)
|
|
624
|
+
} catch (e) {
|
|
625
|
+
// 挂不上时留证据:红条 + <html data-dcs-mounted="error">
|
|
626
|
+
try {
|
|
627
|
+
document.documentElement.dataset.dcsMounted = 'error'
|
|
628
|
+
const d = document.createElement('div')
|
|
629
|
+
d.id = 'dcs-err'
|
|
630
|
+
d.textContent = 'context-switch UI 出错: ' + (e && e.message ? e.message : String(e))
|
|
631
|
+
Object.assign(d.style, {
|
|
632
|
+
position: 'fixed', top: '0', left: '0', zIndex: '2147483647',
|
|
633
|
+
background: '#dc2626', color: '#fff', font: '12px/1.5 monospace',
|
|
634
|
+
padding: '4px 10px', borderRadius: '0 0 6px 0', maxWidth: '80vw',
|
|
635
|
+
})
|
|
636
|
+
document.body.appendChild(d)
|
|
637
|
+
setTimeout(() => d.remove(), 10000)
|
|
638
|
+
} catch {}
|
|
639
|
+
// 也让控制台留个完整堆栈
|
|
640
|
+
console.error('[context-switch] UI 挂载失败', e)
|
|
641
|
+
}
|
|
642
|
+
})()
|