@@ -4,6 +4,7 @@ import { createLazyRuntimeModule } from "openclaw/plugin-sdk/lazy-runtime";
44import type { ModelsProviderData } from "openclaw/plugin-sdk/models-provider-runtime" ;
55import { parseStrictInteger , parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime" ;
66import { normalizeProviderId } from "openclaw/plugin-sdk/provider-model-shared" ;
7+ import { decodeCustomIdComponent , encodeCustomIdComponent } from "../custom-id-codec.js" ;
78import type { ComponentData } from "../internal/discord.js" ;
89
910export const DISCORD_MODEL_PICKER_CUSTOM_ID_KEY = "mdlpk" ;
@@ -110,18 +111,6 @@ const loadModelsProviderRuntime = createLazyRuntimeModule(
110111 ( ) => import ( "openclaw/plugin-sdk/models-provider-runtime" ) ,
111112) ;
112113
113- function encodeCustomIdValue ( value : string ) : string {
114- return encodeURIComponent ( value ) ;
115- }
116-
117- function decodeCustomIdValue ( value : string ) : string {
118- try {
119- return decodeURIComponent ( value ) ;
120- } catch {
121- return value ;
122- }
123- }
124-
125114function isValidCommandContext ( value : string ) : value is DiscordModelPickerCommandContext {
126115 return ( COMMAND_CONTEXTS as readonly string [ ] ) . includes ( value ) ;
127116}
@@ -236,18 +225,18 @@ export function buildDiscordModelPickerCustomId(params: {
236225 : undefined ;
237226
238227 const parts = [
239- `${ DISCORD_MODEL_PICKER_CUSTOM_ID_KEY } :c=${ encodeCustomIdValue ( params . command ) } ` ,
240- `a=${ encodeCustomIdValue ( params . action ) } ` ,
241- `v=${ encodeCustomIdValue ( params . view ) } ` ,
242- `u=${ encodeCustomIdValue ( userId ) } ` ,
228+ `${ DISCORD_MODEL_PICKER_CUSTOM_ID_KEY } :c=${ encodeCustomIdComponent ( params . command ) } ` ,
229+ `a=${ encodeCustomIdComponent ( params . action ) } ` ,
230+ `v=${ encodeCustomIdComponent ( params . view ) } ` ,
231+ `u=${ encodeCustomIdComponent ( userId ) } ` ,
243232 `g=${ String ( page ) } ` ,
244233 ] ;
245234 if ( normalizedProvider ) {
246- parts . push ( `p=${ encodeCustomIdValue ( normalizedProvider ) } ` ) ;
235+ parts . push ( `p=${ encodeCustomIdComponent ( normalizedProvider ) } ` ) ;
247236 }
248237 const runtime = params . runtime ?. trim ( ) ;
249238 if ( runtime ) {
250- parts . push ( `r=${ encodeCustomIdValue ( runtime ) } ` ) ;
239+ parts . push ( `r=${ encodeCustomIdComponent ( runtime ) } ` ) ;
251240 }
252241 const runtimeIndex =
253242 typeof params . runtimeIndex === "number" && Number . isFinite ( params . runtimeIndex )
@@ -267,11 +256,11 @@ export function buildDiscordModelPickerCustomId(params: {
267256 }
268257 const providerBucket = params . providerBucket ?. trim ( ) . toLowerCase ( ) ;
269258 if ( providerBucket ) {
270- parts . push ( `pb=${ encodeCustomIdValue ( providerBucket ) } ` ) ;
259+ parts . push ( `pb=${ encodeCustomIdComponent ( providerBucket ) } ` ) ;
271260 }
272261 const modelBucket = params . modelBucket ?. trim ( ) . toLowerCase ( ) ;
273262 if ( modelBucket ) {
274- parts . push ( `mb=${ encodeCustomIdValue ( modelBucket ) } ` ) ;
263+ parts . push ( `mb=${ encodeCustomIdComponent ( modelBucket ) } ` ) ;
275264 }
276265
277266 const customId = parts . join ( ";" ) ;
@@ -313,19 +302,19 @@ export function parseDiscordModelPickerData(data: ComponentData): DiscordModelPi
313302 return null ;
314303 }
315304
316- const command = decodeCustomIdValue ( coerceString ( data . c ?? data . cmd ) ) ;
317- const action = decodeCustomIdValue ( coerceString ( data . a ?? data . act ) ) ;
318- const view = decodeCustomIdValue ( coerceString ( data . v ?? data . view ) ) ;
319- const userId = decodeCustomIdValue ( coerceString ( data . u ) ) ;
320- const providerRaw = decodeCustomIdValue ( coerceString ( data . p ) ) ;
321- const runtimeRaw = decodeCustomIdValue ( coerceString ( data . r ) ) ;
305+ const command = decodeCustomIdComponent ( coerceString ( data . c ?? data . cmd ) ) ;
306+ const action = decodeCustomIdComponent ( coerceString ( data . a ?? data . act ) ) ;
307+ const view = decodeCustomIdComponent ( coerceString ( data . v ?? data . view ) ) ;
308+ const userId = decodeCustomIdComponent ( coerceString ( data . u ) ) ;
309+ const providerRaw = decodeCustomIdComponent ( coerceString ( data . p ) ) ;
310+ const runtimeRaw = decodeCustomIdComponent ( coerceString ( data . r ) ) ;
322311 const runtimeIndex = parseRawPositiveInt ( data . ri ) ;
323312 const page = parseRawPage ( data . g ?? data . pg ) ;
324313 const providerPage = parseRawPositiveInt ( data . pp ) ;
325314 const modelIndex = parseRawPositiveInt ( data . mi ) ;
326315 const recentSlot = parseRawPositiveInt ( data . rs ) ;
327- const providerBucketRaw = decodeCustomIdValue ( coerceString ( data . pb ) ) . trim ( ) . toLowerCase ( ) ;
328- const modelBucketRaw = decodeCustomIdValue ( coerceString ( data . mb ) ) . trim ( ) . toLowerCase ( ) ;
316+ const providerBucketRaw = decodeCustomIdComponent ( coerceString ( data . pb ) ) . trim ( ) . toLowerCase ( ) ;
317+ const modelBucketRaw = decodeCustomIdComponent ( coerceString ( data . mb ) ) . trim ( ) . toLowerCase ( ) ;
329318
330319 if ( ! isValidCommandContext ( command ) || ! isValidPickerAction ( action ) || ! isValidPickerView ( view ) ) {
331320 return null ;
0 commit comments