@yeaft/webchat-agent 1.0.202 → 1.0.203
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/local-runtime/version.json +1 -1
- package/package.json +1 -1
- package/yeaft/engine.js +27 -5
- package/yeaft/memory/ams.js +12 -5
- package/yeaft/memory/prompt-cleanup.js +142 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":"1.0.
|
|
1
|
+
{"version":"1.0.203"}
|
package/package.json
CHANGED
package/yeaft/engine.js
CHANGED
|
@@ -32,7 +32,7 @@ import { archiveTurn } from './archive/turn-archive.js';
|
|
|
32
32
|
import { archiveToolResults } from './archive/tool-results.js';
|
|
33
33
|
import { readSummary as readScopeSummary } from './memory/store.js';
|
|
34
34
|
import { runAdjust } from './memory/adjust.js';
|
|
35
|
-
import { cleanMemoryPromptText } from './memory/prompt-cleanup.js';
|
|
35
|
+
import { cleanMemoryPromptText, isMemoryPromptRelevant } from './memory/prompt-cleanup.js';
|
|
36
36
|
import { isVpSeedBackfillStub } from './memory/seed-backfill.js';
|
|
37
37
|
import { runStopHooks } from './stop-hooks.js';
|
|
38
38
|
import { perfNowMs, recordAgentPerfTrace } from './perf-trace.js';
|
|
@@ -388,6 +388,20 @@ export function shouldAllowGroupReflection({
|
|
|
388
388
|
* }} args
|
|
389
389
|
* @returns {Array<{scope: string, summary: string}>}
|
|
390
390
|
*/
|
|
391
|
+
export function selectResidentTopicScopes(topicScopes, recallEntries, userMsg = '') {
|
|
392
|
+
const recalledTopicScopes = new Set((recallEntries || [])
|
|
393
|
+
.map(entry => entry?.scope)
|
|
394
|
+
.filter(scope => typeof scope === 'string' && /^sessions\/[^/]+\/topic\//.test(scope)));
|
|
395
|
+
return (Array.isArray(topicScopes) ? topicScopes : [])
|
|
396
|
+
.filter(scope => recalledTopicScopes.has(scope) || isTopicScopeRelevant(scope, userMsg));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function isTopicScopeRelevant(scope, userMsg) {
|
|
400
|
+
if (!userMsg || typeof scope !== 'string') return false;
|
|
401
|
+
const label = scope.replace(/^sessions\/[^/]+\/topic\//, '').replace(/[/-]+/g, ' ');
|
|
402
|
+
return isMemoryPromptRelevant(label, userMsg);
|
|
403
|
+
}
|
|
404
|
+
|
|
391
405
|
export function buildResidentEntries(args) {
|
|
392
406
|
const summaries = (args && args.summaries) || {};
|
|
393
407
|
const out = [];
|
|
@@ -873,6 +887,7 @@ export class Engine {
|
|
|
873
887
|
* ownVpId?: string|null,
|
|
874
888
|
* summaries: { user?: string, session?: string, vp?: string },
|
|
875
889
|
* recallEntries: object[],
|
|
890
|
+
* userMsg?: string,
|
|
876
891
|
* }} args
|
|
877
892
|
* @returns {{
|
|
878
893
|
* ams: import('./memory/ams.js').ActiveMemorySet,
|
|
@@ -912,7 +927,7 @@ export class Engine {
|
|
|
912
927
|
ams.setOnDemand(segs);
|
|
913
928
|
|
|
914
929
|
// (c) Snapshot — render the AMS layers as a single prompt block.
|
|
915
|
-
const snapshotBlock = this.#renderAmsSnapshot(ams, this.#config.language || 'en');
|
|
930
|
+
const snapshotBlock = this.#renderAmsSnapshot(ams, this.#config.language || 'en', args.userMsg || '');
|
|
916
931
|
|
|
917
932
|
const scopes = buildRelevantScopes({
|
|
918
933
|
sessionId: args.sessionId,
|
|
@@ -929,10 +944,11 @@ export class Engine {
|
|
|
929
944
|
*
|
|
930
945
|
* @param {import('./memory/ams.js').ActiveMemorySet} ams
|
|
931
946
|
* @param {string} [language]
|
|
947
|
+
* @param {string} [userMsg]
|
|
932
948
|
* @returns {string}
|
|
933
949
|
*/
|
|
934
|
-
#renderAmsSnapshot(ams, language = 'en') {
|
|
935
|
-
const snap = ams.snapshot();
|
|
950
|
+
#renderAmsSnapshot(ams, language = 'en', userMsg = '') {
|
|
951
|
+
const snap = ams.snapshot({ userMsg });
|
|
936
952
|
if (!snap) return '';
|
|
937
953
|
const parts = [];
|
|
938
954
|
if (snap.resident.length === 0 && snap.recent.length === 0 && snap.onDemand.length === 0) {
|
|
@@ -1906,6 +1922,11 @@ export class Engine {
|
|
|
1906
1922
|
if (recallEntryCount > 0) {
|
|
1907
1923
|
yield { type: 'recall', entryCount: recallEntryCount, cached: false, threadId };
|
|
1908
1924
|
}
|
|
1925
|
+
const topicScopesForResident = selectResidentTopicScopes(
|
|
1926
|
+
topicScopesForMemory,
|
|
1927
|
+
recallResult?.entries || [],
|
|
1928
|
+
prompt,
|
|
1929
|
+
);
|
|
1909
1930
|
|
|
1910
1931
|
// Layer-A summaries — same scopes AMS Resident will surface, loaded
|
|
1911
1932
|
// here so we can pass them into #prepareAms. (Rolling per-scope
|
|
@@ -1916,7 +1937,7 @@ export class Engine {
|
|
|
1916
1937
|
? vpPersona.vpId
|
|
1917
1938
|
: (typeof senderVpId === 'string' ? senderVpId : undefined),
|
|
1918
1939
|
language: this.#config.language || 'en',
|
|
1919
|
-
topicScopes:
|
|
1940
|
+
topicScopes: topicScopesForResident,
|
|
1920
1941
|
});
|
|
1921
1942
|
|
|
1922
1943
|
// ─── AMS: populate + snapshot ───────────────────────────────
|
|
@@ -1935,6 +1956,7 @@ export class Engine {
|
|
|
1935
1956
|
ownVpId: ownVpIdForAms,
|
|
1936
1957
|
summaries,
|
|
1937
1958
|
recallEntries: recallResult ? (recallResult.entries || []) : [],
|
|
1959
|
+
userMsg: prompt,
|
|
1938
1960
|
});
|
|
1939
1961
|
if (amsContext && amsContext.snapshotBlock) {
|
|
1940
1962
|
memoryInjection = amsContext.snapshotBlock;
|
package/yeaft/memory/ams.js
CHANGED
|
@@ -17,7 +17,12 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { approxTokens } from './budget.js';
|
|
20
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
cleanMemoryPromptText,
|
|
22
|
+
filterMemoryPromptTextForPrompt,
|
|
23
|
+
isDuplicateMemoryText,
|
|
24
|
+
rememberMemoryText,
|
|
25
|
+
} from './prompt-cleanup.js';
|
|
21
26
|
import { isVpForeign } from './store.js';
|
|
22
27
|
|
|
23
28
|
const RECENT_DEFAULT_CAPACITY = 64;
|
|
@@ -144,16 +149,18 @@ export class ActiveMemorySet {
|
|
|
144
149
|
* system prompt. Each layer is greedily packed within its budget;
|
|
145
150
|
* overflow is dropped from this turn but not from disk.
|
|
146
151
|
*
|
|
152
|
+
* @param {{ userMsg?: string }} [opts]
|
|
147
153
|
* @returns {AmsSnapshot}
|
|
148
154
|
*/
|
|
149
|
-
snapshot() {
|
|
155
|
+
snapshot(opts = {}) {
|
|
156
|
+
const userMsg = typeof opts.userMsg === 'string' ? opts.userMsg : '';
|
|
150
157
|
const seenPromptText = new Set();
|
|
151
158
|
|
|
152
159
|
// Resident: pack scopes by priority order (caller provides via insert
|
|
153
160
|
// order — current group's own vp first, then user, etc.).
|
|
154
161
|
const { picked: resPicked, cost: resCost } = pickMemoryItems({
|
|
155
162
|
items: [...this._resident.entries()].map(([scope, summary]) => ({
|
|
156
|
-
scope, summary:
|
|
163
|
+
scope, summary: filterMemoryPromptTextForPrompt(summary, userMsg),
|
|
157
164
|
})),
|
|
158
165
|
budget: this.budget.resident,
|
|
159
166
|
seen: seenPromptText,
|
|
@@ -165,7 +172,7 @@ export class ActiveMemorySet {
|
|
|
165
172
|
const { picked: recPicked, cost: recCost } = pickMemoryItems({
|
|
166
173
|
items: [...this._recent.values()]
|
|
167
174
|
.reverse()
|
|
168
|
-
.map(e => ({ ...e.seg, body:
|
|
175
|
+
.map(e => ({ ...e.seg, body: filterMemoryPromptTextForPrompt(e.seg?.body, userMsg) })),
|
|
169
176
|
budget: this.budget.recent,
|
|
170
177
|
seen: seenPromptText,
|
|
171
178
|
textOf: seg => seg.body,
|
|
@@ -175,7 +182,7 @@ export class ActiveMemorySet {
|
|
|
175
182
|
// OnDemand: insertion order from caller (already FTS-ranked).
|
|
176
183
|
const { picked: odPicked, cost: odCost } = pickMemoryItems({
|
|
177
184
|
items: [...this._onDemand.values()]
|
|
178
|
-
.map(seg => ({ ...seg, body:
|
|
185
|
+
.map(seg => ({ ...seg, body: filterMemoryPromptTextForPrompt(seg?.body, userMsg) })),
|
|
179
186
|
budget: this.budget.onDemand,
|
|
180
187
|
seen: seenPromptText,
|
|
181
188
|
textOf: seg => seg.body,
|
|
@@ -8,6 +8,20 @@
|
|
|
8
8
|
|
|
9
9
|
const DREAM_STATE_BLOCK_RE = /<!--\s*dream-state\s*-->[\s\S]*?<!--\s*\/dream-state\s*-->/gi;
|
|
10
10
|
|
|
11
|
+
const TRANSIENT_MEMORY_RE = /\b(work\s*item|current\s+(?:state|work|task)|in[_ -]?progress|todo|next\s+step|blocker|blocked|pr\s*#?\d+|pull\s+request|review|merge\s+commit|release\s+tag|tag\s+v\d|v\d+\.\d+\.\d+)\b|(?:工作项|当前(?:状态|任务|工作)|正在|待办|下一步|阻塞|评审|合并|发布|标签|已推|已合并|已完成)/i;
|
|
12
|
+
const ASCII_WORD_RE = /[a-z0-9_]{3,}/gi;
|
|
13
|
+
const CJK_RUN_RE = /[\u4e00-\u9fff]{2,}/g;
|
|
14
|
+
const MARKDOWN_LIST_ITEM_RE = /^\s*(?:[-*+]\s+|\d+[.)]\s+)/;
|
|
15
|
+
const COMMON_INTENT_TOKENS = new Set([
|
|
16
|
+
'current', 'state', 'work', 'task', 'item', 'items', 'todo', 'todos',
|
|
17
|
+
'next', 'step', 'steps', 'done', 'completed', 'finish', 'finished',
|
|
18
|
+
'merge', 'merged', 'review', 'reviewed', 'release', 'tag', 'tags',
|
|
19
|
+
'pr', 'pull', 'request', 'issue', 'fix', 'feat', 'test', 'tests',
|
|
20
|
+
'dream', 'memory', 'session', 'topic', 'status', 'blocker', 'blocked',
|
|
21
|
+
'当前', '状态', '任务', '工作', '工作项', '待办', '下一步', '完成', '已完成',
|
|
22
|
+
'合并', '评审', '发布', '标签', '记忆', '主题', '阻塞', '正在',
|
|
23
|
+
]);
|
|
24
|
+
|
|
11
25
|
/**
|
|
12
26
|
* Remove Dream scheduler metadata blocks from memory text.
|
|
13
27
|
*
|
|
@@ -31,6 +45,134 @@ export function cleanMemoryPromptText(text) {
|
|
|
31
45
|
.trim();
|
|
32
46
|
}
|
|
33
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Whether a memory item describes short-lived execution state rather than a
|
|
50
|
+
* stable preference / project fact. Transient memory is still valid on disk, but
|
|
51
|
+
* should only enter the prompt when it is related to the current user turn.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} text
|
|
54
|
+
* @returns {boolean}
|
|
55
|
+
*/
|
|
56
|
+
export function isTransientMemoryText(text) {
|
|
57
|
+
return TRANSIENT_MEMORY_RE.test(cleanMemoryPromptText(text));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Conservative prompt relevance gate for transient Dream memories. Stable
|
|
62
|
+
* memories are handled by the caller; this function only answers whether a
|
|
63
|
+
* known-transient item has lexical overlap with the current user request.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} memoryText
|
|
66
|
+
* @param {string} userText
|
|
67
|
+
* @returns {boolean}
|
|
68
|
+
*/
|
|
69
|
+
export function isTransientMemoryRelevant(memoryText, userText) {
|
|
70
|
+
return isMemoryPromptRelevant(memoryText, userText);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Conservative lexical relevance check used only at prompt assembly time. It is
|
|
75
|
+
* deliberately not semantic clustering: if there is no concrete token overlap,
|
|
76
|
+
* the memory should win again through FTS when the user asks for it explicitly.
|
|
77
|
+
*
|
|
78
|
+
* @param {string} memoryText
|
|
79
|
+
* @param {string} userText
|
|
80
|
+
* @returns {boolean}
|
|
81
|
+
*/
|
|
82
|
+
export function isMemoryPromptRelevant(memoryText, userText) {
|
|
83
|
+
const memoryTokens = promptRelevanceTokens(memoryText);
|
|
84
|
+
const userTokens = promptRelevanceTokens(userText);
|
|
85
|
+
if (memoryTokens.size === 0 || userTokens.size === 0) return false;
|
|
86
|
+
for (const token of memoryTokens) {
|
|
87
|
+
if (userTokens.has(token)) return true;
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Drop irrelevant transient paragraphs while preserving stable memory text. This
|
|
94
|
+
* keeps disk memory intact; it only trims what enters the current prompt.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} text
|
|
97
|
+
* @param {string} userText
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
100
|
+
export function filterMemoryPromptTextForPrompt(text, userText) {
|
|
101
|
+
const cleaned = cleanMemoryPromptText(text);
|
|
102
|
+
if (!cleaned || !userText) return cleaned;
|
|
103
|
+
const kept = [];
|
|
104
|
+
for (const chunk of splitMemoryPromptChunks(cleaned)) {
|
|
105
|
+
if (!isTransientMemoryText(chunk) || isTransientMemoryRelevant(chunk, userText)) {
|
|
106
|
+
kept.push(chunk);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return joinMemoryPromptChunks(kept).trim();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function splitMemoryPromptChunks(text) {
|
|
113
|
+
const chunks = [];
|
|
114
|
+
for (const block of String(text || '').split(/\n{2,}/)) {
|
|
115
|
+
const trimmed = block.trim();
|
|
116
|
+
if (!trimmed) continue;
|
|
117
|
+
chunks.push(...splitListBlock(trimmed));
|
|
118
|
+
}
|
|
119
|
+
return chunks;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function splitListBlock(block) {
|
|
123
|
+
const lines = block.split('\n');
|
|
124
|
+
if (!lines.some(line => MARKDOWN_LIST_ITEM_RE.test(line))) return [block];
|
|
125
|
+
|
|
126
|
+
const chunks = [];
|
|
127
|
+
let current = [];
|
|
128
|
+
for (const line of lines) {
|
|
129
|
+
if (MARKDOWN_LIST_ITEM_RE.test(line) && current.length > 0) {
|
|
130
|
+
chunks.push(current.join('\n').trim());
|
|
131
|
+
current = [];
|
|
132
|
+
}
|
|
133
|
+
current.push(line);
|
|
134
|
+
}
|
|
135
|
+
if (current.length > 0) chunks.push(current.join('\n').trim());
|
|
136
|
+
return chunks.filter(Boolean);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function joinMemoryPromptChunks(chunks) {
|
|
140
|
+
const out = [];
|
|
141
|
+
let previousWasList = false;
|
|
142
|
+
for (const chunk of chunks) {
|
|
143
|
+
const isList = MARKDOWN_LIST_ITEM_RE.test(chunk);
|
|
144
|
+
if (out.length > 0 && !(previousWasList && isList)) out.push('');
|
|
145
|
+
out.push(chunk);
|
|
146
|
+
previousWasList = isList;
|
|
147
|
+
}
|
|
148
|
+
return out.join('\n');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @param {string} text
|
|
153
|
+
* @returns {Set<string>}
|
|
154
|
+
*/
|
|
155
|
+
export function promptRelevanceTokens(text) {
|
|
156
|
+
const cleaned = cleanMemoryPromptText(text).toLowerCase();
|
|
157
|
+
const out = new Set();
|
|
158
|
+
for (const match of cleaned.matchAll(ASCII_WORD_RE)) {
|
|
159
|
+
const token = match[0];
|
|
160
|
+
if (!COMMON_INTENT_TOKENS.has(token)) out.add(token);
|
|
161
|
+
}
|
|
162
|
+
for (const match of cleaned.matchAll(CJK_RUN_RE)) {
|
|
163
|
+
for (const token of cjkBigrams(match[0])) {
|
|
164
|
+
if (!COMMON_INTENT_TOKENS.has(token)) out.add(token);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function cjkBigrams(text) {
|
|
171
|
+
const out = [];
|
|
172
|
+
for (let i = 0; i < text.length - 1; i += 1) out.push(text.slice(i, i + 2));
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
|
|
34
176
|
/**
|
|
35
177
|
* Normalized key for conservative prompt dedupe. This is intentionally simple:
|
|
36
178
|
* exact semantic clustering belongs in Dream; prompt assembly only removes
|