@pushary/agent-hooks 0.8.0 → 0.8.2
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.
- package/dist/bin/pushary-codex.js +2 -2
- package/dist/bin/pushary-hook.js +2 -2
- package/dist/bin/pushary-post-hook.js +2 -2
- package/dist/bin/pushary-setup.js +17 -2
- package/dist/bin/pushary-stop-hook.js +2 -2
- package/dist/chunk-2CRLBMOX.js +244 -0
- package/dist/chunk-KTM4ZQ3W.js +96 -0
- package/dist/chunk-OP4QAN23.js +99 -0
- package/dist/src/index.js +3 -3
- package/package.json +1 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
reportEvent
|
|
4
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-OP4QAN23.js";
|
|
5
5
|
import {
|
|
6
6
|
askUser,
|
|
7
7
|
waitForAnswer
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-KTM4ZQ3W.js";
|
|
9
9
|
import "../chunk-3MIR7ODJ.js";
|
|
10
10
|
import {
|
|
11
11
|
getApiKey
|
package/dist/bin/pushary-hook.js
CHANGED
|
@@ -26,6 +26,14 @@ var green = (s) => `\x1B[32m${s}\x1B[0m`;
|
|
|
26
26
|
var cyan = (s) => `\x1B[36m${s}\x1B[0m`;
|
|
27
27
|
var yellow = (s) => `\x1B[33m${s}\x1B[0m`;
|
|
28
28
|
var check = green("\u2713");
|
|
29
|
+
var parseKeyFlag = () => {
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
for (let i = 0; i < args.length; i++) {
|
|
32
|
+
if (args[i] === "--key" && args[i + 1]) return args[i + 1].trim();
|
|
33
|
+
if (args[i].startsWith("--key=")) return args[i].slice(6).trim();
|
|
34
|
+
}
|
|
35
|
+
return void 0;
|
|
36
|
+
};
|
|
29
37
|
var readJson = (path) => {
|
|
30
38
|
try {
|
|
31
39
|
return JSON.parse(readFileSync(path, "utf-8"));
|
|
@@ -394,9 +402,16 @@ var main = async () => {
|
|
|
394
402
|
console.log(` ${dim("Push notifications for AI coding agents")}`);
|
|
395
403
|
console.log();
|
|
396
404
|
await checkForUpdates(version);
|
|
405
|
+
const keyPattern = /^pk_[a-f0-9]+\.[a-f0-9]+$/;
|
|
406
|
+
const flagKey = parseKeyFlag();
|
|
397
407
|
const envKey = process.env.PUSHARY_API_KEY?.trim();
|
|
398
408
|
let trimmedKey;
|
|
399
|
-
if (
|
|
409
|
+
if (flagKey && keyPattern.test(flagKey)) {
|
|
410
|
+
const masked = `${flagKey.slice(0, 8)}...${flagKey.slice(-4)}`;
|
|
411
|
+
console.log(` ${check} Using API key: ${dim(masked)}`);
|
|
412
|
+
console.log();
|
|
413
|
+
trimmedKey = flagKey;
|
|
414
|
+
} else if (envKey && keyPattern.test(envKey)) {
|
|
400
415
|
const masked = `${envKey.slice(0, 8)}...${envKey.slice(-4)}`;
|
|
401
416
|
console.log(` ${check} Found API key in environment: ${dim(masked)}`);
|
|
402
417
|
console.log();
|
|
@@ -414,7 +429,7 @@ var main = async () => {
|
|
|
414
429
|
const apiKey = await input({ message: "API key:" });
|
|
415
430
|
trimmedKey = apiKey.trim();
|
|
416
431
|
}
|
|
417
|
-
if (!trimmedKey ||
|
|
432
|
+
if (!trimmedKey || !keyPattern.test(trimmedKey)) {
|
|
418
433
|
console.log(`
|
|
419
434
|
${yellow("!")} Invalid key format. Expected: ${dim("pk_xxx.xxx")}`);
|
|
420
435
|
console.log(` ${dim("Copy your key from")} ${cyan("https://pushary.com/dashboard/agent/settings")}`);
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import {
|
|
2
|
+
askUser,
|
|
3
|
+
describeToolCall,
|
|
4
|
+
isPolicyConfig,
|
|
5
|
+
savePendingQuestion,
|
|
6
|
+
sendNotification,
|
|
7
|
+
waitForAnswer
|
|
8
|
+
} from "./chunk-KTM4ZQ3W.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
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
callMcpTool
|
|
3
|
+
} from "./chunk-3MIR7ODJ.js";
|
|
4
|
+
|
|
5
|
+
// src/validate.ts
|
|
6
|
+
var isPolicyConfig = (data) => {
|
|
7
|
+
if (!data || typeof data !== "object") return false;
|
|
8
|
+
const d = data;
|
|
9
|
+
return Array.isArray(d.policies) && typeof d.defaultTimeoutSeconds === "number" && typeof d.defaultTimeoutAction === "string";
|
|
10
|
+
};
|
|
11
|
+
var isAskUserResponse = (data) => {
|
|
12
|
+
if (!data || typeof data !== "object") return false;
|
|
13
|
+
const d = data;
|
|
14
|
+
return typeof d.correlationId === "string" && typeof d.status === "string";
|
|
15
|
+
};
|
|
16
|
+
var isWaitForAnswerResponse = (data) => {
|
|
17
|
+
if (!data || typeof data !== "object") return false;
|
|
18
|
+
const d = data;
|
|
19
|
+
return typeof d.answered === "boolean";
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/api.ts
|
|
23
|
+
var askUser = async (apiKey, params) => {
|
|
24
|
+
const result = await callMcpTool(apiKey, "ask_user", { ...params, wait: false });
|
|
25
|
+
if (!isAskUserResponse(result)) throw new Error("Invalid ask_user response");
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
29
|
+
const result = await callMcpTool(apiKey, "wait_for_answer", {
|
|
30
|
+
correlationId,
|
|
31
|
+
timeoutMs
|
|
32
|
+
});
|
|
33
|
+
if (!isWaitForAnswerResponse(result)) throw new Error("Invalid wait_for_answer response");
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
var cancelQuestion = async (apiKey, correlationId) => {
|
|
37
|
+
await callMcpTool(apiKey, "cancel_question", { correlationId });
|
|
38
|
+
};
|
|
39
|
+
var sendNotification = async (apiKey, params) => {
|
|
40
|
+
await callMcpTool(apiKey, "send_notification", { ...params });
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/describe.ts
|
|
44
|
+
var hookPrefixes = {
|
|
45
|
+
Bash: (input) => `bash: ${input.command ?? "(no command)"}`,
|
|
46
|
+
Write: (input) => `write file: ${input.file_path ?? "(unknown path)"}`,
|
|
47
|
+
Edit: (input) => `edit file: ${input.file_path ?? "(unknown path)"}`,
|
|
48
|
+
Read: (input) => `read file: ${input.file_path ?? "(unknown path)"}`
|
|
49
|
+
};
|
|
50
|
+
var eventPrefixes = {
|
|
51
|
+
Bash: (input) => `ran: ${String(input.command ?? "").slice(0, 120)}`,
|
|
52
|
+
Write: (input) => `wrote: ${input.file_path ?? "unknown"}`,
|
|
53
|
+
Edit: (input) => `edited: ${input.file_path ?? "unknown"}`,
|
|
54
|
+
Read: (input) => `read: ${input.file_path ?? "unknown"}`
|
|
55
|
+
};
|
|
56
|
+
var describeToolCall = (toolName, toolInput, format = "hook") => {
|
|
57
|
+
const prefixes = format === "hook" ? hookPrefixes : eventPrefixes;
|
|
58
|
+
const builder = prefixes[toolName];
|
|
59
|
+
if (builder) return builder(toolInput);
|
|
60
|
+
return format === "hook" ? `${toolName}: ${JSON.stringify(toolInput).slice(0, 200)}` : `${toolName}: done`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/pending.ts
|
|
64
|
+
import { join } from "path";
|
|
65
|
+
import { tmpdir } from "os";
|
|
66
|
+
import { existsSync, mkdirSync, writeFileSync, readdirSync, unlinkSync } from "fs";
|
|
67
|
+
var PENDING_DIR = join(tmpdir(), "pushary-pending");
|
|
68
|
+
var savePendingQuestion = (correlationId) => {
|
|
69
|
+
if (!existsSync(PENDING_DIR)) mkdirSync(PENDING_DIR, { recursive: true });
|
|
70
|
+
writeFileSync(join(PENDING_DIR, correlationId), "", "utf-8");
|
|
71
|
+
};
|
|
72
|
+
var listPendingQuestions = () => {
|
|
73
|
+
try {
|
|
74
|
+
return readdirSync(PENDING_DIR);
|
|
75
|
+
} catch {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var removePendingQuestion = (correlationId) => {
|
|
80
|
+
try {
|
|
81
|
+
unlinkSync(join(PENDING_DIR, correlationId));
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
isPolicyConfig,
|
|
88
|
+
askUser,
|
|
89
|
+
waitForAnswer,
|
|
90
|
+
cancelQuestion,
|
|
91
|
+
sendNotification,
|
|
92
|
+
describeToolCall,
|
|
93
|
+
savePendingQuestion,
|
|
94
|
+
listPendingQuestions,
|
|
95
|
+
removePendingQuestion
|
|
96
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cancelQuestion,
|
|
3
|
+
describeToolCall,
|
|
4
|
+
listPendingQuestions,
|
|
5
|
+
removePendingQuestion
|
|
6
|
+
} from "./chunk-KTM4ZQ3W.js";
|
|
7
|
+
import {
|
|
8
|
+
withRetry
|
|
9
|
+
} from "./chunk-3MIR7ODJ.js";
|
|
10
|
+
import {
|
|
11
|
+
getApiKey,
|
|
12
|
+
getBaseUrl
|
|
13
|
+
} from "./chunk-VUNL35KE.js";
|
|
14
|
+
|
|
15
|
+
// src/events.ts
|
|
16
|
+
import { hostname } from "os";
|
|
17
|
+
import { basename } from "path";
|
|
18
|
+
var cleanupPendingQuestions = async () => {
|
|
19
|
+
try {
|
|
20
|
+
const files = listPendingQuestions();
|
|
21
|
+
const apiKey = getApiKey();
|
|
22
|
+
for (const correlationId of files) {
|
|
23
|
+
try {
|
|
24
|
+
await cancelQuestion(apiKey, correlationId);
|
|
25
|
+
} catch {
|
|
26
|
+
}
|
|
27
|
+
removePendingQuestion(correlationId);
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var reportEvent = async (event) => {
|
|
33
|
+
const apiKey = getApiKey();
|
|
34
|
+
const baseUrl = getBaseUrl();
|
|
35
|
+
await withRetry(async () => {
|
|
36
|
+
await fetch(`${baseUrl}/api/agent/event`, {
|
|
37
|
+
method: "POST",
|
|
38
|
+
headers: {
|
|
39
|
+
"Content-Type": "application/json",
|
|
40
|
+
"Authorization": `Bearer ${apiKey}`
|
|
41
|
+
},
|
|
42
|
+
body: JSON.stringify({
|
|
43
|
+
...event,
|
|
44
|
+
machineId: event.machineId ?? hostname()
|
|
45
|
+
}),
|
|
46
|
+
signal: AbortSignal.timeout(1e4)
|
|
47
|
+
});
|
|
48
|
+
}, { maxAttempts: 2, baseDelayMs: 300 });
|
|
49
|
+
};
|
|
50
|
+
var handlePostToolUse = async (input) => {
|
|
51
|
+
try {
|
|
52
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
53
|
+
const action = describeToolCall(input.tool_name, input.tool_input, "event");
|
|
54
|
+
const isError = input.tool_result && ("error" in input.tool_result || "is_error" in input.tool_result);
|
|
55
|
+
await Promise.allSettled([
|
|
56
|
+
cleanupPendingQuestions(),
|
|
57
|
+
reportEvent({
|
|
58
|
+
event: isError ? "tool_error" : "tool_complete",
|
|
59
|
+
agentType: "claude_code",
|
|
60
|
+
agentName: `Claude Code - ${projectName}`,
|
|
61
|
+
action,
|
|
62
|
+
error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0
|
|
63
|
+
})
|
|
64
|
+
]);
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var handleStop = async (input) => {
|
|
69
|
+
try {
|
|
70
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
71
|
+
await reportEvent({
|
|
72
|
+
event: "session_end",
|
|
73
|
+
agentType: "claude_code",
|
|
74
|
+
agentName: `Claude Code - ${projectName}`,
|
|
75
|
+
action: "Session ended"
|
|
76
|
+
});
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var handleNotification = async (input) => {
|
|
81
|
+
try {
|
|
82
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
83
|
+
await reportEvent({
|
|
84
|
+
event: input.type === "error" ? "error" : "notification",
|
|
85
|
+
agentType: "claude_code",
|
|
86
|
+
agentName: `Claude Code - ${projectName}`,
|
|
87
|
+
action: input.title ?? input.message ?? "Notification",
|
|
88
|
+
error: input.type === "error" ? input.message : void 0
|
|
89
|
+
});
|
|
90
|
+
} catch {
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export {
|
|
95
|
+
reportEvent,
|
|
96
|
+
handlePostToolUse,
|
|
97
|
+
handleStop,
|
|
98
|
+
handleNotification
|
|
99
|
+
};
|
package/dist/src/index.js
CHANGED
|
@@ -3,18 +3,18 @@ import {
|
|
|
3
3
|
getPolicy,
|
|
4
4
|
handlePreToolUse,
|
|
5
5
|
resolvePolicy
|
|
6
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-2CRLBMOX.js";
|
|
7
7
|
import {
|
|
8
8
|
handleNotification,
|
|
9
9
|
handlePostToolUse,
|
|
10
10
|
handleStop,
|
|
11
11
|
reportEvent
|
|
12
|
-
} from "../chunk-
|
|
12
|
+
} from "../chunk-OP4QAN23.js";
|
|
13
13
|
import {
|
|
14
14
|
askUser,
|
|
15
15
|
cancelQuestion,
|
|
16
16
|
waitForAnswer
|
|
17
|
-
} from "../chunk-
|
|
17
|
+
} from "../chunk-KTM4ZQ3W.js";
|
|
18
18
|
import "../chunk-3MIR7ODJ.js";
|
|
19
19
|
import {
|
|
20
20
|
getApiKey,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushary/agent-hooks",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
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",
|