toolnet-memory 0.2.20 → 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.
- package/.env.example +9 -0
- package/bin/toolnet-memory +76 -0
- package/bundle/agy-hook.js +67 -46
- package/bundle/agy.js +104 -38
- package/bundle/api.js +52 -0
- package/bundle/auto-integrate.js +66 -19
- package/bundle/claude-hook.js +78 -0
- package/bundle/claude.js +3 -0
- package/bundle/codex-context.js +30 -25
- package/bundle/codex.js +80 -61
- package/bundle/context-runtime.js +43 -38
- package/bundle/context.js +15 -14
- package/bundle/continuity-certify.js +49 -0
- package/bundle/doctor.js +4 -4
- package/bundle/full-index.js +18 -8
- package/bundle/graph-index.js +4 -4
- package/bundle/handoff-refresh.js +10 -10
- package/bundle/impact.js +3 -3
- package/bundle/incremental.js +4 -4
- package/bundle/init.js +763 -0
- package/bundle/learner.js +5 -5
- package/bundle/mcp.js +19 -15
- package/bundle/memory-agent.js +10 -11
- package/bundle/memory-query.js +7 -7
- package/bundle/opencode.js +72 -53
- package/bundle/production-certify.js +5 -0
- package/bundle/project-manual.js +6 -6
- package/bundle/recovery-certify.js +55 -0
- package/bundle/runtime.js +11 -11
- package/bundle/semantic.js +6 -6
- package/bundle/setup.js +69 -22
- package/bundle/snapshot.js +4 -4
- package/bundle/update.js +4 -1
- package/bundle/work-continuity.js +6 -6
- package/dist/sdk/public.d.ts +2 -0
- package/dist/sdk/public.js +2 -0
- package/dist/sdk/remote-client.d.ts +41 -0
- package/dist/sdk/remote-client.js +185 -0
- package/dist/sdk/types.d.ts +402 -0
- package/dist/sdk/types.js +1 -0
- package/package.json +14 -5
package/bundle/snapshot.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import"dotenv/config";import{existsSync as B,readFileSync as V}from"node:fs";import{homedir as U}from"node:os";import{join as J}from"node:path";function F(
|
|
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 Y(){let
|
|
3
|
-
`,{encoding:"utf8",mode:384}),
|
|
4
|
-
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=
|
|
1
|
+
import"dotenv/config";import{existsSync as B,readFileSync as V}from"node:fs";import{homedir as U}from"node:os";import{join as J}from"node:path";function F(s){let e=s.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 Y(){let s=process.env.TOOLNET_GLOBAL_ENV??J(U(),".config","toolnet-memory",".env");if(!B(s))return;let e=V(s,"utf8");for(let t of e.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 n=r.slice(0,o).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(n)&&process.env[n]===void 0&&(process.env[n]=F(r.slice(o+1)))}}Y();function m(s,e){return s===void 0?e:["1","true","yes","on"].includes(s.toLowerCase())}function g(s,e){if(!s)return e;let t=Number(s);return Number.isFinite(t)?t:e}function A(){return{memory:{autoCapture:m(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:m(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:m(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:m(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:g(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:g(process.env.MEMORY_RERANK_TOP,10),finalContext:g(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:g(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:m(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:g(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as H}from"node:crypto";import{existsSync as b,mkdirSync as G,readFileSync as W,renameSync as Q,writeFileSync as Z}from"node:fs";import{basename as q,dirname as S,join as w,parse as T,resolve as f}from"node:path";var E=".toolnet",X="project.json";function ee(s){return H("sha256").update(s).digest("hex").slice(0,16)}function k(s){return w(s,E,X)}function te(s){return b(k(s))}function re(s,e){let t=f(s),r=T(t).root;for(;;){if(te(t))return t;if(t===r||e&&t===f(e))break;let o=S(t);if(o===t)break;t=o}return null}function se(s){let e=f(s),t=T(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(n=>b(w(e,n))))return e;if(e===t)break;let o=S(e);if(o===e)break;e=o}return f(s)}function oe(s){let e;try{e=JSON.parse(W(s,"utf8"))}catch(o){throw new Error(`Invalid ToolNet project manifest: ${s}: ${o instanceof Error?o.message:String(o)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${s}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${s}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${s}`);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(s)),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 M(s,e){let t=w(s,E);G(t,{recursive:!0});let r=k(s),o=`${r}.tmp-${process.pid}`;Z(o,JSON.stringify(e,null,2)+`
|
|
3
|
+
`,{encoding:"utf8",mode:384}),Q(o,r)}function C(s,e){return{id:s.id,name:s.name,remote:s.remote,rootPath:e,createdAt:s.createdAt,updatedAt:s.updatedAt,graphVersion:s.graphVersion,memoryVersion:s.memoryVersion,metadata:s.metadata}}var v=class{detect(e=process.cwd()){let t=f(e),r=se(t),n=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(u=>b(w(r,u))),i=re(t,n?r:void 0);if(i){let u=k(i),d=oe(u);return d.rootPath!==i&&(d.rootPath=i,d.updatedAt=new Date().toISOString(),M(i,d)),C(d,i)}let a=new Date().toISOString(),c=q(r),p={version:1,id:ee(r),name:c,remote:c,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return M(r,p),C(p,r)}};import{homedir as Ae}from"node:os";import{join as Me}from"node:path";import{DeleteObjectCommand as ne,GetObjectCommand as ie,HeadObjectCommand as ae,ListObjectsV2Command as ce,PutObjectCommand as pe,S3Client as ue}from"@aws-sdk/client-s3";import{getSignedUrl as de}from"@aws-sdk/s3-request-presigner";var j=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new ue({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,r="application/octet-stream"){let o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new pe({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await de(this.client,new ie({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.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 ne({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new ce({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};import{access as I,mkdir as le,readFile as me,readdir as ge,rm as fe,stat as O,writeFile as ye}from"node:fs/promises";import{dirname as he,join as Se,relative as _,resolve as ve}from"node:path";var y=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return ve(this.root,t)}async put(e,t){let r=this.path(e);await le(he(r),{recursive:!0}),await ye(r,t)}async get(e){try{return await me(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 I(this.path(e)),!0}catch{return!1}}async delete(e){await fe(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await I(t)}catch{return r}let o=async i=>{let a=await ge(i,{withFileTypes:!0});for(let c of a){let p=Se(i,c.name);if(c.isDirectory()){await o(p);continue}let u=await O(p);r.push({key:_(this.root,p),size:u.size,updatedAt:u.mtime.toISOString()})}},n=await O(t);return n.isDirectory()?await o(t):r.push({key:_(this.root,t),size:n.size,updatedAt:n.mtime.toISOString()}),r}};import{DeleteObjectCommand as we,GetObjectCommand as je,HeadObjectCommand as xe,ListObjectsV2Command as be,PutObjectCommand as ke,S3Client as Pe}from"@aws-sdk/client-s3";import{getSignedUrl as Re}from"@aws-sdk/s3-request-presigner";var h=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new Pe({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,r="application/octet-stream"){let o=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new ke({Bucket:this.bucket,Key:e,Body:o,ContentType:r}))}async get(e){let t=await Re(this.client,new je({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.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 xe({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 we({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let o=await this.client.send(new be({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let n of o.Contents??[])n.Key&&t.push({key:n.Key,size:n.Size,updatedAt:n.LastModified?.toISOString()});r=o.IsTruncated?o.NextContinuationToken:void 0}while(r);return t}};function P(s,e){return console.warn(e),new y(s)}function $(s){let e=s.localRoot??Me(Ae(),".toolnet-memory","storage");if(s.provider==="r2"){let t=s.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new h({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):P(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(s.provider==="s3"){let t=s.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new h({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):P(e,"[storage] S3 credentials missing. Using local fallback.")}if(s.provider==="huggingface"){let t=s.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new j({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):P(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new y(e)}function Ce(s){return new Promise(e=>setTimeout(e,s))}async function K(s,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,o=e.maxDelayMs??2e3,n;for(let i=1;i<=t;i++)try{return await s()}catch(a){if(n=a,i>=t)break;let c=Math.min(o,r*2**(i-1)),p=Math.floor(Math.random()*Math.max(1,c*.2));await Ce(c+p)}throw n}var Te=new Set(["put","get","getText","delete","list"]);function N(s,e={}){return new Proxy(s,{get(t,r){let o=Reflect.get(t,r,t);return typeof o!="function"?o:Te.has(r)?(...n)=>K(()=>Promise.resolve(o.apply(t,n)),e):o.bind(t)}})}function D(s){let e=s.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 L(s){let e=s.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 r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[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 x=class{constructor(e,t,r,o){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=D(o??r),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(),r=t,o=await this.provider.getText(e);if(o){let i;try{i=JSON.parse(o)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof i.id=="string"&&i.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${i.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof i.createdAt=="string"&&(r=i.createdAt)}let n={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,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=L(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,r){return await this.ensureRegistered(),this.provider.put(this.key(e),t,r)}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))}};var Ee=["memories/current.json","vectors/current.json","graph/current.json","graph/manifest.json","graph/resolution/current.json","code/chunks/current.json","code/vectors/current.json","code/architecture/current.json","code/architecture/entry-points.json","code/architecture/hotspots.json","code/architecture/layers.json","code/architecture/clusters.json","code/analysis/current.json","code/analysis/dead-code.json","code/analysis/dependencies.json","code/visualization/graph.json"],l=class{constructor(e){this.storage=e}storage;projectPrefix(e){return`projects/${e}`}snapshotPrefix(e,t){return[this.projectPrefix(e),"snapshots",t].join("/")}async create(e,t="automatic",r){let o=new Date().toISOString(),n=o.replace(/[:.]/g,"-"),i=this.projectPrefix(e),a=this.snapshotPrefix(e,n),c=[];for(let u of Ee){let d=`${i}/${u}`,R=await this.storage.get(d);R&&(await this.storage.put(`${a}/${u}`,R,"application/json"),c.push(u))}if(c.length===0)return null;let p={version:1,id:n,projectId:e,createdAt:o,reason:t,objects:c,metadata:r};return await this.storage.put(`${a}/snapshot.json`,JSON.stringify(p,null,2),"application/json"),p}async list(e){let t=[this.projectPrefix(e),"snapshots"].join("/"),o=(await this.storage.list(t)).filter(i=>i.key.endsWith("/snapshot.json")),n=[];for(let i of o){let a=await this.storage.getText(i.key);if(a)try{n.push(JSON.parse(a))}catch{}}return n.sort((i,a)=>a.createdAt.localeCompare(i.createdAt))}async restore(e,t){let r=this.snapshotPrefix(e,t),o=await this.storage.getText(`${r}/snapshot.json`);if(!o)throw new Error(`Snapshot not found: ${t}`);let n=JSON.parse(o);if(n.projectId!==e)throw new Error("Snapshot project mismatch");let i=this.projectPrefix(e),a=[];for(let c of n.objects){let p=await this.storage.get(`${r}/${c}`);p&&(await this.storage.put(`${i}/${c}`,p,"application/json"),a.push(c))}return{restored:a}}async remove(e,t){let r=this.snapshotPrefix(e,t),o=await this.storage.list(r);for(let n of o)await this.storage.delete(n.key);return o.length}async prune(e,t=10){let o=(await this.list(e)).slice(t),n=0;for(let i of o)await this.remove(e,i.id),n++;return n}};async function z(s,e){let t=new l(s),o=(await t.list(e))[0];if(!o)throw new Error("No snapshot available");let n=await t.restore(e,o.id);return{snapshot:o.id,createdAt:o.createdAt,...n}}async function Ie(){let s=process.argv[2],e=A(),t=new v().detect(),r=N($({provider:e.storage.provider,huggingface:e.storage.huggingface,localRoot:e.storage.localRoot}),{attempts:3}),o=new x(r,t.id,t.name,t.remote??t.name),n=new l(o);if(s==="list"){let i=await n.list(t.id);console.log(i.map(a=>({id:a.id,createdAt:a.createdAt,reason:a.reason})));return}if(s==="create"){let i=process.argv.slice(3).join(" ")||"manual-cli";console.log(await n.create(t.id,i));return}if(s==="restore"){let i=process.argv[3];if(!i)throw new Error("snapshot id required");await n.create(t.id,"before-cli-restore"),console.log(await n.restore(t.id,i));return}if(s==="recover-latest"){console.log(await z(o,t.id));return}throw new Error("Usage: snapshot-cli list|create|restore|recover-latest")}Ie().catch(s=>{console.error(s instanceof Error?s.message:s),process.exit(1)});
|
package/bundle/update.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import p from"node:fs";import c from"node:path";import{fileURLToPath as v}from"node:url";import{spawn as $}from"node:child_process";var m=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"],i={clear:"\r\x1B[2K",cyan:"\x1B[36m",green:"\x1B[32m",red:"\x1B[31m",yellow:"\x1B[33m",dim:"\x1B[2m",reset:"\x1B[0m"};function f(r){let e=Date.now()-r;return e<1e3?`${e}ms`:e<1e4?`${(e/1e3).toFixed(1)}s`:`${Math.round(e/1e3)}s`}var d=class{stream;enabled;interactive;color;intervalMs;label;frame=0;startedAt=0;timer;active=!1;constructor(e,s={}){this.label=e,this.stream=s.stream??process.stderr,this.enabled=s.enabled??!0,this.interactive=s.interactive??this.stream.isTTY===!0,this.color=s.color??(this.interactive&&process.env.NO_COLOR===void 0),this.intervalMs=Math.max(40,s.intervalMs??80)}start(){return!this.enabled||this.active?this:(this.active=!0,this.startedAt=Date.now(),this.interactive?(this.render(),this.timer=setInterval(()=>{this.frame=(this.frame+1)%m.length,this.render()},this.intervalMs),this.timer.unref?.(),this):(this.stream.write(`\u2192 ${this.label}
|
|
2
|
+
`),this))}update(e){return this.label=e,this.enabled&&this.active&&this.interactive&&this.render(),this}succeed(e){this.finish("\u2713",e??this.label,i.green)}fail(e){this.finish("\u2717",e??this.label,i.red)}warn(e){this.finish("!",e??this.label,i.yellow)}stop(){this.active&&(this.timer&&(clearInterval(this.timer),this.timer=void 0),this.enabled&&this.interactive&&this.stream.write(i.clear),this.active=!1)}render(){if(!this.enabled||!this.active||!this.interactive)return;let e=m[this.frame],s=this.color?`${i.cyan}${e}${i.reset}`:e,o=f(this.startedAt),t=this.color?`${i.dim}${o}${i.reset}`:o;this.stream.write(`${i.clear}${s} ${this.label} ${t}`)}finish(e,s,o){if(!this.enabled){this.active=!1;return}this.startedAt||(this.startedAt=Date.now()),this.timer&&(clearInterval(this.timer),this.timer=void 0);let t=f(this.startedAt),a=this.color?`${o}${e}${i.reset}`:e,l=this.color?`${i.dim}${t}${i.reset}`:t;this.interactive?this.stream.write(`${i.clear}${a} ${s} ${l}
|
|
3
|
+
`):this.stream.write(`${a} ${s} (${t})
|
|
4
|
+
`),this.active=!1}};var h="toolnet-memory";function g(r,e){return new Promise((s,o)=>{let t=$(r,e,{stdio:["ignore","pipe","pipe"],env:process.env}),a="",l="";t.stdout?.setEncoding("utf8"),t.stderr?.setEncoding("utf8"),t.stdout?.on("data",n=>{a+=n}),t.stderr?.on("data",n=>{l+=n}),t.once("error",o),t.once("close",n=>{s({status:n??1,stdout:a,stderr:l})})})}function b(){let r=c.dirname(v(import.meta.url));return c.resolve(r,"..")}function w(r){try{let e=JSON.parse(p.readFileSync(c.join(r,"package.json"),"utf8"));return String(e.version??"unknown")}catch{return"unknown"}}function y(r){let e=`${c.sep}lib${c.sep}node_modules${c.sep}${h}`;return r.endsWith(e)?r.slice(0,-e.length)||c.parse(r).root:null}async function x(){let r=b(),e=w(r);console.log(""),console.log("ToolNet Memory Update"),console.log("====================="),console.log("");let s=new d("Checking npm registry").start(),o;try{o=await g("npm",["view",`${h}@latest`,"version","--silent"])}catch(u){throw s.fail("Unable to reach npm registry"),u}if(o.status!==0){s.fail("Unable to check latest version"),o.stderr.trim()&&console.error(o.stderr.trim()),process.exitCode=1;return}let t=o.stdout.trim();if(s.succeed(`Latest version: v${t}`),console.log(""),console.log(`Current: v${e}`),console.log(`Latest : v${t}`),console.log(""),e===t){console.log("\u2713 ToolNet Memory is already up to date"),console.log("");return}let a=y(r);if(!a){console.log("This appears to be a development checkout."),console.log(""),console.log("Self-update is disabled for source checkouts."),console.log(""),console.log("For the published CLI, install/update with:"),console.log(" npm install -g toolnet-memory@latest"),console.log(""),process.exitCode=1;return}let l=new d(`Installing ToolNet Memory v${t}`).start(),n;try{n=await g("npm",["install","-g","--prefix",a,`${h}@${t}`,"--no-fund","--no-audit","--loglevel=error"])}catch(u){throw l.fail("ToolNet Memory update failed"),u}if(n.status!==0){l.fail("ToolNet Memory update failed"),n.stderr.trim()&&(console.error(""),console.error(n.stderr.trim())),console.error(""),console.error("Retry with:"),console.error(` npm install -g ${h}@${t}`),console.error(""),process.exitCode=n.status||1;return}l.succeed(`Installed ToolNet Memory v${t}`),console.log(""),console.log(`\u2713 ToolNet Memory updated to v${t}`),console.log("")}x().catch(r=>{console.error(""),console.error(r instanceof Error?r.message:String(r)),console.error(""),process.exitCode=1});
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import{existsSync as ae,readFileSync as ce}from"node:fs";import{homedir as
|
|
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 le(){let r=process.env.TOOLNET_GLOBAL_ENV??ue(
|
|
3
|
-
`,{encoding:"utf8",mode:384}),ye(s,o)}function V(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
|
|
4
|
-
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=X(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,o){return await this.ensureRegistered(),this.provider.put(this.key(e),t,o)}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{join as te}from"node:path";import{mkdirSync as rt}from"node:fs";import{createHash as
|
|
5
|
-
`,{encoding:"utf8",mode:384}),et(t,r)}function ot(r){return r.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/g," ").trim()}function y(r,e=20){let t=[],o=new Set;for(let s of r.slice().reverse()){let i=ot(s);if(!(!i||o.has(i))&&(o.add(i),t.push(s),t.length>=e))break}return t.reverse()}function st(r){return r.kind==="phase"?/^Phase\s+\d+$/iu.test(r.text):r.kind==="task"?/^(?:TODO|Task|Việc)\s+\d+$/iu.test(r.text):!1}function re(r,e){let t=e.status??r?.status??"pending",o=t;r&&(r.status==="completed"&&t!=="completed"?o="completed":t==="pending"&&(r.status==="in_progress"||r.status==="blocked")&&(o=r.status));let s=r&&st(e)?r.title:e.text;return{id:r?.id??q(e.key).slice(0,24),title:s,status:o,order:e.order??r?.order,confidence:Math.max(e.confidence,r?.confidence??0),updatedAt:e.occurredAt,updatedBy:{agent:e.agent,nativeSessionId:e.nativeSessionId,eventId:e.eventId}}}async function nt(r,e){let t=`projects/${r.id}/work/observations/`,o=await e.list(t),s=[];for(let i of o.filter(a=>a.key.endsWith(".json")).sort((a,c)=>a.key.localeCompare(c.key))){let a=await e.getText(i.key);if(a)try{let c=JSON.parse(a);c.version===1&&Array.isArray(c.observations)&&s.push(c)}catch{}}return s}async function oe(r,e){let o=(await nt(r,e)).flatMap(n=>n.observations).sort((n,f)=>{let D=n.occurredAt.localeCompare(f.occurredAt);if(D!==0)return D;let
|
|
6
|
-
`,"application/json"),
|
|
1
|
+
import{existsSync as ae,readFileSync as ce}from"node:fs";import{homedir as ue}from"node:os";import{join as pe}from"node:path";function de(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 le(){let r=process.env.TOOLNET_GLOBAL_ENV??pe(ue(),".config","toolnet-memory",".env");if(!ae(r))return;let e=ce(r,"utf8");for(let t of e.split(/\r?\n/)){let o=t.trim();if(!o||o.startsWith("#"))continue;o.startsWith("export ")&&(o=o.slice(7));let s=o.indexOf("=");if(s<=0)continue;let i=o.slice(0,s).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=de(o.slice(s+1)))}}le();function h(r,e){return r===void 0?e:["1","true","yes","on"].includes(r.toLowerCase())}function S(r,e){if(!r)return e;let t=Number(r);return Number.isFinite(t)?t:e}function z(){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:S(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:S(process.env.MEMORY_RERANK_TOP,10),finalContext:S(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:S(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:S(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as me}from"node:crypto";import{existsSync as I,mkdirSync as ge,readFileSync as fe,renameSync as ye,writeFileSync as he}from"node:fs";import{basename as Se,dirname as j,join as A,parse as W,resolve as k}from"node:path";var F=".toolnet",ke="project.json";function we(r){return me("sha256").update(r).digest("hex").slice(0,16)}function T(r){return A(r,F,ke)}function ve(r){return I(T(r))}function be(r,e){let t=k(r),o=W(t).root;for(;;){if(ve(t))return t;if(t===o||e&&t===k(e))break;let s=j(t);if(s===t)break;t=s}return null}function je(r){let e=k(r),t=W(e).root,o=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(o.some(i=>I(A(e,i))))return e;if(e===t)break;let s=j(e);if(s===e)break;e=s}return k(r)}function xe(r){let e;try{e=JSON.parse(fe(r,"utf8"))}catch(s){throw new Error(`Invalid ToolNet project manifest: ${r}: ${s instanceof Error?s.message:String(s)}`)}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 o=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:j(j(r)),createdAt:typeof t.createdAt=="string"?t.createdAt:o,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:o,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 B(r,e){let t=A(r,F);ge(t,{recursive:!0});let o=T(r),s=`${o}.tmp-${process.pid}`;he(s,JSON.stringify(e,null,2)+`
|
|
3
|
+
`,{encoding:"utf8",mode:384}),ye(s,o)}function V(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=k(e),o=je(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(d=>I(A(o,d))),a=be(t,i?o:void 0);if(a){let d=T(a),m=xe(d);return m.rootPath!==a&&(m.rootPath=a,m.updatedAt=new Date().toISOString(),B(a,m)),V(m,a)}let c=new Date().toISOString(),u=Se(o),p={version:1,id:we(o),name:u,remote:u,rootPath:o,createdAt:c,updatedAt:c,graphVersion:0,memoryVersion:0};return B(o,p),V(p,o)}};import{homedir as He}from"node:os";import{join as Ye}from"node:path";import{DeleteObjectCommand as Ae,GetObjectCommand as Pe,HeadObjectCommand as Re,ListObjectsV2Command as Me,PutObjectCommand as Ce,S3Client as Ie}from"@aws-sdk/client-s3";import{getSignedUrl as Te}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,o="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Ce({Bucket:this.bucket,Key:e,Body:s,ContentType:o}))}async get(e){let t=await Te(this.client,new Pe({Bucket:this.bucket,Key:e}),{expiresIn:60}),o=await fetch(t,{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(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 Re({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 Ae({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],o;do{let s=await this.client.send(new Me({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:o}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});o=s.IsTruncated?s.NextContinuationToken:void 0}while(o);return t}};import{access as J,mkdir as Ee,readFile as Oe,readdir as _e,rm as Ne,stat as U,writeFile as $e}from"node:fs/promises";import{dirname as Ke,join as De,relative as H,resolve as Le}from"node:path";var w=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return Le(this.root,t)}async put(e,t){let o=this.path(e);await Ee(Ke(o),{recursive:!0}),await $e(o,t)}async get(e){try{return await Oe(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 J(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),o=[];try{await J(t)}catch{return o}let s=async a=>{let c=await _e(a,{withFileTypes:!0});for(let u of c){let p=De(a,u.name);if(u.isDirectory()){await s(p);continue}let d=await U(p);o.push({key:H(this.root,p),size:d.size,updatedAt:d.mtime.toISOString()})}},i=await U(t);return i.isDirectory()?await s(t):o.push({key:H(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),o}};import{DeleteObjectCommand as ze,GetObjectCommand as Be,HeadObjectCommand as Ve,ListObjectsV2Command as We,PutObjectCommand as Fe,S3Client as Je}from"@aws-sdk/client-s3";import{getSignedUrl as Ue}from"@aws-sdk/s3-request-presigner";var v=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,o="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new Fe({Bucket:this.bucket,Key:e,Body:s,ContentType:o}))}async get(e){let t=await Ue(this.client,new Be({Bucket:this.bucket,Key:e}),{expiresIn:60}),o=await fetch(t,{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(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 Ve({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 ze({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],o;do{let s=await this.client.send(new We({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:o}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});o=s.IsTruncated?s.NextContinuationToken:void 0}while(o);return t}};function E(r,e){return console.warn(e),new w(r)}function Y(r){let e=r.localRoot??Ye(He(),".toolnet-memory","storage");if(r.provider==="r2"){let t=r.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new v({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):E(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 v({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):E(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}):E(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new w(e)}function Ge(r){return new Promise(e=>setTimeout(e,r))}async function G(r,e={}){let t=Math.max(1,e.attempts??3),o=e.baseDelayMs??150,s=e.maxDelayMs??2e3,i;for(let a=1;a<=t;a++)try{return await r()}catch(c){if(i=c,a>=t)break;let u=Math.min(s,o*2**(a-1)),p=Math.floor(Math.random()*Math.max(1,u*.2));await Ge(u+p)}throw i}var Qe=new Set(["put","get","getText","delete","list"]);function Q(r,e={}){return new Proxy(r,{get(t,o){let s=Reflect.get(t,o,t);return typeof s!="function"?s:Qe.has(o)?(...i)=>G(()=>Promise.resolve(s.apply(t,i)),e):s.bind(t)}})}function Z(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 X(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 o=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(o)return`${o[1]}memory/records/${o[2]}`;if(o=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),o)return`${o[1]}memory/vectors/${o[2]}`;if(o=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),o)return`${o[1]}code/graph/${o[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var R=class{constructor(e,t,o,s){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=o,this.folder=Z(s??o),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(),o=t,s=await this.provider.getText(e);if(s){let a;try{a=JSON.parse(s)}catch(c){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${c instanceof Error?c.message:String(c)}`)}if(typeof a.id=="string"&&a.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${a.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof a.createdAt=="string"&&(o=a.createdAt)}let i={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:o,updatedAt:t};await this.provider.put(e,JSON.stringify(i,null,2)+`
|
|
4
|
+
`,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=X(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,o){return await this.ensureRegistered(),this.provider.put(this.key(e),t,o)}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{join as te}from"node:path";import{mkdirSync as rt}from"node:fs";import{createHash as Ze}from"node:crypto";import{dirname as Xe}from"node:path";import{mkdirSync as qe,readFileSync as ao,renameSync as et,writeFileSync as tt}from"node:fs";function q(r){return Ze("sha256").update(r).digest("hex")}function ee(r,e){qe(Xe(r),{recursive:!0});let t=`${r}.${process.pid}.tmp`;tt(t,JSON.stringify(e,null,2)+`
|
|
5
|
+
`,{encoding:"utf8",mode:384}),et(t,r)}function ot(r){return r.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu," ").replace(/\s+/g," ").trim()}function y(r,e=20){let t=[],o=new Set;for(let s of r.slice().reverse()){let i=ot(s);if(!(!i||o.has(i))&&(o.add(i),t.push(s),t.length>=e))break}return t.reverse()}function st(r){return r.kind==="phase"?/^Phase\s+\d+$/iu.test(r.text):r.kind==="task"?/^(?:TODO|Task|Việc)\s+\d+$/iu.test(r.text):!1}function re(r,e){let t=e.status??r?.status??"pending",o=t;r&&(r.status==="completed"&&t!=="completed"?o="completed":t==="pending"&&(r.status==="in_progress"||r.status==="blocked")&&(o=r.status));let s=r&&st(e)?r.title:e.text;return{id:r?.id??q(e.key).slice(0,24),title:s,status:o,order:e.order??r?.order,confidence:Math.max(e.confidence,r?.confidence??0),updatedAt:e.occurredAt,updatedBy:{agent:e.agent,nativeSessionId:e.nativeSessionId,eventId:e.eventId}}}async function nt(r,e){let t=`projects/${r.id}/work/observations/`,o=await e.list(t),s=[];for(let i of o.filter(a=>a.key.endsWith(".json")).sort((a,c)=>a.key.localeCompare(c.key))){let a=await e.getText(i.key);if(a)try{let c=JSON.parse(a);c.version===1&&Array.isArray(c.observations)&&s.push(c)}catch{}}return s}async function oe(r,e){let o=(await nt(r,e)).flatMap(n=>n.observations).sort((n,f)=>{let D=n.occurredAt.localeCompare(f.occurredAt);if(D!==0)return D;let L=n.sequence-f.sequence;return L!==0?L:n.id.localeCompare(f.id)}),s=new Map,i=new Map,a,c,u,p=[],d=[],m=[],_=[],N=[],$=[];for(let n of o)switch(n.kind){case"goal":a=n.text;break;case"plan":c=n.text;break;case"phase":s.set(n.key,re(s.get(n.key),n));break;case"task":i.set(n.key,re(i.get(n.key),n));break;case"decision":p.push(n.text);break;case"blocker":d.push(n.text);break;case"warning":m.push(n.text);break;case"next_action":_.push(n.text);break;case"file":N.push(n.text);break;case"test":$.push(n.text);break;case"session":u={agent:n.agent,nativeSessionId:n.nativeSessionId,sessionKey:n.sessionKey,updatedAt:n.occurredAt};break}let g=Array.from(s.values()).sort((n,f)=>(n.order??Number.MAX_SAFE_INTEGER)-(f.order??Number.MAX_SAFE_INTEGER)),l=Array.from(i.values()).sort((n,f)=>(n.order??Number.MAX_SAFE_INTEGER)-(f.order??Number.MAX_SAFE_INTEGER)),M=g.find(n=>n.status==="in_progress")??g.find(n=>n.status==="blocked")??g.find(n=>n.status==="pending"),b=l.find(n=>n.status==="in_progress")??l.find(n=>n.status==="blocked")??l.find(n=>n.status==="pending"),ne=y([..._,...b?[b.title]:[],...!b&&M?[M.title]:[],...l.filter(n=>n.status==="pending").slice(0,5).map(n=>n.title)],8),ie=y([...d,...g.filter(n=>n.status==="blocked").map(n=>n.title),...l.filter(n=>n.status==="blocked").map(n=>n.title)],20),C={version:1,projectId:r.id,projectName:r.name,goal:a,plan:c,phases:g,tasks:l,decisions:y(p,20),blockers:ie,warnings:y(m,20),nextActions:ne,filesTouched:y(N,30),tests:y($,20),currentPhase:M,currentTask:b,progress:{phasesTotal:g.length,phasesCompleted:g.filter(n=>n.status==="completed").length,tasksTotal:l.length,tasksCompleted:l.filter(n=>n.status==="completed").length,blocked:g.filter(n=>n.status==="blocked").length+l.filter(n=>n.status==="blocked").length},lastSession:u,updatedAt:o.length?o[o.length-1].occurredAt:new Date().toISOString()},K=te(r.rootPath,".toolnet","work");return rt(K,{recursive:!0}),ee(te(K,"current.json"),C),await e.put(`projects/${r.id}/work/current.json`,JSON.stringify(C,null,2)+`
|
|
6
|
+
`,"application/json"),C}async function O(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}}function it(r,e){let t=r.indexOf(e);return t>=0?r[t+1]:void 0}function at(r){let e=z(),t=Q(Y({provider:e.storage.provider,huggingface:e.storage.huggingface,localRoot:e.storage.localRoot}),{attempts:3});return new R(t,r.id,r.name,r.remote??r.name)}function se(r){console.log(),console.log(`Project: ${r.projectName}`),r.goal&&console.log(`Goal: ${r.goal}`),r.currentPhase&&console.log(`Current phase: ${r.currentPhase.title} [${r.currentPhase.status}]`),r.currentTask&&console.log(`Current task: ${r.currentTask.title} [${r.currentTask.status}]`),console.log(`Phases: ${r.progress.phasesCompleted}/${r.progress.phasesTotal}`),console.log(`Tasks: ${r.progress.tasksCompleted}/${r.progress.tasksTotal}`),r.nextActions.length&&(console.log(),console.log("Next actions:"),r.nextActions.forEach((e,t)=>console.log(`${t+1}. ${e}`))),r.blockers.length&&(console.log(),console.log("Blockers:"),r.blockers.forEach(e=>console.log(`- ${e}`))),r.lastSession&&(console.log(),console.log(`Last session: ${r.lastSession.agent} / ${r.lastSession.nativeSessionId}`))}async function ct(){let[r="help",...e]=process.argv.slice(2),t=new x().detect(it(e,"--project")??process.cwd()),o=at(t);if(r==="reconcile"){let s=await oe(t,o);se(s);return}if(r==="status"){let s=await O(t,o);if(!s){console.log("No active work state yet.");return}se(s);return}if(r==="json"){let s=await O(t,o);console.log(JSON.stringify(s,null,2));return}console.log(`Work Continuity
|
|
7
7
|
|
|
8
8
|
Commands:
|
|
9
9
|
status [--project PATH]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ToolNetApiContextOffloadReadInput, ToolNetApiContextOffloadRead, ToolNetApiClientOptions, ToolNetApiHealth, ToolNetApiMemoryAsk, ToolNetApiMemoryAskInput, ToolNetApiMemorySearch, ToolNetApiMemorySearchInput, ToolNetApiProject, ToolNetApiSkillSearch, ToolNetApiSkillSearchInput, ToolNetApiCreateHubAgentInput, ToolNetApiCreateHubTeamInput, ToolNetApiGrantHubAclInput, ToolNetApiHubAcl, ToolNetApiHubAclGrant, ToolNetApiHubAclRevoke, ToolNetApiHubAgent, ToolNetApiHubAgents, ToolNetApiHubLoadout, ToolNetApiHubLoadouts, ToolNetApiHubObservability, ToolNetApiHubSummary, ToolNetApiHubTeam, ToolNetApiHubTeams, ToolNetApiSetHubLoadoutInput, ToolNetApiCreateWikiPageInput, ToolNetApiUpdateWikiPageInput, ToolNetApiWikiBacklinks, ToolNetApiWikiHistory, ToolNetApiWikiPage, ToolNetApiWikiPages, ToolNetApiWikiSearch, ToolNetApiWikiSummary } from './types.js';
|
|
2
|
+
import type { ToolNetApiGovernancePolicy, ToolNetApiGovernanceReview, ToolNetApiGovernanceReviews, ToolNetApiGovernanceSummary, ToolNetApiKnowledgeQuality, ToolNetGovernanceDecisionInput, ToolNetKnowledgeGovernancePolicy, ToolNetKnowledgeGovernanceReviewStatus } from './types.js';
|
|
3
|
+
export declare class ToolNetApiClient {
|
|
4
|
+
private readonly baseUrl;
|
|
5
|
+
private readonly token?;
|
|
6
|
+
private readonly principal?;
|
|
7
|
+
private readonly timeoutMs;
|
|
8
|
+
constructor(options: ToolNetApiClientOptions);
|
|
9
|
+
health(): Promise<ToolNetApiHealth>;
|
|
10
|
+
project(): Promise<ToolNetApiProject>;
|
|
11
|
+
memoryAsk(input: ToolNetApiMemoryAskInput): Promise<ToolNetApiMemoryAsk>;
|
|
12
|
+
memorySearch(input: ToolNetApiMemorySearchInput): Promise<ToolNetApiMemorySearch>;
|
|
13
|
+
skillSearch(input: ToolNetApiSkillSearchInput): Promise<ToolNetApiSkillSearch>;
|
|
14
|
+
offloadRead(input: ToolNetApiContextOffloadReadInput): Promise<ToolNetApiContextOffloadRead>;
|
|
15
|
+
hub(): Promise<ToolNetApiHubSummary>;
|
|
16
|
+
hubTeams(): Promise<ToolNetApiHubTeams>;
|
|
17
|
+
createHubTeam(input: ToolNetApiCreateHubTeamInput): Promise<ToolNetApiHubTeam>;
|
|
18
|
+
hubAgents(): Promise<ToolNetApiHubAgents>;
|
|
19
|
+
createHubAgent(input: ToolNetApiCreateHubAgentInput): Promise<ToolNetApiHubAgent>;
|
|
20
|
+
hubAcl(): Promise<ToolNetApiHubAcl>;
|
|
21
|
+
grantHubAcl(input: ToolNetApiGrantHubAclInput): Promise<ToolNetApiHubAclGrant>;
|
|
22
|
+
revokeHubAcl(principal: string): Promise<ToolNetApiHubAclRevoke>;
|
|
23
|
+
hubLoadouts(): Promise<ToolNetApiHubLoadouts>;
|
|
24
|
+
setHubLoadout(input: ToolNetApiSetHubLoadoutInput): Promise<ToolNetApiHubLoadout>;
|
|
25
|
+
hubObservability(): Promise<ToolNetApiHubObservability>;
|
|
26
|
+
wiki(): Promise<ToolNetApiWikiSummary>;
|
|
27
|
+
wikiPages(): Promise<ToolNetApiWikiPages>;
|
|
28
|
+
createWikiPage(input: ToolNetApiCreateWikiPageInput): Promise<ToolNetApiWikiPage>;
|
|
29
|
+
wikiPage(slug: string): Promise<ToolNetApiWikiPage>;
|
|
30
|
+
updateWikiPage(slug: string, input: ToolNetApiUpdateWikiPageInput): Promise<ToolNetApiWikiPage>;
|
|
31
|
+
wikiSearch(query: string, limit?: number): Promise<ToolNetApiWikiSearch>;
|
|
32
|
+
wikiHistory(slug: string): Promise<ToolNetApiWikiHistory>;
|
|
33
|
+
wikiBacklinks(slug: string): Promise<ToolNetApiWikiBacklinks>;
|
|
34
|
+
governance(): Promise<ToolNetApiGovernanceSummary>;
|
|
35
|
+
governanceReviews(status?: ToolNetKnowledgeGovernanceReviewStatus): Promise<ToolNetApiGovernanceReviews>;
|
|
36
|
+
reviewKnowledge(reviewId: string, input: ToolNetGovernanceDecisionInput): Promise<ToolNetApiGovernanceReview>;
|
|
37
|
+
knowledgeQuality(): Promise<ToolNetApiKnowledgeQuality>;
|
|
38
|
+
governancePolicy(): Promise<ToolNetApiGovernancePolicy>;
|
|
39
|
+
setGovernancePolicy(input: Partial<ToolNetKnowledgeGovernancePolicy>): Promise<ToolNetApiGovernancePolicy>;
|
|
40
|
+
protected request<T>(path: string, init?: RequestInit): Promise<T>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
export class ToolNetApiClient {
|
|
2
|
+
baseUrl;
|
|
3
|
+
token;
|
|
4
|
+
principal;
|
|
5
|
+
timeoutMs;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.baseUrl = options.baseUrl.replace(/\/+$/u, '');
|
|
8
|
+
this.token = options.token;
|
|
9
|
+
this.principal = options.principal;
|
|
10
|
+
this.timeoutMs = options.timeoutMs ?? 10_000;
|
|
11
|
+
}
|
|
12
|
+
health() {
|
|
13
|
+
return this.request('/v1/health');
|
|
14
|
+
}
|
|
15
|
+
project() {
|
|
16
|
+
return this.request('/v1/project');
|
|
17
|
+
}
|
|
18
|
+
memoryAsk(input) {
|
|
19
|
+
return this.request('/v1/memory/ask', {
|
|
20
|
+
method: 'POST',
|
|
21
|
+
body: JSON.stringify(input),
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
memorySearch(input) {
|
|
25
|
+
return this.request('/v1/memory/search', {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
body: JSON.stringify(input),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
skillSearch(input) {
|
|
31
|
+
return this.request('/v1/skills/search', {
|
|
32
|
+
method: 'POST',
|
|
33
|
+
body: JSON.stringify(input),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
offloadRead(input) {
|
|
37
|
+
return this.request('/v1/offload/read', {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
body: JSON.stringify(input),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
hub() {
|
|
43
|
+
return this.request('/v1/hub');
|
|
44
|
+
}
|
|
45
|
+
hubTeams() {
|
|
46
|
+
return this.request('/v1/hub/teams');
|
|
47
|
+
}
|
|
48
|
+
createHubTeam(input) {
|
|
49
|
+
return this.request('/v1/hub/teams', {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
body: JSON.stringify(input),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
hubAgents() {
|
|
55
|
+
return this.request('/v1/hub/agents');
|
|
56
|
+
}
|
|
57
|
+
createHubAgent(input) {
|
|
58
|
+
return this.request('/v1/hub/agents', {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
body: JSON.stringify(input),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
hubAcl() {
|
|
64
|
+
return this.request('/v1/hub/acl');
|
|
65
|
+
}
|
|
66
|
+
grantHubAcl(input) {
|
|
67
|
+
return this.request('/v1/hub/acl/grant', {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
body: JSON.stringify(input),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
revokeHubAcl(principal) {
|
|
73
|
+
return this.request('/v1/hub/acl/revoke', {
|
|
74
|
+
method: 'POST',
|
|
75
|
+
body: JSON.stringify({ principal }),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
hubLoadouts() {
|
|
79
|
+
return this.request('/v1/hub/loadouts');
|
|
80
|
+
}
|
|
81
|
+
setHubLoadout(input) {
|
|
82
|
+
return this.request('/v1/hub/loadouts', {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
body: JSON.stringify(input),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
hubObservability() {
|
|
88
|
+
return this.request('/v1/hub/observability');
|
|
89
|
+
}
|
|
90
|
+
wiki() {
|
|
91
|
+
return this.request('/v1/wiki');
|
|
92
|
+
}
|
|
93
|
+
wikiPages() {
|
|
94
|
+
return this.request('/v1/wiki/pages');
|
|
95
|
+
}
|
|
96
|
+
createWikiPage(input) {
|
|
97
|
+
return this.request('/v1/wiki/pages', {
|
|
98
|
+
method: 'POST',
|
|
99
|
+
body: JSON.stringify(input),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
wikiPage(slug) {
|
|
103
|
+
return this.request(`/v1/wiki/pages/${encodeURIComponent(slug)}`);
|
|
104
|
+
}
|
|
105
|
+
updateWikiPage(slug, input) {
|
|
106
|
+
return this.request(`/v1/wiki/pages/${encodeURIComponent(slug)}`, {
|
|
107
|
+
method: 'PUT',
|
|
108
|
+
body: JSON.stringify(input),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
wikiSearch(query, limit = 10) {
|
|
112
|
+
const params = new URLSearchParams({
|
|
113
|
+
q: query,
|
|
114
|
+
limit: String(limit),
|
|
115
|
+
});
|
|
116
|
+
return this.request(`/v1/wiki/search?${params.toString()}`);
|
|
117
|
+
}
|
|
118
|
+
wikiHistory(slug) {
|
|
119
|
+
return this.request(`/v1/wiki/pages/${encodeURIComponent(slug)}/history`);
|
|
120
|
+
}
|
|
121
|
+
wikiBacklinks(slug) {
|
|
122
|
+
return this.request(`/v1/wiki/pages/${encodeURIComponent(slug)}/backlinks`);
|
|
123
|
+
}
|
|
124
|
+
governance() {
|
|
125
|
+
return this.request('/v1/governance');
|
|
126
|
+
}
|
|
127
|
+
governanceReviews(status) {
|
|
128
|
+
const suffix = status ? `?status=${encodeURIComponent(status)}` : '';
|
|
129
|
+
return this.request(`/v1/governance/reviews${suffix}`);
|
|
130
|
+
}
|
|
131
|
+
reviewKnowledge(reviewId, input) {
|
|
132
|
+
return this.request(`/v1/governance/reviews/${encodeURIComponent(reviewId)}`, {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
body: JSON.stringify(input),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
knowledgeQuality() {
|
|
138
|
+
return this.request('/v1/governance/quality');
|
|
139
|
+
}
|
|
140
|
+
governancePolicy() {
|
|
141
|
+
return this.request('/v1/governance/policy');
|
|
142
|
+
}
|
|
143
|
+
setGovernancePolicy(input) {
|
|
144
|
+
return this.request('/v1/governance/policy', {
|
|
145
|
+
method: 'PUT',
|
|
146
|
+
body: JSON.stringify(input),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
async request(path, init = {}) {
|
|
150
|
+
const controller = new AbortController();
|
|
151
|
+
const timeout = setTimeout(() => {
|
|
152
|
+
controller.abort();
|
|
153
|
+
}, this.timeoutMs);
|
|
154
|
+
try {
|
|
155
|
+
const headers = new Headers(init.headers);
|
|
156
|
+
headers.set('accept', 'application/json');
|
|
157
|
+
if (init.body !== undefined) {
|
|
158
|
+
headers.set('content-type', 'application/json');
|
|
159
|
+
}
|
|
160
|
+
if (this.token) {
|
|
161
|
+
headers.set('authorization', `Bearer ${this.token}`);
|
|
162
|
+
}
|
|
163
|
+
if (this.principal) {
|
|
164
|
+
headers.set('x-toolnet-principal', this.principal);
|
|
165
|
+
}
|
|
166
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
167
|
+
...init,
|
|
168
|
+
headers,
|
|
169
|
+
signal: controller.signal,
|
|
170
|
+
});
|
|
171
|
+
const text = await response.text();
|
|
172
|
+
const body = text ? JSON.parse(text) : null;
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
const message = body && typeof body === 'object' && 'error' in body && typeof body.error === 'string'
|
|
175
|
+
? body.error
|
|
176
|
+
: `ToolNet API request failed: HTTP ${response.status}`;
|
|
177
|
+
throw new Error(message);
|
|
178
|
+
}
|
|
179
|
+
return body;
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
clearTimeout(timeout);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|