Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++
5.6 KB
Raw Blame
import { useSyncExternalStore } from 'react';
export interface ChatPhoto {id:string;name:string}
const queues=new Map<string,ChatPhoto[]>(),listeners=new Set<()=>void>(),empty:ChatPhoto[]=[];
const key=(id?:string|null)=>id??'new';
function 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.'));});}
export function useChatPhotos(id?:string|null){return useSyncExternalStore(fn=>{listeners.add(fn);return()=>listeners.delete(fn);},()=>queues.get(key(id))??empty);}
/** 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. */
export 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.');}
export 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;}
export function removeChatPhoto(photo:string,id?:string|null){queues.set(key(id),(queues.get(key(id))??[]).filter(i=>i.id!==photo));listeners.forEach(fn=>fn());}
export function pendingChatPhotos(id?:string|null){return queues.get(key(id))??empty;}
export function takeChatPhotos(id?:string|null){const value=queues.get(key(id))??[];queues.delete(key(id));listeners.forEach(fn=>fn());return value;}
/** The stored picture itself, for uploading it to the host. */
export 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;}
/** 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. */
export 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;}
export 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);});}

/** Keep at most three pictures in the inference context, including this turn. Originals remain in IndexedDB. */
export 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;}