toolnet-memory 0.2.7 → 0.2.8
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.
- package/bundle/agy-hook.js +1 -1
- package/package.json +1 -1
package/bundle/agy-hook.js
CHANGED
|
@@ -62,7 +62,7 @@ Things an AI agent should always remember.
|
|
|
62
62
|
`)?e.text:e.text+`
|
|
63
63
|
`,{encoding:"utf8",mode:384}),x(on(t),e)}async function he(t,e,r=900){let n=await Qe({project:t,storage:e,maxTokens:r}),s={version:1,projectId:t.id,projectName:t.name,text:n.text,digest:v(n.text),estimatedTokens:n.estimatedTokens,generatedAt:new Date().toISOString()};return rt(t,s),await e.put(`projects/${t.id}/context/startup.md`,s.text+`
|
|
64
64
|
`,"text/markdown"),await e.put(`projects/${t.id}/context/startup.json`,JSON.stringify(s,null,2)+`
|
|
65
|
-
`,"application/json"),s}async function nt(t,e,r=900){try{let s=await e.getText(`projects/${t.id}/context/startup.md`);if(s&&s.trim()){let o=s.trim(),i={version:1,projectId:t.id,projectName:t.name,text:o,digest:v(o),estimatedTokens:Math.ceil(o.length/3.5),generatedAt:new Date().toISOString()};return rt(t,i),i}}catch{}let n=tt(t);if(en(n))try{let s=rn(n,"utf8").trim();if(s)return{version:1,projectId:t.id,projectName:t.name,text:s,digest:v(s),estimatedTokens:Math.ceil(s.length/3.5),generatedAt:new Date().toISOString()}}catch{}try{return await he(t,e,r)}catch{return null}}import{existsSync as an,mkdirSync as cn,readFileSync as un}from"node:fs";import{homedir as pn}from"node:os";import{dirname as ln,join as st}from"node:path";function dn(t,e,r){let n=r??st(pn(),".config","toolnet-memory","injections","agy");return st(n,`${t.id}-${e}.json`)}function fn(t){if(!an(t))return null;try{return JSON.parse(un(t,"utf8"))}catch{return null}}async function ot(t){let e=t.now??Date.now(),r=t.resumeAfterMs??360*60*1e3,n=dn(t.project,t.conversationId,t.markerDirectory),s=fn(n),o=s?Date.parse(s.injectedAt):0,i=o?e-o:Number.POSITIVE_INFINITY;if(s&&i<6e4)return{};let u=t.invocationNum===0,a=i>=r;if(s&&!u&&!a)return{};let c=await nt(t.project,t.storage,900);return c?.text?(cn(ln(n),{recursive:!0}),x(n,{digest:c.digest,injectedAt:new Date(e).toISOString()}),{injectSteps:[{
|
|
65
|
+
`,"application/json"),s}async function nt(t,e,r=900){try{let s=await e.getText(`projects/${t.id}/context/startup.md`);if(s&&s.trim()){let o=s.trim(),i={version:1,projectId:t.id,projectName:t.name,text:o,digest:v(o),estimatedTokens:Math.ceil(o.length/3.5),generatedAt:new Date().toISOString()};return rt(t,i),i}}catch{}let n=tt(t);if(en(n))try{let s=rn(n,"utf8").trim();if(s)return{version:1,projectId:t.id,projectName:t.name,text:s,digest:v(s),estimatedTokens:Math.ceil(s.length/3.5),generatedAt:new Date().toISOString()}}catch{}try{return await he(t,e,r)}catch{return null}}import{existsSync as an,mkdirSync as cn,readFileSync as un}from"node:fs";import{homedir as pn}from"node:os";import{dirname as ln,join as st}from"node:path";function dn(t,e,r){let n=r??st(pn(),".config","toolnet-memory","injections","agy");return st(n,`${t.id}-${e}.json`)}function fn(t){if(!an(t))return null;try{return JSON.parse(un(t,"utf8"))}catch{return null}}async function ot(t){let e=t.now??Date.now(),r=t.resumeAfterMs??360*60*1e3,n=dn(t.project,t.conversationId,t.markerDirectory),s=fn(n),o=s?Date.parse(s.injectedAt):0,i=o?e-o:Number.POSITIVE_INFINITY;if(s&&i<6e4)return{};let u=t.invocationNum===0,a=i>=r;if(s&&!u&&!a)return{};let c=await nt(t.project,t.storage,900);return c?.text?(cn(ln(n),{recursive:!0}),x(n,{digest:c.digest,injectedAt:new Date(e).toISOString()}),{injectSteps:[{userMessage:c.text}]}):{}}import{existsSync as mn}from"node:fs";import{dirname as gn,join as hn,parse as yn,resolve as Sn}from"node:path";function it(t){for(let e of t){let r=Sn(e),n=yn(r).root;for(;;){if(mn(hn(r,".toolnet","project.json")))return new q().detect(r);if(r===n)break;let s=gn(r);if(s===r)break;r=s}}return null}import{homedir as bt}from"node:os";import{join as ns,resolve as ss}from"node:path";import{join as vn}from"node:path";function at(t,e){let r=t.trim(),n=r.replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,""),s=v(r).slice(0,12);if(!n||n==="."||n==="..")return`${e}--${s}`;let o=n.slice(0,100);return o===r&&r.length<=100?o:`${o.slice(0,85)}--${s}`}function ct(t,e,r){let n=e.trim(),s=r.trim();if(!n)throw new Error("Session agent is required");if(!s)throw new Error("Native session ID is required");let o=at(n.toLowerCase(),"agent"),i=at(s,"session");return{projectId:t.id,projectName:t.name,projectRoot:t.rootPath,agent:n,nativeSessionId:s,sessionKey:`${n}:${s}`,remotePrefix:["projects",t.id,"sessions",o,i].join("/"),localDirectory:vn(t.rootPath,".toolnet","sessions",o,i)}}function ut(t){return String(t).padStart(12,"0")}var oe=class{constructor(e,r=100,n=512*1024){this.storage=e;this.maxEventsPerChunk=r;this.maxChunkBytes=n;if(r<1)throw new Error("maxEventsPerChunk must be positive");if(n<1024)throw new Error("maxChunkBytes is too small")}storage;maxEventsPerChunk;maxChunkBytes;async getJson(e){let r=await this.storage.getText(e);return r?JSON.parse(r):null}async putJson(e,r){await this.storage.put(e,JSON.stringify(r,null,2)+`
|
|
66
66
|
`,"application/json")}async scan(e){let r=`${e.remotePrefix}/events/`,n=await this.storage.list(r),s=[],o=0;for(let i of n){let u=i.key.match(/\/events\/(\d+)-(\d+)-[a-f0-9]+\.jsonl$/);if(!u)continue;let a=Number(u[1]),c=Number(u[2]);!Number.isFinite(a)||!Number.isFinite(c)||(s.push({key:i.key,start:a,end:c}),o=Math.max(o,c))}return s.sort((i,u)=>i.start-u.start),{chunks:s,maxSequence:o}}split(e){let r=[],n=[],s=0;for(let o of e){let i=Buffer.byteLength(JSON.stringify(o)+`
|
|
67
67
|
`,"utf8");n.length>0&&(n.length>=this.maxEventsPerChunk||s+i>this.maxChunkBytes)&&(r.push(n),n=[],s=0),n.push(o),s+=i}return n.length>0&&r.push(n),r}async loadManifest(e){return this.getJson(`${e.remotePrefix}/session.json`)}async loadCursor(e){return this.getJson(`${e.remotePrefix}/cursor.json`)}async recover(e){let r=await this.scan(e);return{maxSequence:r.maxSequence,chunkCount:r.chunks.length}}async append(e,r,n,s={}){let o=await this.loadManifest(e),i=await this.scan(e),u=r.filter(m=>m.sequence>i.maxSequence),a=0;for(let m of this.split(u)){let S=m[0],b=m[m.length-1],N=m.map(T=>JSON.stringify(T)).join(`
|
|
68
68
|
`)+`
|