import{Controller}from"../base.mjs";import{ParentView}from"../../view/base.mjs";import{FormView}from"../../view/forms/base.mjs";import{TableView,ModelTableView}from"../../view/table.mjs";import{StringInputView,TextInputView,FloatInputView,NumberInputView,FormInputView,RepeatableInputView,SearchListInputView,ButtonInputView}from"../../view/forms/input.mjs";import{isEmpty,deepClone}from"../../base/helpers.mjs";class InversionInputView extends SearchListInputView{}class LoraInputView extends SearchListInputView{}class CheckpointInputView extends SearchListInputView{}class LoraFormView extends FormView{static autoSubmit=!0;static fieldSets={LoRA:{model:{label:"Model",class:LoraInputView,config:{required:!0}},weight:{label:"Weight",class:FloatInputView,config:{min:0,value:1,step:.01,required:!0}}}}}class LoraFormInputView extends FormInputView{static formClass=LoraFormView}class MultiLoraInputView extends RepeatableInputView{static memberClass=LoraFormInputView}class MultiInversionInputView extends RepeatableInputView{static memberClass=InversionInputView}class ModelForm extends FormView{static canCancel=!0;static fieldSets={Name:{name:{class:StringInputView,label:"Name",config:{required:!0}}},Model:{checkpoint:{class:CheckpointInputView,label:"Checkpoint",config:{required:!0}},lora:{class:MultiLoraInputView,label:"LoRA"},inversion:{class:MultiInversionInputView,label:"Textual Inversions"}},Engine:{size:{class:NumberInputView,label:"Size",config:{required:!0,value:512,min:128,max:2048,step:8}}},Prompts:{prompt:{class:TextInputView,label:"Prompt"},negative_prompt:{class:TextInputView,label:"Negative Prompt"}}}}class NewModelInputView extends ButtonInputView{static defaultValue="New Model";static className="new-model-input-view"}class ModelManagerController extends Controller{static modelWindowWidth=400;static modelWindowHeight=1e3;static managerWindowWidth=800;static managerWindowHeight=600;async createManager(){this.tableView=new ModelTableView(this.config,this.model.DiffusionModel),this.buttonView=new NewModelInputView(this.config),this.tableView.setColumns({name:"Name",model:"Model",size:"Size",prompt:"Prompt",negative_prompt:"Negative Prompt"}),this.tableView.setFormatter("size",(e=>`${e}px`)),this.tableView.addButton("Edit","fa-solid fa-edit",(async e=>{let t=e.getAttributes();t.checkpoint=t.model,t.lora=isEmpty(e.lora)?[]:e.lora.map((e=>e.getAttributes())),t.inversion=isEmpty(e.inversion)?[]:e.inversion.map((e=>e.model));let i,o=new ModelForm(this.config,deepClone(t));o.onSubmit((async t=>{try{await this.model.put(`/models/${e.name}`,null,null,t),isEmpty(i)||i.remove(),this.tableView.requery()}catch(e){let t=isEmpty(e)?"Couldn't communicate with server.":isEmpty(e.detail)?`${e}`:e.detail;this.notify("error","Couldn't update model",t),o.enable()}})),o.onCancel((()=>i.remove())),i=await this.spawnWindow(`Edit ${e.name}`,o,this.constructor.modelWindowWidth,this.constructor.modelWindowHeight)})),this.tableView.addButton("Delete","fa-solid fa-trash",(async e=>{try{await this.model.delete(`/models/${e.name}`),this.tableView.requery()}catch(e){let t=isEmpty(e)?"Couldn't communicate with server.":isEmpty(e.detail)?`${e}`:e.detail;this.notify("error","Couldn't delete model",t),modelForm.enable()}})),this.buttonView.onChange((()=>{this.showNewModel()}));let e=new ParentView(this.config);return e.addChild(this.tableView),e.addChild(this.buttonView),e}async createModelForm(){let e=new ModelForm(this.config);return e.onSubmit((async e=>{try{await this.model.post("/models",null,null,e);isEmpty(this.newModelWindow)||(this.newModelWindow.remove(),this.newModelWindow=null),isEmpty(this.managerWindow)||isEmpty(this.tableView)||this.tableView.requery()}catch(e){let t=isEmpty(e)?"Couldn't communicate with server.":isEmpty(e.detail)?`${e}`:e.detail;this.notify("Error","Couldn't create model",t)}})),e.onCancel((()=>{this.newModelWindow.remove(),this.newModelWindow=null})),e}async showNewModel(){isEmpty(this.newModelWindow)?(this.newModelWindow=await this.spawnWindow("New Model",this.createModelForm(),this.constructor.modelWindowWidth,this.constructor.modelWindowHeight),this.newModelWindow.onClose((()=>{delete this.newModelWindow}))):this.newModelWindow.focus()}async showManager(){isEmpty(this.managerWindow)?(this.managerWindow=await this.spawnWindow("Model Manager",this.createManager(),this.constructor.managerWindowWidth,this.constructor.managerWindowHeight),this.managerWindow.onClose((()=>{delete this.managerWindow}))):this.managerWindow.focus()}async initialize(){LoraInputView.defaultOptions=async()=>this.model.get("/lora"),CheckpointInputView.defaultOptions=async()=>this.model.get("/checkpoints"),InversionInputView.defaultOptions=async()=>this.model.get("/inversions")}}export{ModelManagerController};
