@pushary/agent-hooks 0.23.0 → 0.24.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,194 @@
1
+ import {
2
+ denyReasonFrom,
3
+ isDeferAnswer
4
+ } from "./chunk-KQYIHZ5E.js";
5
+ import {
6
+ DEFAULT_SESSION,
7
+ askUser,
8
+ deriveAction,
9
+ deriveActionBody,
10
+ deriveBlocker,
11
+ deriveToolTarget,
12
+ describeToolCall,
13
+ fetchModeState,
14
+ getMachineId,
15
+ getPolicy,
16
+ readLastPrompt,
17
+ readLastUserPrompt,
18
+ resolvePolicy,
19
+ savePendingQuestion,
20
+ sendNotification,
21
+ waitForAnswer
22
+ } from "./chunk-OQZH5PVA.js";
23
+ import {
24
+ getApiKey
25
+ } from "./chunk-NKXSILEW.js";
26
+
27
+ // src/hook.ts
28
+ import { basename } from "path";
29
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
30
+ var allow = () => ({
31
+ hookSpecificOutput: {
32
+ hookEventName: "PreToolUse",
33
+ permissionDecision: "allow"
34
+ }
35
+ });
36
+ var deny = (reason) => ({
37
+ hookSpecificOutput: {
38
+ hookEventName: "PreToolUse",
39
+ permissionDecision: "deny",
40
+ permissionDecisionReason: reason
41
+ }
42
+ });
43
+ var ask = (reason) => ({
44
+ hookSpecificOutput: {
45
+ hookEventName: "PreToolUse",
46
+ permissionDecision: "ask",
47
+ ...reason ? { permissionDecisionReason: reason } : {}
48
+ }
49
+ });
50
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
51
+ while (Date.now() < deadlineMs) {
52
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
53
+ let answer;
54
+ try {
55
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
56
+ } catch {
57
+ if (Date.now() + pollInterval >= deadlineMs) break;
58
+ await sleep(pollInterval);
59
+ continue;
60
+ }
61
+ if (answer.answered) return answer;
62
+ if (Date.now() + pollInterval >= deadlineMs) break;
63
+ await sleep(pollInterval);
64
+ }
65
+ return { answered: false };
66
+ };
67
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction, sessionId, machineId, toolName, toolTarget, decision) => {
68
+ let result;
69
+ try {
70
+ result = await askUser(apiKey, {
71
+ question: `Allow ${description}?`,
72
+ type: "confirm",
73
+ context: `Agent wants to run this in ${projectName}`,
74
+ agentName: `Claude Code - ${projectName}`,
75
+ sessionId,
76
+ machineId,
77
+ toolName,
78
+ toolTarget,
79
+ ...decision
80
+ });
81
+ } catch {
82
+ switch (timeoutAction) {
83
+ case "approve":
84
+ return allow();
85
+ case "deny":
86
+ return deny("Push notification failed, denying per policy");
87
+ default:
88
+ return ask("Push notification failed, asking in terminal");
89
+ }
90
+ }
91
+ const deadline = Date.now() + timeoutSeconds * 1e3;
92
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
93
+ if (answer.answered) {
94
+ if (isDeferAnswer(answer.value)) return ask("Handling on your machine");
95
+ return answer.value === "yes" ? allow() : deny(denyReasonFrom(answer.value));
96
+ }
97
+ switch (timeoutAction) {
98
+ case "approve":
99
+ return allow();
100
+ case "deny":
101
+ return deny("No response within timeout");
102
+ default:
103
+ return ask("No push response, asking in terminal");
104
+ }
105
+ };
106
+ var handleTerminalOnly = () => {
107
+ return ask();
108
+ };
109
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds, sessionId, machineId, toolName, toolTarget, decision) => {
110
+ let result;
111
+ try {
112
+ result = await askUser(apiKey, {
113
+ question: `Allow ${description}?`,
114
+ type: "confirm",
115
+ context: `Agent wants to run this in ${projectName}`,
116
+ agentName: `Claude Code - ${projectName}`,
117
+ sessionId,
118
+ machineId,
119
+ toolName,
120
+ toolTarget,
121
+ ...decision
122
+ });
123
+ } catch {
124
+ return ask("Push notification failed, asking in terminal");
125
+ }
126
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
127
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
128
+ if (answer.answered) {
129
+ if (isDeferAnswer(answer.value)) return ask("Handling on your machine");
130
+ return answer.value === "yes" ? allow() : deny(denyReasonFrom(answer.value));
131
+ }
132
+ savePendingQuestion(sessionId || DEFAULT_SESSION, result.correlationId);
133
+ return ask("Sent as push notification. You can also approve here.");
134
+ };
135
+ var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machineId) => {
136
+ try {
137
+ await sendNotification(apiKey, {
138
+ title: "Agent needs approval",
139
+ body: description,
140
+ agentName: `Claude Code - ${projectName}`,
141
+ sessionId,
142
+ machineId
143
+ });
144
+ } catch {
145
+ }
146
+ return ask();
147
+ };
148
+ var handlePreToolUse = async (input) => {
149
+ try {
150
+ const apiKey = getApiKey();
151
+ const modeState = await fetchModeState(apiKey, input.session_id);
152
+ const policy = await getPolicy(apiKey, modeState.policyVersion);
153
+ if (modeState.kill) {
154
+ return deny("Stopped by user \u2014 this agent was halted from Pushary");
155
+ }
156
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
157
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
158
+ return allow();
159
+ }
160
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
161
+ return deny(`Denied by policy for ${toolPolicy.tool}`);
162
+ }
163
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
164
+ const projectName = basename(input.cwd ?? process.cwd());
165
+ const sessionId = input.session_id;
166
+ const machineId = getMachineId();
167
+ const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
168
+ const intent = readLastPrompt(sessionId ?? DEFAULT_SESSION) ?? (input.transcript_path ? readLastUserPrompt(input.transcript_path) : void 0);
169
+ const decision = {
170
+ intent,
171
+ action: deriveAction(input.tool_name, input.tool_input),
172
+ blocker: deriveBlocker(toolPolicy.mode),
173
+ actionBody: deriveActionBody(input.tool_name, input.tool_input)
174
+ };
175
+ switch (toolPolicy.mode) {
176
+ case "push_only":
177
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget, decision);
178
+ case "terminal_only":
179
+ return handleTerminalOnly();
180
+ case "push_first":
181
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
182
+ case "notify_only":
183
+ return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
184
+ default:
185
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget, decision);
186
+ }
187
+ } catch {
188
+ return ask("Pushary unavailable, falling back to terminal approval");
189
+ }
190
+ };
191
+
192
+ export {
193
+ handlePreToolUse
194
+ };
@@ -24,6 +24,17 @@ interface ReceiptMeta {
24
24
  ok: boolean;
25
25
  }
26
26
 
27
+ declare const getPolicy: (apiKey: string, expectedVersion?: string | null) => Promise<PolicyConfig>;
28
+ type AutoResolveOrigin = 'safe_readonly' | 'policy_timeout';
29
+ declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null, toolInput?: Record<string, unknown>) => ToolPolicy;
30
+ interface ModeState {
31
+ readonly mode: ApprovalMode | null;
32
+ readonly kill: boolean;
33
+ readonly policyVersion: string | null;
34
+ }
35
+ declare const fetchModeState: (apiKey: string, sessionId?: string) => Promise<ModeState>;
36
+ declare const fetchModeOverride: (apiKey: string) => Promise<ApprovalMode | null>;
37
+
27
38
  interface UsageReport {
28
39
  readonly tokensIn: number;
29
40
  readonly tokensOut: number;
@@ -46,6 +57,10 @@ interface AgentEvent {
46
57
  error?: string;
47
58
  taskTitle?: string;
48
59
  decisionSource?: DecisionSource;
60
+ decisionOrigin?: AutoResolveOrigin;
61
+ toolName?: string;
62
+ toolTarget?: string;
63
+ agents?: readonly string[];
49
64
  meta?: ReceiptMeta;
50
65
  usage?: UsageReport;
51
66
  }
@@ -111,16 +126,6 @@ declare const askUser: (apiKey: string, params: AskUserParams, timeoutMs?: numbe
111
126
  declare const waitForAnswer: (apiKey: string, correlationId: string, timeoutMs?: number) => Promise<WaitForAnswerResponse>;
112
127
  declare const cancelQuestion: (apiKey: string, correlationId: string) => Promise<void>;
113
128
 
114
- declare const getPolicy: (apiKey: string, expectedVersion?: string | null) => Promise<PolicyConfig>;
115
- declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null, toolInput?: Record<string, unknown>) => ToolPolicy;
116
- interface ModeState {
117
- readonly mode: ApprovalMode | null;
118
- readonly kill: boolean;
119
- readonly policyVersion: string | null;
120
- }
121
- declare const fetchModeState: (apiKey: string, sessionId?: string) => Promise<ModeState>;
122
- declare const fetchModeOverride: (apiKey: string) => Promise<ApprovalMode | null>;
123
-
124
129
  declare const getApiKey: () => string;
125
130
  declare const getBaseUrl: () => string;
126
131
 
package/dist/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  handlePreToolUse
3
- } from "../chunk-FGZZRCOG.js";
3
+ } from "../chunk-U4SSWRVM.js";
4
4
  import "../chunk-KQYIHZ5E.js";
5
5
  import {
6
6
  askUser,
@@ -14,8 +14,7 @@ import {
14
14
  reportEvent,
15
15
  resolvePolicy,
16
16
  waitForAnswer
17
- } from "../chunk-M4GDBGXF.js";
18
- import "../chunk-WPVL6VIT.js";
17
+ } from "../chunk-OQZH5PVA.js";
19
18
  import "../chunk-DWED7BS3.js";
20
19
  import {
21
20
  getApiKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.23.0",
3
+ "version": "0.24.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",