@pushary/agent-hooks 0.6.0 → 0.7.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.
@@ -1,14 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  reportEvent
4
- } from "../chunk-KYARP7KP.js";
4
+ } from "../chunk-5JEDLXEC.js";
5
5
  import {
6
6
  askUser,
7
7
  waitForAnswer
8
- } from "../chunk-4Z4MB37G.js";
8
+ } from "../chunk-EMPL27ZV.js";
9
+ import "../chunk-3MIR7ODJ.js";
9
10
  import {
10
11
  getApiKey
11
- } from "../chunk-O6A5RHWY.js";
12
+ } from "../chunk-VUNL35KE.js";
12
13
 
13
14
  // bin/pushary-codex.ts
14
15
  import { hostname } from "os";
@@ -2,7 +2,8 @@
2
2
  import {
3
3
  callMcpTool,
4
4
  sendMcpRequest
5
- } from "../chunk-O6A5RHWY.js";
5
+ } from "../chunk-3MIR7ODJ.js";
6
+ import "../chunk-VUNL35KE.js";
6
7
 
7
8
  // bin/pushary-doctor.ts
8
9
  import { existsSync, readFileSync } from "fs";
@@ -20,6 +21,7 @@ var fail = red("\u2717");
20
21
  var warn = yellow("!");
21
22
  var CLAUDE_SETTINGS = join(homedir(), ".claude", "settings.json");
22
23
  var SKILL_PATH = join(homedir(), ".claude", "skills", "pushary", "SKILL.md");
24
+ var SHELL_FILES = [".zshrc", ".zprofile", ".bashrc", ".bash_profile"].map((f) => join(homedir(), f));
23
25
  var readJson = (path) => {
24
26
  try {
25
27
  return JSON.parse(readFileSync(path, "utf-8"));
@@ -39,8 +41,28 @@ var main = async () => {
39
41
  console.log(` ${bold("Pushary Doctor")}`);
40
42
  console.log();
41
43
  console.log(` ${dim("Configuration")}`);
42
- const apiKey = process.env.PUSHARY_API_KEY;
43
- check(!!apiKey, "API key in environment", apiKey ? `pk_${apiKey.split(".")[0]?.slice(3, 7)}...` : "PUSHARY_API_KEY not set");
44
+ let apiKey = process.env.PUSHARY_API_KEY;
45
+ let keyFromProfile = false;
46
+ if (!apiKey) {
47
+ for (const f of SHELL_FILES) {
48
+ try {
49
+ const content = readFileSync(f, "utf-8");
50
+ const match = content.match(/export\s+PUSHARY_API_KEY=['"](pk_[a-f0-9]+\.[a-f0-9]+)['"]/);
51
+ if (match) {
52
+ apiKey = match[1];
53
+ keyFromProfile = true;
54
+ break;
55
+ }
56
+ } catch {
57
+ }
58
+ }
59
+ }
60
+ if (keyFromProfile) {
61
+ console.log(` ${pass} API key in shell profile ${dim(`(pk_${apiKey.split(".")[0]?.slice(3, 7)}...)`)}`);
62
+ console.log(` ${warn} Not loaded in this shell \u2014 run ${cyan("source ~/.zshrc")} or open a new terminal`);
63
+ } else {
64
+ check(!!apiKey, "API key in environment", apiKey ? `pk_${apiKey.split(".")[0]?.slice(3, 7)}...` : "PUSHARY_API_KEY not set");
65
+ }
44
66
  const CLAUDE_JSON = join(homedir(), ".claude.json");
45
67
  const claudeJson = readJson(CLAUDE_JSON);
46
68
  const mcpServers = claudeJson?.mcpServers ?? {};
@@ -61,7 +83,7 @@ var main = async () => {
61
83
  const permissions = settings.permissions;
62
84
  const hasWildcard = permissions?.allow?.some((r) => r === "mcp__pushary__*" || r === "MCP(pushary:*)") ?? false;
63
85
  check(hasWildcard, "Claude Code: Pushary tools auto-allowed", hasWildcard ? "mcp__pushary__*" : "missing");
64
- const hasLegacyPerms = permissions?.allow?.some((r) => r.startsWith("mcp__pushary__")) ?? false;
86
+ const hasLegacyPerms = permissions?.allow?.some((r) => r.startsWith("mcp__pushary__") && r !== "mcp__pushary__*") ?? false;
65
87
  if (hasLegacyPerms) {
66
88
  console.log(` ${warn} Legacy individual permissions detected ${dim("(run pushary clean, then setup again)")}`);
67
89
  }
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePreToolUse
4
- } from "../chunk-WNXGIEX7.js";
5
- import "../chunk-4Z4MB37G.js";
6
- import "../chunk-O6A5RHWY.js";
4
+ } from "../chunk-C5TFTNHG.js";
5
+ import "../chunk-EMPL27ZV.js";
6
+ import "../chunk-3MIR7ODJ.js";
7
+ import "../chunk-VUNL35KE.js";
7
8
 
8
9
  // bin/pushary-hook.ts
9
10
  var main = async () => {
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ getApiKey,
4
+ getBaseUrl
5
+ } from "../chunk-VUNL35KE.js";
6
+
7
+ // bin/pushary-mode.ts
8
+ var VALID_MODES = ["push_only", "push_first", "terminal_only", "notify_only"];
9
+ var dim = (s) => `\x1B[2m${s}\x1B[0m`;
10
+ var green = (s) => `\x1B[32m${s}\x1B[0m`;
11
+ var cyan = (s) => `\x1B[36m${s}\x1B[0m`;
12
+ var yellow = (s) => `\x1B[33m${s}\x1B[0m`;
13
+ var parseDuration = (value) => {
14
+ const match = value.match(/^(\d+)(m|h)$/);
15
+ if (!match) return null;
16
+ const num = Number(match[1]);
17
+ return match[2] === "h" ? num * 3600 : num * 60;
18
+ };
19
+ var main = async () => {
20
+ const apiKey = getApiKey();
21
+ const baseUrl = getBaseUrl();
22
+ const mode = process.argv[2];
23
+ const forFlag = process.argv[3];
24
+ const forValue = process.argv[4];
25
+ const headers = {
26
+ "Authorization": `Bearer ${apiKey}`,
27
+ "Content-Type": "application/json"
28
+ };
29
+ if (!mode || mode === "status") {
30
+ const res2 = await fetch(`${baseUrl}/api/mcp/mode`, { headers });
31
+ const data2 = await res2.json();
32
+ if (!data2.override) {
33
+ console.log(` Mode: ${cyan("default")} ${dim("(using per-tool policies)")}`);
34
+ } else {
35
+ console.log(` Mode: ${green(data2.override.mode)}`);
36
+ if (data2.override.expiresAt) {
37
+ console.log(` Expires: ${new Date(data2.override.expiresAt).toLocaleString()}`);
38
+ } else {
39
+ console.log(` ${dim("Sticky (no expiry)")}`);
40
+ }
41
+ }
42
+ return;
43
+ }
44
+ if (mode === "clear" || mode === "reset") {
45
+ await fetch(`${baseUrl}/api/mcp/mode`, { method: "DELETE", headers });
46
+ console.log(` ${green("\u2713")} Mode override cleared \u2014 using per-tool policies`);
47
+ return;
48
+ }
49
+ if (!VALID_MODES.includes(mode)) {
50
+ console.log(` ${yellow("!")} Invalid mode: ${mode}`);
51
+ console.log(` Valid modes: ${VALID_MODES.join(", ")}`);
52
+ console.log(` ${dim("Usage: pushary mode <mode> [--for <duration>]")}`);
53
+ console.log(` ${dim("Example: pushary mode push_only --for 30m")}`);
54
+ return;
55
+ }
56
+ let ttlSeconds;
57
+ if (forFlag === "--for" && forValue) {
58
+ const parsed = parseDuration(forValue);
59
+ if (!parsed) {
60
+ console.log(` ${yellow("!")} Invalid duration: ${forValue} ${dim("(use e.g. 30m, 1h)")}`);
61
+ return;
62
+ }
63
+ ttlSeconds = parsed;
64
+ }
65
+ const res = await fetch(`${baseUrl}/api/mcp/mode`, {
66
+ method: "PUT",
67
+ headers,
68
+ body: JSON.stringify({ mode, ttlSeconds })
69
+ });
70
+ if (!res.ok) {
71
+ const err = await res.json();
72
+ console.log(` ${yellow("!")} Failed: ${err.error ?? res.statusText}`);
73
+ return;
74
+ }
75
+ const data = await res.json();
76
+ console.log(` ${green("\u2713")} Mode set to ${cyan(data.override.mode)}`);
77
+ if (data.override.expiresAt) {
78
+ console.log(` Expires: ${new Date(data.override.expiresAt).toLocaleString()}`);
79
+ }
80
+ };
81
+ main().catch((err) => {
82
+ console.error(` ${yellow("!")} ${err instanceof Error ? err.message : err}`);
83
+ process.exit(1);
84
+ });
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handlePostToolUse
4
- } from "../chunk-KYARP7KP.js";
5
- import "../chunk-4Z4MB37G.js";
6
- import "../chunk-O6A5RHWY.js";
4
+ } from "../chunk-5JEDLXEC.js";
5
+ import "../chunk-EMPL27ZV.js";
6
+ import "../chunk-3MIR7ODJ.js";
7
+ import "../chunk-VUNL35KE.js";
7
8
 
8
9
  // bin/pushary-post-hook.ts
9
10
  var main = async () => {
@@ -442,9 +442,10 @@ var main = async () => {
442
442
  console.log(` ${green(bold("Setup complete."))}`);
443
443
  console.log();
444
444
  console.log(` ${dim("Next:")}`);
445
- console.log(` ${dim("1.")} Enable notifications on your phone at ${cyan("pushary.com")}`);
446
- console.log(` ${dim("2.")} Restart your agent to load the new config`);
447
- console.log(` ${dim("3.")} Run ${cyan("npx @pushary/agent-hooks doctor")} to verify`);
445
+ console.log(` ${dim("1.")} Load your API key: ${cyan("source ~/.zshrc")} ${dim("(or open a new terminal)")}`);
446
+ console.log(` ${dim("2.")} Enable notifications on your phone at ${cyan("pushary.com")}`);
447
+ console.log(` ${dim("3.")} Restart your agent to load the new config`);
448
+ console.log(` ${dim("4.")} Run ${cyan("npx @pushary/agent-hooks doctor")} to verify`);
448
449
  console.log();
449
450
  };
450
451
  main().catch((err) => {
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  handleStop
4
- } from "../chunk-KYARP7KP.js";
5
- import "../chunk-4Z4MB37G.js";
6
- import "../chunk-O6A5RHWY.js";
4
+ } from "../chunk-5JEDLXEC.js";
5
+ import "../chunk-EMPL27ZV.js";
6
+ import "../chunk-3MIR7ODJ.js";
7
+ import "../chunk-VUNL35KE.js";
7
8
 
8
9
  // bin/pushary-stop-hook.ts
9
10
  var main = async () => {
@@ -10,6 +10,8 @@ if (command === "setup") {
10
10
  await import("./pushary-clean.js");
11
11
  } else if (command === "doctor") {
12
12
  await import("./pushary-doctor.js");
13
+ } else if (command === "mode") {
14
+ await import("./pushary-mode.js");
13
15
  } else {
14
16
  console.log(`
15
17
  Pushary Agent Hooks
@@ -18,11 +20,12 @@ Commands:
18
20
  setup Configure Claude Code, Codex, Hermes, or Cursor with Pushary
19
21
  doctor Verify your Pushary installation is working
20
22
  clean Remove all Pushary configuration
23
+ mode Switch approval mode (push_only, push_first, terminal_only)
21
24
  hook Run as a PreToolUse hook (reads stdin, writes stdout)
22
25
 
23
26
  Usage:
24
27
  npx @pushary/agent-hooks@latest setup
25
28
  npx @pushary/agent-hooks@latest doctor
26
- npx @pushary/agent-hooks@latest clean
29
+ npx @pushary/agent-hooks@latest mode push_only --for 30m
27
30
  `);
28
31
  }
@@ -0,0 +1,219 @@
1
+ import {
2
+ askUser,
3
+ describeToolCall,
4
+ isPolicyConfig,
5
+ savePendingQuestion,
6
+ sendNotification,
7
+ waitForAnswer
8
+ } from "./chunk-4Z4MB37G.js";
9
+ import {
10
+ getApiKey,
11
+ getBaseUrl,
12
+ withRetry
13
+ } from "./chunk-O6A5RHWY.js";
14
+
15
+ // src/policy.ts
16
+ import { createHash } from "crypto";
17
+ import { existsSync, readFileSync, writeFileSync } from "fs";
18
+ import { join } from "path";
19
+ import { tmpdir } from "os";
20
+ var CACHE_TTL_MS = 5 * 60 * 1e3;
21
+ var cacheFile = (apiKey) => {
22
+ const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
23
+ return join(tmpdir(), `pushary-policy-${hash}.json`);
24
+ };
25
+ var fetchPolicy = async (apiKey) => {
26
+ return withRetry(async () => {
27
+ const baseUrl = getBaseUrl();
28
+ const response = await fetch(`${baseUrl}/api/mcp/policy`, {
29
+ headers: { "Authorization": `Bearer ${apiKey}` },
30
+ signal: AbortSignal.timeout(1e4)
31
+ });
32
+ if (!response.ok) {
33
+ throw new Error(`Failed to fetch policy: ${response.status}`);
34
+ }
35
+ const raw = await response.json();
36
+ if (!isPolicyConfig(raw)) throw new Error("Invalid policy response");
37
+ return raw;
38
+ }, { maxAttempts: 2 });
39
+ };
40
+ var getPolicy = async (apiKey) => {
41
+ const path = cacheFile(apiKey);
42
+ let staleCache = null;
43
+ if (existsSync(path)) {
44
+ try {
45
+ const stat = readFileSync(path, "utf-8");
46
+ const cached = JSON.parse(stat);
47
+ if (!isPolicyConfig(cached)) throw new Error("Corrupted cache");
48
+ if (!cached._cachedAt || Date.now() - cached._cachedAt < CACHE_TTL_MS) {
49
+ return cached;
50
+ }
51
+ staleCache = cached;
52
+ } catch {
53
+ }
54
+ }
55
+ try {
56
+ const policy = await fetchPolicy(apiKey);
57
+ try {
58
+ writeFileSync(path, JSON.stringify({ ...policy, _cachedAt: Date.now() }), "utf-8");
59
+ } catch {
60
+ }
61
+ return policy;
62
+ } catch {
63
+ if (staleCache) return staleCache;
64
+ throw new Error("Failed to fetch policy and no cached policy available");
65
+ }
66
+ };
67
+ var resolvePolicy = (config, toolName) => {
68
+ const exact = config.policies.find((p) => p.tool === toolName);
69
+ if (exact) return exact;
70
+ const wildcard = config.policies.find((p) => p.tool === "*");
71
+ if (wildcard) return wildcard;
72
+ return {
73
+ tool: toolName,
74
+ timeoutSeconds: config.defaultTimeoutSeconds,
75
+ timeoutAction: config.defaultTimeoutAction,
76
+ mode: config.defaultMode ?? "push_first",
77
+ pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
78
+ };
79
+ };
80
+
81
+ // src/hook.ts
82
+ import { basename } from "path";
83
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
84
+ var allow = () => ({
85
+ hookSpecificOutput: {
86
+ hookEventName: "PreToolUse",
87
+ permissionDecision: "allow"
88
+ }
89
+ });
90
+ var deny = (reason) => ({
91
+ hookSpecificOutput: {
92
+ hookEventName: "PreToolUse",
93
+ permissionDecision: "deny",
94
+ permissionDecisionReason: reason
95
+ }
96
+ });
97
+ var ask = (reason) => ({
98
+ hookSpecificOutput: {
99
+ hookEventName: "PreToolUse",
100
+ permissionDecision: "ask",
101
+ ...reason ? { permissionDecisionReason: reason } : {}
102
+ }
103
+ });
104
+ var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
105
+ while (Date.now() < deadlineMs) {
106
+ const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
107
+ let answer;
108
+ try {
109
+ answer = await waitForAnswer(apiKey, correlationId, remaining);
110
+ } catch {
111
+ if (Date.now() + pollInterval >= deadlineMs) break;
112
+ await sleep(pollInterval);
113
+ continue;
114
+ }
115
+ if (answer.answered) return answer;
116
+ if (Date.now() + pollInterval >= deadlineMs) break;
117
+ await sleep(pollInterval);
118
+ }
119
+ return { answered: false };
120
+ };
121
+ var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
122
+ let result;
123
+ try {
124
+ result = await askUser(apiKey, {
125
+ question: `Allow ${description}?`,
126
+ type: "confirm",
127
+ context: `Agent wants to run this in ${projectName}`,
128
+ agentName: `Claude Code - ${projectName}`
129
+ });
130
+ } catch {
131
+ switch (timeoutAction) {
132
+ case "approve":
133
+ return allow();
134
+ case "deny":
135
+ return deny("Push notification failed, denying per policy");
136
+ default:
137
+ return ask("Push notification failed, asking in terminal");
138
+ }
139
+ }
140
+ const deadline = Date.now() + timeoutSeconds * 1e3;
141
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
142
+ if (answer.answered) {
143
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
144
+ }
145
+ switch (timeoutAction) {
146
+ case "approve":
147
+ return allow();
148
+ case "deny":
149
+ return deny("No response within timeout");
150
+ default:
151
+ return ask("No push response, asking in terminal");
152
+ }
153
+ };
154
+ var handleTerminalOnly = () => {
155
+ return ask();
156
+ };
157
+ var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
158
+ let result;
159
+ try {
160
+ result = await askUser(apiKey, {
161
+ question: `Allow ${description}?`,
162
+ type: "confirm",
163
+ context: `Agent wants to run this in ${projectName}`,
164
+ agentName: `Claude Code - ${projectName}`
165
+ });
166
+ } catch {
167
+ return ask("Push notification failed, asking in terminal");
168
+ }
169
+ const deadline = Date.now() + pushFirstSeconds * 1e3;
170
+ const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
171
+ if (answer.answered) {
172
+ return answer.value === "yes" ? allow() : deny("Denied via push notification");
173
+ }
174
+ savePendingQuestion(result.correlationId);
175
+ return ask("Sent as push notification. You can also approve here.");
176
+ };
177
+ var handleNotifyOnly = async (apiKey, description, projectName) => {
178
+ try {
179
+ await sendNotification(apiKey, {
180
+ title: "Agent needs approval",
181
+ body: description,
182
+ agentName: `Claude Code - ${projectName}`
183
+ });
184
+ } catch {
185
+ }
186
+ return ask();
187
+ };
188
+ var handlePreToolUse = async (input) => {
189
+ try {
190
+ const apiKey = getApiKey();
191
+ const policy = await getPolicy(apiKey);
192
+ const toolPolicy = resolvePolicy(policy, input.tool_name);
193
+ if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
194
+ return allow();
195
+ }
196
+ const description = describeToolCall(input.tool_name, input.tool_input, "hook");
197
+ const projectName = basename(input.cwd ?? process.cwd());
198
+ switch (toolPolicy.mode) {
199
+ case "push_only":
200
+ return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
201
+ case "terminal_only":
202
+ return handleTerminalOnly();
203
+ case "push_first":
204
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
205
+ case "notify_only":
206
+ return handleNotifyOnly(apiKey, description, projectName);
207
+ default:
208
+ return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
209
+ }
210
+ } catch {
211
+ return ask("Pushary unavailable, falling back to terminal approval");
212
+ }
213
+ };
214
+
215
+ export {
216
+ getPolicy,
217
+ resolvePolicy,
218
+ handlePreToolUse
219
+ };
@@ -0,0 +1,112 @@
1
+ import {
2
+ getBaseUrl
3
+ } from "./chunk-VUNL35KE.js";
4
+
5
+ // src/retry.ts
6
+ var isRetryable = (err) => {
7
+ if (!(err instanceof Error)) return false;
8
+ const msg = err.message;
9
+ return /\b(502|503|429)\b/.test(msg) || /ECONNRESET|ECONNREFUSED|ETIMEDOUT|UND_ERR_CONNECT_TIMEOUT|fetch failed/i.test(msg) || msg.includes("AbortError");
10
+ };
11
+ var withRetry = async (fn, options = {}) => {
12
+ const {
13
+ maxAttempts = 3,
14
+ baseDelayMs = 500,
15
+ maxDelayMs = 5e3,
16
+ shouldRetry = isRetryable
17
+ } = options;
18
+ let lastError;
19
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
20
+ try {
21
+ return await fn();
22
+ } catch (err) {
23
+ lastError = err;
24
+ if (attempt + 1 >= maxAttempts || !shouldRetry(err)) throw err;
25
+ const jitter = Math.random() * 0.3 + 0.85;
26
+ const delay = Math.min(baseDelayMs * 2 ** attempt * jitter, maxDelayMs);
27
+ await new Promise((r) => setTimeout(r, delay));
28
+ }
29
+ }
30
+ throw lastError;
31
+ };
32
+
33
+ // src/mcp-http.ts
34
+ var parseSseJson = (body) => {
35
+ const messages = [];
36
+ for (const event of body.split(/\r?\n\r?\n/)) {
37
+ const data = event.split(/\r?\n/).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n").trim();
38
+ if (!data) continue;
39
+ try {
40
+ messages.push(JSON.parse(data));
41
+ } catch {
42
+ }
43
+ }
44
+ const message = messages.at(-1);
45
+ if (!message) throw new Error("Empty response from Pushary");
46
+ return message;
47
+ };
48
+ var parseMcpResponse = (body, contentType) => {
49
+ if (contentType?.includes("text/event-stream")) {
50
+ return parseSseJson(body);
51
+ }
52
+ return JSON.parse(body);
53
+ };
54
+ var sendMcpRequest = async (apiKey, message, options = {}) => {
55
+ return withRetry(async () => {
56
+ const baseUrl = options.baseUrl ?? getBaseUrl();
57
+ const fetchFn = options.fetchFn ?? fetch;
58
+ const headers = {
59
+ "Content-Type": "application/json",
60
+ "Accept": "application/json, text/event-stream",
61
+ "Authorization": `Bearer ${apiKey}`
62
+ };
63
+ if (options.sessionId) {
64
+ headers["Mcp-Session-Id"] = options.sessionId;
65
+ }
66
+ const response = await fetchFn(`${baseUrl}/api/mcp/mcp`, {
67
+ method: "POST",
68
+ headers,
69
+ body: JSON.stringify(message),
70
+ signal: options.timeoutMs ? AbortSignal.timeout(options.timeoutMs) : void 0
71
+ });
72
+ const body = await response.text();
73
+ const contentType = response.headers.get("content-type");
74
+ let data = null;
75
+ if (body.trim()) {
76
+ try {
77
+ data = parseMcpResponse(body, contentType);
78
+ } catch (err) {
79
+ if (response.ok) throw err;
80
+ }
81
+ }
82
+ if (!response.ok) {
83
+ const msg = data?.error?.message ?? (body.trim() || response.statusText);
84
+ throw new Error(`Pushary MCP error: ${response.status} ${msg}`);
85
+ }
86
+ if (!data) throw new Error("Empty response from Pushary");
87
+ if (data.error) throw new Error(data.error.message ?? "Pushary MCP error");
88
+ return {
89
+ data,
90
+ sessionId: response.headers.get("mcp-session-id") ?? "",
91
+ status: response.status,
92
+ statusText: response.statusText
93
+ };
94
+ }, { maxAttempts: options.maxRetries ?? 1 });
95
+ };
96
+ var callMcpTool = async (apiKey, toolName, params, options = {}) => {
97
+ const { data } = await sendMcpRequest(apiKey, {
98
+ jsonrpc: "2.0",
99
+ id: options.id ?? Date.now(),
100
+ method: "tools/call",
101
+ params: { name: toolName, arguments: params }
102
+ }, options);
103
+ const text = data.result?.content?.[0]?.text;
104
+ if (!text) throw new Error("Empty response from Pushary");
105
+ return JSON.parse(text);
106
+ };
107
+
108
+ export {
109
+ withRetry,
110
+ sendMcpRequest,
111
+ callMcpTool
112
+ };
@@ -0,0 +1,99 @@
1
+ import {
2
+ cancelQuestion,
3
+ describeToolCall,
4
+ listPendingQuestions,
5
+ removePendingQuestion
6
+ } from "./chunk-EMPL27ZV.js";
7
+ import {
8
+ withRetry
9
+ } from "./chunk-3MIR7ODJ.js";
10
+ import {
11
+ getApiKey,
12
+ getBaseUrl
13
+ } from "./chunk-VUNL35KE.js";
14
+
15
+ // src/events.ts
16
+ import { hostname } from "os";
17
+ import { basename } from "path";
18
+ var cleanupPendingQuestions = async () => {
19
+ try {
20
+ const files = listPendingQuestions();
21
+ const apiKey = getApiKey();
22
+ for (const correlationId of files) {
23
+ try {
24
+ await cancelQuestion(apiKey, correlationId);
25
+ } catch {
26
+ }
27
+ removePendingQuestion(correlationId);
28
+ }
29
+ } catch {
30
+ }
31
+ };
32
+ var reportEvent = async (event) => {
33
+ const apiKey = getApiKey();
34
+ const baseUrl = getBaseUrl();
35
+ await withRetry(async () => {
36
+ await fetch(`${baseUrl}/api/agent/event`, {
37
+ method: "POST",
38
+ headers: {
39
+ "Content-Type": "application/json",
40
+ "Authorization": `Bearer ${apiKey}`
41
+ },
42
+ body: JSON.stringify({
43
+ ...event,
44
+ machineId: event.machineId ?? hostname()
45
+ }),
46
+ signal: AbortSignal.timeout(1e4)
47
+ });
48
+ }, { maxAttempts: 2, baseDelayMs: 300 });
49
+ };
50
+ var handlePostToolUse = async (input) => {
51
+ try {
52
+ const projectName = basename(input.cwd ?? process.cwd());
53
+ const action = describeToolCall(input.tool_name, input.tool_input, "event");
54
+ const isError = input.tool_result && ("error" in input.tool_result || "is_error" in input.tool_result);
55
+ await Promise.allSettled([
56
+ cleanupPendingQuestions(),
57
+ reportEvent({
58
+ event: isError ? "tool_error" : "tool_complete",
59
+ agentType: "claude_code",
60
+ agentName: `Claude Code - ${projectName}`,
61
+ action,
62
+ error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0
63
+ })
64
+ ]);
65
+ } catch {
66
+ }
67
+ };
68
+ var handleStop = async (input) => {
69
+ try {
70
+ const projectName = basename(input.cwd ?? process.cwd());
71
+ await reportEvent({
72
+ event: "session_end",
73
+ agentType: "claude_code",
74
+ agentName: `Claude Code - ${projectName}`,
75
+ action: "Session ended"
76
+ });
77
+ } catch {
78
+ }
79
+ };
80
+ var handleNotification = async (input) => {
81
+ try {
82
+ const projectName = basename(input.cwd ?? process.cwd());
83
+ await reportEvent({
84
+ event: input.type === "error" ? "error" : "notification",
85
+ agentType: "claude_code",
86
+ agentName: `Claude Code - ${projectName}`,
87
+ action: input.title ?? input.message ?? "Notification",
88
+ error: input.type === "error" ? input.message : void 0
89
+ });
90
+ } catch {
91
+ }
92
+ };
93
+
94
+ export {
95
+ reportEvent,
96
+ handlePostToolUse,
97
+ handleStop,
98
+ handleNotification
99
+ };