Skip to content

Commit 06bb032

Browse files
committed
fix: connect uploadedFiles image_asset_pointer and metadata.attachments to chatgptMessages
1 parent 7e23c64 commit 06bb032

1 file changed

Lines changed: 87 additions & 41 deletions

File tree

‎chrome_extension/injected.js‎

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -455,22 +455,38 @@
455455
var headers = baseHeaders();
456456
headers['Content-Type'] = 'application/json';
457457

458-
var initResp = await originalFetch('/backend-api/files', {
459-
method: 'POST',
460-
headers: headers,
461-
body: JSON.stringify({
462-
file_name: name,
463-
file_size: size,
464-
use_case: 'multimodal'
465-
})
466-
});
467-
console.log('[Bridge] /backend-api/files init status:', initResp.status);
468-
if (!initResp.ok) {
469-
var errTxt = await initResp.text();
470-
console.error('[Bridge] Failed to init file upload:', errTxt);
458+
var useCases = ['multimodal', 'ace_upload', 'my_files'];
459+
var initResp = null;
460+
var initData = null;
461+
for (var u = 0; u < useCases.length; u++) {
462+
try {
463+
initResp = await originalFetch('/backend-api/files', {
464+
method: 'POST',
465+
headers: headers,
466+
body: JSON.stringify({
467+
file_name: name,
468+
file_size: size,
469+
use_case: useCases[u]
470+
})
471+
});
472+
console.log('[Bridge] /backend-api/files init status (use_case=' + useCases[u] + '):', initResp.status);
473+
if (initResp.ok) {
474+
initData = await initResp.json();
475+
break;
476+
} else {
477+
var errTxt = await initResp.text();
478+
console.error('[Bridge] Failed to init file upload with use_case=' + useCases[u] + ':', errTxt);
479+
}
480+
} catch(e) {
481+
console.error('[Bridge] Error calling /backend-api/files:', e);
482+
}
483+
}
484+
485+
if (!initData || !initData.file_id) {
486+
console.error('[Bridge] Failed to obtain file_id from /backend-api/files');
471487
return null;
472488
}
473-
var initData = await initResp.json();
489+
474490
console.log('[Bridge] /backend-api/files init data:', initData);
475491
var fileId = initData.file_id;
476492
var uploadUrl = initData.upload_url;
@@ -480,27 +496,32 @@
480496
if (uploadUrl.includes('blob.core.windows.net') || uploadUrl.includes('azure')) {
481497
uploadHeaders['x-ms-blob-type'] = 'BlockBlob';
482498
}
483-
var uploadResp = await window.fetch(uploadUrl, {
484-
method: 'PUT',
485-
headers: uploadHeaders,
486-
body: blob
487-
});
488-
console.log('[Bridge] uploadUrl PUT status:', uploadResp.status);
489-
if (!uploadResp.ok) {
490-
console.error('[Bridge] Failed to upload file bytes:', await uploadResp.text());
499+
try {
500+
var uploadResp = await window.fetch(uploadUrl, {
501+
method: 'PUT',
502+
headers: uploadHeaders,
503+
body: blob
504+
});
505+
console.log('[Bridge] uploadUrl PUT status:', uploadResp.status);
506+
if (!uploadResp.ok) {
507+
console.error('[Bridge] Failed to upload file bytes:', await uploadResp.text());
508+
return null;
509+
}
510+
} catch(e) {
511+
console.error('[Bridge] Error uploading file bytes to uploadUrl:', e);
491512
return null;
492513
}
493514
}
494515

495-
var completeResp = await originalFetch('/backend-api/files/' + fileId + '/uploaded', {
496-
method: 'POST',
497-
headers: Object.assign({}, baseHeaders(), { 'Content-Type': 'application/json' }),
498-
body: JSON.stringify({ file_id: fileId })
499-
});
500-
console.log('[Bridge] /backend-api/files/uploaded status:', completeResp.status);
501-
if (!completeResp.ok) {
502-
console.error('[Bridge] Failed to mark file uploaded:', await completeResp.text());
503-
return null;
516+
try {
517+
var completeResp = await originalFetch('/backend-api/files/' + fileId + '/uploaded', {
518+
method: 'POST',
519+
headers: Object.assign({}, baseHeaders(), { 'Content-Type': 'application/json' }),
520+
body: JSON.stringify({ file_id: fileId })
521+
});
522+
console.log('[Bridge] /backend-api/files/uploaded status:', completeResp.status);
523+
} catch(e) {
524+
console.warn('[Bridge] Warning on file uploaded completion:', e);
504525
}
505526

506527
return {
@@ -525,26 +546,51 @@
525546

526547
var chatgptMessages = messages.map(function(m, idx) {
527548
var text = typeof m.content === 'string' ? m.content : JSON.stringify(m.content);
528-
if (idx === messages.length - 1 && Array.isArray(attachments) && attachments.length > 0) {
549+
if (idx === messages.length - 1 && (uploadedFiles.length > 0 || (Array.isArray(attachments) && attachments.length > 0))) {
529550
var parts = [];
530-
attachments.forEach(function(att) {
531-
var b64 = att.data_base64 || att.data || '';
532-
var mime = att.mime_type || att.media_type || 'image/png';
533-
if (b64) {
551+
var metadataAttachments = [];
552+
if (uploadedFiles.length > 0) {
553+
uploadedFiles.forEach(function(f) {
534554
parts.push({
535-
content_type: 'image_url',
536-
image_url: { url: 'data:' + mime + ';base64,' + b64 }
555+
asset_pointer: 'file-service://' + f.file_id,
556+
content_type: 'image_asset_pointer',
557+
size_bytes: f.file_size,
558+
width: 1000,
559+
height: 1000
537560
});
538-
}
539-
});
561+
metadataAttachments.push({
562+
id: f.file_id,
563+
size: f.file_size,
564+
name: f.file_name,
565+
mime_type: f.mime_type,
566+
width: 1000,
567+
height: 1000
568+
});
569+
});
570+
} else {
571+
attachments.forEach(function(att) {
572+
var b64 = att.data_base64 || att.data || '';
573+
var mime = att.mime_type || att.media_type || 'image/png';
574+
if (b64) {
575+
parts.push({
576+
content_type: 'image_url',
577+
image_url: { url: 'data:' + mime + ';base64,' + b64 }
578+
});
579+
}
580+
});
581+
}
540582
parts.push(text);
541-
return {
583+
var msgObj = {
542584
id: crypto.randomUUID(),
543585
author: { role: m.role || 'user' },
544586
create_time: Date.now() / 1000,
545587
content: { content_type: 'multimodal_text', parts: parts },
546588
metadata: {},
547589
};
590+
if (metadataAttachments.length > 0) {
591+
msgObj.metadata.attachments = metadataAttachments;
592+
}
593+
return msgObj;
548594
}
549595
return {
550596
id: crypto.randomUUID(),

0 commit comments

Comments
 (0)