|
9 | 9 | WRITE_FILE_TOOL_PARAMETERS
|
10 | 10 | } from '@onlook/ai';
|
11 | 11 | import { MessageContextType } from '@onlook/models';
|
| 12 | +import { v4 as uuidv4 } from 'uuid'; |
12 | 13 | import { z } from 'zod';
|
13 | 14 |
|
14 | 15 | export async function handleSearchReplaceEditFileTool(args: z.infer<typeof SEARCH_REPLACE_EDIT_FILE_TOOL_PARAMETERS>, editorEngine: EditorEngine): Promise<string> {
|
@@ -187,7 +188,6 @@ export async function handleViewImageTool(args: z.infer<typeof VIEW_IMAGE_TOOL_P
|
187 | 188 | });
|
188 | 189 |
|
189 | 190 | if (!imageContext || imageContext.type !== MessageContextType.IMAGE) {
|
190 |
| - // Try to find by index number |
191 | 191 | const imageContexts = context.filter(ctx => ctx.type === MessageContextType.IMAGE);
|
192 | 192 | const indexMatch = args.image_reference.match(/^\d+$/);
|
193 | 193 | if (indexMatch) {
|
@@ -228,75 +228,55 @@ export async function handleUploadImageTool(args: z.infer<typeof UPLOAD_IMAGE_TO
|
228 | 228 | throw new Error(`Sandbox not found for branch ID: ${args.branchId}`);
|
229 | 229 | }
|
230 | 230 |
|
231 |
| - // Find the image in the chat context by reference |
232 | 231 | const context = editorEngine.chat.context.context;
|
233 | 232 | const imageContext = context.find((ctx) => {
|
234 | 233 | if (ctx.type !== MessageContextType.IMAGE) {
|
235 | 234 | return false;
|
236 | 235 | }
|
237 |
| - // Try to match by display name or description |
238 | 236 | return ctx.displayName.toLowerCase().includes(args.image_reference.toLowerCase()) ||
|
239 | 237 | args.image_reference.toLowerCase().includes(ctx.displayName.toLowerCase());
|
240 | 238 | });
|
241 | 239 |
|
242 | 240 | if (!imageContext || imageContext.type !== MessageContextType.IMAGE) {
|
243 |
| - // Try to find the most recent image if no specific match |
244 | 241 | const recentImages = context.filter(ctx => ctx.type === MessageContextType.IMAGE);
|
245 | 242 | if (recentImages.length === 0) {
|
246 | 243 | throw new Error(`No image found matching reference: ${args.image_reference}`);
|
247 | 244 | }
|
248 | 245 |
|
249 |
| - // Use the most recent image if no specific match |
250 | 246 | const mostRecentImage = recentImages[recentImages.length - 1];
|
251 | 247 | if (!mostRecentImage || mostRecentImage.type !== MessageContextType.IMAGE) {
|
252 | 248 | throw new Error(`No image found matching reference: ${args.image_reference}`);
|
253 | 249 | }
|
254 | 250 |
|
255 | 251 | console.warn(`No exact match for "${args.image_reference}", using most recent image: ${mostRecentImage.displayName}`);
|
256 | 252 |
|
257 |
| - // Extract MIME type and file extension |
258 | 253 | const mimeType = mostRecentImage.mimeType;
|
259 | 254 | const extension = getExtensionFromMimeType(mimeType);
|
260 | 255 |
|
261 |
| - // Generate filename |
262 |
| - const filename = args.filename ? `${args.filename}.${extension}` : `${generateUUID()}.${extension}`; |
263 |
| - |
264 |
| - // Determine destination path |
| 256 | + const filename = args.filename ? `${args.filename}.${extension}` : `${uuidv4()}.${extension}`; |
265 | 257 | const destinationPath = args.destination_path || 'public/assets/images';
|
266 | 258 | const fullPath = `${destinationPath}/${filename}`;
|
267 | 259 |
|
268 |
| - // Convert base64 to binary data |
269 | 260 | const base64Data = mostRecentImage.content.replace(/^data:image\/[a-z]+;base64,/, '');
|
270 | 261 | const binaryData = base64ToUint8Array(base64Data);
|
271 | 262 |
|
272 |
| - // Upload to sandbox |
273 | 263 | await sandbox.writeBinaryFile(fullPath, binaryData);
|
274 |
| - |
275 |
| - // Refresh image scanning to update the UI |
276 | 264 | await editorEngine.image.scanImages();
|
277 | 265 |
|
278 | 266 | return `Image "${mostRecentImage.displayName}" uploaded successfully to ${fullPath}`;
|
279 | 267 | }
|
280 | 268 |
|
281 |
| - // Extract MIME type and file extension |
282 | 269 | const mimeType = imageContext.mimeType;
|
283 | 270 | const extension = getExtensionFromMimeType(mimeType);
|
284 | 271 |
|
285 |
| - // Generate filename |
286 |
| - const filename = args.filename ? `${args.filename}.${extension}` : `${generateUUID()}.${extension}`; |
287 |
| - |
288 |
| - // Determine destination path |
| 272 | + const filename = args.filename ? `${args.filename}.${extension}` : `${uuidv4()}.${extension}`; |
289 | 273 | const destinationPath = args.destination_path || 'public/assets/images';
|
290 | 274 | const fullPath = `${destinationPath}/${filename}`;
|
291 | 275 |
|
292 |
| - // Convert base64 to binary data |
293 | 276 | const base64Data = imageContext.content.replace(/^data:image\/[a-z]+;base64,/, '');
|
294 | 277 | const binaryData = base64ToUint8Array(base64Data);
|
295 | 278 |
|
296 |
| - // Upload to sandbox |
297 | 279 | await sandbox.writeBinaryFile(fullPath, binaryData);
|
298 |
| - |
299 |
| - // Refresh image scanning to update the UI |
300 | 280 | await editorEngine.image.scanImages();
|
301 | 281 |
|
302 | 282 | return `Image "${imageContext.displayName}" uploaded successfully to ${fullPath}`;
|
@@ -327,11 +307,3 @@ function base64ToUint8Array(base64: string): Uint8Array {
|
327 | 307 | }
|
328 | 308 | return bytes;
|
329 | 309 | }
|
330 |
| - |
331 |
| -function generateUUID(): string { |
332 |
| - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
333 |
| - const r = Math.random() * 16 | 0; |
334 |
| - const v = c === 'x' ? r : (r & 0x3 | 0x8); |
335 |
| - return v.toString(16); |
336 |
| - }); |
337 |
| -} |
0 commit comments