Skip to content

Commit 1cde6d4

Browse files
committed
fix(gateway): sync plugin hook aliases
1 parent 66e484d commit 1cde6d4

5 files changed

Lines changed: 445 additions & 142 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
return resolvePreservedRawExternalAliasKeys({
103+
preservedRawKey: preservedRaw?.key,
104+
preserveMissingAlias: preservedRaw?.preserveMissingAlias === true,
105+
storeKeys: params.storeKeys,
106+
});
107+
}
108+
109+
export function syncGatewaySessionStoreAliases(params: {
110+
store: Record<string, SessionEntry>;
111+
primaryKey: string;
112+
aliasKeys: Iterable<string>;
113+
}) {
114+
const entry = params.store[params.primaryKey];
115+
if (!entry) {
116+
return;
117+
}
118+
for (const aliasKey of normalizeSessionStoreKeys(params.aliasKeys)) {
119+
if (aliasKey !== params.primaryKey) {
120+
params.store[aliasKey] = entry;
121+
}
122+
}
123+
}
124+
125+
export function writeGatewaySessionStoreEntry(params: {
126+
store: Record<string, SessionEntry>;
127+
primaryKey: string;
128+
aliasKeys: Iterable<string>;
129+
entry: SessionEntry;
130+
}) {
131+
params.store[params.primaryKey] = params.entry;
132+
syncGatewaySessionStoreAliases({
133+
store: params.store,
134+
primaryKey: params.primaryKey,
135+
aliasKeys: params.aliasKeys,
136+
});
137+
}
138+
139+
export function deleteGatewaySessionStoreEntry(params: {
140+
store: Record<string, SessionEntry>;
141+
primaryKey: string;
142+
aliasKeys: Iterable<string>;
143+
}): boolean {
144+
let hadEntry = false;
145+
for (const key of normalizeSessionStoreKeys([params.primaryKey, ...params.aliasKeys])) {
146+
if (params.store[key]) {
147+
hadEntry = true;
148+
}
149+
delete params.store[key];
150+
}
151+
return hadEntry;
152+
}

‎src/gateway/session-utils.ts‎

Lines changed: 12 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ import {
8484
import { normalizeSessionDeliveryFields } from "../utils/delivery-context.shared.js";
8585
import type { ModelCostConfig } from "../utils/usage-format.js";
8686
import { estimateUsageCost, resolveModelCostConfig } from "../utils/usage-format.js";
87+
import {
88+
deleteGatewaySessionStoreEntry,
89+
resolvePreservedRawExternalAliasKeys,
90+
resolvePreservedRawExternalStoreKey,
91+
syncGatewaySessionStoreAliases,
92+
writeGatewaySessionStoreEntry,
93+
} from "./session-store-aliases.js";
8794
import {
8895
resolveSessionStoreAgentId,
8996
resolveSessionStoreKey,
@@ -126,6 +133,11 @@ export {
126133
} from "./session-utils.fs.js";
127134
export type { ReadSessionMessagesAsyncOptions } from "./session-utils.fs.js";
128135
export { canonicalizeSpawnedByForAgent, resolveSessionStoreKey } from "./session-store-key.js";
136+
export {
137+
deleteGatewaySessionStoreEntry,
138+
syncGatewaySessionStoreAliases,
139+
writeGatewaySessionStoreEntry,
140+
} from "./session-store-aliases.js";
129141
export type {
130142
GatewayAgentRow,
131143
GatewaySessionRow,
@@ -892,132 +904,6 @@ export function pruneLegacyStoreKeys(params: {
892904
}
893905
}
894906

895-
function resolvePreservedRawExternalStoreKeyCandidate(params: {
896-
cfg: OpenClawConfig;
897-
key: string;
898-
canonicalKey: string;
899-
}): string | undefined {
900-
const key = normalizeSessionKeyPreservingOpaquePeerIds(params.key);
901-
if (!key || key === params.canonicalKey || !key.includes(":")) {
902-
return undefined;
903-
}
904-
const lowered = normalizeLowercaseStringOrEmpty(key);
905-
const mainKey = normalizeMainKey(params.cfg.session?.mainKey);
906-
if (
907-
lowered === "global" ||
908-
lowered === "unknown" ||
909-
lowered === "main" ||
910-
lowered === mainKey ||
911-
lowered.startsWith("agent:") ||
912-
parseAgentSessionKey(key)
913-
) {
914-
return undefined;
915-
}
916-
return key;
917-
}
918-
919-
function resolveAgentSessionRestPreservingSeparators(key: string): string | undefined {
920-
const normalized = normalizeSessionKeyPreservingOpaquePeerIds(key);
921-
const match = /^agent:([^:]+):(.+)$/.exec(normalized);
922-
const rest = match?.[2];
923-
return rest ? rest : undefined;
924-
}
925-
926-
function resolvePreservedRawExternalStoreKey(params: {
927-
cfg: OpenClawConfig;
928-
key: string;
929-
canonicalKey: string;
930-
}): { key: string; preserveMissingAlias: boolean } | undefined {
931-
const directKey = resolvePreservedRawExternalStoreKeyCandidate(params);
932-
if (directKey) {
933-
return { key: directKey, preserveMissingAlias: true };
934-
}
935-
const canonicalRest = resolveAgentSessionRestPreservingSeparators(params.canonicalKey);
936-
if (!canonicalRest) {
937-
return undefined;
938-
}
939-
const canonicalRawKey = resolvePreservedRawExternalStoreKeyCandidate({
940-
cfg: params.cfg,
941-
key: canonicalRest,
942-
canonicalKey: params.canonicalKey,
943-
});
944-
return canonicalRawKey ? { key: canonicalRawKey, preserveMissingAlias: false } : undefined;
945-
}
946-
947-
function normalizeSessionStoreKeys(keys: Iterable<string | undefined>): string[] {
948-
const normalized = new Set<string>();
949-
for (const key of keys) {
950-
const trimmed = normalizeOptionalString(key ?? "") ?? "";
951-
if (trimmed) {
952-
normalized.add(trimmed);
953-
}
954-
}
955-
return Array.from(normalized);
956-
}
957-
958-
function resolvePreservedRawExternalAliasKeys(params: {
959-
preservedRawKey: string | undefined;
960-
preserveMissingAlias: boolean;
961-
storeKeys: Iterable<string>;
962-
}): string[] {
963-
if (!params.preservedRawKey) {
964-
return [];
965-
}
966-
const aliases = Array.from(params.storeKeys).filter(
967-
(key) => normalizeSessionKeyPreservingOpaquePeerIds(key) === params.preservedRawKey,
968-
);
969-
if (!params.preserveMissingAlias && aliases.length === 0) {
970-
return [];
971-
}
972-
aliases.push(params.preservedRawKey);
973-
return normalizeSessionStoreKeys(aliases);
974-
}
975-
976-
export function syncGatewaySessionStoreAliases(params: {
977-
store: Record<string, SessionEntry>;
978-
primaryKey: string;
979-
aliasKeys: Iterable<string>;
980-
}) {
981-
const entry = params.store[params.primaryKey];
982-
if (!entry) {
983-
return;
984-
}
985-
for (const aliasKey of normalizeSessionStoreKeys(params.aliasKeys)) {
986-
if (aliasKey !== params.primaryKey) {
987-
params.store[aliasKey] = entry;
988-
}
989-
}
990-
}
991-
992-
export function writeGatewaySessionStoreEntry(params: {
993-
store: Record<string, SessionEntry>;
994-
primaryKey: string;
995-
aliasKeys: Iterable<string>;
996-
entry: SessionEntry;
997-
}) {
998-
params.store[params.primaryKey] = params.entry;
999-
syncGatewaySessionStoreAliases({
1000-
store: params.store,
1001-
primaryKey: params.primaryKey,
1002-
aliasKeys: params.aliasKeys,
1003-
});
1004-
}
1005-
1006-
export function deleteGatewaySessionStoreEntry(params: {
1007-
store: Record<string, SessionEntry>;
1008-
primaryKey: string;
1009-
aliasKeys: Iterable<string>;
1010-
}): boolean {
1011-
let hadEntry = false;
1012-
for (const key of normalizeSessionStoreKeys([params.primaryKey, ...params.aliasKeys])) {
1013-
if (params.store[key]) {
1014-
hadEntry = true;
1015-
}
1016-
delete params.store[key];
1017-
}
1018-
return hadEntry;
1019-
}
1020-
1021907
export function migrateAndPruneGatewaySessionStoreKey(params: {
1022908
cfg: OpenClawConfig;
1023909
key: string;

0 commit comments

Comments
 (0)