fix: derive citations chat type via session parser

This commit is contained in:
Vignesh Natarajan
2026-02-02 20:10:45 -08:00
committed by Vignesh
parent d0b98c75e5
commit edd6289f26
2 changed files with 47 additions and 5 deletions

View File

@@ -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:/);
});
});

View File

@@ -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";
}