import{s as e}from"./DrKWWDpL.js";import{t}from"./DYP3EJMY.js";import{_ as n,w as r}from"./DsanjglN.js";import{j as i}from"./CvaH7m0y.js";import"./DOPTvi6T.js";import{n as a}from"./CGVD7EpU.js";import{i as o}from"./CG84LPq-.js";var s=e(t(),1);function c(e){if(e)try{return JSON.parse(e)}catch{return}}function l(e){if(e)try{let t=JSON.parse(e);return Array.isArray(t)&&t.length===0?void 0:t}catch{return}}function u(e){return{id:e.id,title:e.title,status:e.status,priority:e.priority,type:e.type??void 0,parentId:e.parentId??void 0,phase:e.phase??void 0,size:e.size??void 0,position:e.position??void 0,positionVersion:e.positionVersion??void 0,description:e.description??``,labels:l(e.labelsJson),notes:l(e.notesJson),acceptance:l(e.acceptanceJson),files:l(e.filesJson),depends:void 0,origin:e.origin??void 0,blockedBy:e.blockedBy??void 0,epicLifecycle:e.epicLifecycle??void 0,noAutoComplete:e.noAutoComplete??void 0,createdAt:e.createdAt,updatedAt:e.updatedAt??void 0,completedAt:e.completedAt??void 0,cancelledAt:e.cancelledAt??void 0,cancellationReason:e.cancellationReason??void 0,verification:e.verificationJson?c(e.verificationJson):void 0,provenance:e.createdBy||e.modifiedBy||e.sessionId?{createdBy:e.createdBy??null,modifiedBy:e.modifiedBy??null,sessionId:e.sessionId??null}:void 0,pipelineStage:e.pipelineStage??void 0,assignee:e.assignee??void 0,kind:e.kind??void 0,scope:e.scope??void 0,severity:e.severity??void 0}}function d(e){let t=e.status??`pending`,n=e.pipelineStage??null;return n??(t===`done`?n=`contribution`:t===`cancelled`&&(n=`cancelled`)),{id:e.id,title:e.title??``,description:e.description??null,status:t,priority:e.priority??`medium`,type:e.type??null,parentId:e.parentId??null,phase:e.phase??null,size:e.size??null,position:e.position??null,positionVersion:e.positionVersion??0,labelsJson:e.labels?JSON.stringify(e.labels):`[]`,notesJson:e.notes?JSON.stringify(e.notes):`[]`,acceptanceJson:e.acceptance?JSON.stringify(e.acceptance):`[]`,filesJson:e.files?JSON.stringify(e.files):`[]`,origin:e.origin??null,blockedBy:e.blockedBy??null,epicLifecycle:e.epicLifecycle??null,noAutoComplete:e.noAutoComplete??null,createdAt:e.createdAt??new Date().toISOString(),updatedAt:e.updatedAt??null,completedAt:e.completedAt??null,cancelledAt:e.cancelledAt??null,cancellationReason:e.cancellationReason??null,verificationJson:e.verification?JSON.stringify(e.verification):null,createdBy:e.provenance?.createdBy??null,modifiedBy:e.provenance?.modifiedBy??null,sessionId:e.provenance?.sessionId??null,pipelineStage:n,assignee:e.assignee??null,kind:e.kind??void 0,scope:e.scope??void 0,severity:e.severity??void 0}}function f(e){let t=d(e);return t.status=`archived`,t.archivedAt||=e.completedAt??new Date().toISOString(),t}function p(e){let t={taskId:e.currentTask??null,setAt:e.taskStartedAt??null};return{id:e.id,name:e.name,status:e.status,scope:c(e.scopeJson)??{type:`global`},taskWork:t,startedAt:e.startedAt,endedAt:e.endedAt??void 0,agent:e.agent??void 0,notes:l(e.notesJson),tasksCompleted:l(e.tasksCompletedJson),tasksCreated:l(e.tasksCreatedJson),handoffJson:e.handoffJson??null,previousSessionId:e.previousSessionId??null,nextSessionId:e.nextSessionId??null,parentSessionId:e.parentSessionId??null,agentIdentifier:e.agentIdentifier??null,handoffConsumedAt:e.handoffConsumedAt??null,handoffConsumedBy:e.handoffConsumedBy??null,debriefJson:e.debriefJson??null,stats:e.statsJson?c(e.statsJson):void 0,resumeCount:e.resumeCount??void 0,gradeMode:e.gradeMode?!!e.gradeMode:void 0,agentHandle:e.agentHandle??null,scopeKind:e.scopeKind??null,scopeId:e.scopeId??null,lastActivity:e.lastActivity??null}}var m=new Map,h=new s.AsyncLocalStorage;function g(e){return m.get(e)??null}function _(){let e=h.getStore();return e?e.sessionId?e.sessionId:g(e.connId):null}async function v(e,t){let n=await(await o(t)).select().from(i).where(r(i.id,e)).all();return n.length===0?null:p(n[0])}async function y(e){let t=await(await o(e)).select().from(i).where(r(i.status,`active`)).orderBy(n(i.startedAt)).limit(1).all();return t.length===0?null:p(t[0])}async function b(e){let t=_();if(t){let n=await v(t,e);if(n)return n}let n=a();if(n){let t=await v(n,e);if(t)return t}return y(e)}async function x(e){let t=_();if(t){let n=await v(t,e);if(n)return n.id}let n=a();if(n){let t=await v(n,e);if(t)return t.id}return(await y(e))?.id??null}export{g as a,p as c,x as i,u as l,v as n,_ as o,b as r,f as s,y as t,d as u};