@@ -70,8 +70,9 @@ export function compressImage(file: Blob, maxSize: number): Promise<string> {
7070 } ) ;
7171}
7272
73- export async function preProcessImageContent (
73+ export async function preProcessImageContentBase (
7474 content : RequestMessage [ "content" ] ,
75+ transformImageUrl : ( url : string ) => Promise < { [ key : string ] : any } > ,
7576) {
7677 if ( typeof content === "string" ) {
7778 return content ;
@@ -81,7 +82,7 @@ export async function preProcessImageContent(
8182 if ( part ?. type == "image_url" && part ?. image_url ?. url ) {
8283 try {
8384 const url = await cacheImageToBase64Image ( part ?. image_url ?. url ) ;
84- result . push ( { type : part . type , image_url : { url } } ) ;
85+ result . push ( await transformImageUrl ( url ) ) ;
8586 } catch ( error ) {
8687 console . error ( "Error processing image URL:" , error ) ;
8788 }
@@ -92,26 +93,21 @@ export async function preProcessImageContent(
9293 return result ;
9394}
9495
96+ export async function preProcessImageContent (
97+ content : RequestMessage [ "content" ] ,
98+ ) {
99+ return preProcessImageContentBase ( content , async ( url ) => ( {
100+ type : "image_url" ,
101+ image_url : { url } ,
102+ } ) ) ;
103+ }
104+
95105export async function preProcessImageContentForAlibabaDashScope (
96106 content : RequestMessage [ "content" ] ,
97107) {
98- if ( typeof content === "string" ) {
99- return content ;
100- }
101- const result = [ ] ;
102- for ( const part of content ) {
103- if ( part ?. type == "image_url" && part ?. image_url ?. url ) {
104- try {
105- const url = await cacheImageToBase64Image ( part ?. image_url ?. url ) ;
106- result . push ( { image : url } ) ;
107- } catch ( error ) {
108- console . error ( "Error processing image URL:" , error ) ;
109- }
110- } else {
111- result . push ( { ...part } ) ;
112- }
113- }
114- return result ;
108+ return preProcessImageContentBase ( content , async ( url ) => ( {
109+ image : url ,
110+ } ) ) ;
115111}
116112
117113const imageCaches : Record < string , string > = { } ;
0 commit comments