openclaw-agent-id.ts
1 const VALID_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/i 2 const INVALID_CHARS_RE = /[^a-z0-9_-]+/g 3 const LEADING_DASH_RE = /^-+/ 4 const TRAILING_DASH_RE = /-+$/ 5 6 export function normalizeOpenClawAgentId(value: string | undefined | null): string { 7 const trimmed = (value ?? '').trim() 8 if (!trimmed) { 9 return 'main' 10 } 11 if (VALID_ID_RE.test(trimmed)) { 12 return trimmed.toLowerCase() 13 } 14 return ( 15 trimmed 16 .toLowerCase() 17 .replace(INVALID_CHARS_RE, '-') 18 .replace(LEADING_DASH_RE, '') 19 .replace(TRAILING_DASH_RE, '') 20 .slice(0, 64) 21 || 'main' 22 ) 23 } 24 25 export function buildOpenClawMainSessionKey(agentNameOrId: string | undefined | null): string | null { 26 const trimmed = (agentNameOrId ?? '').trim() 27 if (!trimmed) { 28 return null 29 } 30 return `agent:${normalizeOpenClawAgentId(trimmed)}:main` 31 }