207 lines
7.5 KiB
TypeScript
207 lines
7.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import {
|
|
resolveRelevantMemoriesForTesting,
|
|
resolveRuntimeRelevantMemoriesForTesting,
|
|
} from "@/lib/execution/memory-resolver";
|
|
|
|
test("MemoryResolver 在 master-agent 会话下优先挑当前请求命中的项目记忆", () => {
|
|
const resolved = resolveRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
requestText: "boss-console 的审批流",
|
|
memories: [
|
|
{
|
|
memoryId: "m1",
|
|
scope: "project",
|
|
projectId: "boss-console",
|
|
title: "审批流",
|
|
content: "boss-console approval",
|
|
tags: ["approval"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "m2",
|
|
scope: "project",
|
|
projectId: "wenshenapp",
|
|
title: "UI",
|
|
content: "wechat ui",
|
|
tags: ["ui"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-02T00:00:00.000Z",
|
|
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(resolved.projectMemories.length, 1);
|
|
assert.equal(resolved.projectMemories[0]?.projectId, "boss-console");
|
|
});
|
|
|
|
test("MemoryResolver 会按请求里的自然语言关键词命中更相关的项目记忆", () => {
|
|
const resolved = resolveRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
requestText: "继续推进 boss 项目的会话归档逻辑",
|
|
memories: [
|
|
{
|
|
memoryId: "m1",
|
|
scope: "project",
|
|
projectId: "boss-console",
|
|
title: "boss 项目进度",
|
|
content: "boss 项目当前按项目聚合加线程下钻展示。",
|
|
tags: ["boss", "会话"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "m2",
|
|
scope: "project",
|
|
projectId: "project-wenshenapp",
|
|
title: "wenshenapp 项目进度",
|
|
content: "wenshenapp 当前只有一个主线程。",
|
|
tags: ["wenshenapp"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-02T00:00:00.000Z",
|
|
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(resolved.projectMemories.length, 1);
|
|
assert.equal(resolved.projectMemories[0]?.projectId, "boss-console");
|
|
});
|
|
|
|
test("MemoryResolver 会保留全局记忆的输入顺序并只截断到 8 条", () => {
|
|
const resolved = resolveRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
memories: [
|
|
{
|
|
memoryId: "g1",
|
|
scope: "global",
|
|
title: "一",
|
|
content: "one",
|
|
tags: [],
|
|
memoryType: "decision",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "g2",
|
|
scope: "global",
|
|
title: "二",
|
|
content: "two",
|
|
tags: [],
|
|
memoryType: "decision",
|
|
createdAt: "2026-01-02T00:00:00.000Z",
|
|
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
resolved.userMemories.map((memory) => memory.memoryId),
|
|
["g1", "g2"],
|
|
);
|
|
});
|
|
|
|
test("Runtime MemoryResolver 会优先排布 workflow_rule 和 user_preference 全局记忆", () => {
|
|
const resolved = resolveRuntimeRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
memories: [
|
|
{
|
|
memoryId: "g1",
|
|
scope: "global",
|
|
title: "普通记忆",
|
|
content: "normal",
|
|
tags: [],
|
|
memoryType: "decision",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "g2",
|
|
scope: "global",
|
|
title: "规则记忆",
|
|
content: "workflow",
|
|
tags: [],
|
|
memoryType: "workflow_rule",
|
|
createdAt: "2026-01-03T00:00:00.000Z",
|
|
updatedAt: "2026-01-03T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "g3",
|
|
scope: "global",
|
|
title: "偏好记忆",
|
|
content: "preference",
|
|
tags: [],
|
|
memoryType: "user_preference",
|
|
createdAt: "2026-01-02T00:00:00.000Z",
|
|
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
resolved.userMemories.map((memory) => memory.memoryId),
|
|
["g2", "g3", "g1"],
|
|
);
|
|
});
|
|
|
|
test("Runtime MemoryResolver 也会按自然语言关键词优先挑中更相关的项目记忆", () => {
|
|
const resolved = resolveRuntimeRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
requestText: "继续推进 boss 项目的会话归档逻辑",
|
|
memories: [
|
|
{
|
|
memoryId: "m2",
|
|
scope: "project",
|
|
projectId: "project-wenshenapp",
|
|
title: "wenshenapp 项目进度",
|
|
content: "wenshenapp 当前只有一个主线程。",
|
|
tags: ["wenshenapp"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-02T00:00:00.000Z",
|
|
updatedAt: "2026-01-02T00:00:00.000Z",
|
|
},
|
|
{
|
|
memoryId: "m1",
|
|
scope: "project",
|
|
projectId: "boss-console",
|
|
title: "boss 项目进度",
|
|
content: "boss 项目当前按项目聚合加线程下钻展示。",
|
|
tags: ["boss", "会话"],
|
|
memoryType: "project_progress",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
updatedAt: "2026-01-01T00:00:00.000Z",
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(resolved.projectMemories.length, 1);
|
|
assert.equal(resolved.projectMemories[0]?.projectId, "boss-console");
|
|
});
|
|
|
|
test("Runtime MemoryResolver 在 master-agent 非空请求但无 lexical 命中时回退到前 6 个项目记忆", () => {
|
|
const resolved = resolveRuntimeRelevantMemoriesForTesting({
|
|
projectId: "master-agent",
|
|
requestText: "xyzxyz no overlap",
|
|
memories: [
|
|
{ memoryId: "p1", scope: "project", projectId: "boss-console", title: "alpha", content: "alpha-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-01T00:00:00.000Z", updatedAt: "2026-01-01T00:00:00.000Z" },
|
|
{ memoryId: "p2", scope: "project", projectId: "boss-console", title: "bravo", content: "bravo-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-02T00:00:00.000Z", updatedAt: "2026-01-02T00:00:00.000Z" },
|
|
{ memoryId: "p3", scope: "project", projectId: "boss-console", title: "charlie", content: "charlie-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-03T00:00:00.000Z", updatedAt: "2026-01-03T00:00:00.000Z" },
|
|
{ memoryId: "p4", scope: "project", projectId: "boss-console", title: "delta", content: "delta-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-04T00:00:00.000Z", updatedAt: "2026-01-04T00:00:00.000Z" },
|
|
{ memoryId: "p5", scope: "project", projectId: "boss-console", title: "echo", content: "echo-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-05T00:00:00.000Z", updatedAt: "2026-01-05T00:00:00.000Z" },
|
|
{ memoryId: "p6", scope: "project", projectId: "boss-console", title: "foxtrot", content: "foxtrot-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-06T00:00:00.000Z", updatedAt: "2026-01-06T00:00:00.000Z" },
|
|
{ memoryId: "p7", scope: "project", projectId: "boss-console", title: "golf", content: "golf-content", tags: [], memoryType: "project_progress", createdAt: "2026-01-07T00:00:00.000Z", updatedAt: "2026-01-07T00:00:00.000Z" },
|
|
],
|
|
});
|
|
|
|
assert.equal(resolved.projectMemories.length, 6);
|
|
assert.deepEqual(
|
|
resolved.projectMemories.map((memory) => memory.memoryId),
|
|
["p1", "p2", "p3", "p4", "p5", "p6"],
|
|
);
|
|
});
|