@@ -59,33 +59,13 @@ export class UploadImageTool extends ClientTool {
59
59
60
60
console . warn ( `No exact match for "${ args . image_reference } ", using most recent image: ${ mostRecentImage . displayName } ` ) ;
61
61
62
- const mimeType = mostRecentImage . mimeType ;
63
- const extension = this . getExtensionFromMimeType ( mimeType ) ;
64
-
65
- const filename = args . filename ? `${ args . filename } .${ extension } ` : `${ uuidv4 ( ) } .${ extension } ` ;
66
- const destinationPath = args . destination_path || 'public/assets/images' ;
67
- const fullPath = `${ destinationPath } /${ filename } ` ;
68
-
69
- const base64Data = mostRecentImage . content . replace ( / ^ d a t a : i m a g e \/ [ a - z ] + ; b a s e 6 4 , / , '' ) ;
70
- const binaryData = this . base64ToUint8Array ( base64Data ) ;
71
-
72
- await sandbox . writeBinaryFile ( fullPath , binaryData ) ;
62
+ const fullPath = await this . uploadImageToSandbox ( mostRecentImage , args , sandbox ) ;
73
63
await editorEngine . image . scanImages ( ) ;
74
64
75
65
return `Image "${ mostRecentImage . displayName } " uploaded successfully to ${ fullPath } ` ;
76
66
}
77
67
78
- const mimeType = imageContext . mimeType ;
79
- const extension = this . getExtensionFromMimeType ( mimeType ) ;
80
-
81
- const filename = args . filename ? `${ args . filename } .${ extension } ` : `${ uuidv4 ( ) } .${ extension } ` ;
82
- const destinationPath = args . destination_path || 'public/assets/images' ;
83
- const fullPath = `${ destinationPath } /${ filename } ` ;
84
-
85
- const base64Data = imageContext . content . replace ( / ^ d a t a : i m a g e \/ [ a - z ] + ; b a s e 6 4 , / , '' ) ;
86
- const binaryData = this . base64ToUint8Array ( base64Data ) ;
87
-
88
- await sandbox . writeBinaryFile ( fullPath , binaryData ) ;
68
+ const fullPath = await this . uploadImageToSandbox ( imageContext , args , sandbox ) ;
89
69
await editorEngine . image . scanImages ( ) ;
90
70
91
71
return `Image "${ imageContext . displayName } " uploaded successfully to ${ fullPath } ` ;
@@ -101,6 +81,22 @@ export class UploadImageTool extends ClientTool {
101
81
return 'Uploading image' ;
102
82
}
103
83
84
+ private async uploadImageToSandbox (
85
+ imageContext : Extract < import ( '@onlook/models' ) . MessageContext , { type : MessageContextType . IMAGE } > ,
86
+ args : z . infer < typeof UploadImageTool . parameters > ,
87
+ sandbox : any
88
+ ) : Promise < string > {
89
+ const mimeType = imageContext . mimeType ;
90
+ const extension = this . getExtensionFromMimeType ( mimeType ) ;
91
+ const filename = args . filename ? `${ args . filename } .${ extension } ` : `${ uuidv4 ( ) } .${ extension } ` ;
92
+ const destinationPath = args . destination_path || 'public/assets/images' ;
93
+ const fullPath = `${ destinationPath } /${ filename } ` ;
94
+ const base64Data = imageContext . content . replace ( / ^ d a t a : i m a g e \/ [ a - z A - Z 0 - 9 + . - ] + ; b a s e 6 4 , / , '' ) ;
95
+ const binaryData = this . base64ToUint8Array ( base64Data ) ;
96
+ await sandbox . writeBinaryFile ( fullPath , binaryData ) ;
97
+ return fullPath ;
98
+ }
99
+
104
100
private getExtensionFromMimeType ( mimeType : string ) : string {
105
101
const mimeToExt : Record < string , string > = {
106
102
'image/jpeg' : 'jpg' ,
0 commit comments