@pushary/agent-hooks 0.18.2 → 0.19.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.
@@ -0,0 +1,156 @@
1
+ // ../contracts/src/index.ts
2
+ var APPROVAL_MODES = ["push_only", "terminal_only", "push_first", "notify_only"];
3
+ var isApprovalMode = (value) => typeof value === "string" && APPROVAL_MODES.includes(value);
4
+ var MATCH_RANKS = ["none", "tool", "prefix", "exact"];
5
+ var matchRankWeight = (rank) => MATCH_RANKS.indexOf(rank);
6
+ var matchToolPattern = (pattern, toolName, arg) => {
7
+ const open = pattern.indexOf("(");
8
+ if (open === -1 || !pattern.endsWith(")")) {
9
+ return pattern === toolName ? "tool" : "none";
10
+ }
11
+ if (pattern.slice(0, open) !== toolName || arg === void 0) return "none";
12
+ const inner = pattern.slice(open + 1, -1);
13
+ if (inner.endsWith(":*")) {
14
+ return arg.startsWith(inner.slice(0, -2)) ? "prefix" : "none";
15
+ }
16
+ return arg === inner ? "exact" : "none";
17
+ };
18
+ var POLICY_ARG_KEYS = {
19
+ Bash: "command",
20
+ Edit: "file_path",
21
+ Write: "file_path"
22
+ };
23
+ var extractPolicyArg = (toolName, toolInput) => {
24
+ const key = POLICY_ARG_KEYS[toolName];
25
+ if (!key) return void 0;
26
+ const value = toolInput[key];
27
+ return typeof value === "string" ? value : void 0;
28
+ };
29
+ var SAFE_SHELL_COMMANDS = /* @__PURE__ */ new Set([
30
+ "ls",
31
+ "pwd",
32
+ "cd",
33
+ "cat",
34
+ "head",
35
+ "tail",
36
+ "wc",
37
+ "echo",
38
+ "printf",
39
+ "which",
40
+ "type",
41
+ "whoami",
42
+ "id",
43
+ "uname",
44
+ "arch",
45
+ "printenv",
46
+ "locale",
47
+ "tty",
48
+ "dirname",
49
+ "basename",
50
+ "realpath",
51
+ "readlink",
52
+ "stat",
53
+ "cut",
54
+ "nl",
55
+ "tr",
56
+ "comm",
57
+ "diff",
58
+ "cmp",
59
+ "grep",
60
+ "egrep",
61
+ "fgrep",
62
+ "jq",
63
+ "cksum",
64
+ "md5sum",
65
+ "sha1sum",
66
+ "sha256sum",
67
+ "du",
68
+ "df",
69
+ "ps",
70
+ "true"
71
+ ]);
72
+ var SAFE_GIT_SUBCOMMANDS = /* @__PURE__ */ new Set([
73
+ "status",
74
+ "log",
75
+ "diff",
76
+ "show",
77
+ "rev-parse",
78
+ "describe",
79
+ "blame",
80
+ "shortlog",
81
+ "ls-files",
82
+ "ls-tree",
83
+ "cat-file",
84
+ "whatchanged",
85
+ "rev-list",
86
+ "name-rev",
87
+ "for-each-ref",
88
+ "var",
89
+ "count-objects"
90
+ ]);
91
+ var GIT_WRITE_FLAGS = (token) => token === "--output" || token.startsWith("--output=");
92
+ var UNSAFE_SHELL_CHARS = /[;&|<>(){}`\\\n\r]/;
93
+ var basenameOf = (token) => {
94
+ const slash = Math.max(token.lastIndexOf("/"), token.lastIndexOf("\\"));
95
+ return slash === -1 ? token : token.slice(slash + 1);
96
+ };
97
+ var tokenizeShellCommand = (command) => {
98
+ const tokens = [];
99
+ let current = "";
100
+ let quote = null;
101
+ let started = false;
102
+ for (const ch of command) {
103
+ if (quote) {
104
+ if (ch === quote) quote = null;
105
+ else current += ch;
106
+ started = true;
107
+ continue;
108
+ }
109
+ if (ch === '"' || ch === "'") {
110
+ quote = ch;
111
+ started = true;
112
+ continue;
113
+ }
114
+ if (ch === " " || ch === " ") {
115
+ if (started) {
116
+ tokens.push(current);
117
+ current = "";
118
+ started = false;
119
+ }
120
+ continue;
121
+ }
122
+ current += ch;
123
+ started = true;
124
+ }
125
+ if (quote) return null;
126
+ if (started) tokens.push(current);
127
+ return tokens;
128
+ };
129
+ var isSafeReadOnlyCommand = (command) => {
130
+ const trimmed = command.trim();
131
+ if (!trimmed || trimmed.length > 2e3) return false;
132
+ if (UNSAFE_SHELL_CHARS.test(trimmed)) return false;
133
+ const tokens = tokenizeShellCommand(trimmed);
134
+ if (!tokens || tokens.length === 0) return false;
135
+ const first = tokens[0];
136
+ if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(first) || first.includes("$")) return false;
137
+ const exe = basenameOf(first);
138
+ if (exe === "git") {
139
+ const sub = tokens[1];
140
+ if (!sub || sub.startsWith("-")) return false;
141
+ if (!SAFE_GIT_SUBCOMMANDS.has(sub)) return false;
142
+ return !tokens.some(GIT_WRITE_FLAGS);
143
+ }
144
+ return SAFE_SHELL_COMMANDS.has(exe);
145
+ };
146
+ var API_KEY_PATTERN = /^pk_[a-f0-9]+\.[a-f0-9]+$/;
147
+ var isValidApiKey = (value) => API_KEY_PATTERN.test(value);
148
+
149
+ export {
150
+ isApprovalMode,
151
+ matchRankWeight,
152
+ matchToolPattern,
153
+ extractPolicyArg,
154
+ isSafeReadOnlyCommand,
155
+ isValidApiKey
156
+ };
@@ -0,0 +1,244 @@
1
+ // src/codex-config.ts
2
+ import { createHash } from "crypto";
3
+ var CODEX_HOOK_BINARY = "pushary-codex-hook";
4
+ var CODEX_HOOK_EVENTS = [
5
+ { event: "PermissionRequest", matcher: "Bash|apply_patch", timeout: 180, statusMessage: "Waiting for your phone" },
6
+ { event: "PreToolUse", matcher: "Bash|apply_patch", timeout: 180, statusMessage: "Checking Pushary policy" },
7
+ { event: "PostToolUse", matcher: "Bash|apply_patch", timeout: 10 },
8
+ { event: "UserPromptSubmit", timeout: 10 },
9
+ { event: "Stop", timeout: 10 },
10
+ { event: "SessionStart", matcher: "startup|resume", timeout: 10 }
11
+ ];
12
+ var CODEX_EVENT_KEY = {
13
+ PermissionRequest: "permission_request",
14
+ PreToolUse: "pre_tool_use",
15
+ PostToolUse: "post_tool_use",
16
+ UserPromptSubmit: "user_prompt_submit",
17
+ Stop: "stop",
18
+ SessionStart: "session_start"
19
+ };
20
+ var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
21
+ var ensureRecord = (target, key) => {
22
+ const existing = asRecord(target[key]);
23
+ if (existing) return existing;
24
+ const created = {};
25
+ target[key] = created;
26
+ return created;
27
+ };
28
+ var isPusharyCodexHook = (entry) => {
29
+ const hooks = asRecord(entry)?.hooks;
30
+ if (!Array.isArray(hooks)) return false;
31
+ return hooks.some((hook) => String(asRecord(hook)?.command ?? "").includes(CODEX_HOOK_BINARY));
32
+ };
33
+ var addCodexHooks = (config, command) => {
34
+ const hooks = ensureRecord(config, "hooks");
35
+ for (const definition of CODEX_HOOK_EVENTS) {
36
+ const existing = Array.isArray(hooks[definition.event]) ? hooks[definition.event] : [];
37
+ const entries = existing.filter((entry) => !isPusharyCodexHook(entry));
38
+ entries.push({
39
+ ...definition.matcher ? { matcher: definition.matcher } : {},
40
+ hooks: [{
41
+ type: "command",
42
+ command,
43
+ timeout: definition.timeout,
44
+ ...definition.statusMessage ? { statusMessage: definition.statusMessage } : {}
45
+ }]
46
+ });
47
+ hooks[definition.event] = entries;
48
+ }
49
+ };
50
+ var removeCodexHooks = (config) => {
51
+ const hooks = asRecord(config.hooks);
52
+ if (!hooks) return false;
53
+ let changed = false;
54
+ for (const definition of CODEX_HOOK_EVENTS) {
55
+ const entries = hooks[definition.event];
56
+ if (!Array.isArray(entries)) continue;
57
+ const filtered = entries.filter((entry) => !isPusharyCodexHook(entry));
58
+ if (filtered.length !== entries.length) {
59
+ if (filtered.length === 0) {
60
+ delete hooks[definition.event];
61
+ } else {
62
+ hooks[definition.event] = filtered;
63
+ }
64
+ changed = true;
65
+ }
66
+ }
67
+ if (Object.keys(hooks).length === 0) delete config.hooks;
68
+ return changed;
69
+ };
70
+ var hasCodexHooks = (config) => {
71
+ const hooks = asRecord(config.hooks);
72
+ if (!hooks) return false;
73
+ return CODEX_HOOK_EVENTS.some((definition) => {
74
+ const entries = hooks[definition.event];
75
+ return Array.isArray(entries) && entries.some(isPusharyCodexHook);
76
+ });
77
+ };
78
+ var missingCodexHookEvents = (config) => {
79
+ const hooks = asRecord(config.hooks);
80
+ return CODEX_HOOK_EVENTS.filter((definition) => {
81
+ const entries = hooks?.[definition.event];
82
+ return !Array.isArray(entries) || !entries.some(isPusharyCodexHook);
83
+ }).map((definition) => definition.event);
84
+ };
85
+ var canonicalJson = (value) => {
86
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
87
+ if (Array.isArray(value)) return "[" + value.map(canonicalJson).join(",") + "]";
88
+ const obj = value;
89
+ return "{" + Object.keys(obj).sort().map((key) => JSON.stringify(key) + ":" + canonicalJson(obj[key])).join(",") + "}";
90
+ };
91
+ var codexHookTrustHash = (definition, command) => {
92
+ const hook = { async: false, command, timeout: definition.timeout, type: "command" };
93
+ if (definition.statusMessage) hook.statusMessage = definition.statusMessage;
94
+ const identity = { event_name: CODEX_EVENT_KEY[definition.event], hooks: [hook] };
95
+ if (definition.matcher) identity.matcher = definition.matcher;
96
+ return "sha256:" + createHash("sha256").update(canonicalJson(identity), "utf8").digest("hex");
97
+ };
98
+ var codexHookStateKey = (hooksJsonPath, event) => `${hooksJsonPath}:${CODEX_EVENT_KEY[event]}:0:0`;
99
+ var addCodexHookTrust = (config, hooksJsonPath, command) => {
100
+ const hooks = ensureRecord(config, "hooks");
101
+ const state = ensureRecord(hooks, "state");
102
+ for (const definition of CODEX_HOOK_EVENTS) {
103
+ const key = codexHookStateKey(hooksJsonPath, definition.event);
104
+ const existing = asRecord(state[key]) ?? {};
105
+ state[key] = { ...existing, trusted_hash: codexHookTrustHash(definition, command) };
106
+ }
107
+ };
108
+
109
+ // src/gemini-config.ts
110
+ var GEMINI_HOOK_BINARY = "pushary-gemini-hook";
111
+ var GEMINI_MCP_URL = "https://pushary.com/api/mcp/mcp";
112
+ var GEMINI_HOOK_EVENTS = [
113
+ { event: "BeforeTool", matcher: "run_shell_command|write_file|replace", timeoutMs: 18e4 },
114
+ { event: "AfterTool", matcher: "run_shell_command|write_file|replace", timeoutMs: 1e4 },
115
+ { event: "BeforeAgent", timeoutMs: 1e4 },
116
+ { event: "SessionStart", timeoutMs: 1e4 },
117
+ { event: "SessionEnd", timeoutMs: 1e4 }
118
+ ];
119
+ var asRecord2 = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
120
+ var ensureRecord2 = (target, key) => {
121
+ const existing = asRecord2(target[key]);
122
+ if (existing) return existing;
123
+ const created = {};
124
+ target[key] = created;
125
+ return created;
126
+ };
127
+ var isPusharyGeminiHook = (entry) => {
128
+ const hooks = asRecord2(entry)?.hooks;
129
+ if (!Array.isArray(hooks)) return false;
130
+ return hooks.some((hook) => String(asRecord2(hook)?.command ?? "").includes(GEMINI_HOOK_BINARY));
131
+ };
132
+ var addGeminiMcpServer = (settings, apiKey) => {
133
+ const mcpServers = ensureRecord2(settings, "mcpServers");
134
+ mcpServers.pushary = {
135
+ httpUrl: GEMINI_MCP_URL,
136
+ headers: { Authorization: `Bearer ${apiKey}` },
137
+ trust: true
138
+ };
139
+ };
140
+ var addGeminiHooks = (settings, command) => {
141
+ const hooks = ensureRecord2(settings, "hooks");
142
+ for (const definition of GEMINI_HOOK_EVENTS) {
143
+ const existing = Array.isArray(hooks[definition.event]) ? hooks[definition.event] : [];
144
+ const entries = existing.filter((entry) => !isPusharyGeminiHook(entry));
145
+ entries.push({
146
+ ...definition.matcher ? { matcher: definition.matcher } : {},
147
+ hooks: [{
148
+ name: "pushary",
149
+ type: "command",
150
+ command,
151
+ timeout: definition.timeoutMs
152
+ }]
153
+ });
154
+ hooks[definition.event] = entries;
155
+ }
156
+ };
157
+ var removeGeminiHooks = (settings) => {
158
+ const hooks = asRecord2(settings.hooks);
159
+ if (!hooks) return false;
160
+ let changed = false;
161
+ for (const definition of GEMINI_HOOK_EVENTS) {
162
+ const entries = hooks[definition.event];
163
+ if (!Array.isArray(entries)) continue;
164
+ const filtered = entries.filter((entry) => !isPusharyGeminiHook(entry));
165
+ if (filtered.length !== entries.length) {
166
+ if (filtered.length === 0) {
167
+ delete hooks[definition.event];
168
+ } else {
169
+ hooks[definition.event] = filtered;
170
+ }
171
+ changed = true;
172
+ }
173
+ }
174
+ if (Object.keys(hooks).length === 0) delete settings.hooks;
175
+ return changed;
176
+ };
177
+ var removeGeminiMcpServer = (settings) => {
178
+ const mcpServers = asRecord2(settings.mcpServers);
179
+ if (!mcpServers?.pushary) return false;
180
+ delete mcpServers.pushary;
181
+ if (Object.keys(mcpServers).length === 0) delete settings.mcpServers;
182
+ return true;
183
+ };
184
+ var removeGeminiSettings = (settings) => {
185
+ const mcpRemoved = removeGeminiMcpServer(settings);
186
+ const hooksRemoved = removeGeminiHooks(settings);
187
+ return mcpRemoved || hooksRemoved;
188
+ };
189
+ var hasGeminiHooks = (settings) => {
190
+ const hooks = asRecord2(settings.hooks);
191
+ if (!hooks) return false;
192
+ return GEMINI_HOOK_EVENTS.some((definition) => {
193
+ const entries = hooks[definition.event];
194
+ return Array.isArray(entries) && entries.some(isPusharyGeminiHook);
195
+ });
196
+ };
197
+ var missingGeminiHookEvents = (settings) => {
198
+ const hooks = asRecord2(settings.hooks);
199
+ return GEMINI_HOOK_EVENTS.filter((definition) => {
200
+ const entries = hooks?.[definition.event];
201
+ return !Array.isArray(entries) || !entries.some(isPusharyGeminiHook);
202
+ }).map((definition) => definition.event);
203
+ };
204
+
205
+ // src/npm.ts
206
+ import { execSync } from "child_process";
207
+ var cleanNpmEnv = () => {
208
+ const env = {};
209
+ for (const [key, value] of Object.entries(process.env)) {
210
+ if (key.toLowerCase().startsWith("npm_config_workspace")) continue;
211
+ env[key] = value;
212
+ }
213
+ return env;
214
+ };
215
+ var npmErrorMessage = (err) => {
216
+ const e = err;
217
+ const text = [e?.stderr, e?.stdout].map((part) => part ? part.toString() : "").join("\n");
218
+ const line = text.split("\n").map((l) => l.replace(/^npm error\s*/i, "").trim()).find((l) => l && !l.startsWith("A complete log") && !/^code\s/i.test(l));
219
+ return line || e?.message || String(err);
220
+ };
221
+ var execNpm = (args, options = {}) => {
222
+ return execSync(`npm ${args}`, {
223
+ timeout: 12e4,
224
+ stdio: "pipe",
225
+ ...options,
226
+ env: { ...cleanNpmEnv(), ...options.env ?? {} }
227
+ });
228
+ };
229
+
230
+ export {
231
+ addCodexHooks,
232
+ removeCodexHooks,
233
+ hasCodexHooks,
234
+ missingCodexHookEvents,
235
+ addCodexHookTrust,
236
+ GEMINI_HOOK_BINARY,
237
+ addGeminiMcpServer,
238
+ addGeminiHooks,
239
+ removeGeminiSettings,
240
+ hasGeminiHooks,
241
+ missingGeminiHookEvents,
242
+ npmErrorMessage,
243
+ execNpm
244
+ };
package/dist/src/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  handlePreToolUse
3
- } from "../chunk-DKZVKEOW.js";
3
+ } from "../chunk-SDREQWNI.js";
4
4
  import {
5
5
  handleNotification,
6
6
  handlePostToolUse,
7
7
  handleStop,
8
8
  reportEvent
9
- } from "../chunk-Y633ZIJF.js";
9
+ } from "../chunk-QY4L6XHN.js";
10
10
  import {
11
11
  askUser,
12
12
  cancelQuestion,
@@ -15,8 +15,8 @@ import {
15
15
  getPolicy,
16
16
  resolvePolicy,
17
17
  waitForAnswer
18
- } from "../chunk-2BE2IPMO.js";
19
- import "../chunk-RZLVE57X.js";
18
+ } from "../chunk-ACE77TKQ.js";
19
+ import "../chunk-USUCPCUC.js";
20
20
  import "../chunk-DWED7BS3.js";
21
21
  import {
22
22
  getApiKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.18.2",
3
+ "version": "0.19.0",
4
4
  "description": "Permission hooks for AI coding agents: route tool approvals through Pushary push notifications",
5
5
  "author": "Pushary <business@pushary.com>",
6
6
  "homepage": "https://pushary.com",
@@ -25,6 +25,7 @@
25
25
  "pushary-prompt-hook": "./dist/bin/pushary-prompt-hook.js",
26
26
  "pushary-codex": "./dist/bin/pushary-codex.js",
27
27
  "pushary-codex-hook": "./dist/bin/pushary-codex-hook.js",
28
+ "pushary-gemini-hook": "./dist/bin/pushary-gemini-hook.js",
28
29
  "pushary-setup": "./dist/bin/pushary-setup.js",
29
30
  "pushary-clean": "./dist/bin/pushary-clean.js",
30
31
  "pushary-doctor": "./dist/bin/pushary-doctor.js",
@@ -37,7 +38,7 @@
37
38
  "scripts": {
38
39
  "build": "node scripts/bundle-plugin.mjs && tsup",
39
40
  "dev": "tsup --watch",
40
- "test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/suggestions.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts && bun test src/pairing.test.ts"
41
+ "test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/suggestions.test.ts && bun test src/safe-commands.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts && bun test src/gemini-adapter.test.ts && bun test src/gemini-config.test.ts && bun test src/pairing.test.ts"
41
42
  },
42
43
  "dependencies": {
43
44
  "@inquirer/prompts": "^8.4.2",