@pushary/agent-hooks 0.15.0 → 0.16.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-codex-hook.js +1 -1
- package/dist/bin/pushary-codex.js +1 -1
- package/dist/bin/pushary-post-hook.js +1 -1
- package/dist/bin/pushary-prompt-hook.js +1 -1
- package/dist/bin/pushary-setup.js +150 -6
- package/dist/bin/pushary-stop-hook.js +3 -2
- package/dist/chunk-U6OXZPYC.js +384 -0
- package/dist/src/index.d.ts +10 -2
- package/dist/src/index.js +1 -1
- package/package.json +6 -3
|
@@ -25,6 +25,47 @@ import { parse as parseTOML, stringify as stringifyTOML } from "smol-toml";
|
|
|
25
25
|
|
|
26
26
|
// src/onboarding.ts
|
|
27
27
|
import qrcodeTerminal from "qrcode-terminal";
|
|
28
|
+
|
|
29
|
+
// src/pairing.ts
|
|
30
|
+
import crypto from "crypto";
|
|
31
|
+
var X25519_SPKI_PREFIX = Buffer.from("302a300506032b656e032100", "hex");
|
|
32
|
+
var HKDF_INFO = Buffer.from("pushary-pair-v1");
|
|
33
|
+
var HEADER_BYTES = 32 + 12 + 16;
|
|
34
|
+
var rawToPublicKey = (raw) => crypto.createPublicKey({
|
|
35
|
+
key: Buffer.concat([X25519_SPKI_PREFIX, raw]),
|
|
36
|
+
format: "der",
|
|
37
|
+
type: "spki"
|
|
38
|
+
});
|
|
39
|
+
var publicKeyToRaw = (key) => key.export({ format: "der", type: "spki" }).subarray(-32);
|
|
40
|
+
var generateKeypair = () => {
|
|
41
|
+
const { publicKey, privateKey } = crypto.generateKeyPairSync("x25519");
|
|
42
|
+
const publicKeyRaw = publicKeyToRaw(publicKey);
|
|
43
|
+
return { publicKeyB64: publicKeyRaw.toString("base64"), publicKeyRaw, privateKey };
|
|
44
|
+
};
|
|
45
|
+
var publicKeyFingerprint = (b64) => {
|
|
46
|
+
const raw = Buffer.from(b64, "base64");
|
|
47
|
+
const digest = crypto.createHash("sha256").update(raw).digest("hex");
|
|
48
|
+
return (digest.slice(0, 8).match(/.{2}/g) ?? []).join(" ").toUpperCase();
|
|
49
|
+
};
|
|
50
|
+
var openSealedKey = (blobB64, keypair) => {
|
|
51
|
+
const blob = Buffer.from(blobB64, "base64");
|
|
52
|
+
if (blob.length <= HEADER_BYTES) throw new Error("sealed payload too short");
|
|
53
|
+
const ephemeralRaw = blob.subarray(0, 32);
|
|
54
|
+
const nonce = blob.subarray(32, 44);
|
|
55
|
+
const tag = blob.subarray(44, 60);
|
|
56
|
+
const ciphertext = blob.subarray(60);
|
|
57
|
+
const shared = crypto.diffieHellman({
|
|
58
|
+
privateKey: keypair.privateKey,
|
|
59
|
+
publicKey: rawToPublicKey(ephemeralRaw)
|
|
60
|
+
});
|
|
61
|
+
const salt = Buffer.concat([ephemeralRaw, keypair.publicKeyRaw]);
|
|
62
|
+
const key = Buffer.from(crypto.hkdfSync("sha256", shared, salt, HKDF_INFO, 32));
|
|
63
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
64
|
+
decipher.setAuthTag(tag);
|
|
65
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString("utf8");
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// src/onboarding.ts
|
|
28
69
|
var dim = (s) => `\x1B[2m${s}\x1B[0m`;
|
|
29
70
|
var bold = (s) => `\x1B[1m${s}\x1B[0m`;
|
|
30
71
|
var green = (s) => `\x1B[32m${s}\x1B[0m`;
|
|
@@ -174,6 +215,80 @@ var printConnectInstructions = async (apiKey) => {
|
|
|
174
215
|
const target = site?.subscribeUrl ?? "https://pushary.com";
|
|
175
216
|
console.log(` ${dim("Connect your phone for push notifications at")} ${cyan(target)}`);
|
|
176
217
|
};
|
|
218
|
+
var PAIR_POLL_INTERVAL_MS = 2e3;
|
|
219
|
+
var PAIR_TIMEOUT_MS = 18e4;
|
|
220
|
+
var startPairing = async (cliPublicKey) => {
|
|
221
|
+
try {
|
|
222
|
+
const res = await fetch(`${API_BASE}/api/mobile/pair/start`, {
|
|
223
|
+
method: "POST",
|
|
224
|
+
headers: { "Content-Type": "application/json" },
|
|
225
|
+
body: JSON.stringify({ cliPublicKey }),
|
|
226
|
+
signal: AbortSignal.timeout(1e4)
|
|
227
|
+
});
|
|
228
|
+
if (!res.ok) return null;
|
|
229
|
+
const data = await res.json().catch(() => null);
|
|
230
|
+
return data && typeof data.pairId === "string" ? data.pairId : null;
|
|
231
|
+
} catch {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var claimPairing = async (pairId) => {
|
|
236
|
+
try {
|
|
237
|
+
const res = await fetch(`${API_BASE}/api/mobile/pair/claim?id=${encodeURIComponent(pairId)}`, {
|
|
238
|
+
signal: AbortSignal.timeout(1e4)
|
|
239
|
+
});
|
|
240
|
+
if (!res.ok) return null;
|
|
241
|
+
return await res.json().catch(() => null);
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
var connectViaAppPairing = async () => {
|
|
247
|
+
const keypair = generateKeypair();
|
|
248
|
+
const pairId = await startPairing(keypair.publicKeyB64);
|
|
249
|
+
if (!pairId) {
|
|
250
|
+
console.log(` ${yellow("!")} Couldn't start pairing. Check your connection and try again.`);
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
const deepLink = `pushary://pair?pk=${encodeURIComponent(keypair.publicKeyB64)}&id=${encodeURIComponent(pairId)}`;
|
|
254
|
+
console.log();
|
|
255
|
+
console.log(` ${bold("Connect your Pushary app")}`);
|
|
256
|
+
console.log(` ${dim("Open the Pushary app, tap Connect agent, and scan this:")}`);
|
|
257
|
+
await printQr(deepLink);
|
|
258
|
+
console.log(` ${dim("Confirm the app shows fingerprint")} ${cyan(publicKeyFingerprint(keypair.publicKeyB64))}`);
|
|
259
|
+
console.log();
|
|
260
|
+
const deadline = Date.now() + PAIR_TIMEOUT_MS;
|
|
261
|
+
const stop = startSpinner(() => {
|
|
262
|
+
const left = Math.max(0, Math.ceil((deadline - Date.now()) / 1e3));
|
|
263
|
+
return `Waiting for the app to authorize ${dim(`(${left}s)`)}`;
|
|
264
|
+
});
|
|
265
|
+
try {
|
|
266
|
+
while (Date.now() < deadline) {
|
|
267
|
+
const claim = await claimPairing(pairId);
|
|
268
|
+
if (claim?.status === "authorized") {
|
|
269
|
+
let apiKey;
|
|
270
|
+
try {
|
|
271
|
+
apiKey = openSealedKey(claim.sealed, keypair);
|
|
272
|
+
} catch {
|
|
273
|
+
stop(yellow("!"), "Pairing failed to decrypt \u2014 re-run setup to try again");
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
stop(check, "App connected");
|
|
277
|
+
return { apiKey, handle: claim.siteSlug };
|
|
278
|
+
}
|
|
279
|
+
if (claim?.status === "expired") {
|
|
280
|
+
stop(yellow("!"), "Pairing expired \u2014 re-run setup to try again");
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
await sleep(PAIR_POLL_INTERVAL_MS);
|
|
284
|
+
}
|
|
285
|
+
stop(yellow("!"), "Timed out waiting for the app");
|
|
286
|
+
return null;
|
|
287
|
+
} catch (err) {
|
|
288
|
+
stop(yellow("!"), `Pairing error ${dim(`(${err instanceof Error ? err.message : "network"})`)}`);
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
177
292
|
|
|
178
293
|
// bin/pushary-setup.ts
|
|
179
294
|
var CLAUDE_SETTINGS = join(homedir(), ".claude", "settings.json");
|
|
@@ -199,6 +314,18 @@ var parseKeyFlag = () => {
|
|
|
199
314
|
}
|
|
200
315
|
return void 0;
|
|
201
316
|
};
|
|
317
|
+
var parseConnectFlag = () => {
|
|
318
|
+
const args = process.argv.slice(2);
|
|
319
|
+
for (let i = 0; i < args.length; i++) {
|
|
320
|
+
if (args[i] === "--connect" && args[i + 1]) {
|
|
321
|
+
return args[i + 1].trim() === "app" ? "app" : "web";
|
|
322
|
+
}
|
|
323
|
+
if (args[i].startsWith("--connect=")) {
|
|
324
|
+
return args[i].slice(10).trim() === "app" ? "app" : "web";
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return "web";
|
|
328
|
+
};
|
|
202
329
|
var isInstalled = (command) => {
|
|
203
330
|
const whichCmd = process.platform === "win32" ? "where" : "which";
|
|
204
331
|
try {
|
|
@@ -648,10 +775,25 @@ var main = async () => {
|
|
|
648
775
|
console.log(` ${dim2("Push notifications for AI coding agents")}`);
|
|
649
776
|
console.log();
|
|
650
777
|
await checkForUpdates(version);
|
|
778
|
+
const connectMode = parseConnectFlag();
|
|
651
779
|
const flagKey = parseKeyFlag();
|
|
652
780
|
const envKey = process.env.PUSHARY_API_KEY?.trim();
|
|
653
781
|
let trimmedKey;
|
|
654
|
-
if (
|
|
782
|
+
if (connectMode === "app") {
|
|
783
|
+
console.log(` ${dim2("Pairing with your Pushary app.")}`);
|
|
784
|
+
const paired = await connectViaAppPairing();
|
|
785
|
+
if (!paired) {
|
|
786
|
+
console.log();
|
|
787
|
+
console.log(` ${yellow2("!")} Pairing didn't complete. Re-run setup when you're ready to scan.`);
|
|
788
|
+
console.log(` ${dim2("No app yet? Get it at")} ${cyan2("https://pushary.com/app")}`);
|
|
789
|
+
process.exit(1);
|
|
790
|
+
}
|
|
791
|
+
trimmedKey = paired.apiKey;
|
|
792
|
+
if (paired.handle) {
|
|
793
|
+
console.log(` ${check2} Connected to ${cyan2("@" + paired.handle)}`);
|
|
794
|
+
}
|
|
795
|
+
console.log();
|
|
796
|
+
} else if (flagKey && isValidApiKey(flagKey)) {
|
|
655
797
|
const masked = `${flagKey.slice(0, 8)}...${flagKey.slice(-4)}`;
|
|
656
798
|
console.log(` ${check2} Using API key: ${dim2(masked)}`);
|
|
657
799
|
console.log();
|
|
@@ -720,11 +862,13 @@ var main = async () => {
|
|
|
720
862
|
console.log(` ${yellow2("!")} Failed: ${failed.join(", ")} ${dim2("(others completed successfully)")}`);
|
|
721
863
|
}
|
|
722
864
|
}
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
865
|
+
if (connectMode !== "app") {
|
|
866
|
+
const skipPhone = process.argv.includes("--skip-phone");
|
|
867
|
+
if (skipPhone || !process.stdout.isTTY) {
|
|
868
|
+
await printConnectInstructions(trimmedKey);
|
|
869
|
+
} else {
|
|
870
|
+
await connectDevice(trimmedKey);
|
|
871
|
+
}
|
|
728
872
|
}
|
|
729
873
|
console.log();
|
|
730
874
|
console.log(` ${green2(bold2("Setup complete."))}`);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
handleStop
|
|
4
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-U6OXZPYC.js";
|
|
5
5
|
import "../chunk-HRQEECB6.js";
|
|
6
6
|
import "../chunk-22CV7V7A.js";
|
|
7
7
|
import "../chunk-DWED7BS3.js";
|
|
@@ -15,7 +15,8 @@ var main = async () => {
|
|
|
15
15
|
}
|
|
16
16
|
try {
|
|
17
17
|
const input = rawInput.trim() ? JSON.parse(rawInput) : {};
|
|
18
|
-
await handleStop(input);
|
|
18
|
+
const output = await handleStop(input);
|
|
19
|
+
if (output) process.stdout.write(JSON.stringify(output));
|
|
19
20
|
} catch {
|
|
20
21
|
}
|
|
21
22
|
};
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_SESSION,
|
|
3
|
+
cancelQuestion,
|
|
4
|
+
deriveReceiptMeta,
|
|
5
|
+
describeToolCall,
|
|
6
|
+
fetchModeState,
|
|
7
|
+
getMachineId,
|
|
8
|
+
isDefaultSession,
|
|
9
|
+
isPolicyConfig,
|
|
10
|
+
isToolResultError,
|
|
11
|
+
listPendingQuestions,
|
|
12
|
+
removePendingQuestion,
|
|
13
|
+
removePendingSession,
|
|
14
|
+
resolvePolicy
|
|
15
|
+
} from "./chunk-HRQEECB6.js";
|
|
16
|
+
import {
|
|
17
|
+
withRetry
|
|
18
|
+
} from "./chunk-DWED7BS3.js";
|
|
19
|
+
import {
|
|
20
|
+
getApiKey,
|
|
21
|
+
getBaseUrl
|
|
22
|
+
} from "./chunk-NKXSILEW.js";
|
|
23
|
+
|
|
24
|
+
// src/codex-adapter.ts
|
|
25
|
+
var CODEX_AGENT = { type: "codex", label: "Codex" };
|
|
26
|
+
var codexAllow = () => ({ kind: "allow" });
|
|
27
|
+
var codexDeny = (reason) => ({ kind: "deny", reason });
|
|
28
|
+
var codexPass = () => ({ kind: "pass" });
|
|
29
|
+
var toCodexWire = (event, decision) => {
|
|
30
|
+
if (event === "PermissionRequest") {
|
|
31
|
+
if (decision.kind === "allow") {
|
|
32
|
+
return {
|
|
33
|
+
hookSpecificOutput: {
|
|
34
|
+
hookEventName: "PermissionRequest",
|
|
35
|
+
decision: { behavior: "allow" }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (decision.kind === "deny") {
|
|
40
|
+
return {
|
|
41
|
+
hookSpecificOutput: {
|
|
42
|
+
hookEventName: "PermissionRequest",
|
|
43
|
+
decision: { behavior: "deny", message: decision.reason }
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (event === "PreToolUse" && decision.kind === "deny") {
|
|
50
|
+
return {
|
|
51
|
+
hookSpecificOutput: {
|
|
52
|
+
hookEventName: "PreToolUse",
|
|
53
|
+
permissionDecision: "deny",
|
|
54
|
+
permissionDecisionReason: decision.reason
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
};
|
|
60
|
+
var toPolicyLookup = (toolName, toolInput) => {
|
|
61
|
+
if (toolName !== "apply_patch") return { tool: toolName, input: toolInput };
|
|
62
|
+
const command = toolInput.command;
|
|
63
|
+
return {
|
|
64
|
+
tool: "Edit",
|
|
65
|
+
input: typeof command === "string" ? { file_path: command } : {}
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
var permissionTimeoutDecision = (timeoutAction) => {
|
|
69
|
+
if (timeoutAction === "approve") return codexAllow();
|
|
70
|
+
if (timeoutAction === "deny") return codexDeny("No response within timeout");
|
|
71
|
+
return codexPass();
|
|
72
|
+
};
|
|
73
|
+
var preToolUseTimeoutDecision = (timeoutAction, denyReason = "No response within timeout") => timeoutAction === "deny" ? codexDeny(denyReason) : codexPass();
|
|
74
|
+
|
|
75
|
+
// src/usage.ts
|
|
76
|
+
import { closeSync, mkdirSync, openSync, readFileSync, readSync, statSync, writeFileSync } from "fs";
|
|
77
|
+
import { join } from "path";
|
|
78
|
+
import { tmpdir } from "os";
|
|
79
|
+
var DEFAULT_PRICES = [
|
|
80
|
+
{ match: "opus-4-1", in: 15, out: 75 },
|
|
81
|
+
{ match: "opus-4-0", in: 15, out: 75 },
|
|
82
|
+
{ match: "3-opus", in: 15, out: 75 },
|
|
83
|
+
{ match: "opus", in: 5, out: 25 },
|
|
84
|
+
{ match: "haiku", in: 1, out: 5 },
|
|
85
|
+
{ match: "sonnet", in: 3, out: 15 }
|
|
86
|
+
];
|
|
87
|
+
var FALLBACK_PRICE = { match: "", in: 3, out: 15 };
|
|
88
|
+
var CACHE_WRITE_MULTIPLIER = 1.25;
|
|
89
|
+
var CACHE_READ_MULTIPLIER = 0.1;
|
|
90
|
+
var RECENT_ID_LIMIT = 200;
|
|
91
|
+
var READ_CHUNK_BYTES = 1024 * 1024;
|
|
92
|
+
var isModelPrice = (value) => {
|
|
93
|
+
if (!value || typeof value !== "object") return false;
|
|
94
|
+
const candidate = value;
|
|
95
|
+
return typeof candidate.match === "string" && typeof candidate.in === "number" && typeof candidate.out === "number";
|
|
96
|
+
};
|
|
97
|
+
var priceTable = () => {
|
|
98
|
+
const raw = process.env.PUSHARY_MODEL_PRICING?.trim();
|
|
99
|
+
if (!raw) return DEFAULT_PRICES;
|
|
100
|
+
try {
|
|
101
|
+
const parsed = JSON.parse(raw);
|
|
102
|
+
if (Array.isArray(parsed) && parsed.length > 0 && parsed.every(isModelPrice)) return parsed;
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
return DEFAULT_PRICES;
|
|
106
|
+
};
|
|
107
|
+
var estimateCostUsd = (usage, model) => {
|
|
108
|
+
const price = priceTable().find((p) => model.includes(p.match)) ?? FALLBACK_PRICE;
|
|
109
|
+
const perTokenIn = price.in / 1e6;
|
|
110
|
+
const perTokenOut = price.out / 1e6;
|
|
111
|
+
return usage.inputTokens * perTokenIn + usage.outputTokens * perTokenOut + usage.cacheCreationTokens * perTokenIn * CACHE_WRITE_MULTIPLIER + usage.cacheReadTokens * perTokenIn * CACHE_READ_MULTIPLIER;
|
|
112
|
+
};
|
|
113
|
+
var stateDir = () => process.env.PUSHARY_USAGE_DIR?.trim() || join(tmpdir(), "pushary-usage");
|
|
114
|
+
var stateFile = (sessionId) => join(stateDir(), sessionId.replace(/[^a-zA-Z0-9_-]/g, "_"));
|
|
115
|
+
var emptyState = () => ({ offset: 0, tokensIn: 0, tokensOut: 0, costUsd: 0, recentIds: [] });
|
|
116
|
+
var readState = (path) => {
|
|
117
|
+
try {
|
|
118
|
+
const parsed = JSON.parse(readFileSync(path, "utf-8"));
|
|
119
|
+
if (typeof parsed.offset === "number" && parsed.offset >= 0 && typeof parsed.tokensIn === "number" && typeof parsed.tokensOut === "number" && typeof parsed.costUsd === "number" && Array.isArray(parsed.recentIds)) {
|
|
120
|
+
return {
|
|
121
|
+
offset: parsed.offset,
|
|
122
|
+
tokensIn: parsed.tokensIn,
|
|
123
|
+
tokensOut: parsed.tokensOut,
|
|
124
|
+
costUsd: parsed.costUsd,
|
|
125
|
+
recentIds: parsed.recentIds.filter((id) => typeof id === "string")
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
130
|
+
return emptyState();
|
|
131
|
+
};
|
|
132
|
+
var readRange = (path, start, end) => {
|
|
133
|
+
const fd = openSync(path, "r");
|
|
134
|
+
try {
|
|
135
|
+
const chunks = [];
|
|
136
|
+
let position = start;
|
|
137
|
+
while (position < end) {
|
|
138
|
+
const length = Math.min(READ_CHUNK_BYTES, end - position);
|
|
139
|
+
const buffer = Buffer.alloc(length);
|
|
140
|
+
const bytesRead = readSync(fd, buffer, 0, length, position);
|
|
141
|
+
if (bytesRead <= 0) break;
|
|
142
|
+
chunks.push(buffer.subarray(0, bytesRead));
|
|
143
|
+
position += bytesRead;
|
|
144
|
+
}
|
|
145
|
+
return Buffer.concat(chunks);
|
|
146
|
+
} finally {
|
|
147
|
+
closeSync(fd);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
var toCount = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
151
|
+
var applyLine = (state, line) => {
|
|
152
|
+
let parsed;
|
|
153
|
+
try {
|
|
154
|
+
parsed = JSON.parse(line);
|
|
155
|
+
} catch {
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
if (parsed.type !== "assistant") return 0;
|
|
159
|
+
const usage = parsed.message?.usage;
|
|
160
|
+
if (!usage || typeof usage !== "object") return 0;
|
|
161
|
+
const model = typeof parsed.message?.model === "string" ? parsed.message.model : "";
|
|
162
|
+
if (model.includes("<synthetic>")) return 0;
|
|
163
|
+
const id = typeof parsed.message?.id === "string" ? parsed.message.id : null;
|
|
164
|
+
if (id) {
|
|
165
|
+
if (state.recentIds.includes(id)) return 0;
|
|
166
|
+
state.recentIds.push(id);
|
|
167
|
+
if (state.recentIds.length > RECENT_ID_LIMIT) state.recentIds.splice(0, state.recentIds.length - RECENT_ID_LIMIT);
|
|
168
|
+
}
|
|
169
|
+
const messageUsage = {
|
|
170
|
+
inputTokens: toCount(usage.input_tokens),
|
|
171
|
+
outputTokens: toCount(usage.output_tokens),
|
|
172
|
+
cacheCreationTokens: toCount(usage.cache_creation_input_tokens),
|
|
173
|
+
cacheReadTokens: toCount(usage.cache_read_input_tokens)
|
|
174
|
+
};
|
|
175
|
+
const cost = estimateCostUsd(messageUsage, model);
|
|
176
|
+
state.tokensIn += messageUsage.inputTokens + messageUsage.cacheCreationTokens + messageUsage.cacheReadTokens;
|
|
177
|
+
state.tokensOut += messageUsage.outputTokens;
|
|
178
|
+
state.costUsd += cost;
|
|
179
|
+
return cost;
|
|
180
|
+
};
|
|
181
|
+
var readNewUsage = (transcriptPath, sessionId) => {
|
|
182
|
+
try {
|
|
183
|
+
const size = statSync(transcriptPath).size;
|
|
184
|
+
const path = stateFile(sessionId);
|
|
185
|
+
let state = readState(path);
|
|
186
|
+
if (size < state.offset) state = { ...emptyState(), recentIds: state.recentIds };
|
|
187
|
+
let deltaUsd = 0;
|
|
188
|
+
if (size > state.offset) {
|
|
189
|
+
const buffer = readRange(transcriptPath, state.offset, size);
|
|
190
|
+
const lastNewline = buffer.lastIndexOf(10);
|
|
191
|
+
if (lastNewline >= 0) {
|
|
192
|
+
const complete = buffer.subarray(0, lastNewline + 1);
|
|
193
|
+
for (const line of complete.toString("utf-8").split("\n")) {
|
|
194
|
+
if (line.trim()) deltaUsd += applyLine(state, line);
|
|
195
|
+
}
|
|
196
|
+
state.offset += lastNewline + 1;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
200
|
+
writeFileSync(path, JSON.stringify(state), "utf-8");
|
|
201
|
+
if (state.tokensIn === 0 && state.tokensOut === 0) return null;
|
|
202
|
+
return {
|
|
203
|
+
tokensIn: state.tokensIn,
|
|
204
|
+
tokensOut: state.tokensOut,
|
|
205
|
+
costUsd: state.costUsd,
|
|
206
|
+
deltaUsd
|
|
207
|
+
};
|
|
208
|
+
} catch {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
// src/events.ts
|
|
214
|
+
import { basename, join as join2 } from "path";
|
|
215
|
+
import { createHash } from "crypto";
|
|
216
|
+
import { existsSync, readFileSync as readFileSync2 } from "fs";
|
|
217
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
218
|
+
var cleanupPendingQuestions = async (sessionId) => {
|
|
219
|
+
try {
|
|
220
|
+
const files = listPendingQuestions(sessionId);
|
|
221
|
+
const apiKey = getApiKey();
|
|
222
|
+
for (const correlationId of files) {
|
|
223
|
+
try {
|
|
224
|
+
await cancelQuestion(apiKey, correlationId);
|
|
225
|
+
} catch {
|
|
226
|
+
}
|
|
227
|
+
removePendingQuestion(sessionId, correlationId);
|
|
228
|
+
}
|
|
229
|
+
if (!isDefaultSession(sessionId)) removePendingSession(sessionId);
|
|
230
|
+
} catch {
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
var CLAUDE_CODE_AGENT = { type: "claude_code", label: "Claude Code" };
|
|
234
|
+
var POLICY_CACHE_TTL_MS = 5 * 60 * 1e3;
|
|
235
|
+
var readFreshCachedPolicy = (apiKey) => {
|
|
236
|
+
const hash = createHash("sha256").update(apiKey).digest("hex").slice(0, 12);
|
|
237
|
+
const path = join2(tmpdir2(), `pushary-policy-${hash}.json`);
|
|
238
|
+
if (!existsSync(path)) return null;
|
|
239
|
+
const cached = JSON.parse(readFileSync2(path, "utf-8"));
|
|
240
|
+
if (!isPolicyConfig(cached)) return null;
|
|
241
|
+
if (!cached._cachedAt || Date.now() - cached._cachedAt >= POLICY_CACHE_TTL_MS) return null;
|
|
242
|
+
return cached;
|
|
243
|
+
};
|
|
244
|
+
var deriveDecisionSource = (toolName, toolInput, liveMode) => {
|
|
245
|
+
try {
|
|
246
|
+
if (liveMode.kill) return "terminal";
|
|
247
|
+
const policy = readFreshCachedPolicy(getApiKey());
|
|
248
|
+
if (!policy) return void 0;
|
|
249
|
+
const resolved = resolvePolicy(policy, toolName, liveMode.mode, toolInput);
|
|
250
|
+
if (resolved.timeoutSeconds === 0 && resolved.timeoutAction === "approve") return "policy_auto";
|
|
251
|
+
if (resolved.mode === "push_only" || resolved.mode === "push_first") return "human";
|
|
252
|
+
return "terminal";
|
|
253
|
+
} catch {
|
|
254
|
+
return void 0;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
var deriveUsage = (transcriptPath, sessionId) => {
|
|
258
|
+
if (!transcriptPath || process.env.PUSHARY_COST_TRACKING === "off") return void 0;
|
|
259
|
+
try {
|
|
260
|
+
return readNewUsage(transcriptPath, sessionId || DEFAULT_SESSION) ?? void 0;
|
|
261
|
+
} catch {
|
|
262
|
+
return void 0;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var reportEvent = async (event, options = {}) => {
|
|
266
|
+
const apiKey = getApiKey();
|
|
267
|
+
const baseUrl = getBaseUrl();
|
|
268
|
+
return withRetry(async () => {
|
|
269
|
+
const res = await fetch(`${baseUrl}/api/agent/event`, {
|
|
270
|
+
method: "POST",
|
|
271
|
+
headers: {
|
|
272
|
+
"Content-Type": "application/json",
|
|
273
|
+
"Authorization": `Bearer ${apiKey}`
|
|
274
|
+
},
|
|
275
|
+
body: JSON.stringify({
|
|
276
|
+
...event,
|
|
277
|
+
machineId: event.machineId ?? getMachineId()
|
|
278
|
+
}),
|
|
279
|
+
signal: AbortSignal.timeout(options.timeoutMs ?? 1e4)
|
|
280
|
+
});
|
|
281
|
+
try {
|
|
282
|
+
return await res.json();
|
|
283
|
+
} catch {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
}, { maxAttempts: options.maxAttempts ?? 2, baseDelayMs: 300 });
|
|
287
|
+
};
|
|
288
|
+
var handlePostToolUse = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
289
|
+
try {
|
|
290
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
291
|
+
const action = describeToolCall(input.tool_name, input.tool_input, "event");
|
|
292
|
+
const lookup = toPolicyLookup(input.tool_name, input.tool_input);
|
|
293
|
+
const isError = isToolResultError(input.tool_result);
|
|
294
|
+
const receiptsEnabled = process.env.PUSHARY_RECEIPTS !== "off";
|
|
295
|
+
const liveMode = await fetchModeState(getApiKey(), input.session_id);
|
|
296
|
+
await Promise.allSettled([
|
|
297
|
+
cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
|
|
298
|
+
reportEvent({
|
|
299
|
+
event: isError ? "tool_error" : "tool_complete",
|
|
300
|
+
agentType: agent.type,
|
|
301
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
302
|
+
action,
|
|
303
|
+
sessionId: input.session_id,
|
|
304
|
+
error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0,
|
|
305
|
+
decisionSource: deriveDecisionSource(lookup.tool, lookup.input, liveMode),
|
|
306
|
+
meta: receiptsEnabled ? deriveReceiptMeta(lookup.tool, lookup.input, input.tool_result, input.cwd ?? process.cwd()) : void 0,
|
|
307
|
+
usage: deriveUsage(input.transcript_path, input.session_id)
|
|
308
|
+
})
|
|
309
|
+
]);
|
|
310
|
+
} catch {
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
var TASK_TITLE_MAX_LENGTH = 120;
|
|
314
|
+
var handleUserPrompt = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
315
|
+
try {
|
|
316
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
317
|
+
const titlesEnabled = process.env.PUSHARY_TASK_TITLES !== "off";
|
|
318
|
+
const taskTitle = titlesEnabled ? input.prompt?.replace(/\s+/g, " ").trim().slice(0, TASK_TITLE_MAX_LENGTH) || void 0 : void 0;
|
|
319
|
+
await reportEvent({
|
|
320
|
+
event: "user_prompt",
|
|
321
|
+
agentType: agent.type,
|
|
322
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
323
|
+
sessionId: input.session_id,
|
|
324
|
+
taskTitle
|
|
325
|
+
}, { maxAttempts: 1, timeoutMs: 800 });
|
|
326
|
+
} catch {
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
var handleStop = async (input, agent = CLAUDE_CODE_AGENT) => {
|
|
330
|
+
try {
|
|
331
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
332
|
+
const [, reported] = await Promise.allSettled([
|
|
333
|
+
cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
|
|
334
|
+
reportEvent({
|
|
335
|
+
event: "session_end",
|
|
336
|
+
agentType: agent.type,
|
|
337
|
+
agentName: `${agent.label} - ${projectName}`,
|
|
338
|
+
action: "Session ended",
|
|
339
|
+
sessionId: input.session_id,
|
|
340
|
+
usage: deriveUsage(input.transcript_path, input.session_id)
|
|
341
|
+
})
|
|
342
|
+
]);
|
|
343
|
+
const pendingCommand = reported.status === "fulfilled" ? reported.value?.pendingCommand : void 0;
|
|
344
|
+
if (typeof pendingCommand === "string" && pendingCommand.trim().length > 0) {
|
|
345
|
+
return {
|
|
346
|
+
decision: "block",
|
|
347
|
+
reason: `The user sent a new instruction from their phone via Pushary: ${pendingCommand.trim()}`
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
return void 0;
|
|
351
|
+
} catch {
|
|
352
|
+
return void 0;
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
var handleNotification = async (input) => {
|
|
356
|
+
try {
|
|
357
|
+
const projectName = basename(input.cwd ?? process.cwd());
|
|
358
|
+
await reportEvent({
|
|
359
|
+
event: input.type === "error" ? "error" : "notification",
|
|
360
|
+
agentType: "claude_code",
|
|
361
|
+
agentName: `Claude Code - ${projectName}`,
|
|
362
|
+
action: input.title ?? input.message ?? "Notification",
|
|
363
|
+
sessionId: input.session_id,
|
|
364
|
+
error: input.type === "error" ? input.message : void 0
|
|
365
|
+
});
|
|
366
|
+
} catch {
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
export {
|
|
371
|
+
CODEX_AGENT,
|
|
372
|
+
codexAllow,
|
|
373
|
+
codexDeny,
|
|
374
|
+
codexPass,
|
|
375
|
+
toCodexWire,
|
|
376
|
+
toPolicyLookup,
|
|
377
|
+
permissionTimeoutDecision,
|
|
378
|
+
preToolUseTimeoutDecision,
|
|
379
|
+
reportEvent,
|
|
380
|
+
handlePostToolUse,
|
|
381
|
+
handleUserPrompt,
|
|
382
|
+
handleStop,
|
|
383
|
+
handleNotification
|
|
384
|
+
};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -53,7 +53,11 @@ interface ReportEventOptions {
|
|
|
53
53
|
maxAttempts?: number;
|
|
54
54
|
timeoutMs?: number;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
interface EventResponse {
|
|
57
|
+
readonly ok?: boolean;
|
|
58
|
+
readonly pendingCommand?: string;
|
|
59
|
+
}
|
|
60
|
+
declare const reportEvent: (event: AgentEvent, options?: ReportEventOptions) => Promise<EventResponse | null>;
|
|
57
61
|
declare const handlePostToolUse: (input: {
|
|
58
62
|
tool_name: string;
|
|
59
63
|
tool_input: Record<string, unknown>;
|
|
@@ -62,12 +66,16 @@ declare const handlePostToolUse: (input: {
|
|
|
62
66
|
session_id?: string;
|
|
63
67
|
transcript_path?: string;
|
|
64
68
|
}, agent?: AgentIdentity) => Promise<void>;
|
|
69
|
+
interface StopHookOutput {
|
|
70
|
+
readonly decision: 'block';
|
|
71
|
+
readonly reason: string;
|
|
72
|
+
}
|
|
65
73
|
declare const handleStop: (input: {
|
|
66
74
|
cwd?: string;
|
|
67
75
|
session_id?: string;
|
|
68
76
|
stop_hook_active?: boolean;
|
|
69
77
|
transcript_path?: string;
|
|
70
|
-
}, agent?: AgentIdentity) => Promise<
|
|
78
|
+
}, agent?: AgentIdentity) => Promise<StopHookOutput | undefined>;
|
|
71
79
|
declare const handleNotification: (input: {
|
|
72
80
|
message?: string;
|
|
73
81
|
title?: string;
|
package/dist/src/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pushary/agent-hooks",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.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",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
},
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"type": "module",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20"
|
|
16
|
+
},
|
|
14
17
|
"main": "./dist/src/index.js",
|
|
15
18
|
"types": "./dist/src/index.d.ts",
|
|
16
19
|
"bin": {
|
|
@@ -34,7 +37,7 @@
|
|
|
34
37
|
"scripts": {
|
|
35
38
|
"build": "node scripts/bundle-plugin.mjs && tsup",
|
|
36
39
|
"dev": "tsup --watch",
|
|
37
|
-
"test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.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 && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts"
|
|
40
|
+
"test": "bun test src/api.test.ts && bun test src/claude-config.test.ts && bun test src/config.test.ts && bun test src/mcp-http.test.ts && bun test src/retry.test.ts && bun test src/usage.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 && bun test src/codex-adapter.test.ts && bun test src/codex-config.test.ts && bun test src/pairing.test.ts"
|
|
38
41
|
},
|
|
39
42
|
"dependencies": {
|
|
40
43
|
"@inquirer/prompts": "^8.4.2",
|
|
@@ -42,7 +45,7 @@
|
|
|
42
45
|
"smol-toml": "^1.6.1"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
|
-
"@pushary/contracts": "
|
|
48
|
+
"@pushary/contracts": "0.1.0",
|
|
46
49
|
"tsup": "^8.0.0",
|
|
47
50
|
"typescript": "^5.0.0"
|
|
48
51
|
}
|