@pushary/agent-hooks 0.12.0 → 0.13.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.
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  removeClaudeMcpServers,
4
4
  removePusharySettings
5
- } from "../chunk-5GFUI5N6.js";
5
+ } from "../chunk-5MA3CPZB.js";
6
6
  import {
7
7
  execNpm
8
8
  } from "../chunk-RSHN2AQ7.js";
@@ -1,16 +1,17 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  reportEvent
4
- } from "../chunk-AB4KX4XT.js";
4
+ } from "../chunk-WCGKLHCL.js";
5
5
  import {
6
6
  askUser,
7
7
  getMachineId,
8
8
  waitForAnswer
9
- } from "../chunk-OF5WIOYS.js";
9
+ } from "../chunk-CH53PBQN.js";
10
10
  import "../chunk-3MIR7ODJ.js";
11
11
  import {
12
12
  getApiKey
13
13
  } from "../chunk-VUNL35KE.js";
14
+ import "../chunk-22CV7V7A.js";
14
15
 
15
16
  // bin/pushary-codex.ts
16
17
  import { basename } from "path";
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- execNpm
4
- } from "../chunk-RSHN2AQ7.js";
5
2
  import {
6
3
  callMcpTool,
7
4
  sendMcpRequest
8
5
  } from "../chunk-3MIR7ODJ.js";
9
6
  import "../chunk-VUNL35KE.js";
7
+ import {
8
+ execNpm
9
+ } from "../chunk-RSHN2AQ7.js";
10
10
 
11
11
  // bin/pushary-doctor.ts
12
12
  import { existsSync, readFileSync } from "fs";
@@ -103,9 +103,11 @@ var main = async () => {
103
103
  const hasPreHook = JSON.stringify(hooks?.PreToolUse ?? []).includes("pushary-hook");
104
104
  const hasPostHook = JSON.stringify(hooks?.PostToolUse ?? []).includes("pushary-post-hook");
105
105
  const hasStopHook = JSON.stringify(hooks?.Stop ?? []).includes("pushary-stop-hook");
106
+ const hasPromptHook = JSON.stringify(hooks?.UserPromptSubmit ?? []).includes("pushary-prompt-hook");
106
107
  check(hasPreHook, "Claude Code: PreToolUse hook");
107
108
  check(hasPostHook, "Claude Code: PostToolUse hook");
108
109
  check(hasStopHook, "Claude Code: Stop hook");
110
+ check(hasPromptHook, "Claude Code: UserPromptSubmit hook", hasPromptHook ? void 0 : "missing, re-run setup to register it");
109
111
  const preHookCommand = extractHookCommand(hooks?.PreToolUse, "pushary-hook");
110
112
  if (preHookCommand) {
111
113
  const resolves = commandResolves(preHookCommand);
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePreToolUse
4
- } from "../chunk-W5KRWUNE.js";
5
- import "../chunk-IBWCHA5M.js";
6
- import "../chunk-OF5WIOYS.js";
4
+ } from "../chunk-RNWPCELY.js";
5
+ import "../chunk-CH53PBQN.js";
7
6
  import "../chunk-3MIR7ODJ.js";
8
7
  import "../chunk-VUNL35KE.js";
8
+ import "../chunk-22CV7V7A.js";
9
9
 
10
10
  // bin/pushary-hook.ts
11
11
  var main = async () => {
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePostToolUse
4
- } from "../chunk-AB4KX4XT.js";
5
- import "../chunk-OF5WIOYS.js";
4
+ } from "../chunk-WCGKLHCL.js";
5
+ import "../chunk-CH53PBQN.js";
6
6
  import "../chunk-3MIR7ODJ.js";
7
7
  import "../chunk-VUNL35KE.js";
8
+ import "../chunk-22CV7V7A.js";
8
9
 
9
10
  // bin/pushary-post-hook.ts
10
11
  var main = async () => {
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ handleUserPrompt
4
+ } from "../chunk-WCGKLHCL.js";
5
+ import "../chunk-CH53PBQN.js";
6
+ import "../chunk-3MIR7ODJ.js";
7
+ import "../chunk-VUNL35KE.js";
8
+ import "../chunk-22CV7V7A.js";
9
+
10
+ // bin/pushary-prompt-hook.ts
11
+ var main = async () => {
12
+ let rawInput = "";
13
+ for await (const chunk of process.stdin) {
14
+ rawInput += chunk;
15
+ }
16
+ if (!rawInput.trim()) {
17
+ process.exit(0);
18
+ }
19
+ try {
20
+ const input = JSON.parse(rawInput);
21
+ await handleUserPrompt(input);
22
+ } catch {
23
+ }
24
+ };
25
+ main();
@@ -3,14 +3,14 @@ import {
3
3
  addClaudeMcpServer,
4
4
  addPusharyHooks,
5
5
  addPusharyToolPermissions
6
- } from "../chunk-5GFUI5N6.js";
6
+ } from "../chunk-5MA3CPZB.js";
7
7
  import {
8
8
  execNpm,
9
9
  npmErrorMessage
10
10
  } from "../chunk-RSHN2AQ7.js";
11
11
  import {
12
12
  isValidApiKey
13
- } from "../chunk-IBWCHA5M.js";
13
+ } from "../chunk-22CV7V7A.js";
14
14
 
15
15
  // bin/pushary-setup.ts
16
16
  import { existsSync, readFileSync, writeFileSync, appendFileSync, mkdirSync, cpSync, rmSync } from "fs";
@@ -310,7 +310,7 @@ var setupClaudeCode = async (apiKey) => {
310
310
  addPusharyToolPermissions(settings);
311
311
  });
312
312
  await installGlobally();
313
- await spinner("Adding hooks (PreToolUse, PostToolUse, Stop)", async () => {
313
+ await spinner("Adding hooks (PreToolUse, PostToolUse, UserPromptSubmit, Stop)", async () => {
314
314
  let binDir;
315
315
  try {
316
316
  binDir = join(execNpm("prefix -g --no-workspaces", { timeout: 5e3 }).toString().trim(), "bin");
@@ -1,10 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleStop
4
- } from "../chunk-AB4KX4XT.js";
5
- import "../chunk-OF5WIOYS.js";
4
+ } from "../chunk-WCGKLHCL.js";
5
+ import "../chunk-CH53PBQN.js";
6
6
  import "../chunk-3MIR7ODJ.js";
7
7
  import "../chunk-VUNL35KE.js";
8
+ import "../chunk-22CV7V7A.js";
8
9
 
9
10
  // bin/pushary-stop-hook.ts
10
11
  var main = async () => {
@@ -0,0 +1,38 @@
1
+ // ../contracts/src/index.ts
2
+ var APPROVAL_MODES = ["push_only", "terminal_only", "push_first", "notify_only"];
3
+ var isApprovalMode = (value) => typeof value === "string" && APPROVAL_MODES.includes(value);
4
+ var MATCH_RANKS = ["none", "tool", "prefix", "exact"];
5
+ var matchRankWeight = (rank) => MATCH_RANKS.indexOf(rank);
6
+ var matchToolPattern = (pattern, toolName, arg) => {
7
+ const open = pattern.indexOf("(");
8
+ if (open === -1 || !pattern.endsWith(")")) {
9
+ return pattern === toolName ? "tool" : "none";
10
+ }
11
+ if (pattern.slice(0, open) !== toolName || arg === void 0) return "none";
12
+ const inner = pattern.slice(open + 1, -1);
13
+ if (inner.endsWith(":*")) {
14
+ return arg.startsWith(inner.slice(0, -2)) ? "prefix" : "none";
15
+ }
16
+ return arg === inner ? "exact" : "none";
17
+ };
18
+ var POLICY_ARG_KEYS = {
19
+ Bash: "command",
20
+ Edit: "file_path",
21
+ Write: "file_path"
22
+ };
23
+ var extractPolicyArg = (toolName, toolInput) => {
24
+ const key = POLICY_ARG_KEYS[toolName];
25
+ if (!key) return void 0;
26
+ const value = toolInput[key];
27
+ return typeof value === "string" ? value : void 0;
28
+ };
29
+ var API_KEY_PATTERN = /^pk_[a-f0-9]+\.[a-f0-9]+$/;
30
+ var isValidApiKey = (value) => API_KEY_PATTERN.test(value);
31
+
32
+ export {
33
+ isApprovalMode,
34
+ matchRankWeight,
35
+ matchToolPattern,
36
+ extractPolicyArg,
37
+ isValidApiKey
38
+ };
@@ -0,0 +1,141 @@
1
+ // src/claude-config.ts
2
+ import { join } from "path";
3
+ var PUSHARY_MCP_URL = "https://pushary.com/api/mcp/mcp";
4
+ var PUSHARY_PERMISSION_RULE = "mcp__pushary__*";
5
+ var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
6
+ var ensureRecord = (target, key) => {
7
+ const existing = asRecord(target[key]);
8
+ if (existing) return existing;
9
+ const created = {};
10
+ target[key] = created;
11
+ return created;
12
+ };
13
+ var isPusharyPermission = (rule) => typeof rule === "string" && (rule.includes("pushary") || rule.includes("MCP(pushary"));
14
+ var isPusharyHook = (entry) => {
15
+ const hooks = asRecord(entry)?.hooks;
16
+ if (!Array.isArray(hooks)) return false;
17
+ return hooks.some((hook) => {
18
+ const command = String(asRecord(hook)?.command ?? "");
19
+ return command.includes("pushary-hook") || command.includes("pushary-post-hook") || command.includes("pushary-stop-hook") || command.includes("pushary-prompt-hook");
20
+ });
21
+ };
22
+ var addClaudeMcpServer = (config, apiKey) => {
23
+ const mcpServers = ensureRecord(config, "mcpServers");
24
+ mcpServers.pushary = {
25
+ type: "http",
26
+ url: PUSHARY_MCP_URL,
27
+ headers: { Authorization: `Bearer ${apiKey}` }
28
+ };
29
+ };
30
+ var removeClaudeMcpServers = (config) => {
31
+ let changed = false;
32
+ const removeFrom = (target) => {
33
+ const mcpServers = asRecord(target.mcpServers);
34
+ if (!mcpServers?.pushary) return;
35
+ delete mcpServers.pushary;
36
+ if (Object.keys(mcpServers).length === 0) delete target.mcpServers;
37
+ changed = true;
38
+ };
39
+ removeFrom(config);
40
+ const projects = asRecord(config.projects);
41
+ if (projects) {
42
+ for (const project of Object.values(projects)) {
43
+ const projectConfig = asRecord(project);
44
+ if (projectConfig) removeFrom(projectConfig);
45
+ }
46
+ }
47
+ return changed;
48
+ };
49
+ var addPusharyToolPermissions = (settings) => {
50
+ const permissions = ensureRecord(settings, "permissions");
51
+ const allow = Array.isArray(permissions.allow) ? permissions.allow : [];
52
+ const filtered = allow.filter((rule) => !isPusharyPermission(rule));
53
+ if (!filtered.includes(PUSHARY_PERMISSION_RULE)) {
54
+ filtered.push(PUSHARY_PERMISSION_RULE);
55
+ }
56
+ permissions.allow = filtered;
57
+ };
58
+ var addPusharyHooks = (settings, binDir) => {
59
+ const resolve = (name) => binDir ? join(binDir, name) : name;
60
+ const hooks = ensureRecord(settings, "hooks");
61
+ const preToolUse = (Array.isArray(hooks.PreToolUse) ? hooks.PreToolUse : []).filter((entry) => !isPusharyHook(entry));
62
+ preToolUse.push({
63
+ matcher: "Bash|Write|Edit",
64
+ hooks: [{
65
+ type: "command",
66
+ command: resolve("pushary-hook"),
67
+ timeout: 120
68
+ }]
69
+ });
70
+ hooks.PreToolUse = preToolUse;
71
+ const postToolUse = (Array.isArray(hooks.PostToolUse) ? hooks.PostToolUse : []).filter((entry) => !isPusharyHook(entry));
72
+ postToolUse.push({
73
+ matcher: "Bash|Write|Edit",
74
+ hooks: [{
75
+ type: "command",
76
+ command: resolve("pushary-post-hook"),
77
+ timeout: 10
78
+ }]
79
+ });
80
+ hooks.PostToolUse = postToolUse;
81
+ const stop = (Array.isArray(hooks.Stop) ? hooks.Stop : []).filter((entry) => !isPusharyHook(entry));
82
+ stop.push({
83
+ hooks: [{
84
+ type: "command",
85
+ command: resolve("pushary-stop-hook"),
86
+ timeout: 10
87
+ }]
88
+ });
89
+ hooks.Stop = stop;
90
+ const userPromptSubmit = (Array.isArray(hooks.UserPromptSubmit) ? hooks.UserPromptSubmit : []).filter((entry) => !isPusharyHook(entry));
91
+ userPromptSubmit.push({
92
+ hooks: [{
93
+ type: "command",
94
+ command: resolve("pushary-prompt-hook"),
95
+ timeout: 10
96
+ }]
97
+ });
98
+ hooks.UserPromptSubmit = userPromptSubmit;
99
+ };
100
+ var removePusharySettings = (settings) => {
101
+ let changed = removeClaudeMcpServers(settings);
102
+ const permissions = asRecord(settings.permissions);
103
+ if (permissions && Array.isArray(permissions.allow)) {
104
+ const filtered = permissions.allow.filter((rule) => !isPusharyPermission(rule));
105
+ if (filtered.length !== permissions.allow.length) {
106
+ if (filtered.length === 0) {
107
+ delete permissions.allow;
108
+ } else {
109
+ permissions.allow = filtered;
110
+ }
111
+ if (Object.keys(permissions).length === 0) delete settings.permissions;
112
+ changed = true;
113
+ }
114
+ }
115
+ const hooks = asRecord(settings.hooks);
116
+ if (hooks) {
117
+ for (const key of ["PreToolUse", "PostToolUse", "Stop", "UserPromptSubmit"]) {
118
+ const entries = hooks[key];
119
+ if (!Array.isArray(entries)) continue;
120
+ const filtered = entries.filter((entry) => !isPusharyHook(entry));
121
+ if (filtered.length !== entries.length) {
122
+ if (filtered.length === 0) {
123
+ delete hooks[key];
124
+ } else {
125
+ hooks[key] = filtered;
126
+ }
127
+ changed = true;
128
+ }
129
+ }
130
+ if (Object.keys(hooks).length === 0) delete settings.hooks;
131
+ }
132
+ return changed;
133
+ };
134
+
135
+ export {
136
+ addClaudeMcpServer,
137
+ removeClaudeMcpServers,
138
+ addPusharyToolPermissions,
139
+ addPusharyHooks,
140
+ removePusharySettings
141
+ };
@@ -0,0 +1,265 @@
1
+ import {
2
+ callMcpTool,
3
+ withRetry
4
+ } from "./chunk-3MIR7ODJ.js";
5
+ import {
6
+ getBaseUrl
7
+ } from "./chunk-VUNL35KE.js";
8
+ import {
9
+ extractPolicyArg,
10
+ isApprovalMode,
11
+ matchRankWeight,
12
+ matchToolPattern
13
+ } from "./chunk-22CV7V7A.js";
14
+
15
+ // src/validate.ts
16
+ var isPolicyConfig = (data) => {
17
+ if (!data || typeof data !== "object") return false;
18
+ const d = data;
19
+ return Array.isArray(d.policies) && typeof d.defaultTimeoutSeconds === "number" && typeof d.defaultTimeoutAction === "string";
20
+ };
21
+ var isAskUserResponse = (data) => {
22
+ if (!data || typeof data !== "object") return false;
23
+ const d = data;
24
+ return typeof d.correlationId === "string" && typeof d.status === "string";
25
+ };
26
+ var isWaitForAnswerResponse = (data) => {
27
+ if (!data || typeof data !== "object") return false;
28
+ const d = data;
29
+ return typeof d.answered === "boolean";
30
+ };
31
+
32
+ // src/api.ts
33
+ var askUser = async (apiKey, params) => {
34
+ const result = await callMcpTool(apiKey, "ask_user", { ...params, wait: false }, { maxRetries: 3 });
35
+ if (!isAskUserResponse(result)) throw new Error("Invalid ask_user response");
36
+ return result;
37
+ };
38
+ var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
39
+ const result = await callMcpTool(apiKey, "wait_for_answer", {
40
+ correlationId,
41
+ timeoutMs
42
+ });
43
+ if (!isWaitForAnswerResponse(result)) throw new Error("Invalid wait_for_answer response");
44
+ return result;
45
+ };
46
+ var cancelQuestion = async (apiKey, correlationId) => {
47
+ await callMcpTool(apiKey, "cancel_question", { correlationId });
48
+ };
49
+ var sendNotification = async (apiKey, params) => {
50
+ await callMcpTool(apiKey, "send_notification", { ...params }, { maxRetries: 3 });
51
+ };
52
+
53
+ // src/identity.ts
54
+ import { createHash } from "crypto";
55
+ import { hostname } from "os";
56
+ var deriveMachineId = (host) => createHash("sha256").update(host).digest("hex").slice(0, 8);
57
+ var getMachineId = () => deriveMachineId(hostname());
58
+
59
+ // src/policy.ts
60
+ import { createHash as createHash2 } from "crypto";
61
+ import { existsSync, readFileSync, writeFileSync } from "fs";
62
+ import { join } from "path";
63
+ import { tmpdir } from "os";
64
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
65
+ var cacheFile = (apiKey) => {
66
+ const hash = createHash2("sha256").update(apiKey).digest("hex").slice(0, 12);
67
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
68
+ };
69
+ var fetchPolicy = async (apiKey) => {
70
+ return withRetry(async () => {
71
+ const baseUrl = getBaseUrl();
72
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
73
+ headers: { "Authorization": `Bearer ${apiKey}` },
74
+ signal: AbortSignal.timeout(1e4)
75
+ });
76
+ if (!response.ok) {
77
+ throw new Error(`Failed to fetch policy: ${response.status}`);
78
+ }
79
+ const raw = await response.json();
80
+ if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
81
+ return raw;
82
+ }, { maxAttempts: 2 });
83
+ };
84
+ var getPolicy = async (apiKey) => {
85
+ const path = cacheFile(apiKey);
86
+ let staleCache = null;
87
+ if (existsSync(path)) {
88
+ try {
89
+ const stat = readFileSync(path, "utf-8");
90
+ const cached = JSON.parse(stat);
91
+ if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
92
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
93
+ return cached;
94
+ }
95
+ staleCache = cached;
96
+ } catch {
97
+ }
98
+ }
99
+ try {
100
+ const policy = await fetchPolicy(apiKey);
101
+ try {
102
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
103
+ } catch {
104
+ }
105
+ return policy;
106
+ } catch {
107
+ if (staleCache) return staleCache;
108
+ throw new Error("Failed to fetch policy and no cached policy available");
109
+ }
110
+ };
111
+ var findBestMatch = (policies, toolName, arg) => {
112
+ let best;
113
+ let bestWeight = 0;
114
+ let bestLength = -1;
115
+ for (const candidate of policies) {
116
+ const rank = matchToolPattern(candidate.tool, toolName, arg);
117
+ if (rank === "none") continue;
118
+ const weight = matchRankWeight(rank);
119
+ const length = rank === "prefix" ? candidate.tool.length : -1;
120
+ if (weight > bestWeight || weight === bestWeight && length > bestLength) {
121
+ best = candidate;
122
+ bestWeight = weight;
123
+ bestLength = length;
124
+ }
125
+ }
126
+ return best;
127
+ };
128
+ var resolvePolicy = (config, toolName, modeOverride, toolInput) => {
129
+ const arg = toolInput ? extractPolicyArg(toolName, toolInput) : void 0;
130
+ const base = findBestMatch(config.policies, toolName, arg) ?? config.policies.find((p) => p.tool === "*") ?? {
131
+ tool: toolName,
132
+ timeoutSeconds: config.defaultTimeoutSeconds,
133
+ timeoutAction: config.defaultTimeoutAction,
134
+ mode: config.defaultMode ?? "push_first",
135
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
136
+ };
137
+ const effectiveOverride = modeOverride ?? config.modeOverride;
138
+ if (effectiveOverride) {
139
+ return { ...base, mode: effectiveOverride };
140
+ }
141
+ return base;
142
+ };
143
+ var fetchModeState = async (apiKey, sessionId) => {
144
+ try {
145
+ const baseUrl = getBaseUrl();
146
+ const url = sessionId ? `${baseUrl}/api/mcp/mode?session=${encodeURIComponent(sessionId)}` : `${baseUrl}/api/mcp/mode`;
147
+ const response = await fetch(url, {
148
+ headers: { "Authorization": `Bearer ${apiKey}` },
149
+ signal: AbortSignal.timeout(3e3)
150
+ });
151
+ if (!response.ok) return { mode: null, kill: false };
152
+ const data = await response.json();
153
+ const mode = data.override?.mode;
154
+ return { mode: isApprovalMode(mode) ? mode : null, kill: data.kill === true };
155
+ } catch {
156
+ return { mode: null, kill: false };
157
+ }
158
+ };
159
+ var fetchModeOverride = async (apiKey) => (await fetchModeState(apiKey)).mode;
160
+
161
+ // src/describe.ts
162
+ var hookPrefixes = {
163
+ Bash: (input) => `bash: ${input.command ?? "(no command)"}`,
164
+ Write: (input) => `write file: ${input.file_path ?? "(unknown path)"}`,
165
+ Edit: (input) => `edit file: ${input.file_path ?? "(unknown path)"}`,
166
+ Read: (input) => `read file: ${input.file_path ?? "(unknown path)"}`
167
+ };
168
+ var eventPrefixes = {
169
+ Bash: (input) => `ran: ${String(input.command ?? "").slice(0, 120)}`,
170
+ Write: (input) => `wrote: ${input.file_path ?? "unknown"}`,
171
+ Edit: (input) => `edited: ${input.file_path ?? "unknown"}`,
172
+ Read: (input) => `read: ${input.file_path ?? "unknown"}`
173
+ };
174
+ var describeToolCall = (toolName, toolInput, format = "hook") => {
175
+ const prefixes = format === "hook" ? hookPrefixes : eventPrefixes;
176
+ const builder = prefixes[toolName];
177
+ if (builder) return builder(toolInput);
178
+ return format === "hook" ? `${toolName}: ${JSON.stringify(toolInput).slice(0, 200)}` : `${toolName}: done`;
179
+ };
180
+ var TOOL_TARGET_MAX_LENGTH = 80;
181
+ var deriveToolTarget = (toolName, toolInput) => {
182
+ if (toolName === "Bash") {
183
+ const command = toolInput.command;
184
+ if (typeof command !== "string") return void 0;
185
+ const head = command.trim().split(/\s+/).slice(0, 2).join(" ");
186
+ return head ? head.slice(0, TOOL_TARGET_MAX_LENGTH) : void 0;
187
+ }
188
+ if (toolName === "Edit" || toolName === "Write") {
189
+ const filePath = toolInput.file_path;
190
+ if (typeof filePath !== "string") return void 0;
191
+ const separator = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"));
192
+ const base = filePath.slice(separator + 1);
193
+ const dot = base.lastIndexOf(".");
194
+ if (dot <= 0) return void 0;
195
+ return base.slice(dot).slice(0, TOOL_TARGET_MAX_LENGTH);
196
+ }
197
+ return void 0;
198
+ };
199
+
200
+ // src/pending.ts
201
+ import { join as join2 } from "path";
202
+ import { tmpdir as tmpdir2 } from "os";
203
+ import { existsSync as existsSync2, mkdirSync, writeFileSync as writeFileSync2, readdirSync, unlinkSync, rmSync, statSync } from "fs";
204
+ var PENDING_DIR = join2(tmpdir2(), "pushary-pending");
205
+ var DEFAULT_SESSION = "_no_session";
206
+ var GRACE_MS = 10 * 60 * 1e3;
207
+ var sanitize = (sessionId) => sessionId.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 128) || DEFAULT_SESSION;
208
+ var dirFor = (sessionId) => join2(PENDING_DIR, sanitize(sessionId));
209
+ var isDefaultSession = (sessionId) => sanitize(sessionId) === DEFAULT_SESSION;
210
+ var savePendingQuestion = (sessionId, correlationId) => {
211
+ const dir = dirFor(sessionId);
212
+ if (!existsSync2(dir)) mkdirSync(dir, { recursive: true });
213
+ writeFileSync2(join2(dir, correlationId), "", "utf-8");
214
+ };
215
+ var listPendingQuestions = (sessionId) => {
216
+ const dir = dirFor(sessionId);
217
+ let files;
218
+ try {
219
+ files = readdirSync(dir);
220
+ } catch {
221
+ return [];
222
+ }
223
+ if (!isDefaultSession(sessionId)) return files;
224
+ const cutoff = Date.now() - GRACE_MS;
225
+ return files.filter((name) => {
226
+ try {
227
+ return statSync(join2(dir, name)).mtimeMs < cutoff;
228
+ } catch {
229
+ return false;
230
+ }
231
+ });
232
+ };
233
+ var removePendingQuestion = (sessionId, correlationId) => {
234
+ try {
235
+ unlinkSync(join2(dirFor(sessionId), correlationId));
236
+ } catch {
237
+ }
238
+ };
239
+ var removePendingSession = (sessionId) => {
240
+ try {
241
+ rmSync(dirFor(sessionId), { recursive: true, force: true });
242
+ } catch {
243
+ }
244
+ };
245
+
246
+ export {
247
+ isPolicyConfig,
248
+ askUser,
249
+ waitForAnswer,
250
+ cancelQuestion,
251
+ sendNotification,
252
+ describeToolCall,
253
+ deriveToolTarget,
254
+ DEFAULT_SESSION,
255
+ isDefaultSession,
256
+ savePendingQuestion,
257
+ listPendingQuestions,
258
+ removePendingQuestion,
259
+ removePendingSession,
260
+ getMachineId,
261
+ getPolicy,
262
+ resolvePolicy,
263
+ fetchModeState,
264
+ fetchModeOverride
265
+ };
@@ -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-CH53PBQN.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,7 @@ interface HookOutput {
16
16
  }
17
17
  declare const handlePreToolUse: (input: ToolInput) => Promise<HookOutput>;
18
18
 
19
+ type DecisionSource = 'policy_auto' | 'human' | 'terminal';
19
20
  interface AgentEvent {
20
21
  event: string;
21
22
  agentType: string;
@@ -24,8 +25,14 @@ interface AgentEvent {
24
25
  machineId?: string;
25
26
  sessionId?: string;
26
27
  error?: string;
28
+ taskTitle?: string;
29
+ decisionSource?: DecisionSource;
27
30
  }
28
- declare const reportEvent: (event: AgentEvent) => Promise<void>;
31
+ interface ReportEventOptions {
32
+ maxAttempts?: number;
33
+ timeoutMs?: number;
34
+ }
35
+ declare const reportEvent: (event: AgentEvent, options?: ReportEventOptions) => Promise<void>;
29
36
  declare const handlePostToolUse: (input: {
30
37
  tool_name: string;
31
38
  tool_input: Record<string, unknown>;
@@ -55,6 +62,7 @@ interface AskUserParams {
55
62
  sessionId?: string;
56
63
  machineId?: string;
57
64
  toolName?: string;
65
+ toolTarget?: string;
58
66
  }
59
67
  interface AskUserResponse {
60
68
  correlationId: string;
@@ -69,7 +77,7 @@ declare const waitForAnswer: (apiKey: string, correlationId: string, timeoutMs?:
69
77
  declare const cancelQuestion: (apiKey: string, correlationId: string) => Promise<void>;
70
78
 
71
79
  declare const getPolicy: (apiKey: string) => Promise<PolicyConfig>;
72
- declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null) => ToolPolicy;
80
+ declare const resolvePolicy: (config: PolicyConfig, toolName: string, modeOverride?: ApprovalMode | null, toolInput?: Record<string, unknown>) => ToolPolicy;
73
81
  interface ModeState {
74
82
  readonly mode: ApprovalMode | null;
75
83
  readonly kill: boolean;
package/dist/src/index.js CHANGED
@@ -1,27 +1,27 @@
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-RNWPCELY.js";
9
4
  import {
10
5
  handleNotification,
11
6
  handlePostToolUse,
12
7
  handleStop,
13
8
  reportEvent
14
- } from "../chunk-AB4KX4XT.js";
9
+ } from "../chunk-WCGKLHCL.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-CH53PBQN.js";
20
19
  import "../chunk-3MIR7ODJ.js";
21
20
  import {
22
21
  getApiKey,
23
22
  getBaseUrl
24
23
  } from "../chunk-VUNL35KE.js";
24
+ import "../chunk-22CV7V7A.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.12.0",
3
+ "version": "0.13.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,6 +19,7 @@
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",
23
24
  "pushary-setup": "./dist/bin/pushary-setup.js",
24
25
  "pushary-clean": "./dist/bin/pushary-clean.js",
@@ -32,7 +33,7 @@
32
33
  "scripts": {
33
34
  "build": "node scripts/bundle-plugin.mjs && tsup",
34
35
  "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"
36
+ "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"
36
37
  },
37
38
  "dependencies": {
38
39
  "@inquirer/prompts": "^8.4.2",