okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7b06ea51b
main
1import { useSyncExternalStore } from 'react' ; 2export interface ChatPhoto { id :string ; name :string } 3const queues = new Map < string , ChatPhoto []>(), listeners = new Set <() => void >(), empty :ChatPhoto [] = []; 4const key = ( id ?:string | null ) => id ?? 'new' ; 5function database (){ return new Promise < IDBDatabase >(( resolve , reject ) => { const request = indexedDB . open ( 'strata-photos' , 1 ); request . onupgradeneeded = () => request . result . createObjectStore ( 'photos' ); request . onsuccess = () => resolve ( request . result ); request . onerror = () => reject ( new Error ( 'Could not store this picture. Check browser storage.' ));});} 6export function useChatPhotos ( id ?:string | null ){ return useSyncExternalStore ( fn => { listeners . add ( fn ); return () => listeners . delete ( fn );},() => queues . get ( key ( id )) ?? empty );} 7/** A big picture (a screenshot of a large screen) is made smaller so it can be sent: at most 2048 pixels on the long side, as a JPEG, under 2 MB. */ 8export async function shrinkPicture ( file :File , limit = 2 * 1024 * 1024 ) :Promise < File >{ if ( file . size <= limit ) return file ; if ( typeof createImageBitmap !== 'function' || typeof document === 'undefined' ) throw new Error ( 'Choose a picture smaller than 2 MB.' ); const bitmap = await createImageBitmap ( file ); let side = Math . min ( 1 , 2048 / Math . max ( bitmap . width , bitmap . height )); for ( let round = 0 ; round < 5 ; round ++ ){ const canvas = document . createElement ( 'canvas' ); canvas . width = Math . max ( 1 , Math . round ( bitmap . width * side )); canvas . height = Math . max ( 1 , Math . round ( bitmap . height * side )); const context = canvas . getContext ( '2d' ); if ( ! context ) break ; context . fillStyle = '#fff' ; context . fillRect ( 0 , 0 , canvas . width , canvas . height ); context . drawImage ( bitmap , 0 , 0 , canvas . width , canvas . height ); const blob = await new Promise < Blob | null >( resolve => canvas . toBlob ( resolve , 'image/jpeg' , round < 2 ?0.9 :0.75 )); if ( blob && blob . size <= limit ){ bitmap . close ?.(); return new File ([ blob ], file . name . replace ( / [.][a-z0-9]+$ / i , '' ) + '.jpg' ,{ type :'image/jpeg' });} side *= 0.75 ;} bitmap . close ?.(); throw new Error ( 'Choose a picture smaller than 2 MB.' );} 9export async function attachChatPhoto ( file :File , id ?:string | null ){ if ( ! [ 'image/png' , 'image/jpeg' , 'image/webp' ]. includes ( file . type )) throw new Error ( 'Attach a PNG, JPEG or WebP picture. Other images can be opened in preview.' ); file = await shrinkPicture ( file ); const current = queues . get ( key ( id )) ?? []; if ( current . length >= 3 ) throw new Error ( 'Attach up to three pictures per message.' ); const photo = { id :crypto . randomUUID (), name :file . name }; const db = await database (); await new Promise < void >(( resolve , reject ) => { const tx = db . transaction ( 'photos' , 'readwrite' ); tx . objectStore ( 'photos' ). put ( file , photo . id ); tx . oncomplete = () => resolve (); tx . onerror = () => reject ( new Error ( 'This picture could not be saved.' ));}); db . close (); queues . set ( key ( id ),[ ...current , photo ]); listeners . forEach ( fn => fn ()); return photo ;} 10export function removeChatPhoto ( photo :string , id ?:string | null ){ queues . set ( key ( id ),( queues . get ( key ( id )) ?? []). filter ( i => i . id !== photo )); listeners . forEach ( fn => fn ());} 11export function pendingChatPhotos ( id ?:string | null ){ return queues . get ( key ( id )) ?? empty ;} 12export function takeChatPhotos ( id ?:string | null ){ const value = queues . get ( key ( id )) ?? []; queues . delete ( key ( id )); listeners . forEach ( fn => fn ()); return value ;} 13/** The stored picture itself, for uploading it to the host. */ 14export async function photoBlob ( photo :ChatPhoto ) :Promise < Blob >{ const db = await database (); const file = await new Promise < Blob | undefined >(( resolve , reject ) => { const request = db . transaction ( 'photos' , 'readonly' ). objectStore ( 'photos' ). get ( photo . id ); request . onsuccess = () => resolve ( request . result ); request . onerror = () => reject ( new Error ( 'Could not read the attached picture.' ));}); db . close (); if ( ! file ) throw new Error ( ` ${ photo . name } is no longer in browser storage. Attach it again.` ); return file ;} 15/** Upload the pictures of a message to the chat (POST /api/chat/upload); the host answers an id for each, which the send names as imageIds. */ 16export async function uploadChatPhotos ( photos :ChatPhoto [], sessionId :string ) :Promise < string []>{ const ids :string [] = []; for ( const photo of photos ){ const blob = await photoBlob ( photo ); const res = await fetch ( ` ${( globalThis as { __STRATA_HOST__ ?: string }). __STRATA_HOST__ ?? "" } /api/chat/upload?sessionId= ${ encodeURIComponent ( sessionId )} &name= ${ encodeURIComponent ( photo . name )} ` ,{ method :'POST' , headers :{ 'content-type' :blob . type || 'application/octet-stream' }, body :blob }); const data = await res . json (). catch (() => ({})) as { files ?:{ id :string | null ; kind :string }[]; error ?:string }; if ( ! res . ok ) throw new Error ( data . error || 'The picture could not be sent. Try another one.' ); const id = data . files ?. find ( f => f . kind === 'image' && f . id )?. id ; if ( ! id ) throw new Error ( 'The picture could not be read. Use a PNG, JPEG or WebP picture.' ); ids . push ( id );} return ids ;} 17export async function photoData ( photo :ChatPhoto ){ const db = await database (); const file = await new Promise < Blob | undefined >(( resolve , reject ) => { const request = db . transaction ( 'photos' , 'readonly' ). objectStore ( 'photos' ). get ( photo . id ); request . onsuccess = () => resolve ( request . result ); request . onerror = () => reject ( new Error ( 'Could not read the attached picture.' ));}); db . close (); if ( ! file ) throw new Error ( ` ${ photo . name } is no longer in browser storage. Attach it again.` ); return new Promise <{ name :string ; url :string }>(( resolve , reject ) => { const reader = new FileReader (); reader . onload = () => resolve ({ name :photo . name , url :String ( reader . result )}); reader . onerror = () => reject ( new Error ( 'Could not read this picture.' )); reader . readAsDataURL ( file );});} 18 19/** Keep at most three pictures in the inference context, including this turn. Originals remain in IndexedDB. */ 20export function photoHistorySlots ( messages :{ photos ?:ChatPhoto []}[], currentCount :number ){ let room = Math . max ( 0 , 3 - currentCount ); const slots = new Set < string >(); for ( let m = messages . length - 1 ; m >= 0 && room ; m -- ) for ( let p = ( messages [ m ]. photos ?. length ?? 0 ) - 1 ; p >= 0 && room ; p -- ){ slots . add ( ` ${ m } : ${ p } ` ); room -- ;} return slots ;}