circuschief 0.1.4 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/package.json +1 -1
  2. package/packages/server/src/api/commands.js +50 -55
  3. package/packages/server/src/api/projects-helpers.js +13 -4
  4. package/packages/server/src/api/projects.js +33 -18
  5. package/packages/server/src/cli.js +82 -0
  6. package/packages/server/src/db/AgentCallLogRepository.js +30 -31
  7. package/packages/server/src/db/ConversationRepository.js +27 -16
  8. package/packages/server/src/db/ProjectRepository.js +21 -31
  9. package/packages/server/src/db/QuickResponseRepository.js +14 -19
  10. package/packages/server/src/db/migrations/sessionsMigrations.js +61 -61
  11. package/packages/server/src/index.js +42 -29
  12. package/packages/server/src/services/commandRunner.js +52 -99
  13. package/packages/server/src/services/kanbanTriggers.js +83 -56
  14. package/packages/server/src/services/schedulerService.js +68 -44
  15. package/packages/server/src/services/sessionExecution.js +102 -61
  16. package/packages/server/src/services/sessionManager.js +63 -37
  17. package/packages/server/src/services/summaryService.js +56 -53
  18. package/packages/server/src/services/templateTriggerService.js +58 -31
  19. package/packages/server/src/ws/WebSocketManager.js +5 -0
  20. package/packages/web/dist/assets/ActiveSessionsView-3697sD8N.js +1 -0
  21. package/packages/web/dist/assets/ActiveSessionsView-DfYXc6dz.css +1 -0
  22. package/packages/web/dist/assets/{AgentLogsView-c42v_j_5.js → AgentLogsView-D4l0N9ZA.js} +1 -1
  23. package/packages/web/dist/assets/{ArchiveConfirmModal-DBuOmtXu.js → ArchiveConfirmModal-Bv3vGOMM.js} +1 -1
  24. package/packages/web/dist/assets/{CommandButtonDetailView-CkKJ3Htz.js → CommandButtonDetailView-Bk_SHxpu.js} +1 -1
  25. package/packages/web/dist/assets/{EffortLevelSelector-BHJHSqul.js → EffortLevelSelector-VfBEelvO.js} +1 -1
  26. package/packages/web/dist/assets/{GeneralSettingsView-CdxfteZ2.js → GeneralSettingsView-BqCzCX-z.js} +1 -1
  27. package/packages/web/dist/assets/{InterpolationHelp-DabnHhE4.js → InterpolationHelp-Dc1Y0T6v.js} +1 -1
  28. package/packages/web/dist/assets/MarkdownEditor-DwBQkZbs.js +2 -0
  29. package/packages/web/dist/assets/{ModelSelector-BWIU4ud7.js → ModelSelector-DSxaZWBL.js} +1 -1
  30. package/packages/web/dist/assets/{NewSessionView-BIZl8QlH.js → NewSessionView-BsI7JtO9.js} +2 -2
  31. package/packages/web/dist/assets/{PathChooser-nhat_Pz4.js → PathChooser-CXFxb8Oj.js} +1 -1
  32. package/packages/web/dist/assets/{ProjectEditView-DD-2_VrW.js → ProjectEditView-Bes4Mib4.js} +1 -1
  33. package/packages/web/dist/assets/{ProjectListView-BOWbfoXQ.js → ProjectListView-DzEu-C36.js} +1 -1
  34. package/packages/web/dist/assets/{ProjectNewView-DC4uvSn2.js → ProjectNewView-Cv-iEAgl.js} +1 -1
  35. package/packages/web/dist/assets/ProvidersView-CgAr0qms.js +1 -0
  36. package/packages/web/dist/assets/{QuickResponseSettings-Bk9mq96x.js → QuickResponseSettings-uDDpwaza.js} +1 -1
  37. package/packages/web/dist/assets/{QuickResponsesPanel-BRvcnkQr.js → QuickResponsesPanel-D0qs0Fm_.js} +1 -1
  38. package/packages/web/dist/assets/{ResizableTextarea-CwGM4P3c.js → ResizableTextarea-_kHi1Mg3.js} +1 -1
  39. package/packages/web/dist/assets/{SessionCard-BGDVHU9u.js → SessionCard-Be1-bK0C.js} +1 -1
  40. package/packages/web/dist/assets/{SessionCard-D20G3bX8.css → SessionCard-CcqIjL8q.css} +1 -1
  41. package/packages/web/dist/assets/{SessionDetailView-CHYrx2Ab.js → SessionDetailView-DUYb7qTA.js} +17 -17
  42. package/packages/web/dist/assets/{SessionDetailView-7bWgC7Es.css → SessionDetailView-mnGRMaLY.css} +1 -1
  43. package/packages/web/dist/assets/{SessionFormOptions-8qvL25ca.js → SessionFormOptions-DvhOyP6z.js} +1 -1
  44. package/packages/web/dist/assets/{SessionListView-BAIBtJF7.css → SessionListView-78k6TTz6.css} +1 -1
  45. package/packages/web/dist/assets/SessionListView-CuHsWj85.js +1 -0
  46. package/packages/web/dist/assets/{SessionLogStream-B-w3n4c3.js → SessionLogStream-Da_GniUZ.js} +1 -1
  47. package/packages/web/dist/assets/{SettingsView-Dd0ZJ4Nv.js → SettingsView-5RDCXNUa.js} +1 -1
  48. package/packages/web/dist/assets/{SlashCommandWizard-CzyLjsdJ.js → SlashCommandWizard-B_8ifpxN.js} +1 -1
  49. package/packages/web/dist/assets/{SummarySettingsView-DTbh7uAF.js → SummarySettingsView-KvgSGHdd.js} +1 -1
  50. package/packages/web/dist/assets/{TemplateDetailView-BOnhkdtH.js → TemplateDetailView-BhOjYIvS.js} +1 -1
  51. package/packages/web/dist/assets/{commandButtons-CY87n64i.js → commandButtons-B4OYZP0J.js} +1 -1
  52. package/packages/web/dist/assets/{index-DxboI9i-.js → index-80Qu7W6P.js} +1 -1
  53. package/packages/web/dist/assets/{index-NzLFVaCi.js → index-B8_Iqwcq.js} +1 -1
  54. package/packages/web/dist/assets/{index-aCw-iXPX.js → index-B9JErft2.js} +1 -1
  55. package/packages/web/dist/assets/{index-Ce6sL47U.js → index-BHVnr8MO.js} +1 -1
  56. package/packages/web/dist/assets/{index-BXUcbV4K.js → index-BarVnQIj.js} +1 -1
  57. package/packages/web/dist/assets/{index--OtPwBbF.js → index-BqVgX_Jy.js} +3 -3
  58. package/packages/web/dist/assets/{index-gMpnPf1V.js → index-BsvRdU0B.js} +1 -1
  59. package/packages/web/dist/assets/{index-BRUlEEHm.js → index-Bugg2M-E.js} +1 -1
  60. package/packages/web/dist/assets/{index-Dx0sYW7H.js → index-C2Pjy-M8.js} +1 -1
  61. package/packages/web/dist/assets/{index-DkLkDgig.js → index-CSOPrlmq.js} +23 -23
  62. package/packages/web/dist/assets/{index-CO4EBOFw.js → index-CS_wb_Vj.js} +1 -1
  63. package/packages/web/dist/assets/{index-CjHb9rXv.js → index-ClzNIdCp.js} +1 -1
  64. package/packages/web/dist/assets/{index-DPwwgloE.js → index-Cn9Ajkye.js} +1 -1
  65. package/packages/web/dist/assets/{index-i1o916sk.js → index-CucpVX4L.js} +1 -1
  66. package/packages/web/dist/assets/{index-C6m-WfqP.js → index-D9hZYvW3.js} +1 -1
  67. package/packages/web/dist/assets/{index-jGjvGBfk.js → index-DA0dK_PG.js} +1 -1
  68. package/packages/web/dist/assets/{index-Bi4bQ_UB.js → index-DgpSn-jR.js} +1 -1
  69. package/packages/web/dist/assets/{index-DcA6pqXV.js → index-HZwIyC9t.js} +1 -1
  70. package/packages/web/dist/assets/{index-BshkV3r5.js → index-SS3wA2sI.js} +1 -1
  71. package/packages/web/dist/assets/{projects-C2Y29PSJ.js → projects-B2du-GX8.js} +1 -1
  72. package/packages/web/dist/assets/{providers-CeJXuo0Q.js → providers-B__J6FX0.js} +1 -1
  73. package/packages/web/dist/assets/sessions-VDrd87yA.js +1 -0
  74. package/packages/web/dist/assets/{settings-BplIxCbi.js → settings-CZ7Pc-Pt.js} +1 -1
  75. package/packages/web/dist/index.html +1 -1
  76. package/packages/web/dist/assets/ActiveSessionsView-BryJ-V3f.js +0 -1
  77. package/packages/web/dist/assets/ActiveSessionsView-ofSvx-K1.css +0 -1
  78. package/packages/web/dist/assets/MarkdownEditor-k4zBLGqU.js +0 -2
  79. package/packages/web/dist/assets/ProvidersView-DT5afh1V.js +0 -1
  80. package/packages/web/dist/assets/SessionListView-927Yq6Il.js +0 -1
  81. package/packages/web/dist/assets/sessions-CMby7ij3.js +0 -1
@@ -1 +0,0 @@
1
- import{U as _,L as y}from"./index-DkLkDgig.js";import{a as c}from"./ApiClient-Dbs1H78V.js";import{u as p}from"./settings-BplIxCbi.js";function R(){return Math.random().toString(16).substring(2,6)}function d(e,s=_){return e?Math.round((e.inputTokens||0)*s.input+(e.outputTokens||0)*s.output+(e.cacheReadInputTokens||0)*s.cacheRead+(e.cacheCreationInputTokens||0)*s.cacheCreation):0}function w(e){return!e||e===0?"0":e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:String(e)}function x(e,s=30){return e.toLowerCase().replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"").substring(0,s)}function A(e){const s=["a","an","the","to","and","or","for","in","on","at","of","with","is","it","this","that","i","me","my","we","our","you","your","can","please","help"];return e.toLowerCase().replace(/[^a-z0-9\s]/g,"").split(/\s+/).filter(i=>i.length>1&&!s.includes(i)).slice(0,4).join("-")||"session"}function O(e,s){const t=R();let i;return s&&s.trim()?i=x(A(s.trim())):i="session",i||(i="session"),`claude-tools/${t}-${i}`}const L=y("sessionFilters",{state:()=>({statusFilter:null,starredFilter:null,scheduledFilter:null}),actions:{setStatusFilter(e){this.statusFilter=e,this.saveStatusFilter()},saveStatusFilter(){try{this.statusFilter?localStorage.setItem("sessionStatusFilter",this.statusFilter):localStorage.removeItem("sessionStatusFilter")}catch(e){console.warn("Failed to save status filter:",e)}},restoreStatusFilter(){try{const e=localStorage.getItem("sessionStatusFilter");(e==="running"||e==="idle")&&(this.statusFilter=e)}catch(e){console.warn("Failed to restore status filter:",e)}},setStarredFilter(e){this.starredFilter=e,this.saveStarredFilter()},saveStarredFilter(){try{this.starredFilter?sessionStorage.setItem("sessionStarredFilter",this.starredFilter):sessionStorage.removeItem("sessionStarredFilter")}catch(e){console.warn("Failed to save starred filter:",e)}},restoreStarredFilter(){try{const e=sessionStorage.getItem("sessionStarredFilter");e==="starred"||e==="unstarred"?this.starredFilter=e:this.starredFilter=null}catch(e){console.warn("Failed to restore starred filter:",e)}},setScheduledFilter(e){this.scheduledFilter=e,this.saveScheduledFilter()},saveScheduledFilter(){try{this.scheduledFilter?sessionStorage.setItem("sessionScheduledFilter",this.scheduledFilter):sessionStorage.removeItem("sessionScheduledFilter")}catch(e){console.warn("Failed to save scheduled filter:",e)}},restoreScheduledFilter(){try{const e=sessionStorage.getItem("sessionScheduledFilter");e==="scheduled"||e==="not-scheduled"?this.scheduledFilter=e:this.scheduledFilter=null}catch(e){console.warn("Failed to restore scheduled filter:",e)}}}});function g(e){return e==null?"-":e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(1)}K`:String(e)}function I(e,s,t,i){return{input:g(e),output:g(s),total:g((e||0)+(s||0)),cacheRead:g(t),cacheCreation:g(i)}}function T(e){return e.runningUsage?!e.activeConversationId||e.runningUsage.conversationId===e.activeConversationId:!1}function l(e){return e.conversations.find(s=>s.id===e.activeConversationId)}function k(e,s){return{inputTokens:((e==null?void 0:e.inputTokens)||0)+(s.inputTokens||0),outputTokens:((e==null?void 0:e.outputTokens)||0)+(s.outputTokens||0),cacheReadInputTokens:((e==null?void 0:e.cacheReadInputTokens)||0)+(s.cacheReadInputTokens||0),cacheCreationInputTokens:((e==null?void 0:e.cacheCreationInputTokens)||0)+(s.cacheCreationInputTokens||0)}}function C(e){return{inputTokens:e.inputTokens,outputTokens:e.outputTokens,cacheReadInputTokens:e.cacheReadInputTokens,cacheCreationInputTokens:e.cacheCreationInputTokens}}const U={conversationTokens:e=>{const s=e.conversations.find(t=>t.id===e.activeConversationId);return s?{inputTokens:s.inputTokens||0,outputTokens:s.outputTokens||0,cacheReadInputTokens:s.cacheReadInputTokens||0,cacheCreationInputTokens:s.cacheCreationInputTokens||0,webSearchRequests:s.webSearchRequests||0,contextWindow:s.contextWindow||2e5}:null},totalTokens:e=>{const s=e.conversations.find(t=>t.id===e.activeConversationId);return s?(s.inputTokens||0)+(s.outputTokens||0):e.currentSession?(e.currentSession.inputTokens||0)+(e.currentSession.outputTokens||0):0},formattedTokens:e=>{if(T(e)){const s=k(l(e),e.runningUsage);return I(s.inputTokens,s.outputTokens,s.cacheReadInputTokens,s.cacheCreationInputTokens)}if(e.activeConversationId&&e.conversations.length>0){const s=l(e);if(s)return I(s.inputTokens,s.outputTokens,s.cacheReadInputTokens,s.cacheCreationInputTokens)}return{input:"-",output:"-",total:"-",cacheRead:"-",cacheCreation:"-"}},contextPercentage:e=>{if(T(e)){const n=l(e),o=k(n,e.runningUsage),r=o.inputTokens+o.outputTokens,a=e.runningUsage.contextWindow||(n==null?void 0:n.contextWindow)||2e5;return Math.min(100,Math.round(r/a*100))}const t=l(e)||e.currentSession;if(!t)return 0;const i=(t.inputTokens||0)+(t.outputTokens||0);return Math.min(100,Math.round(i/(t.contextWindow||2e5)*100))},isUsageUpdating:e=>!(!e.runningUsage||e.activeConversationId&&e.runningUsage.conversationId!==e.activeConversationId),getConversationDisplayTokens:e=>s=>{const t=e.conversations.find(i=>i.id===s);if(!t)return{inputTokens:0,outputTokens:0,total:0};if(e.runningUsage&&e.runningUsage.conversationId===s){const i=(t.inputTokens||0)+(e.runningUsage.inputTokens||0),n=(t.outputTokens||0)+(e.runningUsage.outputTokens||0);return{inputTokens:i,outputTokens:n,total:i+n}}return{inputTokens:t.inputTokens||0,outputTokens:t.outputTokens||0,total:(t.inputTokens||0)+(t.outputTokens||0)}},billableTokens:e=>{const t=p().tokenCostWeights;if(T(e))return d(k(l(e),e.runningUsage),t);if(e.activeConversationId&&e.conversations.length>0){const i=l(e);if(i)return d(C(i),t)}return 0},formattedBillableTokens:e=>{const t=p().tokenCostWeights;if(T(e))return w(d(k(l(e),e.runningUsage),t));if(e.activeConversationId&&e.conversations.length>0){const i=l(e);if(i)return w(d(C(i),t))}return"-"},getConversationBillableTokens:e=>s=>{const i=p().tokenCostWeights,n=e.conversations.find(o=>o.id===s);return n?e.runningUsage&&e.runningUsage.conversationId===s?d({inputTokens:(n.inputTokens||0)+(e.runningUsage.inputTokens||0),outputTokens:(n.outputTokens||0)+(e.runningUsage.outputTokens||0),cacheReadInputTokens:(n.cacheReadInputTokens||0)+(e.runningUsage.cacheReadInputTokens||0),cacheCreationInputTokens:(n.cacheCreationInputTokens||0)+(e.runningUsage.cacheCreationInputTokens||0)},i):d({inputTokens:n.inputTokens,outputTokens:n.outputTokens,cacheReadInputTokens:n.cacheReadInputTokens,cacheCreationInputTokens:n.cacheCreationInputTokens},i):0},getFormattedConversationBillableTokens:e=>s=>{const i=p().tokenCostWeights,n=e.conversations.find(r=>r.id===s);if(!n)return"-";let o;return e.runningUsage&&e.runningUsage.conversationId===s?o={inputTokens:(n.inputTokens||0)+(e.runningUsage.inputTokens||0),outputTokens:(n.outputTokens||0)+(e.runningUsage.outputTokens||0),cacheReadInputTokens:(n.cacheReadInputTokens||0)+(e.runningUsage.cacheReadInputTokens||0),cacheCreationInputTokens:(n.cacheCreationInputTokens||0)+(e.runningUsage.cacheCreationInputTokens||0)}:o={inputTokens:n.inputTokens,outputTokens:n.outputTokens,cacheReadInputTokens:n.cacheReadInputTokens,cacheCreationInputTokens:n.cacheCreationInputTokens},w(d(o,i))},getSessionBillableTokens:e=>s=>{var f;const i=p().tokenCostWeights,n=h=>h?d({inputTokens:h.inputTokens||0,outputTokens:h.outputTokens||0,cacheReadInputTokens:h.cacheReadInputTokens||0,cacheCreationInputTokens:h.cacheCreationInputTokens||0},i):0,o=e.sessions.find(h=>h.id===s)||(((f=e.currentSession)==null?void 0:f.id)===s?e.currentSession:null);if(!o)return 0;let r=n(o);const a=[s],u=new Set;for(;a.length>0;){const h=a.pop();if(u.has(h))continue;u.add(h);const m=e.sessions.filter(S=>S.parentSessionId===h);for(const S of m)r+=n(S),a.push(S.id)}return r}};function v(e,s,t=!1){const i=e,n=i.findIndex(o=>o.id===s.id);n!==-1?i[n]={...i[n],...s}:t&&i.unshift(s)}async function P(e,s,t){try{const o=(await c.getProjectSessions(s)).filter(r=>r.parentSessionId===t).filter(r=>!e.find(a=>a.id===r.id));e.push(...o)}catch(i){console.error("Failed to fetch child sessions:",i)}}function F(e,s,t){const i=e.find(o=>o.id===t.id),n=i?{...i,...t}:t;return v(s,n,!0),e.filter(o=>o.id!==t.id)}async function b(e,s){let t=s;for(;t;){const i=e.find(n=>n.id===t);if(i){t=i.parentSessionId;continue}try{const n=await c.getSession(t);e.push(n),t=n.parentSessionId}catch(n){console.error("Failed to fetch parent session:",n);break}}}const W={async fetchActiveSessions(e=!0){e&&(this.loading=!0),this.error=null;try{this.activeSessions=await c.getActiveSessions()}catch(s){this.error=s.message}finally{e&&(this.loading=!1)}},async fetchScheduledSessions(e=null){this.loadingScheduled=!0,this.error=null;try{const s=await c.getScheduledSessions(e);this.scheduledSessions=s.sort((t,i)=>new Date(t.scheduledAt)-new Date(i.scheduledAt))}catch(s){this.error=s.message,console.error("Failed to fetch scheduled sessions:",s)}finally{this.loadingScheduled=!1}},async fetchSessions(e){this.loading=!0,this.error=null;try{this.sessions=await c.getProjectSessions(e,!1,null)}catch(s){this.error=s.message}finally{this.loading=!1}},async fetchArchivedSessions(e,{reset:s=!0}={}){s&&(this.archivedSessions=[],this.archivedPagination.offset=0),this.archivedPagination.loading=!0,this.error=null;try{const i=await c.getProjectSessions(e,!0,this.starredFilter,{limit:25,offset:this.archivedPagination.offset});this.archivedSessions=s?i.sessions:[...this.archivedSessions,...i.sessions],this.archivedPagination={total:i.pagination.total,offset:this.archivedPagination.offset+i.sessions.length,hasMore:i.pagination.hasMore,loading:!1}}catch(i){this.error=i.message,this.archivedPagination.loading=!1}},async loadMoreArchivedSessions(e){this.archivedPagination.hasMore&&!this.archivedPagination.loading&&await this.fetchArchivedSessions(e,{reset:!1})},async fetchSession(e,s=!0){var t,i;s&&(this.loading=!0),this.error=null;try{const n=await c.getSession(e);if(this.viewedSessionId&&this.viewedSessionId!==e){const r=this.sessions.findIndex(a=>a.id===e);r!==-1&&(this.sessions[r]={...this.sessions[r],...n});return}this.currentSession=n;const o=this.sessions.findIndex(r=>r.id===e);o!==-1?this.sessions[o]=n:this.sessions.push(n),(t=this.currentSession)!=null&&t.parentSessionId&&await b(this.sessions,this.currentSession.parentSessionId),(i=this.currentSession)!=null&&i.projectId&&await P(this.sessions,this.currentSession.projectId,e)}catch(n){this.error=n.message}finally{s&&(this.loading=!1)}},async createSession(e,s){this.loading=!0,this.error=null;try{const t=await c.createSession(e,s);return this.sessions.unshift(t),t}catch(t){throw this.error=t.message,t}finally{this.loading=!1}},async sendMessage(e,s,t=[],i=null){this.error=null;try{await c.sendMessage(e,s,t,i),this._updateSessionInAllLists(e,{status:"running"})}catch(n){throw this.error=n.message,n}},async stopSession(e){this.error=null;try{await c.stopSession(e),this._updateSessionInAllLists(e,{status:"stopped"})}catch(s){throw this.error=s.message,s}},async restartSession(e){this.error=null;try{await c.restartSession(e),this._updateSessionInAllLists(e,{status:"stopped",error:null})}catch(s){throw this.error=s.message,s}},async startSession(e,s=void 0,t=void 0){this.error=null;try{const i=await c.startSession(e,s,t);return this._updateSessionInAllLists(e,{status:"starting"}),i}catch(i){throw this.error=i.message,i}},async deleteSession(e){var s;this.error=null;try{await c.deleteSession(e),this.sessions=this.sessions.filter(t=>t.id!==e),this.archivedSessions=this.archivedSessions.filter(t=>t.id!==e),((s=this.currentSession)==null?void 0:s.id)===e&&(this.currentSession=null)}catch(t){throw this.error=t.message,t}},async archiveSession(e,{cleanup:s=!1}={}){var t;this.error=null;try{const i=await c.archiveSession(e,{cleanup:s});return this.sessions=this.sessions.filter(n=>n.id!==e),this.archivedSessions.unshift(i),this.activeSessions=this.activeSessions.filter(n=>n.id!==e),((t=this.currentSession)==null?void 0:t.id)===e&&(this.currentSession={...this.currentSession,archived:!0}),i}catch(i){throw this.error=i.message,i}},async unarchiveSession(e){var s;this.error=null;try{const t=await c.unarchiveSession(e);return this.archivedSessions=this.archivedSessions.filter(i=>i.id!==e),this.sessions.unshift(t),((s=this.currentSession)==null?void 0:s.id)===e&&(this.currentSession={...this.currentSession,archived:!1}),t}catch(t){throw this.error=t.message,t}},async duplicateSession(e,s={}){this.error=null;try{return await c.duplicateSession(e,s)}catch(t){throw this.error=t.message,t}},async toggleSessionStar(e){this.error=null;try{const s=await c.toggleSessionStar(e);return this._updateSessionInAllLists(e,{starred:s.starred}),s}catch(s){throw this.error=s.message,s}},updateSessionStatus(e,s){const t=this.sessions.find(o=>o.id===e)||this.currentSession,i=(t==null?void 0:t.status)==="running",n={status:s};i&&(s==="waiting"||s==="completed")&&(n.hasResponses=!0),this._updateSessionInAllLists(e,n)},async updateSessionThinking(e,s){return this.updateSessionFields(e,{thinkingEnabled:s})},async updateSessionMode(e,s){return this.updateSessionFields(e,{mode:s})},async updateSessionModel(e,s,t=void 0){var i;this.error=null;try{const n={model:s};t!==void 0&&(n.providerId=t);const o=this.sessions.find(a=>a.id===e)||(((i=this.currentSession)==null?void 0:i.id)===e?this.currentSession:null);o&&o.status==="waiting"&&(n.pendingModel=s);const r=await c.updateSession(e,n);return this._updateSessionInAllLists(e,n),r}catch(n){throw this.error=n.message,n}},async updateNextTemplate(e,s){return this.updateSessionFields(e,{nextTemplateId:s})},async updateAutoSendPendingPrompt(e,s){return this.updateSessionFields(e,{autoSendPendingPrompt:s})},async updateSessionFields(e,s){this.error=null;try{const t=await c.updateSession(e,s);return this._updateSessionInAllLists(e,s),t}catch(t){throw this.error=t.message,t}},updateSession(e){var s;e!=null&&e.id&&(e.archived===!0?(this.sessions=F(this.sessions,this.archivedSessions,e),this.activeSessions=this.activeSessions.filter(t=>t.id!==e.id)):e.archived===!1?this.archivedSessions=F(this.archivedSessions,this.sessions,e):(v(this.sessions,e),v(this.archivedSessions,e)),((s=this.currentSession)==null?void 0:s.id)===e.id&&(this.currentSession={...this.currentSession,...e}),v(this.activeSessions,e),e.status==="scheduled"?(v(this.scheduledSessions,e,!0),this.scheduledSessions.sort((t,i)=>new Date(t.scheduledAt)-new Date(i.scheduledAt))):this.scheduledSessions=this.scheduledSessions.filter(t=>t.id!==e.id))},addSessionToList(e){e!=null&&e.id&&(this.sessions.some(s=>s.id===e.id)||this.sessions.unshift(e),["running","waiting","starting"].includes(e.status)&&(this.activeSessions.some(s=>s.id===e.id)||this.activeSessions.unshift(e)))},removeSessionFromList(e){var s;e&&(this.sessions=this.sessions.filter(t=>t.id!==e),this.archivedSessions=this.archivedSessions.filter(t=>t.id!==e),this.activeSessions=this.activeSessions.filter(t=>t.id!==e),((s=this.currentSession)==null?void 0:s.id)===e&&(this.currentSession=null))}},M={async fetchConversations(e){var s;if(!(this.viewedSessionId&&this.viewedSessionId!==e)){this.error=null;try{const t=await c.getConversations(e);if(this.viewedSessionId&&this.viewedSessionId!==e)return;this.conversations=t;const i=this.conversations.find(n=>n.isActive);this.activeConversationId=(i==null?void 0:i.id)||((s=this.conversations[0])==null?void 0:s.id)||null}catch(t){this.viewedSessionId&&this.viewedSessionId===e&&(this.error=t.message,this.conversations=[],this.activeConversationId=null)}}},async createConversation(e,s=null){this.error=null;try{const t=await c.createConversation(e,s);return this.conversations.push(t),this.conversations=this.conversations.map(i=>({...i,isActive:i.id===t.id})),this.activeConversationId=t.id,this.messages=[],this.runningUsage=null,t}catch(t){throw this.error=t.message,t}},async switchConversation(e,s){if(this.activeConversationId!==s){this.error=null;try{this.runningUsage=null,this.clearPartialThinking(),this.messages=[],this.workLogs={},await c.updateConversation(e,s,{isActive:!0}),this.conversations=this.conversations.map(t=>({...t,isActive:t.id===s})),this.activeConversationId=s,this.messages=await c.getConversationMessages(e,s),this.clearPartialThinking(e),await this.fetchWorkLogs(e)}catch(t){throw this.error=t.message,t}}},async renameConversation(e,s,t){this.error=null;try{const i=await c.updateConversation(e,s,{name:t}),n=this.conversations.findIndex(o=>o.id===s);return n!==-1&&(this.conversations[n]={...this.conversations[n],...i}),i}catch(i){throw this.error=i.message,i}},async deleteConversation(e,s){this.error=null;try{await c.deleteConversation(e,s),this.conversations=this.conversations.filter(t=>t.id!==s),this.activeConversationId===s&&this.conversations.length>0?(await this.fetchConversations(e),this.activeConversationId&&(this.messages=await c.getConversationMessages(e,this.activeConversationId))):this.activeConversationId===s&&(this.activeConversationId=null,this.messages=[])}catch(t){throw this.error=t.message,t}},async branchConversation(e,s,t={}){const{messageId:i=null,prompt:n=null}=t||{};this.error=null;try{const o=await c.branchConversation(e,s,{messageId:i,prompt:n});return this.conversations.some(r=>r.id===o.id)||this.conversations.push(o),this.conversations=this.conversations.map(r=>({...r,isActive:r.id===o.id})),this.activeConversationId=o.id,this.messages=[],this.workLogs={},this.clearPartialThinking(e),Promise.all([c.getConversationMessages(e,o.id).then(r=>{this.activeConversationId===o.id&&(this.messages=r)}).catch(r=>console.error("Failed to fetch messages for branch:",r)),this.fetchWorkLogs(e).catch(r=>console.error("Failed to fetch work logs for branch:",r))]),o}catch(o){throw this.error=o.message,o}},updateConversation(e){if(!(e!=null&&e.id)||this.currentSession&&e.sessionId&&e.sessionId!==this.currentSession.id)return;const s=this.conversations.findIndex(t=>t.id===e.id);s!==-1&&this.conversations.splice(s,1,{...this.conversations[s],...e}),e.isActive&&(this.conversations=this.conversations.map(t=>({...t,isActive:t.id===e.id})),this.activeConversationId=e.id)},addConversation(e){e!=null&&e.id&&(this.currentSession&&e.sessionId&&e.sessionId!==this.currentSession.id||(this.conversations.some(s=>s.id===e.id)||this.conversations.push(e),e.isActive&&(this.conversations=this.conversations.map(s=>({...s,isActive:s.id===e.id})),this.activeConversationId=e.id)))},removeConversation(e,s=null,t=null){var i;this.currentSession&&t&&t!==this.currentSession.id||(this.conversations=this.conversations.filter(n=>n.id!==e),s?(this.conversations.some(n=>n.id===s.id)||this.conversations.push(s),this.conversations=this.conversations.map(n=>({...n,isActive:n.id===s.id})),this.activeConversationId=s.id):this.activeConversationId===e&&(this.activeConversationId=((i=this.conversations[0])==null?void 0:i.id)||null))},clearConversations(){this.conversations=[],this.activeConversationId=null}},B={_isStaleSession(e){return this.viewedSessionId&&this.viewedSessionId!==e},_isActiveSession(e){return this.viewedSessionId&&this.viewedSessionId===e},async fetchMessages(e,s=!0,t=null){if(!this._isStaleSession(e)){s&&(this.loading=!0),this.error=null;try{const i=t||this.activeConversationId,n=i?await c.getConversationMessages(e,i):await c.getSessionMessages(e);if(this._isStaleSession(e))return;const o=new Set(n.map(a=>a.id)),r=this.messages.filter(a=>a.sessionId===e&&!o.has(a.id));r.length>0?this.messages=[...n,...r]:this.messages=n}catch(i){this._isActiveSession(e)&&(this.error=i.message)}finally{s&&(this.loading=!1)}}},addMessage(e){this.currentSession&&e.sessionId&&e.sessionId!==this.currentSession.id||this.messages.some(s=>s.id===e.id)||this.messages.push(e)},async fetchWorkLogs(e){if(!this._isStaleSession(e)){this.error=null;try{const s=await c.getSessionWorkLogs(e);if(this._isStaleSession(e))return;const t=new Set;for(const r of Object.keys(s))for(const a of s[r]||[])t.add(a.id);const n=(this.workLogs._unassociated||[]).filter(r=>!t.has(r.id)),o=s._unassociated||[];this.workLogs={...s,_unassociated:[...o,...n]}}catch(s){this._isActiveSession(e)&&(this.error=s.message)}}},addWorkLog(e){if(this.currentSession&&e.sessionId&&e.sessionId!==this.currentSession.id)return;const s=e.messageId||"_unassociated",t=this.workLogs[s]||[];t.some(i=>i.id===e.id)||(this.workLogs={...this.workLogs,[s]:[...t,e]})},setWorkLogs(e){this.workLogs=e},clearWorkLogs(){this.workLogs={},this.clearAllPartialThinking()},associateWorkLogs(e){const s=this.workLogs._unassociated||[];if(s.length>0){const t=this.workLogs[e]||[],i=new Set(t.map(o=>o.id)),n=s.filter(o=>!i.has(o.id));this.workLogs={...this.workLogs,[e]:[...t,...n],_unassociated:[]}}},setPartialThinking(e,s=null){var i;const t=s||((i=this.currentSession)==null?void 0:i.id);t&&(this.partialThinkingBySession={...this.partialThinkingBySession,[t]:e})},clearPartialThinking(e=null){var t;const s=e||((t=this.currentSession)==null?void 0:t.id);s&&(this.partialThinkingBySession={...this.partialThinkingBySession,[s]:null})},clearAllPartialThinking(){this.partialThinkingBySession={}},setPartialText(e){this._pendingPartialText=e,this._partialThrottleTimer||(this.partialText=e,this._partialThrottleTimer=setTimeout(()=>{this._pendingPartialText!==null&&this._pendingPartialText!==this.partialText&&(this.partialText=this._pendingPartialText),this._partialThrottleTimer=null,this._pendingPartialText=null},150))},clearPartialText(){this.partialText="",this._pendingPartialText=null,this._partialThrottleTimer&&(clearTimeout(this._partialThrottleTimer),this._partialThrottleTimer=null)},updateRunningUsage(e,s=null){this.runningUsage={...e,conversationId:s}},finalizeUsage(e,s=null){if(s){const t=this.conversations.findIndex(i=>i.id===s);t!==-1&&this.conversations.splice(t,1,{...this.conversations[t],inputTokens:e.inputTokens,outputTokens:e.outputTokens,cacheReadInputTokens:e.cacheReadInputTokens,cacheCreationInputTokens:e.cacheCreationInputTokens,webSearchRequests:e.webSearchRequests,contextWindow:e.contextWindow,model:e.model})}this.currentSession&&(this.currentSession={...this.currentSession,inputTokens:e.inputTokens,outputTokens:e.outputTokens,cacheReadInputTokens:e.cacheReadInputTokens,cacheCreationInputTokens:e.cacheCreationInputTokens,webSearchRequests:e.webSearchRequests,contextWindow:e.contextWindow}),this.runningUsage=null},updateConversationUsage(e,s){const t=this.conversations.findIndex(i=>i.id===e);t!==-1&&this.conversations.splice(t,1,{...this.conversations[t],inputTokens:s.inputTokens,outputTokens:s.outputTokens,cacheReadInputTokens:s.cacheReadInputTokens,cacheCreationInputTokens:s.cacheCreationInputTokens,webSearchRequests:s.webSearchRequests,contextWindow:s.contextWindow,model:s.model})},clearRunningUsage(){this.runningUsage=null,this.clearPartialThinking()}},E={isDraftSession:e=>s=>!s||s.status!=="waiting"?!1:s.hasResponses!==void 0?!s.hasResponses:!e.messages.some(t=>t.role==="assistant"),isScheduledDraft:e=>s=>!s||s.status!=="scheduled"?!1:s.hasResponses!==void 0?!s.hasResponses:!e.messages.some(t=>t.role==="assistant"),getWorkLogsForMessage:e=>s=>e.workLogs[s]||[],getUnassociatedWorkLogs:e=>e.workLogs._unassociated||[],partialThinking:e=>{var s;return(s=e.currentSession)!=null&&s.id&&e.partialThinkingBySession[e.currentSession.id]||null},activeConversation:e=>e.conversations.find(s=>s.id===e.activeConversationId)||null,getConversationById:e=>s=>e.conversations.find(t=>t.id===s),rootConversations:e=>e.conversations.filter(s=>!s.parentConversationId),conversationTree:e=>{const s=(t=null)=>e.conversations.filter(i=>i.parentConversationId===t).map(i=>({...i,children:s(i.id)}));return s(null)},getConversationChildren:e=>s=>e.conversations.filter(t=>t.parentConversationId===s),getConversationParent:e=>s=>{const t=e.conversations.find(i=>i.id===s);return t!=null&&t.parentConversationId?e.conversations.find(i=>i.id===t.parentConversationId):null}},G=y("sessions",{state:()=>({sessions:[],archivedSessions:[],activeSessions:[],scheduledSessions:[],currentSession:null,viewedSessionId:null,messages:[],conversations:[],activeConversationId:null,workLogs:{},partialThinkingBySession:{},partialText:"",_partialThrottleTimer:null,_pendingPartialText:null,statusFilter:null,starredFilter:null,scheduledFilter:null,runningUsage:null,loading:!1,loadingScheduled:!1,error:null,commandRunVersion:0,archivedPagination:{total:0,offset:0,hasMore:!1,loading:!1}}),getters:{_findSessionById:e=>s=>e.sessions.find(t=>t.id===s)||e.activeSessions.find(t=>t.id===s),_findChildren:e=>s=>{const t=e.sessions.filter(r=>r.parentSessionId===s),i=e.activeSessions.filter(r=>r.parentSessionId===s),n=new Set(t.map(r=>r.id)),o=[...t];for(const r of i)n.has(r.id)||(o.push(r),n.add(r.id));return o},getSessionById:e=>s=>e.sessions.find(t=>t.id===s),getChildSessions(){return e=>this._findChildren(e)},hasChildren(){return e=>this._findChildren(e).length>0},getChildCount(){return e=>this._findChildren(e).length},getAllDescendants(){return e=>{const s=[],t=[e],i=new Set;for(;t.length>0;){const n=t.pop();if(i.has(n))continue;i.add(n);const o=this._findChildren(n);for(const r of o)s.push(r),t.push(r.id)}return s}},getWorkflowEffectiveStatus(){return e=>{const s=this._findSessionById(e);if(!s)return"idle";const t=[s],i=[e],n=new Set;for(;i.length>0;){const r=i.pop();if(n.has(r))continue;n.add(r);const a=this._findChildren(r);for(const u of a)t.push(u),i.push(u.id)}const o=["running","starting"];return t.some(r=>o.includes(r.status))?"running":"idle"}},getSessionPath(){return e=>{const s=[],t=n=>{var o;return((o=this.currentSession)==null?void 0:o.id)===n?this.currentSession:this._findSessionById(n)};let i=t(e);for(;i&&(s.unshift(i),!!i.parentSessionId);)i=t(i.parentSessionId);return s}},getRootSession(){return e=>{let s=this._findSessionById(e);for(;s!=null&&s.parentSessionId;)s=this._findSessionById(s.parentSessionId);return s||null}},getWorkflowAggregatedStatus(){return e=>{const s=this._findSessionById(e);if(!s)return{effectiveStatus:"idle",runningCount:0,scheduledCount:0,waitingCount:0,completedCount:0,totalCount:0,hasScheduledDescendant:!1,rootIsScheduled:!1};const t=[s],i=[e],n=new Set;for(;i.length>0;){const h=i.pop();if(n.has(h))continue;n.add(h);const m=this._findChildren(h);for(const S of m)t.push(S),i.push(S.id)}const o=["running","starting"];let r=0,a=0,u=0,f=0;for(const h of t)o.includes(h.status)?r++:h.status==="scheduled"?a++:h.status==="waiting"?u++:(h.status==="completed"||h.status==="stopped")&&f++;return{effectiveStatus:r>0?"running":"idle",runningCount:r,scheduledCount:a,waitingCount:u,completedCount:f,totalCount:t.length-1,hasScheduledDescendant:t.slice(1).some(h=>h.status==="scheduled"),rootIsScheduled:s.status==="scheduled"}}},getWorkflowSessions(){return e=>{const s=this._findSessionById(e);if(!s)return[];const t=[s],i=[e],n=new Set;for(;i.length>0;){const o=i.pop();if(n.has(o))continue;n.add(o);const r=this._findChildren(o);for(const a of r)t.push(a),i.push(a.id)}return t}},groupedSessions:e=>{const s=[],t=new Set;return e.sessions.forEach(i=>{!i.parentSessionId&&!t.has(i.id)&&(s.push({parent:i,children:e.sessions.filter(n=>n.parentSessionId===i.id)}),t.add(i.id))}),e.sessions.forEach(i=>{!i.parentSessionId&&!s.find(n=>n.parent.id===i.id)&&s.push({parent:i,children:[]})}),s},...E,...U},actions:{_updateSessionInAllLists(e,s){var i;const t=(n,o)=>{const r=n;o!==-1&&(r[o]={...r[o],...s})};t(this.sessions,this.sessions.findIndex(n=>n.id===e)),t(this.archivedSessions,this.archivedSessions.findIndex(n=>n.id===e)),t(this.activeSessions,this.activeSessions.findIndex(n=>n.id===e)),((i=this.currentSession)==null?void 0:i.id)===e&&(this.currentSession={...this.currentSession,...s})},...W,setStatusFilter(e){this.statusFilter=e,this._syncFilterToSubStore("statusFilter"),this._filtersStore().saveStatusFilter()},saveStatusFilter(){this._syncFilterToSubStore("statusFilter"),this._filtersStore().saveStatusFilter()},restoreStatusFilter(){this._filtersStore().restoreStatusFilter(),this.statusFilter=this._filtersStore().statusFilter},setStarredFilter(e){this.starredFilter=e,this._syncFilterToSubStore("starredFilter"),this._filtersStore().saveStarredFilter()},saveStarredFilter(){this._syncFilterToSubStore("starredFilter"),this._filtersStore().saveStarredFilter()},restoreStarredFilter(){this._filtersStore().restoreStarredFilter(),this.starredFilter=this._filtersStore().starredFilter},setScheduledFilter(e){this.scheduledFilter=e,this._syncFilterToSubStore("scheduledFilter"),this._filtersStore().saveScheduledFilter()},saveScheduledFilter(){this._syncFilterToSubStore("scheduledFilter"),this._filtersStore().saveScheduledFilter()},restoreScheduledFilter(){this._filtersStore().restoreScheduledFilter(),this.scheduledFilter=this._filtersStore().scheduledFilter},_filtersStore(){return L()},_syncFilterToSubStore(e){this._filtersStore()[e]=this[e]},updateSessionCommandRun(e,s,t){const i=r=>{if(!r)return null;const a=[...r.latestCommandRuns||[]],u=a.findIndex(f=>f.buttonId===s);return u>=0?a[u]=t:a.push(t),a},n=this.sessions.find(r=>r.id===e)||this.archivedSessions.find(r=>r.id===e)||this.activeSessions.find(r=>r.id===e)||this.currentSession,o=i(n);o&&this._updateSessionInAllLists(e,{latestCommandRuns:o}),this.commandRunVersion++},removeSessionCommandRun(e,s){const t=this.sessions.find(n=>n.id===e)||this.archivedSessions.find(n=>n.id===e)||this.activeSessions.find(n=>n.id===e)||this.currentSession;if(!t)return;const i=(t.latestCommandRuns||[]).filter(n=>n.buttonId!==s);this._updateSessionInAllLists(e,{latestCommandRuns:i}),this.commandRunVersion++},updateSessionCommandRuns(e,s){this._updateSessionInAllLists(e,{latestCommandRuns:s}),this.commandRunVersion++},...B,...M}});export{E as a,M as c,w as f,O as g,B as p,U as t,G as u};