@pushary/agent-hooks 0.12.0 → 0.14.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,176 @@
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-QRXWPZKN.js";
14
+ import {
15
+ getApiKey
16
+ } from "./chunk-VUNL35KE.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 [policy, modeState] = await Promise.all([
139
+ getPolicy(apiKey),
140
+ fetchModeState(apiKey, input.session_id)
141
+ ]);
142
+ if (modeState.kill) {
143
+ return deny("Stopped by user \u2014 this agent was halted from Pushary");
144
+ }
145
+ const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode, input.tool_input);
146
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
147
+ return allow();
148
+ }
149
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "deny") {
150
+ return deny(`Denied by policy for ${toolPolicy.tool}`);
151
+ }
152
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
153
+ const projectName = basename(input.cwd ?? process.cwd());
154
+ const sessionId = input.session_id;
155
+ const machineId = getMachineId();
156
+ const toolTarget = deriveToolTarget(input.tool_name, input.tool_input);
157
+ switch (toolPolicy.mode) {
158
+ case "push_only":
159
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name, toolTarget);
160
+ case "terminal_only":
161
+ return handleTerminalOnly();
162
+ case "push_first":
163
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
164
+ case "notify_only":
165
+ return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
166
+ default:
167
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name, toolTarget);
168
+ }
169
+ } catch {
170
+ return ask("Pushary unavailable, falling back to terminal approval");
171
+ }
172
+ };
173
+
174
+ export {
175
+ handlePreToolUse
176
+ };
@@ -0,0 +1,154 @@
1
+ import {
2
+ DEFAULT_SESSION,
3
+ cancelQuestion,
4
+ describeToolCall,
5
+ getMachineId,
6
+ isDefaultSession,
7
+ isPolicyConfig,
8
+ listPendingQuestions,
9
+ removePendingQuestion,
10
+ removePendingSession,
11
+ resolvePolicy
12
+ } from "./chunk-CH53PBQN.js";
13
+ import {
14
+ withRetry
15
+ } from "./chunk-3MIR7ODJ.js";
16
+ import {
17
+ getApiKey,
18
+ getBaseUrl
19
+ } from "./chunk-VUNL35KE.js";
20
+
21
+ // src/events.ts
22
+ import { basename, join } from "path";
23
+ import { createHash } from "crypto";
24
+ import { existsSync, readFileSync } from "fs";
25
+ import { tmpdir } from "os";
26
+ var cleanupPendingQuestions = async (sessionId) => {
27
+ try {
28
+ const files = listPendingQuestions(sessionId);
29
+ const apiKey = getApiKey();
30
+ for (const correlationId of files) {
31
+ try {
32
+ await cancelQuestion(apiKey, correlationId);
33
+ } catch {
34
+ }
35
+ removePendingQuestion(sessionId, correlationId);
36
+ }
37
+ if (!isDefaultSession(sessionId)) removePendingSession(sessionId);
38
+ } catch {
39
+ }
40
+ };
41
+ var POLICY_CACHE_TTL_MS = 5 * 60 * 1e3;
42
+ var readFreshCachedPolicy = (apiKey) => {
43
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
44
+ const path = join(tmpdir(), `pushary-policy-${hash}.json`);
45
+ if (!existsSync(path)) return null;
46
+ const cached = JSON.parse(readFileSync(path, "utf-8"));
47
+ if (!isPolicyConfig(cached)) return null;
48
+ if (!cached._cachedAt || Date.now() - cached._cachedAt >= POLICY_CACHE_TTL_MS) return null;
49
+ return cached;
50
+ };
51
+ var deriveDecisionSource = (toolName, toolInput) => {
52
+ try {
53
+ const policy = readFreshCachedPolicy(getApiKey());
54
+ if (!policy) return void 0;
55
+ const resolved = resolvePolicy(policy, toolName, null, toolInput);
56
+ if (resolved.timeoutSeconds === 0 && resolved.timeoutAction === "approve") return "policy_auto";
57
+ if (resolved.mode === "push_only" || resolved.mode === "push_first") return "human";
58
+ return "terminal";
59
+ } catch {
60
+ return void 0;
61
+ }
62
+ };
63
+ var reportEvent = async (event, options = {}) => {
64
+ const apiKey = getApiKey();
65
+ const baseUrl = getBaseUrl();
66
+ await withRetry(async () => {
67
+ await fetch(`${baseUrl}/api/agent/event`, {
68
+ method: "POST",
69
+ headers: {
70
+ "Content-Type": "application/json",
71
+ "Authorization": `Bearer ${apiKey}`
72
+ },
73
+ body: JSON.stringify({
74
+ ...event,
75
+ machineId: event.machineId ?? getMachineId()
76
+ }),
77
+ signal: AbortSignal.timeout(options.timeoutMs ?? 1e4)
78
+ });
79
+ }, { maxAttempts: options.maxAttempts ?? 2, baseDelayMs: 300 });
80
+ };
81
+ var handlePostToolUse = async (input) => {
82
+ try {
83
+ const projectName = basename(input.cwd ?? process.cwd());
84
+ const action = describeToolCall(input.tool_name, input.tool_input, "event");
85
+ const isError = input.tool_result && ("error" in input.tool_result || "is_error" in input.tool_result);
86
+ await Promise.allSettled([
87
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
88
+ reportEvent({
89
+ event: isError ? "tool_error" : "tool_complete",
90
+ agentType: "claude_code",
91
+ agentName: `Claude Code - ${projectName}`,
92
+ action,
93
+ sessionId: input.session_id,
94
+ error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0,
95
+ decisionSource: deriveDecisionSource(input.tool_name, input.tool_input)
96
+ })
97
+ ]);
98
+ } catch {
99
+ }
100
+ };
101
+ var TASK_TITLE_MAX_LENGTH = 120;
102
+ var handleUserPrompt = async (input) => {
103
+ try {
104
+ const projectName = basename(input.cwd ?? process.cwd());
105
+ const titlesEnabled = process.env.PUSHARY_TASK_TITLES !== "off";
106
+ const taskTitle = titlesEnabled ? input.prompt?.replace(/\s+/g, " ").trim().slice(0, TASK_TITLE_MAX_LENGTH) || void 0 : void 0;
107
+ await reportEvent({
108
+ event: "user_prompt",
109
+ agentType: "claude_code",
110
+ agentName: `Claude Code - ${projectName}`,
111
+ sessionId: input.session_id,
112
+ taskTitle
113
+ }, { maxAttempts: 1, timeoutMs: 800 });
114
+ } catch {
115
+ }
116
+ };
117
+ var handleStop = async (input) => {
118
+ try {
119
+ const projectName = basename(input.cwd ?? process.cwd());
120
+ await Promise.allSettled([
121
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
122
+ reportEvent({
123
+ event: "session_end",
124
+ agentType: "claude_code",
125
+ agentName: `Claude Code - ${projectName}`,
126
+ action: "Session ended",
127
+ sessionId: input.session_id
128
+ })
129
+ ]);
130
+ } catch {
131
+ }
132
+ };
133
+ var handleNotification = async (input) => {
134
+ try {
135
+ const projectName = basename(input.cwd ?? process.cwd());
136
+ await reportEvent({
137
+ event: input.type === "error" ? "error" : "notification",
138
+ agentType: "claude_code",
139
+ agentName: `Claude Code - ${projectName}`,
140
+ action: input.title ?? input.message ?? "Notification",
141
+ sessionId: input.session_id,
142
+ error: input.type === "error" ? input.message : void 0
143
+ });
144
+ } catch {
145
+ }
146
+ };
147
+
148
+ export {
149
+ reportEvent,
150
+ handlePostToolUse,
151
+ handleUserPrompt,
152
+ handleStop,
153
+ handleNotification
154
+ };
@@ -16,6 +16,18 @@ interface HookOutput {
16
16
  }
17
17
  declare const handlePreToolUse: (input: ToolInput) => Promise<HookOutput>;
18
18
 
19
+ type ReceiptKind = 'edit' | 'write' | 'bash' | 'commit';
20
+ interface ReceiptMeta {
21
+ kind: ReceiptKind;
22
+ target: string;
23
+ ok: boolean;
24
+ }
25
+
26
+ type DecisionSource = 'policy_auto' | 'human' | 'terminal';
27
+ interface AgentIdentity {
28
+ readonly type: string;
29
+ readonly label: string;
30
+ }
19
31
  interface AgentEvent {
20
32
  event: string;
21
33
  agentType: string;
@@ -24,20 +36,27 @@ interface AgentEvent {
24
36
  machineId?: string;
25
37
  sessionId?: string;
26
38
  error?: string;
39
+ taskTitle?: string;
40
+ decisionSource?: DecisionSource;
41
+ meta?: ReceiptMeta;
27
42
  }
28
- declare const reportEvent: (event: AgentEvent) => Promise<void>;
43
+ interface ReportEventOptions {
44
+ maxAttempts?: number;
45
+ timeoutMs?: number;
46
+ }
47
+ declare const reportEvent: (event: AgentEvent, options?: ReportEventOptions) => Promise<void>;
29
48
  declare const handlePostToolUse: (input: {
30
49
  tool_name: string;
31
50
  tool_input: Record<string, unknown>;
32
51
  tool_result?: Record<string, unknown>;
33
52
  cwd?: string;
34
53
  session_id?: string;
35
- }) => Promise<void>;
54
+ }, agent?: AgentIdentity) => Promise<void>;
36
55
  declare const handleStop: (input: {
37
56
  cwd?: string;
38
57
  session_id?: string;
39
58
  stop_hook_active?: boolean;
40
- }) => Promise<void>;
59
+ }, agent?: AgentIdentity) => Promise<void>;
41
60
  declare const handleNotification: (input: {
42
61
  message?: string;
43
62
  title?: string;
@@ -55,6 +74,7 @@ interface AskUserParams {
55
74
  sessionId?: string;
56
75
  machineId?: string;
57
76
  toolName?: string;
77
+ toolTarget?: string;
58
78
  }
59
79
  interface AskUserResponse {
60
80
  correlationId: string;
@@ -69,7 +89,7 @@ declare const waitForAnswer: (apiKey: string, correlationId: string, timeoutMs?:
69
89
  declare const cancelQuestion: (apiKey: string, correlationId: string) => Promise<void>;
70
90
 
71
91
  declare const getPolicy: (apiKey: string) => Promise<PolicyConfig>;
72
- declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null) => ToolPolicy;
92
+ declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null, toolInput?: Record<string, unknown>) => ToolPolicy;
73
93
  interface ModeState {
74
94
  readonly mode: ApprovalMode | null;
75
95
  readonly kill: boolean;
package/dist/src/index.js CHANGED
@@ -1,22 +1,22 @@
1
1
  import {
2
- fetchModeOverride,
3
- fetchModeState,
4
- getPolicy,
5
- handlePreToolUse,
6
- resolvePolicy
7
- } from "../chunk-W5KRWUNE.js";
8
- import "../chunk-IBWCHA5M.js";
2
+ handlePreToolUse
3
+ } from "../chunk-TRLBBLSS.js";
9
4
  import {
10
5
  handleNotification,
11
6
  handlePostToolUse,
12
7
  handleStop,
13
8
  reportEvent
14
- } from "../chunk-AB4KX4XT.js";
9
+ } from "../chunk-SH26ZOHU.js";
15
10
  import {
16
11
  askUser,
17
12
  cancelQuestion,
13
+ fetchModeOverride,
14
+ fetchModeState,
15
+ getPolicy,
16
+ resolvePolicy,
18
17
  waitForAnswer
19
- } from "../chunk-OF5WIOYS.js";
18
+ } from "../chunk-QRXWPZKN.js";
19
+ import "../chunk-22CV7V7A.js";
20
20
  import "../chunk-3MIR7ODJ.js";
21
21
  import {
22
22
  getApiKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushary/agent-hooks",
3
- "version": "0.12.0",
3
+ "version": "0.14.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",
@@ -19,7 +19,9 @@
19
19
  "pushary-hook": "./dist/bin/pushary-hook.js",
20
20
  "pushary-post-hook": "./dist/bin/pushary-post-hook.js",
21
21
  "pushary-stop-hook": "./dist/bin/pushary-stop-hook.js",
22
+ "pushary-prompt-hook": "./dist/bin/pushary-prompt-hook.js",
22
23
  "pushary-codex": "./dist/bin/pushary-codex.js",
24
+ "pushary-codex-hook": "./dist/bin/pushary-codex-hook.js",
23
25
  "pushary-setup": "./dist/bin/pushary-setup.js",
24
26
  "pushary-clean": "./dist/bin/pushary-clean.js",
25
27
  "pushary-doctor": "./dist/bin/pushary-doctor.js",
@@ -32,7 +34,7 @@
32
34
  "scripts": {
33
35
  "build": "node scripts/bundle-plugin.mjs && tsup",
34
36
  "dev": "tsup --watch",
35
- "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/hook.test.ts"
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"
36
38
  },
37
39
  "dependencies": {
38
40
  "@inquirer/prompts": "^8.4.2",