@primitive.ai/prim 0.1.0-alpha.61 → 0.1.0-alpha.63
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/{chunk-I3HIPAIR.js → chunk-CPX3BUOC.js} +7 -4
- package/dist/chunk-DA4BMHEB.js +77 -0
- package/dist/{chunk-B4SHJYIN.js → chunk-GF5QII7T.js} +83 -110
- package/dist/{chunk-S4LFRVGD.js → chunk-HIMWM526.js} +1 -1
- package/dist/{chunk-YIMUIPMD.js → chunk-KBFFVYBC.js} +7 -1
- package/dist/{chunk-R4CUX6FY.js → chunk-KHZNYEUV.js} +1 -1
- package/dist/chunk-QGVHLP5J.js +117 -0
- package/dist/{chunk-IMAIBPUC.js → chunk-UVIWZWM6.js} +1 -0
- package/dist/chunk-WNIY6Z6N.js +1244 -0
- package/dist/{chunk-YITJ2H3B.js → chunk-WS3MCNFD.js} +1 -1
- package/dist/chunk-XTNMSS4C.js +283 -0
- package/dist/daemon/server.js +343 -44
- package/dist/hooks/post-commit.js +84 -18
- package/dist/hooks/post-tool-use.js +42 -26
- package/dist/hooks/pre-commit.js +2 -2
- package/dist/hooks/pre-tool-use.js +103 -11
- package/dist/hooks/prim-hook.js +33 -12
- package/dist/hooks/session-start.js +29 -8
- package/dist/index.js +361 -155
- package/dist/statusline-main.js +57 -42
- package/package.json +1 -1
- package/dist/chunk-GUXBUBQ7.js +0 -84
- package/dist/chunk-LDQZASIF.js +0 -166
- package/dist/chunk-N4R7EXZT.js +0 -620
- package/dist/chunk-YDBZTFW5.js +0 -67
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// src/lib/git.ts
|
|
2
|
+
import { execFileSync } from "child_process";
|
|
3
|
+
import { createHash } from "crypto";
|
|
4
|
+
import { lstatSync, realpathSync } from "fs";
|
|
5
|
+
import { basename, dirname, isAbsolute, relative, resolve, sep } from "path";
|
|
6
|
+
var GIT_TIMEOUT_MS = 1e3;
|
|
7
|
+
function gitToplevel(cwd) {
|
|
8
|
+
try {
|
|
9
|
+
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
|
|
10
|
+
cwd,
|
|
11
|
+
encoding: "utf-8",
|
|
12
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
13
|
+
timeout: GIT_TIMEOUT_MS
|
|
14
|
+
}).trim();
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function gitValue(cwd, args) {
|
|
20
|
+
try {
|
|
21
|
+
const value = execFileSync("git", args, {
|
|
22
|
+
cwd,
|
|
23
|
+
encoding: "utf-8",
|
|
24
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
25
|
+
timeout: GIT_TIMEOUT_MS
|
|
26
|
+
}).trim();
|
|
27
|
+
return value || void 0;
|
|
28
|
+
} catch {
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function githubRepositoryFullName(cwd) {
|
|
33
|
+
const remote = gitValue(cwd, ["config", "--get", "remote.origin.url"]);
|
|
34
|
+
if (!remote || [...remote].some((char) => char < " " || char === "\x7F")) return null;
|
|
35
|
+
const match = /^(?:git@github\.com:([^?#]+)|(?:https|ssh):\/\/(?:[^/@]+@)?github\.com(?::\d+)?\/([^?#]+))$/i.exec(
|
|
36
|
+
remote
|
|
37
|
+
);
|
|
38
|
+
const path = match?.[1] ?? match?.[2];
|
|
39
|
+
if (!path) return null;
|
|
40
|
+
const parts = path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "").split("/");
|
|
41
|
+
if (parts.length !== 2 || parts.some((part) => !part || part !== part.trim())) return null;
|
|
42
|
+
return `${parts[0]}/${parts[1]}`;
|
|
43
|
+
}
|
|
44
|
+
function canonicalGitRoot(cwd) {
|
|
45
|
+
try {
|
|
46
|
+
const top = gitToplevel(cwd);
|
|
47
|
+
return top ? realpathSync.native(top) : null;
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function normalizeOriginRemote(remote) {
|
|
53
|
+
const value = remote.trim();
|
|
54
|
+
if (!value) return void 0;
|
|
55
|
+
const scp = !value.includes("://") ? /^(?:[^@/\s]+@)?(?<host>[^:/\s]+):(?<path>.+)$/.exec(value)?.groups : void 0;
|
|
56
|
+
if (scp?.host && scp.path) {
|
|
57
|
+
const path = scp.path.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
58
|
+
return path ? `${scp.host.toLowerCase()}/${path}` : void 0;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const url = new URL(value);
|
|
62
|
+
url.username = "";
|
|
63
|
+
url.password = "";
|
|
64
|
+
url.search = "";
|
|
65
|
+
url.hash = "";
|
|
66
|
+
const path = url.pathname.replace(/^\/+|\/+$/g, "").replace(/\.git$/i, "");
|
|
67
|
+
if (url.host && path) return `${url.host.toLowerCase()}/${path}`;
|
|
68
|
+
return void 0;
|
|
69
|
+
} catch {
|
|
70
|
+
return void 0;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function opaqueRepoKey(source, value) {
|
|
74
|
+
return `repo_v1_${createHash("sha256").update(`${source}\0${value}`).digest("hex")}`;
|
|
75
|
+
}
|
|
76
|
+
function resolveRepositoryContext(cwd = process.cwd()) {
|
|
77
|
+
const top = gitToplevel(cwd);
|
|
78
|
+
if (!top) return null;
|
|
79
|
+
let repoRoot;
|
|
80
|
+
try {
|
|
81
|
+
repoRoot = realpathSync.native(top);
|
|
82
|
+
} catch {
|
|
83
|
+
repoRoot = resolve(top);
|
|
84
|
+
}
|
|
85
|
+
const origin = gitValue(repoRoot, ["config", "--get", "remote.origin.url"]);
|
|
86
|
+
const repoFullName = githubRepositoryFullName(repoRoot) ?? void 0;
|
|
87
|
+
const normalizedOrigin = origin ? normalizeOriginRemote(origin) : void 0;
|
|
88
|
+
if (normalizedOrigin) {
|
|
89
|
+
return {
|
|
90
|
+
repoRoot,
|
|
91
|
+
repoKey: opaqueRepoKey("origin", normalizedOrigin),
|
|
92
|
+
identitySource: "origin",
|
|
93
|
+
repoFullName
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const roots = gitValue(repoRoot, ["rev-list", "--max-parents=0", "HEAD"])?.split(/\s+/).filter(Boolean).sort();
|
|
97
|
+
if (roots && roots.length > 0) {
|
|
98
|
+
return {
|
|
99
|
+
repoRoot,
|
|
100
|
+
repoKey: opaqueRepoKey("root_commit", roots.join("\n")),
|
|
101
|
+
identitySource: "root_commit",
|
|
102
|
+
repoFullName
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return { repoRoot, repoFullName };
|
|
106
|
+
}
|
|
107
|
+
function nearestExistingAncestor(path) {
|
|
108
|
+
const suffix = [];
|
|
109
|
+
let cursor = path;
|
|
110
|
+
while (true) {
|
|
111
|
+
try {
|
|
112
|
+
lstatSync(cursor);
|
|
113
|
+
break;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const code = typeof error === "object" && error !== null && "code" in error ? error.code : void 0;
|
|
116
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") return null;
|
|
117
|
+
const parent = dirname(cursor);
|
|
118
|
+
if (parent === cursor) return null;
|
|
119
|
+
suffix.unshift(cursor.slice(parent.length + (parent.endsWith(sep) ? 0 : 1)));
|
|
120
|
+
cursor = parent;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
return { real: realpathSync.native(cursor), suffix };
|
|
125
|
+
} catch {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function canonicalRepositoryPathScoped(filePath, context, baseDir) {
|
|
130
|
+
if (!filePath || filePath !== filePath.trim() || filePath.includes("\0")) {
|
|
131
|
+
return { ok: false, reason: "invalid_path" };
|
|
132
|
+
}
|
|
133
|
+
const absolute = resolve(baseDir, filePath);
|
|
134
|
+
const resolved = nearestExistingAncestor(absolute);
|
|
135
|
+
if (!resolved) return { ok: false, reason: "invalid_path" };
|
|
136
|
+
const target = resolve(resolved.real, ...resolved.suffix);
|
|
137
|
+
const rel = relative(context.repoRoot, target);
|
|
138
|
+
if (!rel || rel === ".") return { ok: false, reason: "invalid_path" };
|
|
139
|
+
if (isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
140
|
+
return { ok: false, reason: "outside_repository" };
|
|
141
|
+
}
|
|
142
|
+
return { ok: true, file: sep === "/" ? rel : rel.split(sep).join("/") };
|
|
143
|
+
}
|
|
144
|
+
function canonicalRepositoryPathLegacy(filePath, cwd, repositoryRoot = canonicalGitRoot(cwd)) {
|
|
145
|
+
if (!repositoryRoot || !filePath || [...filePath].some((char) => char < " " || char === "\x7F" || "~*?[]{}$`\\".includes(char)))
|
|
146
|
+
return null;
|
|
147
|
+
const absolute = resolve(cwd, filePath);
|
|
148
|
+
let target;
|
|
149
|
+
try {
|
|
150
|
+
const stat = lstatSync(absolute);
|
|
151
|
+
if (!stat.isFile() || stat.isSymbolicLink()) return null;
|
|
152
|
+
target = realpathSync.native(absolute);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (!(error instanceof Error) || error.code !== "ENOENT") {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
const parent = realpathSync.native(dirname(absolute));
|
|
159
|
+
if (!lstatSync(parent).isDirectory()) return null;
|
|
160
|
+
target = resolve(parent, basename(absolute));
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const rel = relative(repositoryRoot, target);
|
|
166
|
+
if (!rel || isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return sep === "/" ? rel : rel.split(sep).join("/");
|
|
170
|
+
}
|
|
171
|
+
function canonicalRepositoryPath(filePath, contextOrCwd, baseDirOrRoot) {
|
|
172
|
+
if (typeof contextOrCwd === "string") {
|
|
173
|
+
return canonicalRepositoryPathLegacy(filePath, contextOrCwd, baseDirOrRoot);
|
|
174
|
+
}
|
|
175
|
+
return canonicalRepositoryPathScoped(
|
|
176
|
+
filePath,
|
|
177
|
+
contextOrCwd,
|
|
178
|
+
baseDirOrRoot ?? contextOrCwd.repoRoot
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/lib/activation.ts
|
|
183
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
184
|
+
import { existsSync, readFileSync } from "fs";
|
|
185
|
+
import { homedir } from "os";
|
|
186
|
+
import { join } from "path";
|
|
187
|
+
var PRIM_ACTIVE_KEY = "prim.active";
|
|
188
|
+
var PRIM_REPO_SYNC_ID_KEY = "prim.repoSyncId";
|
|
189
|
+
var GIT_TIMEOUT_MS2 = 1e3;
|
|
190
|
+
var REPO_SYNC_ID_RE = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
191
|
+
var PROJECT_INSTALL_FILES = [".claude/settings.json", ".codex/hooks.json"];
|
|
192
|
+
var PRIM_HOOK_BINS = ["prim-hook", "prim-pre-tool-use", "prim-post-tool-use"];
|
|
193
|
+
function repoActiveFlag(cwd) {
|
|
194
|
+
try {
|
|
195
|
+
const value = execFileSync2("git", ["config", "--get", PRIM_ACTIVE_KEY], {
|
|
196
|
+
cwd,
|
|
197
|
+
encoding: "utf-8",
|
|
198
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
199
|
+
timeout: GIT_TIMEOUT_MS2
|
|
200
|
+
}).trim();
|
|
201
|
+
if (value === "true") return "true";
|
|
202
|
+
if (value === "false") return "false";
|
|
203
|
+
return void 0;
|
|
204
|
+
} catch {
|
|
205
|
+
return void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
function isRepoActive(cwd) {
|
|
209
|
+
return repoActiveFlag(cwd) === "true";
|
|
210
|
+
}
|
|
211
|
+
function setRepoActive(cwd, active) {
|
|
212
|
+
execFileSync2("git", ["config", "--local", PRIM_ACTIVE_KEY, active ? "true" : "false"], {
|
|
213
|
+
cwd,
|
|
214
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
215
|
+
timeout: GIT_TIMEOUT_MS2
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function isValidRepoSyncId(value) {
|
|
219
|
+
return typeof value === "string" && REPO_SYNC_ID_RE.test(value);
|
|
220
|
+
}
|
|
221
|
+
function repoSyncId(cwd) {
|
|
222
|
+
try {
|
|
223
|
+
const value = execFileSync2("git", ["config", "--local", "--get", PRIM_REPO_SYNC_ID_KEY], {
|
|
224
|
+
cwd,
|
|
225
|
+
encoding: "utf-8",
|
|
226
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
227
|
+
timeout: GIT_TIMEOUT_MS2
|
|
228
|
+
}).trim();
|
|
229
|
+
return isValidRepoSyncId(value) ? value : void 0;
|
|
230
|
+
} catch {
|
|
231
|
+
return void 0;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function setRepoSyncId(cwd, value) {
|
|
235
|
+
if (!isValidRepoSyncId(value)) {
|
|
236
|
+
throw new Error("invalid repository binding returned by server");
|
|
237
|
+
}
|
|
238
|
+
execFileSync2("git", ["config", "--local", PRIM_REPO_SYNC_ID_KEY, value], {
|
|
239
|
+
cwd,
|
|
240
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
241
|
+
timeout: GIT_TIMEOUT_MS2
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
function hasProjectPrimInstall(cwd) {
|
|
245
|
+
const root = gitToplevel(cwd);
|
|
246
|
+
if (root === null || root === homedir()) return false;
|
|
247
|
+
for (const rel of PROJECT_INSTALL_FILES) {
|
|
248
|
+
const path = join(root, rel);
|
|
249
|
+
if (existsSync(path)) {
|
|
250
|
+
try {
|
|
251
|
+
const content = readFileSync(path, "utf-8");
|
|
252
|
+
if (PRIM_HOOK_BINS.some((bin) => content.includes(bin))) return true;
|
|
253
|
+
} catch {
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
function isRepoActiveForCapture(cwd) {
|
|
260
|
+
const flag = repoActiveFlag(cwd);
|
|
261
|
+
if (flag === "true") return true;
|
|
262
|
+
if (flag === "false") return false;
|
|
263
|
+
return hasProjectPrimInstall(cwd);
|
|
264
|
+
}
|
|
265
|
+
function decisionIngestionStatus(cwd) {
|
|
266
|
+
return isRepoActiveForCapture(cwd) ? "enabled" : "disabled";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export {
|
|
270
|
+
gitToplevel,
|
|
271
|
+
githubRepositoryFullName,
|
|
272
|
+
canonicalGitRoot,
|
|
273
|
+
resolveRepositoryContext,
|
|
274
|
+
canonicalRepositoryPath,
|
|
275
|
+
repoActiveFlag,
|
|
276
|
+
isRepoActive,
|
|
277
|
+
setRepoActive,
|
|
278
|
+
isValidRepoSyncId,
|
|
279
|
+
repoSyncId,
|
|
280
|
+
setRepoSyncId,
|
|
281
|
+
isRepoActiveForCapture,
|
|
282
|
+
decisionIngestionStatus
|
|
283
|
+
};
|