Agents/Replies: scope done fallback to direct sessions

This commit is contained in:
Vignesh Natarajan
2026-02-22 13:30:16 -08:00
parent e4d67137db
commit d75b594e07
7 changed files with 114 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import { normalizeChatType } from "../channels/chat-type.js";
import type { OpenClawConfig } from "../config/config.js";
import type { SessionChatType, SessionEntry } from "../config/sessions.js";
import { deriveSessionChatType } from "./session-key-utils.js";
export type SessionSendPolicyDecision = "allow" | "deny";
@@ -45,17 +46,8 @@ function deriveChannelFromKey(key?: string) {
}
function deriveChatTypeFromKey(key?: string): SessionChatType | undefined {
const normalizedKey = stripAgentSessionKeyPrefix(key);
if (!normalizedKey) {
return undefined;
}
if (normalizedKey.includes(":group:")) {
return "group";
}
if (normalizedKey.includes(":channel:")) {
return "channel";
}
return undefined;
const chatType = deriveSessionChatType(key);
return chatType === "unknown" ? undefined : chatType;
}
export function resolveSendPolicy(params: {

View File

@@ -3,6 +3,8 @@ export type ParsedAgentSessionKey = {
rest: string;
};
export type SessionKeyChatType = "direct" | "group" | "channel" | "unknown";
export function parseAgentSessionKey(
sessionKey: string | undefined | null,
): ParsedAgentSessionKey | null {
@@ -25,6 +27,33 @@ export function parseAgentSessionKey(
return { agentId, rest };
}
/**
* Best-effort chat-type extraction from session keys across canonical and legacy formats.
*/
export function deriveSessionChatType(sessionKey: string | undefined | null): SessionKeyChatType {
const raw = (sessionKey ?? "").trim().toLowerCase();
if (!raw) {
return "unknown";
}
const scoped = parseAgentSessionKey(raw)?.rest ?? raw;
const tokens = new Set(scoped.split(":").filter(Boolean));
if (tokens.has("group")) {
return "group";
}
if (tokens.has("channel")) {
return "channel";
}
if (tokens.has("direct") || tokens.has("dm")) {
return "direct";
}
// Legacy Discord keys can be shaped like:
// discord:<accountId>:guild-<guildId>:channel-<channelId>
if (/^discord:(?:[^:]+:)?guild-[^:]+:channel-[^:]+$/.test(scoped)) {
return "channel";
}
return "unknown";
}
export function isCronRunSessionKey(sessionKey: string | undefined | null): boolean {
const parsed = parseAgentSessionKey(sessionKey);
if (!parsed) {