@primitive.ai/prim 0.1.0-alpha.123 → 0.1.0-alpha.125
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 +90 -0
- package/dist/{chunk-RUEIVZ6T.js → chunk-2BEUR3DU.js} +2 -2
- package/dist/{chunk-5ME3WD3P.js → chunk-6KXQQ7CQ.js} +6 -3
- package/dist/{chunk-CWV5ZQR6.js → chunk-6OPSSJHO.js} +5 -5
- package/dist/{chunk-4NUGCUQC.js → chunk-74DQDWLW.js} +2 -2
- package/dist/{chunk-WN7OURQ3.js → chunk-BFYJLI7V.js} +1 -1
- package/dist/{chunk-T2T45KF6.js → chunk-DT7E3SD5.js} +4 -4
- package/dist/{chunk-DQ4I3FLF.js → chunk-EZXIKEMA.js} +3 -3
- package/dist/{chunk-4M23K2KZ.js → chunk-FYYHBOOM.js} +2 -2
- package/dist/{chunk-3WQ7SBOZ.js → chunk-HPA7RGSZ.js} +3 -3
- package/dist/{chunk-NL5VU7EU.js → chunk-JFWJN2ME.js} +3 -3
- package/dist/{chunk-GOMEWFMG.js → chunk-L32WYFJT.js} +143 -61
- package/dist/{chunk-OXW6PSPT.js → chunk-NBNQI3YJ.js} +15 -3
- package/dist/{chunk-LOFMI3D4.js → chunk-ND6TYHNI.js} +2 -2
- package/dist/{chunk-4GYJHN26.js → chunk-NJBN3WXF.js} +1607 -549
- package/dist/{chunk-HLMC7VS4.js → chunk-O73J3KQQ.js} +5702 -2860
- package/dist/{chunk-JZC2LHS6.js → chunk-PPZ56EVJ.js} +1 -1
- package/dist/{chunk-FXXMDO7D.js → chunk-R35M4RCY.js} +4 -4
- package/dist/chunk-TI37ME2D.js +210 -0
- package/dist/{chunk-NMZSCSMY.js → chunk-YP4OXKNX.js} +13 -1
- package/dist/{chunk-S3ZIMGXV.js → chunk-ZKXNJB6U.js} +76 -2
- package/dist/daemon/server.js +12321 -8354
- package/dist/hook-runtime/daemon/server.js +12321 -8354
- package/dist/hook-runtime/hooks/post-commit.js +6743 -3634
- package/dist/hook-runtime/hooks/post-rewrite.js +6776 -3668
- package/dist/hook-runtime/hooks/post-tool-use.js +7108 -3140
- package/dist/hook-runtime/hooks/pre-commit.js +1263 -280
- package/dist/hook-runtime/hooks/pre-tool-use-worker.js +15115 -11297
- package/dist/hook-runtime/hooks/prim-hook.js +10879 -7024
- package/dist/hook-runtime/hooks/session-end.js +399 -139
- package/dist/hook-runtime/hooks/session-start.js +13277 -9454
- package/dist/hook-runtime/index.js +17575 -13050
- package/dist/hook-runtime/statusline-main.js +375 -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 +9 -9
- 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 +9124 -8763
- package/dist/statusline-main.js +375 -122
- package/package.json +3 -1
- 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,283 @@ 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 {
|
|
219
|
+
issuer: record.issuer,
|
|
220
|
+
audience: record.audience,
|
|
221
|
+
...record.developer_enrollment === true ? { developerEnrollment: true } : {}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function parseMachineIdentity(value, clientId) {
|
|
225
|
+
const data = machineRecord(value);
|
|
226
|
+
const principal = machineRecord(data.principal);
|
|
227
|
+
if (data.authenticated !== true || data.captureAuthorityKind !== "workos_m2m" || ![2, 3].includes(data.organizationBindingVersion) || (data.organizationBindingVersion === 3 ? !machineString(principal.authorizedUserId, 256) : principal.authorizedUserId !== void 0) || 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))) {
|
|
228
|
+
throw new MachineAuthError(
|
|
229
|
+
"machine_identity_rejected",
|
|
230
|
+
"The API did not verify a machine identity.",
|
|
231
|
+
true
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
machineId: principal.machineId,
|
|
236
|
+
...data.organizationBindingVersion === 3 ? { authorizedUserId: principal.authorizedUserId } : {},
|
|
237
|
+
clientId,
|
|
238
|
+
displayName: principal.displayName,
|
|
239
|
+
organizationId: data.organizationId,
|
|
240
|
+
workosOrganizationId: data.workosOrganizationId,
|
|
241
|
+
scopes: principal.scopes
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// src/lib/paths.ts
|
|
246
|
+
import { homedir } from "os";
|
|
247
|
+
import { isAbsolute as isAbsolute2, join as join3, normalize } from "path";
|
|
248
|
+
var UNSAFE_CONFIG_PATH_CHARACTERS = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
249
|
+
function absolutePath(value, variable) {
|
|
250
|
+
if (value !== void 0 && UNSAFE_CONFIG_PATH_CHARACTERS.test(value)) {
|
|
251
|
+
throw new Error(`${variable} contains unsafe characters`);
|
|
252
|
+
}
|
|
253
|
+
if (!value || value.trim() !== value || !isAbsolute2(value)) return;
|
|
254
|
+
const normalized = normalize(value);
|
|
255
|
+
return normalized === value ? value : void 0;
|
|
256
|
+
}
|
|
257
|
+
function resolvePrimConfigDirectory(options = {}) {
|
|
258
|
+
const env = options.env ?? process.env;
|
|
259
|
+
const explicit = absolutePath(env.PRIM_CONFIG_DIR, "PRIM_CONFIG_DIR");
|
|
260
|
+
if (explicit) return { path: explicit, source: "explicit" };
|
|
261
|
+
const xdgConfigHome = absolutePath(env.XDG_CONFIG_HOME, "XDG_CONFIG_HOME");
|
|
262
|
+
if (xdgConfigHome) return { path: join3(xdgConfigHome, "prim"), source: "xdg" };
|
|
263
|
+
const home = absolutePath(options.homeDir ?? homedir(), "HOME");
|
|
264
|
+
if (!home) {
|
|
265
|
+
throw new Error("cannot resolve Primitive config directory: HOME is not an absolute path");
|
|
266
|
+
}
|
|
267
|
+
return { path: join3(home, ".config", "prim"), source: "default" };
|
|
268
|
+
}
|
|
269
|
+
function primConfigDirectory(options = {}) {
|
|
270
|
+
return resolvePrimConfigDirectory(options).path;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/lib/machine-broker.ts
|
|
274
|
+
var MAX_RESPONSE_BYTES = 96 * 1024;
|
|
275
|
+
var IDLE_MS = 5 * 6e4;
|
|
276
|
+
|
|
277
|
+
// src/lib/machine-retry.ts
|
|
278
|
+
import { createHash as createHash2 } from "crypto";
|
|
279
|
+
import { lstatSync as lstatSync2, readFileSync as readFileSync4, rmSync as rmSync4 } from "fs";
|
|
280
|
+
import { join as join5 } from "path";
|
|
281
|
+
|
|
282
|
+
// src/lib/machine-credentials.ts
|
|
283
|
+
var require2 = createRequire(import.meta.url);
|
|
284
|
+
var DEFAULT_API_URL = "https://api.getprimitive.ai";
|
|
285
|
+
var PROFILE_FILE = "machine_profile.json";
|
|
286
|
+
var cached;
|
|
287
|
+
function configurationDirectory(deps) {
|
|
288
|
+
return deps.directory ?? primConfigDirectory({ env: deps.env });
|
|
289
|
+
}
|
|
290
|
+
function apiUrlFor(deps) {
|
|
291
|
+
return machineApiUrl((deps.env ?? process.env).PRIM_API_URL?.trim() || DEFAULT_API_URL);
|
|
292
|
+
}
|
|
293
|
+
function hash(value) {
|
|
294
|
+
return createHash3("sha256").update(value).digest("hex");
|
|
295
|
+
}
|
|
296
|
+
function environmentContextPath(deps, clientId) {
|
|
297
|
+
return join6(
|
|
298
|
+
configurationDirectory(deps),
|
|
299
|
+
`machine_context_${hash(`${apiUrlFor(deps)}\0${clientId}`)}.json`
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
function readEnvironmentContext(environment, deps) {
|
|
303
|
+
try {
|
|
304
|
+
const data = machineRecord(readPrivate(environmentContextPath(deps, environment.clientId)));
|
|
305
|
+
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())
|
|
306
|
+
return void 0;
|
|
307
|
+
const configuration = parseMachineConfiguration({
|
|
308
|
+
protocol_version: data.version,
|
|
309
|
+
issuer: data.issuer,
|
|
310
|
+
audience: data.audience
|
|
311
|
+
});
|
|
312
|
+
const identity = parseStoredIdentity(data);
|
|
313
|
+
return {
|
|
314
|
+
...configuration,
|
|
315
|
+
...identity,
|
|
316
|
+
apiUrl: data.apiUrl,
|
|
317
|
+
generation: environment.generation
|
|
318
|
+
};
|
|
319
|
+
} catch {
|
|
320
|
+
return void 0;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
function readPrivate(path) {
|
|
324
|
+
let raw;
|
|
325
|
+
try {
|
|
326
|
+
const stat = lstatSync3(path);
|
|
327
|
+
if (!stat.isFile() || stat.size > 96 * 1024 || process.platform !== "win32" && ((stat.mode & 63) !== 0 || stat.uid !== process.getuid?.())) {
|
|
328
|
+
throw new MachineAuthError(
|
|
329
|
+
"unsafe_storage",
|
|
330
|
+
"Machine credential files must be owner-only regular files."
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
raw = readFileSync5(path, "utf8");
|
|
334
|
+
} catch (error) {
|
|
335
|
+
if (error.code === "ENOENT") return void 0;
|
|
336
|
+
throw error;
|
|
337
|
+
}
|
|
338
|
+
try {
|
|
339
|
+
return JSON.parse(raw);
|
|
340
|
+
} catch {
|
|
341
|
+
throw new MachineAuthError("invalid_storage", "Stored machine credentials are malformed.");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function parseStoredIdentity(data) {
|
|
345
|
+
return parseMachineIdentity(
|
|
346
|
+
{
|
|
347
|
+
authenticated: true,
|
|
348
|
+
organizationBindingVersion: data.authorizedUserId ? 3 : 2,
|
|
349
|
+
captureAuthorityKind: "workos_m2m",
|
|
350
|
+
organizationId: data.organizationId,
|
|
351
|
+
workosOrganizationId: data.workosOrganizationId,
|
|
352
|
+
principal: { ...data, kind: "machine" }
|
|
353
|
+
},
|
|
354
|
+
String(data.clientId)
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
function parseProfile(value) {
|
|
358
|
+
const data = machineRecord(value);
|
|
359
|
+
const config = parseMachineConfiguration({
|
|
360
|
+
protocol_version: data.version,
|
|
361
|
+
issuer: data.issuer,
|
|
362
|
+
audience: data.audience
|
|
363
|
+
});
|
|
364
|
+
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)) {
|
|
365
|
+
throw new MachineAuthError("invalid_storage", "Stored machine profile is malformed.");
|
|
366
|
+
}
|
|
367
|
+
const identity = parseStoredIdentity(data);
|
|
368
|
+
return {
|
|
369
|
+
...config,
|
|
370
|
+
...identity,
|
|
371
|
+
version: 1,
|
|
372
|
+
apiUrl: data.apiUrl,
|
|
373
|
+
generation: data.generation,
|
|
374
|
+
storage: data.storage
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
function readMachineProfile(deps = {}) {
|
|
378
|
+
const value = readPrivate(join6(configurationDirectory(deps), PROFILE_FILE));
|
|
379
|
+
return value === void 0 ? void 0 : parseProfile(value);
|
|
380
|
+
}
|
|
381
|
+
function environmentCredentials(deps) {
|
|
382
|
+
const env = deps.env ?? process.env;
|
|
383
|
+
const { PRIM_M2M_CLIENT_ID: clientId, PRIM_M2M_CLIENT_SECRET: clientSecret } = env;
|
|
384
|
+
if (clientId === void 0 && clientSecret === void 0) return void 0;
|
|
385
|
+
if (!clientId || !clientSecret || !/^client_[\w-]+$/u.test(clientId) || !machineString(clientSecret)) {
|
|
386
|
+
throw new MachineAuthError(
|
|
387
|
+
"incomplete_machine_credentials",
|
|
388
|
+
"Set both PRIM_M2M_CLIENT_ID and PRIM_M2M_CLIENT_SECRET."
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
clientId,
|
|
393
|
+
clientSecret,
|
|
394
|
+
generation: hash(`${apiUrlFor(deps)}\0${clientId}\0${clientSecret}`)
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
function hasMachineConfiguration(deps = {}) {
|
|
398
|
+
const env = deps.env ?? process.env;
|
|
399
|
+
const configured = env.PRIM_M2M_CLIENT_ID !== void 0 || env.PRIM_M2M_CLIENT_SECRET !== void 0 || existsSync3(join6(configurationDirectory(deps), PROFILE_FILE));
|
|
400
|
+
if (configured && env.PRIM_TOKEN?.trim()) {
|
|
401
|
+
throw new MachineAuthError(
|
|
402
|
+
"conflicting_credentials",
|
|
403
|
+
"Unset PRIM_TOKEN when using machine credentials."
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
if (configured) environmentCredentials(deps);
|
|
407
|
+
return configured;
|
|
408
|
+
}
|
|
409
|
+
function machineContext(deps = {}) {
|
|
410
|
+
if (!hasMachineConfiguration(deps)) return void 0;
|
|
411
|
+
const environment = environmentCredentials(deps);
|
|
412
|
+
if (environment) {
|
|
413
|
+
if (environmentTerminal === environment.generation) return void 0;
|
|
414
|
+
return cached?.generation === environment.generation && cached.expiresAt > Date.now() ? cached : readEnvironmentContext(environment, deps);
|
|
415
|
+
}
|
|
416
|
+
const profile = readMachineProfile(deps);
|
|
417
|
+
if (profile && profile.apiUrl !== apiUrlFor(deps))
|
|
418
|
+
throw new MachineAuthError(
|
|
419
|
+
"deployment_mismatch",
|
|
420
|
+
"Machine credentials belong to another API deployment. Configure this deployment or inject its credentials."
|
|
421
|
+
);
|
|
422
|
+
return profile;
|
|
423
|
+
}
|
|
424
|
+
function cachedMachineCredential(deps = {}) {
|
|
425
|
+
const context = machineContext(deps);
|
|
426
|
+
return context && cached?.generation === context.generation && cached.apiUrl === context.apiUrl ? cached : void 0;
|
|
427
|
+
}
|
|
428
|
+
var environmentTerminal;
|
|
429
|
+
|
|
141
430
|
// src/lib/credentials.ts
|
|
142
431
|
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 =
|
|
432
|
+
var TOKEN_FILE_PATH = join7(CONFIG_DIRECTORY, "token");
|
|
433
|
+
var REFRESH_TOKEN_PATH = join7(CONFIG_DIRECTORY, "refresh_token");
|
|
434
|
+
var TOKEN_EXPIRES_PATH = join7(CONFIG_DIRECTORY, "token_expires_at");
|
|
435
|
+
var TERMINAL_REFRESH_PATH = join7(CONFIG_DIRECTORY, "refresh_terminal");
|
|
436
|
+
var CREDENTIAL_METADATA_PATH = join7(CONFIG_DIRECTORY, "credential_metadata.json");
|
|
437
|
+
var CREDENTIAL_FAMILY_PATH = join7(CONFIG_DIRECTORY, "credential_family.json");
|
|
438
|
+
var CREDENTIAL_MIGRATION_PATH = join7(CONFIG_DIRECTORY, "credential_migration.json");
|
|
439
|
+
var CREDENTIAL_LOCK_PATH = join7(CONFIG_DIRECTORY, "credentials.lock");
|
|
151
440
|
var MAX_CREDENTIAL_METADATA_BYTES = 4096;
|
|
152
441
|
var CREDENTIAL_MIGRATION_VERSION = 1;
|
|
153
442
|
var CREDENTIAL_MIGRATION_STATE = "family_bound";
|
|
154
443
|
function readTrimmed(path) {
|
|
155
444
|
try {
|
|
156
|
-
const value =
|
|
445
|
+
const value = readFileSync6(path, "utf8").trim();
|
|
157
446
|
return value || void 0;
|
|
158
447
|
} catch {
|
|
159
448
|
return void 0;
|
|
160
449
|
}
|
|
161
450
|
}
|
|
162
451
|
function resolveAuthCredential(options = {}) {
|
|
452
|
+
if (!options.tokenFilePath && hasMachineConfiguration({ env: options.env })) {
|
|
453
|
+
const machine = cachedMachineCredential({ env: options.env });
|
|
454
|
+
return machine ? { token: machine.accessToken, source: "machine" } : void 0;
|
|
455
|
+
}
|
|
163
456
|
const envToken = (options.env ?? process.env).PRIM_TOKEN?.trim();
|
|
164
457
|
if (envToken) return { token: envToken, source: "environment" };
|
|
165
458
|
const stored = readTrimmed(options.tokenFilePath ?? TOKEN_FILE_PATH);
|
|
@@ -186,7 +479,7 @@ function resolveAuthCredential(options = {}) {
|
|
|
186
479
|
return { token: stored, source: "token_file" };
|
|
187
480
|
}
|
|
188
481
|
function credentialFingerprint(value) {
|
|
189
|
-
return
|
|
482
|
+
return createHash4("sha256").update(value).digest("hex");
|
|
190
483
|
}
|
|
191
484
|
function parseLegacyBrokerCredentialMetadata(value) {
|
|
192
485
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
@@ -206,7 +499,7 @@ function parseLegacyBrokerCredentialMetadata(value) {
|
|
|
206
499
|
function readBoundedCredentialJson(path) {
|
|
207
500
|
let raw;
|
|
208
501
|
try {
|
|
209
|
-
raw =
|
|
502
|
+
raw = readFileSync6(path, "utf8");
|
|
210
503
|
} catch (error) {
|
|
211
504
|
return error.code === "ENOENT" ? void 0 : null;
|
|
212
505
|
}
|
|
@@ -273,64 +566,14 @@ function decodeJwtPayload(token) {
|
|
|
273
566
|
}
|
|
274
567
|
}
|
|
275
568
|
|
|
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
569
|
// src/daemon/client-instance-id.ts
|
|
283
|
-
import { randomBytes as
|
|
284
|
-
import { chmodSync, lstatSync, readFileSync as
|
|
285
|
-
import { join as
|
|
570
|
+
import { randomBytes as randomBytes3 } from "crypto";
|
|
571
|
+
import { chmodSync as chmodSync2, lstatSync as lstatSync4, readFileSync as readFileSync7 } from "fs";
|
|
572
|
+
import { join as join8 } from "path";
|
|
286
573
|
var INSTANCE_BODY_CHARS = 43;
|
|
287
574
|
var INSTANCE_PREFIX = "pci_";
|
|
288
575
|
var MAX_STORED_BYTES = INSTANCE_PREFIX.length + INSTANCE_BODY_CHARS + 1;
|
|
289
576
|
|
|
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
577
|
// src/lib/ingest-uploader.ts
|
|
335
578
|
var runtimeVersion = process.env.PRIM_RUNTIME_VERSION ?? packageVersion();
|
|
336
579
|
|
|
@@ -374,25 +617,35 @@ function terminalSafeText(value) {
|
|
|
374
617
|
var stripControlChars = terminalSafeText;
|
|
375
618
|
|
|
376
619
|
// src/client.ts
|
|
377
|
-
var
|
|
620
|
+
var DEFAULT_API_URL2 = "https://api.getprimitive.ai";
|
|
378
621
|
function getSiteUrl() {
|
|
379
622
|
return getSiteUrlForEnvironment(process.env.PRIM_API_URL);
|
|
380
623
|
}
|
|
381
624
|
function getSiteUrlForEnvironment(primApiUrl) {
|
|
382
|
-
return primApiUrl?.trim() ||
|
|
625
|
+
return primApiUrl?.trim() || DEFAULT_API_URL2;
|
|
383
626
|
}
|
|
384
627
|
var CLI_MANAGEMENT_RESPONSE_MAX_BYTES = 2 * 1024 * 1024;
|
|
385
628
|
|
|
386
629
|
// src/daemon/principal.ts
|
|
387
|
-
import { createHash as
|
|
630
|
+
import { createHash as createHash6 } from "crypto";
|
|
388
631
|
var PRINCIPAL_COMPONENT_MAX_LENGTH = 512;
|
|
389
632
|
function boundedClaim(value) {
|
|
390
633
|
return typeof value === "string" && value.length > 0 && value.length <= PRINCIPAL_COMPONENT_MAX_LENGTH ? value : void 0;
|
|
391
634
|
}
|
|
392
635
|
function sha256(value) {
|
|
393
|
-
return
|
|
636
|
+
return createHash6("sha256").update(value).digest("hex");
|
|
394
637
|
}
|
|
395
638
|
function resolveDaemonPrincipal(token = resolveAuthCredential()?.token) {
|
|
639
|
+
if (hasMachineConfiguration()) {
|
|
640
|
+
const machine = machineContext();
|
|
641
|
+
return machine ? {
|
|
642
|
+
principalId: sha256(
|
|
643
|
+
`${machine.issuer}\0${machine.clientId}${machine.authorizedUserId ? `\0${machine.authorizedUserId}` : ""}`
|
|
644
|
+
),
|
|
645
|
+
organizationId: machine.workosOrganizationId,
|
|
646
|
+
credentialFingerprint: sha256(machine.generation)
|
|
647
|
+
} : void 0;
|
|
648
|
+
}
|
|
396
649
|
if (!token) return void 0;
|
|
397
650
|
const payload = decodeJwtPayload(token);
|
|
398
651
|
const subject = boundedClaim(payload?.sub);
|
|
@@ -431,8 +684,8 @@ function parseDaemonResponseEnvelope(value) {
|
|
|
431
684
|
|
|
432
685
|
// src/daemon/client.ts
|
|
433
686
|
import { createConnection } from "net";
|
|
434
|
-
import { join as
|
|
435
|
-
var SOCK_PATH =
|
|
687
|
+
import { join as join10 } from "path";
|
|
688
|
+
var SOCK_PATH = join10(primConfigDirectory(), "sock");
|
|
436
689
|
var DEFAULT_TIMEOUT_MS = 250;
|
|
437
690
|
var nextRequestId = 1;
|
|
438
691
|
function daemonRequest(method, params = {}, opts = {}) {
|
|
@@ -522,19 +775,19 @@ function daemonRequest(method, params = {}, opts = {}) {
|
|
|
522
775
|
}
|
|
523
776
|
|
|
524
777
|
// src/lib/activation.ts
|
|
525
|
-
import { execFileSync as
|
|
526
|
-
import { existsSync as
|
|
778
|
+
import { execFileSync as execFileSync4 } from "child_process";
|
|
779
|
+
import { existsSync as existsSync5, readFileSync as readFileSync9 } from "fs";
|
|
527
780
|
import { homedir as homedir2 } from "os";
|
|
528
|
-
import { join as
|
|
781
|
+
import { join as join11 } from "path";
|
|
529
782
|
|
|
530
783
|
// src/lib/demo-pause.ts
|
|
531
|
-
import { execFileSync } from "child_process";
|
|
784
|
+
import { execFileSync as execFileSync2 } from "child_process";
|
|
532
785
|
var DEMO_PAUSE_MS = 30 * 6e4;
|
|
533
786
|
var PAUSE_KEY = "prim.demoPause";
|
|
534
787
|
var GIT_TIMEOUT_MS = 1e3;
|
|
535
788
|
function git(cwd, args) {
|
|
536
789
|
try {
|
|
537
|
-
return
|
|
790
|
+
return execFileSync2("git", args, {
|
|
538
791
|
cwd,
|
|
539
792
|
encoding: "utf-8",
|
|
540
793
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -546,7 +799,7 @@ function git(cwd, args) {
|
|
|
546
799
|
}
|
|
547
800
|
function setLocal(cwd, key, value) {
|
|
548
801
|
try {
|
|
549
|
-
|
|
802
|
+
execFileSync2(
|
|
550
803
|
"git",
|
|
551
804
|
value === null ? ["config", "--local", "--unset", key] : ["config", "--local", key, value],
|
|
552
805
|
{ cwd, stdio: ["ignore", "ignore", "pipe"], timeout: GIT_TIMEOUT_MS }
|
|
@@ -589,16 +842,16 @@ function healDemoPause(cwd, pause, options = {}) {
|
|
|
589
842
|
}
|
|
590
843
|
|
|
591
844
|
// src/lib/git.ts
|
|
592
|
-
import { execFileSync as
|
|
593
|
-
import { createHash as
|
|
594
|
-
import { lstatSync as
|
|
595
|
-
import { basename, dirname as
|
|
845
|
+
import { execFileSync as execFileSync3 } from "child_process";
|
|
846
|
+
import { createHash as createHash7 } from "crypto";
|
|
847
|
+
import { lstatSync as lstatSync5, realpathSync } from "fs";
|
|
848
|
+
import { basename as basename2, dirname as dirname6, isAbsolute as isAbsolute3, relative, resolve as resolve2, sep } from "path";
|
|
596
849
|
var GIT_TIMEOUT_MS2 = 1e3;
|
|
597
850
|
var GITHUB_OWNER_PATTERN = /^(?![A-Za-z0-9-]*--)[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/u;
|
|
598
851
|
var GITHUB_REPOSITORY_PATTERN = /^[A-Za-z0-9._-]{1,100}$/u;
|
|
599
852
|
function gitToplevel(cwd) {
|
|
600
853
|
try {
|
|
601
|
-
return
|
|
854
|
+
return execFileSync3("git", ["rev-parse", "--show-toplevel"], {
|
|
602
855
|
cwd,
|
|
603
856
|
encoding: "utf-8",
|
|
604
857
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -610,7 +863,7 @@ function gitToplevel(cwd) {
|
|
|
610
863
|
}
|
|
611
864
|
function gitValue(cwd, args) {
|
|
612
865
|
try {
|
|
613
|
-
const value =
|
|
866
|
+
const value = execFileSync3("git", args, {
|
|
614
867
|
cwd,
|
|
615
868
|
encoding: "utf-8",
|
|
616
869
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -647,7 +900,7 @@ var PROJECT_INSTALL_FILES = [".claude/settings.json", ".codex/hooks.json", ".cur
|
|
|
647
900
|
var PRIM_HOOK_BINS = ["prim-hook", "prim-pre-tool-use", "prim-post-tool-use"];
|
|
648
901
|
function repoActiveFlag(cwd, strict = false) {
|
|
649
902
|
try {
|
|
650
|
-
const value =
|
|
903
|
+
const value = execFileSync4("git", ["config", "--get", PRIM_ACTIVE_KEY], {
|
|
651
904
|
cwd,
|
|
652
905
|
encoding: "utf-8",
|
|
653
906
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -668,7 +921,7 @@ function isValidRepoSyncId(value) {
|
|
|
668
921
|
}
|
|
669
922
|
function localGitConfigValue(cwd, key) {
|
|
670
923
|
try {
|
|
671
|
-
const value =
|
|
924
|
+
const value = execFileSync4("git", ["config", "--local", "--get", key], {
|
|
672
925
|
cwd,
|
|
673
926
|
encoding: "utf-8",
|
|
674
927
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -698,10 +951,10 @@ function hasProjectPrimInstall(cwd) {
|
|
|
698
951
|
const root = gitToplevel(cwd);
|
|
699
952
|
if (root === null || root === homedir2()) return false;
|
|
700
953
|
for (const rel of PROJECT_INSTALL_FILES) {
|
|
701
|
-
const path =
|
|
702
|
-
if (
|
|
954
|
+
const path = join11(root, rel);
|
|
955
|
+
if (existsSync5(path)) {
|
|
703
956
|
try {
|
|
704
|
-
const content =
|
|
957
|
+
const content = readFileSync9(path, "utf-8");
|
|
705
958
|
if (PRIM_HOOK_BINS.some((bin) => content.includes(bin))) return true;
|
|
706
959
|
} catch {
|
|
707
960
|
}
|
|
@@ -802,7 +1055,7 @@ function formatStatusline(version, snapshot, resolveIngestionStatus, options = {
|
|
|
802
1055
|
const ingestionSuffix = (status) => status === void 0 ? "" : ` \xB7 Decision ingestion ${status}`;
|
|
803
1056
|
const ingestionStatus = snapshot === null || snapshot.healthy === false ? options.includeIngestionWhenUnavailable ? resolveIngestionStatus() : void 0 : resolveIngestionStatus();
|
|
804
1057
|
if (snapshot?.healthy === false && snapshot.needsReauth) {
|
|
805
|
-
return `primitive ${version} (daemon: paused \xB7 run
|
|
1058
|
+
return `primitive ${version} (daemon: paused \xB7 run \`${hasMachineConfiguration() ? "prim auth status" : "prim auth login"}\`${ingestionSuffix(ingestionStatus)})`;
|
|
806
1059
|
}
|
|
807
1060
|
const repositoryLabel = repositoryBindingDiagnosticLabel(
|
|
808
1061
|
snapshot !== null && snapshot.healthy !== false || options.includeIngestionWhenUnavailable === true ? options.resolveRepositoryBindingState?.() : void 0
|
|
@@ -849,7 +1102,7 @@ function formatStatusline(version, snapshot, resolveIngestionStatus, options = {
|
|
|
849
1102
|
var STATUSLINE_TIMEOUT_MS = 200;
|
|
850
1103
|
function readPackageVersion() {
|
|
851
1104
|
try {
|
|
852
|
-
const here =
|
|
1105
|
+
const here = dirname7(fileURLToPath4(import.meta.url));
|
|
853
1106
|
const candidates = [
|
|
854
1107
|
// Schema-v2 runtimes staged a manifest beside this standalone bundle.
|
|
855
1108
|
// Keep resolving it while those legacy launchers transition to schema v3.
|
|
@@ -859,7 +1112,7 @@ function readPackageVersion() {
|
|
|
859
1112
|
];
|
|
860
1113
|
for (const path of candidates) {
|
|
861
1114
|
try {
|
|
862
|
-
const pkg = JSON.parse(
|
|
1115
|
+
const pkg = JSON.parse(readFileSync10(path, "utf-8"));
|
|
863
1116
|
if (pkg.version) {
|
|
864
1117
|
return pkg.version;
|
|
865
1118
|
}
|