fix(memory-core): align prompt with available tools

This commit is contained in:
Frank Yang 2026-03-23 13:20:13 +08:00
parent 2c02a62be9
commit df7f968581
2 changed files with 34 additions and 8 deletions

View File

@ -6,18 +6,31 @@ describe("buildPromptSection", () => {
expect(buildPromptSection({ availableTools: new Set() })).toEqual([]);
});
it("returns Memory Recall section when memory_search is available", () => {
const result = buildPromptSection({ availableTools: new Set(["memory_search"]) });
it("describes the two-step flow when both memory tools are available", () => {
const result = buildPromptSection({
availableTools: new Set(["memory_search", "memory_get"]),
});
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_search");
expect(result[1]).toContain("then use memory_get");
expect(result).toContain(
"Citations: include Source: <path#line> when it helps the user verify memory snippets.",
);
expect(result.at(-1)).toBe("");
});
it("returns Memory Recall section when memory_get is available", () => {
it("limits the guidance to memory_search when only search is available", () => {
const result = buildPromptSection({ availableTools: new Set(["memory_search"]) });
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_search");
expect(result[1]).not.toContain("then use memory_get");
});
it("limits the guidance to memory_get when only get is available", () => {
const result = buildPromptSection({ availableTools: new Set(["memory_get"]) });
expect(result[0]).toBe("## Memory Recall");
expect(result[1]).toContain("run memory_get");
expect(result[1]).not.toContain("run memory_search");
});
it("includes citations-off instruction when citationsMode is off", () => {

View File

@ -5,13 +5,26 @@ export const buildPromptSection: MemoryPromptSectionBuilder = ({
availableTools,
citationsMode,
}) => {
if (!availableTools.has("memory_search") && !availableTools.has("memory_get")) {
const hasMemorySearch = availableTools.has("memory_search");
const hasMemoryGet = availableTools.has("memory_get");
if (!hasMemorySearch && !hasMemoryGet) {
return [];
}
const lines = [
"## Memory Recall",
"Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md; then use memory_get to pull only the needed lines. If low confidence after search, say you checked.",
];
let toolGuidance: string;
if (hasMemorySearch && hasMemoryGet) {
toolGuidance =
"Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md; then use memory_get to pull only the needed lines. If low confidence after search, say you checked.";
} else if (hasMemorySearch) {
toolGuidance =
"Before answering anything about prior work, decisions, dates, people, preferences, or todos: run memory_search on MEMORY.md + memory/*.md and answer from the matching results. If low confidence after search, say you checked.";
} else {
toolGuidance =
"Before answering anything about prior work, decisions, dates, people, preferences, or todos that already point to a specific memory file or note: run memory_get to pull only the needed lines. If low confidence after reading them, say you checked.";
}
const lines = ["## Memory Recall", toolGuidance];
if (citationsMode === "off") {
lines.push(
"Citations are disabled: do not mention file paths or line numbers in replies unless the user explicitly asks.",