@chenhw7/dsh-memory 0.1.1 → 0.1.3
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/README.md +65 -16
- package/README.zh-CN.md +64 -15
- package/cordis.patch.yml +10 -0
- package/lib/brand.d.ts +7 -0
- package/lib/brand.d.ts.map +1 -1
- package/lib/brand.js +7 -0
- package/lib/brand.js.map +1 -1
- package/lib/client/index.js +826 -0
- package/lib/context/conflict.d.ts +59 -0
- package/lib/context/conflict.d.ts.map +1 -0
- package/lib/context/conflict.js +75 -0
- package/lib/context/conflict.js.map +1 -0
- package/lib/context/index.d.ts +11 -2
- package/lib/context/index.d.ts.map +1 -1
- package/lib/context/index.js +36 -9
- package/lib/context/index.js.map +1 -1
- package/lib/context/policy.d.ts +23 -2
- package/lib/context/policy.d.ts.map +1 -1
- package/lib/context/policy.js +77 -1
- package/lib/context/policy.js.map +1 -1
- package/lib/index.d.ts +50 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +22 -3
- package/lib/index.js.map +1 -1
- package/lib/invariant.d.ts.map +1 -1
- package/lib/remote/index.d.ts +146 -0
- package/lib/remote/index.d.ts.map +1 -0
- package/lib/remote/index.js +257 -0
- package/lib/remote/index.js.map +1 -0
- package/lib/remote/typert.remote-client.d.ts +75 -0
- package/lib/remote/typert.remote-client.js +86 -0
- package/lib/remote/types.d.ts +9 -0
- package/lib/remote/types.d.ts.map +1 -0
- package/lib/remote/types.js +9 -0
- package/lib/remote/types.js.map +1 -0
- package/lib/review/accumulator.d.ts +18 -5
- package/lib/review/accumulator.d.ts.map +1 -1
- package/lib/review/accumulator.js +52 -14
- package/lib/review/accumulator.js.map +1 -1
- package/lib/review/dedup.d.ts +79 -0
- package/lib/review/dedup.d.ts.map +1 -0
- package/lib/review/dedup.js +171 -0
- package/lib/review/dedup.js.map +1 -0
- package/lib/review/extract.d.ts +28 -8
- package/lib/review/extract.d.ts.map +1 -1
- package/lib/review/extract.js +161 -23
- package/lib/review/extract.js.map +1 -1
- package/lib/review/index.d.ts +10 -0
- package/lib/review/index.d.ts.map +1 -1
- package/lib/review/index.js +55 -11
- package/lib/review/index.js.map +1 -1
- package/lib/scanner.d.ts +16 -0
- package/lib/scanner.d.ts.map +1 -1
- package/lib/scanner.js +25 -2
- package/lib/scanner.js.map +1 -1
- package/lib/store/index.d.ts +49 -0
- package/lib/store/index.d.ts.map +1 -1
- package/lib/store/index.js +238 -15
- package/lib/store/index.js.map +1 -1
- package/lib/tool/index.d.ts.map +1 -1
- package/lib/tool/index.js +173 -3
- package/lib/tool/index.js.map +1 -1
- package/lib/types.d.ts +60 -4
- package/lib/types.d.ts.map +1 -1
- package/package.json +39 -17
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The periodic-review projection accumulator: a pure, synchronous fold over
|
|
3
|
-
* `user/message` and `
|
|
4
|
-
*
|
|
5
|
-
* the {@link agent/pre-step} listener drains the accumulator once it
|
|
6
|
-
* the configured threshold and runs one extraction call.
|
|
3
|
+
* `user/message` events (keyword/correction signals) and `tool/result` events
|
|
4
|
+
* (tool-call failures) that collects candidate memory fragments. No LLM runs
|
|
5
|
+
* here — the {@link agent/pre-step} listener drains the accumulator once it
|
|
6
|
+
* reaches the configured threshold and runs one extraction call.
|
|
7
7
|
*
|
|
8
8
|
* @module @chenhw7/dsh-memory/review/accumulator
|
|
9
9
|
*/
|
|
@@ -21,7 +21,7 @@ declare module '@deepseek-ai/dsh-session-projection/types' {
|
|
|
21
21
|
export interface MemoryCandidate {
|
|
22
22
|
/** The text fragment that matched a signal. */
|
|
23
23
|
readonly text: string;
|
|
24
|
-
/** Which signal class matched (`keyword` or `
|
|
24
|
+
/** Which signal class matched (`keyword`, `correction`, or `tool-failure`). */
|
|
25
25
|
readonly signal: string;
|
|
26
26
|
/** Seq of the session event the fragment came from. */
|
|
27
27
|
readonly seq: number;
|
|
@@ -50,10 +50,23 @@ export declare function messageText(event: SessionEvent): string | undefined;
|
|
|
50
50
|
* @returns the matching signal name, or `undefined` when no rule fires.
|
|
51
51
|
*/
|
|
52
52
|
export declare function detectSignal(text: string): string | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* Extract a failure fragment from a `tool/result` event that carries an error.
|
|
55
|
+
* @param event - a `tool/result` session event.
|
|
56
|
+
* @returns the failure description text, or `undefined` when the event has no error.
|
|
57
|
+
*/
|
|
58
|
+
export declare function toolFailureText(event: SessionEvent): string | undefined;
|
|
53
59
|
/**
|
|
54
60
|
* The projection's `apply` transition: fold one committed event into the
|
|
55
61
|
* accumulator. Returns the SAME state reference for events that do not
|
|
56
62
|
* contribute a candidate (the registry's `Object.is` gate).
|
|
63
|
+
*
|
|
64
|
+
* Signal sources:
|
|
65
|
+
* - `user/message` — keyword hits (explicit "remember" intent) and corrections
|
|
66
|
+
* (user revises a prior statement).
|
|
67
|
+
* - `tool/result` — tool-call failures (§3.6 richer signals): a failed tool
|
|
68
|
+
* execution may carry a durable workaround worth remembering.
|
|
69
|
+
*
|
|
57
70
|
* @param state - the state covering all prior events.
|
|
58
71
|
* @param event - the next committed session event.
|
|
59
72
|
* @returns the next state (same reference when the event contributes nothing).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accumulator.d.ts","sourceRoot":"","sources":["../../src/review/accumulator.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAA;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAE5D,6EAA6E;AAC7E,eAAO,MAAM,4BAA4B,6BAA6B,CAAA;AAEtE,OAAO,QAAQ,2CAA2C,CAAC;IACzD,UAAU,oBAAoB;QAC5B,sEAAsE;QACtE,0BAA0B,EAAE,eAAe,CAAA;KAC5C;CACF;AAED,2DAA2D;AAC3D,MAAM,WAAW,eAAe;IAC9B,+CAA+C;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAA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BAAgB,CAAC,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,YAAY,GAAG,gBAAgB,CAgB/F;AAED,qDAAqD;AACrD,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,eAAe,CAOrD,CAAA"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The periodic-review projection accumulator: a pure, synchronous fold over
|
|
3
|
-
* `user/message` and `
|
|
4
|
-
*
|
|
5
|
-
* the {@link agent/pre-step} listener drains the accumulator once it
|
|
6
|
-
* the configured threshold and runs one extraction call.
|
|
3
|
+
* `user/message` events (keyword/correction signals) and `tool/result` events
|
|
4
|
+
* (tool-call failures) that collects candidate memory fragments. No LLM runs
|
|
5
|
+
* here — the {@link agent/pre-step} listener drains the accumulator once it
|
|
6
|
+
* reaches the configured threshold and runs one extraction call.
|
|
7
7
|
*
|
|
8
8
|
* @module @chenhw7/dsh-memory/review/accumulator
|
|
9
9
|
*/
|
|
@@ -69,25 +69,63 @@ export function detectSignal(text) {
|
|
|
69
69
|
}
|
|
70
70
|
return undefined;
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Extract a failure fragment from a `tool/result` event that carries an error.
|
|
74
|
+
* @param event - a `tool/result` session event.
|
|
75
|
+
* @returns the failure description text, or `undefined` when the event has no error.
|
|
76
|
+
*/
|
|
77
|
+
export function toolFailureText(event) {
|
|
78
|
+
if (event.type !== 'tool/result')
|
|
79
|
+
return undefined;
|
|
80
|
+
const data = event.data;
|
|
81
|
+
if (data.error === undefined)
|
|
82
|
+
return undefined;
|
|
83
|
+
// Extract text from the result content if available, plus the error code.
|
|
84
|
+
const parts = [];
|
|
85
|
+
const content = data.message?.content;
|
|
86
|
+
if (content !== undefined) {
|
|
87
|
+
for (const block of content) {
|
|
88
|
+
if (block.type === 'text' && typeof block.text === 'string')
|
|
89
|
+
parts.push(block.text);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const errorInfo = `tool error: ${data.error.name} (${data.error.code})`;
|
|
93
|
+
return parts.length > 0 ? `${errorInfo} — ${parts.join(' ')}` : errorInfo;
|
|
94
|
+
}
|
|
72
95
|
/**
|
|
73
96
|
* The projection's `apply` transition: fold one committed event into the
|
|
74
97
|
* accumulator. Returns the SAME state reference for events that do not
|
|
75
98
|
* contribute a candidate (the registry's `Object.is` gate).
|
|
99
|
+
*
|
|
100
|
+
* Signal sources:
|
|
101
|
+
* - `user/message` — keyword hits (explicit "remember" intent) and corrections
|
|
102
|
+
* (user revises a prior statement).
|
|
103
|
+
* - `tool/result` — tool-call failures (§3.6 richer signals): a failed tool
|
|
104
|
+
* execution may carry a durable workaround worth remembering.
|
|
105
|
+
*
|
|
76
106
|
* @param state - the state covering all prior events.
|
|
77
107
|
* @param event - the next committed session event.
|
|
78
108
|
* @returns the next state (same reference when the event contributes nothing).
|
|
79
109
|
*/
|
|
80
110
|
export function applyAccumulator(state, event) {
|
|
81
|
-
if (event.type
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
111
|
+
if (event.type === 'user/message') {
|
|
112
|
+
const text = messageText(event);
|
|
113
|
+
if (text === undefined)
|
|
114
|
+
return state;
|
|
115
|
+
const signal = detectSignal(text);
|
|
116
|
+
if (signal === undefined)
|
|
117
|
+
return state;
|
|
118
|
+
const candidate = { text, signal, seq: event.seq };
|
|
119
|
+
return { candidates: [...state.candidates, candidate], count: state.count + 1 };
|
|
120
|
+
}
|
|
121
|
+
if (event.type === 'tool/result') {
|
|
122
|
+
const text = toolFailureText(event);
|
|
123
|
+
if (text === undefined)
|
|
124
|
+
return state;
|
|
125
|
+
const candidate = { text, signal: 'tool-failure', seq: event.seq };
|
|
126
|
+
return { candidates: [...state.candidates, candidate], count: state.count + 1 };
|
|
127
|
+
}
|
|
128
|
+
return state;
|
|
91
129
|
}
|
|
92
130
|
/** Zod schema for the accumulator's wire payload. */
|
|
93
131
|
export const accumulatorSchema = zod.object({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"accumulator.js","sourceRoot":"","sources":["../../src/review/accumulator.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,CAAC,IAAI,GAAG,EAAE,MAAM,KAAK,CAAA;AAI9B,6EAA6E;AAC7E,MAAM,CAAC,MAAM,4BAA4B,GAAG,0BAA0B,CAAA;AA8BtE,uEAAuE;AACvE,MAAM,gBAAgB,GAA8D;IAClF,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,IAAI,EAAE;IAC7B,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE;IAC9B,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,KAAK,EAAE;IAC9B,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,kBAAkB,EAAE;IAC3C,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,kBAAkB,EAAE;IAC3C,EAAE,IAAI,EAAE,SAAS,EAAE,EAAE,EAAE,kBAAkB,EAAE;CAC5C,CAAA;AAED,+EAA+E;AAC/E,MAAM,mBAAmB,GAA8D;IACrF,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,IAAI,EAAE;IAChC,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,IAAI,EAAE;IAChC,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,kBAAkB,EAAE;IAC9C,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,kBAAkB,EAAE;IAC9C,EAAE,IAAI,EAAE,YAAY,EAAE,EAAE,EAAE,WAAW,EAAE;CACxC,CAAA;AAED,+CAA+C;AAC/C,MAAM,CAAC,MAAM,gBAAgB,GAAqB,EAAE,UAAU,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAA;AAE9E;;;;GAIG;AACH,MAAM,UAAU,WAAW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AAI,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,CAAA;QACnC,IAAI,IAAI,KAAK,SAAS;YAAE,OAAO,KAAK,CAAA;QACpC,MAAM,SAAS,GAAoB,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,KAAK,CAAC,GAAG,EAAE,CAAA;QACnF,OAAO,EAAE,UAAU,EAAE,CAAC,GAAG,KAAK,CAAC,UAAU,EAAE,SAAS,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,CAAA;IACjF,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,qDAAqD;AACrD,MAAM,CAAC,MAAM,iBAAiB,GAA6B,GAAG,CAAC,MAAM,CAAC;IACpE,UAAU,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC;QAC/B,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE;QAClB,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE;QACpB,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE;KAClB,CAAC,CAAC;IACH,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;CACpB,CAAC,CAAA"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deduplication prefilter for the extraction path. A cheap, embedding-free
|
|
3
|
+
* normalized-token Jaccard similarity check that runs before `add`: when a new
|
|
4
|
+
* candidate is near-duplicate to an existing entry, the caller merges (update)
|
|
5
|
+
* instead of creating a new entry.
|
|
6
|
+
*
|
|
7
|
+
* This module is pure and dependency-free — it operates on strings and
|
|
8
|
+
* returned entry shapes, never touching the store or the LLM.
|
|
9
|
+
*
|
|
10
|
+
* @module @chenhw7/dsh-memory/review/dedup
|
|
11
|
+
*/
|
|
12
|
+
import type { MemoryEntry } from '../types.ts';
|
|
13
|
+
/**
|
|
14
|
+
* Tokenize content for dedup comparison. Normalizes to lowercase, splits on
|
|
15
|
+
* word boundaries for Latin, and matches CJK per-character. English stop words
|
|
16
|
+
* are removed from Latin tokens; CJK stop characters (high-frequency
|
|
17
|
+
* grammatical particles) are removed from CJK tokens so unrelated Chinese
|
|
18
|
+
* sentences don't share too many tokens. Returns a Set of unique tokens.
|
|
19
|
+
*/
|
|
20
|
+
export declare function tokenize(content: string): Set<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Jaccard similarity between two token sets: |A ∩ B| / |A ∪ B|.
|
|
23
|
+
* Returns 0 when both sets are empty (no meaningful overlap), 1 when identical.
|
|
24
|
+
*/
|
|
25
|
+
export declare function jaccardSimilarity(a: Set<string>, b: Set<string>): number;
|
|
26
|
+
/** One existing entry projected to the fields the dedup prefilter needs. */
|
|
27
|
+
export interface DedupCandidate {
|
|
28
|
+
readonly id: string;
|
|
29
|
+
readonly scope: string;
|
|
30
|
+
readonly content: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Find the best near-duplicate among existing entries for a new candidate.
|
|
34
|
+
* Returns the matching entry id when similarity exceeds the threshold, or
|
|
35
|
+
* `undefined` when no existing entry is close enough (a genuine new memory).
|
|
36
|
+
*
|
|
37
|
+
* Only entries in the *same scope* are compared — a project convention and a
|
|
38
|
+
* user preference are never duplicates even if they share words.
|
|
39
|
+
*
|
|
40
|
+
* @param candidateContent - the new memory content to check.
|
|
41
|
+
* @param candidateScope - the scope of the new memory.
|
|
42
|
+
* @param existing - all current entries from the store.
|
|
43
|
+
* @param threshold - Jaccard similarity above which two entries are duplicates.
|
|
44
|
+
* @returns the existing entry id to merge into, or `undefined` for a new entry.
|
|
45
|
+
*/
|
|
46
|
+
export declare function findDuplicate(candidateContent: string, candidateScope: string, existing: readonly DedupCandidate[], threshold?: number): string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Merge two memory contents into one. When the new content is a strict
|
|
49
|
+
* superset (contains the old text as a substring), the new content wins
|
|
50
|
+
* outright. Otherwise, append the new content as an addendum so no
|
|
51
|
+
* information is lost.
|
|
52
|
+
*/
|
|
53
|
+
export declare function mergeContent(oldContent: string, newContent: string): string;
|
|
54
|
+
/**
|
|
55
|
+
* Project a MemoryEntry to the minimal shape findDuplicate needs.
|
|
56
|
+
*/
|
|
57
|
+
export declare function toDedupCandidate(entry: MemoryEntry): DedupCandidate;
|
|
58
|
+
/** The LLM judge's verdict for a prefilter-flagged pair. */
|
|
59
|
+
export type JudgeVerdict = 'duplicate' | 'update' | 'new';
|
|
60
|
+
/** System prompt for the dedup judge — minimal, one-word output protocol. */
|
|
61
|
+
export declare const JUDGE_SYSTEM_PROMPT: string;
|
|
62
|
+
/**
|
|
63
|
+
* Build the user message for the dedup judge: present the existing entry
|
|
64
|
+
* content and the new candidate content side by side.
|
|
65
|
+
* @param existingContent - the current stored entry's content.
|
|
66
|
+
* @param newContent - the new candidate content flagged by the prefilter.
|
|
67
|
+
* @returns the model-facing user message text.
|
|
68
|
+
*/
|
|
69
|
+
export declare function buildJudgePrompt(existingContent: string, newContent: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Parse the judge's one-line output into a verdict. Strict: lowercases, trims,
|
|
72
|
+
* and matches against the three valid words. Defaults to `duplicate` on
|
|
73
|
+
* anything unrecognized (the safe fallback — merge rather than create a
|
|
74
|
+
* spurious duplicate).
|
|
75
|
+
* @param text - the raw model output.
|
|
76
|
+
* @returns the verdict.
|
|
77
|
+
*/
|
|
78
|
+
export declare function parseJudgeVerdict(text: string): JudgeVerdict;
|
|
79
|
+
//# sourceMappingURL=dedup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dedup.d.ts","sourceRoot":"","sources":["../../src/review/dedup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAyC9C;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAerD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CAQxE;AAED,4EAA4E;AAC5E,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACzB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAC3B,gBAAgB,EAAE,MAAM,EACxB,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,SAAS,cAAc,EAAE,EACnC,SAAS,GAAE,MAAa,GACvB,MAAM,GAAG,SAAS,CAepB;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAK3E;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,GAAG,cAAc,CAEnE;AAID,4DAA4D;AAC5D,MAAM,MAAM,YAAY,GAAG,WAAW,GAAG,QAAQ,GAAG,KAAK,CAAA;AAEzD,6EAA6E;AAC7E,eAAO,MAAM,mBAAmB,QAKkD,CAAA;AAElF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,eAAe,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAEpF;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAK5D"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deduplication prefilter for the extraction path. A cheap, embedding-free
|
|
3
|
+
* normalized-token Jaccard similarity check that runs before `add`: when a new
|
|
4
|
+
* candidate is near-duplicate to an existing entry, the caller merges (update)
|
|
5
|
+
* instead of creating a new entry.
|
|
6
|
+
*
|
|
7
|
+
* This module is pure and dependency-free — it operates on strings and
|
|
8
|
+
* returned entry shapes, never touching the store or the LLM.
|
|
9
|
+
*
|
|
10
|
+
* @module @chenhw7/dsh-memory/review/dedup
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Common English stop words excluded from dedup tokenization. These carry
|
|
14
|
+
* little semantic signal and inflate Jaccard similarity between unrelated
|
|
15
|
+
* short sentences (e.g. "The server is unstable" vs "The tunnel is long"
|
|
16
|
+
* share "the" and "is" but are unrelated). Removing them sharpens the
|
|
17
|
+
* comparison toward content-bearing words.
|
|
18
|
+
*/
|
|
19
|
+
const STOP_WORDS = new Set([
|
|
20
|
+
'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
|
|
21
|
+
'to', 'of', 'in', 'on', 'at', 'for', 'with', 'by', 'from', 'as', 'or',
|
|
22
|
+
'and', 'not', 'no', 'do', 'does', 'did', 'has', 'have', 'had', 'this',
|
|
23
|
+
'that', 'these', 'those', 'it', 'its', 'but', 'so', 'if', 'then',
|
|
24
|
+
]);
|
|
25
|
+
/**
|
|
26
|
+
* Common CJK (Chinese) stop characters excluded from dedup tokenization.
|
|
27
|
+
* These are high-frequency grammatical particles and structural words that
|
|
28
|
+
* inflate Jaccard similarity between unrelated Chinese sentences (e.g.
|
|
29
|
+
* "这个项目使用pnpm" vs "这个项目使用vitest" share 这/个/项/目/使/用 = 0.75,
|
|
30
|
+
* but are about different tools). Removing them sharpens comparison toward
|
|
31
|
+
* content-bearing characters.
|
|
32
|
+
*/
|
|
33
|
+
const CJK_STOP_CHARS = new Set([
|
|
34
|
+
// 结构词:这个、那、些
|
|
35
|
+
'这', '个', '那', '些',
|
|
36
|
+
// 助词
|
|
37
|
+
'的', '了', '着', '过', '地', '得',
|
|
38
|
+
// 判断/系词
|
|
39
|
+
'是', '在', '为',
|
|
40
|
+
// 介词/连词
|
|
41
|
+
'和', '与', '或', '但', '而', '由', '于', '对', '向', '从', '把', '被', '将',
|
|
42
|
+
// 否定/副词
|
|
43
|
+
'不', '没', '也', '都', '就', '还', '只', '才', '已', '再',
|
|
44
|
+
// 动词虚化高频
|
|
45
|
+
'用', '有', '会', '能', '要', '可', '以',
|
|
46
|
+
// 量词/代词高频
|
|
47
|
+
'一', '上', '下', '中',
|
|
48
|
+
]);
|
|
49
|
+
/**
|
|
50
|
+
* Tokenize content for dedup comparison. Normalizes to lowercase, splits on
|
|
51
|
+
* word boundaries for Latin, and matches CJK per-character. English stop words
|
|
52
|
+
* are removed from Latin tokens; CJK stop characters (high-frequency
|
|
53
|
+
* grammatical particles) are removed from CJK tokens so unrelated Chinese
|
|
54
|
+
* sentences don't share too many tokens. Returns a Set of unique tokens.
|
|
55
|
+
*/
|
|
56
|
+
export function tokenize(content) {
|
|
57
|
+
const lowered = content.toLowerCase();
|
|
58
|
+
const tokens = new Set();
|
|
59
|
+
const re = /[a-z0-9]+|[\u3040-\u309f\u30a0-\u30ff\u4e00-\u9fff\u3400-\u4dbf\uac00-\ud7af]/g;
|
|
60
|
+
let match;
|
|
61
|
+
while ((match = re.exec(lowered)) !== null) {
|
|
62
|
+
const token = match[0];
|
|
63
|
+
// Filter English stop words from Latin tokens.
|
|
64
|
+
if (token.length > 1 && STOP_WORDS.has(token))
|
|
65
|
+
continue;
|
|
66
|
+
if (token.length === 1 && /[a-z0-9]/.test(token))
|
|
67
|
+
continue;
|
|
68
|
+
// Filter CJK stop characters (high-frequency grammatical particles).
|
|
69
|
+
if (CJK_STOP_CHARS.has(token))
|
|
70
|
+
continue;
|
|
71
|
+
tokens.add(token);
|
|
72
|
+
}
|
|
73
|
+
return tokens;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Jaccard similarity between two token sets: |A ∩ B| / |A ∪ B|.
|
|
77
|
+
* Returns 0 when both sets are empty (no meaningful overlap), 1 when identical.
|
|
78
|
+
*/
|
|
79
|
+
export function jaccardSimilarity(a, b) {
|
|
80
|
+
if (a.size === 0 && b.size === 0)
|
|
81
|
+
return 0;
|
|
82
|
+
let intersection = 0;
|
|
83
|
+
for (const token of a) {
|
|
84
|
+
if (b.has(token))
|
|
85
|
+
intersection++;
|
|
86
|
+
}
|
|
87
|
+
const union = a.size + b.size - intersection;
|
|
88
|
+
return union === 0 ? 0 : intersection / union;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Find the best near-duplicate among existing entries for a new candidate.
|
|
92
|
+
* Returns the matching entry id when similarity exceeds the threshold, or
|
|
93
|
+
* `undefined` when no existing entry is close enough (a genuine new memory).
|
|
94
|
+
*
|
|
95
|
+
* Only entries in the *same scope* are compared — a project convention and a
|
|
96
|
+
* user preference are never duplicates even if they share words.
|
|
97
|
+
*
|
|
98
|
+
* @param candidateContent - the new memory content to check.
|
|
99
|
+
* @param candidateScope - the scope of the new memory.
|
|
100
|
+
* @param existing - all current entries from the store.
|
|
101
|
+
* @param threshold - Jaccard similarity above which two entries are duplicates.
|
|
102
|
+
* @returns the existing entry id to merge into, or `undefined` for a new entry.
|
|
103
|
+
*/
|
|
104
|
+
export function findDuplicate(candidateContent, candidateScope, existing, threshold = 0.15) {
|
|
105
|
+
const candidateTokens = tokenize(candidateContent);
|
|
106
|
+
if (candidateTokens.size === 0)
|
|
107
|
+
return undefined;
|
|
108
|
+
let bestId;
|
|
109
|
+
let bestScore = threshold;
|
|
110
|
+
for (const entry of existing) {
|
|
111
|
+
if (entry.scope !== candidateScope)
|
|
112
|
+
continue;
|
|
113
|
+
const score = jaccardSimilarity(candidateTokens, tokenize(entry.content));
|
|
114
|
+
if (score > bestScore) {
|
|
115
|
+
bestScore = score;
|
|
116
|
+
bestId = entry.id;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return bestId;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Merge two memory contents into one. When the new content is a strict
|
|
123
|
+
* superset (contains the old text as a substring), the new content wins
|
|
124
|
+
* outright. Otherwise, append the new content as an addendum so no
|
|
125
|
+
* information is lost.
|
|
126
|
+
*/
|
|
127
|
+
export function mergeContent(oldContent, newContent) {
|
|
128
|
+
if (oldContent.includes(newContent) || newContent.includes(oldContent)) {
|
|
129
|
+
return newContent.length >= oldContent.length ? newContent : oldContent;
|
|
130
|
+
}
|
|
131
|
+
return `${oldContent} ${newContent}`;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Project a MemoryEntry to the minimal shape findDuplicate needs.
|
|
135
|
+
*/
|
|
136
|
+
export function toDedupCandidate(entry) {
|
|
137
|
+
return { id: entry.id, scope: entry.scope, content: entry.content };
|
|
138
|
+
}
|
|
139
|
+
/** System prompt for the dedup judge — minimal, one-word output protocol. */
|
|
140
|
+
export const JUDGE_SYSTEM_PROMPT = 'You are a memory dedup judge. Given an existing memory entry and a new candidate, decide:'
|
|
141
|
+
+ '\n- "duplicate": the new candidate restates the same fact as the existing entry (different wording, same meaning). The existing entry should be kept.'
|
|
142
|
+
+ '\n- "update": the new candidate is a correction or more precise version of the existing entry. The new content should replace the old.'
|
|
143
|
+
+ '\n- "new": the new candidate is a genuinely different fact that happens to share words with the existing entry. Both should be kept as separate entries.'
|
|
144
|
+
+ '\n\nOutput exactly one word: duplicate, update, or new. Output nothing else.';
|
|
145
|
+
/**
|
|
146
|
+
* Build the user message for the dedup judge: present the existing entry
|
|
147
|
+
* content and the new candidate content side by side.
|
|
148
|
+
* @param existingContent - the current stored entry's content.
|
|
149
|
+
* @param newContent - the new candidate content flagged by the prefilter.
|
|
150
|
+
* @returns the model-facing user message text.
|
|
151
|
+
*/
|
|
152
|
+
export function buildJudgePrompt(existingContent, newContent) {
|
|
153
|
+
return `Existing memory:\n${existingContent}\n\nNew candidate:\n${newContent}`;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Parse the judge's one-line output into a verdict. Strict: lowercases, trims,
|
|
157
|
+
* and matches against the three valid words. Defaults to `duplicate` on
|
|
158
|
+
* anything unrecognized (the safe fallback — merge rather than create a
|
|
159
|
+
* spurious duplicate).
|
|
160
|
+
* @param text - the raw model output.
|
|
161
|
+
* @returns the verdict.
|
|
162
|
+
*/
|
|
163
|
+
export function parseJudgeVerdict(text) {
|
|
164
|
+
const word = text.trim().toLowerCase();
|
|
165
|
+
if (word === 'update')
|
|
166
|
+
return 'update';
|
|
167
|
+
if (word === 'new')
|
|
168
|
+
return 'new';
|
|
169
|
+
return 'duplicate';
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=dedup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dedup.js","sourceRoot":"","sources":["../../src/review/dedup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH;;;;;;GAMG;AACH,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC;IACzB,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO;IACnE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI;IACrE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IACrE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM;CACjE,CAAC,CAAA;AAEF;;;;;;;GAOG;AACH,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC;IAC7B,aAAa;IACb,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;IAClB,KAAK;IACL,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;IAC5B,QAAQ;IACR,GAAG,EAAE,G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AKG;AACH,MAAM,UAAU,YAAY,CAAC,UAAkB,EAAE,UAAkB;IACjE,IAAI,UAAU,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACvE,OAAO,UAAU,CAAC,MAAM,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,CAAA;IACzE,CAAC;IACD,OAAO,GAAG,UAAU,IAAI,UAAU,EAAE,CAAA;AACtC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAkB;IACjD,OAAO,EAAE,EAAE,EAAE,KAAK,CAAC,EAAY,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;AAC/E,CAAC;AAOD,6EAA6E;AAC7E,MAAM,CAAC,MAAM,mBAAmB,GAC9B,2FAA2F;MACzF,uJAAuJ;MACvJ,wIAAwI;MACxI,0JAA0J;MAC1J,8EAA8E,CAAA;AAElF;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,eAAuB,EAAE,UAAkB;IAC1E,OAAO,qBAAqB,eAAe,uBAAuB,UAAU,EAAE,CAAA;AAChF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAA;IACtC,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAA;IACtC,IAAI,IAAI,KAAK,KAAK;QAAE,OAAO,KAAK,CAAA;IAChC,OAAO,WAAW,CAAA;AACpB,CAAC"}
|
package/lib/review/extract.d.ts
CHANGED
|
@@ -16,13 +16,13 @@ import type { Context } from '@deepseek-ai/cordis';
|
|
|
16
16
|
import type { GenerateOptions, Message } from '@deepseek-ai/dsh-llm';
|
|
17
17
|
import type { Session } from '@deepseek-ai/dsh-session';
|
|
18
18
|
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
19
|
-
import type { MemoryCategory, MemoryScope } from '../types.ts';
|
|
19
|
+
import type { AuditSource, MemoryCategory, MemoryScope } from '../types.ts';
|
|
20
20
|
import type { MemoryStore } from '../index.ts';
|
|
21
21
|
import type { MemoryCandidate } from './accumulator.ts';
|
|
22
22
|
/** System prompt for the periodic-review extraction. */
|
|
23
|
-
export declare const REVIEW_SYSTEM_PROMPT
|
|
23
|
+
export declare const REVIEW_SYSTEM_PROMPT: string;
|
|
24
24
|
/** System prompt for the compaction/dispose flush (方案 C). */
|
|
25
|
-
export declare const FLUSH_SYSTEM_PROMPT
|
|
25
|
+
export declare const FLUSH_SYSTEM_PROMPT: string;
|
|
26
26
|
/** One parsed memory entry awaiting scanner + store validation. */
|
|
27
27
|
export interface ParsedMemory {
|
|
28
28
|
/** Which scope the extracted memory belongs to. */
|
|
@@ -60,6 +60,11 @@ export declare function buildReviewMessages(memorySnapshot: string, candidates:
|
|
|
60
60
|
* @returns the model-facing user message list.
|
|
61
61
|
*/
|
|
62
62
|
export declare function buildFlushMessages(fragments: readonly string[]): Message[];
|
|
63
|
+
/** An optional provider/model override for extraction calls (§3.6). */
|
|
64
|
+
export interface ExtractionModelOverride {
|
|
65
|
+
readonly provider?: string;
|
|
66
|
+
readonly model?: string;
|
|
67
|
+
}
|
|
63
68
|
/**
|
|
64
69
|
* Collect the assembled text from one LLM stream. Throws on an error/aborted
|
|
65
70
|
* finish; returns the concatenated text-block content on success.
|
|
@@ -79,16 +84,30 @@ export declare function collectStreamText(ctx: Context, options: GenerateOptions
|
|
|
79
84
|
* @param signal - optional abort signal.
|
|
80
85
|
* @returns the parsed memory entries (possibly empty on failure).
|
|
81
86
|
*/
|
|
82
|
-
export declare function extractMemories(ctx: Context, session: Session, system: string, messages: Message[], signal?: AbortSignal): Promise<ParsedMemory[]>;
|
|
87
|
+
export declare function extractMemories(ctx: Context, session: Session, system: string, messages: Message[], signal?: AbortSignal, modelOverride?: ExtractionModelOverride): Promise<ParsedMemory[]>;
|
|
83
88
|
/**
|
|
84
89
|
* Store parsed memory entries through the scanner and the memory store. Each
|
|
85
90
|
* entry is independent: a scanner rejection or store failure skips that
|
|
86
91
|
* entry without throwing. Does nothing when no memory store is mounted.
|
|
92
|
+
*
|
|
93
|
+
* Dedup flow: the cheap Jaccard prefilter runs first. When it flags a
|
|
94
|
+
* near-duplicate, an optional LLM judge (§3.4) determines the verdict:
|
|
95
|
+
* `duplicate` → merge content, `update` → replace with new content,
|
|
96
|
+
* `new` → create a separate entry. The judge is best-effort: on failure
|
|
97
|
+
* it falls back to `duplicate` (safe merge). When `judgeEnabled` is false,
|
|
98
|
+
* prefilter hits merge directly (the original §3.4 behavior).
|
|
87
99
|
* @param ctx - context carrying an optional `memory` service.
|
|
88
100
|
* @param parsed - the entries to store.
|
|
89
|
-
* @param
|
|
101
|
+
* @param attachCategory - optional category to attach to every entry.
|
|
102
|
+
* @param source - provenance tag for the audit trail (`'review'` or `'flush'`).
|
|
103
|
+
* @param sessionId - the session id, recorded in each audit entry.
|
|
104
|
+
* @param inferredProjectName - when the session cwd implies a project, project-scoped
|
|
105
|
+
* entries that lack a projectName get this value (§3.6 project auto-detection).
|
|
106
|
+
* @param session - the live session, for routing the LLM judge call.
|
|
107
|
+
* @param modelOverride - optional provider/model override for the judge (§3.6).
|
|
108
|
+
* @param judgeEnabled - whether to run the LLM judge on prefilter hits (default true).
|
|
90
109
|
*/
|
|
91
|
-
export declare function storeMemories(ctx: Context, parsed: readonly ParsedMemory[], attachCategory?: MemoryCategory): Promise<void>;
|
|
110
|
+
export declare function storeMemories(ctx: Context, parsed: readonly ParsedMemory[], attachCategory?: MemoryCategory, source?: AuditSource, sessionId?: string, inferredProjectName?: string, session?: Session, modelOverride?: ExtractionModelOverride, judgeEnabled?: boolean): Promise<void>;
|
|
92
111
|
/**
|
|
93
112
|
* Run the periodic-review extraction: build the review prompt from the
|
|
94
113
|
* current memory snapshot and the accumulated candidates, extract, and store.
|
|
@@ -97,7 +116,7 @@ export declare function storeMemories(ctx: Context, parsed: readonly ParsedMemor
|
|
|
97
116
|
* @param agent - the live agent whose session routes the call.
|
|
98
117
|
* @param candidates - the accumulated candidate fragments.
|
|
99
118
|
*/
|
|
100
|
-
export declare function runReviewExtraction(ctx: Context, agent: Agent, candidates: readonly MemoryCandidate[]): Promise<number>;
|
|
119
|
+
export declare function runReviewExtraction(ctx: Context, agent: Agent, candidates: readonly MemoryCandidate[], modelOverride?: ExtractionModelOverride, judgeEnabled?: boolean): Promise<number>;
|
|
101
120
|
/**
|
|
102
121
|
* Run the compaction/dispose flush extraction: build the flush prompt from the
|
|
103
122
|
* shadowed conversation fragments, extract, and store. Best-effort and
|
|
@@ -106,7 +125,8 @@ export declare function runReviewExtraction(ctx: Context, agent: Agent, candidat
|
|
|
106
125
|
* @param session - the session whose request header routes the call.
|
|
107
126
|
* @param fragments - the raw conversation fragments being shadowed.
|
|
108
127
|
* @param signal - optional abort signal.
|
|
128
|
+
* @param modelOverride - optional provider/model override (§3.6).
|
|
109
129
|
* @returns the number of parsed entries (before scanner/store filtering).
|
|
110
130
|
*/
|
|
111
|
-
export declare function runFlushExtraction(ctx: Context, session: Session, fragments: readonly string[], signal?: AbortSignal): Promise<number>;
|
|
131
|
+
export declare function runFlushExtraction(ctx: Context, session: Session, fragments: readonly string[], signal?: AbortSignal, modelOverride?: ExtractionModelOverride, judgeEnabled?: boolean): Promise<number>;
|
|
112
132
|
//# sourceMappingURL=extract.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../../src/review/extract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAElD,OAAO,KAAK,EAAE,eAAe,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAA;AAEpE,OAAO,KAAK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