import"node:module";import{existsSync as e,mkdirSync as t,readFileSync as n,rmSync as r,unlinkSync as i,writeFileSync as a}from"node:fs";import{dirname as o,resolve as s}from"node:path";import{pathToFileURL as c}from"node:url";import{createHash as l}from"node:crypto";var u=Object.defineProperty,d=(e,t)=>{let n={};for(var r in e)u(n,r,{get:e[r],enumerable:!0});return t||u(n,Symbol.toStringTag,{value:`Module`}),n};const f=[`inputs`,`outputs`,`requires`,`relatedSkills`],p=[`category`,`domain`,`applicability`],m=new Set([`__proto__`,`constructor`,`prototype`]);function h(e,t){return e.metadata[t]??e.fields[t]}function g(e,t){let n=h(e,t);return n?v(n):[]}function _(e){let t=Object.create(null),n=Object.create(null),r=[],i=e,a=e.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);if(!a)return{fields:t,metadata:n,tools:r,body:i};let o=a[1];i=a[2];let s=!1;for(let e of o.split(/\r?\n/)){if(s){if(e.trim()===``)continue;let t=e.match(/^\s{2,}(\S+?):\s*(.*)$/);if(t){let[,e,r]=t;if(m.has(e))continue;n[e]=r;continue}s=!1}let i=e.match(/^(\S+?):\s*(.*)$/);if(!i)continue;let[,a,o]=i;if(a===`metadata`&&o.trim()===``){s=!0;continue}m.has(a)||(t[a]=o,a===`tools`&&(r=v(o)))}return{fields:t,metadata:n,tools:r,body:i}}function v(e){let t=e.trim();if(!t.startsWith(`[`)||!t.endsWith(`]`))return[];let n=t.slice(1,-1).trim();return n?n.split(`,`).map(e=>e.trim()).filter(Boolean):[]}function y(e){return`[${e.join(`, `)}]`}function b(e,t){let n=_(e),r=_(t),i={...n.fields},a={...n.metadata};for(let[e,t]of Object.entries(r.metadata))e in a||(a[e]=t);for(let e of p){let t=h(r,e);t&&(a[e]=t)}for(let e of[...p,...f])delete i[e];delete i.metadata;let o=n.tools,s=r.tools,c=new Set(o),l=s.filter(e=>!c.has(e)),u=[...o,...l];u.length>0&&(i.tools=y(u));for(let e of f){let t=g(n,e),i=g(r,e),o=new Set(t),s=i.filter(e=>!o.has(e)),c=[...t,...s];c.length>0?a[e]=y(c):delete a[e]}let d=[`---`],m=[`name`,`description`,`argument-hint`,`tools`,`model`],v=[`category`,`domain`,`applicability`,`inputs`,`outputs`,`requires`,`relatedSkills`],b=new Set;for(let e of m)e in i&&(d.push(`${e}: ${i[e]}`),b.add(e));for(let[e,t]of Object.entries(i))b.has(e)||d.push(`${e}: ${t}`);let x=[],S=new Set;for(let e of v){let t=a[e];t&&(x.push(`  ${e}: ${t}`),S.add(e))}for(let[e,t]of Object.entries(a))!S.has(e)&&t&&x.push(`  ${e}: ${t}`);return x.length>0&&(d.push(`metadata:`),d.push(...x)),d.push(`---`),`${d.join(`
`)}\n${n.body}`}function x(e){return l(`sha256`).update(e).digest(`hex`).slice(0,16)}function S(t){if(!e(t))return null;try{let e=n(t,`utf-8`),r=JSON.parse(e);return!r.version||!r.files?null:r}catch{return null}}function C(e,t){a(e,`${JSON.stringify(t,null,2)}\n`,`utf-8`)}function w(e){return e.endsWith(`.agent.md`)&&!e.startsWith(`_shared/`)&&!e.startsWith(`agents/_shared/`)&&!e.startsWith(`templates/`)||e.endsWith(`SKILL.md`)&&!e.startsWith(`_shared/`)&&!e.startsWith(`templates/`)?`merge-frontmatter`:e===`copilot-instructions.md`||e===`CLAUDE.md`?`regenerate`:`overwrite`}function T(e,t,n){if(!e)return`new`;let r=e.files[t];return r?r.sourceHash===x(n)?`current`:`outdated`:`new`}function E(e,t,n,r){e.files[t]={sourceHash:x(n),strategy:r??w(t),updatedAt:new Date().toISOString()}}function D(e){return{version:e,generatedAt:new Date().toISOString(),files:{}}}var O=d({guideFlows:()=>L,guideScaffold:()=>P,guideSkills:()=>F,loadAdapter:()=>k,smartCopyFlows:()=>I,smartCopyFromMemory:()=>A,smartCopyScaffold:()=>M,smartCopySkills:()=>N});async function k(e,t){let n=await import(c(s(e,`scaffold`,`dist`,`adapters`,`${t}.mjs`)).href),r=n[`generate${t.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}`]??n.generate??n.default;if(typeof r!=`function`)throw Error(`Adapter ${t} does not export a generate function`);return r()}function A(r,i,c,l,u=!1){t(i,{recursive:!0});for(let d of r){let r=l?`${l}/${d.path}`:d.path,f=s(i,d.path);if(u){t(o(f),{recursive:!0}),a(f,d.content,`utf-8`),E(c,r,d.content);continue}let p=T(c,r,d.content);if(p===`current`)continue;let m=w(r);if(m===`regenerate`){t(o(f),{recursive:!0}),a(f,d.content,`utf-8`),E(c,r,d.content);continue}if(p===`new`&&!e(f)){t(o(f),{recursive:!0}),a(f,d.content,`utf-8`),E(c,r,d.content);continue}if(p===`new`&&e(f)){E(c,r,d.content);continue}if(m===`merge-frontmatter`&&e(f)){let e=n(f,`utf-8`);a(f,b(d.content,e),`utf-8`)}else t(o(f),{recursive:!0}),a(f,d.content,`utf-8`);E(c,r,d.content)}u&&j(i,c,l,new Set(r.map(e=>l?`${l}/${e.path}`:e.path)))}function j(t,n,r,a){let o=[];for(let c of Object.keys(n.files)){if(!c.startsWith(`${r}/`)||a.has(c))continue;let l=s(t,c.slice(r.length+1));e(l)&&(i(l),o.push(c)),delete n.files[c]}return o}async function M(e,t,n=`copilot`,r,i=!1){let a=await k(t,n),o=a.some(e=>e.path.startsWith(`.`)),c=o?e:s(e,`.github`),l=s(c,`.aikit-scaffold.json`),u=S(l)??D(r);u.version=r;let d=new Map;for(let e of a){let t=e.path.indexOf(`/`);if(t===-1){let t=d.get(``)??[];t.push({path:e.path,content:e.content}),d.set(``,t);continue}let n=e.path.substring(0,t),r=e.path.substring(t+1),i=d.get(n)??[];i.push({path:r,content:e.content}),d.set(n,i)}for(let[e,t]of d)A(t,e?s(c,e):c,u,o?`${n}/${e||`.`}`:e||`.`,i);C(l,u)}async function N(e,t,n,r,i=!1){let a=s(e,`.github`),o=s(a,`.aikit-scaffold.json`),c=S(o)??D(r);c.version=r;let l=await k(t,`skills`),u=new Map;for(let e of l){let t=e.path.indexOf(`/`);if(t===-1)continue;let n=e.path.substring(0,t),r=e.path.substring(t+1),i=u.get(n)??[];i.push({path:r,content:e.content}),u.set(n,i)}for(let[e,t]of u)A(t,s(a,`skills`,e),c,`skills/${e}`,i);C(o,c)}async function P(t,r,i=`copilot`){let a=[],o=await k(r,i),c=o.some(e=>e.path.startsWith(`.`))?t:s(t,`.github`);for(let t of o){let r=s(c,t.path);if(!e(r))a.push({status:`new`,relativePath:t.path,sourcePath:``,content:t.content});else{let e=n(r,`utf-8`);t.content===e?a.push({status:`current`,relativePath:t.path,sourcePath:``}):a.push({status:`outdated`,relativePath:t.path,sourcePath:``,content:t.content})}}return a}async function F(t,r,i){let a=[],o=await k(r,`skills`),c=s(t,`.github`);for(let t of o){let r=`skills/${t.path}`,i=s(c,`skills`,t.path);if(!e(i))a.push({status:`new`,relativePath:r,sourcePath:``,content:t.content});else{let e=n(i,`utf-8`);t.content===e?a.push({status:`current`,relativePath:r,sourcePath:``}):a.push({status:`outdated`,relativePath:r,sourcePath:``,content:t.content})}}return a}async function I(t,n,i,a,o=!1){let c=s(t,`.github`),l=s(c,`.aikit-scaffold.json`),u=S(l)??D(a);u.version=a;let d=await k(n,`flows`),f=new Set,p=new Map;for(let e of d){let t=e.path.indexOf(`/`);if(t===-1)continue;let n=e.path.substring(0,t);f.add(n);let r=e.path.substring(t+1),i=p.get(n)??[];i.push({path:r,content:e.content}),p.set(n,i)}for(let t of f){let n=s(c,`flows`,t,`skills`);e(n)&&r(n,{recursive:!0,force:!0})}for(let[e,t]of p)A(t,s(c,`flows`,e),u,`flows/${e}`,o);C(l,u)}async function L(t,r,i){let a=[],o=await k(r,`flows`),c=s(t,`.github`);for(let t of o){let r=`flows/${t.path}`,i=s(c,`flows`,t.path);if(!e(i))a.push({status:`new`,relativePath:r,sourcePath:``,content:t.content});else{let e=n(i,`utf-8`);t.content===e?a.push({status:`current`,relativePath:r,sourcePath:``}):a.push({status:`outdated`,relativePath:r,sourcePath:``,content:t.content})}}return a}export{O as a,M as c,S as d,C as f,k as i,N as l,P as n,I as o,_ as p,F as r,A as s,L as t,D as u};