@pushary/agent-hooks 0.5.1 → 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.
- package/dist/bin/pushary-clean.js +13 -19
- package/dist/bin/pushary-codex.js +4 -3
- package/dist/bin/pushary-doctor.js +27 -5
- package/dist/bin/pushary-hook.js +12 -6
- package/dist/bin/pushary-mode.d.ts +1 -0
- package/dist/bin/pushary-mode.js +84 -0
- package/dist/bin/pushary-post-hook.js +4 -3
- package/dist/bin/pushary-setup.js +48 -57
- package/dist/bin/pushary-stop-hook.js +4 -3
- package/dist/bin/pushary.js +4 -1
- package/dist/chunk-2I6DLXJN.js +219 -0
- package/dist/chunk-3MIR7ODJ.js +112 -0
- package/dist/chunk-4Z4MB37G.js +96 -0
- package/dist/chunk-5JEDLXEC.js +99 -0
- package/dist/chunk-C5TFTNHG.js +244 -0
- package/dist/chunk-EMPL27ZV.js +96 -0
- package/dist/chunk-KYARP7KP.js +97 -0
- package/dist/chunk-O6A5RHWY.js +122 -0
- package/dist/chunk-ODUXELPM.js +219 -0
- package/dist/chunk-PMD5JSV3.js +242 -0
- package/dist/chunk-SDCIKREA.js +241 -0
- package/dist/chunk-VUNL35KE.js +16 -0
- package/dist/chunk-WNXGIEX7.js +219 -0
- package/dist/chunk-YTMKB44I.js +220 -0
- package/dist/pushary-mode-T7XOPI6Z.js +83 -0
- package/dist/src/index.d.ts +5 -3
- package/dist/src/index.js +7 -4
- package/package.json +8 -6
|
@@ -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 base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
|
|
69
|
+
tool: toolName,
|
|
70
|
+
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
71
|
+
timeoutAction: config.defaultTimeoutAction,
|
|
72
|
+
mode: config.defaultMode ?? "push_first",
|
|
73
|
+
pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
|
|
74
|
+
};
|
|
75
|
+
if (config.modeOverride) {
|
|
76
|
+
return { ...base, mode: config.modeOverride };
|
|
77
|
+
}
|
|
78
|
+
return base;
|
|
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,242 @@
|
|
|
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, modeOverride) => {
|
|
68
|
+
const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
|
|
69
|
+
tool: toolName,
|
|
70
|
+
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
71
|
+
timeoutAction: config.defaultTimeoutAction,
|
|
72
|
+
mode: config.defaultMode ?? "push_first",
|
|
73
|
+
pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
|
|
74
|
+
};
|
|
75
|
+
const effectiveOverride = modeOverride ?? config.modeOverride;
|
|
76
|
+
if (effectiveOverride) {
|
|
77
|
+
return { ...base, mode: effectiveOverride };
|
|
78
|
+
}
|
|
79
|
+
return base;
|
|
80
|
+
};
|
|
81
|
+
var fetchModeOverride = async (apiKey) => {
|
|
82
|
+
try {
|
|
83
|
+
const baseUrl = getBaseUrl();
|
|
84
|
+
const response = await fetch(`${baseUrl}/api/mcp/mode`, {
|
|
85
|
+
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
86
|
+
signal: AbortSignal.timeout(3e3)
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok) return null;
|
|
89
|
+
const data = await response.json();
|
|
90
|
+
const mode = data.override?.mode;
|
|
91
|
+
if (mode && ["push_only", "terminal_only", "push_first", "notify_only"].includes(mode)) {
|
|
92
|
+
return mode;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/hook.ts
|
|
101
|
+
import { basename } from "path";
|
|
102
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
103
|
+
var allow = () => ({
|
|
104
|
+
hookSpecificOutput: {
|
|
105
|
+
hookEventName: "PreToolUse",
|
|
106
|
+
permissionDecision: "allow"
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
var deny = (reason) => ({
|
|
110
|
+
hookSpecificOutput: {
|
|
111
|
+
hookEventName: "PreToolUse",
|
|
112
|
+
permissionDecision: "deny",
|
|
113
|
+
permissionDecisionReason: reason
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
var ask = (reason) => ({
|
|
117
|
+
hookSpecificOutput: {
|
|
118
|
+
hookEventName: "PreToolUse",
|
|
119
|
+
permissionDecision: "ask",
|
|
120
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
|
|
124
|
+
while (Date.now() < deadlineMs) {
|
|
125
|
+
const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
|
|
126
|
+
let answer;
|
|
127
|
+
try {
|
|
128
|
+
answer = await waitForAnswer(apiKey, correlationId, remaining);
|
|
129
|
+
} catch {
|
|
130
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
131
|
+
await sleep(pollInterval);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (answer.answered) return answer;
|
|
135
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
136
|
+
await sleep(pollInterval);
|
|
137
|
+
}
|
|
138
|
+
return { answered: false };
|
|
139
|
+
};
|
|
140
|
+
var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
|
|
141
|
+
let result;
|
|
142
|
+
try {
|
|
143
|
+
result = await askUser(apiKey, {
|
|
144
|
+
question: `Allow ${description}?`,
|
|
145
|
+
type: "confirm",
|
|
146
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
147
|
+
agentName: `Claude Code - ${projectName}`
|
|
148
|
+
});
|
|
149
|
+
} catch {
|
|
150
|
+
switch (timeoutAction) {
|
|
151
|
+
case "approve":
|
|
152
|
+
return allow();
|
|
153
|
+
case "deny":
|
|
154
|
+
return deny("Push notification failed, denying per policy");
|
|
155
|
+
default:
|
|
156
|
+
return ask("Push notification failed, asking in terminal");
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
160
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
|
|
161
|
+
if (answer.answered) {
|
|
162
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
163
|
+
}
|
|
164
|
+
switch (timeoutAction) {
|
|
165
|
+
case "approve":
|
|
166
|
+
return allow();
|
|
167
|
+
case "deny":
|
|
168
|
+
return deny("No response within timeout");
|
|
169
|
+
default:
|
|
170
|
+
return ask("No push response, asking in terminal");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var handleTerminalOnly = () => {
|
|
174
|
+
return ask();
|
|
175
|
+
};
|
|
176
|
+
var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
|
|
177
|
+
let result;
|
|
178
|
+
try {
|
|
179
|
+
result = await askUser(apiKey, {
|
|
180
|
+
question: `Allow ${description}?`,
|
|
181
|
+
type: "confirm",
|
|
182
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
183
|
+
agentName: `Claude Code - ${projectName}`
|
|
184
|
+
});
|
|
185
|
+
} catch {
|
|
186
|
+
return ask("Push notification failed, asking in terminal");
|
|
187
|
+
}
|
|
188
|
+
const deadline = Date.now() + pushFirstSeconds * 1e3;
|
|
189
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
|
|
190
|
+
if (answer.answered) {
|
|
191
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
192
|
+
}
|
|
193
|
+
savePendingQuestion(result.correlationId);
|
|
194
|
+
return ask("Sent as push notification. You can also approve here.");
|
|
195
|
+
};
|
|
196
|
+
var handleNotifyOnly = async (apiKey, description, projectName) => {
|
|
197
|
+
try {
|
|
198
|
+
await sendNotification(apiKey, {
|
|
199
|
+
title: "Agent needs approval",
|
|
200
|
+
body: description,
|
|
201
|
+
agentName: `Claude Code - ${projectName}`
|
|
202
|
+
});
|
|
203
|
+
} catch {
|
|
204
|
+
}
|
|
205
|
+
return ask();
|
|
206
|
+
};
|
|
207
|
+
var handlePreToolUse = async (input) => {
|
|
208
|
+
try {
|
|
209
|
+
const apiKey = getApiKey();
|
|
210
|
+
const [policy, modeOverride] = await Promise.all([
|
|
211
|
+
getPolicy(apiKey),
|
|
212
|
+
fetchModeOverride(apiKey)
|
|
213
|
+
]);
|
|
214
|
+
const toolPolicy = resolvePolicy(policy, input.tool_name, modeOverride);
|
|
215
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
216
|
+
return allow();
|
|
217
|
+
}
|
|
218
|
+
const description = describeToolCall(input.tool_name, input.tool_input, "hook");
|
|
219
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
220
|
+
switch (toolPolicy.mode) {
|
|
221
|
+
case "push_only":
|
|
222
|
+
return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
|
|
223
|
+
case "terminal_only":
|
|
224
|
+
return handleTerminalOnly();
|
|
225
|
+
case "push_first":
|
|
226
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
|
|
227
|
+
case "notify_only":
|
|
228
|
+
return handleNotifyOnly(apiKey, description, projectName);
|
|
229
|
+
default:
|
|
230
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
|
|
231
|
+
}
|
|
232
|
+
} catch {
|
|
233
|
+
return ask("Pushary unavailable, falling back to terminal approval");
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export {
|
|
238
|
+
getPolicy,
|
|
239
|
+
resolvePolicy,
|
|
240
|
+
fetchModeOverride,
|
|
241
|
+
handlePreToolUse
|
|
242
|
+
};
|
|
@@ -0,0 +1,241 @@
|
|
|
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, modeOverride) => {
|
|
68
|
+
const base = config.policies.find((p) => p.tool === toolName) ?? config.policies.find((p) => p.tool === "*") ?? {
|
|
69
|
+
tool: toolName,
|
|
70
|
+
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
71
|
+
timeoutAction: config.defaultTimeoutAction,
|
|
72
|
+
mode: config.defaultMode ?? "push_first",
|
|
73
|
+
pushFirstSeconds: config.defaultPushFirstSeconds ?? 20
|
|
74
|
+
};
|
|
75
|
+
const effectiveOverride = modeOverride ?? config.modeOverride;
|
|
76
|
+
if (effectiveOverride) {
|
|
77
|
+
return { ...base, mode: effectiveOverride };
|
|
78
|
+
}
|
|
79
|
+
return base;
|
|
80
|
+
};
|
|
81
|
+
var fetchModeOverride = async (apiKey) => {
|
|
82
|
+
try {
|
|
83
|
+
const baseUrl = getBaseUrl();
|
|
84
|
+
const response = await fetch(`${baseUrl}/api/mcp/mode`, {
|
|
85
|
+
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
86
|
+
signal: AbortSignal.timeout(3e3)
|
|
87
|
+
});
|
|
88
|
+
if (!response.ok) return null;
|
|
89
|
+
const data = await response.json();
|
|
90
|
+
const mode = data.override?.mode;
|
|
91
|
+
if (mode && ["push_only", "terminal_only", "push_first", "notify_only"].includes(mode)) {
|
|
92
|
+
return mode;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/hook.ts
|
|
101
|
+
import { basename } from "path";
|
|
102
|
+
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
103
|
+
var allow = () => ({
|
|
104
|
+
hookSpecificOutput: {
|
|
105
|
+
hookEventName: "PreToolUse",
|
|
106
|
+
permissionDecision: "allow"
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
var deny = (reason) => ({
|
|
110
|
+
hookSpecificOutput: {
|
|
111
|
+
hookEventName: "PreToolUse",
|
|
112
|
+
permissionDecision: "deny",
|
|
113
|
+
permissionDecisionReason: reason
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
var ask = (reason) => ({
|
|
117
|
+
hookSpecificOutput: {
|
|
118
|
+
hookEventName: "PreToolUse",
|
|
119
|
+
permissionDecision: "ask",
|
|
120
|
+
...reason ? { permissionDecisionReason: reason } : {}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
var pollForAnswer = async (apiKey, correlationId, deadlineMs, pollInterval = 2e3) => {
|
|
124
|
+
while (Date.now() < deadlineMs) {
|
|
125
|
+
const remaining = Math.min(Math.max(deadlineMs - Date.now(), 1e3), 3e4);
|
|
126
|
+
let answer;
|
|
127
|
+
try {
|
|
128
|
+
answer = await waitForAnswer(apiKey, correlationId, remaining);
|
|
129
|
+
} catch {
|
|
130
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
131
|
+
await sleep(pollInterval);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (answer.answered) return answer;
|
|
135
|
+
if (Date.now() + pollInterval >= deadlineMs) break;
|
|
136
|
+
await sleep(pollInterval);
|
|
137
|
+
}
|
|
138
|
+
return { answered: false };
|
|
139
|
+
};
|
|
140
|
+
var handlePushOnly = async (apiKey, description, projectName, timeoutSeconds, timeoutAction) => {
|
|
141
|
+
let result;
|
|
142
|
+
try {
|
|
143
|
+
result = await askUser(apiKey, {
|
|
144
|
+
question: `Allow ${description}?`,
|
|
145
|
+
type: "confirm",
|
|
146
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
147
|
+
agentName: `Claude Code - ${projectName}`
|
|
148
|
+
});
|
|
149
|
+
} catch {
|
|
150
|
+
switch (timeoutAction) {
|
|
151
|
+
case "approve":
|
|
152
|
+
return allow();
|
|
153
|
+
case "deny":
|
|
154
|
+
return deny("Push notification failed, denying per policy");
|
|
155
|
+
default:
|
|
156
|
+
return ask("Push notification failed, asking in terminal");
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const deadline = Date.now() + timeoutSeconds * 1e3;
|
|
160
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline);
|
|
161
|
+
if (answer.answered) {
|
|
162
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
163
|
+
}
|
|
164
|
+
switch (timeoutAction) {
|
|
165
|
+
case "approve":
|
|
166
|
+
return allow();
|
|
167
|
+
case "deny":
|
|
168
|
+
return deny("No response within timeout");
|
|
169
|
+
default:
|
|
170
|
+
return ask("No push response, asking in terminal");
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
var handleTerminalOnly = () => {
|
|
174
|
+
return ask();
|
|
175
|
+
};
|
|
176
|
+
var handlePushFirst = async (apiKey, description, projectName, pushFirstSeconds) => {
|
|
177
|
+
let result;
|
|
178
|
+
try {
|
|
179
|
+
result = await askUser(apiKey, {
|
|
180
|
+
question: `Allow ${description}?`,
|
|
181
|
+
type: "confirm",
|
|
182
|
+
context: `Agent wants to run this in ${projectName}`,
|
|
183
|
+
agentName: `Claude Code - ${projectName}`
|
|
184
|
+
});
|
|
185
|
+
} catch {
|
|
186
|
+
return ask("Push notification failed, asking in terminal");
|
|
187
|
+
}
|
|
188
|
+
const deadline = Date.now() + pushFirstSeconds * 1e3;
|
|
189
|
+
const answer = await pollForAnswer(apiKey, result.correlationId, deadline, 1500);
|
|
190
|
+
if (answer.answered) {
|
|
191
|
+
return answer.value === "yes" ? allow() : deny("Denied via push notification");
|
|
192
|
+
}
|
|
193
|
+
savePendingQuestion(result.correlationId);
|
|
194
|
+
return ask("Sent as push notification. You can also approve here.");
|
|
195
|
+
};
|
|
196
|
+
var handleNotifyOnly = async (apiKey, description, projectName) => {
|
|
197
|
+
try {
|
|
198
|
+
await sendNotification(apiKey, {
|
|
199
|
+
title: "Agent needs approval",
|
|
200
|
+
body: description,
|
|
201
|
+
agentName: `Claude Code - ${projectName}`
|
|
202
|
+
});
|
|
203
|
+
} catch {
|
|
204
|
+
}
|
|
205
|
+
return ask();
|
|
206
|
+
};
|
|
207
|
+
var handlePreToolUse = async (input) => {
|
|
208
|
+
try {
|
|
209
|
+
const apiKey = getApiKey();
|
|
210
|
+
const [policy, modeOverride] = await Promise.all([
|
|
211
|
+
getPolicy(apiKey),
|
|
212
|
+
fetchModeOverride(apiKey)
|
|
213
|
+
]);
|
|
214
|
+
const toolPolicy = resolvePolicy(policy, input.tool_name, modeOverride);
|
|
215
|
+
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
216
|
+
return allow();
|
|
217
|
+
}
|
|
218
|
+
const description = describeToolCall(input.tool_name, input.tool_input, "hook");
|
|
219
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
220
|
+
switch (toolPolicy.mode) {
|
|
221
|
+
case "push_only":
|
|
222
|
+
return handlePushOnly(apiKey, description, projectName, toolPolicy.timeoutSeconds, toolPolicy.timeoutAction);
|
|
223
|
+
case "terminal_only":
|
|
224
|
+
return handleTerminalOnly();
|
|
225
|
+
case "push_first":
|
|
226
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
|
|
227
|
+
case "notify_only":
|
|
228
|
+
return handleNotifyOnly(apiKey, description, projectName);
|
|
229
|
+
default:
|
|
230
|
+
return handlePushFirst(apiKey, description, projectName, toolPolicy.pushFirstSeconds);
|
|
231
|
+
}
|
|
232
|
+
} catch {
|
|
233
|
+
return ask("Pushary unavailable, falling back to terminal approval");
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
export {
|
|
238
|
+
getPolicy,
|
|
239
|
+
resolvePolicy,
|
|
240
|
+
handlePreToolUse
|
|
241
|
+
};
|