Skip to content

Commit b0fe6e1

Browse files
committed
fix 22
1 parent ad97e21 commit b0fe6e1

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

js/mood-palette.js

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,14 @@ function captureImageFromPTT() {
193193
// Function to upload image to catbox.moe
194194
async function uploadToCatbox(imageData) {
195195
try {
196-
showStatus('UPLOADING IMAGE...', 'info');
196+
showStatus('UPLOADING TO CATBOX...', 'info');
197197
console.log('Starting image upload to catbox');
198198

199199
// Validate image data
200200
if (!imageData) {
201201
throw new Error('No image data provided');
202202
}
203203

204-
// Log the image data info for debugging
205-
console.log('Image data length:', imageData.length);
206-
207204
// Convert data URL to Blob
208205
const blob = dataURLToBlob(imageData);
209206
console.log('Blob created, size:', blob.size, 'type:', blob.type);
@@ -213,24 +210,28 @@ async function uploadToCatbox(imageData) {
213210
formData.append('reqtype', 'fileupload');
214211
formData.append('fileToUpload', blob, 'image.jpg');
215212

216-
// Try to upload to catbox with a simpler approach
213+
// Try to upload to catbox
217214
console.log('Sending request to catbox.moe');
218215
const response = await fetch('https://catbox.moe/user/api.php', {
219216
method: 'POST',
220-
body: formData,
221-
// Remove signal to avoid AbortError
217+
body: formData
222218
});
223219

224220
console.log('Catbox response status:', response.status);
225221

226222
if (response.ok) {
227-
const url = await response.text();
223+
const responseText = await response.text();
224+
const url = responseText.trim();
228225
console.log('Image uploaded successfully to:', url);
229-
showStatus('IMAGE UPLOADED SUCCESSFULLY', 'info');
230-
return url.trim(); // Trim any whitespace
226+
227+
// Validate that we got a URL
228+
if (url && url.length > 0) {
229+
showStatus('IMAGE UPLOADED SUCCESSFULLY', 'success');
230+
return url;
231+
} else {
232+
throw new Error('Empty response from catbox');
233+
}
231234
} else {
232-
const errorText = await response.text();
233-
console.error('Catbox upload failed with status:', response.status, 'Response:', errorText);
234235
throw new Error('Upload failed with status: ' + response.status);
235236
}
236237
} catch (error) {
@@ -270,22 +271,12 @@ function analyzeColorsFromImage() {
270271
return;
271272
}
272273

273-
showStatus('ANALYZING IMAGE COLORS...', 'info');
274+
showStatus('UPLOADING IMAGE FOR ANALYSIS...', 'info');
274275

275276
// In a real R1 implementation, we would send this to the LLM
276277
if (typeof PluginMessageHandler !== 'undefined') {
277-
// Try sending the image data directly to the LLM
278-
// The LLM might be able to handle base64 image data
279-
showStatus('SENDING IMAGE TO LLM...', 'info');
280-
281-
const payload = {
282-
message: "Analyze the colors in this image and provide exactly 5 dominant colors in hex format. Response format: {'colors': ['#hex1', '#hex2', '#hex3', '#hex4', '#hex5']}",
283-
useLLM: true,
284-
imageData: capturedImageData // Send the actual image data
285-
};
286-
287-
console.log('Sending image data to LLM, data length:', capturedImageData.length);
288-
PluginMessageHandler.postMessage(JSON.stringify(payload));
278+
// Immediately use catbox to upload the image and get a URL
279+
fallbackToCatboxAnalysis();
289280
} else {
290281
// Simulate analysis for browser testing with more realistic colors
291282
setTimeout(() => {
@@ -380,9 +371,11 @@ function displayPalette(colors) {
380371
}
381372

382373
function emailPalette() {
383-
console.log('Email palette function called, currentPalette:', currentPalette);
374+
console.log('Email palette function called');
375+
console.log('Current palette:', currentPalette);
384376

385-
if (!currentPalette || currentPalette.length === 0) {
377+
// Check if we have a valid palette
378+
if (!currentPalette || !Array.isArray(currentPalette) || currentPalette.length === 0) {
386379
showStatus('NO PALETTE TO SEND', 'error');
387380
return;
388381
}
@@ -454,41 +447,22 @@ window.onPluginMessage = function(data) {
454447
displayPalette(currentPalette);
455448
showStatus('PALETTE READY! EMAIL TO SEND', 'success');
456449
} else {
457-
// If we can't parse colors, check if it's a request for image URL
458-
handleLLMResponse(data.data);
450+
// Show the response as-is if it's not color data
451+
showStatus(data.data, 'info');
459452
}
460453
} catch (e) {
461454
console.error('Error parsing plugin message:', e);
462-
// Handle non-JSON responses
463-
handleLLMResponse(data.data);
455+
// Show the response as-is if we can't parse it
456+
showStatus(data.data, 'info');
464457
}
465458
} else if (data.message) {
466-
handleLLMResponse(data.message);
459+
showStatus(data.message, 'info');
467460
} else {
468461
// Show raw data if no message or data
469462
showStatus('RECEIVED: ' + JSON.stringify(data), 'info');
470463
}
471464
};
472465

473-
// Function to handle LLM responses
474-
function handleLLMResponse(response) {
475-
console.log('Handling LLM response:', response);
476-
477-
// Check if the response is asking for an image URL
478-
if (typeof response === 'string' &&
479-
(response.includes('upload') ||
480-
response.includes('image URL') ||
481-
response.includes('direct link') ||
482-
response.includes('access the image') ||
483-
response.includes('provide a link'))) {
484-
showStatus('LLM REQUESTED IMAGE URL, UPLOADING...', 'info');
485-
fallbackToCatboxAnalysis();
486-
} else {
487-
// Show the response as-is
488-
showStatus(response, 'info');
489-
}
490-
}
491-
492466
// Helper functions
493467
function showStatus(message, type) {
494468
const statusDiv = document.getElementById('statusMessage');
@@ -503,28 +477,29 @@ function isValidEmail(email) {
503477

504478
// Fallback function to use catbox for image hosting and analysis
505479
async function fallbackToCatboxAnalysis() {
506-
showStatus('UPLOADING IMAGE FOR ANALYSIS...', 'info');
480+
showStatus('UPLOADING IMAGE TO CATBOX...', 'info');
507481

508482
try {
509483
// Upload image to catbox
510484
const imageUrl = await uploadToCatbox(capturedImageData);
511485

512-
if (imageUrl) {
513-
showStatus('ANALYZING COLORS...', 'info');
514-
console.log('Sending image URL to LLM:', imageUrl);
486+
if (imageUrl && imageUrl.length > 0) {
487+
showStatus('IMAGE UPLOADED! ANALYZING COLORS...', 'info');
488+
console.log('Image uploaded to:', imageUrl);
515489

516-
// Send image URL to LLM for analysis with a more specific prompt
490+
// Send image URL to LLM for analysis
517491
const payload = {
518-
message: `Analyze the colors in this image: ${imageUrl} and provide exactly 5 dominant colors in hex format. Response format must be valid JSON: {"colors": ["#hex1", "#hex2", "#hex3", "#hex4", "#hex5"]}`,
492+
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}`,
519493
useLLM: true
520494
};
521495

496+
console.log('Sending to LLM:', payload.message);
522497
PluginMessageHandler.postMessage(JSON.stringify(payload));
523498
} else {
524-
throw new Error('Failed to upload image');
499+
throw new Error('Failed to get image URL from catbox');
525500
}
526501
} catch (error) {
527-
console.error('Fallback analysis error:', error);
502+
console.error('Catbox analysis error:', error);
528503
showStatus('ANALYSIS FAILED: ' + error.message, 'error');
529504
}
530505
}

0 commit comments

Comments
 (0)