mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 11:31:37 +00:00
refactor(src): split oversized modules
This commit is contained in:
44
src/config/sessions/session-key.ts
Normal file
44
src/config/sessions/session-key.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { MsgContext } from "../../auto-reply/templating.js";
|
||||
import {
|
||||
buildAgentMainSessionKey,
|
||||
DEFAULT_AGENT_ID,
|
||||
normalizeMainKey,
|
||||
} from "../../routing/session-key.js";
|
||||
import { normalizeE164 } from "../../utils.js";
|
||||
import { resolveGroupSessionKey } from "./group.js";
|
||||
import type { SessionScope } from "./types.js";
|
||||
|
||||
// Decide which session bucket to use (per-sender vs global).
|
||||
export function deriveSessionKey(scope: SessionScope, ctx: MsgContext) {
|
||||
if (scope === "global") return "global";
|
||||
const resolvedGroup = resolveGroupSessionKey(ctx);
|
||||
if (resolvedGroup) return resolvedGroup.key;
|
||||
const from = ctx.From ? normalizeE164(ctx.From) : "";
|
||||
return from || "unknown";
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the session key with a canonical direct-chat bucket (default: "main").
|
||||
* All non-group direct chats collapse to this bucket; groups stay isolated.
|
||||
*/
|
||||
export function resolveSessionKey(
|
||||
scope: SessionScope,
|
||||
ctx: MsgContext,
|
||||
mainKey?: string,
|
||||
) {
|
||||
const explicit = ctx.SessionKey?.trim();
|
||||
if (explicit) return explicit;
|
||||
const raw = deriveSessionKey(scope, ctx);
|
||||
if (scope === "global") return raw;
|
||||
const canonicalMainKey = normalizeMainKey(mainKey);
|
||||
const canonical = buildAgentMainSessionKey({
|
||||
agentId: DEFAULT_AGENT_ID,
|
||||
mainKey: canonicalMainKey,
|
||||
});
|
||||
const isGroup =
|
||||
raw.startsWith("group:") ||
|
||||
raw.includes(":group:") ||
|
||||
raw.includes(":channel:");
|
||||
if (!isGroup) return canonical;
|
||||
return `agent:${DEFAULT_AGENT_ID}:${raw}`;
|
||||
}
|
||||
Reference in New Issue
Block a user