@pushary/agent-hooks 0.14.3 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,370 @@
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
+ await withRetry(async () => {
269
+ 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
+ }, { maxAttempts: options.maxAttempts ?? 2, baseDelayMs: 300 });
282
+ };
283
+ var handlePostToolUse = async (input, agent = CLAUDE_CODE_AGENT) => {
284
+ try {
285
+ const projectName = basename(input.cwd ?? process.cwd());
286
+ const action = describeToolCall(input.tool_name, input.tool_input, "event");
287
+ const lookup = toPolicyLookup(input.tool_name, input.tool_input);
288
+ const isError = isToolResultError(input.tool_result);
289
+ const receiptsEnabled = process.env.PUSHARY_RECEIPTS !== "off";
290
+ const liveMode = await fetchModeState(getApiKey(), input.session_id);
291
+ await Promise.allSettled([
292
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
293
+ reportEvent({
294
+ event: isError ? "tool_error" : "tool_complete",
295
+ agentType: agent.type,
296
+ agentName: `${agent.label} - ${projectName}`,
297
+ action,
298
+ sessionId: input.session_id,
299
+ error: isError ? String(input.tool_result?.error ?? input.tool_result?.stderr ?? "").slice(0, 500) : void 0,
300
+ decisionSource: deriveDecisionSource(lookup.tool, lookup.input, liveMode),
301
+ meta: receiptsEnabled ? deriveReceiptMeta(lookup.tool, lookup.input, input.tool_result, input.cwd ?? process.cwd()) : void 0,
302
+ usage: deriveUsage(input.transcript_path, input.session_id)
303
+ })
304
+ ]);
305
+ } catch {
306
+ }
307
+ };
308
+ var TASK_TITLE_MAX_LENGTH = 120;
309
+ var handleUserPrompt = async (input, agent = CLAUDE_CODE_AGENT) => {
310
+ try {
311
+ const projectName = basename(input.cwd ?? process.cwd());
312
+ const titlesEnabled = process.env.PUSHARY_TASK_TITLES !== "off";
313
+ const taskTitle = titlesEnabled ? input.prompt?.replace(/\s+/g, " ").trim().slice(0, TASK_TITLE_MAX_LENGTH) || void 0 : void 0;
314
+ await reportEvent({
315
+ event: "user_prompt",
316
+ agentType: agent.type,
317
+ agentName: `${agent.label} - ${projectName}`,
318
+ sessionId: input.session_id,
319
+ taskTitle
320
+ }, { maxAttempts: 1, timeoutMs: 800 });
321
+ } catch {
322
+ }
323
+ };
324
+ var handleStop = async (input, agent = CLAUDE_CODE_AGENT) => {
325
+ try {
326
+ const projectName = basename(input.cwd ?? process.cwd());
327
+ await Promise.allSettled([
328
+ cleanupPendingQuestions(input.session_id || DEFAULT_SESSION),
329
+ reportEvent({
330
+ event: "session_end",
331
+ agentType: agent.type,
332
+ agentName: `${agent.label} - ${projectName}`,
333
+ action: "Session ended",
334
+ sessionId: input.session_id,
335
+ usage: deriveUsage(input.transcript_path, input.session_id)
336
+ })
337
+ ]);
338
+ } catch {
339
+ }
340
+ };
341
+ var handleNotification = async (input) => {
342
+ try {
343
+ const projectName = basename(input.cwd ?? process.cwd());
344
+ await reportEvent({
345
+ event: input.type === "error" ? "error" : "notification",
346
+ agentType: "claude_code",
347
+ agentName: `Claude Code - ${projectName}`,
348
+ action: input.title ?? input.message ?? "Notification",
349
+ sessionId: input.session_id,
350
+ error: input.type === "error" ? input.message : void 0
351
+ });
352
+ } catch {
353
+ }
354
+ };
355
+
356
+ export {
357
+ CODEX_AGENT,
358
+ codexAllow,
359
+ codexDeny,
360
+ codexPass,
361
+ toCodexWire,
362
+ toPolicyLookup,
363
+ permissionTimeoutDecision,
364
+ preToolUseTimeoutDecision,
365
+ reportEvent,
366
+ handlePostToolUse,
367
+ handleUserPrompt,
368
+ handleStop,
369
+ handleNotification
370
+ };
@@ -0,0 +1,29 @@
1
+ // src/config.ts
2
+ import { readFileSync } from "fs";
3
+ import { join } from "path";
4
+ import { homedir } from "os";
5
+ var configFilePath = () => process.env.PUSHARY_CONFIG_FILE?.trim() || join(homedir(), ".pushary", "config.json");
6
+ var readKeyFromConfigFile = () => {
7
+ try {
8
+ const parsed = JSON.parse(readFileSync(configFilePath(), "utf-8"));
9
+ const key = typeof parsed.apiKey === "string" ? parsed.apiKey.trim() : "";
10
+ return key || void 0;
11
+ } catch {
12
+ return void 0;
13
+ }
14
+ };
15
+ var getApiKey = () => {
16
+ const envKey = process.env.PUSHARY_API_KEY?.trim();
17
+ if (envKey) return envKey;
18
+ const fileKey = readKeyFromConfigFile();
19
+ if (fileKey) return fileKey;
20
+ throw new Error(
21
+ "PUSHARY_API_KEY is not set and no key was found in ~/.pushary/config.json. Run `npx @pushary/agent-hooks setup`, or get your API key at https://pushary.com/sign-up?from=ai-coding"
22
+ );
23
+ };
24
+ var getBaseUrl = () => process.env.PUSHARY_BASE_URL ?? "https://pushary.com";
25
+
26
+ export {
27
+ getApiKey,
28
+ getBaseUrl
29
+ };
@@ -0,0 +1,142 @@
1
+ // src/codex-config.ts
2
+ import { createHash } from "crypto";
3
+ var CODEX_HOOK_BINARY = "pushary-codex-hook";
4
+ var CODEX_HOOK_EVENTS = [
5
+ { event: "PermissionRequest", matcher: "Bash|apply_patch", timeout: 180, statusMessage: "Waiting for your phone" },
6
+ { event: "PreToolUse", matcher: "Bash|apply_patch", timeout: 180, statusMessage: "Checking Pushary policy" },
7
+ { event: "PostToolUse", matcher: "Bash|apply_patch", timeout: 10 },
8
+ { event: "UserPromptSubmit", timeout: 10 },
9
+ { event: "Stop", timeout: 10 },
10
+ { event: "SessionStart", matcher: "startup|resume", timeout: 10 }
11
+ ];
12
+ var CODEX_EVENT_KEY = {
13
+ PermissionRequest: "permission_request",
14
+ PreToolUse: "pre_tool_use",
15
+ PostToolUse: "post_tool_use",
16
+ UserPromptSubmit: "user_prompt_submit",
17
+ Stop: "stop",
18
+ SessionStart: "session_start"
19
+ };
20
+ var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : void 0;
21
+ var ensureRecord = (target, key) => {
22
+ const existing = asRecord(target[key]);
23
+ if (existing) return existing;
24
+ const created = {};
25
+ target[key] = created;
26
+ return created;
27
+ };
28
+ var isPusharyCodexHook = (entry) => {
29
+ const hooks = asRecord(entry)?.hooks;
30
+ if (!Array.isArray(hooks)) return false;
31
+ return hooks.some((hook) => String(asRecord(hook)?.command ?? "").includes(CODEX_HOOK_BINARY));
32
+ };
33
+ var addCodexHooks = (config, command) => {
34
+ const hooks = ensureRecord(config, "hooks");
35
+ for (const definition of CODEX_HOOK_EVENTS) {
36
+ const existing = Array.isArray(hooks[definition.event]) ? hooks[definition.event] : [];
37
+ const entries = existing.filter((entry) => !isPusharyCodexHook(entry));
38
+ entries.push({
39
+ ...definition.matcher ? { matcher: definition.matcher } : {},
40
+ hooks: [{
41
+ type: "command",
42
+ command,
43
+ timeout: definition.timeout,
44
+ ...definition.statusMessage ? { statusMessage: definition.statusMessage } : {}
45
+ }]
46
+ });
47
+ hooks[definition.event] = entries;
48
+ }
49
+ };
50
+ var removeCodexHooks = (config) => {
51
+ const hooks = asRecord(config.hooks);
52
+ if (!hooks) return false;
53
+ let changed = false;
54
+ for (const definition of CODEX_HOOK_EVENTS) {
55
+ const entries = hooks[definition.event];
56
+ if (!Array.isArray(entries)) continue;
57
+ const filtered = entries.filter((entry) => !isPusharyCodexHook(entry));
58
+ if (filtered.length !== entries.length) {
59
+ if (filtered.length === 0) {
60
+ delete hooks[definition.event];
61
+ } else {
62
+ hooks[definition.event] = filtered;
63
+ }
64
+ changed = true;
65
+ }
66
+ }
67
+ if (Object.keys(hooks).length === 0) delete config.hooks;
68
+ return changed;
69
+ };
70
+ var hasCodexHooks = (config) => {
71
+ const hooks = asRecord(config.hooks);
72
+ if (!hooks) return false;
73
+ return CODEX_HOOK_EVENTS.some((definition) => {
74
+ const entries = hooks[definition.event];
75
+ return Array.isArray(entries) && entries.some(isPusharyCodexHook);
76
+ });
77
+ };
78
+ var missingCodexHookEvents = (config) => {
79
+ const hooks = asRecord(config.hooks);
80
+ return CODEX_HOOK_EVENTS.filter((definition) => {
81
+ const entries = hooks?.[definition.event];
82
+ return !Array.isArray(entries) || !entries.some(isPusharyCodexHook);
83
+ }).map((definition) => definition.event);
84
+ };
85
+ var canonicalJson = (value) => {
86
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
87
+ if (Array.isArray(value)) return "[" + value.map(canonicalJson).join(",") + "]";
88
+ const obj = value;
89
+ return "{" + Object.keys(obj).sort().map((key) => JSON.stringify(key) + ":" + canonicalJson(obj[key])).join(",") + "}";
90
+ };
91
+ var codexHookTrustHash = (definition, command) => {
92
+ const hook = { async: false, command, timeout: definition.timeout, type: "command" };
93
+ if (definition.statusMessage) hook.statusMessage = definition.statusMessage;
94
+ const identity = { event_name: CODEX_EVENT_KEY[definition.event], hooks: [hook] };
95
+ if (definition.matcher) identity.matcher = definition.matcher;
96
+ return "sha256:" + createHash("sha256").update(canonicalJson(identity), "utf8").digest("hex");
97
+ };
98
+ var codexHookStateKey = (hooksJsonPath, event) => `${hooksJsonPath}:${CODEX_EVENT_KEY[event]}:0:0`;
99
+ var addCodexHookTrust = (config, hooksJsonPath, command) => {
100
+ const hooks = ensureRecord(config, "hooks");
101
+ const state = ensureRecord(hooks, "state");
102
+ for (const definition of CODEX_HOOK_EVENTS) {
103
+ const key = codexHookStateKey(hooksJsonPath, definition.event);
104
+ const existing = asRecord(state[key]) ?? {};
105
+ state[key] = { ...existing, trusted_hash: codexHookTrustHash(definition, command) };
106
+ }
107
+ };
108
+
109
+ // src/npm.ts
110
+ import { execSync } from "child_process";
111
+ var cleanNpmEnv = () => {
112
+ const env = {};
113
+ for (const [key, value] of Object.entries(process.env)) {
114
+ if (key.toLowerCase().startsWith("npm_config_workspace")) continue;
115
+ env[key] = value;
116
+ }
117
+ return env;
118
+ };
119
+ var npmErrorMessage = (err) => {
120
+ const e = err;
121
+ const text = [e?.stderr, e?.stdout].map((part) => part ? part.toString() : "").join("\n");
122
+ const line = text.split("\n").map((l) => l.replace(/^npm error\s*/i, "").trim()).find((l) => l && !l.startsWith("A complete log") && !/^code\s/i.test(l));
123
+ return line || e?.message || String(err);
124
+ };
125
+ var execNpm = (args, options = {}) => {
126
+ return execSync(`npm ${args}`, {
127
+ timeout: 12e4,
128
+ stdio: "pipe",
129
+ ...options,
130
+ env: { ...cleanNpmEnv(), ...options.env ?? {} }
131
+ });
132
+ };
133
+
134
+ export {
135
+ addCodexHooks,
136
+ removeCodexHooks,
137
+ hasCodexHooks,
138
+ missingCodexHookEvents,
139
+ addCodexHookTrust,
140
+ npmErrorMessage,
141
+ execNpm
142
+ };