import {canonicalNativeWorkflow,encodeNativeWorkflow,type NativeWorkflowContext,type NativeWorkflowInput,type NativeSessionHandle,type NativeSessionCommand,type NativeDesktopEvent,type NativeWorkflowArtifact,type NativeMainReceipt} from '../../../protocol/native-workflow'; import type {WebEngineClient} from './WebEngineClient'; import {sha256Hex} from '../../../protocol/render-assets'; type NativeSessionReply = { status?: string; error?: string; sequence?: number; inputCapabilities?: { pen: boolean; logicalKeys: boolean; horizontalWheel: boolean }; command?: { sequence: number; status: string; error?: string; output?: string; artifacts?: NativeWorkflowArtifact[]; resources?: NativeWorkflowInput['resources']; mainReceipt?: NativeMainReceipt } }; // Each handle owns one native Main and one Python namespace; outside edits invalidate it. export class NativeSessionClient { handle: NativeSessionHandle | null=null; busy=false; private generation=0; private request:AbortController|null=null; private inputTail:Promise=Promise.resolve(); private frameTag=''; private lastEventAt=0; cursorEmbedded=false; inputCapabilities:{pen:boolean;logicalKeys:boolean;horizontalWheel:boolean}={pen:false,logicalKeys:false,horizontalWheel:false}; private readonly onPageHide=()=>{void this.close();}; private static endpoint='/api/blender-session'; constructor(private readonly engine?:WebEngineClient){ window.addEventListener('pagehide',this.onPageHide); } private async call(action:string,body?:unknown,signal?:AbortSignal):Promise{ const session=this.handle;if(!session)throw new Error('SESSION_NOT_CONNECTED'); const response=await fetch(body===undefined?`${NativeSessionClient.endpoint}?id=${encodeURIComponent(session.id)}`:NativeSessionClient.endpoint, {method:body===undefined?'GET':'POST',headers:{'Content-Type':'application/json','X-Blender-Workflow':'1','X-Blender-Session-Action':action,'X-Blender-Session-Token':session.token}, ...(body===undefined?{}:{body:JSON.stringify({...body as object,id:session.id,token:session.token})}),signal:signal??AbortSignal.timeout(15000)}); const result=await response.json();if(!response.ok)throw new Error(result.error??'SESSION_HTTP_FAILED');return result; } async start(context:NativeWorkflowContext,resources?:NativeWorkflowInput['resources'],raw?:{source:ArrayBuffer;desktop:NonNullable}):Promise{ this.engine?.assertNativeServices(); await this.close();const generation=this.generation;this.busy=true; const controller=new AbortController();this.request=controller;const timer=setTimeout(()=>controller.abort(),120000); try{ let source=raw?.source,revision=0; if(!source){ if(!this.engine)throw new Error('SESSION_SOURCE_REQUIRED'); const before=await this.engine.snapshot();source=await this.engine.saveBlend();const after=await this.engine.snapshot(); if(before.snapshot.revision!==after.snapshot.revision)throw new Error('SESSION_SOURCE_CHANGED');revision=after.snapshot.revision; } const unsigned={schemaVersion:1 as const,kind:'PYTHON' as const,context,resources,desktop:raw?.desktop,python:{source:'',textName:'WebConsole.py',console:true}, jobId:`session-${crypto.randomUUID()}`,sourceRevision:revision,sourceBlendSha256:await sha256Hex(source),sourceBlendByteLength:source.byteLength,timeoutMs:600000}; const request={...unsigned,requestSha256:await sha256Hex(new TextEncoder().encode(canonicalNativeWorkflow(unsigned)).buffer)}; const response=await fetch(NativeSessionClient.endpoint,{method:'POST',headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'start','Content-Type':'application/octet-stream'},body:encodeNativeWorkflow(request,source),signal:controller.signal}); const value=await response.json();if(!response.ok)throw new Error(value.error??'SESSION_START_FAILED'); if(generation!==this.generation){await fetch(NativeSessionClient.endpoint,{method:'POST',headers:{'Content-Type':'application/json','X-Blender-Workflow':'1','X-Blender-Session-Action':'close'},body:JSON.stringify(value)});throw new Error('SESSION_CANCELLED');} this.handle=value; if(raw&&(value.desktopProtocolVersion!==2||value.width!==raw.desktop.width||value.height!==raw.desktop.height))throw new Error('DESKTOP_SERVICE_RESTART_REQUIRED'); while(true){const state=await this.call('read',undefined,controller.signal);if(state.status==='READY'){this.inputCapabilities=state.inputCapabilities??this.inputCapabilities;break;}if(state.status==='FAILED')throw new Error(state.error);await new Promise(r=>setTimeout(r,200));} }catch(error){await this.close();throw error;} finally{clearTimeout(timer);this.busy=false;if(this.request===controller)this.request=null;} } async assertCurrent():Promise{ if(!this.handle)throw new Error('SESSION_NOT_CONNECTED'); if(!this.engine)return; const snapshot=await this.engine.snapshot(); if(snapshot.snapshot.revision!==this.handle.sourceRevision||await sha256Hex(await this.engine.saveBlend())!==this.handle.sourceBlendSha256)throw new Error('SESSION_SOURCE_CHANGED_RESET_REQUIRED'); } async execute(command:NativeSessionCommand):Promise<{output:string;pythonError?:string;resources?:NativeWorkflowInput['resources'];mainReceipt?:NativeMainReceipt;scene?:Awaited>;files:{artifact:NativeWorkflowArtifact;bytes:ArrayBuffer}[]}>{ if(this.busy)throw new Error('SESSION_BUSY');this.busy=true; const controller=new AbortController();this.request=controller;const timer=setTimeout(()=>controller.abort(),125000); try{ await this.inputTail;await this.assertCurrent();const handle=this.handle!; if(!this.engine){ const state=await this.call('read',undefined,controller.signal); if(state.command?.status==='RUNNING')throw new Error('SESSION_PREVIOUS_COMMAND_RUNNING'); if(state.command)await this.call('ack',{sequence:state.command.sequence},controller.signal); } const issued=await this.call('command',{command},controller.signal); let result; while(true){const state=await this.call('read',undefined,controller.signal);result=state.command;if(result&&result.sequence===issued.sequence&&result.status!=='RUNNING')break;await new Promise(r=>setTimeout(r,150));} const pythonError=command.kind==='python'&&result.status==='PYTHON_ERROR'; if(result.status!=='SUCCEEDED'&&!pythonError){ const error=Object.assign(new Error((result.error??'SESSION_COMMAND_FAILED')+'\n'+(result.output??'')),{status:result.status,recoverable:!this.engine||result.status==='CANCELLED'||String(result.error??'').startsWith('SESSION_MODAL_ACTIVE_')}); if(error.recoverable)await this.call('ack',{sequence:issued.sequence},controller.signal); throw error; } const files:{artifact:NativeWorkflowArtifact;bytes:ArrayBuffer}[]=[];let total=0; for(const artifact of result.artifacts??[]){ if(!Number.isSafeInteger(artifact.bytes)||artifact.bytes<1||(total+=artifact.bytes)>512*1024*1024)throw new Error('SESSION_ARTIFACT_BUDGET'); const url=`${NativeSessionClient.endpoint}?id=${handle.id}&artifact=${encodeURIComponent(artifact.id)}`; const response=await fetch(url,{headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'artifact','X-Blender-Session-Token':handle.token},signal:controller.signal}); if(!response.ok)throw new Error('SESSION_ARTIFACT_DOWNLOAD');const bytes=await response.arrayBuffer(); if(bytes.byteLength!==artifact.bytes||await sha256Hex(bytes)!==artifact.sha256)throw new Error('SESSION_ARTIFACT_HASH');files.push({artifact,bytes}); } await this.assertCurrent();controller.signal.throwIfAborted(); const blend=files.find(f=>f.artifact.mime==='application/x-blender');let scene; if(pythonError&&(!blend||typeof result.error!=='string'))throw new Error('SESSION_PYTHON_ERROR_RESULT_INVALID'); if(blend&&this.engine){ await this.engine.adoptNativeResources(result.resources); await this.assertCurrent();controller.signal.throwIfAborted(); scene=await this.engine.applyServerBlendResult(blend.bytes.slice(0),handle.sourceRevision); handle.sourceRevision=scene.snapshot.revision;handle.sourceBlendSha256=await sha256Hex(await this.engine.saveBlend()); } // Scene commit has finished; losing a later ACK must not misreport that commit as failed. if(scene){clearTimeout(timer);await this.call('ack',{sequence:issued.sequence}).catch(()=>this.close());} else await this.call('ack',{sequence:issued.sequence},controller.signal); return {output:result.output??'',...(pythonError?{pythonError:result.error}:{}),resources:result.resources,mainReceipt:result.mainReceipt,files,scene}; }catch(error){if(this.engine&&!(error as {recoverable?:boolean})?.recoverable)await this.close();throw error;} finally{clearTimeout(timer);this.busy=false;if(this.request===controller)this.request=null;} } events(events:NativeDesktopEvent[]):Promise{ if(!this.handle)return Promise.resolve(); const generation=this.generation; this.inputTail=this.inputTail.catch(()=>undefined).then(async()=>{ const delay=10-(Date.now()-this.lastEventAt);if(delay>0)await new Promise(resolve=>setTimeout(resolve,delay)); if(generation===this.generation){await this.call('events',{events});this.lastEventAt=Date.now();} }); return this.inputTail; } cancelModal():Promise{return this.events([{type:'key',code:'Escape',down:true},{type:'key',code:'Escape',down:false}]);} async upload(file:File,path:string):Promise{ const handle=this.handle;if(!handle)throw new Error('SESSION_NOT_CONNECTED'); const response=await fetch(`${NativeSessionClient.endpoint}?id=${handle.id}`,{method:'POST',headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'upload','X-Blender-Session-Token':handle.token,'X-Blender-File-Path':encodeURIComponent(path),'Content-Type':'application/octet-stream'},body:file,signal:AbortSignal.timeout(120000)}); if(!response.ok)throw new Error((await response.json()).error??'SESSION_UPLOAD_FAILED'); } async files():Promise<{files:{path:string;bytes:number;modified:number}[];links?:{id:string;url:string}[];network?:{mode:string;hosts:string[];lastError:string|null};preferenceError?:string;status?:{dirty:boolean;filepath:string;workspace:string;savedSequence:number;fileBrowser:boolean;error?:string}}> { return this.call>>('files'); } async acknowledgeLink(linkId:string):Promise{await this.call('link-ack',{linkId});} async download(path:string):Promise{ const handle=this.handle;if(!handle)throw new Error('SESSION_NOT_CONNECTED'); const response=await fetch(`${NativeSessionClient.endpoint}?id=${handle.id}&file=${encodeURIComponent(path)}`,{headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'download','X-Blender-Session-Token':handle.token},signal:AbortSignal.timeout(120000)}); if(!response.ok)throw new Error((await response.json()).error??'SESSION_DOWNLOAD_FAILED');return response.blob(); } async screen(signal?:AbortSignal):Promise{ const handle=this.handle;if(!handle)return null; const response=await fetch(`${NativeSessionClient.endpoint}?id=${handle.id}`,{headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'screen','X-Blender-Session-Token':handle.token,...(this.frameTag?{'If-None-Match':this.frameTag}:{})},signal:signal??AbortSignal.timeout(15000)}); if(response.status===204||response.status===304)return null;if(!response.ok)throw new Error('SESSION_SCREEN_UNAVAILABLE');this.frameTag=response.headers.get('ETag')??'';this.cursorEmbedded=response.headers.get('X-Blender-Cursor')==='embedded';return response.blob(); } dispose():void{window.removeEventListener('pagehide',this.onPageHide);void this.close();} async close():Promise{ this.generation++;this.request?.abort();this.request=null;const handle=this.handle;this.handle=null; this.inputTail=Promise.resolve();this.frameTag=''; if(handle)await fetch(NativeSessionClient.endpoint,{method:'POST',keepalive:true,headers:{'X-Blender-Workflow':'1','X-Blender-Session-Action':'close','Content-Type':'application/json'},body:JSON.stringify(handle),signal:AbortSignal.timeout(5000)}).catch(()=>undefined); } }