@pushary/agent-hooks 0.6.0 → 0.8.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,16 @@
1
+ // src/config.ts
2
+ var getApiKey = () => {
3
+ const key = process.env.PUSHARY_API_KEY;
4
+ if (!key) {
5
+ throw new Error(
6
+ "PUSHARY_API_KEY environment variable is not set. Get your API key at https://pushary.com/sign-up?from=ai-coding"
7
+ );
8
+ }
9
+ return key;
10
+ };
11
+ var getBaseUrl = () => process.env.PUSHARY_BASE_URL ?? "https://pushary.com";
12
+
13
+ export {
14
+ getApiKey,
15
+ getBaseUrl
16
+ };
@@ -0,0 +1,220 @@
1
+ import {
2
+ askUser,
3
+ describeToolCall,
4
+ isPolicyConfig,
5
+ savePendingQuestion,
6
+ sendNotification,
7
+ waitForAnswer
8
+ } from "./chunk-4Z4MB37G.js";
9
+ import {
10
+ getApiKey,
11
+ getBaseUrl,
12
+ withRetry
13
+ } from "./chunk-O6A5RHWY.js";
14
+
15
+ // src/policy.ts
16
+ import { createHash } from "crypto";
17
+ import { existsSync, readFileSync, writeFileSync } from "fs";
18
+ import { join } from "path";
19
+ import { tmpdir } from "os";
20
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
21
+ var cacheFile = (apiKey) => {
22
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
23
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
24
+ };
25
+ var fetchPolicy = async (apiKey) => {
26
+ return withRetry(async () => {
27
+ const baseUrl = getBaseUrl();
28
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
29
+ headers: { "Authorization": `Bearer ${apiKey}` },
30
+ signal: AbortSignal.timeout(1e4)
31
+ });
32
+ if (!response.ok) {
33
+ throw new Error(`Failed to fetch policy: ${response.status}`);
34
+ }
35
+ const raw = await response.json();
36
+ if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
37
+ return raw;
38
+ }, { maxAttempts: 2 });
39
+ };
40
+ var getPolicy = async (apiKey) => {
41
+ const path = cacheFile(apiKey);
42
+ let staleCache = null;
43
+ if (existsSync(path)) {
44
+ try {
45
+ const stat = readFileSync(path, "utf-8");
46
+ const cached = JSON.parse(stat);
47
+ if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
48
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
49
+ return cached;
50
+ }
51
+ staleCache = cached;
52
+ } catch {
53
+ }
54
+ }
55
+ try {
56
+ const policy = await fetchPolicy(apiKey);
57
+ try {
58
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
59
+ } catch {
60
+ }
61
+ return policy;
62
+ } catch {
63
+ if (staleCache) return staleCache;
64
+ throw new Error("Failed to fetch policy and no cached policy available");
65
+ }
66
+ };
67
+ var resolvePolicy = (config, toolName, modeOverride) => {
68
+ const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
69
+ tool: toolName,
70
+ timeoutSeconds: config.defaultTimeoutSeconds,
71
+ timeoutAction: config.defaultTimeoutAction,
72
+ mode: config.defaultMode ?? "push_first",
73
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
74
+ };
75
+ const effectiveOverride = modeOverride ?? config.modeOverride;
76
+ if (effectiveOverride) {
77
+ return { ...base, mode: effectiveOverride };
78
+ }
79
+ return base;
80
+ };
81
+
82
+ // src/hook.ts
83
+ import { basename } from "path";
84
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
85
+ var allow = () => ({
86
+ hookSpecificOutput: {
87
+ hookEventName: "PreToolUse",
88
+ permissionDecision: "allow"
89
+ }
90
+ });
91
+ var deny = (reason) => ({
92
+ hookSpecificOutput: {
93
+ hookEventName: "PreToolUse",
94
+ permissionDecision: "deny",
95
+ permissionDecisionReason: reason
96
+ }
97
+ });
98
+ var ask = (reason) => ({
99
+ hookSpecificOutput: {
100
+ hookEventName: "PreToolUse",
101
+ permissionDecision: "ask",
102
+ ...reason ? { permissionDecisionReason: reason } : {}
103
+ }
104
+ });
105
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
106
+ while (Date.now() < deadlineMs) {
107
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
108
+ let answer;
109
+ try {
110
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
111
+ } catch {
112
+ if (Date.now() + pollInterval >= deadlineMs) break;
113
+ await sleep(pollInterval);
114
+ continue;
115
+ }
116
+ if (answer.answered) return answer;
117
+ if (Date.now() + pollInterval >= deadlineMs) break;
118
+ await sleep(pollInterval);
119
+ }
120
+ return { answered: false };
121
+ };
122
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
123
+ let result;
124
+ try {
125
+ result = await askUser(apiKey, {
126
+ question: `Allow ${description}?`,
127
+ type: "confirm",
128
+ context: `Agent wants to run this in ${projectName}`,
129
+ agentName: `Claude Code - ${projectName}`
130
+ });
131
+ } catch {
132
+ switch (timeoutAction) {
133
+ case "approve":
134
+ return allow();
135
+ case "deny":
136
+ return deny("Push notification failed, denying per policy");
137
+ default:
138
+ return ask("Push notification failed, asking in terminal");
139
+ }
140
+ }
141
+ const deadline = Date.now() + timeoutSeconds * 1e3;
142
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
143
+ if (answer.answered) {
144
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
145
+ }
146
+ switch (timeoutAction) {
147
+ case "approve":
148
+ return allow();
149
+ case "deny":
150
+ return deny("No response within timeout");
151
+ default:
152
+ return ask("No push response, asking in terminal");
153
+ }
154
+ };
155
+ var handleTerminalOnly = () => {
156
+ return ask();
157
+ };
158
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
159
+ let result;
160
+ try {
161
+ result = await askUser(apiKey, {
162
+ question: `Allow ${description}?`,
163
+ type: "confirm",
164
+ context: `Agent wants to run this in ${projectName}`,
165
+ agentName: `Claude Code - ${projectName}`
166
+ });
167
+ } catch {
168
+ return ask("Push notification failed, asking in terminal");
169
+ }
170
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
171
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
172
+ if (answer.answered) {
173
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
174
+ }
175
+ savePendingQuestion(result.correlationId);
176
+ return ask("Sent as push notification. You can also approve here.");
177
+ };
178
+ var handleNotifyOnly = async (apiKey, description, projectName) => {
179
+ try {
180
+ await sendNotification(apiKey, {
181
+ title: "Agent needs approval",
182
+ body: description,
183
+ agentName: `Claude Code - ${projectName}`
184
+ });
185
+ } catch {
186
+ }
187
+ return ask();
188
+ };
189
+ var handlePreToolUse = async (input) => {
190
+ try {
191
+ const apiKey = getApiKey();
192
+ const policy = await getPolicy(apiKey);
193
+ const toolPolicy = resolvePolicy(policy, input.tool_name);
194
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
195
+ return allow();
196
+ }
197
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
198
+ const projectName = basename(input.cwd ?? process.cwd());
199
+ switch (toolPolicy.mode) {
200
+ case "push_only":
201
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
202
+ case "terminal_only":
203
+ return handleTerminalOnly();
204
+ case "push_first":
205
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
206
+ case "notify_only":
207
+ return handleNotifyOnly(apiKey, description, projectName);
208
+ default:
209
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
210
+ }
211
+ } catch {
212
+ return ask("Pushary unavailable, falling back to terminal approval");
213
+ }
214
+ };
215
+
216
+ export {
217
+ getPolicy,
218
+ resolvePolicy,
219
+ handlePreToolUse
220
+ };
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ getApiKey,
4
+ getBaseUrl
5
+ } from "./chunk-VUNL35KE.js";
6
+
7
+ // bin/pushary-mode.ts
8
+ var VALID_MODES = ["push_only", "push_first", "terminal_only", "notify_only"];
9
+ var dim = (s) => `\x1B[2m${s}\x1B[0m`;
10
+ var green = (s) => `\x1B[32m${s}\x1B[0m`;
11
+ var cyan = (s) => `\x1B[36m${s}\x1B[0m`;
12
+ var yellow = (s) => `\x1B[33m${s}\x1B[0m`;
13
+ var parseDuration = (value) => {
14
+ const match = value.match(/^(\d+)(m|h)$/);
15
+ if (!match) return null;
16
+ const num = Number(match[1]);
17
+ return match[2] === "h" ? num * 3600 : num * 60;
18
+ };
19
+ var main = async () => {
20
+ const apiKey = getApiKey();
21
+ const baseUrl = getBaseUrl();
22
+ const mode = process.argv[2];
23
+ const forFlag = process.argv[3];
24
+ const forValue = process.argv[4];
25
+ const headers = {
26
+ "Authorization": `Bearer ${apiKey}`,
27
+ "Content-Type": "application/json"
28
+ };
29
+ if (!mode || mode === "status") {
30
+ const res2 = await fetch(`${baseUrl}/api/mcp/mode`, { headers });
31
+ const data2 = await res2.json();
32
+ if (!data2.override) {
33
+ console.log(` Mode: ${cyan("default")} ${dim("(using per-tool policies)")}`);
34
+ } else {
35
+ console.log(` Mode: ${green(data2.override.mode)}`);
36
+ if (data2.override.expiresAt) {
37
+ console.log(` Expires: ${new Date(data2.override.expiresAt).toLocaleString()}`);
38
+ } else {
39
+ console.log(` ${dim("Sticky (no expiry)")}`);
40
+ }
41
+ }
42
+ return;
43
+ }
44
+ if (mode === "clear" || mode === "reset") {
45
+ await fetch(`${baseUrl}/api/mcp/mode`, { method: "DELETE", headers });
46
+ console.log(` ${green("\u2713")} Mode override cleared \u2014 using per-tool policies`);
47
+ return;
48
+ }
49
+ if (!VALID_MODES.includes(mode)) {
50
+ console.log(` ${yellow("!")} Invalid mode: ${mode}`);
51
+ console.log(` Valid modes: ${VALID_MODES.join(", ")}`);
52
+ console.log(` ${dim("Usage: pushary mode <mode> [--for <duration>]")}`);
53
+ console.log(` ${dim("Example: pushary mode push_only --for 30m")}`);
54
+ return;
55
+ }
56
+ let ttlSeconds;
57
+ if (forFlag === "--for" && forValue) {
58
+ ttlSeconds = parseDuration(forValue);
59
+ if (!ttlSeconds) {
60
+ console.log(` ${yellow("!")} Invalid duration: ${forValue} ${dim("(use e.g. 30m, 1h)")}`);
61
+ return;
62
+ }
63
+ }
64
+ const res = await fetch(`${baseUrl}/api/mcp/mode`, {
65
+ method: "PUT",
66
+ headers,
67
+ body: JSON.stringify({ mode, ttlSeconds })
68
+ });
69
+ if (!res.ok) {
70
+ const err = await res.json();
71
+ console.log(` ${yellow("!")} Failed: ${err.error ?? res.statusText}`);
72
+ return;
73
+ }
74
+ const data = await res.json();
75
+ console.log(` ${green("\u2713")} Mode set to ${cyan(data.override.mode)}`);
76
+ if (data.override.expiresAt) {
77
+ console.log(` Expires: ${new Date(data.override.expiresAt).toLocaleString()}`);
78
+ }
79
+ };
80
+ main().catch((err) => {
81
+ console.error(` ${yellow("!")} ${err instanceof Error ? err.message : err}`);
82
+ process.exit(1);
83
+ });
@@ -75,11 +75,13 @@ interface PolicyConfig {
75
75
  defaultTimeoutAction: 'approve' | 'deny' | 'escalate';
76
76
  defaultMode: ApprovalMode;
77
77
  defaultPushFirstSeconds: number;
78
+ modeOverride?: ApprovalMode | null;
78
79
  }
79
80
  declare const getPolicy: (apiKey: string) => Promise<PolicyConfig>;
80
- declare const resolvePolicy: (config: PolicyConfig, toolName: string) => ToolPolicy;
81
+ declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null) => ToolPolicy;
82
+ declare const fetchModeOverride: (apiKey: string) => Promise<ApprovalMode | null>;
81
83
 
82
84
  declare const getApiKey: () => string;
83
85
  declare const getBaseUrl: () => string;
84
86
 
85
- export { type PolicyConfig, type ToolPolicy, askUser, cancelQuestion, getApiKey, getBaseUrl, getPolicy, handleNotification, handlePostToolUse, handlePreToolUse, handleStop, reportEvent, resolvePolicy, waitForAnswer };
87
+ export { type ApprovalMode, type PolicyConfig, type ToolPolicy, askUser, cancelQuestion, fetchModeOverride, getApiKey, getBaseUrl, getPolicy, handleNotification, handlePostToolUse, handlePreToolUse, handleStop, reportEvent, resolvePolicy, waitForAnswer };
package/dist/src/index.js CHANGED
@@ -1,26 +1,29 @@
1
1
  import {
2
+ fetchModeOverride,
2
3
  getPolicy,
3
4
  handlePreToolUse,
4
5
  resolvePolicy
5
- } from "../chunk-WNXGIEX7.js";
6
+ } from "../chunk-C5TFTNHG.js";
6
7
  import {
7
8
  handleNotification,
8
9
  handlePostToolUse,
9
10
  handleStop,
10
11
  reportEvent
11
- } from "../chunk-KYARP7KP.js";
12
+ } from "../chunk-5JEDLXEC.js";
12
13
  import {
13
14
  askUser,
14
15
  cancelQuestion,
15
16
  waitForAnswer
16
- } from "../chunk-4Z4MB37G.js";
17
+ } from "../chunk-EMPL27ZV.js";
18
+ import "../chunk-3MIR7ODJ.js";
17
19
  import {
18
20
  getApiKey,
19
21
  getBaseUrl
20
- } from "../chunk-O6A5RHWY.js";
22
+ } from "../chunk-VUNL35KE.js";
21
23
  export {
22
24
  askUser,
23
25
  cancelQuestion,
26
+ fetchModeOverride,
24
27
  getApiKey,
25
28
  getBaseUrl,
26
29
  getPolicy,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.6.0",
3
+ "version": "0.8.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",
@@ -22,15 +22,16 @@
22
22
  "pushary-codex": "./dist/bin/pushary-codex.js",
23
23
  "pushary-setup": "./dist/bin/pushary-setup.js",
24
24
  "pushary-clean": "./dist/bin/pushary-clean.js",
25
- "pushary-doctor": "./dist/bin/pushary-doctor.js"
25
+ "pushary-doctor": "./dist/bin/pushary-doctor.js",
26
+ "pushary-mode": "./dist/bin/pushary-mode.js"
26
27
  },
27
28
  "files": [
28
29
  "dist",
29
30
  "data"
30
31
  ],
31
32
  "scripts": {
32
- "build": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts --format esm --dts --outDir dist",
33
- "dev": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts --format esm --watch",
33
+ "build": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts bin/pushary-mode.ts --format esm --dts --outDir dist",
34
+ "dev": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts bin/pushary-mode.ts --format esm --watch",
34
35
  "test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/events.test.ts && bun test src/hook.test.ts"
35
36
  },
36
37
  "dependencies": {