mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 13:51:26 +00:00
Agents/Replies: scope done fallback to direct sessions
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user