dsh-plugin-soulbridge 0.0.114 → 0.0.115

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 (2) hide show
  1. package/index.js +69 -4
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -694,11 +694,16 @@ export function buildFactHooks(text, options = {}) {
694
694
  * When the question names a subject, the entity timeline is the answer and the
695
695
  * related-pairs search is the contamination: it is how "上次和 a 吃饭" ends up
696
696
  * answered with something about b. So the plan chooses one or the other.
697
+ *
698
+ * A time question is different: the window block answers "what happened last week",
699
+ * and the relevance block stays on because it is where relative/unknown facts and
700
+ * not-yet-extracted conversation still surface (0027 §八: they must never be dropped
701
+ * just because their date cannot be located).
697
702
  */
698
703
  export function planBlocks(plan) {
699
- if (!plan) return { related: true, timeline: false };
700
- if (plan.primitive === 'entity') return { related: false, timeline: true };
701
- return { related: true, timeline: false };
704
+ if (!plan) return { related: true, timeline: false, window: false };
705
+ if (plan.primitive === 'entity') return { related: false, timeline: true, window: false };
706
+ return { related: true, timeline: false, window: Boolean(plan.window) };
702
707
  }
703
708
 
704
709
  const PROFILE_CORE_DIMENSION = '价值观';
@@ -755,7 +760,52 @@ export function renderRulesBlock(rules) {
755
760
  return `<rules fired="${ids}">\n${lines}\n</rules>`;
756
761
  }
757
762
 
758
- function buildRelatedContext(text, state = {}) {
763
+ /** How many facts one window may put in the prompt before it is cut. */
764
+ const TIME_WINDOW_LIMIT = 10;
765
+
766
+ /** A calendar day as the user reads it — local, not UTC: a date is a day, not an instant. */
767
+ function isoDay(ms) {
768
+ const date = new Date(ms);
769
+ const pad = (n) => String(n).padStart(2, '0');
770
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
771
+ }
772
+
773
+ /** When a fact happened, as the injection may say it: a located date, or the words said. */
774
+ function factWhen(fact) {
775
+ // A range ("10 月下旬") is not a day: printing its start as a date would claim an
776
+ // exactness the model never had. The original words are the honest answer.
777
+ if (fact.validFromPrecision === 'range' && fact.validFromText) return fact.validFromText;
778
+ return fact.validFrom ? isoDay(fact.validFrom) : fact.validFromText ?? '';
779
+ }
780
+
781
+ /**
782
+ * The facts that happened in one window (0027 §八, reader 1).
783
+ *
784
+ * Only located dates reach this block — the query already excluded the rest. The dates
785
+ * are shown because "when" is the question; an empty window says so explicitly rather
786
+ * than leaving the model to fill the gap. Truncation is stated, never silent: a block
787
+ * that looks complete but is not is worse than a short one.
788
+ */
789
+ export function renderTimeWindow(window, facts, options = {}) {
790
+ const cue = escapeAttr(String(window.cue ?? ''));
791
+ // `to` is exclusive; the last day in the window is the millisecond before it.
792
+ const head = `<time-window cue="${cue}" from="${isoDay(window.from)}" to="${isoDay(window.to - 1)}">`;
793
+ if (!Array.isArray(facts) || facts.length === 0) {
794
+ return `${head}\n(${escapeBlockText(String(window.cue ?? '这段时间'))}没有记录)\n</time-window>`;
795
+ }
796
+ let sawInferred = false;
797
+ const lines = facts.map((fact) => {
798
+ const marker = fact.source === 'inferred' ? INFERRED_MARKER : '';
799
+ if (marker) sawInferred = true;
800
+ const when = escapeBlockText(String(factWhen(fact)));
801
+ return `- ${marker}[${fact.id}]${when ? ` ${when}` : ''} ${escapeBlockText(fact.statement)}`;
802
+ });
803
+ if (options.truncated) lines.push(`- (只显示最早 ${facts.length} 条)`);
804
+ const block = `${head}\n${lines.join('\n')}\n</time-window>`;
805
+ return sawInferred ? `${block}\n${SOURCE_NOTES}` : block;
806
+ }
807
+
808
+ export function buildRelatedContext(text, state = {}) {
759
809
  const parts = [];
760
810
  const used = [];
761
811
  // The profile is the merged description of the person; it is the only block that
@@ -824,6 +874,21 @@ function buildRelatedContext(text, state = {}) {
824
874
  log(`timeline failed: ${error instanceof Error ? error.message : String(error)}`);
825
875
  }
826
876
  }
877
+ if (policy.window && plan?.window) {
878
+ try {
879
+ // +1 is a probe, not a fact: when it comes back there were more rows than the
880
+ // block may carry, and the renderer says so instead of pretending completeness.
881
+ const hits = ensureSoul().factsInWindow(plan.window, { limit: TIME_WINDOW_LIMIT + 1 });
882
+ const facts = hits.slice(0, TIME_WINDOW_LIMIT);
883
+ const truncated = hits.length > TIME_WINDOW_LIMIT;
884
+ parts.push(renderTimeWindow(plan.window, facts, { truncated }));
885
+ used.push(...facts.map((fact) => fact.id));
886
+ trace({ event: 'time_window', cue: plan.window.cue, from: plan.window.from, to: plan.window.to, hits: facts.length, truncated });
887
+ log(`time-window: ${plan.window.cue} hits=${facts.length}${truncated ? ' (truncated)' : ''}`);
888
+ } catch (error) {
889
+ log(`time window failed: ${error instanceof Error ? error.message : String(error)}`);
890
+ }
891
+ }
827
892
  if (policy.related) {
828
893
  const pairs = ensureSoul().recallTurnPairs(text);
829
894
  if (pairs.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-plugin-soulbridge",
3
- "version": "0.0.114",
3
+ "version": "0.0.115",
4
4
  "description": "Inject soulbridge context into DSH sessions and feed user turns back as observations.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -42,6 +42,6 @@
42
42
  "setup": "node scripts/install-hooks.mjs"
43
43
  },
44
44
  "dependencies": {
45
- "soulbridge": ">=0.0.112 <0.1.0"
45
+ "soulbridge": ">=0.0.114 <0.1.0"
46
46
  }
47
47
  }