@tokmizer/plugin 0.5.11 → 0.5.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.
package/codex-plugin/plugin.json
CHANGED
|
@@ -9,5 +9,6 @@ var ar=Object.defineProperty;var or=(t,e)=>{for(var r in e)ar(t,r,{get:e[r],enum
|
|
|
9
9
|
`,tc=64*1024;function Wt(t){return JSON.stringify(t)+Le}function*Gt(t){let e=0;for(;;){let r=t.indexOf(Le,e);if(r<0)return;yield t.slice(e,r),e=r+1}}function je(){return`${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2,8)}`}import{join as qe}from"node:path";import{existsSync as qt,statSync as Hs,unlinkSync as Ws,mkdirSync as Gs,chmodSync as qs}from"node:fs";function Fe(){if(process.platform==="win32")return{path:`\\\\.\\pipe\\tokmizer-${process.env.USERNAME??"tokmizer"}`,isPipe:!0};let t=typeof process.getuid=="function"?process.getuid():0,e=process.env.XDG_RUNTIME_DIR;if(e&&qt(e))return{path:qe(e,`tokmizer-${t}.sock`),isPipe:!1};let r=qe(C(),"run");try{Gs(r,{recursive:!0,mode:448}),qs(r,448)}catch{}return{path:qe(r,"daemon.sock"),isPipe:!1}}async function Yt(){let{path:t,isPipe:e}=Fe();if(e||!qt(t))return;try{if(!Hs(t).isSocket())return}catch{return}let r=await import("node:net");if(!await new Promise(s=>{let i=r.createConnection(t),a=o=>{i.removeAllListeners();try{i.destroy()}catch{}s(o)};i.once("connect",()=>a(!0)),i.once("error",o=>{let l=o.code;a(l!=="ECONNREFUSED"&&l!=="ENOENT")}),setTimeout(()=>a(!0),250)}))try{Ws(t)}catch{}}var ac=5*6e4;var ri=500,ni=3e3,er=1;async function si(t,e){return new Promise((r,n)=>{let s=Ys(t),i=o=>{if(s.removeAllListeners("connect"),s.removeAllListeners("error"),clearTimeout(a),o){try{s.destroy()}catch{}n(o)}else r(s)},a=setTimeout(()=>i(new Error("connect timeout")),e);s.once("connect",()=>i(null)),s.once("error",o=>i(o))})}function ii(){return process.env.TOKMIZER_DAEMON_BIN?process.env.TOKMIZER_DAEMON_BIN:ti(new URL("../bin/tkr-daemon.js",import.meta.url))}async function tr(t,e=3e3){let r=Math.ceil(e/75);for(let n=0;n<r;n++){if(rr(t))return!0;await new Promise(s=>setTimeout(s,75))}return!1}async function ai(t){let e=ii();if(!rr(e))throw new Error(`tkr-daemon binary not found at ${e}`);let{path:r}=Fe(),n=ei(Qs(r),"daemon.spawn.lock"),s=null;try{s=Jt(n,"wx"),Qt(s,String(process.pid))}catch(i){if(i.code==="EEXIST"){if(await tr(r,5e3))return;try{Xt(n)}catch{}s=Jt(n,"wx"),Qt(s,String(process.pid))}else throw i}try{if(Js(process.execPath,[e],{detached:!0,stdio:["ignore","ignore","ignore"],env:{...process.env,TOKMIZER_DAEMON_INSTALL_ID:t}}).unref(),!await tr(r,3e3))throw new Error("tkr-daemon failed to bind socket within 3s")}finally{if(s!==null){try{Xs(s)}catch{}try{Xt(n)}catch{}}}}async function nr(t={}){let{path:e}=t.socketPath?{path:t.socketPath}:Fe(),r=await Ft(),n=null,s=0;for(;s<=er;)try{n=await si(e,ri);break}catch{if(s+=1,s>er)throw new Error(`tkr-daemon unreachable at ${e}`);await Yt(),await ai(r)}if(!n)throw new Error("tkr-daemon: socket setup failed");let i=new Map,a="";n.setEncoding("utf8"),n.on("data",l=>{a+=l;let c=-1;for(let _=0;_<a.length;_++)a[_]===Le&&(c=_);if(c<0)return;let f=[];for(let _ of Gt(a.slice(0,c+1)))f.push(_);a=a.slice(c+1);for(let _ of f)if(_)try{let w=JSON.parse(_),T=i.get(w.id);if(!T)continue;clearTimeout(T.timer),i.delete(w.id),T.resolve(w)}catch{}}),n.on("close",()=>{for(let l of i.values())clearTimeout(l.timer),l.reject(new Error("daemon socket closed"));i.clear()}),n.on("error",()=>{});function o(l){return new Promise((c,f)=>{let _=setTimeout(()=>{i.delete(l.id),f(new Error(`daemon request timeout (${l.op})`))},ni);i.set(l.id,{resolve:c,reject:f,timer:_}),n.write(Wt(l))})}return{filter:async(l,c)=>{let f=await o({id:je(),op:"filter",cmd:l.slice(0,2048),plan:c,installId:r});if(!f.ok)return null;let _=Ht.safeParse(f.data);return _.success?_.data.filter:null},ping:async()=>{let l=await o({id:je(),op:"ping"});if(!l.ok)throw new Error(l.err);return l.data},reload:async()=>{let l=await o({id:je(),op:"reload",installId:r});if(!l.ok)throw new Error(l.err);return l.data},close:()=>{try{n.end()}catch{}}}}var be=null,Ae=null;async function oi(){return be||Ae||(Ae=nr().then(t=>(be=t,t)).catch(()=>null),Ae)}function ci(t){return t?{...t,matchRegex:new RegExp(t.match_command)}:null}function sr(t){Ve(async(e,r)=>{let n=await oi();if(!n)return null;try{let s=t==="free"?"trial":t,i=await n.filter(e,s);return!i||!(r??Ne(void 0)).has(q(i.slug))?null:ci(i)}catch{return null}})}function ir(){if(Ve(null),be){try{be.close()}catch{}be=null,Ae=null}}var li=new Set(["Read","Grep","Glob","Bash"]),ui=10*1024*1024,di=1024;function fi(t,e){return!t||typeof t=="string"?e:t.file?.content!=null?{...t,file:{...t.file,content:e}}:t.content!=null?{...t,content:e}:t.output!=null?{...t,output:e}:t.stdout!=null||t.stderr!=null?{...t,stdout:e,stderr:""}:Array.isArray(t.filenames)?{...t,filenames:e.split(`
|
|
10
10
|
`)}:e}function pi(t,e){return JSON.stringify({hookSpecificOutput:{hookEventName:"PostToolUse",updatedToolOutput:fi(t.tool_response,e)}})}function mi(t){return t?typeof t=="string"?t:t.file?.content!=null?t.file.content:t.content!=null?t.content:t.output!=null?t.output:t.stdout!=null||t.stderr!=null?[t.stdout??"",t.stderr??""].filter(Boolean).join(`
|
|
11
11
|
`):Array.isArray(t.filenames)?t.filenames.join(`
|
|
12
|
-
`):"":""}function hi(t){let e=t.tool_name??"tool",r=t.tool_input??{};return r.
|
|
12
|
+
`):"":""}function hi(t){let e=t.tool_name??"tool",r=t.tool_input??{};return r.command?r.command.split(`
|
|
13
|
+
`)[0].trim().slice(0,200):r.file_path?`${e.toLowerCase()} ${r.file_path}`:r.pattern?`${e.toLowerCase()} ${r.pattern}`:r.path?`${e.toLowerCase()} ${r.path}`:e.toLowerCase()}async function gi(t){if(!t.tool_name||!li.has(t.tool_name)||t.tool_name==="Bash"&&(t.tool_input?.command??"").includes("tkr-exec"))return null;let e=mi(t.tool_response);if(!e)return null;let r=Buffer.byteLength(e,"utf8");if(r>ui||t.tool_name==="Bash"&&r<di)return null;let n=await He();if(!n||!n.token)return null;let s=await jt();if(!s.enabled)return null;let i=Mt(n);sr(i);let a=Ne(n.allowedFilters,We(n)),o=hi(t),l=`${t.tool_name.toLowerCase()}-tool`,c=`${l}: ${o}`;try{let f=await Dt(c,e,a);return await Vt({cmd:l,filterId:f.filterId,rawBytes:f.rawBytes,filteredBytes:f.filteredBytes,plan:i}),Kt(f.rawBytes,f.filteredBytes),await Ut({cmd:l,tokensSaved:Math.round((f.rawBytes-f.filteredBytes)/4),optimized:f.filterId!=null,telemetryOptIn:s.telemetryOptIn}),f.filteredBytes>=f.rawBytes?null:f.output}finally{ir()}}async function yi(){let t=await Ot();t||process.exit(0);let e;try{e=JSON.parse(t)}catch{process.exit(0)}let r=await gi(e);r||process.exit(0),process.stdout.write(pi(e,r))}var _i=process.argv[1]??"";$t(_i,import.meta.url)&&yi().catch(t=>{process.stderr.write(`tkr post-tool-use: ${t.message}
|
|
13
14
|
`),process.exit(0)});export{pi as buildHookEnvelope,gi as processToolOutput};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokmizer/plugin",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.12",
|
|
4
4
|
"description": "Tokmizer reduces token consumption for AI coding tools (Claude Code, Codex, Cursor, Aider, Gemini CLI, Cline, Windsurf, Copilot). Cross-OS.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|