@pushary/agent-hooks 0.11.1 → 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.
@@ -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.11.1",
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",
@@ -30,9 +31,9 @@
30
31
  "data"
31
32
  ],
32
33
  "scripts": {
33
- "build": "tsup",
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",