@pushary/agent-hooks 0.1.0 → 0.1.1

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,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePreToolUse
4
- } from "../chunk-I546R6K2.js";
4
+ } from "../chunk-5ZMTG7GF.js";
5
5
 
6
6
  // bin/pushary-hook.ts
7
7
  var main = async () => {
@@ -22,9 +22,9 @@ var main = async () => {
22
22
  process.exit(0);
23
23
  }
24
24
  try {
25
- const decision = await handlePreToolUse(input);
26
- if (decision) {
27
- process.stdout.write(JSON.stringify(decision));
25
+ const output = await handlePreToolUse(input);
26
+ if (output) {
27
+ process.stdout.write(JSON.stringify(output));
28
28
  }
29
29
  } catch (err) {
30
30
  process.stderr.write(
@@ -0,0 +1,184 @@
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
+ // src/api.ts
14
+ var mcpToolCall = async (apiKey, toolName, params) => {
15
+ const baseUrl = getBaseUrl();
16
+ const response = await fetch(`${baseUrl}/api/mcp/mcp`, {
17
+ method: "POST",
18
+ headers: {
19
+ "Content-Type": "application/json",
20
+ "Authorization": `Bearer ${apiKey}`
21
+ },
22
+ body: JSON.stringify({
23
+ jsonrpc: "2.0",
24
+ id: Date.now(),
25
+ method: "tools/call",
26
+ params: { name: toolName, arguments: params }
27
+ })
28
+ });
29
+ if (!response.ok) {
30
+ throw new Error(`Pushary API error: ${response.status} ${response.statusText}`);
31
+ }
32
+ const json = await response.json();
33
+ if (json.error) throw new Error(json.error.message);
34
+ const text = json.result?.content?.[0]?.text;
35
+ if (!text) throw new Error("Empty response from Pushary");
36
+ return JSON.parse(text);
37
+ };
38
+ var askUser = async (apiKey, params) => {
39
+ const result = await mcpToolCall(apiKey, "ask_user", { ...params });
40
+ return result;
41
+ };
42
+ var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
43
+ const result = await mcpToolCall(apiKey, "wait_for_answer", {
44
+ correlationId,
45
+ timeoutMs
46
+ });
47
+ return result;
48
+ };
49
+ var cancelQuestion = async (apiKey, correlationId) => {
50
+ await mcpToolCall(apiKey, "cancel_question", { correlationId });
51
+ };
52
+
53
+ // src/policy.ts
54
+ import { createHash } from "crypto";
55
+ import { existsSync, readFileSync, writeFileSync } from "fs";
56
+ import { join } from "path";
57
+ import { tmpdir } from "os";
58
+ var cacheFile = (apiKey) => {
59
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
60
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
61
+ };
62
+ var fetchPolicy = async (apiKey) => {
63
+ const baseUrl = getBaseUrl();
64
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
65
+ headers: { "Authorization": `Bearer ${apiKey}` },
66
+ signal: AbortSignal.timeout(1e4)
67
+ });
68
+ if (!response.ok) {
69
+ throw new Error(`Failed to fetch policy: ${response.status}`);
70
+ }
71
+ return response.json();
72
+ };
73
+ var getPolicy = async (apiKey) => {
74
+ const path = cacheFile(apiKey);
75
+ if (existsSync(path)) {
76
+ try {
77
+ return JSON.parse(readFileSync(path, "utf-8"));
78
+ } catch {
79
+ }
80
+ }
81
+ const policy = await fetchPolicy(apiKey);
82
+ writeFileSync(path, JSON.stringify(policy), "utf-8");
83
+ return policy;
84
+ };
85
+ var resolvePolicy = (config, toolName) => {
86
+ const exact = config.policies.find((p) => p.tool === toolName);
87
+ if (exact) return exact;
88
+ const wildcard = config.policies.find((p) => p.tool === "*");
89
+ if (wildcard) return wildcard;
90
+ return {
91
+ tool: toolName,
92
+ timeoutSeconds: config.defaultTimeoutSeconds,
93
+ timeoutAction: config.defaultTimeoutAction
94
+ };
95
+ };
96
+
97
+ // src/hook.ts
98
+ import { basename } from "path";
99
+ var describeToolCall = (input) => {
100
+ const { tool_name, tool_input } = input;
101
+ switch (tool_name) {
102
+ case "Bash":
103
+ return `bash: ${tool_input.command ?? "(no command)"}`;
104
+ case "Write":
105
+ return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
106
+ case "Edit":
107
+ return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
108
+ case "Read":
109
+ return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
110
+ default:
111
+ return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
112
+ }
113
+ };
114
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
115
+ var allow = () => ({
116
+ hookSpecificOutput: {
117
+ hookEventName: "PreToolUse",
118
+ permissionDecision: "allow"
119
+ }
120
+ });
121
+ var deny = (reason) => ({
122
+ hookSpecificOutput: {
123
+ hookEventName: "PreToolUse",
124
+ permissionDecision: "deny",
125
+ permissionDecisionReason: reason
126
+ }
127
+ });
128
+ var ask = (reason) => ({
129
+ hookSpecificOutput: {
130
+ hookEventName: "PreToolUse",
131
+ permissionDecision: "ask",
132
+ permissionDecisionReason: reason
133
+ }
134
+ });
135
+ var handlePreToolUse = async (input) => {
136
+ const apiKey = getApiKey();
137
+ const policy = await getPolicy(apiKey);
138
+ const toolPolicy = resolvePolicy(policy, input.tool_name);
139
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
140
+ return allow();
141
+ }
142
+ const description = describeToolCall(input);
143
+ const projectName = basename(input.cwd ?? process.cwd());
144
+ const result = await askUser(apiKey, {
145
+ question: `Allow ${description}?`,
146
+ type: "confirm",
147
+ context: `Agent wants to run this in ${projectName}`,
148
+ agentName: `Claude Code - ${projectName}`
149
+ });
150
+ const deadline = Date.now() + toolPolicy.timeoutSeconds * 1e3;
151
+ const pollInterval = 2e3;
152
+ while (Date.now() < deadline) {
153
+ const remaining = Math.min(
154
+ Math.max(deadline - Date.now(), 1e3),
155
+ 3e4
156
+ );
157
+ const answer = await waitForAnswer(apiKey, result.correlationId, remaining);
158
+ if (answer.answered) {
159
+ return answer.value === "yes" ? allow() : deny("Denied via Pushary push notification");
160
+ }
161
+ if (Date.now() + pollInterval >= deadline) break;
162
+ await sleep(pollInterval);
163
+ }
164
+ switch (toolPolicy.timeoutAction) {
165
+ case "approve":
166
+ return allow();
167
+ case "deny":
168
+ return deny("No response within timeout");
169
+ case "escalate":
170
+ default:
171
+ return ask("Pushary: no response \u2014 asking in terminal");
172
+ }
173
+ };
174
+
175
+ export {
176
+ getApiKey,
177
+ getBaseUrl,
178
+ askUser,
179
+ waitForAnswer,
180
+ cancelQuestion,
181
+ getPolicy,
182
+ resolvePolicy,
183
+ handlePreToolUse
184
+ };
@@ -1,12 +1,17 @@
1
1
  interface ToolInput {
2
2
  tool_name: string;
3
3
  tool_input: Record<string, unknown>;
4
+ session_id?: string;
5
+ cwd?: string;
4
6
  }
5
- interface HookDecision {
6
- decision: 'approve' | 'deny';
7
- reason?: string;
7
+ interface HookOutput {
8
+ hookSpecificOutput: {
9
+ hookEventName: 'PreToolUse';
10
+ permissionDecision: 'allow' | 'deny' | 'ask';
11
+ permissionDecisionReason?: string;
12
+ };
8
13
  }
9
- declare const handlePreToolUse: (input: ToolInput) => Promise<HookDecision | null>;
14
+ declare const handlePreToolUse: (input: ToolInput) => Promise<HookOutput | null>;
10
15
 
11
16
  interface AskUserParams {
12
17
  question: string;
package/dist/src/index.js CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  handlePreToolUse,
8
8
  resolvePolicy,
9
9
  waitForAnswer
10
- } from "../chunk-I546R6K2.js";
10
+ } from "../chunk-5ZMTG7GF.js";
11
11
  export {
12
12
  askUser,
13
13
  cancelQuestion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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",