@primitive.ai/prim 0.1.0-alpha.11 → 0.1.0-alpha.110
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 +450 -38
- package/SKILL.md +366 -0
- package/dist/chunk-2IRLFOEL.js +766 -0
- package/dist/chunk-2Q3OGSTV.js +164 -0
- package/dist/chunk-43ZKL27W.js +258 -0
- package/dist/chunk-4GLVJLEQ.js +55 -0
- package/dist/chunk-5BLGBSDM.js +233 -0
- package/dist/chunk-5LRKJTLA.js +155 -0
- package/dist/chunk-7VIMU2HR.js +209 -0
- package/dist/chunk-AOJ254C2.js +511 -0
- package/dist/chunk-CTRAJCPN.js +964 -0
- package/dist/chunk-EZXEWFDG.js +210 -0
- package/dist/chunk-GA2RXPJL.js +168 -0
- package/dist/chunk-HTRFPXMD.js +135 -0
- package/dist/chunk-IQZA65NV.js +1871 -0
- package/dist/chunk-JJI7FU24.js +439 -0
- package/dist/chunk-KGBMU76T.js +175 -0
- package/dist/chunk-KQ4WBK5R.js +262 -0
- package/dist/chunk-NH3VC7YO.js +264 -0
- package/dist/chunk-OYHYCK4K.js +13 -0
- package/dist/chunk-PO5QAYD7.js +1499 -0
- package/dist/chunk-UDBBJWQH.js +20383 -0
- package/dist/chunk-W4ZEPZLK.js +36 -0
- package/dist/chunk-XG7S3LIL.js +1372 -0
- package/dist/daemon/server.js +24051 -0
- package/dist/hook-runtime/daemon/server.js +24051 -0
- package/dist/hook-runtime/hooks/post-commit.js +21524 -0
- package/dist/hook-runtime/hooks/post-rewrite.js +21423 -0
- package/dist/hook-runtime/hooks/post-tool-use.js +28917 -0
- package/dist/hook-runtime/hooks/pre-commit.js +1657 -0
- package/dist/hook-runtime/hooks/pre-tool-use.js +28425 -0
- package/dist/hook-runtime/hooks/prim-hook.js +28977 -0
- package/dist/hook-runtime/hooks/session-end.js +1176 -0
- package/dist/hook-runtime/hooks/session-start.js +31553 -0
- package/dist/hook-runtime/index.js +59630 -0
- package/dist/hook-runtime/statusline-main.js +886 -0
- package/dist/hooks/post-commit.js +196 -0
- package/dist/hooks/post-rewrite.js +205 -0
- package/dist/hooks/post-tool-use.js +292 -0
- package/dist/hooks/pre-commit.js +15 -108
- package/dist/hooks/pre-tool-use.js +470 -0
- package/dist/hooks/prim-hook.js +242 -0
- package/dist/hooks/session-end.js +64 -0
- package/dist/hooks/session-start.js +309 -0
- package/dist/index.js +10651 -427
- package/dist/statusline-main.js +886 -0
- package/package.json +41 -16
- package/dist/chunk-3APLWTLB.js +0 -165
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import {
|
|
2
|
+
boundedHealthError
|
|
3
|
+
} from "./chunk-UDBBJWQH.js";
|
|
4
|
+
import {
|
|
5
|
+
terminalSafeLine
|
|
6
|
+
} from "./chunk-2IRLFOEL.js";
|
|
7
|
+
|
|
8
|
+
// src/hooks/redact.ts
|
|
9
|
+
import { existsSync, readFileSync, statSync } from "fs";
|
|
10
|
+
import { join } from "path";
|
|
11
|
+
import { Worker } from "worker_threads";
|
|
12
|
+
var USER_HOME_RULES = [
|
|
13
|
+
{
|
|
14
|
+
pattern: /\/Users\/[^/\\\s"'`]+/g,
|
|
15
|
+
reason: "user-home-path",
|
|
16
|
+
replacement: "/Users/__redacted_user__"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
pattern: /\/home\/[^/\\\s"'`]+/g,
|
|
20
|
+
reason: "user-home-path",
|
|
21
|
+
replacement: "/home/__redacted_user__"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
pattern: /([A-Za-z]:\\Users\\)[^/\\\s"'`]+/gi,
|
|
25
|
+
reason: "user-home-path",
|
|
26
|
+
replacement: "$1__redacted_user__"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
pattern: /\/root(?=\/|$)/g,
|
|
30
|
+
reason: "user-home-path",
|
|
31
|
+
replacement: "/__redacted_user__"
|
|
32
|
+
}
|
|
33
|
+
];
|
|
34
|
+
var DEFAULT_RULES = [
|
|
35
|
+
{ pattern: /Bearer\s+[A-Za-z0-9._~+/=-]+/gi, reason: "bearer-token" },
|
|
36
|
+
{
|
|
37
|
+
pattern: /\bsk-(?:(?:ant|proj)-)?[A-Za-z0-9_-]{16,}(?![A-Za-z0-9_-])/g,
|
|
38
|
+
reason: "sk-api-key"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
pattern: /-----BEGIN [A-Z ]+PRIVATE KEY-----[\s\S]+?-----END [A-Z ]+PRIVATE KEY-----/g,
|
|
42
|
+
reason: "private-key"
|
|
43
|
+
},
|
|
44
|
+
{ pattern: /xox[abprs]-[A-Za-z0-9-]{10,}/g, reason: "slack-token" },
|
|
45
|
+
{
|
|
46
|
+
pattern: /\b(?:gh[pousr]_[A-Za-z0-9]{36,}|github_pat_[A-Za-z0-9_]{50,})\b/g,
|
|
47
|
+
reason: "github-pat"
|
|
48
|
+
},
|
|
49
|
+
{ pattern: /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g, reason: "aws-access-key" },
|
|
50
|
+
{
|
|
51
|
+
pattern: /\bAWS_(?:SECRET_ACCESS_KEY|SESSION_TOKEN)"?\s*[:=]\s*["']?[A-Za-z0-9/+=]{20,}["']?/gi,
|
|
52
|
+
reason: "aws-secret"
|
|
53
|
+
},
|
|
54
|
+
{ pattern: /\bAIza[0-9A-Za-z_-]{35}(?![0-9A-Za-z_-])/g, reason: "gcp-api-key" },
|
|
55
|
+
{
|
|
56
|
+
pattern: /\bya29\.[0-9A-Za-z_-]{20,}(?![0-9A-Za-z_-])/g,
|
|
57
|
+
reason: "gcp-oauth-token"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
pattern: /\beyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*(?![A-Za-z0-9_-])/g,
|
|
61
|
+
reason: "jwt"
|
|
62
|
+
},
|
|
63
|
+
{ pattern: /\bBasic\s+[A-Za-z0-9+/]+={0,2}/gi, reason: "basic-auth" },
|
|
64
|
+
{ pattern: /\bhttps?:\/\/[^/\s:@]+:[^/\s@]+@/gi, reason: "basic-auth" },
|
|
65
|
+
...USER_HOME_RULES
|
|
66
|
+
];
|
|
67
|
+
var MAX_SCRUB_LEN = 256e3;
|
|
68
|
+
var MAX_WORKSPACE_CONFIG_BYTES = 64 * 1024;
|
|
69
|
+
var MAX_WORKSPACE_RULES = 32;
|
|
70
|
+
var MAX_WORKSPACE_PATTERN_LENGTH = 512;
|
|
71
|
+
var MAX_WORKSPACE_REASON_LENGTH = 64;
|
|
72
|
+
var WORKSPACE_RULE_TIMEOUT_MS = 250;
|
|
73
|
+
var WORKSPACE_FILE = ".prim/redaction.json";
|
|
74
|
+
var SAFE_REASON_RE = /^[A-Za-z0-9][A-Za-z0-9_-]*$/u;
|
|
75
|
+
var SAFE_WORKSPACE_FLAGS_RE = /^[gimsu]*$/u;
|
|
76
|
+
function debugRedaction(message) {
|
|
77
|
+
writeHookDebug(`redaction: ${message}`);
|
|
78
|
+
}
|
|
79
|
+
function normalizedWorkspaceFlags(value) {
|
|
80
|
+
if (value !== void 0 && typeof value !== "string") return null;
|
|
81
|
+
const requested = value ?? "";
|
|
82
|
+
if (!SAFE_WORKSPACE_FLAGS_RE.test(requested)) return null;
|
|
83
|
+
return [...new Set(`g${requested}`)].sort().join("");
|
|
84
|
+
}
|
|
85
|
+
function loadWorkspaceRules(cwd) {
|
|
86
|
+
const path = join(cwd, WORKSPACE_FILE);
|
|
87
|
+
if (!existsSync(path)) return [];
|
|
88
|
+
let cfg;
|
|
89
|
+
try {
|
|
90
|
+
if (statSync(path).size > MAX_WORKSPACE_CONFIG_BYTES) {
|
|
91
|
+
debugRedaction(`ignoring oversized ${WORKSPACE_FILE}`);
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
cfg = JSON.parse(readFileSync(path, "utf-8"));
|
|
95
|
+
} catch {
|
|
96
|
+
debugRedaction(`ignoring unparseable ${WORKSPACE_FILE}`);
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
if (!Array.isArray(cfg.rules)) return [];
|
|
100
|
+
const compiled = [];
|
|
101
|
+
for (const candidate of cfg.rules.slice(0, MAX_WORKSPACE_RULES)) {
|
|
102
|
+
if (!candidate || typeof candidate !== "object") continue;
|
|
103
|
+
const { pattern, reason } = candidate;
|
|
104
|
+
const flags = normalizedWorkspaceFlags(candidate.flags);
|
|
105
|
+
if (typeof pattern !== "string" || pattern.length === 0 || pattern.length > MAX_WORKSPACE_PATTERN_LENGTH || typeof reason !== "string" || reason.length === 0 || reason.length > MAX_WORKSPACE_REASON_LENGTH || !SAFE_REASON_RE.test(reason) || flags === null) {
|
|
106
|
+
debugRedaction("skipping invalid workspace rule metadata");
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
compiled.push({ pattern: new RegExp(pattern, flags), reason });
|
|
111
|
+
} catch {
|
|
112
|
+
debugRedaction(`skipping invalid workspace rule ${reason}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return compiled;
|
|
116
|
+
}
|
|
117
|
+
function applyRules(input, rules) {
|
|
118
|
+
if (input.length > MAX_SCRUB_LEN) return "<REDACTED:oversized>";
|
|
119
|
+
let out = input;
|
|
120
|
+
for (const rule of rules) {
|
|
121
|
+
out = out.replace(rule.pattern, rule.replacement ?? `<REDACTED:${rule.reason}>`);
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
var REDACTED_OBJECT_KEY = "<REDACTED:object-key>";
|
|
126
|
+
function redactedObjectKey(input, transform) {
|
|
127
|
+
return transform(input) === input ? input : REDACTED_OBJECT_KEY;
|
|
128
|
+
}
|
|
129
|
+
function uniqueObjectKey(output, key) {
|
|
130
|
+
let candidate = key;
|
|
131
|
+
let suffix = 2;
|
|
132
|
+
while (Object.hasOwn(output, candidate)) candidate = `${key}_${suffix++}`;
|
|
133
|
+
return candidate;
|
|
134
|
+
}
|
|
135
|
+
function mapStrings(value, transform) {
|
|
136
|
+
if (typeof value === "string") return transform(value);
|
|
137
|
+
if (Array.isArray(value)) return value.map((item) => mapStrings(item, transform));
|
|
138
|
+
if (value && typeof value === "object") {
|
|
139
|
+
const out = /* @__PURE__ */ Object.create(null);
|
|
140
|
+
for (const [key, item] of Object.entries(value)) {
|
|
141
|
+
out[uniqueObjectKey(out, redactedObjectKey(key, transform))] = mapStrings(item, transform);
|
|
142
|
+
}
|
|
143
|
+
return out;
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
function scrub(value, rules = DEFAULT_RULES) {
|
|
148
|
+
return mapStrings(value, (input) => applyRules(input, rules));
|
|
149
|
+
}
|
|
150
|
+
function debugDetail(value) {
|
|
151
|
+
try {
|
|
152
|
+
return String(value instanceof Error ? value.message : value);
|
|
153
|
+
} catch {
|
|
154
|
+
return "unprintable detail";
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function writeHookDebug(context, ...details) {
|
|
158
|
+
if (!process.env.PRIM_HOOK_DEBUG) return;
|
|
159
|
+
const raw = details.length > 0 ? `${context}: ${details.map(debugDetail).join(": ")}` : context;
|
|
160
|
+
const normalized = terminalSafeLine(raw);
|
|
161
|
+
const message = boundedHealthError(scrub(normalized, DEFAULT_RULES)) ?? context;
|
|
162
|
+
process.stderr.write(`[prim-hook] ${message === `${context}:` ? context : message}
|
|
163
|
+
`);
|
|
164
|
+
}
|
|
165
|
+
function redactEveryString(value, reason) {
|
|
166
|
+
return mapStrings(value, () => `<REDACTED:${reason}>`);
|
|
167
|
+
}
|
|
168
|
+
var WORKSPACE_REDACTION_WORKER = String.raw`
|
|
169
|
+
const { parentPort, workerData } = require("node:worker_threads");
|
|
170
|
+
const REDACTED_OBJECT_KEY = "<REDACTED:object-key>";
|
|
171
|
+
const rules = workerData.rules.map((rule) => ({
|
|
172
|
+
pattern: new RegExp(rule.source, rule.flags),
|
|
173
|
+
reason: rule.reason,
|
|
174
|
+
}));
|
|
175
|
+
function redact(input) {
|
|
176
|
+
let output = input;
|
|
177
|
+
for (const rule of rules) {
|
|
178
|
+
output = output.replace(rule.pattern, "<REDACTED:" + rule.reason + ">");
|
|
179
|
+
}
|
|
180
|
+
return output;
|
|
181
|
+
}
|
|
182
|
+
function objectKey(input) {
|
|
183
|
+
return redact(input) === input ? input : REDACTED_OBJECT_KEY;
|
|
184
|
+
}
|
|
185
|
+
function uniqueKey(output, key) {
|
|
186
|
+
let candidate = key;
|
|
187
|
+
let suffix = 2;
|
|
188
|
+
while (Object.hasOwn(output, candidate)) candidate = key + "_" + suffix++;
|
|
189
|
+
return candidate;
|
|
190
|
+
}
|
|
191
|
+
function walk(value) {
|
|
192
|
+
if (typeof value === "string") {
|
|
193
|
+
return redact(value);
|
|
194
|
+
}
|
|
195
|
+
if (Array.isArray(value)) return value.map(walk);
|
|
196
|
+
if (value && typeof value === "object") {
|
|
197
|
+
const output = Object.create(null);
|
|
198
|
+
for (const [key, item] of Object.entries(value)) {
|
|
199
|
+
output[uniqueKey(output, objectKey(key))] = walk(item);
|
|
200
|
+
}
|
|
201
|
+
return output;
|
|
202
|
+
}
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
parentPort.postMessage(walk(workerData.value));
|
|
206
|
+
`;
|
|
207
|
+
function applyWorkspaceRules(value, rules) {
|
|
208
|
+
const serialized = rules.map(({ pattern, reason }) => ({
|
|
209
|
+
source: pattern.source,
|
|
210
|
+
flags: pattern.flags,
|
|
211
|
+
reason
|
|
212
|
+
}));
|
|
213
|
+
return new Promise((resolve) => {
|
|
214
|
+
let worker;
|
|
215
|
+
try {
|
|
216
|
+
worker = new Worker(WORKSPACE_REDACTION_WORKER, {
|
|
217
|
+
eval: true,
|
|
218
|
+
workerData: { value, rules: serialized }
|
|
219
|
+
});
|
|
220
|
+
} catch {
|
|
221
|
+
resolve(redactEveryString(value, "workspace-rule-failed"));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
let settled = false;
|
|
225
|
+
const finish = (result) => {
|
|
226
|
+
if (settled) return;
|
|
227
|
+
settled = true;
|
|
228
|
+
clearTimeout(timer);
|
|
229
|
+
void worker.terminate();
|
|
230
|
+
resolve(result);
|
|
231
|
+
};
|
|
232
|
+
const failClosed = () => finish(redactEveryString(value, "workspace-rule-failed"));
|
|
233
|
+
const timer = setTimeout(failClosed, WORKSPACE_RULE_TIMEOUT_MS);
|
|
234
|
+
worker.once("message", finish);
|
|
235
|
+
worker.once("error", failClosed);
|
|
236
|
+
worker.once("exit", (code) => {
|
|
237
|
+
if (code !== 0) failClosed();
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
async function scrubFromCwd(value, cwd) {
|
|
242
|
+
const defaultScrubbed = scrub(value, DEFAULT_RULES);
|
|
243
|
+
const workspaceRules = loadWorkspaceRules(cwd);
|
|
244
|
+
return workspaceRules.length === 0 ? defaultScrubbed : await applyWorkspaceRules(defaultScrubbed, workspaceRules);
|
|
245
|
+
}
|
|
246
|
+
function scrubEnvironmentPaths(environment) {
|
|
247
|
+
const scrubPath = (value) => value === void 0 ? void 0 : applyRules(value, USER_HOME_RULES);
|
|
248
|
+
return {
|
|
249
|
+
...environment,
|
|
250
|
+
cwd: scrubPath(environment.cwd) ?? environment.cwd,
|
|
251
|
+
...environment.repoRoot === void 0 ? {} : { repoRoot: scrubPath(environment.repoRoot) },
|
|
252
|
+
...environment.gitRoot === void 0 ? {} : { gitRoot: scrubPath(environment.gitRoot) }
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export {
|
|
257
|
+
DEFAULT_RULES,
|
|
258
|
+
scrub,
|
|
259
|
+
writeHookDebug,
|
|
260
|
+
scrubFromCwd,
|
|
261
|
+
scrubEnvironmentPaths
|
|
262
|
+
};
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cursorHooksPath,
|
|
3
|
+
hasExactCursorHandler,
|
|
4
|
+
readCursorHooks
|
|
5
|
+
} from "./chunk-PO5QAYD7.js";
|
|
6
|
+
import {
|
|
7
|
+
resolveRepositoryContext
|
|
8
|
+
} from "./chunk-XG7S3LIL.js";
|
|
9
|
+
|
|
10
|
+
// src/hooks/agent.ts
|
|
11
|
+
function parseAgent(argv) {
|
|
12
|
+
const i = argv.indexOf("--agent");
|
|
13
|
+
const value = i !== -1 ? argv[i + 1] : void 0;
|
|
14
|
+
if (value === "codex" || value === "cursor" || value === "hermes") {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
return "claude_code";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// src/hooks/cursor-coexistence.ts
|
|
21
|
+
var CURSOR_EVENTS = /* @__PURE__ */ new Set([
|
|
22
|
+
"sessionStart",
|
|
23
|
+
"beforeSubmitPrompt",
|
|
24
|
+
"preToolUse",
|
|
25
|
+
"postToolUse",
|
|
26
|
+
"postToolUseFailure",
|
|
27
|
+
"afterAgentResponse",
|
|
28
|
+
"subagentStop",
|
|
29
|
+
"stop",
|
|
30
|
+
"sessionEnd"
|
|
31
|
+
]);
|
|
32
|
+
function cursorEvidence(parsed) {
|
|
33
|
+
return typeof parsed.cursor_version === "string" && parsed.cursor_version.length > 0 && typeof parsed.conversation_id === "string" && parsed.conversation_id.length > 0 && typeof parsed.generation_id === "string" && parsed.generation_id.length > 0 && Array.isArray(parsed.workspace_roots) && typeof parsed.hook_event_name === "string" && CURSOR_EVENTS.has(parsed.hook_event_name);
|
|
34
|
+
}
|
|
35
|
+
function uniqueProjectRoot(parsed) {
|
|
36
|
+
if (!Array.isArray(parsed.workspace_roots)) return;
|
|
37
|
+
const roots = new Set(
|
|
38
|
+
parsed.workspace_roots.filter((value) => typeof value === "string" && value.length > 0).map((value) => resolveRepositoryContext(value)?.repoRoot).filter((value) => !!value)
|
|
39
|
+
);
|
|
40
|
+
return roots.size === 1 ? [...roots][0] : void 0;
|
|
41
|
+
}
|
|
42
|
+
function shouldSuppressImportedCursorHandler(parsed, bin) {
|
|
43
|
+
if (!cursorEvidence(parsed)) return false;
|
|
44
|
+
const project = uniqueProjectRoot(parsed);
|
|
45
|
+
if (project) {
|
|
46
|
+
try {
|
|
47
|
+
if (hasExactCursorHandler(
|
|
48
|
+
readCursorHooks(cursorHooksPath("project", project)),
|
|
49
|
+
parsed.hook_event_name,
|
|
50
|
+
bin,
|
|
51
|
+
"project"
|
|
52
|
+
)) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
return hasExactCursorHandler(
|
|
60
|
+
readCursorHooks(cursorHooksPath("user")),
|
|
61
|
+
parsed.hook_event_name,
|
|
62
|
+
bin,
|
|
63
|
+
"user"
|
|
64
|
+
);
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// src/hooks/hook-stdin.ts
|
|
71
|
+
var DEFAULT_MAX_BYTES = 1024 * 1024;
|
|
72
|
+
function readHookStdin(timeoutMs = 1e3, maxBytes = DEFAULT_MAX_BYTES) {
|
|
73
|
+
return new Promise((resolve2, reject) => {
|
|
74
|
+
const chunks = [];
|
|
75
|
+
let bytes = 0;
|
|
76
|
+
let settled = false;
|
|
77
|
+
const finish = (error) => {
|
|
78
|
+
if (settled) return;
|
|
79
|
+
settled = true;
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
process.stdin.removeAllListeners("data");
|
|
82
|
+
process.stdin.removeAllListeners("end");
|
|
83
|
+
process.stdin.removeAllListeners("error");
|
|
84
|
+
if (error) reject(error);
|
|
85
|
+
else resolve2(Buffer.concat(chunks, bytes).toString("utf-8"));
|
|
86
|
+
};
|
|
87
|
+
const timer = setTimeout(() => finish(new Error("stdin read timeout")), timeoutMs);
|
|
88
|
+
process.stdin.on("data", (chunk) => {
|
|
89
|
+
bytes += chunk.length;
|
|
90
|
+
if (bytes > maxBytes) {
|
|
91
|
+
finish(new Error("stdin envelope exceeds size limit"));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
chunks.push(chunk);
|
|
95
|
+
});
|
|
96
|
+
process.stdin.on("end", () => finish());
|
|
97
|
+
process.stdin.on("error", (error) => finish(error));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/hooks/normalize.ts
|
|
102
|
+
import { createHash } from "crypto";
|
|
103
|
+
import { realpathSync } from "fs";
|
|
104
|
+
import { isAbsolute, resolve } from "path";
|
|
105
|
+
var HERMES_EVENT_MAP = {
|
|
106
|
+
on_session_start: "SessionStart",
|
|
107
|
+
on_session_end: "SessionEnd",
|
|
108
|
+
pre_llm_call: "UserPromptSubmit",
|
|
109
|
+
post_llm_call: "Stop",
|
|
110
|
+
pre_tool_call: "PreToolUse",
|
|
111
|
+
post_tool_call: "PostToolUse",
|
|
112
|
+
subagent_stop: "SubagentStop"
|
|
113
|
+
};
|
|
114
|
+
var CURSOR_EVENT_MAP = {
|
|
115
|
+
sessionStart: "SessionStart",
|
|
116
|
+
beforeSubmitPrompt: "UserPromptSubmit",
|
|
117
|
+
preToolUse: "PreToolUse",
|
|
118
|
+
postToolUse: "PostToolUse",
|
|
119
|
+
postToolUseFailure: "PostToolUseFailure",
|
|
120
|
+
afterAgentResponse: "AssistantResponse",
|
|
121
|
+
subagentStop: "SubagentStop",
|
|
122
|
+
stop: "Stop",
|
|
123
|
+
sessionEnd: "SessionEnd"
|
|
124
|
+
};
|
|
125
|
+
var CURSOR_TOOL_EVENTS = /* @__PURE__ */ new Set(["PreToolUse", "PostToolUse", "PostToolUseFailure"]);
|
|
126
|
+
var SAFE_CORRELATION_ID_RE = /^[A-Za-z0-9._:-]{1,128}$/u;
|
|
127
|
+
var CURSOR_LOCAL_ONLY_FIELDS = /* @__PURE__ */ new Set([
|
|
128
|
+
"conversation_id",
|
|
129
|
+
"generation_id",
|
|
130
|
+
"user_email",
|
|
131
|
+
"account_email",
|
|
132
|
+
"transcript_path",
|
|
133
|
+
"transcript",
|
|
134
|
+
"thought",
|
|
135
|
+
"thoughts",
|
|
136
|
+
"thinking",
|
|
137
|
+
"tool_output"
|
|
138
|
+
]);
|
|
139
|
+
function requiredId(value, field) {
|
|
140
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
141
|
+
throw new TypeError(`Cursor hook envelope is missing ${field}`);
|
|
142
|
+
}
|
|
143
|
+
return value;
|
|
144
|
+
}
|
|
145
|
+
function cursorToolUseId(value) {
|
|
146
|
+
if (SAFE_CORRELATION_ID_RE.test(value)) return value;
|
|
147
|
+
return `cursor:tool:v1:${createHash("sha256").update(value).digest("hex")}`;
|
|
148
|
+
}
|
|
149
|
+
function workspaceCandidates(value) {
|
|
150
|
+
if (!Array.isArray(value)) return [];
|
|
151
|
+
const roots = value.filter((item) => typeof item === "string" && item.length > 0);
|
|
152
|
+
if (roots.some((root) => !isAbsolute(root))) {
|
|
153
|
+
throw new TypeError("Cursor workspace roots must be absolute");
|
|
154
|
+
}
|
|
155
|
+
return roots;
|
|
156
|
+
}
|
|
157
|
+
function pathIdentity(path) {
|
|
158
|
+
try {
|
|
159
|
+
return realpathSync.native(path);
|
|
160
|
+
} catch {
|
|
161
|
+
return resolve(path);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function resolveCursorCwd(parsed) {
|
|
165
|
+
const roots = workspaceCandidates(parsed.workspace_roots).map(pathIdentity);
|
|
166
|
+
const rootRepositories = roots.map((root) => resolveRepositoryContext(root)?.repoRoot);
|
|
167
|
+
const repositoryRoots = new Set(rootRepositories.filter((root) => !!root));
|
|
168
|
+
if (repositoryRoots.size > 1) {
|
|
169
|
+
throw new TypeError("Cursor hook envelope spans multiple repositories");
|
|
170
|
+
}
|
|
171
|
+
if (roots.length > 1 && rootRepositories.some((root) => root === void 0)) {
|
|
172
|
+
throw new TypeError("Cursor hook envelope has ambiguous workspace roots");
|
|
173
|
+
}
|
|
174
|
+
const expectedRepo = [...repositoryRoots][0];
|
|
175
|
+
const rawCwd = typeof parsed.cwd === "string" && parsed.cwd.length > 0 ? parsed.cwd : void 0;
|
|
176
|
+
if (rawCwd && !isAbsolute(rawCwd)) {
|
|
177
|
+
throw new TypeError("Cursor hook cwd must be absolute");
|
|
178
|
+
}
|
|
179
|
+
const envelopeCwd = rawCwd ? pathIdentity(rawCwd) : void 0;
|
|
180
|
+
const input = typeof parsed.tool_input === "object" && parsed.tool_input !== null && !Array.isArray(parsed.tool_input) ? parsed.tool_input : void 0;
|
|
181
|
+
const rawToolCwd = parsed.tool_name === "Shell" && typeof input?.working_directory === "string" ? input.working_directory : void 0;
|
|
182
|
+
const base = envelopeCwd ?? (roots.length === 1 ? roots[0] : expectedRepo);
|
|
183
|
+
if (rawToolCwd && !isAbsolute(rawToolCwd) && !base) {
|
|
184
|
+
throw new TypeError("relative Cursor tool cwd has no workspace base");
|
|
185
|
+
}
|
|
186
|
+
const toolCwd = rawToolCwd ? pathIdentity(isAbsolute(rawToolCwd) ? rawToolCwd : resolve(base, rawToolCwd)) : void 0;
|
|
187
|
+
const selected = toolCwd ?? envelopeCwd ?? (roots.length === 1 ? roots[0] : expectedRepo);
|
|
188
|
+
if (!selected) {
|
|
189
|
+
throw new TypeError("Cursor hook envelope has no unambiguous workspace");
|
|
190
|
+
}
|
|
191
|
+
const selectedRepo = resolveRepositoryContext(selected)?.repoRoot;
|
|
192
|
+
if (expectedRepo && selectedRepo !== expectedRepo) {
|
|
193
|
+
throw new TypeError("Cursor hook cwd conflicts with workspace repository");
|
|
194
|
+
}
|
|
195
|
+
return selected;
|
|
196
|
+
}
|
|
197
|
+
function normalizeCursorEnvelope(parsed) {
|
|
198
|
+
const rawEvent = requiredId(parsed.hook_event_name, "hook_event_name");
|
|
199
|
+
const event = CURSOR_EVENT_MAP[rawEvent];
|
|
200
|
+
if (!event) throw new TypeError(`unsupported Cursor hook event: ${rawEvent}`);
|
|
201
|
+
const sessionId = requiredId(parsed.conversation_id, "conversation_id");
|
|
202
|
+
const turnId = requiredId(parsed.generation_id, "generation_id");
|
|
203
|
+
if (parsed.session_id !== void 0 && parsed.session_id !== sessionId) {
|
|
204
|
+
throw new TypeError("Cursor conversation_id conflicts with session_id");
|
|
205
|
+
}
|
|
206
|
+
if (parsed.turn_id !== void 0 && parsed.turn_id !== turnId) {
|
|
207
|
+
throw new TypeError("Cursor generation_id conflicts with turn_id");
|
|
208
|
+
}
|
|
209
|
+
const rawToolUseId = CURSOR_TOOL_EVENTS.has(event) ? requiredId(parsed.tool_use_id, "tool_use_id") : void 0;
|
|
210
|
+
const normalized = {
|
|
211
|
+
...Object.fromEntries(
|
|
212
|
+
Object.entries(parsed).filter(([key]) => !CURSOR_LOCAL_ONLY_FIELDS.has(key))
|
|
213
|
+
),
|
|
214
|
+
hook_event_name: event,
|
|
215
|
+
session_id: sessionId,
|
|
216
|
+
turn_id: turnId,
|
|
217
|
+
cwd: resolveCursorCwd(parsed),
|
|
218
|
+
...rawToolUseId === void 0 ? {} : { tool_use_id: cursorToolUseId(rawToolUseId) }
|
|
219
|
+
};
|
|
220
|
+
if (typeof parsed.tool_output === "string") {
|
|
221
|
+
try {
|
|
222
|
+
normalized.tool_response = JSON.parse(parsed.tool_output);
|
|
223
|
+
} catch {
|
|
224
|
+
normalized.tool_response = parsed.tool_output;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return normalized;
|
|
228
|
+
}
|
|
229
|
+
function normalizeEnvelope(parsed, agent) {
|
|
230
|
+
if (agent === "cursor") {
|
|
231
|
+
return normalizeCursorEnvelope(parsed);
|
|
232
|
+
}
|
|
233
|
+
if (agent === "codex") {
|
|
234
|
+
const event = parsed.hook_event_name;
|
|
235
|
+
if ((event === "PreToolUse" || event === "PostToolUse") && (typeof parsed.tool_use_id !== "string" || parsed.tool_use_id.length === 0)) {
|
|
236
|
+
const stableToolIdentity = JSON.stringify([
|
|
237
|
+
parsed.session_id ?? null,
|
|
238
|
+
parsed.turn_id ?? null,
|
|
239
|
+
parsed.tool_name ?? null,
|
|
240
|
+
parsed.tool_input ?? null
|
|
241
|
+
]);
|
|
242
|
+
return {
|
|
243
|
+
...parsed,
|
|
244
|
+
tool_use_id: `codex:fallback:v1:${createHash("sha256").update(stableToolIdentity).digest("hex")}`
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return parsed;
|
|
248
|
+
}
|
|
249
|
+
if (agent !== "hermes") {
|
|
250
|
+
return parsed;
|
|
251
|
+
}
|
|
252
|
+
const raw = parsed.hook_event_name;
|
|
253
|
+
if (typeof raw === "string" && raw in HERMES_EVENT_MAP) {
|
|
254
|
+
return { ...parsed, hook_event_name: HERMES_EVENT_MAP[raw] };
|
|
255
|
+
}
|
|
256
|
+
return parsed;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export {
|
|
260
|
+
parseAgent,
|
|
261
|
+
shouldSuppressImportedCursorHandler,
|
|
262
|
+
readHookStdin,
|
|
263
|
+
normalizeEnvelope
|
|
264
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// src/lib/unattended.ts
|
|
2
|
+
var UNATTENDED_ENV = "PRIM_UNATTENDED";
|
|
3
|
+
function unattendedEnv(env = process.env) {
|
|
4
|
+
return { ...env, [UNATTENDED_ENV]: "1" };
|
|
5
|
+
}
|
|
6
|
+
function isUnattended(env = process.env) {
|
|
7
|
+
return env[UNATTENDED_ENV] === "1";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
unattendedEnv,
|
|
12
|
+
isUnattended
|
|
13
|
+
};
|