stratagate-dsh 0.3.4 → 0.3.5

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/plugin.js CHANGED
@@ -9047,25 +9047,56 @@ function lineText(value, maximum) {
9047
9047
  function renderMemoryDirectory(topics, events) {
9048
9048
  const { entries, categories: categories2 } = topicNavigation(topics, events);
9049
9049
  if (entries.length === 0) return "";
9050
- const header = "[StrataGate \u8BB0\u5FC6\u76EE\u5F55]\n\u4EC5\u4F9B\u5BFC\u822A\uFF0C\u6982\u89C8\u4E0D\u662F\u4E8B\u5B9E\u8BC1\u636E\u3002\u9700\u8981\u4E3B\u9898\u8109\u7EDC\u65F6\u7528 memory_expand_topic\uFF1B\u9700\u8981\u4E8B\u5B9E\u8BC1\u636E\u53EF\u76F4\u63A5 memory_search_events(topic_id)\uFF0C\u4E5F\u53EF\u76F4\u63A5\u641C\u7D22\u4E8B\u4EF6/\u56FE\u8C31\u3002";
9051
- const lines = entries.map((entry) => `- ${entry.id}\uFF1A${lineText(entry.title, 64)}${entry.description ? `\uFF1B${lineText(entry.description, 48)}` : ""}`);
9052
- const complete = `${header}
9053
- ${lines.join("\n")}`;
9050
+ const header = "[StrataGate \u8BB0\u5FC6\u76EE\u5F55]\n\u4EC5\u4F9B\u5BFC\u822A\uFF0C\u4E0D\u662F\u4E8B\u5B9E\u8BC1\u636E\u3002\u9700\u8981\u4E3B\u9898\u8109\u7EDC\u65F6\u7528 memory_expand_topic\uFF1B\u9700\u8981\u4E8B\u5B9E\u8BC1\u636E\u53EF\u76F4\u63A5 memory_search_events(topic_id)\uFF0C\u4E5F\u53EF\u76F4\u63A5\u641C\u7D22\u4E8B\u4EF6/\u56FE\u8C31\u3002";
9051
+ const byId = new Map(topics.map((topic) => [topic.id, topic]));
9052
+ const chapters = entries.map((entry) => ({
9053
+ ...entry,
9054
+ line: `- ${entry.id}\uFF1A${lineText(entry.title, 64)}${entry.description ? `\uFF1B${lineText(entry.description, 48)}` : ""}`,
9055
+ // Reuse the book's stable section order, including summary-free and legacy sections.
9056
+ sections: memoryTopicSections(byId.get(entry.id)).map(({ title }) => ` - ${lineText(title, 64)}`)
9057
+ }));
9058
+ const render = (prefix, selected2) => `${prefix}
9059
+ ${selected2.flatMap(({ line, sections }) => [line, ...sections]).join("\n")}`.trim();
9060
+ const complete = render(header, chapters);
9054
9061
  if (estimateTokens(complete) <= MEMORY_DIRECTORY_TOKEN_BUDGET) return complete;
9055
9062
  const branches = categories2.map(({ id, label, count }) => `${id}\uFF08${label} ${count}\uFF09`).join("\uFF1B");
9056
9063
  const navigation = `${header}
9057
- \u5171 ${entries.length} \u9879\uFF1B\u5206\u7C7B\uFF1A${branches}\u3002\u5B8C\u6574\u76EE\u5F55\u7528 memory_list_topics(category, offset) \u5206\u9875\uFF1Bquery \u53EF\u67E5\u4E3B\u9898\u3002\u4E0B\u5217\u4E3A\u90E8\u5206\u5165\u53E3\uFF1A`;
9058
- const selected = [];
9059
- for (const category of categories2) {
9060
- const entry = entries.find((item) => item.category === category.id);
9061
- const line = `- ${entry.id}\uFF1A${lineText(entry.title, 40)}`;
9062
- if (estimateTokens(`${navigation}
9063
- ${[...selected.map(({ line: line2 }) => line2), line].join("\n")}`) <= MEMORY_DIRECTORY_TOKEN_BUDGET) selected.push({ id: entry.id, line });
9064
+ \u5171 ${entries.length} \u9879\uFF1B\u5206\u7C7B\uFF1A${branches}\u3002\u76EE\u5F55\u5DF2\u88C1\u526A\uFF1B\u5B8C\u6574\u76EE\u5F55\u7528 memory_list_topics(category, offset) \u5206\u9875\uFF1Bquery \u53EF\u67E5\u4E3B\u9898\uFF0C\u5C0F\u8282\u7528 memory_expand_topic(id)\u3002`;
9065
+ let selected = chapters.map((chapter) => ({ ...chapter, sections: [] }));
9066
+ if (estimateTokens(render(navigation, selected)) > MEMORY_DIRECTORY_TOKEN_BUDGET) {
9067
+ selected = [];
9068
+ const admit = (chapter) => {
9069
+ for (const line of [chapter.line, `- ${chapter.id}\uFF1A${lineText(chapter.title, 64)}`]) {
9070
+ const candidate = { ...chapter, line, sections: [] };
9071
+ if (estimateTokens(render(navigation, [...selected, candidate])) <= MEMORY_DIRECTORY_TOKEN_BUDGET) {
9072
+ selected.push(candidate);
9073
+ return;
9074
+ }
9075
+ }
9076
+ };
9077
+ for (const category of categories2) admit(chapters.find((chapter) => chapter.category === category.id));
9078
+ for (const chapter of chapters) if (!selected.some(({ id }) => id === chapter.id)) admit(chapter);
9064
9079
  }
9065
9080
  const order = new Map(entries.map(({ id }, index) => [id, index]));
9066
9081
  selected.sort((a, b) => order.get(a.id) - order.get(b.id));
9067
- return `${navigation}
9068
- ${selected.map(({ line }) => line).join("\n")}`.trim();
9082
+ const sectionLines = new Map(chapters.map(({ id, sections }) => [id, sections]));
9083
+ const exhausted = /* @__PURE__ */ new Set();
9084
+ for (let index = 0; exhausted.size < selected.length; index++) {
9085
+ for (const chapter of selected) {
9086
+ if (exhausted.has(chapter.id)) continue;
9087
+ const line = sectionLines.get(chapter.id)[index];
9088
+ if (line === void 0) {
9089
+ exhausted.add(chapter.id);
9090
+ continue;
9091
+ }
9092
+ chapter.sections.push(line);
9093
+ if (estimateTokens(render(navigation, selected)) > MEMORY_DIRECTORY_TOKEN_BUDGET) {
9094
+ chapter.sections.pop();
9095
+ exhausted.add(chapter.id);
9096
+ }
9097
+ }
9098
+ }
9099
+ return render(navigation, selected);
9069
9100
  }
9070
9101
  function boundedTopic(topic, envelope = {}) {
9071
9102
  const { sections: _sections, ...summaryTopic } = topic;
@@ -9611,9 +9642,21 @@ var StrataGateRuntime = class {
9611
9642
  }
9612
9643
  async buildMemoryDirectory(session) {
9613
9644
  await this.flush();
9614
- const memory = await this.space(session);
9615
- await this.refreshExternalImportMemory(this.namespaceFor(session), memory);
9616
- return renderMemoryDirectory(this.visibleTopics(memory), memory.listAllEvents());
9645
+ const namespace = this.namespaceFor(session);
9646
+ const active = this.spaces.get(namespace);
9647
+ if (active) {
9648
+ const memory = await active;
9649
+ await this.refreshExternalImportMemory(namespace, memory);
9650
+ return renderMemoryDirectory(this.visibleTopics(memory), memory.listAllEvents());
9651
+ }
9652
+ if (this.config.database === ":memory:" || !existsSync(this.config.database)) return "";
9653
+ const storage = new SqliteStorage({ filename: this.config.database, readonly: true });
9654
+ try {
9655
+ const memory = await StrataGate.openWithStorage({ storage, namespace });
9656
+ return renderMemoryDirectory(this.visibleTopics(memory), memory.listAllEvents());
9657
+ } finally {
9658
+ await storage.close();
9659
+ }
9617
9660
  }
9618
9661
  visibleTopics(memory) {
9619
9662
  if (this.agentMemoryRetrievalWeight > 0) return memory.listMemoryTopics();
@@ -9890,6 +9933,7 @@ var StrataGateRuntime = class {
9890
9933
  async buildAutoContext(session) {
9891
9934
  await this.flush();
9892
9935
  const memory = await this.space(session);
9936
+ await this.refreshExternalImportMemory(this.namespaceFor(session), memory);
9893
9937
  const threadId = String(session.id);
9894
9938
  const blockContexts = memory.getBlockContext(threadId);
9895
9939
  if (this.syncDecayedBlockSurface(session, memory, blockContexts)) {
@@ -13863,20 +13907,22 @@ async function apply(ctx, config) {
13863
13907
  }
13864
13908
  const session = context.agent?.session;
13865
13909
  if (!session) return { ...assembled, contexts };
13910
+ let autoMemory;
13866
13911
  try {
13867
- const directory = await runtime.buildMemoryDirectory(session);
13868
- if (directory) contexts.push({ name: "stratagate:memory-directory", text: directory });
13912
+ const text3 = await runtime.buildAutoContext(session);
13913
+ autoMemory = { name: "stratagate:auto-memory", text: text3 };
13869
13914
  } catch (error) {
13870
- ctx.logger.warn(`stratagate-memory directory failed: ${renderError(error)}`);
13915
+ ctx.logger.warn(`stratagate-memory auto-context failed: ${renderError(error)}`);
13871
13916
  runtime.notePluginError(session, error);
13872
13917
  }
13873
13918
  try {
13874
- const text3 = await runtime.buildAutoContext(session);
13875
- contexts.push({ name: "stratagate:auto-memory", text: text3 });
13919
+ const directory = await runtime.buildMemoryDirectory(session);
13920
+ if (directory) contexts.push({ name: "stratagate:memory-directory", text: directory });
13876
13921
  } catch (error) {
13877
- ctx.logger.warn(`stratagate-memory auto-context failed: ${renderError(error)}`);
13922
+ ctx.logger.warn(`stratagate-memory directory failed: ${renderError(error)}`);
13878
13923
  runtime.notePluginError(session, error);
13879
13924
  }
13925
+ if (autoMemory) contexts.push(autoMemory);
13880
13926
  const feedbackSuggestion = runtime.takeFeedbackSuggestion(session);
13881
13927
  if (feedbackSuggestion) contexts.push({ name: "stratagate:feedback-suggestion", text: feedbackSuggestion });
13882
13928
  return { ...assembled, contexts };