-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderive.js
More file actions
241 lines (216 loc) · 5.87 KB
/
Copy pathderive.js
File metadata and controls
241 lines (216 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { nowIso, sha256, stableStringify } from "./utils.js";
const KEYWORD_TAGS = [
"followup",
"complaint",
"payment",
"order",
"support",
"agent",
"customer",
"preference",
"approval",
"跟进",
"回访",
"月供",
"预算",
"试驾",
"置换",
"七座",
"金融方案",
"销售"
];
export function buildDerivedMemories(session, entityIds, schemaVersion) {
const timestamp = nowIso();
const summary = buildSummary(session);
const tags = buildTags(session);
const facts = buildFacts(session);
const derived = [];
for (const entityId of entityIds) {
derived.push({
derived_id: `drv_${sha256(`${session.session_id}:${entityId}:summary`).slice(0, 18)}`,
session_id: session.session_id,
entity_id: entityId,
derived_type: "summary",
content: {
text: summary,
layer: "derived"
},
version: 1,
model_info: {
provider: "heuristic",
name: "summary-v1"
},
confidence: 0.72,
created_at: timestamp,
schema_version: schemaVersion
});
for (const tag of tags) {
derived.push({
derived_id: `drv_${sha256(`${session.session_id}:${entityId}:tag:${tag}`).slice(0, 18)}`,
session_id: session.session_id,
entity_id: entityId,
derived_type: "tag",
content: {
tag,
layer: "derived"
},
version: 1,
model_info: {
provider: "heuristic",
name: "tag-v1"
},
confidence: 0.7,
created_at: timestamp,
schema_version: schemaVersion
});
}
for (const fact of facts) {
derived.push({
derived_id: `drv_${sha256(`${session.session_id}:${entityId}:fact:${stableStringify(fact)}`).slice(0, 18)}`,
session_id: session.session_id,
entity_id: entityId,
derived_type: "fact",
content: {
...fact,
layer: "derived"
},
version: 1,
model_info: {
provider: "heuristic",
name: "fact-v1"
},
confidence: 0.62,
created_at: timestamp,
schema_version: schemaVersion
});
}
}
return derived;
}
export function normalizeRawContent(rawContent) {
if (typeof rawContent === "string") {
return rawContent.trim();
}
return stableStringify(rawContent);
}
export function buildLongTermDerivedRecords({
sessionId,
entityIds,
memories,
schemaVersion,
provider = "heuristic",
model = "memory-extractor-v1"
}) {
const timestamp = nowIso();
const records = [];
for (const entityId of entityIds) {
for (const memory of memories) {
records.push({
derived_id: `drv_${sha256(`${sessionId}:${entityId}:long:${stableStringify(memory)}`).slice(0, 18)}`,
session_id: sessionId,
entity_id: entityId,
derived_type: "long_term_memory",
content: {
title: memory.title,
memory: memory.content,
category: memory.category,
importance: memory.importance,
layer: "long_term"
},
version: 1,
model_info: {
provider,
name: model
},
confidence: memory.confidence ?? 0.7,
created_at: timestamp,
schema_version: schemaVersion
});
}
}
return records;
}
function buildSummary(session) {
const normalized = normalizeRawContent(session.raw_content);
const compact = normalized.replace(/\s+/g, " ").trim();
if (compact.length <= 220) {
return compact;
}
return `${compact.slice(0, 217)}...`;
}
function buildTags(session) {
const text = normalizeRawContent(session.raw_content).toLowerCase();
const normalizedText = text.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, " ");
const compactText = text.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, "");
const tags = new Set([
session.session_type,
session.source_system,
session.raw_format,
session.language
].filter(Boolean));
for (const keyword of KEYWORD_TAGS) {
if (text.includes(keyword.toLowerCase()) || normalizedText.includes(keyword.toLowerCase()) || compactText.includes(keyword.toLowerCase())) {
tags.add(keyword);
}
}
if (session.importance_hint) {
tags.add(`importance:${session.importance_hint}`);
}
return [...tags];
}
function buildFacts(session) {
const facts = [];
const raw = session.raw_content;
if (raw && typeof raw === "object" && !Array.isArray(raw)) {
for (const key of ["preference", "preferences", "fact", "facts", "预算", "偏好", "跟进", "风险"]) {
if (raw[key]) {
facts.push({
key: mapFactKey(key),
value: raw[key]
});
}
}
}
const text = normalizeRawContent(raw);
collectRegexFacts(text, facts);
return dedupeFacts(facts);
}
function collectRegexFacts(text, facts) {
const patterns = [
{ key: "preference", regex: /(?:prefers?|likes?)\s+([^.。]+?)(?:[.。]|$)/i },
{ key: "follow_up", regex: /follow[- ]?up\s+(?:on\s+)?([^.。]+?)(?:[.。]|$)/i },
{ key: "preference", regex: /(?:偏好|更喜欢|喜欢|想要|更在意)([^。;\n]+)/ },
{ key: "budget", regex: /(?:预算|月供|总价)([^。;\n]+)/ },
{ key: "follow_up", regex: /(?:跟进|回访|联系)([^。;\n]+)/ },
{ key: "constraint", regex: /(?:担心|顾虑|卡点)([^。;\n]+)/ }
];
for (const pattern of patterns) {
const match = text.match(pattern.regex);
if (match?.[1]?.trim()) {
facts.push({
key: pattern.key,
value: match[1].trim()
});
}
}
}
function mapFactKey(key) {
const mapping = {
偏好: "preference",
预算: "budget",
跟进: "follow_up",
风险: "risk"
};
return mapping[key] || key;
}
function dedupeFacts(facts) {
const seen = new Set();
return facts.filter((fact) => {
const key = stableStringify(fact);
if (seen.has(key)) {
return false;
}
seen.add(key);
return true;
});
}