@pushary/agent-hooks 0.22.0 → 0.23.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.
Files changed (82) hide show
  1. package/data/cursor-plugin/README.md +5 -1
  2. package/data/cursor-plugin/mcp.json +1 -1
  3. package/dist/bin/pushary-clean.js +1 -1
  4. package/dist/bin/pushary-doctor.js +2 -2
  5. package/dist/bin/pushary-setup.js +23 -1
  6. package/dist/{chunk-BHAMEPOP.js → chunk-JPRRYC5X.js} +5 -0
  7. package/package.json +1 -1
  8. package/dist/chunk-22CV7V7A.js +0 -38
  9. package/dist/chunk-2BE2IPMO.js +0 -315
  10. package/dist/chunk-2CRLBMOX.js +0 -244
  11. package/dist/chunk-2HMNOZPY.js +0 -109
  12. package/dist/chunk-2I6DLXJN.js +0 -219
  13. package/dist/chunk-2Q6XPZ4Q.js +0 -395
  14. package/dist/chunk-3LC4YUMA.js +0 -194
  15. package/dist/chunk-3MIR7ODJ.js +0 -112
  16. package/dist/chunk-3QZESA66.js +0 -222
  17. package/dist/chunk-4Z4MB37G.js +0 -96
  18. package/dist/chunk-5GFUI5N6.js +0 -132
  19. package/dist/chunk-5JEDLXEC.js +0 -99
  20. package/dist/chunk-5LTQNC7R.js +0 -824
  21. package/dist/chunk-5QHQVXWN.js +0 -174
  22. package/dist/chunk-6A3WCJO2.js +0 -416
  23. package/dist/chunk-6QWFMVCD.js +0 -328
  24. package/dist/chunk-6SRXMZAN.js +0 -99
  25. package/dist/chunk-7PTU7TGE.js +0 -96
  26. package/dist/chunk-AB4KX4XT.js +0 -109
  27. package/dist/chunk-AC4UYAGX.js +0 -136
  28. package/dist/chunk-ACE77TKQ.js +0 -328
  29. package/dist/chunk-BYNYYZWW.js +0 -412
  30. package/dist/chunk-C5TFTNHG.js +0 -244
  31. package/dist/chunk-CH53PBQN.js +0 -265
  32. package/dist/chunk-DF3BM6BF.js +0 -195
  33. package/dist/chunk-DKZVKEOW.js +0 -175
  34. package/dist/chunk-EMPL27ZV.js +0 -96
  35. package/dist/chunk-EQJXMEIR.js +0 -175
  36. package/dist/chunk-FXYTRZHC.js +0 -177
  37. package/dist/chunk-GTJGZBTQ.js +0 -328
  38. package/dist/chunk-HJMFYDWY.js +0 -222
  39. package/dist/chunk-HRQEECB6.js +0 -315
  40. package/dist/chunk-HWEMAAOY.js +0 -370
  41. package/dist/chunk-IBWCHA5M.js +0 -10
  42. package/dist/chunk-KTM4ZQ3W.js +0 -96
  43. package/dist/chunk-KTP2EPVB.js +0 -27
  44. package/dist/chunk-KYARP7KP.js +0 -97
  45. package/dist/chunk-M2N5DYWN.js +0 -247
  46. package/dist/chunk-M5F2L4KF.js +0 -177
  47. package/dist/chunk-M6S3M6E3.js +0 -244
  48. package/dist/chunk-N7VXDBQU.js +0 -6
  49. package/dist/chunk-O6A5RHWY.js +0 -122
  50. package/dist/chunk-ODUXELPM.js +0 -219
  51. package/dist/chunk-OF5WIOYS.js +0 -129
  52. package/dist/chunk-OP4QAN23.js +0 -99
  53. package/dist/chunk-P4JH2Q7Z.js +0 -102
  54. package/dist/chunk-PMD5JSV3.js +0 -242
  55. package/dist/chunk-PSSHNBEL.js +0 -177
  56. package/dist/chunk-QEPRH7JB.js +0 -315
  57. package/dist/chunk-QRXWPZKN.js +0 -308
  58. package/dist/chunk-QY4L6XHN.js +0 -384
  59. package/dist/chunk-RNWPCELY.js +0 -176
  60. package/dist/chunk-RSHN2AQ7.js +0 -29
  61. package/dist/chunk-RZLVE57X.js +0 -38
  62. package/dist/chunk-SDCIKREA.js +0 -241
  63. package/dist/chunk-SDREQWNI.js +0 -175
  64. package/dist/chunk-SH26ZOHU.js +0 -159
  65. package/dist/chunk-TRLBBLSS.js +0 -176
  66. package/dist/chunk-U6OXZPYC.js +0 -384
  67. package/dist/chunk-UIBTFZUD.js +0 -314
  68. package/dist/chunk-USUCPCUC.js +0 -156
  69. package/dist/chunk-V4GLWPD7.js +0 -174
  70. package/dist/chunk-VC6U3QGF.js +0 -142
  71. package/dist/chunk-VIST7ACL.js +0 -88
  72. package/dist/chunk-VUNL35KE.js +0 -16
  73. package/dist/chunk-VWBNI4SC.js +0 -174
  74. package/dist/chunk-W5KRWUNE.js +0 -262
  75. package/dist/chunk-WCGKLHCL.js +0 -154
  76. package/dist/chunk-WNXGIEX7.js +0 -219
  77. package/dist/chunk-XPVSZIA3.js +0 -315
  78. package/dist/chunk-Y633ZIJF.js +0 -384
  79. package/dist/chunk-YCL4GUFR.js +0 -222
  80. package/dist/chunk-YTMKB44I.js +0 -220
  81. package/dist/chunk-ZWBS3T7Q.js +0 -244
  82. package/dist/pushary-mode-T7XOPI6Z.js +0 -83
@@ -1,174 +0,0 @@
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
- };
@@ -1,262 +0,0 @@
1
- import {
2
- isApprovalMode
3
- } from "./chunk-IBWCHA5M.js";
4
- import {
5
- DEFAULT_SESSION,
6
- askUser,
7
- describeToolCall,
8
- getMachineId,
9
- isPolicyConfig,
10
- savePendingQuestion,
11
- sendNotification,
12
- waitForAnswer
13
- } from "./chunk-OF5WIOYS.js";
14
- import {
15
- withRetry
16
- } from "./chunk-3MIR7ODJ.js";
17
- import {
18
- getApiKey,
19
- getBaseUrl
20
- } from "./chunk-VUNL35KE.js";
21
-
22
- // src/policy.ts
23
- import { createHash } from "crypto";
24
- import { existsSync, readFileSync, writeFileSync } from "fs";
25
- import { join } from "path";
26
- import { tmpdir } from "os";
27
- var CACHE_TTL_MS = 5 * 60 * 1e3;
28
- var cacheFile = (apiKey) => {
29
- const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
30
- return join(tmpdir(), `pushary-policy-${hash}.json`);
31
- };
32
- var fetchPolicy = async (apiKey) => {
33
- return withRetry(async () => {
34
- const baseUrl = getBaseUrl();
35
- const response = await fetch(`${baseUrl}/api/mcp/policy`, {
36
- headers: { "Authorization": `Bearer ${apiKey}` },
37
- signal: AbortSignal.timeout(1e4)
38
- });
39
- if (!response.ok) {
40
- throw new Error(`Failed to fetch policy: ${response.status}`);
41
- }
42
- const raw = await response.json();
43
- if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
44
- return raw;
45
- }, { maxAttempts: 2 });
46
- };
47
- var getPolicy = async (apiKey) => {
48
- const path = cacheFile(apiKey);
49
- let staleCache = null;
50
- if (existsSync(path)) {
51
- try {
52
- const stat = readFileSync(path, "utf-8");
53
- const cached = JSON.parse(stat);
54
- if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
55
- if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
56
- return cached;
57
- }
58
- staleCache = cached;
59
- } catch {
60
- }
61
- }
62
- try {
63
- const policy = await fetchPolicy(apiKey);
64
- try {
65
- writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
66
- } catch {
67
- }
68
- return policy;
69
- } catch {
70
- if (staleCache) return staleCache;
71
- throw new Error("Failed to fetch policy and no cached policy available");
72
- }
73
- };
74
- var resolvePolicy = (config, toolName, modeOverride) => {
75
- const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
76
- tool: toolName,
77
- timeoutSeconds: config.defaultTimeoutSeconds,
78
- timeoutAction: config.defaultTimeoutAction,
79
- mode: config.defaultMode ?? "push_first",
80
- pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
81
- };
82
- const effectiveOverride = modeOverride ?? config.modeOverride;
83
- if (effectiveOverride) {
84
- return { ...base, mode: effectiveOverride };
85
- }
86
- return base;
87
- };
88
- var fetchModeState = async (apiKey, sessionId) => {
89
- try {
90
- const baseUrl = getBaseUrl();
91
- const url = sessionId ? `${baseUrl}/api/mcp/mode?session=${encodeURIComponent(sessionId)}` : `${baseUrl}/api/mcp/mode`;
92
- const response = await fetch(url, {
93
- headers: { "Authorization": `Bearer ${apiKey}` },
94
- signal: AbortSignal.timeout(3e3)
95
- });
96
- if (!response.ok) return { mode: null, kill: false };
97
- const data = await response.json();
98
- const mode = data.override?.mode;
99
- return { mode: isApprovalMode(mode) ? mode : null, kill: data.kill === true };
100
- } catch {
101
- return { mode: null, kill: false };
102
- }
103
- };
104
- var fetchModeOverride = async (apiKey) => (await fetchModeState(apiKey)).mode;
105
-
106
- // src/hook.ts
107
- import { basename } from "path";
108
- var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
109
- var allow = () => ({
110
- hookSpecificOutput: {
111
- hookEventName: "PreToolUse",
112
- permissionDecision: "allow"
113
- }
114
- });
115
- var deny = (reason) => ({
116
- hookSpecificOutput: {
117
- hookEventName: "PreToolUse",
118
- permissionDecision: "deny",
119
- permissionDecisionReason: reason
120
- }
121
- });
122
- var ask = (reason) => ({
123
- hookSpecificOutput: {
124
- hookEventName: "PreToolUse",
125
- permissionDecision: "ask",
126
- ...reason ? { permissionDecisionReason: reason } : {}
127
- }
128
- });
129
- var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
130
- while (Date.now() < deadlineMs) {
131
- const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
132
- let answer;
133
- try {
134
- answer = await waitForAnswer(apiKey, correlationId, remaining);
135
- } catch {
136
- if (Date.now() + pollInterval >= deadlineMs) break;
137
- await sleep(pollInterval);
138
- continue;
139
- }
140
- if (answer.answered) return answer;
141
- if (Date.now() + pollInterval >= deadlineMs) break;
142
- await sleep(pollInterval);
143
- }
144
- return { answered: false };
145
- };
146
- var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction, sessionId, machineId, toolName) => {
147
- let result;
148
- try {
149
- result = await askUser(apiKey, {
150
- question: `Allow ${description}?`,
151
- type: "confirm",
152
- context: `Agent wants to run this in ${projectName}`,
153
- agentName: `Claude Code - ${projectName}`,
154
- sessionId,
155
- machineId,
156
- toolName
157
- });
158
- } catch {
159
- switch (timeoutAction) {
160
- case "approve":
161
- return allow();
162
- case "deny":
163
- return deny("Push notification failed, denying per policy");
164
- default:
165
- return ask("Push notification failed, asking in terminal");
166
- }
167
- }
168
- const deadline = Date.now() + timeoutSeconds * 1e3;
169
- const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
170
- if (answer.answered) {
171
- return answer.value === "yes" ? allow() : deny("Denied via push notification");
172
- }
173
- switch (timeoutAction) {
174
- case "approve":
175
- return allow();
176
- case "deny":
177
- return deny("No response within timeout");
178
- default:
179
- return ask("No push response, asking in terminal");
180
- }
181
- };
182
- var handleTerminalOnly = () => {
183
- return ask();
184
- };
185
- var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds, sessionId, machineId, toolName) => {
186
- let result;
187
- try {
188
- result = await askUser(apiKey, {
189
- question: `Allow ${description}?`,
190
- type: "confirm",
191
- context: `Agent wants to run this in ${projectName}`,
192
- agentName: `Claude Code - ${projectName}`,
193
- sessionId,
194
- machineId,
195
- toolName
196
- });
197
- } catch {
198
- return ask("Push notification failed, asking in terminal");
199
- }
200
- const deadline = Date.now() + pushFirstSeconds * 1e3;
201
- const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
202
- if (answer.answered) {
203
- return answer.value === "yes" ? allow() : deny("Denied via push notification");
204
- }
205
- savePendingQuestion(sessionId || DEFAULT_SESSION, result.correlationId);
206
- return ask("Sent as push notification. You can also approve here.");
207
- };
208
- var handleNotifyOnly = async (apiKey, description, projectName, sessionId, machineId) => {
209
- try {
210
- await sendNotification(apiKey, {
211
- title: "Agent needs approval",
212
- body: description,
213
- agentName: `Claude Code - ${projectName}`,
214
- sessionId,
215
- machineId
216
- });
217
- } catch {
218
- }
219
- return ask();
220
- };
221
- var handlePreToolUse = async (input) => {
222
- try {
223
- const apiKey = getApiKey();
224
- const [policy, modeState] = await Promise.all([
225
- getPolicy(apiKey),
226
- fetchModeState(apiKey, input.session_id)
227
- ]);
228
- if (modeState.kill) {
229
- return deny("Stopped by user \u2014 this agent was halted from Pushary");
230
- }
231
- const toolPolicy = resolvePolicy(policy, input.tool_name, modeState.mode);
232
- if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
233
- return allow();
234
- }
235
- const description = describeToolCall(input.tool_name, input.tool_input, "hook");
236
- const projectName = basename(input.cwd ?? process.cwd());
237
- const sessionId = input.session_id;
238
- const machineId = getMachineId();
239
- switch (toolPolicy.mode) {
240
- case "push_only":
241
- return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction, sessionId, machineId, input.tool_name);
242
- case "terminal_only":
243
- return handleTerminalOnly();
244
- case "push_first":
245
- return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name);
246
- case "notify_only":
247
- return handleNotifyOnly(apiKey, description, projectName, sessionId, machineId);
248
- default:
249
- return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds, sessionId, machineId, input.tool_name);
250
- }
251
- } catch {
252
- return ask("Pushary unavailable, falling back to terminal approval");
253
- }
254
- };
255
-
256
- export {
257
- getPolicy,
258
- resolvePolicy,
259
- fetchModeState,
260
- fetchModeOverride,
261
- handlePreToolUse
262
- };
@@ -1,154 +0,0 @@
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
- };