|
455 | 455 | var headers = baseHeaders(); |
456 | 456 | headers['Content-Type'] = 'application/json'; |
457 | 457 |
|
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'); |
471 | 487 | return null; |
472 | 488 | } |
473 | | - var initData = await initResp.json(); |
| 489 | + |
474 | 490 | console.log('[Bridge] /backend-api/files init data:', initData); |
475 | 491 | var fileId = initData.file_id; |
476 | 492 | var uploadUrl = initData.upload_url; |
|
480 | 496 | if (uploadUrl.includes('blob.core.windows.net') || uploadUrl.includes('azure')) { |
481 | 497 | uploadHeaders['x-ms-blob-type'] = 'BlockBlob'; |
482 | 498 | } |
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); |
491 | 512 | return null; |
492 | 513 | } |
493 | 514 | } |
494 | 515 |
|
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); |
504 | 525 | } |
505 | 526 |
|
506 | 527 | return { |
|
525 | 546 |
|
526 | 547 | var chatgptMessages = messages.map(function(m, idx) { |
527 | 548 | 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))) { |
529 | 550 | 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) { |
534 | 554 | 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 |
537 | 560 | }); |
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 | + } |
540 | 582 | parts.push(text); |
541 | | - return { |
| 583 | + var msgObj = { |
542 | 584 | id: crypto.randomUUID(), |
543 | 585 | author: { role: m.role || 'user' }, |
544 | 586 | create_time: Date.now() / 1000, |
545 | 587 | content: { content_type: 'multimodal_text', parts: parts }, |
546 | 588 | metadata: {}, |
547 | 589 | }; |
| 590 | + if (metadataAttachments.length > 0) { |
| 591 | + msgObj.metadata.attachments = metadataAttachments; |
| 592 | + } |
| 593 | + return msgObj; |
548 | 594 | } |
549 | 595 | return { |
550 | 596 | id: crypto.randomUUID(), |
|
0 commit comments