@primitive.ai/prim 0.1.0-alpha.12 → 0.1.0-alpha.120
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/README.md +540 -38
- package/SKILL.md +315 -75
- package/assets/primitive-gameplan/SKILL.md +134 -0
- package/assets/primitive-gameplan/claude-hooks.json +28 -0
- package/assets/primitive-gameplan/scripts/activate.mjs +37 -0
- package/dist/chunk-2AKDX4RU.js +209 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5BQ2NEMP.js +22695 -0
- package/dist/chunk-A6M2M5A2.js +36 -0
- package/dist/chunk-CCGFRNAA.js +1871 -0
- package/dist/chunk-D235RJNO.js +164 -0
- package/dist/chunk-EYBE724K.js +135 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GYQOZGGF.js +168 -0
- package/dist/chunk-HXICZG5J.js +503 -0
- package/dist/chunk-JSBRDJBE.js +30 -0
- package/dist/chunk-KZB4ABIB.js +13 -0
- package/dist/chunk-L52OD62C.js +1444 -0
- package/dist/chunk-LBCPRHNG.js +175 -0
- package/dist/chunk-LTTF4MBE.js +439 -0
- package/dist/chunk-MWNRXAEB.js +1572 -0
- package/dist/chunk-NQFRQ56V.js +262 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-P2O7NLVN.js +1341 -0
- package/dist/chunk-R2NUH4B7.js +96 -0
- package/dist/chunk-SLDZJD6Q.js +157 -0
- package/dist/chunk-TC4FTRYO.js +253 -0
- package/dist/chunk-U67RBNIZ.js +844 -0
- package/dist/chunk-VOC4KOOP.js +258 -0
- package/dist/chunk-YTF6OCF3.js +459 -0
- package/dist/daemon/server.js +26444 -0
- package/dist/hook-runtime/daemon/server.js +26445 -0
- package/dist/hook-runtime/hooks/post-commit.js +23836 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +23735 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +36151 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1737 -0
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +35067 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +96 -0
- package/dist/hook-runtime/hooks/prim-hook.js +36211 -0
- package/dist/hook-runtime/hooks/session-end.js +1188 -0
- package/dist/hook-runtime/hooks/session-start.js +33950 -0
- package/dist/hook-runtime/index.js +71245 -0
- package/dist/hook-runtime/statusline-main.js +892 -0
- package/dist/hooks/post-commit.js +197 -0
- package/dist/hooks/post-rewrite.js +206 -0
- package/dist/hooks/post-tool-use.js +295 -0
- package/dist/hooks/pre-commit.js +16 -108
- package/dist/hooks/pre-tool-use-worker.js +238 -0
- package/dist/hooks/pre-tool-use.js +87 -0
- package/dist/hooks/prim-hook.js +245 -0
- package/dist/hooks/session-end.js +67 -0
- package/dist/hooks/session-start.js +312 -0
- package/dist/index.js +10998 -572
- package/dist/statusline-main.js +891 -0
- package/package.json +49 -16
- package/dist/chunk-3APLWTLB.js +0 -165
package/dist/hooks/pre-commit.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
checkAffectedDecisions,
|
|
4
|
+
formatDecisionsWarning
|
|
5
|
+
} from "../chunk-D235RJNO.js";
|
|
6
|
+
import "../chunk-SLDZJD6Q.js";
|
|
7
|
+
import "../chunk-U67RBNIZ.js";
|
|
8
|
+
import "../chunk-P2O7NLVN.js";
|
|
9
|
+
import "../chunk-JSBRDJBE.js";
|
|
5
10
|
|
|
6
11
|
// src/hooks/pre-commit.ts
|
|
7
12
|
import { execSync } from "child_process";
|
|
@@ -11,110 +16,19 @@ function getStagedFiles() {
|
|
|
11
16
|
});
|
|
12
17
|
return output.trim().split("\n").filter((f) => f.length > 0);
|
|
13
18
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
encoding: "utf-8"
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
function matchPattern(filePath, pattern) {
|
|
20
|
-
const regexStr = pattern.replaceAll("**", "\xA7GLOBSTAR\xA7").replaceAll("*", "[^/]*").replaceAll("\xA7GLOBSTAR\xA7", ".*");
|
|
21
|
-
const regex = new RegExp(`^${regexStr}$`);
|
|
22
|
-
return regex.test(filePath);
|
|
23
|
-
}
|
|
24
|
-
function findAffectedContexts(stagedFiles, specs) {
|
|
25
|
-
const affected = /* @__PURE__ */ new Map();
|
|
26
|
-
for (const file of stagedFiles) {
|
|
27
|
-
for (const spec of specs) {
|
|
28
|
-
for (const pattern of spec.filePatterns) {
|
|
29
|
-
if (matchPattern(file, pattern)) {
|
|
30
|
-
const existing = affected.get(spec._id);
|
|
31
|
-
if (existing) {
|
|
32
|
-
existing.matchedFiles.push(file);
|
|
33
|
-
} else {
|
|
34
|
-
affected.set(spec._id, {
|
|
35
|
-
contextId: spec._id,
|
|
36
|
-
matchedFiles: [file]
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return affected;
|
|
45
|
-
}
|
|
46
|
-
var HOOK_TIMEOUT_MS = 1e4;
|
|
47
|
-
var defaultDeps = {
|
|
48
|
-
getClient,
|
|
49
|
-
getStagedFiles,
|
|
50
|
-
getStagedDiff
|
|
51
|
-
};
|
|
52
|
-
async function syncAffectedSpecs(deps = defaultDeps) {
|
|
53
|
-
const stagedFiles = deps.getStagedFiles();
|
|
19
|
+
async function runDecisionsCheck() {
|
|
20
|
+
const stagedFiles = getStagedFiles();
|
|
54
21
|
if (stagedFiles.length === 0) {
|
|
55
|
-
return [];
|
|
22
|
+
return { decisions: [], truncated: false };
|
|
56
23
|
}
|
|
57
|
-
|
|
58
|
-
let mappings = [];
|
|
59
|
-
try {
|
|
60
|
-
mappings = await client.get("/api/cli/specs/mappings", {
|
|
61
|
-
signal: AbortSignal.timeout(HOOK_TIMEOUT_MS)
|
|
62
|
-
});
|
|
63
|
-
} catch {
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
if (mappings.length === 0) {
|
|
67
|
-
return [];
|
|
68
|
-
}
|
|
69
|
-
const affectedContexts = findAffectedContexts(stagedFiles, mappings);
|
|
70
|
-
if (affectedContexts.size === 0) {
|
|
71
|
-
return [];
|
|
72
|
-
}
|
|
73
|
-
console.log(`[prim] ${String(affectedContexts.size)} spec(s) affected by staged changes:`);
|
|
74
|
-
const synced = [];
|
|
75
|
-
for (const [contextId, affected] of affectedContexts) {
|
|
76
|
-
try {
|
|
77
|
-
const ctx = await client.get(`/api/cli/contexts/${contextId}`, {
|
|
78
|
-
signal: AbortSignal.timeout(HOOK_TIMEOUT_MS)
|
|
79
|
-
});
|
|
80
|
-
if (!ctx._id) {
|
|
81
|
-
console.log(` [skip] ${contextId} \u2014 not found`);
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (!ctx.isSpecDocument) {
|
|
85
|
-
console.log(` [skip] ${contextId} \u2014 not a spec document`);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const diffContent = deps.getStagedDiff(affected.matchedFiles);
|
|
89
|
-
if (!diffContent) {
|
|
90
|
-
console.log(` [skip] ${contextId} \u2014 no diff content`);
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
const response = await client.post(
|
|
94
|
-
`/api/cli/contexts/${contextId}/sync-diff`,
|
|
95
|
-
{ diffContent, affectedFiles: affected.matchedFiles },
|
|
96
|
-
{ signal: AbortSignal.timeout(HOOK_TIMEOUT_MS) }
|
|
97
|
-
);
|
|
98
|
-
const name = ctx.name ?? "(unnamed)";
|
|
99
|
-
if (response.truncated && response.sizeChars && response.limitChars) {
|
|
100
|
-
const sizeKiB = Math.round(response.sizeChars / 1024);
|
|
101
|
-
const limitKiB = Math.round(response.limitChars / 1024);
|
|
102
|
-
console.log(
|
|
103
|
-
` [synced] ${contextId} \u2014 ${name} (truncated: ${String(sizeKiB)} KiB \u2192 ${String(limitKiB)} KiB analyzed)`
|
|
104
|
-
);
|
|
105
|
-
} else {
|
|
106
|
-
console.log(` [synced] ${contextId} \u2014 ${name}`);
|
|
107
|
-
}
|
|
108
|
-
synced.push(contextId);
|
|
109
|
-
} catch (error) {
|
|
110
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
111
|
-
console.error(` [error] ${contextId} \u2014 ${message}`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return synced;
|
|
24
|
+
return checkAffectedDecisions(stagedFiles);
|
|
115
25
|
}
|
|
116
26
|
async function main() {
|
|
117
|
-
await
|
|
27
|
+
const decisionsResult = await runDecisionsCheck();
|
|
28
|
+
const warning = formatDecisionsWarning(decisionsResult);
|
|
29
|
+
if (warning) {
|
|
30
|
+
console.error(warning);
|
|
31
|
+
}
|
|
118
32
|
process.exit(0);
|
|
119
33
|
}
|
|
120
34
|
if (!process.env.VITEST) {
|
|
@@ -123,9 +37,3 @@ if (!process.env.VITEST) {
|
|
|
123
37
|
process.exit(0);
|
|
124
38
|
});
|
|
125
39
|
}
|
|
126
|
-
export {
|
|
127
|
-
HOOK_TIMEOUT_MS,
|
|
128
|
-
findAffectedContexts,
|
|
129
|
-
matchPattern,
|
|
130
|
-
syncAffectedSpecs
|
|
131
|
-
};
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
PREFLIGHT_PROTOCOL_VERSION,
|
|
4
|
+
boundedClientVersion,
|
|
5
|
+
editPreview,
|
|
6
|
+
hasConfirmedConflict,
|
|
7
|
+
proposalFor,
|
|
8
|
+
proposalIsTruncated,
|
|
9
|
+
resolvePreflightTargets,
|
|
10
|
+
resultForPreflight,
|
|
11
|
+
toolInputDigest,
|
|
12
|
+
waitForPreflight
|
|
13
|
+
} from "../chunk-YTF6OCF3.js";
|
|
14
|
+
import {
|
|
15
|
+
observeDemoInvocation
|
|
16
|
+
} from "../chunk-R2NUH4B7.js";
|
|
17
|
+
import {
|
|
18
|
+
normalizeEnvelope,
|
|
19
|
+
readHookStdin,
|
|
20
|
+
shouldSuppressImportedCursorHandler
|
|
21
|
+
} from "../chunk-TC4FTRYO.js";
|
|
22
|
+
import {
|
|
23
|
+
buildCodexOutput,
|
|
24
|
+
buildCursorOutput,
|
|
25
|
+
buildHermesOutput,
|
|
26
|
+
buildHookOutput,
|
|
27
|
+
demoteForMode,
|
|
28
|
+
failOpenCodex,
|
|
29
|
+
failOpenCursor,
|
|
30
|
+
failOpenHermes,
|
|
31
|
+
failOpenOutput,
|
|
32
|
+
readHookMode
|
|
33
|
+
} from "../chunk-L52OD62C.js";
|
|
34
|
+
import "../chunk-MWNRXAEB.js";
|
|
35
|
+
import "../chunk-NQFRQ56V.js";
|
|
36
|
+
import {
|
|
37
|
+
cachedCollectScopeAdmits
|
|
38
|
+
} from "../chunk-5BQ2NEMP.js";
|
|
39
|
+
import "../chunk-U67RBNIZ.js";
|
|
40
|
+
import {
|
|
41
|
+
canonicalGitRoot,
|
|
42
|
+
currentBranch,
|
|
43
|
+
githubRepositoryFullName,
|
|
44
|
+
packageVersion,
|
|
45
|
+
repoActiveFlag,
|
|
46
|
+
repoSyncId
|
|
47
|
+
} from "../chunk-P2O7NLVN.js";
|
|
48
|
+
import {
|
|
49
|
+
parseAgent
|
|
50
|
+
} from "../chunk-KZB4ABIB.js";
|
|
51
|
+
import "../chunk-JSBRDJBE.js";
|
|
52
|
+
|
|
53
|
+
// src/hooks/pre-tool-use-worker.ts
|
|
54
|
+
import { realpathSync } from "fs";
|
|
55
|
+
import { relative } from "path";
|
|
56
|
+
var STDIN_TIMEOUT_MS = 1e3;
|
|
57
|
+
var deadline = performance.now() + Math.max(0, Number(process.argv.at(-1)) - Date.now());
|
|
58
|
+
var agent = parseAgent(process.argv);
|
|
59
|
+
async function emit(output, confirmed = false) {
|
|
60
|
+
return await new Promise((resolve) => {
|
|
61
|
+
try {
|
|
62
|
+
process.stdout.write(
|
|
63
|
+
`${JSON.stringify({ output, confirmed })}
|
|
64
|
+
`,
|
|
65
|
+
(error) => resolve(!error)
|
|
66
|
+
);
|
|
67
|
+
} catch {
|
|
68
|
+
resolve(false);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function failOpen() {
|
|
73
|
+
if (agent === "hermes") return failOpenHermes();
|
|
74
|
+
if (agent === "cursor") return failOpenCursor();
|
|
75
|
+
return agent === "codex" ? failOpenCodex() : failOpenOutput();
|
|
76
|
+
}
|
|
77
|
+
function invocationId(envelope) {
|
|
78
|
+
const value = agent === "hermes" ? envelope.extra?.tool_call_id : envelope.tool_use_id;
|
|
79
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
80
|
+
}
|
|
81
|
+
function hermesAdvisory(result, aggregate) {
|
|
82
|
+
const parts = [];
|
|
83
|
+
if (aggregate === "warn") parts.push(result.reason);
|
|
84
|
+
if (result.verdict === "unavailable") {
|
|
85
|
+
parts.push(result.unavailable ?? result.reason);
|
|
86
|
+
}
|
|
87
|
+
if (result.additionalContext && result.additionalContext !== result.reason) {
|
|
88
|
+
parts.push(result.additionalContext);
|
|
89
|
+
}
|
|
90
|
+
const message = [...new Set(parts.filter(Boolean))].join("\n");
|
|
91
|
+
return message.startsWith("[primitive]") ? message : message ? `[primitive] ${message}` : "";
|
|
92
|
+
}
|
|
93
|
+
async function main() {
|
|
94
|
+
const mode = readHookMode(process.env);
|
|
95
|
+
if (mode === "off") {
|
|
96
|
+
await emit(failOpen(), true);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let raw;
|
|
100
|
+
try {
|
|
101
|
+
raw = await readHookStdin(STDIN_TIMEOUT_MS);
|
|
102
|
+
} catch {
|
|
103
|
+
await emit(failOpen());
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
let envelope;
|
|
107
|
+
try {
|
|
108
|
+
const incoming = JSON.parse(raw);
|
|
109
|
+
if (!incoming || typeof incoming !== "object" || Array.isArray(incoming)) {
|
|
110
|
+
throw new Error("The hook envelope must be an object");
|
|
111
|
+
}
|
|
112
|
+
const fields = incoming;
|
|
113
|
+
if (agent !== "cursor" && shouldSuppressImportedCursorHandler(fields, "prim-pre-tool-use")) {
|
|
114
|
+
await emit(failOpenCursor(), true);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
envelope = normalizeEnvelope(fields, agent);
|
|
118
|
+
if (typeof envelope.hook_event_name !== "string" || !envelope.hook_event_name) {
|
|
119
|
+
throw new Error("The hook event identity is unavailable");
|
|
120
|
+
}
|
|
121
|
+
} catch {
|
|
122
|
+
await emit(failOpen());
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
if (observeDemoInvocation(envelope, agent, false).invocation) {
|
|
127
|
+
await emit(failOpen(), true);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
} catch {
|
|
131
|
+
}
|
|
132
|
+
if (envelope.hook_event_name !== "PreToolUse") {
|
|
133
|
+
await emit(failOpen(), true);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (typeof envelope.tool_name !== "string" || !envelope.tool_name) {
|
|
137
|
+
await emit(failOpen());
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const toolName = envelope.tool_name;
|
|
141
|
+
const protectedShell = (agent === "claude_code" || agent === "codex") && toolName === "Bash";
|
|
142
|
+
const cwd = typeof envelope.cwd === "string" && envelope.cwd.length > 0 ? envelope.cwd : process.cwd();
|
|
143
|
+
const targets = resolvePreflightTargets({
|
|
144
|
+
toolName,
|
|
145
|
+
toolInput: envelope.tool_input,
|
|
146
|
+
agent,
|
|
147
|
+
cwd
|
|
148
|
+
});
|
|
149
|
+
if (targets.mutation === "none") {
|
|
150
|
+
await emit(failOpen(), true);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (repoActiveFlag(cwd, protectedShell) !== "true") {
|
|
154
|
+
await emit(failOpen(), true);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (!protectedShell && targets.paths.length === 0 && !targets.definite) {
|
|
158
|
+
await emit(failOpen());
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (agent === "cursor" && targets.coverage === "unverified") {
|
|
162
|
+
await emit(failOpen());
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
const binding = repoSyncId(cwd);
|
|
166
|
+
const branch = currentBranch(cwd);
|
|
167
|
+
const sessionId = envelope.session_id;
|
|
168
|
+
const callId = invocationId(envelope);
|
|
169
|
+
if (!binding || typeof sessionId !== "string" || !sessionId || !callId) {
|
|
170
|
+
await emit(failOpen());
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const collectScopeAdmits = cachedCollectScopeAdmits(cwd, {
|
|
174
|
+
repository: githubRepositoryFullName(cwd) ?? void 0,
|
|
175
|
+
branch,
|
|
176
|
+
agent,
|
|
177
|
+
paths: targets.paths,
|
|
178
|
+
pathsComplete: targets.coverage === "complete"
|
|
179
|
+
});
|
|
180
|
+
const root = protectedShell ? canonicalGitRoot(cwd) : null;
|
|
181
|
+
const proposalInput = protectedShell ? {
|
|
182
|
+
...typeof envelope.tool_input === "string" ? { command: envelope.tool_input } : envelope.tool_input,
|
|
183
|
+
repositoryCwd: root ? relative(root, realpathSync(cwd)).replaceAll("\\", "/") || "." : null,
|
|
184
|
+
knownWritePaths: targets.paths,
|
|
185
|
+
targetCoverage: targets.coverage
|
|
186
|
+
} : envelope.tool_input;
|
|
187
|
+
const request = {
|
|
188
|
+
protocolVersion: PREFLIGHT_PROTOCOL_VERSION,
|
|
189
|
+
operation: "start",
|
|
190
|
+
toolInputDigest: toolInputDigest(toolName, envelope.tool_input),
|
|
191
|
+
agent,
|
|
192
|
+
clientMode: mode,
|
|
193
|
+
clientVersion: boundedClientVersion(packageVersion()),
|
|
194
|
+
sessionId,
|
|
195
|
+
invocationId: callId,
|
|
196
|
+
repoSyncId: binding,
|
|
197
|
+
paths: targets.paths,
|
|
198
|
+
coverage: targets.coverage,
|
|
199
|
+
...protectedShell ? {
|
|
200
|
+
shellCheck: {
|
|
201
|
+
targetMode: targets.coverage === "complete" ? "files" : "repository"
|
|
202
|
+
}
|
|
203
|
+
} : {},
|
|
204
|
+
...branch === void 0 ? {} : { branch },
|
|
205
|
+
proposal: collectScopeAdmits ? proposalFor(proposalInput) : ""
|
|
206
|
+
};
|
|
207
|
+
let result;
|
|
208
|
+
try {
|
|
209
|
+
const response = await waitForPreflight(request, {
|
|
210
|
+
deadline,
|
|
211
|
+
stopOnConflict: true
|
|
212
|
+
});
|
|
213
|
+
if (!hasConfirmedConflict(response)) {
|
|
214
|
+
await emit(failOpen(), true);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
result = resultForPreflight(
|
|
218
|
+
response,
|
|
219
|
+
editPreview(toolName, envelope.tool_input, targets.paths),
|
|
220
|
+
request,
|
|
221
|
+
collectScopeAdmits && (toolName === "Bash" || toolName === "Shell") && proposalIsTruncated(proposalInput)
|
|
222
|
+
);
|
|
223
|
+
} catch {
|
|
224
|
+
await emit(failOpen());
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const aggregate = demoteForMode(result.verdict === "unavailable" ? "deny" : result.verdict, mode);
|
|
228
|
+
if (agent === "hermes" && aggregate !== "deny" && aggregate !== "ask") {
|
|
229
|
+
const advisory = hermesAdvisory(result, aggregate);
|
|
230
|
+
if (advisory) process.stderr.write(`${advisory}
|
|
231
|
+
`);
|
|
232
|
+
}
|
|
233
|
+
const output = agent === "hermes" ? buildHermesOutput(aggregate, [result]) : agent === "cursor" ? buildCursorOutput(aggregate, [result]) : agent === "codex" ? buildCodexOutput(aggregate, [result]) : buildHookOutput(aggregate, [result]);
|
|
234
|
+
await emit(output, true);
|
|
235
|
+
}
|
|
236
|
+
main().catch(async () => {
|
|
237
|
+
await emit(failOpen());
|
|
238
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
parseAgent
|
|
4
|
+
} from "../chunk-KZB4ABIB.js";
|
|
5
|
+
import "../chunk-JSBRDJBE.js";
|
|
6
|
+
|
|
7
|
+
// src/hooks/pre-tool-use.ts
|
|
8
|
+
import { spawn } from "child_process";
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
var BUDGET_MS = 2e3;
|
|
11
|
+
var OUTPUT_RESERVE_MS = 150;
|
|
12
|
+
var MAX_OUTPUT_BYTES = 262144;
|
|
13
|
+
var agent = parseAgent(process.argv);
|
|
14
|
+
var silent = agent === "cursor" ? { permission: "allow" } : agent === "claude_code" ? {
|
|
15
|
+
hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "allow" }
|
|
16
|
+
} : {};
|
|
17
|
+
var remaining = Math.max(0, BUDGET_MS - process.uptime() * 1e3 - OUTPUT_RESERVE_MS);
|
|
18
|
+
var child;
|
|
19
|
+
var finished = false;
|
|
20
|
+
var stdout = "";
|
|
21
|
+
var stderr = "";
|
|
22
|
+
function stopWorker() {
|
|
23
|
+
if (!child?.pid) return;
|
|
24
|
+
try {
|
|
25
|
+
if (process.platform !== "win32") process.kill(-child.pid, "SIGKILL");
|
|
26
|
+
else child.kill("SIGKILL");
|
|
27
|
+
} catch {
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function finish(output = silent, diagnostics = "") {
|
|
31
|
+
if (finished) return;
|
|
32
|
+
finished = true;
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
process.stdin.unpipe();
|
|
35
|
+
process.stdin.pause();
|
|
36
|
+
stopWorker();
|
|
37
|
+
if (diagnostics) process.stderr.write(diagnostics);
|
|
38
|
+
process.stdout.write(`${JSON.stringify(output)}
|
|
39
|
+
`, () => process.exit(0));
|
|
40
|
+
}
|
|
41
|
+
var timer = setTimeout(() => finish(), remaining);
|
|
42
|
+
process.once("SIGTERM", () => finish());
|
|
43
|
+
process.once("SIGINT", () => finish());
|
|
44
|
+
process.once("exit", stopWorker);
|
|
45
|
+
if (remaining <= 0) finish();
|
|
46
|
+
else {
|
|
47
|
+
try {
|
|
48
|
+
child = spawn(
|
|
49
|
+
process.execPath,
|
|
50
|
+
[
|
|
51
|
+
fileURLToPath(new URL("./pre-tool-use-worker.js", import.meta.url)),
|
|
52
|
+
"--agent",
|
|
53
|
+
agent,
|
|
54
|
+
String(Date.now() + remaining - 50)
|
|
55
|
+
],
|
|
56
|
+
{
|
|
57
|
+
detached: process.platform !== "win32",
|
|
58
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
child.on("error", () => finish());
|
|
62
|
+
child.stdin?.on("error", () => finish());
|
|
63
|
+
child.stderr?.setEncoding("utf8").on("data", (chunk) => {
|
|
64
|
+
stderr = (stderr + chunk).slice(0, MAX_OUTPUT_BYTES);
|
|
65
|
+
});
|
|
66
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk) => {
|
|
67
|
+
stdout += chunk;
|
|
68
|
+
if (stdout.length > MAX_OUTPUT_BYTES) return finish();
|
|
69
|
+
if (!stdout.includes("\n")) return;
|
|
70
|
+
try {
|
|
71
|
+
const message = JSON.parse(stdout);
|
|
72
|
+
if (!message || typeof message !== "object" || !("output" in message) || !("confirmed" in message))
|
|
73
|
+
return finish();
|
|
74
|
+
if (!message.output || typeof message.output !== "object" || Array.isArray(message.output))
|
|
75
|
+
return finish();
|
|
76
|
+
if (message.confirmed === true) finish(message.output, stderr);
|
|
77
|
+
else finish();
|
|
78
|
+
} catch {
|
|
79
|
+
finish();
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
child.on("exit", () => finish());
|
|
83
|
+
if (child.stdin) process.stdin.pipe(child.stdin);
|
|
84
|
+
} catch {
|
|
85
|
+
finish();
|
|
86
|
+
}
|
|
87
|
+
}
|