@eum-agent/context-management 1.0.0 → 1.0.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.
Files changed (3) hide show
  1. package/dist/index.js +1655 -164
  2. package/package.json +5 -5
  3. package/ui/ui.js +123 -40
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.0",
7
+ "version": "1.0.1",
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.0",
27
- "@eum-agent/plugin": "1.0.0",
26
+ "@eum-agent/access-local": "1.0.1",
27
+ "@eum-agent/plugin": "1.0.1",
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.0"
42
+ "@eum-agent/llm-management": "1.0.1"
43
43
  },
44
44
  "kind": "extension",
45
45
  "foundationDependencies": {
46
- "@eum-agent/storage-sqlite": "1.0.0"
46
+ "@eum-agent/storage-sqlite": "1.0.1"
47
47
  }
48
48
  },
49
49
  "scripts": {
package/ui/ui.js CHANGED
@@ -1,41 +1,75 @@
1
1
  import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
3
  //#region ui-src/lib/status.ts
4
- /** Refreshes observed sessions from this owner's validated method and change frames. */
4
+ /** Coalesces watched IDs into bounded collections and fences late replies to connection and watch epochs. */
5
5
  function createContextStatusView(ctx) {
6
6
  const values = /* @__PURE__ */ new Map();
7
7
  const watched = /* @__PURE__ */ new Map();
8
- let nextEpoch = 0;
9
- const pending = /* @__PURE__ */ new Set();
10
- const dirty = /* @__PURE__ */ new Set();
8
+ const queued = /* @__PURE__ */ new Set();
9
+ const active = /* @__PURE__ */ new Map();
11
10
  const listeners = /* @__PURE__ */ new Set();
12
- function refresh(sessionId) {
13
- if (ctx.signal.aborted || !watched.has(sessionId)) return;
14
- if (pending.has(sessionId)) {
15
- dirty.add(sessionId);
16
- return;
17
- }
18
- const epoch = watched.get(sessionId).epoch;
19
- pending.add(sessionId);
20
- ctx.invoke("status", { sessionId }).then((value) => {
21
- if (ctx.signal.aborted || watched.get(sessionId)?.epoch !== epoch) return;
22
- if (value) values.set(sessionId, value);
23
- else values.delete(sessionId);
24
- for (const listener of [...listeners]) listener();
25
- }).catch(() => {}).finally(() => {
26
- pending.delete(sessionId);
27
- if (dirty.delete(sessionId)) refresh(sessionId);
11
+ let nextEpoch = 0, generation = 0, scheduled = false;
12
+ let online = ctx.gateway.connection.status === "online";
13
+ const notify = () => {
14
+ for (const listener of [...listeners]) listener();
15
+ };
16
+ const schedule = (ids = []) => {
17
+ for (const id of ids) if (watched.has(id)) queued.add(id);
18
+ if (scheduled || ctx.signal.aborted) return;
19
+ scheduled = true;
20
+ queueMicrotask(() => {
21
+ scheduled = false;
22
+ if (ctx.signal.aborted || !online) return;
23
+ const selected = [...queued].filter((id) => watched.has(id) && !active.has(id));
24
+ for (let offset = 0; offset < selected.length; offset += 31) {
25
+ const sessionIds = selected.slice(offset, offset + 31), token = Symbol(), at = generation;
26
+ const epochs = sessionIds.map((id) => watched.get(id).epoch);
27
+ for (const id of sessionIds) {
28
+ queued.delete(id);
29
+ active.set(id, token);
30
+ }
31
+ ctx.invoke("statuses", { sessionIds }).then(({ results }) => {
32
+ if (ctx.signal.aborted || generation !== at || results.length !== sessionIds.length) return;
33
+ if (results.some((one, index) => one.index !== index || one.target !== sessionIds[index])) return;
34
+ let changed = false;
35
+ for (const [index, one] of results.entries()) {
36
+ if (watched.get(one.target)?.epoch !== epochs[index] || !one.ok) continue;
37
+ if (one.value === null) {
38
+ changed = values.delete(one.target) || changed;
39
+ continue;
40
+ }
41
+ if ((values.get(one.target)?.revision ?? -1) > one.value.revision) continue;
42
+ values.set(one.target, one.value);
43
+ changed = true;
44
+ }
45
+ if (changed) notify();
46
+ }).catch(() => {}).finally(() => {
47
+ for (const id of sessionIds) if (active.get(id) === token) active.delete(id);
48
+ if (queued.size) schedule();
49
+ });
50
+ }
28
51
  });
29
- }
52
+ };
30
53
  ctx.on("statusChanged", (event) => {
31
- if (event.sessionId) refresh(event.sessionId);
32
- });
33
- ctx.onCore("session.updated", () => {
34
- for (const id of watched.keys()) refresh(id);
54
+ if (event.sessionId) schedule([event.sessionId]);
35
55
  });
56
+ ctx.onCore("session.updated", () => schedule([...watched.keys()]));
36
57
  ctx.gateway.watchConnection((connection) => {
37
- if (connection.status === "online") for (const id of watched.keys()) refresh(id);
58
+ const next = connection.status === "online";
59
+ if (next !== online) {
60
+ generation++;
61
+ active.clear();
62
+ }
63
+ if (next && !online) schedule([...watched.keys()]);
64
+ online = next;
38
65
  });
66
+ ctx.signal.addEventListener("abort", () => {
67
+ generation++;
68
+ queued.clear();
69
+ active.clear();
70
+ values.clear();
71
+ listeners.clear();
72
+ }, { once: true });
39
73
  return {
40
74
  get: (sessionId) => values.get(sessionId),
41
75
  subscribe(listener) {
@@ -51,18 +85,22 @@ function createContextStatusView(ctx) {
51
85
  count: 1,
52
86
  epoch: ++nextEpoch
53
87
  });
54
- refresh(sessionId);
88
+ if (!current) schedule([sessionId]);
89
+ let stopped = false;
55
90
  return () => {
91
+ if (stopped) return;
92
+ stopped = true;
56
93
  const current = watched.get(sessionId);
57
94
  if (!current) return;
58
- const count = current.count - 1;
59
- if (count) watched.set(sessionId, {
95
+ if (current.count > 1) watched.set(sessionId, {
60
96
  ...current,
61
- count
97
+ count: current.count - 1
62
98
  });
63
99
  else {
64
100
  watched.delete(sessionId);
65
- values.delete(sessionId);
101
+ queued.delete(sessionId);
102
+ active.delete(sessionId);
103
+ if (values.delete(sessionId)) notify();
66
104
  }
67
105
  };
68
106
  }
@@ -227,14 +265,16 @@ function contextUsageFor(host) {
227
265
  const t = host.i18n.t;
228
266
  const context = useSyncExternalStore(host.context.subscribe, () => host.context.get(sessionId));
229
267
  useEffect(() => host.context.watch(sessionId), [sessionId]);
230
- const occupancy = context?.occupancy?.source === "unmeasured" ? void 0 : context?.occupancy;
231
- const limit = context?.measurement?.contextWindow;
268
+ const currentOccupancy = context?.occupancy?.source === "unmeasured" ? void 0 : context?.occupancy;
269
+ const historical = currentOccupancy === void 0 ? context?.lastObservation : void 0;
270
+ const occupancy = currentOccupancy ?? historical?.occupancy;
271
+ const measurement = historical?.measurement ?? context?.measurement;
272
+ const limit = measurement?.contextWindow;
232
273
  const share = occupancy === void 0 || limit === void 0 ? void 0 : Math.min(100, Math.round(occupancy.inputTokens / limit * 100));
233
274
  const level = share === void 0 ? void 0 : contextLevel(share);
234
275
  const mark = occupancy?.source === "estimated" ? "~" : "";
235
276
  const operation = context?.operation;
236
- const measurement = context?.measurement;
237
- const blocked = measurement?.inputBudget !== void 0 && measurement.inputTokens > measurement.inputBudget;
277
+ const blocked = context?.measurement?.inputBudget !== void 0 && context.measurement.inputTokens > context.measurement.inputBudget;
238
278
  const reason = context?.lastResult?.reason ?? context?.diagnostic;
239
279
  const reasonText = reason === void 0 ? void 0 : t(REASONS.has(reason) ? `usage.reason.${reason}` : "usage.failed");
240
280
  const tokens = (value) => value === void 0 ? "—" : compactTokens(value);
@@ -333,6 +373,10 @@ function contextUsageFor(host) {
333
373
  })]
334
374
  })]
335
375
  }),
376
+ historical && /* @__PURE__ */ jsx("p", {
377
+ className: "cu-note",
378
+ children: t("usage.lastObservation")
379
+ }),
336
380
  occupancy !== void 0 && limit !== void 0 && /* @__PURE__ */ jsx("div", {
337
381
  className: "cu-map",
338
382
  "aria-hidden": "true",
@@ -416,7 +460,7 @@ function contextUsageFor(host) {
416
460
  /* @__PURE__ */ jsx("dt", { children: t("usage.actual") }),
417
461
  /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
418
462
  className: "cu-stat",
419
- text: tokens(context?.actualInputTokens)
463
+ text: tokens(historical?.actualInputTokens ?? context?.actualInputTokens)
420
464
  }) }),
421
465
  /* @__PURE__ */ jsx("dt", { children: t("usage.total") }),
422
466
  /* @__PURE__ */ jsx("dd", { children: /* @__PURE__ */ jsx(AlignedNumber, {
@@ -524,7 +568,9 @@ function registerPreviewNotice(ctx, status) {
524
568
  var TOOL_NAMES = [
525
569
  "context_search",
526
570
  "context_restore",
527
- "session_note"
571
+ "session_note",
572
+ "tool_search",
573
+ "tool_call"
528
574
  ];
529
575
  //#endregion
530
576
  //#region ui-src/metadata.ts
@@ -534,14 +580,18 @@ var CATALOGUES = {
534
580
  group: "Context",
535
581
  context_search: "Search history",
536
582
  context_restore: "Restore history",
537
- session_note: "Update session notes"
583
+ session_note: "Update session notes",
584
+ tool_search: "Search tools",
585
+ tool_call: "Call tool"
538
586
  },
539
587
  "zh-CN": {
540
588
  section: "内置插件",
541
589
  group: "上下文",
542
590
  context_search: "搜索历史上下文",
543
591
  context_restore: "恢复历史上下文",
544
- session_note: "更新会话笔记"
592
+ session_note: "更新会话笔记",
593
+ tool_search: "检索工具",
594
+ tool_call: "调用工具"
545
595
  }
546
596
  };
547
597
  var names = new Set(TOOL_NAMES);
@@ -639,6 +689,34 @@ function registerCommands(ctx) {
639
689
  });
640
690
  }
641
691
  //#endregion
692
+ //#region ui-src/lib/tool-preload.ts
693
+ function object(value) {
694
+ if (value === void 0) return {};
695
+ if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error("Tool preload configuration must be an object");
696
+ return value;
697
+ }
698
+ function read(value) {
699
+ const overrides = object(object(value).toolPreload);
700
+ for (const [name, enabled] of Object.entries(overrides)) {
701
+ if (!/^[a-zA-Z0-9_-]{1,64}$/.test(name) || typeof enabled !== "boolean") throw new Error("Tool preload overrides must map valid tool names to booleans");
702
+ if (name === "tool_search" || name === "tool_call") throw new Error("System tool preload cannot be overridden");
703
+ }
704
+ return { ...overrides };
705
+ }
706
+ /** Retains other owner fields and replaces sparse overrides; invalid data throws before editing. */
707
+ var toolPreloadConfiguration = {
708
+ owner: "@eum-agent/context-management",
709
+ read,
710
+ replace: (value, overrides) => {
711
+ read(value);
712
+ const toolPreload = read({ toolPreload: overrides });
713
+ return {
714
+ ...object(value),
715
+ toolPreload
716
+ };
717
+ }
718
+ };
719
+ //#endregion
642
720
  //#region ui-src/locales/en-US.ts
643
721
  /** Context surface translations. */
644
722
  var catalogue$1 = {
@@ -659,6 +737,7 @@ var catalogue$1 = {
659
737
  },
660
738
  status: { context: "Context usage" },
661
739
  usage: {
740
+ lastObservation: "Showing the last saved context observation until a new measurement is available. This value is not used for current request capacity checks.",
662
741
  context: "Context",
663
742
  unmeasured: "unmeasured",
664
743
  used: "Used",
@@ -748,6 +827,7 @@ var catalogue = {
748
827
  },
749
828
  status: { context: "上下文占用" },
750
829
  usage: {
830
+ lastObservation: "显示上次保存的上下文统计;新测量完成后更新。此值不用于当前请求的容量判断。",
751
831
  context: "上下文",
752
832
  unmeasured: "未测得",
753
833
  used: "已用",
@@ -824,7 +904,10 @@ function flatten(value, prefix = "") {
824
904
  return Object.fromEntries(Object.entries(value).flatMap(([key, item]) => typeof item === "string" ? [[prefix + key, item]] : Object.entries(flatten(item, `${prefix}${key}.`))));
825
905
  }
826
906
  var runtimeVersion = 5;
907
+ /** Optional Agent preference editor service, withdrawn with the Context browser owner. */
908
+ var provides = ["tool-preload:configuration"];
827
909
  var register = (ctx) => {
910
+ ctx.services.provide("tool-preload:configuration", toolPreloadConfiguration);
828
911
  ctx.i18n.register({
829
912
  "en-US": flatten(catalogue$1),
830
913
  "zh-CN": flatten(catalogue)
@@ -861,4 +944,4 @@ var register = (ctx) => {
861
944
  registerPreviewNotice(ctx, status);
862
945
  };
863
946
  //#endregion
864
- export { register, runtimeVersion };
947
+ export { provides, register, runtimeVersion };