#!/usr/bin/env node
import*as r from'fs';import*as i from'path';import*as m from'os';import*as c from'js-yaml';var f=".paradigm/agent-state",S=i.join(m.homedir(),".paradigm","agents");function g(s,n){let e=i.join(n,f,`${s}.yaml`);if(!r.existsSync(e))return null;try{let t=r.readFileSync(e,"utf8");return c.load(t)}catch{return null}}function u(s,n,e){let t=i.join(n,f);r.existsSync(t)||r.mkdirSync(t,{recursive:true});let a=i.join(t,`${s}.yaml`);r.writeFileSync(a,c.dump(e,{lineWidth:120,noRefs:true,sortKeys:false}),"utf8");}function j(s,n,e){let t=g(s,n),a=i.basename(n),o={id:s,project:a,lastSession:{date:new Date().toISOString(),sessionId:e.sessionId,summary:e.summary,filesReviewed:e.filesReviewed,symbolsTouched:e.symbolsTouched,decisions:e.decisions},pendingWork:e.pendingWork||t?.pendingWork||[],recentPatterns:e.patterns||t?.recentPatterns||[],sessionsOnProject:(t?.sessionsOnProject||0)+1,lastPurposeUpdate:t?.lastPurposeUpdate};return u(s,n,o),h(s,a),o}function v(s,n,e){let t=g(s,n);t&&(t.pendingWork=[...new Set([...t.pendingWork,...e])],u(s,n,t));}function x(s,n,e){let t=g(s,n);if(!t)return;let a=new Set(e.map(o=>o.toLowerCase()));t.pendingWork=t.pendingWork.filter(o=>!a.has(o.toLowerCase())),u(s,n,t);}function b(s,n,e){let t=g(s,n);t&&(t.recentPatterns.includes(e)||(t.recentPatterns.push(e),t.recentPatterns.length>10&&(t.recentPatterns=t.recentPatterns.slice(-10)),u(s,n,t)));}function P(s){let n=i.join(S,s,"state.yaml");if(!r.existsSync(n))return null;try{let e=r.readFileSync(n,"utf8");return c.load(e)}catch{return null}}function h(s,n){let e=i.join(S,s);r.existsSync(e)||r.mkdirSync(e,{recursive:true});let t=i.join(e,"state.yaml"),a=P(s),o=new Date().toISOString(),l=a?.projectHistory||[],d=l.find(p=>p.project===n);d?(d.sessions+=1,d.lastActive=o):l.push({project:n,sessions:1,lastActive:o}),l.sort((p,A)=>A.lastActive.localeCompare(p.lastActive));let y={id:s,totalSessions:(a?.totalSessions||0)+1,lastActiveProject:n,lastActiveDate:o,projectHistory:l};r.writeFileSync(t,c.dump(y,{lineWidth:120,noRefs:true,sortKeys:false}),"utf8");}function W(s){let n=i.join(s,f);if(!r.existsSync(n))return [];try{return r.readdirSync(n).filter(e=>e.endsWith(".yaml")).map(e=>{try{let t=r.readFileSync(i.join(n,e),"utf8");return c.load(t)}catch{return null}}).filter(Boolean)}catch{return []}}export{g as a,u as b,j as c,v as d,x as e,b as f,P as g,h,W as i};