@@ -84,6 +84,13 @@ import {
8484import { normalizeSessionDeliveryFields } from "../utils/delivery-context.shared.js" ;
8585import type { ModelCostConfig } from "../utils/usage-format.js" ;
8686import { 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" ;
8794import {
8895 resolveSessionStoreAgentId ,
8996 resolveSessionStoreKey ,
@@ -126,6 +133,11 @@ export {
126133} from "./session-utils.fs.js" ;
127134export type { ReadSessionMessagesAsyncOptions } from "./session-utils.fs.js" ;
128135export { canonicalizeSpawnedByForAgent , resolveSessionStoreKey } from "./session-store-key.js" ;
136+ export {
137+ deleteGatewaySessionStoreEntry ,
138+ syncGatewaySessionStoreAliases ,
139+ writeGatewaySessionStoreEntry ,
140+ } from "./session-store-aliases.js" ;
129141export 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 = / ^ a g e n t : ( [ ^ : ] + ) : ( .+ ) $ / . 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-
1021907export function migrateAndPruneGatewaySessionStoreKey ( params : {
1022908 cfg : OpenClawConfig ;
1023909 key : string ;
0 commit comments