Skip to content

Commit 578893f

Browse files
committed
more clean up
1 parent 79ea304 commit 578893f

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

packages/ai/src/tools/classes/upload-image.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,13 @@ export class UploadImageTool extends ClientTool {
5959

6060
console.warn(`No exact match for "${args.image_reference}", using most recent image: ${mostRecentImage.displayName}`);
6161

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(/^data:image\/[a-z]+;base64,/, '');
70-
const binaryData = this.base64ToUint8Array(base64Data);
71-
72-
await sandbox.writeBinaryFile(fullPath, binaryData);
62+
const fullPath = await this.uploadImageToSandbox(mostRecentImage, args, sandbox);
7363
await editorEngine.image.scanImages();
7464

7565
return `Image "${mostRecentImage.displayName}" uploaded successfully to ${fullPath}`;
7666
}
7767

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(/^data:image\/[a-z]+;base64,/, '');
86-
const binaryData = this.base64ToUint8Array(base64Data);
87-
88-
await sandbox.writeBinaryFile(fullPath, binaryData);
68+
const fullPath = await this.uploadImageToSandbox(imageContext, args, sandbox);
8969
await editorEngine.image.scanImages();
9070

9171
return `Image "${imageContext.displayName}" uploaded successfully to ${fullPath}`;
@@ -101,6 +81,22 @@ export class UploadImageTool extends ClientTool {
10181
return 'Uploading image';
10282
}
10383

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(/^data:image\/[a-zA-Z0-9+.-]+;base64,/, '');
95+
const binaryData = this.base64ToUint8Array(base64Data);
96+
await sandbox.writeBinaryFile(fullPath, binaryData);
97+
return fullPath;
98+
}
99+
104100
private getExtensionFromMimeType(mimeType: string): string {
105101
const mimeToExt: Record<string, string> = {
106102
'image/jpeg': 'jpg',

0 commit comments

Comments
 (0)