@pushary/agent-hooks 0.16.0 → 0.17.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.
- package/dist/bin/pushary-hook.js +1 -1
- package/dist/bin/pushary-setup.js +2 -0
- package/dist/chunk-EQJXMEIR.js +175 -0
- package/dist/src/index.js +1 -1
- package/package.json +1 -1
package/dist/bin/pushary-hook.js
CHANGED
|
@@ -255,6 +255,8 @@ var connectViaAppPairing = async () => {
|
|
|
255
255
|
console.log(` ${bold("Connect your Pushary app")}`);
|
|
256
256
|
console.log(` ${dim("Open the Pushary app, tap Connect agent, and scan this:")}`);
|
|
257
257
|
await printQr(deepLink);
|
|
258
|
+
console.log(` ${dim("No camera handy? Open this link on the phone instead:")}`);
|
|
259
|
+
console.log(` ${cyan(deepLink)}`);
|
|
258
260
|
console.log(` ${dim("Confirm the app shows fingerprint")} ${cyan(publicKeyFingerprint(keypair.publicKeyB64))}`);
|
|
259
261
|
console.log();
|
|
260
262
|
const deadline = Date.now() + PAIR_TIMEOUT_MS;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_SESSION,
|
|
3
|
+
askUser,
|
|
4
|
+
deriveToolTarget,
|
|
5
|
+
describeToolCall,
|
|
6
|
+
fetchModeState,
|
|
7
|
+
getMachineId,
|
|
8
|
+
getPolicy,
|
|
9
|
+
resolvePolicy,
|
|
10
|
+
savePendingQuestion,
|
|
11
|
+
sendNotification,
|
|
12
|
+
waitForAnswer
|
|
13
|
+
} from "./chunk-HRQEECB6.js";
|
|
14
|
+
import {
|
|
15
|
+
getApiKey
|
|
16
|
+
} from "./chunk-NKXSILEW.js";
|
|
17
|
+
|
|
18
|
+
// src/hook.ts
|
|
19
|
+
import { basename } from "path";
|
|
20
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
21
|
+
var denyReasonFrom = (value) => value && value !== "no" && value !== "yes" ? `Denied from your phone: ${value}` : "Denied via push notification";
|
|
22
|
+
var allow = () => ({
|
|
23
|
+
hookSpecificOutput: {
|
|
24
|
+
hookEventName: "PreToolUse",
|
|
25
|
+
permissionDecision: "allow"
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
var deny = (reason) => ({
|
|
29
|
+
hookSpecificOutput: {
|
|
30
|
+
hookEventName: "PreToolUse",
|
|
31
|
+
permissionDecision: "deny",
|
|
32
|
+
permissionDecisionReason: reason
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
var ask = (reason) => ({
|
|
36
|
+
hookSpecificOutput: {
|
|
37
|
+
hookEventName: "PreToolUse",
|
|
38
|
+
permissionDecision: "ask",
|
|
39
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
|
|
43
|
+
while (Date.now() < deadlineMs) {
|
|
44
|
+
const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
|
|
45
|
+
let answer;
|
|
46
|
+
try {
|
|
47
|
+
answer = await waitForAnswer(apiKey, correlationId, remaining);
|
|
48
|
+
} catch {
|
|
49
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
50
|
+
await sleep(pollInterval);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (answer.answered) return answer;
|
|
54
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
55
|
+
await sleep(pollInterval);
|
|
56
|
+
}
|
|
57
|
+
return { answered: false };
|
|
58
|
+
};
|
|
59
|
+
var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction, sessionId, machineId, toolName, toolTarget) => {
|
|
60
|
+
let result;
|
|
61
|
+
try {
|
|
62
|
+
result = await askUser(apiKey, {
|
|
63
|
+
question: `Allow ${description}?`,
|
|
64
|
+
type: "confirm",
|
|
65
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
66
|
+
agentName: `Claude Code - ${projectName}`,
|
|
67
|
+
sessionId,
|
|
68
|
+
machineId,
|
|
69
|
+
toolName,
|
|
70
|
+
toolTarget
|
|
71
|
+
});
|
|
72
|
+
} catch {
|
|
73
|
+
switch (timeoutAction) {
|
|
74
|
+
case "approve":
|
|
75
|
+
return allow();
|
|
76
|
+
case "deny":
|
|
77
|
+
return deny("Push notification failed, denying per policy");
|
|
78
|
+
default:
|
|
79
|
+
return ask("Push notification failed, asking in terminal");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
83
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
|
|
84
|
+
if (answer.answered) {
|
|
85
|
+
return answer.value === "yes" ? allow() : deny(denyReasonFrom(answer.value));
|
|
86
|
+
}
|
|
87
|
+
switch (timeoutAction) {
|
|
88
|
+
case "approve":
|
|
89
|
+
return allow();
|
|
90
|
+
case "deny":
|
|
91
|
+
return deny("No response within timeout");
|
|
92
|
+
default:
|
|
93
|
+
return ask("No push response, asking in terminal");
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
var handleTerminalOnly = () => {
|
|
97
|
+
return ask();
|
|
98
|
+
};
|
|
99
|
+
var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds, sessionId, machineId, toolName, toolTarget) => {
|
|
100
|
+
let result;
|
|
101
|
+
try {
|
|
102
|
+
result = await askUser(apiKey, {
|
|
103
|
+
question: `Allow ${description}?`,
|
|
104
|
+
type: "confirm",
|
|
105
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
106
|
+
agentName: `Claude Code - ${projectName}`,
|
|
107
|
+
sessionId,
|
|
108
|
+
machineId,
|
|
109
|
+
toolName,
|
|
110
|
+
toolTarget
|
|
111
|
+
});
|
|
112
|
+
} catch {
|
|
113
|
+
return ask("Push notification failed, asking in terminal");
|
|
114
|
+
}
|
|
115
|
+
const deadline = Date.now() + pushFirstSeconds * 1e3;
|
|
116
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
|
|
117
|
+
if (answer.answered) {
|
|
118
|
+
return answer.value === "yes" ? allow() : deny(denyReasonFrom(answer.value));
|
|
119
|
+
}
|
|
120
|
+
savePendingQuestion(sessionId || DEFAULT_SESSION, result.correlationId);
|
|
121
|
+
return ask("Sent as push notification. You can also approve here.");
|
|
122
|
+
};
|
|
123
|
+
var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machineId) => {
|
|
124
|
+
try {
|
|
125
|
+
await sendNotification(apiKey, {
|
|
126
|
+
title: "Agent needs approval",
|
|
127
|
+
body: description,
|
|
128
|
+
agentName: `Claude Code - ${projectName}`,
|
|
129
|
+
sessionId,
|
|
130
|
+
machineId
|
|
131
|
+
});
|
|
132
|
+
} catch {
|
|
133
|
+
}
|
|
134
|
+
return ask();
|
|
135
|
+
};
|
|
136
|
+
var handlePreToolUse = async (input) => {
|
|
137
|
+
try {
|
|
138
|
+
const apiKey = getApiKey();
|
|
139
|
+
const modeState = await fetchModeState(apiKey, input.session_id);
|
|
140
|
+
const policy = await getPolicy(apiKey, modeState.policyVersion);
|
|
141
|
+
if (modeState.kill) {
|
|
142
|
+
return deny("Stopped by user \u2014 this agent was halted from Pushary");
|
|
143
|
+
}
|
|
144
|
+
const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
|
|
145
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
146
|
+
return allow();
|
|
147
|
+
}
|
|
148
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
|
|
149
|
+
return deny(`Denied by policy for ${toolPolicy.tool}`);
|
|
150
|
+
}
|
|
151
|
+
const description = describeToolCall(input.tool_name, input.tool_input, "hook");
|
|
152
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
153
|
+
const sessionId = input.session_id;
|
|
154
|
+
const machineId = getMachineId();
|
|
155
|
+
const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
|
|
156
|
+
switch (toolPolicy.mode) {
|
|
157
|
+
case "push_only":
|
|
158
|
+
return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget);
|
|
159
|
+
case "terminal_only":
|
|
160
|
+
return handleTerminalOnly();
|
|
161
|
+
case "push_first":
|
|
162
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
|
|
163
|
+
case "notify_only":
|
|
164
|
+
return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
|
|
165
|
+
default:
|
|
166
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
|
|
167
|
+
}
|
|
168
|
+
} catch {
|
|
169
|
+
return ask("Pushary unavailable, falling back to terminal approval");
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export {
|
|
174
|
+
handlePreToolUse
|
|
175
|
+
};
|
package/dist/src/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushary/agent-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.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",
|