11// Discord plugin module implements components registry behavior.
2- import { resolveGlobalMap } from "openclaw/plugin-sdk/global-singleton" ;
32import {
43 asDateTimestampMs ,
54 isFutureDateTimestampMs ,
65 resolveDateTimestampMs ,
76 resolveExpiresAtMsFromDurationMs ,
87} from "openclaw/plugin-sdk/number-runtime" ;
98import { uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime" ;
9+ import {
10+ discordComponentRegistryState ,
11+ type DiscordRegistryStore ,
12+ } from "./components-registry-state.js" ;
1013import type { DiscordComponentEntry , DiscordModalEntry } from "./components.js" ;
1114import { getOptionalDiscordRuntime } from "./runtime.js" ;
1215
@@ -15,41 +18,17 @@ const PERSISTENT_COMPONENT_NAMESPACE = "discord.components";
1518const PERSISTENT_MODAL_NAMESPACE = "discord.modals" ;
1619const PERSISTENT_COMPONENT_MAX_ENTRIES = 500 ;
1720const PERSISTENT_MODAL_MAX_ENTRIES = 500 ;
18- const DISCORD_COMPONENT_ENTRIES_KEY = Symbol . for ( "openclaw.discord.componentEntries" ) ;
19- const DISCORD_MODAL_ENTRIES_KEY = Symbol . for ( "openclaw.discord.modalEntries" ) ;
20-
2121type PersistedDiscordRegistryEntry < T extends { id : string } > = {
2222 version : 1 ;
2323 entry : T ;
2424} ;
2525
26- type DiscordPersistentStore < T > = {
27- register ( key : string , value : T , opts ?: { ttlMs ?: number } ) : Promise < void > ;
28- lookup ( key : string ) : Promise < T | undefined > ;
29- consume ( key : string ) : Promise < T | undefined > ;
30- delete ( key : string ) : Promise < boolean > ;
31- } ;
32-
33- type DiscordRegistryStore < T extends { id : string } > = DiscordPersistentStore <
34- PersistedDiscordRegistryEntry < T >
35- > ;
36-
37- let componentEntries : Map < string , DiscordComponentEntry > | undefined ;
38- let modalEntries : Map < string , DiscordModalEntry > | undefined ;
39- let persistentComponentStore : DiscordRegistryStore < DiscordComponentEntry > | undefined ;
40- let persistentModalStore : DiscordRegistryStore < DiscordModalEntry > | undefined ;
41- let persistentRegistryDisabled = false ;
42-
4326function getComponentEntries ( ) : Map < string , DiscordComponentEntry > {
44- componentEntries ??= resolveGlobalMap < string , DiscordComponentEntry > (
45- DISCORD_COMPONENT_ENTRIES_KEY ,
46- ) ;
47- return componentEntries ;
27+ return discordComponentRegistryState . componentEntries ;
4828}
4929
5030function getModalEntries ( ) : Map < string , DiscordModalEntry > {
51- modalEntries ??= resolveGlobalMap < string , DiscordModalEntry > ( DISCORD_MODAL_ENTRIES_KEY ) ;
52- return modalEntries ;
31+ return discordComponentRegistryState . modalEntries ;
5332}
5433
5534function reportPersistentComponentRegistryError ( error : unknown ) : void {
@@ -111,58 +90,58 @@ function formatRegistryErrorValue(value: unknown): string {
11190}
11291
11392function disablePersistentComponentRegistry ( error : unknown ) : void {
114- persistentRegistryDisabled = true ;
115- persistentComponentStore = undefined ;
116- persistentModalStore = undefined ;
93+ discordComponentRegistryState . persistentRegistryDisabled = true ;
94+ discordComponentRegistryState . persistentComponentStore = undefined ;
95+ discordComponentRegistryState . persistentModalStore = undefined ;
11796 reportPersistentComponentRegistryError ( error ) ;
11897}
11998
12099function getPersistentComponentStore ( ) : DiscordRegistryStore < DiscordComponentEntry > | undefined {
121- if ( persistentRegistryDisabled ) {
100+ if ( discordComponentRegistryState . persistentRegistryDisabled ) {
122101 return undefined ;
123102 }
124- if ( persistentComponentStore ) {
125- return persistentComponentStore ;
103+ if ( discordComponentRegistryState . persistentComponentStore ) {
104+ return discordComponentRegistryState . persistentComponentStore ;
126105 }
127106 const runtime = getOptionalDiscordRuntime ( ) ;
128107 if ( ! runtime ) {
129108 return undefined ;
130109 }
131110 try {
132- persistentComponentStore = runtime . state . openKeyedStore <
111+ discordComponentRegistryState . persistentComponentStore = runtime . state . openKeyedStore <
133112 PersistedDiscordRegistryEntry < DiscordComponentEntry >
134113 > ( {
135114 namespace : PERSISTENT_COMPONENT_NAMESPACE ,
136115 maxEntries : PERSISTENT_COMPONENT_MAX_ENTRIES ,
137116 defaultTtlMs : DEFAULT_COMPONENT_TTL_MS ,
138117 } ) ;
139- return persistentComponentStore ;
118+ return discordComponentRegistryState . persistentComponentStore ;
140119 } catch ( error ) {
141120 disablePersistentComponentRegistry ( error ) ;
142121 return undefined ;
143122 }
144123}
145124
146125function getPersistentModalStore ( ) : DiscordRegistryStore < DiscordModalEntry > | undefined {
147- if ( persistentRegistryDisabled ) {
126+ if ( discordComponentRegistryState . persistentRegistryDisabled ) {
148127 return undefined ;
149128 }
150- if ( persistentModalStore ) {
151- return persistentModalStore ;
129+ if ( discordComponentRegistryState . persistentModalStore ) {
130+ return discordComponentRegistryState . persistentModalStore ;
152131 }
153132 const runtime = getOptionalDiscordRuntime ( ) ;
154133 if ( ! runtime ) {
155134 return undefined ;
156135 }
157136 try {
158- persistentModalStore = runtime . state . openKeyedStore <
137+ discordComponentRegistryState . persistentModalStore = runtime . state . openKeyedStore <
159138 PersistedDiscordRegistryEntry < DiscordModalEntry >
160139 > ( {
161140 namespace : PERSISTENT_MODAL_NAMESPACE ,
162141 maxEntries : PERSISTENT_MODAL_MAX_ENTRIES ,
163142 defaultTtlMs : DEFAULT_COMPONENT_TTL_MS ,
164143 } ) ;
165- return persistentModalStore ;
144+ return discordComponentRegistryState . persistentModalStore ;
166145 } catch ( error ) {
167146 disablePersistentComponentRegistry ( error ) ;
168147 return undefined ;
@@ -370,7 +349,7 @@ export function registerDiscordComponentEntries(params: {
370349 } ) ;
371350}
372351
373- export function resolveDiscordComponentEntry ( params : {
352+ function resolveDiscordComponentEntry ( params : {
374353 id : string ;
375354 consume ?: boolean ;
376355} ) : DiscordComponentEntry | null {
@@ -402,7 +381,7 @@ export async function resolveDiscordComponentEntryWithPersistence(params: {
402381 return persisted ;
403382}
404383
405- export function resolveDiscordModalEntry ( params : {
384+ function resolveDiscordModalEntry ( params : {
406385 id : string ;
407386 consume ?: boolean ;
408387} ) : DiscordModalEntry | null {
@@ -425,11 +404,3 @@ export async function resolveDiscordModalEntryWithPersistence(params: {
425404 openStore : getPersistentModalStore ,
426405 } ) ;
427406}
428-
429- export function clearDiscordComponentEntries ( ) : void {
430- getComponentEntries ( ) . clear ( ) ;
431- getModalEntries ( ) . clear ( ) ;
432- persistentComponentStore = undefined ;
433- persistentModalStore = undefined ;
434- persistentRegistryDisabled = false ;
435- }
0 commit comments