@tokmizer/plugin 0.9.60 → 0.9.61
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/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/README.md +45 -14
- package/command-templates/tkr-status.md +1 -1
- package/dist/bin/tkr-codex-hook.js +4 -4
- package/dist/bin/tkr-daemon.js +58 -58
- package/dist/bin/tkr-exec +35 -35
- package/dist/bin/tkr-exec.js +35 -35
- package/dist/bin/tkr-processor.js +4 -4
- package/dist/bin/tkr-shim.js +4 -4
- package/dist/bin/tkr.js +85 -85
- package/dist/hooks-host/command-expansion.js +3 -0
- package/dist/hooks-host/post-tool-use.js +4 -4
- package/dist/hooks-host/pre-tool-use.js +5 -5
- package/dist/hooks-host/reducer-stop.js +13 -13
- package/dist/hooks-host/session-recap.js +1 -1
- package/dist/index.js +4 -4
- package/dist/native/manifest.json +12 -12
- package/dist/native/single.json +12 -12
- package/dist/native/tkr-exec-darwin-arm64 +0 -0
- package/dist/native/tkr-exec-darwin-x64 +0 -0
- package/dist/native/tkr-exec-linux-arm64 +0 -0
- package/dist/native/tkr-exec-linux-x64 +0 -0
- package/dist/native/tkr-exec-win32-arm64.exe +0 -0
- package/dist/native/tkr-exec-win32-x64.exe +0 -0
- package/hooks/hooks.json +12 -0
- package/package.json +7 -7
|
@@ -3,10 +3,10 @@ function v(t=5e3){return new Promise(e=>{let n=[],r=process.stdin,o=!1,i=()=>{o|
|
|
|
3
3
|
`).filter(c=>c.trim()!==""),o=[],i=0;for(let c=r.length-1;c>=0;c-=1){let u=r[c],l=Buffer.byteLength(u)+1;if(i+l>n)break;o.push(u),i+=l}o.reverse();let s=`${t}.tmp-${process.pid}`;te(s,o.length?`${o.join(`
|
|
4
4
|
`)}
|
|
5
5
|
`:"",{mode:384}),Xt(s,t)}catch{}}var ce="usage.jsonl";var mt=2*1024*1024;function ue(){return ie($(),ce)}function k(t,e,n=ue()){try{let r=se(n);oe(r)||re(r,{recursive:!0,mode:448});let o=t.replace(/[^A-Za-z0-9._-]/g,"").slice(0,64)||"unknown";ee(n,`{"t":${Math.floor(Date.now()/1e3)},"s":"${o}","o":${e?1:0}}
|
|
6
|
-
`,{mode:384}),ne(n,384),pt(n,mt,mt/2)}catch{}}import{rmSync as le,statSync as ae}from"node:fs";import{join as fe}from"node:path";function gt(){return fe($(),"fail-open")}var de=24*36e5;function S(t=Date.now()){try{if(t-ae(gt()).mtimeMs<de)return!0;le(gt(),{force:!0})}catch{}return!1}import{existsSync as B,readFileSync as nt,readdirSync as rt}from"node:fs";import{homedir as A}from"node:os";import{fileURLToPath as Ge}from"node:url";import{dirname as Tt,join as f,resolve as Ze}from"node:path";function M(){return!1}import{execFileSync as He}from"node:child_process";import{mkdirSync as Ne,readFileSync as yt,renameSync as Ie,statSync as Le,writeFileSync as je}from"node:fs";import{join as
|
|
7
|
-
`)){let o=Te.exec(r);if(o&&n!=="# Functions"){let i=kt(o[1].trim());/[$`\\]/.test(i)||e.paths.push(i);continue}if(r.startsWith("# ")&&(n!=="# Functions"||Re.test(r))){n=r;continue}if(n==="# Functions"){let i=ve.exec(r)??Ee.exec(r);i&&e.functions.add(i[1])}else if(n==="# Aliases"){let i=Ae.exec(r);i&&e.aliases.set(i[1],kt(i[2].trim()))}}}function X(t){return t?.code==="ENOENT"}function bt(t){let e={functions:new Set,aliases:new Map,paths:[],uncertain:!1},n=J(t||J(_e(),".claude"),"shell-snapshots"),r=[];try{for(let o of he(n)){if(!o.startsWith("snapshot-")||!o.endsWith(".sh"))continue;let i=J(n,o);try{let s=_t(i);s.isFile()&&r.push({path:i,mtime:s.mtimeMs,size:s.size})}catch(s){X(s)||(e.uncertain=!0)}}}catch(o){return X(o)||(e.uncertain=!0),e}r=r.sort((o,i)=>i.mtime-o.mtime).slice(0,
|
|
8
|
-
`,1)[0].slice(2).trim().split(/\s+/),o=r[0]??"";return o==="/usr/bin/env"?E(r[1]??"",void 0,e):L(o)}catch{return!1}}function We(t,e,n){try{let r=Le(t);return[t,r.size,r.mtimeMs,r.ino,r.mode,e?n??"":""].join("|")}catch{return`${t}|missing`}}function tt(t,e=process.platform,n=process.env.PATH,r=Ue(),o=Date.now()){let i=e!=="win32"&&Q(t)==="#!",s=We(t,i,n),c=Me(r);if(c?.key===s&&(c.ok||o-c.at<De))return c.ok;let u=L(t,e)&&(i?Be(t,n):ze(t));return Je(r,{key:s,ok:u,at:o,skipped:(c?.skipped??0)+(u?0:1)}),u}var Ke=new Set(["tkr","tkr-exec","token","claude","cd","export","source","echo","mkdir","rm","mv","cp","chmod","ln","true","false",":","test","[","exit","return","nohup","sudo","doas","env"]),qe=new Set(["for","while","until","if","then","else","elif","fi","do","done","case","esac","select","function","time","coproc","in","!","[[","]]","{","}",".","declare","typeset","local","readonly","unset","set","shift","trap","wait","read","eval","exec","break","continue","pushd","popd","dirs","alias","unalias","hash","ulimit","umask","getopts","let","shopt","builtin","command","compgen","complete","disown","suspend","jobs","fg","bg","kill","history","fc","bind","enable","caller","type","printf","pwd","times","logout","mapfile","readarray","help","which","print","whence","where","autoload","zmodload","setopt","unsetopt","emulate","noglob","rehash","functions","bindkey","zle","vared","sched","limit","unlimit","zstyle","zparseopts","ttyctl"]),
|
|
9
|
-
`||c==="|"){s(c,1);continue}}r+=c,o+=1}return i(),n}function Nt(t){return t.trim().split(/\s+/)[0]??""}function _(t,e=process.platform){try{let n=Tt(Ge(import.meta.url)),r=e==="win32",o=t&&t!=="claude-code"&&t!=="codex"&&!r?`TOKMIZER_HOST=${t} `:"";if(r&&(!t||t==="claude-code")){let c=f(n,"..","bin","tkr-exec.exe");if(B(c))return tt(c,e)?`'${c.replace(/\\/g,"/").replace(/'/g,"'\\''")}'`:null}let i=f(n,"..","bin","tkr-exec");if(!r&&B(i))return tt(i,e)?`${o}'${i.replace(/'/g,"'\\''")}'`:null;if(M()){let c=r?process.execPath.replace(/\\/g,"/"):process.execPath;return`${o}'${c.replace(/'/g,"'\\''")}' exec`}let s=f(n,"..","bin","tkr-exec.js");return B(s)?`${o}node '${s.replace(/'/g,"'\\''")}'`:null}catch{return null}}function It(t,e="tkr-exec",n=()=>!0,r){return Lt(t,e,0,n,r)}function Lt(t,e,n,r,o){if(t.includes("<<")||un.test(t))return t;let i=G(t),s=[];for(let c=0;c<i.length;c++){let u=i[c];if(u.kind==="sep"||u.unsafe){s.push(u.text);continue}let l=u.text.trim();if(!l){s.push(u.text);continue}let a=Nt(l),p=i[c+1],b=p?.kind==="sep"&&(p.text==="|"||p.text==="|&"),d=p?.kind==="sep"&&p.text==="&",y=!b&&!d&&!vt(u.text)&&!u.text.includes("<(")&&!u.text.includes(">(")?rn(l,e,n,r,o):null;if(y!==null){s.push(`${u.text.match(/^\s*/)?.[0]??""}${y}${u.text.match(/\s*$/)?.[0]??""}`);continue}if(l===e||l.startsWith(e)&&/^\s/.test(l.slice(e.length))||Ke.has(a)||qe.has(a)||(
|
|
6
|
+
`,{mode:384}),ne(n,384),pt(n,mt,mt/2)}catch{}}import{rmSync as le,statSync as ae}from"node:fs";import{join as fe}from"node:path";function gt(){return fe($(),"fail-open")}var de=24*36e5;function S(t=Date.now()){try{if(t-ae(gt()).mtimeMs<de)return!0;le(gt(),{force:!0})}catch{}return!1}import{existsSync as B,readFileSync as nt,readdirSync as rt}from"node:fs";import{homedir as A}from"node:os";import{fileURLToPath as Ge}from"node:url";import{dirname as Tt,join as f,resolve as Ze}from"node:path";function M(){return!1}import{execFileSync as He}from"node:child_process";import{mkdirSync as Ne,readFileSync as yt,renameSync as Ie,statSync as Le,writeFileSync as je}from"node:fs";import{join as xt}from"node:path";import{closeSync as pe,openSync as me,readSync as ge,readdirSync as he,readFileSync as ke,statSync as _t}from"node:fs";import{homedir as _e}from"node:os";import{isAbsolute as be,join as J,resolve as Se}from"node:path";var ye=/^[A-Za-z0-9._+@%:,/-]+$/,xe=new Set(["7f454c46","feedface","feedfacf","cefaedfe","cffaedfe","cafebabe","bebafeca"]),we=32,$e=4*1024*1024;function Q(t){let e;try{e=me(t,"r");let n=Buffer.alloc(4),r=ge(e,n,0,4,0);return r>=2&&n[0]===35&&n[1]===33?"#!":r===4?n.toString("hex"):null}catch{return null}finally{e!==void 0&&pe(e)}}function L(t,e=process.platform){try{let n=_t(t);if(!n.isFile())return!1;if(e==="win32")return!0;if((n.mode&73)===0)return!1;let r=Q(t);return r==="#!"||r!==null&&xe.has(r)}catch{return!1}}function Oe(t){if(/\.[A-Za-z0-9]+$/.test(t))return[t];let e=(process.env.PATHEXT??".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean);return[t,...e.map(n=>t+n.toLowerCase())]}function E(t,e,n,r=process.platform){if(!ye.test(t))return!1;let o=r==="win32";if(t.includes("/"))return L(Se(e??process.cwd(),t),r);if(!n)return!1;let i=o?Oe(t):[t];for(let s of n.split(o?";":":"))if(!(!s||!be(s))){for(let c of i)if(L(J(s,c),r))return!0}return!1}var ve=/^eval \$'([^\s'()=]+) \(\) /,Ee=/^([^\s'"()=]+) \(\) \{$/,Ae=/^alias -- ([^\s=]+)=(.*)$/,Re=/^# (?:Shell Options|Aliases|Shopt|Check for |Shadow )/,Te=/^export PATH=(.*)$/,Ce=/^(?:--colou?r(?:=\S+)?|-G)$/;function kt(t){return t.length>=2&&t.startsWith("'")&&t.endsWith("'")?t.slice(1,-1).replace(/'\\''/g,"'"):t.length>=2&&t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t}function Pe(t,e){let n="";for(let r of t.split(`
|
|
7
|
+
`)){let o=Te.exec(r);if(o&&n!=="# Functions"){let i=kt(o[1].trim());/[$`\\]/.test(i)||e.paths.push(i);continue}if(r.startsWith("# ")&&(n!=="# Functions"||Re.test(r))){n=r;continue}if(n==="# Functions"){let i=ve.exec(r)??Ee.exec(r);i&&e.functions.add(i[1])}else if(n==="# Aliases"){let i=Ae.exec(r);i&&e.aliases.set(i[1],kt(i[2].trim()))}}}function X(t){return t?.code==="ENOENT"}function bt(t){let e={functions:new Set,aliases:new Map,paths:[],uncertain:!1},n=J(t||J(_e(),".claude"),"shell-snapshots"),r=[];try{for(let o of he(n)){if(!o.startsWith("snapshot-")||!o.endsWith(".sh"))continue;let i=J(n,o);try{let s=_t(i);s.isFile()&&r.push({path:i,mtime:s.mtimeMs,size:s.size})}catch(s){X(s)||(e.uncertain=!0)}}}catch(o){return X(o)||(e.uncertain=!0),e}r=r.sort((o,i)=>i.mtime-o.mtime).slice(0,we);for(let o of r)try{if(o.size>$e)throw new Error("snapshot too large");Pe(ke(o.path,"utf8"),e)}catch(i){X(i)||(e.uncertain=!0)}return e}function St(t,e){if(e.uncertain||e.functions.has(t))return!0;let n=e.aliases.get(t);if(n===void 0)return!1;let r=n.trim().split(/\s+/);return r[0]!==t||!r.slice(1).every(o=>Ce.test(o))}var Fe="exec-probe.json",De=6e4;function Ue(){return xt($(),Fe)}function Me(t){try{let e=JSON.parse(yt(t,"utf8"));if(e==null||typeof e!="object")return null;let n=e;return typeof n.key!="string"||typeof n.ok!="boolean"?null:{key:n.key,ok:n.ok,at:typeof n.at=="number"?n.at:0,skipped:typeof n.skipped=="number"&&n.skipped>=0?n.skipped:0}}catch{return null}}function Je(t,e){try{Ne(xt(t,".."),{recursive:!0,mode:448});let n=`${t}.${process.pid}.tmp`;je(n,JSON.stringify(e),{mode:384}),Ie(n,t)}catch{}}function ze(t){try{return He(t,["--native-check"],{encoding:"utf8",timeout:2e3,maxBuffer:1024,stdio:["ignore","pipe","ignore"],windowsHide:!0}).startsWith("tokmizer-native ")}catch{return!1}}function Be(t,e){try{let r=yt(t,"utf8").split(`
|
|
8
|
+
`,1)[0].slice(2).trim().split(/\s+/),o=r[0]??"";return o==="/usr/bin/env"?E(r[1]??"",void 0,e):L(o)}catch{return!1}}function We(t,e,n){try{let r=Le(t);return[t,r.size,r.mtimeMs,r.ino,r.mode,e?n??"":""].join("|")}catch{return`${t}|missing`}}function tt(t,e=process.platform,n=process.env.PATH,r=Ue(),o=Date.now()){let i=e!=="win32"&&Q(t)==="#!",s=We(t,i,n),c=Me(r);if(c?.key===s&&(c.ok||o-c.at<De))return c.ok;let u=L(t,e)&&(i?Be(t,n):ze(t));return Je(r,{key:s,ok:u,at:o,skipped:(c?.skipped??0)+(u?0:1)}),u}var Ke=new Set(["tkr","tkr-exec","token","claude","cd","export","source","echo","mkdir","rm","mv","cp","chmod","ln","true","false",":","test","[","exit","return","nohup","sudo","doas","env"]),qe=new Set(["for","while","until","if","then","else","elif","fi","do","done","case","esac","select","function","time","coproc","in","!","[[","]]","{","}",".","declare","typeset","local","readonly","unset","set","shift","trap","wait","read","eval","exec","break","continue","pushd","popd","dirs","alias","unalias","hash","ulimit","umask","getopts","let","shopt","builtin","command","compgen","complete","disown","suspend","jobs","fg","bg","kill","history","fc","bind","enable","caller","type","printf","pwd","times","logout","mapfile","readarray","help","which","print","whence","where","autoload","zmodload","setopt","unsetopt","emulate","noglob","rehash","functions","bindkey","zle","vared","sched","limit","unlimit","zstyle","zparseopts","ttyctl"]),wt=new Set(["sh","bash","zsh","dash","ksh","fish","python","python3","python2","node","ruby","perl","php","awk","sed","deno","bun"]),$t="(?:dev|start|serve|watch|preview)",Ye=new RegExp(`^(?:\\S*/)?(?:bun\\s+(?:install|i|add|remove|rm|update|outdated|audit|test|build|pm|x)(?:\\s|$)|bun\\s+run\\s+(?!${$t}(?:\\s|$))[A-Za-z0-9:_-]+(?:\\s|$)|deno\\s+(?:test|lint|fmt|check|compile|install|info|doc|bench|outdated)(?:\\s|$)|deno\\s+task\\s+(?!${$t}(?:\\s|$))[A-Za-z0-9:_-]+(?:\\s|$)|php\\s+-l(?:\\s|$)|php\\s+(?:\\S*/)?(?:phpunit|pest|paratest|phpstan|pint|ecs|php-cs-fixer|psalm|rector|run-tests\\.php)(?:\\s|$)|php\\s+artisan\\s+(?!(?:serve|tinker|queue:work|queue:listen|horizon|octane:start|reverb:start|schedule:work|pail|dev)(?:\\s|$))\\S)`),Ve=/(?:^|\s)--watch(?:\s|=|$)/;function Xe(t){return Ye.test(t)&&!Ve.test(t)}var Qe=/^((?:\S*\/)?(sh|bash|zsh|dash|ksh|fish)\s+(-[elux]*c[elux]*)\s+)(['"])/,tn=/[()$]|(?:^|[;&|\n]\s*)(?:and|or|not|begin|end|if|else|switch|case|while|for|function|return)(?:\s|;|$)/,Ct=3;function Pt(t){let e=Qe.exec(t);if(!e)return null;let n=e[2]==="fish";if(n&&e[3]!=="-c")return null;let r=e[4],o=e[1].length+1,i=t.indexOf(r,o);if(i<=o)return null;let s=t.slice(i+1);if(s&&!/^\s/.test(s))return null;let c=t.slice(o,i);return r==='"'&&/[$`\\!]/.test(c)||n&&tn.test(c)?null:{script:c,start:o,end:i,quote:r}}var en=new Set(["cd","export","set","unset","source",".","true",":","pushd","popd"]);function nn(t){return G(t).filter(n=>{if(n.kind!=="cmd"||n.unsafe)return!1;let r=Nt(n.text);return r!==""&&!en.has(r)&&!ot.test(r)}).length>1}function rn(t,e,n,r,o){if(n>=Ct)return null;let i=Pt(t);if(!i)return null;let{script:s,start:c,end:u,quote:l}=i,a=[],p=Lt(s,e,n+1,r,g=>a.push(g));if(p===s||l==='"'&&/["$`]/.test(p))return null;let b=l==="'"?p.replace(/'/g,"'\\''"):p,d=`${t.slice(0,c)}${b}${t.slice(u)}`,y=nn(s);if(o)for(let g of y?[t,...a]:a)o(g);return y?`${e} ${d}`:d}var Ot=/(?:^|\s)(?:--follow|-[0-9a-zA-Z]*f[a-zA-Z]*)(?:\s|=|$)/,on=new Set(["docker","podman","kubectl","nerdctl","docker-compose","stern","flyctl"]);function sn(t,e){let n=W(e);return n==="tail"||n==="journalctl"?Ot.test(t):n==="stern"?!0:on.has(n)?/\blogs?\b/.test(t)&&Ot.test(t):!1}var ot=/^[A-Za-z_][A-Za-z0-9_]*\+?=/,cn=/^[A-Za-z_][A-Za-z0-9_-]*\s*\(\s*\)/,un=/(?:^|[;&|\n({])\s*[A-Za-z_][A-Za-z0-9_-]*\s*\(\s*\)/;function W(t){let e=t.lastIndexOf("/");return e===-1?t:t.slice(e+1)}function Ht(t){let e=new Array(t.length).fill(!1),n=[],r=0;for(;r<t.length;){let o=t[r],i=t[r+1],s=n[n.length-1];if(s==="'"){o==="'"&&n.pop(),r+=1;continue}if(o==="\\"){if(i===void 0)return null;r+=2;continue}if(s==="$'"){o==="'"&&n.pop(),r+=1;continue}if(o==="$"&&(i==="("||i==="{")){n.push(i==="("?")":"}"),r+=2;continue}if(o==="`"){s==="`"?n.pop():n.push("`"),r+=1;continue}if(s==='"'){o==='"'&&n.pop(),r+=1;continue}if(o==="$"&&i==="'"){n.push("$'"),r+=2;continue}if(o==="'"||o==='"'){n.push(o),r+=1;continue}if(o==="("||o==="{"&&s==="}"){n.push(o==="("?")":"}"),r+=1;continue}if(o===s){n.pop(),r+=1;continue}if(o===")"&&s===void 0)return null;e[r]=s===void 0,r+=1}return n.length===0?e:null}function vt(t){let e=Ht(t);if(!e)return!0;for(let n=0;n<t.length;n++)if(e[n]&&t[n]===">")return!0;return!1}function G(t){let e=Ht(t);if(!e)return[{kind:"cmd",text:t,unsafe:!0}];let n=[],r="",o=0,i=()=>{r.length>0&&(n.push({kind:"cmd",text:r}),r="")},s=(c,u)=>{i(),n.push({kind:"sep",text:c}),o+=u};for(;o<t.length;){let c=t[o],u=t[o+1];if(e[o]){if(c===">"&&u==="|"){r+=">|",o+=2;continue}if(c==="&"&&u==="&"){s("&&",2);continue}if(c==="&"&&u!==">"){let l=r.replace(/\s+$/,""),a=l[l.length-1];if(a!==">"&&a!=="<"&&a!=="&"){s("&",1);continue}}if(c==="|"&&u==="|"){s("||",2);continue}if(c==="|"&&u==="&"){s("|&",2);continue}if(c===";"&&u===";"){let l=t[o+2]==="&";s(l?";;&":";;",l?3:2);continue}if(c===";"||c===`
|
|
9
|
+
`||c==="|"){s(c,1);continue}}r+=c,o+=1}return i(),n}function Nt(t){return t.trim().split(/\s+/)[0]??""}function _(t,e=process.platform){try{let n=Tt(Ge(import.meta.url)),r=e==="win32",o=t&&t!=="claude-code"&&t!=="codex"&&!r?`TOKMIZER_HOST=${t} `:"";if(r&&(!t||t==="claude-code")){let c=f(n,"..","bin","tkr-exec.exe");if(B(c))return tt(c,e)?`'${c.replace(/\\/g,"/").replace(/'/g,"'\\''")}'`:null}let i=f(n,"..","bin","tkr-exec");if(!r&&B(i))return tt(i,e)?`${o}'${i.replace(/'/g,"'\\''")}'`:null;if(M()){let c=r?process.execPath.replace(/\\/g,"/"):process.execPath;return`${o}'${c.replace(/'/g,"'\\''")}' exec`}let s=f(n,"..","bin","tkr-exec.js");return B(s)?`${o}node '${s.replace(/'/g,"'\\''")}'`:null}catch{return null}}function It(t,e="tkr-exec",n=()=>!0,r){return Lt(t,e,0,n,r)}function Lt(t,e,n,r,o){if(t.includes("<<")||un.test(t))return t;let i=G(t),s=[];for(let c=0;c<i.length;c++){let u=i[c];if(u.kind==="sep"||u.unsafe){s.push(u.text);continue}let l=u.text.trim();if(!l){s.push(u.text);continue}let a=Nt(l),p=i[c+1],b=p?.kind==="sep"&&(p.text==="|"||p.text==="|&"),d=p?.kind==="sep"&&p.text==="&",y=!b&&!d&&!vt(u.text)&&!u.text.includes("<(")&&!u.text.includes(">(")?rn(l,e,n,r,o):null;if(y!==null){s.push(`${u.text.match(/^\s*/)?.[0]??""}${y}${u.text.match(/\s*$/)?.[0]??""}`);continue}if(l===e||l.startsWith(e)&&/^\s/.test(l.slice(e.length))||Ke.has(a)||qe.has(a)||(wt.has(a)||wt.has(W(a)))&&!Xe(l)||ot.test(a)||a.startsWith("(")||a.startsWith("#")||a.startsWith("<")||a.startsWith(">")||cn.test(l)||sn(l,a)||a.endsWith(")")||b||d||vt(u.text)||u.text.includes("<(")||u.text.includes(">(")||a==="node"&&l.includes("tkr-exec.js")||!r(a)){s.push(u.text);continue}let g=u.text.match(/^\s*/)?.[0]??"",U=u.text.match(/\s*$/)?.[0]??"";o?.(l),s.push(`${g}${e} ${l}${U}`)}return s.join("")}var ln=new Set(["command","workdir","timeout_ms","yield_time_ms","max_output_tokens","shell","login","tty","description"]),an=new Set(["cd","cat","head","tail","ls","tree","find","grep","rg","wc","nl","sort","cut","diff","stat","file","du","df","ps","jq","yq","pwd","which"]),fn=new Set(["log","diff","show","status","blame","ls-files","rev-parse","describe","shortlog"]),dn={find:/^-(?:delete|exec|execdir|ok|okdir|fprint|fls)/,sort:/^(?:-[^-]*o|--output|--compress-program)/,rg:/^(?:--pre|-[^-]*z|--search-zip)/,yq:/^(?:-[^-]*[is]|--inplace|--split-exp)/,file:/^(?:-C|--compile)/,tree:/^(?:-o|--output|-R$|-H$)/,git:/^(?:--output|--ext-diff|--textconv|-c$|--exec-path|--git-dir|--work-tree)/,eslint:/^--fix/},z=["test","run test","run build","run lint","run typecheck"],pn={npm:new Set(z),pnpm:new Set([...z,"build","lint","typecheck"]),yarn:new Set([...z,"build","lint","typecheck"]),bun:new Set(z),cargo:new Set(["test","build","check","clippy"]),go:new Set(["test","build","vet"]),mvn:new Set(["test","compile","verify"]),gradle:new Set(["test","build","check"]),dotnet:new Set(["test","build"])},mn=new Set(["pytest","tsc","vitest","jest","eslint","mypy"]);function jt(t,e,n=0){if(/\$|`|<\(|>\(|\\\r?\n/.test(t)||/>/.test(t.replace(/2>&1|[12]?>\s*\/dev\/null/g,"")))return!1;for(let r of G(t)){if(r.kind==="sep")continue;if(r.unsafe)return!1;let o=n<Ct?Pt(r.text.trim()):null;if(o){if(!jt(o.script,e,n+1))return!1;continue}let i=r.text.trim().split(/\s+/).filter(Boolean).map(l=>l.replace(/['"\\]/g,""));if(i.length===0)continue;if(i.some(l=>ot.test(l)))return!1;let s=W(i[0]);if(s!==i[0])return!1;let c=dn[s];if(c&&i.slice(1).some(l=>c.test(l)))return!1;if((s==="git"?fn.has(i[1]??""):an.has(s))||e&&mn.has(s))continue;let u=e?pn[s]:void 0;if(!(u&&(u.has(i[1]??"")||i[1]==="run"&&u.has(`run ${i[2]??""}`))))return!1}return!0}function st(t,e,n=_(e),r=k,o=K(),i=process.env.PATH){if(!h(t)||t.tool_name!=="Bash")return null;let s=t.tool_input;if(!h(s)||s.run_in_background)return null;let c=s.command;if(typeof c!="string"||!c||e==="codex"&&Object.keys(s).some(d=>!ln.has(d)))return null;let u=typeof t.cwd=="string"&&t.cwd?t.cwd:s.workdir,l=typeof u=="string"&&u?u:void 0,a=typeof t.session_id=="string"&&t.session_id?t.session_id:"unknown",p=typeof t.permission_mode=="string"?t.permission_mode:void 0,b=x(c,{host:e,sessionId:a,cwd:l,claudeDirs:o,pathEnv:i,permissionMode:p},n,r);return b?JSON.stringify({hookSpecificOutput:e==="codex"?{hookEventName:"PreToolUse",permissionDecision:"allow",updatedInput:{...s,command:b}}:{hookEventName:"PreToolUse",updatedInput:{...s,command:b}}}):null}function gn(t,e){if(!B(t))return"missing";try{let n=nt(t,"utf8");return JSON.parse(e?Ft(n):n)}catch{return"unreadable"}}function Ft(t){let e="",n=!1;for(let r=0;r<t.length;r++){let o=t[r];if(n){e+=o,o==="\\"?e+=t[++r]??"":o==='"'&&(n=!1);continue}if(o==='"')n=!0,e+=o;else if(o==="/"&&t[r+1]==="/"){for(;r<t.length&&t[r]!==`
|
|
10
10
|
`;)r++;e+=`
|
|
11
|
-
`}else if(o==="/"&&t[r+1]==="*"){if(r=t.indexOf("*/",r+2),r===-1)return"";r+=1}else e+=o}return e.replace(/,(\s*[}\]])/g,"$1")}function Z(t){let e=[],n=Ze(t);for(;;){e.push(n);let r=Tt(n);if(r===n)return e;n=r}}function hn(){let t=process.platform==="darwin"?"/Library/Application Support/ClaudeCode":process.platform==="win32"?"C:\\Program Files\\ClaudeCode":"/etc/claude-code",e=[f(t,"managed-settings.json")];try{for(let n of rt(f(t,"managed-settings.d")))n.endsWith(".json")&&e.push(f(t,"managed-settings.d",n))}catch{return e}return e}function F(t,e,n,r){let o=[];for(let i of t){let s=gn(i,e);if(s!=="missing"&&(s==="unreadable"||!h(s)||!n(s,o)))return"unreadable"}return{matchers:o,complete:r}}function j(t){return t===void 0?[]:Array.isArray(t)?t.filter(e=>typeof e=="string"):null}function K(){return{projectDir:process.env.CLAUDE_PROJECT_DIR,configDir:process.env.CLAUDE_CONFIG_DIR}}function kn(t,e){let n=d=>[f(d,"settings.json"),f(d,"settings.local.json")],r=d=>n(f(d,".claude")),o=f(A(),".claude"),i=[[hn(),"managed"],[n(e.configDir||o),"loaded"],[e.configDir?n(o):[],"extra"],[e.projectDir?r(e.projectDir):[],"project"],[Z(t).flatMap(r),"extra"]],s=[],c={managed:!1,loaded:!1,project:!1,extra:!1},u=!1,l="managed",a=(d,y)=>{let g=d.permissions;if(l==="managed"&&(d.allowManagedPermissionRulesOnly===!0||h(g)&&g.allowManagedPermissionRulesOnly===!0)&&(u=!0),g===void 0)return!0;if(!h(g))return!1;for(let U of["deny","ask","allow"]){let lt=j(g[U]);if(!lt)return!1;for(let Gt of lt){let I=/^\s*Bash\s*(?:\(([\s\S]*)\))?\s*$/.exec(Gt);I&&(U!=="allow"?y.push(O((I[1]??"*").replace(/:\*$/," *"))):I[1]===void 0||I[1].trim()==="*"?c[l]=!0:s.push(O(I[1].replace(/:\*$/," *"))))}}return!0},p=[];for(let[d,y]of i){l=y;let g=F(d,!1,a,!0);if(g==="unreadable")return"unreadable";p.push(...g.matchers)}let b=c.managed||c.loaded&&!u;return{matchers:[...p,...b?[]:s],complete:!0,allow:s,allowsEverything:b}}function _n(t){let e=[f(A(),".cursor","cli-config.json"),...Z(t).map(n=>f(n,".cursor","cli.json"))];return F(e,!0,(n,r)=>{let o=n.permissions;if(o===void 0)return!0;if(!h(o))return!1;let i=j(o.deny);if(!i)return!1;for(let s of i){let c=/^\s*Shell\s*(?:\(([\s\S]*)\))?\s*$/.exec(s);if(!c)continue;let u=c[1]??"*",l=u.indexOf(":");r.push(O(l===-1?`${u} *`:`${u.slice(0,l)} ${u.slice(l+1)}`))}return!0},!1)}function bn(){return process.platform==="darwin"?"/Library/Application Support/GeminiCli":process.platform==="win32"?"C:\\ProgramData\\gemini-cli":"/etc/gemini-cli"}var Dt=/^\s*(?:run_shell_command|ShellTool)\s*(?:\(([\s\S]*)\))?\s*$/;function Sn(t,e){let n=t.tools;if(n!==void 0&&!h(n))return!1;let r=[j(t.excludeTools),j(h(n)?n.exclude:void 0)];for(let o of r){if(!o)return!1;for(let i of o){let s=Dt.exec(i);s&&e.push(O(s[1]===void 0?"*":`${s[1]}*`))}}return!0}function Et(t){try{return JSON.parse(t)}catch{return null}}function yn(t){let e=t.trim();if(e.startsWith('"""')||e.startsWith("'''"))return null;if(e.startsWith("[")){if(!/\]\s*(?:#.*)?$/.test(e))return null;let n=[];for(let r of e.matchAll(/"((?:\\.|[^"\\])*)"|'([^']*)'/g)){let o=r[2]??Et(`"${r[1]}"`);if(o===null)return null;n.push(o)}return n}if(e.startsWith('"')){let n=/^"((?:\\.|[^"\\])*)"/.exec(e);return n?Et(`"${n[1]}"`):null}if(e.startsWith("'")){let n=/^'([^']*)'/.exec(e);return n?n[1]:null}return e.replace(/\s*#.*$/,"")}function wn(t,e){for(let n of t.split(/^\s*\[\[\s*rule\s*\]\]\s*$/m).slice(1)){let r={};for(let u of n.split(/\r?\n/)){if(/^\s*\[/.test(u))break;let l=/^\s*([A-Za-z_]+)\s*=\s*(.*)$/.exec(u);if(!l)continue;let a=yn(l[2]);if(a===null)return!1;r[l[1]]=a}let o=r.decision;if(o!=="deny"&&o!=="ask_user"||!(r.toolName===void 0?["*"]:[r.toolName].flat()).some(u=>u==="*"||Dt.test(u)))continue;let s=r.commandPrefix===void 0?[]:[r.commandPrefix].flat(),c=[r.commandRegex,r.argsPattern].flat().filter(u=>typeof u=="string");if(s.length===0&&c.length===0){e.push(()=>!0);continue}for(let u of s)e.push(O(`${u}*`));for(let u of c){let l;try{l=new RegExp(u)}catch{return!1}e.push(a=>l.test(JSON.stringify({command:a}))||it(l,a))}}return!0}function At(t){try{return rt(t).filter(e=>e.endsWith(".toml")).map(e=>f(t,e))}catch{return[]}}function xn(t){let e=bn(),n=[f(e,"system-defaults.json"),f(e,"settings.json"),process.env.GEMINI_CLI_SYSTEM_SETTINGS_PATH,process.env.GEMINI_CLI_SYSTEM_DEFAULTS_PATH,f(A(),".gemini","settings.json"),...Z(t).map(o=>f(o,".gemini","settings.json"))].filter(o=>!!o),r=F(n,!0,Sn,!1);if(r==="unreadable")return r;for(let o of[...At(f(A(),".gemini","policies")),...At(f(e,"policies"))]){let i;try{i=nt(o,"utf8")}catch{return"unreadable"}if(!wn(i,r.matchers))return"unreadable"}return r}function et(t,e){if(t===void 0)return!0;if(typeof t=="string")return t!=="allow"&&e.push(()=>!0),!0;if(!h(t))return!1;for(let n of["bash","shell","*"]){let r=t[n];if(r!==void 0){if(typeof r=="string"){r!=="allow"&&e.push(()=>!0);continue}if(!h(r))return!1;for(let[o,i]of Object.entries(r))i!=="allow"&&e.push(O(o.replace(/\?/g,"*")))}}return!0}function Rt(t,e){if(t.permissions!==void 0||!et(t.permission,e))return!1;for(let n of["agent","agents","mode"]){let r=t[n];if(r!==void 0){if(!h(r))return!1;for(let o of Object.values(r))if(h(o)&&(o.permissions!==void 0||!et(o.permission,e)))return!1}}return!0}function $n(t){for(let e of t)for(let n of["agent","agents","mode","modes"]){let r;try{r=rt(f(e,n))}catch{continue}for(let o of r)try{if(o.endsWith(".md")&&/permission/i.test(nt(f(e,n,o),"utf8")))return!0}catch{return!0}}return!1}function On(t){let e=f(process.env.XDG_CONFIG_HOME||f(A(),".config"),"opencode"),n=Z(t),r=[e,...n.map(c=>f(c,".opencode"))];if($n(r))return"unreadable";let o=["opencode.json","opencode.jsonc","config.json"],i=[...o.map(c=>f(e,c)),process.env.OPENCODE_CONFIG,...n.flatMap(c=>[...o.slice(0,2).map(u=>f(c,u)),...o.slice(0,2).map(u=>f(c,".opencode",u))])].filter(c=>!!c),s=F(i,!0,Rt,!0);if(s==="unreadable")return s;for(let c of[process.env.OPENCODE_CONFIG_CONTENT,process.env.OPENCODE_PERMISSION]){if(!c)continue;let u;try{u=JSON.parse(Ft(c))}catch{return"unreadable"}if(!(c===process.env.OPENCODE_PERMISSION?et(u,s.matchers):h(u)&&Rt(u,s.matchers)))return"unreadable"}return s}function vn(){return F([f(A(),".gemini","antigravity-cli","settings.json")],!0,(t,e)=>{let n=t.permissions;if(n===void 0)return!0;if(!h(n))return!1;for(let r of["deny","ask"]){let o=j(n[r]);if(!o)return!1;for(let i of o){let s=/^\s*command\s*\(([\s\S]*)\)\s*$/.exec(i);if(!s)continue;let c=s[1].trim();if(!c.startsWith("regex:")){e.push(O(c==="*"?"*":`${c} *`));continue}let u;try{u=new RegExp(c.slice(6))}catch{return!1}e.push(l=>it(u,l))}}return!0},!1)}function h(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function En(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function An(t){let e=t.trim().replace(/\s+/g," "),n=e.endsWith(" *")&&e.length>2,r=(n?e.slice(0,-2):e).split("*").map(En).join("[\\s\\S]*");return new RegExp(`^${r}${n?"(?: [\\s\\S]*)?":""}(?= |$)`)}var Rn=512;function Tn(t){let e=t.replace(/['"\\]/g,"").split(/[\s;&|(){}<>`$]+/).filter(Boolean);return[e,e.map(W)]}function it(t,e){if(t.test(e)||t.test(e.trim().replace(/\s+/g," ")))return!0;for(let n of Tn(e)){if(n.length>Rn)return!0;for(let r=0;r<n.length;r++)if(t.test(n.slice(r).join(" ")))return!0}return!1}function O(t){let e=An(t);return n=>it(e,n)}var Cn={"claude-code":kn,cursor:_n,"gemini-cli":xn,opencode:On,antigravity:vn},Pn=new Set(["codex"]),Hn=/(?:^|[\s=:/~])\.(?:git|claude|vscode|idea|husky)(?=[/\s;|&)]|$)|(?:^|[\s=:/~])\.(?:bashrc|bash_profile|bash_login|bash_logout|profile|zshrc|zprofile|zshenv|zlogin|zlogout|gitconfig|gitmodules|ripgreprc|mcp\.json)(?![A-Za-z0-9_])/;function Nn(t,e,n){let r=Cn[t];return r?r(e??process.cwd(),n):{matchers:[],complete:!1}}function In(t,e,n){return t==="unreadable"||Hn.test(e.replace(/['"\\]/g,""))||t.matchers.some(r=>r(e))?!1:t.complete||jt(e,!Pn.has(n))}var Ln=new Set(["default","acceptEdits","dontAsk"]);function jn(t,e){return e.host!=="claude-code"||e.permissionMode==="bypassPermissions"?!0:t!=="unreadable"&&t.allowsEverything===!0&&Ln.has(e.permissionMode??"")}function Fn(t,e){let n="pathEnv"in t?t.pathEnv:process.env.PATH,r;return o=>t.host!=="claude-code"?E(o,t.cwd,n):(r??=bt(e.configDir),!E(o,t.cwd,n)&&!r.paths.some(i=>E(o,t.cwd,i))?!1:o.includes("/")||!St(o,r))}function w(t,e,n,r){if(!n||/["\\$`;&|<>(){}]|'\\''/.test(n))return null;let o=e.claudeDirs??K(),i=Nn(e.host,e.cwd,o),s=In(i,t,e.host)?It(t,n,Fn(e,o)):t;return s!==t&&i!=="unreadable"&&(i.matchers.some(c=>c(s))||(i.allow??[]).some(c=>c(s)&&!c(t)))&&(s=t),r(e.sessionId,s!==t),s===t||!jn(i,e)?null:s}function m(t,e){let n=t[e];return typeof n=="string"&&n?n:null}function D(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function Dn(t){return m(t,"prompt_id")||m(t,"scratchpad_dir")?!0:D(t.effort)}function Un(t){return m(t,"tool_use_id")?.startsWith("toolu_")===!0||/^claude/i.test(m(t,"model")??"")}var Mn=new Set(["BeforeTool","AfterTool","BeforeAgent","AfterAgent","BeforeModel","AfterModel","BeforeToolSelection"]),Jn=new Set(["preToolUse","postToolUse","beforeShellExecution","afterShellExecution","beforeSubmitPrompt","sessionStart","sessionEnd","stop"]),zn=new Set(["pre_tool","post_tool"]),Bn=new Set(["before_tool_call","after_tool_call"]),Wn=new Set(["runTerminalCommand","run_in_terminal"]),Gn=[["codex",t=>m(t,"turn_id")!==null],["claude-code",Dn],["cursor",t=>m(t,"cursor_version")!==null||m(t,"generation_id")!==null||m(t,"conversation_id")!==null||Array.isArray(t.workspace_roots)||Jn.has(m(t,"hook_event_name")??"")],["gemini-cli",t=>Mn.has(m(t,"hook_event_name")??"")],["qwen-code",t=>t.hook_event_name==="PreToolUse"&&t.tool_name==="run_shell_command"],["vibe",t=>zn.has(m(t,"hook_event_name")??"")],["antigravity",t=>D(t.toolCall)],["copilot",t=>m(t,"toolName")!==null&&"toolArgs"in t||Wn.has(m(t,"tool_name")??"")],["grok",t=>t.toolName==="run_terminal_command"||t.tool_name==="run_terminal_command"||D(t.toolInput)&&!("toolArgs"in t)],["droid",t=>t.tool_name==="Execute"],["trae",t=>t.tool_name==="RunCommand"],["crush",t=>t.hook_event_name===void 0&&m(t,"event")!==null&&!Bn.has(m(t,"event"))]],Zn=new Set(["codex","antigravity"]);function Kn(t){return D(t)?Gn.filter(([,e])=>e(t)).map(([e])=>e):[]}function Ut(t,e){if(!D(t))return null;let n=Kn(t);return n.length>1?null:n.length===1?n[0]:Un(t)&&Zn.has(e)?"claude-code":e==="codex"?null:e}var ct=JSON.stringify({permission:"allow"});function Mt(t,e=_("cursor"),n=k){if(t.tool_name!=="Shell")return ct;let r=t.tool_input??{},o=r.command;if(typeof o!="string"||!o)return ct;let i=typeof r.working_directory=="string"?r.working_directory:void 0,s=w(o,{host:"cursor",sessionId:t.conversation_id??"unknown",cwd:i},S()?null:e,n);return s?JSON.stringify({permission:"allow",updated_input:{...r,command:s}}):ct}var q="{}";function Jt(t,e=_("gemini-cli"),n=k){if(t.hook_event_name!=="BeforeTool"||t.tool_name!=="run_shell_command")return q;let r=t.tool_input??{};if(r.is_background)return q;let o=r.command;if(typeof o!="string"||!o)return q;let i=typeof t.cwd=="string"?t.cwd:void 0,s=w(o,{host:"gemini-cli",sessionId:t.session_id??"unknown",cwd:i},S()?null:e,n);return s?JSON.stringify({hookSpecificOutput:{tool_input:{command:s}}}):q}function T(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}var Lo=String.raw`(?:'(?:(?:[^']|'\\'')*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?'|"(?:[^"]*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?"|(?:[^\s'"]*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?)`;function C(t,e,n="unknown",r=_(e),o=k){return t?w(t,{host:e,sessionId:n},S()?null:r,o):null}var Yn=new Set(["bash","file_system.bash"]),ut=JSON.stringify({decision:"allow"});function zt(t,e=(n,r)=>C(n,"vibe",r)){if(!T(t)||t.hook_event_name!=="pre_tool"||typeof t.tool_name!="string"||!Yn.has(t.tool_name))return"";let n=t.tool_input;if(!T(n)||typeof n.command!="string"||!n.command)return"";let r=e(n.command,typeof t.session_id=="string"?t.session_id:"unknown");return r?JSON.stringify({hook_specific_output:{tool_input:{...n,command:r}}}):""}function Bt(t,e=(n,r)=>C(n,"antigravity",r)){if(!T(t)||!T(t.toolCall)||t.toolCall.name!=="run_command")return ut;let n=t.toolCall.args;if(!T(n)||typeof n.CommandLine!="string"||!n.CommandLine)return ut;let r=e(n.CommandLine,typeof t.conversationId=="string"?t.conversationId:"unknown");return r?JSON.stringify({decision:"allow",overwrite:{CommandLine:r}}):ut}var x="{}",Vn=new Set(["runTerminalCommand","run_in_terminal","Bash","bash"]),Xn=["run_in_background","is_background","isBackground"];function Y(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function Qn(t){let e=t.session_id??t.sessionId;return typeof e=="string"&&e?e:"unknown"}function tr(t,e){let n=t.cwd??e.cwd??e.workdir??e.working_directory;return typeof n=="string"&&n?n:void 0}function V(t,e,n,r,o){if(!Y(e)||Xn.some(s=>e[s]===!0))return null;let i=e.command;return typeof i!="string"||!i?null:w(i,{host:t,sessionId:Qn(n),cwd:tr(n,e)},S()?null:r,o)}function H(t,e){return JSON.stringify({hookSpecificOutput:{hookEventName:"PreToolUse",updatedInput:{...t,command:e}}})}function er(t){if(Y(t))return t;if(typeof t!="string")return null;try{let e=JSON.parse(t);return Y(e)?e:null}catch{return null}}function nr(t,e,n){if(typeof t.tool_name=="string"){if(!Vn.has(t.tool_name))return x;let i=t.tool_input,s=V("copilot",i,t,e,n);return s?H(i,s):x}if(t.toolName!=="bash")return x;let r=er(t.toolArgs),o=V("copilot",r,t,e,n);return o?JSON.stringify({modifiedArgs:{...r,command:o}}):x}function P(t,e,n){return(r,o,i)=>{if(r.tool_name!==e)return x;let s=r.tool_input,c=V(t,s,r,o,i);return c?n(s,c):x}}var rr=new Set(["run_terminal_command","Bash","bash"]);function or(t,e,n){let r=t.toolName??t.tool_name;if(typeof r!="string"||!rr.has(r))return x;let o=t.toolInput??t.tool_input,i=V("grok",o,t,e,n);return i?H(o,i):x}var sr={copilot:nr,droid:P("droid","Execute",H),trae:P("trae","RunCommand",H),"qwen-code":P("qwen-code","run_shell_command",H),crush:P("crush","bash",(t,e)=>JSON.stringify({updated_input:{command:e}})),junie:P("junie","Bash",(t,e)=>JSON.stringify({updatedInput:{...t,command:e}})),zcode:P("zcode","Bash",H),grok:or};function Wt(t,e,n=_(t),r=k){return Y(e)?sr[t](e,n,r):x}function N(t){return S()?null:_(t)}function ir(t,e,n={}){let r=n.recordUsage??k;switch(t){case"claude-code":case"codex":return e.hook_event_name!==void 0&&e.hook_event_name!=="PreToolUse"?"":st(e,t,N(t),r,n.claudeDirs??K(),n.pathEnv??process.env.PATH)??"";case"cursor":return Mt(e,N(t),r);case"gemini-cli":return Jt(e,N(t),r);case"vibe":return zt(e,(o,i)=>C(o,"vibe",i,N(t),r));case"antigravity":return Bt(e,(o,i)=>C(o,"antigravity",i,N(t),r));default:return Wt(t,e,N(t),r)}}function R(t,e,n={}){let r=Ut(t,e);return r?ir(r,t,n):n.fallback??""}function cr(t){let e;try{e=JSON.parse(t)}catch{return""}return R(e,"claude-code")}async function ur(){let t=await v(),e=t?cr(t):"";e&&process.stdout.write(e)}var lr=process.argv[1]??"";dt(lr,import.meta.url,"pre-tool-use")&&ur().catch(t=>{process.stderr.write(`tkr pre-tool-use: ${t.message}
|
|
11
|
+
`}else if(o==="/"&&t[r+1]==="*"){if(r=t.indexOf("*/",r+2),r===-1)return"";r+=1}else e+=o}return e.replace(/,(\s*[}\]])/g,"$1")}function Z(t){let e=[],n=Ze(t);for(;;){e.push(n);let r=Tt(n);if(r===n)return e;n=r}}function hn(){let t=process.platform==="darwin"?"/Library/Application Support/ClaudeCode":process.platform==="win32"?"C:\\Program Files\\ClaudeCode":"/etc/claude-code",e=[f(t,"managed-settings.json")];try{for(let n of rt(f(t,"managed-settings.d")))n.endsWith(".json")&&e.push(f(t,"managed-settings.d",n))}catch{return e}return e}function F(t,e,n,r){let o=[];for(let i of t){let s=gn(i,e);if(s!=="missing"&&(s==="unreadable"||!h(s)||!n(s,o)))return"unreadable"}return{matchers:o,complete:r}}function j(t){return t===void 0?[]:Array.isArray(t)?t.filter(e=>typeof e=="string"):null}function K(){return{projectDir:process.env.CLAUDE_PROJECT_DIR,configDir:process.env.CLAUDE_CONFIG_DIR}}function kn(t,e){let n=d=>[f(d,"settings.json"),f(d,"settings.local.json")],r=d=>n(f(d,".claude")),o=f(A(),".claude"),i=[[hn(),"managed"],[n(e.configDir||o),"loaded"],[e.configDir?n(o):[],"extra"],[e.projectDir?r(e.projectDir):[],"project"],[Z(t).flatMap(r),"extra"]],s=[],c={managed:!1,loaded:!1,project:!1,extra:!1},u=!1,l="managed",a=(d,y)=>{let g=d.permissions;if(l==="managed"&&(d.allowManagedPermissionRulesOnly===!0||h(g)&&g.allowManagedPermissionRulesOnly===!0)&&(u=!0),g===void 0)return!0;if(!h(g))return!1;for(let U of["deny","ask","allow"]){let lt=j(g[U]);if(!lt)return!1;for(let Gt of lt){let I=/^\s*Bash\s*(?:\(([\s\S]*)\))?\s*$/.exec(Gt);I&&(U!=="allow"?y.push(O((I[1]??"*").replace(/:\*$/," *"))):I[1]===void 0||I[1].trim()==="*"?c[l]=!0:s.push(O(I[1].replace(/:\*$/," *"))))}}return!0},p=[];for(let[d,y]of i){l=y;let g=F(d,!1,a,!0);if(g==="unreadable")return"unreadable";p.push(...g.matchers)}let b=c.managed||c.loaded&&!u;return{matchers:[...p,...b?[]:s],complete:!0,allow:s,allowsEverything:b}}function _n(t){let e=[f(A(),".cursor","cli-config.json"),...Z(t).map(n=>f(n,".cursor","cli.json"))];return F(e,!0,(n,r)=>{let o=n.permissions;if(o===void 0)return!0;if(!h(o))return!1;let i=j(o.deny);if(!i)return!1;for(let s of i){let c=/^\s*Shell\s*(?:\(([\s\S]*)\))?\s*$/.exec(s);if(!c)continue;let u=c[1]??"*",l=u.indexOf(":");r.push(O(l===-1?`${u} *`:`${u.slice(0,l)} ${u.slice(l+1)}`))}return!0},!1)}function bn(){return process.platform==="darwin"?"/Library/Application Support/GeminiCli":process.platform==="win32"?"C:\\ProgramData\\gemini-cli":"/etc/gemini-cli"}var Dt=/^\s*(?:run_shell_command|ShellTool)\s*(?:\(([\s\S]*)\))?\s*$/;function Sn(t,e){let n=t.tools;if(n!==void 0&&!h(n))return!1;let r=[j(t.excludeTools),j(h(n)?n.exclude:void 0)];for(let o of r){if(!o)return!1;for(let i of o){let s=Dt.exec(i);s&&e.push(O(s[1]===void 0?"*":`${s[1]}*`))}}return!0}function Et(t){try{return JSON.parse(t)}catch{return null}}function yn(t){let e=t.trim();if(e.startsWith('"""')||e.startsWith("'''"))return null;if(e.startsWith("[")){if(!/\]\s*(?:#.*)?$/.test(e))return null;let n=[];for(let r of e.matchAll(/"((?:\\.|[^"\\])*)"|'([^']*)'/g)){let o=r[2]??Et(`"${r[1]}"`);if(o===null)return null;n.push(o)}return n}if(e.startsWith('"')){let n=/^"((?:\\.|[^"\\])*)"/.exec(e);return n?Et(`"${n[1]}"`):null}if(e.startsWith("'")){let n=/^'([^']*)'/.exec(e);return n?n[1]:null}return e.replace(/\s*#.*$/,"")}function xn(t,e){for(let n of t.split(/^\s*\[\[\s*rule\s*\]\]\s*$/m).slice(1)){let r={};for(let u of n.split(/\r?\n/)){if(/^\s*\[/.test(u))break;let l=/^\s*([A-Za-z_]+)\s*=\s*(.*)$/.exec(u);if(!l)continue;let a=yn(l[2]);if(a===null)return!1;r[l[1]]=a}let o=r.decision;if(o!=="deny"&&o!=="ask_user"||!(r.toolName===void 0?["*"]:[r.toolName].flat()).some(u=>u==="*"||Dt.test(u)))continue;let s=r.commandPrefix===void 0?[]:[r.commandPrefix].flat(),c=[r.commandRegex,r.argsPattern].flat().filter(u=>typeof u=="string");if(s.length===0&&c.length===0){e.push(()=>!0);continue}for(let u of s)e.push(O(`${u}*`));for(let u of c){let l;try{l=new RegExp(u)}catch{return!1}e.push(a=>l.test(JSON.stringify({command:a}))||it(l,a))}}return!0}function At(t){try{return rt(t).filter(e=>e.endsWith(".toml")).map(e=>f(t,e))}catch{return[]}}function wn(t){let e=bn(),n=[f(e,"system-defaults.json"),f(e,"settings.json"),process.env.GEMINI_CLI_SYSTEM_SETTINGS_PATH,process.env.GEMINI_CLI_SYSTEM_DEFAULTS_PATH,f(A(),".gemini","settings.json"),...Z(t).map(o=>f(o,".gemini","settings.json"))].filter(o=>!!o),r=F(n,!0,Sn,!1);if(r==="unreadable")return r;for(let o of[...At(f(A(),".gemini","policies")),...At(f(e,"policies"))]){let i;try{i=nt(o,"utf8")}catch{return"unreadable"}if(!xn(i,r.matchers))return"unreadable"}return r}function et(t,e){if(t===void 0)return!0;if(typeof t=="string")return t!=="allow"&&e.push(()=>!0),!0;if(!h(t))return!1;for(let n of["bash","shell","*"]){let r=t[n];if(r!==void 0){if(typeof r=="string"){r!=="allow"&&e.push(()=>!0);continue}if(!h(r))return!1;for(let[o,i]of Object.entries(r))i!=="allow"&&e.push(O(o.replace(/\?/g,"*")))}}return!0}function Rt(t,e){if(t.permissions!==void 0||!et(t.permission,e))return!1;for(let n of["agent","agents","mode"]){let r=t[n];if(r!==void 0){if(!h(r))return!1;for(let o of Object.values(r))if(h(o)&&(o.permissions!==void 0||!et(o.permission,e)))return!1}}return!0}function $n(t){for(let e of t)for(let n of["agent","agents","mode","modes"]){let r;try{r=rt(f(e,n))}catch{continue}for(let o of r)try{if(o.endsWith(".md")&&/permission/i.test(nt(f(e,n,o),"utf8")))return!0}catch{return!0}}return!1}function On(t){let e=f(process.env.XDG_CONFIG_HOME||f(A(),".config"),"opencode"),n=Z(t),r=[e,...n.map(c=>f(c,".opencode"))];if($n(r))return"unreadable";let o=["opencode.json","opencode.jsonc","config.json"],i=[...o.map(c=>f(e,c)),process.env.OPENCODE_CONFIG,...n.flatMap(c=>[...o.slice(0,2).map(u=>f(c,u)),...o.slice(0,2).map(u=>f(c,".opencode",u))])].filter(c=>!!c),s=F(i,!0,Rt,!0);if(s==="unreadable")return s;for(let c of[process.env.OPENCODE_CONFIG_CONTENT,process.env.OPENCODE_PERMISSION]){if(!c)continue;let u;try{u=JSON.parse(Ft(c))}catch{return"unreadable"}if(!(c===process.env.OPENCODE_PERMISSION?et(u,s.matchers):h(u)&&Rt(u,s.matchers)))return"unreadable"}return s}function vn(){return F([f(A(),".gemini","antigravity-cli","settings.json")],!0,(t,e)=>{let n=t.permissions;if(n===void 0)return!0;if(!h(n))return!1;for(let r of["deny","ask"]){let o=j(n[r]);if(!o)return!1;for(let i of o){let s=/^\s*command\s*\(([\s\S]*)\)\s*$/.exec(i);if(!s)continue;let c=s[1].trim();if(!c.startsWith("regex:")){e.push(O(c==="*"?"*":`${c} *`));continue}let u;try{u=new RegExp(c.slice(6))}catch{return!1}e.push(l=>it(u,l))}}return!0},!1)}function h(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function En(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function An(t){let e=t.trim().replace(/\s+/g," "),n=e.endsWith(" *")&&e.length>2,r=(n?e.slice(0,-2):e).split("*").map(En).join("[\\s\\S]*");return new RegExp(`^${r}${n?"(?: [\\s\\S]*)?":""}(?= |$)`)}var Rn=512;function Tn(t){let e=t.replace(/['"\\]/g,"").split(/[\s;&|(){}<>`$]+/).filter(Boolean);return[e,e.map(W)]}function it(t,e){if(t.test(e)||t.test(e.trim().replace(/\s+/g," ")))return!0;for(let n of Tn(e)){if(n.length>Rn)return!0;for(let r=0;r<n.length;r++)if(t.test(n.slice(r).join(" ")))return!0}return!1}function O(t){let e=An(t);return n=>it(e,n)}var Cn={"claude-code":kn,cursor:_n,"gemini-cli":wn,opencode:On,antigravity:vn},Pn=new Set(["codex"]),Hn=/(?:^|[\s=:/~])\.(?:git|claude|vscode|idea|husky)(?=[/\s;|&)]|$)|(?:^|[\s=:/~])\.(?:bashrc|bash_profile|bash_login|bash_logout|profile|zshrc|zprofile|zshenv|zlogin|zlogout|gitconfig|gitmodules|ripgreprc|mcp\.json)(?![A-Za-z0-9_])/;function Nn(t,e,n){let r=Cn[t];return r?r(e??process.cwd(),n):{matchers:[],complete:!1}}function In(t,e,n){return t==="unreadable"||Hn.test(e.replace(/['"\\]/g,""))||t.matchers.some(r=>r(e))?!1:t.complete||jt(e,!Pn.has(n))}var Ln=new Set(["default","acceptEdits","dontAsk"]);function jn(t,e){return e.host!=="claude-code"||e.permissionMode==="bypassPermissions"?!0:t!=="unreadable"&&t.allowsEverything===!0&&Ln.has(e.permissionMode??"")}function Fn(t,e){let n="pathEnv"in t?t.pathEnv:process.env.PATH,r;return o=>t.host!=="claude-code"?E(o,t.cwd,n):(r??=bt(e.configDir),!E(o,t.cwd,n)&&!r.paths.some(i=>E(o,t.cwd,i))?!1:o.includes("/")||!St(o,r))}function x(t,e,n,r){if(!n||/["\\$`;&|<>(){}]|'\\''/.test(n))return null;let o=e.claudeDirs??K(),i=Nn(e.host,e.cwd,o),s=In(i,t,e.host)?It(t,n,Fn(e,o)):t;return s!==t&&i!=="unreadable"&&(i.matchers.some(c=>c(s))||(i.allow??[]).some(c=>c(s)&&!c(t)))&&(s=t),r(e.sessionId,s!==t),s===t||!jn(i,e)?null:s}function m(t,e){let n=t[e];return typeof n=="string"&&n?n:null}function D(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function Dn(t){return m(t,"prompt_id")||m(t,"scratchpad_dir")?!0:D(t.effort)}function Un(t){return m(t,"tool_use_id")?.startsWith("toolu_")===!0||/^claude/i.test(m(t,"model")??"")}var Mn=new Set(["BeforeTool","AfterTool","BeforeAgent","AfterAgent","BeforeModel","AfterModel","BeforeToolSelection"]),Jn=new Set(["preToolUse","postToolUse","beforeShellExecution","afterShellExecution","beforeSubmitPrompt","sessionStart","sessionEnd","stop"]),zn=new Set(["pre_tool","post_tool"]),Bn=new Set(["before_tool_call","after_tool_call"]),Wn=new Set(["runTerminalCommand","run_in_terminal"]),Gn=[["codex",t=>m(t,"turn_id")!==null],["claude-code",Dn],["cursor",t=>m(t,"cursor_version")!==null||m(t,"generation_id")!==null||m(t,"conversation_id")!==null||Array.isArray(t.workspace_roots)||Jn.has(m(t,"hook_event_name")??"")],["gemini-cli",t=>Mn.has(m(t,"hook_event_name")??"")],["qwen-code",t=>t.hook_event_name==="PreToolUse"&&t.tool_name==="run_shell_command"],["vibe",t=>zn.has(m(t,"hook_event_name")??"")],["antigravity",t=>D(t.toolCall)],["copilot",t=>m(t,"toolName")!==null&&"toolArgs"in t||Wn.has(m(t,"tool_name")??"")],["grok",t=>t.toolName==="run_terminal_command"||t.tool_name==="run_terminal_command"||D(t.toolInput)&&!("toolArgs"in t)],["droid",t=>t.tool_name==="Execute"],["trae",t=>t.tool_name==="RunCommand"],["crush",t=>t.hook_event_name===void 0&&m(t,"event")!==null&&!Bn.has(m(t,"event"))]],Zn=new Set(["codex","antigravity"]);function Kn(t){return D(t)?Gn.filter(([,e])=>e(t)).map(([e])=>e):[]}function Ut(t,e){if(!D(t))return null;let n=Kn(t);return n.length>1?null:n.length===1?n[0]:Un(t)&&Zn.has(e)?"claude-code":e==="codex"?null:e}var ct=JSON.stringify({permission:"allow"});function Mt(t,e=_("cursor"),n=k){if(t.tool_name!=="Shell")return ct;let r=t.tool_input??{},o=r.command;if(typeof o!="string"||!o)return ct;let i=typeof r.working_directory=="string"?r.working_directory:void 0,s=x(o,{host:"cursor",sessionId:t.conversation_id??"unknown",cwd:i},S()?null:e,n);return s?JSON.stringify({permission:"allow",updated_input:{...r,command:s}}):ct}var q="{}";function Jt(t,e=_("gemini-cli"),n=k){if(t.hook_event_name!=="BeforeTool"||t.tool_name!=="run_shell_command")return q;let r=t.tool_input??{};if(r.is_background)return q;let o=r.command;if(typeof o!="string"||!o)return q;let i=typeof t.cwd=="string"?t.cwd:void 0,s=x(o,{host:"gemini-cli",sessionId:t.session_id??"unknown",cwd:i},S()?null:e,n);return s?JSON.stringify({hookSpecificOutput:{tool_input:{command:s}}}):q}function T(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}var Lo=String.raw`(?:'(?:(?:[^']|'\\'')*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?'|"(?:[^"]*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?"|(?:[^\s'"]*[\\/])?tkr(?:\.(?:exe|cmd|bat|ps1))?)`;function C(t,e,n="unknown",r=_(e),o=k){return t?x(t,{host:e,sessionId:n},S()?null:r,o):null}var Yn=new Set(["bash","file_system.bash"]),ut=JSON.stringify({decision:"allow"});function zt(t,e=(n,r)=>C(n,"vibe",r)){if(!T(t)||t.hook_event_name!=="pre_tool"||typeof t.tool_name!="string"||!Yn.has(t.tool_name))return"";let n=t.tool_input;if(!T(n)||typeof n.command!="string"||!n.command)return"";let r=e(n.command,typeof t.session_id=="string"?t.session_id:"unknown");return r?JSON.stringify({hook_specific_output:{tool_input:{...n,command:r}}}):""}function Bt(t,e=(n,r)=>C(n,"antigravity",r)){if(!T(t)||!T(t.toolCall)||t.toolCall.name!=="run_command")return ut;let n=t.toolCall.args;if(!T(n)||typeof n.CommandLine!="string"||!n.CommandLine)return ut;let r=e(n.CommandLine,typeof t.conversationId=="string"?t.conversationId:"unknown");return r?JSON.stringify({decision:"allow",overwrite:{CommandLine:r}}):ut}var w="{}",Vn=new Set(["runTerminalCommand","run_in_terminal","Bash","bash"]),Xn=["run_in_background","is_background","isBackground"];function Y(t){return t!=null&&typeof t=="object"&&!Array.isArray(t)}function Qn(t){let e=t.session_id??t.sessionId;return typeof e=="string"&&e?e:"unknown"}function tr(t,e){let n=t.cwd??e.cwd??e.workdir??e.working_directory;return typeof n=="string"&&n?n:void 0}function V(t,e,n,r,o){if(!Y(e)||Xn.some(s=>e[s]===!0))return null;let i=e.command;return typeof i!="string"||!i?null:x(i,{host:t,sessionId:Qn(n),cwd:tr(n,e)},S()?null:r,o)}function H(t,e){return JSON.stringify({hookSpecificOutput:{hookEventName:"PreToolUse",updatedInput:{...t,command:e}}})}function er(t){if(Y(t))return t;if(typeof t!="string")return null;try{let e=JSON.parse(t);return Y(e)?e:null}catch{return null}}function nr(t,e,n){if(typeof t.tool_name=="string"){if(!Vn.has(t.tool_name))return w;let i=t.tool_input,s=V("copilot",i,t,e,n);return s?H(i,s):w}if(t.toolName!=="bash")return w;let r=er(t.toolArgs),o=V("copilot",r,t,e,n);return o?JSON.stringify({modifiedArgs:{...r,command:o}}):w}function P(t,e,n){return(r,o,i)=>{if(r.tool_name!==e)return w;let s=r.tool_input,c=V(t,s,r,o,i);return c?n(s,c):w}}var rr=new Set(["run_terminal_command","Bash","bash"]);function or(t,e,n){let r=t.toolName??t.tool_name;if(typeof r!="string"||!rr.has(r))return w;let o=t.toolInput??t.tool_input,i=V("grok",o,t,e,n);return i?H(o,i):w}var sr={copilot:nr,droid:P("droid","Execute",H),trae:P("trae","RunCommand",H),"qwen-code":P("qwen-code","run_shell_command",H),crush:P("crush","bash",(t,e)=>JSON.stringify({updated_input:{command:e}})),junie:P("junie","Bash",(t,e)=>JSON.stringify({updatedInput:{...t,command:e}})),zcode:P("zcode","Bash",H),grok:or};function Wt(t,e,n=_(t),r=k){return Y(e)?sr[t](e,n,r):w}function N(t){return S()?null:_(t)}function ir(t,e,n={}){let r=n.recordUsage??k;switch(t){case"claude-code":case"codex":return e.hook_event_name!==void 0&&e.hook_event_name!=="PreToolUse"?"":st(e,t,N(t),r,n.claudeDirs??K(),n.pathEnv??process.env.PATH)??"";case"cursor":return Mt(e,N(t),r);case"gemini-cli":return Jt(e,N(t),r);case"vibe":return zt(e,(o,i)=>C(o,"vibe",i,N(t),r));case"antigravity":return Bt(e,(o,i)=>C(o,"antigravity",i,N(t),r));default:return Wt(t,e,N(t),r)}}function R(t,e,n={}){let r=Ut(t,e);return r?ir(r,t,n):n.fallback??""}function cr(t){let e;try{e=JSON.parse(t)}catch{return""}return R(e,"claude-code")}async function ur(){let t=await v(),e=t?cr(t):"";e&&process.stdout.write(e)}var lr=process.argv[1]??"";dt(lr,import.meta.url,"pre-tool-use")&&ur().catch(t=>{process.stderr.write(`tkr pre-tool-use: ${t.message}
|
|
12
12
|
`),process.exit(0)});export{st as buildPreToolUseOutput,_ as execInvocation,cr as preToolUseResponse,It as rewrite,G as splitTopLevel};
|