@pushary/agent-hooks 0.4.6 → 0.5.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.
@@ -1,15 +1,14 @@
1
1
  #!/usr/bin/env node
2
+ import {
3
+ reportEvent
4
+ } from "../chunk-P4JH2Q7Z.js";
2
5
  import {
3
6
  askUser,
4
7
  waitForAnswer
5
- } from "../chunk-M5SRSBLS.js";
6
- import "../chunk-RJKW6LLC.js";
7
- import {
8
- reportEvent
9
- } from "../chunk-EQE6Z4YQ.js";
8
+ } from "../chunk-KTP2EPVB.js";
10
9
  import {
11
10
  getApiKey
12
- } from "../chunk-VUNL35KE.js";
11
+ } from "../chunk-VIST7ACL.js";
13
12
 
14
13
  // bin/pushary-codex.ts
15
14
  import { hostname } from "os";
@@ -2,8 +2,7 @@
2
2
  import {
3
3
  callMcpTool,
4
4
  sendMcpRequest
5
- } from "../chunk-RJKW6LLC.js";
6
- import "../chunk-VUNL35KE.js";
5
+ } from "../chunk-VIST7ACL.js";
7
6
 
8
7
  // bin/pushary-doctor.ts
9
8
  import { existsSync, readFileSync } from "fs";
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePreToolUse
4
- } from "../chunk-4TQW4K6T.js";
5
- import "../chunk-M5SRSBLS.js";
6
- import "../chunk-RJKW6LLC.js";
7
- import "../chunk-VUNL35KE.js";
4
+ } from "../chunk-DF3BM6BF.js";
5
+ import "../chunk-KTP2EPVB.js";
6
+ import "../chunk-VIST7ACL.js";
8
7
 
9
8
  // bin/pushary-hook.ts
10
9
  var main = async () => {
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePostToolUse
4
- } from "../chunk-EQE6Z4YQ.js";
5
- import "../chunk-VUNL35KE.js";
4
+ } from "../chunk-P4JH2Q7Z.js";
5
+ import "../chunk-KTP2EPVB.js";
6
+ import "../chunk-VIST7ACL.js";
6
7
 
7
8
  // bin/pushary-post-hook.ts
8
9
  var main = async () => {
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleStop
4
- } from "../chunk-EQE6Z4YQ.js";
5
- import "../chunk-VUNL35KE.js";
4
+ } from "../chunk-P4JH2Q7Z.js";
5
+ import "../chunk-KTP2EPVB.js";
6
+ import "../chunk-VIST7ACL.js";
6
7
 
7
8
  // bin/pushary-stop-hook.ts
8
9
  var main = async () => {
@@ -0,0 +1,195 @@
1
+ import {
2
+ askUser,
3
+ sendNotification,
4
+ waitForAnswer
5
+ } from "./chunk-KTP2EPVB.js";
6
+ import {
7
+ getApiKey,
8
+ getBaseUrl
9
+ } from "./chunk-VIST7ACL.js";
10
+
11
+ // src/policy.ts
12
+ import { createHash } from "crypto";
13
+ import { existsSync, readFileSync, writeFileSync } from "fs";
14
+ import { join } from "path";
15
+ import { tmpdir } from "os";
16
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
17
+ var cacheFile = (apiKey) => {
18
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
19
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
20
+ };
21
+ var fetchPolicy = async (apiKey) => {
22
+ const baseUrl = getBaseUrl();
23
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
24
+ headers: { "Authorization": `Bearer ${apiKey}` },
25
+ signal: AbortSignal.timeout(1e4)
26
+ });
27
+ if (!response.ok) {
28
+ throw new Error(`Failed to fetch policy: ${response.status}`);
29
+ }
30
+ return response.json();
31
+ };
32
+ var getPolicy = async (apiKey) => {
33
+ const path = cacheFile(apiKey);
34
+ if (existsSync(path)) {
35
+ try {
36
+ const stat = readFileSync(path, "utf-8");
37
+ const cached = JSON.parse(stat);
38
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
39
+ return cached;
40
+ }
41
+ } catch {
42
+ }
43
+ }
44
+ const policy = await fetchPolicy(apiKey);
45
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
46
+ return policy;
47
+ };
48
+ var resolvePolicy = (config, toolName) => {
49
+ const exact = config.policies.find((p) => p.tool === toolName);
50
+ if (exact) return exact;
51
+ const wildcard = config.policies.find((p) => p.tool === "*");
52
+ if (wildcard) return wildcard;
53
+ return {
54
+ tool: toolName,
55
+ timeoutSeconds: config.defaultTimeoutSeconds,
56
+ timeoutAction: config.defaultTimeoutAction,
57
+ mode: config.defaultMode ?? "push_first",
58
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 10
59
+ };
60
+ };
61
+
62
+ // src/hook.ts
63
+ import { basename } from "path";
64
+ import { writeFileSync as writeFileSync2, mkdirSync, existsSync as existsSync2 } from "fs";
65
+ import { join as join2 } from "path";
66
+ import { tmpdir as tmpdir2 } from "os";
67
+ var describeToolCall = (input) => {
68
+ const { tool_name, tool_input } = input;
69
+ switch (tool_name) {
70
+ case "Bash":
71
+ return `bash: ${tool_input.command ?? "(no command)"}`;
72
+ case "Write":
73
+ return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
74
+ case "Edit":
75
+ return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
76
+ case "Read":
77
+ return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
78
+ default:
79
+ return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
80
+ }
81
+ };
82
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
83
+ var allow = () => ({
84
+ hookSpecificOutput: {
85
+ hookEventName: "PreToolUse",
86
+ permissionDecision: "allow"
87
+ }
88
+ });
89
+ var deny = (reason) => ({
90
+ hookSpecificOutput: {
91
+ hookEventName: "PreToolUse",
92
+ permissionDecision: "deny",
93
+ permissionDecisionReason: reason
94
+ }
95
+ });
96
+ var ask = (reason) => ({
97
+ hookSpecificOutput: {
98
+ hookEventName: "PreToolUse",
99
+ permissionDecision: "ask",
100
+ ...reason ? { permissionDecisionReason: reason } : {}
101
+ }
102
+ });
103
+ var PENDING_DIR = join2(tmpdir2(), "pushary-pending");
104
+ var savePendingQuestion = (correlationId) => {
105
+ if (!existsSync2(PENDING_DIR)) mkdirSync(PENDING_DIR, { recursive: true });
106
+ writeFileSync2(join2(PENDING_DIR, correlationId), "", "utf-8");
107
+ };
108
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
109
+ while (Date.now() < deadlineMs) {
110
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
111
+ const answer = await waitForAnswer(apiKey, correlationId, remaining);
112
+ if (answer.answered) return answer;
113
+ if (Date.now() + pollInterval >= deadlineMs) break;
114
+ await sleep(pollInterval);
115
+ }
116
+ return { answered: false };
117
+ };
118
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
119
+ const result = await askUser(apiKey, {
120
+ question: `Allow ${description}?`,
121
+ type: "confirm",
122
+ context: `Agent wants to run this in ${projectName}`,
123
+ agentName: `Claude Code - ${projectName}`
124
+ });
125
+ const deadline = Date.now() + timeoutSeconds * 1e3;
126
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
127
+ if (answer.answered) {
128
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
129
+ }
130
+ switch (timeoutAction) {
131
+ case "approve":
132
+ return allow();
133
+ case "deny":
134
+ return deny("No response within timeout");
135
+ default:
136
+ return ask("No push response, asking in terminal");
137
+ }
138
+ };
139
+ var handleTerminalOnly = () => {
140
+ return ask();
141
+ };
142
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
143
+ const result = await askUser(apiKey, {
144
+ question: `Allow ${description}?`,
145
+ type: "confirm",
146
+ context: `Agent wants to run this in ${projectName}`,
147
+ agentName: `Claude Code - ${projectName}`
148
+ });
149
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
150
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
151
+ if (answer.answered) {
152
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
153
+ }
154
+ savePendingQuestion(result.correlationId);
155
+ return ask("Sent as push notification. You can also approve here.");
156
+ };
157
+ var handleNotifyOnly = async (apiKey, description, projectName) => {
158
+ try {
159
+ await sendNotification(apiKey, {
160
+ title: "Agent needs approval",
161
+ body: description,
162
+ agentName: `Claude Code - ${projectName}`
163
+ });
164
+ } catch {
165
+ }
166
+ return ask();
167
+ };
168
+ var handlePreToolUse = async (input) => {
169
+ const apiKey = getApiKey();
170
+ const policy = await getPolicy(apiKey);
171
+ const toolPolicy = resolvePolicy(policy, input.tool_name);
172
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
173
+ return allow();
174
+ }
175
+ const description = describeToolCall(input);
176
+ const projectName = basename(input.cwd ?? process.cwd());
177
+ switch (toolPolicy.mode) {
178
+ case "push_only":
179
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
180
+ case "terminal_only":
181
+ return handleTerminalOnly();
182
+ case "push_first":
183
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
184
+ case "notify_only":
185
+ return handleNotifyOnly(apiKey, description, projectName);
186
+ default:
187
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
188
+ }
189
+ };
190
+
191
+ export {
192
+ getPolicy,
193
+ resolvePolicy,
194
+ handlePreToolUse
195
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  callMcpTool
3
- } from "./chunk-RJKW6LLC.js";
3
+ } from "./chunk-VIST7ACL.js";
4
4
 
5
5
  // src/api.ts
6
6
  var askUser = async (apiKey, params) => {
@@ -15,9 +15,13 @@ var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
15
15
  var cancelQuestion = async (apiKey, correlationId) => {
16
16
  await callMcpTool(apiKey, "cancel_question", { correlationId });
17
17
  };
18
+ var sendNotification = async (apiKey, params) => {
19
+ await callMcpTool(apiKey, "send_notification", { ...params });
20
+ };
18
21
 
19
22
  export {
20
23
  askUser,
21
24
  waitForAnswer,
22
- cancelQuestion
25
+ cancelQuestion,
26
+ sendNotification
23
27
  };
@@ -1,11 +1,35 @@
1
+ import {
2
+ cancelQuestion
3
+ } from "./chunk-KTP2EPVB.js";
1
4
  import {
2
5
  getApiKey,
3
6
  getBaseUrl
4
- } from "./chunk-VUNL35KE.js";
7
+ } from "./chunk-VIST7ACL.js";
5
8
 
6
9
  // src/events.ts
7
10
  import { hostname } from "os";
8
11
  import { basename } from "path";
12
+ import { readdirSync, unlinkSync } from "fs";
13
+ import { join } from "path";
14
+ import { tmpdir } from "os";
15
+ var PENDING_DIR = join(tmpdir(), "pushary-pending");
16
+ var cleanupPendingQuestions = async () => {
17
+ try {
18
+ const files = readdirSync(PENDING_DIR);
19
+ const apiKey = getApiKey();
20
+ for (const correlationId of files) {
21
+ try {
22
+ await cancelQuestion(apiKey, correlationId);
23
+ } catch {
24
+ }
25
+ try {
26
+ unlinkSync(join(PENDING_DIR, correlationId));
27
+ } catch {
28
+ }
29
+ }
30
+ } catch {
31
+ }
32
+ };
9
33
  var reportEvent = async (event) => {
10
34
  const apiKey = getApiKey();
11
35
  const baseUrl = getBaseUrl();
@@ -40,6 +64,7 @@ var handlePostToolUse = async (input) => {
40
64
  default:
41
65
  action = `${input.tool_name}: done`;
42
66
  }
67
+ await cleanupPendingQuestions();
43
68
  const isError = input.tool_result && ("error" in input.tool_result || "is_error" in input.tool_result);
44
69
  await reportEvent({
45
70
  event: isError ? "tool_error" : "tool_complete",
@@ -1,6 +1,14 @@
1
- import {
2
- getBaseUrl
3
- } from "./chunk-VUNL35KE.js";
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";
4
12
 
5
13
  // src/mcp-http.ts
6
14
  var parseSseJson = (body) => {
@@ -73,6 +81,8 @@ var callMcpTool = async (apiKey, toolName, params, options = {}) => {
73
81
  };
74
82
 
75
83
  export {
84
+ getApiKey,
85
+ getBaseUrl,
76
86
  sendMcpRequest,
77
87
  callMcpTool
78
88
  };
@@ -61,15 +61,20 @@ declare const askUser: (apiKey: string, params: AskUserParams) => Promise<AskUse
61
61
  declare const waitForAnswer: (apiKey: string, correlationId: string, timeoutMs?: number) => Promise<WaitForAnswerResponse>;
62
62
  declare const cancelQuestion: (apiKey: string, correlationId: string) => Promise<void>;
63
63
 
64
+ type ApprovalMode = 'push_only' | 'terminal_only' | 'push_first' | 'notify_only';
64
65
  interface ToolPolicy {
65
66
  tool: string;
66
67
  timeoutSeconds: number;
67
68
  timeoutAction: 'approve' | 'deny' | 'escalate';
69
+ mode: ApprovalMode;
70
+ pushFirstSeconds: number;
68
71
  }
69
72
  interface PolicyConfig {
70
73
  policies: ToolPolicy[];
71
74
  defaultTimeoutSeconds: number;
72
75
  defaultTimeoutAction: 'approve' | 'deny' | 'escalate';
76
+ defaultMode: ApprovalMode;
77
+ defaultPushFirstSeconds: number;
73
78
  }
74
79
  declare const getPolicy: (apiKey: string) => Promise<PolicyConfig>;
75
80
  declare const resolvePolicy: (config: PolicyConfig, toolName: string) => ToolPolicy;
package/dist/src/index.js CHANGED
@@ -2,23 +2,22 @@ import {
2
2
  getPolicy,
3
3
  handlePreToolUse,
4
4
  resolvePolicy
5
- } from "../chunk-4TQW4K6T.js";
6
- import {
7
- askUser,
8
- cancelQuestion,
9
- waitForAnswer
10
- } from "../chunk-M5SRSBLS.js";
11
- import "../chunk-RJKW6LLC.js";
5
+ } from "../chunk-DF3BM6BF.js";
12
6
  import {
13
7
  handleNotification,
14
8
  handlePostToolUse,
15
9
  handleStop,
16
10
  reportEvent
17
- } from "../chunk-EQE6Z4YQ.js";
11
+ } from "../chunk-P4JH2Q7Z.js";
12
+ import {
13
+ askUser,
14
+ cancelQuestion,
15
+ waitForAnswer
16
+ } from "../chunk-KTP2EPVB.js";
18
17
  import {
19
18
  getApiKey,
20
19
  getBaseUrl
21
- } from "../chunk-VUNL35KE.js";
20
+ } from "../chunk-VIST7ACL.js";
22
21
  export {
23
22
  askUser,
24
23
  cancelQuestion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.4.6",
3
+ "version": "0.5.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",
@@ -1,136 +0,0 @@
1
- import {
2
- askUser,
3
- waitForAnswer
4
- } from "./chunk-M5SRSBLS.js";
5
- import {
6
- getApiKey,
7
- getBaseUrl
8
- } from "./chunk-VUNL35KE.js";
9
-
10
- // src/policy.ts
11
- import { createHash } from "crypto";
12
- import { existsSync, readFileSync, writeFileSync } from "fs";
13
- import { join } from "path";
14
- import { tmpdir } from "os";
15
- var cacheFile = (apiKey) => {
16
- const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
17
- return join(tmpdir(), `pushary-policy-${hash}.json`);
18
- };
19
- var fetchPolicy = async (apiKey) => {
20
- const baseUrl = getBaseUrl();
21
- const response = await fetch(`${baseUrl}/api/mcp/policy`, {
22
- headers: { "Authorization": `Bearer ${apiKey}` },
23
- signal: AbortSignal.timeout(1e4)
24
- });
25
- if (!response.ok) {
26
- throw new Error(`Failed to fetch policy: ${response.status}`);
27
- }
28
- return response.json();
29
- };
30
- var getPolicy = async (apiKey) => {
31
- const path = cacheFile(apiKey);
32
- if (existsSync(path)) {
33
- try {
34
- return JSON.parse(readFileSync(path, "utf-8"));
35
- } catch {
36
- }
37
- }
38
- const policy = await fetchPolicy(apiKey);
39
- writeFileSync(path, JSON.stringify(policy), "utf-8");
40
- return policy;
41
- };
42
- var resolvePolicy = (config, toolName) => {
43
- const exact = config.policies.find((p) => p.tool === toolName);
44
- if (exact) return exact;
45
- const wildcard = config.policies.find((p) => p.tool === "*");
46
- if (wildcard) return wildcard;
47
- return {
48
- tool: toolName,
49
- timeoutSeconds: config.defaultTimeoutSeconds,
50
- timeoutAction: config.defaultTimeoutAction
51
- };
52
- };
53
-
54
- // src/hook.ts
55
- import { basename } from "path";
56
- var describeToolCall = (input) => {
57
- const { tool_name, tool_input } = input;
58
- switch (tool_name) {
59
- case "Bash":
60
- return `bash: ${tool_input.command ?? "(no command)"}`;
61
- case "Write":
62
- return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
63
- case "Edit":
64
- return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
65
- case "Read":
66
- return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
67
- default:
68
- return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
69
- }
70
- };
71
- var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
72
- var allow = () => ({
73
- hookSpecificOutput: {
74
- hookEventName: "PreToolUse",
75
- permissionDecision: "allow"
76
- }
77
- });
78
- var deny = (reason) => ({
79
- hookSpecificOutput: {
80
- hookEventName: "PreToolUse",
81
- permissionDecision: "deny",
82
- permissionDecisionReason: reason
83
- }
84
- });
85
- var ask = (reason) => ({
86
- hookSpecificOutput: {
87
- hookEventName: "PreToolUse",
88
- permissionDecision: "ask",
89
- permissionDecisionReason: reason
90
- }
91
- });
92
- var handlePreToolUse = async (input) => {
93
- const apiKey = getApiKey();
94
- const policy = await getPolicy(apiKey);
95
- const toolPolicy = resolvePolicy(policy, input.tool_name);
96
- if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
97
- return allow();
98
- }
99
- const description = describeToolCall(input);
100
- const projectName = basename(input.cwd ?? process.cwd());
101
- const result = await askUser(apiKey, {
102
- question: `Allow ${description}?`,
103
- type: "confirm",
104
- context: `Agent wants to run this in ${projectName}`,
105
- agentName: `Claude Code - ${projectName}`
106
- });
107
- const deadline = Date.now() + toolPolicy.timeoutSeconds * 1e3;
108
- const pollInterval = 2e3;
109
- while (Date.now() < deadline) {
110
- const remaining = Math.min(
111
- Math.max(deadline - Date.now(), 1e3),
112
- 3e4
113
- );
114
- const answer = await waitForAnswer(apiKey, result.correlationId, remaining);
115
- if (answer.answered) {
116
- return answer.value === "yes" ? allow() : deny("Denied via Pushary push notification");
117
- }
118
- if (Date.now() + pollInterval >= deadline) break;
119
- await sleep(pollInterval);
120
- }
121
- switch (toolPolicy.timeoutAction) {
122
- case "approve":
123
- return allow();
124
- case "deny":
125
- return deny("No response within timeout");
126
- case "escalate":
127
- default:
128
- return ask("Pushary: no response, asking in terminal");
129
- }
130
- };
131
-
132
- export {
133
- getPolicy,
134
- resolvePolicy,
135
- handlePreToolUse
136
- };
@@ -1,16 +0,0 @@
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
- };