@pushary/agent-hooks 0.14.3 → 0.15.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,174 @@
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 allow = () => ({
22
+ hookSpecificOutput: {
23
+ hookEventName: "PreToolUse",
24
+ permissionDecision: "allow"
25
+ }
26
+ });
27
+ var deny = (reason) => ({
28
+ hookSpecificOutput: {
29
+ hookEventName: "PreToolUse",
30
+ permissionDecision: "deny",
31
+ permissionDecisionReason: reason
32
+ }
33
+ });
34
+ var ask = (reason) => ({
35
+ hookSpecificOutput: {
36
+ hookEventName: "PreToolUse",
37
+ permissionDecision: "ask",
38
+ ...reason ? { permissionDecisionReason: reason } : {}
39
+ }
40
+ });
41
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
42
+ while (Date.now() < deadlineMs) {
43
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
44
+ let answer;
45
+ try {
46
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
47
+ } catch {
48
+ if (Date.now() + pollInterval >= deadlineMs) break;
49
+ await sleep(pollInterval);
50
+ continue;
51
+ }
52
+ if (answer.answered) return answer;
53
+ if (Date.now() + pollInterval >= deadlineMs) break;
54
+ await sleep(pollInterval);
55
+ }
56
+ return { answered: false };
57
+ };
58
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction, sessionId, machineId, toolName, toolTarget) => {
59
+ let result;
60
+ try {
61
+ result = await askUser(apiKey, {
62
+ question: `Allow ${description}?`,
63
+ type: "confirm",
64
+ context: `Agent wants to run this in ${projectName}`,
65
+ agentName: `Claude Code - ${projectName}`,
66
+ sessionId,
67
+ machineId,
68
+ toolName,
69
+ toolTarget
70
+ });
71
+ } catch {
72
+ switch (timeoutAction) {
73
+ case "approve":
74
+ return allow();
75
+ case "deny":
76
+ return deny("Push notification failed, denying per policy");
77
+ default:
78
+ return ask("Push notification failed, asking in terminal");
79
+ }
80
+ }
81
+ const deadline = Date.now() + timeoutSeconds * 1e3;
82
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
83
+ if (answer.answered) {
84
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
85
+ }
86
+ switch (timeoutAction) {
87
+ case "approve":
88
+ return allow();
89
+ case "deny":
90
+ return deny("No response within timeout");
91
+ default:
92
+ return ask("No push response, asking in terminal");
93
+ }
94
+ };
95
+ var handleTerminalOnly = () => {
96
+ return ask();
97
+ };
98
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds, sessionId, machineId, toolName, toolTarget) => {
99
+ let result;
100
+ try {
101
+ result = await askUser(apiKey, {
102
+ question: `Allow ${description}?`,
103
+ type: "confirm",
104
+ context: `Agent wants to run this in ${projectName}`,
105
+ agentName: `Claude Code - ${projectName}`,
106
+ sessionId,
107
+ machineId,
108
+ toolName,
109
+ toolTarget
110
+ });
111
+ } catch {
112
+ return ask("Push notification failed, asking in terminal");
113
+ }
114
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
115
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
116
+ if (answer.answered) {
117
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
118
+ }
119
+ savePendingQuestion(sessionId || DEFAULT_SESSION, result.correlationId);
120
+ return ask("Sent as push notification. You can also approve here.");
121
+ };
122
+ var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machineId) => {
123
+ try {
124
+ await sendNotification(apiKey, {
125
+ title: "Agent needs approval",
126
+ body: description,
127
+ agentName: `Claude Code - ${projectName}`,
128
+ sessionId,
129
+ machineId
130
+ });
131
+ } catch {
132
+ }
133
+ return ask();
134
+ };
135
+ var handlePreToolUse = async (input) => {
136
+ try {
137
+ const apiKey = getApiKey();
138
+ const modeState = await fetchModeState(apiKey, input.session_id);
139
+ const policy = await getPolicy(apiKey, modeState.policyVersion);
140
+ if (modeState.kill) {
141
+ return deny("Stopped by user \u2014 this agent was halted from Pushary");
142
+ }
143
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
144
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
145
+ return allow();
146
+ }
147
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
148
+ return deny(`Denied by policy for ${toolPolicy.tool}`);
149
+ }
150
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
151
+ const projectName = basename(input.cwd ?? process.cwd());
152
+ const sessionId = input.session_id;
153
+ const machineId = getMachineId();
154
+ const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
155
+ switch (toolPolicy.mode) {
156
+ case "push_only":
157
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget);
158
+ case "terminal_only":
159
+ return handleTerminalOnly();
160
+ case "push_first":
161
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
162
+ case "notify_only":
163
+ return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
164
+ default:
165
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
166
+ }
167
+ } catch {
168
+ return ask("Pushary unavailable, falling back to terminal approval");
169
+ }
170
+ };
171
+
172
+ export {
173
+ handlePreToolUse
174
+ };
@@ -6,6 +6,7 @@ interface ToolInput {
6
6
  tool_input: Record<string, unknown>;
7
7
  session_id?: string;
8
8
  cwd?: string;
9
+ transcript_path?: string;
9
10
  }
10
11
  interface HookOutput {
11
12
  hookSpecificOutput: {
@@ -23,6 +24,13 @@ interface ReceiptMeta {
23
24
  ok: boolean;
24
25
  }
25
26
 
27
+ interface UsageReport {
28
+ readonly tokensIn: number;
29
+ readonly tokensOut: number;
30
+ readonly costUsd: number;
31
+ readonly deltaUsd: number;
32
+ }
33
+
26
34
  type DecisionSource = 'policy_auto' | 'human' | 'terminal';
27
35
  interface AgentIdentity {
28
36
  readonly type: string;
@@ -39,6 +47,7 @@ interface AgentEvent {
39
47
  taskTitle?: string;
40
48
  decisionSource?: DecisionSource;
41
49
  meta?: ReceiptMeta;
50
+ usage?: UsageReport;
42
51
  }
43
52
  interface ReportEventOptions {
44
53
  maxAttempts?: number;
@@ -51,11 +60,13 @@ declare const handlePostToolUse: (input: {
51
60
  tool_result?: Record<string, unknown>;
52
61
  cwd?: string;
53
62
  session_id?: string;
63
+ transcript_path?: string;
54
64
  }, agent?: AgentIdentity) => Promise<void>;
55
65
  declare const handleStop: (input: {
56
66
  cwd?: string;
57
67
  session_id?: string;
58
68
  stop_hook_active?: boolean;
69
+ transcript_path?: string;
59
70
  }, agent?: AgentIdentity) => Promise<void>;
60
71
  declare const handleNotification: (input: {
61
72
  message?: string;
package/dist/src/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  handlePreToolUse
3
- } from "../chunk-5QHQVXWN.js";
3
+ } from "../chunk-VWBNI4SC.js";
4
4
  import {
5
5
  handleNotification,
6
6
  handlePostToolUse,
7
7
  handleStop,
8
8
  reportEvent
9
- } from "../chunk-YCL4GUFR.js";
9
+ } from "../chunk-HWEMAAOY.js";
10
10
  import {
11
11
  askUser,
12
12
  cancelQuestion,
@@ -15,13 +15,13 @@ import {
15
15
  getPolicy,
16
16
  resolvePolicy,
17
17
  waitForAnswer
18
- } from "../chunk-QEPRH7JB.js";
18
+ } from "../chunk-HRQEECB6.js";
19
19
  import "../chunk-22CV7V7A.js";
20
- import "../chunk-3MIR7ODJ.js";
20
+ import "../chunk-DWED7BS3.js";
21
21
  import {
22
22
  getApiKey,
23
23
  getBaseUrl
24
- } from "../chunk-VUNL35KE.js";
24
+ } from "../chunk-NKXSILEW.js";
25
25
  export {
26
26
  askUser,
27
27
  cancelQuestion,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.14.3",
3
+ "version": "0.15.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",
@@ -34,7 +34,7 @@
34
34
  "scripts": {
35
35
  "build": "node scripts/bundle-plugin.mjs && tsup",
36
36
  "dev": "tsup --watch",
37
- "test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/suggestions.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts"
37
+ "test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.test.ts && bun test src/validate.test.ts && bun test src/policy.test.ts && bun test src/npm.test.ts && bun test src/identity.test.ts && bun test src/pending.test.ts && bun test src/events.test.ts && bun test src/describe.test.ts && bun test src/suggestions.test.ts && bun test src/hook.test.ts && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts"
38
38
  },
39
39
  "dependencies": {
40
40
  "@inquirer/prompts": "^8.4.2",
@@ -42,7 +42,7 @@
42
42
  "smol-toml": "^1.6.1"
43
43
  },
44
44
  "devDependencies": {
45
- "@pushary/contracts": "0.1.0",
45
+ "@pushary/contracts": "workspace:*",
46
46
  "tsup": "^8.0.0",
47
47
  "typescript": "^5.0.0"
48
48
  }