@@ -267,14 +267,14 @@ function analyzeColorsFromImage() {
267267 return ;
268268 }
269269
270- showStatus ( 'UPLOADING IMAGE TO IMGUR ...' , 'info' ) ;
270+ showStatus ( 'SENDING IMAGE TO LLM ...' , 'info' ) ;
271271 console . log ( 'Analyzing colors. PluginMessageHandler available:' , typeof PluginMessageHandler !== 'undefined' ) ;
272272
273273 // In a real R1 implementation, we would send this to the LLM
274274 if ( typeof PluginMessageHandler !== 'undefined' ) {
275- console . log ( 'Uploading image to imgur.com ' ) ;
276- // Upload to imgur and then send URL to LLM
277- uploadToImgurAndAnalyze ( ) ;
275+ console . log ( 'Sending image directly to LLM ' ) ;
276+ // Send image data directly to LLM for analysis
277+ sendImageToLLM ( ) ;
278278 } else {
279279 console . log ( 'Simulating analysis for browser testing' ) ;
280280 // Simulate analysis for browser testing with more realistic colors
@@ -468,14 +468,14 @@ window.onPluginMessage = function(data) {
468468 resetApp ( ) ;
469469 } , 2000 ) ;
470470 }
471- // Handle case where LLM requests image URL - but we're already using imgur
471+ // Handle case where LLM requests image URL - but we're sending directly
472472 else if ( data . message . includes ( 'image' ) &&
473473 ( data . message . includes ( 'url' ) ||
474474 data . message . includes ( 'link' ) ||
475475 data . message . includes ( 'file' ) ||
476476 data . message . includes ( 'upload' ) ) ) {
477- showStatus ( 'LLM REQUESTED IMAGE URL - ALREADY USING IMGUR ' , 'info' ) ;
478- console . log ( 'LLM requested image URL but we already sent imgur URL ' ) ;
477+ showStatus ( 'LLM REQUESTED IMAGE URL - SENDING DIRECTLY ' , 'info' ) ;
478+ console . log ( 'LLM requested image URL but we are sending image data directly ' ) ;
479479 } else if ( data . message . includes ( 'timeout' ) || data . message . includes ( 'failed' ) || data . message . includes ( 'error' ) ) {
480480 showStatus ( 'LLM ERROR: ' + data . message , 'error' ) ;
481481 } else {
@@ -582,89 +582,27 @@ function createColorShapes(colors) {
582582 return shapesContainer ;
583583}
584584
585- // Function to upload image to imgur and analyze
586- async function uploadToImgurAndAnalyze ( ) {
587- showStatus ( 'UPLOADING IMAGE TO IMGUR ...' , 'info' ) ;
585+ // Function to send image directly to LLM for analysis
586+ function sendImageToLLM ( ) {
587+ showStatus ( 'SENDING IMAGE TO LLM ...' , 'info' ) ;
588588
589589 try {
590- // Check if we have image data
591- if ( ! capturedImageData ) {
592- throw new Error ( 'No captured image data available' ) ;
593- }
594-
595- console . log ( 'Captured image data length:' , capturedImageData . length ) ;
596-
597- // Validate image data
598- if ( capturedImageData . length < 100 ) {
599- throw new Error ( 'Captured image data is too small' ) ;
600- }
601-
602- // Convert data URL to Blob
603- const blob = dataURLToBlob ( capturedImageData ) ;
604- console . log ( 'Blob created, size:' , blob . size , 'type:' , blob . type ) ;
605-
606- // Check if blob is valid
607- if ( blob . size === 0 ) {
608- throw new Error ( 'Image blob is empty' ) ;
609- }
610-
611- // Create FormData for imgur
612- const formData = new FormData ( ) ;
613- formData . append ( 'image' , blob ) ;
614-
615- // Try to upload to imgur with timeout
616- console . log ( 'Sending request to imgur.com' ) ;
617- const controller = new AbortController ( ) ;
618- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , 15000 ) ; // 15 second timeout
619-
620- const response = await fetch ( 'https://api.imgur.com/3/image' , {
621- method : 'POST' ,
622- body : formData ,
623- headers : {
624- 'Authorization' : 'Client-ID 546c223def8fc9a' // Anonymous client ID
625- } ,
626- signal : controller . signal
627- } ) ;
628-
629- clearTimeout ( timeoutId ) ;
590+ // Log the image data info for debugging
591+ console . log ( 'Sending image to LLM. Image data length:' , capturedImageData ? capturedImageData . length : 'null' ) ;
630592
631- console . log ( 'Imgur response status:' , response . status ) ;
593+ // Send image data directly to LLM for analysis with specific instructions
594+ const payload = {
595+ message : `I'm sending you an image captured from the R1 device's camera. Please analyze this image and extract exactly 5 dominant colors in hex format. Return ONLY a JSON object in this exact format: {"colors": ["#hex1", "#hex2", "#hex3", "#hex4", "#hex5"]}. Do not request an image URL as I'm sending the image data directly.` ,
596+ useLLM : true ,
597+ wantsR1Response : false , // Set to false to get JSON response
598+ imageData : capturedImageData // Send image data directly
599+ } ;
632600
633- if ( response . ok ) {
634- const data = await response . json ( ) ;
635- const imageUrl = data . data . link ;
636-
637- console . log ( 'Image uploaded successfully to:' , imageUrl ) ;
638-
639- // Validate that we got a URL
640- if ( imageUrl && imageUrl . length > 0 && ( imageUrl . startsWith ( 'http://' ) || imageUrl . startsWith ( 'https://' ) ) ) {
641- showStatus ( 'IMAGE UPLOADED TO IMGUR! REQUESTING ANALYSIS...' , 'success' ) ;
642-
643- // Send image URL to LLM for analysis
644- const payload = {
645- message : `Please analyze the colors in this image and provide exactly 5 dominant colors in hex format. Response format: {"colors": ["#hex1", "#hex2", "#hex3", "#hex4", "#hex5"]}. Image URL: ${ imageUrl } ` ,
646- useLLM : true ,
647- wantsR1Response : false // Set to false to get JSON response
648- } ;
649-
650- console . log ( 'Sending to LLM:' , payload . message ) ;
651- showStatus ( 'REQUESTING COLOR ANALYSIS...' , 'info' ) ;
652- PluginMessageHandler . postMessage ( JSON . stringify ( payload ) ) ;
653- } else {
654- throw new Error ( 'Invalid URL received from imgur' ) ;
655- }
656- } else {
657- const errorData = await response . json ( ) ;
658- throw new Error ( 'Upload failed with status: ' + response . status + ' - ' + ( errorData . data ? errorData . data . error : 'Unknown error' ) ) ;
659- }
601+ console . log ( 'Sending image to LLM with payload:' , JSON . stringify ( payload , null , 2 ) ) ;
602+ PluginMessageHandler . postMessage ( JSON . stringify ( payload ) ) ;
603+ showStatus ( 'IMAGE SENT TO LLM. WAITING FOR RESPONSE...' , 'info' ) ;
660604 } catch ( error ) {
661- console . error ( 'Imgur upload error:' , error ) ;
662- if ( error . name === 'AbortError' ) {
663- showStatus ( 'UPLOAD TIMED OUT - CHECK NETWORK' , 'error' ) ;
664- } else if ( error . message . includes ( 'fetch' ) ) {
665- showStatus ( 'NETWORK ERROR - IMGUR UNAVAILABLE' , 'error' ) ;
666- } else {
667- showStatus ( 'UPLOAD FAILED: ' + error . message , 'error' ) ;
668- }
605+ console . error ( 'Error sending image to LLM:' , error ) ;
606+ showStatus ( 'LLM COMMUNICATION FAILED' , 'error' ) ;
669607 }
670608}
0 commit comments