@@ -20,6 +20,7 @@ import {
2020} from "./composerAttachmentFiles" ;
2121import { imageMimeType } from "@t3tools/shared/image" ;
2222import { videoMimeType } from "@t3tools/shared/video" ;
23+ import { fitComposerImage , renderPhoto } from "./composerImageFit" ;
2324import { beginForegroundHandoff } from "./foreground-handoff" ;
2425import { uuidv4 } from "./uuid" ;
2526import { writeFileAtomically } from "./atomic-file" ;
@@ -341,46 +342,6 @@ export async function pickComposerFiles(input: {
341342 return { files : attachments , error } ;
342343}
343344
344- /**
345- * Longest edge kept when a photo has to be re-encoded. Matches the web composer's
346- * MAX_DIMENSION so every client hands providers the same resolution.
347- */
348- const PHOTO_MAX_EDGE = 2048 ;
349- const PHOTO_JPEG_QUALITY = 0.85 ;
350-
351- /**
352- * Renders a photo-library pick to a provider-readable JPEG. Decode, downscale, and encode run
353- * natively; only the bounded result crosses the bridge. Camera photos are 12-48 MP HEIC files,
354- * so a full-size conversion is both slow to transfer and far more than a model can use.
355- */
356- async function renderPhotoAsJpeg ( uri : string ) : Promise < { base64 : string ; uri : string } > {
357- const { ImageManipulator, SaveFormat } = await import ( "expo-image-manipulator" ) ;
358- let image = await ImageManipulator . manipulate ( uri ) . renderAsync ( ) ;
359- try {
360- const longestEdge = Math . max ( image . width , image . height ) ;
361- if ( longestEdge > PHOTO_MAX_EDGE ) {
362- const resized = await ImageManipulator . manipulate ( image )
363- . resize (
364- image . width >= image . height ? { width : PHOTO_MAX_EDGE } : { height : PHOTO_MAX_EDGE } ,
365- )
366- . renderAsync ( ) ;
367- image . release ( ) ;
368- image = resized ;
369- }
370- const saved = await image . saveAsync ( {
371- format : SaveFormat . JPEG ,
372- compress : PHOTO_JPEG_QUALITY ,
373- base64 : true ,
374- } ) ;
375- if ( ! saved . base64 ) {
376- throw new Error ( "The rendered photo has no bytes." ) ;
377- }
378- return { base64 : saved . base64 , uri : saved . uri } ;
379- } finally {
380- image . release ( ) ;
381- }
382- }
383-
384345async function loadImagePicker ( ) {
385346 try {
386347 return await import ( "expo-image-picker" ) ;
@@ -530,14 +491,20 @@ export async function pickComposerMedia(input: {
530491 try {
531492 if ( originalMimeType !== null ) {
532493 const { File } = await import ( "expo-file-system" ) ;
494+ const base64 = await new File ( asset . uri ) . base64 ( ) ;
495+ const fitted = await fitComposerImage ( {
496+ uri : asset . uri ,
497+ mimeType : originalMimeType ,
498+ base64,
499+ } ) ;
533500 image = {
534- base64 : await new File ( asset . uri ) . base64 ( ) ,
501+ base64 : fitted ?. base64 ?? base64 ,
535502 mimeType : originalMimeType ,
536503 name,
537- previewUri : asset . uri ,
504+ previewUri : fitted ?. uri ?? asset . uri ,
538505 } ;
539506 } else {
540- const rendered = await renderPhotoAsJpeg ( asset . uri ) ;
507+ const rendered = await renderPhoto ( asset . uri ) ;
541508 image = {
542509 base64 : rendered . base64 ,
543510 mimeType : "image/jpeg" ,
@@ -616,7 +583,14 @@ export async function pasteComposerClipboard(input: { readonly existingCount: nu
616583 } ;
617584 }
618585
619- const base64 = image . data . split ( "," ) [ 1 ] ?? "" ;
586+ const pastedBase64 = image . data . split ( "," ) [ 1 ] ?? "" ;
587+ const fitted = await fitComposerImage ( {
588+ uri : image . data ,
589+ mimeType : "image/png" ,
590+ base64 : pastedBase64 ,
591+ } ) ;
592+ const base64 = fitted ?. base64 ?? pastedBase64 ;
593+ const dataUrl = fitted ? `data:image/png;base64,${ base64 } ` : image . data ;
620594 const sizeBytes = estimateBase64ByteSize ( base64 ) ;
621595 if ( sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
622596 return {
@@ -634,8 +608,8 @@ export async function pasteComposerClipboard(input: { readonly existingCount: nu
634608 name : "pasted-image.png" ,
635609 mimeType : "image/png" ,
636610 sizeBytes,
637- dataUrl : image . data ,
638- previewUri : image . data ,
611+ dataUrl,
612+ previewUri : dataUrl ,
639613 } ,
640614 ] ,
641615 text : null ,
@@ -707,20 +681,22 @@ export async function convertPastedImagesToAttachments(input: {
707681 continue ;
708682 }
709683 const file = new File ( uri ) ;
710- const base64 = await file . base64 ( ) ;
684+ const mimeType = mimeTypeFromUri ( uri ) ;
685+ const pastedBase64 = await file . base64 ( ) ;
686+ const fitted = await fitComposerImage ( { uri, mimeType, base64 : pastedBase64 } ) ;
687+ const base64 = fitted ?. base64 ?? pastedBase64 ;
711688 const sizeBytes = estimateBase64ByteSize ( base64 ) ;
712689 if ( sizeBytes <= 0 || sizeBytes > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES ) {
713690 continue ;
714691 }
715- const mimeType = mimeTypeFromUri ( uri ) ;
716692 results . push ( {
717693 id : uuidv4 ( ) ,
718694 type : "image" ,
719695 name : `pasted-image.${ mimeType . split ( "/" ) [ 1 ] ?? "png" } ` ,
720696 mimeType,
721697 sizeBytes,
722698 dataUrl : `data:${ mimeType } ;base64,${ base64 } ` ,
723- previewUri : ownedTemporaryFile ? `data:${ mimeType } ;base64,${ base64 } ` : uri ,
699+ previewUri : ownedTemporaryFile ? `data:${ mimeType } ;base64,${ base64 } ` : ( fitted ?. uri ?? uri ) ,
724700 } ) ;
725701 } catch ( error ) {
726702 console . warn ( "Failed to read pasted image" , uri , error ) ;
0 commit comments