toolnet-memory 0.2.21 → 0.3.0

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,35 +1,37 @@
1
- import*as p from"fs";import*as u from"path";function k(e){return e?Math.ceil(e.length/3.5):0}function Y(e,t){if(!e)return"";if(k(e)<=t)return e;let n=Math.floor(t*3.5),o=e.slice(0,n),s=o.lastIndexOf("."),i=o.lastIndexOf(`
2
- `),c=Math.max(s,i);return c>n*.7?o.slice(0,c+1):o}function A(e,t){if(!e)return"";let r=e.split(`
3
- `).filter(s=>s.trim());if(r.length<=t)return e;let n=r.slice(0,t),o=r.length-t;return[...n,`... (${o} more items omitted)`].join(`
4
- `)}function H(e,t){let{maxTokens:r,trimMarker:n="[Context trimmed by ToolNet Memory token budget]"}=t;if(e.length===0)return"";let o=[...e].sort((a,d)=>d.priority-a.priority),s=[],i=0,c=!1;for(let a of o){let d=`# ${a.title}
1
+ import*as f from"fs";import*as d from"path";function C(e){return e?Math.ceil(e.length/3.5):0}function Q(e,n){if(!e)return"";if(C(e)<=n)return e;let r=Math.floor(n*3.5),o=e.slice(0,r),s=o.lastIndexOf("."),i=o.lastIndexOf(`
2
+ `),a=Math.max(s,i);return a>r*.7?o.slice(0,a+1):o}function R(e,n){if(!e)return"";let t=e.split(`
3
+ `).filter(s=>s.trim());if(t.length<=n)return e;let r=t.slice(0,n),o=t.length-n;return[...r,`... (${o} more items omitted)`].join(`
4
+ `)}function Z(e,n){let{maxTokens:t,trimMarker:r="[Context trimmed by ToolNet Memory token budget]"}=n;if(e.length===0)return"";let o=[...e].sort((c,u)=>u.priority-c.priority),s=[],i=0,a=!1;for(let c of o){let u=`# ${c.title}
5
5
 
6
- `,m=k(d),l=k(a.content),h=m+l;if(i+h<=r)s.push(d+a.content),i+=h;else{let S=r-i-m;if(S>50){let g=Y(a.content,S);s.push(d+g),i=r,c=!0}else c=!0;break}}return c&&s.push(`
7
- ${n}
6
+ `,p=C(u),l=C(c.content),h=p+l;if(i+h<=t)s.push(u+c.content),i+=h;else{let g=t-i-p;if(g>50){let b=Q(c.content,g);s.push(u+b),i=t,a=!0}else a=!0;break}}return a&&s.push(`
7
+ ${r}
8
8
  `),s.join(`
9
9
 
10
10
  ---
11
11
 
12
- `)}function C(e,t){let r=[{title:"Profile",content:A(e,10),priority:100},{title:"Current Work",content:A(t,15),priority:90}];return H(r,{maxTokens:800})}import{existsSync as v,mkdirSync as Ce,readFileSync as _,renameSync as Ee,writeFileSync as ve}from"node:fs";import{dirname as je,join as j}from"node:path";import{createHash as V}from"node:crypto";function E(e){return V("sha256").update(e).digest("hex")}function U(e){return j(e.rootPath,".toolnet","context","handoff.md")}function G(e){return j(e.rootPath,".toolnet","context","handoff.json")}function J(e){let t=U(e);if(!v(t))return null;try{let r=_(t,"utf8").trim();if(!r)return null;let n=new Date(0).toISOString(),o=G(e);if(v(o))try{let s=JSON.parse(_(o,"utf8"));typeof s.generatedAt=="string"&&(n=s.generatedAt)}catch{}return{version:1,projectId:e.id,projectName:e.name,text:r,digest:E(r),generatedAt:n}}catch{return null}}function I(e,t=1800){let r=J(e);if(!r?.text)return null;let n=r.text;return n.length>t&&(n=`${n.slice(0,t)}
12
+ `)}function T(e,n){let t=[{title:"Profile",content:R(e,10),priority:100},{title:"Current Work",content:R(n,15),priority:90}];return Z(t,{maxTokens:800})}import{chmodSync as Fe,existsSync as X,mkdirSync as $e,readFileSync as q,renameSync as Le,writeFileSync as ze}from"node:fs";import{dirname as Ve,join as _}from"node:path";var I="toolnet.context-offload.v1";function ee(e){return _(e,".toolnet","offload")}function te(e){return _(ee(e),"graph.json")}function E(){return{schema:I,version:1,updatedAt:new Date(0).toISOString(),nodes:[]}}function ne(e){let n=te(e);if(!X(n))return E();try{let t=JSON.parse(q(n,"utf8"));return t.schema!==I||t.version!==1||!Array.isArray(t.nodes)?E():t}catch{return E()}}function O(e,n){let t=e.replace(/\s+/gu," ").trim();return t.length<=n?t:t.slice(0,n-1).trimEnd()+"\u2026"}function j(e,n={}){let t=ne(e);if(t.nodes.length===0)return"";let r=Math.max(1,Math.min(12,n.maxAssets??6)),o=Math.max(320,Math.min(2400,n.maxChars??900)),s=["[TOOLNET CONTEXT OFFLOAD GRAPH]","Large tool/file payloads stay outside prompt context.","Read only a needed asset with MCP context_offload_read."];for(let i of t.nodes.slice(-r).reverse()){let a=O(i.sourceRefs.at(-1)??"unknown",72),c=i.files.length>0?` files=${O(i.files.join(","),120)}`:"",u=`event:${a} --offloads--> asset:${i.id.slice(0,12)} kind=${i.kind} bytes=${i.bytes}${c}`;if([...s,u].join(`
13
+ `).length>o)break;s.push(u)}return s.join(`
14
+ `)}import{existsSync as P,mkdirSync as Je,readFileSync as D,renameSync as Ue,writeFileSync as We}from"node:fs";import{dirname as Ze,join as F}from"node:path";import{createHash as re}from"node:crypto";function N(e){return re("sha256").update(e).digest("hex")}function oe(e){return F(e.rootPath,".toolnet","context","handoff.md")}function se(e){return F(e.rootPath,".toolnet","context","handoff.json")}function ie(e){let n=oe(e);if(!P(n))return null;try{let t=D(n,"utf8").trim();if(!t)return null;let r=new Date(0).toISOString(),o=se(e);if(P(o))try{let s=JSON.parse(D(o,"utf8"));typeof s.generatedAt=="string"&&(r=s.generatedAt)}catch{}return{version:1,projectId:e.id,projectName:e.name,text:t,digest:N(t),generatedAt:r}}catch{return null}}function $(e,n=1800){let t=ie(e);if(!t?.text)return null;let r=t.text;return r.length>n&&(r=`${r.slice(0,n)}
13
15
 
14
- [Fast handoff truncated]`),["[TOOLNET FAST HANDOFF]","",`Project: ${e.name}`,`Updated: ${r.generatedAt}`,"",n].join(`
15
- `)}var b="memory_agent_ask";function O(){return`
16
+ [Fast handoff truncated]`),["[TOOLNET FAST HANDOFF]","",`Project: ${e.name}`,`Updated: ${t.generatedAt}`,"",r].join(`
17
+ `)}var k="memory_agent_ask";function L(){return`
16
18
  [TOOLNET MEMORY AGENT]
17
19
 
18
20
  Tool:
19
- - ${b}
21
+ - ${k}
20
22
 
21
23
  For resume/continue requests:
22
24
 
23
25
  1. Use the injected ToolNet continuity handoff FIRST.
24
26
  2. If the handoff is missing or ambiguous, invoke
25
- ${b} directly BEFORE repository/history exploration.
27
+ ${k} directly BEFORE repository/history exploration.
26
28
  3. NEVER reconstruct prior work from:
27
29
  - .toolnet/sessions/**
28
30
  - state.json
29
31
  - events.jsonl
30
32
  - raw transcripts
31
33
  4. NEVER search for the implementation/schema of
32
- ${b}; invoke the MCP tool directly.
34
+ ${k}; invoke the MCP tool directly.
33
35
  5. Inspect git/source only AFTER continuity context is known.
34
36
 
35
37
  Use:
@@ -37,28 +39,31 @@ Use:
37
39
  - mode="ai" for ambiguous or combined continuity questions.
38
40
 
39
41
  Current repository evidence overrides stale memory.
40
- `.trim()}function W(e){let t=u.resolve(e),r=u.parse(t).root;for(;t!==r;){let n=u.join(t,".toolnet");if(p.existsSync(n)&&p.statSync(n).isDirectory())return t;t=u.dirname(t)}return null}function N(e){try{return p.existsSync(e)?p.readFileSync(e,"utf-8").trim():null}catch{return null}}function P(e){return e.split(`
41
- `).filter(n=>{let o=n.toUpperCase();return!(o.includes("SECRET")||o.includes("TOKEN")||o.includes("API_KEY")||o.includes("APIKEY")||o.includes("PASSWORD")||o.includes("PASS="))}).join(`
42
- `)}function D(e={}){let t=e.projectPath||process.cwd(),r=W(t);if(!r)return null;let n=u.join(r,".toolnet","project.json"),o="Unknown";try{if(p.existsSync(n)){let g=JSON.parse(p.readFileSync(n,"utf-8"));o=g.name||g.projectName||"Unknown"}}catch{}let s=u.join(r,".toolnet","profile.md"),i=u.join(r,".toolnet","current.md"),c=N(s)||"",a=N(i)||"";c=P(c),a=P(a);let d=`[TOOLNET PROJECT CONTEXT]
42
+ `.trim()}function ae(e){let n=d.resolve(e),t=d.parse(n).root;for(;n!==t;){let r=d.join(n,".toolnet");if(f.existsSync(r)&&f.statSync(r).isDirectory())return n;n=d.dirname(n)}return null}function z(e){try{return f.existsSync(e)?f.readFileSync(e,"utf-8").trim():null}catch{return null}}function G(e){return e.split(`
43
+ `).filter(r=>{let o=r.toUpperCase();return!(o.includes("SECRET")||o.includes("TOKEN")||o.includes("API_KEY")||o.includes("APIKEY")||o.includes("PASSWORD")||o.includes("PASS="))}).join(`
44
+ `)}function V(e={}){let n=e.projectPath||process.cwd(),t=ae(n);if(!t)return null;let r=d.join(t,".toolnet","project.json"),o="Unknown";try{if(f.existsSync(r)){let A=JSON.parse(f.readFileSync(r,"utf-8"));o=A.name||A.projectName||"Unknown"}}catch{}let s=d.join(t,".toolnet","profile.md"),i=d.join(t,".toolnet","current.md"),a=z(s)||"",c=z(i)||"";a=G(a),c=G(c);let u=`[TOOLNET PROJECT CONTEXT]
43
45
 
44
46
  Project: ${o}
45
- Root: ${r}
47
+ Root: ${t}
46
48
 
47
- `,m=C(c,a),l=I({id:"",name:o,rootPath:r},1600),h=l?`
49
+ `,p=T(a,c),l=$({id:"",name:o,rootPath:t},1600),h=l?`
48
50
 
49
51
  ${l}
50
- `:"",S=`
52
+ `:"",g=j(t,{maxAssets:6,maxChars:900}),b=g?`
51
53
 
52
- ${O()}
54
+ ${g}
55
+ `:"",W=`
56
+
57
+ ${L()}
53
58
 
54
59
  Forbidden At Startup:
55
60
  - Do not run session:agy-recover, handoff:latest, or brief automatically.
56
61
  - Do not perform deep recovery merely because an agent starts.
57
62
  - If the user asks to resume previous work and fast context is insufficient,
58
63
  use memory_agent_ask before guessing.
59
- `;return d+m+h+S}import{existsSync as fe}from"node:fs";import{dirname as ge,join as ye,parse as he,resolve as Se}from"node:path";import{existsSync as Q,readFileSync as Z}from"node:fs";import{homedir as X}from"node:os";import{join as q}from"node:path";function ee(e){let t=e.trim();return t.length>=2&&t.startsWith('"')&&t.endsWith('"')?(t=t.slice(1,-1),t.replace(/\\n/g,`
60
- `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):t.length>=2&&t.startsWith("'")&&t.endsWith("'")?t.slice(1,-1):t}function te(){let e=process.env.TOOLNET_GLOBAL_ENV??q(X(),".config","toolnet-memory",".env");if(!Q(e))return;let t=Z(e,"utf8");for(let r of t.split(/\r?\n/)){let n=r.trim();if(!n||n.startsWith("#"))continue;n.startsWith("export ")&&(n=n.slice(7));let o=n.indexOf("=");if(o<=0)continue;let s=n.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(s)&&process.env[s]===void 0&&(process.env[s]=ee(n.slice(o+1)))}}te();import{createHash as re}from"node:crypto";import{existsSync as R,mkdirSync as ne,readFileSync as oe,renameSync as se,writeFileSync as ie}from"node:fs";import{basename as ce,dirname as w,join as M,parse as $,resolve as y}from"node:path";var z=".toolnet",ae="project.json";function ue(e){return re("sha256").update(e).digest("hex").slice(0,16)}function T(e){return M(e,z,ae)}function de(e){return R(T(e))}function le(e,t){let r=y(e),n=$(r).root;for(;;){if(de(r))return r;if(r===n||t&&r===y(t))break;let o=w(r);if(o===r)break;r=o}return null}function pe(e){let t=y(e),r=$(t).root,n=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(n.some(s=>R(M(t,s))))return t;if(t===r)break;let o=w(t);if(o===t)break;t=o}return y(e)}function me(e){let t;try{t=JSON.parse(oe(e,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${e}: ${o instanceof Error?o.message:String(o)}`)}if(!t||typeof t!="object")throw new Error(`Invalid ToolNet project manifest: ${e}`);let r=t;if(typeof r.id!="string"||!r.id.trim())throw new Error(`ToolNet project manifest is missing id: ${e}`);if(typeof r.name!="string"||!r.name.trim())throw new Error(`ToolNet project manifest is missing name: ${e}`);let n=new Date().toISOString();return{version:1,id:r.id,name:r.name,remote:typeof r.remote=="string"&&r.remote.trim()?r.remote:r.name,rootPath:typeof r.rootPath=="string"?r.rootPath:w(w(e)),createdAt:typeof r.createdAt=="string"?r.createdAt:n,updatedAt:typeof r.updatedAt=="string"?r.updatedAt:n,graphVersion:typeof r.graphVersion=="number"?r.graphVersion:0,memoryVersion:typeof r.memoryVersion=="number"?r.memoryVersion:0,metadata:r.metadata&&typeof r.metadata=="object"?r.metadata:void 0}}function F(e,t){let r=M(e,z);ne(r,{recursive:!0});let n=T(e),o=`${n}.tmp-${process.pid}`;ie(o,JSON.stringify(t,null,2)+`
61
- `,{encoding:"utf8",mode:384}),se(o,n)}function L(e,t){return{id:e.id,name:e.name,remote:e.remote,rootPath:t,createdAt:e.createdAt,updatedAt:e.updatedAt,graphVersion:e.graphVersion,memoryVersion:e.memoryVersion,metadata:e.metadata}}var x=class{detect(t=process.cwd()){let r=y(t),n=pe(r),s=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>R(M(n,m))),i=le(r,s?n:void 0);if(i){let m=T(i),l=me(m);return l.rootPath!==i&&(l.rootPath=i,l.updatedAt=new Date().toISOString(),F(i,l)),L(l,i)}let c=new Date().toISOString(),a=ce(n),d={version:1,id:ue(n),name:a,remote:a,rootPath:n,createdAt:c,updatedAt:c,graphVersion:0,memoryVersion:0};return F(n,d),L(d,n)}};function K(e){let t=Se(e),r=he(t).root;for(;;){if(fe(ye(t,".toolnet","project.json")))return new x().detect(t);if(t===r)break;let n=ge(t);if(n===t)break;t=n}return null}var B=3200;async function we(){let e="";for await(let t of process.stdin)e+=t.toString();if(!e.trim())return{};try{return JSON.parse(e)}catch{return{}}}function f(){process.stdout.write("{}")}function xe(e){return e.length<=B?e:`${e.slice(0,B)}
64
+ `;return u+p+h+b+W}import{existsSync as ve}from"node:fs";import{dirname as Me,join as Re,parse as Te,resolve as Oe}from"node:path";import{existsSync as ce,readFileSync as ue}from"node:fs";import{homedir as de}from"node:os";import{join as le}from"node:path";function fe(e){let n=e.trim();return n.length>=2&&n.startsWith('"')&&n.endsWith('"')?(n=n.slice(1,-1),n.replace(/\\n/g,`
65
+ `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):n.length>=2&&n.startsWith("'")&&n.endsWith("'")?n.slice(1,-1):n}function pe(){let e=process.env.TOOLNET_GLOBAL_ENV??le(de(),".config","toolnet-memory",".env");if(!ce(e))return;let n=ue(e,"utf8");for(let t of n.split(/\r?\n/)){let r=t.trim();if(!r||r.startsWith("#"))continue;r.startsWith("export ")&&(r=r.slice(7));let o=r.indexOf("=");if(o<=0)continue;let s=r.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(s)&&process.env[s]===void 0&&(process.env[s]=fe(r.slice(o+1)))}}pe();import{createHash as me}from"node:crypto";import{existsSync as v,mkdirSync as ge,readFileSync as ye,renameSync as he,writeFileSync as Se}from"node:fs";import{basename as xe,dirname as S,join as w,parse as K,resolve as y}from"node:path";var Y=".toolnet",we="project.json";function be(e){return me("sha256").update(e).digest("hex").slice(0,16)}function M(e){return w(e,Y,we)}function Ae(e){return v(M(e))}function Ce(e,n){let t=y(e),r=K(t).root;for(;;){if(Ae(t))return t;if(t===r||n&&t===y(n))break;let o=S(t);if(o===t)break;t=o}return null}function Ee(e){let n=y(e),t=K(n).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(s=>v(w(n,s))))return n;if(n===t)break;let o=S(n);if(o===n)break;n=o}return y(e)}function ke(e){let n;try{n=JSON.parse(ye(e,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${e}: ${o instanceof Error?o.message:String(o)}`)}if(!n||typeof n!="object")throw new Error(`Invalid ToolNet project manifest: ${e}`);let t=n;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${e}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${e}`);let r=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:S(S(e)),createdAt:typeof t.createdAt=="string"?t.createdAt:r,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:r,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 H(e,n){let t=w(e,Y);ge(t,{recursive:!0});let r=M(e),o=`${r}.tmp-${process.pid}`;Se(o,JSON.stringify(n,null,2)+`
66
+ `,{encoding:"utf8",mode:384}),he(o,r)}function B(e,n){return{id:e.id,name:e.name,remote:e.remote,rootPath:n,createdAt:e.createdAt,updatedAt:e.updatedAt,graphVersion:e.graphVersion,memoryVersion:e.memoryVersion,metadata:e.metadata}}var x=class{detect(n=process.cwd()){let t=y(n),r=Ee(t),s=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(p=>v(w(r,p))),i=Ce(t,s?r:void 0);if(i){let p=M(i),l=ke(p);return l.rootPath!==i&&(l.rootPath=i,l.updatedAt=new Date().toISOString(),H(i,l)),B(l,i)}let a=new Date().toISOString(),c=xe(r),u={version:1,id:be(r),name:c,remote:c,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return H(r,u),B(u,r)}};function J(e){let n=Oe(e),t=Te(n).root;for(;;){if(ve(Re(n,".toolnet","project.json")))return new x().detect(n);if(n===t)break;let r=Me(n);if(r===n)break;n=r}return null}var U=3200;async function _e(){let e="";for await(let n of process.stdin)e+=n.toString();if(!e.trim())return{};try{return JSON.parse(e)}catch{return{}}}function m(){process.stdout.write("{}")}function Ie(e){return e.length<=U?e:`${e.slice(0,U)}
62
67
 
63
- [ToolNet startup context truncated]`}function Me(e,t,r){if(process.env.TOOLNET_CODEX_STARTUP_DEBUG!=="1")return;let n=Date.now()-e;process.stderr.write(`[toolnet-memory] codex SessionStart ${n}ms cwd=${t} chars=${r}
64
- `)}async function ke(){let e=Date.now(),t=await we();if(t.hook_event_name!=="SessionStart"){f();return}let r=typeof t.cwd=="string"?t.cwd:"";if(!r){f();return}if(!K(r)){f();return}try{let o=D({projectPath:r});if(!o?.trim()){f();return}let s=xe(o),i={hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:s}};Me(e,r,s.length),process.stdout.write(JSON.stringify(i))}catch{f()}}ke().catch(()=>{f(),process.exitCode=0});
68
+ [ToolNet startup context truncated]`}function je(e,n,t){if(process.env.TOOLNET_CODEX_STARTUP_DEBUG!=="1")return;let r=Date.now()-e;process.stderr.write(`[toolnet-memory] codex SessionStart ${r}ms cwd=${n} chars=${t}
69
+ `)}async function Ne(){let e=Date.now(),n=await _e();if(n.hook_event_name!=="SessionStart"){m();return}let t=typeof n.cwd=="string"?n.cwd:"";if(!t){m();return}if(!J(t)){m();return}try{let o=V({projectPath:t});if(!o?.trim()){m();return}let s=Ie(o),i={hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:s}};je(e,t,s.length),process.stdout.write(JSON.stringify(i))}catch{m()}}Ne().catch(()=>{m(),process.exitCode=0});