@pushary/agent-hooks 0.4.5 → 0.5.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/data/SKILL.md +293 -0
- package/dist/bin/pushary-clean.js +10 -50
- package/dist/bin/pushary-codex.js +5 -5
- package/dist/bin/pushary-doctor.js +45 -121
- package/dist/bin/pushary-hook.js +3 -3
- package/dist/bin/pushary-post-hook.js +3 -2
- package/dist/bin/pushary-setup.js +58 -80
- package/dist/bin/pushary-stop-hook.js +3 -2
- package/dist/chunk-AC4UYAGX.js +136 -0
- package/dist/chunk-DF3BM6BF.js +195 -0
- package/dist/chunk-KTP2EPVB.js +27 -0
- package/dist/{chunk-EQE6Z4YQ.js → chunk-P4JH2Q7Z.js} +26 -1
- package/dist/chunk-VIST7ACL.js +88 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +8 -8
- package/package.json +5 -3
- package/dist/chunk-4TWRLEOX.js +0 -49
- package/dist/chunk-5ZMTG7GF.js +0 -184
- package/dist/chunk-I546R6K2.js +0 -165
- package/dist/chunk-KINE5LNQ.js +0 -136
- package/dist/chunk-VUNL35KE.js +0 -16
- package/dist/chunk-YKZWCOVP.js +0 -165
- package/dist/pushary-clean-M5RW2DG6.js +0 -154
- package/dist/pushary-clean-RM6TBJ3H.js +0 -147
- package/dist/pushary-doctor-EHLTPBD3.js +0 -252
- package/dist/pushary-doctor-LYMEFIZN.js +0 -254
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
var getApiKey = () => {
|
|
3
|
+
const key = process.env.PUSHARY_API_KEY;
|
|
4
|
+
if (!key) {
|
|
5
|
+
throw new Error(
|
|
6
|
+
"PUSHARY_API_KEY environment variable is not set. Get your API key at https://pushary.com/sign-up?from=ai-coding"
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
return key;
|
|
10
|
+
};
|
|
11
|
+
var getBaseUrl = () => process.env.PUSHARY_BASE_URL ?? "https://pushary.com";
|
|
12
|
+
|
|
13
|
+
// src/mcp-http.ts
|
|
14
|
+
var parseSseJson = (body) => {
|
|
15
|
+
const messages = [];
|
|
16
|
+
for (const event of body.split(/\r?\n\r?\n/)) {
|
|
17
|
+
const data = event.split(/\r?\n/).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n").trim();
|
|
18
|
+
if (!data) continue;
|
|
19
|
+
messages.push(JSON.parse(data));
|
|
20
|
+
}
|
|
21
|
+
const message = messages.at(-1);
|
|
22
|
+
if (!message) throw new Error("Empty response from Pushary");
|
|
23
|
+
return message;
|
|
24
|
+
};
|
|
25
|
+
var parseMcpResponse = (body, contentType) => {
|
|
26
|
+
if (contentType?.includes("text/event-stream")) {
|
|
27
|
+
return parseSseJson(body);
|
|
28
|
+
}
|
|
29
|
+
return JSON.parse(body);
|
|
30
|
+
};
|
|
31
|
+
var sendMcpRequest = async (apiKey, message, options = {}) => {
|
|
32
|
+
const baseUrl = options.baseUrl ?? getBaseUrl();
|
|
33
|
+
const fetchFn = options.fetchFn ?? fetch;
|
|
34
|
+
const headers = {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
"Accept": "application/json, text/event-stream",
|
|
37
|
+
"Authorization": `Bearer ${apiKey}`
|
|
38
|
+
};
|
|
39
|
+
if (options.sessionId) {
|
|
40
|
+
headers["Mcp-Session-Id"] = options.sessionId;
|
|
41
|
+
}
|
|
42
|
+
const response = await fetchFn(`${baseUrl}/api/mcp/mcp`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers,
|
|
45
|
+
body: JSON.stringify(message),
|
|
46
|
+
signal: options.timeoutMs ? AbortSignal.timeout(options.timeoutMs) : void 0
|
|
47
|
+
});
|
|
48
|
+
const body = await response.text();
|
|
49
|
+
const contentType = response.headers.get("content-type");
|
|
50
|
+
let data = null;
|
|
51
|
+
if (body.trim()) {
|
|
52
|
+
try {
|
|
53
|
+
data = parseMcpResponse(body, contentType);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (response.ok) throw err;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const message2 = data?.error?.message ?? (body.trim() || response.statusText);
|
|
60
|
+
throw new Error(`Pushary MCP error: ${response.status} ${message2}`);
|
|
61
|
+
}
|
|
62
|
+
if (!data) throw new Error("Empty response from Pushary");
|
|
63
|
+
if (data.error) throw new Error(data.error.message ?? "Pushary MCP error");
|
|
64
|
+
return {
|
|
65
|
+
data,
|
|
66
|
+
sessionId: response.headers.get("mcp-session-id") ?? "",
|
|
67
|
+
status: response.status,
|
|
68
|
+
statusText: response.statusText
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
var callMcpTool = async (apiKey, toolName, params, options = {}) => {
|
|
72
|
+
const { data } = await sendMcpRequest(apiKey, {
|
|
73
|
+
jsonrpc: "2.0",
|
|
74
|
+
id: options.id ?? Date.now(),
|
|
75
|
+
method: "tools/call",
|
|
76
|
+
params: { name: toolName, arguments: params }
|
|
77
|
+
}, options);
|
|
78
|
+
const text = data.result?.content?.[0]?.text;
|
|
79
|
+
if (!text) throw new Error("Empty response from Pushary");
|
|
80
|
+
return JSON.parse(text);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export {
|
|
84
|
+
getApiKey,
|
|
85
|
+
getBaseUrl,
|
|
86
|
+
sendMcpRequest,
|
|
87
|
+
callMcpTool
|
|
88
|
+
};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -61,15 +61,20 @@ declare const askUser: (apiKey: string, params: AskUserParams) => Promise<AskUse
|
|
|
61
61
|
declare const waitForAnswer: (apiKey: string, correlationId: string, timeoutMs?: number) => Promise<WaitForAnswerResponse>;
|
|
62
62
|
declare const cancelQuestion: (apiKey: string, correlationId: string) => Promise<void>;
|
|
63
63
|
|
|
64
|
+
type ApprovalMode = 'push_only' | 'terminal_only' | 'push_first' | 'notify_only';
|
|
64
65
|
interface ToolPolicy {
|
|
65
66
|
tool: string;
|
|
66
67
|
timeoutSeconds: number;
|
|
67
68
|
timeoutAction: 'approve' | 'deny' | 'escalate';
|
|
69
|
+
mode: ApprovalMode;
|
|
70
|
+
pushFirstSeconds: number;
|
|
68
71
|
}
|
|
69
72
|
interface PolicyConfig {
|
|
70
73
|
policies: ToolPolicy[];
|
|
71
74
|
defaultTimeoutSeconds: number;
|
|
72
75
|
defaultTimeoutAction: 'approve' | 'deny' | 'escalate';
|
|
76
|
+
defaultMode: ApprovalMode;
|
|
77
|
+
defaultPushFirstSeconds: number;
|
|
73
78
|
}
|
|
74
79
|
declare const getPolicy: (apiKey: string) => Promise<PolicyConfig>;
|
|
75
80
|
declare const resolvePolicy: (config: PolicyConfig, toolName: string) => ToolPolicy;
|
package/dist/src/index.js
CHANGED
|
@@ -2,22 +2,22 @@ import {
|
|
|
2
2
|
getPolicy,
|
|
3
3
|
handlePreToolUse,
|
|
4
4
|
resolvePolicy
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import {
|
|
7
|
-
askUser,
|
|
8
|
-
cancelQuestion,
|
|
9
|
-
waitForAnswer
|
|
10
|
-
} from "../chunk-4TWRLEOX.js";
|
|
5
|
+
} from "../chunk-DF3BM6BF.js";
|
|
11
6
|
import {
|
|
12
7
|
handleNotification,
|
|
13
8
|
handlePostToolUse,
|
|
14
9
|
handleStop,
|
|
15
10
|
reportEvent
|
|
16
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-P4JH2Q7Z.js";
|
|
12
|
+
import {
|
|
13
|
+
askUser,
|
|
14
|
+
cancelQuestion,
|
|
15
|
+
waitForAnswer
|
|
16
|
+
} from "../chunk-KTP2EPVB.js";
|
|
17
17
|
import {
|
|
18
18
|
getApiKey,
|
|
19
19
|
getBaseUrl
|
|
20
|
-
} from "../chunk-
|
|
20
|
+
} from "../chunk-VIST7ACL.js";
|
|
21
21
|
export {
|
|
22
22
|
askUser,
|
|
23
23
|
cancelQuestion,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushary/agent-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.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",
|
|
@@ -25,11 +25,13 @@
|
|
|
25
25
|
"pushary-doctor": "./dist/bin/pushary-doctor.js"
|
|
26
26
|
},
|
|
27
27
|
"files": [
|
|
28
|
-
"dist"
|
|
28
|
+
"dist",
|
|
29
|
+
"data"
|
|
29
30
|
],
|
|
30
31
|
"scripts": {
|
|
31
32
|
"build": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts --format esm --dts --outDir dist",
|
|
32
|
-
"dev": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts --format esm --watch"
|
|
33
|
+
"dev": "tsup src/index.ts bin/pushary.ts bin/pushary-hook.ts bin/pushary-post-hook.ts bin/pushary-stop-hook.ts bin/pushary-codex.ts bin/pushary-setup.ts bin/pushary-clean.ts bin/pushary-doctor.ts --format esm --watch",
|
|
34
|
+
"test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/mcp-http.test.ts"
|
|
33
35
|
},
|
|
34
36
|
"dependencies": {
|
|
35
37
|
"@inquirer/prompts": "^8.4.2"
|
package/dist/chunk-4TWRLEOX.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getBaseUrl
|
|
3
|
-
} from "./chunk-VUNL35KE.js";
|
|
4
|
-
|
|
5
|
-
// src/api.ts
|
|
6
|
-
var mcpToolCall = async (apiKey, toolName, params) => {
|
|
7
|
-
const baseUrl = getBaseUrl();
|
|
8
|
-
const response = await fetch(`${baseUrl}/api/mcp/mcp`, {
|
|
9
|
-
method: "POST",
|
|
10
|
-
headers: {
|
|
11
|
-
"Content-Type": "application/json",
|
|
12
|
-
"Authorization": `Bearer ${apiKey}`
|
|
13
|
-
},
|
|
14
|
-
body: JSON.stringify({
|
|
15
|
-
jsonrpc: "2.0",
|
|
16
|
-
id: Date.now(),
|
|
17
|
-
method: "tools/call",
|
|
18
|
-
params: { name: toolName, arguments: params }
|
|
19
|
-
})
|
|
20
|
-
});
|
|
21
|
-
if (!response.ok) {
|
|
22
|
-
throw new Error(`Pushary API error: ${response.status} ${response.statusText}`);
|
|
23
|
-
}
|
|
24
|
-
const json = await response.json();
|
|
25
|
-
if (json.error) throw new Error(json.error.message);
|
|
26
|
-
const text = json.result?.content?.[0]?.text;
|
|
27
|
-
if (!text) throw new Error("Empty response from Pushary");
|
|
28
|
-
return JSON.parse(text);
|
|
29
|
-
};
|
|
30
|
-
var askUser = async (apiKey, params) => {
|
|
31
|
-
const result = await mcpToolCall(apiKey, "ask_user", { ...params });
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
35
|
-
const result = await mcpToolCall(apiKey, "wait_for_answer", {
|
|
36
|
-
correlationId,
|
|
37
|
-
timeoutMs
|
|
38
|
-
});
|
|
39
|
-
return result;
|
|
40
|
-
};
|
|
41
|
-
var cancelQuestion = async (apiKey, correlationId) => {
|
|
42
|
-
await mcpToolCall(apiKey, "cancel_question", { correlationId });
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export {
|
|
46
|
-
askUser,
|
|
47
|
-
waitForAnswer,
|
|
48
|
-
cancelQuestion
|
|
49
|
-
};
|
package/dist/chunk-5ZMTG7GF.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
var getApiKey = () => {
|
|
3
|
-
const key = process.env.PUSHARY_API_KEY;
|
|
4
|
-
if (!key) {
|
|
5
|
-
throw new Error(
|
|
6
|
-
"PUSHARY_API_KEY environment variable is not set. Get your API key at https://pushary.com/sign-up?from=ai-coding"
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
return key;
|
|
10
|
-
};
|
|
11
|
-
var getBaseUrl = () => process.env.PUSHARY_BASE_URL ?? "https://pushary.com";
|
|
12
|
-
|
|
13
|
-
// src/api.ts
|
|
14
|
-
var mcpToolCall = async (apiKey, toolName, params) => {
|
|
15
|
-
const baseUrl = getBaseUrl();
|
|
16
|
-
const response = await fetch(`${baseUrl}/api/mcp/mcp`, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
"Authorization": `Bearer ${apiKey}`
|
|
21
|
-
},
|
|
22
|
-
body: JSON.stringify({
|
|
23
|
-
jsonrpc: "2.0",
|
|
24
|
-
id: Date.now(),
|
|
25
|
-
method: "tools/call",
|
|
26
|
-
params: { name: toolName, arguments: params }
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
throw new Error(`Pushary API error: ${response.status} ${response.statusText}`);
|
|
31
|
-
}
|
|
32
|
-
const json = await response.json();
|
|
33
|
-
if (json.error) throw new Error(json.error.message);
|
|
34
|
-
const text = json.result?.content?.[0]?.text;
|
|
35
|
-
if (!text) throw new Error("Empty response from Pushary");
|
|
36
|
-
return JSON.parse(text);
|
|
37
|
-
};
|
|
38
|
-
var askUser = async (apiKey, params) => {
|
|
39
|
-
const result = await mcpToolCall(apiKey, "ask_user", { ...params });
|
|
40
|
-
return result;
|
|
41
|
-
};
|
|
42
|
-
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
43
|
-
const result = await mcpToolCall(apiKey, "wait_for_answer", {
|
|
44
|
-
correlationId,
|
|
45
|
-
timeoutMs
|
|
46
|
-
});
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
var cancelQuestion = async (apiKey, correlationId) => {
|
|
50
|
-
await mcpToolCall(apiKey, "cancel_question", { correlationId });
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/policy.ts
|
|
54
|
-
import { createHash } from "crypto";
|
|
55
|
-
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
56
|
-
import { join } from "path";
|
|
57
|
-
import { tmpdir } from "os";
|
|
58
|
-
var cacheFile = (apiKey) => {
|
|
59
|
-
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
60
|
-
return join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
61
|
-
};
|
|
62
|
-
var fetchPolicy = async (apiKey) => {
|
|
63
|
-
const baseUrl = getBaseUrl();
|
|
64
|
-
const response = await fetch(`${baseUrl}/api/mcp/policy`, {
|
|
65
|
-
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
66
|
-
signal: AbortSignal.timeout(1e4)
|
|
67
|
-
});
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
throw new Error(`Failed to fetch policy: ${response.status}`);
|
|
70
|
-
}
|
|
71
|
-
return response.json();
|
|
72
|
-
};
|
|
73
|
-
var getPolicy = async (apiKey) => {
|
|
74
|
-
const path = cacheFile(apiKey);
|
|
75
|
-
if (existsSync(path)) {
|
|
76
|
-
try {
|
|
77
|
-
return JSON.parse(readFileSync(path, "utf-8"));
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const policy = await fetchPolicy(apiKey);
|
|
82
|
-
writeFileSync(path, JSON.stringify(policy), "utf-8");
|
|
83
|
-
return policy;
|
|
84
|
-
};
|
|
85
|
-
var resolvePolicy = (config, toolName) => {
|
|
86
|
-
const exact = config.policies.find((p) => p.tool === toolName);
|
|
87
|
-
if (exact) return exact;
|
|
88
|
-
const wildcard = config.policies.find((p) => p.tool === "*");
|
|
89
|
-
if (wildcard) return wildcard;
|
|
90
|
-
return {
|
|
91
|
-
tool: toolName,
|
|
92
|
-
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
93
|
-
timeoutAction: config.defaultTimeoutAction
|
|
94
|
-
};
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// src/hook.ts
|
|
98
|
-
import { basename } from "path";
|
|
99
|
-
var describeToolCall = (input) => {
|
|
100
|
-
const { tool_name, tool_input } = input;
|
|
101
|
-
switch (tool_name) {
|
|
102
|
-
case "Bash":
|
|
103
|
-
return `bash: ${tool_input.command ?? "(no command)"}`;
|
|
104
|
-
case "Write":
|
|
105
|
-
return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
106
|
-
case "Edit":
|
|
107
|
-
return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
108
|
-
case "Read":
|
|
109
|
-
return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
110
|
-
default:
|
|
111
|
-
return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
115
|
-
var allow = () => ({
|
|
116
|
-
hookSpecificOutput: {
|
|
117
|
-
hookEventName: "PreToolUse",
|
|
118
|
-
permissionDecision: "allow"
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
var deny = (reason) => ({
|
|
122
|
-
hookSpecificOutput: {
|
|
123
|
-
hookEventName: "PreToolUse",
|
|
124
|
-
permissionDecision: "deny",
|
|
125
|
-
permissionDecisionReason: reason
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
var ask = (reason) => ({
|
|
129
|
-
hookSpecificOutput: {
|
|
130
|
-
hookEventName: "PreToolUse",
|
|
131
|
-
permissionDecision: "ask",
|
|
132
|
-
permissionDecisionReason: reason
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
var handlePreToolUse = async (input) => {
|
|
136
|
-
const apiKey = getApiKey();
|
|
137
|
-
const policy = await getPolicy(apiKey);
|
|
138
|
-
const toolPolicy = resolvePolicy(policy, input.tool_name);
|
|
139
|
-
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
140
|
-
return allow();
|
|
141
|
-
}
|
|
142
|
-
const description = describeToolCall(input);
|
|
143
|
-
const projectName = basename(input.cwd ?? process.cwd());
|
|
144
|
-
const result = await askUser(apiKey, {
|
|
145
|
-
question: `Allow ${description}?`,
|
|
146
|
-
type: "confirm",
|
|
147
|
-
context: `Agent wants to run this in ${projectName}`,
|
|
148
|
-
agentName: `Claude Code - ${projectName}`
|
|
149
|
-
});
|
|
150
|
-
const deadline = Date.now() + toolPolicy.timeoutSeconds * 1e3;
|
|
151
|
-
const pollInterval = 2e3;
|
|
152
|
-
while (Date.now() < deadline) {
|
|
153
|
-
const remaining = Math.min(
|
|
154
|
-
Math.max(deadline - Date.now(), 1e3),
|
|
155
|
-
3e4
|
|
156
|
-
);
|
|
157
|
-
const answer = await waitForAnswer(apiKey, result.correlationId, remaining);
|
|
158
|
-
if (answer.answered) {
|
|
159
|
-
return answer.value === "yes" ? allow() : deny("Denied via Pushary push notification");
|
|
160
|
-
}
|
|
161
|
-
if (Date.now() + pollInterval >= deadline) break;
|
|
162
|
-
await sleep(pollInterval);
|
|
163
|
-
}
|
|
164
|
-
switch (toolPolicy.timeoutAction) {
|
|
165
|
-
case "approve":
|
|
166
|
-
return allow();
|
|
167
|
-
case "deny":
|
|
168
|
-
return deny("No response within timeout");
|
|
169
|
-
case "escalate":
|
|
170
|
-
default:
|
|
171
|
-
return ask("Pushary: no response \u2014 asking in terminal");
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
export {
|
|
176
|
-
getApiKey,
|
|
177
|
-
getBaseUrl,
|
|
178
|
-
askUser,
|
|
179
|
-
waitForAnswer,
|
|
180
|
-
cancelQuestion,
|
|
181
|
-
getPolicy,
|
|
182
|
-
resolvePolicy,
|
|
183
|
-
handlePreToolUse
|
|
184
|
-
};
|
package/dist/chunk-I546R6K2.js
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
var getApiKey = () => {
|
|
3
|
-
const key = process.env.PUSHARY_API_KEY;
|
|
4
|
-
if (!key) {
|
|
5
|
-
throw new Error(
|
|
6
|
-
"PUSHARY_API_KEY environment variable is not set. Get your API key at https://pushary.com/sign-up?from=ai-coding"
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
return key;
|
|
10
|
-
};
|
|
11
|
-
var getBaseUrl = () => process.env.PUSHARY_BASE_URL ?? "https://pushary.com";
|
|
12
|
-
|
|
13
|
-
// src/api.ts
|
|
14
|
-
var mcpToolCall = async (apiKey, toolName, params) => {
|
|
15
|
-
const baseUrl = getBaseUrl();
|
|
16
|
-
const response = await fetch(`${baseUrl}/api/mcp/mcp`, {
|
|
17
|
-
method: "POST",
|
|
18
|
-
headers: {
|
|
19
|
-
"Content-Type": "application/json",
|
|
20
|
-
"Authorization": `Bearer ${apiKey}`
|
|
21
|
-
},
|
|
22
|
-
body: JSON.stringify({
|
|
23
|
-
jsonrpc: "2.0",
|
|
24
|
-
id: Date.now(),
|
|
25
|
-
method: "tools/call",
|
|
26
|
-
params: { name: toolName, arguments: params }
|
|
27
|
-
})
|
|
28
|
-
});
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
throw new Error(`Pushary API error: ${response.status} ${response.statusText}`);
|
|
31
|
-
}
|
|
32
|
-
const json = await response.json();
|
|
33
|
-
if (json.error) throw new Error(json.error.message);
|
|
34
|
-
const text = json.result?.content?.[0]?.text;
|
|
35
|
-
if (!text) throw new Error("Empty response from Pushary");
|
|
36
|
-
return JSON.parse(text);
|
|
37
|
-
};
|
|
38
|
-
var askUser = async (apiKey, params) => {
|
|
39
|
-
const result = await mcpToolCall(apiKey, "ask_user", { ...params });
|
|
40
|
-
return result;
|
|
41
|
-
};
|
|
42
|
-
var waitForAnswer = async (apiKey, correlationId, timeoutMs = 3e4) => {
|
|
43
|
-
const result = await mcpToolCall(apiKey, "wait_for_answer", {
|
|
44
|
-
correlationId,
|
|
45
|
-
timeoutMs
|
|
46
|
-
});
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
var cancelQuestion = async (apiKey, correlationId) => {
|
|
50
|
-
await mcpToolCall(apiKey, "cancel_question", { correlationId });
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// src/policy.ts
|
|
54
|
-
import { createHash } from "crypto";
|
|
55
|
-
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
56
|
-
import { join } from "path";
|
|
57
|
-
import { tmpdir } from "os";
|
|
58
|
-
var cacheFile = (apiKey) => {
|
|
59
|
-
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
60
|
-
return join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
61
|
-
};
|
|
62
|
-
var fetchPolicy = async (apiKey) => {
|
|
63
|
-
const baseUrl = getBaseUrl();
|
|
64
|
-
const response = await fetch(`${baseUrl}/api/mcp/policy`, {
|
|
65
|
-
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
66
|
-
signal: AbortSignal.timeout(1e4)
|
|
67
|
-
});
|
|
68
|
-
if (!response.ok) {
|
|
69
|
-
throw new Error(`Failed to fetch policy: ${response.status}`);
|
|
70
|
-
}
|
|
71
|
-
return response.json();
|
|
72
|
-
};
|
|
73
|
-
var getPolicy = async (apiKey) => {
|
|
74
|
-
const path = cacheFile(apiKey);
|
|
75
|
-
if (existsSync(path)) {
|
|
76
|
-
try {
|
|
77
|
-
return JSON.parse(readFileSync(path, "utf-8"));
|
|
78
|
-
} catch {
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const policy = await fetchPolicy(apiKey);
|
|
82
|
-
writeFileSync(path, JSON.stringify(policy), "utf-8");
|
|
83
|
-
return policy;
|
|
84
|
-
};
|
|
85
|
-
var resolvePolicy = (config, toolName) => {
|
|
86
|
-
const exact = config.policies.find((p) => p.tool === toolName);
|
|
87
|
-
if (exact) return exact;
|
|
88
|
-
const wildcard = config.policies.find((p) => p.tool === "*");
|
|
89
|
-
if (wildcard) return wildcard;
|
|
90
|
-
return {
|
|
91
|
-
tool: toolName,
|
|
92
|
-
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
93
|
-
timeoutAction: config.defaultTimeoutAction
|
|
94
|
-
};
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
// src/hook.ts
|
|
98
|
-
import { basename } from "path";
|
|
99
|
-
var describeToolCall = (input) => {
|
|
100
|
-
const { tool_name, tool_input } = input;
|
|
101
|
-
switch (tool_name) {
|
|
102
|
-
case "Bash":
|
|
103
|
-
return `bash: ${tool_input.command ?? "(no command)"}`;
|
|
104
|
-
case "Write":
|
|
105
|
-
return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
106
|
-
case "Edit":
|
|
107
|
-
return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
108
|
-
case "Read":
|
|
109
|
-
return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
110
|
-
default:
|
|
111
|
-
return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
115
|
-
var handlePreToolUse = async (input) => {
|
|
116
|
-
const apiKey = getApiKey();
|
|
117
|
-
const policy = await getPolicy(apiKey);
|
|
118
|
-
const toolPolicy = resolvePolicy(policy, input.tool_name);
|
|
119
|
-
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
120
|
-
return { decision: "approve" };
|
|
121
|
-
}
|
|
122
|
-
const description = describeToolCall(input);
|
|
123
|
-
const cwd = process.cwd();
|
|
124
|
-
const projectName = basename(cwd);
|
|
125
|
-
const result = await askUser(apiKey, {
|
|
126
|
-
question: `Allow ${description}?`,
|
|
127
|
-
type: "confirm",
|
|
128
|
-
context: `Agent wants to run this in ${projectName}`,
|
|
129
|
-
agentName: `Claude Code - ${projectName}`
|
|
130
|
-
});
|
|
131
|
-
const deadline = Date.now() + toolPolicy.timeoutSeconds * 1e3;
|
|
132
|
-
const pollInterval = 2e3;
|
|
133
|
-
while (Date.now() < deadline) {
|
|
134
|
-
const remaining = Math.min(
|
|
135
|
-
Math.max(deadline - Date.now(), 1e3),
|
|
136
|
-
3e4
|
|
137
|
-
);
|
|
138
|
-
const answer = await waitForAnswer(apiKey, result.correlationId, remaining);
|
|
139
|
-
if (answer.answered) {
|
|
140
|
-
return answer.value === "yes" ? { decision: "approve" } : { decision: "deny", reason: "Denied via Pushary push notification" };
|
|
141
|
-
}
|
|
142
|
-
if (Date.now() + pollInterval >= deadline) break;
|
|
143
|
-
await sleep(pollInterval);
|
|
144
|
-
}
|
|
145
|
-
switch (toolPolicy.timeoutAction) {
|
|
146
|
-
case "approve":
|
|
147
|
-
return { decision: "approve" };
|
|
148
|
-
case "deny":
|
|
149
|
-
return { decision: "deny", reason: "No response within timeout" };
|
|
150
|
-
case "escalate":
|
|
151
|
-
default:
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export {
|
|
157
|
-
getApiKey,
|
|
158
|
-
getBaseUrl,
|
|
159
|
-
askUser,
|
|
160
|
-
waitForAnswer,
|
|
161
|
-
cancelQuestion,
|
|
162
|
-
getPolicy,
|
|
163
|
-
resolvePolicy,
|
|
164
|
-
handlePreToolUse
|
|
165
|
-
};
|
package/dist/chunk-KINE5LNQ.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
askUser,
|
|
3
|
-
waitForAnswer
|
|
4
|
-
} from "./chunk-4TWRLEOX.js";
|
|
5
|
-
import {
|
|
6
|
-
getApiKey,
|
|
7
|
-
getBaseUrl
|
|
8
|
-
} from "./chunk-VUNL35KE.js";
|
|
9
|
-
|
|
10
|
-
// src/policy.ts
|
|
11
|
-
import { createHash } from "crypto";
|
|
12
|
-
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
13
|
-
import { join } from "path";
|
|
14
|
-
import { tmpdir } from "os";
|
|
15
|
-
var cacheFile = (apiKey) => {
|
|
16
|
-
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
17
|
-
return join(tmpdir(), `pushary-policy-${hash}.json`);
|
|
18
|
-
};
|
|
19
|
-
var fetchPolicy = async (apiKey) => {
|
|
20
|
-
const baseUrl = getBaseUrl();
|
|
21
|
-
const response = await fetch(`${baseUrl}/api/mcp/policy`, {
|
|
22
|
-
headers: { "Authorization": `Bearer ${apiKey}` },
|
|
23
|
-
signal: AbortSignal.timeout(1e4)
|
|
24
|
-
});
|
|
25
|
-
if (!response.ok) {
|
|
26
|
-
throw new Error(`Failed to fetch policy: ${response.status}`);
|
|
27
|
-
}
|
|
28
|
-
return response.json();
|
|
29
|
-
};
|
|
30
|
-
var getPolicy = async (apiKey) => {
|
|
31
|
-
const path = cacheFile(apiKey);
|
|
32
|
-
if (existsSync(path)) {
|
|
33
|
-
try {
|
|
34
|
-
return JSON.parse(readFileSync(path, "utf-8"));
|
|
35
|
-
} catch {
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const policy = await fetchPolicy(apiKey);
|
|
39
|
-
writeFileSync(path, JSON.stringify(policy), "utf-8");
|
|
40
|
-
return policy;
|
|
41
|
-
};
|
|
42
|
-
var resolvePolicy = (config, toolName) => {
|
|
43
|
-
const exact = config.policies.find((p) => p.tool === toolName);
|
|
44
|
-
if (exact) return exact;
|
|
45
|
-
const wildcard = config.policies.find((p) => p.tool === "*");
|
|
46
|
-
if (wildcard) return wildcard;
|
|
47
|
-
return {
|
|
48
|
-
tool: toolName,
|
|
49
|
-
timeoutSeconds: config.defaultTimeoutSeconds,
|
|
50
|
-
timeoutAction: config.defaultTimeoutAction
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// src/hook.ts
|
|
55
|
-
import { basename } from "path";
|
|
56
|
-
var describeToolCall = (input) => {
|
|
57
|
-
const { tool_name, tool_input } = input;
|
|
58
|
-
switch (tool_name) {
|
|
59
|
-
case "Bash":
|
|
60
|
-
return `bash: ${tool_input.command ?? "(no command)"}`;
|
|
61
|
-
case "Write":
|
|
62
|
-
return `write file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
63
|
-
case "Edit":
|
|
64
|
-
return `edit file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
65
|
-
case "Read":
|
|
66
|
-
return `read file: ${tool_input.file_path ?? "(unknown path)"}`;
|
|
67
|
-
default:
|
|
68
|
-
return `${tool_name}: ${JSON.stringify(tool_input).slice(0, 200)}`;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
72
|
-
var allow = () => ({
|
|
73
|
-
hookSpecificOutput: {
|
|
74
|
-
hookEventName: "PreToolUse",
|
|
75
|
-
permissionDecision: "allow"
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
var deny = (reason) => ({
|
|
79
|
-
hookSpecificOutput: {
|
|
80
|
-
hookEventName: "PreToolUse",
|
|
81
|
-
permissionDecision: "deny",
|
|
82
|
-
permissionDecisionReason: reason
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
var ask = (reason) => ({
|
|
86
|
-
hookSpecificOutput: {
|
|
87
|
-
hookEventName: "PreToolUse",
|
|
88
|
-
permissionDecision: "ask",
|
|
89
|
-
permissionDecisionReason: reason
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
var handlePreToolUse = async (input) => {
|
|
93
|
-
const apiKey = getApiKey();
|
|
94
|
-
const policy = await getPolicy(apiKey);
|
|
95
|
-
const toolPolicy = resolvePolicy(policy, input.tool_name);
|
|
96
|
-
if (toolPolicy.timeoutSeconds === 0 && toolPolicy.timeoutAction === "approve") {
|
|
97
|
-
return allow();
|
|
98
|
-
}
|
|
99
|
-
const description = describeToolCall(input);
|
|
100
|
-
const projectName = basename(input.cwd ?? process.cwd());
|
|
101
|
-
const 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
|
-
});
|
|
107
|
-
const deadline = Date.now() + toolPolicy.timeoutSeconds * 1e3;
|
|
108
|
-
const pollInterval = 2e3;
|
|
109
|
-
while (Date.now() < deadline) {
|
|
110
|
-
const remaining = Math.min(
|
|
111
|
-
Math.max(deadline - Date.now(), 1e3),
|
|
112
|
-
3e4
|
|
113
|
-
);
|
|
114
|
-
const answer = await waitForAnswer(apiKey, result.correlationId, remaining);
|
|
115
|
-
if (answer.answered) {
|
|
116
|
-
return answer.value === "yes" ? allow() : deny("Denied via Pushary push notification");
|
|
117
|
-
}
|
|
118
|
-
if (Date.now() + pollInterval >= deadline) break;
|
|
119
|
-
await sleep(pollInterval);
|
|
120
|
-
}
|
|
121
|
-
switch (toolPolicy.timeoutAction) {
|
|
122
|
-
case "approve":
|
|
123
|
-
return allow();
|
|
124
|
-
case "deny":
|
|
125
|
-
return deny("No response within timeout");
|
|
126
|
-
case "escalate":
|
|
127
|
-
default:
|
|
128
|
-
return ask("Pushary: no response, asking in terminal");
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export {
|
|
133
|
-
getPolicy,
|
|
134
|
-
resolvePolicy,
|
|
135
|
-
handlePreToolUse
|
|
136
|
-
};
|