@@ -31,6 +31,7 @@ import {
3131
3232import { fromNodeProviderChain } from "@aws-sdk/credential-providers" ;
3333import { fromStatic } from "@aws-sdk/token-providers" ;
34+ import { parseDataUrl } from "../../../../core/util/url.js" ;
3435import { BedrockConfig } from "../types.js" ;
3536import { chatChunk , chatChunkFromDelta , embedding , rerank } from "../util.js" ;
3637import { safeParseArgs } from "../util/parseArgs.js" ;
@@ -134,35 +135,35 @@ export class BedrockApi implements BaseLlmApi {
134135 throw new Error ( "Unsupported part type: input_audio" ) ;
135136 case "image_url" :
136137 default :
137- try {
138- const [ mimeType , base64Data ] = (
139- part as ChatCompletionContentPartImage
140- ) . image_url . url . split ( "," ) ;
141- const format = mimeType . split ( "/" ) [ 1 ] ?. split ( ";" ) [ 0 ] || "jpeg" ;
142- if (
143- format === ImageFormat . JPEG ||
144- format === ImageFormat . PNG ||
145- format === ImageFormat . WEBP ||
146- format === ImageFormat . GIF
147- ) {
148- return {
149- image : {
150- format,
151- source : {
152- bytes : Uint8Array . from ( Buffer . from ( base64Data , "base64" ) ) ,
153- } ,
154- } ,
155- } ;
156- } else {
157- console . warn (
158- `Bedrock: skipping unsupported image part format: ${ format } ` ,
159- ) ;
160- return { text : "[Unsupported image format]" } ;
161- }
162- } catch ( error ) {
163- console . warn ( "Bedrock: failed to process image part" , error ) ;
138+ const parsed = parseDataUrl (
139+ ( part as ChatCompletionContentPartImage ) . image_url . url ,
140+ ) ;
141+ if ( ! parsed ) {
142+ console . warn ( "Bedrock: failed to process image part - invalid URL" ) ;
164143 return { text : "[Failed to process image]" } ;
165144 }
145+ const { mimeType, base64Data } = parsed ;
146+ const format = mimeType . split ( "/" ) [ 1 ] ?. split ( ";" ) [ 0 ] || "jpeg" ;
147+ if (
148+ format === ImageFormat . JPEG ||
149+ format === ImageFormat . PNG ||
150+ format === ImageFormat . WEBP ||
151+ format === ImageFormat . GIF
152+ ) {
153+ return {
154+ image : {
155+ format,
156+ source : {
157+ bytes : Uint8Array . from ( Buffer . from ( base64Data , "base64" ) ) ,
158+ } ,
159+ } ,
160+ } ;
161+ } else {
162+ console . warn (
163+ `Bedrock: skipping unsupported image part format: ${ format } ` ,
164+ ) ;
165+ return { text : "[Unsupported image format]" } ;
166+ }
166167 }
167168 }
168169
0 commit comments