UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

56 lines (55 loc) 2.33 kB
import { o as isBlockedObjectKey, p as normalizeLowercaseStringOrEmpty } from "./channel-doctor-helpers-CKvCIMDR.js"; import { configureFsSafeNative } from "@openclaw/fs-safe/config"; //#region src/infra/map-size.ts /** Prunes a Map in insertion order until it fits the requested maximum size. */ function pruneMapToMaxSize(map, maxSize) { if (Number.isNaN(maxSize) || maxSize === Number.POSITIVE_INFINITY) return; const limit = Math.max(0, Math.floor(maxSize)); if (limit <= 0) { map.clear(); return; } if (map.size <= limit) return; const keys = map.keys(); while (map.size > limit) { const oldest = keys.next(); if (oldest.done) break; map.delete(oldest.value); } } //#endregion //#region src/routing/account-id.ts const DEFAULT_ACCOUNT_ID = "default"; const VALID_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i; const INVALID_CHARS_RE = /[^a-z0-9_-]+/g; const LEADING_DASH_RE = /^-+/; const TRAILING_DASH_RE = /-+$/; const ACCOUNT_ID_CACHE_MAX = 512; const normalizedAccountIdCache = /* @__PURE__ */ new Map(); function canonicalizeAccountId(value) { const normalized = normalizeLowercaseStringOrEmpty(value); if (VALID_ID_RE.test(value)) return normalized; return normalized.replace(INVALID_CHARS_RE, "-").replace(LEADING_DASH_RE, "").replace(TRAILING_DASH_RE, "").slice(0, 64); } function normalizeCanonicalAccountId(value) { const canonical = canonicalizeAccountId(value); if (!canonical || isBlockedObjectKey(canonical)) return; return canonical; } function resolveCachedCanonicalAccountId(value) { if (normalizedAccountIdCache.has(value)) return normalizedAccountIdCache.get(value); const normalized = normalizeCanonicalAccountId(value); normalizedAccountIdCache.set(value, normalized); pruneMapToMaxSize(normalizedAccountIdCache, ACCOUNT_ID_CACHE_MAX); return normalized; } function normalizeAccountId(value) { const trimmed = (value ?? "").trim(); if (!trimmed) return DEFAULT_ACCOUNT_ID; return resolveCachedCanonicalAccountId(trimmed) ?? "default"; } //#endregion //#region src/infra/fs-safe-defaults.ts if (!Object.keys(process.env).some((key) => /^(?:OPENCLAW_)?FS_SAFE_(?:NATIVE|PYTHON)_MODE$/u.test(process.platform === "win32" ? key.toUpperCase() : key))) configureFsSafeNative({ mode: "off" }); //#endregion export { normalizeAccountId as n, pruneMapToMaxSize as r, DEFAULT_ACCOUNT_ID as t };