@vpxa/aikit 0.1.274 → 0.1.275

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 (31) hide show
  1. package/package.json +7 -1
  2. package/packages/cli/dist/index.js +14 -14
  3. package/packages/cli/dist/{init-BgelSos0.js → init-B3a2fygD.js} +1 -1
  4. package/packages/cli/dist/{scaffold-Cxjwg531.js → scaffold-BNPHP-QC.js} +1 -1
  5. package/packages/cli/dist/{templates-C92mODRl.js → templates-CDa0UuoE.js} +19 -19
  6. package/packages/core/dist/index.d.ts +45 -17
  7. package/packages/core/dist/index.js +1 -1
  8. package/packages/flows/dist/index.d.ts +23 -2
  9. package/packages/flows/dist/index.js +1 -1
  10. package/packages/server/dist/bin.js +6 -6
  11. package/packages/server/dist/config-DZ-6Zy94.js +2 -0
  12. package/packages/server/dist/config-DxWyWSb9.js +1 -0
  13. package/packages/server/dist/curated-manager-C5uOPept.js +7 -0
  14. package/packages/server/dist/index.js +1 -1
  15. package/packages/server/dist/{promotion-BNEScZVD.js → promotion-D9anNXv8.js} +1 -1
  16. package/packages/server/dist/{routes-CR3fI-HJ.js → routes-1wkXLxXe.js} +1 -1
  17. package/packages/server/dist/{routes-Afg7J7xK.js → routes-KC-D2U8n.js} +1 -1
  18. package/packages/server/dist/{server-4h0Cclv3.js → server-CkCRBlz4.js} +93 -93
  19. package/packages/server/dist/{server-DIz2FGOX.js → server-DlE6A6sd.js} +93 -93
  20. package/packages/server/dist/{version-check-gazMo-D4.js → version-check-CgfflkJX.js} +1 -1
  21. package/packages/server/dist/{version-check-BgHzxxCW.js → version-check-ruLtfyDd.js} +1 -1
  22. package/packages/server/viewers/canvas.html +2 -1
  23. package/packages/server/viewers/task-plan-static.html +2 -1
  24. package/packages/tools/dist/index.d.ts +5 -5
  25. package/packages/tools/dist/index.js +72 -72
  26. package/scaffold/dist/definitions/hooks.mjs +1 -1
  27. package/scaffold/dist/definitions/skills/c4-architecture.mjs +1 -1
  28. package/scaffold/dist/definitions/skills/session-handoff.mjs +2 -732
  29. package/packages/server/dist/config-CZuVxRpX.js +0 -1
  30. package/packages/server/dist/config-WpN5CWM7.js +0 -2
  31. package/packages/server/dist/curated-manager-CfwN96rp.js +0 -7
@@ -1 +0,0 @@
1
- import{a as e,t}from"./retention-B4ITAs7F.js";import{o as n,t as r}from"./supersession-CWEne3av.js";import{existsSync as i,mkdirSync as a,readFileSync as o,writeFileSync as s}from"node:fs";import{dirname as c,isAbsolute as l,relative as u,resolve as d}from"node:path";import{fileURLToPath as f}from"node:url";import{AIKIT_PATHS as p,EMBEDDING_DEFAULTS as m,createLogger as h,getPartitionDir as g,isUserInstalled as _,registerWorkspace as v,serializeError as y}from"../../core/dist/index.js";const b=c(f(import.meta.url)),x=h(`server`),S=[`auto`,`manual`,`smart`],C={model:m.model,nativeDim:m.nativeDim,dimensions:m.dimensions,queryPrefix:m.queryPrefix,childProcess:!0,idleTimeoutMs:6e4};function w(e){return typeof e==`string`&&S.includes(e)}function T(e,t,n){let r=d(e),i=u(d(t),r);if(i.startsWith(`..`)||l(i))throw Error(`Config ${n} path escapes workspace root: ${e} is not under ${t}`);return r}function E(e){let t=[e.store?.path,e.curated?.path,e.onboardDir,e.stateDir];for(let e of t)e&&a(e,{recursive:!0})}function D(e){let t=process.env.AIKIT_INDEX_MODE;if(w(t))return t;if(e.indexMode)return e.indexMode;let n=process.env.AIKIT_AUTO_INDEX;return n===void 0?e.autoIndex===void 0?`smart`:e.autoIndex?`auto`:`manual`:n===`true`?`auto`:`manual`}function O(){let a=process.env.AIKIT_CONFIG_PATH??(i(d(process.cwd(),`aikit.config.json`))?d(process.cwd(),`aikit.config.json`):d(b,`..`,`..`,`..`,`aikit.config.json`));try{if(!i(a))return x.info(`No config file found, using defaults`,{configPath:a}),A();let s=o(a,`utf-8`),l=JSON.parse(s),u={...C,...l.embedding};if(l.embedding=u,l.memory={retention:{...t,...l.memory?.retention},lessons:{...n,...l.memory?.lessons},consolidation:{...e,...l.memory?.consolidation},supersession:{...r,...l.memory?.supersession}},l.logging?.errorDetails!==void 0&&typeof l.logging.errorDetails!=`boolean`)throw Error(`Config logging.errorDetails must be a boolean`);if(l.logging={errorDetails:l.logging?.errorDetails===!0},!l.sources||!Array.isArray(l.sources)||l.sources.length===0)throw Error(`Config must have at least one source`);if(!l.store?.path)throw Error(`Config must specify store.path`);if(l.autoIndex!==void 0&&typeof l.autoIndex!=`boolean`)throw Error(`Config autoIndex must be a boolean`);if(l.indexMode!==void 0&&!w(l.indexMode))throw Error(`Config indexMode must be one of: ${S.join(`, `)}`);if(typeof u.model!=`string`||u.model.trim()===``)throw Error(`Config embedding.model must be a non-empty string`);if(!Number.isInteger(u.nativeDim)||u.nativeDim<=0)throw Error(`Config embedding.nativeDim must be a positive integer`);if(!Number.isInteger(u.dimensions)||u.dimensions<=0)throw Error(`Config embedding.dimensions must be a positive integer`);if(u.dimensions>u.nativeDim)throw Error(`Config embedding.dimensions must not exceed embedding.nativeDim`);if(typeof u.queryPrefix!=`string`)throw Error(`Config embedding.queryPrefix must be a string`);let f=c(a);return l.sources=l.sources.map(e=>({...e,path:T(d(f,e.path),f,`source`)})),l.store.path=T(d(f,l.store.path),f,`store`),l.curated=l.curated??{path:p.aiCurated},l.curated.path=T(d(f,l.curated.path),f,`curated`),M(l,f),j(l,a),l.indexMode=D(l),l}catch(e){return x.error(`Failed to load config`,{configPath:a,...y(e)}),x.warn(`Falling back to default configuration`,{configPath:a}),A()}}const k=[`.git/**`,`**/node_modules/**`,`*.lock`,`pnpm-lock.yaml`,`package-lock.json`,`**/dist/**`,`**/build/**`,`**/out/**`,`**/.output/**`,`**/cdk.out/**`,`**/.next/**`,`**/.nuxt/**`,`**/.vercel/**`,`**/.serverless/**`,`**/.turbo/**`,`**/.cache/**`,`**/.parcel-cache/**`,`**/coverage/**`,`**/.terraform/**`,`**/__pycache__/**`,`**/.venv/**`,`**/.docusaurus/**`,`**/.temp/**`,`**/tmp/**`];function A(){let i=process.env.AIKIT_WORKSPACE_ROOT??process.cwd(),a={sources:[{path:i,excludePatterns:[...k]}],serverName:`aikit`,indexing:{chunkSize:1500,chunkOverlap:200,minChunkSize:100},embedding:{...C},store:{backend:`sqlite-vec`,path:d(i,p.data)},curated:{path:d(i,p.aiCurated)},memory:{retention:{...t},lessons:{...n},consolidation:{...e},supersession:{...r}},logging:{errorDetails:!1},onboardDir:d(i,p.aiContext),stateDir:d(i,p.state)};return M(a,i),a.indexMode=D(a),a}function j(e,t){let n=e.configVersion??0;if(n>=1)return e;if(n<1)for(let t of e.sources){t.excludePatterns=t.excludePatterns??[];let e=new Set(t.excludePatterns);for(let n of k)e.has(n)||t.excludePatterns.push(n)}e.configVersion=1;try{s(t,`${JSON.stringify(e,null,2)}\n`,`utf-8`),x.info(`Config auto-upgraded`,{from:n,to:1,configPath:t})}catch(e){x.warn(`Failed to write upgraded config`,{configPath:t,...y(e)})}return e}function M(e,t){if(!_())return;let n=v(t);e.store.path=d(g(n.partition)),e.onboardDir=d(g(n.partition),`onboard`),e.stateDir=d(g(n.partition),`state`),e.curated={path:d(g(n.partition),`curated`)}}function N(e,t){if(!i(t))throw Error(`Workspace root does not exist: ${t}`);x.debug(`Reconfiguring for workspace root`,{workspaceRoot:t});try{process.chdir(t),x.debug(`Changed process cwd to workspace root`,{cwd:process.cwd()})}catch(e){x.warn(`Failed to chdir to workspace root`,{workspaceRoot:t,...y(e)})}e.sources=[{path:t,excludePatterns:e.sources[0]?.excludePatterns??[`node_modules/**`,`dist/**`,`.git/**`,`coverage/**`,`*.lock`,`pnpm-lock.yaml`]}],e.store.path=d(t,p.data),e.curated={path:d(t,p.aiCurated)},e.onboardDir=d(t,p.aiContext),e.stateDir=d(t,p.state),M(e,t),E(e)}export{k as DEFAULT_EXCLUDE_PATTERNS,O as loadConfig,N as reconfigureForWorkspace,D as resolveIndexMode};
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- import{a as e,t}from"./retention-C3tsarCT.js";import{o as n,t as r}from"./supersession-DO_ZROFl.js";import{existsSync as i,mkdirSync as a,readFileSync as o,writeFileSync as s}from"node:fs";import{dirname as c,isAbsolute as l,relative as u,resolve as d}from"node:path";import{fileURLToPath as f}from"node:url";import{AIKIT_PATHS as p,EMBEDDING_DEFAULTS as m,createLogger as h,getPartitionDir as g,isUserInstalled as _,registerWorkspace as v,serializeError as y}from"../../core/dist/index.js";const b=c(f(import.meta.url)),x=h(`server`),S=[`auto`,`manual`,`smart`],C={model:m.model,nativeDim:m.nativeDim,dimensions:m.dimensions,queryPrefix:m.queryPrefix,childProcess:!0,idleTimeoutMs:6e4};function w(e){return typeof e==`string`&&S.includes(e)}function T(e,t,n){let r=d(e),i=u(d(t),r);if(i.startsWith(`..`)||l(i))throw Error(`Config ${n} path escapes workspace root: ${e} is not under ${t}`);return r}function E(e){let t=[e.store?.path,e.curated?.path,e.onboardDir,e.stateDir];for(let e of t)e&&a(e,{recursive:!0})}function D(e){let t=process.env.AIKIT_INDEX_MODE;if(w(t))return t;if(e.indexMode)return e.indexMode;let n=process.env.AIKIT_AUTO_INDEX;return n===void 0?e.autoIndex===void 0?`smart`:e.autoIndex?`auto`:`manual`:n===`true`?`auto`:`manual`}function O(){let a=process.env.AIKIT_CONFIG_PATH??(i(d(process.cwd(),`aikit.config.json`))?d(process.cwd(),`aikit.config.json`):d(b,`..`,`..`,`..`,`aikit.config.json`));try{if(!i(a))return x.info(`No config file found, using defaults`,{configPath:a}),A();let s=o(a,`utf-8`),l=JSON.parse(s),u={...C,...l.embedding};if(l.embedding=u,l.memory={retention:{...t,...l.memory?.retention},lessons:{...n,...l.memory?.lessons},consolidation:{...e,...l.memory?.consolidation},supersession:{...r,...l.memory?.supersession}},l.logging?.errorDetails!==void 0&&typeof l.logging.errorDetails!=`boolean`)throw Error(`Config logging.errorDetails must be a boolean`);if(l.logging={errorDetails:l.logging?.errorDetails===!0},!l.sources||!Array.isArray(l.sources)||l.sources.length===0)throw Error(`Config must have at least one source`);if(!l.store?.path)throw Error(`Config must specify store.path`);if(l.autoIndex!==void 0&&typeof l.autoIndex!=`boolean`)throw Error(`Config autoIndex must be a boolean`);if(l.indexMode!==void 0&&!w(l.indexMode))throw Error(`Config indexMode must be one of: ${S.join(`, `)}`);if(typeof u.model!=`string`||u.model.trim()===``)throw Error(`Config embedding.model must be a non-empty string`);if(!Number.isInteger(u.nativeDim)||u.nativeDim<=0)throw Error(`Config embedding.nativeDim must be a positive integer`);if(!Number.isInteger(u.dimensions)||u.dimensions<=0)throw Error(`Config embedding.dimensions must be a positive integer`);if(u.dimensions>u.nativeDim)throw Error(`Config embedding.dimensions must not exceed embedding.nativeDim`);if(typeof u.queryPrefix!=`string`)throw Error(`Config embedding.queryPrefix must be a string`);let f=c(a);return l.sources=l.sources.map(e=>({...e,path:T(d(f,e.path),f,`source`)})),l.store.path=T(d(f,l.store.path),f,`store`),l.curated=l.curated??{path:p.aiCurated},l.curated.path=T(d(f,l.curated.path),f,`curated`),M(l,f),j(l,a),l.indexMode=D(l),l}catch(e){return x.error(`Failed to load config`,{configPath:a,...y(e)}),x.warn(`Falling back to default configuration`,{configPath:a}),A()}}const k=[`.git/**`,`**/node_modules/**`,`*.lock`,`pnpm-lock.yaml`,`package-lock.json`,`**/dist/**`,`**/build/**`,`**/out/**`,`**/.output/**`,`**/cdk.out/**`,`**/.next/**`,`**/.nuxt/**`,`**/.vercel/**`,`**/.serverless/**`,`**/.turbo/**`,`**/.cache/**`,`**/.parcel-cache/**`,`**/coverage/**`,`**/.terraform/**`,`**/__pycache__/**`,`**/.venv/**`,`**/.docusaurus/**`,`**/.temp/**`,`**/tmp/**`];function A(){let i=process.env.AIKIT_WORKSPACE_ROOT??process.cwd(),a={sources:[{path:i,excludePatterns:[...k]}],serverName:`aikit`,indexing:{chunkSize:1500,chunkOverlap:200,minChunkSize:100},embedding:{...C},store:{backend:`sqlite-vec`,path:d(i,p.data)},curated:{path:d(i,p.aiCurated)},memory:{retention:{...t},lessons:{...n},consolidation:{...e},supersession:{...r}},logging:{errorDetails:!1},onboardDir:d(i,p.aiContext),stateDir:d(i,p.state)};return M(a,i),a.indexMode=D(a),a}function j(e,t){let n=e.configVersion??0;if(n>=1)return e;if(n<1)for(let t of e.sources){t.excludePatterns=t.excludePatterns??[];let e=new Set(t.excludePatterns);for(let n of k)e.has(n)||t.excludePatterns.push(n)}e.configVersion=1;try{s(t,`${JSON.stringify(e,null,2)}\n`,`utf-8`),x.info(`Config auto-upgraded`,{from:n,to:1,configPath:t})}catch(e){x.warn(`Failed to write upgraded config`,{configPath:t,...y(e)})}return e}function M(e,t){if(!_())return;let n=v(t);e.store.path=d(g(n.partition)),e.onboardDir=d(g(n.partition),`onboard`),e.stateDir=d(g(n.partition),`state`),e.curated={path:d(g(n.partition),`curated`)}}function N(e,t){if(!i(t))throw Error(`Workspace root does not exist: ${t}`);x.debug(`Reconfiguring for workspace root`,{workspaceRoot:t});try{process.chdir(t),x.debug(`Changed process cwd to workspace root`,{cwd:process.cwd()})}catch(e){x.warn(`Failed to chdir to workspace root`,{workspaceRoot:t,...y(e)})}e.sources=[{path:t,excludePatterns:e.sources[0]?.excludePatterns??[`node_modules/**`,`dist/**`,`.git/**`,`coverage/**`,`*.lock`,`pnpm-lock.yaml`]}],e.store.path=d(t,p.data),e.curated={path:d(t,p.aiCurated)},e.onboardDir=d(t,p.aiContext),e.stateDir=d(t,p.state),M(e,t),E(e)}export{k as DEFAULT_EXCLUDE_PATTERNS,O as loadConfig,N as reconfigureForWorkspace,D as resolveIndexMode};
@@ -1,7 +0,0 @@
1
- import{t as e}from"./rolldown-runtime-DT7IzrpZ.js";import{createHash as t}from"node:crypto";import{dirname as n,isAbsolute as r,join as i}from"node:path";import{HealthBus as a,createLogger as o,serializeError as s}from"../../core/dist/index.js";import{GIT_REF_SLUG_PATTERN as c,gitAvailable as l,gitCommitToRef as u,gitExec as d,slugForRef as f}from"../../tools/dist/index.js";import{mkdir as p,readFile as m,readdir as h,stat as g,unlink as _,writeFile as v}from"node:fs/promises";var y=class{baseDir;constructor(e){this.baseDir=e}async initialize(){await p(this.baseDir,{recursive:!0})}async read(e){let t=i(this.baseDir,e);try{return await m(t,`utf-8`)}catch(e){if(e.code===`ENOENT`)return null;throw e}}async write(e,t,r){let a=i(this.baseDir,e);await p(n(a),{recursive:!0}),await v(a,t,{encoding:`utf-8`,flag:r?.exclusive?`wx`:`w`})}async delete(e){let t=i(this.baseDir,e);try{return await _(t),!0}catch(e){if(e.code===`ENOENT`)return!1;throw e}}async list(e){let t=[],n=e?[e]:await this.listDirectories();for(let e of n){let n=i(this.baseDir,e),r;try{r=await h(n)}catch{continue}for(let n of r){if(!n.endsWith(`.md`))continue;let r=`${e}/${n}`,a=i(this.baseDir,r);try{let e=await g(a);t.push({path:r,size:e.size})}catch{}}}return t}async exists(e){let t=i(this.baseDir,e);try{return await g(t),!0}catch{return!1}}async listDirectories(){try{return(await h(this.baseDir,{withFileTypes:!0})).filter(e=>e.isDirectory()&&/^[a-z][a-z0-9-]*$/.test(e.name)).map(e=>e.name)}catch{return[]}}async close(){}},b=e({CuratedKnowledgeManager:()=>w});const x=50*1024,S=`refs/aikit/knowledge`,C=o(`server`);var w=class{curatedDir;store;embedder;adapter;constructor(e,t,n,r=new y(e)){this.curatedDir=e,this.store=t,this.embedder=n,this.adapter=r}async remember(e,t,n,r=[]){this.validateCategoryName(n),this.validateContentSize(t);let i=this.normalizeTags(r),a=this.slugify(e),o=await this.uniqueRelativePath(n,a),s=new Date().toISOString(),c={title:e,category:n,tags:i,created:s,updated:s,version:1,origin:`curated`,changelog:[{version:1,date:s,reason:`Initial creation`}]},l=this.serializeFile(t,c);try{await this.adapter.write(o,l,{exclusive:!0})}catch(e){throw e.code===`EEXIST`?Error(`Concurrent write collision for "${o}" — retry the operation`):e}return this.gitCommitKnowledge(o,l,`remember: ${e}\n\nCategory: ${n}\nTags: ${i.join(`, `)}`),await this.indexCuratedFileBestEffort(o,t,c,`remember`),{path:o}}async upsertAutoKnowledge(e,t,n,r,i=[],a=`Auto-knowledge refresh`){e=this.guardPath(e),this.validateCategoryName(r),this.validateContentSize(n),this.ensureCategoryPath(e,r);let o=this.normalizeTags(i),s=await this.adapter.read(e);if(s==null){let i=new Date().toISOString(),a={title:t,category:r,tags:o,created:i,updated:i,version:1,origin:`curated`,changelog:[{version:1,date:i,reason:`Initial creation`}]},s=this.serializeFile(n,a);try{await this.adapter.write(e,s,{exclusive:!0})}catch(t){throw t.code===`EEXIST`?Error(`Concurrent write collision for "${e}" — retry the operation`):t}return this.gitCommitKnowledge(e,s,`remember: ${t}\n\nCategory: ${r}\nTags: ${o.join(`, `)}`),await this.indexCuratedFileBestEffort(e,n,a,`remember`),{path:e,status:`created`,version:1}}let{frontmatter:c,content:l}=this.parseFile(s),u=this.normalizeTags(c.tags??[]),d=t.trim()||c.title;if(l===n&&c.category===r&&c.title===d&&this.sameTags(u,o))return{path:e,status:`unchanged`,version:c.version??1};let f=(c.version??1)+1,p=new Date().toISOString();c.title=d,c.category=r,c.tags=o,c.version=f,c.updated=p,c.changelog=[...c.changelog??[],{version:f,date:p,reason:a}];let m=this.serializeFile(n,c);return await this.adapter.write(e,m),this.gitCommitKnowledge(e,m,`update(v${f}): ${c.title}\n\nReason: ${a}\nVersion: ${f}`),await this.indexCuratedFileBestEffort(e,n,c,`update`),{path:e,status:`updated`,version:f}}async update(e,t,n){e=this.guardPath(e),this.validateContentSize(t);let r=await this.adapter.read(e);if(r==null)throw Error(`Curated entry not found: ${e}`);let{frontmatter:i}=this.parseFile(r),a=(i.version??1)+1,o=new Date().toISOString();i.version=a,i.updated=o,i.changelog=[...i.changelog??[],{version:a,date:o,reason:n}];let s=this.serializeFile(t,i);return await this.adapter.write(e,s),this.gitCommitKnowledge(e,s,`update(v${a}): ${i.title}\n\nReason: ${n}\nVersion: ${a}`),await this.indexCuratedFileBestEffort(e,t,i,`update`),{path:e,version:a}}async forget(e,t){if(e=this.guardPath(e),!await this.adapter.delete(e))throw Error(`Curated entry not found: ${e}`);let n=`.ai/curated/${e}`;return await this.store.deleteBySourcePath(n).catch(e=>{C.warn(`File deleted but vector cleanup failed`,{sourcePath:n,...s(e)})}),this.gitDeleteKnowledgeRef(e),{path:e}}async history(e,t=20){if(e=this.guardPath(e),!l(this.curatedDir))return[];let n=this.knowledgeRefForPath(e);if(!n)return[];let r=d([`log`,`--format=%H|%aI|%s`,n,`-n`,String(t)],this.curatedDir);return r?r.split(`
2
- `).filter(Boolean).map(e=>{let[t,n,...r]=e.split(`|`);return{sha:t,date:n,message:r.join(`|`)}}):[]}async diff(e,t,n){if(e=this.guardPath(e),!l(this.curatedDir))return``;if(!n){let r=await this.history(e,2);if(r.length===0)return``;n=r[0].sha,r.length>1&&!t&&(t=r[1].sha)}return t&&n?d([`diff`,`${t}:entry.md`,`${n}:entry.md`],this.curatedDir)??``:n?d([`show`,`${n}:entry.md`],this.curatedDir)??``:``}async recover(e){if(e=this.guardPath(e),await this.adapter.exists(e))throw Error(`Entry already exists on filesystem: ${e}. Use update() instead.`);if(!l(this.curatedDir))return null;let t=this.knowledgeRefForPath(e);if(!t)return null;let n=d([`show`,`${t}:entry.md`],this.curatedDir);if(!n)return null;await this.adapter.write(e,n);let{frontmatter:r,content:i}=this.parseFile(n);return await this.indexCuratedFile(e,i,r),{path:e,version:r.version??1}}async listOrphaned(){if(!l(this.curatedDir))return[];let e=d([`for-each-ref`,`--format=%(refname)|%(subject)`,`${S}/`],this.curatedDir);if(!e)return[];let t=[];for(let n of e.split(`
3
- `).filter(Boolean)){let[e,...r]=n.split(`|`),i=r.join(`|`),a=`${e.replace(`${S}/`,``)}.md`;await this.adapter.exists(a)||t.push({ref:e,path:a,lastMessage:i})}return t}async read(e){e=this.guardPath(e);let t=await this.adapter.read(e);if(t==null)throw Error(`Curated entry not found: ${e}`);let{frontmatter:n,content:r}=this.parseFile(t),i=e.split(`/`)[0];return{path:e,title:n.title??e,category:i,tags:n.tags??[],version:n.version??1,created:n.created??``,updated:n.updated??``,contentPreview:r.slice(0,200),content:r}}async list(e){let t=[],n=e?.category?[e.category]:await this.discoverCategories();for(let r of n){let n=await this.adapter.list(r);for(let i of n){let n=await this.adapter.read(i.path);if(n==null)continue;let{frontmatter:a,content:o}=this.parseFile(n);e?.tag&&!(a.tags??[]).includes(e.tag)||t.push({path:i.path,title:a.title??i.path,category:r,tags:a.tags??[],version:a.version??1,created:a.created??``,updated:a.updated??``,contentPreview:o.slice(0,200)})}}return t}async reindexAll(){let e=await this.discoverCategories(),t=[],n=[];for(let r of e){let e=await this.adapter.list(r);for(let r of e){let e=r.path;try{let r=await this.adapter.read(e);if(r==null){t.push(`${e}: read failed`);continue}let{frontmatter:i,content:a}=this.parseFile(r);n.push({relativePath:e,content:a,frontmatter:i})}catch(n){C.error(`Failed to read curated file`,{relativePath:e,...s(n)}),t.push(`${e}: read failed`)}}}if(n.length===0)return{indexed:0,errors:t};let r=new Date().toISOString(),i=0;for(let e=0;e<n.length;e+=16){let a=n.slice(e,e+16);i+=await this.embedAndUpsertBatch(a,r,t)}return{indexed:i,errors:t}}async embedAndUpsertBatch(e,t,n){try{let r=await this.embedder.embedBatch(e.map(e=>e.content)),i=e.map(e=>{let n=`.ai/curated/${e.relativePath}`;return{id:this.hashId(n,0),content:e.content,sourcePath:n,contentType:`curated-knowledge`,headingPath:e.frontmatter.title,chunkIndex:0,totalChunks:1,startLine:1,endLine:e.content.split(`
4
- `).length,fileHash:this.hash(e.content),indexedAt:t,origin:`curated`,tags:e.frontmatter.tags,category:e.frontmatter.category,version:e.frontmatter.version}});try{return await this.store.upsert(i,r),e.length}catch(t){C.error(`Failed to upsert curated batch`,{batchSize:e.length,...s(t)});for(let t of e)n.push(`${t.relativePath}: upsert failed`);return 0}}catch(r){if(e.length===1)return C.error(`Failed to embed curated item`,{relativePath:e[0].relativePath,...s(r)}),n.push(`${e[0].relativePath}: reindex failed`),0;C.warn(`Curated embed batch failed, retrying with smaller chunks`,{batchSize:e.length,...s(r)});let i=Math.ceil(e.length/2),a=e.slice(0,i),o=e.slice(i);return await this.embedAndUpsertBatch(a,t,n)+await this.embedAndUpsertBatch(o,t,n)}}gitCommitKnowledge(e,t,n){try{if(!l(this.curatedDir))return;let r=this.knowledgeRefForPath(e);if(!r)return;u(r,`entry.md`,t,n,this.curatedDir)}catch{}}gitDeleteKnowledgeRef(e){try{if(!l(this.curatedDir))return;let t=this.knowledgeRefForPath(e);if(!t)return;d([`update-ref`,`-d`,t],this.curatedDir)}catch{}}knowledgeRefForPath(e){let t=e.replace(/\.md$/,``).split(`/`).map(e=>f(e)).join(`/`);return t.split(`/`).every(e=>c.test(e))?`${S}/${t}`:null}async indexCuratedFile(e,t,n){let r=await this.embedder.embed(t),i=`.ai/curated/${e}`,a=new Date().toISOString(),o={id:this.hashId(i,0),content:t,sourcePath:i,contentType:`curated-knowledge`,headingPath:n.title,chunkIndex:0,totalChunks:1,startLine:1,endLine:t.split(`
5
- `).length,fileHash:this.hash(t),indexedAt:a,origin:`curated`,tags:n.tags,category:n.category,version:n.version};await this.store.upsert([o],[r])}async indexCuratedFileBestEffort(e,t,n,r){if(a.instance().isDegraded(`embedder`)){C.debug(`Skipping vector indexing — embedder degraded`,{relativePath:e,operation:r,subsystem:`embedder`});return}try{await this.indexCuratedFile(e,t,n)}catch(t){C.warn(`Curated file persisted but vector indexing deferred`,{relativePath:e,operation:r,...s(t)})}}async discoverCategories(){return this.adapter.listDirectories()}guardPath(e){let t=e.replace(/^\.ai\/curated\//,``);if(t.endsWith(`.md`)||(t+=`.md`),t.includes(`..`)||r(t))throw Error(`Invalid path: ${t}. Must be relative within .ai/curated/ directory.`);let n=t.split(`/`)[0];return this.validateCategoryName(n),t}validateCategoryName(e){if(!/^[a-z][a-z0-9-]*$/.test(e))throw Error(`Invalid category name: "${e}". Must be lowercase kebab-case (e.g., "decisions", "api-contracts").`)}validateContentSize(e){if(Buffer.byteLength(e,`utf-8`)>x)throw Error(`Content exceeds maximum size of ${x/1024}KB`)}slugify(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-|-$/g,``).slice(0,80)}normalizeTags(e){return[...new Set(e.map(e=>e.trim()).filter(Boolean))]}sameTags(e,t){if(e.length!==t.length)return!1;let n=new Set(e);return t.every(e=>n.has(e))}ensureCategoryPath(e,t){if(!e.startsWith(`${t}/`))throw Error(`Curated path "${e}" must stay within category "${t}"`)}async uniqueRelativePath(e,t){let n=`${e}/${t}.md`;if(!await this.adapter.exists(n))return n;for(let n=2;n<=100;n++){let r=`${e}/${t}-${n}.md`;if(!await this.adapter.exists(r))return r}throw Error(`Too many entries with slug "${t}" in category "${e}"`)}hash(e){return t(`sha256`).update(e).digest(`hex`).slice(0,16)}hashId(e,t){return this.hash(`${e}::${t}`)}serializeFile(e,t){return`${[`---`,`title: "${t.title.replace(/"/g,`\\"`)}"`,`category: ${t.category}`,`tags: [${t.tags.map(e=>`"${e}"`).join(`, `)}]`,`created: ${t.created}`,`updated: ${t.updated}`,`version: ${t.version}`,`origin: ${t.origin}`,`changelog:`,...t.changelog.map(e=>` - version: ${e.version}\n date: ${e.date}\n reason: "${e.reason.replace(/"/g,`\\"`)}"`),`---`].join(`
6
- `)}\n\n${e}\n`}parseFile(e){let t=e.match(/^---\n([\s\S]*?)\n---\n\n?([\s\S]*)$/);if(!t)return{frontmatter:{title:`Untitled`,category:`notes`,tags:[],created:``,updated:``,version:1,origin:`curated`,changelog:[]},content:e};let n=t[1],r=t[2].trim(),i={},a=[],o=n.split(`
7
- `),s=!1,c={};for(let e of o){if(/^changelog:\s*$/.test(e)){s=!0;continue}if(s){let t=e.match(/^\s+-\s+version:\s*(\d+)$/);if(t){c.version!=null&&a.push(c),c={version:parseInt(t[1],10)};continue}let n=e.match(/^\s+date:\s*(.+)$/);if(n){c.date=n[1].trim();continue}let r=e.match(/^\s+reason:\s*"?(.*?)"?\s*$/);if(r){c.reason=r[1];continue}/^\w/.test(e)&&(s=!1,c.version!=null&&a.push(c),c={});continue}let t=e.match(/^(\w+):\s*(.*)$/);if(t){let e=t[1],n=t[2];typeof n==`string`&&n.startsWith(`[`)&&n.endsWith(`]`)?n=n.slice(1,-1).split(`,`).map(e=>e.trim().replace(/^"|"$/g,``)).filter(e=>e.length>0):typeof n==`string`&&/^\d+$/.test(n)?n=parseInt(n,10):typeof n==`string`&&n.startsWith(`"`)&&n.endsWith(`"`)&&(n=n.slice(1,-1)),i[e]=n}}return c.version!=null&&a.push(c),{frontmatter:{title:i.title??`Untitled`,category:i.category??`notes`,tags:i.tags??[],created:i.created??``,updated:i.updated??``,version:i.version??1,origin:`curated`,changelog:a},content:r}}};export{b as n,y as r,w as t};