@zifeng1023/dsh-bot 0.0.0-stage → 0.3.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 +96 -2
- package/assets/icon-180.png +0 -0
- package/cordis.patch.yml +37 -0
- package/dsh-plugin.json +124 -0
- package/icon.png +0 -0
- package/lib/client.js +4017 -0
- package/lib/index.js +7197 -0
- package/locale/en.json +15 -0
- package/locale/zh.json +15 -0
- package/package.json +91 -4
- package/screenshots.json +3 -0
package/lib/client.js
ADDED
|
@@ -0,0 +1,4017 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-bot 面板 — 浏览器端
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ 反复崩溃换来的约束(每条都有实际报错佐证):
|
|
5
|
+
* 1. id 必须是 package.json 的包名(dsh-bot)—— 否则 duplicate factory
|
|
6
|
+
* 2. 产物必须是 lib/client.js —— 否则 import failed
|
|
7
|
+
* 3. 用到 ctx.slots 必须在 inject 声明 'slots' —— 否则 entry did not activate
|
|
8
|
+
* 4. apply 与组件都要 try/catch —— 否则拖垮整个 web boot
|
|
9
|
+
*
|
|
10
|
+
* 🎨 样式规范全部来自 DSH 自己的 CSS(不是猜的):
|
|
11
|
+
* · 按钮: height 28px / padding 0 10px / radius-sm
|
|
12
|
+
* · 主按钮文字色必须用 --dsw-alias-label-primary-foreground
|
|
13
|
+
* (之前写死 #fff,在浅色主题下会白底白字,看不出按钮)
|
|
14
|
+
* · 卡片: bg-layer-2 + border-l1 + radius-lg
|
|
15
|
+
* · 过渡: .15s ease
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
// ============================================================
|
|
19
|
+
// 设计令牌
|
|
20
|
+
// ============================================================
|
|
21
|
+
|
|
22
|
+
const BG_BASE = 'var(--dsw-alias-bg-base)';
|
|
23
|
+
const BG_L1 = 'var(--dsw-alias-bg-layer-1)';
|
|
24
|
+
const BG_L2 = 'var(--dsw-alias-bg-layer-2)';
|
|
25
|
+
const BG_L3 = 'var(--dsw-alias-bg-layer-3)';
|
|
26
|
+
|
|
27
|
+
const BORDER_L1 = 'var(--dsw-alias-border-l1)';
|
|
28
|
+
const BORDER_L2 = 'var(--dsw-alias-border-l2)';
|
|
29
|
+
|
|
30
|
+
const LABEL_1 = 'var(--dsw-alias-label-primary)';
|
|
31
|
+
const LABEL_2 = 'var(--dsw-alias-label-secondary)';
|
|
32
|
+
const LABEL_3 = 'var(--dsw-alias-label-tertiary)';
|
|
33
|
+
const LABEL_FG = 'var(--dsw-alias-label-primary-foreground)';
|
|
34
|
+
|
|
35
|
+
const BRAND = 'var(--dsw-alias-brand-primary)';
|
|
36
|
+
const BTN_PRIMARY = 'var(--dsw-alias-button-primary-fill)';
|
|
37
|
+
const BTN_PRIMARY_HOVER = 'var(--dsw-alias-button-primary-hover)';
|
|
38
|
+
const BTN_TOOLBAR_HOVER = 'var(--dsw-alias-button-tool-bar-hover)';
|
|
39
|
+
const HOVER_BG = 'var(--dsw-alias-interactive-bg-hover)';
|
|
40
|
+
const ACTIVE_BG = 'var(--dsw-alias-interactive-bg-active)';
|
|
41
|
+
const HOVER_DANGER = 'var(--dsw-alias-interactive-bg-hover-danger)';
|
|
42
|
+
|
|
43
|
+
const OK = 'var(--dsw-alias-state-success-primary)';
|
|
44
|
+
const WARN = 'var(--dsw-alias-state-warn-primary)';
|
|
45
|
+
const ERR = 'var(--dsw-alias-state-error-primary)';
|
|
46
|
+
const OK_TER = 'var(--dsw-alias-state-success-tertiary)';
|
|
47
|
+
const WARN_TER = 'var(--dsw-alias-state-warn-tertiary)';
|
|
48
|
+
|
|
49
|
+
const R_XS = 'var(--dsw-radius-xs)';
|
|
50
|
+
const R_SM = 'var(--dsw-radius-sm)';
|
|
51
|
+
const R_MD = 'var(--dsw-radius-md)';
|
|
52
|
+
const R_LG = 'var(--dsw-radius-lg)';
|
|
53
|
+
|
|
54
|
+
const FONT = 'var(--dsw-font-family)';
|
|
55
|
+
const SHADOW = 'var(--dsw-shadow-lv3)';
|
|
56
|
+
const FOCUS = 'var(--dsw-focus-ring-color)';
|
|
57
|
+
|
|
58
|
+
// ============================================================
|
|
59
|
+
// 执行节奏预设
|
|
60
|
+
//
|
|
61
|
+
// 用选择器而不是自由文本:自然语言识别总有猜不中的时候,
|
|
62
|
+
// 而"猜不中"的表现是静默失效——用户以为设好了,
|
|
63
|
+
// 其实 Bot 永远不会自己跑。
|
|
64
|
+
// ============================================================
|
|
65
|
+
|
|
66
|
+
const CADENCE_PRESETS = [
|
|
67
|
+
{ label: '每 30 分钟', value: '*/30 * * * *' },
|
|
68
|
+
{ label: '每小时', value: '0 * * * *' },
|
|
69
|
+
{ label: '每 4 小时', value: '0 */4 * * *' },
|
|
70
|
+
{ label: '每 6 小时', value: '0 */6 * * *' },
|
|
71
|
+
{ label: '每 12 小时', value: '0 */12 * * *' },
|
|
72
|
+
{ label: '每天早上 9 点', value: '0 9 * * *' },
|
|
73
|
+
{ label: '每天晚上 20 点', value: '0 20 * * *' },
|
|
74
|
+
{ label: '自定义时间', value: 'custom' },
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
/** 把 HH:MM 转成 cron */
|
|
78
|
+
function timeToCron(hhmm) {
|
|
79
|
+
const m = String(hhmm).match(/^(\d{1,2})\s*[::]\s*(\d{1,2})$/);
|
|
80
|
+
if (!m) return undefined;
|
|
81
|
+
const h = Number(m[1]);
|
|
82
|
+
const min = Number(m[2]);
|
|
83
|
+
if (h < 0 || h > 23 || min < 0 || min > 59) return undefined;
|
|
84
|
+
return `${min} ${h} * * *`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ============================================================
|
|
88
|
+
// React 句柄
|
|
89
|
+
//
|
|
90
|
+
// 图标组件在模块顶层就定义,需要 h();
|
|
91
|
+
// 但 react 只能在 factory 里 require。
|
|
92
|
+
// 所以这里先占位,factory 执行时再赋值。
|
|
93
|
+
// ============================================================
|
|
94
|
+
|
|
95
|
+
let React = null;
|
|
96
|
+
let h = null;
|
|
97
|
+
|
|
98
|
+
// ============================================================
|
|
99
|
+
// 图标(内联 SVG,不用 emoji)
|
|
100
|
+
// ============================================================
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 品牌图标:DeepSeek 鲸鱼 + Bot 天线
|
|
104
|
+
*
|
|
105
|
+
* 与 icon.svg 同源:鲸鱼轮廓取自 DeepSeek 官方 favicon,
|
|
106
|
+
* 加上天线 / 眼睛 / 微笑,成为「鲸鱼机器人」。
|
|
107
|
+
* currentColor 让它跟随主题文字色。
|
|
108
|
+
*/
|
|
109
|
+
const WHALE_PATH =
|
|
110
|
+
'M48.8354 10.0479C48.3232 9.79199 48.1025 10.2798 47.8032 10.5278C47.7007 10.6079 47.6143 10.7119 47.5273 10.8076C46.7793 11.624 45.9048 12.1597 44.7622 12.0957C43.0923 12 41.666 12.5356 40.4058 13.8398C40.1377 12.2319 39.2476 11.272 37.8926 10.6558C37.1836 10.3359 36.4668 10.0156 35.9702 9.31982C35.6235 8.82373 35.5293 8.27197 35.356 7.72754C35.2456 7.3999 35.1353 7.06396 34.7651 7.00781C34.3633 6.94385 34.2056 7.2876 34.0479 7.57568C33.418 8.75195 33.1733 10.0479 33.1973 11.3599C33.2524 14.312 34.4736 16.6641 36.8999 18.3359C37.1758 18.5278 37.2466 18.7197 37.1597 19C36.9946 19.5757 36.7974 20.1357 36.624 20.7119C36.5137 21.0801 36.3486 21.1597 35.9624 21C34.6309 20.4321 33.481 19.5918 32.4644 18.5757C30.7393 16.8721 29.1792 14.9917 27.2334 13.52C26.7764 13.1758 26.3193 12.856 25.8467 12.5518C23.8618 10.584 26.1069 8.96777 26.627 8.77588C27.1704 8.57568 26.8159 7.8877 25.0591 7.896C23.3022 7.90381 21.6953 8.50391 19.647 9.30371C19.3477 9.42383 19.0322 9.51172 18.7095 9.58398C16.8501 9.22363 14.9199 9.14355 12.9033 9.37598C9.10596 9.80762 6.07275 11.6396 3.84326 14.7681C1.16455 18.5278 0.53418 22.7998 1.30664 27.2559C2.11768 31.9521 4.46582 35.8398 8.07373 38.8799C11.8159 42.0322 16.1255 43.5762 21.041 43.2803C24.0269 43.104 27.3516 42.6963 31.1016 39.4561C32.0469 39.936 33.0396 40.1279 34.686 40.272C35.9546 40.3921 37.1758 40.208 38.1211 40.0078C39.6021 39.688 39.4995 38.2881 38.9639 38.0322C34.623 35.9678 35.5762 36.8081 34.71 36.1279C36.9155 33.4639 40.2402 30.6958 41.54 21.728C41.6426 21.0161 41.5557 20.5679 41.54 19.9917C41.5322 19.6396 41.6108 19.5039 42.0049 19.4639C43.0923 19.3359 44.1479 19.0317 45.1167 18.4878C47.9292 16.9199 49.064 14.3438 49.3315 11.2559C49.3711 10.7837 49.3237 10.2959 48.8354 10.0479ZM24.3262 37.8398C20.1196 34.4639 18.0791 33.3521 17.2358 33.3999C16.4482 33.4482 16.5898 34.3682 16.7632 34.9678C16.9443 35.5601 17.1812 35.9683 17.5117 36.4878C17.7402 36.832 17.8979 37.3442 17.2832 37.728C15.9282 38.584 13.5728 37.4399 13.4624 37.3838C10.7207 35.7358 8.42822 33.5601 6.81348 30.584C5.25342 27.7197 4.34766 24.6479 4.19775 21.3677C4.1582 20.5757 4.38672 20.2959 5.15869 20.1519C6.17529 19.96 7.22314 19.9199 8.23926 20.0718C12.5327 20.7119 16.1885 22.6719 19.2529 25.7759C21.002 27.5439 22.3252 29.6558 23.6885 31.7202C25.1377 33.9121 26.6978 36 28.6831 37.7119C29.3843 38.312 29.9434 38.7681 30.479 39.104C28.8643 39.2881 26.1699 39.3281 24.3262 37.8398ZM26.3433 24.6001C26.3433 24.248 26.6191 23.9678 26.9658 23.9678C27.0444 23.9678 27.1152 23.9839 27.1782 24.0078C27.2651 24.04 27.3438 24.0879 27.4067 24.1602C27.5171 24.272 27.5801 24.4321 27.5801 24.6001C27.5801 24.9521 27.3042 25.2319 26.9575 25.2319C26.6108 25.2319 26.3433 24.9521 26.3433 24.6001ZM32.6064 27.8799C32.2046 28.0479 31.8027 28.1919 31.4165 28.208C30.8179 28.2397 30.1641 27.9922 29.8096 27.688C29.2583 27.2158 28.8643 26.9521 28.6987 26.1279C28.6279 25.7759 28.6675 25.2319 28.7305 24.9199C28.8721 24.248 28.7144 23.8159 28.2495 23.4238C27.8716 23.104 27.3911 23.0161 26.8633 23.0161C26.666 23.0161 26.4849 22.9277 26.3511 22.856C26.1304 22.7441 25.9492 22.4639 26.1226 22.1201C26.1777 22.0078 26.4458 21.7358 26.5088 21.688C27.2256 21.272 28.0527 21.4077 28.8169 21.7197C29.5259 22.0161 30.0615 22.5601 30.834 23.3281C31.6216 24.2559 31.7632 24.5117 32.2124 25.208C32.5669 25.752 32.8901 26.312 33.1104 26.9521C33.2446 27.3521 33.0713 27.6802 32.6064 27.8799Z';
|
|
111
|
+
|
|
112
|
+
function IconBot({ size = 16, color }) {
|
|
113
|
+
const c = color || 'currentColor';
|
|
114
|
+
return h(
|
|
115
|
+
'svg',
|
|
116
|
+
{
|
|
117
|
+
width: size,
|
|
118
|
+
height: size,
|
|
119
|
+
viewBox: '0 0 50 56',
|
|
120
|
+
fill: 'none',
|
|
121
|
+
style: { display: 'block', flexShrink: 0 },
|
|
122
|
+
'aria-hidden': 'true',
|
|
123
|
+
},
|
|
124
|
+
// 天线(从鲸鱼背部伸出)
|
|
125
|
+
h('path', {
|
|
126
|
+
d: 'M24 12V4',
|
|
127
|
+
stroke: c,
|
|
128
|
+
strokeWidth: 1.8,
|
|
129
|
+
strokeLinecap: 'round',
|
|
130
|
+
fill: 'none',
|
|
131
|
+
}),
|
|
132
|
+
h('circle', { cx: 24, cy: 3.4, r: 2.6, fill: c }),
|
|
133
|
+
// 鲸鱼主体
|
|
134
|
+
h('path', {
|
|
135
|
+
d: WHALE_PATH,
|
|
136
|
+
fill: c,
|
|
137
|
+
fillRule: 'nonzero',
|
|
138
|
+
transform: 'translate(0 4)',
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 播放/演习:三角形 + 虚线方框 */
|
|
144
|
+
function IconDryRun({ size = 14 }) {
|
|
145
|
+
return h(
|
|
146
|
+
'svg',
|
|
147
|
+
{
|
|
148
|
+
width: size,
|
|
149
|
+
height: size,
|
|
150
|
+
viewBox: '0 0 24 24',
|
|
151
|
+
fill: 'none',
|
|
152
|
+
style: { display: 'block' },
|
|
153
|
+
'aria-hidden': 'true',
|
|
154
|
+
},
|
|
155
|
+
h('rect', {
|
|
156
|
+
x: 2.2,
|
|
157
|
+
y: 3.2,
|
|
158
|
+
width: 19.6,
|
|
159
|
+
height: 17.6,
|
|
160
|
+
rx: 3,
|
|
161
|
+
stroke: 'currentColor',
|
|
162
|
+
strokeWidth: 1.6,
|
|
163
|
+
strokeDasharray: '3 2.5',
|
|
164
|
+
}),
|
|
165
|
+
h('path', {
|
|
166
|
+
d: 'M10 8.6l5.4 3.4L10 15.4z',
|
|
167
|
+
fill: 'currentColor',
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 执行:实心闪电 */
|
|
173
|
+
function IconRun({ size = 14 }) {
|
|
174
|
+
return h(
|
|
175
|
+
'svg',
|
|
176
|
+
{
|
|
177
|
+
width: size,
|
|
178
|
+
height: size,
|
|
179
|
+
viewBox: '0 0 24 24',
|
|
180
|
+
fill: 'none',
|
|
181
|
+
style: { display: 'block' },
|
|
182
|
+
'aria-hidden': 'true',
|
|
183
|
+
},
|
|
184
|
+
h('path', {
|
|
185
|
+
d: 'M13.2 2L4.8 13.2h5.4l-1.4 8.8L19.2 10h-5.4z',
|
|
186
|
+
fill: 'currentColor',
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 详情/展开:chevron */
|
|
192
|
+
function IconChevron({ size = 14, open }) {
|
|
193
|
+
return h(
|
|
194
|
+
'svg',
|
|
195
|
+
{
|
|
196
|
+
width: size,
|
|
197
|
+
height: size,
|
|
198
|
+
viewBox: '0 0 24 24',
|
|
199
|
+
fill: 'none',
|
|
200
|
+
style: {
|
|
201
|
+
display: 'block',
|
|
202
|
+
transition: 'transform .18s ease',
|
|
203
|
+
transform: open ? 'rotate(90deg)' : 'none',
|
|
204
|
+
},
|
|
205
|
+
'aria-hidden': 'true',
|
|
206
|
+
},
|
|
207
|
+
h('path', {
|
|
208
|
+
d: 'M9 5l7 7-7 7',
|
|
209
|
+
stroke: 'currentColor',
|
|
210
|
+
strokeWidth: 2,
|
|
211
|
+
strokeLinecap: 'round',
|
|
212
|
+
strokeLinejoin: 'round',
|
|
213
|
+
fill: 'none',
|
|
214
|
+
}),
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** 快照:相机 */
|
|
219
|
+
function IconSnapshot({ size = 14 }) {
|
|
220
|
+
return h(
|
|
221
|
+
'svg',
|
|
222
|
+
{
|
|
223
|
+
width: size,
|
|
224
|
+
height: size,
|
|
225
|
+
viewBox: '0 0 24 24',
|
|
226
|
+
fill: 'none',
|
|
227
|
+
style: { display: 'block' },
|
|
228
|
+
'aria-hidden': 'true',
|
|
229
|
+
},
|
|
230
|
+
h('path', {
|
|
231
|
+
d: 'M4 7.5h3l1.4-2h7.2L17 7.5h3a1.5 1.5 0 011.5 1.5v8a1.5 1.5 0 01-1.5 1.5H4A1.5 1.5 0 012.5 17V9A1.5 1.5 0 014 7.5z',
|
|
232
|
+
stroke: 'currentColor',
|
|
233
|
+
strokeWidth: 1.6,
|
|
234
|
+
strokeLinejoin: 'round',
|
|
235
|
+
}),
|
|
236
|
+
h('circle', {
|
|
237
|
+
cx: 12,
|
|
238
|
+
cy: 12.8,
|
|
239
|
+
r: 3.2,
|
|
240
|
+
stroke: 'currentColor',
|
|
241
|
+
strokeWidth: 1.6,
|
|
242
|
+
}),
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 回滚:逆时针箭头 */
|
|
247
|
+
function IconRollback({ size = 14 }) {
|
|
248
|
+
return h(
|
|
249
|
+
'svg',
|
|
250
|
+
{
|
|
251
|
+
width: size,
|
|
252
|
+
height: size,
|
|
253
|
+
viewBox: '0 0 24 24',
|
|
254
|
+
fill: 'none',
|
|
255
|
+
style: { display: 'block' },
|
|
256
|
+
'aria-hidden': 'true',
|
|
257
|
+
},
|
|
258
|
+
h('path', {
|
|
259
|
+
d: 'M4.5 12a7.5 7.5 0 1 1 2.2 5.3',
|
|
260
|
+
stroke: 'currentColor',
|
|
261
|
+
strokeWidth: 1.7,
|
|
262
|
+
strokeLinecap: 'round',
|
|
263
|
+
fill: 'none',
|
|
264
|
+
}),
|
|
265
|
+
h('path', {
|
|
266
|
+
d: 'M3.6 7.2l1 5 5-1',
|
|
267
|
+
stroke: 'currentColor',
|
|
268
|
+
strokeWidth: 1.7,
|
|
269
|
+
strokeLinecap: 'round',
|
|
270
|
+
strokeLinejoin: 'round',
|
|
271
|
+
fill: 'none',
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** 刷新 */
|
|
277
|
+
function IconRefresh({ size = 14, spinning }) {
|
|
278
|
+
return h(
|
|
279
|
+
'svg',
|
|
280
|
+
{
|
|
281
|
+
width: size,
|
|
282
|
+
height: size,
|
|
283
|
+
viewBox: '0 0 24 24',
|
|
284
|
+
fill: 'none',
|
|
285
|
+
style: {
|
|
286
|
+
display: 'block',
|
|
287
|
+
transition: 'transform .2s ease',
|
|
288
|
+
animation: spinning ? 'dshbot-spin 1s linear infinite' : 'none',
|
|
289
|
+
},
|
|
290
|
+
'aria-hidden': 'true',
|
|
291
|
+
},
|
|
292
|
+
h('path', {
|
|
293
|
+
d: 'M20 12a8 8 0 1 1-2.3-5.6',
|
|
294
|
+
stroke: 'currentColor',
|
|
295
|
+
strokeWidth: 1.8,
|
|
296
|
+
strokeLinecap: 'round',
|
|
297
|
+
fill: 'none',
|
|
298
|
+
}),
|
|
299
|
+
h('path', {
|
|
300
|
+
d: 'M20.4 3.6v4.8h-4.8',
|
|
301
|
+
stroke: 'currentColor',
|
|
302
|
+
strokeWidth: 1.8,
|
|
303
|
+
strokeLinecap: 'round',
|
|
304
|
+
strokeLinejoin: 'round',
|
|
305
|
+
fill: 'none',
|
|
306
|
+
}),
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** 收起 */
|
|
311
|
+
function IconCollapse({ size = 14 }) {
|
|
312
|
+
return h(
|
|
313
|
+
'svg',
|
|
314
|
+
{
|
|
315
|
+
width: size,
|
|
316
|
+
height: size,
|
|
317
|
+
viewBox: '0 0 24 24',
|
|
318
|
+
fill: 'none',
|
|
319
|
+
style: { display: 'block' },
|
|
320
|
+
'aria-hidden': 'true',
|
|
321
|
+
},
|
|
322
|
+
h('path', {
|
|
323
|
+
d: 'M6 14l6-6 6 6',
|
|
324
|
+
stroke: 'currentColor',
|
|
325
|
+
strokeWidth: 1.9,
|
|
326
|
+
strokeLinecap: 'round',
|
|
327
|
+
strokeLinejoin: 'round',
|
|
328
|
+
fill: 'none',
|
|
329
|
+
}),
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// ============================================================
|
|
334
|
+
// 样式注入(一次性)
|
|
335
|
+
// ============================================================
|
|
336
|
+
|
|
337
|
+
const NS = 'dsh-bot';
|
|
338
|
+
let styleInjected = false;
|
|
339
|
+
|
|
340
|
+
function injectStyle() {
|
|
341
|
+
if (styleInjected) return;
|
|
342
|
+
styleInjected = true;
|
|
343
|
+
const el = document.createElement('style');
|
|
344
|
+
el.setAttribute('data-plugin', NS);
|
|
345
|
+
el.textContent = `
|
|
346
|
+
@keyframes dshbot-spin { to { transform: rotate(360deg); } }
|
|
347
|
+
.${NS}-fade { animation: dshbot-fade .18s ease; }
|
|
348
|
+
@keyframes dshbot-fade { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }
|
|
349
|
+
|
|
350
|
+
/* ⭐ 外层宽度约束 —— 与官方 input.dock 插件一致。
|
|
351
|
+
没有这段,面板会撑满整行,比输入框宽很多,极其不协调。
|
|
352
|
+
变量由 DSH 运行时注入,默认值兜底。 */
|
|
353
|
+
.${NS}-dock {
|
|
354
|
+
box-sizing: border-box;
|
|
355
|
+
width: calc(
|
|
356
|
+
100%
|
|
357
|
+
- var(--dsh-composer-side-clearance, 0px)
|
|
358
|
+
- var(--dsh-composer-side-clearance, 0px)
|
|
359
|
+
- var(--dsh-composer-dock-inset, 0px) * 4
|
|
360
|
+
);
|
|
361
|
+
margin: 0 auto 8px;
|
|
362
|
+
}
|
|
363
|
+
.${NS}-dock-inner {
|
|
364
|
+
box-sizing: border-box;
|
|
365
|
+
width: 100%;
|
|
366
|
+
max-width: calc(
|
|
367
|
+
var(--dsh-composer-card-max-width, 748px)
|
|
368
|
+
- var(--dsh-composer-dock-inset, 0px) * 4
|
|
369
|
+
);
|
|
370
|
+
margin: 0 auto;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/* ⭐ 展开态的高度约束 + 滚动
|
|
374
|
+
没有这段,详情区会把面板一路撑高,超出可视区域后
|
|
375
|
+
下面的内容(模型、花费、按钮)根本够不着。
|
|
376
|
+
|
|
377
|
+
结构:头部固定 → 中部滚动 → 底部固定
|
|
378
|
+
这是唯一能同时满足"内容很多"和"不溢出窗口"的布局。 */
|
|
379
|
+
.${NS}-dock-inner.${NS}-expanded {
|
|
380
|
+
display: flex;
|
|
381
|
+
flex-direction: column;
|
|
382
|
+
/* 上限:窗口高度的一半多一点,保证下方的对话框仍然可见 */
|
|
383
|
+
max-height: min(520px, calc(100vh - 220px));
|
|
384
|
+
overflow: hidden;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/* 可滚动主体 */
|
|
388
|
+
.${NS}-scroll {
|
|
389
|
+
flex: 1 1 auto;
|
|
390
|
+
min-height: 0; /* ⭐ flex 子项能收缩的前提,缺了它就不滚 */
|
|
391
|
+
overflow-y: auto;
|
|
392
|
+
overscroll-behavior: contain; /* 滚到底不带动外层页面 */
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/* 细滚动条,跟随主题 */
|
|
396
|
+
.${NS}-scroll::-webkit-scrollbar { width: 8px; }
|
|
397
|
+
.${NS}-scroll::-webkit-scrollbar-track { background: transparent; }
|
|
398
|
+
.${NS}-scroll::-webkit-scrollbar-thumb {
|
|
399
|
+
background: var(--dsw-alias-border-l2);
|
|
400
|
+
border-radius: 999px;
|
|
401
|
+
border: 2px solid transparent;
|
|
402
|
+
background-clip: content-box;
|
|
403
|
+
}
|
|
404
|
+
.${NS}-scroll::-webkit-scrollbar-thumb:hover {
|
|
405
|
+
background: var(--dsw-alias-label-tertiary);
|
|
406
|
+
background-clip: content-box;
|
|
407
|
+
}
|
|
408
|
+
.${NS}-btn {
|
|
409
|
+
font-family: ${FONT};
|
|
410
|
+
font-size: 12px;
|
|
411
|
+
line-height: 18px;
|
|
412
|
+
height: 28px;
|
|
413
|
+
padding: 0 10px;
|
|
414
|
+
border-radius: ${R_SM};
|
|
415
|
+
border: 1px solid transparent;
|
|
416
|
+
display: inline-flex;
|
|
417
|
+
align-items: center;
|
|
418
|
+
gap: 5px;
|
|
419
|
+
cursor: pointer;
|
|
420
|
+
transition: background .15s ease, border-color .15s ease, color .15s ease, opacity .15s ease;
|
|
421
|
+
box-sizing: border-box;
|
|
422
|
+
white-space: nowrap;
|
|
423
|
+
}
|
|
424
|
+
.${NS}-btn:disabled { opacity: .45; cursor: not-allowed; }
|
|
425
|
+
.${NS}-btn:focus-visible {
|
|
426
|
+
outline: 2px solid ${FOCUS};
|
|
427
|
+
outline-offset: 1px;
|
|
428
|
+
}
|
|
429
|
+
.${NS}-btn-primary {
|
|
430
|
+
background: ${BTN_PRIMARY};
|
|
431
|
+
color: ${LABEL_FG};
|
|
432
|
+
}
|
|
433
|
+
.${NS}-btn-primary:hover:not(:disabled) { background: ${BTN_PRIMARY_HOVER}; }
|
|
434
|
+
.${NS}-btn-ghost {
|
|
435
|
+
background: transparent;
|
|
436
|
+
border-color: ${BORDER_L2};
|
|
437
|
+
color: ${LABEL_1};
|
|
438
|
+
}
|
|
439
|
+
.${NS}-btn-ghost:hover:not(:disabled) { background: ${BTN_TOOLBAR_HOVER}; }
|
|
440
|
+
.${NS}-btn-danger {
|
|
441
|
+
background: transparent;
|
|
442
|
+
border-color: transparent;
|
|
443
|
+
color: ${ERR};
|
|
444
|
+
}
|
|
445
|
+
.${NS}-btn-danger:hover:not(:disabled) { background: ${HOVER_DANGER}; }
|
|
446
|
+
.${NS}-row-btn {
|
|
447
|
+
background: transparent;
|
|
448
|
+
border: none;
|
|
449
|
+
color: ${LABEL_2};
|
|
450
|
+
padding: 4px 7px;
|
|
451
|
+
border-radius: ${R_XS};
|
|
452
|
+
cursor: pointer;
|
|
453
|
+
display: inline-flex;
|
|
454
|
+
align-items: center;
|
|
455
|
+
gap: 5px;
|
|
456
|
+
font-family: ${FONT};
|
|
457
|
+
font-size: 12px;
|
|
458
|
+
transition: background .15s ease, color .15s ease;
|
|
459
|
+
}
|
|
460
|
+
.${NS}-row-btn:hover { background: ${HOVER_BG}; color: ${LABEL_1}; }
|
|
461
|
+
.${NS}-row-btn:focus-visible { outline: 2px solid ${FOCUS}; outline-offset: 1px; }
|
|
462
|
+
`;
|
|
463
|
+
document.head.appendChild(el);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// ============================================================
|
|
467
|
+
// 状态上报
|
|
468
|
+
// ============================================================
|
|
469
|
+
|
|
470
|
+
function report(status, detail) {
|
|
471
|
+
try {
|
|
472
|
+
fetch('/_dsh-bot/client-status', {
|
|
473
|
+
method: 'POST',
|
|
474
|
+
headers: { 'content-type': 'application/json' },
|
|
475
|
+
body: JSON.stringify({ status, detail: detail ?? null, at: new Date().toISOString() }),
|
|
476
|
+
}).catch(() => {});
|
|
477
|
+
} catch {
|
|
478
|
+
/* ignore */
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
report('bundle-executing', 'v2 design-system');
|
|
483
|
+
|
|
484
|
+
// ============================================================
|
|
485
|
+
// 插件
|
|
486
|
+
// ============================================================
|
|
487
|
+
|
|
488
|
+
window.__ModuleLoader__.load({
|
|
489
|
+
id: 'dsh-bot',
|
|
490
|
+
factory: (require) => {
|
|
491
|
+
const inject = ['slots'];
|
|
492
|
+
|
|
493
|
+
// ⭐ 在这里给模块级的 React / h 赋值。
|
|
494
|
+
// 图标组件在模块顶层定义,它们闭包引用这两个变量;
|
|
495
|
+
// 必须在 apply 之前就绪,否则渲染时会抛 "h is not a function"。
|
|
496
|
+
React = require('react');
|
|
497
|
+
h = React.createElement;
|
|
498
|
+
|
|
499
|
+
function apply(ctx) {
|
|
500
|
+
try {
|
|
501
|
+
injectStyle();
|
|
502
|
+
|
|
503
|
+
// ---------- 格式化 ----------
|
|
504
|
+
const fmtCny = (n) =>
|
|
505
|
+
Number.isFinite(n) ? (n < 0.01 ? `¥${n.toFixed(4)}` : `¥${n.toFixed(2)}`) : '¥0';
|
|
506
|
+
|
|
507
|
+
// ⭐ 成本三态展示:免费 / 未知 / 精确金额。
|
|
508
|
+
// 「¥0」有两种含义(真免费 vs 算不出来),长得必须不一样。
|
|
509
|
+
const fmtCost = (usage) => {
|
|
510
|
+
if (!usage) return '—';
|
|
511
|
+
if (usage.costKind === 'free') return '免费';
|
|
512
|
+
const c = usage.costCny;
|
|
513
|
+
if (c === undefined || c === null || Number.isNaN(c) || usage.costKind === 'unknown') {
|
|
514
|
+
return usage.costNote ? `未知(${usage.costNote})` : '未知';
|
|
515
|
+
}
|
|
516
|
+
return fmtCny(c);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const fmtAgo = (iso) => {
|
|
520
|
+
if (!iso) return '从未';
|
|
521
|
+
const ms = Date.now() - Date.parse(iso);
|
|
522
|
+
if (!Number.isFinite(ms)) return '—';
|
|
523
|
+
if (ms < 60_000) return '刚刚';
|
|
524
|
+
if (ms < 3_600_000) return `${Math.floor(ms / 60_000)} 分钟前`;
|
|
525
|
+
if (ms < 86_400_000) return `${Math.floor(ms / 3_600_000)} 小时前`;
|
|
526
|
+
return `${Math.floor(ms / 86_400_000)} 天前`;
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
const statusColor = (s) =>
|
|
530
|
+
s === 'success' ? OK : s === 'failed' ? ERR : s === 'partial' ? WARN : LABEL_3;
|
|
531
|
+
|
|
532
|
+
const statusText = (s) =>
|
|
533
|
+
s === 'success'
|
|
534
|
+
? '成功'
|
|
535
|
+
: s === 'failed'
|
|
536
|
+
? '失败'
|
|
537
|
+
: s === 'partial'
|
|
538
|
+
? '部分'
|
|
539
|
+
: s === 'budget_exceeded'
|
|
540
|
+
? '超预算'
|
|
541
|
+
: s || '—';
|
|
542
|
+
|
|
543
|
+
// ---------- 目标(「守得住」) ----------
|
|
544
|
+
const goalStatusText = (s) =>
|
|
545
|
+
({
|
|
546
|
+
proposed: '待接受',
|
|
547
|
+
active: '进行中',
|
|
548
|
+
pending_confirmation: '待你确认',
|
|
549
|
+
achieved: '已达成',
|
|
550
|
+
blocked: '卡住了',
|
|
551
|
+
abandoned: '已放弃',
|
|
552
|
+
})[s] || s || '';
|
|
553
|
+
|
|
554
|
+
const goalStatusColor = (s) =>
|
|
555
|
+
s === 'active'
|
|
556
|
+
? BRAND
|
|
557
|
+
: s === 'pending_confirmation'
|
|
558
|
+
? WARN
|
|
559
|
+
: s === 'blocked'
|
|
560
|
+
? ERR
|
|
561
|
+
: s === 'achieved'
|
|
562
|
+
? OK
|
|
563
|
+
: LABEL_3;
|
|
564
|
+
|
|
565
|
+
const goalTypeText = (t) => (t === 'standing' ? '常驻守护' : '一次性');
|
|
566
|
+
|
|
567
|
+
// ---------- 通用按钮 ----------
|
|
568
|
+
function Btn({ children, onClick, kind = 'ghost', disabled, title, icon }) {
|
|
569
|
+
return h(
|
|
570
|
+
'button',
|
|
571
|
+
{
|
|
572
|
+
className: `${NS}-btn ${NS}-btn-${kind}`,
|
|
573
|
+
onClick,
|
|
574
|
+
disabled,
|
|
575
|
+
title,
|
|
576
|
+
type: 'button',
|
|
577
|
+
},
|
|
578
|
+
icon || null,
|
|
579
|
+
children,
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// ============================================================
|
|
584
|
+
// 内嵌表单
|
|
585
|
+
//
|
|
586
|
+
// ⚠️ 为什么不用 window.prompt / window.confirm:
|
|
587
|
+
// DSH 跑在 Electron 的渲染进程里,prompt() 被直接禁用
|
|
588
|
+
// (报 "prompt() is not supported"),confirm() 恒返回 false。
|
|
589
|
+
// 用它们的结果是按钮"点了没反应"或直接红框报错。
|
|
590
|
+
// 所以所有输入都走真正的 DOM 表单。
|
|
591
|
+
// ============================================================
|
|
592
|
+
|
|
593
|
+
/** 下拉框样式(与 Input 一致,避免观感割裂) */
|
|
594
|
+
const SELECT_STYLE = {
|
|
595
|
+
flex: 1,
|
|
596
|
+
boxSizing: 'border-box',
|
|
597
|
+
fontFamily: FONT,
|
|
598
|
+
fontSize: 12,
|
|
599
|
+
color: LABEL_1,
|
|
600
|
+
background: BG_L1,
|
|
601
|
+
border: `1px solid ${BORDER_L2}`,
|
|
602
|
+
borderRadius: R_SM,
|
|
603
|
+
padding: '5px 8px',
|
|
604
|
+
outline: 'none',
|
|
605
|
+
cursor: 'pointer',
|
|
606
|
+
};
|
|
607
|
+
|
|
608
|
+
function Input({ value, onChange, placeholder, multiline, onEnter, autoFocus }) {
|
|
609
|
+
const base = {
|
|
610
|
+
width: '100%',
|
|
611
|
+
boxSizing: 'border-box',
|
|
612
|
+
fontFamily: FONT,
|
|
613
|
+
fontSize: 12,
|
|
614
|
+
lineHeight: 1.6,
|
|
615
|
+
color: LABEL_1,
|
|
616
|
+
background: BG_L1,
|
|
617
|
+
border: `1px solid ${BORDER_L2}`,
|
|
618
|
+
borderRadius: R_SM,
|
|
619
|
+
padding: '5px 8px',
|
|
620
|
+
outline: 'none',
|
|
621
|
+
resize: 'none',
|
|
622
|
+
}; if (multiline) {
|
|
623
|
+
return h('textarea', {
|
|
624
|
+
style: { ...base, minHeight: 48 },
|
|
625
|
+
value,
|
|
626
|
+
placeholder,
|
|
627
|
+
autoFocus,
|
|
628
|
+
onChange: (e) => onChange(e.target.value),
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
return h('input', {
|
|
632
|
+
style: base,
|
|
633
|
+
type: 'text',
|
|
634
|
+
value,
|
|
635
|
+
placeholder,
|
|
636
|
+
autoFocus,
|
|
637
|
+
onChange: (e) => onChange(e.target.value),
|
|
638
|
+
onKeyDown: (e) => {
|
|
639
|
+
if (e.key === 'Enter' && onEnter) onEnter();
|
|
640
|
+
},
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* 表单容器。
|
|
646
|
+
*
|
|
647
|
+
* 支持三种形态:
|
|
648
|
+
* · goal —— 设目标(描述 + 类型)
|
|
649
|
+
* · reject —— 驳回意见
|
|
650
|
+
* · createBot —— 建 Bot(名字 + 职责 + 可选目标)
|
|
651
|
+
*/
|
|
652
|
+
function FormPanel({ form, onClose, onSubmit, busy }) {
|
|
653
|
+
const [name, setName] = React.useState('');
|
|
654
|
+
const [role, setRole] = React.useState('');
|
|
655
|
+
const [goal, setGoal] = React.useState('');
|
|
656
|
+
const [note, setNote] = React.useState('');
|
|
657
|
+
const [desc, setDesc] = React.useState('');
|
|
658
|
+
const [type, setType] = React.useState('one-shot');
|
|
659
|
+
// ⭐ 执行节奏:这一栏决定「持续任务」会不会真的自动跑
|
|
660
|
+
const [cadence, setCadence] = React.useState('');
|
|
661
|
+
const [customTime, setCustomTime] = React.useState('09:00');
|
|
662
|
+
// 模型编辑用
|
|
663
|
+
const [provider, setProvider] = React.useState(form.provider ?? '');
|
|
664
|
+
const [modelName, setModelName] = React.useState(form.model ?? '');
|
|
665
|
+
// 真实可用的 provider / model 列表(来自 /_dsh-bot/models)
|
|
666
|
+
const [providerList, setProviderList] = React.useState([]);
|
|
667
|
+
const [modelsLoading, setModelsLoading] = React.useState(true);
|
|
668
|
+
|
|
669
|
+
React.useEffect(() => {
|
|
670
|
+
if (form.kind !== 'model') return;
|
|
671
|
+
let alive = true;
|
|
672
|
+
setModelsLoading(true);
|
|
673
|
+
(async () => {
|
|
674
|
+
try {
|
|
675
|
+
const r = await fetch('/_dsh-bot/models', {
|
|
676
|
+
headers: { accept: 'application/json' },
|
|
677
|
+
});
|
|
678
|
+
const j = await r.json();
|
|
679
|
+
if (alive && j?.ok) setProviderList(j.value?.available ?? []);
|
|
680
|
+
} catch {
|
|
681
|
+
/* 拿不到就退化成手填 */
|
|
682
|
+
} finally {
|
|
683
|
+
if (alive) setModelsLoading(false);
|
|
684
|
+
}
|
|
685
|
+
})();
|
|
686
|
+
return () => {
|
|
687
|
+
alive = false;
|
|
688
|
+
};
|
|
689
|
+
}, [form.kind]);
|
|
690
|
+
|
|
691
|
+
const availableProviders = providerList.map((x) => x?.provider).filter(Boolean);
|
|
692
|
+
const modelsForProvider =
|
|
693
|
+
providerList.find((x) => x?.provider === provider)?.models ?? [];
|
|
694
|
+
|
|
695
|
+
/** 把选择器状态收敛成最终的 cron */
|
|
696
|
+
const resolvedCadence = () => {
|
|
697
|
+
if (!cadence) return undefined;
|
|
698
|
+
if (cadence === 'custom') return timeToCron(customTime);
|
|
699
|
+
return cadence;
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
const submit = () => {
|
|
703
|
+
if (form.kind === 'goal') {
|
|
704
|
+
if (!desc.trim()) return;
|
|
705
|
+
onSubmit('goalSet', {
|
|
706
|
+
description: desc.trim(),
|
|
707
|
+
goalType: type,
|
|
708
|
+
cadence: resolvedCadence(),
|
|
709
|
+
});
|
|
710
|
+
} else if (form.kind === 'unblock') {
|
|
711
|
+
onSubmit('goalUnblock', { note: note.trim() });
|
|
712
|
+
} else if (form.kind === 'giveup') {
|
|
713
|
+
onSubmit('goalGiveup', { note: note.trim() });
|
|
714
|
+
} else if (form.kind === 'reject') {
|
|
715
|
+
onSubmit('goalReject', { note: note.trim() || '还没做完' });
|
|
716
|
+
} else if (form.kind === 'model') {
|
|
717
|
+
if (!provider.trim() || !modelName.trim()) return;
|
|
718
|
+
onSubmit('botModel', {
|
|
719
|
+
provider: provider.trim(),
|
|
720
|
+
model: modelName.trim(),
|
|
721
|
+
});
|
|
722
|
+
} else if (form.kind === 'createBot') {
|
|
723
|
+
if (!name.trim() || !role.trim()) return;
|
|
724
|
+
onSubmit('botCreate', {
|
|
725
|
+
name: name.trim(),
|
|
726
|
+
role: role.trim(),
|
|
727
|
+
goal: goal.trim() || undefined,
|
|
728
|
+
goalType: type,
|
|
729
|
+
cadence: resolvedCadence(),
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
const title =
|
|
735
|
+
form.kind === 'goal'
|
|
736
|
+
? `给「${form.botName}」设目标`
|
|
737
|
+
: form.kind === 'unblock'
|
|
738
|
+
? '你是怎么解决的?(可选,会记进目标进展)'
|
|
739
|
+
: form.kind === 'giveup'
|
|
740
|
+
? '为什么放弃?(会记进目标史)'
|
|
741
|
+
: form.kind === 'reject'
|
|
742
|
+
? '驳回这个目标的理由'
|
|
743
|
+
: form.kind === 'model'
|
|
744
|
+
? `改「${form.botName}」用的模型`
|
|
745
|
+
: '新建一个 Bot';
|
|
746
|
+
|
|
747
|
+
return h(
|
|
748
|
+
'div',
|
|
749
|
+
{
|
|
750
|
+
style: {
|
|
751
|
+
margin: '8px 12px',
|
|
752
|
+
padding: '10px',
|
|
753
|
+
borderRadius: R_SM,
|
|
754
|
+
background: BG_L2,
|
|
755
|
+
border: `1px solid ${BORDER_L2}`,
|
|
756
|
+
},
|
|
757
|
+
},
|
|
758
|
+
h(
|
|
759
|
+
'div',
|
|
760
|
+
{
|
|
761
|
+
style: {
|
|
762
|
+
fontSize: 11,
|
|
763
|
+
fontWeight: 600,
|
|
764
|
+
color: LABEL_1,
|
|
765
|
+
marginBottom: 8,
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
title,
|
|
769
|
+
),
|
|
770
|
+
|
|
771
|
+
form.kind === 'createBot'
|
|
772
|
+
? h(
|
|
773
|
+
'div',
|
|
774
|
+
{ style: { display: 'flex', gap: 6, marginBottom: 6 } },
|
|
775
|
+
h('div', { style: { flex: 1 } },
|
|
776
|
+
h(Input, {
|
|
777
|
+
value: name,
|
|
778
|
+
onChange: setName,
|
|
779
|
+
placeholder: '名字,如「文件管家」',
|
|
780
|
+
autoFocus: true,
|
|
781
|
+
}),
|
|
782
|
+
),
|
|
783
|
+
h('div', { style: { flex: 2 } },
|
|
784
|
+
h(Input, {
|
|
785
|
+
value: role,
|
|
786
|
+
onChange: setRole,
|
|
787
|
+
placeholder: '职责,如「整理下载文件夹」',
|
|
788
|
+
}),
|
|
789
|
+
),
|
|
790
|
+
)
|
|
791
|
+
: null,
|
|
792
|
+
|
|
793
|
+
form.kind === 'createBot' || form.kind === 'goal'
|
|
794
|
+
? h(Input, {
|
|
795
|
+
value: form.kind === 'goal' ? desc : goal,
|
|
796
|
+
onChange: form.kind === 'goal' ? setDesc : setGoal,
|
|
797
|
+
placeholder:
|
|
798
|
+
form.kind === 'goal'
|
|
799
|
+
? '目标,如「整理下载文件夹」'
|
|
800
|
+
: '目标(可留空,稍后再设)',
|
|
801
|
+
onEnter: submit,
|
|
802
|
+
autoFocus: form.kind === 'goal',
|
|
803
|
+
})
|
|
804
|
+
: null,
|
|
805
|
+
|
|
806
|
+
form.kind === 'reject' ||
|
|
807
|
+
form.kind === 'unblock' ||
|
|
808
|
+
form.kind === 'giveup'
|
|
809
|
+
? h(Input, {
|
|
810
|
+
value: note,
|
|
811
|
+
onChange: setNote,
|
|
812
|
+
placeholder:
|
|
813
|
+
form.kind === 'reject'
|
|
814
|
+
? '哪里还没做好?(Bot 会看到这条)'
|
|
815
|
+
: form.kind === 'unblock'
|
|
816
|
+
? '如:已重配 /admin 鉴权'
|
|
817
|
+
: '如:这个站点已经不维护了',
|
|
818
|
+
onEnter: submit,
|
|
819
|
+
autoFocus: true,
|
|
820
|
+
})
|
|
821
|
+
: null,
|
|
822
|
+
|
|
823
|
+
// 模型编辑:provider + model
|
|
824
|
+
//
|
|
825
|
+
// 优先用下拉(从 /models 拿真实可用的 provider),
|
|
826
|
+
// 拿不到才退化成手填。手输 provider 名极易打错,
|
|
827
|
+
// 而打错的后果是执行时拿不到模型(EMPTY_OUTPUT)。
|
|
828
|
+
form.kind === 'model'
|
|
829
|
+
? h(
|
|
830
|
+
'div',
|
|
831
|
+
null,
|
|
832
|
+
h(
|
|
833
|
+
'div',
|
|
834
|
+
{ style: { display: 'flex', gap: 6, alignItems: 'center' } },
|
|
835
|
+
availableProviders && availableProviders.length > 0
|
|
836
|
+
? h(
|
|
837
|
+
'select',
|
|
838
|
+
{
|
|
839
|
+
style: SELECT_STYLE,
|
|
840
|
+
value: provider,
|
|
841
|
+
onChange: (e) => setProvider(e.target.value),
|
|
842
|
+
},
|
|
843
|
+
h('option', { value: '' }, '选择 provider…'),
|
|
844
|
+
...availableProviders.map((p) =>
|
|
845
|
+
h('option', { key: p, value: p }, p),
|
|
846
|
+
),
|
|
847
|
+
)
|
|
848
|
+
: h(
|
|
849
|
+
'div',
|
|
850
|
+
{ style: { flex: 1 } },
|
|
851
|
+
h(Input, {
|
|
852
|
+
value: provider,
|
|
853
|
+
onChange: setProvider,
|
|
854
|
+
placeholder: 'provider,如 trae',
|
|
855
|
+
autoFocus: true,
|
|
856
|
+
}),
|
|
857
|
+
),
|
|
858
|
+
|
|
859
|
+
modelsForProvider && modelsForProvider.length > 0
|
|
860
|
+
? h(
|
|
861
|
+
'select',
|
|
862
|
+
{
|
|
863
|
+
style: { ...SELECT_STYLE, flex: 1.3 },
|
|
864
|
+
value: modelName,
|
|
865
|
+
onChange: (e) => setModelName(e.target.value),
|
|
866
|
+
},
|
|
867
|
+
h('option', { value: '' }, '选择模型…'),
|
|
868
|
+
...modelsForProvider.map((mo) =>
|
|
869
|
+
h('option', { key: mo, value: mo }, mo),
|
|
870
|
+
),
|
|
871
|
+
)
|
|
872
|
+
: h(
|
|
873
|
+
'div',
|
|
874
|
+
{ style: { flex: 1.3 } },
|
|
875
|
+
h(Input, {
|
|
876
|
+
value: modelName,
|
|
877
|
+
onChange: setModelName,
|
|
878
|
+
placeholder: 'model,如 deepseek-v4.1-flash',
|
|
879
|
+
onEnter: submit,
|
|
880
|
+
}),
|
|
881
|
+
),
|
|
882
|
+
),
|
|
883
|
+
h(
|
|
884
|
+
'div',
|
|
885
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 5 } },
|
|
886
|
+
modelsLoading
|
|
887
|
+
? '正在读取这个环境可用的模型…'
|
|
888
|
+
: availableProviders.length === 0
|
|
889
|
+
? '读取不到模型列表,请手填。'
|
|
890
|
+
: modelsForProvider.length === 0
|
|
891
|
+
? `provider「${provider || '未选'}」没有报告可用模型,请手填 model。`
|
|
892
|
+
: '下拉里是这个环境真实可用的选项。',
|
|
893
|
+
),
|
|
894
|
+
)
|
|
895
|
+
: null,
|
|
896
|
+
|
|
897
|
+
// 类型选择 + 执行节奏
|
|
898
|
+
form.kind === 'goal' || form.kind === 'createBot'
|
|
899
|
+
? h(
|
|
900
|
+
'div',
|
|
901
|
+
{ style: { marginTop: 8 } },
|
|
902
|
+
h(
|
|
903
|
+
'div',
|
|
904
|
+
{
|
|
905
|
+
style: {
|
|
906
|
+
display: 'flex',
|
|
907
|
+
alignItems: 'center',
|
|
908
|
+
gap: 10,
|
|
909
|
+
fontSize: 11,
|
|
910
|
+
color: LABEL_2,
|
|
911
|
+
},
|
|
912
|
+
},
|
|
913
|
+
h('span', { style: { color: LABEL_3 } }, '类型'),
|
|
914
|
+
...[
|
|
915
|
+
['one-shot', '一次性(做完就归档)'],
|
|
916
|
+
['standing', '常驻守护(一直守着)'],
|
|
917
|
+
].map(([v, label]) =>
|
|
918
|
+
h(
|
|
919
|
+
'label',
|
|
920
|
+
{
|
|
921
|
+
key: v,
|
|
922
|
+
style: {
|
|
923
|
+
display: 'inline-flex',
|
|
924
|
+
alignItems: 'center',
|
|
925
|
+
gap: 4,
|
|
926
|
+
cursor: 'pointer',
|
|
927
|
+
color: type === v ? LABEL_1 : LABEL_2,
|
|
928
|
+
},
|
|
929
|
+
},
|
|
930
|
+
h('input', {
|
|
931
|
+
type: 'radio',
|
|
932
|
+
checked: type === v,
|
|
933
|
+
onChange: () => setType(v),
|
|
934
|
+
style: { margin: 0, cursor: 'pointer' },
|
|
935
|
+
}),
|
|
936
|
+
label,
|
|
937
|
+
),
|
|
938
|
+
),
|
|
939
|
+
),
|
|
940
|
+
|
|
941
|
+
// ⭐ 执行节奏:改成选择器。
|
|
942
|
+
//
|
|
943
|
+
// 之前是自由文本「每 4 小时」,靠 parseCadence 猜。
|
|
944
|
+
// 猜不中就静默变成"没有节奏"——用户以为设好了,
|
|
945
|
+
// 面板却一直警告它会自己停。这种"猜"不该由用户承担。
|
|
946
|
+
// 现在直接给常用选项 + 精确时间,所见即所得。
|
|
947
|
+
h(
|
|
948
|
+
'div',
|
|
949
|
+
{ style: { marginTop: 8 } },
|
|
950
|
+
h(
|
|
951
|
+
'div',
|
|
952
|
+
{ style: { fontSize: 11, color: LABEL_3, marginBottom: 5 } },
|
|
953
|
+
'多久自动跑一次',
|
|
954
|
+
),
|
|
955
|
+
h(
|
|
956
|
+
'div',
|
|
957
|
+
{ style: { display: 'flex', gap: 5, flexWrap: 'wrap' } },
|
|
958
|
+
...CADENCE_PRESETS.map((p) =>
|
|
959
|
+
h(
|
|
960
|
+
'button',
|
|
961
|
+
{
|
|
962
|
+
key: p.value,
|
|
963
|
+
type: 'button',
|
|
964
|
+
onClick: () => setCadence(p.value),
|
|
965
|
+
style: {
|
|
966
|
+
fontFamily: FONT,
|
|
967
|
+
fontSize: 11,
|
|
968
|
+
lineHeight: '18px',
|
|
969
|
+
padding: '3px 9px',
|
|
970
|
+
borderRadius: R_SM,
|
|
971
|
+
cursor: 'pointer',
|
|
972
|
+
background:
|
|
973
|
+
cadence === p.value ? BRAND : BG_L1,
|
|
974
|
+
color:
|
|
975
|
+
cadence === p.value ? LABEL_FG : LABEL_2,
|
|
976
|
+
border: `1px solid ${
|
|
977
|
+
cadence === p.value ? BRAND : BORDER_L2
|
|
978
|
+
}`,
|
|
979
|
+
transition: 'all .12s ease',
|
|
980
|
+
},
|
|
981
|
+
},
|
|
982
|
+
p.label,
|
|
983
|
+
),
|
|
984
|
+
),
|
|
985
|
+
),
|
|
986
|
+
// 自定义时间(时:分)
|
|
987
|
+
cadence === 'custom'
|
|
988
|
+
? h(
|
|
989
|
+
'div',
|
|
990
|
+
{
|
|
991
|
+
style: {
|
|
992
|
+
display: 'flex',
|
|
993
|
+
alignItems: 'center',
|
|
994
|
+
gap: 6,
|
|
995
|
+
marginTop: 6,
|
|
996
|
+
fontSize: 11,
|
|
997
|
+
color: LABEL_2,
|
|
998
|
+
},
|
|
999
|
+
},
|
|
1000
|
+
h('span', { style: { color: LABEL_3 } }, '每天'),
|
|
1001
|
+
h(Input, {
|
|
1002
|
+
value: customTime,
|
|
1003
|
+
onChange: setCustomTime,
|
|
1004
|
+
placeholder: 'HH:MM',
|
|
1005
|
+
}),
|
|
1006
|
+
h('span', { style: { color: LABEL_3 } }, '执行'),
|
|
1007
|
+
)
|
|
1008
|
+
: null,
|
|
1009
|
+
h(
|
|
1010
|
+
'div',
|
|
1011
|
+
{
|
|
1012
|
+
style: {
|
|
1013
|
+
fontSize: 10,
|
|
1014
|
+
marginTop: 5,
|
|
1015
|
+
color: cadence ? LABEL_3 : WARN,
|
|
1016
|
+
},
|
|
1017
|
+
},
|
|
1018
|
+
cadence
|
|
1019
|
+
? 'Bot 会按这个节奏自己醒来干活,不用你守着'
|
|
1020
|
+
: '⚠️ 不选的话它不会自己跑 —— 只有你点「执行」才动一次',
|
|
1021
|
+
),
|
|
1022
|
+
),
|
|
1023
|
+
)
|
|
1024
|
+
: null,
|
|
1025
|
+
|
|
1026
|
+
h(
|
|
1027
|
+
'div',
|
|
1028
|
+
{
|
|
1029
|
+
style: {
|
|
1030
|
+
display: 'flex',
|
|
1031
|
+
justifyContent: 'flex-end',
|
|
1032
|
+
gap: 6,
|
|
1033
|
+
marginTop: 10,
|
|
1034
|
+
},
|
|
1035
|
+
},
|
|
1036
|
+
h(Btn, { onClick: onClose, disabled: busy }, '取消'),
|
|
1037
|
+
h(
|
|
1038
|
+
Btn,
|
|
1039
|
+
{ kind: 'primary', onClick: submit, disabled: busy },
|
|
1040
|
+
form.kind === 'reject' ? '驳回' : '确定',
|
|
1041
|
+
),
|
|
1042
|
+
),
|
|
1043
|
+
);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* 需要二次确认的动作按钮。
|
|
1048
|
+
*
|
|
1049
|
+
* 取代 window.confirm:第一次点击进入 armed 态(按钮变红、文案变确认),
|
|
1050
|
+
* 3 秒内再点一次才真正执行。
|
|
1051
|
+
*/
|
|
1052
|
+
function ConfirmBtn({ label, confirmLabel, icon, kind, onConfirm, disabled }) {
|
|
1053
|
+
const [armed, setArmed] = React.useState(false);
|
|
1054
|
+
|
|
1055
|
+
React.useEffect(() => {
|
|
1056
|
+
if (!armed) return;
|
|
1057
|
+
const t = setTimeout(() => setArmed(false), 3000);
|
|
1058
|
+
return () => clearTimeout(t);
|
|
1059
|
+
}, [armed]);
|
|
1060
|
+
|
|
1061
|
+
return h(
|
|
1062
|
+
Btn,
|
|
1063
|
+
{
|
|
1064
|
+
kind: armed ? 'danger' : kind,
|
|
1065
|
+
disabled,
|
|
1066
|
+
icon: armed ? null : icon,
|
|
1067
|
+
onClick: () => {
|
|
1068
|
+
if (armed) {
|
|
1069
|
+
setArmed(false);
|
|
1070
|
+
onConfirm();
|
|
1071
|
+
} else {
|
|
1072
|
+
setArmed(true);
|
|
1073
|
+
}
|
|
1074
|
+
},
|
|
1075
|
+
},
|
|
1076
|
+
armed ? confirmLabel : label,
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
// ---------- 预算条 ----------
|
|
1081
|
+
function Meter({ value, max }) {
|
|
1082
|
+
const pct = max > 0 ? Math.min(100, (value / max) * 100) : 0;
|
|
1083
|
+
const color = pct > 80 ? ERR : pct > 50 ? WARN : OK;
|
|
1084
|
+
return h(
|
|
1085
|
+
'div',
|
|
1086
|
+
{
|
|
1087
|
+
style: {
|
|
1088
|
+
height: 4,
|
|
1089
|
+
width: '100%',
|
|
1090
|
+
borderRadius: 999,
|
|
1091
|
+
background: BG_L3,
|
|
1092
|
+
overflow: 'hidden',
|
|
1093
|
+
marginTop: 6,
|
|
1094
|
+
},
|
|
1095
|
+
},
|
|
1096
|
+
h('div', {
|
|
1097
|
+
style: {
|
|
1098
|
+
height: '100%',
|
|
1099
|
+
width: `${pct}%`,
|
|
1100
|
+
background: color,
|
|
1101
|
+
borderRadius: 999,
|
|
1102
|
+
transition: 'width .35s cubic-bezier(.4,0,.2,1)',
|
|
1103
|
+
},
|
|
1104
|
+
}),
|
|
1105
|
+
);
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// ---------- 状态徽标 ----------
|
|
1109
|
+
function Badge({ children, tone = 'neutral' }) {
|
|
1110
|
+
const map = {
|
|
1111
|
+
ok: { bg: OK_TER, fg: OK },
|
|
1112
|
+
warn: { bg: WARN_TER, fg: WARN },
|
|
1113
|
+
err: { bg: 'transparent', fg: ERR },
|
|
1114
|
+
neutral: { bg: BG_L3, fg: LABEL_2 },
|
|
1115
|
+
brand: { bg: BG_L3, fg: BRAND },
|
|
1116
|
+
};
|
|
1117
|
+
const t = map[tone] || map.neutral;
|
|
1118
|
+
return h(
|
|
1119
|
+
'span',
|
|
1120
|
+
{
|
|
1121
|
+
style: {
|
|
1122
|
+
fontSize: 11,
|
|
1123
|
+
lineHeight: '16px',
|
|
1124
|
+
padding: '1px 7px',
|
|
1125
|
+
borderRadius: 999,
|
|
1126
|
+
background: t.bg,
|
|
1127
|
+
color: t.fg,
|
|
1128
|
+
fontFamily: FONT,
|
|
1129
|
+
whiteSpace: 'nowrap',
|
|
1130
|
+
},
|
|
1131
|
+
},
|
|
1132
|
+
children,
|
|
1133
|
+
);
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// ============================================================
|
|
1137
|
+
// 设置页
|
|
1138
|
+
//
|
|
1139
|
+
// DSH 没有通用的 settings slot(官方插件都挂在对话区),
|
|
1140
|
+
// 所以做成面板内的一个独立视图,从头部齿轮进入。
|
|
1141
|
+
//
|
|
1142
|
+
// 分组遵循「改这个是为了什么」而不是「技术上属于哪类」:
|
|
1143
|
+
// 通知(什么时候打扰我)/ 模型(用啥干活)/ 预算(能花多少)
|
|
1144
|
+
// 目标(怎么推进)/ 高级(省钱与安全)/ 数据(清记录)
|
|
1145
|
+
// ============================================================
|
|
1146
|
+
|
|
1147
|
+
/** 一行开关 */
|
|
1148
|
+
function ToggleRow({ label, hint, value, onChange }) {
|
|
1149
|
+
return h(
|
|
1150
|
+
'div',
|
|
1151
|
+
{
|
|
1152
|
+
style: {
|
|
1153
|
+
display: 'flex',
|
|
1154
|
+
alignItems: 'center',
|
|
1155
|
+
gap: 10,
|
|
1156
|
+
padding: '6px 0',
|
|
1157
|
+
},
|
|
1158
|
+
},
|
|
1159
|
+
h(
|
|
1160
|
+
'div',
|
|
1161
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
1162
|
+
h('div', { style: { fontSize: 12, color: LABEL_1 } }, label),
|
|
1163
|
+
hint
|
|
1164
|
+
? h(
|
|
1165
|
+
'div',
|
|
1166
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 2 } },
|
|
1167
|
+
hint,
|
|
1168
|
+
)
|
|
1169
|
+
: null,
|
|
1170
|
+
),
|
|
1171
|
+
h(
|
|
1172
|
+
'button',
|
|
1173
|
+
{
|
|
1174
|
+
type: 'button',
|
|
1175
|
+
onClick: () => onChange(!value),
|
|
1176
|
+
style: {
|
|
1177
|
+
width: 36,
|
|
1178
|
+
height: 20,
|
|
1179
|
+
borderRadius: 999,
|
|
1180
|
+
border: 'none',
|
|
1181
|
+
cursor: 'pointer',
|
|
1182
|
+
background: value ? BRAND : BORDER_L2,
|
|
1183
|
+
position: 'relative',
|
|
1184
|
+
flexShrink: 0,
|
|
1185
|
+
transition: 'background .15s ease',
|
|
1186
|
+
},
|
|
1187
|
+
'aria-pressed': value,
|
|
1188
|
+
},
|
|
1189
|
+
h('span', {
|
|
1190
|
+
style: {
|
|
1191
|
+
position: 'absolute',
|
|
1192
|
+
top: 2,
|
|
1193
|
+
left: value ? 18 : 2,
|
|
1194
|
+
width: 16,
|
|
1195
|
+
height: 16,
|
|
1196
|
+
borderRadius: 999,
|
|
1197
|
+
background: LABEL_FG,
|
|
1198
|
+
transition: 'left .15s ease',
|
|
1199
|
+
},
|
|
1200
|
+
}),
|
|
1201
|
+
),
|
|
1202
|
+
);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/** 一行数字/文本输入 */
|
|
1206
|
+
function FieldRow({ label, hint, value, onChange, placeholder, width }) {
|
|
1207
|
+
return h(
|
|
1208
|
+
'div',
|
|
1209
|
+
{
|
|
1210
|
+
style: {
|
|
1211
|
+
display: 'flex',
|
|
1212
|
+
alignItems: 'center',
|
|
1213
|
+
gap: 10,
|
|
1214
|
+
padding: '6px 0',
|
|
1215
|
+
},
|
|
1216
|
+
},
|
|
1217
|
+
h(
|
|
1218
|
+
'div',
|
|
1219
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
1220
|
+
h('div', { style: { fontSize: 12, color: LABEL_1 } }, label),
|
|
1221
|
+
hint
|
|
1222
|
+
? h(
|
|
1223
|
+
'div',
|
|
1224
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 2 } },
|
|
1225
|
+
hint,
|
|
1226
|
+
)
|
|
1227
|
+
: null,
|
|
1228
|
+
),
|
|
1229
|
+
h(
|
|
1230
|
+
'div',
|
|
1231
|
+
{ style: { width: width ?? 120, flexShrink: 0 } },
|
|
1232
|
+
h(Input, { value, onChange, placeholder }),
|
|
1233
|
+
),
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
/**
|
|
1238
|
+
* 最近运行列表(设置页「最近运行」组)。
|
|
1239
|
+
*
|
|
1240
|
+
* 花费透明是硬要求:每一轮必须能回答「谁在跑、花了多少、
|
|
1241
|
+
* 钱花在哪」。三态展示沿用 fmtCost 的契约:
|
|
1242
|
+
* exact=付费(显示金额)/ free=免费 / unknown=无价目数据。
|
|
1243
|
+
*
|
|
1244
|
+
* 两种形态(用户可切换,偏好存 localStorage):
|
|
1245
|
+
* compact = 固定高度内部滚动(约 6 条记录的高度)
|
|
1246
|
+
* paged = 分页器(每页 5 条,页面干净)
|
|
1247
|
+
*/
|
|
1248
|
+
const RUNS_MODE_KEY = 'dsh-bot:runsMode';
|
|
1249
|
+
const RUNS_PAGE_SIZE = 5;
|
|
1250
|
+
// compact 形态固定高度:行高约 44px,6 行 + 边距
|
|
1251
|
+
const RUNS_COMPACT_HEIGHT = 6 * 44 + 8;
|
|
1252
|
+
|
|
1253
|
+
function RunsSection({ busy }) {
|
|
1254
|
+
const [data, setData] = React.useState(null);
|
|
1255
|
+
const [err, setErr] = React.useState('');
|
|
1256
|
+
const [openRun, setOpenRun] = React.useState(null);
|
|
1257
|
+
const [mode, setMode] = React.useState(() => {
|
|
1258
|
+
try {
|
|
1259
|
+
const v = localStorage.getItem(RUNS_MODE_KEY);
|
|
1260
|
+
return v === 'compact' || v === 'paged' ? v : 'paged';
|
|
1261
|
+
} catch {
|
|
1262
|
+
return 'paged';
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
const [page, setPage] = React.useState(0);
|
|
1266
|
+
const switchMode = (m) => {
|
|
1267
|
+
setMode(m);
|
|
1268
|
+
setPage(0);
|
|
1269
|
+
try { localStorage.setItem(RUNS_MODE_KEY, m); } catch {}
|
|
1270
|
+
};
|
|
1271
|
+
|
|
1272
|
+
const load = React.useCallback(async () => {
|
|
1273
|
+
try {
|
|
1274
|
+
const o = await fetch('/_dsh-bot/overview', { headers: { accept: 'application/json' } });
|
|
1275
|
+
const oj = await o.json();
|
|
1276
|
+
if (!oj?.ok) throw new Error(oj?.message || '读取失败');
|
|
1277
|
+
const bots = oj.value?.bots ?? [];
|
|
1278
|
+
if (!bots.length) {
|
|
1279
|
+
setData({ bots: [], runs: [], botMap: {} });
|
|
1280
|
+
return;
|
|
1281
|
+
}
|
|
1282
|
+
// 汇总所有 Bot 的最近运行(每 Bot 最多 15 条)
|
|
1283
|
+
const results = await Promise.all(
|
|
1284
|
+
bots.map(async (b) => {
|
|
1285
|
+
try {
|
|
1286
|
+
const r = await fetch(
|
|
1287
|
+
`/_dsh-bot/runs/list?botId=${encodeURIComponent(b.id)}&limit=15`,
|
|
1288
|
+
{ headers: { accept: 'application/json' } },
|
|
1289
|
+
);
|
|
1290
|
+
const rj = await r.json();
|
|
1291
|
+
if (!rj?.ok) return [];
|
|
1292
|
+
return (rj.value?.runs ?? []).map((run) => ({ ...run, botName: b.name, botId: b.id }));
|
|
1293
|
+
} catch {
|
|
1294
|
+
return [];
|
|
1295
|
+
}
|
|
1296
|
+
}),
|
|
1297
|
+
);
|
|
1298
|
+
const all = results.flat().sort((a, b) => Date.parse(b.startedAt) - Date.parse(a.startedAt));
|
|
1299
|
+
setData({ bots, runs: all.slice(0, 30) });
|
|
1300
|
+
setErr('');
|
|
1301
|
+
} catch (e) {
|
|
1302
|
+
setErr(String(e?.message ?? e));
|
|
1303
|
+
}
|
|
1304
|
+
}, []);
|
|
1305
|
+
|
|
1306
|
+
React.useEffect(() => {
|
|
1307
|
+
void load();
|
|
1308
|
+
}, [load]);
|
|
1309
|
+
|
|
1310
|
+
const fmtTime = (iso) => {
|
|
1311
|
+
if (!iso) return '—';
|
|
1312
|
+
const d = new Date(iso);
|
|
1313
|
+
return d.toLocaleString('zh-CN', { hour12: false });
|
|
1314
|
+
};
|
|
1315
|
+
const statusBadge = (run) => {
|
|
1316
|
+
const map = {
|
|
1317
|
+
success: { tone: 'ok', label: run.dryRun ? '演习' : '成功' },
|
|
1318
|
+
partial: { tone: 'warn', label: '部分成功' },
|
|
1319
|
+
failed: { tone: 'err', label: '失败' },
|
|
1320
|
+
budget_exceeded: { tone: 'err', label: '预算熔断' },
|
|
1321
|
+
};
|
|
1322
|
+
const m = map[run.status] ?? { tone: 'neutral', label: run.status };
|
|
1323
|
+
return h(Badge, { tone: m.tone }, m.label);
|
|
1324
|
+
};
|
|
1325
|
+
|
|
1326
|
+
if (err) {
|
|
1327
|
+
return h('div', { style: { fontSize: 11, color: ERR, padding: '4px 0' } }, `读取失败:${err}`);
|
|
1328
|
+
}
|
|
1329
|
+
if (!data) {
|
|
1330
|
+
return h('div', { style: { fontSize: 11, color: LABEL_3, padding: '4px 0' } }, '正在读取…');
|
|
1331
|
+
}
|
|
1332
|
+
if (!data.runs.length) {
|
|
1333
|
+
return h('div', { style: { fontSize: 11, color: LABEL_3, padding: '4px 0' } }, '还没有运行记录');
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// ---- 形态切换(segmented 两档) ----
|
|
1337
|
+
const modeBtn = (m, label) =>
|
|
1338
|
+
h(
|
|
1339
|
+
'button',
|
|
1340
|
+
{
|
|
1341
|
+
key: m,
|
|
1342
|
+
type: 'button',
|
|
1343
|
+
onClick: () => switchMode(m),
|
|
1344
|
+
style: {
|
|
1345
|
+
border: 'none',
|
|
1346
|
+
cursor: 'pointer',
|
|
1347
|
+
fontSize: 11,
|
|
1348
|
+
padding: '3px 10px',
|
|
1349
|
+
borderRadius: R_SM,
|
|
1350
|
+
background: mode === m ? BRAND : 'transparent',
|
|
1351
|
+
color: mode === m ? LABEL_FG : LABEL_3,
|
|
1352
|
+
},
|
|
1353
|
+
},
|
|
1354
|
+
label,
|
|
1355
|
+
);
|
|
1356
|
+
|
|
1357
|
+
// ---- 分页计算(paged 形态用) ----
|
|
1358
|
+
const totalPages = Math.max(1, Math.ceil(data.runs.length / RUNS_PAGE_SIZE));
|
|
1359
|
+
const safePage = Math.min(page, totalPages - 1);
|
|
1360
|
+
const visible =
|
|
1361
|
+
mode === 'paged'
|
|
1362
|
+
? data.runs.slice(safePage * RUNS_PAGE_SIZE, safePage * RUNS_PAGE_SIZE + RUNS_PAGE_SIZE)
|
|
1363
|
+
: data.runs;
|
|
1364
|
+
|
|
1365
|
+
const runRow = (run) =>
|
|
1366
|
+
h(
|
|
1367
|
+
'div',
|
|
1368
|
+
{
|
|
1369
|
+
key: run.id,
|
|
1370
|
+
onClick: () => setOpenRun(run),
|
|
1371
|
+
style: {
|
|
1372
|
+
display: 'flex',
|
|
1373
|
+
alignItems: 'center',
|
|
1374
|
+
gap: 8,
|
|
1375
|
+
padding: '7px 8px',
|
|
1376
|
+
borderRadius: R_SM,
|
|
1377
|
+
cursor: 'pointer',
|
|
1378
|
+
borderBottom: `1px solid ${BORDER_L1}`,
|
|
1379
|
+
},
|
|
1380
|
+
},
|
|
1381
|
+
statusBadge(run),
|
|
1382
|
+
h(
|
|
1383
|
+
'div',
|
|
1384
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
1385
|
+
h(
|
|
1386
|
+
'div',
|
|
1387
|
+
{ style: { fontSize: 12, color: LABEL_1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
1388
|
+
`${run.botName} · ${run.outputPreview || '(无文本输出)'}`,
|
|
1389
|
+
),
|
|
1390
|
+
h(
|
|
1391
|
+
'div',
|
|
1392
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 1 } },
|
|
1393
|
+
`${fmtTime(run.startedAt)} · ${run.trigger}${run.modelFallback ? ' · 模型已兜底' : ''}`,
|
|
1394
|
+
),
|
|
1395
|
+
),
|
|
1396
|
+
h(
|
|
1397
|
+
'div',
|
|
1398
|
+
{ style: { textAlign: 'right', flexShrink: 0 } },
|
|
1399
|
+
h('div', { style: { fontSize: 12, color: LABEL_1 } }, fmtCost(run.usage)),
|
|
1400
|
+
h(
|
|
1401
|
+
'div',
|
|
1402
|
+
{ style: { fontSize: 10, color: run.usage?.costKind === 'free' ? OK : run.usage?.costKind === 'unknown' ? WARN : LABEL_3 } },
|
|
1403
|
+
run.usage?.costKind === 'free' ? '免费渠道' : run.usage?.costKind === 'unknown' ? '无价目' : '付费',
|
|
1404
|
+
),
|
|
1405
|
+
),
|
|
1406
|
+
);
|
|
1407
|
+
|
|
1408
|
+
return h(
|
|
1409
|
+
'div',
|
|
1410
|
+
null,
|
|
1411
|
+
// ---- 工具条:形态切换 + 计数 ----
|
|
1412
|
+
h(
|
|
1413
|
+
'div',
|
|
1414
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 } },
|
|
1415
|
+
h(
|
|
1416
|
+
'div',
|
|
1417
|
+
{
|
|
1418
|
+
style: {
|
|
1419
|
+
display: 'inline-flex',
|
|
1420
|
+
gap: 2,
|
|
1421
|
+
padding: 2,
|
|
1422
|
+
borderRadius: R_SM,
|
|
1423
|
+
background: BG_L3,
|
|
1424
|
+
},
|
|
1425
|
+
},
|
|
1426
|
+
modeBtn('paged', '分页'),
|
|
1427
|
+
modeBtn('compact', '滚动'),
|
|
1428
|
+
),
|
|
1429
|
+
h('div', { style: { flex: 1 } }),
|
|
1430
|
+
h('div', { style: { fontSize: 10, color: LABEL_3 } }, `共 ${data.runs.length} 条`),
|
|
1431
|
+
),
|
|
1432
|
+
// ---- 列表主体 ----
|
|
1433
|
+
h(
|
|
1434
|
+
'div',
|
|
1435
|
+
mode === 'compact'
|
|
1436
|
+
? { style: { maxHeight: RUNS_COMPACT_HEIGHT, overflowY: 'auto', overflowX: 'hidden' } }
|
|
1437
|
+
: null,
|
|
1438
|
+
visible.map(runRow),
|
|
1439
|
+
),
|
|
1440
|
+
// ---- 分页器(paged 形态) ----
|
|
1441
|
+
mode === 'paged' && totalPages > 1
|
|
1442
|
+
? h(
|
|
1443
|
+
'div',
|
|
1444
|
+
{ style: { display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8, marginTop: 6 } },
|
|
1445
|
+
h(
|
|
1446
|
+
'button',
|
|
1447
|
+
{
|
|
1448
|
+
type: 'button',
|
|
1449
|
+
disabled: safePage === 0,
|
|
1450
|
+
onClick: () => setPage(safePage - 1),
|
|
1451
|
+
style: {
|
|
1452
|
+
border: 'none', background: 'transparent', cursor: safePage === 0 ? 'default' : 'pointer',
|
|
1453
|
+
color: safePage === 0 ? LABEL_3 : LABEL_1, fontSize: 12, padding: '2px 8px', opacity: safePage === 0 ? 0.4 : 1,
|
|
1454
|
+
},
|
|
1455
|
+
},
|
|
1456
|
+
'‹',
|
|
1457
|
+
),
|
|
1458
|
+
...Array.from({ length: totalPages }, (_, i) =>
|
|
1459
|
+
h(
|
|
1460
|
+
'button',
|
|
1461
|
+
{
|
|
1462
|
+
key: i,
|
|
1463
|
+
type: 'button',
|
|
1464
|
+
onClick: () => setPage(i),
|
|
1465
|
+
style: {
|
|
1466
|
+
width: 22, height: 22, borderRadius: R_SM, fontSize: 11,
|
|
1467
|
+
border: 'none', cursor: 'pointer',
|
|
1468
|
+
background: i === safePage ? BRAND : 'transparent',
|
|
1469
|
+
color: i === safePage ? LABEL_FG : LABEL_2,
|
|
1470
|
+
},
|
|
1471
|
+
},
|
|
1472
|
+
String(i + 1),
|
|
1473
|
+
),
|
|
1474
|
+
),
|
|
1475
|
+
h(
|
|
1476
|
+
'button',
|
|
1477
|
+
{
|
|
1478
|
+
type: 'button',
|
|
1479
|
+
disabled: safePage >= totalPages - 1,
|
|
1480
|
+
onClick: () => setPage(safePage + 1),
|
|
1481
|
+
style: {
|
|
1482
|
+
border: 'none', background: 'transparent', cursor: safePage >= totalPages - 1 ? 'default' : 'pointer',
|
|
1483
|
+
color: safePage >= totalPages - 1 ? LABEL_3 : LABEL_1, fontSize: 12, padding: '2px 8px', opacity: safePage >= totalPages - 1 ? 0.4 : 1,
|
|
1484
|
+
},
|
|
1485
|
+
},
|
|
1486
|
+
'›',
|
|
1487
|
+
),
|
|
1488
|
+
)
|
|
1489
|
+
: null,
|
|
1490
|
+
openRun ? h(RunDetailModal, { run: openRun, onClose: () => setOpenRun(null) }) : null,
|
|
1491
|
+
);
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
/** 单轮运行详情弹窗 */
|
|
1495
|
+
function RunDetailModal({ run, onClose }) {
|
|
1496
|
+
const u = run.usage ?? {};
|
|
1497
|
+
const tokenRows = [
|
|
1498
|
+
['输入', u.inputTokens],
|
|
1499
|
+
['输出', u.outputTokens],
|
|
1500
|
+
['缓存命中', u.cacheHitTokens],
|
|
1501
|
+
['缓存未命中', u.cacheMissTokens],
|
|
1502
|
+
['思考', u.reasoningTokens],
|
|
1503
|
+
];
|
|
1504
|
+
const totalTokens = tokenRows.reduce((s, [, v]) => s + (Number(v) || 0), 0);
|
|
1505
|
+
const costLabel = fmtCost(u);
|
|
1506
|
+
const costTone =
|
|
1507
|
+
u.costKind === 'free' ? OK : u.costKind === 'unknown' ? WARN : LABEL_1;
|
|
1508
|
+
const fmtTime = (iso) => (iso ? new Date(iso).toLocaleString('zh-CN', { hour12: false }) : '—');
|
|
1509
|
+
return h(
|
|
1510
|
+
'div',
|
|
1511
|
+
{
|
|
1512
|
+
onClick: onClose,
|
|
1513
|
+
style: {
|
|
1514
|
+
position: 'fixed', inset: 0, zIndex: 10000,
|
|
1515
|
+
background: 'rgba(0,0,0,.45)',
|
|
1516
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
1517
|
+
fontFamily: FONT,
|
|
1518
|
+
},
|
|
1519
|
+
},
|
|
1520
|
+
h(
|
|
1521
|
+
'div',
|
|
1522
|
+
{
|
|
1523
|
+
onClick: (e) => e.stopPropagation(),
|
|
1524
|
+
style: {
|
|
1525
|
+
width: 520, maxHeight: '80vh', overflow: 'auto',
|
|
1526
|
+
background: BG_L3, borderRadius: R_LG, boxShadow: SHADOW,
|
|
1527
|
+
padding: 16,
|
|
1528
|
+
},
|
|
1529
|
+
},
|
|
1530
|
+
// ---- 头部 ----
|
|
1531
|
+
h(
|
|
1532
|
+
'div',
|
|
1533
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 12 } },
|
|
1534
|
+
h('div', { style: { fontSize: 14, fontWeight: 600, color: LABEL_1, flex: 1 } }, `运行详情 · ${run.botName}`),
|
|
1535
|
+
h(Badge, { tone: run.dryRun ? 'brand' : 'neutral' }, run.dryRun ? '演习' : '真实执行'),
|
|
1536
|
+
h(
|
|
1537
|
+
'button',
|
|
1538
|
+
{ onClick: onClose, type: 'button', style: { border: 'none', background: 'transparent', cursor: 'pointer', color: LABEL_2, fontSize: 14 } },
|
|
1539
|
+
'✕',
|
|
1540
|
+
),
|
|
1541
|
+
),
|
|
1542
|
+
// ---- 概要 ----
|
|
1543
|
+
h(
|
|
1544
|
+
'div',
|
|
1545
|
+
{ style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginBottom: 12 } },
|
|
1546
|
+
...[
|
|
1547
|
+
['状态', run.status],
|
|
1548
|
+
['触发方式', run.trigger],
|
|
1549
|
+
['开始', fmtTime(run.startedAt)],
|
|
1550
|
+
['结束', fmtTime(run.endedAt)],
|
|
1551
|
+
['步骤数', String(run.stepCount ?? 0)],
|
|
1552
|
+
['运行 ID', run.id],
|
|
1553
|
+
].map(([k, v]) =>
|
|
1554
|
+
h(
|
|
1555
|
+
'div',
|
|
1556
|
+
{ key: k, style: { padding: '6px 8px', background: BG_L2 || BG_L3, borderRadius: R_SM } },
|
|
1557
|
+
h('div', { style: { fontSize: 10, color: LABEL_3 } }, k),
|
|
1558
|
+
h('div', { style: { fontSize: 12, color: LABEL_1, wordBreak: 'break-all' } }, v),
|
|
1559
|
+
),
|
|
1560
|
+
),
|
|
1561
|
+
),
|
|
1562
|
+
// ---- 费用(透明化核心块) ----
|
|
1563
|
+
h(
|
|
1564
|
+
'div',
|
|
1565
|
+
{ style: { padding: 10, borderRadius: R_MD, border: `1px solid ${BORDER_L1}`, marginBottom: 12 } },
|
|
1566
|
+
h('div', { style: { fontSize: 11, fontWeight: 600, color: LABEL_2, marginBottom: 6 } }, '费用与渠道'),
|
|
1567
|
+
h(
|
|
1568
|
+
'div',
|
|
1569
|
+
{ style: { display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 6 } },
|
|
1570
|
+
h('div', { style: { fontSize: 20, fontWeight: 600, color: costTone } }, costLabel),
|
|
1571
|
+
h(
|
|
1572
|
+
'span',
|
|
1573
|
+
{ style: { fontSize: 11, color: u.costKind === 'free' ? OK : u.costKind === 'unknown' ? WARN : LABEL_3 } },
|
|
1574
|
+
u.costKind === 'free'
|
|
1575
|
+
? '免费渠道(订阅内/免费模型,未产生 API 计费)'
|
|
1576
|
+
: u.costKind === 'unknown'
|
|
1577
|
+
? '渠道无价目数据,无法计费'
|
|
1578
|
+
: '付费渠道(按 token 计价)',
|
|
1579
|
+
),
|
|
1580
|
+
),
|
|
1581
|
+
u.costNote ? h('div', { style: { fontSize: 10, color: LABEL_3, marginBottom: 4 } }, u.costNote) : null,
|
|
1582
|
+
h(
|
|
1583
|
+
'div',
|
|
1584
|
+
{ style: { display: 'grid', gridTemplateColumns: 'repeat(5, 1fr)', gap: 4 } },
|
|
1585
|
+
...tokenRows.map(([k, v]) =>
|
|
1586
|
+
h(
|
|
1587
|
+
'div',
|
|
1588
|
+
{ key: k, style: { textAlign: 'center', padding: '4px 2px', background: BG_L3, borderRadius: R_XS } },
|
|
1589
|
+
h('div', { style: { fontSize: 10, color: LABEL_3 } }, k),
|
|
1590
|
+
h('div', { style: { fontSize: 12, color: LABEL_1 } }, String(Number(v) || 0)),
|
|
1591
|
+
),
|
|
1592
|
+
),
|
|
1593
|
+
),
|
|
1594
|
+
h('div', { style: { fontSize: 10, color: LABEL_3, marginTop: 6 } }, `合计 ${totalTokens} tokens(含缓存与思考)`),
|
|
1595
|
+
),
|
|
1596
|
+
// ---- 模型兜底说明 ----
|
|
1597
|
+
run.modelFallback
|
|
1598
|
+
? h(
|
|
1599
|
+
'div',
|
|
1600
|
+
{ style: { fontSize: 11, color: WARN, padding: '6px 8px', borderRadius: R_SM, background: BG_L3, marginBottom: 12 } },
|
|
1601
|
+
`⚠ ${run.modelFallback}`,
|
|
1602
|
+
)
|
|
1603
|
+
: null,
|
|
1604
|
+
// ---- 错误 ----
|
|
1605
|
+
run.error
|
|
1606
|
+
? h(
|
|
1607
|
+
'div',
|
|
1608
|
+
{ style: { fontSize: 11, color: ERR, padding: '6px 8px', borderRadius: R_SM, background: BG_L3, marginBottom: 12 } },
|
|
1609
|
+
`${run.error.code}: ${run.error.message}`,
|
|
1610
|
+
)
|
|
1611
|
+
: null,
|
|
1612
|
+
// ---- 工具调用 ----
|
|
1613
|
+
run.toolSteps?.length
|
|
1614
|
+
? h(
|
|
1615
|
+
'div',
|
|
1616
|
+
{ style: { marginBottom: 12 } },
|
|
1617
|
+
h('div', { style: { fontSize: 11, fontWeight: 600, color: LABEL_2, marginBottom: 4 } }, `工具调用(${run.toolSteps.length} 次)`),
|
|
1618
|
+
...run.toolSteps.map((t, i) =>
|
|
1619
|
+
h(
|
|
1620
|
+
'div',
|
|
1621
|
+
{ key: i, style: { display: 'flex', gap: 8, fontSize: 11, padding: '3px 0', color: LABEL_1 } },
|
|
1622
|
+
h('span', { style: { color: LABEL_3, width: 60, flexShrink: 0 } }, `${t.durationMs ?? 0}ms`),
|
|
1623
|
+
h('span', { style: { fontFamily: 'monospace' } }, t.name),
|
|
1624
|
+
t.blocked ? h(Badge, { tone: 'warn' }, '拦截') : null,
|
|
1625
|
+
t.error ? h('span', { style: { color: ERR, flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, t.error.message ?? '') : null,
|
|
1626
|
+
),
|
|
1627
|
+
),
|
|
1628
|
+
)
|
|
1629
|
+
: null,
|
|
1630
|
+
// ---- 输出预览 ----
|
|
1631
|
+
h(
|
|
1632
|
+
'div',
|
|
1633
|
+
null,
|
|
1634
|
+
h('div', { style: { fontSize: 11, fontWeight: 600, color: LABEL_2, marginBottom: 4 } }, '输出预览(前 200 字)'),
|
|
1635
|
+
h(
|
|
1636
|
+
'div',
|
|
1637
|
+
{
|
|
1638
|
+
style: {
|
|
1639
|
+
fontSize: 11, color: LABEL_1, padding: 8, borderRadius: R_SM,
|
|
1640
|
+
background: BG_L3, whiteSpace: 'pre-wrap', wordBreak: 'break-word',
|
|
1641
|
+
maxHeight: 160, overflow: 'auto',
|
|
1642
|
+
},
|
|
1643
|
+
},
|
|
1644
|
+
run.outputPreview || '(无)',
|
|
1645
|
+
),
|
|
1646
|
+
),
|
|
1647
|
+
),
|
|
1648
|
+
);
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
/** 设置视图 */
|
|
1652
|
+
function SettingsView({ busy, onClose, onLoad, onSave, onClearRuns, hideClose }) {
|
|
1653
|
+
const [s, setS] = React.useState(null);
|
|
1654
|
+
const [err, setErr] = React.useState('');
|
|
1655
|
+
|
|
1656
|
+
React.useEffect(() => {
|
|
1657
|
+
let alive = true;
|
|
1658
|
+
(async () => {
|
|
1659
|
+
try {
|
|
1660
|
+
const r = await onLoad();
|
|
1661
|
+
if (alive) setS(r);
|
|
1662
|
+
} catch (e) {
|
|
1663
|
+
if (alive) setErr(String(e?.message ?? e));
|
|
1664
|
+
}
|
|
1665
|
+
})();
|
|
1666
|
+
return () => {
|
|
1667
|
+
alive = false;
|
|
1668
|
+
};
|
|
1669
|
+
}, [onLoad]);
|
|
1670
|
+
|
|
1671
|
+
if (err) {
|
|
1672
|
+
return h(
|
|
1673
|
+
'div',
|
|
1674
|
+
{ style: { padding: 12, fontSize: 12, color: ERR } },
|
|
1675
|
+
`读取设置失败:${err}`,
|
|
1676
|
+
);
|
|
1677
|
+
}
|
|
1678
|
+
if (!s) {
|
|
1679
|
+
return h(
|
|
1680
|
+
'div',
|
|
1681
|
+
{ style: { padding: 12, fontSize: 12, color: LABEL_3 } },
|
|
1682
|
+
'正在读取设置…',
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
const patch = (k, v) => {
|
|
1687
|
+
const next = { ...s, [k]: v };
|
|
1688
|
+
setS(next);
|
|
1689
|
+
void onSave(next);
|
|
1690
|
+
};
|
|
1691
|
+
|
|
1692
|
+
const group = (title, children) =>
|
|
1693
|
+
h(
|
|
1694
|
+
'div',
|
|
1695
|
+
{ style: { marginBottom: 14 } },
|
|
1696
|
+
h(
|
|
1697
|
+
'div',
|
|
1698
|
+
{
|
|
1699
|
+
style: {
|
|
1700
|
+
fontSize: 11,
|
|
1701
|
+
fontWeight: 600,
|
|
1702
|
+
color: LABEL_2,
|
|
1703
|
+
marginBottom: 4,
|
|
1704
|
+
paddingBottom: 4,
|
|
1705
|
+
borderBottom: `1px solid ${BORDER_L1}`,
|
|
1706
|
+
},
|
|
1707
|
+
},
|
|
1708
|
+
title,
|
|
1709
|
+
),
|
|
1710
|
+
...children,
|
|
1711
|
+
);
|
|
1712
|
+
|
|
1713
|
+
return h(
|
|
1714
|
+
'div',
|
|
1715
|
+
{ style: { padding: '4px 0' } },
|
|
1716
|
+
|
|
1717
|
+
// ⭐ 诚实原则:这里的每一项都在下面真正被消费。
|
|
1718
|
+
// · 通知三项 → client 全局轮询器(apply 内 notify poller)
|
|
1719
|
+
// · 模型 / 预算 → 每个 Bot 的「配置与危险操作」层
|
|
1720
|
+
// · 峰谷调度 / 快照策略 → 插件配置(cordis.patch.yml)
|
|
1721
|
+
h(
|
|
1722
|
+
'div',
|
|
1723
|
+
{
|
|
1724
|
+
style: {
|
|
1725
|
+
fontSize: 11,
|
|
1726
|
+
color: LABEL_3,
|
|
1727
|
+
lineHeight: 1.8,
|
|
1728
|
+
padding: '8px 10px',
|
|
1729
|
+
borderRadius: R_SM,
|
|
1730
|
+
background: BG_L3,
|
|
1731
|
+
marginBottom: 14,
|
|
1732
|
+
},
|
|
1733
|
+
},
|
|
1734
|
+
'模型与预算在每个 Bot 的',
|
|
1735
|
+
h('br'),
|
|
1736
|
+
'「配置与危险操作」层里;峰谷调度与快照策略在插件配置(cordis.patch.yml)。',
|
|
1737
|
+
),
|
|
1738
|
+
|
|
1739
|
+
group('通知', [
|
|
1740
|
+
h(ToggleRow, {
|
|
1741
|
+
label: '运行完成后系统通知',
|
|
1742
|
+
hint: '轮询检测新完成的运行;首次需在系统弹窗里授权通知权限',
|
|
1743
|
+
value: s.notifySystem !== false,
|
|
1744
|
+
onChange: (v) => patch('notifySystem', v),
|
|
1745
|
+
}),
|
|
1746
|
+
h(ToggleRow, {
|
|
1747
|
+
label: '仅异常时打扰',
|
|
1748
|
+
hint: '开启后只有失败/部分成功/需要拍板才响;关闭则每次运行都通知',
|
|
1749
|
+
value: s.notifyOnlyOnDecision !== false,
|
|
1750
|
+
onChange: (v) => patch('notifyOnlyOnDecision', v),
|
|
1751
|
+
}),
|
|
1752
|
+
h(
|
|
1753
|
+
'div',
|
|
1754
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 10, padding: '6px 0' } },
|
|
1755
|
+
h(
|
|
1756
|
+
'div',
|
|
1757
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
1758
|
+
h('div', { style: { fontSize: 12, color: LABEL_1 } }, '免打扰时段'),
|
|
1759
|
+
h(
|
|
1760
|
+
'div',
|
|
1761
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 2 } },
|
|
1762
|
+
s.quietHours ? `${s.quietHours.start} – ${s.quietHours.end}(跨午夜支持)` : '未启用',
|
|
1763
|
+
),
|
|
1764
|
+
),
|
|
1765
|
+
s.quietHours
|
|
1766
|
+
? h(
|
|
1767
|
+
'button',
|
|
1768
|
+
{
|
|
1769
|
+
type: 'button',
|
|
1770
|
+
onClick: () => patch('quietHours', null),
|
|
1771
|
+
style: {
|
|
1772
|
+
border: 'none', background: 'transparent', cursor: 'pointer',
|
|
1773
|
+
color: LABEL_2, fontSize: 11, textDecoration: 'underline',
|
|
1774
|
+
},
|
|
1775
|
+
},
|
|
1776
|
+
'关闭',
|
|
1777
|
+
)
|
|
1778
|
+
: h(
|
|
1779
|
+
'button',
|
|
1780
|
+
{
|
|
1781
|
+
type: 'button',
|
|
1782
|
+
onClick: () => patch('quietHours', { start: '22:00', end: '08:00' }),
|
|
1783
|
+
style: {
|
|
1784
|
+
border: 'none', background: 'transparent', cursor: 'pointer',
|
|
1785
|
+
color: BRAND, fontSize: 11, textDecoration: 'underline',
|
|
1786
|
+
},
|
|
1787
|
+
},
|
|
1788
|
+
'启用 22:00–08:00',
|
|
1789
|
+
),
|
|
1790
|
+
),
|
|
1791
|
+
]),
|
|
1792
|
+
|
|
1793
|
+
group('最近运行', [
|
|
1794
|
+
h(RunsSection, { busy }),
|
|
1795
|
+
]),
|
|
1796
|
+
|
|
1797
|
+
group('数据', [
|
|
1798
|
+
h(
|
|
1799
|
+
'div',
|
|
1800
|
+
{
|
|
1801
|
+
style: {
|
|
1802
|
+
display: 'flex',
|
|
1803
|
+
alignItems: 'center',
|
|
1804
|
+
gap: 8,
|
|
1805
|
+
padding: '6px 0',
|
|
1806
|
+
},
|
|
1807
|
+
},
|
|
1808
|
+
h(
|
|
1809
|
+
'div',
|
|
1810
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
1811
|
+
h(
|
|
1812
|
+
'div',
|
|
1813
|
+
{ style: { fontSize: 12, color: LABEL_1 } },
|
|
1814
|
+
'运行记录',
|
|
1815
|
+
),
|
|
1816
|
+
h(
|
|
1817
|
+
'div',
|
|
1818
|
+
{ style: { fontSize: 10, color: LABEL_3, marginTop: 2 } },
|
|
1819
|
+
'清空后不可恢复,但目标和记忆保留',
|
|
1820
|
+
),
|
|
1821
|
+
),
|
|
1822
|
+
h(ConfirmBtn, {
|
|
1823
|
+
label: '清空记录',
|
|
1824
|
+
confirmLabel: '确认清空?不可恢复',
|
|
1825
|
+
kind: 'danger',
|
|
1826
|
+
disabled: busy,
|
|
1827
|
+
onConfirm: () => onClearRuns(),
|
|
1828
|
+
}),
|
|
1829
|
+
),
|
|
1830
|
+
]),
|
|
1831
|
+
|
|
1832
|
+
hideClose
|
|
1833
|
+
? null
|
|
1834
|
+
: h(
|
|
1835
|
+
'div',
|
|
1836
|
+
{
|
|
1837
|
+
style: {
|
|
1838
|
+
display: 'flex',
|
|
1839
|
+
justifyContent: 'flex-end',
|
|
1840
|
+
marginTop: 4,
|
|
1841
|
+
},
|
|
1842
|
+
},
|
|
1843
|
+
h(Btn, { onClick: onClose }, '关闭设置'),
|
|
1844
|
+
),
|
|
1845
|
+
);
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
|
|
1849
|
+
//
|
|
1850
|
+
// 核心洞察:用户说"挤成一坨,不可阅览",本质不是排版问题,
|
|
1851
|
+
// 而是**该显示的和不该显示的都在显示**。
|
|
1852
|
+
//
|
|
1853
|
+
// 所以这里换个思路:不再"有什么数据画什么",
|
|
1854
|
+
// 而是先判断 Bot 现在处于什么状态,只画那个状态需要的东西。
|
|
1855
|
+
//
|
|
1856
|
+
// ┌─────────────┬──────────────────────────────┐
|
|
1857
|
+
// │ Bot 状态 │ 需要显示 │
|
|
1858
|
+
// ├─────────────┼──────────────────────────────┤
|
|
1859
|
+
// │ 没有目标 │ 一行提示 + [设目标] │
|
|
1860
|
+
// │ 待你确认 │ 目标 + 确认/驳回(大按钮) │
|
|
1861
|
+
// │ 卡住了 │ 目标 + 原因 + 怎么处理 │
|
|
1862
|
+
// │ 上次失败 │ 目标 + 失败原因 + [看看] │
|
|
1863
|
+
// │ 正常 │ 目标 + 一行最近结果 │
|
|
1864
|
+
// │ 空闲 │ 目标 + "一切都好" │
|
|
1865
|
+
// └─────────────┴──────────────────────────────┘
|
|
1866
|
+
//
|
|
1867
|
+
// 其余内容(记忆、例行、模型、预算、快照)收进「更多」。
|
|
1868
|
+
// ============================================================
|
|
1869
|
+
|
|
1870
|
+
/** 判断 Bot 当前最需要注意的状态 */
|
|
1871
|
+
function botSituation(bot, detail) {
|
|
1872
|
+
const goal = detail?.goal;
|
|
1873
|
+
const runs = detail?.runs ?? [];
|
|
1874
|
+
const last = runs[0];
|
|
1875
|
+
|
|
1876
|
+
if (detail?.proposals?.length) return 'proposal';
|
|
1877
|
+
if (goal?.status === 'pending_confirmation') return 'pending';
|
|
1878
|
+
if (goal?.status === 'blocked') return 'blocked';
|
|
1879
|
+
if (last?.status === 'failed') return 'failed';
|
|
1880
|
+
if (last?.status === 'budget_exceeded') return 'budget';
|
|
1881
|
+
if (!goal) return 'no-goal';
|
|
1882
|
+
if (goal.status === 'achieved' || goal.status === 'abandoned') return 'done';
|
|
1883
|
+
return 'normal';
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
/** 一行式状态摘要 —— A 方案的"摘要层" */
|
|
1887
|
+
function renderSituation(bot, detail, act, busy) {
|
|
1888
|
+
const sit = botSituation(bot, detail);
|
|
1889
|
+
const goal = detail?.goal;
|
|
1890
|
+
const last = detail?.runs?.[0];
|
|
1891
|
+
|
|
1892
|
+
// 统一样式的小卡片
|
|
1893
|
+
const box = (children, tone) =>
|
|
1894
|
+
h(
|
|
1895
|
+
'div',
|
|
1896
|
+
{
|
|
1897
|
+
style: {
|
|
1898
|
+
margin: '0 0 8px',
|
|
1899
|
+
padding: '8px 10px',
|
|
1900
|
+
borderRadius: R_SM,
|
|
1901
|
+
background: BG_L2,
|
|
1902
|
+
border: `1px solid ${tone ?? BORDER_L1}`,
|
|
1903
|
+
fontSize: 12,
|
|
1904
|
+
lineHeight: 1.6,
|
|
1905
|
+
color: LABEL_2,
|
|
1906
|
+
},
|
|
1907
|
+
},
|
|
1908
|
+
children,
|
|
1909
|
+
);
|
|
1910
|
+
|
|
1911
|
+
switch (sit) {
|
|
1912
|
+
case 'no-goal':
|
|
1913
|
+
return box(
|
|
1914
|
+
h(
|
|
1915
|
+
'div',
|
|
1916
|
+
{
|
|
1917
|
+
style: {
|
|
1918
|
+
display: 'flex',
|
|
1919
|
+
alignItems: 'center',
|
|
1920
|
+
justifyContent: 'space-between',
|
|
1921
|
+
gap: 8,
|
|
1922
|
+
},
|
|
1923
|
+
},
|
|
1924
|
+
h('span', null, '他还没有目标,不知道该守什么'),
|
|
1925
|
+
h(
|
|
1926
|
+
Btn,
|
|
1927
|
+
{
|
|
1928
|
+
onClick: () =>
|
|
1929
|
+
act.onOpenForm?.({
|
|
1930
|
+
kind: 'goal',
|
|
1931
|
+
botId: bot.id,
|
|
1932
|
+
botName: bot.name,
|
|
1933
|
+
}),
|
|
1934
|
+
disabled: busy,
|
|
1935
|
+
},
|
|
1936
|
+
'设目标',
|
|
1937
|
+
),
|
|
1938
|
+
),
|
|
1939
|
+
);
|
|
1940
|
+
|
|
1941
|
+
case 'pending':
|
|
1942
|
+
return box(
|
|
1943
|
+
h(
|
|
1944
|
+
'div',
|
|
1945
|
+
null,
|
|
1946
|
+
h(
|
|
1947
|
+
'div',
|
|
1948
|
+
{ style: { color: WARN, fontWeight: 600, marginBottom: 4 } },
|
|
1949
|
+
'他自己判断做完了,等你拍板',
|
|
1950
|
+
),
|
|
1951
|
+
h('div', { style: { color: LABEL_2, marginBottom: 8 } }, goal.description),
|
|
1952
|
+
h(
|
|
1953
|
+
'div',
|
|
1954
|
+
{ style: { display: 'flex', gap: 6 } },
|
|
1955
|
+
h(ConfirmBtn, {
|
|
1956
|
+
label: '确认达成',
|
|
1957
|
+
confirmLabel: '确认归档?',
|
|
1958
|
+
kind: 'primary',
|
|
1959
|
+
disabled: busy,
|
|
1960
|
+
onConfirm: () => act('goalConfirm', bot),
|
|
1961
|
+
}),
|
|
1962
|
+
h(
|
|
1963
|
+
Btn,
|
|
1964
|
+
{
|
|
1965
|
+
kind: 'danger',
|
|
1966
|
+
disabled: busy,
|
|
1967
|
+
onClick: () =>
|
|
1968
|
+
act.onOpenForm?.({
|
|
1969
|
+
kind: 'reject',
|
|
1970
|
+
botId: bot.id,
|
|
1971
|
+
botName: bot.name,
|
|
1972
|
+
}),
|
|
1973
|
+
},
|
|
1974
|
+
'驳回',
|
|
1975
|
+
),
|
|
1976
|
+
),
|
|
1977
|
+
),
|
|
1978
|
+
WARN,
|
|
1979
|
+
);
|
|
1980
|
+
|
|
1981
|
+
case 'blocked':
|
|
1982
|
+
return box(
|
|
1983
|
+
h(
|
|
1984
|
+
'div',
|
|
1985
|
+
null,
|
|
1986
|
+
h(
|
|
1987
|
+
'div',
|
|
1988
|
+
{
|
|
1989
|
+
style: { color: ERR, fontWeight: 600, marginBottom: 4 },
|
|
1990
|
+
},
|
|
1991
|
+
'卡住了,需要你处理',
|
|
1992
|
+
),
|
|
1993
|
+
h(
|
|
1994
|
+
'div',
|
|
1995
|
+
{ style: { color: LABEL_2, marginBottom: 8 } },
|
|
1996
|
+
goal.progress || '(没有更多细节)',
|
|
1997
|
+
),
|
|
1998
|
+
// ⭐ 关键:光说"需要你处理"是不够的,必须给出**能点的动作**。
|
|
1999
|
+
// 之前这里只有一行红字,用户只能看着——
|
|
2000
|
+
// 这是个设计漏洞,不是文案问题。
|
|
2001
|
+
h(
|
|
2002
|
+
'div',
|
|
2003
|
+
{ style: { display: 'flex', gap: 6, flexWrap: 'wrap' } },
|
|
2004
|
+
h(
|
|
2005
|
+
Btn,
|
|
2006
|
+
{
|
|
2007
|
+
kind: 'primary',
|
|
2008
|
+
disabled: busy,
|
|
2009
|
+
onClick: () =>
|
|
2010
|
+
act.onOpenForm?.({
|
|
2011
|
+
kind: 'unblock',
|
|
2012
|
+
botId: bot.id,
|
|
2013
|
+
botName: bot.name,
|
|
2014
|
+
}),
|
|
2015
|
+
},
|
|
2016
|
+
'已处理好,继续',
|
|
2017
|
+
),
|
|
2018
|
+
h(
|
|
2019
|
+
Btn,
|
|
2020
|
+
{
|
|
2021
|
+
disabled: busy,
|
|
2022
|
+
onClick: () =>
|
|
2023
|
+
act.onOpenForm?.({
|
|
2024
|
+
kind: 'giveup',
|
|
2025
|
+
botId: bot.id,
|
|
2026
|
+
botName: bot.name,
|
|
2027
|
+
}),
|
|
2028
|
+
},
|
|
2029
|
+
'这事做不了,放弃',
|
|
2030
|
+
),
|
|
2031
|
+
),
|
|
2032
|
+
h(
|
|
2033
|
+
'div',
|
|
2034
|
+
{
|
|
2035
|
+
style: {
|
|
2036
|
+
fontSize: 10,
|
|
2037
|
+
color: LABEL_3,
|
|
2038
|
+
marginTop: 6,
|
|
2039
|
+
},
|
|
2040
|
+
},
|
|
2041
|
+
'比如:去把 /admin 的鉴权重配一下,再点「继续」。',
|
|
2042
|
+
),
|
|
2043
|
+
),
|
|
2044
|
+
ERR,
|
|
2045
|
+
);
|
|
2046
|
+
|
|
2047
|
+
case 'failed':
|
|
2048
|
+
return box(
|
|
2049
|
+
h(
|
|
2050
|
+
'div',
|
|
2051
|
+
null,
|
|
2052
|
+
h(
|
|
2053
|
+
'div',
|
|
2054
|
+
{
|
|
2055
|
+
style: { color: ERR, fontWeight: 600, marginBottom: 4 },
|
|
2056
|
+
},
|
|
2057
|
+
'上次执行失败了',
|
|
2058
|
+
),
|
|
2059
|
+
h(
|
|
2060
|
+
'div',
|
|
2061
|
+
{ style: { color: LABEL_2, whiteSpace: 'pre-wrap' } },
|
|
2062
|
+
last?.error?.message ?? '没有记录到原因',
|
|
2063
|
+
),
|
|
2064
|
+
),
|
|
2065
|
+
ERR,
|
|
2066
|
+
);
|
|
2067
|
+
|
|
2068
|
+
case 'budget':
|
|
2069
|
+
return box(
|
|
2070
|
+
h(
|
|
2071
|
+
'div',
|
|
2072
|
+
{ style: { color: ERR, fontWeight: 600 } },
|
|
2073
|
+
`预算用完了:${last?.error?.message ?? '超出每日上限'}`,
|
|
2074
|
+
),
|
|
2075
|
+
ERR,
|
|
2076
|
+
);
|
|
2077
|
+
|
|
2078
|
+
case 'proposal':
|
|
2079
|
+
return box(
|
|
2080
|
+
h(
|
|
2081
|
+
'div',
|
|
2082
|
+
null,
|
|
2083
|
+
h(
|
|
2084
|
+
'div',
|
|
2085
|
+
{ style: { color: BRAND, fontWeight: 600, marginBottom: 4 } },
|
|
2086
|
+
`他建议了 ${detail.proposals.length} 个下一步`,
|
|
2087
|
+
),
|
|
2088
|
+
h(
|
|
2089
|
+
'div',
|
|
2090
|
+
{ style: { color: LABEL_3, marginBottom: 8 } },
|
|
2091
|
+
'他不会自己开张,要不要做得你定',
|
|
2092
|
+
),
|
|
2093
|
+
...detail.proposals.map((p, i) =>
|
|
2094
|
+
h(
|
|
2095
|
+
'div',
|
|
2096
|
+
{
|
|
2097
|
+
key: p.id,
|
|
2098
|
+
style: {
|
|
2099
|
+
display: 'flex',
|
|
2100
|
+
alignItems: 'center',
|
|
2101
|
+
gap: 8,
|
|
2102
|
+
paddingTop: i === 0 ? 0 : 6,
|
|
2103
|
+
marginTop: i === 0 ? 0 : 6,
|
|
2104
|
+
borderTop: i === 0 ? 'none' : `1px solid ${BORDER_L1}`,
|
|
2105
|
+
},
|
|
2106
|
+
},
|
|
2107
|
+
h(
|
|
2108
|
+
'span',
|
|
2109
|
+
{ style: { flex: 1, color: LABEL_1 } },
|
|
2110
|
+
p.description,
|
|
2111
|
+
),
|
|
2112
|
+
h(
|
|
2113
|
+
Btn,
|
|
2114
|
+
{
|
|
2115
|
+
disabled: busy,
|
|
2116
|
+
onClick: () => act('goalAccept', { ...bot, goalId: p.id }),
|
|
2117
|
+
},
|
|
2118
|
+
'接受',
|
|
2119
|
+
),
|
|
2120
|
+
),
|
|
2121
|
+
),
|
|
2122
|
+
),
|
|
2123
|
+
BRAND,
|
|
2124
|
+
);
|
|
2125
|
+
|
|
2126
|
+
case 'done':
|
|
2127
|
+
return box(
|
|
2128
|
+
h(
|
|
2129
|
+
'div',
|
|
2130
|
+
{ style: { color: OK, fontWeight: 600 } },
|
|
2131
|
+
`目标已${goal.status === 'achieved' ? '达成' : '放弃'}`,
|
|
2132
|
+
),
|
|
2133
|
+
OK,
|
|
2134
|
+
);
|
|
2135
|
+
|
|
2136
|
+
// normal
|
|
2137
|
+
default: {
|
|
2138
|
+
const bits = [];
|
|
2139
|
+
if (goal) bits.push(`在守「${goal.description}」`);
|
|
2140
|
+
if (goal?.cadence) {
|
|
2141
|
+
bits.push(
|
|
2142
|
+
goal.lastFiredAt
|
|
2143
|
+
? `上次自动跑 ${fmtAgo(goal.lastFiredAt)}`
|
|
2144
|
+
: '还没自动跑过',
|
|
2145
|
+
);
|
|
2146
|
+
} else {
|
|
2147
|
+
bits.push('没设自动节奏');
|
|
2148
|
+
}
|
|
2149
|
+
if (last) bits.push(`上次 ${statusText(last.status)} ${fmtAgo(last.startedAt)}`);
|
|
2150
|
+
return box(h('div', { style: { color: LABEL_3 } }, bits.join(' · ')));
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
|
|
2156
|
+
|
|
2157
|
+
/** 小标题 */
|
|
2158
|
+
function SectionTitle({ children, hint }) {
|
|
2159
|
+
return h(
|
|
2160
|
+
'div',
|
|
2161
|
+
{
|
|
2162
|
+
style: {
|
|
2163
|
+
display: 'flex',
|
|
2164
|
+
alignItems: 'baseline',
|
|
2165
|
+
gap: 6,
|
|
2166
|
+
marginBottom: 6,
|
|
2167
|
+
},
|
|
2168
|
+
},
|
|
2169
|
+
h(
|
|
2170
|
+
'span',
|
|
2171
|
+
{ style: { fontSize: 11, fontWeight: 600, color: LABEL_2 } },
|
|
2172
|
+
children,
|
|
2173
|
+
),
|
|
2174
|
+
hint
|
|
2175
|
+
? h('span', { style: { fontSize: 10, color: LABEL_3 } }, hint)
|
|
2176
|
+
: null,
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
/** ① 目标卡:进度、下一步、待确认按钮 */
|
|
2181
|
+
function renderGoalCard(bot, detail, act, busy) {
|
|
2182
|
+
const goal = detail.goal;
|
|
2183
|
+
// 没有目标 → 给一个明确的行动入口
|
|
2184
|
+
if (!goal) {
|
|
2185
|
+
return h(
|
|
2186
|
+
'div',
|
|
2187
|
+
{ style: { marginBottom: 10 } },
|
|
2188
|
+
h(SectionTitle, null, '目标'),
|
|
2189
|
+
h(
|
|
2190
|
+
'div',
|
|
2191
|
+
{
|
|
2192
|
+
style: {
|
|
2193
|
+
display: 'flex',
|
|
2194
|
+
alignItems: 'center',
|
|
2195
|
+
justifyContent: 'space-between',
|
|
2196
|
+
gap: 8,
|
|
2197
|
+
fontSize: 12,
|
|
2198
|
+
color: LABEL_2,
|
|
2199
|
+
},
|
|
2200
|
+
},
|
|
2201
|
+
h('span', null, '他还没有目标,不知道该守什么'),
|
|
2202
|
+
h(
|
|
2203
|
+
Btn,
|
|
2204
|
+
{
|
|
2205
|
+
onClick: () =>
|
|
2206
|
+
act.onOpenForm
|
|
2207
|
+
? act.onOpenForm({ kind: 'goal', botId: bot.id, botName: bot.name })
|
|
2208
|
+
: null,
|
|
2209
|
+
disabled: busy,
|
|
2210
|
+
},
|
|
2211
|
+
'设目标',
|
|
2212
|
+
),
|
|
2213
|
+
),
|
|
2214
|
+
);
|
|
2215
|
+
}
|
|
2216
|
+
|
|
2217
|
+
const isPending = goal.status === 'pending_confirmation';
|
|
2218
|
+
const isBlocked = goal.status === 'blocked';
|
|
2219
|
+
const isStanding = goal.type === 'standing';
|
|
2220
|
+
|
|
2221
|
+
return h(
|
|
2222
|
+
'div',
|
|
2223
|
+
{
|
|
2224
|
+
style: {
|
|
2225
|
+
marginBottom: 10,
|
|
2226
|
+
padding: '8px 10px',
|
|
2227
|
+
borderRadius: R_SM,
|
|
2228
|
+
background: BG_L2,
|
|
2229
|
+
border: `1px solid ${
|
|
2230
|
+
isPending || isBlocked ? goalStatusColor(goal.status) : BORDER_L1
|
|
2231
|
+
}`,
|
|
2232
|
+
},
|
|
2233
|
+
},
|
|
2234
|
+
h(
|
|
2235
|
+
'div',
|
|
2236
|
+
{
|
|
2237
|
+
style: {
|
|
2238
|
+
display: 'flex',
|
|
2239
|
+
alignItems: 'center',
|
|
2240
|
+
gap: 7,
|
|
2241
|
+
marginBottom: 4,
|
|
2242
|
+
},
|
|
2243
|
+
},
|
|
2244
|
+
h(
|
|
2245
|
+
'span',
|
|
2246
|
+
{ style: { fontSize: 12, fontWeight: 600, color: LABEL_1 } },
|
|
2247
|
+
goal.description,
|
|
2248
|
+
),
|
|
2249
|
+
h(
|
|
2250
|
+
'span',
|
|
2251
|
+
{
|
|
2252
|
+
style: {
|
|
2253
|
+
fontSize: 10,
|
|
2254
|
+
color: goalStatusColor(goal.status),
|
|
2255
|
+
flexShrink: 0,
|
|
2256
|
+
},
|
|
2257
|
+
},
|
|
2258
|
+
goalStatusText(goal.status),
|
|
2259
|
+
),
|
|
2260
|
+
h(
|
|
2261
|
+
'span',
|
|
2262
|
+
{ style: { fontSize: 10, color: LABEL_3, flexShrink: 0 } },
|
|
2263
|
+
goalTypeText(goal.type),
|
|
2264
|
+
),
|
|
2265
|
+
isStanding
|
|
2266
|
+
? h(
|
|
2267
|
+
'span',
|
|
2268
|
+
{ style: { fontSize: 10, color: LABEL_3, flexShrink: 0 } },
|
|
2269
|
+
`第 ${goal.cycleCount ?? 0} 轮`,
|
|
2270
|
+
)
|
|
2271
|
+
: null,
|
|
2272
|
+
),
|
|
2273
|
+
h(
|
|
2274
|
+
'div',
|
|
2275
|
+
{ style: { fontSize: 11, color: LABEL_2, lineHeight: 1.6 } },
|
|
2276
|
+
goal.progress,
|
|
2277
|
+
),
|
|
2278
|
+
goal.nextStep
|
|
2279
|
+
? h(
|
|
2280
|
+
'div',
|
|
2281
|
+
{ style: { fontSize: 11, color: LABEL_3, marginTop: 3 } },
|
|
2282
|
+
`下一步:${goal.nextStep}`,
|
|
2283
|
+
)
|
|
2284
|
+
: null,
|
|
2285
|
+
|
|
2286
|
+
// ⭐ 执行节奏 —— 这是"它会不会自己跑"的答案,
|
|
2287
|
+
// 必须直接显示出来,不能藏在日志里。
|
|
2288
|
+
h(
|
|
2289
|
+
'div',
|
|
2290
|
+
{
|
|
2291
|
+
style: {
|
|
2292
|
+
fontSize: 10,
|
|
2293
|
+
marginTop: 5,
|
|
2294
|
+
color: goal.cadence ? LABEL_3 : WARN,
|
|
2295
|
+
display: 'flex',
|
|
2296
|
+
alignItems: 'center',
|
|
2297
|
+
gap: 4,
|
|
2298
|
+
},
|
|
2299
|
+
},
|
|
2300
|
+
goal.cadence
|
|
2301
|
+
? `自动执行节奏:${goal.cadence}` +
|
|
2302
|
+
(goal.lastFiredAt
|
|
2303
|
+
? ` · 上次自动跑 ${fmtAgo(goal.lastFiredAt)}`
|
|
2304
|
+
: ' · 还没自动跑过')
|
|
2305
|
+
: '⚠️ 没有设置执行节奏 —— 它不会自己跑,只有你点「执行」才动',
|
|
2306
|
+
),
|
|
2307
|
+
|
|
2308
|
+
// 待确认:这是「Bot 不能自己宣布胜利」的可见形态
|
|
2309
|
+
isPending
|
|
2310
|
+
? h(
|
|
2311
|
+
'div',
|
|
2312
|
+
{
|
|
2313
|
+
style: {
|
|
2314
|
+
display: 'flex',
|
|
2315
|
+
alignItems: 'center',
|
|
2316
|
+
gap: 6,
|
|
2317
|
+
marginTop: 8,
|
|
2318
|
+
paddingTop: 8,
|
|
2319
|
+
borderTop: `1px solid ${BORDER_L1}`,
|
|
2320
|
+
},
|
|
2321
|
+
},
|
|
2322
|
+
h(
|
|
2323
|
+
'span',
|
|
2324
|
+
{ style: { fontSize: 11, color: WARN, flex: 1 } },
|
|
2325
|
+
'他自己判断做完了,等你拍板',
|
|
2326
|
+
),
|
|
2327
|
+
h(
|
|
2328
|
+
ConfirmBtn,
|
|
2329
|
+
{
|
|
2330
|
+
label: '确认达成',
|
|
2331
|
+
confirmLabel: '确认归档?',
|
|
2332
|
+
kind: 'primary',
|
|
2333
|
+
onConfirm: () => act('goalConfirm', bot),
|
|
2334
|
+
disabled: busy,
|
|
2335
|
+
},
|
|
2336
|
+
),
|
|
2337
|
+
h(
|
|
2338
|
+
Btn,
|
|
2339
|
+
{
|
|
2340
|
+
kind: 'danger',
|
|
2341
|
+
onClick: () =>
|
|
2342
|
+
act.onOpenForm
|
|
2343
|
+
? act.onOpenForm({ kind: 'reject', botId: bot.id, botName: bot.name })
|
|
2344
|
+
: null,
|
|
2345
|
+
disabled: busy,
|
|
2346
|
+
},
|
|
2347
|
+
'驳回',
|
|
2348
|
+
),
|
|
2349
|
+
)
|
|
2350
|
+
: null,
|
|
2351
|
+
|
|
2352
|
+
// 卡住了
|
|
2353
|
+
isBlocked
|
|
2354
|
+
? h(
|
|
2355
|
+
'div',
|
|
2356
|
+
{
|
|
2357
|
+
style: {
|
|
2358
|
+
fontSize: 11,
|
|
2359
|
+
color: ERR,
|
|
2360
|
+
marginTop: 8,
|
|
2361
|
+
paddingTop: 8,
|
|
2362
|
+
borderTop: `1px solid ${BORDER_L1}`,
|
|
2363
|
+
},
|
|
2364
|
+
},
|
|
2365
|
+
'卡住了,需要你介入处理。处理好后在对话框说「解除阻塞」即可。',
|
|
2366
|
+
)
|
|
2367
|
+
: null,
|
|
2368
|
+
);
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
/** ② 接续建议:目标完成后的下一步候选 */
|
|
2372
|
+
function renderProposals(proposals, bot, act, busy) {
|
|
2373
|
+
return h(
|
|
2374
|
+
'div',
|
|
2375
|
+
{ style: { marginBottom: 10 } },
|
|
2376
|
+
h(SectionTitle, { hint: '他不会自己开张' }, '建议接下来'),
|
|
2377
|
+
h(
|
|
2378
|
+
'div',
|
|
2379
|
+
{
|
|
2380
|
+
style: {
|
|
2381
|
+
padding: '8px 10px',
|
|
2382
|
+
borderRadius: R_SM,
|
|
2383
|
+
background: BG_L2,
|
|
2384
|
+
border: `1px solid ${BORDER_L1}`,
|
|
2385
|
+
},
|
|
2386
|
+
},
|
|
2387
|
+
...proposals.map((p, i) =>
|
|
2388
|
+
h(
|
|
2389
|
+
'div',
|
|
2390
|
+
{
|
|
2391
|
+
key: p.id,
|
|
2392
|
+
style: {
|
|
2393
|
+
display: 'flex',
|
|
2394
|
+
alignItems: 'center',
|
|
2395
|
+
gap: 8,
|
|
2396
|
+
paddingTop: i === 0 ? 0 : 6,
|
|
2397
|
+
marginTop: i === 0 ? 0 : 6,
|
|
2398
|
+
borderTop: i === 0 ? 'none' : `1px solid ${BORDER_L1}`,
|
|
2399
|
+
},
|
|
2400
|
+
},
|
|
2401
|
+
h(
|
|
2402
|
+
'span',
|
|
2403
|
+
{ style: { fontSize: 11, color: LABEL_1, flex: 1 } },
|
|
2404
|
+
p.description,
|
|
2405
|
+
),
|
|
2406
|
+
h(
|
|
2407
|
+
'span',
|
|
2408
|
+
{ style: { fontSize: 10, color: LABEL_3, flexShrink: 0 } },
|
|
2409
|
+
goalTypeText(p.type),
|
|
2410
|
+
),
|
|
2411
|
+
h(
|
|
2412
|
+
Btn,
|
|
2413
|
+
{
|
|
2414
|
+
onClick: () =>
|
|
2415
|
+
act('goalAccept', { ...bot, goalId: p.id }),
|
|
2416
|
+
disabled: busy,
|
|
2417
|
+
},
|
|
2418
|
+
'接受',
|
|
2419
|
+
),
|
|
2420
|
+
),
|
|
2421
|
+
),
|
|
2422
|
+
h(
|
|
2423
|
+
'div',
|
|
2424
|
+
{ style: { marginTop: 8, textAlign: 'right' } },
|
|
2425
|
+
h(
|
|
2426
|
+
Btn,
|
|
2427
|
+
{ onClick: () => act('goalDismiss', bot), disabled: busy },
|
|
2428
|
+
'都不用',
|
|
2429
|
+
),
|
|
2430
|
+
),
|
|
2431
|
+
),
|
|
2432
|
+
);
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
/** ③ 最近运行 */
|
|
2436
|
+
function renderRuns(runs) {
|
|
2437
|
+
return h(
|
|
2438
|
+
'div',
|
|
2439
|
+
{ style: { marginBottom: 10 } },
|
|
2440
|
+
h(SectionTitle, { hint: '全量记录,不设上限' }, '最近运行'),
|
|
2441
|
+
h(
|
|
2442
|
+
'div',
|
|
2443
|
+
{ style: { display: 'flex', flexDirection: 'column', gap: 3 } },
|
|
2444
|
+
...runs.slice(0, 3).map((r) => {
|
|
2445
|
+
const blocked = (r.steps || []).filter((s) => s.blocked).length;
|
|
2446
|
+
const effect = r.goalEffect;
|
|
2447
|
+
return h(
|
|
2448
|
+
'div',
|
|
2449
|
+
{
|
|
2450
|
+
key: r.id,
|
|
2451
|
+
style: {
|
|
2452
|
+
display: 'flex',
|
|
2453
|
+
alignItems: 'center',
|
|
2454
|
+
gap: 6,
|
|
2455
|
+
fontSize: 11,
|
|
2456
|
+
lineHeight: '18px',
|
|
2457
|
+
},
|
|
2458
|
+
},
|
|
2459
|
+
h('span', {
|
|
2460
|
+
style: {
|
|
2461
|
+
width: 5,
|
|
2462
|
+
height: 5,
|
|
2463
|
+
borderRadius: 999,
|
|
2464
|
+
background: statusColor(r.status),
|
|
2465
|
+
flexShrink: 0,
|
|
2466
|
+
},
|
|
2467
|
+
}),
|
|
2468
|
+
h(
|
|
2469
|
+
'span',
|
|
2470
|
+
{ style: { color: LABEL_3, flexShrink: 0 } },
|
|
2471
|
+
r.startedAt.slice(5, 16).replace('T', ' '),
|
|
2472
|
+
),
|
|
2473
|
+
h(
|
|
2474
|
+
'span',
|
|
2475
|
+
{ style: { color: statusColor(r.status), flexShrink: 0 } },
|
|
2476
|
+
statusText(r.status),
|
|
2477
|
+
),
|
|
2478
|
+
r.dryRun
|
|
2479
|
+
? h(
|
|
2480
|
+
'span',
|
|
2481
|
+
{ style: { color: LABEL_3, flexShrink: 0 } },
|
|
2482
|
+
'演习',
|
|
2483
|
+
)
|
|
2484
|
+
: null,
|
|
2485
|
+
blocked
|
|
2486
|
+
? h(
|
|
2487
|
+
'span',
|
|
2488
|
+
{ style: { color: WARN, flexShrink: 0 } },
|
|
2489
|
+
`${blocked} 步拦截`,
|
|
2490
|
+
)
|
|
2491
|
+
: null,
|
|
2492
|
+
effect === 'pending'
|
|
2493
|
+
? h('span', { style: { color: WARN, flexShrink: 0 } }, '待确认')
|
|
2494
|
+
: null,
|
|
2495
|
+
effect === 'cycle'
|
|
2496
|
+
? h('span', { style: { color: BRAND, flexShrink: 0 } }, '轮次+1')
|
|
2497
|
+
: null,
|
|
2498
|
+
h(
|
|
2499
|
+
'span',
|
|
2500
|
+
{ style: { color: LABEL_3, marginLeft: 'auto', flexShrink: 0 } },
|
|
2501
|
+
fmtCost(r.usage),
|
|
2502
|
+
),
|
|
2503
|
+
);
|
|
2504
|
+
}),
|
|
2505
|
+
),
|
|
2506
|
+
);
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
/** ④ 他记得:记忆事实 */
|
|
2510
|
+
function renderMemory(semantic) {
|
|
2511
|
+
const items = [...(semantic.preferences || []), ...(semantic.facts || [])].slice(0, 4);
|
|
2512
|
+
if (!items.length) return null;
|
|
2513
|
+
return h(
|
|
2514
|
+
'div',
|
|
2515
|
+
{ style: { marginBottom: 10 } },
|
|
2516
|
+
h(SectionTitle, { hint: '会被带进每一轮' }, '他记得'),
|
|
2517
|
+
h(
|
|
2518
|
+
'div',
|
|
2519
|
+
{ style: { display: 'flex', flexDirection: 'column', gap: 2 } },
|
|
2520
|
+
...items.map((f, i) =>
|
|
2521
|
+
h(
|
|
2522
|
+
'div',
|
|
2523
|
+
{
|
|
2524
|
+
key: i,
|
|
2525
|
+
style: {
|
|
2526
|
+
fontSize: 11,
|
|
2527
|
+
color: LABEL_2,
|
|
2528
|
+
lineHeight: 1.6,
|
|
2529
|
+
overflow: 'hidden',
|
|
2530
|
+
textOverflow: 'ellipsis',
|
|
2531
|
+
whiteSpace: 'nowrap',
|
|
2532
|
+
},
|
|
2533
|
+
},
|
|
2534
|
+
`· ${f}`,
|
|
2535
|
+
),
|
|
2536
|
+
),
|
|
2537
|
+
),
|
|
2538
|
+
);
|
|
2539
|
+
}
|
|
2540
|
+
|
|
2541
|
+
/** ⑤ 例行流程 */
|
|
2542
|
+
function renderRoutines(routines) {
|
|
2543
|
+
return h(
|
|
2544
|
+
'div',
|
|
2545
|
+
{ style: { marginBottom: 10 } },
|
|
2546
|
+
h(SectionTitle, { hint: '目标完成也不中断' }, '例行流程'),
|
|
2547
|
+
h(
|
|
2548
|
+
'div',
|
|
2549
|
+
{ style: { display: 'flex', flexDirection: 'column', gap: 3 } },
|
|
2550
|
+
...routines.slice(0, 3).map((r) =>
|
|
2551
|
+
h(
|
|
2552
|
+
'div',
|
|
2553
|
+
{
|
|
2554
|
+
key: r.id,
|
|
2555
|
+
style: {
|
|
2556
|
+
display: 'flex',
|
|
2557
|
+
alignItems: 'center',
|
|
2558
|
+
gap: 6,
|
|
2559
|
+
fontSize: 11,
|
|
2560
|
+
},
|
|
2561
|
+
},
|
|
2562
|
+
h('span', { style: { color: LABEL_1 } }, r.name || r.id),
|
|
2563
|
+
r.trigger
|
|
2564
|
+
? h(
|
|
2565
|
+
'span',
|
|
2566
|
+
{ style: { color: LABEL_3, marginLeft: 'auto' } },
|
|
2567
|
+
r.trigger,
|
|
2568
|
+
)
|
|
2569
|
+
: null,
|
|
2570
|
+
),
|
|
2571
|
+
),
|
|
2572
|
+
),
|
|
2573
|
+
);
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
// ---------- 主面板 ----------
|
|
2577
|
+
function BotDock() {
|
|
2578
|
+
try {
|
|
2579
|
+
const [open, setOpen] = React.useState(false);
|
|
2580
|
+
const [data, setData] = React.useState(null);
|
|
2581
|
+
const [err, setErr] = React.useState('');
|
|
2582
|
+
const [busy, setBusy] = React.useState(false);
|
|
2583
|
+
const [notice, setNotice] = React.useState('');
|
|
2584
|
+
const [detailId, setDetailId] = React.useState(null);
|
|
2585
|
+
const [detail, setDetail] = React.useState(null);
|
|
2586
|
+
const [headHover, setHeadHover] = React.useState(false);
|
|
2587
|
+
const [expanded, setExpanded] = React.useState(false);
|
|
2588
|
+
/** 内嵌表单:{ kind, botId, ... } —— 取代 window.prompt */
|
|
2589
|
+
const [form, setForm] = React.useState(null);
|
|
2590
|
+
/** 哪个 Bot 的「更多」是展开的(A 方案:第三层折叠) */
|
|
2591
|
+
const [moreOpen, setMoreOpen] = React.useState(null);
|
|
2592
|
+
/** 哪个 Bot 的「配置与危险操作」展开(与「他知道什么」分属两层) */
|
|
2593
|
+
const [dangerOpen, setDangerOpen] = React.useState(null);
|
|
2594
|
+
/** 当前视图:bots 列表 / settings 设置页 */
|
|
2595
|
+
const [view, setView] = React.useState('bots');
|
|
2596
|
+
|
|
2597
|
+
const refresh = React.useCallback(async () => {
|
|
2598
|
+
setBusy(true);
|
|
2599
|
+
try {
|
|
2600
|
+
const r = await fetch('/_dsh-bot/overview', {
|
|
2601
|
+
headers: { accept: 'application/json' },
|
|
2602
|
+
});
|
|
2603
|
+
const j = await r.json();
|
|
2604
|
+
if (j?.ok) {
|
|
2605
|
+
setData(j.value);
|
|
2606
|
+
// ⚠️ 这里曾经 setErr(''),导致刚显示的错误被刷新清空。
|
|
2607
|
+
// 用户看到红框"闪了一下就没了"——比不显示还糟。
|
|
2608
|
+
// 错误只能由用户主动操作(关掉 / 再次操作)清除,
|
|
2609
|
+
// 不能被一个后台刷新顺手抹掉。
|
|
2610
|
+
} else {
|
|
2611
|
+
setErr(j?.message || '读取失败');
|
|
2612
|
+
}
|
|
2613
|
+
} catch (e) {
|
|
2614
|
+
setErr(String(e?.message || e));
|
|
2615
|
+
} finally {
|
|
2616
|
+
setBusy(false);
|
|
2617
|
+
}
|
|
2618
|
+
}, []);
|
|
2619
|
+
|
|
2620
|
+
React.useEffect(() => {
|
|
2621
|
+
if (open && !data) void refresh();
|
|
2622
|
+
}, [open, data, refresh]);
|
|
2623
|
+
|
|
2624
|
+
// 展开某个 Bot 的详情时,拉一次完整数据
|
|
2625
|
+
// (目标 / 运行史 / 记忆 / 例行流程 / 快照都在这个接口里)
|
|
2626
|
+
const loadDetail = React.useCallback(async (botId) => {
|
|
2627
|
+
if (!botId) {
|
|
2628
|
+
setDetail(null);
|
|
2629
|
+
return;
|
|
2630
|
+
}
|
|
2631
|
+
try {
|
|
2632
|
+
const r = await fetch(
|
|
2633
|
+
`/_dsh-bot/bot?botId=${encodeURIComponent(botId)}`,
|
|
2634
|
+
{ headers: { accept: 'application/json' } },
|
|
2635
|
+
);
|
|
2636
|
+
const j = await r.json();
|
|
2637
|
+
if (j?.ok) setDetail(j.value);
|
|
2638
|
+
else setErr(j?.message || '读取详情失败');
|
|
2639
|
+
} catch (e) {
|
|
2640
|
+
setErr(String(e?.message || e));
|
|
2641
|
+
}
|
|
2642
|
+
}, []);
|
|
2643
|
+
|
|
2644
|
+
React.useEffect(() => {
|
|
2645
|
+
void loadDetail(detailId);
|
|
2646
|
+
}, [detailId, loadDetail]);
|
|
2647
|
+
|
|
2648
|
+
// ⭐ 运行中实时刷新:长任务(工具循环)进行时每 3s 拉一次详情,
|
|
2649
|
+
// 结束即停。轮询而不是 WebSocket——够用,不引入新依赖。
|
|
2650
|
+
React.useEffect(() => {
|
|
2651
|
+
if (!busy || !detailId) return;
|
|
2652
|
+
const t = setInterval(() => {
|
|
2653
|
+
void loadDetail(detailId);
|
|
2654
|
+
}, 3000);
|
|
2655
|
+
return () => clearInterval(t);
|
|
2656
|
+
}, [busy, detailId, loadDetail]);
|
|
2657
|
+
|
|
2658
|
+
const act = React.useCallback(
|
|
2659
|
+
async (kind, bot, extra) => {
|
|
2660
|
+
setBusy(true);
|
|
2661
|
+
setErr('');
|
|
2662
|
+
setNotice('');
|
|
2663
|
+
try {
|
|
2664
|
+
const post = async (path, body) => {
|
|
2665
|
+
const r = await fetch('/_dsh-bot' + path, {
|
|
2666
|
+
method: 'POST',
|
|
2667
|
+
headers: { 'content-type': 'application/json' },
|
|
2668
|
+
body: JSON.stringify(body),
|
|
2669
|
+
});
|
|
2670
|
+
return r.json();
|
|
2671
|
+
};
|
|
2672
|
+
|
|
2673
|
+
let j;
|
|
2674
|
+
if (kind === 'dry') {
|
|
2675
|
+
j = await post('/dry-run', { botId: bot.id });
|
|
2676
|
+
if (j?.ok) {
|
|
2677
|
+
const st = j.value?.status;
|
|
2678
|
+
const bl = (j.value.steps || []).filter((s) => s.blocked).length;
|
|
2679
|
+
const errMsg = j.value?.error?.message;
|
|
2680
|
+
|
|
2681
|
+
if (st === 'failed') {
|
|
2682
|
+
setErr(`演习失败:${errMsg ?? '未知原因'}`);
|
|
2683
|
+
} else {
|
|
2684
|
+
setNotice(
|
|
2685
|
+
`空跑完成:${bl} 步会被拦截,未产生真实影响`,
|
|
2686
|
+
);
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
} else if (kind === 'run') {
|
|
2690
|
+
// ⚠️ 不用 window.confirm:Electron 里 confirm() 是空实现,
|
|
2691
|
+
// 永远返回 false,按钮会变成"点了没反应"。
|
|
2692
|
+
// 真实执行的二次确认由按钮的 armed 态承担(见下方 UI)。
|
|
2693
|
+
j = await post('/run', { botId: bot.id, confirm: true });
|
|
2694
|
+
if (j?.ok) {
|
|
2695
|
+
// ⚠️ 这里曾经把失败也当成成功提示显示(绿底 + "执行完成")。
|
|
2696
|
+
// API 返回 ok:true 只代表"请求执行完了",
|
|
2697
|
+
// 运行本身可能是 failed —— 必须分开处理。
|
|
2698
|
+
const st = j.value?.status;
|
|
2699
|
+
const errMsg = j.value?.error?.message;
|
|
2700
|
+
|
|
2701
|
+
if (st === 'failed' || st === 'budget_exceeded') {
|
|
2702
|
+
setErr(
|
|
2703
|
+
`执行失败(${st})` + (errMsg ? `:${errMsg}` : ''),
|
|
2704
|
+
);
|
|
2705
|
+
} else if (st === 'partial') {
|
|
2706
|
+
const bl = (j.value.steps || []).filter(
|
|
2707
|
+
(s) => s.blocked,
|
|
2708
|
+
).length;
|
|
2709
|
+
setNotice(
|
|
2710
|
+
`执行部分完成${bl ? `,${bl} 步被跳过` : ''}`,
|
|
2711
|
+
);
|
|
2712
|
+
} else {
|
|
2713
|
+
setNotice(`执行完成`);
|
|
2714
|
+
}
|
|
2715
|
+
} else if (j?.code === 'PRECHECK_FAILED') {
|
|
2716
|
+
// 执行前检查没过:直接把原因显示出来,
|
|
2717
|
+
// 不要让用户看到含糊的"执行失败"
|
|
2718
|
+
setErr(j.message);
|
|
2719
|
+
}
|
|
2720
|
+
} else if (kind === 'snapshot') {
|
|
2721
|
+
j = await post('/snapshot', { botId: bot.id });
|
|
2722
|
+
if (j?.ok) setNotice(`已创建快照(${j.value.fileCount} 个文件)`);
|
|
2723
|
+
} else if (kind === 'rollback') {
|
|
2724
|
+
j = await post('/rollback', { botId: bot.id, confirm: true });
|
|
2725
|
+
if (j?.ok) {
|
|
2726
|
+
setNotice(
|
|
2727
|
+
`已回滚:恢复 ${j.value.restored.length} 个,删除 ${j.value.removed.length} 个`,
|
|
2728
|
+
);
|
|
2729
|
+
}
|
|
2730
|
+
} else if (kind === 'goalConfirm') {
|
|
2731
|
+
j = await post('/goal/confirm', {
|
|
2732
|
+
botId: bot.id,
|
|
2733
|
+
confirm: true,
|
|
2734
|
+
note: extra?.note,
|
|
2735
|
+
});
|
|
2736
|
+
if (j?.ok) setNotice('已确认达成并归档,下面看看建议的下一步');
|
|
2737
|
+
} else if (kind === 'goalReject') {
|
|
2738
|
+
j = await post('/goal/reject', {
|
|
2739
|
+
botId: bot.id,
|
|
2740
|
+
note: extra?.note ?? '还没做完',
|
|
2741
|
+
});
|
|
2742
|
+
if (j?.ok) setNotice('已驳回,目标回到进行中');
|
|
2743
|
+
} else if (kind === 'goalUnblock') {
|
|
2744
|
+
j = await post('/goal/unblock', {
|
|
2745
|
+
botId: bot.id,
|
|
2746
|
+
note: extra?.note,
|
|
2747
|
+
});
|
|
2748
|
+
if (j?.ok) setNotice('已解除阻塞,目标回到进行中');
|
|
2749
|
+
} else if (kind === 'goalGiveup') {
|
|
2750
|
+
j = await post('/goal/giveup', {
|
|
2751
|
+
botId: bot.id,
|
|
2752
|
+
note: extra?.note,
|
|
2753
|
+
});
|
|
2754
|
+
if (j?.ok) setNotice('已放弃并归档');
|
|
2755
|
+
} else if (kind === 'goalAccept') {
|
|
2756
|
+
j = await post('/goal/accept', { botId: bot.id, goalId: bot.goalId });
|
|
2757
|
+
if (j?.ok) setNotice('已接受,Bot 开始守护新目标');
|
|
2758
|
+
} else if (kind === 'goalDismiss') {
|
|
2759
|
+
j = await post('/goal/dismiss', { botId: bot.id });
|
|
2760
|
+
if (j?.ok) setNotice('已忽略建议');
|
|
2761
|
+
} else if (kind === 'goalSet') {
|
|
2762
|
+
// extra = { description, goalType }
|
|
2763
|
+
j = await post('/goal/set', {
|
|
2764
|
+
botId: bot.id,
|
|
2765
|
+
description: extra.description,
|
|
2766
|
+
goalType: extra.goalType,
|
|
2767
|
+
});
|
|
2768
|
+
if (j?.ok) setNotice('目标已设定');
|
|
2769
|
+
} else if (kind === 'botCreate') {
|
|
2770
|
+
j = await post('/bot/create', {
|
|
2771
|
+
name: extra.name,
|
|
2772
|
+
role: extra.role,
|
|
2773
|
+
goal: extra.goal,
|
|
2774
|
+
goalType: extra.goalType,
|
|
2775
|
+
// ⚠️ 之前漏了这一行,导致「新建时填的节奏」被静默丢弃,
|
|
2776
|
+
// 用户以为设好了,其实 goal.json 里根本没有 cadence。
|
|
2777
|
+
cadence: extra.cadence,
|
|
2778
|
+
});
|
|
2779
|
+
if (j?.ok) setNotice(`已创建「${extra.name}」`);
|
|
2780
|
+
} else if (kind === 'botModel') {
|
|
2781
|
+
j = await post('/bot/model', {
|
|
2782
|
+
botId: bot.id,
|
|
2783
|
+
provider: extra.provider,
|
|
2784
|
+
model: extra.model,
|
|
2785
|
+
});
|
|
2786
|
+
if (j?.ok) setNotice('模型已更新');
|
|
2787
|
+
} else if (kind === 'clearRuns') {
|
|
2788
|
+
j = await post('/runs/clear', {
|
|
2789
|
+
botId: bot.id,
|
|
2790
|
+
confirm: true,
|
|
2791
|
+
});
|
|
2792
|
+
if (j?.ok) {
|
|
2793
|
+
setNotice(`已清空 ${j.value.removed} 条运行记录`);
|
|
2794
|
+
}
|
|
2795
|
+
} else if (kind === 'botDelete') {
|
|
2796
|
+
j = await post('/bot/delete', { botId: bot.id, confirm: true });
|
|
2797
|
+
if (j?.ok) {
|
|
2798
|
+
setNotice(`已删除「${bot.name}」`);
|
|
2799
|
+
setDetailId(null);
|
|
2800
|
+
setDetail(null);
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
if (j && !j.ok) setErr(j.message || '操作失败');
|
|
2804
|
+
await refresh();
|
|
2805
|
+
// 目标操作会改变详情区内容,一并刷新
|
|
2806
|
+
if (detailId) await loadDetail(detailId);
|
|
2807
|
+
} catch (e) {
|
|
2808
|
+
setErr(String(e?.message || e));
|
|
2809
|
+
} finally {
|
|
2810
|
+
setBusy(false);
|
|
2811
|
+
}
|
|
2812
|
+
},
|
|
2813
|
+
[refresh, loadDetail, detailId],
|
|
2814
|
+
);
|
|
2815
|
+
|
|
2816
|
+
// 把「打开表单」挂到 act 上,让分区渲染函数能调用
|
|
2817
|
+
act.onOpenForm = (f) => setForm(f);
|
|
2818
|
+
act.onOpenModel = (f) => setForm({ kind: 'model', ...f });
|
|
2819
|
+
|
|
2820
|
+
const bots = data?.bots ?? [];
|
|
2821
|
+
const stats = data?.stats;
|
|
2822
|
+
|
|
2823
|
+
// 卡片样式:贴近官方的 dock 卡片
|
|
2824
|
+
// · radius-md(官方用 md,不是 lg)
|
|
2825
|
+
// · 左侧内边距大、右侧小(给图标按钮留位)
|
|
2826
|
+
const card = {
|
|
2827
|
+
fontFamily: FONT,
|
|
2828
|
+
border: `1px solid ${BORDER_L1}`,
|
|
2829
|
+
borderRadius: R_MD,
|
|
2830
|
+
background: BG_L2,
|
|
2831
|
+
overflow: 'hidden',
|
|
2832
|
+
};
|
|
2833
|
+
|
|
2834
|
+
// ========== 收起态 ==========
|
|
2835
|
+
if (!open) {
|
|
2836
|
+
return h(
|
|
2837
|
+
'div',
|
|
2838
|
+
{ className: `${NS}-dock` },
|
|
2839
|
+
h(
|
|
2840
|
+
'div',
|
|
2841
|
+
{
|
|
2842
|
+
className: `${NS}-dock-inner`,
|
|
2843
|
+
style: {
|
|
2844
|
+
...card,
|
|
2845
|
+
display: 'flex',
|
|
2846
|
+
alignItems: 'center',
|
|
2847
|
+
justifyContent: 'space-between',
|
|
2848
|
+
padding: '7px 12px',
|
|
2849
|
+
cursor: 'pointer',
|
|
2850
|
+
transition: 'background .15s ease',
|
|
2851
|
+
background: headHover ? HOVER_BG : BG_L2,
|
|
2852
|
+
},
|
|
2853
|
+
onClick: () => setOpen(true),
|
|
2854
|
+
onMouseEnter: () => setHeadHover(true),
|
|
2855
|
+
onMouseLeave: () => setHeadHover(false),
|
|
2856
|
+
},
|
|
2857
|
+
h(
|
|
2858
|
+
'div',
|
|
2859
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8 } },
|
|
2860
|
+
h(IconBot, { size: 16, color: BRAND }),
|
|
2861
|
+
h(
|
|
2862
|
+
'span',
|
|
2863
|
+
{ style: { fontSize: 13, fontWeight: 500, color: LABEL_1 } },
|
|
2864
|
+
'Bot',
|
|
2865
|
+
),
|
|
2866
|
+
stats ? h(Badge, { tone: 'brand' }, `${stats.botCount} 个`) : null,
|
|
2867
|
+
),
|
|
2868
|
+
h(
|
|
2869
|
+
'div',
|
|
2870
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8 } },
|
|
2871
|
+
stats
|
|
2872
|
+
? h(
|
|
2873
|
+
'span',
|
|
2874
|
+
{ style: { fontSize: 12, color: LABEL_3 } },
|
|
2875
|
+
`今日 ${fmtCny(stats.todayCostCny)}`,
|
|
2876
|
+
)
|
|
2877
|
+
: null,
|
|
2878
|
+
h(IconChevron, { size: 13 }),
|
|
2879
|
+
),
|
|
2880
|
+
),
|
|
2881
|
+
);
|
|
2882
|
+
}
|
|
2883
|
+
|
|
2884
|
+
// ========== 展开态 ==========
|
|
2885
|
+
//
|
|
2886
|
+
// 布局:头部固定 → 主体滚动
|
|
2887
|
+
//
|
|
2888
|
+
// ⚠️ 之前所有内容平铺在一层,详情区一展开就把面板撑得很高,
|
|
2889
|
+
// 超出可视区域后既看不到下面、也没法滚(因为根本没有滚动容器)。
|
|
2890
|
+
// 现在用 flex 列布局 + 中部 overflow-y:auto 解决。
|
|
2891
|
+
return h(
|
|
2892
|
+
'div',
|
|
2893
|
+
{ className: `${NS}-dock` },
|
|
2894
|
+
h(
|
|
2895
|
+
'div',
|
|
2896
|
+
{
|
|
2897
|
+
className: `${NS}-dock-inner ${NS}-fade ${NS}-expanded`,
|
|
2898
|
+
style: card,
|
|
2899
|
+
},
|
|
2900
|
+
// ---------- 头部(固定) ----------
|
|
2901
|
+
h(
|
|
2902
|
+
'div',
|
|
2903
|
+
{
|
|
2904
|
+
style: {
|
|
2905
|
+
display: 'flex',
|
|
2906
|
+
alignItems: 'center',
|
|
2907
|
+
justifyContent: 'space-between',
|
|
2908
|
+
padding: '8px 12px',
|
|
2909
|
+
borderBottom: `1px solid ${BORDER_L1}`,
|
|
2910
|
+
},
|
|
2911
|
+
},
|
|
2912
|
+
h(
|
|
2913
|
+
'div',
|
|
2914
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8 } },
|
|
2915
|
+
h(IconBot, { size: 16, color: BRAND }),
|
|
2916
|
+
h(
|
|
2917
|
+
'span',
|
|
2918
|
+
{ style: { fontSize: 13, fontWeight: 600, color: LABEL_1 } },
|
|
2919
|
+
'Bot 面板',
|
|
2920
|
+
),
|
|
2921
|
+
stats ? h(Badge, { tone: 'brand' }, `${stats.botCount} 个`) : null,
|
|
2922
|
+
// ⭐ 需要你拍板的数量(否则"他在等你"这件事永远看不见)
|
|
2923
|
+
(() => {
|
|
2924
|
+
const need = bots.filter(
|
|
2925
|
+
(x) => x.pendingConfirmCount || x.blocked,
|
|
2926
|
+
).length;
|
|
2927
|
+
return need > 0
|
|
2928
|
+
? h(Badge, { tone: 'warn' }, `${need} 项等你`)
|
|
2929
|
+
: null;
|
|
2930
|
+
})(),
|
|
2931
|
+
// ⭐ 对话介入的回声:最近 48h 新接手的 Bot,提示用户回来看
|
|
2932
|
+
(() => {
|
|
2933
|
+
const cnt = data?.recentAdoptions?.length ?? 0;
|
|
2934
|
+
return cnt > 0
|
|
2935
|
+
? h(Badge, { tone: 'ok' }, `新接手 ${cnt}`)
|
|
2936
|
+
: null;
|
|
2937
|
+
})(),
|
|
2938
|
+
),
|
|
2939
|
+
h(
|
|
2940
|
+
'div',
|
|
2941
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 6 } },
|
|
2942
|
+
stats
|
|
2943
|
+
? h(
|
|
2944
|
+
'span',
|
|
2945
|
+
{ style: { fontSize: 12, color: LABEL_3, marginRight: 2 } },
|
|
2946
|
+
`今日 ${fmtCny(stats.todayCostCny)}`,
|
|
2947
|
+
)
|
|
2948
|
+
: null,
|
|
2949
|
+
h(
|
|
2950
|
+
'button',
|
|
2951
|
+
{
|
|
2952
|
+
className: `${NS}-row-btn`,
|
|
2953
|
+
onClick: () => setForm({ kind: 'createBot' }),
|
|
2954
|
+
title: '新建 Bot',
|
|
2955
|
+
type: 'button',
|
|
2956
|
+
},
|
|
2957
|
+
'+',
|
|
2958
|
+
),
|
|
2959
|
+
h(
|
|
2960
|
+
'button',
|
|
2961
|
+
{
|
|
2962
|
+
className: `${NS}-row-btn`,
|
|
2963
|
+
onClick: refresh,
|
|
2964
|
+
disabled: busy,
|
|
2965
|
+
title: '刷新',
|
|
2966
|
+
type: 'button',
|
|
2967
|
+
style: { opacity: busy ? 0.5 : 1 },
|
|
2968
|
+
},
|
|
2969
|
+
h(IconRefresh, { size: 13, spinning: busy }),
|
|
2970
|
+
),
|
|
2971
|
+
h(
|
|
2972
|
+
'button',
|
|
2973
|
+
{
|
|
2974
|
+
className: `${NS}-row-btn`,
|
|
2975
|
+
onClick: () => setOpen(false),
|
|
2976
|
+
title: '收起',
|
|
2977
|
+
type: 'button',
|
|
2978
|
+
},
|
|
2979
|
+
h(IconCollapse, { size: 13 }),
|
|
2980
|
+
),
|
|
2981
|
+
),
|
|
2982
|
+
),
|
|
2983
|
+
|
|
2984
|
+
// ---------- 主体(可滚动) ----------
|
|
2985
|
+
h(
|
|
2986
|
+
'div',
|
|
2987
|
+
{ className: `${NS}-scroll` },
|
|
2988
|
+
|
|
2989
|
+
// ===== 设置视图 =====
|
|
2990
|
+
// 设置页是"另一个房间",打开时盖住 Bot 列表,
|
|
2991
|
+
// 不再往下堆内容(否则又变成"挤成一坨")。
|
|
2992
|
+
view === 'settings'
|
|
2993
|
+
? h(SettingsView, {
|
|
2994
|
+
busy,
|
|
2995
|
+
onClose: () => setView('bots'),
|
|
2996
|
+
onLoad: async () => {
|
|
2997
|
+
const r = await fetch('/_dsh-bot/settings', {
|
|
2998
|
+
headers: { accept: 'application/json' },
|
|
2999
|
+
});
|
|
3000
|
+
const j = await r.json();
|
|
3001
|
+
if (!j?.ok) throw new Error(j?.message || '读取失败');
|
|
3002
|
+
return j.value.effective;
|
|
3003
|
+
},
|
|
3004
|
+
onSave: async (next) => {
|
|
3005
|
+
try {
|
|
3006
|
+
await fetch('/_dsh-bot/settings', {
|
|
3007
|
+
method: 'POST',
|
|
3008
|
+
headers: { 'content-type': 'application/json' },
|
|
3009
|
+
body: JSON.stringify({ settings: next }),
|
|
3010
|
+
});
|
|
3011
|
+
} catch {
|
|
3012
|
+
/* 保存失败下次再试 */
|
|
3013
|
+
}
|
|
3014
|
+
},
|
|
3015
|
+
onClearRuns: () => {
|
|
3016
|
+
const target = detailId
|
|
3017
|
+
? bots.find((x) => x.id === detailId)
|
|
3018
|
+
: bots[0];
|
|
3019
|
+
if (!target) return;
|
|
3020
|
+
void act('clearRuns', target);
|
|
3021
|
+
},
|
|
3022
|
+
})
|
|
3023
|
+
: null,
|
|
3024
|
+
|
|
3025
|
+
// ===== Bot 列表视图 =====
|
|
3026
|
+
// 内嵌表单(取代 prompt)—— 必须放在详情区之上,
|
|
3027
|
+
// 因为它是"当前正在进行的操作",优先级高于展示信息
|
|
3028
|
+
form
|
|
3029
|
+
? h(FormPanel, {
|
|
3030
|
+
form,
|
|
3031
|
+
busy,
|
|
3032
|
+
onClose: () => setForm(null),
|
|
3033
|
+
onSubmit: (kind, extra) => {
|
|
3034
|
+
const target = bots.find((x) => x.id === form.botId) || {
|
|
3035
|
+
id: form.botId,
|
|
3036
|
+
name: form.botName,
|
|
3037
|
+
};
|
|
3038
|
+
setForm(null);
|
|
3039
|
+
void act(kind, target, extra);
|
|
3040
|
+
},
|
|
3041
|
+
})
|
|
3042
|
+
: null,
|
|
3043
|
+
|
|
3044
|
+
// 错误提示
|
|
3045
|
+
//
|
|
3046
|
+
// 三个改进(都来自真实反馈):
|
|
3047
|
+
// 1. 不再被 refresh 清空(之前会"闪一下就没了")
|
|
3048
|
+
// 2. 加关闭按钮 —— 何时消失由用户决定
|
|
3049
|
+
// 3. 更显眼:有色背景 + 图标,不再只是一条细边框
|
|
3050
|
+
err
|
|
3051
|
+
? h(
|
|
3052
|
+
'div',
|
|
3053
|
+
{
|
|
3054
|
+
style: {
|
|
3055
|
+
margin: '8px 12px',
|
|
3056
|
+
padding: '8px 10px',
|
|
3057
|
+
borderRadius: R_SM,
|
|
3058
|
+
background: 'var(--dsw-alias-bg-layer-3)',
|
|
3059
|
+
border: `1px solid ${ERR}`,
|
|
3060
|
+
color: ERR,
|
|
3061
|
+
fontSize: 12,
|
|
3062
|
+
lineHeight: 1.6,
|
|
3063
|
+
display: 'flex',
|
|
3064
|
+
alignItems: 'flex-start',
|
|
3065
|
+
gap: 8,
|
|
3066
|
+
},
|
|
3067
|
+
},
|
|
3068
|
+
h(
|
|
3069
|
+
'span',
|
|
3070
|
+
{
|
|
3071
|
+
style: {
|
|
3072
|
+
flexShrink: 0,
|
|
3073
|
+
fontWeight: 700,
|
|
3074
|
+
lineHeight: '18px',
|
|
3075
|
+
},
|
|
3076
|
+
},
|
|
3077
|
+
'!',
|
|
3078
|
+
),
|
|
3079
|
+
h(
|
|
3080
|
+
'span',
|
|
3081
|
+
{ style: { flex: 1, whiteSpace: 'pre-wrap' } },
|
|
3082
|
+
err,
|
|
3083
|
+
),
|
|
3084
|
+
h(
|
|
3085
|
+
'button',
|
|
3086
|
+
{
|
|
3087
|
+
className: `${NS}-row-btn`,
|
|
3088
|
+
onClick: () => setErr(''),
|
|
3089
|
+
title: '关闭',
|
|
3090
|
+
type: 'button',
|
|
3091
|
+
style: { flexShrink: 0 },
|
|
3092
|
+
},
|
|
3093
|
+
'×',
|
|
3094
|
+
),
|
|
3095
|
+
)
|
|
3096
|
+
: null,
|
|
3097
|
+
notice
|
|
3098
|
+
? h(
|
|
3099
|
+
'div',
|
|
3100
|
+
{
|
|
3101
|
+
style: {
|
|
3102
|
+
margin: '8px 12px',
|
|
3103
|
+
padding: '6px 10px',
|
|
3104
|
+
borderRadius: R_SM,
|
|
3105
|
+
background: OK_TER,
|
|
3106
|
+
color: OK,
|
|
3107
|
+
fontSize: 12,
|
|
3108
|
+
},
|
|
3109
|
+
},
|
|
3110
|
+
notice,
|
|
3111
|
+
)
|
|
3112
|
+
: null,
|
|
3113
|
+
|
|
3114
|
+
// 空状态
|
|
3115
|
+
data && bots.length === 0
|
|
3116
|
+
? h(
|
|
3117
|
+
'div',
|
|
3118
|
+
{
|
|
3119
|
+
style: {
|
|
3120
|
+
padding: '20px 14px',
|
|
3121
|
+
textAlign: 'center',
|
|
3122
|
+
},
|
|
3123
|
+
},
|
|
3124
|
+
h(
|
|
3125
|
+
'div',
|
|
3126
|
+
{ style: { fontSize: 13, color: LABEL_1, marginBottom: 4 } },
|
|
3127
|
+
'还没有 Bot',
|
|
3128
|
+
),
|
|
3129
|
+
h(
|
|
3130
|
+
'div',
|
|
3131
|
+
{ style: { fontSize: 11, color: LABEL_3, marginBottom: 12 } },
|
|
3132
|
+
'Bot 是一个常驻的 AI 同事:有自己的目标、记忆和工作区',
|
|
3133
|
+
),
|
|
3134
|
+
h(
|
|
3135
|
+
Btn,
|
|
3136
|
+
{
|
|
3137
|
+
kind: 'primary',
|
|
3138
|
+
onClick: () => setForm({ kind: 'createBot' }),
|
|
3139
|
+
},
|
|
3140
|
+
'新建 Bot',
|
|
3141
|
+
),
|
|
3142
|
+
h(
|
|
3143
|
+
'div',
|
|
3144
|
+
{
|
|
3145
|
+
style: {
|
|
3146
|
+
marginTop: 16,
|
|
3147
|
+
paddingTop: 12,
|
|
3148
|
+
borderTop: `1px solid ${BORDER_L1}`,
|
|
3149
|
+
fontSize: 11,
|
|
3150
|
+
color: LABEL_3,
|
|
3151
|
+
textAlign: 'left',
|
|
3152
|
+
},
|
|
3153
|
+
},
|
|
3154
|
+
h('div', { style: { marginBottom: 6, color: LABEL_2 } },
|
|
3155
|
+
'也可以直接在对话框里说:',
|
|
3156
|
+
),
|
|
3157
|
+
h(
|
|
3158
|
+
'div',
|
|
3159
|
+
{
|
|
3160
|
+
style: {
|
|
3161
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
3162
|
+
lineHeight: 1.9,
|
|
3163
|
+
color: LABEL_2,
|
|
3164
|
+
},
|
|
3165
|
+
},
|
|
3166
|
+
'「创建一个 Bot,叫文件管家,把下载文件夹整理干净」',
|
|
3167
|
+
h('br'),
|
|
3168
|
+
'「创建一个 Bot,叫日报员,每天 9 点汇总 Git 提交」',
|
|
3169
|
+
h('br'),
|
|
3170
|
+
'「创建一个 Bot,叫巡检员,长期盯着构建是否变红」',
|
|
3171
|
+
),
|
|
3172
|
+
),
|
|
3173
|
+
)
|
|
3174
|
+
: null,
|
|
3175
|
+
|
|
3176
|
+
// 列表
|
|
3177
|
+
bots.map((b) => {
|
|
3178
|
+
const isOpen = detailId === b.id;
|
|
3179
|
+
const budgetMax = b.budgetDaily ?? 5;
|
|
3180
|
+
return h(
|
|
3181
|
+
'div',
|
|
3182
|
+
{ key: b.id, style: { borderTop: `1px solid ${BORDER_L1}` } },
|
|
3183
|
+
h(
|
|
3184
|
+
'div',
|
|
3185
|
+
{
|
|
3186
|
+
style: {
|
|
3187
|
+
display: 'flex',
|
|
3188
|
+
alignItems: 'center',
|
|
3189
|
+
gap: 12,
|
|
3190
|
+
padding: '10px 12px',
|
|
3191
|
+
},
|
|
3192
|
+
},
|
|
3193
|
+
// 信息
|
|
3194
|
+
h(
|
|
3195
|
+
'div',
|
|
3196
|
+
{ style: { flex: 1, minWidth: 0 } },
|
|
3197
|
+
h(
|
|
3198
|
+
'div',
|
|
3199
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 7 } },
|
|
3200
|
+
h(
|
|
3201
|
+
'span',
|
|
3202
|
+
{ style: { fontSize: 13, fontWeight: 500, color: LABEL_1 } },
|
|
3203
|
+
b.name,
|
|
3204
|
+
),
|
|
3205
|
+
b.lastRunStatus
|
|
3206
|
+
? h(
|
|
3207
|
+
'span',
|
|
3208
|
+
{
|
|
3209
|
+
style: {
|
|
3210
|
+
fontSize: 10,
|
|
3211
|
+
color: statusColor(b.lastRunStatus),
|
|
3212
|
+
display: 'inline-flex',
|
|
3213
|
+
alignItems: 'center',
|
|
3214
|
+
gap: 3,
|
|
3215
|
+
},
|
|
3216
|
+
},
|
|
3217
|
+
h('span', {
|
|
3218
|
+
style: {
|
|
3219
|
+
width: 5,
|
|
3220
|
+
height: 5,
|
|
3221
|
+
borderRadius: 999,
|
|
3222
|
+
background: statusColor(b.lastRunStatus),
|
|
3223
|
+
},
|
|
3224
|
+
}),
|
|
3225
|
+
statusText(b.lastRunStatus),
|
|
3226
|
+
)
|
|
3227
|
+
: null,
|
|
3228
|
+
),
|
|
3229
|
+
h(
|
|
3230
|
+
'div',
|
|
3231
|
+
{
|
|
3232
|
+
style: {
|
|
3233
|
+
fontSize: 12,
|
|
3234
|
+
color: LABEL_2,
|
|
3235
|
+
marginTop: 3,
|
|
3236
|
+
overflow: 'hidden',
|
|
3237
|
+
textOverflow: 'ellipsis',
|
|
3238
|
+
whiteSpace: 'nowrap',
|
|
3239
|
+
},
|
|
3240
|
+
},
|
|
3241
|
+
b.role,
|
|
3242
|
+
),
|
|
3243
|
+
// ⭐ 「他在守什么」——一行说清,这是 Dots 那一半的可见性
|
|
3244
|
+
b.goalDescription
|
|
3245
|
+
? h(
|
|
3246
|
+
'div',
|
|
3247
|
+
{
|
|
3248
|
+
style: {
|
|
3249
|
+
display: 'flex',
|
|
3250
|
+
alignItems: 'center',
|
|
3251
|
+
gap: 5,
|
|
3252
|
+
fontSize: 11,
|
|
3253
|
+
marginTop: 4,
|
|
3254
|
+
overflow: 'hidden',
|
|
3255
|
+
whiteSpace: 'nowrap',
|
|
3256
|
+
},
|
|
3257
|
+
},
|
|
3258
|
+
h('span', { style: { color: LABEL_3, flexShrink: 0 } }, '他在守'),
|
|
3259
|
+
h(
|
|
3260
|
+
'span',
|
|
3261
|
+
{
|
|
3262
|
+
style: {
|
|
3263
|
+
color: LABEL_1,
|
|
3264
|
+
overflow: 'hidden',
|
|
3265
|
+
textOverflow: 'ellipsis',
|
|
3266
|
+
},
|
|
3267
|
+
},
|
|
3268
|
+
b.goalDescription,
|
|
3269
|
+
),
|
|
3270
|
+
h(
|
|
3271
|
+
'span',
|
|
3272
|
+
{
|
|
3273
|
+
style: {
|
|
3274
|
+
color: goalStatusColor(b.goalStatus),
|
|
3275
|
+
flexShrink: 0,
|
|
3276
|
+
},
|
|
3277
|
+
},
|
|
3278
|
+
`· ${goalStatusText(b.goalStatus)}`,
|
|
3279
|
+
),
|
|
3280
|
+
b.goalType === 'standing'
|
|
3281
|
+
? h(
|
|
3282
|
+
'span',
|
|
3283
|
+
{ style: { color: LABEL_3, flexShrink: 0 } },
|
|
3284
|
+
'· 常驻',
|
|
3285
|
+
)
|
|
3286
|
+
: null,
|
|
3287
|
+
)
|
|
3288
|
+
: h(
|
|
3289
|
+
'div',
|
|
3290
|
+
{ style: { fontSize: 11, color: LABEL_3, marginTop: 4 } },
|
|
3291
|
+
'还没有目标 — 点「设目标」让他知道该守什么',
|
|
3292
|
+
),
|
|
3293
|
+
h(
|
|
3294
|
+
'div',
|
|
3295
|
+
{ style: { fontSize: 11, color: LABEL_3, marginTop: 4 } },
|
|
3296
|
+
`${b.runCount} 次运行 · 上次 ${fmtAgo(b.lastRunAt)}` +
|
|
3297
|
+
(b.memoryCount ? ` · 记得 ${b.memoryCount} 条` : ''),
|
|
3298
|
+
),
|
|
3299
|
+
),
|
|
3300
|
+
// 动作
|
|
3301
|
+
//
|
|
3302
|
+
// 关于「演习」按钮的取舍:
|
|
3303
|
+
// 用户反馈"不明所以,作用不大"。
|
|
3304
|
+
// 原因是:它把"要不要先试"这个决策推给了用户,
|
|
3305
|
+
// 而用户并不知道试一次能看出什么。
|
|
3306
|
+
//
|
|
3307
|
+
// 更好的做法不是删掉它,而是**让它只在有意义时出现**:
|
|
3308
|
+
// · 从没跑过 → 建议先演习
|
|
3309
|
+
// · 已跑成功过 → 收起,避免干扰
|
|
3310
|
+
// 同时把文案从"演习"改成"先跑一遍看看",
|
|
3311
|
+
// 让人一眼知道它做什么。
|
|
3312
|
+
h(
|
|
3313
|
+
'div',
|
|
3314
|
+
{ style: { display: 'flex', gap: 6, flexShrink: 0 } },
|
|
3315
|
+
// 仅在"还没成功跑过"时显示,降低干扰
|
|
3316
|
+
!b.lastRunStatus || b.lastRunStatus === 'failed'
|
|
3317
|
+
? h(
|
|
3318
|
+
Btn,
|
|
3319
|
+
{
|
|
3320
|
+
onClick: () => act('dry', b),
|
|
3321
|
+
disabled: busy,
|
|
3322
|
+
title: '先空跑一遍:会调用模型(有少量花费),但工具不会真实执行',
|
|
3323
|
+
icon: h(IconDryRun, { size: 13 }),
|
|
3324
|
+
},
|
|
3325
|
+
'先看看',
|
|
3326
|
+
)
|
|
3327
|
+
: null,
|
|
3328
|
+
h(
|
|
3329
|
+
Btn,
|
|
3330
|
+
{
|
|
3331
|
+
kind: 'primary',
|
|
3332
|
+
onClick: () => act('run', b),
|
|
3333
|
+
disabled: busy,
|
|
3334
|
+
title: '真实执行(执行前自动打快照)',
|
|
3335
|
+
icon: h(IconRun, { size: 13 }),
|
|
3336
|
+
},
|
|
3337
|
+
'执行',
|
|
3338
|
+
),
|
|
3339
|
+
h(
|
|
3340
|
+
'button',
|
|
3341
|
+
{
|
|
3342
|
+
className: `${NS}-row-btn`,
|
|
3343
|
+
onClick: () => setDetailId(isOpen ? null : b.id),
|
|
3344
|
+
title: isOpen ? '收起详情' : '查看详情',
|
|
3345
|
+
type: 'button',
|
|
3346
|
+
},
|
|
3347
|
+
'详情',
|
|
3348
|
+
h(IconChevron, { size: 12, open: isOpen }),
|
|
3349
|
+
),
|
|
3350
|
+
),
|
|
3351
|
+
),
|
|
3352
|
+
|
|
3353
|
+
// 详情
|
|
3354
|
+
//
|
|
3355
|
+
// A 方案(三层折叠)+ D 方案(状态驱动)
|
|
3356
|
+
//
|
|
3357
|
+
// 第一层:状态摘要 —— 只显示"现在需要你知道的"
|
|
3358
|
+
// 第二层:目标一行(正常时)
|
|
3359
|
+
// 第三层:「更多」折叠 —— 记忆/例行/模型/预算/快照
|
|
3360
|
+
//
|
|
3361
|
+
// 结果:常态高度 ~3 行,不再挤成一坨。
|
|
3362
|
+
isOpen
|
|
3363
|
+
? h(
|
|
3364
|
+
'div',
|
|
3365
|
+
{
|
|
3366
|
+
className: `${NS}-fade`,
|
|
3367
|
+
style: {
|
|
3368
|
+
padding: '10px 12px 12px',
|
|
3369
|
+
borderTop: `1px solid ${BORDER_L1}`,
|
|
3370
|
+
background: BG_L3,
|
|
3371
|
+
},
|
|
3372
|
+
},
|
|
3373
|
+
|
|
3374
|
+
// 没展开详情数据时给个占位,避免闪空
|
|
3375
|
+
!detail || detail.bot?.id !== b.id
|
|
3376
|
+
? h(
|
|
3377
|
+
'div',
|
|
3378
|
+
{
|
|
3379
|
+
style: {
|
|
3380
|
+
fontSize: 11,
|
|
3381
|
+
color: LABEL_3,
|
|
3382
|
+
padding: '4px 0',
|
|
3383
|
+
},
|
|
3384
|
+
},
|
|
3385
|
+
'正在读取…',
|
|
3386
|
+
)
|
|
3387
|
+
: h(
|
|
3388
|
+
'div',
|
|
3389
|
+
null,
|
|
3390
|
+
// ---------- 第一层:状态摘要 ----------
|
|
3391
|
+
renderSituation(b, detail, act, busy),
|
|
3392
|
+
|
|
3393
|
+
// ---------- 第二层:目标一行 ----------
|
|
3394
|
+
detail.goal
|
|
3395
|
+
? h(
|
|
3396
|
+
'div',
|
|
3397
|
+
{
|
|
3398
|
+
style: {
|
|
3399
|
+
display: 'flex',
|
|
3400
|
+
alignItems: 'center',
|
|
3401
|
+
gap: 6,
|
|
3402
|
+
fontSize: 11,
|
|
3403
|
+
color: LABEL_3,
|
|
3404
|
+
marginBottom: 8,
|
|
3405
|
+
minWidth: 0,
|
|
3406
|
+
},
|
|
3407
|
+
},
|
|
3408
|
+
h(
|
|
3409
|
+
'span',
|
|
3410
|
+
{ style: { flexShrink: 0 } },
|
|
3411
|
+
'目标',
|
|
3412
|
+
),
|
|
3413
|
+
h(
|
|
3414
|
+
'span',
|
|
3415
|
+
{
|
|
3416
|
+
style: {
|
|
3417
|
+
color: LABEL_1,
|
|
3418
|
+
overflow: 'hidden',
|
|
3419
|
+
textOverflow: 'ellipsis',
|
|
3420
|
+
whiteSpace: 'nowrap',
|
|
3421
|
+
},
|
|
3422
|
+
},
|
|
3423
|
+
detail.goal.description,
|
|
3424
|
+
),
|
|
3425
|
+
h(
|
|
3426
|
+
'span',
|
|
3427
|
+
{
|
|
3428
|
+
style: {
|
|
3429
|
+
color: goalStatusColor(detail.goal.status),
|
|
3430
|
+
flexShrink: 0,
|
|
3431
|
+
},
|
|
3432
|
+
},
|
|
3433
|
+
goalStatusText(detail.goal.status),
|
|
3434
|
+
),
|
|
3435
|
+
detail.goal.cadence
|
|
3436
|
+
? h(
|
|
3437
|
+
'span',
|
|
3438
|
+
{ style: { flexShrink: 0 } },
|
|
3439
|
+
`· ${detail.goal.cadence}`,
|
|
3440
|
+
)
|
|
3441
|
+
: null,
|
|
3442
|
+
)
|
|
3443
|
+
: null,
|
|
3444
|
+
|
|
3445
|
+
// ---------- 第三层:最近运行(常驻,不再藏进「更多」) ----------
|
|
3446
|
+
//
|
|
3447
|
+
// 用户明确要求:运行记录不该藏在折叠里。
|
|
3448
|
+
// 但也不能全量平铺(那就是"挤成一坨")。
|
|
3449
|
+
// 折中:**只显示最近 2 条**,完整列表和管理进设置页。
|
|
3450
|
+
detail.runs?.length
|
|
3451
|
+
? h(
|
|
3452
|
+
'div',
|
|
3453
|
+
{ style: { marginBottom: 6 } },
|
|
3454
|
+
h(
|
|
3455
|
+
'div',
|
|
3456
|
+
{
|
|
3457
|
+
style: {
|
|
3458
|
+
display: 'flex',
|
|
3459
|
+
alignItems: 'baseline',
|
|
3460
|
+
gap: 6,
|
|
3461
|
+
marginBottom: 4,
|
|
3462
|
+
},
|
|
3463
|
+
},
|
|
3464
|
+
h(
|
|
3465
|
+
'span',
|
|
3466
|
+
{
|
|
3467
|
+
style: {
|
|
3468
|
+
fontSize: 11,
|
|
3469
|
+
fontWeight: 600,
|
|
3470
|
+
color: LABEL_2,
|
|
3471
|
+
},
|
|
3472
|
+
},
|
|
3473
|
+
'最近运行',
|
|
3474
|
+
),
|
|
3475
|
+
h(
|
|
3476
|
+
'span',
|
|
3477
|
+
{ style: { fontSize: 10, color: LABEL_3 } },
|
|
3478
|
+
`共 ${detail.runs.length} 条 · 管理在设置里`,
|
|
3479
|
+
),
|
|
3480
|
+
),
|
|
3481
|
+
...detail.runs.slice(0, 2).map((r) =>
|
|
3482
|
+
h(
|
|
3483
|
+
'div',
|
|
3484
|
+
{
|
|
3485
|
+
key: r.id,
|
|
3486
|
+
style: {
|
|
3487
|
+
display: 'flex',
|
|
3488
|
+
alignItems: 'center',
|
|
3489
|
+
gap: 6,
|
|
3490
|
+
fontSize: 11,
|
|
3491
|
+
lineHeight: '18px',
|
|
3492
|
+
},
|
|
3493
|
+
},
|
|
3494
|
+
h('span', {
|
|
3495
|
+
style: {
|
|
3496
|
+
width: 5,
|
|
3497
|
+
height: 5,
|
|
3498
|
+
borderRadius: 999,
|
|
3499
|
+
background: statusColor(r.status),
|
|
3500
|
+
flexShrink: 0,
|
|
3501
|
+
},
|
|
3502
|
+
}),
|
|
3503
|
+
h(
|
|
3504
|
+
'span',
|
|
3505
|
+
{ style: { color: LABEL_3, flexShrink: 0 } },
|
|
3506
|
+
r.startedAt.slice(5, 16).replace('T', ' '),
|
|
3507
|
+
),
|
|
3508
|
+
h(
|
|
3509
|
+
'span',
|
|
3510
|
+
{
|
|
3511
|
+
style: {
|
|
3512
|
+
color: statusColor(r.status),
|
|
3513
|
+
flexShrink: 0,
|
|
3514
|
+
},
|
|
3515
|
+
},
|
|
3516
|
+
statusText(r.status),
|
|
3517
|
+
),
|
|
3518
|
+
r.dryRun
|
|
3519
|
+
? h(
|
|
3520
|
+
'span',
|
|
3521
|
+
{ style: { color: LABEL_3 } },
|
|
3522
|
+
'演习',
|
|
3523
|
+
)
|
|
3524
|
+
: null,
|
|
3525
|
+
h(
|
|
3526
|
+
'span',
|
|
3527
|
+
{
|
|
3528
|
+
style: {
|
|
3529
|
+
color: LABEL_3,
|
|
3530
|
+
marginLeft: 'auto',
|
|
3531
|
+
flexShrink: 0,
|
|
3532
|
+
},
|
|
3533
|
+
},
|
|
3534
|
+
fmtCost(r.usage),
|
|
3535
|
+
),
|
|
3536
|
+
),
|
|
3537
|
+
),
|
|
3538
|
+
)
|
|
3539
|
+
: null,
|
|
3540
|
+
|
|
3541
|
+
// ---------- 第四层 L2:他知道什么(默认折叠) ----------
|
|
3542
|
+
h(
|
|
3543
|
+
'button',
|
|
3544
|
+
{
|
|
3545
|
+
className: `${NS}-row-btn`,
|
|
3546
|
+
onClick: () =>
|
|
3547
|
+
setMoreOpen((v) => (v === b.id ? null : b.id)),
|
|
3548
|
+
type: 'button',
|
|
3549
|
+
style: {
|
|
3550
|
+
fontSize: 11,
|
|
3551
|
+
color: LABEL_3,
|
|
3552
|
+
marginBottom: 6,
|
|
3553
|
+
},
|
|
3554
|
+
},
|
|
3555
|
+
moreOpen === b.id
|
|
3556
|
+
? '收起「他知道什么」'
|
|
3557
|
+
: '他知道什么 ▾(记忆 / 例行流程)',
|
|
3558
|
+
),
|
|
3559
|
+
|
|
3560
|
+
moreOpen === b.id
|
|
3561
|
+
? h(
|
|
3562
|
+
'div',
|
|
3563
|
+
null,
|
|
3564
|
+
detail.semantic?.facts?.length
|
|
3565
|
+
? renderMemory(detail.semantic)
|
|
3566
|
+
: null,
|
|
3567
|
+
detail.routines?.length
|
|
3568
|
+
? renderRoutines(detail.routines)
|
|
3569
|
+
: null,
|
|
3570
|
+
)
|
|
3571
|
+
: null,
|
|
3572
|
+
|
|
3573
|
+
// ---------- 第五层 L3:配置与危险操作(默认折叠,红色隔离) ----------
|
|
3574
|
+
h(
|
|
3575
|
+
'button',
|
|
3576
|
+
{
|
|
3577
|
+
className: `${NS}-row-btn`,
|
|
3578
|
+
onClick: () =>
|
|
3579
|
+
setDangerOpen((v) => (v === b.id ? null : b.id)),
|
|
3580
|
+
type: 'button',
|
|
3581
|
+
style: {
|
|
3582
|
+
fontSize: 11,
|
|
3583
|
+
color: ERR,
|
|
3584
|
+
marginBottom: 6,
|
|
3585
|
+
},
|
|
3586
|
+
},
|
|
3587
|
+
dangerOpen === b.id
|
|
3588
|
+
? '收起「配置与危险操作」'
|
|
3589
|
+
: '配置与危险操作 ▾(模型 / 预算 / 快照 / 回滚 / 删除)',
|
|
3590
|
+
),
|
|
3591
|
+
|
|
3592
|
+
dangerOpen === b.id
|
|
3593
|
+
? h(
|
|
3594
|
+
'div',
|
|
3595
|
+
{
|
|
3596
|
+
style: {
|
|
3597
|
+
border: `1px solid ${ERR}`,
|
|
3598
|
+
borderRadius: R_SM,
|
|
3599
|
+
padding: '8px 10px',
|
|
3600
|
+
marginBottom: 6,
|
|
3601
|
+
},
|
|
3602
|
+
},
|
|
3603
|
+
// 模型
|
|
3604
|
+
h(
|
|
3605
|
+
'div',
|
|
3606
|
+
{ style: { marginBottom: 10 } },
|
|
3607
|
+
h(
|
|
3608
|
+
SectionTitle,
|
|
3609
|
+
{ hint: '用哪个模型干活' },
|
|
3610
|
+
'模型',
|
|
3611
|
+
),
|
|
3612
|
+
h(
|
|
3613
|
+
'div',
|
|
3614
|
+
{
|
|
3615
|
+
style: {
|
|
3616
|
+
display: 'flex',
|
|
3617
|
+
alignItems: 'center',
|
|
3618
|
+
gap: 8,
|
|
3619
|
+
fontSize: 11,
|
|
3620
|
+
},
|
|
3621
|
+
},
|
|
3622
|
+
h(
|
|
3623
|
+
'span',
|
|
3624
|
+
{
|
|
3625
|
+
style: {
|
|
3626
|
+
color: LABEL_1,
|
|
3627
|
+
fontFamily:
|
|
3628
|
+
'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
3629
|
+
overflow: 'hidden',
|
|
3630
|
+
textOverflow: 'ellipsis',
|
|
3631
|
+
whiteSpace: 'nowrap',
|
|
3632
|
+
},
|
|
3633
|
+
},
|
|
3634
|
+
`${detail.bot.model?.provider ?? '?'} / ${
|
|
3635
|
+
detail.bot.model?.model ?? '?'
|
|
3636
|
+
}`,
|
|
3637
|
+
),
|
|
3638
|
+
h(
|
|
3639
|
+
Btn,
|
|
3640
|
+
{
|
|
3641
|
+
onClick: () =>
|
|
3642
|
+
act.onOpenModel?.({
|
|
3643
|
+
botId: b.id,
|
|
3644
|
+
botName: b.name,
|
|
3645
|
+
provider: detail.bot.model?.provider,
|
|
3646
|
+
model: detail.bot.model?.model,
|
|
3647
|
+
}),
|
|
3648
|
+
disabled: busy,
|
|
3649
|
+
},
|
|
3650
|
+
'改',
|
|
3651
|
+
),
|
|
3652
|
+
),
|
|
3653
|
+
),
|
|
3654
|
+
// 预算
|
|
3655
|
+
h(
|
|
3656
|
+
'div',
|
|
3657
|
+
{ style: { marginBottom: 10 } },
|
|
3658
|
+
h(
|
|
3659
|
+
'div',
|
|
3660
|
+
{
|
|
3661
|
+
style: {
|
|
3662
|
+
display: 'flex',
|
|
3663
|
+
justifyContent: 'space-between',
|
|
3664
|
+
fontSize: 11,
|
|
3665
|
+
color: LABEL_2,
|
|
3666
|
+
},
|
|
3667
|
+
},
|
|
3668
|
+
h('span', null, '今日花费'),
|
|
3669
|
+
h(
|
|
3670
|
+
'span',
|
|
3671
|
+
{ style: { color: LABEL_1 } },
|
|
3672
|
+
`${fmtCny(b.todayCostCny)} / ¥${budgetMax.toFixed(2)}`,
|
|
3673
|
+
),
|
|
3674
|
+
),
|
|
3675
|
+
h(Meter, {
|
|
3676
|
+
value: b.todayCostCny,
|
|
3677
|
+
max: budgetMax,
|
|
3678
|
+
}),
|
|
3679
|
+
),
|
|
3680
|
+
// 可撤回 + 危险操作
|
|
3681
|
+
h(
|
|
3682
|
+
'div',
|
|
3683
|
+
{
|
|
3684
|
+
style: {
|
|
3685
|
+
display: 'flex',
|
|
3686
|
+
gap: 6,
|
|
3687
|
+
flexWrap: 'wrap',
|
|
3688
|
+
},
|
|
3689
|
+
},
|
|
3690
|
+
h(
|
|
3691
|
+
Btn,
|
|
3692
|
+
{
|
|
3693
|
+
onClick: () => act('snapshot', b),
|
|
3694
|
+
disabled: busy,
|
|
3695
|
+
icon: h(IconSnapshot, { size: 13 }),
|
|
3696
|
+
},
|
|
3697
|
+
'打快照',
|
|
3698
|
+
),
|
|
3699
|
+
h(ConfirmBtn, {
|
|
3700
|
+
label: '回滚',
|
|
3701
|
+
confirmLabel: '确认回滚?',
|
|
3702
|
+
kind: 'danger',
|
|
3703
|
+
icon: h(IconRollback, { size: 13 }),
|
|
3704
|
+
disabled: busy,
|
|
3705
|
+
onConfirm: () => act('rollback', b),
|
|
3706
|
+
}),
|
|
3707
|
+
h(ConfirmBtn, {
|
|
3708
|
+
label: '删除',
|
|
3709
|
+
confirmLabel: '确认删除?不可恢复',
|
|
3710
|
+
kind: 'danger',
|
|
3711
|
+
disabled: busy,
|
|
3712
|
+
onConfirm: () => act('botDelete', b),
|
|
3713
|
+
}),
|
|
3714
|
+
),
|
|
3715
|
+
)
|
|
3716
|
+
: null,
|
|
3717
|
+
),
|
|
3718
|
+
)
|
|
3719
|
+
: null,
|
|
3720
|
+
);
|
|
3721
|
+
}),
|
|
3722
|
+
),
|
|
3723
|
+
|
|
3724
|
+
// 关闭滚动容器
|
|
3725
|
+
),
|
|
3726
|
+
);
|
|
3727
|
+
} catch (e) {
|
|
3728
|
+
report('render-error', String(e?.message ?? e));
|
|
3729
|
+
return null;
|
|
3730
|
+
}
|
|
3731
|
+
}
|
|
3732
|
+
|
|
3733
|
+
/**
|
|
3734
|
+
* DSH 官方设置页里的「Bot」分区。
|
|
3735
|
+
*
|
|
3736
|
+
* 与面板内的设置视图共用同一个 SettingsView,只是外壳不同:
|
|
3737
|
+
* · 官方设置页:由 shell 提供页签与标题,我们只渲染内容
|
|
3738
|
+
* · 面板内:自带「关闭设置」按钮
|
|
3739
|
+
*
|
|
3740
|
+
* 这样用户既能从系统设置进,也能从 Bot 面板进——两条路一份实现。
|
|
3741
|
+
*/
|
|
3742
|
+
function BotSettingsSection() {
|
|
3743
|
+
try {
|
|
3744
|
+
const save = React.useCallback(async (next) => {
|
|
3745
|
+
try {
|
|
3746
|
+
await fetch('/_dsh-bot/settings', {
|
|
3747
|
+
method: 'POST',
|
|
3748
|
+
headers: { 'content-type': 'application/json' },
|
|
3749
|
+
body: JSON.stringify({ settings: next }),
|
|
3750
|
+
});
|
|
3751
|
+
} catch {
|
|
3752
|
+
/* 保存失败下次再试 */
|
|
3753
|
+
}
|
|
3754
|
+
}, []);
|
|
3755
|
+
|
|
3756
|
+
const load = React.useCallback(async () => {
|
|
3757
|
+
const r = await fetch('/_dsh-bot/settings', {
|
|
3758
|
+
headers: { accept: 'application/json' },
|
|
3759
|
+
});
|
|
3760
|
+
const j = await r.json();
|
|
3761
|
+
if (!j?.ok) throw new Error(j?.message || '读取失败');
|
|
3762
|
+
return j.value.effective;
|
|
3763
|
+
}, []);
|
|
3764
|
+
|
|
3765
|
+
const clearRuns = React.useCallback(async () => {
|
|
3766
|
+
try {
|
|
3767
|
+
const r = await fetch('/_dsh-bot/overview', {
|
|
3768
|
+
headers: { accept: 'application/json' },
|
|
3769
|
+
});
|
|
3770
|
+
const j = await r.json();
|
|
3771
|
+
const first = j?.value?.bots?.[0];
|
|
3772
|
+
if (!first) return;
|
|
3773
|
+
await fetch('/_dsh-bot/runs/clear', {
|
|
3774
|
+
method: 'POST',
|
|
3775
|
+
headers: { 'content-type': 'application/json' },
|
|
3776
|
+
body: JSON.stringify({ botId: first.id, confirm: true }),
|
|
3777
|
+
});
|
|
3778
|
+
} catch {
|
|
3779
|
+
/* 忽略 */
|
|
3780
|
+
}
|
|
3781
|
+
}, []);
|
|
3782
|
+
|
|
3783
|
+
return h(
|
|
3784
|
+
'div',
|
|
3785
|
+
{
|
|
3786
|
+
style: {
|
|
3787
|
+
padding: '4px 0 16px',
|
|
3788
|
+
fontFamily: FONT,
|
|
3789
|
+
color: LABEL_1,
|
|
3790
|
+
maxWidth: 620,
|
|
3791
|
+
},
|
|
3792
|
+
},
|
|
3793
|
+
h(
|
|
3794
|
+
'div',
|
|
3795
|
+
{ style: { fontSize: 13, fontWeight: 600, marginBottom: 10 } },
|
|
3796
|
+
'Bot',
|
|
3797
|
+
),
|
|
3798
|
+
h(SettingsView, {
|
|
3799
|
+
busy: false,
|
|
3800
|
+
onClose: () => {},
|
|
3801
|
+
onLoad: load,
|
|
3802
|
+
onSave: save,
|
|
3803
|
+
onClearRuns: clearRuns,
|
|
3804
|
+
hideClose: true,
|
|
3805
|
+
}),
|
|
3806
|
+
);
|
|
3807
|
+
} catch (e) {
|
|
3808
|
+
report('settings-section-render-error', String(e?.message ?? e));
|
|
3809
|
+
return null;
|
|
3810
|
+
}
|
|
3811
|
+
}
|
|
3812
|
+
|
|
3813
|
+
ctx.slots.inject('conversation.input.dock', () =>
|
|
3814
|
+
ctx.slots.register(
|
|
3815
|
+
{ name: 'conversation.input.dock', id: 'dsh-bot', order: 20 },
|
|
3816
|
+
BotDock,
|
|
3817
|
+
),
|
|
3818
|
+
);
|
|
3819
|
+
|
|
3820
|
+
// ⭐ 注册到 DSH 官方设置页(settings.section)
|
|
3821
|
+
//
|
|
3822
|
+
// 之前我判断「DSH 没有通用 settings slot」,那是错的——
|
|
3823
|
+
// 官方确实有 `settings.section`,第三方插件(如壁纸引擎)就在用:
|
|
3824
|
+
//
|
|
3825
|
+
// ctx.slots.inject("settings.section", () =>
|
|
3826
|
+
// ctx.slots.register(
|
|
3827
|
+
// { name: "settings.section", id: "wallpaper-engine",
|
|
3828
|
+
// order: 500, label: () => "壁纸引擎" },
|
|
3829
|
+
// () => React.createElement(Section),
|
|
3830
|
+
// ))
|
|
3831
|
+
//
|
|
3832
|
+
// 契约:只收 id / order / label(图标由官方 shell 按 id 硬编码兜底齿轮)。
|
|
3833
|
+
//
|
|
3834
|
+
// ⚠️ 这一段必须**完全隔离**:
|
|
3835
|
+
// · 老版本宿主没有这个座位 → inject 回调不执行,正常
|
|
3836
|
+
// · 新宿主有但契约不同 → 抛错也不能影响下面的 report
|
|
3837
|
+
// · 组件本身渲染失败 → 已在 BotSettingsSection 内部 try/catch
|
|
3838
|
+
// 所以用独立的 try/catch 包住,且不改变任何后续控制流。
|
|
3839
|
+
try {
|
|
3840
|
+
if (ctx?.slots?.inject) {
|
|
3841
|
+
ctx.slots.inject('settings.section', () =>
|
|
3842
|
+
ctx.slots.register(
|
|
3843
|
+
{
|
|
3844
|
+
name: 'settings.section',
|
|
3845
|
+
id: 'dsh-bot',
|
|
3846
|
+
order: 600,
|
|
3847
|
+
label: () => 'Bot',
|
|
3848
|
+
},
|
|
3849
|
+
BotSettingsSection,
|
|
3850
|
+
),
|
|
3851
|
+
);
|
|
3852
|
+
report('ok', 'settings.section registered');
|
|
3853
|
+
} else {
|
|
3854
|
+
report('settings-section-skip', 'ctx.slots.inject 不可用');
|
|
3855
|
+
}
|
|
3856
|
+
} catch (e) {
|
|
3857
|
+
// 注册失败只影响"设置页里没有 Bot 分区",
|
|
3858
|
+
// 但绝不能让它把 client 的激活状态搞坏
|
|
3859
|
+
report('settings-section-error', String(e?.message ?? e));
|
|
3860
|
+
}
|
|
3861
|
+
|
|
3862
|
+
// ---------- 运行完成通知(全局轮询器) ----------
|
|
3863
|
+
//
|
|
3864
|
+
// 为什么是轮询而不是推送:宿主没有给插件暴露「向会话投递消息」
|
|
3865
|
+
// 或系统通知的 API(app.asar 里 Notification 只用于更新提醒,
|
|
3866
|
+
// ctx 上没有 notification 服务)。client bundle 活在 GUI 页面里、
|
|
3867
|
+
// 面板收起也照常运行,轮询是最稳的通道。
|
|
3868
|
+
//
|
|
3869
|
+
// 规则(与 settings 对齐):
|
|
3870
|
+
// · notifySystem=false → 完全静默
|
|
3871
|
+
// · notifyOnlyOnDecision=true → 只在失败/部分成功/需要人拍板时响
|
|
3872
|
+
// · quietHours 免打扰时段内不响
|
|
3873
|
+
// · lastSeenRun 存 localStorage:只对「新完成的运行」响一次
|
|
3874
|
+
try {
|
|
3875
|
+
const NOTIFY_POLL_MS = 60_000;
|
|
3876
|
+
const lastSeenKey = 'dsh-bot:lastSeenRunAt';
|
|
3877
|
+
const readLastSeen = () => {
|
|
3878
|
+
try {
|
|
3879
|
+
return Number(localStorage.getItem(lastSeenKey)) || 0;
|
|
3880
|
+
} catch {
|
|
3881
|
+
return 0;
|
|
3882
|
+
}
|
|
3883
|
+
};
|
|
3884
|
+
const inQuietHours = (q, now = new Date()) => {
|
|
3885
|
+
if (!q || !q.start || !q.end) return false;
|
|
3886
|
+
const toMin = (s) => {
|
|
3887
|
+
const [h, m] = String(s).split(':').map(Number);
|
|
3888
|
+
return (h || 0) * 60 + (m || 0);
|
|
3889
|
+
};
|
|
3890
|
+
const cur = now.getHours() * 60 + now.getMinutes();
|
|
3891
|
+
const start = toMin(q.start);
|
|
3892
|
+
const end = toMin(q.end);
|
|
3893
|
+
if (start === end) return false;
|
|
3894
|
+
if (start < end) return cur >= start && cur < end;
|
|
3895
|
+
return cur >= start || cur < end; // 跨午夜
|
|
3896
|
+
};
|
|
3897
|
+
|
|
3898
|
+
const fireNotification = (botName, run) => {
|
|
3899
|
+
const title =
|
|
3900
|
+
run.status === 'success'
|
|
3901
|
+
? `✅ ${botName}:运行完成`
|
|
3902
|
+
: run.status === 'partial'
|
|
3903
|
+
? `🟡 ${botName}:部分成功`
|
|
3904
|
+
: run.status === 'budget_exceeded'
|
|
3905
|
+
? `🛑 ${botName}:预算熔断`
|
|
3906
|
+
: `❌ ${botName}:运行失败`;
|
|
3907
|
+
const cost = run.usage
|
|
3908
|
+
? run.usage.costKind === 'free'
|
|
3909
|
+
? '免费'
|
|
3910
|
+
: Number.isFinite(run.usage.costCny)
|
|
3911
|
+
? `¥${run.usage.costCny.toFixed(4)}`
|
|
3912
|
+
: '费用未知'
|
|
3913
|
+
: '—';
|
|
3914
|
+
const body = `${new Date(run.startedAt).toLocaleTimeString('zh-CN')} · ${cost}` +
|
|
3915
|
+
(run.error?.message ? ` · ${String(run.error.message).slice(0, 80)}` : '') +
|
|
3916
|
+
(run.outputPreview ? `\n${run.outputPreview.slice(0, 100)}` : '');
|
|
3917
|
+
try {
|
|
3918
|
+
if (typeof Notification === 'undefined') return;
|
|
3919
|
+
if (Notification.permission === 'granted') {
|
|
3920
|
+
const n = new Notification(title, { body, tag: run.id, silent: false });
|
|
3921
|
+
n.onclick = () => {
|
|
3922
|
+
try { window.focus(); } catch {}
|
|
3923
|
+
};
|
|
3924
|
+
}
|
|
3925
|
+
} catch {
|
|
3926
|
+
/* 通知失败不影响主流程 */
|
|
3927
|
+
}
|
|
3928
|
+
};
|
|
3929
|
+
|
|
3930
|
+
let notifying = false;
|
|
3931
|
+
const notifyTick = async () => {
|
|
3932
|
+
if (notifying) return;
|
|
3933
|
+
notifying = true;
|
|
3934
|
+
try {
|
|
3935
|
+
const sRes = await fetch('/_dsh-bot/settings', { headers: { accept: 'application/json' } });
|
|
3936
|
+
const sJson = await sRes.json();
|
|
3937
|
+
const settings = sJson?.ok ? sJson.value?.effective : null;
|
|
3938
|
+
if (!settings?.notifySystem) return;
|
|
3939
|
+
if (inQuietHours(settings.quietHours)) return;
|
|
3940
|
+
|
|
3941
|
+
const oRes = await fetch('/_dsh-bot/overview', { headers: { accept: 'application/json' } });
|
|
3942
|
+
const oJson = await oRes.json();
|
|
3943
|
+
if (!oJson?.ok) return;
|
|
3944
|
+
const bots = oJson.value?.bots ?? [];
|
|
3945
|
+
if (!bots.length) return;
|
|
3946
|
+
|
|
3947
|
+
const lastSeen = readLastSeen();
|
|
3948
|
+
let newest = lastSeen;
|
|
3949
|
+
// 每个新完成运行都要看;为控制请求量只拉每 Bot 最近 5 条
|
|
3950
|
+
for (const b of bots) {
|
|
3951
|
+
const rRes = await fetch(`/_dsh-bot/runs/list?botId=${encodeURIComponent(b.id)}&limit=5`, {
|
|
3952
|
+
headers: { accept: 'application/json' },
|
|
3953
|
+
});
|
|
3954
|
+
const rJson = await rRes.json();
|
|
3955
|
+
if (!rJson?.ok) continue;
|
|
3956
|
+
for (const run of rJson.value?.runs ?? []) {
|
|
3957
|
+
const ended = Date.parse(run.endedAt || run.startedAt);
|
|
3958
|
+
if (!Number.isFinite(ended) || ended <= lastSeen) continue;
|
|
3959
|
+
if (ended > newest) newest = ended;
|
|
3960
|
+
const attention =
|
|
3961
|
+
run.status !== 'success' ||
|
|
3962
|
+
(run.goalAssessment?.needsHuman === true);
|
|
3963
|
+
if (settings.notifyOnlyOnDecision !== true && !attention) {
|
|
3964
|
+
// 全部提醒模式:成功也响
|
|
3965
|
+
fireNotification(b.name, run);
|
|
3966
|
+
} else if (attention) {
|
|
3967
|
+
fireNotification(b.name, run);
|
|
3968
|
+
}
|
|
3969
|
+
}
|
|
3970
|
+
}
|
|
3971
|
+
if (newest > lastSeen) {
|
|
3972
|
+
try { localStorage.setItem(lastSeenKey, String(newest)); } catch {}
|
|
3973
|
+
}
|
|
3974
|
+
} catch {
|
|
3975
|
+
/* 网络失败下轮再试 */
|
|
3976
|
+
} finally {
|
|
3977
|
+
notifying = false;
|
|
3978
|
+
}
|
|
3979
|
+
};
|
|
3980
|
+
// 首次运行先校准 lastSeen(避免升级后把历史全响一遍),再挂定时
|
|
3981
|
+
setTimeout(() => {
|
|
3982
|
+
if (readLastSeen() === 0) {
|
|
3983
|
+
// 第一次:只记水位不响
|
|
3984
|
+
void (async () => {
|
|
3985
|
+
try {
|
|
3986
|
+
const oRes = await fetch('/_dsh-bot/overview', { headers: { accept: 'application/json' } });
|
|
3987
|
+
const oJson = await oRes.json();
|
|
3988
|
+
let newest = Date.now();
|
|
3989
|
+
for (const b of oJson?.value?.bots ?? []) {
|
|
3990
|
+
const rRes = await fetch(`/_dsh-bot/runs/list?botId=${encodeURIComponent(b.id)}&limit=1`, {
|
|
3991
|
+
headers: { accept: 'application/json' },
|
|
3992
|
+
});
|
|
3993
|
+
const rJson = await rRes.json();
|
|
3994
|
+
const ended = Date.parse(rJson?.value?.runs?.[0]?.endedAt || '');
|
|
3995
|
+
if (Number.isFinite(ended) && ended > newest) newest = ended;
|
|
3996
|
+
}
|
|
3997
|
+
try { localStorage.setItem(lastSeenKey, String(newest)); } catch {}
|
|
3998
|
+
} catch {}
|
|
3999
|
+
})();
|
|
4000
|
+
}
|
|
4001
|
+
void notifyTick();
|
|
4002
|
+
setInterval(() => void notifyTick(), NOTIFY_POLL_MS);
|
|
4003
|
+
}, 15_000);
|
|
4004
|
+
report('ok', 'notify poller armed');
|
|
4005
|
+
} catch (e) {
|
|
4006
|
+
report('notify-poller-error', String(e?.message ?? e));
|
|
4007
|
+
}
|
|
4008
|
+
|
|
4009
|
+
report('ok', 'v2 activated, slot=conversation.input.dock');
|
|
4010
|
+
} catch (e) {
|
|
4011
|
+
report('error', String(e?.message ?? e));
|
|
4012
|
+
}
|
|
4013
|
+
}
|
|
4014
|
+
|
|
4015
|
+
return { name: 'dsh-bot-client', inject, apply };
|
|
4016
|
+
},
|
|
4017
|
+
});
|