@primitive.ai/prim 0.1.0-alpha.122 → 0.1.0-alpha.124
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 +81 -5
- package/dist/{chunk-5ME3WD3P.js → chunk-3OKVMKK2.js} +6 -3
- package/dist/chunk-5BZWFN62.js +206 -0
- package/dist/{chunk-QG3GASJO.js → chunk-5KPDY5GF.js} +4 -4
- package/dist/{chunk-HLMC7VS4.js → chunk-5MH2C5QG.js} +3878 -2644
- package/dist/{chunk-NMZSCSMY.js → chunk-6PSCGY2A.js} +11 -1
- package/dist/{chunk-OXW6PSPT.js → chunk-6VJ7C2B5.js} +15 -3
- package/dist/{chunk-DQ4I3FLF.js → chunk-6W72SEXC.js} +3 -3
- package/dist/{chunk-4M23K2KZ.js → chunk-6WKJWW6D.js} +2 -2
- package/dist/{chunk-JYPVPYW3.js → chunk-A6YTFBQ4.js} +3 -3
- package/dist/{chunk-7LXRFXZG.js → chunk-DGZX3MID.js} +3 -3
- package/dist/{chunk-4NUGCUQC.js → chunk-EER7VG3P.js} +2 -2
- package/dist/{chunk-JZC2LHS6.js → chunk-EWV7VIZ3.js} +1 -1
- package/dist/{chunk-WN7OURQ3.js → chunk-KMWXI7KO.js} +1 -1
- package/dist/{chunk-HG7FJK7R.js → chunk-KTXJJE5K.js} +2 -2
- package/dist/{chunk-4GYJHN26.js → chunk-Q2PVD5W2.js} +1575 -549
- package/dist/{chunk-S3ZIMGXV.js → chunk-QZWLZH6D.js} +76 -2
- package/dist/{chunk-ZQ7UK5US.js → chunk-TS2NX6YT.js} +31 -9
- package/dist/{chunk-AKGRY7OU.js → chunk-WLVEXOHC.js} +145 -63
- package/dist/{chunk-FT673ALA.js → chunk-WQFJ5TEG.js} +2 -2
- package/dist/{chunk-T2T45KF6.js → chunk-ZMH6GJUG.js} +4 -4
- package/dist/daemon/server.js +7795 -5449
- package/dist/hook-runtime/daemon/server.js +7795 -5449
- package/dist/hook-runtime/hooks/post-commit.js +5828 -4329
- package/dist/hook-runtime/hooks/post-rewrite.js +4331 -2833
- package/dist/hook-runtime/hooks/post-tool-use.js +5242 -2892
- package/dist/hook-runtime/hooks/pre-commit.js +1253 -280
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +4272 -1982
- package/dist/hook-runtime/hooks/pre-tool-use.js +13 -3
- package/dist/hook-runtime/hooks/prim-hook.js +4719 -2479
- package/dist/hook-runtime/hooks/session-end.js +393 -140
- package/dist/hook-runtime/hooks/session-start.js +4207 -1998
- package/dist/hook-runtime/index.js +6157 -3544
- package/dist/hook-runtime/statusline-main.js +368 -122
- package/dist/hooks/post-commit.js +7 -7
- package/dist/hooks/post-rewrite.js +6 -6
- package/dist/hooks/post-tool-use.js +31 -21
- package/dist/hooks/pre-commit.js +4 -4
- package/dist/hooks/pre-tool-use-worker.js +88 -18
- package/dist/hooks/pre-tool-use.js +13 -3
- package/dist/hooks/prim-hook.js +16 -16
- package/dist/hooks/session-end.js +4 -4
- package/dist/hooks/session-start.js +16 -12
- package/dist/index.js +230 -150
- package/dist/statusline-main.js +368 -122
- package/package.json +5 -2
- package/dist/chunk-RRAC76LJ.js +0 -36
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
import { createRequire as __primCreateRequire } from "node:module"; const require = __primCreateRequire(import.meta.url);
|
|
3
3
|
|
|
4
4
|
// src/commands/statusline.ts
|
|
5
|
-
import { readFileSync as
|
|
6
|
-
import { dirname as
|
|
7
|
-
import { fileURLToPath as
|
|
5
|
+
import { readFileSync as readFileSync10 } from "fs";
|
|
6
|
+
import { dirname as dirname7, resolve as resolve3 } from "path";
|
|
7
|
+
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
8
8
|
|
|
9
9
|
// src/client.ts
|
|
10
|
-
import { createHash as
|
|
11
|
-
import { chmodSync as
|
|
12
|
-
import { dirname as
|
|
10
|
+
import { createHash as createHash5 } from "crypto";
|
|
11
|
+
import { chmodSync as chmodSync3, existsSync as existsSync4, mkdirSync as mkdirSync4, readFileSync as readFileSync8, rmSync as rmSync6 } from "fs";
|
|
12
|
+
import { dirname as dirname5 } from "path";
|
|
13
13
|
|
|
14
14
|
// src/lib/atomic-file.ts
|
|
15
15
|
import { randomUUID } from "crypto";
|
|
@@ -27,37 +27,75 @@ import {
|
|
|
27
27
|
import { dirname } from "path";
|
|
28
28
|
|
|
29
29
|
// src/lib/credentials.ts
|
|
30
|
-
import { createHash } from "crypto";
|
|
31
|
-
import { readFileSync } from "fs";
|
|
32
|
-
import { join as
|
|
30
|
+
import { createHash as createHash4 } from "crypto";
|
|
31
|
+
import { readFileSync as readFileSync6 } from "fs";
|
|
32
|
+
import { join as join7 } from "path";
|
|
33
33
|
|
|
34
|
-
// src/lib/
|
|
35
|
-
import {
|
|
36
|
-
import {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
// src/lib/machine-credentials.ts
|
|
35
|
+
import { execFileSync } from "child_process";
|
|
36
|
+
import { createHash as createHash3, randomUUID as randomUUID2 } from "crypto";
|
|
37
|
+
import { chmodSync, existsSync as existsSync3, lstatSync as lstatSync3, mkdirSync as mkdirSync3, readFileSync as readFileSync5, rmSync as rmSync5 } from "fs";
|
|
38
|
+
import { createRequire } from "module";
|
|
39
|
+
import { dirname as dirname4, join as join6 } from "path";
|
|
40
|
+
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
41
|
+
|
|
42
|
+
// src/lib/file-lock.ts
|
|
43
|
+
import { randomBytes } from "crypto";
|
|
44
|
+
import { existsSync, mkdirSync as mkdirSync2, readFileSync, rmSync as rmSync2, statSync, writeFileSync as writeFileSync2 } from "fs";
|
|
45
|
+
import { dirname as dirname2, join } from "path";
|
|
46
|
+
var syncSleeper = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
|
|
47
|
+
|
|
48
|
+
// src/lib/machine-broker.ts
|
|
49
|
+
import { spawn } from "child_process";
|
|
50
|
+
import { createHash, createHmac, randomBytes as randomBytes2, timingSafeEqual } from "crypto";
|
|
51
|
+
import { lstatSync, readFileSync as readFileSync3, rmSync as rmSync3 } from "fs";
|
|
52
|
+
import { createServer } from "http";
|
|
53
|
+
import { basename, join as join4 } from "path";
|
|
54
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
55
|
+
|
|
56
|
+
// src/lib/bin-path.ts
|
|
57
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2 } from "fs";
|
|
58
|
+
import { dirname as dirname3, isAbsolute, join as join2 } from "path";
|
|
59
|
+
import { fileURLToPath } from "url";
|
|
60
|
+
var PKG_NAME = "@primitive.ai/prim";
|
|
61
|
+
var ROOT_WALK_LIMIT = 6;
|
|
62
|
+
var resolvedRoot;
|
|
63
|
+
function locateRoot() {
|
|
64
|
+
if (resolvedRoot !== void 0) {
|
|
65
|
+
return resolvedRoot;
|
|
41
66
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
67
|
+
let dir = dirname3(fileURLToPath(import.meta.url));
|
|
68
|
+
for (let depth = 0; depth < ROOT_WALK_LIMIT; depth++) {
|
|
69
|
+
const manifestPath = join2(dir, "package.json");
|
|
70
|
+
if (existsSync2(manifestPath)) {
|
|
71
|
+
try {
|
|
72
|
+
const manifest = JSON.parse(readFileSync2(manifestPath, "utf-8"));
|
|
73
|
+
if (manifest.name === PKG_NAME && manifest.bin) {
|
|
74
|
+
resolvedRoot = { dir, version: manifest.version, bin: manifest.bin };
|
|
75
|
+
return resolvedRoot;
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const parent = dirname3(dir);
|
|
81
|
+
if (parent === dir) {
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
dir = parent;
|
|
55
85
|
}
|
|
56
|
-
|
|
86
|
+
resolvedRoot = null;
|
|
87
|
+
return resolvedRoot;
|
|
57
88
|
}
|
|
58
|
-
function
|
|
59
|
-
return
|
|
89
|
+
function packageVersion() {
|
|
90
|
+
return locateRoot()?.version ?? null;
|
|
60
91
|
}
|
|
92
|
+
var PRIM_CONFIG_SHELL_RESOLVER = [
|
|
93
|
+
'prim_absolute() { case "$1" in [/]* ) ;; *) return 1 ;; esac; case "$1" in [[:space:]]*|*[[:space:]]|*[/][/]*|*[/].[/]*|*[/]..[/]*|*[/].|*[/]..|?*[/]) return 1 ;; esac; }; ',
|
|
94
|
+
'prim_config=${PRIM_CONFIG_DIR:-}; if ! prim_absolute "$prim_config"; then prim_config=${XDG_CONFIG_HOME:-}; if prim_absolute "$prim_config"; then ',
|
|
95
|
+
'case "$prim_config" in /) prim_config=/prim ;; *) prim_config="$prim_config/prim" ;; esac; ',
|
|
96
|
+
'else prim_home=${HOME:-}; prim_absolute "$prim_home" || exit 78; case "$prim_home" in /) prim_config=/.config/prim ;; *) prim_config="$prim_home/.config/prim" ;; esac; ',
|
|
97
|
+
"fi; fi; "
|
|
98
|
+
].join("");
|
|
61
99
|
|
|
62
100
|
// src/lib/workos-connect.ts
|
|
63
101
|
var DISCOVERY_MAX_BYTES = 16 * 1024;
|
|
@@ -138,28 +176,278 @@ function parseWorkosConnectCredentialMetadata(value) {
|
|
|
138
176
|
};
|
|
139
177
|
}
|
|
140
178
|
|
|
179
|
+
// src/lib/machine-protocol.ts
|
|
180
|
+
var MachineAuthError = class extends Error {
|
|
181
|
+
constructor(code, message, terminal = false, retryAt) {
|
|
182
|
+
super(message);
|
|
183
|
+
this.code = code;
|
|
184
|
+
this.terminal = terminal;
|
|
185
|
+
this.retryAt = retryAt;
|
|
186
|
+
this.name = "MachineAuthError";
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
function machineString(value, maximum = 4096) {
|
|
190
|
+
return typeof value === "string" && value.length > 0 && value.length <= maximum && value.trim() === value && !/[\p{Cc}\p{Cf}]/u.test(value);
|
|
191
|
+
}
|
|
192
|
+
function machineRecord(value) {
|
|
193
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
194
|
+
throw new MachineAuthError("invalid_response", "Invalid machine authentication response.");
|
|
195
|
+
}
|
|
196
|
+
return value;
|
|
197
|
+
}
|
|
198
|
+
function machineApiUrl(value) {
|
|
199
|
+
const url = new URL(value);
|
|
200
|
+
const local = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
201
|
+
if (url.protocol !== "https:" && !(local && url.protocol === "http:") || url.username || url.password || url.search || url.hash || url.pathname !== "/") {
|
|
202
|
+
throw new MachineAuthError("invalid_configuration", "The API URL must be an HTTPS origin.");
|
|
203
|
+
}
|
|
204
|
+
return url.origin;
|
|
205
|
+
}
|
|
206
|
+
function parseMachineConfiguration(value) {
|
|
207
|
+
const record = machineRecord(value);
|
|
208
|
+
if (record.protocol_version !== 1 || !machineString(record.issuer, 2048) || !machineString(record.audience, 256) || !/^client_[\w-]+$/u.test(record.audience)) {
|
|
209
|
+
throw new MachineAuthError("invalid_configuration", "Invalid M2M discovery configuration.");
|
|
210
|
+
}
|
|
211
|
+
const issuer = new URL(record.issuer);
|
|
212
|
+
if (issuer.protocol !== "https:" || issuer.origin !== record.issuer) {
|
|
213
|
+
throw new MachineAuthError(
|
|
214
|
+
"invalid_configuration",
|
|
215
|
+
"The WorkOS issuer must be an HTTPS origin."
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
return { issuer: record.issuer, audience: record.audience };
|
|
219
|
+
}
|
|
220
|
+
function parseMachineIdentity(value, clientId) {
|
|
221
|
+
const data = machineRecord(value);
|
|
222
|
+
const principal = machineRecord(data.principal);
|
|
223
|
+
if (data.authenticated !== true || data.captureAuthorityKind !== "workos_m2m" || data.organizationBindingVersion !== 2 || principal.kind !== "machine" || principal.clientId !== clientId || !machineString(principal.machineId, 256) || !machineString(principal.displayName, 256) || !machineString(data.organizationId, 256) || !machineString(data.workosOrganizationId, 256) || !Array.isArray(principal.scopes) || !principal.scopes.every((scope) => machineString(scope, 128))) {
|
|
224
|
+
throw new MachineAuthError(
|
|
225
|
+
"machine_identity_rejected",
|
|
226
|
+
"The API did not verify a machine identity.",
|
|
227
|
+
true
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
machineId: principal.machineId,
|
|
232
|
+
clientId,
|
|
233
|
+
displayName: principal.displayName,
|
|
234
|
+
organizationId: data.organizationId,
|
|
235
|
+
workosOrganizationId: data.workosOrganizationId,
|
|
236
|
+
scopes: principal.scopes
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// src/lib/paths.ts
|
|
241
|
+
import { homedir } from "os";
|
|
242
|
+
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
243
|
+
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
244
|
+
function absolutePath(value, variable) {
|
|
245
|
+
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
246
|
+
throw new Error(`${variable} contains unsafe characters`);
|
|
247
|
+
}
|
|
248
|
+
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
249
|
+
const normalized = normalize(value);
|
|
250
|
+
return normalized === value ? value : void 0;
|
|
251
|
+
}
|
|
252
|
+
function resolvePrimConfigDirectory(options = {}) {
|
|
253
|
+
const env = options.env ?? process.env;
|
|
254
|
+
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
255
|
+
if (explicit) return { path: explicit, source: "explicit" };
|
|
256
|
+
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
257
|
+
if (xdgConfigHome) return { path: join3(xdgConfigHome, "prim"), source: "xdg" };
|
|
258
|
+
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
259
|
+
if (!home) {
|
|
260
|
+
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
261
|
+
}
|
|
262
|
+
return { path: join3(home, ".config", "prim"), source: "default" };
|
|
263
|
+
}
|
|
264
|
+
function primConfigDirectory(options = {}) {
|
|
265
|
+
return resolvePrimConfigDirectory(options).path;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/lib/machine-broker.ts
|
|
269
|
+
var MAX_RESPONSE_BYTES = 96 * 1024;
|
|
270
|
+
var IDLE_MS = 5 * 6e4;
|
|
271
|
+
|
|
272
|
+
// src/lib/machine-retry.ts
|
|
273
|
+
import { createHash as createHash2 } from "crypto";
|
|
274
|
+
import { lstatSync as lstatSync2, readFileSync as readFileSync4, rmSync as rmSync4 } from "fs";
|
|
275
|
+
import { join as join5 } from "path";
|
|
276
|
+
|
|
277
|
+
// src/lib/machine-credentials.ts
|
|
278
|
+
var require2 = createRequire(import.meta.url);
|
|
279
|
+
var DEFAULT_API_URL = "https://api.getprimitive.ai";
|
|
280
|
+
var PROFILE_FILE = "machine_profile.json";
|
|
281
|
+
var cached;
|
|
282
|
+
function configurationDirectory(deps) {
|
|
283
|
+
return deps.directory ?? primConfigDirectory({ env: deps.env });
|
|
284
|
+
}
|
|
285
|
+
function apiUrlFor(deps) {
|
|
286
|
+
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL);
|
|
287
|
+
}
|
|
288
|
+
function hash(value) {
|
|
289
|
+
return createHash3("sha256").update(value).digest("hex");
|
|
290
|
+
}
|
|
291
|
+
function environmentContextPath(deps, clientId) {
|
|
292
|
+
return join6(
|
|
293
|
+
configurationDirectory(deps),
|
|
294
|
+
`machine_context_${hash(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
function readEnvironmentContext(environment, deps) {
|
|
298
|
+
try {
|
|
299
|
+
const data = machineRecord(readPrivate(environmentContextPath(deps, environment.clientId)));
|
|
300
|
+
if (data.generation !== environment.generation || data.clientId !== environment.clientId || data.apiUrl !== apiUrlFor(deps) || typeof data.expiresAt !== "number" || !Number.isFinite(data.expiresAt) || data.expiresAt <= Date.now())
|
|
301
|
+
return void 0;
|
|
302
|
+
const configuration = parseMachineConfiguration({
|
|
303
|
+
protocol_version: data.version,
|
|
304
|
+
issuer: data.issuer,
|
|
305
|
+
audience: data.audience
|
|
306
|
+
});
|
|
307
|
+
const identity = parseStoredIdentity(data);
|
|
308
|
+
return {
|
|
309
|
+
...configuration,
|
|
310
|
+
...identity,
|
|
311
|
+
apiUrl: data.apiUrl,
|
|
312
|
+
generation: environment.generation
|
|
313
|
+
};
|
|
314
|
+
} catch {
|
|
315
|
+
return void 0;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
function readPrivate(path) {
|
|
319
|
+
let raw;
|
|
320
|
+
try {
|
|
321
|
+
const stat = lstatSync3(path);
|
|
322
|
+
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
323
|
+
throw new MachineAuthError(
|
|
324
|
+
"unsafe_storage",
|
|
325
|
+
"Machine credential files must be owner-only regular files."
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
raw = readFileSync5(path, "utf8");
|
|
329
|
+
} catch (error) {
|
|
330
|
+
if (error.code === "ENOENT") return void 0;
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
return JSON.parse(raw);
|
|
335
|
+
} catch {
|
|
336
|
+
throw new MachineAuthError("invalid_storage", "Stored machine credentials are malformed.");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
function parseStoredIdentity(data) {
|
|
340
|
+
return parseMachineIdentity(
|
|
341
|
+
{
|
|
342
|
+
authenticated: true,
|
|
343
|
+
organizationBindingVersion: 2,
|
|
344
|
+
captureAuthorityKind: "workos_m2m",
|
|
345
|
+
organizationId: data.organizationId,
|
|
346
|
+
workosOrganizationId: data.workosOrganizationId,
|
|
347
|
+
principal: { ...data, kind: "machine" }
|
|
348
|
+
},
|
|
349
|
+
String(data.clientId)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
function parseProfile(value) {
|
|
353
|
+
const data = machineRecord(value);
|
|
354
|
+
const config = parseMachineConfiguration({
|
|
355
|
+
protocol_version: data.version,
|
|
356
|
+
issuer: data.issuer,
|
|
357
|
+
audience: data.audience
|
|
358
|
+
});
|
|
359
|
+
if (typeof data.apiUrl !== "string" || machineApiUrl(data.apiUrl) !== data.apiUrl || !machineString(data.generation, 64) || !/^[a-f0-9-]{36}$/u.test(data.generation) || data.storage !== "keychain" && data.storage !== "file" || !machineString(data.clientId, 256)) {
|
|
360
|
+
throw new MachineAuthError("invalid_storage", "Stored machine profile is malformed.");
|
|
361
|
+
}
|
|
362
|
+
const identity = parseStoredIdentity(data);
|
|
363
|
+
return {
|
|
364
|
+
...config,
|
|
365
|
+
...identity,
|
|
366
|
+
version: 1,
|
|
367
|
+
apiUrl: data.apiUrl,
|
|
368
|
+
generation: data.generation,
|
|
369
|
+
storage: data.storage
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function readMachineProfile(deps = {}) {
|
|
373
|
+
const value = readPrivate(join6(configurationDirectory(deps), PROFILE_FILE));
|
|
374
|
+
return value === void 0 ? void 0 : parseProfile(value);
|
|
375
|
+
}
|
|
376
|
+
function environmentCredentials(deps) {
|
|
377
|
+
const env = deps.env ?? process.env;
|
|
378
|
+
const { PRIM_M2M_CLIENT_ID: clientId, PRIM_M2M_CLIENT_SECRET: clientSecret } = env;
|
|
379
|
+
if (clientId === void 0 && clientSecret === void 0) return void 0;
|
|
380
|
+
if (!clientId || !clientSecret || !/^client_[\w-]+$/u.test(clientId) || !machineString(clientSecret)) {
|
|
381
|
+
throw new MachineAuthError(
|
|
382
|
+
"incomplete_machine_credentials",
|
|
383
|
+
"Set both PRIM_M2M_CLIENT_ID and PRIM_M2M_CLIENT_SECRET."
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
return {
|
|
387
|
+
clientId,
|
|
388
|
+
clientSecret,
|
|
389
|
+
generation: hash(`${apiUrlFor(deps)}\0${clientId}\0${clientSecret}`)
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
function hasMachineConfiguration(deps = {}) {
|
|
393
|
+
const env = deps.env ?? process.env;
|
|
394
|
+
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || existsSync3(join6(configurationDirectory(deps), PROFILE_FILE));
|
|
395
|
+
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
396
|
+
throw new MachineAuthError(
|
|
397
|
+
"conflicting_credentials",
|
|
398
|
+
"Unset PRIM_TOKEN when using machine credentials."
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
if (configured) environmentCredentials(deps);
|
|
402
|
+
return configured;
|
|
403
|
+
}
|
|
404
|
+
function machineContext(deps = {}) {
|
|
405
|
+
if (!hasMachineConfiguration(deps)) return void 0;
|
|
406
|
+
const environment = environmentCredentials(deps);
|
|
407
|
+
if (environment) {
|
|
408
|
+
if (environmentTerminal === environment.generation) return void 0;
|
|
409
|
+
return cached?.generation === environment.generation && cached.expiresAt > Date.now() ? cached : readEnvironmentContext(environment, deps);
|
|
410
|
+
}
|
|
411
|
+
const profile = readMachineProfile(deps);
|
|
412
|
+
if (profile && profile.apiUrl !== apiUrlFor(deps))
|
|
413
|
+
throw new MachineAuthError(
|
|
414
|
+
"deployment_mismatch",
|
|
415
|
+
"Machine credentials belong to another API deployment. Configure this deployment or inject its credentials."
|
|
416
|
+
);
|
|
417
|
+
return profile;
|
|
418
|
+
}
|
|
419
|
+
function cachedMachineCredential(deps = {}) {
|
|
420
|
+
const context = machineContext(deps);
|
|
421
|
+
return context && cached?.generation === context.generation && cached.apiUrl === context.apiUrl ? cached : void 0;
|
|
422
|
+
}
|
|
423
|
+
var environmentTerminal;
|
|
424
|
+
|
|
141
425
|
// src/lib/credentials.ts
|
|
142
426
|
var CONFIG_DIRECTORY = primConfigDirectory();
|
|
143
|
-
var TOKEN_FILE_PATH =
|
|
144
|
-
var REFRESH_TOKEN_PATH =
|
|
145
|
-
var TOKEN_EXPIRES_PATH =
|
|
146
|
-
var TERMINAL_REFRESH_PATH =
|
|
147
|
-
var CREDENTIAL_METADATA_PATH =
|
|
148
|
-
var CREDENTIAL_FAMILY_PATH =
|
|
149
|
-
var CREDENTIAL_MIGRATION_PATH =
|
|
150
|
-
var CREDENTIAL_LOCK_PATH =
|
|
427
|
+
var TOKEN_FILE_PATH = join7(CONFIG_DIRECTORY, "token");
|
|
428
|
+
var REFRESH_TOKEN_PATH = join7(CONFIG_DIRECTORY, "refresh_token");
|
|
429
|
+
var TOKEN_EXPIRES_PATH = join7(CONFIG_DIRECTORY, "token_expires_at");
|
|
430
|
+
var TERMINAL_REFRESH_PATH = join7(CONFIG_DIRECTORY, "refresh_terminal");
|
|
431
|
+
var CREDENTIAL_METADATA_PATH = join7(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
432
|
+
var CREDENTIAL_FAMILY_PATH = join7(CONFIG_DIRECTORY, "credential_family.json");
|
|
433
|
+
var CREDENTIAL_MIGRATION_PATH = join7(CONFIG_DIRECTORY, "credential_migration.json");
|
|
434
|
+
var CREDENTIAL_LOCK_PATH = join7(CONFIG_DIRECTORY, "credentials.lock");
|
|
151
435
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
152
436
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
153
437
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
154
438
|
function readTrimmed(path) {
|
|
155
439
|
try {
|
|
156
|
-
const value =
|
|
440
|
+
const value = readFileSync6(path, "utf8").trim();
|
|
157
441
|
return value || void 0;
|
|
158
442
|
} catch {
|
|
159
443
|
return void 0;
|
|
160
444
|
}
|
|
161
445
|
}
|
|
162
446
|
function resolveAuthCredential(options = {}) {
|
|
447
|
+
if (!options.tokenFilePath && hasMachineConfiguration({ env: options.env })) {
|
|
448
|
+
const machine = cachedMachineCredential({ env: options.env });
|
|
449
|
+
return machine ? { token: machine.accessToken, source: "machine" } : void 0;
|
|
450
|
+
}
|
|
163
451
|
const envToken = (options.env ?? process.env).PRIM_TOKEN?.trim();
|
|
164
452
|
if (envToken) return { token: envToken, source: "environment" };
|
|
165
453
|
const stored = readTrimmed(options.tokenFilePath ?? TOKEN_FILE_PATH);
|
|
@@ -186,7 +474,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
186
474
|
return { token: stored, source: "token_file" };
|
|
187
475
|
}
|
|
188
476
|
function credentialFingerprint(value) {
|
|
189
|
-
return
|
|
477
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
190
478
|
}
|
|
191
479
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
192
480
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -206,7 +494,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
206
494
|
function readBoundedCredentialJson(path) {
|
|
207
495
|
let raw;
|
|
208
496
|
try {
|
|
209
|
-
raw =
|
|
497
|
+
raw = readFileSync6(path, "utf8");
|
|
210
498
|
} catch (error) {
|
|
211
499
|
return error.code === "ENOENT" ? void 0 : null;
|
|
212
500
|
}
|
|
@@ -273,64 +561,14 @@ function decodeJwtPayload(token) {
|
|
|
273
561
|
}
|
|
274
562
|
}
|
|
275
563
|
|
|
276
|
-
// src/lib/file-lock.ts
|
|
277
|
-
import { randomBytes } from "crypto";
|
|
278
|
-
import { existsSync, mkdirSync as mkdirSync2, readFileSync as readFileSync2, rmSync as rmSync2, statSync, writeFileSync as writeFileSync2 } from "fs";
|
|
279
|
-
import { dirname as dirname2, join as join3 } from "path";
|
|
280
|
-
var syncSleeper = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
|
|
281
|
-
|
|
282
564
|
// src/daemon/client-instance-id.ts
|
|
283
|
-
import { randomBytes as
|
|
284
|
-
import { chmodSync, lstatSync, readFileSync as
|
|
285
|
-
import { join as
|
|
565
|
+
import { randomBytes as randomBytes3 } from "crypto";
|
|
566
|
+
import { chmodSync as chmodSync2, lstatSync as lstatSync4, readFileSync as readFileSync7 } from "fs";
|
|
567
|
+
import { join as join8 } from "path";
|
|
286
568
|
var INSTANCE_BODY_CHARS = 43;
|
|
287
569
|
var INSTANCE_PREFIX = "pci_";
|
|
288
570
|
var MAX_STORED_BYTES = INSTANCE_PREFIX.length + INSTANCE_BODY_CHARS + 1;
|
|
289
571
|
|
|
290
|
-
// src/lib/bin-path.ts
|
|
291
|
-
import { existsSync as existsSync2, readFileSync as readFileSync4 } from "fs";
|
|
292
|
-
import { dirname as dirname3, isAbsolute as isAbsolute2, join as join5 } from "path";
|
|
293
|
-
import { fileURLToPath } from "url";
|
|
294
|
-
var PKG_NAME = "@primitive.ai/prim";
|
|
295
|
-
var ROOT_WALK_LIMIT = 6;
|
|
296
|
-
var resolvedRoot;
|
|
297
|
-
function locateRoot() {
|
|
298
|
-
if (resolvedRoot !== void 0) {
|
|
299
|
-
return resolvedRoot;
|
|
300
|
-
}
|
|
301
|
-
let dir = dirname3(fileURLToPath(import.meta.url));
|
|
302
|
-
for (let depth = 0; depth < ROOT_WALK_LIMIT; depth++) {
|
|
303
|
-
const manifestPath = join5(dir, "package.json");
|
|
304
|
-
if (existsSync2(manifestPath)) {
|
|
305
|
-
try {
|
|
306
|
-
const manifest = JSON.parse(readFileSync4(manifestPath, "utf-8"));
|
|
307
|
-
if (manifest.name === PKG_NAME && manifest.bin) {
|
|
308
|
-
resolvedRoot = { dir, version: manifest.version, bin: manifest.bin };
|
|
309
|
-
return resolvedRoot;
|
|
310
|
-
}
|
|
311
|
-
} catch {
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
const parent = dirname3(dir);
|
|
315
|
-
if (parent === dir) {
|
|
316
|
-
break;
|
|
317
|
-
}
|
|
318
|
-
dir = parent;
|
|
319
|
-
}
|
|
320
|
-
resolvedRoot = null;
|
|
321
|
-
return resolvedRoot;
|
|
322
|
-
}
|
|
323
|
-
function packageVersion() {
|
|
324
|
-
return locateRoot()?.version ?? null;
|
|
325
|
-
}
|
|
326
|
-
var PRIM_CONFIG_SHELL_RESOLVER = [
|
|
327
|
-
'prim_absolute() { case "$1" in [/]* ) ;; *) return 1 ;; esac; case "$1" in [[:space:]]*|*[[:space:]]|*[/][/]*|*[/].[/]*|*[/]..[/]*|*[/].|*[/]..|?*[/]) return 1 ;; esac; }; ',
|
|
328
|
-
'prim_config=${PRIM_CONFIG_DIR:-}; if ! prim_absolute "$prim_config"; then prim_config=${XDG_CONFIG_HOME:-}; if prim_absolute "$prim_config"; then ',
|
|
329
|
-
'case "$prim_config" in /) prim_config=/prim ;; *) prim_config="$prim_config/prim" ;; esac; ',
|
|
330
|
-
'else prim_home=${HOME:-}; prim_absolute "$prim_home" || exit 78; case "$prim_home" in /) prim_config=/.config/prim ;; *) prim_config="$prim_home/.config/prim" ;; esac; ',
|
|
331
|
-
"fi; fi; "
|
|
332
|
-
].join("");
|
|
333
|
-
|
|
334
572
|
// src/lib/ingest-uploader.ts
|
|
335
573
|
var runtimeVersion = process.env.PRIM_RUNTIME_VERSION ?? packageVersion();
|
|
336
574
|
|
|
@@ -374,25 +612,33 @@ function terminalSafeText(value) {
|
|
|
374
612
|
var stripControlChars = terminalSafeText;
|
|
375
613
|
|
|
376
614
|
// src/client.ts
|
|
377
|
-
var
|
|
615
|
+
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
378
616
|
function getSiteUrl() {
|
|
379
617
|
return getSiteUrlForEnvironment(process.env.PRIM_API_URL);
|
|
380
618
|
}
|
|
381
619
|
function getSiteUrlForEnvironment(primApiUrl) {
|
|
382
|
-
return primApiUrl?.trim() ||
|
|
620
|
+
return primApiUrl?.trim() || DEFAULT_API_URL2;
|
|
383
621
|
}
|
|
384
622
|
var CLI_MANAGEMENT_RESPONSE_MAX_BYTES = 2 * 1024 * 1024;
|
|
385
623
|
|
|
386
624
|
// src/daemon/principal.ts
|
|
387
|
-
import { createHash as
|
|
625
|
+
import { createHash as createHash6 } from "crypto";
|
|
388
626
|
var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
389
627
|
function boundedClaim(value) {
|
|
390
628
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
391
629
|
}
|
|
392
630
|
function sha256(value) {
|
|
393
|
-
return
|
|
631
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
394
632
|
}
|
|
395
633
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
634
|
+
if (hasMachineConfiguration()) {
|
|
635
|
+
const machine = machineContext();
|
|
636
|
+
return machine ? {
|
|
637
|
+
principalId: sha256(`${machine.issuer}\0${machine.clientId}`),
|
|
638
|
+
organizationId: machine.workosOrganizationId,
|
|
639
|
+
credentialFingerprint: sha256(machine.generation)
|
|
640
|
+
} : void 0;
|
|
641
|
+
}
|
|
396
642
|
if (!token) return void 0;
|
|
397
643
|
const payload = decodeJwtPayload(token);
|
|
398
644
|
const subject = boundedClaim(payload?.sub);
|
|
@@ -431,8 +677,8 @@ function parseDaemonResponseEnvelope(value) {
|
|
|
431
677
|
|
|
432
678
|
// src/daemon/client.ts
|
|
433
679
|
import { createConnection } from "net";
|
|
434
|
-
import { join as
|
|
435
|
-
var SOCK_PATH =
|
|
680
|
+
import { join as join10 } from "path";
|
|
681
|
+
var SOCK_PATH = join10(primConfigDirectory(), "sock");
|
|
436
682
|
var DEFAULT_TIMEOUT_MS = 250;
|
|
437
683
|
var nextRequestId = 1;
|
|
438
684
|
function daemonRequest(method, params = {}, opts = {}) {
|
|
@@ -522,19 +768,19 @@ function daemonRequest(method, params = {}, opts = {}) {
|
|
|
522
768
|
}
|
|
523
769
|
|
|
524
770
|
// src/lib/activation.ts
|
|
525
|
-
import { execFileSync as
|
|
526
|
-
import { existsSync as
|
|
771
|
+
import { execFileSync as execFileSync4 } from "child_process";
|
|
772
|
+
import { existsSync as existsSync5, readFileSync as readFileSync9 } from "fs";
|
|
527
773
|
import { homedir as homedir2 } from "os";
|
|
528
|
-
import { join as
|
|
774
|
+
import { join as join11 } from "path";
|
|
529
775
|
|
|
530
776
|
// src/lib/demo-pause.ts
|
|
531
|
-
import { execFileSync } from "child_process";
|
|
777
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
532
778
|
var DEMO_PAUSE_MS = 30 * 6e4;
|
|
533
779
|
var PAUSE_KEY = "prim.demoPause";
|
|
534
780
|
var GIT_TIMEOUT_MS = 1e3;
|
|
535
781
|
function git(cwd, args) {
|
|
536
782
|
try {
|
|
537
|
-
return
|
|
783
|
+
return execFileSync2("git", args, {
|
|
538
784
|
cwd,
|
|
539
785
|
encoding: "utf-8",
|
|
540
786
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -546,7 +792,7 @@ function git(cwd, args) {
|
|
|
546
792
|
}
|
|
547
793
|
function setLocal(cwd, key, value) {
|
|
548
794
|
try {
|
|
549
|
-
|
|
795
|
+
execFileSync2(
|
|
550
796
|
"git",
|
|
551
797
|
value === null ? ["config", "--local", "--unset", key] : ["config", "--local", key, value],
|
|
552
798
|
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout: GIT_TIMEOUT_MS }
|
|
@@ -589,16 +835,16 @@ function healDemoPause(cwd, pause, options = {}) {
|
|
|
589
835
|
}
|
|
590
836
|
|
|
591
837
|
// src/lib/git.ts
|
|
592
|
-
import { execFileSync as
|
|
593
|
-
import { createHash as
|
|
594
|
-
import { lstatSync as
|
|
595
|
-
import { basename, dirname as
|
|
838
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
839
|
+
import { createHash as createHash7 } from "crypto";
|
|
840
|
+
import { lstatSync as lstatSync5, realpathSync } from "fs";
|
|
841
|
+
import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute3, relative, resolve as resolve2, sep } from "path";
|
|
596
842
|
var GIT_TIMEOUT_MS2 = 1e3;
|
|
597
843
|
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
598
844
|
var GITHUB_REPOSITORY_PATTERN = /^[A-Za-z0-9._-]{1,100}$/u;
|
|
599
845
|
function gitToplevel(cwd) {
|
|
600
846
|
try {
|
|
601
|
-
return
|
|
847
|
+
return execFileSync3("git", ["rev-parse", "--show-toplevel"], {
|
|
602
848
|
cwd,
|
|
603
849
|
encoding: "utf-8",
|
|
604
850
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -610,7 +856,7 @@ function gitToplevel(cwd) {
|
|
|
610
856
|
}
|
|
611
857
|
function gitValue(cwd, args) {
|
|
612
858
|
try {
|
|
613
|
-
const value =
|
|
859
|
+
const value = execFileSync3("git", args, {
|
|
614
860
|
cwd,
|
|
615
861
|
encoding: "utf-8",
|
|
616
862
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -647,7 +893,7 @@ var PROJECT_INSTALL_FILES = [".claude/settings.json", ".codex/hooks.json", ".cur
|
|
|
647
893
|
var PRIM_HOOK_BINS = ["prim-hook", "prim-pre-tool-use", "prim-post-tool-use"];
|
|
648
894
|
function repoActiveFlag(cwd, strict = false) {
|
|
649
895
|
try {
|
|
650
|
-
const value =
|
|
896
|
+
const value = execFileSync4("git", ["config", "--get", PRIM_ACTIVE_KEY], {
|
|
651
897
|
cwd,
|
|
652
898
|
encoding: "utf-8",
|
|
653
899
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -668,7 +914,7 @@ function isValidRepoSyncId(value) {
|
|
|
668
914
|
}
|
|
669
915
|
function localGitConfigValue(cwd, key) {
|
|
670
916
|
try {
|
|
671
|
-
const value =
|
|
917
|
+
const value = execFileSync4("git", ["config", "--local", "--get", key], {
|
|
672
918
|
cwd,
|
|
673
919
|
encoding: "utf-8",
|
|
674
920
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -698,10 +944,10 @@ function hasProjectPrimInstall(cwd) {
|
|
|
698
944
|
const root = gitToplevel(cwd);
|
|
699
945
|
if (root === null || root === homedir2()) return false;
|
|
700
946
|
for (const rel of PROJECT_INSTALL_FILES) {
|
|
701
|
-
const path =
|
|
702
|
-
if (
|
|
947
|
+
const path = join11(root, rel);
|
|
948
|
+
if (existsSync5(path)) {
|
|
703
949
|
try {
|
|
704
|
-
const content =
|
|
950
|
+
const content = readFileSync9(path, "utf-8");
|
|
705
951
|
if (PRIM_HOOK_BINS.some((bin) => content.includes(bin))) return true;
|
|
706
952
|
} catch {
|
|
707
953
|
}
|
|
@@ -802,7 +1048,7 @@ function formatStatusline(version, snapshot, resolveIngestionStatus, options = {
|
|
|
802
1048
|
const ingestionSuffix = (status) => status === void 0 ? "" : ` \xB7 Decision ingestion ${status}`;
|
|
803
1049
|
const ingestionStatus = snapshot === null || snapshot.healthy === false ? options.includeIngestionWhenUnavailable ? resolveIngestionStatus() : void 0 : resolveIngestionStatus();
|
|
804
1050
|
if (snapshot?.healthy === false && snapshot.needsReauth) {
|
|
805
|
-
return `primitive ${version} (daemon: paused \xB7 run
|
|
1051
|
+
return `primitive ${version} (daemon: paused \xB7 run \`${hasMachineConfiguration() ? "prim auth status" : "prim auth login"}\`${ingestionSuffix(ingestionStatus)})`;
|
|
806
1052
|
}
|
|
807
1053
|
const repositoryLabel = repositoryBindingDiagnosticLabel(
|
|
808
1054
|
snapshot !== null && snapshot.healthy !== false || options.includeIngestionWhenUnavailable === true ? options.resolveRepositoryBindingState?.() : void 0
|
|
@@ -849,7 +1095,7 @@ function formatStatusline(version, snapshot, resolveIngestionStatus, options = {
|
|
|
849
1095
|
var STATUSLINE_TIMEOUT_MS = 200;
|
|
850
1096
|
function readPackageVersion() {
|
|
851
1097
|
try {
|
|
852
|
-
const here =
|
|
1098
|
+
const here = dirname7(fileURLToPath4(import.meta.url));
|
|
853
1099
|
const candidates = [
|
|
854
1100
|
// Schema-v2 runtimes staged a manifest beside this standalone bundle.
|
|
855
1101
|
// Keep resolving it while those legacy launchers transition to schema v3.
|
|
@@ -859,7 +1105,7 @@ function readPackageVersion() {
|
|
|
859
1105
|
];
|
|
860
1106
|
for (const path of candidates) {
|
|
861
1107
|
try {
|
|
862
|
-
const pkg = JSON.parse(
|
|
1108
|
+
const pkg = JSON.parse(readFileSync10(path, "utf-8"));
|
|
863
1109
|
if (pkg.version) {
|
|
864
1110
|
return pkg.version;
|
|
865
1111
|
}
|