File tree Expand file tree Collapse file tree 6 files changed +45
-8
lines changed
packages/openai-adapters/src/apis Expand file tree Collapse file tree 6 files changed +45
-8
lines changed Original file line number Diff line number Diff line change @@ -107,7 +107,14 @@ class Anthropic extends BaseLLM {
107
107
source : {
108
108
type : "base64" ,
109
109
media_type : getAnthropicMediaTypeFromDataUrl ( part . imageUrl . url ) ,
110
- data : part . imageUrl . url . split ( "," ) [ 1 ] ,
110
+ data : ( ( ) => {
111
+ const urlParts = part . imageUrl . url . split ( "," ) ;
112
+ if ( urlParts . length < 2 ) {
113
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
114
+ }
115
+ const [ ...base64Parts ] = urlParts ;
116
+ return base64Parts . join ( "," ) ;
117
+ } ) ( ) ,
111
118
} ,
112
119
} ;
113
120
} ) ;
Original file line number Diff line number Diff line change @@ -546,7 +546,12 @@ class Bedrock extends BaseLLM {
546
546
blocks . push ( { text : part . text } ) ;
547
547
} else if ( part . type === "imageUrl" && part . imageUrl ) {
548
548
try {
549
- const [ mimeType , base64Data ] = part . imageUrl . url . split ( "," ) ;
549
+ const urlParts = part . imageUrl . url . split ( "," ) ;
550
+ if ( urlParts . length < 2 ) {
551
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
552
+ }
553
+ const [ mimeType , ...base64Parts ] = urlParts ;
554
+ const base64Data = base64Parts . join ( "," ) ;
550
555
const format = mimeType . split ( "/" ) [ 1 ] ?. split ( ";" ) [ 0 ] || "jpeg" ;
551
556
if (
552
557
format === ImageFormat . JPEG ||
Original file line number Diff line number Diff line change @@ -191,7 +191,14 @@ class Gemini extends BaseLLM {
191
191
: {
192
192
inlineData : {
193
193
mimeType : "image/jpeg" ,
194
- data : part . imageUrl ?. url . split ( "," ) [ 1 ] ,
194
+ data : ( ( ) => {
195
+ const urlParts = part . imageUrl . url . split ( "," ) ;
196
+ if ( urlParts . length < 2 ) {
197
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
198
+ }
199
+ const [ , ...base64Parts ] = urlParts ;
200
+ return base64Parts . join ( "," ) ;
201
+ } ) ( ) ,
195
202
} ,
196
203
} ;
197
204
}
Original file line number Diff line number Diff line change @@ -303,7 +303,15 @@ class Ollama extends BaseLLM implements ModelInstaller {
303
303
const images : string [ ] = [ ] ;
304
304
message . content . forEach ( ( part ) => {
305
305
if ( part . type === "imageUrl" && part . imageUrl ) {
306
- const image = part . imageUrl ?. url . split ( "," ) . at ( - 1 ) ;
306
+ const image = ( ( ) => {
307
+ if ( ! part . imageUrl ?. url ) return undefined ;
308
+ const urlParts = part . imageUrl . url . split ( "," ) ;
309
+ if ( urlParts . length < 2 ) {
310
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
311
+ }
312
+ const [ , ...base64Parts ] = urlParts ;
313
+ return base64Parts . join ( "," ) ;
314
+ } ) ( ) ;
307
315
if ( image ) {
308
316
images . push ( image ) ;
309
317
}
Original file line number Diff line number Diff line change @@ -199,7 +199,14 @@ export class AnthropicApi implements BaseLlmApi {
199
199
source : {
200
200
type : "base64" ,
201
201
media_type : getAnthropicMediaTypeFromDataUrl ( dataUrl ) ,
202
- data : dataUrl . split ( "," ) [ 1 ] ,
202
+ data : ( ( ) => {
203
+ const urlParts = dataUrl . split ( "," ) ;
204
+ if ( urlParts . length < 2 ) {
205
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
206
+ }
207
+ const [ , ...base64Parts ] = urlParts ;
208
+ return base64Parts . join ( "," ) ;
209
+ } ) ( ) ,
203
210
} ,
204
211
} ) ;
205
212
}
Original file line number Diff line number Diff line change @@ -121,9 +121,12 @@ export class BedrockApi implements BaseLlmApi {
121
121
case "image_url" :
122
122
default :
123
123
try {
124
- const [ mimeType , base64Data ] = (
125
- part as ChatCompletionContentPartImage
126
- ) . image_url . url . split ( "," ) ;
124
+ const urlParts = ( part as ChatCompletionContentPartImage ) . image_url . url . split ( "," ) ;
125
+ if ( urlParts . length < 2 ) {
126
+ throw new Error ( "Invalid data URL format: missing comma separator" ) ;
127
+ }
128
+ const [ mimeType , ...base64Parts ] = urlParts ;
129
+ const base64Data = base64Parts . join ( "," ) ;
127
130
const format = mimeType . split ( "/" ) [ 1 ] ?. split ( ";" ) [ 0 ] || "jpeg" ;
128
131
if (
129
132
format === ImageFormat . JPEG ||
You can’t perform that action at this time.
0 commit comments