Enable meaningful session titles via priority-based derivation: 1. displayName (user-set) 2. subject (group name) 3. First user message (truncated to 60 chars) 4. sessionId prefix + date fallback Opt-in via includeDerivedTitles param to avoid perf impact on regular listing. Reads only first 10 lines of transcript files. Closes #1161
190 lines
6.5 KiB
TypeScript
190 lines
6.5 KiB
TypeScript
import os from "node:os";
|
|
import path from "node:path";
|
|
import { describe, expect, test } from "vitest";
|
|
import type { ClawdbotConfig } from "../config/config.js";
|
|
import type { SessionEntry } from "../config/sessions.js";
|
|
import {
|
|
capArrayByJsonBytes,
|
|
classifySessionKey,
|
|
deriveSessionTitle,
|
|
parseGroupKey,
|
|
resolveGatewaySessionStoreTarget,
|
|
resolveSessionStoreKey,
|
|
} from "./session-utils.js";
|
|
|
|
describe("gateway session utils", () => {
|
|
test("capArrayByJsonBytes trims from the front", () => {
|
|
const res = capArrayByJsonBytes(["a", "b", "c"], 10);
|
|
expect(res.items).toEqual(["b", "c"]);
|
|
});
|
|
|
|
test("parseGroupKey handles group keys", () => {
|
|
expect(parseGroupKey("discord:group:dev")).toEqual({
|
|
channel: "discord",
|
|
kind: "group",
|
|
id: "dev",
|
|
});
|
|
expect(parseGroupKey("agent:ops:discord:group:dev")).toEqual({
|
|
channel: "discord",
|
|
kind: "group",
|
|
id: "dev",
|
|
});
|
|
expect(parseGroupKey("foo:bar")).toBeNull();
|
|
});
|
|
|
|
test("classifySessionKey respects chat type + prefixes", () => {
|
|
expect(classifySessionKey("global")).toBe("global");
|
|
expect(classifySessionKey("unknown")).toBe("unknown");
|
|
expect(classifySessionKey("discord:group:dev")).toBe("group");
|
|
expect(classifySessionKey("main")).toBe("direct");
|
|
const entry = { chatType: "group" } as SessionEntry;
|
|
expect(classifySessionKey("main", entry)).toBe("group");
|
|
});
|
|
|
|
test("resolveSessionStoreKey maps main aliases to default agent main", () => {
|
|
const cfg = {
|
|
session: { mainKey: "work" },
|
|
agents: { list: [{ id: "ops", default: true }] },
|
|
} as ClawdbotConfig;
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "main" })).toBe("agent:ops:work");
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "work" })).toBe("agent:ops:work");
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "agent:ops:main" })).toBe("agent:ops:work");
|
|
});
|
|
|
|
test("resolveSessionStoreKey canonicalizes bare keys to default agent", () => {
|
|
const cfg = {
|
|
session: { mainKey: "main" },
|
|
agents: { list: [{ id: "ops", default: true }] },
|
|
} as ClawdbotConfig;
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "discord:group:123" })).toBe(
|
|
"agent:ops:discord:group:123",
|
|
);
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "agent:alpha:main" })).toBe(
|
|
"agent:alpha:main",
|
|
);
|
|
});
|
|
|
|
test("resolveSessionStoreKey honors global scope", () => {
|
|
const cfg = {
|
|
session: { scope: "global", mainKey: "work" },
|
|
agents: { list: [{ id: "ops", default: true }] },
|
|
} as ClawdbotConfig;
|
|
expect(resolveSessionStoreKey({ cfg, sessionKey: "main" })).toBe("global");
|
|
const target = resolveGatewaySessionStoreTarget({ cfg, key: "main" });
|
|
expect(target.canonicalKey).toBe("global");
|
|
expect(target.agentId).toBe("ops");
|
|
});
|
|
|
|
test("resolveGatewaySessionStoreTarget uses canonical key for main alias", () => {
|
|
const storeTemplate = path.join(
|
|
os.tmpdir(),
|
|
"clawdbot-session-utils",
|
|
"{agentId}",
|
|
"sessions.json",
|
|
);
|
|
const cfg = {
|
|
session: { mainKey: "main", store: storeTemplate },
|
|
agents: { list: [{ id: "ops", default: true }] },
|
|
} as ClawdbotConfig;
|
|
const target = resolveGatewaySessionStoreTarget({ cfg, key: "main" });
|
|
expect(target.canonicalKey).toBe("agent:ops:main");
|
|
expect(target.storeKeys).toEqual(expect.arrayContaining(["agent:ops:main", "main"]));
|
|
expect(target.storePath).toBe(path.resolve(storeTemplate.replace("{agentId}", "ops")));
|
|
});
|
|
});
|
|
|
|
describe("deriveSessionTitle", () => {
|
|
test("returns undefined for undefined entry", () => {
|
|
expect(deriveSessionTitle(undefined)).toBeUndefined();
|
|
});
|
|
|
|
test("prefers displayName when set", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
displayName: "My Custom Session",
|
|
subject: "Group Chat",
|
|
} as SessionEntry;
|
|
expect(deriveSessionTitle(entry)).toBe("My Custom Session");
|
|
});
|
|
|
|
test("falls back to subject when displayName is missing", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
subject: "Dev Team Chat",
|
|
} as SessionEntry;
|
|
expect(deriveSessionTitle(entry)).toBe("Dev Team Chat");
|
|
});
|
|
|
|
test("uses first user message when displayName and subject missing", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
} as SessionEntry;
|
|
expect(deriveSessionTitle(entry, "Hello, how are you?")).toBe("Hello, how are you?");
|
|
});
|
|
|
|
test("truncates long first user message to 60 chars with ellipsis", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
} as SessionEntry;
|
|
const longMsg =
|
|
"This is a very long message that exceeds sixty characters and should be truncated appropriately";
|
|
const result = deriveSessionTitle(entry, longMsg);
|
|
expect(result).toBeDefined();
|
|
expect(result!.length).toBeLessThanOrEqual(60);
|
|
expect(result!.endsWith("…")).toBe(true);
|
|
});
|
|
|
|
test("truncates at word boundary when possible", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
} as SessionEntry;
|
|
const longMsg = "This message has many words and should be truncated at a word boundary nicely";
|
|
const result = deriveSessionTitle(entry, longMsg);
|
|
expect(result).toBeDefined();
|
|
expect(result!.endsWith("…")).toBe(true);
|
|
expect(result!.includes(" ")).toBe(false);
|
|
});
|
|
|
|
test("falls back to sessionId prefix with date", () => {
|
|
const entry = {
|
|
sessionId: "abcd1234-5678-90ef-ghij-klmnopqrstuv",
|
|
updatedAt: new Date("2024-03-15T10:30:00Z").getTime(),
|
|
} as SessionEntry;
|
|
const result = deriveSessionTitle(entry);
|
|
expect(result).toBe("abcd1234 (2024-03-15)");
|
|
});
|
|
|
|
test("falls back to sessionId prefix without date when updatedAt missing", () => {
|
|
const entry = {
|
|
sessionId: "abcd1234-5678-90ef-ghij-klmnopqrstuv",
|
|
updatedAt: 0,
|
|
} as SessionEntry;
|
|
const result = deriveSessionTitle(entry);
|
|
expect(result).toBe("abcd1234");
|
|
});
|
|
|
|
test("trims whitespace from displayName", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
displayName: " Padded Name ",
|
|
} as SessionEntry;
|
|
expect(deriveSessionTitle(entry)).toBe("Padded Name");
|
|
});
|
|
|
|
test("ignores empty displayName and falls through", () => {
|
|
const entry = {
|
|
sessionId: "abc123",
|
|
updatedAt: Date.now(),
|
|
displayName: " ",
|
|
subject: "Actual Subject",
|
|
} as SessionEntry;
|
|
expect(deriveSessionTitle(entry)).toBe("Actual Subject");
|
|
});
|
|
});
|