diff --git a/src/agents/tools/memory-tool.citations.test.ts b/src/agents/tools/memory-tool.citations.test.ts index 3a1b4c590..8e4d5c1b7 100644 --- a/src/agents/tools/memory-tool.citations.test.ts +++ b/src/agents/tools/memory-tool.citations.test.ts @@ -84,4 +84,40 @@ describe("memory search citations", () => { const details = result.details as { results: Array<{ snippet: string; citation?: string }> }; expect(details.results[0]?.snippet.length).toBeLessThanOrEqual(20); }); + + it("honors auto mode for direct chats", async () => { + backend = "builtin"; + const cfg = { + memory: { citations: "auto" }, + agents: { list: [{ id: "main", default: true }] }, + }; + const tool = createMemorySearchTool({ + config: cfg, + agentSessionKey: "agent:main:discord:dm:u123", + }); + if (!tool) { + throw new Error("tool missing"); + } + const result = await tool.execute("auto_mode_direct", { query: "notes" }); + const details = result.details as { results: Array<{ snippet: string }> }; + expect(details.results[0]?.snippet).toMatch(/Source:/); + }); + + it("suppresses citations for auto mode in group chats", async () => { + backend = "builtin"; + const cfg = { + memory: { citations: "auto" }, + agents: { list: [{ id: "main", default: true }] }, + }; + const tool = createMemorySearchTool({ + config: cfg, + agentSessionKey: "agent:main:discord:group:c123", + }); + if (!tool) { + throw new Error("tool missing"); + } + const result = await tool.execute("auto_mode_group", { query: "notes" }); + const details = result.details as { results: Array<{ snippet: string }> }; + expect(details.results[0]?.snippet).not.toMatch(/Source:/); + }); }); diff --git a/src/agents/tools/memory-tool.ts b/src/agents/tools/memory-tool.ts index e9499e5f8..139e5da90 100644 --- a/src/agents/tools/memory-tool.ts +++ b/src/agents/tools/memory-tool.ts @@ -5,6 +5,7 @@ import type { MemoryCitationsMode } from "../../config/types.memory.js"; import { resolveMemoryBackendConfig } from "../../memory/backend-config.js"; import { getMemorySearchManager } from "../../memory/index.js"; import type { MemorySearchResult } from "../../memory/types.js"; +import { parseAgentSessionKey } from "../../routing/session-key.js"; import { resolveSessionAgentId } from "../agent-scope.js"; import { resolveMemorySearchConfig } from "../memory-search.js"; import type { AnyAgentTool } from "./common.js"; @@ -195,14 +196,19 @@ function shouldIncludeCitations(params: { } function deriveChatTypeFromSessionKey(sessionKey?: string): "direct" | "group" | "channel" { - if (!sessionKey) { + const parsed = parseAgentSessionKey(sessionKey); + if (!parsed?.rest) { return "direct"; } - if (sessionKey.includes(":group:")) { - return "group"; - } - if (sessionKey.includes(":channel:")) { + const tokens = parsed.rest + .toLowerCase() + .split(":") + .filter(Boolean); + if (tokens.includes("channel")) { return "channel"; } + if (tokens.includes("group")) { + return "group"; + } return "direct"; }