refactor: prune legacy group targets
This commit is contained in:
@@ -40,43 +40,43 @@ describe("history helpers", () => {
|
||||
|
||||
appendHistoryEntry({
|
||||
historyMap,
|
||||
historyKey: "room",
|
||||
historyKey: "group",
|
||||
limit: 2,
|
||||
entry: { sender: "A", body: "one" },
|
||||
});
|
||||
appendHistoryEntry({
|
||||
historyMap,
|
||||
historyKey: "room",
|
||||
historyKey: "group",
|
||||
limit: 2,
|
||||
entry: { sender: "B", body: "two" },
|
||||
});
|
||||
appendHistoryEntry({
|
||||
historyMap,
|
||||
historyKey: "room",
|
||||
historyKey: "group",
|
||||
limit: 2,
|
||||
entry: { sender: "C", body: "three" },
|
||||
});
|
||||
|
||||
expect(historyMap.get("room")?.map((entry) => entry.body)).toEqual(["two", "three"]);
|
||||
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["two", "three"]);
|
||||
});
|
||||
|
||||
it("builds context from map and appends entry", () => {
|
||||
const historyMap = new Map<string, { sender: string; body: string }[]>();
|
||||
historyMap.set("room", [
|
||||
historyMap.set("group", [
|
||||
{ sender: "A", body: "one" },
|
||||
{ sender: "B", body: "two" },
|
||||
]);
|
||||
|
||||
const result = buildHistoryContextFromMap({
|
||||
historyMap,
|
||||
historyKey: "room",
|
||||
historyKey: "group",
|
||||
limit: 3,
|
||||
entry: { sender: "C", body: "three" },
|
||||
currentMessage: "current",
|
||||
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
|
||||
});
|
||||
|
||||
expect(historyMap.get("room")?.map((entry) => entry.body)).toEqual(["one", "two", "three"]);
|
||||
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["one", "two", "three"]);
|
||||
expect(result).toContain(HISTORY_CONTEXT_MARKER);
|
||||
expect(result).toContain("A: one");
|
||||
expect(result).toContain("B: two");
|
||||
@@ -85,20 +85,20 @@ describe("history helpers", () => {
|
||||
|
||||
it("builds context from pending map without appending", () => {
|
||||
const historyMap = new Map<string, { sender: string; body: string }[]>();
|
||||
historyMap.set("room", [
|
||||
historyMap.set("group", [
|
||||
{ sender: "A", body: "one" },
|
||||
{ sender: "B", body: "two" },
|
||||
]);
|
||||
|
||||
const result = buildPendingHistoryContextFromMap({
|
||||
historyMap,
|
||||
historyKey: "room",
|
||||
historyKey: "group",
|
||||
limit: 3,
|
||||
currentMessage: "current",
|
||||
formatEntry: (entry) => `${entry.sender}: ${entry.body}`,
|
||||
});
|
||||
|
||||
expect(historyMap.get("room")?.map((entry) => entry.body)).toEqual(["one", "two"]);
|
||||
expect(historyMap.get("group")?.map((entry) => entry.body)).toEqual(["one", "two"]);
|
||||
expect(result).toContain(HISTORY_CONTEXT_MARKER);
|
||||
expect(result).toContain("A: one");
|
||||
expect(result).toContain("B: two");
|
||||
|
||||
@@ -229,7 +229,7 @@ export async function initSessionState(params: {
|
||||
channel: baseEntry?.channel,
|
||||
groupId: baseEntry?.groupId,
|
||||
subject: baseEntry?.subject,
|
||||
room: baseEntry?.room,
|
||||
groupChannel: baseEntry?.groupChannel,
|
||||
space: baseEntry?.space,
|
||||
deliveryContext: deliveryFields.deliveryContext,
|
||||
// Track originating channel for subagent announce routing.
|
||||
@@ -247,24 +247,24 @@ export async function initSessionState(params: {
|
||||
normalizedChannel &&
|
||||
getChannelDock(normalizedChannel)?.capabilities.chatTypes.includes("channel"),
|
||||
);
|
||||
const nextRoom =
|
||||
const nextGroupChannel =
|
||||
explicitChannel ??
|
||||
((groupResolution.chatType === "channel" || isChannelProvider) &&
|
||||
subject &&
|
||||
subject.startsWith("#")
|
||||
? subject
|
||||
: undefined);
|
||||
const nextSubject = nextRoom ? undefined : subject;
|
||||
const nextSubject = nextGroupChannel ? undefined : subject;
|
||||
sessionEntry.chatType = groupResolution.chatType ?? "group";
|
||||
sessionEntry.channel = channel;
|
||||
sessionEntry.groupId = groupResolution.id;
|
||||
if (nextSubject) sessionEntry.subject = nextSubject;
|
||||
if (nextRoom) sessionEntry.room = nextRoom;
|
||||
if (nextGroupChannel) sessionEntry.groupChannel = nextGroupChannel;
|
||||
if (space) sessionEntry.space = space;
|
||||
sessionEntry.displayName = buildGroupDisplayName({
|
||||
provider: sessionEntry.channel,
|
||||
subject: sessionEntry.subject,
|
||||
room: sessionEntry.room,
|
||||
groupChannel: sessionEntry.groupChannel,
|
||||
space: sessionEntry.space,
|
||||
id: groupResolution.id,
|
||||
key: sessionKey,
|
||||
|
||||
@@ -55,7 +55,7 @@ describe("sessions", () => {
|
||||
expect(
|
||||
buildGroupDisplayName({
|
||||
provider: "discord",
|
||||
room: "#general",
|
||||
groupChannel: "#general",
|
||||
space: "friends-of-clawd",
|
||||
id: "123",
|
||||
key: "discord:group:123",
|
||||
|
||||
@@ -22,26 +22,26 @@ function shortenGroupId(value?: string) {
|
||||
export function buildGroupDisplayName(params: {
|
||||
provider?: string;
|
||||
subject?: string;
|
||||
room?: string;
|
||||
groupChannel?: string;
|
||||
space?: string;
|
||||
id?: string;
|
||||
key: string;
|
||||
}) {
|
||||
const providerKey = (params.provider?.trim().toLowerCase() || "group").trim();
|
||||
const room = params.room?.trim();
|
||||
const groupChannel = params.groupChannel?.trim();
|
||||
const space = params.space?.trim();
|
||||
const subject = params.subject?.trim();
|
||||
const detail =
|
||||
(room && space
|
||||
? `${space}${room.startsWith("#") ? "" : "#"}${room}`
|
||||
: room || subject || space || "") || "";
|
||||
(groupChannel && space
|
||||
? `${space}${groupChannel.startsWith("#") ? "" : "#"}${groupChannel}`
|
||||
: groupChannel || subject || space || "") || "";
|
||||
const fallbackId = params.id?.trim() || params.key;
|
||||
const rawLabel = detail || fallbackId;
|
||||
let token = normalizeGroupLabel(rawLabel);
|
||||
if (!token) {
|
||||
token = normalizeGroupLabel(shortenGroupId(rawLabel));
|
||||
}
|
||||
if (!params.room && token.startsWith("#")) {
|
||||
if (!params.groupChannel && token.startsWith("#")) {
|
||||
token = token.replace(/^#+/, "");
|
||||
}
|
||||
if (token && !/^[@#]/.test(token) && !token.startsWith("g-") && !token.includes("#")) {
|
||||
|
||||
@@ -130,6 +130,14 @@ export function loadSessionStore(
|
||||
rec.lastChannel = rec.lastProvider;
|
||||
delete rec.lastProvider;
|
||||
}
|
||||
|
||||
// Best-effort migration: legacy `room` field → `groupChannel` (keep value, prune old key).
|
||||
if (typeof rec.groupChannel !== "string" && typeof rec.room === "string") {
|
||||
rec.groupChannel = rec.room;
|
||||
delete rec.room;
|
||||
} else if ("room" in rec) {
|
||||
delete rec.room;
|
||||
}
|
||||
}
|
||||
|
||||
// Cache the result if caching is enabled
|
||||
|
||||
@@ -67,7 +67,7 @@ export type SessionEntry = {
|
||||
channel?: string;
|
||||
groupId?: string;
|
||||
subject?: string;
|
||||
room?: string;
|
||||
groupChannel?: string;
|
||||
space?: string;
|
||||
deliveryContext?: DeliveryContext;
|
||||
lastChannel?: SessionChannelId;
|
||||
|
||||
@@ -209,7 +209,7 @@ export const handleSessionsBridgeMethods: BridgeMethodHandler = async (
|
||||
chatType: entry?.chatType,
|
||||
channel: entry?.channel,
|
||||
subject: entry?.subject,
|
||||
room: entry?.room,
|
||||
groupChannel: entry?.groupChannel,
|
||||
space: entry?.space,
|
||||
lastChannel: entry?.lastChannel,
|
||||
lastTo: entry?.lastTo,
|
||||
|
||||
@@ -378,7 +378,7 @@ export function listSessionsFromStore(params: {
|
||||
const parsed = parseGroupKey(key);
|
||||
const channel = entry?.channel ?? parsed?.channel;
|
||||
const subject = entry?.subject;
|
||||
const room = entry?.room;
|
||||
const groupChannel = entry?.groupChannel;
|
||||
const space = entry?.space;
|
||||
const id = parsed?.id;
|
||||
const displayName =
|
||||
@@ -387,7 +387,7 @@ export function listSessionsFromStore(params: {
|
||||
? buildGroupDisplayName({
|
||||
provider: channel,
|
||||
subject,
|
||||
room,
|
||||
groupChannel,
|
||||
space,
|
||||
id,
|
||||
key,
|
||||
@@ -401,7 +401,7 @@ export function listSessionsFromStore(params: {
|
||||
displayName,
|
||||
channel,
|
||||
subject,
|
||||
room,
|
||||
groupChannel,
|
||||
space,
|
||||
chatType: entry?.chatType,
|
||||
updatedAt,
|
||||
|
||||
@@ -15,7 +15,7 @@ export type GatewaySessionRow = {
|
||||
displayName?: string;
|
||||
channel?: string;
|
||||
subject?: string;
|
||||
room?: string;
|
||||
groupChannel?: string;
|
||||
space?: string;
|
||||
chatType?: NormalizedChatType;
|
||||
updatedAt: number | null;
|
||||
|
||||
@@ -61,7 +61,7 @@ function normalizeQuery(value: string): string {
|
||||
|
||||
function stripTargetPrefixes(value: string): string {
|
||||
return value
|
||||
.replace(/^(channel|group|user):/i, "")
|
||||
.replace(/^(channel|user):/i, "")
|
||||
.replace(/^[@#]/, "")
|
||||
.trim();
|
||||
}
|
||||
@@ -88,7 +88,7 @@ export function formatTargetDisplay(params: {
|
||||
params.kind ??
|
||||
(lowered.startsWith("user:")
|
||||
? "user"
|
||||
: lowered.startsWith("channel:") || lowered.startsWith("group:")
|
||||
: lowered.startsWith("channel:")
|
||||
? "group"
|
||||
: undefined);
|
||||
|
||||
@@ -103,8 +103,8 @@ export function formatTargetDisplay(params: {
|
||||
if (trimmedTarget.startsWith("#") || trimmedTarget.startsWith("@")) return trimmedTarget;
|
||||
|
||||
const withoutPrefix = trimmedTarget.replace(/^telegram:/i, "");
|
||||
if (/^(channel|group):/i.test(withoutPrefix)) {
|
||||
return `#${withoutPrefix.replace(/^(channel|group):/i, "")}`;
|
||||
if (/^channel:/i.test(withoutPrefix)) {
|
||||
return `#${withoutPrefix.replace(/^channel:/i, "")}`;
|
||||
}
|
||||
if (/^user:/i.test(withoutPrefix)) {
|
||||
return `@${withoutPrefix.replace(/^user:/i, "")}`;
|
||||
@@ -126,7 +126,7 @@ function detectTargetKind(raw: string, preferred?: TargetResolveKind): TargetRes
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return "group";
|
||||
if (trimmed.startsWith("@") || /^<@!?/.test(trimmed) || /^user:/i.test(trimmed)) return "user";
|
||||
if (trimmed.startsWith("#") || /^channel:/i.test(trimmed) || /^group:/i.test(trimmed)) {
|
||||
if (trimmed.startsWith("#") || /^channel:/i.test(trimmed)) {
|
||||
return "group";
|
||||
}
|
||||
return "group";
|
||||
|
||||
@@ -131,7 +131,13 @@ function normalizeSessionEntry(entry: SessionEntryLike): SessionEntry | null {
|
||||
typeof entry.updatedAt === "number" && Number.isFinite(entry.updatedAt)
|
||||
? entry.updatedAt
|
||||
: Date.now();
|
||||
return { ...(entry as unknown as SessionEntry), sessionId, updatedAt };
|
||||
const normalized = { ...(entry as unknown as SessionEntry), sessionId, updatedAt };
|
||||
const rec = normalized as unknown as Record<string, unknown>;
|
||||
if (typeof rec.groupChannel !== "string" && typeof rec.room === "string") {
|
||||
rec.groupChannel = rec.room;
|
||||
}
|
||||
delete rec.room;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function emptyDirOrMissing(dir: string): boolean {
|
||||
|
||||
@@ -21,7 +21,6 @@ describe("resolveTelegramTargetChatType", () => {
|
||||
|
||||
it("handles tg/group prefixes and topic suffixes", () => {
|
||||
expect(resolveTelegramTargetChatType("tg:5232990709")).toBe("direct");
|
||||
expect(resolveTelegramTargetChatType("group:-123456789")).toBe("group");
|
||||
expect(resolveTelegramTargetChatType("telegram:group:-1001234567890")).toBe("group");
|
||||
expect(resolveTelegramTargetChatType("telegram:group:-1001234567890:topic:456")).toBe("group");
|
||||
expect(resolveTelegramTargetChatType("-1001234567890:456")).toBe("group");
|
||||
|
||||
@@ -11,6 +11,10 @@ describe("stripTelegramInternalPrefixes", () => {
|
||||
expect(stripTelegramInternalPrefixes("telegram:group:-100123")).toBe("-100123");
|
||||
});
|
||||
|
||||
it("does not strip group prefix without telegram prefix", () => {
|
||||
expect(stripTelegramInternalPrefixes("group:-100123")).toBe("group:-100123");
|
||||
});
|
||||
|
||||
it("is idempotent", () => {
|
||||
expect(stripTelegramInternalPrefixes("@mychannel")).toBe("@mychannel");
|
||||
});
|
||||
|
||||
@@ -5,8 +5,19 @@ export type TelegramTarget = {
|
||||
|
||||
export function stripTelegramInternalPrefixes(to: string): string {
|
||||
let trimmed = to.trim();
|
||||
let strippedTelegramPrefix = false;
|
||||
while (true) {
|
||||
const next = trimmed.replace(/^(telegram|tg|group):/i, "").trim();
|
||||
const next = (() => {
|
||||
if (/^(telegram|tg):/i.test(trimmed)) {
|
||||
strippedTelegramPrefix = true;
|
||||
return trimmed.replace(/^(telegram|tg):/i, "").trim();
|
||||
}
|
||||
// Legacy internal form: `telegram:group:<id>` (still emitted by session keys).
|
||||
if (strippedTelegramPrefix && /^group:/i.test(trimmed)) {
|
||||
return trimmed.replace(/^group:/i, "").trim();
|
||||
}
|
||||
return trimmed;
|
||||
})();
|
||||
if (next === trimmed) return trimmed;
|
||||
trimmed = next;
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ export type GatewaySessionList = {
|
||||
label?: string;
|
||||
displayName?: string;
|
||||
provider?: string;
|
||||
room?: string;
|
||||
groupChannel?: string;
|
||||
space?: string;
|
||||
subject?: string;
|
||||
chatType?: string;
|
||||
|
||||
Reference in New Issue
Block a user