From 15a8366f9c58f942e85f2ccd82cd991c2a8ef8fc Mon Sep 17 00:00:00 2001 From: nogueiraguh Date: Sun, 8 Mar 2026 19:09:28 -0300 Subject: [PATCH] fix: delete terraform workspace and files when deleting chat session --- backend/app.py | 7 +++++++ frontend/index.html | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/app.py b/backend/app.py index 3dec1cc..6cd88b8 100644 --- a/backend/app.py +++ b/backend/app.py @@ -3518,6 +3518,13 @@ async def rename_session(sid: str, req: dict = Body(...), u=Depends(current_user @app.delete("/api/chat/{sid}") async def clear_chat(sid, u=Depends(current_user)): with db() as c: + # Delete associated terraform workspaces and their files + ws_rows = c.execute("SELECT id FROM terraform_workspaces WHERE session_id=? AND user_id=?", (sid, u["id"])).fetchall() + for ws in ws_rows: + wdir = TERRAFORM_DIR / ws["id"] + if wdir.exists(): + shutil.rmtree(wdir, ignore_errors=True) + c.execute("DELETE FROM terraform_workspaces WHERE session_id=? AND user_id=?", (sid, u["id"])) c.execute("DELETE FROM chat_messages WHERE session_id=? AND user_id=?", (sid, u["id"])) c.execute("DELETE FROM chat_sessions WHERE id=? AND user_id=?", (sid, u["id"])) return {"ok": True} diff --git a/frontend/index.html b/frontend/index.html index 5113c15..51cd43d 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -1062,7 +1062,11 @@ function tfSwitchBtab(tab){ function tfToggleModelDrop(){const dd=document.getElementById('tfmdd');dd.classList.toggle('open');S.tfMdOpen=dd.classList.contains('open');if(S.tfMdOpen){const inp=document.getElementById('tfmds');if(inp){inp.value='';inp.focus();tfFilterModels('')}}} function tfFilterModels(q){const list=document.getElementById('tfmdl');if(!list)return;const items=list.querySelectorAll('.itm,.grp');const ql=q.toLowerCase();let lastGrp=null,vis=false;items.forEach(el=>{if(el.classList.contains('grp')){if(lastGrp)lastGrp.style.display=vis?'':'none';lastGrp=el;vis=false}else{const show=!q||el.textContent.toLowerCase().includes(ql);el.style.display=show?'':'none';if(show)vis=true}});if(lastGrp)lastGrp.style.display=vis?'':'none'} function tfTogglePanel(p){S.tfPanel=S.tfPanel===p?'':p;R()} -async function tfClear(){if(S.tfSid)try{await $api('/chat/'+S.tfSid,{method:'DELETE'})}catch(e){}S.tfMsgs=[];S.tfSid=null;S.tfPlan=[];S.tfCode='';S.tfFiles=[];S.tfWs=null;S.tfPlanOut='';S.tfApplyOut='';S.tfDestroyOut='';S.tfStatus='draft';S.tfRunning=false;S.tfConfirmDest=false;S.tfComps=[];S.tfCompartment='';S.tfResources=null;S.tfResLoading=false;R()} +async function tfClear(){ + if(S.tfWs)try{await $api('/terraform/workspaces/'+S.tfWs,{method:'DELETE'})}catch(e){} + if(S.tfSid)try{await $api('/chat/'+S.tfSid,{method:'DELETE'})}catch(e){} + S.tfMsgs=[];S.tfSid=null;S.tfPlan=[];S.tfCode='';S.tfFiles=[];S.tfWs=null;S.tfPlanOut='';S.tfApplyOut='';S.tfDestroyOut='';S.tfStatus='draft';S.tfRunning=false;S.tfConfirmDest=false;S.tfComps=[];S.tfCompartment='';S.tfResources=null;S.tfResLoading=false;R() +} function tfScroll(){setTimeout(()=>{const e=document.getElementById('tfchm');if(e)e.scrollTop=e.scrollHeight},50)} function copyTfCode(){navigator.clipboard.writeText(S.tfCode).then(()=>alert('Código copiado!'))} function dlTfCode(){if(!S.tfFiles.length)return;if(S.tfFiles.length===1){const f=S.tfFiles[0];const b=new Blob([f.content],{type:'text/plain'});const a=document.createElement('a');a.href=URL.createObjectURL(b);a.download=f.name;a.click();URL.revokeObjectURL(a.href)}else{tfDlAll()}}