mocode-ai 1.7.11 → 1.8.1
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/dist/agent/index.js +50 -14
- package/dist/agent/run-coordinator.js +10 -4
- package/dist/agent/stages/tool-dispatcher.js +1 -0
- package/dist/agent/tool-helpers.js +37 -9
- package/dist/config/file.js +11 -3
- package/dist/config/index.js +13 -3
- package/dist/config/presets.js +9 -0
- package/dist/headless.js +10 -1
- package/dist/i18n/index.js +6 -0
- package/dist/llm/index.js +16 -3
- package/dist/llm/providers/anthropic.js +9 -1
- package/dist/llm/providers/vertex.js +7 -1
- package/dist/memory/graph.js +181 -11
- package/dist/memory/index.js +3 -3
- package/dist/memory/persona.js +94 -3
- package/dist/memory/reflect.js +229 -20
- package/dist/memory/relation-vocab.js +19 -0
- package/dist/memory/store.js +19 -0
- package/dist/models/map-preset.js +4 -0
- package/dist/repl/commands/memory.js +90 -5
- package/dist/repl/commands/model-actions.js +3 -0
- package/dist/repl/commands/model.js +39 -3
- package/dist/repl/commands.js +3 -0
- package/dist/repl/runtime.js +11 -1
- package/dist/tools/builtins/edit-file.js +110 -22
- package/dist/tools/builtins/memory-forget.js +26 -5
- package/dist/tools/builtins/memory-graph.js +7 -0
- package/dist/tools/builtins/write-file.js +4 -2
- package/dist/tools/tool-runtime.js +65 -2
- package/dist/ui/diff.js +68 -4
- package/package.json +1 -1
package/dist/memory/graph.js
CHANGED
|
@@ -14,6 +14,7 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from '
|
|
|
14
14
|
import os from 'node:os';
|
|
15
15
|
import path from 'node:path';
|
|
16
16
|
import { MAX_GRAPH_EDGES, MAX_GRAPH_ENTITIES } from '../tools/constants.js';
|
|
17
|
+
import { isSingleValueProfile, isKnownRelation } from './relation-vocab.js';
|
|
17
18
|
// ── 路径 / 原子写(同 store.ts 风格)──────────────────────────────────────
|
|
18
19
|
function globalGraphPath() {
|
|
19
20
|
return path.join(os.homedir(), '.mocode', 'memory-graph.json');
|
|
@@ -182,9 +183,11 @@ export function upsertEntity(name, opts = {}) {
|
|
|
182
183
|
}
|
|
183
184
|
/**
|
|
184
185
|
* 写一条三元组。src/dst 自动 upsert 为实体。
|
|
185
|
-
*
|
|
186
|
-
* -
|
|
187
|
-
*
|
|
186
|
+
* 冲突策略:
|
|
187
|
+
* - 画像单值关系(global,如 located_in/works_at):同 src+relation 的**所有** active 旧边
|
|
188
|
+
* 一律 invalidAt=now——即便 dst 不同(换城市/换工作),不允许新旧值并存;
|
|
189
|
+
* - 其余关系:同(src,dst,relation)已有 active 边,fact 相同 → 幂等跳过(duplicate),
|
|
190
|
+
* fact 不同 → 旧边时序失效,新边入库。
|
|
188
191
|
* 容量保护:边超限先清已失效边;仍超 → 拒绝。
|
|
189
192
|
*/
|
|
190
193
|
export function addTriple(input) {
|
|
@@ -205,12 +208,26 @@ export function addTriple(input) {
|
|
|
205
208
|
return { ok: false, reason: `dst entity: ${d.rejected}` };
|
|
206
209
|
const g = loadScopeGraph(scope);
|
|
207
210
|
const fact = (input.fact ?? '').trim();
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
const singleValue = scope === 'global' && isSingleValueProfile(relation);
|
|
212
|
+
// 单值关系:先圈定同 src+relation 的全部 active 旧边(换 dst 也失效)。
|
|
213
|
+
const sameKey = [];
|
|
214
|
+
for (const e of g.edges) {
|
|
215
|
+
if (e.invalidAt || e.src !== s.id || e.relation !== relation)
|
|
216
|
+
continue;
|
|
217
|
+
if (singleValue || e.dst === d.id)
|
|
218
|
+
sameKey.push(e);
|
|
219
|
+
}
|
|
220
|
+
// 完全等价(src+relation+dst 全等且 fact 相同/空)→ 幂等跳过。
|
|
221
|
+
const exact = sameKey.find((e) => e.dst === d.id && (!fact || e.fact === fact));
|
|
222
|
+
if (exact) {
|
|
223
|
+
return { ok: true, edgeId: exact.id, superseded: 0, duplicate: true };
|
|
224
|
+
}
|
|
225
|
+
let superseded = 0;
|
|
226
|
+
const ts = nowIso();
|
|
227
|
+
for (const e of sameKey) {
|
|
228
|
+
e.invalidAt = ts;
|
|
229
|
+
e.invalidReason = singleValue ? 'single-value-replaced' : 'fact-changed';
|
|
230
|
+
superseded++;
|
|
214
231
|
}
|
|
215
232
|
// 容量保护:先清失效边
|
|
216
233
|
if (g.edges.length >= MAX_GRAPH_EDGES) {
|
|
@@ -225,14 +242,133 @@ export function addTriple(input) {
|
|
|
225
242
|
dst: d.id,
|
|
226
243
|
relation,
|
|
227
244
|
fact,
|
|
228
|
-
validAt:
|
|
245
|
+
validAt: ts,
|
|
229
246
|
invalidAt: null,
|
|
230
247
|
sourceEntry: input.sourceEntry ?? null,
|
|
231
248
|
scope,
|
|
232
249
|
};
|
|
250
|
+
if (input.prov)
|
|
251
|
+
edge.prov = input.prov;
|
|
252
|
+
if (typeof input.confidence === 'number' && Number.isFinite(input.confidence)) {
|
|
253
|
+
edge.confidence = Math.min(1, Math.max(0, input.confidence));
|
|
254
|
+
}
|
|
233
255
|
g.edges.push(edge);
|
|
234
256
|
writeGraphForScope(scope, g);
|
|
235
|
-
return { ok: true, edgeId: edge.id, superseded
|
|
257
|
+
return { ok: true, edgeId: edge.id, superseded };
|
|
258
|
+
}
|
|
259
|
+
/** 按时序失效一条边(不删除,可审计);edge id 或 (src,relation,dst 候选) 定位。 */
|
|
260
|
+
export function invalidateEdge(edgeId, reason = 'manual') {
|
|
261
|
+
const id = edgeId.trim();
|
|
262
|
+
if (!id)
|
|
263
|
+
return { ok: false, reason: 'missing edge id' };
|
|
264
|
+
for (const scope of ['global', 'project']) {
|
|
265
|
+
const g = loadScopeGraph(scope);
|
|
266
|
+
const e = g.edges.find((x) => x.id === id && !x.invalidAt);
|
|
267
|
+
if (e) {
|
|
268
|
+
e.invalidAt = nowIso();
|
|
269
|
+
e.invalidReason = reason;
|
|
270
|
+
writeGraphForScope(scope, g);
|
|
271
|
+
return { ok: true };
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return { ok: false, reason: 'not-found' };
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* 用户确认一条画像边:补 prov=user_confirmed / confidence=1,立即获得 L2 内化资格。
|
|
278
|
+
* 仅对 person-primary 出发的画像边生效(/profile confirm 的落地点)。
|
|
279
|
+
*/
|
|
280
|
+
export function promoteEdge(edgeId) {
|
|
281
|
+
const id = edgeId.trim();
|
|
282
|
+
if (!id)
|
|
283
|
+
return { ok: false, reason: 'missing edge id' };
|
|
284
|
+
const scope = 'global';
|
|
285
|
+
const g = loadScopeGraph(scope);
|
|
286
|
+
const e = g.edges.find((x) => x.id === id);
|
|
287
|
+
if (!e)
|
|
288
|
+
return { ok: false, reason: 'not-found' };
|
|
289
|
+
if (e.src !== PRIMARY_PERSON_ID)
|
|
290
|
+
return { ok: false, reason: 'not-a-profile-edge' };
|
|
291
|
+
e.prov = 'user_confirmed';
|
|
292
|
+
e.confidence = 1;
|
|
293
|
+
// 显式确认是用户主权动作,优先级高于自动行为证据:清零相悖计数(否则一条被强反证
|
|
294
|
+
// 自动降级的边,用户 /memory confirm 翻案后仍会被 persona 的强反证否决挡在门外)。
|
|
295
|
+
e.evidence = { confirmed: (e.evidence?.confirmed ?? 0) + 1, contradicted: 0 };
|
|
296
|
+
if (e.invalidAt) {
|
|
297
|
+
e.invalidAt = null; // 确认一条被误失效的边=复活
|
|
298
|
+
e.invalidReason = undefined;
|
|
299
|
+
}
|
|
300
|
+
writeGraphForScope(scope, g);
|
|
301
|
+
return { ok: true };
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* 环境证据累计(反思 evidence 桶的落地点):对一条 active 画像边加一张确认/反对票。
|
|
305
|
+
* 与 promoteEdge 的本质区别——**不碰 prov/confidence**:这里只做可涨可跌、可逆的纯证据
|
|
306
|
+
* 计数,能否内化交给 persona.isInternalizedEdge 按 β 后验统一判定。因此:
|
|
307
|
+
* - 反思候选可经多次行为印证自动转正(无需用户手敲 /memory confirm);
|
|
308
|
+
* - 已内化边被多轮行为矛盾后自动退回候选(不硬删,用户仍可 promoteEdge 翻案)。
|
|
309
|
+
* 只对 person-primary 出发的 global 画像边开放;project triples 不参与画像分级。
|
|
310
|
+
*/
|
|
311
|
+
export function voteEdge(edgeId, vote) {
|
|
312
|
+
const id = edgeId.trim();
|
|
313
|
+
if (!id)
|
|
314
|
+
return { ok: false, reason: 'missing edge id' };
|
|
315
|
+
if (vote !== 'confirm' && vote !== 'contradict')
|
|
316
|
+
return { ok: false, reason: 'bad-vote' };
|
|
317
|
+
const scope = 'global';
|
|
318
|
+
const g = loadScopeGraph(scope);
|
|
319
|
+
const e = g.edges.find((x) => x.id === id);
|
|
320
|
+
if (!e)
|
|
321
|
+
return { ok: false, reason: 'not-found' };
|
|
322
|
+
if (e.invalidAt)
|
|
323
|
+
return { ok: false, reason: 'edge-invalid' };
|
|
324
|
+
if (e.src !== PRIMARY_PERSON_ID)
|
|
325
|
+
return { ok: false, reason: 'not-a-profile-edge' };
|
|
326
|
+
const prev = e.evidence ?? { confirmed: 0, contradicted: 0 };
|
|
327
|
+
const next = vote === 'confirm'
|
|
328
|
+
? { confirmed: prev.confirmed + 1, contradicted: prev.contradicted }
|
|
329
|
+
: { confirmed: prev.confirmed, contradicted: prev.contradicted + 1 };
|
|
330
|
+
e.evidence = next;
|
|
331
|
+
writeGraphForScope(scope, g);
|
|
332
|
+
return {
|
|
333
|
+
ok: true,
|
|
334
|
+
confirmed: next.confirmed,
|
|
335
|
+
contradicted: next.contradicted,
|
|
336
|
+
posterior: (next.confirmed + 1) / (next.confirmed + next.contradicted + 2),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* 失效 person-primary 在某个画像关系上的 active 边(自然语言纠正的落地点:
|
|
341
|
+
* "别再当我在 X 了" → invalidateProfileEdges('located_in'))。多值关系只按 dst 过滤。
|
|
342
|
+
* 返回失效条数。
|
|
343
|
+
*/
|
|
344
|
+
export function invalidateProfileEdges(relation, dst) {
|
|
345
|
+
const rel = relation.trim().toLowerCase().replace(/\s+/g, '_');
|
|
346
|
+
if (!rel)
|
|
347
|
+
return 0;
|
|
348
|
+
const scope = 'global';
|
|
349
|
+
const g = loadScopeGraph(scope);
|
|
350
|
+
const dstWant = dst?.trim().toLowerCase();
|
|
351
|
+
let n = 0;
|
|
352
|
+
const ts = nowIso();
|
|
353
|
+
for (const e of g.edges) {
|
|
354
|
+
if (e.invalidAt || e.src !== PRIMARY_PERSON_ID || e.relation !== rel)
|
|
355
|
+
continue;
|
|
356
|
+
if (dstWant) {
|
|
357
|
+
const target = g.entities.find((x) => x.id === e.dst);
|
|
358
|
+
const names = [e.dst, target?.name ?? '', ...(target?.aliases ?? [])]
|
|
359
|
+
.map((s) => s.trim().toLowerCase())
|
|
360
|
+
.filter((s) => s.length > 0);
|
|
361
|
+
const hit = names.some((s) => s.includes(dstWant) || dstWant.includes(s));
|
|
362
|
+
if (!hit)
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
e.invalidAt = ts;
|
|
366
|
+
e.invalidReason = 'user-correction';
|
|
367
|
+
n++;
|
|
368
|
+
}
|
|
369
|
+
if (n > 0)
|
|
370
|
+
writeGraphForScope(scope, g);
|
|
371
|
+
return n;
|
|
236
372
|
}
|
|
237
373
|
/** 实体关键词搜索(多词子串,name/alias/id 加权),返回命中实体 + 相连 active 边。 */
|
|
238
374
|
export function searchGraph(query, limit = 8) {
|
|
@@ -668,3 +804,37 @@ export function canonicalizePrimaryPerson() {
|
|
|
668
804
|
}
|
|
669
805
|
return { primary: PRIMARY_PERSON_ID, displayName, merged, dedupedEdges: deduped };
|
|
670
806
|
}
|
|
807
|
+
// ── 遗留画像边溯源迁移(M2)─────────────────────────────────────────────────
|
|
808
|
+
//
|
|
809
|
+
// 引入 prov/confidence 之前,所有画像边都"挂上 person-primary 即无条件常驻",
|
|
810
|
+
// 反思单次推断与用户亲述同权。升级后新反思边带 prov='reflection'(0.4,不内化);
|
|
811
|
+
// 而升级前落库的边没有 prov——它们大多来自用户亲口陈述或人工添加,一次性保守补成
|
|
812
|
+
// user_said(0.9,可内化),避免收紧 About you 门槛后真实画像整体消失。
|
|
813
|
+
// 幂等:已有 prov 的边不动;进程内只跑一次(initPersona 守卫)。
|
|
814
|
+
let legacyMigrationDone = false;
|
|
815
|
+
export function migrateLegacyProfileProvenance(force = false) {
|
|
816
|
+
if (legacyMigrationDone && !force)
|
|
817
|
+
return { migrated: 0 };
|
|
818
|
+
legacyMigrationDone = true;
|
|
819
|
+
const scope = 'global';
|
|
820
|
+
const g = loadScopeGraph(scope);
|
|
821
|
+
let migrated = 0;
|
|
822
|
+
for (const e of g.edges) {
|
|
823
|
+
if (e.prov || e.invalidAt)
|
|
824
|
+
continue;
|
|
825
|
+
if (e.src !== PRIMARY_PERSON_ID)
|
|
826
|
+
continue;
|
|
827
|
+
if (!isKnownRelation(e.relation, 'global'))
|
|
828
|
+
continue; // 只提升词表内画像关系
|
|
829
|
+
e.prov = 'user_said';
|
|
830
|
+
e.confidence = 0.9;
|
|
831
|
+
migrated++;
|
|
832
|
+
}
|
|
833
|
+
if (migrated > 0)
|
|
834
|
+
writeGraphForScope(scope, g);
|
|
835
|
+
return { migrated };
|
|
836
|
+
}
|
|
837
|
+
/** 测试专用:重置遗留迁移的进程内守卫。 */
|
|
838
|
+
export function __resetLegacyMigrationForTests() {
|
|
839
|
+
legacyMigrationDone = false;
|
|
840
|
+
}
|
package/dist/memory/index.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// AGENTS.md is intentionally not loaded here either: config/index.ts
|
|
3
3
|
// (buildAgentsImportSection) auto-imports the workspace-root AGENTS.md body into the
|
|
4
4
|
// system prompt — independent of the memory switch — truncating it when it exceeds the cap.
|
|
5
|
-
export { buildMemoryIndexSection, loadAll, gcMemories, } from './store.js';
|
|
6
|
-
export { addTriple, upsertEntity, findEntity, searchGraph, searchGraphFacts, neighborsOf, pathBetween, graphStats, ensurePrimaryPerson, mergeEntities, canonicalizePrimaryPerson, PRIMARY_PERSON_ID, } from './graph.js';
|
|
7
|
-
export { buildAboutYouSection, initPersona, __resetPersonaInitForTests, ABOUT_YOU_MAX_CHARS } from './persona.js';
|
|
5
|
+
export { buildMemoryIndexSection, loadAll, gcMemories, supersedeEntry, } from './store.js';
|
|
6
|
+
export { addTriple, upsertEntity, findEntity, searchGraph, searchGraphFacts, neighborsOf, pathBetween, graphStats, ensurePrimaryPerson, mergeEntities, canonicalizePrimaryPerson, invalidateEdge, promoteEdge, voteEdge, invalidateProfileEdges, migrateLegacyProfileProvenance, __resetLegacyMigrationForTests, PRIMARY_PERSON_ID, } from './graph.js';
|
|
7
|
+
export { buildAboutYouSection, initPersona, isInternalizedEdge, listProfileCandidates, listInternalizedProfile, inspectPrimaryProfile, __resetPersonaInitForTests, ABOUT_YOU_MAX_CHARS, } from './persona.js';
|
|
8
8
|
export { buildProjectGraphHint, GRAPH_HINT_MAX_CHARS } from './recall.js';
|
|
9
9
|
export { kickoffReflection, drainMemoryBackground, getLastReflectResult, clearLastReflectResult, snapshotTranscript, formatReflectResult, runReflection, } from './reflect.js';
|
package/dist/memory/persona.js
CHANGED
|
@@ -7,7 +7,47 @@
|
|
|
7
7
|
// 30 天衰减影响——这是"越用越懂用户"那条易失通道之外的确定性内化通道。
|
|
8
8
|
//
|
|
9
9
|
// 纯读、不建实体(建实体/归一走 graph.canonicalizePrimaryPerson,在启动期显式做)。
|
|
10
|
-
import { loadScopeGraph, canonicalizePrimaryPerson, PRIMARY_PERSON_ID, } from './graph.js';
|
|
10
|
+
import { loadScopeGraph, canonicalizePrimaryPerson, migrateLegacyProfileProvenance, PRIMARY_PERSON_ID, } from './graph.js';
|
|
11
|
+
/** β(1,1) 后验:P(信念为真 | 证据)。confirmed/contradicted 来自反思对对话行为的计票。 */
|
|
12
|
+
export function edgePosterior(e) {
|
|
13
|
+
const c = e.evidence?.confirmed ?? 0;
|
|
14
|
+
const x = e.evidence?.contradicted ?? 0;
|
|
15
|
+
return (c + 1) / (c + x + 2);
|
|
16
|
+
}
|
|
17
|
+
/** 行为印证自动转正:连续 ≥3 次确认且零反驳(β=0.8)——"多次默认、从不反对"。 */
|
|
18
|
+
const BEHAVIOR_PROMOTE_MIN_CONFIRMS = 3;
|
|
19
|
+
/** 强反证自动降级:≥3 次行为相悖且后验 <0.4,连 user_said 也退回候选(用户 promote 可翻案)。 */
|
|
20
|
+
const STRONG_VETO_MIN_CONTRADICTS = 3;
|
|
21
|
+
const STRONG_VETO_POSTERIOR = 0.4;
|
|
22
|
+
/**
|
|
23
|
+
* L2 内化判定:只有"被验证过的"画像边才配常驻每轮 system prompt。
|
|
24
|
+
* - 强反证否决优先:≥3 次行为相悖且后验 <0.4 → 一律退回候选(即使用户亲述过;
|
|
25
|
+
* 显式纠正应走 corrections 桶,这里只处理"用下来发现不对"的弱信号累积);
|
|
26
|
+
* - prov=user_said / user_confirmed:用户亲述或确认(迁移的旧边保守视作亲述);
|
|
27
|
+
* - confidence ≥ 0.85:显式高置信;
|
|
28
|
+
* - β 后验 ≥ 0.85 且至少 1 次环境确认;
|
|
29
|
+
* - 或连续 ≥3 次行为印证、零反驳(反思候选的自动转正通道,无需手敲 confirm)。
|
|
30
|
+
* 反思单次推断(prov=reflection,0.4)不达标 → 只作候选,不进 About you。
|
|
31
|
+
*/
|
|
32
|
+
export function isInternalizedEdge(e) {
|
|
33
|
+
const x = e.evidence?.contradicted ?? 0;
|
|
34
|
+
if (x >= STRONG_VETO_MIN_CONTRADICTS && edgePosterior(e) < STRONG_VETO_POSTERIOR)
|
|
35
|
+
return false;
|
|
36
|
+
if (e.prov === 'user_said' || e.prov === 'user_confirmed')
|
|
37
|
+
return true;
|
|
38
|
+
if (typeof e.confidence === 'number' && e.confidence >= 0.85)
|
|
39
|
+
return true;
|
|
40
|
+
const c = e.evidence?.confirmed ?? 0;
|
|
41
|
+
if (c >= 1) {
|
|
42
|
+
const posterior = edgePosterior(e);
|
|
43
|
+
if (posterior >= 0.85)
|
|
44
|
+
return true;
|
|
45
|
+
// 纯行为通道:多次印证 + 零反驳。任何一次相悖都留在候选,等用户显式确认。
|
|
46
|
+
if (c >= BEHAVIOR_PROMOTE_MIN_CONFIRMS && x === 0)
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
11
51
|
/** About you 段硬预算(字符):只放高等级画像,超了按置信/顺序截断。 */
|
|
12
52
|
export const ABOUT_YOU_MAX_CHARS = 800;
|
|
13
53
|
/** 关系 → 人类可读标签(画像白名单;不在表内的关系用 fact 原文,仍可展示)。 */
|
|
@@ -35,7 +75,9 @@ function displayName(name) {
|
|
|
35
75
|
return name.replace(/[_-]+/g, ' ').trim();
|
|
36
76
|
}
|
|
37
77
|
/**
|
|
38
|
-
* 渲染全局 L2 画像段。无 person-primary /
|
|
78
|
+
* 渲染全局 L2 画像段。无 person-primary / 无**达内化门槛**的 active 画像边 → 空串。
|
|
79
|
+
* 内化门槛见 isInternalizedEdge:反思推断(0.4)不常驻,用户亲述/确认/高置信,
|
|
80
|
+
* 或多次行为印证零反驳(≥3 confirm)才在;强反证(≥3 contradict 且后验<0.4)会降回候选。
|
|
39
81
|
* 优先用边上的中文 fact 原文;没有 fact 时用「关系标签 + 目标实体名」。
|
|
40
82
|
* 同名去重;has_hobby 合并为一行;整体截断到 ABOUT_YOU_MAX_CHARS。
|
|
41
83
|
*/
|
|
@@ -47,7 +89,7 @@ export function buildAboutYouSection(memoryEnabled = true) {
|
|
|
47
89
|
if (!me)
|
|
48
90
|
return '';
|
|
49
91
|
const byId = new Map(g.entities.map((e) => [e.id, e]));
|
|
50
|
-
const outgoing = g.edges.filter((e) => !e.invalidAt && e.src === PRIMARY_PERSON_ID && e.dst !== PRIMARY_PERSON_ID);
|
|
92
|
+
const outgoing = g.edges.filter((e) => !e.invalidAt && e.src === PRIMARY_PERSON_ID && e.dst !== PRIMARY_PERSON_ID && isInternalizedEdge(e));
|
|
51
93
|
if (outgoing.length === 0)
|
|
52
94
|
return '';
|
|
53
95
|
const hobbies = [];
|
|
@@ -117,6 +159,48 @@ export function inspectPrimaryProfile() {
|
|
|
117
159
|
const edges = g.edges.filter((e) => !e.invalidAt && (e.src === PRIMARY_PERSON_ID || e.dst === PRIMARY_PERSON_ID));
|
|
118
160
|
return { me, edges };
|
|
119
161
|
}
|
|
162
|
+
/** L2 内化画像列表(当前会渲染进 About you 的边),与 listProfileCandidates 对称。 */
|
|
163
|
+
export function listInternalizedProfile(limit = 20) {
|
|
164
|
+
const g = loadScopeGraph('global');
|
|
165
|
+
const byId = new Map(g.entities.map((e) => [e.id, e]));
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const e of g.edges) {
|
|
168
|
+
if (e.invalidAt || e.src !== PRIMARY_PERSON_ID || e.dst === PRIMARY_PERSON_ID)
|
|
169
|
+
continue;
|
|
170
|
+
if (!isInternalizedEdge(e))
|
|
171
|
+
continue;
|
|
172
|
+
const dst = byId.get(e.dst);
|
|
173
|
+
const target = dst ? displayName(dst.name) : displayName(e.dst);
|
|
174
|
+
const text = e.fact?.trim() || `${RELATION_LABEL[e.relation] ?? e.relation.replace(/_/g, ' ')}:${target}`;
|
|
175
|
+
out.push({ edge: e, text, relation: e.relation });
|
|
176
|
+
if (out.length >= limit)
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* L1 候选画像:挂在 person-primary 上、active、但**未达内化门槛**的边
|
|
183
|
+
* (典型:反思单次推断 prov=reflection/0.4)。供 /profile 展示"待确认"列表,
|
|
184
|
+
* 用户确认后 promoteEdge 升 L2。不含已渲染在 About you 里的内化边。
|
|
185
|
+
*/
|
|
186
|
+
export function listProfileCandidates(limit = 10) {
|
|
187
|
+
const g = loadScopeGraph('global');
|
|
188
|
+
const byId = new Map(g.entities.map((e) => [e.id, e]));
|
|
189
|
+
const out = [];
|
|
190
|
+
for (const e of g.edges) {
|
|
191
|
+
if (e.invalidAt || e.src !== PRIMARY_PERSON_ID || e.dst === PRIMARY_PERSON_ID)
|
|
192
|
+
continue;
|
|
193
|
+
if (isInternalizedEdge(e))
|
|
194
|
+
continue;
|
|
195
|
+
const dst = byId.get(e.dst);
|
|
196
|
+
const target = dst ? displayName(dst.name) : displayName(e.dst);
|
|
197
|
+
const text = e.fact?.trim() || `${RELATION_LABEL[e.relation] ?? e.relation.replace(/_/g, ' ')}:${target}`;
|
|
198
|
+
out.push({ edge: e, text, relation: e.relation });
|
|
199
|
+
if (out.length >= limit)
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
120
204
|
let personaInitialized = false;
|
|
121
205
|
/**
|
|
122
206
|
* 启动期幂等初始化:做一次全局身份归一(把 mocode-user / user-name-* / 自称等碎片
|
|
@@ -135,6 +219,13 @@ export function initPersona(memoryEnabled = true) {
|
|
|
135
219
|
catch {
|
|
136
220
|
// 归一只是增强(图谱损坏/IO 异常),绝不能拖垮 REPL 启动。
|
|
137
221
|
}
|
|
222
|
+
try {
|
|
223
|
+
// M2:升级前落库的画像边没有 prov,保守补 user_said(详见函数注释)。
|
|
224
|
+
migrateLegacyProfileProvenance();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
// 迁移同样只是增强。
|
|
228
|
+
}
|
|
138
229
|
}
|
|
139
230
|
/** 测试专用:重置进程内初始化标记。 */
|
|
140
231
|
export function __resetPersonaInitForTests() {
|