@eum-agent/context-management 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +471 -109
- package/package.json +5 -5
- package/ui/ui.js +183 -197
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"en-US": "Session Context Management",
|
|
5
5
|
"zh-CN": "会话上下文管理"
|
|
6
6
|
},
|
|
7
|
-
"version": "1.0
|
|
7
|
+
"version": "1.1.0",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"description": "Conversation history restoration, compaction controls and current-time context",
|
|
10
10
|
"license": "MIT",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@eum-agent/container": "1.0.0",
|
|
25
25
|
"@eum-agent/kernel": "1.0.0",
|
|
26
|
-
"@eum-agent/access-local": "1.0
|
|
27
|
-
"@eum-agent/plugin": "1.0
|
|
26
|
+
"@eum-agent/access-local": "1.1.0",
|
|
27
|
+
"@eum-agent/plugin": "1.1.0",
|
|
28
28
|
"@types/react": "^19.2.18",
|
|
29
29
|
"@types/react-dom": "^19.2.5",
|
|
30
30
|
"vite": "^8.2.2",
|
|
@@ -39,11 +39,11 @@
|
|
|
39
39
|
"zh-CN": "对话历史恢复、压缩控制与当前时间上下文"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@eum-agent/llm-management": "1.0
|
|
42
|
+
"@eum-agent/llm-management": "1.1.0"
|
|
43
43
|
},
|
|
44
44
|
"kind": "extension",
|
|
45
45
|
"foundationDependencies": {
|
|
46
|
-
"@eum-agent/storage-sqlite": "1.0
|
|
46
|
+
"@eum-agent/storage-sqlite": "1.1.0"
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
package/ui/ui.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEffect, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
3
|
//#region ui-src/lib/status.ts
|
|
4
4
|
/** Coalesces watched IDs into bounded collections and fences late replies to connection and watch epochs. */
|
|
5
5
|
function createContextStatusView(ctx) {
|
|
@@ -7,6 +7,7 @@ function createContextStatusView(ctx) {
|
|
|
7
7
|
const watched = /* @__PURE__ */ new Map();
|
|
8
8
|
const queued = /* @__PURE__ */ new Set();
|
|
9
9
|
const active = /* @__PURE__ */ new Map();
|
|
10
|
+
const serverEpochs = /* @__PURE__ */ new Map();
|
|
10
11
|
const listeners = /* @__PURE__ */ new Set();
|
|
11
12
|
let nextEpoch = 0, generation = 0, scheduled = false;
|
|
12
13
|
let online = ctx.gateway.connection.status === "online";
|
|
@@ -38,7 +39,15 @@ function createContextStatusView(ctx) {
|
|
|
38
39
|
changed = values.delete(one.target) || changed;
|
|
39
40
|
continue;
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
+
const nextOrder = one.value.statusOrder;
|
|
43
|
+
const previous = values.get(one.target);
|
|
44
|
+
if (nextOrder) {
|
|
45
|
+
const epoch = serverEpochs.get(one.target);
|
|
46
|
+
if (epoch !== void 0 && epoch !== nextOrder.epoch) continue;
|
|
47
|
+
const prior = previous?.statusOrder;
|
|
48
|
+
if (epoch && prior && (nextOrder.sequence <= prior.sequence || nextOrder.historyGeneration < prior.historyGeneration)) continue;
|
|
49
|
+
serverEpochs.set(one.target, nextOrder.epoch);
|
|
50
|
+
} else if ((previous?.revision ?? -1) > one.value.revision) continue;
|
|
42
51
|
values.set(one.target, one.value);
|
|
43
52
|
changed = true;
|
|
44
53
|
}
|
|
@@ -59,6 +68,7 @@ function createContextStatusView(ctx) {
|
|
|
59
68
|
if (next !== online) {
|
|
60
69
|
generation++;
|
|
61
70
|
active.clear();
|
|
71
|
+
serverEpochs.clear();
|
|
62
72
|
}
|
|
63
73
|
if (next && !online) schedule([...watched.keys()]);
|
|
64
74
|
online = next;
|
|
@@ -68,6 +78,7 @@ function createContextStatusView(ctx) {
|
|
|
68
78
|
queued.clear();
|
|
69
79
|
active.clear();
|
|
70
80
|
values.clear();
|
|
81
|
+
serverEpochs.clear();
|
|
71
82
|
listeners.clear();
|
|
72
83
|
}, { once: true });
|
|
73
84
|
return {
|
|
@@ -100,6 +111,7 @@ function createContextStatusView(ctx) {
|
|
|
100
111
|
watched.delete(sessionId);
|
|
101
112
|
queued.delete(sessionId);
|
|
102
113
|
active.delete(sessionId);
|
|
114
|
+
serverEpochs.delete(sessionId);
|
|
103
115
|
if (values.delete(sessionId)) notify();
|
|
104
116
|
}
|
|
105
117
|
};
|
|
@@ -107,31 +119,25 @@ function createContextStatusView(ctx) {
|
|
|
107
119
|
};
|
|
108
120
|
}
|
|
109
121
|
//#endregion
|
|
110
|
-
//#region ui-src/lib/
|
|
111
|
-
/**
|
|
112
|
-
function
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
id: `failure:${title ?? ""}:${text}`,
|
|
130
|
-
tone: "destructive",
|
|
131
|
-
title: title ?? text,
|
|
132
|
-
...title === void 0 ? {} : { description: text }
|
|
133
|
-
});
|
|
134
|
-
}, [host, toast]);
|
|
122
|
+
//#region ui-src/lib/host.ts
|
|
123
|
+
/** Shares popover dismissal without any gateway state or requests. */
|
|
124
|
+
function createStatusPopovers() {
|
|
125
|
+
let value;
|
|
126
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
127
|
+
return {
|
|
128
|
+
get: () => value,
|
|
129
|
+
subscribe: (listener) => {
|
|
130
|
+
listeners.add(listener);
|
|
131
|
+
return () => {
|
|
132
|
+
listeners.delete(listener);
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
set: (next) => {
|
|
136
|
+
if (value === next) return;
|
|
137
|
+
value = next;
|
|
138
|
+
for (const listener of listeners) listener();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
135
141
|
}
|
|
136
142
|
//#endregion
|
|
137
143
|
//#region ui-src/lib/context-window.ts
|
|
@@ -144,23 +150,6 @@ function formatContextWindow(value) {
|
|
|
144
150
|
}
|
|
145
151
|
//#endregion
|
|
146
152
|
//#region ui-src/components/context-usage.tsx
|
|
147
|
-
var REASONS = /* @__PURE__ */ new Set([
|
|
148
|
-
"capacity_unknown",
|
|
149
|
-
"context_projection_degraded",
|
|
150
|
-
"fixed_context_too_large",
|
|
151
|
-
"recovery_unavailable",
|
|
152
|
-
"summary_budget_too_small",
|
|
153
|
-
"summary_failed",
|
|
154
|
-
"summary_no_gain",
|
|
155
|
-
"summary_network_error",
|
|
156
|
-
"summary_invalid",
|
|
157
|
-
"summary_unavailable",
|
|
158
|
-
"summary_provider_unavailable",
|
|
159
|
-
"summary_group_too_large",
|
|
160
|
-
"protected_context_exceeds_budget",
|
|
161
|
-
"context_still_exceeds_budget",
|
|
162
|
-
"cancelled"
|
|
163
|
-
]);
|
|
164
153
|
/** Cells in the popover's window map, one per half percent of the window. */
|
|
165
154
|
var CELLS = 200;
|
|
166
155
|
/** Share of the window at which the meter turns from green to yellow, and from yellow to red. */
|
|
@@ -237,46 +226,21 @@ function cellsOf(occupancy, window) {
|
|
|
237
226
|
const filled = categories === void 0 ? Array.from({ length: used }, () => "used") : apportion(used, categories.map((one) => one.tokens)).flatMap((count, index) => Array.from({ length: count }, () => categories[index]?.category ?? "used"));
|
|
238
227
|
return [...filled, ...Array.from({ length: CELLS - filled.length }, () => "free")];
|
|
239
228
|
}
|
|
240
|
-
/** Elapsed time is display-only; it never cancels a context operation. */
|
|
241
|
-
function PreparationProgress({ startedAt, completedChunks, t }) {
|
|
242
|
-
const [now, setNow] = useState(Date.now);
|
|
243
|
-
useEffect(() => {
|
|
244
|
-
if (startedAt === void 0) return;
|
|
245
|
-
const timer = setInterval(() => setNow(Date.now()), 1e3);
|
|
246
|
-
return () => clearInterval(timer);
|
|
247
|
-
}, [startedAt]);
|
|
248
|
-
return /* @__PURE__ */ jsxs("p", {
|
|
249
|
-
role: "status",
|
|
250
|
-
children: [
|
|
251
|
-
t("usage.preparing"),
|
|
252
|
-
startedAt !== void 0 && ` · ${t("usage.elapsed", { seconds: Math.max(0, Math.floor((now - startedAt) / 1e3)) })}`,
|
|
253
|
-
completedChunks !== void 0 && ` · ${t("usage.completedChunks", { count: completedChunks })}`
|
|
254
|
-
]
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
229
|
/** Draws the host's occupancy as a meter, its window map and the legend by category. */
|
|
258
230
|
function contextUsageFor(host) {
|
|
259
231
|
return function ContextUsage({ sessionId }) {
|
|
260
|
-
const state = useSyncExternalStore(host.host.subscribe, host.host.get);
|
|
261
|
-
const fail = useFailureToast(host);
|
|
262
232
|
const [toolsOpen, setToolsOpen] = useState(false);
|
|
263
|
-
const
|
|
264
|
-
const running = useRef(false);
|
|
233
|
+
const popover = useSyncExternalStore(host.popovers.subscribe, host.popovers.get);
|
|
265
234
|
const t = host.i18n.t;
|
|
266
235
|
const context = useSyncExternalStore(host.context.subscribe, () => host.context.get(sessionId));
|
|
267
236
|
useEffect(() => host.context.watch(sessionId), [sessionId]);
|
|
268
237
|
const currentOccupancy = context?.occupancy?.source === "unmeasured" ? void 0 : context?.occupancy;
|
|
269
238
|
const historical = currentOccupancy === void 0 ? context?.lastObservation : void 0;
|
|
270
239
|
const occupancy = currentOccupancy ?? historical?.occupancy;
|
|
271
|
-
const
|
|
272
|
-
const limit = measurement?.contextWindow;
|
|
240
|
+
const limit = (historical?.measurement ?? context?.measurement)?.contextWindow;
|
|
273
241
|
const share = occupancy === void 0 || limit === void 0 ? void 0 : Math.min(100, Math.round(occupancy.inputTokens / limit * 100));
|
|
274
242
|
const level = share === void 0 ? void 0 : contextLevel(share);
|
|
275
243
|
const mark = occupancy?.source === "estimated" ? "~" : "";
|
|
276
|
-
const operation = context?.operation;
|
|
277
|
-
const blocked = context?.measurement?.inputBudget !== void 0 && context.measurement.inputTokens > context.measurement.inputBudget;
|
|
278
|
-
const reason = context?.lastResult?.reason ?? context?.diagnostic;
|
|
279
|
-
const reasonText = reason === void 0 ? void 0 : t(REASONS.has(reason) ? `usage.reason.${reason}` : "usage.failed");
|
|
280
244
|
const tokens = (value) => value === void 0 ? "—" : compactTokens(value);
|
|
281
245
|
const percent = (value) => {
|
|
282
246
|
if (limit === void 0) return "";
|
|
@@ -294,35 +258,14 @@ function contextUsageFor(host) {
|
|
|
294
258
|
tokens: Math.max(0, limit - occupancy.inputTokens)
|
|
295
259
|
}]];
|
|
296
260
|
const owners = [...occupancy?.tools ?? []].filter((one) => one.tokens > 0).sort((a, b) => b.tokens - a.tokens);
|
|
297
|
-
async function run(action) {
|
|
298
|
-
if (running.current) return;
|
|
299
|
-
running.current = true;
|
|
300
|
-
setPending(true);
|
|
301
|
-
try {
|
|
302
|
-
await action();
|
|
303
|
-
} catch {
|
|
304
|
-
fail(t("usage.failed"));
|
|
305
|
-
} finally {
|
|
306
|
-
running.current = false;
|
|
307
|
-
setPending(false);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
function cancel() {
|
|
311
|
-
if (operation === void 0) return Promise.resolve();
|
|
312
|
-
return run(() => host.call("cancel", {
|
|
313
|
-
sessionId,
|
|
314
|
-
operationId: operation.operationId
|
|
315
|
-
}));
|
|
316
|
-
}
|
|
317
|
-
function retry() {
|
|
318
|
-
return run(() => host.call("compact", { sessionId }));
|
|
319
|
-
}
|
|
320
261
|
function nameOf(key) {
|
|
321
262
|
if (key === "used") return t("usage.used");
|
|
322
263
|
if (key === "free") return t("usage.free");
|
|
323
264
|
return t(`usage.category.${key}`);
|
|
324
265
|
}
|
|
325
266
|
return /* @__PURE__ */ jsxs(host.ui.Popover, {
|
|
267
|
+
open: popover === `context:${sessionId}`,
|
|
268
|
+
onOpenChange: (open) => host.popovers.set(open ? `context:${sessionId}` : void 0),
|
|
326
269
|
width: 360,
|
|
327
270
|
label: t("usage.context"),
|
|
328
271
|
side: "top",
|
|
@@ -373,10 +316,6 @@ function contextUsageFor(host) {
|
|
|
373
316
|
})]
|
|
374
317
|
})]
|
|
375
318
|
}),
|
|
376
|
-
historical && /* @__PURE__ */ jsx("p", {
|
|
377
|
-
className: "cu-note",
|
|
378
|
-
children: t("usage.lastObservation")
|
|
379
|
-
}),
|
|
380
319
|
occupancy !== void 0 && limit !== void 0 && /* @__PURE__ */ jsx("div", {
|
|
381
320
|
className: "cu-map",
|
|
382
321
|
"aria-hidden": "true",
|
|
@@ -441,106 +380,111 @@ function contextUsageFor(host) {
|
|
|
441
380
|
}, one.owner ?? ""))
|
|
442
381
|
})]
|
|
443
382
|
}, row.key))
|
|
444
|
-
}),
|
|
445
|
-
occupancy !== void 0 && /* @__PURE__ */ jsxs("p", {
|
|
446
|
-
className: "cu-note",
|
|
447
|
-
children: [t(occupancy.source === "reported" ? "usage.noteReported" : "usage.noteEstimated"), occupancy.categories === void 0 && ` ${t("usage.noSplit")}`]
|
|
448
|
-
}),
|
|
449
|
-
/* @__PURE__ */ jsxs("dl", {
|
|
450
|
-
className: "cu-stats",
|
|
451
|
-
children: [
|
|
452
|
-
measurement?.inputBudget !== void 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", { children: t("usage.inputBudget") }), /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
453
|
-
className: "cu-stat",
|
|
454
|
-
text: tokens(measurement.inputBudget)
|
|
455
|
-
}) })] }),
|
|
456
|
-
measurement !== void 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", { children: t("usage.outputReserve") }), /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
457
|
-
className: "cu-stat",
|
|
458
|
-
text: tokens(measurement.outputReserve)
|
|
459
|
-
}) })] }),
|
|
460
|
-
/* @__PURE__ */ jsx("dt", { children: t("usage.actual") }),
|
|
461
|
-
/* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
462
|
-
className: "cu-stat",
|
|
463
|
-
text: tokens(historical?.actualInputTokens ?? context?.actualInputTokens)
|
|
464
|
-
}) }),
|
|
465
|
-
/* @__PURE__ */ jsx("dt", { children: t("usage.total") }),
|
|
466
|
-
/* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
467
|
-
className: "cu-stat",
|
|
468
|
-
text: tokens(context?.totalTokens ?? state.tokensUsed ?? void 0)
|
|
469
|
-
}) }),
|
|
470
|
-
/* @__PURE__ */ jsx("dt", { children: t("usage.auxiliary") }),
|
|
471
|
-
/* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
472
|
-
className: "cu-stat",
|
|
473
|
-
text: tokens(context?.auxiliaryTokens)
|
|
474
|
-
}) })
|
|
475
|
-
]
|
|
476
|
-
}),
|
|
477
|
-
measurement?.outputReserveSource !== void 0 && /* @__PURE__ */ jsx("p", {
|
|
478
|
-
className: "cu-note",
|
|
479
|
-
children: t(`usage.reserveSource.${measurement.outputReserveSource}`)
|
|
480
|
-
}),
|
|
481
|
-
operation !== void 0 && /* @__PURE__ */ jsx(PreparationProgress, {
|
|
482
|
-
startedAt: operation.startedAt,
|
|
483
|
-
completedChunks: operation.completedChunks,
|
|
484
|
-
t
|
|
485
|
-
}),
|
|
486
|
-
context?.lastResult && /* @__PURE__ */ jsxs("section", {
|
|
487
|
-
"aria-label": t(operation ? "usage.previousResult" : "usage.latestResult"),
|
|
488
|
-
children: [
|
|
489
|
-
/* @__PURE__ */ jsx("p", {
|
|
490
|
-
className: "cu-note",
|
|
491
|
-
children: t(operation ? "usage.previousResult" : "usage.latestResult")
|
|
492
|
-
}),
|
|
493
|
-
/* @__PURE__ */ jsxs("p", {
|
|
494
|
-
role: "status",
|
|
495
|
-
className: "cu-note",
|
|
496
|
-
children: [
|
|
497
|
-
t(context.lastResult.status === "changed" && context.lastResult.layer === "none" ? "usage.previewChanged" : `usage.${context.lastResult.status}`),
|
|
498
|
-
context.lastResult.layer !== "none" && ` · ${context.lastResult.layer}`,
|
|
499
|
-
" · ",
|
|
500
|
-
t(`usage.trigger.${context.lastResult.trigger}`),
|
|
501
|
-
" · ",
|
|
502
|
-
context.lastResult.elapsedMs,
|
|
503
|
-
" ms",
|
|
504
|
-
context.lastResult.before && context.lastResult.after && ` · ${tokens(context.lastResult.before.inputTokens)} → ${tokens(context.lastResult.after.inputTokens)}`,
|
|
505
|
-
context.lastResult.summaryModel && ` · ${t("usage.summaryModel")} ${context.lastResult.summaryModel.model}`,
|
|
506
|
-
context.lastResult.completedChunks !== void 0 && ` · ${t("usage.completedChunks", { count: context.lastResult.completedChunks })}`
|
|
507
|
-
]
|
|
508
|
-
}),
|
|
509
|
-
context.lastResult.reason && reasonText && /* @__PURE__ */ jsx(host.ui.Alert, {
|
|
510
|
-
tone: "warning",
|
|
511
|
-
children: reasonText
|
|
512
|
-
})
|
|
513
|
-
]
|
|
514
|
-
}),
|
|
515
|
-
!context?.lastResult?.reason && reasonText && /* @__PURE__ */ jsx(host.ui.Alert, {
|
|
516
|
-
tone: "warning",
|
|
517
|
-
children: reasonText
|
|
518
|
-
}),
|
|
519
|
-
context?.lastResult?.status === "failed" && operation === void 0 && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("p", {
|
|
520
|
-
role: "status",
|
|
521
|
-
className: "cu-note",
|
|
522
|
-
children: t(blocked ? "usage.blocked" : "usage.canContinue")
|
|
523
|
-
}), /* @__PURE__ */ jsx(host.ui.Button, {
|
|
524
|
-
variant: "ghost",
|
|
525
|
-
type: "button",
|
|
526
|
-
loading: pending,
|
|
527
|
-
disabled: pending || state.connection !== "online" || state.phase !== void 0 && state.phase !== "idle",
|
|
528
|
-
onClick: () => void retry(),
|
|
529
|
-
children: t("usage.retry")
|
|
530
|
-
})] }),
|
|
531
|
-
operation !== void 0 && /* @__PURE__ */ jsx(host.ui.Button, {
|
|
532
|
-
variant: "ghost",
|
|
533
|
-
type: "button",
|
|
534
|
-
loading: pending,
|
|
535
|
-
disabled: pending || state.connection !== "online",
|
|
536
|
-
onClick: () => void cancel(),
|
|
537
|
-
children: t("usage.cancel")
|
|
538
383
|
})
|
|
539
384
|
]
|
|
540
385
|
});
|
|
541
386
|
};
|
|
542
387
|
}
|
|
543
388
|
//#endregion
|
|
389
|
+
//#region ui-src/components/session-usage.tsx
|
|
390
|
+
/** Physical session consumption, independent of the current context window. */
|
|
391
|
+
/** Renders committed input/output totals and only cache counters reported by a provider. */
|
|
392
|
+
function sessionUsageFor(host) {
|
|
393
|
+
return function SessionUsage({ sessionId }) {
|
|
394
|
+
const context = useSyncExternalStore(host.context.subscribe, () => host.context.get(sessionId));
|
|
395
|
+
const popover = useSyncExternalStore(host.popovers.subscribe, host.popovers.get);
|
|
396
|
+
useEffect(() => host.context.watch(sessionId), [sessionId]);
|
|
397
|
+
const t = host.i18n.t;
|
|
398
|
+
const usage = context?.usage;
|
|
399
|
+
const rows = [
|
|
400
|
+
["input", usage?.inputTokens ?? 0],
|
|
401
|
+
["output", usage?.outputTokens ?? 0],
|
|
402
|
+
["cacheRead", usage?.cacheReadTokens],
|
|
403
|
+
["cacheWrite", usage?.cacheWriteTokens]
|
|
404
|
+
];
|
|
405
|
+
return /* @__PURE__ */ jsxs(host.ui.Popover, {
|
|
406
|
+
label: t("consumption.title"),
|
|
407
|
+
width: 260,
|
|
408
|
+
side: "top",
|
|
409
|
+
align: "end",
|
|
410
|
+
className: "cu-panel",
|
|
411
|
+
open: popover === `usage:${sessionId}`,
|
|
412
|
+
onOpenChange: (open) => host.popovers.set(open ? `usage:${sessionId}` : void 0),
|
|
413
|
+
trigger: /* @__PURE__ */ jsxs(host.ui.Button, {
|
|
414
|
+
variant: "plain",
|
|
415
|
+
className: "cu cu-consumption",
|
|
416
|
+
children: [compactTokens(usage?.totalTokens ?? 0), " Tokens"]
|
|
417
|
+
}),
|
|
418
|
+
children: [/* @__PURE__ */ jsxs("p", {
|
|
419
|
+
className: "cu-head",
|
|
420
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
421
|
+
className: "cu-head-name",
|
|
422
|
+
children: t("consumption.total")
|
|
423
|
+
}), /* @__PURE__ */ jsx(AlignedNumber, {
|
|
424
|
+
className: "cu-head-value cu-consumption-total",
|
|
425
|
+
text: compactTokens(usage?.totalTokens ?? 0)
|
|
426
|
+
})]
|
|
427
|
+
}), /* @__PURE__ */ jsx("dl", {
|
|
428
|
+
className: "cu-stats",
|
|
429
|
+
children: rows.map(([key, value]) => value === void 0 ? null : /* @__PURE__ */ jsxs("div", {
|
|
430
|
+
className: "cu-consumption-row",
|
|
431
|
+
children: [/* @__PURE__ */ jsx("dt", { children: t(`consumption.${key}`) }), /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
|
|
432
|
+
className: "cu-stat",
|
|
433
|
+
text: compactTokens(value)
|
|
434
|
+
}) })]
|
|
435
|
+
}, key))
|
|
436
|
+
})]
|
|
437
|
+
});
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
//#endregion
|
|
441
|
+
//#region ui-src/components/run-summary.ts
|
|
442
|
+
/** Registers owner-bound summaries without changing transcript messages or host terminology. */
|
|
443
|
+
function registerContextSummary(ctx, status) {
|
|
444
|
+
const matches = (left, right) => left.kind === "run" && right.kind === "run" ? left.historyGeneration === right.historyGeneration && left.turn === right.turn : left.kind === "maintenance" && right.kind === "maintenance" && left.operationId === right.operationId;
|
|
445
|
+
ctx.slots.register("chat.run.summary", {
|
|
446
|
+
id: "preparation",
|
|
447
|
+
order: 20,
|
|
448
|
+
subscribe: (changed) => {
|
|
449
|
+
let sessionId = null;
|
|
450
|
+
let unwatch = () => {};
|
|
451
|
+
const update = () => {
|
|
452
|
+
const next = ctx.host.get().sessionId ?? null;
|
|
453
|
+
if (next !== sessionId) {
|
|
454
|
+
unwatch();
|
|
455
|
+
sessionId = next;
|
|
456
|
+
unwatch = next === null ? () => {} : status.watch(next);
|
|
457
|
+
}
|
|
458
|
+
changed();
|
|
459
|
+
};
|
|
460
|
+
const offHost = ctx.host.subscribe(update);
|
|
461
|
+
const offStatus = status.subscribe(changed);
|
|
462
|
+
const offLanguage = ctx.i18n.subscribe(changed);
|
|
463
|
+
update();
|
|
464
|
+
return () => {
|
|
465
|
+
offHost();
|
|
466
|
+
offStatus();
|
|
467
|
+
offLanguage();
|
|
468
|
+
unwatch();
|
|
469
|
+
};
|
|
470
|
+
},
|
|
471
|
+
get: (props) => {
|
|
472
|
+
const operation = status.get(props.sessionId)?.operation;
|
|
473
|
+
if (!props.latest || !operation?.target || !matches(operation.target, props.target) || operation.phase !== "reduction" && operation.phase !== "summary") return null;
|
|
474
|
+
return {
|
|
475
|
+
key: operation.operationId,
|
|
476
|
+
label: ctx.i18n.t("usage.compacting"),
|
|
477
|
+
elapsedMs: operation.elapsedMs ?? 0,
|
|
478
|
+
running: true
|
|
479
|
+
};
|
|
480
|
+
},
|
|
481
|
+
maintenance: (sessionId) => {
|
|
482
|
+
const operation = status.get(sessionId)?.operation;
|
|
483
|
+
return operation?.target?.kind === "maintenance" && (operation.phase === "reduction" || operation.phase === "summary") ? [operation.target] : [];
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
//#endregion
|
|
544
488
|
//#region ui-src/components/tool-preview.tsx
|
|
545
489
|
/** Observes only mounted tool lines and withdraws together with the Context extension. */
|
|
546
490
|
function registerPreviewNotice(ctx, status) {
|
|
@@ -638,6 +582,19 @@ function registerMetadata(ctx) {
|
|
|
638
582
|
});
|
|
639
583
|
}
|
|
640
584
|
//#endregion
|
|
585
|
+
//#region ui-src/lib/failure.ts
|
|
586
|
+
function failureText(value, t) {
|
|
587
|
+
if (value == null || value === "") return null;
|
|
588
|
+
if (typeof value === "string") return value === t("connection.offline") ? null : value;
|
|
589
|
+
if (typeof value === "object") {
|
|
590
|
+
const failure = value;
|
|
591
|
+
const kind = failure.kind ?? failure.failure;
|
|
592
|
+
if (kind === "offline" || kind === "authentication") return null;
|
|
593
|
+
if (kind === "refused") return failure.reason ?? failure.message ?? String(value);
|
|
594
|
+
}
|
|
595
|
+
return value instanceof Error ? value.message : String(value);
|
|
596
|
+
}
|
|
597
|
+
//#endregion
|
|
641
598
|
//#region ui-src/lib/commands.ts
|
|
642
599
|
/** Registers the manual context command with the plugin's withdrawal lifetime. */
|
|
643
600
|
function registerCommands(ctx) {
|
|
@@ -720,6 +677,14 @@ var toolPreloadConfiguration = {
|
|
|
720
677
|
//#region ui-src/locales/en-US.ts
|
|
721
678
|
/** Context surface translations. */
|
|
722
679
|
var catalogue$1 = {
|
|
680
|
+
consumption: {
|
|
681
|
+
title: "Token usage",
|
|
682
|
+
total: "Total",
|
|
683
|
+
input: "Input",
|
|
684
|
+
output: "Output",
|
|
685
|
+
cacheRead: "Cache read",
|
|
686
|
+
cacheWrite: "Cache write"
|
|
687
|
+
},
|
|
723
688
|
connection: {
|
|
724
689
|
offline: "Not connected to the gateway",
|
|
725
690
|
authenticationRequired: "Authentication is unavailable. Run yaoguang webui to reopen this page."
|
|
@@ -737,7 +702,7 @@ var catalogue$1 = {
|
|
|
737
702
|
},
|
|
738
703
|
status: { context: "Context usage" },
|
|
739
704
|
usage: {
|
|
740
|
-
|
|
705
|
+
compacting: "Compacting context",
|
|
741
706
|
context: "Context",
|
|
742
707
|
unmeasured: "unmeasured",
|
|
743
708
|
used: "Used",
|
|
@@ -810,6 +775,14 @@ var catalogue$1 = {
|
|
|
810
775
|
//#region ui-src/locales/zh-CN.ts
|
|
811
776
|
/** Context surface translations. */
|
|
812
777
|
var catalogue = {
|
|
778
|
+
consumption: {
|
|
779
|
+
title: "Token 用量",
|
|
780
|
+
total: "总用量",
|
|
781
|
+
input: "输入",
|
|
782
|
+
output: "输出",
|
|
783
|
+
cacheRead: "缓存读取",
|
|
784
|
+
cacheWrite: "缓存写入"
|
|
785
|
+
},
|
|
813
786
|
connection: {
|
|
814
787
|
offline: "未连接到网关服务",
|
|
815
788
|
authenticationRequired: "认证不可用,请运行 yaoguang webui 重新打开"
|
|
@@ -827,7 +800,7 @@ var catalogue = {
|
|
|
827
800
|
},
|
|
828
801
|
status: { context: "上下文占用" },
|
|
829
802
|
usage: {
|
|
830
|
-
|
|
803
|
+
compacting: "正在压缩上下文",
|
|
831
804
|
context: "上下文",
|
|
832
805
|
unmeasured: "未测得",
|
|
833
806
|
used: "已用",
|
|
@@ -897,7 +870,7 @@ var catalogue = {
|
|
|
897
870
|
};
|
|
898
871
|
//#endregion
|
|
899
872
|
//#region ui-src/styles.css?raw
|
|
900
|
-
var styles_default = "/* A portalled panel can be the scope root; :where preserves selector specificity. */\n:is(:where(:scope).cu-panel, .cu-panel) {\n overflow-wrap: anywhere;\n}\n:is(:where(:scope).cu-panel, .cu-panel) > p {\n margin: 0 0 0.5rem;\n}\n.cu {\n --cu-tone: var(--meter-low);\n display: flex;\n align-items: center;\n padding: 0;\n height: auto;\n}\n.cu[data-level=\"mid\"],\n.cu-map[data-level=\"mid\"],\n.cu-legend[data-level=\"mid\"] {\n --cu-tone: var(--meter-mid);\n}\n.cu[data-level=\"high\"],\n.cu-map[data-level=\"high\"],\n.cu-legend[data-level=\"high\"] {\n --cu-tone: var(--meter-high);\n}\n.cu-map,\n.cu-legend {\n --cu-tone: var(--meter-low);\n}\n.cu-bar {\n position: relative;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 7rem;\n height: 1.125rem;\n overflow: hidden;\n border-radius: var(--radius-xs);\n background: var(--meter-free);\n}\n.cu-fill {\n position: absolute;\n inset-block: 0;\n inset-inline-start: 0;\n background: var(--cu-tone);\n transition: inline-size 240ms ease;\n}\n.cu-share,\n.cu-unknown {\n position: relative;\n color: var(--foreground);\n font-variant-numeric: tabular-nums;\n line-height: 1;\n}\n.cu-unknown {\n color: var(--muted-foreground);\n}\n.cu-name {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n}\n.cu-head {\n display: flex;\n align-items: baseline;\n justify-content: space-between;\n gap: 0.75rem;\n}\n.cu-head-name {\n font-weight: 600;\n}\n.cu-head-value {\n display: inline-flex;\n align-items: baseline;\n gap: 0.25rem;\n font-variant-numeric: tabular-nums;\n}\n.cu-head-share {\n font-weight: 600;\n}\n.cu-head-count {\n color: var(--muted-foreground);\n}\n/* A column of numbers aligned on the decimal point: the whole part is\n right-aligned by the cell, the fraction and the unit share one fixed slot. */\n.cu-num {\n display: inline-flex;\n justify-content: flex-end;\n font-variant-numeric: tabular-nums;\n}\n.cu-num-tail {\n display: inline-block;\n min-inline-size: 3ch;\n text-align: start;\n}\n.cu-head-count,\n.cu-legend-count,\n.cu-legend-share,\n.cu-stats dd {\n font-variant-numeric: tabular-nums;\n}\n.cu-map {\n display: grid;\n grid-template-columns: repeat(20, 1fr);\n gap: 2px;\n margin-block-end: 0.75rem;\n}\n.cu-cell {\n aspect-ratio: 1;\n border-radius: 2px;\n background: var(--meter-free);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"used\"] {\n background: var(--cu-tone);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"system\"] {\n background: var(--meter-cat-system);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"rules\"] {\n background: var(--meter-cat-rules);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"skills\"] {\n background: var(--meter-cat-skills);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"agents\"] {\n background: var(--meter-cat-agents);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"tools\"] {\n background: var(--meter-cat-tools);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"messages\"] {\n background: var(--meter-cat-messages);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"volatile\"] {\n background: var(--meter-cat-volatile);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"files\"] {\n background: var(--meter-cat-files);\n}\n.cu-legend {\n display: grid;\n gap: 0.25rem;\n margin: 0 0 0.75rem;\n padding: 0;\n list-style: none;\n}\n.cu-legend-row {\n display: grid;\n grid-template-columns: 0.625rem 1fr auto 3.5rem;\n align-items: center;\n gap: 0.5rem;\n}\n.cu-owners {\n display: grid;\n gap: 0.25rem;\n margin: 0.25rem 0 0;\n padding: 0;\n list-style: none;\n}\n.cu-owner-name {\n overflow: hidden;\n color: var(--muted-foreground);\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n.cu-tools-toggle {\n padding: 0;\n border: 0;\n background: none;\n color: inherit;\n font: inherit;\n text-align: start;\n text-decoration: underline dotted;\n text-underline-offset: 3px;\n cursor: pointer;\n}\n.cu-swatch {\n width: 0.625rem;\n height: 0.625rem;\n border-radius: 2px;\n background: var(--meter-free);\n}\n.cu-legend-count,\n.cu-legend-share {\n text-align: end;\n}\n.cu-legend-share,\n.cu-note {\n color: var(--muted-foreground);\n}\n.cu-stats {\n display: grid;\n grid-template-columns: 1fr auto;\n gap: 0.25rem 0.75rem;\n margin: 0 0 0.5rem;\n padding-block-start: 0.5rem;\n
|
|
873
|
+
var styles_default = "/* A portalled panel can be the scope root; :where preserves selector specificity. */\n:is(:where(:scope).cu-panel, .cu-panel) {\n overflow-wrap: anywhere;\n}\n:is(:where(:scope).cu-panel, .cu-panel) > p {\n margin: 0 0 0.5rem;\n}\n.cu {\n --cu-tone: var(--meter-low);\n display: flex;\n align-items: center;\n padding: 0;\n height: auto;\n}\n.cu[data-level=\"mid\"],\n.cu-map[data-level=\"mid\"],\n.cu-legend[data-level=\"mid\"] {\n --cu-tone: var(--meter-mid);\n}\n.cu[data-level=\"high\"],\n.cu-map[data-level=\"high\"],\n.cu-legend[data-level=\"high\"] {\n --cu-tone: var(--meter-high);\n}\n.cu-map,\n.cu-legend {\n --cu-tone: var(--meter-low);\n}\n.cu-bar {\n position: relative;\n display: flex;\n align-items: center;\n justify-content: center;\n width: 7rem;\n height: 1.125rem;\n overflow: hidden;\n border-radius: var(--radius-xs);\n background: var(--meter-free);\n}\n.cu-fill {\n position: absolute;\n inset-block: 0;\n inset-inline-start: 0;\n background: var(--cu-tone);\n transition: inline-size 240ms ease;\n}\n.cu-share,\n.cu-unknown {\n position: relative;\n color: var(--foreground);\n font-variant-numeric: tabular-nums;\n line-height: 1;\n}\n.cu-unknown {\n color: var(--muted-foreground);\n}\n.cu-name {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n}\n.cu-head {\n display: flex;\n align-items: baseline;\n justify-content: space-between;\n gap: 0.75rem;\n}\n.cu-head-name {\n font-weight: 600;\n}\n.cu-head-value {\n display: inline-flex;\n align-items: baseline;\n gap: 0.25rem;\n font-variant-numeric: tabular-nums;\n}\n.cu-head-share {\n font-weight: 600;\n}\n.cu-head-count {\n color: var(--muted-foreground);\n}\n.cu-consumption-total {\n font-size: 1rem;\n font-weight: 600;\n}\n/* A column of numbers aligned on the decimal point: the whole part is\n right-aligned by the cell, the fraction and the unit share one fixed slot. */\n.cu-num {\n display: inline-flex;\n justify-content: flex-end;\n font-variant-numeric: tabular-nums;\n}\n.cu-num-tail {\n display: inline-block;\n min-inline-size: 3ch;\n text-align: start;\n}\n.cu-head-count,\n.cu-legend-count,\n.cu-legend-share,\n.cu-stats dd {\n font-variant-numeric: tabular-nums;\n}\n.cu-map {\n display: grid;\n grid-template-columns: repeat(20, 1fr);\n gap: 2px;\n margin-block-end: 0.75rem;\n}\n.cu-cell {\n aspect-ratio: 1;\n border-radius: 2px;\n background: var(--meter-free);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"used\"] {\n background: var(--cu-tone);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"system\"] {\n background: var(--meter-cat-system);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"rules\"] {\n background: var(--meter-cat-rules);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"skills\"] {\n background: var(--meter-cat-skills);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"agents\"] {\n background: var(--meter-cat-agents);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"tools\"] {\n background: var(--meter-cat-tools);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"messages\"] {\n background: var(--meter-cat-messages);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"volatile\"] {\n background: var(--meter-cat-volatile);\n}\n:is(:where(:scope).cu-panel, .cu-panel) [data-cell=\"files\"] {\n background: var(--meter-cat-files);\n}\n.cu-legend {\n display: grid;\n gap: 0.25rem;\n margin: 0 0 0.75rem;\n padding: 0;\n list-style: none;\n}\n.cu-legend-row {\n display: grid;\n grid-template-columns: 0.625rem 1fr auto 3.5rem;\n align-items: center;\n gap: 0.5rem;\n}\n.cu-owners {\n display: grid;\n gap: 0.25rem;\n margin: 0.25rem 0 0;\n padding: 0;\n list-style: none;\n}\n.cu-owner-name {\n overflow: hidden;\n color: var(--muted-foreground);\n text-overflow: ellipsis;\n white-space: nowrap;\n}\n.cu-tools-toggle {\n padding: 0;\n border: 0;\n background: none;\n color: inherit;\n font: inherit;\n text-align: start;\n text-decoration: underline dotted;\n text-underline-offset: 3px;\n cursor: pointer;\n}\n.cu-swatch {\n width: 0.625rem;\n height: 0.625rem;\n border-radius: 2px;\n background: var(--meter-free);\n}\n.cu-legend-count,\n.cu-legend-share {\n text-align: end;\n}\n.cu-legend-share,\n.cu-note {\n color: var(--muted-foreground);\n}\n.cu-stats {\n display: grid;\n grid-template-columns: 1fr auto;\n gap: 0.25rem 0.75rem;\n margin: 0 0 0.5rem;\n padding-block-start: 0.5rem;\n}\n.cu-stats dt {\n color: var(--muted-foreground);\n}\n.cu-stats dd {\n margin: 0;\n text-align: end;\n}\n\n.cu-consumption-row {\n display: contents;\n}\n";
|
|
901
874
|
//#endregion
|
|
902
875
|
//#region ui-src/index.tsx
|
|
903
876
|
function flatten(value, prefix = "") {
|
|
@@ -914,12 +887,24 @@ var register = (ctx) => {
|
|
|
914
887
|
}, "en-US");
|
|
915
888
|
ctx.styles.add(styles_default, { layer: "unlayered" });
|
|
916
889
|
const status = createContextStatusView(ctx);
|
|
917
|
-
const
|
|
890
|
+
const host = {
|
|
891
|
+
popovers: createStatusPopovers(),
|
|
918
892
|
ui: ctx.ui,
|
|
919
893
|
host: ctx.host,
|
|
920
894
|
context: status,
|
|
921
895
|
i18n: ctx.i18n,
|
|
922
896
|
call: (method, params) => ctx.invoke(method, params ?? {})
|
|
897
|
+
};
|
|
898
|
+
const Context = contextUsageFor(host);
|
|
899
|
+
const Usage = sessionUsageFor(host);
|
|
900
|
+
function UsageStatus({ sessionId }) {
|
|
901
|
+
useSyncExternalStore(ctx.i18n.subscribe, ctx.i18n.language);
|
|
902
|
+
return sessionId === null ? null : /* @__PURE__ */ jsx(Usage, { sessionId });
|
|
903
|
+
}
|
|
904
|
+
ctx.slots.register("chat.status.item", {
|
|
905
|
+
id: "usage",
|
|
906
|
+
order: 10,
|
|
907
|
+
component: UsageStatus
|
|
923
908
|
});
|
|
924
909
|
function Status({ sessionId }) {
|
|
925
910
|
useSyncExternalStore(ctx.i18n.subscribe, ctx.i18n.language);
|
|
@@ -939,6 +924,7 @@ var register = (ctx) => {
|
|
|
939
924
|
order: 20,
|
|
940
925
|
component: Status
|
|
941
926
|
});
|
|
927
|
+
registerContextSummary(ctx, status);
|
|
942
928
|
registerCommands(ctx);
|
|
943
929
|
registerMetadata(ctx);
|
|
944
930
|
registerPreviewNotice(ctx, status);
|