toolnet-memory 0.2.10 → 0.2.12

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.
@@ -1,8 +1,50 @@
1
- import{existsSync as jt}from"node:fs";import{dirname as kt,join as vt,resolve as wt}from"node:path";import{existsSync as ie,readFileSync as ae}from"node:fs";import{homedir as ce}from"node:os";import{join as ue}from"node:path";function pe(r){let e=r.trim();return e.length>=2&&e.startsWith('"')&&e.endsWith('"')?(e=e.slice(1,-1),e.replace(/\\n/g,`
2
- `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function de(){let r=process.env.TOOLNET_GLOBAL_ENV??ue(ce(),".config","toolnet-memory",".env");if(!ie(r))return;let e=ae(r,"utf8");for(let t of e.split(/\r?\n/)){let s=t.trim();if(!s||s.startsWith("#"))continue;s.startsWith("export ")&&(s=s.slice(7));let o=s.indexOf("=");if(o<=0)continue;let n=s.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(n)&&process.env[n]===void 0&&(process.env[n]=pe(s.slice(o+1)))}}de();function h(r,e){return r===void 0?e:["1","true","yes","on"].includes(r.toLowerCase())}function y(r,e){if(!r)return e;let t=Number(r);return Number.isFinite(t)?t:e}function _(){return{memory:{autoCapture:h(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:h(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:h(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:h(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:y(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:y(process.env.MEMORY_RERANK_TOP,10),finalContext:y(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:y(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:h(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:y(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as le}from"node:crypto";import{existsSync as R,mkdirSync as me,readFileSync as fe,renameSync as ge,writeFileSync as he}from"node:fs";import{basename as ye,dirname as w,join as b,parse as B,resolve as S}from"node:path";var D=".toolnet",Se="project.json";function je(r){return le("sha256").update(r).digest("hex").slice(0,16)}function I(r){return b(r,D,Se)}function ke(r){return R(I(r))}function ve(r,e){let t=S(r),s=B(t).root;for(;;){if(ke(t))return t;if(t===s||e&&t===S(e))break;let o=w(t);if(o===t)break;t=o}return null}function we(r){let e=S(r),t=B(e).root,s=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(s.some(n=>R(b(e,n))))return e;if(e===t)break;let o=w(e);if(o===e)break;e=o}return S(r)}function xe(r){let e;try{e=JSON.parse(fe(r,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${r}: ${o instanceof Error?o.message:String(o)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${r}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${r}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${r}`);let s=new Date().toISOString();return{version:1,id:t.id,name:t.name,remote:typeof t.remote=="string"&&t.remote.trim()?t.remote:t.name,rootPath:typeof t.rootPath=="string"?t.rootPath:w(w(r)),createdAt:typeof t.createdAt=="string"?t.createdAt:s,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:s,graphVersion:typeof t.graphVersion=="number"?t.graphVersion:0,memoryVersion:typeof t.memoryVersion=="number"?t.memoryVersion:0,metadata:t.metadata&&typeof t.metadata=="object"?t.metadata:void 0}}function K(r,e){let t=b(r,D);me(t,{recursive:!0});let s=I(r),o=`${s}.tmp-${process.pid}`;he(o,JSON.stringify(e,null,2)+`
3
- `,{encoding:"utf8",mode:384}),ge(o,s)}function W(r,e){return{id:r.id,name:r.name,remote:r.remote,rootPath:e,createdAt:r.createdAt,updatedAt:r.updatedAt,graphVersion:r.graphVersion,memoryVersion:r.memoryVersion,metadata:r.metadata}}var x=class{detect(e=process.cwd()){let t=S(e),s=we(t),n=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>R(b(s,m))),c=ve(t,n?s:void 0);if(c){let m=I(c),i=xe(m);return i.rootPath!==c&&(i.rootPath=c,i.updatedAt=new Date().toISOString(),K(c,i)),W(i,c)}let p=new Date().toISOString(),a=ye(s),d={version:1,id:je(s),name:a,remote:a,rootPath:s,createdAt:p,updatedAt:p,graphVersion:0,memoryVersion:0};return K(s,d),W(d,s)}};import{homedir as Ue}from"node:os";import{join as He}from"node:path";import{DeleteObjectCommand as be,GetObjectCommand as Pe,HeadObjectCommand as Ae,ListObjectsV2Command as Te,PutObjectCommand as Re,S3Client as Ie}from"@aws-sdk/client-s3";import{getSignedUrl as Oe}from"@aws-sdk/s3-request-presigner";var P=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Ie({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,s="application/octet-stream"){let o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Re({Bucket:this.bucket,Key:e,Body:o,ContentType:s}))}async get(e){let t=await Oe(this.client,new Pe({Bucket:this.bucket,Key:e}),{expiresIn:60}),s=await fetch(t,{redirect:"follow"});if(s.status===404)return null;if(!s.ok)throw new Error(`HF download failed: ${s.status} ${s.statusText}`);return new Uint8Array(await s.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new Ae({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new be({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],s;do{let o=await this.client.send(new Te({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:s}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});s=o.IsTruncated?o.NextContinuationToken:void 0}while(s);return t}};import{access as L,mkdir as Ee,readFile as Ce,readdir as Me,rm as Ne,stat as V,writeFile as $e}from"node:fs/promises";import{dirname as _e,join as Ke,relative as F,resolve as We}from"node:path";var j=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return We(this.root,t)}async put(e,t){let s=this.path(e);await Ee(_e(s),{recursive:!0}),await $e(s,t)}async get(e){try{return await Ce(this.path(e))}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return null;throw t}}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await L(this.path(e)),!0}catch{return!1}}async delete(e){await Ne(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),s=[];try{await L(t)}catch{return s}let o=async c=>{let p=await Me(c,{withFileTypes:!0});for(let a of p){let d=Ke(c,a.name);if(a.isDirectory()){await o(d);continue}let m=await V(d);s.push({key:F(this.root,d),size:m.size,updatedAt:m.mtime.toISOString()})}},n=await V(t);return n.isDirectory()?await o(t):s.push({key:F(this.root,t),size:n.size,updatedAt:n.mtime.toISOString()}),s}};import{DeleteObjectCommand as Be,GetObjectCommand as De,HeadObjectCommand as Le,ListObjectsV2Command as Ve,PutObjectCommand as Fe,S3Client as Je}from"@aws-sdk/client-s3";import{getSignedUrl as ze}from"@aws-sdk/s3-request-presigner";var k=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new Je({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,s="application/octet-stream"){let o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Fe({Bucket:this.bucket,Key:e,Body:o,ContentType:s}))}async get(e){let t=await ze(this.client,new De({Bucket:this.bucket,Key:e}),{expiresIn:60}),s=await fetch(t,{redirect:"follow"});if(s.status===404)return null;if(!s.ok)throw new Error(`${this.name} download failed: ${s.status} ${s.statusText}`);return new Uint8Array(await s.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new Le({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new Be({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],s;do{let o=await this.client.send(new Ve({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:s}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});s=o.IsTruncated?o.NextContinuationToken:void 0}while(s);return t}};function O(r,e){return console.warn(e),new j(r)}function J(r){let e=r.localRoot??He(Ue(),".toolnet-memory","storage");if(r.provider==="r2"){let t=r.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new k({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):O(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(r.provider==="s3"){let t=r.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new k({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):O(e,"[storage] S3 credentials missing. Using local fallback.")}if(r.provider==="huggingface"){let t=r.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new P({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):O(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new j(e)}function Ye(r){return new Promise(e=>setTimeout(e,r))}async function z(r,e={}){let t=Math.max(1,e.attempts??3),s=e.baseDelayMs??150,o=e.maxDelayMs??2e3,n;for(let c=1;c<=t;c++)try{return await r()}catch(p){if(n=p,c>=t)break;let a=Math.min(o,s*2**(c-1)),d=Math.floor(Math.random()*Math.max(1,a*.2));await Ye(a+d)}throw n}var Ge=new Set(["put","get","getText","delete","list"]);function U(r,e={}){return new Proxy(r,{get(t,s){let o=Reflect.get(t,s,t);return typeof o!="function"?o:Ge.has(s)?(...n)=>z(()=>Promise.resolve(o.apply(t,n)),e):o.bind(t)}})}function H(r){let e=r.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function Y(r){let e=r.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let t=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(t)return`${t[1]}memory/records/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),t)return`${t[1]}memory/vectors/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),t)return`${t[1]}code/graph/${t[2]}`;let s=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(s)return`${s[1]}memory/records/${s[2]}`;if(s=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),s)return`${s[1]}memory/vectors/${s[2]}`;if(s=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),s)return`${s[1]}code/graph/${s[2]}`;let o=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return o?`${o[1]}memory/records/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),o?`${o[1]}memory/vectors/${o[2]}`:(o=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),o?`${o[1]}code/graph/${o[2]}`:e))}var A=class{constructor(e,t,s,o){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=s,this.folder=H(o??s),this.sourcePrefix=`projects/${t}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,t=new Date().toISOString(),s=t,o=await this.provider.getText(e);if(o){let c;try{c=JSON.parse(o)}catch(p){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${p instanceof Error?p.message:String(p)}`)}if(typeof c.id=="string"&&c.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${c.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof c.createdAt=="string"&&(s=c.createdAt)}let n={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:s,updatedAt:t};await this.provider.put(e,JSON.stringify(n,null,2)+`
4
- `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=Y(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,t,s){return await this.ensureRegistered(),this.provider.put(this.key(e),t,s)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};import{existsSync as mt,mkdirSync as ft,readFileSync as gt,writeFileSync as ht}from"node:fs";import{dirname as yt,join as te}from"node:path";import{createHash as Qe}from"node:crypto";import{dirname as qe}from"node:path";import{mkdirSync as Ze,readFileSync as Ss,renameSync as Xe,writeFileSync as et}from"node:fs";function g(r){return Qe("sha256").update(r).digest("hex")}function v(r,e){Ze(qe(r),{recursive:!0});let t=`${r}.${process.pid}.tmp`;et(t,JSON.stringify(e,null,2)+`
5
- `,{encoding:"utf8",mode:384}),Xe(t,r)}import{existsSync as q,mkdirSync as tt,readFileSync as rt,statSync as G,writeFileSync as st}from"node:fs";import{dirname as ot,join as nt}from"node:path";var Q=64*1024,it=`# ToolNet Project Operating Manual
1
+ #!/usr/bin/env node
2
+ import*as N from"fs";import*as he from"path";import*as y from"fs";import*as f from"path";import*as U from"crypto";function j(r){return r?Math.ceil(r.length/3.5):0}function $(r,t){if(!r)return"";if(j(r)<=t)return r;let o=Math.floor(t*3.5),n=r.slice(0,o),s=n.lastIndexOf("."),c=n.lastIndexOf(`
3
+ `),l=Math.max(s,c);return l>o*.7?n.slice(0,l+1):n}function V(r,t){if(!r)return"";let e=r.split(`
4
+ `).filter(s=>s.trim());if(e.length<=t)return r;let o=e.slice(0,t),n=e.length-t;return[...o,`... (${n} more items omitted)`].join(`
5
+ `)}function ye(r,t){let{maxTokens:e,trimMarker:o="[Context trimmed by ToolNet Memory token budget]"}=t;if(r.length===0)return"";let n=[...r].sort((i,u)=>u.priority-i.priority),s=[],c=0,l=!1;for(let i of n){let u=`# ${i.title}
6
+
7
+ `,m=j(u),a=j(i.content),g=m+a;if(c+g<=e)s.push(u+i.content),c+=g;else{let p=e-c-m;if(p>50){let h=$(i.content,p);s.push(u+h),c=e,l=!0}else l=!0;break}}return l&&s.push(`
8
+ ${o}
9
+ `),s.join(`
10
+
11
+ ---
12
+
13
+ `)}function J(r,t){let e=[{title:"Profile",content:V(r,10),priority:100},{title:"Current Work",content:V(t,15),priority:90}];return ye(e,{maxTokens:800})}function k(r){let t=f.resolve(r),e=f.parse(t).root;for(;t!==e;){let o=f.join(t,".toolnet");if(y.existsSync(o)&&y.statSync(o).isDirectory())return t;t=f.dirname(t)}return null}function C(r){try{return y.existsSync(r)?y.readFileSync(r,"utf-8").trim():null}catch{return null}}function z(r){return r.split(`
14
+ `).filter(o=>{let n=o.toUpperCase();return!(n.includes("SECRET")||n.includes("TOKEN")||n.includes("API_KEY")||n.includes("APIKEY")||n.includes("PASSWORD")||n.includes("PASS="))}).join(`
15
+ `)}function _(r={}){let t=r.projectPath||process.cwd(),e=k(t);if(!e)return null;let o=f.join(e,".toolnet","project.json"),n="Unknown";try{if(y.existsSync(o)){let g=JSON.parse(y.readFileSync(o,"utf-8"));n=g.name||g.projectName||"Unknown"}}catch{}let s=f.join(e,".toolnet","profile.md"),c=f.join(e,".toolnet","current.md"),l=C(s)||"",i=C(c)||"";l=z(l),i=z(i);let u=`[TOOLNET PROJECT CONTEXT]
16
+
17
+ Project: ${n}
18
+ Root: ${e}
19
+
20
+ `,m=J(l,i);return u+m+`
21
+ Forbidden At Startup:
22
+ - Do not run session:agy-recover, handoff:latest, or brief automatically
23
+ - Deep memory only when user explicitly asks
24
+ `}function H(r={}){let t=r.projectPath||process.cwd(),e=k(t);if(!e)throw new Error("Not in a ToolNet project (no .toolnet directory found)");let o=f.join(e,".toolnet","profile.md"),n=f.join(e,".toolnet","current.md"),s=C(o)||"",c=C(n)||"",l=`# AI Startup Instructions
25
+
26
+ Read and follow:
27
+ - .toolnet/profile.md
28
+ - .toolnet/current.md
29
+
30
+ Rules:
31
+ - Fast context first.
32
+ - Do not run session:agy-recover, handoff:latest, or brief automatically.
33
+ - Deep memory only when the user explicitly asks.
34
+
35
+ ---
36
+
37
+ ## Profile
38
+
39
+ ${s}
40
+
41
+ ---
42
+
43
+ ## Current Work
44
+
45
+ ${c}
46
+ `,i=["GEMINI.md","AGENTS.md","CLAUDE.md"],u=[];for(let m of i){let a=f.join(e,m);try{y.writeFileSync(a,l,"utf-8"),u.push(m)}catch(g){console.error(`Failed to write ${m}:`,g)}}return u}function Y(r){return U.createHash("sha256").update(r,"utf-8").digest("hex")}import{existsSync as dr,mkdirSync as $e,readFileSync as mr,writeFileSync as _e}from"node:fs";import{dirname as Ke,join as te}from"node:path";import{createHash as Se}from"node:crypto";import{dirname as je}from"node:path";import{mkdirSync as ke,readFileSync as zt,renameSync as ve,writeFileSync as xe}from"node:fs";function S(r){return Se("sha256").update(r).digest("hex")}function v(r,t){ke(je(r),{recursive:!0});let e=`${r}.${process.pid}.tmp`;xe(e,JSON.stringify(t,null,2)+`
47
+ `,{encoding:"utf8",mode:384}),ve(e,r)}import{existsSync as q,mkdirSync as we,readFileSync as be,statSync as G,writeFileSync as Pe}from"node:fs";import{dirname as Ae,join as Ce}from"node:path";var Q=64*1024,Te=`# ToolNet Project Operating Manual
6
48
 
7
49
  This file contains persistent instructions for AI agents working on this project.
8
50
 
@@ -45,12 +87,17 @@ Things an AI agent should always remember.
45
87
  <!--
46
88
  - [advisory] Prefer small focused files.
47
89
  -->
48
- `;function T(r){return nt(r.rootPath,".toolnet","PROJECT.md")}function at(r){return r.normalize("NFKC").replace(/\s+/g," ").trim()}function ct(r){let e=[],t=new Set,s=/^\s*[-*]\s+\[(enforce|advisory)\]\s+(.+?)\s*$/gimu,o;for(;o=s.exec(r);){let n=o[1].toLowerCase(),c=at(o[2]);if(!c)continue;let p=`${n}:${c.toLowerCase()}`;t.has(p)||(t.add(p),e.push({id:g(p).slice(0,24),mode:n,text:c,source:"manual"}))}return e}function ut(r){let e=T(r);return q(e)||(tt(ot(e),{recursive:!0}),st(e,it,{encoding:"utf8",mode:384})),e}function E(r,e=!1){let t=e?ut(r):T(r);if(!q(t))return null;if(G(t).size>Q)throw new Error(`PROJECT.md exceeds ${Q} bytes`);let o=rt(t,"utf8");return{path:t,content:o,digest:g(o),rules:ct(o),bytes:Buffer.byteLength(o,"utf8"),updatedAt:new Date(G(t).mtimeMs).toISOString()}}async function C(r,e){let t=await e.getText(`projects/${r.id}/work/current.json`);if(!t)return null;try{return JSON.parse(t)}catch{return null}}async function Z(r,e){let t=await e.getText(`projects/${r.id}/work/handoff-latest.json`);if(!t)return null;try{return JSON.parse(t)}catch{return null}}async function X(r,e){let t=await e.getText(`projects/${r.id}/work/semantic/current.json`);if(!t)return null;try{return JSON.parse(t)}catch{return null}}function M(r){if(!r)return 0;let e=Array.from(r).length,t=r.trim().split(/\s+/u).filter(Boolean).length;return Math.ceil(Math.max(e/3.5,t*1.3))}function l(r,e){let t=r.replace(/\s+/g," ").trim();return t.length<=e?t:t.slice(0,Math.max(0,e-1)).trimEnd()+"\u2026"}function pt(r){let e=[],t=!1;for(let s of r.split(/\r?\n/u)){let o=s.trim();if(o.includes("<!--")&&(t=!0),t){o.includes("-->")&&(t=!1);continue}let n=o.toLowerCase();if(!(!o||o.startsWith("#")||o==="```"||n.startsWith("- [enforce]")||n.startsWith("* [enforce]")||n.startsWith("- [advisory]")||n.startsWith("* [advisory]"))&&(o=o.replace(/^[-*]\s+/u,""),o&&e.push(l(o,280)),e.length>=16))break}return e}function dt(r){let e=[],t=[];for(let s of r.split(/\\r?\\n/u)){let o=s.trim(),n=o.toLowerCase(),p=["- [enforce]","* [enforce]","- [advisory]","* [advisory]"].find(d=>n.startsWith(d));if(!p)continue;let a=o.slice(p.length).trim();a&&(p.includes("enforce")?e.push(a):t.push(a))}return{enforce:e,advisory:t}}function lt(r,e){let t=[];for(let s of r){let o=[...t,s].join(`
49
- `);if(M(o)<=e){t.push(s);continue}let n=M(t.join(`
50
- `)),c=Math.max(0,e-n);if(c>=16){let p=Math.floor(c*3.2),a=l(s,p);a&&t.push(a)}break}return t.join(`
51
- `).trim()}async function ee(r){let e=Math.max(256,Math.min(2e3,r.maxTokens??1e3)),t=E(r.project,!1),s=t?.content??"";s||(s=await r.storage.getText(`projects/${r.project.id}/project/manual.md`)??"");let o=dt(s),n=t?t.rules.filter(u=>u.mode==="enforce").map(u=>u.text):o.enforce,c=t?t.rules.filter(u=>u.mode==="advisory").map(u=>u.text):o.advisory,p=s?pt(s):[],a=await C(r.project,r.storage),d=await X(r.project,r.storage),m=await Z(r.project,r.storage),i=[];if(i.push("[TOOLNET PROJECT CONTEXT]"),i.push(`Project: ${r.project.name}`),i.push("Continue existing project state. Do not restart completed work unless evidence shows it is necessary."),s&&i.push(`Full operating manual: ${T(r.project)}`),n.length){i.push("","PROJECT RULES \u2014 MUST FOLLOW");for(let u of n.slice(0,24))i.push(`- [ENFORCE] ${l(u,240)}`)}if(c.length){i.push("","PROJECT PREFERENCES");for(let u of c.slice(0,10))i.push(`- ${l(u,220)}`)}if(d&&(d.mission&&i.push("","MISSION",l(d.mission.value,420)),d.activeObjective&&i.push("","CURRENT OBJECTIVE",l(d.activeObjective.value,420)),d.why&&i.push("","WHY THIS WORK MATTERS",l(d.why.value,420)),d.desiredOutcome&&i.push("","DESIRED OUTCOME",l(d.desiredOutcome.value,420)),d.planRationale&&i.push("","WHY THIS APPROACH",l(d.planRationale.value,420))),a){if(i.push("","ACTIVE WORK"),a.goal&&i.push(`Goal: ${l(a.goal,320)}`),a.plan&&i.push(`Plan: ${l(a.plan,320)}`),i.push(`Progress: phases ${a.progress.phasesCompleted}/${a.progress.phasesTotal}; tasks ${a.progress.tasksCompleted}/${a.progress.tasksTotal}; blocked ${a.progress.blocked}`),a.currentPhase&&i.push(`Current phase: ${a.currentPhase.title} [${a.currentPhase.status}]`),a.currentPhase&&d){let u=d.phases.find(f=>f.order===a.currentPhase?.order);u&&(u.objective&&i.push(`Phase objective: ${l(u.objective.value,340)}`),u.why?i.push(`Why this phase: ${l(u.why.value,340)}`):i.push("Why this phase: not explicitly recorded. Inspect existing implementation before assuming intent."),u.deliverable&&i.push(`Deliverable: ${l(u.deliverable.value,340)}`),u.dependencies.length&&i.push(`Depends on: ${u.dependencies.slice(0,4).map(f=>l(f.value,180)).join("; ")}`),u.acceptanceCriteria.length&&(i.push("","DEFINITION OF DONE"),u.acceptanceCriteria.slice(0,6).forEach(f=>{i.push(`- ${l(f.value,260)}`)})),u.openQuestions.length&&(i.push("","OPEN QUESTIONS FOR CURRENT PHASE"),u.openQuestions.slice(0,4).forEach(f=>{i.push(`- ${l(f.value,260)}`)})))}a.currentTask&&i.push(`Current task: ${a.currentTask.title} [${a.currentTask.status}]`),a.nextActions.length&&(i.push("","NEXT ACTIONS"),a.nextActions.slice(0,6).forEach((u,f)=>{i.push(`${f+1}. ${l(u,260)}`)})),a.blockers.length&&(i.push("","BLOCKERS"),a.blockers.slice(0,5).forEach(u=>{i.push(`- ${l(u,260)}`)})),a.warnings.length&&(i.push("","ATTENTION"),a.warnings.slice(-5).forEach(u=>{i.push(`- ${l(u,260)}`)})),a.decisions.length&&(i.push("","RECENT DECISIONS"),a.decisions.slice(-5).forEach(u=>{i.push(`- ${l(u,260)}`)})),a.lastSession&&i.push("",`Last work session: ${a.lastSession.agent} / ${a.lastSession.nativeSessionId}`)}if(d&&d.openQuestions.length&&(i.push("","UNRESOLVED QUESTIONS"),d.openQuestions.slice(0,5).forEach(u=>{i.push(`- ${l(u.value,260)}`)})),m&&i.push(`Latest handoff: ${m.reason} / ${m.sourceSession.agent}`),p.length){i.push("","OPERATING NOTES");for(let u of p)i.push(`- ${u}`)}i.push("","Before changing anything: verify the current repository state and continue from the active phase/task above.");let $=lt(i,e);return{version:1,projectId:r.project.id,projectName:r.project.name,text:$,estimatedTokens:M($),maxTokens:e,hasManual:!!s,hasWorkState:!!a,hasHandoff:!!m,generatedAt:new Date().toISOString()}}function re(r){return te(r.rootPath,".toolnet","context","startup.md")}function St(r){return te(r.rootPath,".toolnet","context","startup.json")}function se(r,e){let t=re(r);ft(yt(t),{recursive:!0}),ht(t,e.text.endsWith(`
52
- `)?e.text:e.text+`
53
- `,{encoding:"utf8",mode:384}),v(St(r),e)}async function N(r,e,t=900){let s=await ee({project:r,storage:e,maxTokens:t}),o={version:1,projectId:r.id,projectName:r.name,text:s.text,digest:g(s.text),estimatedTokens:s.estimatedTokens,generatedAt:new Date().toISOString()};return se(r,o),await e.put(`projects/${r.id}/context/startup.md`,o.text+`
54
- `,"text/markdown"),await e.put(`projects/${r.id}/context/startup.json`,JSON.stringify(o,null,2)+`
55
- `,"application/json"),o}async function oe(r,e,t=900){try{let o=await e.getText(`projects/${r.id}/context/startup.md`);if(o&&o.trim()){let n=o.trim(),c={version:1,projectId:r.id,projectName:r.name,text:n,digest:g(n),estimatedTokens:Math.ceil(n.length/3.5),generatedAt:new Date().toISOString()};return se(r,c),c}}catch{}let s=re(r);if(mt(s))try{let o=gt(s,"utf8").trim();if(o)return{version:1,projectId:r.id,projectName:r.name,text:o,digest:g(o),estimatedTokens:Math.ceil(o.length/3.5),generatedAt:new Date().toISOString()}}catch{}try{return await N(r,e,t)}catch{return null}}function ne(r,e){let t=r.indexOf(e);return t>=0?r[t+1]:void 0}function xt(r){let e=wt(r);for(;;){if(jt(vt(e,".toolnet","project.json")))return e;let t=kt(e);if(t===e)return null;e=t}}function bt(r){let e=_(),t=U(J({provider:e.storage.provider,huggingface:e.storage.huggingface,localRoot:e.storage.localRoot}),{attempts:2});return new A(t,r.id,r.name,r.remote??r.name)}async function Pt(){let[r="print",...e]=process.argv.slice(2),t=ne(e,"--project")??process.cwd(),s=xt(t);if(!s)return;let o=new x().detect(s),n=bt(o),c=Number(ne(e,"--tokens")??900);if(r==="refresh"){let a=await N(o,n,Number.isFinite(c)?c:900);console.log(a.text);return}let p=await oe(o,n,Number.isFinite(c)?c:900);if(p){if(r==="sync"){console.log(p.digest);return}process.stdout.write(p.text+`
56
- `)}}Pt().catch(()=>{process.exitCode=0});
90
+ `;function T(r){return Ce(r.rootPath,".toolnet","PROJECT.md")}function Oe(r){return r.normalize("NFKC").replace(/\s+/g," ").trim()}function Re(r){let t=[],e=new Set,o=/^\s*[-*]\s+\[(enforce|advisory)\]\s+(.+?)\s*$/gimu,n;for(;n=o.exec(r);){let s=n[1].toLowerCase(),c=Oe(n[2]);if(!c)continue;let l=`${s}:${c.toLowerCase()}`;e.has(l)||(e.add(l),t.push({id:S(l).slice(0,24),mode:s,text:c,source:"manual"}))}return t}function Ee(r){let t=T(r);return q(t)||(we(Ae(t),{recursive:!0}),Pe(t,Te,{encoding:"utf8",mode:384})),t}function K(r,t=!1){let e=t?Ee(r):T(r);if(!q(e))return null;if(G(e).size>Q)throw new Error(`PROJECT.md exceeds ${Q} bytes`);let n=be(e,"utf8");return{path:e,content:n,digest:S(n),rules:Re(n),bytes:Buffer.byteLength(n,"utf8"),updatedAt:new Date(G(e).mtimeMs).toISOString()}}async function D(r,t){let e=await t.getText(`projects/${r.id}/work/current.json`);if(!e)return null;try{return JSON.parse(e)}catch{return null}}async function Z(r,t){let e=await t.getText(`projects/${r.id}/work/handoff-latest.json`);if(!e)return null;try{return JSON.parse(e)}catch{return null}}async function X(r,t){let e=await t.getText(`projects/${r.id}/work/semantic/current.json`);if(!e)return null;try{return JSON.parse(e)}catch{return null}}function B(r){if(!r)return 0;let t=Array.from(r).length,e=r.trim().split(/\s+/u).filter(Boolean).length;return Math.ceil(Math.max(t/3.5,e*1.3))}function d(r,t){let e=r.replace(/\s+/g," ").trim();return e.length<=t?e:e.slice(0,Math.max(0,t-1)).trimEnd()+"\u2026"}function Ie(r){let t=[],e=!1;for(let o of r.split(/\r?\n/u)){let n=o.trim();if(n.includes("<!--")&&(e=!0),e){n.includes("-->")&&(e=!1);continue}let s=n.toLowerCase();if(!(!n||n.startsWith("#")||n==="```"||s.startsWith("- [enforce]")||s.startsWith("* [enforce]")||s.startsWith("- [advisory]")||s.startsWith("* [advisory]"))&&(n=n.replace(/^[-*]\s+/u,""),n&&t.push(d(n,280)),t.length>=16))break}return t}function Me(r){let t=[],e=[];for(let o of r.split(/\\r?\\n/u)){let n=o.trim(),s=n.toLowerCase(),l=["- [enforce]","* [enforce]","- [advisory]","* [advisory]"].find(u=>s.startsWith(u));if(!l)continue;let i=n.slice(l.length).trim();i&&(l.includes("enforce")?t.push(i):e.push(i))}return{enforce:t,advisory:e}}function Ne(r,t){let e=[];for(let o of r){let n=[...e,o].join(`
91
+ `);if(B(n)<=t){e.push(o);continue}let s=B(e.join(`
92
+ `)),c=Math.max(0,t-s);if(c>=16){let l=Math.floor(c*3.2),i=d(o,l);i&&e.push(i)}break}return e.join(`
93
+ `).trim()}async function ee(r){let t=Math.max(256,Math.min(2e3,r.maxTokens??1e3)),e=K(r.project,!1),o=e?.content??"";o||(o=await r.storage.getText(`projects/${r.project.id}/project/manual.md`)??"");let n=Me(o),s=e?e.rules.filter(p=>p.mode==="enforce").map(p=>p.text):n.enforce,c=e?e.rules.filter(p=>p.mode==="advisory").map(p=>p.text):n.advisory,l=o?Ie(o):[],i=await D(r.project,r.storage),u=await X(r.project,r.storage),m=await Z(r.project,r.storage),a=[];if(a.push("[TOOLNET PROJECT CONTEXT]"),a.push(`Project: ${r.project.name}`),a.push("Continue existing project state. Do not restart completed work unless evidence shows it is necessary."),o&&a.push(`Full operating manual: ${T(r.project)}`),s.length){a.push("","PROJECT RULES \u2014 MUST FOLLOW");for(let p of s.slice(0,24))a.push(`- [ENFORCE] ${d(p,240)}`)}if(c.length){a.push("","PROJECT PREFERENCES");for(let p of c.slice(0,10))a.push(`- ${d(p,220)}`)}if(u&&(u.mission&&a.push("","MISSION",d(u.mission.value,420)),u.activeObjective&&a.push("","CURRENT OBJECTIVE",d(u.activeObjective.value,420)),u.why&&a.push("","WHY THIS WORK MATTERS",d(u.why.value,420)),u.desiredOutcome&&a.push("","DESIRED OUTCOME",d(u.desiredOutcome.value,420)),u.planRationale&&a.push("","WHY THIS APPROACH",d(u.planRationale.value,420))),i){if(a.push("","ACTIVE WORK"),i.goal&&a.push(`Goal: ${d(i.goal,320)}`),i.plan&&a.push(`Plan: ${d(i.plan,320)}`),a.push(`Progress: phases ${i.progress.phasesCompleted}/${i.progress.phasesTotal}; tasks ${i.progress.tasksCompleted}/${i.progress.tasksTotal}; blocked ${i.progress.blocked}`),i.currentPhase&&a.push(`Current phase: ${i.currentPhase.title} [${i.currentPhase.status}]`),i.currentPhase&&u){let p=u.phases.find(h=>h.order===i.currentPhase?.order);p&&(p.objective&&a.push(`Phase objective: ${d(p.objective.value,340)}`),p.why?a.push(`Why this phase: ${d(p.why.value,340)}`):a.push("Why this phase: not explicitly recorded. Inspect existing implementation before assuming intent."),p.deliverable&&a.push(`Deliverable: ${d(p.deliverable.value,340)}`),p.dependencies.length&&a.push(`Depends on: ${p.dependencies.slice(0,4).map(h=>d(h.value,180)).join("; ")}`),p.acceptanceCriteria.length&&(a.push("","DEFINITION OF DONE"),p.acceptanceCriteria.slice(0,6).forEach(h=>{a.push(`- ${d(h.value,260)}`)})),p.openQuestions.length&&(a.push("","OPEN QUESTIONS FOR CURRENT PHASE"),p.openQuestions.slice(0,4).forEach(h=>{a.push(`- ${d(h.value,260)}`)})))}i.currentTask&&a.push(`Current task: ${i.currentTask.title} [${i.currentTask.status}]`),i.nextActions.length&&(a.push("","NEXT ACTIONS"),i.nextActions.slice(0,6).forEach((p,h)=>{a.push(`${h+1}. ${d(p,260)}`)})),i.blockers.length&&(a.push("","BLOCKERS"),i.blockers.slice(0,5).forEach(p=>{a.push(`- ${d(p,260)}`)})),i.warnings.length&&(a.push("","ATTENTION"),i.warnings.slice(-5).forEach(p=>{a.push(`- ${d(p,260)}`)})),i.decisions.length&&(a.push("","RECENT DECISIONS"),i.decisions.slice(-5).forEach(p=>{a.push(`- ${d(p,260)}`)})),i.lastSession&&a.push("",`Last work session: ${i.lastSession.agent} / ${i.lastSession.nativeSessionId}`)}if(u&&u.openQuestions.length&&(a.push("","UNRESOLVED QUESTIONS"),u.openQuestions.slice(0,5).forEach(p=>{a.push(`- ${d(p.value,260)}`)})),m&&a.push(`Latest handoff: ${m.reason} / ${m.sourceSession.agent}`),l.length){a.push("","OPERATING NOTES");for(let p of l)a.push(`- ${p}`)}a.push("","Before changing anything: verify the current repository state and continue from the active phase/task above.");let g=Ne(a,t);return{version:1,projectId:r.project.id,projectName:r.project.name,text:g,estimatedTokens:B(g),maxTokens:t,hasManual:!!o,hasWorkState:!!i,hasHandoff:!!m,generatedAt:new Date().toISOString()}}function De(r){return te(r.rootPath,".toolnet","context","startup.md")}function Be(r){return te(r.rootPath,".toolnet","context","startup.json")}function We(r,t){let e=De(r);$e(Ke(e),{recursive:!0}),_e(e,t.text.endsWith(`
94
+ `)?t.text:t.text+`
95
+ `,{encoding:"utf8",mode:384}),v(Be(r),t)}async function re(r,t,e=800){let n=(await ee({project:r,storage:t,maxTokens:e})).text;j(n)>e&&(n=$(n,e),n+=`
96
+
97
+ [Context trimmed by ToolNet Memory token budget]
98
+ `);let c={version:1,projectId:r.id,projectName:r.name,text:n,digest:S(n),estimatedTokens:j(n),generatedAt:new Date().toISOString()};return We(r,c),await t.put(`projects/${r.id}/context/startup.md`,c.text+`
99
+ `,"text/markdown"),await t.put(`projects/${r.id}/context/startup.json`,JSON.stringify(c,null,2)+`
100
+ `,"application/json"),c}import{existsSync as Fe,readFileSync as Le}from"node:fs";import{homedir as Ve}from"node:os";import{join as Je}from"node:path";function ze(r){let t=r.trim();return t.length>=2&&t.startsWith('"')&&t.endsWith('"')?(t=t.slice(1,-1),t.replace(/\\n/g,`
101
+ `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):t.length>=2&&t.startsWith("'")&&t.endsWith("'")?t.slice(1,-1):t}function Ue(){let r=process.env.TOOLNET_GLOBAL_ENV??Je(Ve(),".config","toolnet-memory",".env");if(!Fe(r))return;let t=Le(r,"utf8");for(let e of t.split(/\r?\n/)){let o=e.trim();if(!o||o.startsWith("#"))continue;o.startsWith("export ")&&(o=o.slice(7));let n=o.indexOf("=");if(n<=0)continue;let s=o.slice(0,n).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(s)&&process.env[s]===void 0&&(process.env[s]=ze(o.slice(n+1)))}}Ue();function x(r,t){return r===void 0?t:["1","true","yes","on"].includes(r.toLowerCase())}function w(r,t){if(!r)return t;let e=Number(r);return Number.isFinite(e)?e:t}function oe(){return{memory:{autoCapture:x(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:x(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:x(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:x(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:w(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:w(process.env.MEMORY_RERANK_TOP,10),finalContext:w(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:w(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:x(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:w(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as He}from"node:crypto";import{existsSync as W,mkdirSync as Ye,readFileSync as Ge,renameSync as Qe,writeFileSync as qe}from"node:fs";import{basename as Ze,dirname as O,join as E,parse as ie,resolve as b}from"node:path";var ae=".toolnet",Xe="project.json";function et(r){return He("sha256").update(r).digest("hex").slice(0,16)}function F(r){return E(r,ae,Xe)}function tt(r){return W(F(r))}function rt(r,t){let e=b(r),o=ie(e).root;for(;;){if(tt(e))return e;if(e===o||t&&e===b(t))break;let n=O(e);if(n===e)break;e=n}return null}function ot(r){let t=b(r),e=ie(t).root,o=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(o.some(s=>W(E(t,s))))return t;if(t===e)break;let n=O(t);if(n===t)break;t=n}return b(r)}function nt(r){let t;try{t=JSON.parse(Ge(r,"utf8"))}catch(n){throw new Error(`Invalid ToolNet project manifest: ${r}: ${n instanceof Error?n.message:String(n)}`)}if(!t||typeof t!="object")throw new Error(`Invalid ToolNet project manifest: ${r}`);let e=t;if(typeof e.id!="string"||!e.id.trim())throw new Error(`ToolNet project manifest is missing id: ${r}`);if(typeof e.name!="string"||!e.name.trim())throw new Error(`ToolNet project manifest is missing name: ${r}`);let o=new Date().toISOString();return{version:1,id:e.id,name:e.name,remote:typeof e.remote=="string"&&e.remote.trim()?e.remote:e.name,rootPath:typeof e.rootPath=="string"?e.rootPath:O(O(r)),createdAt:typeof e.createdAt=="string"?e.createdAt:o,updatedAt:typeof e.updatedAt=="string"?e.updatedAt:o,graphVersion:typeof e.graphVersion=="number"?e.graphVersion:0,memoryVersion:typeof e.memoryVersion=="number"?e.memoryVersion:0,metadata:e.metadata&&typeof e.metadata=="object"?e.metadata:void 0}}function ne(r,t){let e=E(r,ae);Ye(e,{recursive:!0});let o=F(r),n=`${o}.tmp-${process.pid}`;qe(n,JSON.stringify(t,null,2)+`
102
+ `,{encoding:"utf8",mode:384}),Qe(n,o)}function se(r,t){return{id:r.id,name:r.name,remote:r.remote,rootPath:t,createdAt:r.createdAt,updatedAt:r.updatedAt,graphVersion:r.graphVersion,memoryVersion:r.memoryVersion,metadata:r.metadata}}var R=class{detect(t=process.cwd()){let e=b(t),o=ot(e),s=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>W(E(o,m))),c=rt(e,s?o:void 0);if(c){let m=F(c),a=nt(m);return a.rootPath!==c&&(a.rootPath=c,a.updatedAt=new Date().toISOString(),ne(c,a)),se(a,c)}let l=new Date().toISOString(),i=Ze(o),u={version:1,id:et(o),name:i,remote:i,rootPath:o,createdAt:l,updatedAt:l,graphVersion:0,memoryVersion:0};return ne(o,u),se(u,o)}};import{homedir as Ct}from"node:os";import{join as Tt}from"node:path";import{DeleteObjectCommand as st,GetObjectCommand as it,HeadObjectCommand as at,ListObjectsV2Command as ct,PutObjectCommand as pt,S3Client as ut}from"@aws-sdk/client-s3";import{getSignedUrl as lt}from"@aws-sdk/s3-request-presigner";var I=class{name="huggingface";client;bucket;constructor(t){this.bucket=t.bucket,this.client=new ut({region:"us-east-1",endpoint:`https://s3.hf.co/${t.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}})}async put(t,e,o="application/octet-stream"){let n=typeof e=="string"?Buffer.from(e,"utf8"):e;await this.client.send(new pt({Bucket:this.bucket,Key:t,Body:n,ContentType:o}))}async get(t){let e=await lt(this.client,new it({Bucket:this.bucket,Key:t}),{expiresIn:60}),o=await fetch(e,{redirect:"follow"});if(o.status===404)return null;if(!o.ok)throw new Error(`HF download failed: ${o.status} ${o.statusText}`);return new Uint8Array(await o.arrayBuffer())}async getText(t){let e=await this.get(t);return e?Buffer.from(e).toString("utf8"):null}async exists(t){try{return await this.client.send(new at({Bucket:this.bucket,Key:t})),!0}catch(e){if(typeof e=="object"&&e!==null&&"$metadata"in e&&e.$metadata?.httpStatusCode===404)return!1;throw e}}async delete(t){await this.client.send(new st({Bucket:this.bucket,Key:t}))}async list(t=""){let e=[],o;do{let n=await this.client.send(new ct({Bucket:this.bucket,Prefix:t||void 0,ContinuationToken:o}));for(let s of n.Contents??[])s.Key&&e.push({key:s.Key,size:s.Size,updatedAt:s.LastModified?.toISOString()});o=n.IsTruncated?n.NextContinuationToken:void 0}while(o);return e}};import{access as ce,mkdir as dt,readFile as mt,readdir as ft,rm as gt,stat as pe,writeFile as ht}from"node:fs/promises";import{dirname as yt,join as St,relative as ue,resolve as jt}from"node:path";var P=class{constructor(t){this.root=t}root;name="local";path(t){let e=t.replace(/^\/+/,"");return jt(this.root,e)}async put(t,e){let o=this.path(t);await dt(yt(o),{recursive:!0}),await ht(o,e)}async get(t){try{return await mt(this.path(t))}catch(e){if(typeof e=="object"&&e!==null&&"code"in e&&e.code==="ENOENT")return null;throw e}}async getText(t){let e=await this.get(t);return e?Buffer.from(e).toString("utf8"):null}async exists(t){try{return await ce(this.path(t)),!0}catch{return!1}}async delete(t){await gt(this.path(t),{force:!0})}async list(t=""){let e=this.path(t),o=[];try{await ce(e)}catch{return o}let n=async c=>{let l=await ft(c,{withFileTypes:!0});for(let i of l){let u=St(c,i.name);if(i.isDirectory()){await n(u);continue}let m=await pe(u);o.push({key:ue(this.root,u),size:m.size,updatedAt:m.mtime.toISOString()})}},s=await pe(e);return s.isDirectory()?await n(e):o.push({key:ue(this.root,e),size:s.size,updatedAt:s.mtime.toISOString()}),o}};import{DeleteObjectCommand as kt,GetObjectCommand as vt,HeadObjectCommand as xt,ListObjectsV2Command as wt,PutObjectCommand as bt,S3Client as Pt}from"@aws-sdk/client-s3";import{getSignedUrl as At}from"@aws-sdk/s3-request-presigner";var A=class{name;client;bucket;constructor(t){this.name=t.name??"s3",this.bucket=t.bucket,this.client=new Pt({region:t.region??"us-east-1",endpoint:t.endpoint||void 0,forcePathStyle:t.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}})}async put(t,e,o="application/octet-stream"){let n=typeof e=="string"?Buffer.from(e,"utf8"):e;await this.client.send(new bt({Bucket:this.bucket,Key:t,Body:n,ContentType:o}))}async get(t){let e=await At(this.client,new vt({Bucket:this.bucket,Key:t}),{expiresIn:60}),o=await fetch(e,{redirect:"follow"});if(o.status===404)return null;if(!o.ok)throw new Error(`${this.name} download failed: ${o.status} ${o.statusText}`);return new Uint8Array(await o.arrayBuffer())}async getText(t){let e=await this.get(t);return e?Buffer.from(e).toString("utf8"):null}async exists(t){try{return await this.client.send(new xt({Bucket:this.bucket,Key:t})),!0}catch(e){if(typeof e=="object"&&e!==null&&"$metadata"in e&&e.$metadata?.httpStatusCode===404)return!1;throw e}}async delete(t){await this.client.send(new kt({Bucket:this.bucket,Key:t}))}async list(t=""){let e=[],o;do{let n=await this.client.send(new wt({Bucket:this.bucket,Prefix:t||void 0,ContinuationToken:o}));for(let s of n.Contents??[])s.Key&&e.push({key:s.Key,size:s.Size,updatedAt:s.LastModified?.toISOString()});o=n.IsTruncated?n.NextContinuationToken:void 0}while(o);return e}};function L(r,t){return console.warn(t),new P(r)}function le(r){let t=r.localRoot??Tt(Ct(),".toolnet-memory","storage");if(r.provider==="r2"){let e=r.r2;return e?.accountId&&e.bucket&&e.accessKeyId&&e.secretAccessKey?new A({name:"r2",endpoint:`https://${e.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:e.bucket,forcePathStyle:!0,accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}):L(t,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(r.provider==="s3"){let e=r.s3;return e?.bucket&&e.accessKeyId&&e.secretAccessKey?new A({name:"s3",endpoint:e.endpoint,region:e.region??"us-east-1",bucket:e.bucket,forcePathStyle:e.forcePathStyle??!1,accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}):L(t,"[storage] S3 credentials missing. Using local fallback.")}if(r.provider==="huggingface"){let e=r.huggingface;return e?.namespace&&e.bucket&&e.accessKeyId&&e.secretAccessKey?new I({namespace:e.namespace,bucket:e.bucket,accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}):L(t,"[storage] Hugging Face credentials missing. Using local fallback.")}return new P(t)}function Ot(r){return new Promise(t=>setTimeout(t,r))}async function de(r,t={}){let e=Math.max(1,t.attempts??3),o=t.baseDelayMs??150,n=t.maxDelayMs??2e3,s;for(let c=1;c<=e;c++)try{return await r()}catch(l){if(s=l,c>=e)break;let i=Math.min(n,o*2**(c-1)),u=Math.floor(Math.random()*Math.max(1,i*.2));await Ot(i+u)}throw s}var Rt=new Set(["put","get","getText","delete","list"]);function me(r,t={}){return new Proxy(r,{get(e,o){let n=Reflect.get(e,o,e);return typeof n!="function"?n:Rt.has(o)?(...s)=>de(()=>Promise.resolve(n.apply(e,s)),t):n.bind(e)}})}function fe(r){let t=r.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!t||t==="."||t==="..")throw new Error("Invalid project storage folder");return t}function ge(r){let t=r.replace(/^\/+/,"");if(t.startsWith("memories/"))return"memory/records/"+t.slice(9);if(t.startsWith("vectors/"))return"memory/vectors/"+t.slice(8);if(t.startsWith("graph/"))return"code/graph/"+t.slice(6);let e=t.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(e)return`${e[1]}memory/records/${e[2]}`;if(e=t.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),e)return`${e[1]}memory/vectors/${e[2]}`;if(e=t.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),e)return`${e[1]}code/graph/${e[2]}`;let o=t.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(o)return`${o[1]}memory/records/${o[2]}`;if(o=t.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),o)return`${o[1]}memory/vectors/${o[2]}`;if(o=t.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),o)return`${o[1]}code/graph/${o[2]}`;let n=t.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return n?`${n[1]}memory/records/${n[2]}`:(n=t.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),n?`${n[1]}memory/vectors/${n[2]}`:(n=t.match(/^(projects\/[^/]+\/)graph\/(.+)$/),n?`${n[1]}code/graph/${n[2]}`:t))}var M=class{constructor(t,e,o,n){this.provider=t;this.name=t.name,this.projectId=e,this.projectName=o,this.folder=fe(n??o),this.sourcePrefix=`projects/${e}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let t=`${this.targetPrefix}/project.json`,e=new Date().toISOString(),o=e,n=await this.provider.getText(t);if(n){let c;try{c=JSON.parse(n)}catch(l){throw new Error(`Invalid remote ToolNet project manifest at ${t}: ${l instanceof Error?l.message:String(l)}`)}if(typeof c.id=="string"&&c.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${c.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof c.createdAt=="string"&&(o=c.createdAt)}let s={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:o,updatedAt:e};await this.provider.put(t,JSON.stringify(s,null,2)+`
103
+ `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(t){if(t=ge(t),t===this.sourcePrefix)return this.targetPrefix;if(t.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+t.slice(this.sourcePrefix.length);if(t===this.targetPrefix||t.startsWith(`${this.targetPrefix}/`))return t;if(t.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${t}`].join(" "));return t}async put(t,e,o){return await this.ensureRegistered(),this.provider.put(this.key(t),e,o)}async get(t){return await this.ensureRegistered(),this.provider.get(this.key(t))}async getText(t){return await this.ensureRegistered(),this.provider.getText(this.key(t))}async delete(t){return await this.ensureRegistered(),this.provider.delete(this.key(t))}async exists(t){return await this.ensureRegistered(),this.provider.exists(this.key(t))}async list(t){return await this.ensureRegistered(),this.provider.list(this.key(t))}};function Et(){let r=process.argv.slice(2),t=r[0]||"print",e={mode:"minimal"};for(let o=1;o<r.length;o++)r[o]==="--project"&&r[o+1]?(e.project=r[o+1],o++):r[o]==="--limit"&&r[o+1]?(e.limit=parseInt(r[o+1],10),o++):r[o]==="--focused"&&r[o+1]?(e.mode="focused",e.query=r[o+1],o++):r[o]==="--deep"&&(e.mode="deep");return{command:t,options:e}}async function It(){let{command:r,options:t}=Et();try{switch(r){case"print":await Mt(t);break;case"sync":await Nt(t);break;case"refresh":await $t(t);break;case"profile-show":await _t(t);break;case"profile-sync":await Kt(t);break;default:console.error(`Unknown command: ${r}`),console.error("Available commands: print, sync, refresh, profile-show, profile-sync"),process.exit(1)}}catch(e){console.error("Error:",e instanceof Error?e.message:String(e)),process.exit(1)}}async function Mt(r){let t=r.mode||"minimal";if(t==="minimal"){let e=_({projectPath:r.project});e||(console.error("No ToolNet project found. Run toolnet-memory init first."),process.exit(1)),process.stdout.write(e)}else(t==="focused"||t==="deep")&&(console.error("Focused and deep modes require storage access."),console.error("Use: toolnet-memory brief --deep for deep context"),process.exit(1))}async function Nt(r){let t=_({projectPath:r.project});t||(console.error("No ToolNet project found."),process.exit(1));let e=Y(t);console.log(`Context hash: ${e}`),console.log(`Context size: ${t.length} chars`)}async function $t(r){console.log("Refreshing deep startup brief cache...");let t=r.project||process.cwd(),e=k(t);e||(console.error("No ToolNet project found."),process.exit(1));let o=new R().detect(e),n=oe(),s=me(le({provider:n.storage.provider,huggingface:n.storage.huggingface,localRoot:n.storage.localRoot}),{attempts:2}),c=new M(s,o.id,o.name,o.remote??o.name);await re(o,c),console.log("Deep startup brief cache refreshed.")}async function _t(r){let t=r.project||process.cwd(),e=k(t);e||(console.error("No ToolNet project found."),process.exit(1));let o=he.join(e,".toolnet","profile.md");N.existsSync(o)||(console.error("No profile.md found in .toolnet directory."),process.exit(1));let n=N.readFileSync(o,"utf-8");process.stdout.write(n)}async function Kt(r){let t=H({projectPath:r.project});console.log("Created/updated:");for(let e of t)console.log(`- ${e}`)}It();