UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

79 lines (78 loc) 4.16 kB
import { s as normalizeNullableString } from "./string-coerce-CIXf7egm.js"; import { n as normalizeAgentId } from "./agent-id-CeT3w4ap.js"; import { a as normalizeControlUiBasePath, i as isReservedSessionRest, n as DEFAULT_MAIN_KEY, o as parseShortSessionRef, r as controlUiSessionSlug } from "./share-DPKiewIx.js"; //#region packages/session-url-contract/src/index.ts const SESSION_UUID_SUFFIX_RE = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/iu; const SHORT_SESSION_ID_RE = /^[0-9a-f]{8,32}$/iu; function agentSessionKeyParts(sessionKey) { const parts = sessionKey.split(":"); if (parts.length < 3 || parts[0]?.toLowerCase() !== "agent") return null; const agentId = normalizeNullableString(parts[1]); const restSegments = parts.slice(2); if (!agentId || restSegments.some((segment) => !segment)) return null; return { agentId: normalizeAgentId(agentId), rest: restSegments.join(":") }; } function encodePathSegment(segment) { if (segment === ".") return "~dot"; if (segment === "..") return "~dotdot"; const encoded = encodeURIComponent(segment).replaceAll(".", "%2E"); return encoded.startsWith("~") ? `~${encoded}` : encoded; } function buildControlUiSessionPath(params) { const rawKey = normalizeNullableString(params.sessionKey); const parsed = rawKey ? agentSessionKeyParts(rawKey) : null; const fallbackAgentId = normalizeNullableString(params.fallbackAgentId); const agentId = parsed?.agentId ?? (fallbackAgentId ? normalizeAgentId(fallbackAgentId) : null); if (!rawKey || !agentId || !parsed && rawKey.toLowerCase().startsWith("agent:")) return null; const namespace = `${normalizeControlUiBasePath(params.basePath)}/${params.namespace}`; const encodedAgentId = encodePathSegment(agentId); const rest = parsed?.rest ?? rawKey; const normalizedRest = rest.toLowerCase(); const mainKey = normalizeNullableString(params.mainKey)?.toLowerCase() ?? "main"; if (!parsed && normalizedRest === "main" || normalizedRest === mainKey || normalizedRest === "global") return `${namespace}/${encodedAgentId}`; const segments = rest.split(":"); if (segments.some((segment) => !segment)) return null; if (params.exactKey) { const segment = segments[0] ?? ""; return segments.length === 1 && (isReservedSessionRest(segment, params.mainKey) || parseShortSessionRef(segment)) ? `${namespace}/${encodedAgentId}/~key/${encodePathSegment(segment)}` : `${namespace}/${encodedAgentId}/${segments.map(encodePathSegment).join("/")}`; } const uuid = (parsed?.rest.match(SESSION_UUID_SUFFIX_RE)?.[1])?.toLowerCase().replaceAll("-", "") ?? null; if (uuid) { const requestedLength = params.shortIdLength ?? 8; let length = Math.min(uuid.length, Math.max(8, Math.floor(requestedLength))); const slug = controlUiSessionSlug(params.displayName); let sessionRef = `${slug ? `${slug}-` : ""}${uuid.slice(0, length)}`; while (length < uuid.length && isReservedSessionRest(sessionRef, params.mainKey)) { length += 1; sessionRef = `${slug ? `${slug}-` : ""}${uuid.slice(0, length)}`; } return isReservedSessionRest(sessionRef, params.mainKey) ? null : `${namespace}/${encodedAgentId}/${sessionRef}`; } if (segments.length === 1) { const segment = segments[0] ?? ""; if (!isReservedSessionRest(segment, params.mainKey) && parseShortSessionRef(segment)) return `${namespace}/${encodedAgentId}/~key/${encodePathSegment(segment)}`; } return `${namespace}/${encodedAgentId}/${segments.map(encodePathSegment).join("/")}`; } function buildControlUiCatalogSessionUrl(params) { const catalog = normalizeNullableString(params.catalog); const host = normalizeNullableString(params.host); const thread = normalizeNullableString(params.thread); const path = buildControlUiSessionPath({ namespace: params.namespace, sessionKey: DEFAULT_MAIN_KEY, fallbackAgentId: params.agentId, basePath: params.basePath }); if (!path || !catalog || !host || !thread) return null; return `${path}?${new URLSearchParams({ catalog, host, thread }).toString()}`; } //#endregion export { buildControlUiSessionPath as i, SHORT_SESSION_ID_RE as n, buildControlUiCatalogSessionUrl as r, SESSION_UUID_SUFFIX_RE as t };