tmux-weblink 3.1.0 → 3.1.1
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/dist/lib/bots/store.js +3 -3
- package/package.json +1 -1
package/dist/lib/bots/store.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{existsSync as o,mkdirSync as
|
|
2
|
-
`,{mode:384})}catch{}}readEvents(t,e=2e3){const s=n.join(this.instanceDir(t),"events.jsonl");if(!o(s))return[];const r=
|
|
3
|
-
`).filter(Boolean).slice(-e),l=[];for(const
|
|
1
|
+
import{existsSync as o,mkdirSync as c,readFileSync as u,writeFileSync as h,readdirSync as m,rmSync as p,appendFileSync as S,copyFileSync as D}from"node:fs";import n from"node:path";import{randomBytes as j,randomUUID as y}from"node:crypto";import{getDataRoot as g}from"../state-paths.js";class v{root;instancesFile;instances=new Map;token;constructor(t){this.root=t||process.env.TMUX_WEB_BOTS_DIR||n.join(g(),"bots"),c(this.root,{recursive:!0,mode:448}),c(n.join(this.root,"templates"),{recursive:!0,mode:448}),c(n.join(this.root,"templates",".history"),{recursive:!0,mode:448}),c(n.join(this.root,"instances"),{recursive:!0,mode:448}),this.instancesFile=n.join(this.root,"instances.json"),this.loadInstances(),this.token=this.loadOrCreateToken()}loadOrCreateToken(){const t=n.join(this.root,"api-token");if(o(t)){const s=u(t,"utf8").trim();if(s)return s}const e=j(24).toString("hex");return h(t,e,{mode:384}),e}templatesDir=()=>n.join(this.root,"templates");listTemplates(){const t=this.templatesDir();if(!o(t))return[];const e=[];for(const s of m(t))if(s.endsWith(".json"))try{const i=JSON.parse(u(n.join(t,s),"utf8"));i&&i.id&&i.manifest&&e.push(i)}catch{}return e.sort((s,i)=>s.manifest.name.localeCompare(i.manifest.name))}getTemplate(t){const e=n.join(this.templatesDir(),`${t}.json`);if(!o(e))return null;try{return JSON.parse(u(e,"utf8"))}catch{return null}}saveTemplate(t){const e=new Date().toISOString(),s=t.id||`${Date.now().toString(36)}-${j(3).toString("hex")}`,i=this.getTemplate(s);i&&this.snapshotTemplate(n.join(this.templatesDir(),`${s}.json`),s);const r={id:s,manifest:{...t.manifest,schema:1},createdAt:i?.createdAt||e,updatedAt:e};return h(n.join(this.templatesDir(),`${s}.json`),JSON.stringify(r,null,2),{mode:384}),r}deleteTemplate(t){const e=n.join(this.templatesDir(),`${t}.json`);return o(e)?(this.snapshotTemplate(e,t),p(e),!0):!1}snapshotTemplate(t,e){try{const s=n.join(this.templatesDir(),".history",e);c(s,{recursive:!0,mode:448});const i=new Date().toISOString().replace(/[:.]/g,"-");let r="";for(let a=1;a<1e3;a++){const d=`${i}-${String(a).padStart(3,"0")}`;if(!o(n.join(s,`${d}.json`))){r=d;break}}if(!r)return;D(t,n.join(s,`${r}.json`));const l=m(s).sort();for(const a of l.slice(0,Math.max(0,l.length-10)))p(n.join(s,a),{force:!0})}catch{}}listTemplateHistory(t){const e=n.join(this.templatesDir(),".history",t);return o(e)?m(e).sort():[]}loadInstances(){if(o(this.instancesFile))try{const t=JSON.parse(u(this.instancesFile,"utf8"));for(const e of t)this.instances.set(e.id,e)}catch{}}persistInstances(){h(this.instancesFile,JSON.stringify([...this.instances.values()],null,2),{mode:384})}listInstances(){return[...this.instances.values()].sort((t,e)=>t.createdAt.localeCompare(e.createdAt))}getInstance(t){return this.instances.get(t)||null}saveInstance(t){t.updatedAt=new Date().toISOString(),this.instances.set(t.id,t),this.persistInstances()}deleteInstance(t){this.instances.delete(t),this.persistInstances(),p(this.instanceDir(t),{recursive:!0,force:!0})}instanceDir(t){return n.join(this.root,"instances",t)}agentDir(t){return n.join(this.instanceDir(t),"agent")}sessionsDir(t){return n.join(this.instanceDir(t),"sessions")}appendEvent(t,e){try{c(this.instanceDir(t),{recursive:!0,mode:448}),S(n.join(this.instanceDir(t),"events.jsonl"),`${JSON.stringify(e)}
|
|
2
|
+
`,{mode:384})}catch{}}readEvents(t,e=2e3){const s=n.join(this.instanceDir(t),"events.jsonl");if(!o(s))return[];const r=u(s,"utf8").split(`
|
|
3
|
+
`).filter(Boolean).slice(-e),l=[];for(const a of r)try{l.push(JSON.parse(a))}catch{}return l}newId(){return y()}}let f=null;function F(){return f||(f=new v),f}export{v as BotStore,F as getBotStore};
|