import{isEmpty,isEquivalent,sleep}from"../../base/helpers.mjs";import{ElementBuilder}from"../../base/builder.mjs";import{View}from"../../view/base.mjs";import{MenuController}from"../menu.mjs";import{CaptionUpsampleFormView}from"../../forms/enfugue/prompts.mjs";const E=new ElementBuilder({message:"enfugue-message-view",conversationStatus:"enfugue-conversation-status-view"});class CaptionUpsamplerConversationView extends View{static pollingInterval=500;static tagName="enfugue-conversation-view";constructor(t,s,e){super(t),this.prompts=s,this.promptIndex=0,this.getStatus=e,this.lastCaptions=[],this.lastTask=null}async startMonitor(){for(;;)try{let t=await this.getStatus();if(-1===["processing","queued"].indexOf(t.status)){this.setFinalStatus(t);break}this.setIntermediateStatus(t),await sleep(this.constructor.pollingInterval)}catch(t){console.error(t),this.notify("error","Invocation Error",`${t}`);break}}async setIntermediateStatus(t){if(isEmpty(t.task)||t.task!==this.lastTask&&(this.node.find(E.getCustomTag("conversationStatus")).content(t.task),this.lastTask=t.task),!isEmpty(t.captions)&&!isEquivalent(this.lastCaptions,t.captions)){let s=this.node.findAll(".agent");if(s[s.length-1].content(E.div().content(t.captions[0])).removeClass("loading"),t.captions.length>1)for(let s=1;s<t.captions.length;s++)this.node.append(E.message().class("agent").content(E.div().content(t.captions[s])));this.lastCaptions=t.captions}if(!isEmpty(t.step)&&!isEmpty(t.total)){let s=t.total/this.prompts.length,e=Math.floor(t.step/s);e!==this.promptIndex&&(this.node.append(E.message().class("user").content(E.div().content(this.prompts[e])),E.message().class("agent loading").content(E.div().content("&hellip;"))),this.promptIndex=e)}let s=(100*(isEmpty(t.progress)?0:t.progress)).toFixed(1);this.node.find(E.getCustomTag("conversationStatus")).css({"background-image":`linear-gradient(to right, var(--theme-color-primary) 0%, var(--theme-color-primary) calc(${s}% - 1px), transparent ${s}%`}),this.node.render()}async setFinalStatus(t){let s=[];if(isEmpty(t.result)||isEmpty(t.result.captions)){if("error"===t.status&&!isEmpty(t.message)){for(let t of this.node.findAll(".loading"))this.node.remove(t);this.node.append(E.message().class("agent").content(E.div().content(`ERROR: ${t.message}`))),this.node.render()}}else{let e=0;for(let n of t.result.captions){s.push(E.message().class("user").content(E.div().content(this.prompts[e++])));for(let t of n)s.push(E.message().class("agent").content(E.div().content(t)))}this.node.content(...s)}}async build(){let t=await super.build();return t.content(E.conversationStatus().content("Loading"),E.message().class("user").content(E.div().content(this.prompts[0])),E.message().class("agent loading").content(E.div().content("&hellip;"))),t}}class CaptionUpsamplerController extends MenuController{static captionUpsampleWindowWidth=400;static captionUpsampleWindowHeight=505;static conversationWindowWidth=400;static conversationWindowHeight=300;static menuName="Caption Upsampler";static menuIcon="fa-solid fa-comments";static menuShortcut="c";async onClick(){this.showCaptionUpsampler()}async startInvocationMonitor(t,s){let e=new CaptionUpsamplerConversationView(this.config,t,(()=>this.model.get(`/invocation/${s.uuid}`)));await this.spawnWindow("Upsampler Conversation",e,this.constructor.conversationWindowWidth,this.constructor.conversationWindowHeight),e.startMonitor()}async showCaptionUpsampler(){if(!isEmpty(this.captionUpsamplerWindow))return void this.captionUpsampleWindow.focus();let t=new CaptionUpsampleFormView(this.config);t.onSubmit((async s=>{t.clearError();try{let e=await this.model.post("/invoke/language",null,null,s);this.startInvocationMonitor(s.prompts,e),t.enable()}catch(s){t.setError(s),t.enable()}})),this.captionUpsampleWindow=await this.spawnWindow("Caption Upsampler",t,this.constructor.captionUpsampleWindowWidth,this.constructor.captionUpsampleWindowHeight)}}export{CaptionUpsamplerController as MenuController};
