@pushary/agent-hooks 0.8.2 → 0.9.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,109 @@
1
+ import {
2
+ DEFAULT_SESSION,
3
+ cancelQuestion,
4
+ describeToolCall,
5
+ getMachineId,
6
+ isDefaultSession,
7
+ listPendingQuestions,
8
+ removePendingQuestion,
9
+ removePendingSession
10
+ } from "./chunk-OF5WIOYS.js";
11
+ import {
12
+ withRetry
13
+ } from "./chunk-3MIR7ODJ.js";
14
+ import {
15
+ getApiKey,
16
+ getBaseUrl
17
+ } from "./chunk-VUNL35KE.js";
18
+
19
+ // src/events.ts
20
+ import { basename } from "path";
21
+ var cleanupPendingQuestions = async (sessionId) => {
22
+ try {
23
+ const files = listPendingQuestions(sessionId);
24
+ const apiKey = getApiKey();
25
+ for (const correlationId of files) {
26
+ try {
27
+ await cancelQuestion(apiKey, correlationId);
28
+ } catch {
29
+ }
30
+ removePendingQuestion(sessionId, correlationId);
31
+ }
32
+ if (!isDefaultSession(sessionId)) removePendingSession(sessionId);
33
+ } catch {
34
+ }
35
+ };
36
+ var reportEvent = async (event) => {
37
+ const apiKey = getApiKey();
38
+ const baseUrl = getBaseUrl();
39
+ await withRetry(async () => {
40
+ await fetch(`${baseUrl}/api/agent/event`, {
41
+ method: "POST",
42
+ headers: {
43
+ "Content-Type": "application/json",
44
+ "Authorization": `Bearer ${apiKey}`
45
+ },
46
+ body: JSON.stringify({
47
+ ...event,
48
+ machineId: event.machineId ?? getMachineId()
49
+ }),
50
+ signal: AbortSignal.timeout(1e4)
51
+ });
52
+ }, { maxAttempts: 2, baseDelayMs: 300 });
53
+ };
54
+ var handlePostToolUse = async (input) => {
55
+ try {
56
+ const projectName = basename(input.cwd ?? process.cwd());
57
+ const action = describeToolCall(input.tool_name, input.tool_input, "event");
58
+ const isError = input.tool_result && ("error" in input.tool_result || "is_error" in input.tool_result);
59
+ await Promise.allSettled([
60
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
61
+ reportEvent({
62
+ event: isError ? "tool_error" : "tool_complete",
63
+ agentType: "claude_code",
64
+ agentName: `Claude Code - ${projectName}`,
65
+ action,
66
+ sessionId: input.session_id,
67
+ error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0
68
+ })
69
+ ]);
70
+ } catch {
71
+ }
72
+ };
73
+ var handleStop = async (input) => {
74
+ try {
75
+ const projectName = basename(input.cwd ?? process.cwd());
76
+ await Promise.allSettled([
77
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
78
+ reportEvent({
79
+ event: "session_end",
80
+ agentType: "claude_code",
81
+ agentName: `Claude Code - ${projectName}`,
82
+ action: "Session ended",
83
+ sessionId: input.session_id
84
+ })
85
+ ]);
86
+ } catch {
87
+ }
88
+ };
89
+ var handleNotification = async (input) => {
90
+ try {
91
+ const projectName = basename(input.cwd ?? process.cwd());
92
+ await reportEvent({
93
+ event: input.type === "error" ? "error" : "notification",
94
+ agentType: "claude_code",
95
+ agentName: `Claude Code - ${projectName}`,
96
+ action: input.title ?? input.message ?? "Notification",
97
+ sessionId: input.session_id,
98
+ error: input.type === "error" ? input.message : void 0
99
+ });
100
+ } catch {
101
+ }
102
+ };
103
+
104
+ export {
105
+ reportEvent,
106
+ handlePostToolUse,
107
+ handleStop,
108
+ handleNotification
109
+ };
@@ -0,0 +1,10 @@
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 API_KEY_PATTERN = /^pk_[a-f0-9]+\.[a-f0-9]+$/;
5
+ var isValidApiKey = (value) => API_KEY_PATTERN.test(value);
6
+
7
+ export {
8
+ isApprovalMode,
9
+ isValidApiKey
10
+ };
@@ -0,0 +1,247 @@
1
+ import {
2
+ isApprovalMode
3
+ } from "./chunk-IBWCHA5M.js";
4
+ import {
5
+ askUser,
6
+ describeToolCall,
7
+ isPolicyConfig,
8
+ savePendingQuestion,
9
+ sendNotification,
10
+ waitForAnswer
11
+ } from "./chunk-7PTU7TGE.js";
12
+ import {
13
+ withRetry
14
+ } from "./chunk-3MIR7ODJ.js";
15
+ import {
16
+ getApiKey,
17
+ getBaseUrl
18
+ } from "./chunk-VUNL35KE.js";
19
+
20
+ // src/policy.ts
21
+ import { createHash } from "crypto";
22
+ import { existsSync, readFileSync, writeFileSync } from "fs";
23
+ import { join } from "path";
24
+ import { tmpdir } from "os";
25
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
26
+ var cacheFile = (apiKey) => {
27
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
28
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
29
+ };
30
+ var fetchPolicy = async (apiKey) => {
31
+ return withRetry(async () => {
32
+ const baseUrl = getBaseUrl();
33
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
34
+ headers: { "Authorization": `Bearer ${apiKey}` },
35
+ signal: AbortSignal.timeout(1e4)
36
+ });
37
+ if (!response.ok) {
38
+ throw new Error(`Failed to fetch policy: ${response.status}`);
39
+ }
40
+ const raw = await response.json();
41
+ if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
42
+ return raw;
43
+ }, { maxAttempts: 2 });
44
+ };
45
+ var getPolicy = async (apiKey) => {
46
+ const path = cacheFile(apiKey);
47
+ let staleCache = null;
48
+ if (existsSync(path)) {
49
+ try {
50
+ const stat = readFileSync(path, "utf-8");
51
+ const cached = JSON.parse(stat);
52
+ if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
53
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
54
+ return cached;
55
+ }
56
+ staleCache = cached;
57
+ } catch {
58
+ }
59
+ }
60
+ try {
61
+ const policy = await fetchPolicy(apiKey);
62
+ try {
63
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
64
+ } catch {
65
+ }
66
+ return policy;
67
+ } catch {
68
+ if (staleCache) return staleCache;
69
+ throw new Error("Failed to fetch policy and no cached policy available");
70
+ }
71
+ };
72
+ var resolvePolicy = (config, toolName, modeOverride) => {
73
+ const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
74
+ tool: toolName,
75
+ timeoutSeconds: config.defaultTimeoutSeconds,
76
+ timeoutAction: config.defaultTimeoutAction,
77
+ mode: config.defaultMode ?? "push_first",
78
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
79
+ };
80
+ const effectiveOverride = modeOverride ?? config.modeOverride;
81
+ if (effectiveOverride) {
82
+ return { ...base, mode: effectiveOverride };
83
+ }
84
+ return base;
85
+ };
86
+ var fetchModeOverride = async (apiKey) => {
87
+ try {
88
+ const baseUrl = getBaseUrl();
89
+ const response = await fetch(`${baseUrl}/api/mcp/mode`, {
90
+ headers: { "Authorization": `Bearer ${apiKey}` },
91
+ signal: AbortSignal.timeout(3e3)
92
+ });
93
+ if (!response.ok) return null;
94
+ const data = await response.json();
95
+ const mode = data.override?.mode;
96
+ if (isApprovalMode(mode)) {
97
+ return mode;
98
+ }
99
+ return null;
100
+ } catch {
101
+ return null;
102
+ }
103
+ };
104
+
105
+ // src/hook.ts
106
+ import { basename } from "path";
107
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
108
+ var allow = () => ({
109
+ hookSpecificOutput: {
110
+ hookEventName: "PreToolUse",
111
+ permissionDecision: "allow"
112
+ }
113
+ });
114
+ var deny = (reason) => ({
115
+ hookSpecificOutput: {
116
+ hookEventName: "PreToolUse",
117
+ permissionDecision: "deny",
118
+ permissionDecisionReason: reason
119
+ }
120
+ });
121
+ var ask = (reason) => ({
122
+ hookSpecificOutput: {
123
+ hookEventName: "PreToolUse",
124
+ permissionDecision: "ask",
125
+ ...reason ? { permissionDecisionReason: reason } : {}
126
+ }
127
+ });
128
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
129
+ while (Date.now() < deadlineMs) {
130
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
131
+ let answer;
132
+ try {
133
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
134
+ } catch {
135
+ if (Date.now() + pollInterval >= deadlineMs) break;
136
+ await sleep(pollInterval);
137
+ continue;
138
+ }
139
+ if (answer.answered) return answer;
140
+ if (Date.now() + pollInterval >= deadlineMs) break;
141
+ await sleep(pollInterval);
142
+ }
143
+ return { answered: false };
144
+ };
145
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
146
+ let result;
147
+ try {
148
+ result = await askUser(apiKey, {
149
+ question: `Allow ${description}?`,
150
+ type: "confirm",
151
+ context: `Agent wants to run this in ${projectName}`,
152
+ agentName: `Claude Code - ${projectName}`
153
+ });
154
+ } catch {
155
+ switch (timeoutAction) {
156
+ case "approve":
157
+ return allow();
158
+ case "deny":
159
+ return deny("Push notification failed, denying per policy");
160
+ default:
161
+ return ask("Push notification failed, asking in terminal");
162
+ }
163
+ }
164
+ const deadline = Date.now() + timeoutSeconds * 1e3;
165
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
166
+ if (answer.answered) {
167
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
168
+ }
169
+ switch (timeoutAction) {
170
+ case "approve":
171
+ return allow();
172
+ case "deny":
173
+ return deny("No response within timeout");
174
+ default:
175
+ return ask("No push response, asking in terminal");
176
+ }
177
+ };
178
+ var handleTerminalOnly = () => {
179
+ return ask();
180
+ };
181
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
182
+ let result;
183
+ try {
184
+ result = await askUser(apiKey, {
185
+ question: `Allow ${description}?`,
186
+ type: "confirm",
187
+ context: `Agent wants to run this in ${projectName}`,
188
+ agentName: `Claude Code - ${projectName}`
189
+ });
190
+ } catch {
191
+ return ask("Push notification failed, asking in terminal");
192
+ }
193
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
194
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
195
+ if (answer.answered) {
196
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
197
+ }
198
+ savePendingQuestion(result.correlationId);
199
+ return ask("Sent as push notification. You can also approve here.");
200
+ };
201
+ var handleNotifyOnly = async (apiKey, description, projectName) => {
202
+ try {
203
+ await sendNotification(apiKey, {
204
+ title: "Agent needs approval",
205
+ body: description,
206
+ agentName: `Claude Code - ${projectName}`
207
+ });
208
+ } catch {
209
+ }
210
+ return ask();
211
+ };
212
+ var handlePreToolUse = async (input) => {
213
+ try {
214
+ const apiKey = getApiKey();
215
+ const [policy, modeOverride] = await Promise.all([
216
+ getPolicy(apiKey),
217
+ fetchModeOverride(apiKey)
218
+ ]);
219
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeOverride);
220
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
221
+ return allow();
222
+ }
223
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
224
+ const projectName = basename(input.cwd ?? process.cwd());
225
+ switch (toolPolicy.mode) {
226
+ case "push_only":
227
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
228
+ case "terminal_only":
229
+ return handleTerminalOnly();
230
+ case "push_first":
231
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
232
+ case "notify_only":
233
+ return handleNotifyOnly(apiKey, description, projectName);
234
+ default:
235
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
236
+ }
237
+ } catch {
238
+ return ask("Pushary unavailable, falling back to terminal approval");
239
+ }
240
+ };
241
+
242
+ export {
243
+ getPolicy,
244
+ resolvePolicy,
245
+ fetchModeOverride,
246
+ handlePreToolUse
247
+ };
@@ -0,0 +1,244 @@
1
+ import {
2
+ askUser,
3
+ describeToolCall,
4
+ isPolicyConfig,
5
+ savePendingQuestion,
6
+ sendNotification,
7
+ waitForAnswer
8
+ } from "./chunk-7PTU7TGE.js";
9
+ import {
10
+ withRetry
11
+ } from "./chunk-3MIR7ODJ.js";
12
+ import {
13
+ getApiKey,
14
+ getBaseUrl
15
+ } from "./chunk-VUNL35KE.js";
16
+
17
+ // src/policy.ts
18
+ import { createHash } from "crypto";
19
+ import { existsSync, readFileSync, writeFileSync } from "fs";
20
+ import { join } from "path";
21
+ import { tmpdir } from "os";
22
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
23
+ var cacheFile = (apiKey) => {
24
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
25
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
26
+ };
27
+ var fetchPolicy = async (apiKey) => {
28
+ return withRetry(async () => {
29
+ const baseUrl = getBaseUrl();
30
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
31
+ headers: { "Authorization": `Bearer ${apiKey}` },
32
+ signal: AbortSignal.timeout(1e4)
33
+ });
34
+ if (!response.ok) {
35
+ throw new Error(`Failed to fetch policy: ${response.status}`);
36
+ }
37
+ const raw = await response.json();
38
+ if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
39
+ return raw;
40
+ }, { maxAttempts: 2 });
41
+ };
42
+ var getPolicy = async (apiKey) => {
43
+ const path = cacheFile(apiKey);
44
+ let staleCache = null;
45
+ if (existsSync(path)) {
46
+ try {
47
+ const stat = readFileSync(path, "utf-8");
48
+ const cached = JSON.parse(stat);
49
+ if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
50
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
51
+ return cached;
52
+ }
53
+ staleCache = cached;
54
+ } catch {
55
+ }
56
+ }
57
+ try {
58
+ const policy = await fetchPolicy(apiKey);
59
+ try {
60
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
61
+ } catch {
62
+ }
63
+ return policy;
64
+ } catch {
65
+ if (staleCache) return staleCache;
66
+ throw new Error("Failed to fetch policy and no cached policy available");
67
+ }
68
+ };
69
+ var resolvePolicy = (config, toolName, modeOverride) => {
70
+ const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
71
+ tool: toolName,
72
+ timeoutSeconds: config.defaultTimeoutSeconds,
73
+ timeoutAction: config.defaultTimeoutAction,
74
+ mode: config.defaultMode ?? "push_first",
75
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
76
+ };
77
+ const effectiveOverride = modeOverride ?? config.modeOverride;
78
+ if (effectiveOverride) {
79
+ return { ...base, mode: effectiveOverride };
80
+ }
81
+ return base;
82
+ };
83
+ var fetchModeOverride = async (apiKey) => {
84
+ try {
85
+ const baseUrl = getBaseUrl();
86
+ const response = await fetch(`${baseUrl}/api/mcp/mode`, {
87
+ headers: { "Authorization": `Bearer ${apiKey}` },
88
+ signal: AbortSignal.timeout(3e3)
89
+ });
90
+ if (!response.ok) return null;
91
+ const data = await response.json();
92
+ const mode = data.override?.mode;
93
+ if (mode && ["push_only", "terminal_only", "push_first", "notify_only"].includes(mode)) {
94
+ return mode;
95
+ }
96
+ return null;
97
+ } catch {
98
+ return null;
99
+ }
100
+ };
101
+
102
+ // src/hook.ts
103
+ import { basename } from "path";
104
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
105
+ var allow = () => ({
106
+ hookSpecificOutput: {
107
+ hookEventName: "PreToolUse",
108
+ permissionDecision: "allow"
109
+ }
110
+ });
111
+ var deny = (reason) => ({
112
+ hookSpecificOutput: {
113
+ hookEventName: "PreToolUse",
114
+ permissionDecision: "deny",
115
+ permissionDecisionReason: reason
116
+ }
117
+ });
118
+ var ask = (reason) => ({
119
+ hookSpecificOutput: {
120
+ hookEventName: "PreToolUse",
121
+ permissionDecision: "ask",
122
+ ...reason ? { permissionDecisionReason: reason } : {}
123
+ }
124
+ });
125
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
126
+ while (Date.now() < deadlineMs) {
127
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
128
+ let answer;
129
+ try {
130
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
131
+ } catch {
132
+ if (Date.now() + pollInterval >= deadlineMs) break;
133
+ await sleep(pollInterval);
134
+ continue;
135
+ }
136
+ if (answer.answered) return answer;
137
+ if (Date.now() + pollInterval >= deadlineMs) break;
138
+ await sleep(pollInterval);
139
+ }
140
+ return { answered: false };
141
+ };
142
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
143
+ let result;
144
+ try {
145
+ result = await askUser(apiKey, {
146
+ question: `Allow ${description}?`,
147
+ type: "confirm",
148
+ context: `Agent wants to run this in ${projectName}`,
149
+ agentName: `Claude Code - ${projectName}`
150
+ });
151
+ } catch {
152
+ switch (timeoutAction) {
153
+ case "approve":
154
+ return allow();
155
+ case "deny":
156
+ return deny("Push notification failed, denying per policy");
157
+ default:
158
+ return ask("Push notification failed, asking in terminal");
159
+ }
160
+ }
161
+ const deadline = Date.now() + timeoutSeconds * 1e3;
162
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
163
+ if (answer.answered) {
164
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
165
+ }
166
+ switch (timeoutAction) {
167
+ case "approve":
168
+ return allow();
169
+ case "deny":
170
+ return deny("No response within timeout");
171
+ default:
172
+ return ask("No push response, asking in terminal");
173
+ }
174
+ };
175
+ var handleTerminalOnly = () => {
176
+ return ask();
177
+ };
178
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
179
+ let result;
180
+ try {
181
+ result = await askUser(apiKey, {
182
+ question: `Allow ${description}?`,
183
+ type: "confirm",
184
+ context: `Agent wants to run this in ${projectName}`,
185
+ agentName: `Claude Code - ${projectName}`
186
+ });
187
+ } catch {
188
+ return ask("Push notification failed, asking in terminal");
189
+ }
190
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
191
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
192
+ if (answer.answered) {
193
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
194
+ }
195
+ savePendingQuestion(result.correlationId);
196
+ return ask("Sent as push notification. You can also approve here.");
197
+ };
198
+ var handleNotifyOnly = async (apiKey, description, projectName) => {
199
+ try {
200
+ await sendNotification(apiKey, {
201
+ title: "Agent needs approval",
202
+ body: description,
203
+ agentName: `Claude Code - ${projectName}`
204
+ });
205
+ } catch {
206
+ }
207
+ return ask();
208
+ };
209
+ var handlePreToolUse = async (input) => {
210
+ try {
211
+ const apiKey = getApiKey();
212
+ const [policy, modeOverride] = await Promise.all([
213
+ getPolicy(apiKey),
214
+ fetchModeOverride(apiKey)
215
+ ]);
216
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeOverride);
217
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
218
+ return allow();
219
+ }
220
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
221
+ const projectName = basename(input.cwd ?? process.cwd());
222
+ switch (toolPolicy.mode) {
223
+ case "push_only":
224
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
225
+ case "terminal_only":
226
+ return handleTerminalOnly();
227
+ case "push_first":
228
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
229
+ case "notify_only":
230
+ return handleNotifyOnly(apiKey, description, projectName);
231
+ default:
232
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
233
+ }
234
+ } catch {
235
+ return ask("Pushary unavailable, falling back to terminal approval");
236
+ }
237
+ };
238
+
239
+ export {
240
+ getPolicy,
241
+ resolvePolicy,
242
+ fetchModeOverride,
243
+ handlePreToolUse
244
+ };