@pushary/agent-hooks 0.1.0 → 0.2.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,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 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env node
2
+
3
+ // bin/pushary-setup.ts
4
+ import { existsSync, readFileSync, writeFileSync, appendFileSync, mkdirSync } from "fs";
5
+ import { join } from "path";
6
+ import { homedir } from "os";
7
+ import { createInterface } from "readline";
8
+ var rl = createInterface({ input: process.stdin, output: process.stdout });
9
+ var ask = (q) => new Promise((r) => rl.question(q, r));
10
+ var CLAUDE_SETTINGS = join(homedir(), ".claude", "settings.json");
11
+ var CURSOR_MCP = join(".cursor", "mcp.json");
12
+ var SHELL_FILES = [".zshrc", ".bashrc"].map((f) => join(homedir(), f));
13
+ var dim = (s) => `\x1B[2m${s}\x1B[0m`;
14
+ var bold = (s) => `\x1B[1m${s}\x1B[0m`;
15
+ var green = (s) => `\x1B[32m${s}\x1B[0m`;
16
+ var cyan = (s) => `\x1B[36m${s}\x1B[0m`;
17
+ var readJson = (path) => {
18
+ try {
19
+ return JSON.parse(readFileSync(path, "utf-8"));
20
+ } catch {
21
+ return {};
22
+ }
23
+ };
24
+ var writeJson = (path, data) => {
25
+ const dir = path.substring(0, path.lastIndexOf("/"));
26
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
27
+ writeFileSync(path, JSON.stringify(data, null, 2) + "\n", "utf-8");
28
+ };
29
+ var setupClaudeCode = async (apiKey) => {
30
+ console.log(`
31
+ ${bold("Claude Code Setup")}
32
+ `);
33
+ const settings = readJson(CLAUDE_SETTINGS);
34
+ const mcpServers = settings.mcpServers ?? {};
35
+ mcpServers.pushary = {
36
+ url: "https://pushary.com/api/mcp/mcp",
37
+ headers: { Authorization: `Bearer ${apiKey}` }
38
+ };
39
+ settings.mcpServers = mcpServers;
40
+ const addHooks = await ask(`Add permission hooks? (approve/deny tools from your phone) ${dim("[Y/n]")} `);
41
+ if (addHooks.toLowerCase() !== "n") {
42
+ const hooks = settings.hooks ?? {};
43
+ const preToolUse = hooks.PreToolUse ?? [];
44
+ const alreadyHasPushary = JSON.stringify(preToolUse).includes("pushary-hook");
45
+ if (!alreadyHasPushary) {
46
+ preToolUse.push({
47
+ matcher: "Bash|Write|Edit",
48
+ hooks: [{
49
+ type: "command",
50
+ command: "pushary-hook",
51
+ timeout: 120
52
+ }]
53
+ });
54
+ hooks.PreToolUse = preToolUse;
55
+ settings.hooks = hooks;
56
+ }
57
+ }
58
+ writeJson(CLAUDE_SETTINGS, settings);
59
+ console.log(` ${green("done")} MCP server added to ${dim(CLAUDE_SETTINGS)}`);
60
+ if (settings.hooks) {
61
+ console.log(` ${green("done")} Permission hooks configured`);
62
+ }
63
+ };
64
+ var setupCursor = async (apiKey) => {
65
+ console.log(`
66
+ ${bold("Cursor Setup")}
67
+ `);
68
+ const config = readJson(CURSOR_MCP);
69
+ const mcpServers = config.mcpServers ?? {};
70
+ mcpServers.pushary = {
71
+ url: "https://pushary.com/api/mcp/mcp",
72
+ headers: { Authorization: `Bearer ${apiKey}` }
73
+ };
74
+ config.mcpServers = mcpServers;
75
+ writeJson(CURSOR_MCP, config);
76
+ console.log(` ${green("done")} MCP server added to ${dim(CURSOR_MCP)}`);
77
+ };
78
+ var saveApiKey = (apiKey) => {
79
+ const exportLine = `
80
+ export PUSHARY_API_KEY="${apiKey}"
81
+ `;
82
+ const shellFile = SHELL_FILES.find((f) => existsSync(f));
83
+ if (shellFile) {
84
+ const content = readFileSync(shellFile, "utf-8");
85
+ if (content.includes("PUSHARY_API_KEY")) {
86
+ console.log(` ${dim("PUSHARY_API_KEY already in")} ${shellFile}`);
87
+ } else {
88
+ appendFileSync(shellFile, exportLine, "utf-8");
89
+ console.log(` ${green("done")} Added PUSHARY_API_KEY to ${dim(shellFile)}`);
90
+ }
91
+ }
92
+ process.env.PUSHARY_API_KEY = apiKey;
93
+ };
94
+ var sendTestNotification = async (apiKey) => {
95
+ console.log(`
96
+ ${bold("Sending test notification...")}`);
97
+ try {
98
+ const response = await fetch("https://pushary.com/api/mcp/mcp", {
99
+ method: "POST",
100
+ headers: {
101
+ "Content-Type": "application/json",
102
+ Authorization: `Bearer ${apiKey}`
103
+ },
104
+ body: JSON.stringify({
105
+ jsonrpc: "2.0",
106
+ id: 1,
107
+ method: "tools/call",
108
+ params: {
109
+ name: "send_notification",
110
+ arguments: {
111
+ title: "Pushary is working",
112
+ body: "Your AI agent can now send you push notifications.",
113
+ agentName: "Pushary Setup"
114
+ }
115
+ }
116
+ })
117
+ });
118
+ if (response.ok) {
119
+ console.log(` ${green("done")} Check your phone!`);
120
+ } else {
121
+ console.log(` ${dim("Could not send test notification. Make sure you enabled notifications at pushary.com")}`);
122
+ }
123
+ } catch {
124
+ console.log(` ${dim("Could not reach Pushary API. Check your internet connection.")}`);
125
+ }
126
+ };
127
+ var main = async () => {
128
+ console.log(`
129
+ ${bold("Pushary Setup")}
130
+ `);
131
+ console.log(`Push notifications for your AI coding agents.
132
+ `);
133
+ const apiKey = await ask(`Paste your API key ${dim("(from pushary.com/dashboard/agent/settings)")}: `);
134
+ if (!apiKey.trim() || !apiKey.includes(".")) {
135
+ console.log("\nInvalid API key. Get yours at https://pushary.com/sign-up?from=ai-coding\n");
136
+ rl.close();
137
+ process.exit(1);
138
+ }
139
+ const trimmedKey = apiKey.trim();
140
+ console.log(`
141
+ Which agents do you use?
142
+ `);
143
+ console.log(` 1. ${cyan("Claude Code")}`);
144
+ console.log(` 2. ${cyan("Cursor")}`);
145
+ console.log(` 3. ${cyan("Both")}`);
146
+ console.log(` 4. ${cyan("Other")} ${dim("(just save the API key)")}`);
147
+ console.log();
148
+ const choice = await ask(`Choice ${dim("[1-4]")}: `);
149
+ saveApiKey(trimmedKey);
150
+ switch (choice.trim()) {
151
+ case "1":
152
+ await setupClaudeCode(trimmedKey);
153
+ break;
154
+ case "2":
155
+ await setupCursor(trimmedKey);
156
+ break;
157
+ case "3":
158
+ await setupClaudeCode(trimmedKey);
159
+ await setupCursor(trimmedKey);
160
+ break;
161
+ case "4":
162
+ default:
163
+ break;
164
+ }
165
+ const test = await ask(`
166
+ Send a test notification? ${dim("[Y/n]")} `);
167
+ if (test.toLowerCase() !== "n") {
168
+ await sendTestNotification(trimmedKey);
169
+ }
170
+ console.log(`
171
+ ${green("Setup complete.")} Your agents will now send you push notifications.
172
+ `);
173
+ console.log(`${dim("Next steps:")}`);
174
+ console.log(` ${dim("1.")} Enable notifications on your phone at ${cyan("pushary.com")}`);
175
+ console.log(` ${dim("2.")} Configure timeout policies at ${cyan("pushary.com/dashboard/agent/policies")}`);
176
+ console.log(` ${dim("3.")} Start coding with your agent
177
+ `);
178
+ rl.close();
179
+ };
180
+ main();
@@ -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.2.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",
@@ -14,14 +14,15 @@
14
14
  "main": "./dist/src/index.js",
15
15
  "types": "./dist/src/index.d.ts",
16
16
  "bin": {
17
- "pushary-hook": "./dist/bin/pushary-hook.js"
17
+ "pushary-hook": "./dist/bin/pushary-hook.js",
18
+ "pushary-setup": "./dist/bin/pushary-setup.js"
18
19
  },
19
20
  "files": [
20
21
  "dist"
21
22
  ],
22
23
  "scripts": {
23
- "build": "tsup src/index.ts bin/pushary-hook.ts --format esm --dts --outDir dist",
24
- "dev": "tsup src/index.ts bin/pushary-hook.ts --format esm --watch"
24
+ "build": "tsup src/index.ts bin/pushary-hook.ts bin/pushary-setup.ts --format esm --dts --outDir dist",
25
+ "dev": "tsup src/index.ts bin/pushary-hook.ts bin/pushary-setup.ts --format esm --watch"
25
26
  },
26
27
  "devDependencies": {
27
28
  "tsup": "^8.0.0",