File tree Expand file tree Collapse file tree
apps/web/src/components/chat Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,4 +19,13 @@ describe("model picker item keys", () => {
1919 slug : "codex" ,
2020 } ) ;
2121 } ) ;
22+
23+ it ( "round-trips arbitrary strings without throwing" , ( ) => {
24+ const instanceId = ProviderInstanceId . make ( "custom" ) ;
25+ const slug = "model:\udfff" ;
26+
27+ const key = modelPickerModelKey ( instanceId , slug ) ;
28+
29+ expect ( parseModelPickerModelKey ( key ) ) . toEqual ( { instanceId, slug } ) ;
30+ } ) ;
2231} ) ;
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ const MODEL_KEY_PREFIX = "model:";
44const LEGACY_SECTION_KEY_PREFIX = "legacy-models:" ;
55
66export function modelPickerModelKey ( instanceId : ProviderInstanceId , slug : string ) : string {
7- return `${ MODEL_KEY_PREFIX } ${ encodeURIComponent ( instanceId ) } :${ encodeURIComponent ( slug ) } ` ;
7+ return `${ MODEL_KEY_PREFIX } ${ instanceId . length } :${ instanceId } ${ slug } ` ;
88}
99
1010export function parseModelPickerModelKey (
@@ -18,14 +18,22 @@ export function parseModelPickerModelKey(
1818 if ( separatorIndex === - 1 ) {
1919 return null ;
2020 }
21- try {
22- return {
23- instanceId : decodeURIComponent ( encoded . slice ( 0 , separatorIndex ) ) as ProviderInstanceId ,
24- slug : decodeURIComponent ( encoded . slice ( separatorIndex + 1 ) ) ,
25- } ;
26- } catch {
21+
22+ const instanceIdLengthText = encoded . slice ( 0 , separatorIndex ) ;
23+ if ( ! / ^ \d + $ / . test ( instanceIdLengthText ) ) {
2724 return null ;
2825 }
26+
27+ const instanceIdLength = Number ( instanceIdLengthText ) ;
28+ const value = encoded . slice ( separatorIndex + 1 ) ;
29+ if ( ! Number . isSafeInteger ( instanceIdLength ) || instanceIdLength > value . length ) {
30+ return null ;
31+ }
32+
33+ return {
34+ instanceId : value . slice ( 0 , instanceIdLength ) as ProviderInstanceId ,
35+ slug : value . slice ( instanceIdLength ) ,
36+ } ;
2937}
3038
3139export function modelPickerLegacySectionKey ( instanceId : ProviderInstanceId ) : string {
You can’t perform that action at this time.
0 commit comments