opencode-subagent-magazine 1.5.2 → 1.6.0-beta.1
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 -21
- package/README.md +192 -192
- package/dist/_version.d.ts +1 -1
- package/dist/_version.js +1 -1
- package/dist/core/color.d.ts +20 -0
- package/dist/core/color.js +68 -0
- package/dist/core/format.d.ts +5 -0
- package/dist/core/format.js +68 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.js +6 -0
- package/dist/core/kv.d.ts +20 -0
- package/dist/core/kv.js +30 -0
- package/dist/core/state-machine.d.ts +17 -0
- package/dist/core/state-machine.js +27 -0
- package/dist/core/types.d.ts +51 -0
- package/dist/core/types.js +2 -0
- package/dist/core/usage.d.ts +15 -0
- package/dist/core/usage.js +1 -0
- package/dist/index.js +148 -1484
- package/dist/panel/SubAgentPanel.d.ts +13 -0
- package/dist/panel/SubAgentPanel.js +1237 -0
- package/dist/panel/panel-api.d.ts +67 -0
- package/dist/panel/panel-api.js +1 -0
- package/dist/panel/store.d.ts +5 -0
- package/dist/panel/store.js +5 -0
- package/dist/tui.js +388 -292
- package/dist/v2/commands.d.ts +7 -0
- package/dist/v2/commands.js +263 -0
- package/dist/v2/index.d.ts +8 -0
- package/dist/v2/index.js +62 -0
- package/dist/v2/theme.d.ts +3 -0
- package/dist/v2/theme.js +12 -0
- package/dist/v2/types.d.ts +231 -0
- package/dist/v2/types.js +6 -0
- package/dist/v2/v2-panel-api.d.ts +12 -0
- package/dist/v2/v2-panel-api.js +350 -0
- package/dist/v2.js +3008 -0
- package/install.mjs +103 -103
- package/package.json +64 -63
- package/src/_version.ts +1 -1
- package/src/clipboard.ts +134 -134
- package/src/core/color.ts +70 -0
- package/src/core/format.ts +61 -0
- package/src/core/index.ts +6 -0
- package/src/core/kv.ts +36 -0
- package/src/core/state-machine.ts +46 -0
- package/src/core/types.ts +58 -0
- package/src/core/usage.ts +16 -0
- package/src/i18n.ts +261 -261
- package/src/index.tsx +124 -1750
- package/src/panel/SubAgentPanel.tsx +1465 -0
- package/src/panel/panel-api.ts +72 -0
- package/src/panel/store.ts +8 -0
- package/src/server.ts +10 -10
- package/src/v2/commands.ts +251 -0
- package/src/v2/index.tsx +98 -0
- package/src/v2/theme.ts +14 -0
- package/src/v2/types.ts +165 -0
- package/src/v2/v2-panel-api.ts +306 -0
- package/tui/index.js +11 -0
|
@@ -0,0 +1,1237 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "@opentui/solid/jsx-runtime";
|
|
2
|
+
import { createMemo, createSignal, createEffect, onMount, onCleanup, untrack, Show, For, } from "solid-js";
|
|
3
|
+
import { PLUGIN_VERSION } from "../_version";
|
|
4
|
+
import { copyText } from "../clipboard";
|
|
5
|
+
import { createT } from "../i18n";
|
|
6
|
+
import { SUBAGENT_TOOLS } from "../core/types";
|
|
7
|
+
import { visualWidth, truncate, fmtDurationShort, fmtTokens, safeErrorMsg } from "../core/format";
|
|
8
|
+
import { rgb, desaturateTo, dimColor, FALLBACK, MAX_SAT } from "../core/color";
|
|
9
|
+
import { KV_PREFIX } from "../core/kv";
|
|
10
|
+
import { globalEntryCache, clearTick } from "./store";
|
|
11
|
+
/** Entry line left prefix: icon + space + status dot + space */
|
|
12
|
+
const LEFT_PAD = 4;
|
|
13
|
+
/** Detail row indent: two spaces */
|
|
14
|
+
const INDENT = 2;
|
|
15
|
+
// ===================================================================
|
|
16
|
+
// Sidebar component
|
|
17
|
+
// ===================================================================
|
|
18
|
+
export function SubAgentPanel(props) {
|
|
19
|
+
const t = createT(() => props.lang());
|
|
20
|
+
// ── session data (single-key, true deletion on cleanup) ──
|
|
21
|
+
const SESSION_DATA_KEY = `${KV_PREFIX}.session_data`;
|
|
22
|
+
const ttlDaysRaw = parseInt(String(props.api.kv.get(`${KV_PREFIX}.ttl_days`, "3")), 10);
|
|
23
|
+
const ttlDays = Number.isNaN(ttlDaysRaw) ? 3 : ttlDaysRaw;
|
|
24
|
+
const TTL_MS = ttlDays * 24 * 60 * 60 * 1000;
|
|
25
|
+
const loadSessionData = () => {
|
|
26
|
+
try {
|
|
27
|
+
const raw = props.api.kv.get(SESSION_DATA_KEY, "{}");
|
|
28
|
+
return JSON.parse(String(raw));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const saveSessionData = (data) => {
|
|
35
|
+
try {
|
|
36
|
+
props.api.kv.set(SESSION_DATA_KEY, JSON.stringify(data));
|
|
37
|
+
}
|
|
38
|
+
catch { }
|
|
39
|
+
};
|
|
40
|
+
/** 将任意 session ID 解析为父会话 ID + 是否子会话。
|
|
41
|
+
* 通过 SDK session.get(sid).parentID 判断,无 parentID 即为主会话。 */
|
|
42
|
+
const resolveParent = (sid) => {
|
|
43
|
+
try {
|
|
44
|
+
const session = props.api.session.get(sid);
|
|
45
|
+
const parentID = session?.parentID;
|
|
46
|
+
if (parentID)
|
|
47
|
+
return { parentSid: parentID, isChild: true };
|
|
48
|
+
}
|
|
49
|
+
catch { }
|
|
50
|
+
return { parentSid: sid, isChild: false };
|
|
51
|
+
};
|
|
52
|
+
const loadEntries = (sid) => {
|
|
53
|
+
const m = new Map();
|
|
54
|
+
try {
|
|
55
|
+
const { parentSid, isChild } = resolveParent(sid);
|
|
56
|
+
const rec = loadSessionData()[parentSid];
|
|
57
|
+
if (rec) {
|
|
58
|
+
const source = isChild ? rec.children?.[sid]?.entries : rec.entries;
|
|
59
|
+
if (source) {
|
|
60
|
+
for (const e of source)
|
|
61
|
+
m.set(e.id, e);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch { }
|
|
66
|
+
return m;
|
|
67
|
+
};
|
|
68
|
+
let persistTimer;
|
|
69
|
+
const persistEntries = (sid, entries) => {
|
|
70
|
+
clearTimeout(persistTimer);
|
|
71
|
+
persistTimer = setTimeout(() => {
|
|
72
|
+
try {
|
|
73
|
+
const data = loadSessionData();
|
|
74
|
+
const { parentSid, isChild } = resolveParent(sid);
|
|
75
|
+
if (isChild) {
|
|
76
|
+
if (!data[parentSid])
|
|
77
|
+
data[parentSid] = { ts: Date.now(), entries: [], scroll: 0, expanded: "", children: {} };
|
|
78
|
+
if (!data[parentSid].children)
|
|
79
|
+
data[parentSid].children = {};
|
|
80
|
+
if (!data[parentSid].children[sid])
|
|
81
|
+
data[parentSid].children[sid] = { scroll: 0, expanded: "", entries: [] };
|
|
82
|
+
data[parentSid].children[sid] = { ...data[parentSid].children[sid], entries: [...entries.values()] };
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
data[sid] = { ...data[sid], ts: Date.now(), entries: [...entries.values()], children: data[sid]?.children ?? {} };
|
|
86
|
+
}
|
|
87
|
+
saveSessionData(data);
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
}, 200);
|
|
91
|
+
};
|
|
92
|
+
const persistScroll = (sid, scroll) => {
|
|
93
|
+
try {
|
|
94
|
+
const data = loadSessionData();
|
|
95
|
+
const { parentSid, isChild } = resolveParent(sid);
|
|
96
|
+
if (isChild) {
|
|
97
|
+
if (!data[parentSid])
|
|
98
|
+
data[parentSid] = { ts: Date.now(), entries: [], scroll: 0, expanded: "", children: {} };
|
|
99
|
+
if (!data[parentSid].children)
|
|
100
|
+
data[parentSid].children = {};
|
|
101
|
+
if (!data[parentSid].children[sid])
|
|
102
|
+
data[parentSid].children[sid] = { scroll: 0, expanded: "", entries: [] };
|
|
103
|
+
data[parentSid].children[sid] = { ...data[parentSid].children[sid], scroll };
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
data[sid] = { ...data[sid], ts: Date.now(), scroll, children: data[sid]?.children ?? {} };
|
|
107
|
+
}
|
|
108
|
+
saveSessionData(data);
|
|
109
|
+
}
|
|
110
|
+
catch { }
|
|
111
|
+
};
|
|
112
|
+
const persistExpanded = (sid, expanded) => {
|
|
113
|
+
try {
|
|
114
|
+
const data = loadSessionData();
|
|
115
|
+
const { parentSid, isChild } = resolveParent(sid);
|
|
116
|
+
if (isChild) {
|
|
117
|
+
if (!data[parentSid])
|
|
118
|
+
data[parentSid] = { ts: Date.now(), entries: [], scroll: 0, expanded: "", children: {} };
|
|
119
|
+
if (!data[parentSid].children)
|
|
120
|
+
data[parentSid].children = {};
|
|
121
|
+
if (!data[parentSid].children[sid])
|
|
122
|
+
data[parentSid].children[sid] = { scroll: 0, expanded: "", entries: [] };
|
|
123
|
+
data[parentSid].children[sid] = { ...data[parentSid].children[sid], expanded };
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
data[sid] = { ...data[sid], ts: Date.now(), expanded, children: data[sid]?.children ?? {} };
|
|
127
|
+
}
|
|
128
|
+
saveSessionData(data);
|
|
129
|
+
}
|
|
130
|
+
catch { }
|
|
131
|
+
};
|
|
132
|
+
const cleanupOldSessions = () => {
|
|
133
|
+
if (ttlDays <= 0)
|
|
134
|
+
return; // 无期限,跳过清理
|
|
135
|
+
try {
|
|
136
|
+
const data = loadSessionData();
|
|
137
|
+
const cutoff = Date.now() - TTL_MS;
|
|
138
|
+
let changed = false;
|
|
139
|
+
for (const sid of Object.keys(data)) {
|
|
140
|
+
if (data[sid].ts < cutoff) {
|
|
141
|
+
delete data[sid];
|
|
142
|
+
changed = true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (changed)
|
|
146
|
+
saveSessionData(data);
|
|
147
|
+
}
|
|
148
|
+
catch { }
|
|
149
|
+
};
|
|
150
|
+
cleanupOldSessions();
|
|
151
|
+
const [entryMap, setEntryMapRaw] = createSignal(loadEntries(props.sessionId));
|
|
152
|
+
// Wrapped setter — also persists to kv on every mutation
|
|
153
|
+
const setEntryMap = (arg) => {
|
|
154
|
+
setEntryMapRaw((prev) => {
|
|
155
|
+
const next = typeof arg === "function" ? arg(prev) : arg;
|
|
156
|
+
// entry 状态落定(done/error)时立即持久化到 KV,跳过常规 debounce,
|
|
157
|
+
// 确保跨视图的状态一致性。
|
|
158
|
+
let needsImmediateFlush = false;
|
|
159
|
+
for (const [id, entry] of next) {
|
|
160
|
+
const prevEntry = prev.get(id);
|
|
161
|
+
if (prevEntry?.status === "running" && (entry.status === "done" || entry.status === "error" || entry.status === "cancelled")) {
|
|
162
|
+
needsImmediateFlush = true;
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (needsImmediateFlush) {
|
|
167
|
+
clearTimeout(persistTimer);
|
|
168
|
+
try {
|
|
169
|
+
const data = loadSessionData();
|
|
170
|
+
const { parentSid, isChild } = resolveParent(props.sessionId);
|
|
171
|
+
if (isChild) {
|
|
172
|
+
if (!data[parentSid])
|
|
173
|
+
data[parentSid] = { ts: Date.now(), entries: [], scroll: 0, expanded: "", children: {} };
|
|
174
|
+
if (!data[parentSid].children)
|
|
175
|
+
data[parentSid].children = {};
|
|
176
|
+
if (!data[parentSid].children[props.sessionId])
|
|
177
|
+
data[parentSid].children[props.sessionId] = { scroll: 0, expanded: "", entries: [] };
|
|
178
|
+
data[parentSid].children[props.sessionId] = { ...data[parentSid].children[props.sessionId], entries: [...next.values()] };
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
data[props.sessionId] = { ...data[props.sessionId], ts: Date.now(), entries: [...next.values()], children: data[props.sessionId]?.children ?? {} };
|
|
182
|
+
}
|
|
183
|
+
saveSessionData(data);
|
|
184
|
+
}
|
|
185
|
+
catch { }
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
persistEntries(props.sessionId, next);
|
|
189
|
+
}
|
|
190
|
+
// 同步到模块级缓存,供其他视图读取当前 session 的最新状态
|
|
191
|
+
globalEntryCache.set(props.sessionId, new Map(next));
|
|
192
|
+
return next;
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
const [panelWidth, setPanelWidth] = createSignal(28);
|
|
196
|
+
const [open, setOpen] = createSignal((() => { try {
|
|
197
|
+
return props.api.kv.get(`${KV_PREFIX}.open`, true);
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return true;
|
|
201
|
+
} })());
|
|
202
|
+
const [expanded, setExpanded] = createSignal((() => {
|
|
203
|
+
try {
|
|
204
|
+
const { parentSid, isChild } = resolveParent(props.sessionId);
|
|
205
|
+
const rec = loadSessionData()[parentSid];
|
|
206
|
+
if (rec)
|
|
207
|
+
return isChild ? rec.children?.[props.sessionId]?.expanded || undefined : rec.expanded || undefined;
|
|
208
|
+
}
|
|
209
|
+
catch { }
|
|
210
|
+
return undefined;
|
|
211
|
+
})());
|
|
212
|
+
const [hoveredOpen, setHoveredOpen] = createSignal(undefined);
|
|
213
|
+
const [hoveredDismiss, setHoveredDismiss] = createSignal(undefined);
|
|
214
|
+
const [hoveredCancel, setHoveredCancel] = createSignal(undefined);
|
|
215
|
+
const [hoveredTop, setHoveredTop] = createSignal(false);
|
|
216
|
+
const [hoveredMoreAbove, setHoveredMoreAbove] = createSignal(false);
|
|
217
|
+
const [hoveredMoreBelow, setHoveredMoreBelow] = createSignal(false);
|
|
218
|
+
const [scrollOffset, setScrollOffset] = createSignal((() => {
|
|
219
|
+
try {
|
|
220
|
+
const { parentSid, isChild } = resolveParent(props.sessionId);
|
|
221
|
+
const rec = loadSessionData()[parentSid];
|
|
222
|
+
return isChild ? rec?.children?.[props.sessionId]?.scroll ?? 0 : rec?.scroll ?? 0;
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
})());
|
|
228
|
+
const [now, setNow] = createSignal(Date.now());
|
|
229
|
+
const [renderTick, setRenderTick] = createSignal(0);
|
|
230
|
+
let boxEl;
|
|
231
|
+
let disposed = false;
|
|
232
|
+
// ── upsert ──
|
|
233
|
+
const upsertEntry = (partial) => {
|
|
234
|
+
setEntryMap((prev) => {
|
|
235
|
+
const existing = prev.get(partial.id);
|
|
236
|
+
const next = new Map(prev);
|
|
237
|
+
const nowTs = Date.now();
|
|
238
|
+
const e = partial.status;
|
|
239
|
+
const ended = e === "done" || e === "error" || e === "cancelled";
|
|
240
|
+
next.set(partial.id, {
|
|
241
|
+
...(existing ?? { startedAt: nowTs }),
|
|
242
|
+
...partial,
|
|
243
|
+
startedAt: existing?.startedAt || partial.startedAt || nowTs,
|
|
244
|
+
endedAt: ended ? (existing?.endedAt || nowTs) : undefined,
|
|
245
|
+
});
|
|
246
|
+
return next;
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
// ── cancel helpers ──
|
|
250
|
+
const isDescendantOf = (childId, rootId) => {
|
|
251
|
+
const visited = new Set();
|
|
252
|
+
try {
|
|
253
|
+
let current = props.api.session.get(childId);
|
|
254
|
+
while (current?.parentID) {
|
|
255
|
+
if (visited.has(current.id))
|
|
256
|
+
return false;
|
|
257
|
+
visited.add(current.id);
|
|
258
|
+
if (current.parentID === rootId)
|
|
259
|
+
return true;
|
|
260
|
+
current = props.api.session.get(current.parentID);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
catch { }
|
|
264
|
+
return false;
|
|
265
|
+
};
|
|
266
|
+
const settleOnIdle = (entry) => {
|
|
267
|
+
if (entry.status === "cancel_requested" && entry.abortAccepted)
|
|
268
|
+
return "cancelled";
|
|
269
|
+
return "done";
|
|
270
|
+
};
|
|
271
|
+
const cancelEntry = async (entry) => {
|
|
272
|
+
const childId = entry.sessionId;
|
|
273
|
+
if (!childId) {
|
|
274
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.no_session"), { title: entry.title || entry.agent });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
try {
|
|
278
|
+
const child = props.api.session.get(childId);
|
|
279
|
+
if (!child?.parentID) {
|
|
280
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.not_child"), { title: entry.title || entry.agent });
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.read_error"), { title: entry.title || entry.agent });
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (!isDescendantOf(childId, props.sessionId)) {
|
|
289
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.outside_tree"), { title: entry.title || entry.agent });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
try {
|
|
293
|
+
const st = props.api.session.status(childId);
|
|
294
|
+
if (st?.type !== "busy") {
|
|
295
|
+
const tokens = props.api.usage.readSessionTokens(childId);
|
|
296
|
+
const cost = props.api.usage.readSessionCost(childId);
|
|
297
|
+
upsertEntry({
|
|
298
|
+
id: entry.id, title: entry.title, agent: entry.agent, prompt: entry.prompt,
|
|
299
|
+
status: "done", sessionId: entry.sessionId,
|
|
300
|
+
tokens, cost,
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.status_error"), { title: entry.title || entry.agent });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
upsertEntry({
|
|
310
|
+
id: entry.id, title: entry.title, agent: entry.agent, prompt: entry.prompt,
|
|
311
|
+
status: "cancel_requested", sessionId: entry.sessionId,
|
|
312
|
+
cancelRequestedAt: Date.now(), abortAccepted: false, cancelReason: "manual",
|
|
313
|
+
});
|
|
314
|
+
try {
|
|
315
|
+
await props.api.client.abort({ sessionID: childId });
|
|
316
|
+
upsertEntry({
|
|
317
|
+
id: entry.id, title: entry.title, agent: entry.agent, prompt: entry.prompt,
|
|
318
|
+
status: "cancel_requested", sessionId: entry.sessionId,
|
|
319
|
+
abortAccepted: true,
|
|
320
|
+
});
|
|
321
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.sent"));
|
|
322
|
+
}
|
|
323
|
+
catch (err) {
|
|
324
|
+
upsertEntry({
|
|
325
|
+
id: entry.id, title: entry.title, agent: entry.agent, prompt: entry.prompt,
|
|
326
|
+
status: "error", sessionId: entry.sessionId,
|
|
327
|
+
error: String(err),
|
|
328
|
+
});
|
|
329
|
+
props.api.ui.toast(t("cancel.label") + ": " + t("cancel.failed"), { title: entry.title || entry.agent });
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
// ── event handlers ──
|
|
333
|
+
const handlePartUpdated = (event) => {
|
|
334
|
+
const part = event.payload?.part;
|
|
335
|
+
if (!part)
|
|
336
|
+
return;
|
|
337
|
+
// V2 事件流是全局的——若 payload 带发起会话 ID,则只归账到正在查看的会话,
|
|
338
|
+
// 避免其他会话的子代理写进当前侧边栏;V1 宿主已按会话 scope,不传 sessionID。
|
|
339
|
+
const eventSid = event.payload?.sessionID !== undefined ? String(event.payload.sessionID) : undefined;
|
|
340
|
+
if (eventSid !== undefined && eventSid !== props.sessionId)
|
|
341
|
+
return;
|
|
342
|
+
// SubtaskPart
|
|
343
|
+
if (part.type === "subtask") {
|
|
344
|
+
const agent = String(part.agent ?? "?");
|
|
345
|
+
const prompt = String(part.prompt ?? "");
|
|
346
|
+
const desc = String(part.description ?? "");
|
|
347
|
+
const title = desc || truncate(prompt.replace(/\n/g, " ").replace(/\s+/g, " ").trim(), 40);
|
|
348
|
+
const id = `sub:${String(part.id ?? crypto.randomUUID())}`;
|
|
349
|
+
const subSid = part.sessionID !== undefined ? String(part.sessionID) : undefined;
|
|
350
|
+
const partModel = part.model;
|
|
351
|
+
const modelId = partModel?.modelID ? String(partModel.modelID) : undefined;
|
|
352
|
+
upsertEntry({ id, title, agent, prompt, sessionId: subSid, status: "running", model: modelId });
|
|
353
|
+
}
|
|
354
|
+
// ToolPart
|
|
355
|
+
if (part.type === "tool") {
|
|
356
|
+
const tool = String(part.tool ?? "");
|
|
357
|
+
if (!SUBAGENT_TOOLS.has(tool))
|
|
358
|
+
return;
|
|
359
|
+
const st = part.state;
|
|
360
|
+
const rawStatus = String(st?.status ?? "");
|
|
361
|
+
// Only create entries for tool calls that actually entered execution.
|
|
362
|
+
// "pending" / empty → state unknown yet, wait for next event
|
|
363
|
+
if (rawStatus === "pending" || rawStatus === "")
|
|
364
|
+
return;
|
|
365
|
+
// "error" → tool call failed, sub-agent never spawned.
|
|
366
|
+
// Only update an existing entry (e.g. previously running → now error),
|
|
367
|
+
// never create a new one.
|
|
368
|
+
if (rawStatus === "error") {
|
|
369
|
+
const id = `tool:${String(part.id ?? "")}`;
|
|
370
|
+
if (!part.id)
|
|
371
|
+
return;
|
|
372
|
+
const existing = entryMap().get(id);
|
|
373
|
+
if (existing) {
|
|
374
|
+
upsertEntry({ id, title: existing.title, agent: existing.agent, prompt: existing.prompt, status: "error" });
|
|
375
|
+
}
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
// rawStatus is "running" or "completed" — tool entered execution, track it.
|
|
379
|
+
const input = st?.input;
|
|
380
|
+
let status = "running";
|
|
381
|
+
if (rawStatus === "completed")
|
|
382
|
+
status = "done";
|
|
383
|
+
// Background tasks: tool completion ≠ agent completion — keep running until session.idle
|
|
384
|
+
// Only keep running if state metadata confirms a child session was spawned;
|
|
385
|
+
// otherwise (failed spawn, invalid agent) mark as done so the entry isn't stuck forever.
|
|
386
|
+
if ((input?.run_in_background === true || input?.background === true) && status === "done") {
|
|
387
|
+
const stMetaCheck = st?.metadata;
|
|
388
|
+
const hasChild = stMetaCheck?.session_id !== undefined || stMetaCheck?.sessionId !== undefined;
|
|
389
|
+
if (hasChild)
|
|
390
|
+
status = "running";
|
|
391
|
+
}
|
|
392
|
+
const agent = String(part.subagent_type ?? input?.agent ?? input?.subagent_type ?? input?.category ?? tool);
|
|
393
|
+
const prompt = String(input?.prompt ?? part.description ?? "");
|
|
394
|
+
const desc = input?.description !== undefined ? String(input.description) : "";
|
|
395
|
+
const title = desc || truncate(prompt.replace(/\n/g, " ").replace(/\s+/g, " ").trim(), 40);
|
|
396
|
+
const id = `tool:${String(part.id ?? crypto.randomUUID())}`;
|
|
397
|
+
// Child session ID lives in state-level metadata (ToolStateCompleted.metadata),
|
|
398
|
+
// injected by the tool executor. ToolPart.sessionID is the parent session.
|
|
399
|
+
const stMeta = st?.metadata;
|
|
400
|
+
const subSid = stMeta?.session_id !== undefined ? String(stMeta.session_id)
|
|
401
|
+
: stMeta?.sessionId !== undefined ? String(stMeta.sessionId)
|
|
402
|
+
: undefined;
|
|
403
|
+
upsertEntry({ id, title, agent, prompt, sessionId: subSid, status });
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
const handleSessionEnd = (event, status) => {
|
|
407
|
+
const props_ = event.payload;
|
|
408
|
+
const sid = String(props_?.sessionID ?? "");
|
|
409
|
+
if (!sid)
|
|
410
|
+
return;
|
|
411
|
+
const sessionTokens = props.api.usage.readSessionTokens(sid);
|
|
412
|
+
const sessionCost = props.api.usage.readSessionCost(sid);
|
|
413
|
+
const sessionModel = props.api.usage.readSessionModel(sid);
|
|
414
|
+
const sessionTodo = props.api.usage.readSessionTodo(sid);
|
|
415
|
+
let sessionAgent;
|
|
416
|
+
let errorMsg;
|
|
417
|
+
try {
|
|
418
|
+
const s = props.api.session.get(sid);
|
|
419
|
+
sessionAgent = s?.agent;
|
|
420
|
+
if (status === "error") {
|
|
421
|
+
const evtErr = props_?.error;
|
|
422
|
+
errorMsg = safeErrorMsg(evtErr) || safeErrorMsg(props_?.message);
|
|
423
|
+
if (!errorMsg) {
|
|
424
|
+
const msgs = props.api.session.messages(sid);
|
|
425
|
+
if (msgs) {
|
|
426
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
427
|
+
const m = msgs[i];
|
|
428
|
+
if (m.role === "assistant" && m.error) {
|
|
429
|
+
errorMsg = safeErrorMsg(m.error);
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
catch { }
|
|
438
|
+
// 在给定的 entries Map 中查找并更新匹配的子代理 entry。
|
|
439
|
+
// 返回 true 表示找到并更新了,false 表示未找到。
|
|
440
|
+
const tryMatchAndUpdate = (entriesMap, targetSid, targetStatus, nowTs) => {
|
|
441
|
+
// 精确匹配:sessionId 对得上 + 状态为 running / cancel_requested
|
|
442
|
+
for (const [, entry] of entriesMap) {
|
|
443
|
+
if (entry.sessionId === targetSid && (entry.status === "running" || entry.status === "cancel_requested")) {
|
|
444
|
+
const finalStatus = targetStatus === "error" ? "error" : settleOnIdle(entry);
|
|
445
|
+
entry.status = finalStatus;
|
|
446
|
+
entry.endedAt = nowTs;
|
|
447
|
+
entry.tokens = entry.tokens ?? sessionTokens;
|
|
448
|
+
entry.cost = entry.cost ?? sessionCost;
|
|
449
|
+
entry.model = entry.model ?? sessionModel;
|
|
450
|
+
entry.todoTotal = entry.todoTotal ?? sessionTodo?.total;
|
|
451
|
+
entry.todoDone = entry.todoDone ?? sessionTodo?.done;
|
|
452
|
+
entry.error = errorMsg || entry.error;
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
// 回退:sessionId 未关联但 agent 名匹配 + 状态为 running / cancel_requested
|
|
457
|
+
if (sessionAgent) {
|
|
458
|
+
const normalize = (s) => s.toLowerCase().replace(/[^a-z0-9-]/g, "");
|
|
459
|
+
const saNorm = normalize(sessionAgent);
|
|
460
|
+
let best = null;
|
|
461
|
+
for (const [, entry] of entriesMap) {
|
|
462
|
+
if (entry.status !== "running" && entry.status !== "cancel_requested")
|
|
463
|
+
continue;
|
|
464
|
+
const eaNorm = normalize(entry.agent);
|
|
465
|
+
if (!eaNorm || !saNorm)
|
|
466
|
+
continue;
|
|
467
|
+
if (!eaNorm.includes(saNorm) && !saNorm.includes(eaNorm))
|
|
468
|
+
continue;
|
|
469
|
+
const gap = nowTs - (entry.startedAt || 0);
|
|
470
|
+
if (!best || gap > best.gap)
|
|
471
|
+
best = { entry, gap };
|
|
472
|
+
}
|
|
473
|
+
if (!best) {
|
|
474
|
+
for (const [, entry] of entriesMap) {
|
|
475
|
+
if (entry.status !== "running" && entry.status !== "cancel_requested")
|
|
476
|
+
continue;
|
|
477
|
+
if (entry.sessionId)
|
|
478
|
+
continue;
|
|
479
|
+
const gap = nowTs - (entry.startedAt || 0);
|
|
480
|
+
if (!best || gap > best.gap)
|
|
481
|
+
best = { entry, gap };
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (best) {
|
|
485
|
+
const finalStatus = targetStatus === "error" ? "error" : settleOnIdle(best.entry);
|
|
486
|
+
best.entry.status = finalStatus;
|
|
487
|
+
best.entry.endedAt = nowTs;
|
|
488
|
+
best.entry.tokens = best.entry.tokens ?? sessionTokens;
|
|
489
|
+
best.entry.cost = best.entry.cost ?? sessionCost;
|
|
490
|
+
best.entry.model = best.entry.model ?? sessionModel;
|
|
491
|
+
best.entry.todoTotal = best.entry.todoTotal ?? sessionTodo?.total;
|
|
492
|
+
best.entry.todoDone = best.entry.todoDone ?? sessionTodo?.done;
|
|
493
|
+
best.entry.sessionId = targetSid;
|
|
494
|
+
best.entry.error = errorMsg || best.entry.error;
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return false;
|
|
499
|
+
};
|
|
500
|
+
setEntryMap((prev) => {
|
|
501
|
+
let changed = false;
|
|
502
|
+
const next = new Map(prev);
|
|
503
|
+
for (const [id, entry] of next) {
|
|
504
|
+
if (entry.sessionId !== sid)
|
|
505
|
+
continue;
|
|
506
|
+
if (entry.status !== "running" && entry.status !== "done" && entry.status !== "cancel_requested")
|
|
507
|
+
continue;
|
|
508
|
+
// Skip parent session idle — subagent entries belong to child sessions only
|
|
509
|
+
if (sid === props.sessionId)
|
|
510
|
+
continue;
|
|
511
|
+
// For "done" entries (sync tasks completed before session.idle), only backfill tokens/cost
|
|
512
|
+
const alreadySettled = entry.status !== "running" && entry.status !== "cancel_requested";
|
|
513
|
+
const finalStatus = status === "error" ? "error" : settleOnIdle(entry);
|
|
514
|
+
next.set(id, {
|
|
515
|
+
...entry,
|
|
516
|
+
...(alreadySettled ? {} : { status: finalStatus, endedAt: Date.now() }),
|
|
517
|
+
tokens: entry.tokens ?? sessionTokens,
|
|
518
|
+
cost: entry.cost ?? sessionCost,
|
|
519
|
+
model: entry.model ?? sessionModel,
|
|
520
|
+
todoTotal: entry.todoTotal ?? sessionTodo?.total,
|
|
521
|
+
todoDone: entry.todoDone ?? sessionTodo?.done,
|
|
522
|
+
error: errorMsg || entry.error,
|
|
523
|
+
});
|
|
524
|
+
changed = true;
|
|
525
|
+
}
|
|
526
|
+
if (!changed && sessionAgent) {
|
|
527
|
+
const nowTs = Date.now();
|
|
528
|
+
const normalize = (s) => s.toLowerCase().replace(/[^a-z0-9-]/g, "");
|
|
529
|
+
const saNorm = normalize(sessionAgent);
|
|
530
|
+
let best = null;
|
|
531
|
+
// Phase 1: try matching by agent name(agent 名有交集)
|
|
532
|
+
for (const [id, entry] of next) {
|
|
533
|
+
if (entry.status !== "running" && entry.status !== "cancel_requested")
|
|
534
|
+
continue;
|
|
535
|
+
const eaNorm = normalize(entry.agent);
|
|
536
|
+
if (!eaNorm || !saNorm)
|
|
537
|
+
continue;
|
|
538
|
+
if (!eaNorm.includes(saNorm) && !saNorm.includes(eaNorm))
|
|
539
|
+
continue;
|
|
540
|
+
const gap = nowTs - (entry.startedAt || 0);
|
|
541
|
+
if (!best || gap > best.gap)
|
|
542
|
+
best = { id, gap };
|
|
543
|
+
}
|
|
544
|
+
// Phase 2: if agent name has no overlap (e.g. category calls: agent="deep" vs sessionAgent="Sisyphus-Junior"),
|
|
545
|
+
// fall back to time proximity for entries that have no sessionId yet
|
|
546
|
+
if (!best) {
|
|
547
|
+
for (const [id, entry] of next) {
|
|
548
|
+
if (entry.status !== "running" && entry.status !== "cancel_requested")
|
|
549
|
+
continue;
|
|
550
|
+
if (entry.sessionId)
|
|
551
|
+
continue;
|
|
552
|
+
const gap = nowTs - (entry.startedAt || 0);
|
|
553
|
+
if (!best || gap > best.gap)
|
|
554
|
+
best = { id, gap };
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (best) {
|
|
558
|
+
const entry = next.get(best.id);
|
|
559
|
+
const finalStatus = status === "error" ? "error" : settleOnIdle(entry);
|
|
560
|
+
next.set(best.id, {
|
|
561
|
+
...entry, status: finalStatus, endedAt: nowTs,
|
|
562
|
+
tokens: sessionTokens || entry.tokens,
|
|
563
|
+
cost: sessionCost || entry.cost,
|
|
564
|
+
sessionId: sid,
|
|
565
|
+
error: errorMsg || entry.error,
|
|
566
|
+
});
|
|
567
|
+
changed = true;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return changed ? next : prev;
|
|
571
|
+
});
|
|
572
|
+
// 当子代理所属的父 session 与当前视图不同时,通过模块级缓存定位
|
|
573
|
+
// 并更新父 session 的 entry 状态,随后写回 KV。
|
|
574
|
+
try {
|
|
575
|
+
const sessionObj = props.api.session.get(sid);
|
|
576
|
+
const parentSid = sessionObj?.parentID;
|
|
577
|
+
if (parentSid && parentSid !== props.sessionId) {
|
|
578
|
+
// 优先从模块级缓存获取父 session 的 entries,不受当前视图切换影响
|
|
579
|
+
const parentCache = globalEntryCache.get(parentSid);
|
|
580
|
+
const nowTs = Date.now();
|
|
581
|
+
let found = false;
|
|
582
|
+
if (parentCache) {
|
|
583
|
+
found = tryMatchAndUpdate(parentCache, sid, status, nowTs);
|
|
584
|
+
}
|
|
585
|
+
// 缓存未命中时回退到 KV 读取
|
|
586
|
+
if (!found) {
|
|
587
|
+
const data = loadSessionData();
|
|
588
|
+
const rec = data[parentSid];
|
|
589
|
+
if (rec?.entries) {
|
|
590
|
+
const fallbackMap = new Map(rec.entries.map((e) => [e.id, e]));
|
|
591
|
+
found = tryMatchAndUpdate(fallbackMap, sid, status, nowTs);
|
|
592
|
+
if (found) {
|
|
593
|
+
// 回退命中后写入 KV 并回填缓存
|
|
594
|
+
data[parentSid] = { ...rec, ts: nowTs, entries: [...fallbackMap.values()] };
|
|
595
|
+
saveSessionData(data);
|
|
596
|
+
globalEntryCache.set(parentSid, fallbackMap);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
// 将模块级缓存中的最新状态同步到 KV
|
|
601
|
+
if (found && parentCache) {
|
|
602
|
+
const data = loadSessionData();
|
|
603
|
+
data[parentSid] = { ...data[parentSid], ts: nowTs, entries: [...parentCache.values()] };
|
|
604
|
+
saveSessionData(data);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
catch { }
|
|
609
|
+
// Delayed backfill: re-read data after state sync catches up, to capture the final
|
|
610
|
+
// token/cost values that may not have been available when session.idle fired.
|
|
611
|
+
setTimeout(() => {
|
|
612
|
+
if (disposed)
|
|
613
|
+
return;
|
|
614
|
+
const finalTokens = props.api.usage.readSessionTokens(sid);
|
|
615
|
+
const finalCost = props.api.usage.readSessionCost(sid);
|
|
616
|
+
const finalModel = props.api.usage.readSessionModel(sid);
|
|
617
|
+
const finalTodo = props.api.usage.readSessionTodo(sid);
|
|
618
|
+
setEntryMap((prev) => {
|
|
619
|
+
let changed = false;
|
|
620
|
+
const next = new Map(prev);
|
|
621
|
+
for (const [id, entry] of next) {
|
|
622
|
+
if (entry.sessionId !== sid)
|
|
623
|
+
continue;
|
|
624
|
+
const t = finalTokens ?? entry.tokens;
|
|
625
|
+
const c = finalCost ?? entry.cost;
|
|
626
|
+
const m = finalModel ?? entry.model;
|
|
627
|
+
const tt = finalTodo?.total ?? entry.todoTotal;
|
|
628
|
+
const td = finalTodo?.done ?? entry.todoDone;
|
|
629
|
+
if (t !== entry.tokens || c !== entry.cost || m !== entry.model ||
|
|
630
|
+
tt !== entry.todoTotal || td !== entry.todoDone) {
|
|
631
|
+
next.set(id, { ...entry, tokens: t, cost: c, model: m, todoTotal: tt, todoDone: td });
|
|
632
|
+
changed = true;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
return changed ? next : prev;
|
|
636
|
+
});
|
|
637
|
+
bump();
|
|
638
|
+
}, 150);
|
|
639
|
+
};
|
|
640
|
+
// ── bumpRenderTick: force re-render (visual-cache pattern) ──
|
|
641
|
+
const bump = () => setRenderTick((v) => v + 1);
|
|
642
|
+
onMount(() => {
|
|
643
|
+
// Fast clock for smooth time display, separate from token polling
|
|
644
|
+
const clock = setInterval(() => { setNow(Date.now()); bump(); }, 100);
|
|
645
|
+
// Token poll — runs every 500ms for running entries
|
|
646
|
+
const tokenTimer = setInterval(() => {
|
|
647
|
+
untrack(() => {
|
|
648
|
+
setEntryMapRaw((prev) => {
|
|
649
|
+
let changed = false;
|
|
650
|
+
const next = new Map(prev);
|
|
651
|
+
for (const [id, entry] of next) {
|
|
652
|
+
if (entry.status === "running" && entry.sessionId) {
|
|
653
|
+
// Only read from child sessions, never the parent
|
|
654
|
+
let isChild = false;
|
|
655
|
+
try {
|
|
656
|
+
const s = props.api.session.get(entry.sessionId);
|
|
657
|
+
isChild = s?.parentID === props.sessionId;
|
|
658
|
+
}
|
|
659
|
+
catch { }
|
|
660
|
+
if (!isChild)
|
|
661
|
+
continue;
|
|
662
|
+
const total = props.api.usage.readSessionTokens(entry.sessionId);
|
|
663
|
+
const todo = props.api.usage.readSessionTodo(entry.sessionId);
|
|
664
|
+
const model = entry.model ?? props.api.usage.readSessionModel(entry.sessionId);
|
|
665
|
+
const nextEntry = { ...entry };
|
|
666
|
+
if (total !== undefined && total !== entry.tokens) {
|
|
667
|
+
nextEntry.tokens = total;
|
|
668
|
+
changed = true;
|
|
669
|
+
}
|
|
670
|
+
if (todo !== undefined) {
|
|
671
|
+
if (todo.total !== entry.todoTotal || todo.done !== entry.todoDone) {
|
|
672
|
+
nextEntry.todoTotal = todo.total;
|
|
673
|
+
nextEntry.todoDone = todo.done;
|
|
674
|
+
changed = true;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
if (model && !entry.model) {
|
|
678
|
+
nextEntry.model = model;
|
|
679
|
+
changed = true;
|
|
680
|
+
}
|
|
681
|
+
if (changed)
|
|
682
|
+
next.set(id, nextEntry);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return changed ? next : prev;
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
bump();
|
|
689
|
+
}, 500);
|
|
690
|
+
bump();
|
|
691
|
+
const unsubPart = props.api.event.on("part.updated", (e) => {
|
|
692
|
+
handlePartUpdated(e);
|
|
693
|
+
bump();
|
|
694
|
+
});
|
|
695
|
+
const unsubMsg = props.api.event.on("message.updated", () => bump());
|
|
696
|
+
const unsubIdle = props.api.event.on("session.idle", (e) => {
|
|
697
|
+
handleSessionEnd(e, "done");
|
|
698
|
+
bump();
|
|
699
|
+
});
|
|
700
|
+
const unsubError = props.api.event.on("session.error", (e) => {
|
|
701
|
+
handleSessionEnd(e, "error");
|
|
702
|
+
bump();
|
|
703
|
+
});
|
|
704
|
+
onCleanup(() => {
|
|
705
|
+
disposed = true;
|
|
706
|
+
clearInterval(clock);
|
|
707
|
+
clearInterval(tokenTimer);
|
|
708
|
+
unsubPart();
|
|
709
|
+
unsubMsg();
|
|
710
|
+
unsubIdle();
|
|
711
|
+
unsubError();
|
|
712
|
+
});
|
|
713
|
+
});
|
|
714
|
+
// ── session‑switch & initial‑load scan ──
|
|
715
|
+
// On session change: load from kv (entries survive component unmount), then scan+merge.
|
|
716
|
+
// On same session: only scan+merge (keep event‑driven running entries).
|
|
717
|
+
let lastSid = props.sessionId;
|
|
718
|
+
let lastTick = 0;
|
|
719
|
+
createEffect(() => {
|
|
720
|
+
const sid = props.sessionId;
|
|
721
|
+
const switched = sid !== lastSid;
|
|
722
|
+
lastSid = sid;
|
|
723
|
+
const tick = clearTick(); // 外部触发清除时 +1,effect 重跑
|
|
724
|
+
const forceReload = tick !== lastTick && !switched;
|
|
725
|
+
lastTick = tick;
|
|
726
|
+
const t = setTimeout(() => {
|
|
727
|
+
untrack(() => {
|
|
728
|
+
if (switched) {
|
|
729
|
+
const { parentSid, isChild } = resolveParent(sid);
|
|
730
|
+
const data = loadSessionData();
|
|
731
|
+
const saved = isChild
|
|
732
|
+
? data[parentSid]?.children?.[sid]?.scroll ?? 0
|
|
733
|
+
: data[sid]?.scroll ?? 0;
|
|
734
|
+
setScrollOffset(saved);
|
|
735
|
+
// 刷新父会话的访问时间 TTL,防止活跃会话的数据过期
|
|
736
|
+
if (!isChild && data[sid]?.entries?.length) {
|
|
737
|
+
data[sid].ts = Date.now();
|
|
738
|
+
saveSessionData(data);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
// scan uses setEntryMapRaw — ephemeral data, not persisted to kv.
|
|
742
|
+
// Only event-driven changes (handlePartUpdated, handleSessionEnd) persist.
|
|
743
|
+
setEntryMapRaw((prev) => {
|
|
744
|
+
// 优先从模块级缓存加载,KV 仅作缓存未命中时的回退
|
|
745
|
+
const next = (switched || forceReload)
|
|
746
|
+
? new Map(globalEntryCache.get(sid) ?? loadEntries(sid))
|
|
747
|
+
: new Map(prev);
|
|
748
|
+
// 从 KV 加载当前会话的清除名单,扫描时跳过被手动清除的历史条目
|
|
749
|
+
const { parentSid: scanPSid, isChild: scanChild } = resolveParent(sid);
|
|
750
|
+
const scanRec = loadSessionData()[scanPSid];
|
|
751
|
+
const clearedIds = new Set(scanChild ? scanRec?.children?.[sid]?.clearedIds : scanRec?.clearedIds);
|
|
752
|
+
try {
|
|
753
|
+
const msgs = props.api.session.messages(sid);
|
|
754
|
+
if (msgs && msgs.length) {
|
|
755
|
+
for (const msg of msgs) {
|
|
756
|
+
const parts = props.api.session.part(msg.id) ?? [];
|
|
757
|
+
for (const partRaw of parts) {
|
|
758
|
+
const part = partRaw;
|
|
759
|
+
// Subtask entries are purely event-driven — never created by scan.
|
|
760
|
+
// (SubtaskPart exists from spawn, not completion, so we cannot infer status.)
|
|
761
|
+
if (part.type === "tool") {
|
|
762
|
+
const tool = String(part.tool ?? "");
|
|
763
|
+
if (!SUBAGENT_TOOLS.has(tool))
|
|
764
|
+
continue;
|
|
765
|
+
const id = `tool:${String(part.id ?? "")}`;
|
|
766
|
+
if (!part.id)
|
|
767
|
+
continue;
|
|
768
|
+
const st = part.state;
|
|
769
|
+
const rawStatus = String(st?.status ?? "");
|
|
770
|
+
const exists = next.get(id);
|
|
771
|
+
// 已手动清除的条目:scan 发现但不在内存 → 跳过重建
|
|
772
|
+
if (!exists && clearedIds.has(id))
|
|
773
|
+
continue;
|
|
774
|
+
// Only create entries for tool calls that entered execution.
|
|
775
|
+
// "pending" / empty: skip new entries; allow heuristics for existing ones below.
|
|
776
|
+
if ((rawStatus === "pending" || rawStatus === "") && !exists)
|
|
777
|
+
continue;
|
|
778
|
+
// "error": only update existing, never create a new entry
|
|
779
|
+
if (rawStatus === "error") {
|
|
780
|
+
if (exists && exists.status === "running") {
|
|
781
|
+
next.set(id, { ...exists, status: "error", endedAt: Date.now() });
|
|
782
|
+
}
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
let status = "running";
|
|
786
|
+
if (rawStatus === "completed")
|
|
787
|
+
status = "done";
|
|
788
|
+
// Background tasks: tool completion ≠ agent completion — keep running until session.idle
|
|
789
|
+
// Only keep running if state metadata confirms a child session was spawned.
|
|
790
|
+
if ((st?.input?.run_in_background === true || st?.input?.background === true) && status === "done") {
|
|
791
|
+
const scanStMeta = st?.metadata;
|
|
792
|
+
const scanHasChild = scanStMeta?.session_id !== undefined || scanStMeta?.sessionId !== undefined;
|
|
793
|
+
if (scanHasChild)
|
|
794
|
+
status = "running";
|
|
795
|
+
}
|
|
796
|
+
// Already settled → skip
|
|
797
|
+
if (exists && exists.status !== "running" && exists.status !== "cancel_requested")
|
|
798
|
+
continue;
|
|
799
|
+
// Running entry with no explicit status improvement from part:
|
|
800
|
+
// try message-level heuristics first, then time-based fallback.
|
|
801
|
+
if (exists && status === "running") {
|
|
802
|
+
if (!rawStatus) {
|
|
803
|
+
const msgTokens = msg?.tokens;
|
|
804
|
+
if (msgTokens && (Number(msgTokens.input) > 0 || Number(msgTokens.output) > 0)) {
|
|
805
|
+
status = "done"; // LLM returned tokens → agent completed
|
|
806
|
+
}
|
|
807
|
+
else if (Date.now() - exists.startedAt > 30 * 60 * 1000) {
|
|
808
|
+
status = "done"; // >30 min idle → assume completed
|
|
809
|
+
}
|
|
810
|
+
else {
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
// If already tracked as running but tool state says completed/error → update
|
|
819
|
+
// If not tracked → add fresh
|
|
820
|
+
const input = st?.input;
|
|
821
|
+
const agent = String(part.subagent_type ?? input?.agent ?? input?.subagent_type ?? tool);
|
|
822
|
+
const prompt = String(input?.prompt ?? part.description ?? "");
|
|
823
|
+
const desc = input?.description !== undefined ? String(input.description) : "";
|
|
824
|
+
const title = desc || truncate(prompt.replace(/\n/g, " ").trim(), 40);
|
|
825
|
+
let tokens;
|
|
826
|
+
const scanStMeta2 = st?.metadata;
|
|
827
|
+
const scanSubSid = scanStMeta2?.session_id !== undefined ? String(scanStMeta2.session_id)
|
|
828
|
+
: scanStMeta2?.sessionId !== undefined ? String(scanStMeta2.sessionId)
|
|
829
|
+
: undefined;
|
|
830
|
+
if (scanSubSid)
|
|
831
|
+
tokens = props.api.usage.readSessionTokens(scanSubSid);
|
|
832
|
+
const ended = status === "done"; // "error" handled above, never reaches here
|
|
833
|
+
next.set(id, {
|
|
834
|
+
id, title, agent, prompt,
|
|
835
|
+
// Preserve existing values (from handleSessionEnd / KV) — scan must not overwrite
|
|
836
|
+
tokens: exists?.tokens ?? tokens,
|
|
837
|
+
sessionId: exists?.sessionId ?? scanSubSid,
|
|
838
|
+
status,
|
|
839
|
+
startedAt: exists?.startedAt || Date.now(),
|
|
840
|
+
endedAt: ended ? (exists?.endedAt || Date.now()) : undefined,
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
catch { }
|
|
848
|
+
return next;
|
|
849
|
+
});
|
|
850
|
+
// Reconcile: check running entries against live child session status.
|
|
851
|
+
// Covers session.idle events missed while user was inside a child session.
|
|
852
|
+
setEntryMapRaw((prev) => {
|
|
853
|
+
let changed = false;
|
|
854
|
+
const next = new Map(prev);
|
|
855
|
+
for (const [id, entry] of next) {
|
|
856
|
+
if ((entry.status !== "running" && entry.status !== "cancel_requested") || !entry.sessionId)
|
|
857
|
+
continue;
|
|
858
|
+
try {
|
|
859
|
+
const st = props.api.session.status(entry.sessionId);
|
|
860
|
+
if (!st || st.type !== "idle")
|
|
861
|
+
continue;
|
|
862
|
+
const tokens = props.api.usage.readSessionTokens(entry.sessionId);
|
|
863
|
+
const cost = props.api.usage.readSessionCost(entry.sessionId);
|
|
864
|
+
const finalStatus = entry.status === "cancel_requested" && entry.abortAccepted
|
|
865
|
+
? "cancelled"
|
|
866
|
+
: "done";
|
|
867
|
+
next.set(id, {
|
|
868
|
+
...entry, status: finalStatus, endedAt: Date.now(),
|
|
869
|
+
tokens: tokens ?? entry.tokens,
|
|
870
|
+
cost: cost ?? entry.cost,
|
|
871
|
+
});
|
|
872
|
+
changed = true;
|
|
873
|
+
}
|
|
874
|
+
catch { }
|
|
875
|
+
}
|
|
876
|
+
return changed ? next : prev;
|
|
877
|
+
});
|
|
878
|
+
bump();
|
|
879
|
+
});
|
|
880
|
+
}, 150);
|
|
881
|
+
onCleanup(() => clearTimeout(t));
|
|
882
|
+
});
|
|
883
|
+
// ── palette ──
|
|
884
|
+
const pal = createMemo(() => {
|
|
885
|
+
const th = props.theme;
|
|
886
|
+
const sat = (k, fb) => desaturateTo(th[k], MAX_SAT, fb);
|
|
887
|
+
return {
|
|
888
|
+
primary: sat("primary", FALLBACK.primary),
|
|
889
|
+
text: sat("text", FALLBACK.text),
|
|
890
|
+
muted: sat("textMuted", FALLBACK.muted),
|
|
891
|
+
success: sat("success", FALLBACK.success),
|
|
892
|
+
warning: sat("warning", FALLBACK.warning),
|
|
893
|
+
error: sat("error", FALLBACK.error),
|
|
894
|
+
border: sat("border", FALLBACK.border),
|
|
895
|
+
};
|
|
896
|
+
});
|
|
897
|
+
// ── derived signals ──
|
|
898
|
+
// Stable list — only changes when entryMap changes
|
|
899
|
+
const entryList = createMemo(() => {
|
|
900
|
+
const entries = [...entryMap().values()];
|
|
901
|
+
if (props.sortOrder() === "desc") {
|
|
902
|
+
return entries.sort((a, b) => b.startedAt - a.startedAt);
|
|
903
|
+
}
|
|
904
|
+
return entries.sort((a, b) => a.startedAt - b.startedAt);
|
|
905
|
+
});
|
|
906
|
+
const max = props.maxEntries;
|
|
907
|
+
const clampedOffset = createMemo(() => {
|
|
908
|
+
const total = entryList().length;
|
|
909
|
+
const m = max();
|
|
910
|
+
if (total <= m)
|
|
911
|
+
return 0;
|
|
912
|
+
return Math.min(scrollOffset(), total - m);
|
|
913
|
+
});
|
|
914
|
+
const visibleList = createMemo(() => entryList().slice(clampedOffset(), clampedOffset() + max()));
|
|
915
|
+
const hiddenAbove = createMemo(() => clampedOffset());
|
|
916
|
+
const hiddenBelow = createMemo(() => Math.max(0, entryList().length - clampedOffset() - max()));
|
|
917
|
+
// Drop hover state when ↑ more disappears (hiddenAbove hits zero)
|
|
918
|
+
createEffect(() => {
|
|
919
|
+
if (hiddenAbove() === 0)
|
|
920
|
+
setHoveredMoreAbove(false);
|
|
921
|
+
});
|
|
922
|
+
// Reset scroll on sort order change: jump to newest in view
|
|
923
|
+
let sortInitialized = false;
|
|
924
|
+
createEffect(() => {
|
|
925
|
+
props.sortOrder();
|
|
926
|
+
if (!sortInitialized) {
|
|
927
|
+
sortInitialized = true;
|
|
928
|
+
return;
|
|
929
|
+
}
|
|
930
|
+
const total = untrack(() => entryList().length);
|
|
931
|
+
const m = untrack(() => max());
|
|
932
|
+
const target = props.sortOrder() === "desc" ? 0 : Math.max(0, total - m);
|
|
933
|
+
setScrollOffset(target);
|
|
934
|
+
setTimeout(() => {
|
|
935
|
+
try {
|
|
936
|
+
persistScroll(props.sessionId, target);
|
|
937
|
+
}
|
|
938
|
+
catch { }
|
|
939
|
+
}, 0);
|
|
940
|
+
});
|
|
941
|
+
// When new entries arrive while viewing the newest end, keep the view at newest
|
|
942
|
+
let prevEntryCount = 0;
|
|
943
|
+
createEffect(() => {
|
|
944
|
+
const total = entryList().length;
|
|
945
|
+
if (prevEntryCount === 0) {
|
|
946
|
+
prevEntryCount = total;
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
if (total === prevEntryCount)
|
|
950
|
+
return;
|
|
951
|
+
const m = max();
|
|
952
|
+
const wasAtNewest = props.sortOrder() === "desc"
|
|
953
|
+
? untrack(() => scrollOffset() === 0)
|
|
954
|
+
: untrack(() => scrollOffset() >= prevEntryCount - m);
|
|
955
|
+
prevEntryCount = total;
|
|
956
|
+
if (wasAtNewest) {
|
|
957
|
+
const target = props.sortOrder() === "desc" ? 0 : Math.max(0, total - m);
|
|
958
|
+
setScrollOffset(target);
|
|
959
|
+
setTimeout(() => {
|
|
960
|
+
try {
|
|
961
|
+
persistScroll(props.sessionId, target);
|
|
962
|
+
}
|
|
963
|
+
catch { }
|
|
964
|
+
}, 0);
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
const entries = createMemo(() => {
|
|
968
|
+
const nowVal = now();
|
|
969
|
+
return entryList().map((e) => ({
|
|
970
|
+
...e,
|
|
971
|
+
elapsed: (e.endedAt ?? nowVal) - e.startedAt,
|
|
972
|
+
}));
|
|
973
|
+
});
|
|
974
|
+
const doneCount = createMemo(() => entryList().filter((e) => e.status === "done" || e.status === "cancelled").length);
|
|
975
|
+
const runningCount = createMemo(() => entryList().filter((e) => e.status === "running" || e.status === "cancel_requested").length);
|
|
976
|
+
const errCount = createMemo(() => entryList().filter((e) => e.status === "error").length);
|
|
977
|
+
const anyEntry = () => entryList().length > 0;
|
|
978
|
+
const totalTokens = createMemo(() => {
|
|
979
|
+
let sum = 0;
|
|
980
|
+
for (const e of entryList()) {
|
|
981
|
+
if (e.tokens)
|
|
982
|
+
sum += e.tokens;
|
|
983
|
+
}
|
|
984
|
+
return sum;
|
|
985
|
+
});
|
|
986
|
+
const totalCost = createMemo(() => {
|
|
987
|
+
let sum = 0;
|
|
988
|
+
for (const e of entryList()) {
|
|
989
|
+
if (e.cost)
|
|
990
|
+
sum += e.cost;
|
|
991
|
+
}
|
|
992
|
+
return sum;
|
|
993
|
+
});
|
|
994
|
+
const toggleExpand = (id) => {
|
|
995
|
+
setExpanded((prev) => {
|
|
996
|
+
const next = prev === id ? undefined : id;
|
|
997
|
+
try {
|
|
998
|
+
persistExpanded(props.sessionId, next ?? "");
|
|
999
|
+
}
|
|
1000
|
+
catch { }
|
|
1001
|
+
return next;
|
|
1002
|
+
});
|
|
1003
|
+
};
|
|
1004
|
+
const sep = () => "\u2500".repeat(Math.max(1, panelWidth()));
|
|
1005
|
+
// ── expanded detail right-align ──
|
|
1006
|
+
const expandedMaxLabelW = createMemo(() => {
|
|
1007
|
+
const labels = [
|
|
1008
|
+
t("agent.label"), t("status.label"), t("time.label"), t("tokens.label"),
|
|
1009
|
+
t("error.label"), t("cost.label"), t("model.label"), t("todo.label"), t("session.label"),
|
|
1010
|
+
];
|
|
1011
|
+
return Math.max(...labels.map(l => visualWidth(l + ": ")));
|
|
1012
|
+
});
|
|
1013
|
+
const expandedPad = (label) => Math.max(0, expandedMaxLabelW() - visualWidth(label + ": "));
|
|
1014
|
+
const expandedValAvail = () => Math.max(6, panelWidth() - INDENT - expandedMaxLabelW());
|
|
1015
|
+
// ── header parts for colored spans ──
|
|
1016
|
+
const summaryParts = createMemo(() => {
|
|
1017
|
+
if (!anyEntry())
|
|
1018
|
+
return null;
|
|
1019
|
+
const dot = "\u25cf";
|
|
1020
|
+
const cost = totalCost();
|
|
1021
|
+
return {
|
|
1022
|
+
done: `${dot}${doneCount()}`,
|
|
1023
|
+
running: runningCount() > 0 ? `${dot}${runningCount()}` : null,
|
|
1024
|
+
err: errCount() > 0 ? `${dot}${errCount()}` : null,
|
|
1025
|
+
duration: totalTokens() > 0 ? fmtTokens(totalTokens()) : "",
|
|
1026
|
+
cost: cost > 0 ? `$${cost.toFixed(2)}` : "",
|
|
1027
|
+
};
|
|
1028
|
+
});
|
|
1029
|
+
const summaryCols = createMemo(() => {
|
|
1030
|
+
const p = summaryParts();
|
|
1031
|
+
if (!p)
|
|
1032
|
+
return 0;
|
|
1033
|
+
let w = visualWidth(p.done);
|
|
1034
|
+
if (p.running)
|
|
1035
|
+
w += 1 + visualWidth(p.running);
|
|
1036
|
+
if (p.err)
|
|
1037
|
+
w += 1 + visualWidth(p.err);
|
|
1038
|
+
w += p.duration ? 1 + visualWidth(p.duration) : 0;
|
|
1039
|
+
w += p.cost ? 1 + visualWidth(p.cost) : 0;
|
|
1040
|
+
return w;
|
|
1041
|
+
});
|
|
1042
|
+
const versionText = ` v${PLUGIN_VERSION}`;
|
|
1043
|
+
const versionW = visualWidth(versionText);
|
|
1044
|
+
const showVersion = createMemo(() => {
|
|
1045
|
+
if (!open())
|
|
1046
|
+
return false;
|
|
1047
|
+
const icon = "\u25bc";
|
|
1048
|
+
const need = visualWidth(icon) + 1 + visualWidth(t("panel.title")) + versionW + summaryCols();
|
|
1049
|
+
return need <= panelWidth();
|
|
1050
|
+
});
|
|
1051
|
+
const leftCols = createMemo(() => {
|
|
1052
|
+
const icon = open() ? "\u25bc" : "\u25b6";
|
|
1053
|
+
let w = visualWidth(icon) + 1 + visualWidth(t("panel.title"));
|
|
1054
|
+
if (showVersion())
|
|
1055
|
+
w += versionW;
|
|
1056
|
+
return w;
|
|
1057
|
+
});
|
|
1058
|
+
const spacerCols = createMemo(() => {
|
|
1059
|
+
if (!anyEntry())
|
|
1060
|
+
return 0;
|
|
1061
|
+
return Math.max(0, panelWidth() - leftCols() - summaryCols());
|
|
1062
|
+
});
|
|
1063
|
+
const valueCols = (label) => Math.max(4, panelWidth() - INDENT - visualWidth(label + ": "));
|
|
1064
|
+
// ── render ──
|
|
1065
|
+
return (_jsxs("box", { border: false, paddingTop: 0, paddingBottom: 0, paddingLeft: 0, paddingRight: 0, flexDirection: "column", gap: 0, ref: boxEl, onSizeChange: () => {
|
|
1066
|
+
const w = boxEl ? Math.max(20, boxEl.width ?? 0) : 28;
|
|
1067
|
+
setPanelWidth((prev) => (prev === w ? prev : w));
|
|
1068
|
+
}, children: [_jsxs("text", { onMouseUp: () => {
|
|
1069
|
+
setOpen((o) => {
|
|
1070
|
+
const n = !o;
|
|
1071
|
+
try {
|
|
1072
|
+
props.api.kv.set(`${KV_PREFIX}.open`, n);
|
|
1073
|
+
}
|
|
1074
|
+
catch { }
|
|
1075
|
+
return n;
|
|
1076
|
+
});
|
|
1077
|
+
bump();
|
|
1078
|
+
}, children: [_jsx("span", { style: { fg: pal().muted }, children: renderTick() >= 0 && open() ? "\u25bc " : "\u25b6 " }), _jsx("span", { style: { fg: pal().primary }, children: t("panel.title") }), _jsx(Show, { when: showVersion(), children: _jsx("span", { style: { fg: dimColor(pal().muted, 0.75) }, children: versionText }) }), anyEntry() ? (_jsxs(_Fragment, { children: [_jsx("span", { style: { fg: pal().muted }, children: " ".repeat(spacerCols()) }), _jsx("span", { style: { fg: pal().success }, children: summaryParts().done }), runningCount() > 0 && (_jsxs("span", { style: { fg: pal().warning }, children: [" ", summaryParts().running] })), errCount() > 0 && (_jsxs("span", { style: { fg: pal().error }, children: [" ", summaryParts().err] })), summaryParts().duration ? (_jsxs("span", { style: { fg: pal().muted }, children: [" ", summaryParts().duration] })) : null, summaryParts().cost ? (_jsxs("span", { style: { fg: pal().warning }, children: [" ", summaryParts().cost] })) : null] })) : null] }), _jsxs(Show, { when: open(), children: [_jsx("text", { fg: pal().muted, children: sep() }), _jsx(Show, { when: anyEntry(), fallback: _jsxs("text", { style: { fg: pal().muted }, children: [" ", "> ", t("status.none"), " "] }), children: _jsxs("box", { onMouseScroll: (e) => {
|
|
1079
|
+
if (props.scrollMode() === "click")
|
|
1080
|
+
return;
|
|
1081
|
+
const total = entryList().length;
|
|
1082
|
+
const m = max();
|
|
1083
|
+
if (total <= m)
|
|
1084
|
+
return;
|
|
1085
|
+
const dir = e.button === 0 ? 1 : -1;
|
|
1086
|
+
setScrollOffset((prev) => {
|
|
1087
|
+
const next = Math.max(0, Math.min(prev + dir, total - m));
|
|
1088
|
+
try {
|
|
1089
|
+
persistScroll(props.sessionId, next);
|
|
1090
|
+
}
|
|
1091
|
+
catch { }
|
|
1092
|
+
return next;
|
|
1093
|
+
});
|
|
1094
|
+
}, children: [_jsx(Show, { when: hiddenAbove() > 0, children: _jsx("text", { onMouseOver: () => setHoveredMoreAbove(true), onMouseOut: () => setHoveredMoreAbove(false), onMouseUp: () => {
|
|
1095
|
+
const total = entryList().length;
|
|
1096
|
+
const m = max();
|
|
1097
|
+
if (total <= m)
|
|
1098
|
+
return;
|
|
1099
|
+
const next = Math.max(0, scrollOffset() - m);
|
|
1100
|
+
if (next === 0) {
|
|
1101
|
+
setTimeout(() => {
|
|
1102
|
+
setScrollOffset(next);
|
|
1103
|
+
try {
|
|
1104
|
+
persistScroll(props.sessionId, next);
|
|
1105
|
+
}
|
|
1106
|
+
catch { }
|
|
1107
|
+
}, 0);
|
|
1108
|
+
}
|
|
1109
|
+
else {
|
|
1110
|
+
setScrollOffset(next);
|
|
1111
|
+
try {
|
|
1112
|
+
persistScroll(props.sessionId, next);
|
|
1113
|
+
}
|
|
1114
|
+
catch { }
|
|
1115
|
+
}
|
|
1116
|
+
}, children: _jsxs("span", { style: { fg: hoveredMoreAbove() ? pal().warning : pal().muted }, children: [" ", "\u2191 ", hiddenAbove(), " ", t("scroll.more")] }) }) }), _jsx(For, { each: visibleList(), children: (entry) => {
|
|
1117
|
+
const isExpanded = () => expanded() === entry.id;
|
|
1118
|
+
const isRunning = entry.status === "running";
|
|
1119
|
+
const isCancelRequested = entry.status === "cancel_requested";
|
|
1120
|
+
const isCancelled = entry.status === "cancelled";
|
|
1121
|
+
const isError = entry.status === "error";
|
|
1122
|
+
const isActiveRunning = isRunning || isCancelRequested;
|
|
1123
|
+
const elapsed = () => (entry.endedAt ?? now()) - entry.startedAt;
|
|
1124
|
+
const statusDot = () => "\u25cf";
|
|
1125
|
+
const statusColor = () => {
|
|
1126
|
+
if (isCancelled)
|
|
1127
|
+
return pal().muted;
|
|
1128
|
+
if (!isActiveRunning)
|
|
1129
|
+
return isError ? pal().error : pal().success;
|
|
1130
|
+
const t = (Math.sin(((now() % 2000) / 2000) * Math.PI * 2 - Math.PI / 2) + 1) / 2;
|
|
1131
|
+
const a = rgb(pal().muted), b = rgb(pal().warning);
|
|
1132
|
+
if (!a || !b)
|
|
1133
|
+
return pal().warning;
|
|
1134
|
+
const r = Math.round(a.r + (b.r - a.r) * t);
|
|
1135
|
+
const g = Math.round(a.g + (b.g - a.g) * t);
|
|
1136
|
+
const bl = Math.round(a.b + (b.b - a.b) * t);
|
|
1137
|
+
return "#" + [r, g, bl].map((v) => Math.max(0, Math.min(255, v)).toString(16).padStart(2, "0")).join("");
|
|
1138
|
+
};
|
|
1139
|
+
const timeColor = () => isActiveRunning ? pal().warning : isError ? pal().error : pal().muted;
|
|
1140
|
+
// Entry label: collapsed shows title only, expanded shows title only too
|
|
1141
|
+
const tokenText = () => !isExpanded() && entry.tokens !== undefined && entry.tokens > 0
|
|
1142
|
+
? ` ${fmtTokens(entry.tokens)}`
|
|
1143
|
+
: "";
|
|
1144
|
+
const timeText = () => !isExpanded() && (elapsed() >= 2000 || entry.endedAt !== undefined)
|
|
1145
|
+
? fmtDurationShort(elapsed(), isActiveRunning)
|
|
1146
|
+
: "";
|
|
1147
|
+
const suffixW = () => {
|
|
1148
|
+
let w = 0;
|
|
1149
|
+
const t = timeText();
|
|
1150
|
+
if (t)
|
|
1151
|
+
w += 1 + visualWidth(t);
|
|
1152
|
+
const tk = tokenText();
|
|
1153
|
+
if (tk)
|
|
1154
|
+
w += visualWidth(tk);
|
|
1155
|
+
return w;
|
|
1156
|
+
};
|
|
1157
|
+
const labelAvail = () => Math.max(6, panelWidth() - LEFT_PAD - suffixW());
|
|
1158
|
+
const labelText = () => {
|
|
1159
|
+
const max = labelAvail();
|
|
1160
|
+
const text = entry.title || entry.agent;
|
|
1161
|
+
const truncated = truncate(text, max);
|
|
1162
|
+
const pad = Math.max(0, max - visualWidth(truncated));
|
|
1163
|
+
return truncated + " ".repeat(pad);
|
|
1164
|
+
};
|
|
1165
|
+
return (_jsxs(_Fragment, { children: [_jsxs("text", { onMouseUp: () => toggleExpand(entry.id), children: [_jsx("span", { style: { fg: pal().muted }, children: isExpanded() ? "\u25bc" : "\u25b6" }), " ", _jsx("span", { style: { fg: statusColor() }, children: statusDot() }), " ", _jsx("span", { style: { fg: pal().text }, children: labelText() }), timeText() ? (_jsxs(_Fragment, { children: [" ", _jsx("span", { style: { fg: timeColor() }, children: timeText() })] })) : null, tokenText() ? (_jsx("span", { style: { fg: pal().muted }, children: tokenText() })) : null] }), _jsxs(Show, { when: isExpanded(), children: [_jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("agent.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("agent.label"))) }), _jsx("span", { style: { fg: pal().muted }, children: entry.agent })] }), _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("status.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("status.label"))) }), _jsx("span", { style: { fg: isActiveRunning ? pal().warning : isCancelled ? pal().muted : isError ? pal().error : pal().success }, children: isActiveRunning ? t("status.running") : isCancelled ? t("status.cancelled") : isError ? t("status.error") : t("status.done") })] }), _jsx(Show, { when: elapsed() >= 2000 || entry.endedAt !== undefined, children: _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("time.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("time.label"))) }), _jsx("span", { style: { fg: pal().muted }, children: fmtDurationShort(elapsed(), isActiveRunning) })] }) }), _jsx(Show, { when: entry.tokens !== undefined, children: _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("tokens.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("tokens.label"))) }), _jsx("span", { style: { fg: pal().muted }, children: fmtTokens(entry.tokens) })] }) }), _jsx(Show, { when: entry.error, children: _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().error }, children: [t("error.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("error.label"))) }), _jsx("span", { style: { fg: pal().error }, children: truncate(String(entry.error), expandedValAvail()) })] }) }), _jsx(Show, { when: entry.cost !== undefined, children: (() => {
|
|
1166
|
+
const cost = entry.cost;
|
|
1167
|
+
return (_jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("cost.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("cost.label"))) }), _jsxs("span", { style: { fg: pal().muted }, children: ["$", cost.toFixed(4)] })] }));
|
|
1168
|
+
})() }), _jsx(Show, { when: entry.model, children: _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("model.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("model.label"))) }), _jsx("span", { style: { fg: pal().muted }, children: truncate(entry.model, expandedValAvail()) })] }) }), _jsx(Show, { when: entry.todoTotal !== undefined, children: _jsxs("text", { children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("todo.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("todo.label"))) }), _jsxs("span", { style: { fg: pal().muted }, children: [entry.todoDone, "/", entry.todoTotal] })] }) }), _jsx(Show, { when: entry.sessionId, children: _jsxs("text", { onMouseUp: async () => {
|
|
1169
|
+
const sessionId = entry.sessionId;
|
|
1170
|
+
if (!sessionId)
|
|
1171
|
+
return;
|
|
1172
|
+
const result = await copyText(sessionId);
|
|
1173
|
+
if (result.copied) {
|
|
1174
|
+
props.api.ui.toast(t("session.toast.copied"), {
|
|
1175
|
+
variant: "success",
|
|
1176
|
+
title: entry.title || entry.agent,
|
|
1177
|
+
duration: 2500,
|
|
1178
|
+
});
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
props.api.ui.toast(`${sessionId}\n\n${t("session.toast.copy_failed")}`, {
|
|
1182
|
+
variant: "warning",
|
|
1183
|
+
title: entry.title || entry.agent,
|
|
1184
|
+
duration: 8000,
|
|
1185
|
+
});
|
|
1186
|
+
}, children: [" ", _jsxs("span", { style: { fg: pal().primary }, children: [t("session.label"), ": "] }), _jsx("span", { style: { fg: pal().muted }, children: " ".repeat(expandedPad(t("session.label"))) }), _jsx("span", { style: { fg: pal().muted }, children: truncate(entry.sessionId, expandedValAvail() - visualWidth(" ⎘")) }), _jsx("span", { style: { fg: pal().warning }, children: " \u2398" })] }) }), _jsx(Show, { when: entry.sessionId || isRunning, children: (() => {
|
|
1187
|
+
const openPrefix = () => " \u2192 ";
|
|
1188
|
+
const openFull = () => entry.sessionId ? openPrefix() + t("open.label") : "";
|
|
1189
|
+
const openW = () => entry.sessionId ? visualWidth(openFull()) : 0;
|
|
1190
|
+
const cancelLabel = () => ` ${t("cancel.label")}`;
|
|
1191
|
+
const dismissLabel = () => ` ${t("dismiss.label")}`;
|
|
1192
|
+
const rightW = (isRunning ? visualWidth(dismissLabel()) : 0) + (isRunning && entry.sessionId ? visualWidth(cancelLabel()) : 0);
|
|
1193
|
+
const spacerW = () => Math.max(1, panelWidth() - openW() - rightW - 2);
|
|
1194
|
+
return (_jsxs("box", { flexDirection: "row", children: [_jsx(Show, { when: entry.sessionId, children: _jsxs("text", { onMouseOver: () => setHoveredOpen(entry.id), onMouseOut: () => setHoveredOpen(undefined), onMouseUp: () => {
|
|
1195
|
+
if (entry.sessionId) {
|
|
1196
|
+
props.api.route.navigateSession(entry.sessionId);
|
|
1197
|
+
}
|
|
1198
|
+
}, children: [_jsx("span", { style: { fg: hoveredOpen() === entry.id ? pal().warning : pal().primary }, children: openPrefix() }), _jsx("span", { style: { fg: hoveredOpen() === entry.id ? pal().warning : pal().primary }, children: t("open.label") })] }) }), _jsx("text", { style: { fg: pal().muted }, children: " ".repeat(spacerW()) }), _jsx(Show, { when: isRunning && entry.sessionId, children: _jsx("text", { onMouseOver: () => setHoveredCancel(entry.id), onMouseOut: () => setHoveredCancel(undefined), onMouseUp: () => cancelEntry(entry), children: _jsx("span", { style: { fg: hoveredCancel() === entry.id ? pal().warning : pal().error }, children: cancelLabel() }) }) }), _jsx(Show, { when: isRunning, children: _jsx("text", { onMouseOver: () => setHoveredDismiss(entry.id), onMouseOut: () => setHoveredDismiss(undefined), onMouseUp: () => {
|
|
1199
|
+
upsertEntry({ id: entry.id, title: entry.title, agent: entry.agent, prompt: entry.prompt, status: "done" });
|
|
1200
|
+
}, children: _jsx("span", { style: { fg: hoveredDismiss() === entry.id ? pal().warning : pal().muted }, children: dismissLabel() }) }) })] }));
|
|
1201
|
+
})() })] })] }));
|
|
1202
|
+
} }), _jsx(Show, { when: hiddenBelow() > 0 || (props.sortOrder() === "desc" ? scrollOffset() > 0 : entryList().length > max() && clampedOffset() < entryList().length - max()), children: (() => {
|
|
1203
|
+
const showMore = hiddenBelow() > 0;
|
|
1204
|
+
const showTop = props.sortOrder() === "desc"
|
|
1205
|
+
? scrollOffset() > 0
|
|
1206
|
+
: entryList().length > max() && clampedOffset() < entryList().length - max();
|
|
1207
|
+
const left = showMore ? ` \u2193 ${hiddenBelow()} ${t("scroll.more")}` : " ";
|
|
1208
|
+
const right = props.sortOrder() === "desc"
|
|
1209
|
+
? `\u2191 ${t("scroll.top")}`
|
|
1210
|
+
: `\u2193 ${t("scroll.bottom")}`;
|
|
1211
|
+
const pad = showTop ? Math.max(1, panelWidth() - visualWidth(left) - visualWidth(right)) : 0;
|
|
1212
|
+
return (_jsxs("box", { flexDirection: "row", children: [_jsx("text", { onMouseOver: () => showMore && setHoveredMoreBelow(true), onMouseOut: () => setHoveredMoreBelow(false), onMouseUp: () => {
|
|
1213
|
+
if (!showMore)
|
|
1214
|
+
return;
|
|
1215
|
+
const total = entryList().length;
|
|
1216
|
+
const m = max();
|
|
1217
|
+
if (total <= m)
|
|
1218
|
+
return;
|
|
1219
|
+
setScrollOffset((prev) => Math.min(total - m, prev + m));
|
|
1220
|
+
try {
|
|
1221
|
+
persistScroll(props.sessionId, scrollOffset());
|
|
1222
|
+
}
|
|
1223
|
+
catch { }
|
|
1224
|
+
setHoveredMoreBelow(false);
|
|
1225
|
+
}, children: _jsx("span", { style: { fg: showMore && hoveredMoreBelow() ? pal().warning : pal().muted }, children: left }) }), showTop ? (_jsxs(_Fragment, { children: [_jsx("text", { style: { fg: pal().muted }, children: " ".repeat(pad) }), _jsx("text", { onMouseOver: () => setHoveredTop(true), onMouseOut: () => setHoveredTop(false), onMouseUp: () => {
|
|
1226
|
+
const total = entryList().length;
|
|
1227
|
+
const m = max();
|
|
1228
|
+
if (props.sortOrder() === "desc") {
|
|
1229
|
+
setScrollOffset(0);
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
setScrollOffset(Math.max(0, total - m));
|
|
1233
|
+
}
|
|
1234
|
+
setHoveredTop(false);
|
|
1235
|
+
}, children: _jsx("span", { style: { fg: hoveredTop() ? pal().warning : pal().muted }, children: right }) })] })) : null] }));
|
|
1236
|
+
})() })] }) })] })] }));
|
|
1237
|
+
}
|