|
| 1 | +import type { SessionEntry } from "../config/sessions.js"; |
| 2 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 3 | +import { normalizeMainKey, parseAgentSessionKey } from "../routing/session-key.js"; |
| 4 | +import { normalizeSessionKeyPreservingOpaquePeerIds } from "../sessions/session-key-utils.js"; |
| 5 | +import { |
| 6 | + normalizeLowercaseStringOrEmpty, |
| 7 | + normalizeOptionalString, |
| 8 | +} from "../shared/string-coerce.js"; |
| 9 | + |
| 10 | +function resolvePreservedRawExternalStoreKeyCandidate(params: { |
| 11 | + cfg: OpenClawConfig; |
| 12 | + key: string; |
| 13 | + canonicalKey: string; |
| 14 | +}): string | undefined { |
| 15 | + const key = normalizeSessionKeyPreservingOpaquePeerIds(params.key); |
| 16 | + if (!key || key === params.canonicalKey || !key.includes(":")) { |
| 17 | + return undefined; |
| 18 | + } |
| 19 | + const lowered = normalizeLowercaseStringOrEmpty(key); |
| 20 | + const mainKey = normalizeMainKey(params.cfg.session?.mainKey); |
| 21 | + if ( |
| 22 | + lowered === "global" || |
| 23 | + lowered === "unknown" || |
| 24 | + lowered === "main" || |
| 25 | + lowered === mainKey || |
| 26 | + lowered.startsWith("agent:") || |
| 27 | + parseAgentSessionKey(key) |
| 28 | + ) { |
| 29 | + return undefined; |
| 30 | + } |
| 31 | + return key; |
| 32 | +} |
| 33 | + |
| 34 | +function resolveAgentSessionRestPreservingSeparators(key: string): string | undefined { |
| 35 | + const normalized = normalizeSessionKeyPreservingOpaquePeerIds(key); |
| 36 | + const match = /^agent:([^:]+):(.+)$/.exec(normalized); |
| 37 | + const rest = match?.[2]; |
| 38 | + return rest ? rest : undefined; |
| 39 | +} |
| 40 | + |
| 41 | +export function resolvePreservedRawExternalStoreKey(params: { |
| 42 | + cfg: OpenClawConfig; |
| 43 | + key: string; |
| 44 | + canonicalKey: string; |
| 45 | +}): { key: string; preserveMissingAlias: boolean } | undefined { |
| 46 | + const directKey = resolvePreservedRawExternalStoreKeyCandidate(params); |
| 47 | + if (directKey) { |
| 48 | + return { key: directKey, preserveMissingAlias: true }; |
| 49 | + } |
| 50 | + const canonicalRest = resolveAgentSessionRestPreservingSeparators(params.canonicalKey); |
| 51 | + if (!canonicalRest) { |
| 52 | + return undefined; |
| 53 | + } |
| 54 | + const canonicalRawKey = resolvePreservedRawExternalStoreKeyCandidate({ |
| 55 | + cfg: params.cfg, |
| 56 | + key: canonicalRest, |
| 57 | + canonicalKey: params.canonicalKey, |
| 58 | + }); |
| 59 | + return canonicalRawKey ? { key: canonicalRawKey, preserveMissingAlias: false } : undefined; |
| 60 | +} |
| 61 | + |
| 62 | +export function normalizeSessionStoreKeys(keys: Iterable<string | undefined>): string[] { |
| 63 | + const normalized = new Set<string>(); |
| 64 | + for (const key of keys) { |
| 65 | + const trimmed = normalizeOptionalString(key ?? "") ?? ""; |
| 66 | + if (trimmed) { |
| 67 | + normalized.add(trimmed); |
| 68 | + } |
| 69 | + } |
| 70 | + return Array.from(normalized); |
| 71 | +} |
| 72 | + |
| 73 | +export function resolvePreservedRawExternalAliasKeys(params: { |
| 74 | + preservedRawKey: string | undefined; |
| 75 | + preserveMissingAlias: boolean; |
| 76 | + storeKeys: Iterable<string>; |
| 77 | +}): string[] { |
| 78 | + if (!params.preservedRawKey) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + const aliases = Array.from(params.storeKeys).filter( |
| 82 | + (key) => normalizeSessionKeyPreservingOpaquePeerIds(key) === params.preservedRawKey, |
| 83 | + ); |
| 84 | + if (!params.preserveMissingAlias && aliases.length === 0) { |
| 85 | + return []; |
| 86 | + } |
| 87 | + aliases.push(params.preservedRawKey); |
| 88 | + return normalizeSessionStoreKeys(aliases); |
| 89 | +} |
| 90 | + |
| 91 | +export function resolveGatewaySessionStorePreservedAliasKeys(params: { |
| 92 | + cfg: OpenClawConfig; |
| 93 | + key: string; |
| 94 | + canonicalKey: string; |
| 95 | + storeKeys: Iterable<string>; |
| 96 | +}): string[] { |
| 97 | + const preservedRaw = resolvePreservedRawExternalStoreKey({ |
| 98 | + cfg: params.cfg, |
| 99 | + key: params.key, |
| 100 | + canonicalKey: params.canonicalKey, |
| 101 | + }); |
| 102 | + if (!preservedRaw) { |
| 103 | + return []; |
| 104 | + } |
| 105 | + return resolvePreservedRawExternalAliasKeys({ |
| 106 | + preservedRawKey: preservedRaw.key, |
| 107 | + preserveMissingAlias: preservedRaw.preserveMissingAlias, |
| 108 | + storeKeys: params.storeKeys, |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +export function syncSessionStoreAliases(params: { |
| 113 | + store: Record<string, SessionEntry>; |
| 114 | + canonicalKey: string; |
| 115 | + entry: SessionEntry; |
| 116 | + aliasKeys: readonly string[]; |
| 117 | +}): void { |
| 118 | + for (const aliasKey of params.aliasKeys) { |
| 119 | + if (aliasKey !== params.canonicalKey) { |
| 120 | + params.store[aliasKey] = params.entry; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export function deleteSessionStoreAliases(params: { |
| 126 | + store: Record<string, SessionEntry>; |
| 127 | + canonicalKey: string; |
| 128 | + aliasKeys: readonly string[]; |
| 129 | +}): void { |
| 130 | + for (const aliasKey of params.aliasKeys) { |
| 131 | + if (aliasKey !== params.canonicalKey) { |
| 132 | + delete params.store[aliasKey]; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments