@chenhw7/dsh-memory 0.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/LICENSE +21 -0
- package/README.md +288 -0
- package/README.zh-CN.md +280 -0
- package/cordis.patch.yml +32 -0
- package/lib/brand.d.ts +10 -0
- package/lib/brand.d.ts.map +1 -0
- package/lib/brand.js +10 -0
- package/lib/brand.js.map +1 -0
- package/lib/context/index.d.ts +70 -0
- package/lib/context/index.d.ts.map +1 -0
- package/lib/context/index.js +123 -0
- package/lib/context/index.js.map +1 -0
- package/lib/context/policy.d.ts +28 -0
- package/lib/context/policy.d.ts.map +1 -0
- package/lib/context/policy.js +52 -0
- package/lib/context/policy.js.map +1 -0
- package/lib/index.d.ts +75 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +33 -0
- package/lib/index.js.map +1 -0
- package/lib/invariant.d.ts +27 -0
- package/lib/invariant.d.ts.map +1 -0
- package/lib/invariant.js +42 -0
- package/lib/invariant.js.map +1 -0
- package/lib/review/accumulator.d.ts +64 -0
- package/lib/review/accumulator.d.ts.map +1 -0
- package/lib/review/accumulator.js +101 -0
- package/lib/review/accumulator.js.map +1 -0
- package/lib/review/extract.d.ts +112 -0
- package/lib/review/extract.d.ts.map +1 -0
- package/lib/review/extract.js +249 -0
- package/lib/review/extract.js.map +1 -0
- package/lib/review/index.d.ts +45 -0
- package/lib/review/index.d.ts.map +1 -0
- package/lib/review/index.js +173 -0
- package/lib/review/index.js.map +1 -0
- package/lib/scanner.d.ts +21 -0
- package/lib/scanner.d.ts.map +1 -0
- package/lib/scanner.js +75 -0
- package/lib/scanner.js.map +1 -0
- package/lib/store/index.d.ts +24 -0
- package/lib/store/index.d.ts.map +1 -0
- package/lib/store/index.js +137 -0
- package/lib/store/index.js.map +1 -0
- package/lib/tool/index.d.ts +33 -0
- package/lib/tool/index.d.ts.map +1 -0
- package/lib/tool/index.js +455 -0
- package/lib/tool/index.js.map +1 -0
- package/lib/types.d.ts +123 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +10 -0
- package/lib/types.js.map +1 -0
- package/package.json +94 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The periodic-review projection accumulator: a pure, synchronous fold over
|
|
3
|
+
* `user/message` and `assistant/message` events that collects candidate memory
|
|
4
|
+
* fragments using lightweight keyword/correction rules. No LLM runs here —
|
|
5
|
+
* the {@link agent/pre-step} listener drains the accumulator once it reaches
|
|
6
|
+
* the configured threshold and runs one extraction call.
|
|
7
|
+
*
|
|
8
|
+
* @module @chenhw7/dsh-memory/review/accumulator
|
|
9
|
+
*/
|
|
10
|
+
import { z as zod } from 'zod';
|
|
11
|
+
/** The projection key this unit owns in the {@link SessionProjectionMap}. */
|
|
12
|
+
export const MEMORY_REVIEW_PROJECTION_KEY = 'memory-review-candidates';
|
|
13
|
+
/** Keyword hits in user messages: explicit instruction to remember. */
|
|
14
|
+
const KEYWORD_PATTERNS = [
|
|
15
|
+
{ name: 'keyword', re: /记住/ },
|
|
16
|
+
{ name: 'keyword', re: /别忘了/ },
|
|
17
|
+
{ name: 'keyword', re: /以后都/ },
|
|
18
|
+
{ name: 'keyword', re: /remember\s+that/i },
|
|
19
|
+
{ name: 'keyword', re: /don'?t\s+forget/i },
|
|
20
|
+
{ name: 'keyword', re: /from\s+now\s+on/i },
|
|
21
|
+
];
|
|
22
|
+
/** Correction signals in user messages: the user revises a prior statement. */
|
|
23
|
+
const CORRECTION_PATTERNS = [
|
|
24
|
+
{ name: 'correction', re: /不对/ },
|
|
25
|
+
{ name: 'correction', re: /不要/ },
|
|
26
|
+
{ name: 'correction', re: /no,?\s+i\s+said/i },
|
|
27
|
+
{ name: 'correction', re: /that'?s\s+wrong/i },
|
|
28
|
+
{ name: 'correction', re: /actually/i },
|
|
29
|
+
];
|
|
30
|
+
/** The empty-log state for the accumulator. */
|
|
31
|
+
export const emptyAccumulator = { candidates: [], count: 0 };
|
|
32
|
+
/**
|
|
33
|
+
* Extract plain text from a message event's content blocks.
|
|
34
|
+
* @param event - a `user/message` or `assistant/message` session event.
|
|
35
|
+
* @returns the concatenated text, or `undefined` when the event carries no text.
|
|
36
|
+
*/
|
|
37
|
+
export function messageText(event) {
|
|
38
|
+
let content;
|
|
39
|
+
if (event.type === 'user/message') {
|
|
40
|
+
content = event.data.content;
|
|
41
|
+
}
|
|
42
|
+
else if (event.type === 'assistant/message') {
|
|
43
|
+
content = event.data.message.content;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
const parts = [];
|
|
49
|
+
for (const block of content) {
|
|
50
|
+
if (block.type === 'text' && typeof block.text === 'string')
|
|
51
|
+
parts.push(block.text);
|
|
52
|
+
}
|
|
53
|
+
return parts.length === 0 ? undefined : parts.join('\n');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Detect the first matching signal in a user message. Keyword hits take
|
|
57
|
+
* priority over correction signals when both match.
|
|
58
|
+
* @param text - the message text to scan.
|
|
59
|
+
* @returns the matching signal name, or `undefined` when no rule fires.
|
|
60
|
+
*/
|
|
61
|
+
export function detectSignal(text) {
|
|
62
|
+
for (const { name, re } of KEYWORD_PATTERNS) {
|
|
63
|
+
if (re.test(text))
|
|
64
|
+
return name;
|
|
65
|
+
}
|
|
66
|
+
for (const { name, re } of CORRECTION_PATTERNS) {
|
|
67
|
+
if (re.test(text))
|
|
68
|
+
return name;
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The projection's `apply` transition: fold one committed event into the
|
|
74
|
+
* accumulator. Returns the SAME state reference for events that do not
|
|
75
|
+
* contribute a candidate (the registry's `Object.is` gate).
|
|
76
|
+
* @param state - the state covering all prior events.
|
|
77
|
+
* @param event - the next committed session event.
|
|
78
|
+
* @returns the next state (same reference when the event contributes nothing).
|
|
79
|
+
*/
|
|
80
|
+
export function applyAccumulator(state, event) {
|
|
81
|
+
if (event.type !== 'user/message')
|
|
82
|
+
return state;
|
|
83
|
+
const text = messageText(event);
|
|
84
|
+
if (text === undefined)
|
|
85
|
+
return state;
|
|
86
|
+
const signal = detectSignal(text);
|
|
87
|
+
if (signal === undefined)
|
|
88
|
+
return state;
|
|
89
|
+
const candidate = { text, signal, seq: event.seq };
|
|
90
|
+
return { candidates: [...state.candidates, candidate], count: state.count + 1 };
|
|
91
|
+
}
|
|
92
|
+
/** Zod schema for the accumulator's wire payload. */
|
|
93
|
+
export const accumulatorSchema = zod.object({
|
|
94
|
+
candidates: zod.array(zod.object({
|
|
95
|
+
text: zod.string(),
|
|
96
|
+
signal: zod.string(),
|
|
97
|
+
seq: zod.number(),
|
|
98
|
+
})),
|
|
99
|
+
count: zod.number(),
|
|
100
|
+
});
|
|
101
|
+
//# sourceMappingURL=accumulator.js.map
|
|
@@ -0,0 +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,+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M,EAAE;QACpB,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE;KAClB,CAAC,CAAC;IACH,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE;CACpB,CAAC,CAAA"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM extraction for the memory-review plugin: build review/flush prompts,
|
|
3
|
+
* run one {@link ctx.llm.stream} call routed on the session's request header,
|
|
4
|
+
* parse the line-oriented output into memory entries, and store each through
|
|
5
|
+
* {@link scanContent} + {@link MemoryStore.add}.
|
|
6
|
+
*
|
|
7
|
+
* Every extraction call is best-effort: a missing provider/model route, a
|
|
8
|
+
* stream error, or a scanner rejection skips the offending entry without
|
|
9
|
+
* throwing into the caller. The flush paths use fire-and-forget scheduling so
|
|
10
|
+
* a slow or failing extraction never blocks `compaction/end` or
|
|
11
|
+
* `session/disposed`.
|
|
12
|
+
*
|
|
13
|
+
* @module @chenhw7/dsh-memory/review/extract
|
|
14
|
+
*/
|
|
15
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
16
|
+
import type { GenerateOptions, Message } from '@deepseek-ai/dsh-llm';
|
|
17
|
+
import type { Session } from '@deepseek-ai/dsh-session';
|
|
18
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
19
|
+
import type { MemoryCategory, MemoryScope } from '../types.ts';
|
|
20
|
+
import type { MemoryStore } from '../index.ts';
|
|
21
|
+
import type { MemoryCandidate } from './accumulator.ts';
|
|
22
|
+
/** System prompt for the periodic-review extraction. */
|
|
23
|
+
export declare const REVIEW_SYSTEM_PROMPT = "You are a memory extraction assistant. Read the conversation fragments below and extract durable, reusable memories the assistant should remember across sessions. Output one memory per line in the exact format \"scope: content\" where scope is one of \"global\", \"project\", or \"user\". Prioritize user preferences, corrections, and recurring patterns over task-specific or one-off details. Omit anything already present in the current memory snapshot. Output only the memory lines, nothing else.";
|
|
24
|
+
/** System prompt for the compaction/dispose flush (方案 C). */
|
|
25
|
+
export declare const FLUSH_SYSTEM_PROMPT = "The session is being compressed. Save anything worth remembering \u2014 prioritize user preferences, corrections, and recurring patterns over task-specific details. Output one memory per line in the exact format \"scope: content\" where scope is one of \"global\", \"project\", or \"user\". Output only the memory lines, nothing else.";
|
|
26
|
+
/** One parsed memory entry awaiting scanner + store validation. */
|
|
27
|
+
export interface ParsedMemory {
|
|
28
|
+
/** Which scope the extracted memory belongs to. */
|
|
29
|
+
readonly scope: MemoryScope;
|
|
30
|
+
/** Human-readable memory content. */
|
|
31
|
+
readonly content: string;
|
|
32
|
+
/** Optional category inferred from the matched signal, when available. */
|
|
33
|
+
readonly category?: MemoryCategory;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse the line-oriented extraction output into memory entries. Each
|
|
37
|
+
* non-empty line must read "scope: content"; lines without a recognized scope
|
|
38
|
+
* tag or with empty content are dropped. The parsing is pure and synchronous.
|
|
39
|
+
* @param text - the raw model output.
|
|
40
|
+
* @returns the parsed entries, in output order.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseExtractedMemories(text: string): ParsedMemory[];
|
|
43
|
+
/**
|
|
44
|
+
* Render the current memory snapshot as a human-readable block for the
|
|
45
|
+
* extraction prompt. Returns an empty string when the store has no entries.
|
|
46
|
+
* @param memory - the live memory store, or `undefined` when none is mounted.
|
|
47
|
+
* @returns the rendered snapshot text.
|
|
48
|
+
*/
|
|
49
|
+
export declare function renderMemorySnapshot(memory: MemoryStore | undefined): string;
|
|
50
|
+
/**
|
|
51
|
+
* Build the messages for the periodic-review extraction prompt.
|
|
52
|
+
* @param memorySnapshot - rendered current memory text (possibly empty).
|
|
53
|
+
* @param candidates - the accumulated candidate fragments.
|
|
54
|
+
* @returns the model-facing user message list.
|
|
55
|
+
*/
|
|
56
|
+
export declare function buildReviewMessages(memorySnapshot: string, candidates: readonly MemoryCandidate[]): Message[];
|
|
57
|
+
/**
|
|
58
|
+
* Build the messages for the compaction/dispose flush prompt.
|
|
59
|
+
* @param fragments - the raw conversation fragments being shadowed.
|
|
60
|
+
* @returns the model-facing user message list.
|
|
61
|
+
*/
|
|
62
|
+
export declare function buildFlushMessages(fragments: readonly string[]): Message[];
|
|
63
|
+
/**
|
|
64
|
+
* Collect the assembled text from one LLM stream. Throws on an error/aborted
|
|
65
|
+
* finish; returns the concatenated text-block content on success.
|
|
66
|
+
* @param ctx - context carrying the LLM seam.
|
|
67
|
+
* @param options - the full request (provider selects the adapter).
|
|
68
|
+
* @returns the assembled output text.
|
|
69
|
+
*/
|
|
70
|
+
export declare function collectStreamText(ctx: Context, options: GenerateOptions): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Run one extraction call and parse its output. Resolves the provider/model
|
|
73
|
+
* from the session's request header; returns an empty list when no route is
|
|
74
|
+
* available or the stream fails (best-effort).
|
|
75
|
+
* @param ctx - context carrying the LLM seam.
|
|
76
|
+
* @param session - the session whose request header routes the call.
|
|
77
|
+
* @param system - the system prompt.
|
|
78
|
+
* @param messages - the model-facing user messages.
|
|
79
|
+
* @param signal - optional abort signal.
|
|
80
|
+
* @returns the parsed memory entries (possibly empty on failure).
|
|
81
|
+
*/
|
|
82
|
+
export declare function extractMemories(ctx: Context, session: Session, system: string, messages: Message[], signal?: AbortSignal): Promise<ParsedMemory[]>;
|
|
83
|
+
/**
|
|
84
|
+
* Store parsed memory entries through the scanner and the memory store. Each
|
|
85
|
+
* entry is independent: a scanner rejection or store failure skips that
|
|
86
|
+
* entry without throwing. Does nothing when no memory store is mounted.
|
|
87
|
+
* @param ctx - context carrying an optional `memory` service.
|
|
88
|
+
* @param parsed - the entries to store.
|
|
89
|
+
* @param category - optional category to attach to every entry.
|
|
90
|
+
*/
|
|
91
|
+
export declare function storeMemories(ctx: Context, parsed: readonly ParsedMemory[], attachCategory?: MemoryCategory): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Run the periodic-review extraction: build the review prompt from the
|
|
94
|
+
* current memory snapshot and the accumulated candidates, extract, and store.
|
|
95
|
+
* Returns the number of entries actually stored.
|
|
96
|
+
* @param ctx - context carrying the LLM and optional memory seams.
|
|
97
|
+
* @param agent - the live agent whose session routes the call.
|
|
98
|
+
* @param candidates - the accumulated candidate fragments.
|
|
99
|
+
*/
|
|
100
|
+
export declare function runReviewExtraction(ctx: Context, agent: Agent, candidates: readonly MemoryCandidate[]): Promise<number>;
|
|
101
|
+
/**
|
|
102
|
+
* Run the compaction/dispose flush extraction: build the flush prompt from the
|
|
103
|
+
* shadowed conversation fragments, extract, and store. Best-effort and
|
|
104
|
+
* fire-and-forget at the call site.
|
|
105
|
+
* @param ctx - context carrying the LLM and optional memory seams.
|
|
106
|
+
* @param session - the session whose request header routes the call.
|
|
107
|
+
* @param fragments - the raw conversation fragments being shadowed.
|
|
108
|
+
* @param signal - optional abort signal.
|
|
109
|
+
* @returns the number of parsed entries (before scanner/store filtering).
|
|
110
|
+
*/
|
|
111
|
+
export declare function runFlushExtraction(ctx: Context, session: Session, fragments: readonly string[], signal?: AbortSignal): Promise<number>;
|
|
112
|
+
//# sourceMappingURL=extract.d.ts.map
|
|
@@ -0,0 +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,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AAEnD,OAAO,KAAK,EAAkB,cAAc,EAAe,WAAW,EAAE,MAAM,aAAa,CAAA;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAKvD,wDAAwD;AACxD,eAAO,MAAM,o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;GAOG;AACH,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,OAAO,EACZ,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,SAAS,eAAe,EAAE,GACrC,OAAO,CAAC,MAAM,CAAC,CASjB;AAED;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CACtC,GAAG,EAAE,OAAO,EACZ,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,MAAM,CAAC,CAKjB"}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LLM extraction for the memory-review plugin: build review/flush prompts,
|
|
3
|
+
* run one {@link ctx.llm.stream} call routed on the session's request header,
|
|
4
|
+
* parse the line-oriented output into memory entries, and store each through
|
|
5
|
+
* {@link scanContent} + {@link MemoryStore.add}.
|
|
6
|
+
*
|
|
7
|
+
* Every extraction call is best-effort: a missing provider/model route, a
|
|
8
|
+
* stream error, or a scanner rejection skips the offending entry without
|
|
9
|
+
* throwing into the caller. The flush paths use fire-and-forget scheduling so
|
|
10
|
+
* a slow or failing extraction never blocks `compaction/end` or
|
|
11
|
+
* `session/disposed`.
|
|
12
|
+
*
|
|
13
|
+
* @module @chenhw7/dsh-memory/review/extract
|
|
14
|
+
*/
|
|
15
|
+
import { BlockAssembler, createUserMessage } from '@deepseek-ai/dsh-llm';
|
|
16
|
+
import { scanContent } from "../scanner.js";
|
|
17
|
+
/** Producer attribution for the synthetic extraction request message. */
|
|
18
|
+
const PLUGIN_SOURCE = { kind: 'plugin', plugin: 'dsh-memory-review' };
|
|
19
|
+
/** System prompt for the periodic-review extraction. */
|
|
20
|
+
export const REVIEW_SYSTEM_PROMPT = 'You are a memory extraction assistant. Read the conversation fragments below and extract durable, reusable memories the assistant should remember across sessions. Output one memory per line in the exact format "scope: content" where scope is one of "global", "project", or "user". Prioritize user preferences, corrections, and recurring patterns over task-specific or one-off details. Omit anything already present in the current memory snapshot. Output only the memory lines, nothing else.';
|
|
21
|
+
/** System prompt for the compaction/dispose flush (方案 C). */
|
|
22
|
+
export const FLUSH_SYSTEM_PROMPT = 'The session is being compressed. Save anything worth remembering — prioritize user preferences, corrections, and recurring patterns over task-specific details. Output one memory per line in the exact format "scope: content" where scope is one of "global", "project", or "user". Output only the memory lines, nothing else.';
|
|
23
|
+
/** The valid scope tags a parsed line may declare. */
|
|
24
|
+
const SCOPE_TAGS = ['global', 'project', 'user'];
|
|
25
|
+
/**
|
|
26
|
+
* Parse the line-oriented extraction output into memory entries. Each
|
|
27
|
+
* non-empty line must read "scope: content"; lines without a recognized scope
|
|
28
|
+
* tag or with empty content are dropped. The parsing is pure and synchronous.
|
|
29
|
+
* @param text - the raw model output.
|
|
30
|
+
* @returns the parsed entries, in output order.
|
|
31
|
+
*/
|
|
32
|
+
export function parseExtractedMemories(text) {
|
|
33
|
+
const results = [];
|
|
34
|
+
for (const rawLine of text.split('\n')) {
|
|
35
|
+
const line = rawLine.trim();
|
|
36
|
+
if (line.length === 0)
|
|
37
|
+
continue;
|
|
38
|
+
const colon = line.indexOf(':');
|
|
39
|
+
if (colon <= 0)
|
|
40
|
+
continue;
|
|
41
|
+
const scopeRaw = line.slice(0, colon).trim().toLowerCase();
|
|
42
|
+
const content = line.slice(colon + 1).trim();
|
|
43
|
+
if (content.length === 0)
|
|
44
|
+
continue;
|
|
45
|
+
let scope;
|
|
46
|
+
for (const tag of SCOPE_TAGS) {
|
|
47
|
+
if (tag === scopeRaw) {
|
|
48
|
+
scope = tag;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (scope === undefined)
|
|
53
|
+
continue;
|
|
54
|
+
results.push({ scope, content });
|
|
55
|
+
}
|
|
56
|
+
return results;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Render the current memory snapshot as a human-readable block for the
|
|
60
|
+
* extraction prompt. Returns an empty string when the store has no entries.
|
|
61
|
+
* @param memory - the live memory store, or `undefined` when none is mounted.
|
|
62
|
+
* @returns the rendered snapshot text.
|
|
63
|
+
*/
|
|
64
|
+
export function renderMemorySnapshot(memory) {
|
|
65
|
+
if (memory === undefined)
|
|
66
|
+
return '';
|
|
67
|
+
const entries = memory.list();
|
|
68
|
+
if (entries.length === 0)
|
|
69
|
+
return '';
|
|
70
|
+
const lines = entries.map(renderEntry);
|
|
71
|
+
return `Current memory snapshot:\n${lines.join('\n')}`;
|
|
72
|
+
}
|
|
73
|
+
/** Render one memory entry as a single prompt line. */
|
|
74
|
+
function renderEntry(entry) {
|
|
75
|
+
const tag = entry.category === undefined ? `[${entry.scope}]` : `[${entry.scope}/${entry.category}]`;
|
|
76
|
+
const project = entry.projectName === undefined ? '' : ` (${entry.projectName})`;
|
|
77
|
+
return `- ${tag}${project} ${entry.content}`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Build the messages for the periodic-review extraction prompt.
|
|
81
|
+
* @param memorySnapshot - rendered current memory text (possibly empty).
|
|
82
|
+
* @param candidates - the accumulated candidate fragments.
|
|
83
|
+
* @returns the model-facing user message list.
|
|
84
|
+
*/
|
|
85
|
+
export function buildReviewMessages(memorySnapshot, candidates) {
|
|
86
|
+
const fragments = candidates.map((c, i) => `[${i + 1}] (${c.signal}) ${c.text}`).join('\n');
|
|
87
|
+
const parts = [
|
|
88
|
+
memorySnapshot.length === 0 ? '' : `${memorySnapshot}\n\n`,
|
|
89
|
+
'Conversation fragments to extract memories from:\n',
|
|
90
|
+
fragments,
|
|
91
|
+
];
|
|
92
|
+
const text = parts.join('');
|
|
93
|
+
return [createUserMessage({ content: [{ type: 'text', text }], source: PLUGIN_SOURCE })];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Build the messages for the compaction/dispose flush prompt.
|
|
97
|
+
* @param fragments - the raw conversation fragments being shadowed.
|
|
98
|
+
* @returns the model-facing user message list.
|
|
99
|
+
*/
|
|
100
|
+
export function buildFlushMessages(fragments) {
|
|
101
|
+
const body = fragments.length === 0 ? '(no fragments available)' : fragments.map((f, i) => `[${i + 1}] ${f}`).join('\n');
|
|
102
|
+
const text = `Conversation being compressed:\n${body}`;
|
|
103
|
+
return [createUserMessage({ content: [{ type: 'text', text }], source: PLUGIN_SOURCE })];
|
|
104
|
+
}
|
|
105
|
+
/** Resolve the provider/model for an extraction call from the session header. */
|
|
106
|
+
function resolveTarget(session) {
|
|
107
|
+
const config = session.requestHeader()?.config;
|
|
108
|
+
if (config === undefined || config.provider.length === 0 || config.model.length === 0)
|
|
109
|
+
return undefined;
|
|
110
|
+
return { provider: config.provider, model: config.model };
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Collect the assembled text from one LLM stream. Throws on an error/aborted
|
|
114
|
+
* finish; returns the concatenated text-block content on success.
|
|
115
|
+
* @param ctx - context carrying the LLM seam.
|
|
116
|
+
* @param options - the full request (provider selects the adapter).
|
|
117
|
+
* @returns the assembled output text.
|
|
118
|
+
*/
|
|
119
|
+
export async function collectStreamText(ctx, options) {
|
|
120
|
+
const assembler = new BlockAssembler();
|
|
121
|
+
for await (const chunk of ctx.llm.stream(options))
|
|
122
|
+
assembler.push(chunk);
|
|
123
|
+
const error = finishError(assembler.finish);
|
|
124
|
+
if (error !== undefined)
|
|
125
|
+
throw error;
|
|
126
|
+
return textOf(assembler.blocks());
|
|
127
|
+
}
|
|
128
|
+
/** Extract the concatenated text from assembled content blocks. */
|
|
129
|
+
function textOf(blocks) {
|
|
130
|
+
const parts = [];
|
|
131
|
+
for (const block of blocks) {
|
|
132
|
+
if (block.type === 'text' && typeof block.text === 'string')
|
|
133
|
+
parts.push(block.text);
|
|
134
|
+
}
|
|
135
|
+
return parts.join('');
|
|
136
|
+
}
|
|
137
|
+
/** Map a terminal stream finish to its fail-closed error (mirrors compaction). */
|
|
138
|
+
function finishError(finish) {
|
|
139
|
+
switch (finish.kind) {
|
|
140
|
+
case 'error':
|
|
141
|
+
case 'aborted': {
|
|
142
|
+
const error = new Error(finish.failure.message);
|
|
143
|
+
error.code = finish.failure.code;
|
|
144
|
+
return error;
|
|
145
|
+
}
|
|
146
|
+
case 'max-tokens': {
|
|
147
|
+
const error = new Error('memory extraction truncated at the token cap');
|
|
148
|
+
error.code = 'MAX_TOKENS';
|
|
149
|
+
return error;
|
|
150
|
+
}
|
|
151
|
+
default:
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Run one extraction call and parse its output. Resolves the provider/model
|
|
157
|
+
* from the session's request header; returns an empty list when no route is
|
|
158
|
+
* available or the stream fails (best-effort).
|
|
159
|
+
* @param ctx - context carrying the LLM seam.
|
|
160
|
+
* @param session - the session whose request header routes the call.
|
|
161
|
+
* @param system - the system prompt.
|
|
162
|
+
* @param messages - the model-facing user messages.
|
|
163
|
+
* @param signal - optional abort signal.
|
|
164
|
+
* @returns the parsed memory entries (possibly empty on failure).
|
|
165
|
+
*/
|
|
166
|
+
export async function extractMemories(ctx, session, system, messages, signal) {
|
|
167
|
+
const target = resolveTarget(session);
|
|
168
|
+
if (target === undefined)
|
|
169
|
+
return [];
|
|
170
|
+
const options = {
|
|
171
|
+
provider: target.provider,
|
|
172
|
+
model: target.model,
|
|
173
|
+
messages,
|
|
174
|
+
system,
|
|
175
|
+
sessionId: session.id,
|
|
176
|
+
...signal === undefined ? {} : { signal },
|
|
177
|
+
};
|
|
178
|
+
let text;
|
|
179
|
+
try {
|
|
180
|
+
text = await collectStreamText(ctx, options);
|
|
181
|
+
}
|
|
182
|
+
catch (_extractionError) {
|
|
183
|
+
return [];
|
|
184
|
+
}
|
|
185
|
+
return parseExtractedMemories(text);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Store parsed memory entries through the scanner and the memory store. Each
|
|
189
|
+
* entry is independent: a scanner rejection or store failure skips that
|
|
190
|
+
* entry without throwing. Does nothing when no memory store is mounted.
|
|
191
|
+
* @param ctx - context carrying an optional `memory` service.
|
|
192
|
+
* @param parsed - the entries to store.
|
|
193
|
+
* @param category - optional category to attach to every entry.
|
|
194
|
+
*/
|
|
195
|
+
export async function storeMemories(ctx, parsed, attachCategory) {
|
|
196
|
+
const memory = ctx.get('memory');
|
|
197
|
+
if (memory === undefined)
|
|
198
|
+
return;
|
|
199
|
+
for (const entry of parsed) {
|
|
200
|
+
const scan = scanContent(entry.content);
|
|
201
|
+
if (!scan.allowed)
|
|
202
|
+
continue;
|
|
203
|
+
const category = entry.category ?? attachCategory;
|
|
204
|
+
const input = category === undefined
|
|
205
|
+
? { scope: entry.scope, content: entry.content }
|
|
206
|
+
: { scope: entry.scope, content: entry.content, category };
|
|
207
|
+
try {
|
|
208
|
+
await memory.add(input);
|
|
209
|
+
}
|
|
210
|
+
catch (_storeError) {
|
|
211
|
+
// Best-effort: a store failure for one entry does not abort the rest.
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Run the periodic-review extraction: build the review prompt from the
|
|
217
|
+
* current memory snapshot and the accumulated candidates, extract, and store.
|
|
218
|
+
* Returns the number of entries actually stored.
|
|
219
|
+
* @param ctx - context carrying the LLM and optional memory seams.
|
|
220
|
+
* @param agent - the live agent whose session routes the call.
|
|
221
|
+
* @param candidates - the accumulated candidate fragments.
|
|
222
|
+
*/
|
|
223
|
+
export async function runReviewExtraction(ctx, agent, candidates) {
|
|
224
|
+
const memory = ctx.get('memory');
|
|
225
|
+
const snapshot = renderMemorySnapshot(memory);
|
|
226
|
+
const messages = buildReviewMessages(snapshot, candidates);
|
|
227
|
+
const parsed = await extractMemories(ctx, agent.session, REVIEW_SYSTEM_PROMPT, messages);
|
|
228
|
+
// A correction signal maps naturally to the `correction` category.
|
|
229
|
+
const correctionOnly = candidates.length > 0 && candidates.every(c => c.signal === 'correction');
|
|
230
|
+
await storeMemories(ctx, parsed, correctionOnly ? 'correction' : undefined);
|
|
231
|
+
return parsed.length;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Run the compaction/dispose flush extraction: build the flush prompt from the
|
|
235
|
+
* shadowed conversation fragments, extract, and store. Best-effort and
|
|
236
|
+
* fire-and-forget at the call site.
|
|
237
|
+
* @param ctx - context carrying the LLM and optional memory seams.
|
|
238
|
+
* @param session - the session whose request header routes the call.
|
|
239
|
+
* @param fragments - the raw conversation fragments being shadowed.
|
|
240
|
+
* @param signal - optional abort signal.
|
|
241
|
+
* @returns the number of parsed entries (before scanner/store filtering).
|
|
242
|
+
*/
|
|
243
|
+
export async function runFlushExtraction(ctx, session, fragments, signal) {
|
|
244
|
+
const messages = buildFlushMessages(fragments);
|
|
245
|
+
const parsed = await extractMemories(ctx, session, FLUSH_SYSTEM_PROMPT, messages, signal);
|
|
246
|
+
await storeMemories(ctx, parsed);
|
|
247
|
+
return parsed.length;
|
|
248
|
+
}
|
|
249
|
+
//# sourceMappingURL=extract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extract.js","sourceRoot":"","sources":["../../src/review/extract.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAKxE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAK3C,yEAAyE;AACzE,MAAM,aAAa,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,mBAAmB,EAAW,CAAA;AAE9E,wDAAwD;AACxD,MAAM,CAAC,MAAM,oBAAoB,GAC/B,4eAA4e,CAAA;AAE9e,6DAA6D;AAC7D,MAAM,CAAC,MAAM,mBAAmB,GAC9B,mUAAmU,CAAA;AAErU,sDAAsD;AACtD,MAAM,UAAU,GAA2B,CAAC,QAAQ,EAAE,SAAS,EAAE,MAAM,CAAC,CAAA;AAYxE;;;;;;GAMG;AACH,MAAM,UAAU,sBAAsB,CAAC,IAAY;IACjD,MAAM,OAAO,GAAmB,EAAE,CAAA;IAClC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0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@chenhw7/dsh-memory/review`: the automatic memory sediment layer. A
|
|
3
|
+
* function plugin that registers a `session-projection` accumulator over
|
|
4
|
+
* `user/message` / `assistant/message` events and triggers one LLM extraction
|
|
5
|
+
* call when the accumulator crosses its threshold, plus a fire-and-forget
|
|
6
|
+
* flush on `compaction/end` and `session/disposed` (方案 C).
|
|
7
|
+
*
|
|
8
|
+
* Two mechanisms, one store:
|
|
9
|
+
* 1. **Periodic review** — a pure synchronous projection collects candidate
|
|
10
|
+
* fragments (keyword/correction signals); on `agent/pre-step`, when the
|
|
11
|
+
* unprocessed candidates reach the threshold, one `ctx.llm.stream`
|
|
12
|
+
* extraction runs and the high-water mark advances (the logical "clear").
|
|
13
|
+
* 2. **Flush** — on `compaction/end` the shadowed raw events are read from the
|
|
14
|
+
* matching `compaction/summary` and extracted; on `session/disposed` the
|
|
15
|
+
* recent derived messages are extracted. Both are fire-and-forget and never
|
|
16
|
+
* block their event.
|
|
17
|
+
*
|
|
18
|
+
* @module @chenhw7/dsh-memory/review
|
|
19
|
+
*/
|
|
20
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
21
|
+
import z from '@deepseek-ai/schemastery';
|
|
22
|
+
/** Cordis function-plugin name. */
|
|
23
|
+
export declare const name = "memory-review";
|
|
24
|
+
/** Services required before the plugin applies. The LLM seam is mandatory;
|
|
25
|
+
* `sessionProjections` and `memory` are optional (read via `ctx.get`). */
|
|
26
|
+
export declare const inject: string[];
|
|
27
|
+
/** Plugin configuration. */
|
|
28
|
+
export interface Config {
|
|
29
|
+
/** Enable the periodic-review extraction. Defaults to `true`. */
|
|
30
|
+
reviewEnabled?: boolean;
|
|
31
|
+
/** Number of unprocessed candidates that trigger one extraction. Defaults to `10`. */
|
|
32
|
+
reviewCandidateThreshold?: number;
|
|
33
|
+
/** Enable the compaction-end flush. Defaults to `true`. */
|
|
34
|
+
flushOnCompaction?: boolean;
|
|
35
|
+
/** Enable the session-disposed flush. Defaults to `true`. */
|
|
36
|
+
flushOnDispose?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export declare const Config: z<Config>;
|
|
39
|
+
/**
|
|
40
|
+
* Install the memory-review function plugin.
|
|
41
|
+
* @param ctx - Cordis context carrying the LLM seam.
|
|
42
|
+
* @param config - plugin configuration (defaults applied by schemastery).
|
|
43
|
+
*/
|
|
44
|
+
export declare function apply(ctx: Context, config?: Config): void;
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/review/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,CAAC,MAAM,0BAA0B,CAAA;AAmBxC,mCAAmC;AACnC,eAAO,MAAM,IAAI,kBAAkB,CAAA;AAEnC;2EAC2E;AAC3E,eAAO,MAAM,MAAM,UAAU,CAAA;AAE7B,4BAA4B;AAC5B,MAAM,WAAW,MAAM;IACrB,iEAAiE;IACjE,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,sFAAsF;IACtF,wBAAwB,CAAC,EAAE,MAAM,CAAA;IACjC,2DAA2D;IAC3D,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,6DAA6D;IAC7D,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB;AAED,eAAO,MAAM,MAAM,EAAE,CAAC,CAAC,MAAM,CAK3B,CAAA;AAkFF;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,GAAE,MAAW,GAAG,IAAI,CAmD7D"}
|