@hasna/hooks 0.12.23 → 0.13.1
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 +2 -1
- package/bin/hooks-mcp.js +1198 -228
- package/bin/index.js +1881 -695
- package/bin/native-safety-worker.cjs +1 -1
- package/bin/serve.js +4 -0
- package/dist/index.js +1332 -294
- package/dist/lib/installer.d.ts +44 -5
- package/dist/lib/manifest.d.ts +21 -0
- package/dist/lib/native-target.d.ts +11 -2
- package/dist/lib/registry.d.ts +12 -0
- package/dist/lib/target-adapters.d.ts +151 -0
- package/dist/lib/targets.d.ts +99 -0
- package/package.json +1 -1
package/bin/hooks-mcp.js
CHANGED
|
@@ -891,6 +891,8 @@ var init_manifest = __esm(() => {
|
|
|
891
891
|
script_kind: z.enum(["inline", "file"]).optional(),
|
|
892
892
|
args: z.array(z.string()).optional(),
|
|
893
893
|
nativeTargetDispatch: z.boolean().optional(),
|
|
894
|
+
requires_final_message: z.boolean().optional(),
|
|
895
|
+
requires_block: z.boolean().optional(),
|
|
894
896
|
timeout_ms: z.number().int().positive().optional(),
|
|
895
897
|
env: z.record(z.string(), z.string()).optional()
|
|
896
898
|
});
|
|
@@ -972,6 +974,8 @@ function resolveHookMeta(name) {
|
|
|
972
974
|
matcher: "",
|
|
973
975
|
tags: ["custom"],
|
|
974
976
|
nativeTargetDispatch: manifest.nativeTargetDispatch,
|
|
977
|
+
requiresFinalMessage: manifest.requires_final_message,
|
|
978
|
+
requiresBlock: manifest.requires_block,
|
|
975
979
|
timeoutSeconds: manifest.timeout_ms === undefined ? undefined : Math.ceil(manifest.timeout_ms / 1000)
|
|
976
980
|
};
|
|
977
981
|
}
|
|
@@ -1207,8 +1211,8 @@ var init_migrations = __esm(() => {
|
|
|
1207
1211
|
|
|
1208
1212
|
// src/db/legacy-import.ts
|
|
1209
1213
|
import { existsSync as existsSync6, readdirSync as readdirSync2, readFileSync as readFileSync4 } from "fs";
|
|
1210
|
-
import { join as
|
|
1211
|
-
import { homedir as
|
|
1214
|
+
import { join as join8 } from "path";
|
|
1215
|
+
import { homedir as homedir5 } from "os";
|
|
1212
1216
|
function ensureMetaTable(db) {
|
|
1213
1217
|
db.exec(`
|
|
1214
1218
|
CREATE TABLE IF NOT EXISTS _meta (
|
|
@@ -1282,25 +1286,25 @@ function importErrorsLog(db, filePath) {
|
|
|
1282
1286
|
} catch {}
|
|
1283
1287
|
return count;
|
|
1284
1288
|
}
|
|
1285
|
-
function runLegacyImport(db, homeDir =
|
|
1289
|
+
function runLegacyImport(db, homeDir = homedir5()) {
|
|
1286
1290
|
try {
|
|
1287
1291
|
if (isAlreadyDone(db))
|
|
1288
1292
|
return;
|
|
1289
1293
|
let total = 0;
|
|
1290
|
-
const claudeProjectsDir =
|
|
1294
|
+
const claudeProjectsDir = join8(homeDir, ".claude", "projects");
|
|
1291
1295
|
if (existsSync6(claudeProjectsDir)) {
|
|
1292
1296
|
try {
|
|
1293
1297
|
const projectDirs = readdirSync2(claudeProjectsDir);
|
|
1294
1298
|
for (const dir of projectDirs) {
|
|
1295
|
-
const projectDir =
|
|
1299
|
+
const projectDir = join8(claudeProjectsDir, dir);
|
|
1296
1300
|
try {
|
|
1297
1301
|
const files = readdirSync2(projectDir);
|
|
1298
1302
|
for (const file of files) {
|
|
1299
1303
|
if (file.match(/^session-log-\d{4}-\d{2}-\d{2}\.jsonl$/)) {
|
|
1300
|
-
total += importJsonlFile(db,
|
|
1304
|
+
total += importJsonlFile(db, join8(projectDir, file));
|
|
1301
1305
|
}
|
|
1302
1306
|
if (file === "errors.log") {
|
|
1303
|
-
total += importErrorsLog(db,
|
|
1307
|
+
total += importErrorsLog(db, join8(projectDir, file));
|
|
1304
1308
|
}
|
|
1305
1309
|
}
|
|
1306
1310
|
} catch {}
|
|
@@ -1348,7 +1352,7 @@ __export(exports_db, {
|
|
|
1348
1352
|
});
|
|
1349
1353
|
import { Database } from "bun:sqlite";
|
|
1350
1354
|
import { existsSync as existsSync7, mkdirSync as mkdirSync2, cpSync } from "fs";
|
|
1351
|
-
import { join as
|
|
1355
|
+
import { join as join9 } from "path";
|
|
1352
1356
|
function refuseLocalStore(message) {
|
|
1353
1357
|
localStoreRefusal = message;
|
|
1354
1358
|
}
|
|
@@ -1363,7 +1367,7 @@ function localStoreRefusalMessage() {
|
|
|
1363
1367
|
}
|
|
1364
1368
|
function resolveDataDir() {
|
|
1365
1369
|
const effective = getEffectiveDataRoot();
|
|
1366
|
-
const oldDir =
|
|
1370
|
+
const oldDir = join9(getHomeDir(), ".hooks");
|
|
1367
1371
|
if (!existsSync7(effective) && existsSync7(oldDir)) {
|
|
1368
1372
|
mkdirSync2(effective, { recursive: true });
|
|
1369
1373
|
cpSync(oldDir, effective, { recursive: true });
|
|
@@ -1375,7 +1379,7 @@ function getDbPath() {
|
|
|
1375
1379
|
if (explicitDb)
|
|
1376
1380
|
return explicitDb;
|
|
1377
1381
|
const dataDir2 = resolveDataDir();
|
|
1378
|
-
return
|
|
1382
|
+
return join9(dataDir2, "hooks.db");
|
|
1379
1383
|
}
|
|
1380
1384
|
function ensureDir(dbPath) {
|
|
1381
1385
|
const dir = dbPath.substring(0, dbPath.lastIndexOf("/"));
|
|
@@ -1497,11 +1501,11 @@ var init_redact = __esm(() => {
|
|
|
1497
1501
|
|
|
1498
1502
|
// ../contracts/dist/client/transport.js
|
|
1499
1503
|
import { isIP } from "net";
|
|
1500
|
-
import { spawnSync } from "child_process";
|
|
1504
|
+
import { spawnSync as spawnSync4 } from "child_process";
|
|
1501
1505
|
import { closeSync as closeSync23, fstatSync as fstatSync23, openSync as openSync23, readFileSync as readFileSync22 } from "fs";
|
|
1502
1506
|
import { O_NOFOLLOW as O_NOFOLLOW2, O_NONBLOCK as O_NONBLOCK2, O_RDONLY as O_RDONLY2 } from "constants";
|
|
1503
1507
|
import { hostname as osHostname } from "os";
|
|
1504
|
-
import { isAbsolute as isAbsolute9, join as
|
|
1508
|
+
import { isAbsolute as isAbsolute9, join as join210 } from "path";
|
|
1505
1509
|
import { createRequire as createRequire2 } from "module";
|
|
1506
1510
|
function envToken2(name) {
|
|
1507
1511
|
return name.toUpperCase().replace(/-/g, "_");
|
|
@@ -1532,14 +1536,14 @@ function hasnaHomeDir(env) {
|
|
|
1532
1536
|
if (override)
|
|
1533
1537
|
return override;
|
|
1534
1538
|
const home = homeDir(env);
|
|
1535
|
-
return home ?
|
|
1539
|
+
return home ? join210(home, HASNA_HOME_DIR) : null;
|
|
1536
1540
|
}
|
|
1537
1541
|
function appConfigDir(name, env) {
|
|
1538
1542
|
const configRoot = absoluteOverride(env, HASNA_CONFIG_HOME_ENV_KEY);
|
|
1539
1543
|
if (configRoot)
|
|
1540
|
-
return
|
|
1544
|
+
return join210(configRoot, name);
|
|
1541
1545
|
const root = hasnaHomeDir(env);
|
|
1542
|
-
return root ?
|
|
1546
|
+
return root ? join210(root, name, CONFIG_SUBDIR) : null;
|
|
1543
1547
|
}
|
|
1544
1548
|
function credentialDiskSourceList(name, env, profile = null) {
|
|
1545
1549
|
if (!SAFE_APP_SLUG.test(name))
|
|
@@ -1548,7 +1552,7 @@ function credentialDiskSourceList(name, env, profile = null) {
|
|
|
1548
1552
|
if (!directory3)
|
|
1549
1553
|
return [];
|
|
1550
1554
|
const file2 = profile ? `${CREDENTIALS_FILE}-${profile}` : CREDENTIALS_FILE;
|
|
1551
|
-
return [{ path:
|
|
1555
|
+
return [{ path: join210(directory3, file2), tier: "disk" }];
|
|
1552
1556
|
}
|
|
1553
1557
|
function credentialDiskSources(name, env) {
|
|
1554
1558
|
return credentialDiskSourceList(name, env, null).map((s) => s.path);
|
|
@@ -1744,7 +1748,7 @@ function isAmbientEnvironment(env) {
|
|
|
1744
1748
|
return env === process.env || env[AMBIENT_ENVIRONMENT2] === true;
|
|
1745
1749
|
}
|
|
1746
1750
|
function defaultKeychainRunner(argv) {
|
|
1747
|
-
const result =
|
|
1751
|
+
const result = spawnSync4(KEYCHAIN_SECURITY_BIN, [...argv], {
|
|
1748
1752
|
encoding: "utf8",
|
|
1749
1753
|
stdio: ["ignore", "pipe", "pipe"],
|
|
1750
1754
|
timeout: KEYCHAIN_SPAWN_TIMEOUT_MS
|
|
@@ -2418,9 +2422,9 @@ var init_stop_review_context = __esm(() => {
|
|
|
2418
2422
|
});
|
|
2419
2423
|
|
|
2420
2424
|
// src/lib/hook-env.ts
|
|
2421
|
-
import { dirname as
|
|
2422
|
-
import { existsSync as
|
|
2423
|
-
import { tmpdir } from "os";
|
|
2425
|
+
import { dirname as dirname23, isAbsolute as isAbsolute14 } from "path";
|
|
2426
|
+
import { existsSync as existsSync15, realpathSync as realpathSync7, statSync as statSync2 } from "fs";
|
|
2427
|
+
import { tmpdir as tmpdir2 } from "os";
|
|
2424
2428
|
function isDeniedEnvName(name) {
|
|
2425
2429
|
const upper = name.toUpperCase();
|
|
2426
2430
|
if (DENY_PREFIXES.some((prefix3) => upper.startsWith(prefix3)))
|
|
@@ -2454,22 +2458,22 @@ function isUnsafePathEntry(entry, home) {
|
|
|
2454
2458
|
}
|
|
2455
2459
|
};
|
|
2456
2460
|
const resolved = canonical9(entry);
|
|
2457
|
-
for (const root of [home,
|
|
2461
|
+
for (const root of [home, tmpdir2(), "/tmp", "/var/tmp"].filter((value) => Boolean(value))) {
|
|
2458
2462
|
const blocked = canonical9(root);
|
|
2459
2463
|
if (resolved === blocked || resolved.startsWith(blocked + "/"))
|
|
2460
2464
|
return true;
|
|
2461
2465
|
}
|
|
2462
2466
|
try {
|
|
2463
|
-
if ((
|
|
2467
|
+
if ((statSync2(entry).mode & 2) !== 0)
|
|
2464
2468
|
return true;
|
|
2465
2469
|
} catch {}
|
|
2466
2470
|
return false;
|
|
2467
2471
|
}
|
|
2468
2472
|
function sanitizeHookPath(pathValue, home) {
|
|
2469
2473
|
const baseline = PATH_BASELINE.slice();
|
|
2470
|
-
if (process.platform === "darwin" &&
|
|
2474
|
+
if (process.platform === "darwin" && existsSync15("/opt/homebrew/bin"))
|
|
2471
2475
|
baseline.push("/opt/homebrew/bin");
|
|
2472
|
-
const bunDir =
|
|
2476
|
+
const bunDir = dirname23(process.execPath);
|
|
2473
2477
|
const seen = new Set;
|
|
2474
2478
|
const out = [];
|
|
2475
2479
|
for (const dir of baseline) {
|
|
@@ -2602,10 +2606,10 @@ __export(exports_run, {
|
|
|
2602
2606
|
BUN_ISOLATION_FLAGS: () => BUN_ISOLATION_FLAGS
|
|
2603
2607
|
});
|
|
2604
2608
|
import { randomBytes as randomBytes2 } from "crypto";
|
|
2605
|
-
import { rmSync as rmSync5, writeFileSync as
|
|
2606
|
-
import { basename as
|
|
2609
|
+
import { rmSync as rmSync5, writeFileSync as writeFileSync11 } from "fs";
|
|
2610
|
+
import { basename as basename16, dirname as dirname27, isAbsolute as isAbsolute17, join as join32 } from "path";
|
|
2607
2611
|
function extensionOf(scriptPath) {
|
|
2608
|
-
const base =
|
|
2612
|
+
const base = basename16(scriptPath);
|
|
2609
2613
|
const dot = base.lastIndexOf(".");
|
|
2610
2614
|
return dot > 0 ? base.slice(dot + 1) : "ts";
|
|
2611
2615
|
}
|
|
@@ -2617,9 +2621,9 @@ function shebangInterpreter(firstLine) {
|
|
|
2617
2621
|
return null;
|
|
2618
2622
|
const parts = rest.split(/\s+/);
|
|
2619
2623
|
if (parts[0] === "env" || parts[0] === "/usr/bin/env") {
|
|
2620
|
-
return parts[1] && parts[1] !== "-S" ?
|
|
2624
|
+
return parts[1] && parts[1] !== "-S" ? basename16(parts[1]) : parts[2] ? basename16(parts[2]) : null;
|
|
2621
2625
|
}
|
|
2622
|
-
return
|
|
2626
|
+
return basename16(parts[0]);
|
|
2623
2627
|
}
|
|
2624
2628
|
function interpreterFor(name, scriptPath, content) {
|
|
2625
2629
|
const ext = extensionOf(scriptPath);
|
|
@@ -2672,9 +2676,9 @@ async function readPipeWithDeadline(stream, deadlineMs) {
|
|
|
2672
2676
|
return chunks.join("");
|
|
2673
2677
|
}
|
|
2674
2678
|
async function executeVerifiedScript(options) {
|
|
2675
|
-
const scriptDir =
|
|
2679
|
+
const scriptDir = dirname27(options.scriptPath);
|
|
2676
2680
|
const interpreter = interpreterFor(options.name, options.scriptPath, options.content);
|
|
2677
|
-
const tempPath =
|
|
2681
|
+
const tempPath = join32(scriptDir, `.hooks-verified-${randomBytes2(12).toString("hex")}.${interpreter.tempExt}`);
|
|
2678
2682
|
let proc = null;
|
|
2679
2683
|
let timedOut = false;
|
|
2680
2684
|
const timeoutTimer = options.timeout ? setTimeout(() => {
|
|
@@ -2682,7 +2686,7 @@ async function executeVerifiedScript(options) {
|
|
|
2682
2686
|
killGroup(proc, { leaderAlive: proc !== null && proc.exitCode === null });
|
|
2683
2687
|
}, options.timeout) : null;
|
|
2684
2688
|
try {
|
|
2685
|
-
|
|
2689
|
+
writeFileSync11(tempPath, options.content, { flag: "wx", mode: 384 });
|
|
2686
2690
|
proc = Bun.spawn([...interpreter.command, isAbsolute17(tempPath) ? tempPath : `./${tempPath}`, ...options.args ?? []], {
|
|
2687
2691
|
stdin: new Response(options.stdin),
|
|
2688
2692
|
stdout: "pipe",
|
|
@@ -2884,15 +2888,15 @@ import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
|
2884
2888
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
2885
2889
|
import { z as z3 } from "zod";
|
|
2886
2890
|
init_registry();
|
|
2887
|
-
import { existsSync as
|
|
2888
|
-
import { join as
|
|
2891
|
+
import { existsSync as existsSync16, readFileSync as readFileSync18 } from "fs";
|
|
2892
|
+
import { join as join34, dirname as dirname29 } from "path";
|
|
2889
2893
|
import { fileURLToPath as fileURLToPath4 } from "url";
|
|
2890
2894
|
|
|
2891
2895
|
// src/lib/installer.ts
|
|
2892
2896
|
init_registry();
|
|
2893
|
-
import { existsSync as
|
|
2894
|
-
import { join as
|
|
2895
|
-
import { homedir as
|
|
2897
|
+
import { existsSync as existsSync14, mkdirSync as mkdirSync12, readFileSync as readFileSync13, readdirSync as readdirSync12, realpathSync as realpathSync4, rmdirSync as rmdirSync4, unlinkSync as unlinkSync6, writeFileSync as writeFileSync10 } from "fs";
|
|
2898
|
+
import { join as join24, dirname as dirname16, basename as basename12, resolve as resolve28 } from "path";
|
|
2899
|
+
import { homedir as homedir8 } from "os";
|
|
2896
2900
|
import { fileURLToPath as fileURLToPath3 } from "url";
|
|
2897
2901
|
|
|
2898
2902
|
// src/lib/readiness-process.ts
|
|
@@ -3399,8 +3403,232 @@ function retainedMementosRegistration(commands) {
|
|
|
3399
3403
|
return values[0];
|
|
3400
3404
|
}
|
|
3401
3405
|
|
|
3406
|
+
// src/lib/targets.ts
|
|
3407
|
+
import { homedir as homedir4 } from "os";
|
|
3408
|
+
import { dirname as dirname5, join as join7, resolve as resolve3 } from "path";
|
|
3409
|
+
var TARGET_NAMES = [
|
|
3410
|
+
"claude",
|
|
3411
|
+
"codex",
|
|
3412
|
+
"gemini",
|
|
3413
|
+
"codewith",
|
|
3414
|
+
"qwen",
|
|
3415
|
+
"muse",
|
|
3416
|
+
"kimi",
|
|
3417
|
+
"pi",
|
|
3418
|
+
"cursor",
|
|
3419
|
+
"opencode",
|
|
3420
|
+
"sumi"
|
|
3421
|
+
];
|
|
3422
|
+
var INSTALL_TARGET_NAMES = [
|
|
3423
|
+
"claude",
|
|
3424
|
+
"codex",
|
|
3425
|
+
"gemini",
|
|
3426
|
+
"codewith",
|
|
3427
|
+
"qwen",
|
|
3428
|
+
"muse",
|
|
3429
|
+
"kimi",
|
|
3430
|
+
"pi",
|
|
3431
|
+
"cursor"
|
|
3432
|
+
];
|
|
3433
|
+
function isTargetName(value) {
|
|
3434
|
+
return typeof value === "string" && TARGET_NAMES.includes(value);
|
|
3435
|
+
}
|
|
3436
|
+
var settingsJson = (dir) => (o) => o.scope === "project" ? join7(resolve3(o.cwd), dir, "settings.json") : join7(o.home, dir, "settings.json");
|
|
3437
|
+
var DEFINITIONS = {
|
|
3438
|
+
claude: {
|
|
3439
|
+
install: true,
|
|
3440
|
+
run: true,
|
|
3441
|
+
stopEvent: "Stop",
|
|
3442
|
+
canBlock: true,
|
|
3443
|
+
deliversFinalMessage: true,
|
|
3444
|
+
finalMessageField: "last_assistant_message",
|
|
3445
|
+
path: settingsJson(".claude")
|
|
3446
|
+
},
|
|
3447
|
+
codex: {
|
|
3448
|
+
install: true,
|
|
3449
|
+
run: true,
|
|
3450
|
+
stopEvent: "Stop",
|
|
3451
|
+
canBlock: true,
|
|
3452
|
+
deliversFinalMessage: true,
|
|
3453
|
+
finalMessageField: "last_assistant_message",
|
|
3454
|
+
path: (o) => o.scope === "project" ? join7(resolve3(o.cwd), ".codex", "hooks.json") : join7(o.home, ".codex", "hooks.json")
|
|
3455
|
+
},
|
|
3456
|
+
gemini: {
|
|
3457
|
+
install: true,
|
|
3458
|
+
run: true,
|
|
3459
|
+
stopEvent: "AfterAgent",
|
|
3460
|
+
canBlock: true,
|
|
3461
|
+
deliversFinalMessage: true,
|
|
3462
|
+
finalMessageField: "prompt_response",
|
|
3463
|
+
path: settingsJson(".gemini")
|
|
3464
|
+
},
|
|
3465
|
+
codewith: {
|
|
3466
|
+
install: true,
|
|
3467
|
+
run: true,
|
|
3468
|
+
stopEvent: "Stop",
|
|
3469
|
+
canBlock: true,
|
|
3470
|
+
deliversFinalMessage: true,
|
|
3471
|
+
finalMessageField: "last_assistant_message",
|
|
3472
|
+
path: (o) => o.scope === "project" ? join7(resolve3(o.cwd), ".codewith", "config.toml") : join7(o.home, ".codewith", "config.toml")
|
|
3473
|
+
},
|
|
3474
|
+
qwen: {
|
|
3475
|
+
install: true,
|
|
3476
|
+
run: true,
|
|
3477
|
+
stopEvent: "Stop",
|
|
3478
|
+
canBlock: true,
|
|
3479
|
+
deliversFinalMessage: true,
|
|
3480
|
+
finalMessageField: "last_assistant_message",
|
|
3481
|
+
path: settingsJson(".qwen")
|
|
3482
|
+
},
|
|
3483
|
+
muse: {
|
|
3484
|
+
install: true,
|
|
3485
|
+
run: true,
|
|
3486
|
+
stopEvent: "Stop",
|
|
3487
|
+
canBlock: true,
|
|
3488
|
+
deliversFinalMessage: true,
|
|
3489
|
+
finalMessageField: "last_assistant_message",
|
|
3490
|
+
installNote: "Muse receives the final message as a quoted JSON string; `hooks run --target muse` parses it once before the hook sees it.",
|
|
3491
|
+
path: (o) => join7(o.home, ".hasna", "hooks", "muse-plugins", o.hook ?? "")
|
|
3492
|
+
},
|
|
3493
|
+
kimi: {
|
|
3494
|
+
install: true,
|
|
3495
|
+
run: true,
|
|
3496
|
+
stopEvent: "Stop",
|
|
3497
|
+
canBlock: true,
|
|
3498
|
+
deliversFinalMessage: false,
|
|
3499
|
+
finalMessageNote: "Kimi's Stop payload carries only hook_event_name, session_id, cwd, client_type, session_title and stop_hook_active",
|
|
3500
|
+
path: (o) => join7(o.home, ".kimi-code", "config.toml")
|
|
3501
|
+
},
|
|
3502
|
+
pi: {
|
|
3503
|
+
install: true,
|
|
3504
|
+
run: true,
|
|
3505
|
+
stopEvent: "turn_end",
|
|
3506
|
+
canBlock: true,
|
|
3507
|
+
deliversFinalMessage: false,
|
|
3508
|
+
finalMessageNote: "the pi-hooks bridge sends only hook_event_name, session_id, cwd and stop_hook_active (transcript_path is declared but never populated)",
|
|
3509
|
+
installNote: "Pi runs hooks through the hasna-pi-hooks bridge: the config points at a Hooks profile, so registration is not by itself proof that a turn was held.",
|
|
3510
|
+
path: (o) => join7(o.home, ".pi", "agent", "state", "hasna-pi-hooks.config.json")
|
|
3511
|
+
},
|
|
3512
|
+
cursor: {
|
|
3513
|
+
install: true,
|
|
3514
|
+
run: true,
|
|
3515
|
+
stopEvent: "stop",
|
|
3516
|
+
canBlock: false,
|
|
3517
|
+
deliversFinalMessage: false,
|
|
3518
|
+
finalMessageNote: "Cursor's stop hook carries no assistant text",
|
|
3519
|
+
blockNote: "a block verdict is ignored, and only followup_message continues a turn (loop_limit 5)",
|
|
3520
|
+
installNote: "Cursor's stop hook cannot block the turn: a block verdict is ignored, and only followup_message continues a turn (loop_limit 5). The registration alone does not hold anything.",
|
|
3521
|
+
path: (o) => o.scope === "project" ? join7(resolve3(o.cwd), ".cursor", "hooks.json") : join7(o.home, ".cursor", "hooks.json")
|
|
3522
|
+
},
|
|
3523
|
+
opencode: {
|
|
3524
|
+
install: false,
|
|
3525
|
+
run: true,
|
|
3526
|
+
stopEvent: null,
|
|
3527
|
+
canBlock: false,
|
|
3528
|
+
deliversFinalMessage: false,
|
|
3529
|
+
blockNote: "opencode has no turn-end event to hold: session.prompt schedules a new agent-loop run instead of continuing the current turn",
|
|
3530
|
+
stopEventSubstitute: "opencode has only the session.idle bus event, which is observe-only and cannot hold the turn; `hooks install stop-review --target opencode` registers the managed observe-and-re-prompt plugin",
|
|
3531
|
+
path: (o) => {
|
|
3532
|
+
const xdg = o.env.XDG_CONFIG_HOME;
|
|
3533
|
+
const base = xdg && xdg.startsWith("/") ? xdg : join7(o.home, ".config");
|
|
3534
|
+
return join7(resolve3(base), "opencode", "opencode.json");
|
|
3535
|
+
}
|
|
3536
|
+
},
|
|
3537
|
+
sumi: {
|
|
3538
|
+
install: false,
|
|
3539
|
+
run: true,
|
|
3540
|
+
stopEvent: "Stop",
|
|
3541
|
+
canBlock: true,
|
|
3542
|
+
deliversFinalMessage: true,
|
|
3543
|
+
finalMessageField: "last_assistant_message",
|
|
3544
|
+
stopEventSubstitute: "Sumi stop review is registered through the managed path: `hooks install stop-review --target sumi`",
|
|
3545
|
+
path: (o) => join7(o.home, ".sumi", "plugins")
|
|
3546
|
+
}
|
|
3547
|
+
};
|
|
3548
|
+
function getTargetCapabilities(target, options = {}) {
|
|
3549
|
+
if (!isTargetName(target))
|
|
3550
|
+
return;
|
|
3551
|
+
const definition = DEFINITIONS[target];
|
|
3552
|
+
const resolved = {
|
|
3553
|
+
scope: options.scope ?? "global",
|
|
3554
|
+
home: options.home ?? homedir4(),
|
|
3555
|
+
cwd: options.cwd ?? process.cwd(),
|
|
3556
|
+
env: options.env ?? process.env,
|
|
3557
|
+
hook: options.hook
|
|
3558
|
+
};
|
|
3559
|
+
return {
|
|
3560
|
+
install: definition.install,
|
|
3561
|
+
run: definition.run,
|
|
3562
|
+
stopEvent: definition.stopEvent,
|
|
3563
|
+
canBlock: definition.canBlock,
|
|
3564
|
+
deliversFinalMessage: definition.deliversFinalMessage,
|
|
3565
|
+
configPath: definition.path(resolved)
|
|
3566
|
+
};
|
|
3567
|
+
}
|
|
3568
|
+
function musePluginRoot(options = {}) {
|
|
3569
|
+
return dirname5(getTargetCapabilities("muse", { ...options, hook: "bundle" }).configPath);
|
|
3570
|
+
}
|
|
3571
|
+
function targetInstallNote(target) {
|
|
3572
|
+
return isTargetName(target) ? DEFINITIONS[target].installNote : undefined;
|
|
3573
|
+
}
|
|
3574
|
+
function runTargetHelp() {
|
|
3575
|
+
return TARGET_NAMES.join(", ");
|
|
3576
|
+
}
|
|
3577
|
+
function blockingInstallTargets() {
|
|
3578
|
+
return INSTALL_TARGET_NAMES.filter((name) => DEFINITIONS[name].canBlock);
|
|
3579
|
+
}
|
|
3580
|
+
function installCapabilityRefusal(hook, target, requirement, options = {}) {
|
|
3581
|
+
const definition = isTargetName(target) ? DEFINITIONS[target] : undefined;
|
|
3582
|
+
if (!definition) {
|
|
3583
|
+
return `Refused: Unknown target '${target}'. Known targets: ${runTargetHelp()}.`;
|
|
3584
|
+
}
|
|
3585
|
+
const capabilities = getTargetCapabilities(target, options);
|
|
3586
|
+
if (requirement.requiresBlock && !capabilities.canBlock) {
|
|
3587
|
+
const others = [];
|
|
3588
|
+
if (!capabilities.install)
|
|
3589
|
+
others.push("no generic install path (install: false)");
|
|
3590
|
+
if (requirement.requiresFinalMessage && !capabilities.deliversFinalMessage)
|
|
3591
|
+
others.push("no final assistant message (deliversFinalMessage: false)");
|
|
3592
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null)
|
|
3593
|
+
others.push("no turn-end event (stopEvent: null)");
|
|
3594
|
+
const also = others.length === 0 ? "" : ` It also has ${others.join(" and ")}.`;
|
|
3595
|
+
return `Refused: '${hook}' declares requires_block, and target '${target}' cannot hold the turn (canBlock: false) \u2014 ${definition.blockNote ?? "a block verdict is ignored there"}.${also} ` + `Unblock it by installing on a target that can block (${blockingInstallTargets().join(", ")}), or by clearing requires_block in the hook's manifest.`;
|
|
3596
|
+
}
|
|
3597
|
+
if (requirement.requiresFinalMessage && !capabilities.deliversFinalMessage) {
|
|
3598
|
+
const others = [];
|
|
3599
|
+
if (!capabilities.install)
|
|
3600
|
+
others.push("no generic install path (install: false)");
|
|
3601
|
+
if (requirement.requiresBlock && !capabilities.canBlock)
|
|
3602
|
+
others.push("no turn-holding verdict (canBlock: false)");
|
|
3603
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null)
|
|
3604
|
+
others.push("no turn-end event (stopEvent: null)");
|
|
3605
|
+
const also = others.length === 0 ? "" : ` It also has ${others.join(" and ")}.`;
|
|
3606
|
+
return `Refused: '${hook}' declares requires_final_message, and target '${target}' does not deliver the assistant's final message (deliversFinalMessage: false) \u2014 ${definition.finalMessageNote ?? "its payload carries no assistant text"}.${also} ` + `Unblock it by installing on a target that delivers the message (${INSTALL_TARGET_NAMES.filter((name) => DEFINITIONS[name].deliversFinalMessage).join(", ")}), or by clearing requires_final_message in the hook's manifest.`;
|
|
3607
|
+
}
|
|
3608
|
+
if (!capabilities.install) {
|
|
3609
|
+
return `Refused: Hooks has no generic install path for target '${target}' (install: false). ${definition.stopEventSubstitute ?? `Supported install targets: ${INSTALL_TARGET_NAMES.join(", ")}.`}`;
|
|
3610
|
+
}
|
|
3611
|
+
if (requirement.events.includes("Stop") && capabilities.stopEvent === null) {
|
|
3612
|
+
return `Refused: '${hook}' runs on the Stop event, and target '${target}' has no turn-end event (stopEvent: null), so nothing would ever run it. ${definition.stopEventSubstitute ?? ""}`.trim();
|
|
3613
|
+
}
|
|
3614
|
+
return;
|
|
3615
|
+
}
|
|
3616
|
+
|
|
3617
|
+
// src/lib/native-target.ts
|
|
3618
|
+
var HOOK_RUN_TARGETS = TARGET_NAMES;
|
|
3619
|
+
function isHookRunTarget(value) {
|
|
3620
|
+
return typeof value === "string" && HOOK_RUN_TARGETS.includes(value);
|
|
3621
|
+
}
|
|
3622
|
+
function nativeTargetCommandSuffix(dispatch, target) {
|
|
3623
|
+
if (!dispatch)
|
|
3624
|
+
return "";
|
|
3625
|
+
if (!isHookRunTarget(target))
|
|
3626
|
+
throw new Error("Native registration requires a supported target");
|
|
3627
|
+
return ` --target ${target}`;
|
|
3628
|
+
}
|
|
3629
|
+
|
|
3402
3630
|
// src/lib/registration.ts
|
|
3403
|
-
var REGISTRATION_COMMAND_RE =
|
|
3631
|
+
var REGISTRATION_COMMAND_RE = new RegExp(`^(?:hooks run ([\\w-]+)(?:\\s+--profile\\s+[\\w-]+)?(?:\\s+--target\\s+(?:${HOOK_RUN_TARGETS.join("|")}))?|hook-([\\w-]+))$`);
|
|
3404
3632
|
function registrationName(command) {
|
|
3405
3633
|
if (typeof command !== "string")
|
|
3406
3634
|
return;
|
|
@@ -3477,7 +3705,7 @@ function hookRegisteredInSettings(settings, name, eventSpec, hookMatcher) {
|
|
|
3477
3705
|
// src/lib/store.ts
|
|
3478
3706
|
import { createHash as createHash4 } from "crypto";
|
|
3479
3707
|
import { existsSync as existsSync8, mkdirSync as mkdirSync3, readFileSync as readFileSync5, writeFileSync as writeFileSync2, renameSync, lstatSync as lstatSync3 } from "fs";
|
|
3480
|
-
import { join as
|
|
3708
|
+
import { join as join10 } from "path";
|
|
3481
3709
|
init_db();
|
|
3482
3710
|
init_config();
|
|
3483
3711
|
init_manifest();
|
|
@@ -3595,7 +3823,7 @@ function getPinnedHook(name) {
|
|
|
3595
3823
|
return readLock().hooks[name];
|
|
3596
3824
|
}
|
|
3597
3825
|
function registryManagedMarker(name) {
|
|
3598
|
-
return
|
|
3826
|
+
return join10(getHooksDataDir(), "registry-managed", sha256Of(name) + ".json");
|
|
3599
3827
|
}
|
|
3600
3828
|
function isRegistryManaged(name) {
|
|
3601
3829
|
try {
|
|
@@ -3669,7 +3897,7 @@ function checkScriptHash(name, actual) {
|
|
|
3669
3897
|
|
|
3670
3898
|
// src/lib/codex-settings.ts
|
|
3671
3899
|
import { closeSync as closeSync3, existsSync as existsSync9, lstatSync as lstatSync4, mkdirSync as mkdirSync4, openSync as openSync3, readFileSync as readFileSync6, renameSync as renameSync2, unlinkSync, writeFileSync as writeFileSync3 } from "fs";
|
|
3672
|
-
import { dirname as
|
|
3900
|
+
import { dirname as dirname6 } from "path";
|
|
3673
3901
|
import { createHash as createHash5, randomUUID } from "crypto";
|
|
3674
3902
|
var snapshots = new WeakMap;
|
|
3675
3903
|
function current(path) {
|
|
@@ -3722,7 +3950,7 @@ function writeCodexSettings(path, settings, expectedSha256, beforeWrite) {
|
|
|
3722
3950
|
` });
|
|
3723
3951
|
}
|
|
3724
3952
|
function writeHookSettingsPreimage(path, settings, before, beforeWrite) {
|
|
3725
|
-
mkdirSync4(
|
|
3953
|
+
mkdirSync4(dirname6(path), { recursive: true, mode: 448 });
|
|
3726
3954
|
const lock = `${path}.hooks-write.lock`;
|
|
3727
3955
|
let fd;
|
|
3728
3956
|
try {
|
|
@@ -3752,13 +3980,13 @@ function writeHookSettingsPreimage(path, settings, before, beforeWrite) {
|
|
|
3752
3980
|
// src/lib/claude-skills-coordination.ts
|
|
3753
3981
|
import { createHash as createHash8 } from "crypto";
|
|
3754
3982
|
import { existsSync as existsSync11, lstatSync as lstatSync9 } from "fs";
|
|
3755
|
-
import { homedir as
|
|
3756
|
-
import { join as
|
|
3983
|
+
import { homedir as homedir7 } from "os";
|
|
3984
|
+
import { join as join17, resolve as resolve9 } from "path";
|
|
3757
3985
|
|
|
3758
3986
|
// ../../node_modules/.bun/@hasna+skills@0.10.78+a00d3064559565bb/node_modules/@hasna/skills/dist/index.js
|
|
3759
3987
|
import { existsSync as existsSync10 } from "fs";
|
|
3760
|
-
import { homedir as
|
|
3761
|
-
import { join as
|
|
3988
|
+
import { homedir as homedir6 } from "os";
|
|
3989
|
+
import { join as join11, resolve as resolve4 } from "path";
|
|
3762
3990
|
import { homedir as pathsResolverHomedir2 } from "os";
|
|
3763
3991
|
import { join as pathsResolverJoin2 } from "path";
|
|
3764
3992
|
import { createHash as createHash6 } from "crypto";
|
|
@@ -3773,12 +4001,12 @@ import { createHash as createHash42 } from "crypto";
|
|
|
3773
4001
|
import { basename as basename3, dirname as dirname42, isAbsolute as isAbsolute32, resolve as resolve32 } from "path";
|
|
3774
4002
|
import { createHash as createHash52 } from "crypto";
|
|
3775
4003
|
import { closeSync as closeSync32, constants as constants3, fstatSync as fstatSync3, lstatSync as lstatSync5, openSync as openSync32, readSync as readSync2 } from "fs";
|
|
3776
|
-
import { basename as basename4, dirname as dirname52, isAbsolute as isAbsolute4, resolve as
|
|
4004
|
+
import { basename as basename4, dirname as dirname52, isAbsolute as isAbsolute4, resolve as resolve42, sep as sep22 } from "path";
|
|
3777
4005
|
import { createHash as createHash30 } from "crypto";
|
|
3778
4006
|
import { lstatSync as lstatSync32, readFileSync as readFileSync26, readdirSync as readdirSync13, realpathSync as realpathSync13 } from "fs";
|
|
3779
4007
|
import { basename as basename15, dirname as dirname26, isAbsolute as isAbsolute28, join as join38, relative as relative10, resolve as resolve27, sep as sep15 } from "path";
|
|
3780
4008
|
import { isDeepStrictEqual as isDeepStrictEqual9 } from "util";
|
|
3781
|
-
import { dirname as
|
|
4009
|
+
import { dirname as dirname62, isAbsolute as isAbsolute6, join as join82, resolve as resolve6 } from "path";
|
|
3782
4010
|
import { lstatSync as lstatSync6, readFileSync as readFileSync62, opendirSync } from "fs";
|
|
3783
4011
|
import { createHash as createHash62 } from "crypto";
|
|
3784
4012
|
import { lstatSync as lstatSync7, opendirSync as opendirSync2 } from "fs";
|
|
@@ -10707,10 +10935,10 @@ var SKILLS_HOME_ENV = "SKILLS_HOME";
|
|
|
10707
10935
|
var DEFAULT_SQLITE_FILENAME = "server.db";
|
|
10708
10936
|
var GLOBAL_CONFIG_FILENAME = "config.json";
|
|
10709
10937
|
function effectiveHome() {
|
|
10710
|
-
return process.env["HOME"] || process.env["USERPROFILE"] ||
|
|
10938
|
+
return process.env["HOME"] || process.env["USERPROFILE"] || homedir6() || "/tmp";
|
|
10711
10939
|
}
|
|
10712
10940
|
function legacyDataRoot() {
|
|
10713
|
-
return
|
|
10941
|
+
return join11(effectiveHome(), ".hasna", "skills");
|
|
10714
10942
|
}
|
|
10715
10943
|
function resolverDataRoot(home = effectiveHome(), env) {
|
|
10716
10944
|
return dataDir2({ app: "skills", home, env });
|
|
@@ -10719,13 +10947,13 @@ function adoptResolverDataRoot2(resolved, env = process.env) {
|
|
|
10719
10947
|
const dataOverride = env.HASNA_DATA_HOME;
|
|
10720
10948
|
if (typeof dataOverride === "string" && dataOverride.trim().length > 0)
|
|
10721
10949
|
return true;
|
|
10722
|
-
return existsSync10(
|
|
10950
|
+
return existsSync10(join11(resolved, DEFAULT_SQLITE_FILENAME)) || existsSync10(join11(resolved, GLOBAL_CONFIG_FILENAME));
|
|
10723
10951
|
}
|
|
10724
10952
|
function exactDataRoot() {
|
|
10725
10953
|
for (const key of [DATA_DIR_ENV, HASNA_SKILLS_HOME_ENV, SKILLS_HOME_ENV]) {
|
|
10726
10954
|
const dir = process.env[key]?.trim();
|
|
10727
10955
|
if (dir)
|
|
10728
|
-
return
|
|
10956
|
+
return resolve4(dir);
|
|
10729
10957
|
}
|
|
10730
10958
|
return;
|
|
10731
10959
|
}
|
|
@@ -10734,7 +10962,7 @@ function getDataRoot() {
|
|
|
10734
10962
|
if (exact)
|
|
10735
10963
|
return exact;
|
|
10736
10964
|
const resolved = resolverDataRoot();
|
|
10737
|
-
return adoptResolverDataRoot2(resolved) ?
|
|
10965
|
+
return adoptResolverDataRoot2(resolved) ? resolve4(resolved) : resolve4(legacyDataRoot());
|
|
10738
10966
|
}
|
|
10739
10967
|
function getDataDirReadOnly() {
|
|
10740
10968
|
return getDataRoot();
|
|
@@ -11830,7 +12058,7 @@ function captureClaudeMarketplaceEntry(path, marketplace, plugin, budget2 = { re
|
|
|
11830
12058
|
var CLAUDE_ORPHAN_MARKER_MODE = "claude-plugin-orphaned-at-v1";
|
|
11831
12059
|
var CACHE = `${sep22}.claude${sep22}plugins${sep22}cache${sep22}`;
|
|
11832
12060
|
function isClaudePluginCacheOrphanMarker(path) {
|
|
11833
|
-
if (typeof path !== "string" || basename4(path) !== ".orphaned_at" || !isAbsolute4(path) ||
|
|
12061
|
+
if (typeof path !== "string" || basename4(path) !== ".orphaned_at" || !isAbsolute4(path) || resolve42(path) !== path || path.includes("\x00"))
|
|
11834
12062
|
return false;
|
|
11835
12063
|
const at = path.indexOf(CACHE);
|
|
11836
12064
|
if (at < 0 || path.indexOf(CACHE, at + 1) >= 0)
|
|
@@ -12284,10 +12512,10 @@ function assertHermesEnvironment(home) {
|
|
|
12284
12512
|
throw new Error("NATIVE_SKILL_DRIFT: Hermes project plugins require a dedicated discovery review");
|
|
12285
12513
|
}
|
|
12286
12514
|
function read(path) {
|
|
12287
|
-
for (let current2 = path;; current2 =
|
|
12515
|
+
for (let current2 = path;; current2 = dirname62(current2)) {
|
|
12288
12516
|
if (lstatSync6(current2, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
12289
12517
|
throw new Error("NATIVE_SKILL_DRIFT: symlink Hermes protection input");
|
|
12290
|
-
if (
|
|
12518
|
+
if (dirname62(current2) === current2)
|
|
12291
12519
|
break;
|
|
12292
12520
|
}
|
|
12293
12521
|
const stat = lstatSync6(path, { throwIfNoEntry: false });
|
|
@@ -12327,10 +12555,10 @@ function assertHermesProtection(home, config, command, profile, supervisor2) {
|
|
|
12327
12555
|
}
|
|
12328
12556
|
function assertNoHermesLegacyShadow(root) {
|
|
12329
12557
|
let entries = 0;
|
|
12330
|
-
for (let cursor = resolve6(root);; cursor =
|
|
12558
|
+
for (let cursor = resolve6(root);; cursor = dirname62(cursor)) {
|
|
12331
12559
|
if (lstatSync6(cursor, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
12332
12560
|
throw new Error("NATIVE_SKILL_DRIFT: symlink Hermes legacy discovery root");
|
|
12333
|
-
if (
|
|
12561
|
+
if (dirname62(cursor) === cursor)
|
|
12334
12562
|
break;
|
|
12335
12563
|
}
|
|
12336
12564
|
function scan(path, depth) {
|
|
@@ -41657,7 +41885,7 @@ function claudeSkillsUsage(before, home) {
|
|
|
41657
41885
|
}
|
|
41658
41886
|
if (events3.length)
|
|
41659
41887
|
evidence.push(`Skills-managed hook entries in ${events3.join(", ")}`);
|
|
41660
|
-
const bridge =
|
|
41888
|
+
const bridge = join17(home, ".claude", "skills", "skills-cli", "SKILL.md");
|
|
41661
41889
|
let bridgePresent;
|
|
41662
41890
|
try {
|
|
41663
41891
|
bridgePresent = lstatSync9(bridge, { throwIfNoEntry: false }) !== undefined;
|
|
@@ -41671,8 +41899,8 @@ function claudeSkillsUsage(before, home) {
|
|
|
41671
41899
|
function skillsPolicyLocations(dataDir3) {
|
|
41672
41900
|
const state = (path) => existsSync11(path) ? "present" : "absent";
|
|
41673
41901
|
if (dataDir3 !== undefined)
|
|
41674
|
-
return [`${
|
|
41675
|
-
const env = process.env, home = env.HOME || env.USERPROFILE ||
|
|
41902
|
+
return [`${join17(resolve9(dataDir3), "agent-policy.json")} (explicit data directory, absent)`];
|
|
41903
|
+
const env = process.env, home = env.HOME || env.USERPROFILE || homedir7() || "/tmp", searched = [];
|
|
41676
41904
|
let selected = false;
|
|
41677
41905
|
for (const key2 of ["HASNA_SKILLS_DIR", "HASNA_SKILLS_HOME", "SKILLS_HOME"]) {
|
|
41678
41906
|
const value = env[key2]?.trim();
|
|
@@ -41682,13 +41910,13 @@ function skillsPolicyLocations(dataDir3) {
|
|
|
41682
41910
|
searched.push(`$${key2} (unset)`);
|
|
41683
41911
|
else {
|
|
41684
41912
|
selected = true;
|
|
41685
|
-
searched.push(`$${key2} -> ${
|
|
41913
|
+
searched.push(`$${key2} -> ${join17(resolve9(value), "agent-policy.json")} (selected, absent)`);
|
|
41686
41914
|
}
|
|
41687
41915
|
}
|
|
41688
41916
|
const override = env.HASNA_DATA_HOME;
|
|
41689
|
-
const base = typeof override === "string" && override.length > 0 ? override : process.platform === "darwin" ?
|
|
41690
|
-
const xdg =
|
|
41691
|
-
const adopted = typeof override === "string" && override.trim().length > 0 || existsSync11(
|
|
41917
|
+
const base = typeof override === "string" && override.length > 0 ? override : process.platform === "darwin" ? join17(home, "Library", "Application Support", "Hasna") : join17(home, ".local", "share", "hasna");
|
|
41918
|
+
const xdg = join17(base, "skills"), xdgPolicy = join17(xdg, "agent-policy.json");
|
|
41919
|
+
const adopted = typeof override === "string" && override.trim().length > 0 || existsSync11(join17(xdg, "server.db")) || existsSync11(join17(xdg, "config.json"));
|
|
41692
41920
|
if (selected)
|
|
41693
41921
|
searched.push(`${xdgPolicy} (not consulted, ${state(xdgPolicy)})`);
|
|
41694
41922
|
else if (adopted) {
|
|
@@ -41696,7 +41924,7 @@ function skillsPolicyLocations(dataDir3) {
|
|
|
41696
41924
|
searched.push(`${xdgPolicy} (selected, absent)`);
|
|
41697
41925
|
} else
|
|
41698
41926
|
searched.push(`${xdgPolicy} (not selected: the directory holds no server.db or config.json and HASNA_DATA_HOME is unset; ${state(xdgPolicy)})`);
|
|
41699
|
-
const legacy =
|
|
41927
|
+
const legacy = join17(home, ".hasna", "skills", "agent-policy.json");
|
|
41700
41928
|
searched.push(selected ? `${legacy} (not consulted, ${state(legacy)})` : `${legacy} (selected, absent)`);
|
|
41701
41929
|
return searched;
|
|
41702
41930
|
}
|
|
@@ -41714,7 +41942,7 @@ function planCoordinatedClaudeSettings(before, replacement, options) {
|
|
|
41714
41942
|
if (!plan) {
|
|
41715
41943
|
const usage = claudeSkillsUsage(before, options.home);
|
|
41716
41944
|
if (usage.length)
|
|
41717
|
-
throw new ClaudeSettingsRefusal("skills_policy_unresolved", `Skills manages ${
|
|
41945
|
+
throw new ClaudeSettingsRefusal("skills_policy_unresolved", `Skills manages ${join17(options.home, ".claude", "settings.json")} (${usage.join("; ")}), but no Skills agent policy resolved, ` + "so Hooks cannot coordinate this change and wrote nothing. Locations searched, in Skills resolution order: " + skillsPolicyLocations(options.dataDir).map((location, index) => `${index + 1}) ${location}`).join("; ") + ". Run hooks with the environment that selects the Skills data directory (for example HASNA_SKILLS_DIR), " + "or repair the integration with `skills hook install`.");
|
|
41718
41946
|
}
|
|
41719
41947
|
return plan;
|
|
41720
41948
|
}
|
|
@@ -41727,22 +41955,771 @@ function coordinateClaudeSettings(before, replacement, options, beforeWrite) {
|
|
|
41727
41955
|
return true;
|
|
41728
41956
|
}
|
|
41729
41957
|
|
|
41730
|
-
// src/lib/
|
|
41731
|
-
|
|
41732
|
-
|
|
41733
|
-
|
|
41958
|
+
// src/lib/target-adapters.ts
|
|
41959
|
+
import { spawnSync } from "child_process";
|
|
41960
|
+
import { chmodSync as chmodSync2, existsSync as existsSync13, mkdirSync as mkdirSync10, readFileSync as readFileSync8, readdirSync as readdirSync8, rmdirSync as rmdirSync2, statSync, unlinkSync as unlinkSync3, writeFileSync as writeFileSync9, renameSync as renameSync5 } from "fs";
|
|
41961
|
+
import { tmpdir } from "os";
|
|
41962
|
+
import { basename as basename9, dirname as dirname9, join as join22 } from "path";
|
|
41963
|
+
import { randomUUID as randomUUID5 } from "crypto";
|
|
41964
|
+
|
|
41965
|
+
// src/lib/profiles.ts
|
|
41966
|
+
init_app_home();
|
|
41967
|
+
import { existsSync as existsSync12, mkdirSync as mkdirSync6, readFileSync as readFileSync7, writeFileSync as writeFileSync7, readdirSync as readdirSync4, rmSync as rmSync3, cpSync as cpSync2 } from "fs";
|
|
41968
|
+
import { join as join20 } from "path";
|
|
41969
|
+
function getProfilesDir() {
|
|
41970
|
+
const newDir = join20(getEffectiveDataRoot(), "profiles");
|
|
41971
|
+
const oldDir = join20(getHomeDir(), ".hooks", "profiles");
|
|
41972
|
+
if (!existsSync12(newDir) && existsSync12(oldDir)) {
|
|
41973
|
+
mkdirSync6(newDir, { recursive: true });
|
|
41974
|
+
cpSync2(oldDir, newDir, { recursive: true });
|
|
41975
|
+
}
|
|
41976
|
+
return newDir;
|
|
41734
41977
|
}
|
|
41735
|
-
function
|
|
41736
|
-
|
|
41737
|
-
|
|
41738
|
-
|
|
41739
|
-
|
|
41740
|
-
|
|
41978
|
+
function ensureProfilesDir() {
|
|
41979
|
+
const dir = getProfilesDir();
|
|
41980
|
+
if (!existsSync12(dir)) {
|
|
41981
|
+
mkdirSync6(dir, { recursive: true });
|
|
41982
|
+
}
|
|
41983
|
+
}
|
|
41984
|
+
function profilePath(id) {
|
|
41985
|
+
return join20(getProfilesDir(), `${id}.json`);
|
|
41986
|
+
}
|
|
41987
|
+
function shortUuid() {
|
|
41988
|
+
return crypto.randomUUID().slice(0, 8);
|
|
41989
|
+
}
|
|
41990
|
+
function createProfile(input2) {
|
|
41991
|
+
ensureProfilesDir();
|
|
41992
|
+
const id = shortUuid();
|
|
41993
|
+
const now = new Date().toISOString();
|
|
41994
|
+
const profile = {
|
|
41995
|
+
agent_id: id,
|
|
41996
|
+
agent_type: input2.agent_type,
|
|
41997
|
+
created_at: now,
|
|
41998
|
+
last_seen_at: now,
|
|
41999
|
+
preferences: {}
|
|
42000
|
+
};
|
|
42001
|
+
if (input2.name) {
|
|
42002
|
+
profile.name = input2.name;
|
|
42003
|
+
}
|
|
42004
|
+
writeFileSync7(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
42005
|
+
`);
|
|
42006
|
+
return profile;
|
|
42007
|
+
}
|
|
42008
|
+
function getProfile(id) {
|
|
42009
|
+
const path = profilePath(id);
|
|
42010
|
+
try {
|
|
42011
|
+
if (!existsSync12(path))
|
|
42012
|
+
return null;
|
|
42013
|
+
return JSON.parse(readFileSync7(path, "utf-8"));
|
|
42014
|
+
} catch {
|
|
42015
|
+
return null;
|
|
42016
|
+
}
|
|
42017
|
+
}
|
|
42018
|
+
function listProfiles() {
|
|
42019
|
+
const dir = getProfilesDir();
|
|
42020
|
+
if (!existsSync12(dir))
|
|
42021
|
+
return [];
|
|
42022
|
+
try {
|
|
42023
|
+
const files = readdirSync4(dir).filter((f) => f.endsWith(".json"));
|
|
42024
|
+
const profiles = [];
|
|
42025
|
+
for (const file2 of files) {
|
|
42026
|
+
try {
|
|
42027
|
+
const content = readFileSync7(join20(dir, file2), "utf-8");
|
|
42028
|
+
profiles.push(JSON.parse(content));
|
|
42029
|
+
} catch {}
|
|
42030
|
+
}
|
|
42031
|
+
return profiles.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
|
42032
|
+
} catch {
|
|
42033
|
+
return [];
|
|
42034
|
+
}
|
|
42035
|
+
}
|
|
42036
|
+
function updateProfile(id, data) {
|
|
42037
|
+
const profile = getProfile(id);
|
|
42038
|
+
if (!profile)
|
|
42039
|
+
return null;
|
|
42040
|
+
if (data.name !== undefined)
|
|
42041
|
+
profile.name = data.name;
|
|
42042
|
+
if (data.preferences !== undefined)
|
|
42043
|
+
profile.preferences = data.preferences;
|
|
42044
|
+
writeFileSync7(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
42045
|
+
`);
|
|
42046
|
+
return profile;
|
|
42047
|
+
}
|
|
42048
|
+
|
|
42049
|
+
// src/lib/target-adapters.ts
|
|
42050
|
+
var ADAPTER_TARGETS = ["muse", "kimi", "pi", "cursor"];
|
|
42051
|
+
function isAdapterTarget(value) {
|
|
42052
|
+
return typeof value === "string" && ADAPTER_TARGETS.includes(value);
|
|
42053
|
+
}
|
|
42054
|
+
var defaultRunner = (command, args) => {
|
|
42055
|
+
const result = spawnSync(command, args, { encoding: "utf8", maxBuffer: 4 * 1024 * 1024 });
|
|
42056
|
+
return {
|
|
42057
|
+
status: result.status ?? 1,
|
|
42058
|
+
stdout: result.stdout ?? "",
|
|
42059
|
+
stderr: result.stderr ?? "",
|
|
42060
|
+
notFound: result.error?.code === "ENOENT"
|
|
42061
|
+
};
|
|
42062
|
+
};
|
|
42063
|
+
var HOOK_COMMAND_RE = /^hooks run ([\w-]+)(?:\s|$)/;
|
|
42064
|
+
function hookFromCommand(command) {
|
|
42065
|
+
return command.trim().match(HOOK_COMMAND_RE)?.[1];
|
|
42066
|
+
}
|
|
42067
|
+
function homeOf(options) {
|
|
42068
|
+
return options.home ?? process.env.HOME ?? process.env.USERPROFILE ?? "";
|
|
42069
|
+
}
|
|
42070
|
+
function adapterPath(target, options, hook) {
|
|
42071
|
+
const capabilities = getTargetCapabilities(target, {
|
|
42072
|
+
scope: options.scope,
|
|
42073
|
+
home: homeOf(options),
|
|
42074
|
+
cwd: options.cwd ?? process.cwd(),
|
|
42075
|
+
env: options.env ?? process.env,
|
|
42076
|
+
...hook ? { hook } : {}
|
|
42077
|
+
});
|
|
42078
|
+
if (!capabilities)
|
|
42079
|
+
throw new Error(`No capability record for target '${target}'`);
|
|
42080
|
+
return capabilities.configPath;
|
|
42081
|
+
}
|
|
42082
|
+
function redactedDiagnostic(text3) {
|
|
42083
|
+
const line = (text3.split(`
|
|
42084
|
+
`).find((entry) => entry.trim()) ?? "").trim();
|
|
42085
|
+
return line.slice(0, 300).replace(/[A-Za-z0-9_\-]{24,}/g, "<redacted>");
|
|
42086
|
+
}
|
|
42087
|
+
function redactedCode(value) {
|
|
42088
|
+
return typeof value === "string" && value.length <= 64 && /^[a-z][a-z0-9]*(?:-[a-z0-9]+)+$/.test(value) ? value : "<redacted>";
|
|
42089
|
+
}
|
|
42090
|
+
function messageOf(error61) {
|
|
42091
|
+
return error61 instanceof Error ? error61.message : String(error61);
|
|
42092
|
+
}
|
|
42093
|
+
function readJsonObject(path) {
|
|
42094
|
+
let text3;
|
|
42095
|
+
try {
|
|
42096
|
+
text3 = readFileSync8(path, "utf-8");
|
|
42097
|
+
} catch (error61) {
|
|
42098
|
+
if (error61?.code === "ENOENT")
|
|
42099
|
+
return { status: "absent" };
|
|
42100
|
+
return { status: "unreadable", kind: "file", reason: `cannot read ${path} (${redactedDiagnostic(messageOf(error61))})` };
|
|
42101
|
+
}
|
|
42102
|
+
try {
|
|
42103
|
+
const value = JSON.parse(text3);
|
|
42104
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
42105
|
+
return { status: "unreadable", kind: "content", reason: `${path} is not a JSON object` };
|
|
42106
|
+
}
|
|
42107
|
+
return { status: "verified", value };
|
|
42108
|
+
} catch (error61) {
|
|
42109
|
+
return { status: "unreadable", kind: "content", reason: `${path} is not valid JSON (${redactedDiagnostic(messageOf(error61))})` };
|
|
42110
|
+
}
|
|
42111
|
+
}
|
|
42112
|
+
function writeText(path, content) {
|
|
42113
|
+
const dir = dirname9(path);
|
|
42114
|
+
mkdirSync10(dir, { recursive: true });
|
|
42115
|
+
const temp = join22(dir, `.${basename9(path)}.${randomUUID5()}.tmp`);
|
|
42116
|
+
try {
|
|
42117
|
+
writeFileSync9(temp, content);
|
|
42118
|
+
if (existsSync13(path))
|
|
42119
|
+
chmodSync2(temp, statSync(path).mode & 4095);
|
|
42120
|
+
renameSync5(temp, path);
|
|
42121
|
+
} catch (error61) {
|
|
42122
|
+
try {
|
|
42123
|
+
if (existsSync13(temp))
|
|
42124
|
+
unlinkSync3(temp);
|
|
42125
|
+
} catch {}
|
|
42126
|
+
throw error61;
|
|
42127
|
+
}
|
|
42128
|
+
}
|
|
42129
|
+
function tomlString(value) {
|
|
42130
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
42131
|
+
}
|
|
42132
|
+
function tomlStringValue(raw) {
|
|
42133
|
+
return raw.replace(/\\"/g, '"').replace(/\\\\/g, "\\");
|
|
42134
|
+
}
|
|
42135
|
+
var TOML_HEADER_RE = /^\s*\[\[?[^\]]/;
|
|
42136
|
+
var KIMI_HOOK_HEADER_RE = /^\s*\[\[hooks\]\]\s*$/;
|
|
42137
|
+
function splitToml(text3) {
|
|
42138
|
+
const preamble = [];
|
|
42139
|
+
const chunks = [];
|
|
42140
|
+
let current2 = null;
|
|
42141
|
+
for (const line of text3.split(`
|
|
42142
|
+
`)) {
|
|
42143
|
+
if (TOML_HEADER_RE.test(line)) {
|
|
42144
|
+
if (current2)
|
|
42145
|
+
chunks.push(current2);
|
|
42146
|
+
current2 = { header: line.trim(), lines: [line] };
|
|
42147
|
+
} else if (current2) {
|
|
42148
|
+
current2.lines.push(line);
|
|
42149
|
+
} else {
|
|
42150
|
+
preamble.push(line);
|
|
42151
|
+
}
|
|
42152
|
+
}
|
|
42153
|
+
if (current2)
|
|
42154
|
+
chunks.push(current2);
|
|
42155
|
+
return { preamble, chunks };
|
|
42156
|
+
}
|
|
42157
|
+
function chunkCommand(chunk) {
|
|
42158
|
+
const text3 = chunk.lines.join(`
|
|
42159
|
+
`);
|
|
42160
|
+
const basic = text3.match(/^\s*command\s*=\s*"((?:[^"\\]|\\.)*)"/m);
|
|
42161
|
+
if (basic)
|
|
42162
|
+
return tomlStringValue(basic[1]);
|
|
42163
|
+
const literal2 = text3.match(/^\s*command\s*=\s*'([^']*)'/m);
|
|
42164
|
+
return literal2 ? literal2[1] : undefined;
|
|
42165
|
+
}
|
|
42166
|
+
function isKimiHookChunk(chunk) {
|
|
42167
|
+
return KIMI_HOOK_HEADER_RE.test(chunk.header);
|
|
42168
|
+
}
|
|
42169
|
+
function kimiRegistrations(text3) {
|
|
42170
|
+
const out = [];
|
|
42171
|
+
for (const chunk of splitToml(text3).chunks) {
|
|
42172
|
+
if (!isKimiHookChunk(chunk))
|
|
42173
|
+
continue;
|
|
42174
|
+
const command = chunkCommand(chunk);
|
|
42175
|
+
const hook = command ? hookFromCommand(command) : undefined;
|
|
42176
|
+
if (command && hook)
|
|
42177
|
+
out.push({ hook, command });
|
|
42178
|
+
}
|
|
42179
|
+
return out;
|
|
42180
|
+
}
|
|
42181
|
+
function kimiBlock(command, timeoutSeconds) {
|
|
42182
|
+
return [
|
|
42183
|
+
"[[hooks]]",
|
|
42184
|
+
'event = "Stop"',
|
|
42185
|
+
`command = ${tomlString(command)}`,
|
|
42186
|
+
`timeout = ${Math.max(1, Math.min(600, timeoutSeconds))}`
|
|
42187
|
+
].join(`
|
|
42188
|
+
`);
|
|
42189
|
+
}
|
|
42190
|
+
function removeKimiChunk(text3, hook) {
|
|
42191
|
+
const { preamble, chunks } = splitToml(text3);
|
|
42192
|
+
const kept = [...preamble];
|
|
42193
|
+
let removed = false;
|
|
42194
|
+
for (const chunk of chunks) {
|
|
42195
|
+
const command = isKimiHookChunk(chunk) ? chunkCommand(chunk) : undefined;
|
|
42196
|
+
if (command && hookFromCommand(command) === hook) {
|
|
42197
|
+
removed = true;
|
|
42198
|
+
while (kept.length > 0 && kept[kept.length - 1].trim() === "")
|
|
42199
|
+
kept.pop();
|
|
42200
|
+
continue;
|
|
42201
|
+
}
|
|
42202
|
+
if (TOML_HEADER_RE.test(chunk.header) && kept.length > 0 && kept[kept.length - 1].trim() !== "") {
|
|
42203
|
+
kept.push("");
|
|
42204
|
+
}
|
|
42205
|
+
kept.push(...chunk.lines);
|
|
42206
|
+
}
|
|
42207
|
+
return { text: kept.join(`
|
|
42208
|
+
`), removed };
|
|
42209
|
+
}
|
|
42210
|
+
function installKimi(hook, command, options, timeoutSeconds) {
|
|
42211
|
+
const path = adapterPath("kimi", options);
|
|
42212
|
+
const read3 = readTextState(path);
|
|
42213
|
+
if (read3.status === "unreadable") {
|
|
42214
|
+
return { ok: false, configPath: path, error: `Refused: ${read3.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` };
|
|
42215
|
+
}
|
|
42216
|
+
const existing = read3.status === "verified" ? read3.text : "";
|
|
42217
|
+
if (kimiRegistrations(existing).some((entry) => entry.hook === hook)) {
|
|
42218
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
42219
|
+
}
|
|
42220
|
+
const next = existing.trim() === "" ? `${kimiBlock(command, timeoutSeconds)}
|
|
42221
|
+
` : `${existing.trimEnd()}
|
|
42222
|
+
|
|
42223
|
+
${kimiBlock(command, timeoutSeconds)}
|
|
42224
|
+
`;
|
|
42225
|
+
const candidate = join22(tmpdir(), `hooks-kimi-config-${randomUUID5()}.toml`);
|
|
42226
|
+
writeFileSync9(candidate, next);
|
|
42227
|
+
let validationNote;
|
|
42228
|
+
try {
|
|
42229
|
+
const result = (options.run ?? defaultRunner)("kimi", ["doctor", "config", candidate]);
|
|
42230
|
+
if (result.notFound) {
|
|
42231
|
+
validationNote = "kimi is not installed here, so `kimi doctor config` did not validate the entry";
|
|
42232
|
+
} else if (result.status !== 0) {
|
|
42233
|
+
const detail = redactedDiagnostic(result.stderr) || redactedDiagnostic(result.stdout) || `exit ${result.status}`;
|
|
42234
|
+
return { ok: false, configPath: path, error: `Refused: \`kimi doctor config\` rejected the candidate config (${detail}). Nothing was written to ${path}.` };
|
|
42235
|
+
} else {
|
|
42236
|
+
validationNote = "validated with `kimi doctor config`";
|
|
42237
|
+
}
|
|
42238
|
+
} finally {
|
|
42239
|
+
try {
|
|
42240
|
+
if (existsSync13(candidate))
|
|
42241
|
+
unlinkSync3(candidate);
|
|
42242
|
+
} catch {}
|
|
42243
|
+
}
|
|
42244
|
+
writeText(path, next);
|
|
42245
|
+
return {
|
|
42246
|
+
ok: true,
|
|
42247
|
+
configPath: path,
|
|
42248
|
+
note: `Kimi blocks with exit 2 + stderr once per turn; ${validationNote}.`
|
|
42249
|
+
};
|
|
42250
|
+
}
|
|
42251
|
+
var PI_PREFERENCE_KEY = "hasna-pi-hooks";
|
|
42252
|
+
function piPointerPath(options) {
|
|
42253
|
+
return adapterPath("pi", options);
|
|
42254
|
+
}
|
|
42255
|
+
function readPiPointer(options) {
|
|
42256
|
+
return readJsonObject(piPointerPath(options));
|
|
42257
|
+
}
|
|
42258
|
+
function piProfile(options, pointer) {
|
|
42259
|
+
const state = pointer === undefined ? readPiPointer(options) : undefined;
|
|
42260
|
+
const config2 = pointer ?? (state?.status === "verified" ? state.value : undefined);
|
|
42261
|
+
if (typeof config2?.profile === "string") {
|
|
42262
|
+
const pointed = getProfile(config2.profile);
|
|
42263
|
+
if (pointed)
|
|
42264
|
+
return pointed;
|
|
42265
|
+
}
|
|
42266
|
+
return listProfiles().find((profile) => profile.agent_type === "custom" && profile.name === "pi");
|
|
42267
|
+
}
|
|
42268
|
+
function piCommands(profile) {
|
|
42269
|
+
const stored = profile?.preferences?.[PI_PREFERENCE_KEY];
|
|
42270
|
+
const hooks = stored?.hooks;
|
|
42271
|
+
return Array.isArray(hooks) ? hooks.filter((entry) => typeof entry === "string") : [];
|
|
42272
|
+
}
|
|
42273
|
+
function writePiCommands(profileId, commands) {
|
|
42274
|
+
const profile = getProfile(profileId);
|
|
42275
|
+
if (!profile)
|
|
42276
|
+
return;
|
|
42277
|
+
updateProfile(profileId, {
|
|
42278
|
+
preferences: {
|
|
42279
|
+
...profile.preferences,
|
|
42280
|
+
[PI_PREFERENCE_KEY]: { hooks: commands, updated_at: new Date().toISOString() }
|
|
42281
|
+
}
|
|
42282
|
+
});
|
|
42283
|
+
}
|
|
42284
|
+
function installPi(hook, command, options) {
|
|
42285
|
+
const path = piPointerPath(options);
|
|
42286
|
+
const pointerRead = readPiPointer(options);
|
|
42287
|
+
if (pointerRead.status === "unreadable") {
|
|
42288
|
+
return { ok: false, configPath: path, error: `Refused: ${pointerRead.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` };
|
|
42289
|
+
}
|
|
42290
|
+
const profile = piProfile(options) ?? createProfile({ agent_type: "custom", name: "pi" });
|
|
42291
|
+
const commands = piCommands(profile);
|
|
42292
|
+
if (commands.some((entry) => hookFromCommand(entry) === hook)) {
|
|
42293
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
42294
|
+
}
|
|
42295
|
+
const pointer = pointerRead.status === "verified" ? pointerRead.value : {};
|
|
42296
|
+
writeText(path, `${JSON.stringify({ ...pointer, profile: profile.agent_id }, null, 2)}
|
|
42297
|
+
`);
|
|
42298
|
+
writePiCommands(profile.agent_id, [...commands, command]);
|
|
42299
|
+
return {
|
|
42300
|
+
ok: true,
|
|
42301
|
+
configPath: path,
|
|
42302
|
+
note: `registered through Hooks profile ${profile.agent_id}, which the hasna-pi-hooks bridge reads`
|
|
42303
|
+
};
|
|
42304
|
+
}
|
|
42305
|
+
function readCursorFile(path) {
|
|
42306
|
+
return readJsonObject(path);
|
|
42307
|
+
}
|
|
42308
|
+
function cursorRegistrations(config2) {
|
|
42309
|
+
const out = [];
|
|
42310
|
+
const hooks = config2?.hooks;
|
|
42311
|
+
if (!hooks || typeof hooks !== "object")
|
|
42312
|
+
return out;
|
|
42313
|
+
for (const key2 of Object.keys(hooks)) {
|
|
42314
|
+
const entries = hooks[key2];
|
|
42315
|
+
if (!Array.isArray(entries))
|
|
42316
|
+
continue;
|
|
42317
|
+
for (const entry of entries) {
|
|
42318
|
+
const command = typeof entry?.command === "string" ? entry.command : undefined;
|
|
42319
|
+
const hook = command ? hookFromCommand(command) : undefined;
|
|
42320
|
+
if (command && hook)
|
|
42321
|
+
out.push({ hook, command });
|
|
42322
|
+
}
|
|
42323
|
+
}
|
|
42324
|
+
return out;
|
|
42325
|
+
}
|
|
42326
|
+
function installCursor(hook, command, options, timeoutSeconds) {
|
|
42327
|
+
const path = adapterPath("cursor", options);
|
|
42328
|
+
const read3 = readCursorFile(path);
|
|
42329
|
+
if (read3.status === "unreadable") {
|
|
42330
|
+
return {
|
|
42331
|
+
ok: false,
|
|
42332
|
+
configPath: path,
|
|
42333
|
+
error: read3.kind === "file" ? `Refused: ${read3.reason}, so Hooks wrote nothing. Repair the file or move it aside, then retry.` : `Refused: ${path} is not a JSON object, so Hooks wrote nothing. Repair the file or move it aside, then retry.`
|
|
42334
|
+
};
|
|
42335
|
+
}
|
|
42336
|
+
const config2 = read3.status === "verified" ? read3.value : undefined;
|
|
42337
|
+
if (cursorRegistrations(config2).some((entry) => entry.hook === hook)) {
|
|
42338
|
+
return { ok: true, configPath: path, note: "already registered; nothing was written" };
|
|
42339
|
+
}
|
|
42340
|
+
const next = config2 ?? {};
|
|
42341
|
+
const hooks = next.hooks && typeof next.hooks === "object" ? { ...next.hooks } : {};
|
|
42342
|
+
const stop = Array.isArray(hooks.stop) ? [...hooks.stop] : [];
|
|
42343
|
+
stop.push({ command, timeout: Math.max(1, Math.min(600, timeoutSeconds)) });
|
|
42344
|
+
hooks.stop = stop;
|
|
42345
|
+
writeText(path, `${JSON.stringify({ version: 1, ...next, hooks }, null, 2)}
|
|
42346
|
+
`);
|
|
42347
|
+
return { ok: true, configPath: path };
|
|
42348
|
+
}
|
|
42349
|
+
function splitArgv(command) {
|
|
42350
|
+
return command.trim().split(/\s+/).filter(Boolean);
|
|
42351
|
+
}
|
|
42352
|
+
function hookFromMuseCommand(command) {
|
|
42353
|
+
if (!Array.isArray(command))
|
|
42354
|
+
return;
|
|
42355
|
+
const joined = command.filter((part) => typeof part === "string").join(" ");
|
|
42356
|
+
const match = joined.match(/(?:^|\s)\S*hooks run ([\w-]+)(?:\s|$)/);
|
|
42357
|
+
return match?.[1];
|
|
42358
|
+
}
|
|
42359
|
+
function museManifest(hook, command, options, timeoutSeconds) {
|
|
42360
|
+
return {
|
|
42361
|
+
name: `hasna-hook-${hook}`,
|
|
42362
|
+
version: options.version ?? "0.0.0",
|
|
42363
|
+
description: options.description ?? `Hasna hook ${hook}`,
|
|
42364
|
+
schemaVersion: 1,
|
|
42365
|
+
compat: { source: "native", manifestDir: ".muse-plugin" },
|
|
42366
|
+
capabilities: {
|
|
42367
|
+
hooks: [{
|
|
42368
|
+
id: `hasna-${hook}-stop`,
|
|
42369
|
+
event: "Stop",
|
|
42370
|
+
command: splitArgv(command),
|
|
42371
|
+
timeoutMs: timeoutSeconds * 1000,
|
|
42372
|
+
statusMessage: `Running Hasna hook ${hook}`
|
|
42373
|
+
}]
|
|
42374
|
+
}
|
|
42375
|
+
};
|
|
42376
|
+
}
|
|
42377
|
+
function museDiagnostic(outcome) {
|
|
42378
|
+
try {
|
|
42379
|
+
const parsed = JSON.parse(outcome.stdout);
|
|
42380
|
+
const message = parsed?.error?.message;
|
|
42381
|
+
if (typeof message === "string" && message.trim()) {
|
|
42382
|
+
const code2 = parsed.error?.code === undefined ? "" : `${redactedCode(parsed.error.code)}: `;
|
|
42383
|
+
return `${code2}${redactedDiagnostic(message)}`;
|
|
42384
|
+
}
|
|
42385
|
+
} catch {}
|
|
42386
|
+
return redactedDiagnostic(outcome.stderr) || redactedDiagnostic(outcome.stdout) || `exit ${outcome.status}`;
|
|
42387
|
+
}
|
|
42388
|
+
function removeMuseBundle(bundle) {
|
|
42389
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
42390
|
+
try {
|
|
42391
|
+
if (existsSync13(manifestPath))
|
|
42392
|
+
unlinkSync3(manifestPath);
|
|
42393
|
+
} catch {}
|
|
42394
|
+
for (const dir of [join22(bundle, ".muse-plugin"), bundle]) {
|
|
42395
|
+
try {
|
|
42396
|
+
if (existsSync13(dir))
|
|
42397
|
+
rmdirSync2(dir);
|
|
42398
|
+
} catch {}
|
|
42399
|
+
}
|
|
42400
|
+
}
|
|
42401
|
+
function museBundles(options) {
|
|
42402
|
+
const root = musePluginRoot({ home: homeOf(options), env: options.env ?? process.env });
|
|
42403
|
+
if (!existsSync13(root))
|
|
42404
|
+
return [];
|
|
42405
|
+
const out = [];
|
|
42406
|
+
for (const entry of readdirSync8(root)) {
|
|
42407
|
+
const manifestPath = join22(root, entry, ".muse-plugin", "plugin.json");
|
|
42408
|
+
if (!existsSync13(manifestPath))
|
|
42409
|
+
continue;
|
|
42410
|
+
try {
|
|
42411
|
+
const manifest = JSON.parse(readFileSync8(manifestPath, "utf-8"));
|
|
42412
|
+
for (const capability of manifest.capabilities?.hooks ?? []) {
|
|
42413
|
+
const command = Array.isArray(capability.command) ? capability.command.join(" ") : "";
|
|
42414
|
+
const hook = hookFromCommand(command) ?? hookFromMuseCommand(capability.command);
|
|
42415
|
+
if (hook)
|
|
42416
|
+
out.push({ hook, command });
|
|
42417
|
+
}
|
|
42418
|
+
} catch {}
|
|
42419
|
+
}
|
|
42420
|
+
return out;
|
|
42421
|
+
}
|
|
42422
|
+
function museOwnFiles(options) {
|
|
42423
|
+
const home = homeOf(options);
|
|
42424
|
+
const env = options.env ?? process.env;
|
|
42425
|
+
const configBase = env.XDG_CONFIG_HOME?.startsWith("/") ? env.XDG_CONFIG_HOME : join22(home, ".config");
|
|
42426
|
+
const dataBase = env.XDG_DATA_HOME?.startsWith("/") ? env.XDG_DATA_HOME : join22(home, ".local", "share");
|
|
42427
|
+
const searched = [join22(configBase, "muse", "settings.json"), join22(dataBase, "muse")];
|
|
42428
|
+
const found = searched.filter((path) => existsSync13(path));
|
|
42429
|
+
return { present: found.length > 0, found, searched };
|
|
42430
|
+
}
|
|
42431
|
+
function readMuseState(options, runner = options.run ?? defaultRunner) {
|
|
42432
|
+
const source = "`muse plugins list --json`";
|
|
42433
|
+
const bundleRoot = musePluginRoot({ home: homeOf(options), env: options.env ?? process.env });
|
|
42434
|
+
const unverified = (reason2) => ({ status: "unverified", registrations: [], inactive: [], source, reason: reason2 });
|
|
42435
|
+
const result = runner("muse", ["plugins", "list", "--json"]);
|
|
42436
|
+
if (result.notFound) {
|
|
42437
|
+
const muse = museOwnFiles(options);
|
|
42438
|
+
if (muse.present) {
|
|
42439
|
+
return unverified(`Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so ${source} cannot say what Muse carries \u2014 ` + `a plugin Muse registered may still run, and a bundle under ${bundleRoot} is a file Hooks wrote, not proof either way`);
|
|
42440
|
+
}
|
|
42441
|
+
return {
|
|
42442
|
+
...unverified(`Muse is not installed here: neither its store (${muse.searched.join(" or ")}) nor the \`muse\` command exists, so it carries no registration`),
|
|
42443
|
+
absent: true
|
|
42444
|
+
};
|
|
42445
|
+
}
|
|
42446
|
+
if (result.status !== 0) {
|
|
42447
|
+
return unverified(`${source} failed (${museDiagnostic(result)})`);
|
|
42448
|
+
}
|
|
42449
|
+
let parsed;
|
|
42450
|
+
try {
|
|
42451
|
+
parsed = JSON.parse(result.stdout);
|
|
42452
|
+
} catch {
|
|
42453
|
+
return unverified(`${source} did not return JSON`);
|
|
42454
|
+
}
|
|
42455
|
+
const entries = parsed?.plugins;
|
|
42456
|
+
if (!Array.isArray(entries))
|
|
42457
|
+
return unverified(`${source} returned no plugin list`);
|
|
42458
|
+
const registrations = [];
|
|
42459
|
+
const inactive = [];
|
|
42460
|
+
for (const entry of entries) {
|
|
42461
|
+
const hooks = entry?.plugin?.capabilities?.hooks;
|
|
42462
|
+
if (!Array.isArray(hooks))
|
|
42463
|
+
continue;
|
|
42464
|
+
const enabled = entry?.record?.enabled === true;
|
|
42465
|
+
for (const capability of hooks) {
|
|
42466
|
+
const command = Array.isArray(capability?.command) ? capability.command.filter((part) => typeof part === "string").join(" ") : "";
|
|
42467
|
+
const hook = hookFromMuseCommand(capability?.command);
|
|
42468
|
+
if (!hook)
|
|
42469
|
+
continue;
|
|
42470
|
+
if (enabled) {
|
|
42471
|
+
registrations.push({ hook, command });
|
|
42472
|
+
} else if (!inactive.includes(hook)) {
|
|
42473
|
+
inactive.push(hook);
|
|
42474
|
+
}
|
|
42475
|
+
}
|
|
42476
|
+
}
|
|
42477
|
+
return { status: "verified", registrations, inactive, source };
|
|
42478
|
+
}
|
|
42479
|
+
function installMuse(hook, command, options, timeoutSeconds) {
|
|
42480
|
+
const runner = options.run ?? defaultRunner;
|
|
42481
|
+
const bundle = adapterPath("muse", options, hook);
|
|
42482
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
42483
|
+
const existedBefore = existsSync13(manifestPath);
|
|
42484
|
+
const manifest = museManifest(hook, command, options, timeoutSeconds);
|
|
42485
|
+
writeText(manifestPath, `${JSON.stringify(manifest, null, 2)}
|
|
42486
|
+
`);
|
|
42487
|
+
const installCommand = `muse plugins install ${bundle} --scope user`;
|
|
42488
|
+
const approveCommand = `muse plugins approve ${manifest.name}`;
|
|
42489
|
+
const cleanup = () => {
|
|
42490
|
+
if (!existedBefore)
|
|
42491
|
+
removeMuseBundle(bundle);
|
|
42492
|
+
};
|
|
42493
|
+
const left = existedBefore ? ` The existing bundle at ${bundle} was left as it was.` : " The bundle was removed again, so nothing is left half-written.";
|
|
42494
|
+
const validated = runner("muse", ["plugins", "validate", bundle, "--json"]);
|
|
42495
|
+
if (validated.notFound) {
|
|
42496
|
+
cleanup();
|
|
42497
|
+
const muse = museOwnFiles(options);
|
|
42498
|
+
return {
|
|
42499
|
+
ok: false,
|
|
42500
|
+
configPath: bundle,
|
|
42501
|
+
error: muse.present ? `Refused: Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so \`muse plugins validate\` could not check the bundle.${left} Run it where the Muse command is available: \`${installCommand}\` then \`${approveCommand}\`.` : `Refused: Muse is not installed here, so \`muse plugins validate\` could not check the bundle.${left} On a station with Muse run \`${installCommand}\` then \`${approveCommand}\`.`
|
|
42502
|
+
};
|
|
42503
|
+
}
|
|
42504
|
+
if (validated.status !== 0) {
|
|
42505
|
+
cleanup();
|
|
42506
|
+
return {
|
|
42507
|
+
ok: false,
|
|
42508
|
+
configPath: bundle,
|
|
42509
|
+
error: `Refused: Muse rejected the plugin manifest (${museDiagnostic(validated)}).${left} Nothing was registered.`
|
|
42510
|
+
};
|
|
42511
|
+
}
|
|
42512
|
+
const installed = runner("muse", ["plugins", "install", bundle, "--scope", "user"]);
|
|
42513
|
+
if (installed.notFound) {
|
|
42514
|
+
cleanup();
|
|
42515
|
+
return {
|
|
42516
|
+
ok: false,
|
|
42517
|
+
configPath: bundle,
|
|
42518
|
+
error: `Refused: Muse disappeared between validation and install, so the plugin was not registered.${left} On a station with Muse run \`${installCommand}\` then \`${approveCommand}\`.`
|
|
42519
|
+
};
|
|
42520
|
+
}
|
|
42521
|
+
if (installed.status !== 0) {
|
|
42522
|
+
cleanup();
|
|
42523
|
+
return {
|
|
42524
|
+
ok: false,
|
|
42525
|
+
configPath: bundle,
|
|
42526
|
+
error: `Refused: \`${installCommand}\` failed (${museDiagnostic(installed)}).${left}`
|
|
42527
|
+
};
|
|
42528
|
+
}
|
|
42529
|
+
const approved = runner("muse", ["plugins", "approve", manifest.name]);
|
|
42530
|
+
if (approved.status !== 0) {
|
|
42531
|
+
const detail = museDiagnostic(approved);
|
|
42532
|
+
return {
|
|
42533
|
+
ok: false,
|
|
42534
|
+
configPath: bundle,
|
|
42535
|
+
error: `Refused: the plugin installed but Muse did not approve it (${detail}); an unapproved plugin does not run. Run \`${approveCommand}\` and retry.`
|
|
42536
|
+
};
|
|
42537
|
+
}
|
|
42538
|
+
return { ok: true, configPath: bundle, note: `installed at user scope and approved as ${manifest.name}` };
|
|
42539
|
+
}
|
|
42540
|
+
function readTextState(path) {
|
|
42541
|
+
try {
|
|
42542
|
+
return { status: "verified", text: readFileSync8(path, "utf-8") };
|
|
42543
|
+
} catch (error61) {
|
|
42544
|
+
if (error61?.code === "ENOENT")
|
|
42545
|
+
return { status: "absent" };
|
|
42546
|
+
return { status: "unreadable", reason: `cannot read ${path} (${redactedDiagnostic(messageOf(error61))})` };
|
|
42547
|
+
}
|
|
42548
|
+
}
|
|
42549
|
+
function readKimiState(options) {
|
|
42550
|
+
const read3 = readTextState(adapterPath("kimi", options));
|
|
42551
|
+
if (read3.status === "unreadable")
|
|
42552
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
42553
|
+
if (read3.status === "absent")
|
|
42554
|
+
return { status: "verified", registrations: [] };
|
|
42555
|
+
return { status: "verified", registrations: kimiRegistrations(read3.text) };
|
|
42556
|
+
}
|
|
42557
|
+
function readPiState(options) {
|
|
42558
|
+
const read3 = readPiPointer(options);
|
|
42559
|
+
if (read3.status === "unreadable")
|
|
42560
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
42561
|
+
const profile = piProfile(options, read3.status === "verified" ? read3.value : {});
|
|
42562
|
+
const registrations = piCommands(profile).flatMap((command) => {
|
|
42563
|
+
const hook = hookFromCommand(command);
|
|
42564
|
+
return hook ? [{ hook, command }] : [];
|
|
42565
|
+
});
|
|
42566
|
+
return { status: "verified", registrations };
|
|
42567
|
+
}
|
|
42568
|
+
function readCursorState(options) {
|
|
42569
|
+
const read3 = readCursorFile(adapterPath("cursor", options));
|
|
42570
|
+
if (read3.status === "unreadable")
|
|
42571
|
+
return { status: "unverified", registrations: [], reason: read3.reason };
|
|
42572
|
+
if (read3.status === "absent")
|
|
42573
|
+
return { status: "verified", registrations: [] };
|
|
42574
|
+
return { status: "verified", registrations: cursorRegistrations(read3.value) };
|
|
42575
|
+
}
|
|
42576
|
+
function readAdapterState(target, options, runner) {
|
|
42577
|
+
try {
|
|
42578
|
+
if (target === "kimi")
|
|
42579
|
+
return readKimiState(options);
|
|
42580
|
+
if (target === "pi")
|
|
42581
|
+
return readPiState(options);
|
|
42582
|
+
if (target === "cursor")
|
|
42583
|
+
return readCursorState(options);
|
|
42584
|
+
return readMuseState(options, runner ?? options.run ?? defaultRunner);
|
|
42585
|
+
} catch (error61) {
|
|
42586
|
+
return {
|
|
42587
|
+
status: "unverified",
|
|
42588
|
+
registrations: [],
|
|
42589
|
+
reason: `${target}: cannot read its registration state (${redactedDiagnostic(messageOf(error61))})`
|
|
42590
|
+
};
|
|
42591
|
+
}
|
|
42592
|
+
}
|
|
42593
|
+
function readAdapterRegistrations(target, options) {
|
|
42594
|
+
if (target === "muse") {
|
|
42595
|
+
try {
|
|
42596
|
+
return museBundles(options);
|
|
42597
|
+
} catch {
|
|
42598
|
+
return [];
|
|
42599
|
+
}
|
|
42600
|
+
}
|
|
42601
|
+
return readAdapterState(target, options).registrations;
|
|
42602
|
+
}
|
|
42603
|
+
function adapterStateCarries(state, hook) {
|
|
42604
|
+
if (state.registrations.some((entry) => entry.hook === hook))
|
|
42605
|
+
return true;
|
|
42606
|
+
const inactive = state.inactive;
|
|
42607
|
+
return Array.isArray(inactive) && inactive.includes(hook);
|
|
42608
|
+
}
|
|
42609
|
+
function installInAdapter(target, hook, command, options, timeoutSeconds = 10) {
|
|
42610
|
+
try {
|
|
42611
|
+
if (target === "kimi")
|
|
42612
|
+
return installKimi(hook, command, options, timeoutSeconds);
|
|
42613
|
+
if (target === "pi")
|
|
42614
|
+
return installPi(hook, command, options);
|
|
42615
|
+
if (target === "cursor")
|
|
42616
|
+
return installCursor(hook, command, options, timeoutSeconds);
|
|
42617
|
+
return installMuse(hook, command, options, timeoutSeconds);
|
|
42618
|
+
} catch (error61) {
|
|
42619
|
+
return { ok: false, configPath: "", error: `Refused: ${target}: the install did not complete (${redactedDiagnostic(messageOf(error61))})` };
|
|
42620
|
+
}
|
|
42621
|
+
}
|
|
42622
|
+
function removeFromAdapter(target, hook, options) {
|
|
42623
|
+
try {
|
|
42624
|
+
if (target === "kimi") {
|
|
42625
|
+
const path = adapterPath("kimi", options);
|
|
42626
|
+
const existing = readTextState(path);
|
|
42627
|
+
if (existing.status === "unreadable") {
|
|
42628
|
+
return { removed: false, reason: `${existing.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
42629
|
+
}
|
|
42630
|
+
const { text: text3, removed } = removeKimiChunk(existing.status === "verified" ? existing.text : "", hook);
|
|
42631
|
+
if (!removed)
|
|
42632
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
42633
|
+
writeText(path, text3.endsWith(`
|
|
42634
|
+
`) ? text3 : `${text3}
|
|
42635
|
+
`);
|
|
42636
|
+
return { removed: true };
|
|
42637
|
+
}
|
|
42638
|
+
if (target === "pi") {
|
|
42639
|
+
const pointer = readPiPointer(options);
|
|
42640
|
+
if (pointer.status === "unreadable") {
|
|
42641
|
+
return { removed: false, reason: `${pointer.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
42642
|
+
}
|
|
42643
|
+
const profile = piProfile(options);
|
|
42644
|
+
if (!profile)
|
|
42645
|
+
return { removed: false, reason: `no Hooks profile is registered for Pi; nothing to remove` };
|
|
42646
|
+
const commands = piCommands(profile);
|
|
42647
|
+
const kept = commands.filter((command) => hookFromCommand(command) !== hook);
|
|
42648
|
+
if (kept.length === commands.length)
|
|
42649
|
+
return { removed: false, reason: `no '${hook}' entry in profile ${profile.agent_id}` };
|
|
42650
|
+
writePiCommands(profile.agent_id, kept);
|
|
42651
|
+
return { removed: true };
|
|
42652
|
+
}
|
|
42653
|
+
if (target === "cursor") {
|
|
42654
|
+
const path = adapterPath("cursor", options);
|
|
42655
|
+
const read3 = readCursorFile(path);
|
|
42656
|
+
if (read3.status === "unreadable") {
|
|
42657
|
+
return { removed: false, reason: `${read3.reason}, so Hooks cannot tell whether '${hook}' is registered; nothing was removed` };
|
|
42658
|
+
}
|
|
42659
|
+
const config2 = read3.status === "verified" ? read3.value : undefined;
|
|
42660
|
+
if (!config2?.hooks)
|
|
42661
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
42662
|
+
let removed = false;
|
|
42663
|
+
const hooks = {};
|
|
42664
|
+
for (const key2 of Object.keys(config2.hooks)) {
|
|
42665
|
+
const entries = config2.hooks[key2];
|
|
42666
|
+
if (!Array.isArray(entries)) {
|
|
42667
|
+
hooks[key2] = entries;
|
|
42668
|
+
continue;
|
|
42669
|
+
}
|
|
42670
|
+
const kept = entries.filter((entry) => {
|
|
42671
|
+
if (typeof entry?.command !== "string" || hookFromCommand(entry.command) !== hook)
|
|
42672
|
+
return true;
|
|
42673
|
+
removed = true;
|
|
42674
|
+
return false;
|
|
42675
|
+
});
|
|
42676
|
+
if (kept.length > 0)
|
|
42677
|
+
hooks[key2] = kept;
|
|
42678
|
+
}
|
|
42679
|
+
if (!removed)
|
|
42680
|
+
return { removed: false, reason: `no '${hook}' entry in ${path}` };
|
|
42681
|
+
const next = { ...config2, hooks };
|
|
42682
|
+
if (Object.keys(hooks).length === 0)
|
|
42683
|
+
delete next.hooks;
|
|
42684
|
+
writeText(path, `${JSON.stringify(next, null, 2)}
|
|
42685
|
+
`);
|
|
42686
|
+
return { removed: true };
|
|
42687
|
+
}
|
|
42688
|
+
const bundle = adapterPath("muse", options, hook);
|
|
42689
|
+
const manifestPath = join22(bundle, ".muse-plugin", "plugin.json");
|
|
42690
|
+
const hadBundle = existsSync13(manifestPath);
|
|
42691
|
+
const bundleNote = hadBundle ? `the bundle at ${bundle} was left in place` : `no bundle was present at ${bundle}`;
|
|
42692
|
+
const pluginId2 = `hasna-hook-${hook}`;
|
|
42693
|
+
const result = (options.run ?? defaultRunner)("muse", ["plugins", "remove", pluginId2, "--json"]);
|
|
42694
|
+
if (result.notFound) {
|
|
42695
|
+
const muse = museOwnFiles(options);
|
|
42696
|
+
return {
|
|
42697
|
+
removed: false,
|
|
42698
|
+
reason: muse.present ? `Refused: Muse's own store is here (${muse.found.join(", ")}), but the \`muse\` command is not on PATH, so '${hook}' cannot be unregistered from it and may still run; ${bundleNote}. Run \`muse plugins remove ${pluginId2}\` where the Muse command is available.` : `Refused: Muse is not installed here, so '${hook}' cannot be unregistered from Muse's own store; ${bundleNote}. Run \`muse plugins remove ${pluginId2}\` on a station with Muse.`
|
|
42699
|
+
};
|
|
42700
|
+
}
|
|
42701
|
+
const refusal3 = result.status === 0 ? undefined : museDiagnostic(result);
|
|
42702
|
+
const alreadyGone = refusal3 !== undefined && /unknown-plugin|not installed/i.test(refusal3);
|
|
42703
|
+
if (refusal3 !== undefined && !alreadyGone) {
|
|
42704
|
+
return {
|
|
42705
|
+
removed: false,
|
|
42706
|
+
reason: `Refused: \`muse plugins remove ${pluginId2}\` failed (${refusal3}); ${bundleNote}.`
|
|
42707
|
+
};
|
|
42708
|
+
}
|
|
42709
|
+
removeMuseBundle(bundle);
|
|
42710
|
+
return {
|
|
42711
|
+
removed: true,
|
|
42712
|
+
bundleRemoved: hadBundle,
|
|
42713
|
+
reason: alreadyGone ? `Muse did not carry ${pluginId2}; ${hadBundle ? `the bundle at ${bundle} was removed` : `no bundle was present at ${bundle}`}` : hadBundle ? `unregistered ${pluginId2} from Muse and removed ${bundle}` : `unregistered ${pluginId2} from Muse; no bundle was present at ${bundle}`
|
|
42714
|
+
};
|
|
42715
|
+
} catch (error61) {
|
|
42716
|
+
return { removed: false, reason: `${target}: the removal failed (${redactedDiagnostic(messageOf(error61))}); nothing was removed` };
|
|
42717
|
+
}
|
|
41741
42718
|
}
|
|
41742
42719
|
|
|
41743
42720
|
// src/lib/installer.ts
|
|
41744
|
-
var __dirname2 =
|
|
41745
|
-
var HOOKS_DIR =
|
|
42721
|
+
var __dirname2 = dirname16(fileURLToPath3(import.meta.url));
|
|
42722
|
+
var HOOKS_DIR = existsSync14(join24(__dirname2, "..", "..", "hooks", "hook-gitguard")) ? join24(__dirname2, "..", "..", "hooks") : join24(__dirname2, "..", "hooks");
|
|
41746
42723
|
function normalizeHookName(name) {
|
|
41747
42724
|
return name.startsWith("hook-") ? name : `hook-${name}`;
|
|
41748
42725
|
}
|
|
@@ -41759,17 +42736,19 @@ function removeHookEntriesByName(entries, hookName) {
|
|
|
41759
42736
|
return kept.length === entry.hooks.length ? [entry] : kept.length ? [{ ...entry, hooks: kept }] : [];
|
|
41760
42737
|
});
|
|
41761
42738
|
}
|
|
42739
|
+
var CLAUDE_EVENT_MAP = {
|
|
42740
|
+
PreToolUse: "PreToolUse",
|
|
42741
|
+
PostToolUse: "PostToolUse",
|
|
42742
|
+
Stop: "Stop",
|
|
42743
|
+
Notification: "Notification",
|
|
42744
|
+
SessionStart: "SessionStart",
|
|
42745
|
+
SessionEnd: "SessionEnd",
|
|
42746
|
+
UserPromptSubmit: "UserPromptSubmit",
|
|
42747
|
+
SubagentStart: null
|
|
42748
|
+
};
|
|
41762
42749
|
var EVENT_MAP = {
|
|
41763
|
-
claude:
|
|
41764
|
-
|
|
41765
|
-
PostToolUse: "PostToolUse",
|
|
41766
|
-
Stop: "Stop",
|
|
41767
|
-
Notification: "Notification",
|
|
41768
|
-
SessionStart: "SessionStart",
|
|
41769
|
-
SessionEnd: "SessionEnd",
|
|
41770
|
-
UserPromptSubmit: "UserPromptSubmit",
|
|
41771
|
-
SubagentStart: null
|
|
41772
|
-
},
|
|
42750
|
+
claude: CLAUDE_EVENT_MAP,
|
|
42751
|
+
qwen: CLAUDE_EVENT_MAP,
|
|
41773
42752
|
gemini: {
|
|
41774
42753
|
PreToolUse: "BeforeTool",
|
|
41775
42754
|
PostToolUse: "AfterTool",
|
|
@@ -41801,15 +42780,6 @@ var EVENT_MAP = {
|
|
|
41801
42780
|
SubagentStart: "SubagentStart"
|
|
41802
42781
|
}
|
|
41803
42782
|
};
|
|
41804
|
-
function getTargetSettingsDir(target) {
|
|
41805
|
-
if (target === "codex")
|
|
41806
|
-
return ".codex";
|
|
41807
|
-
if (target === "codewith")
|
|
41808
|
-
return ".codewith";
|
|
41809
|
-
if (target === "gemini")
|
|
41810
|
-
return ".gemini";
|
|
41811
|
-
return ".claude";
|
|
41812
|
-
}
|
|
41813
42783
|
function getGlobalSettingsPathOverride(target) {
|
|
41814
42784
|
if (target === "codex")
|
|
41815
42785
|
return process.env.HASNA_HOOKS_CODEX_SETTINGS_PATH ?? (process.env.CODEX_HOME ? resolve28(process.env.CODEX_HOME, "hooks.json") : undefined);
|
|
@@ -41817,34 +42787,38 @@ function getGlobalSettingsPathOverride(target) {
|
|
|
41817
42787
|
return process.env.HASNA_HOOKS_CLAUDE_SETTINGS_PATH ?? (process.env.CLAUDE_CONFIG_DIR ? resolve28(process.env.CLAUDE_CONFIG_DIR, "settings.json") : undefined);
|
|
41818
42788
|
if (target === "gemini")
|
|
41819
42789
|
return process.env.HASNA_HOOKS_GEMINI_SETTINGS_PATH;
|
|
42790
|
+
if (target === "qwen")
|
|
42791
|
+
return process.env.HASNA_HOOKS_QWEN_SETTINGS_PATH;
|
|
42792
|
+
if (target === "codewith")
|
|
42793
|
+
return process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH;
|
|
41820
42794
|
return;
|
|
41821
42795
|
}
|
|
42796
|
+
function capabilityOptions(scope, hook) {
|
|
42797
|
+
return { scope, home: homedir8(), cwd: process.cwd(), env: process.env, ...hook ? { hook } : {} };
|
|
42798
|
+
}
|
|
42799
|
+
function adapterOptions(scope, home, run) {
|
|
42800
|
+
return { scope, home: home ?? process.env.HOME ?? homedir8(), cwd: process.cwd(), env: process.env, ...run ? { run } : {} };
|
|
42801
|
+
}
|
|
41822
42802
|
function getSettingsPath(scope = "global", target = "claude", codewithConfigPath) {
|
|
41823
42803
|
if (target === "codewith" && codewithConfigPath)
|
|
41824
42804
|
return codewithConfigPath;
|
|
41825
|
-
if (target === "codewith" && scope === "global" && process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH) {
|
|
41826
|
-
return process.env.HASNA_HOOKS_CODEWITH_CONFIG_PATH;
|
|
41827
|
-
}
|
|
41828
42805
|
const globalOverride = scope === "global" ? getGlobalSettingsPathOverride(target) : undefined;
|
|
41829
42806
|
if (globalOverride)
|
|
41830
42807
|
return globalOverride;
|
|
41831
|
-
const
|
|
41832
|
-
if (
|
|
41833
|
-
return
|
|
41834
|
-
|
|
41835
|
-
return target === "codewith" ? join17(process.cwd(), dir, "config.toml") : join17(process.cwd(), dir, "settings.json");
|
|
41836
|
-
}
|
|
41837
|
-
return target === "codewith" ? join17(homedir7(), dir, "config.toml") : join17(homedir7(), dir, "settings.json");
|
|
42808
|
+
const capabilities = getTargetCapabilities(target, capabilityOptions(scope));
|
|
42809
|
+
if (capabilities)
|
|
42810
|
+
return capabilities.configPath;
|
|
42811
|
+
return join24(homedir8(), ".claude", "settings.json");
|
|
41838
42812
|
}
|
|
41839
42813
|
function getHookPath(name) {
|
|
41840
42814
|
const shortName = shortHookName(name);
|
|
41841
42815
|
const resolved = resolveHookDir(shortName);
|
|
41842
42816
|
if (resolved)
|
|
41843
42817
|
return resolved;
|
|
41844
|
-
return
|
|
42818
|
+
return join24(HOOKS_DIR, normalizeHookName(shortName));
|
|
41845
42819
|
}
|
|
41846
42820
|
function hookExists(name) {
|
|
41847
|
-
return
|
|
42821
|
+
return existsSync14(getHookPath(name));
|
|
41848
42822
|
}
|
|
41849
42823
|
var settingsPreimages = new WeakMap;
|
|
41850
42824
|
var unreadableSettings = new WeakMap;
|
|
@@ -41854,8 +42828,8 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
41854
42828
|
return readCodexSettings(path);
|
|
41855
42829
|
let reason2 = "cannot be read";
|
|
41856
42830
|
try {
|
|
41857
|
-
if (
|
|
41858
|
-
const text3 =
|
|
42831
|
+
if (existsSync14(path)) {
|
|
42832
|
+
const text3 = readFileSync13(path, "utf-8");
|
|
41859
42833
|
reason2 = "is not valid JSON";
|
|
41860
42834
|
const value = JSON.parse(text3);
|
|
41861
42835
|
reason2 = "does not hold a JSON object";
|
|
@@ -41871,7 +42845,7 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
41871
42845
|
console.warn("[hooks] Cannot read hook settings; repair the selected file before installation.");
|
|
41872
42846
|
}
|
|
41873
42847
|
const empty = {};
|
|
41874
|
-
if (!
|
|
42848
|
+
if (!existsSync14(path))
|
|
41875
42849
|
settingsPreimages.set(empty, { path, text: null });
|
|
41876
42850
|
else
|
|
41877
42851
|
unreadableSettings.set(empty, { path, reason: reason2 });
|
|
@@ -41879,19 +42853,19 @@ function readSettings(scope = "global", target = "claude") {
|
|
|
41879
42853
|
}
|
|
41880
42854
|
function unreadableSettingsRefusal(settings, path) {
|
|
41881
42855
|
const captured = settingsPreimages.get(settings);
|
|
41882
|
-
if (captured?.path === path && captured.text !== null || !
|
|
42856
|
+
if (captured?.path === path && captured.text !== null || !existsSync14(path))
|
|
41883
42857
|
return;
|
|
41884
42858
|
const failed = unreadableSettings.get(settings);
|
|
41885
42859
|
const reason2 = captured?.path === path ? "was created after Hooks read the settings" : failed?.path === path ? failed.reason : "was not read as a settings object";
|
|
41886
42860
|
return new ClaudeSettingsRefusal("settings_unreadable", `${path} ${reason2}, so Hooks wrote nothing. Repair the file or move it aside, then retry.`);
|
|
41887
42861
|
}
|
|
41888
42862
|
function settingsIdentity(path) {
|
|
41889
|
-
if (
|
|
42863
|
+
if (existsSync14(path))
|
|
41890
42864
|
return realpathSync4(path);
|
|
41891
|
-
return
|
|
42865
|
+
return existsSync14(dirname16(path)) ? join24(realpathSync4(dirname16(path)), basename12(path)) : resolve28(path);
|
|
41892
42866
|
}
|
|
41893
42867
|
function coordinatesWithSkills(path, target) {
|
|
41894
|
-
return target === "claude" && settingsIdentity(path) === settingsIdentity(
|
|
42868
|
+
return target === "claude" && settingsIdentity(path) === settingsIdentity(join24(homedir8(), ".claude/settings.json"));
|
|
41895
42869
|
}
|
|
41896
42870
|
function writerRefusal(error61) {
|
|
41897
42871
|
return { error: error61 instanceof Error ? error61.message : "Unknown error", ...error61 instanceof ClaudeSettingsRefusal ? { code: error61.code } : {} };
|
|
@@ -41913,21 +42887,21 @@ function writeSettings(settings, scope = "global", target = "claude", expectedSe
|
|
|
41913
42887
|
const before = settingsPreimages.get(settings);
|
|
41914
42888
|
if (!before || before.path !== path)
|
|
41915
42889
|
throw new Error("Claude settings require a valid captured preimage before installation.");
|
|
41916
|
-
const current2 =
|
|
42890
|
+
const current2 = existsSync14(path) ? readFileSync13(path, "utf8") : null;
|
|
41917
42891
|
if (current2 !== before.text)
|
|
41918
42892
|
throw new Error("Claude settings changed during installation; no settings were written.");
|
|
41919
42893
|
if (coordinateClaudeSettings(before.text, JSON.stringify(settings, null, 2) + `
|
|
41920
|
-
`, { home:
|
|
42894
|
+
`, { home: homedir8() }, beforeWrite))
|
|
41921
42895
|
return;
|
|
41922
42896
|
}
|
|
41923
42897
|
if (expectedSettingsSha256 !== undefined)
|
|
41924
42898
|
return writeHookSettingsPreimage(path, settings, captured.text, beforeWrite);
|
|
41925
42899
|
beforeWrite?.();
|
|
41926
|
-
const dir =
|
|
41927
|
-
if (!
|
|
41928
|
-
|
|
42900
|
+
const dir = dirname16(path);
|
|
42901
|
+
if (!existsSync14(dir)) {
|
|
42902
|
+
mkdirSync12(dir, { recursive: true });
|
|
41929
42903
|
}
|
|
41930
|
-
|
|
42904
|
+
writeFileSync10(path, JSON.stringify(settings, null, 2) + `
|
|
41931
42905
|
`);
|
|
41932
42906
|
}
|
|
41933
42907
|
function getTargetEventName(internalEvent, target) {
|
|
@@ -41936,7 +42910,7 @@ function getTargetEventName(internalEvent, target) {
|
|
|
41936
42910
|
function isEventSupported(internalEvent, target) {
|
|
41937
42911
|
return getTargetEventName(internalEvent, target) !== null;
|
|
41938
42912
|
}
|
|
41939
|
-
function
|
|
42913
|
+
function tomlString2(value) {
|
|
41940
42914
|
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"`;
|
|
41941
42915
|
}
|
|
41942
42916
|
function codewithMatcher(matcher) {
|
|
@@ -42010,8 +42984,8 @@ function buildCodewithTomlFragment(name, profile, mementos) {
|
|
|
42010
42984
|
`[[hooks.${eventKey}]]`
|
|
42011
42985
|
];
|
|
42012
42986
|
if (matcher)
|
|
42013
|
-
lines.push(`matcher = ${
|
|
42014
|
-
lines.push("", `[[hooks.${eventKey}.hooks]]`, `type = "command"`, `command = ${
|
|
42987
|
+
lines.push(`matcher = ${tomlString2(matcher)}`);
|
|
42988
|
+
lines.push("", `[[hooks.${eventKey}.hooks]]`, `type = "command"`, `command = ${tomlString2(command)}`, `timeout = ${codewithTimeout(shortName)}`, `statusMessage = ${tomlString2(codewithStatusMessage(shortName))}`);
|
|
42015
42989
|
fragments.push(lines.join(`
|
|
42016
42990
|
`));
|
|
42017
42991
|
}
|
|
@@ -42023,21 +42997,21 @@ function buildCodewithTomlFragment(name, profile, mementos) {
|
|
|
42023
42997
|
function readCodewithConfig(scope, configPath) {
|
|
42024
42998
|
const path = getSettingsPath(scope, "codewith", configPath);
|
|
42025
42999
|
try {
|
|
42026
|
-
return
|
|
43000
|
+
return existsSync14(path) ? readFileSync13(path, "utf-8") : "";
|
|
42027
43001
|
} catch {
|
|
42028
43002
|
return "";
|
|
42029
43003
|
}
|
|
42030
43004
|
}
|
|
42031
43005
|
function appendCodewithFragment(fragment, scope, configPath) {
|
|
42032
43006
|
const path = getSettingsPath(scope, "codewith", configPath);
|
|
42033
|
-
const dir =
|
|
42034
|
-
if (!
|
|
42035
|
-
|
|
42036
|
-
const existing =
|
|
43007
|
+
const dir = dirname16(path);
|
|
43008
|
+
if (!existsSync14(dir))
|
|
43009
|
+
mkdirSync12(dir, { recursive: true });
|
|
43010
|
+
const existing = existsSync14(path) ? readFileSync13(path, "utf-8") : "";
|
|
42037
43011
|
const sep10 = existing.trim() ? `
|
|
42038
43012
|
|
|
42039
43013
|
` : "";
|
|
42040
|
-
|
|
43014
|
+
writeFileSync10(path, `${existing.replace(/\s*$/, "")}${sep10}${fragment}`);
|
|
42041
43015
|
return path;
|
|
42042
43016
|
}
|
|
42043
43017
|
function detectConflict(name, scope, target) {
|
|
@@ -42080,14 +43054,59 @@ function detectRewriteConflict(name, scope, target, lookup = getHook) {
|
|
|
42080
43054
|
}
|
|
42081
43055
|
return;
|
|
42082
43056
|
}
|
|
43057
|
+
function installForAdapter(shortName, scope, overwrite, target, events3, timeoutSeconds, profile, mementos) {
|
|
43058
|
+
const stopEvent = getTargetCapabilities(target, capabilityOptions(scope, shortName))?.stopEvent ?? "turn-end";
|
|
43059
|
+
const unsupported = events3.filter((event) => event !== "Stop");
|
|
43060
|
+
if (events3.length === 0 || unsupported.length > 0) {
|
|
43061
|
+
return {
|
|
43062
|
+
hook: shortName,
|
|
43063
|
+
success: false,
|
|
43064
|
+
scope,
|
|
43065
|
+
target,
|
|
43066
|
+
error: `Refused: '${shortName}' runs on ${events3.length > 0 ? events3.join(", ") : "no event"}, and target '${target}' only has the ${stopEvent} turn-end event. Install it on claude, codex, gemini, codewith or qwen; a harness config is never given an event it does not carry.`
|
|
43067
|
+
};
|
|
43068
|
+
}
|
|
43069
|
+
const options = adapterOptions(scope);
|
|
43070
|
+
const registered = readAdapterRegistrations(target, options).some((entry) => entry.hook === shortName);
|
|
43071
|
+
if (registered && !overwrite) {
|
|
43072
|
+
return { hook: shortName, success: false, error: "Already installed. Use --overwrite to replace.", scope, target };
|
|
43073
|
+
}
|
|
43074
|
+
if (registered)
|
|
43075
|
+
removeFromAdapter(target, shortName, options);
|
|
43076
|
+
const conflict = detectConflict(shortName, scope, target);
|
|
43077
|
+
const command = (profile ? `hooks run ${shortName} --profile ${profile}` : `hooks run ${shortName}`) + ` --target ${target}` + mementosCommandSuffix(mementos);
|
|
43078
|
+
const outcome = installInAdapter(target, shortName, command, options, timeoutSeconds);
|
|
43079
|
+
if (!outcome.ok) {
|
|
43080
|
+
return { hook: shortName, success: false, error: outcome.error, scope, target, ...outcome.configPath ? { configPath: outcome.configPath } : {} };
|
|
43081
|
+
}
|
|
43082
|
+
const note = [outcome.note, targetInstallNote(target)].filter(Boolean).join(" ");
|
|
43083
|
+
return {
|
|
43084
|
+
hook: shortName,
|
|
43085
|
+
success: true,
|
|
43086
|
+
scope,
|
|
43087
|
+
target,
|
|
43088
|
+
applied: true,
|
|
43089
|
+
conflict,
|
|
43090
|
+
configPath: outcome.configPath,
|
|
43091
|
+
...note ? { note } : {}
|
|
43092
|
+
};
|
|
43093
|
+
}
|
|
42083
43094
|
function installForTarget(name, scope, overwrite, target, profile, codewithMode = "fragment", codewithConfigPath, mementos, expectedSettingsSha256, beforeWrite) {
|
|
42084
43095
|
const shortName = shortHookName(name);
|
|
42085
|
-
if (isNativeSafetyName(shortName) && (profile || target === "gemini" || target === "codewith" && codewithMode === "write")) {
|
|
43096
|
+
if (isNativeSafetyName(shortName) && (profile || target === "gemini" || isAdapterTarget(target) || target === "codewith" && codewithMode === "write")) {
|
|
42086
43097
|
return { hook: shortName, success: false, error: "Bundled native safety guards support Claude/Codex and managed Codewith fragments, without registry profiles.", target };
|
|
42087
43098
|
}
|
|
42088
43099
|
if (!isNativeSafetyName(shortName) && !hookExists(shortName)) {
|
|
42089
43100
|
return { hook: shortName, success: false, error: `Hook '${shortName}' not found`, target };
|
|
42090
43101
|
}
|
|
43102
|
+
const meta3 = isNativeSafetyName(shortName) ? getHook(shortName) : resolveHookMeta(shortName);
|
|
43103
|
+
const events3 = meta3 ? getHookEvents(meta3) : [];
|
|
43104
|
+
const capabilityRefusal = installCapabilityRefusal(shortName, target, { events: events3, requiresFinalMessage: meta3?.requiresFinalMessage === true, requiresBlock: meta3?.requiresBlock === true }, capabilityOptions(scope, shortName));
|
|
43105
|
+
if (capabilityRefusal)
|
|
43106
|
+
return { hook: shortName, success: false, error: capabilityRefusal, scope, target };
|
|
43107
|
+
if (isAdapterTarget(target)) {
|
|
43108
|
+
return installForAdapter(shortName, scope, overwrite, target, events3, meta3?.timeoutSeconds ?? 10, profile, mementos);
|
|
43109
|
+
}
|
|
42091
43110
|
if (target === "codewith") {
|
|
42092
43111
|
try {
|
|
42093
43112
|
const fragment = buildCodewithTomlFragment(shortName, profile, mementos);
|
|
@@ -42138,7 +43157,8 @@ function installForTarget(name, scope, overwrite, target, profile, codewithMode
|
|
|
42138
43157
|
}
|
|
42139
43158
|
}
|
|
42140
43159
|
try {
|
|
42141
|
-
const
|
|
43160
|
+
const settingsTarget = target;
|
|
43161
|
+
const registered = getRegisteredHooksForTarget(scope, settingsTarget);
|
|
42142
43162
|
if (registered.includes(shortName) && !overwrite) {
|
|
42143
43163
|
return { hook: shortName, success: false, error: "Already installed. Use --overwrite to replace.", scope, target };
|
|
42144
43164
|
}
|
|
@@ -42147,7 +43167,7 @@ function installForTarget(name, scope, overwrite, target, profile, codewithMode
|
|
|
42147
43167
|
if (rewriteConflict) {
|
|
42148
43168
|
return { hook: shortName, success: false, error: `Refused: ${rewriteConflict}`, scope, target };
|
|
42149
43169
|
}
|
|
42150
|
-
registerHook(shortName, scope,
|
|
43170
|
+
registerHook(shortName, scope, settingsTarget, profile, mementos, (settings, selectedScope, selectedTarget) => writeSettings(settings, selectedScope, selectedTarget, expectedSettingsSha256, beforeWrite));
|
|
42151
43171
|
return {
|
|
42152
43172
|
hook: shortName,
|
|
42153
43173
|
success: true,
|
|
@@ -42330,6 +43350,9 @@ function unregisterHook(name, scope = "global", target = "claude") {
|
|
|
42330
43350
|
writeSettings(settings, scope, target);
|
|
42331
43351
|
}
|
|
42332
43352
|
function getRegisteredHooksForTarget(scope = "global", target = "claude") {
|
|
43353
|
+
if (isAdapterTarget(target)) {
|
|
43354
|
+
return [...new Set(readAdapterRegistrations(target, adapterOptions(scope)).map((entry) => entry.hook))];
|
|
43355
|
+
}
|
|
42333
43356
|
if (target === "codewith") {
|
|
42334
43357
|
const config2 = readCodewithConfig(scope);
|
|
42335
43358
|
const registered2 = [];
|
|
@@ -42368,20 +43391,37 @@ function getRegisteredHooksForTarget(scope = "global", target = "claude") {
|
|
|
42368
43391
|
function getRegisteredHooks(scope = "global") {
|
|
42369
43392
|
return getRegisteredHooksForTarget(scope, "claude");
|
|
42370
43393
|
}
|
|
42371
|
-
function removeHook(name, scope = "global", target = "claude") {
|
|
43394
|
+
function removeHook(name, scope = "global", target = "claude", options = {}) {
|
|
42372
43395
|
const shortName = shortHookName(name);
|
|
42373
43396
|
if (target === "all") {
|
|
42374
|
-
|
|
42375
|
-
const
|
|
42376
|
-
|
|
42377
|
-
|
|
43397
|
+
let removedAny = false;
|
|
43398
|
+
for (const candidate of ALL_REMOVABLE_TARGETS) {
|
|
43399
|
+
if (removeHookForTarget(shortName, scope, candidate, options.run))
|
|
43400
|
+
removedAny = true;
|
|
43401
|
+
}
|
|
43402
|
+
return removedAny;
|
|
42378
43403
|
}
|
|
42379
43404
|
if (target === "codewith") {
|
|
42380
43405
|
return false;
|
|
42381
43406
|
}
|
|
42382
|
-
|
|
43407
|
+
if (isAdapterTarget(target)) {
|
|
43408
|
+
return removeFromAdapter(target, shortName, adapterOptions(scope, undefined, options.run)).removed;
|
|
43409
|
+
}
|
|
43410
|
+
if (target === "opencode" || target === "sumi") {
|
|
43411
|
+
return false;
|
|
43412
|
+
}
|
|
43413
|
+
return removeHookForTarget(shortName, scope, target, options.run);
|
|
42383
43414
|
}
|
|
42384
|
-
function removeHookForTarget(name, scope, target) {
|
|
43415
|
+
function removeHookForTarget(name, scope, target, run) {
|
|
43416
|
+
if (isAdapterTarget(target)) {
|
|
43417
|
+
const options = adapterOptions(scope, undefined, run);
|
|
43418
|
+
const state = readAdapterState(target, options);
|
|
43419
|
+
if (state.status !== "verified")
|
|
43420
|
+
return false;
|
|
43421
|
+
if (!adapterStateCarries(state, name))
|
|
43422
|
+
return false;
|
|
43423
|
+
return removeFromAdapter(target, name, options).removed;
|
|
43424
|
+
}
|
|
42385
43425
|
const registered = getRegisteredHooksForTarget(scope, target);
|
|
42386
43426
|
if (!registered.includes(name)) {
|
|
42387
43427
|
return false;
|
|
@@ -42389,78 +43429,8 @@ function removeHookForTarget(name, scope, target) {
|
|
|
42389
43429
|
unregisterHook(name, scope, target);
|
|
42390
43430
|
return true;
|
|
42391
43431
|
}
|
|
42392
|
-
|
|
42393
|
-
|
|
42394
|
-
init_app_home();
|
|
42395
|
-
import { existsSync as existsSync13, mkdirSync as mkdirSync10, readFileSync as readFileSync8, writeFileSync as writeFileSync9, readdirSync as readdirSync8, rmSync as rmSync3, cpSync as cpSync2 } from "fs";
|
|
42396
|
-
import { join as join20 } from "path";
|
|
42397
|
-
function getProfilesDir() {
|
|
42398
|
-
const newDir = join20(getEffectiveDataRoot(), "profiles");
|
|
42399
|
-
const oldDir = join20(getHomeDir(), ".hooks", "profiles");
|
|
42400
|
-
if (!existsSync13(newDir) && existsSync13(oldDir)) {
|
|
42401
|
-
mkdirSync10(newDir, { recursive: true });
|
|
42402
|
-
cpSync2(oldDir, newDir, { recursive: true });
|
|
42403
|
-
}
|
|
42404
|
-
return newDir;
|
|
42405
|
-
}
|
|
42406
|
-
function ensureProfilesDir() {
|
|
42407
|
-
const dir = getProfilesDir();
|
|
42408
|
-
if (!existsSync13(dir)) {
|
|
42409
|
-
mkdirSync10(dir, { recursive: true });
|
|
42410
|
-
}
|
|
42411
|
-
}
|
|
42412
|
-
function profilePath(id) {
|
|
42413
|
-
return join20(getProfilesDir(), `${id}.json`);
|
|
42414
|
-
}
|
|
42415
|
-
function shortUuid() {
|
|
42416
|
-
return crypto.randomUUID().slice(0, 8);
|
|
42417
|
-
}
|
|
42418
|
-
function createProfile(input2) {
|
|
42419
|
-
ensureProfilesDir();
|
|
42420
|
-
const id = shortUuid();
|
|
42421
|
-
const now = new Date().toISOString();
|
|
42422
|
-
const profile = {
|
|
42423
|
-
agent_id: id,
|
|
42424
|
-
agent_type: input2.agent_type,
|
|
42425
|
-
created_at: now,
|
|
42426
|
-
last_seen_at: now,
|
|
42427
|
-
preferences: {}
|
|
42428
|
-
};
|
|
42429
|
-
if (input2.name) {
|
|
42430
|
-
profile.name = input2.name;
|
|
42431
|
-
}
|
|
42432
|
-
writeFileSync9(profilePath(id), JSON.stringify(profile, null, 2) + `
|
|
42433
|
-
`);
|
|
42434
|
-
return profile;
|
|
42435
|
-
}
|
|
42436
|
-
function getProfile(id) {
|
|
42437
|
-
const path = profilePath(id);
|
|
42438
|
-
try {
|
|
42439
|
-
if (!existsSync13(path))
|
|
42440
|
-
return null;
|
|
42441
|
-
return JSON.parse(readFileSync8(path, "utf-8"));
|
|
42442
|
-
} catch {
|
|
42443
|
-
return null;
|
|
42444
|
-
}
|
|
42445
|
-
}
|
|
42446
|
-
function listProfiles() {
|
|
42447
|
-
const dir = getProfilesDir();
|
|
42448
|
-
if (!existsSync13(dir))
|
|
42449
|
-
return [];
|
|
42450
|
-
try {
|
|
42451
|
-
const files = readdirSync8(dir).filter((f) => f.endsWith(".json"));
|
|
42452
|
-
const profiles = [];
|
|
42453
|
-
for (const file2 of files) {
|
|
42454
|
-
try {
|
|
42455
|
-
const content = readFileSync8(join20(dir, file2), "utf-8");
|
|
42456
|
-
profiles.push(JSON.parse(content));
|
|
42457
|
-
} catch {}
|
|
42458
|
-
}
|
|
42459
|
-
return profiles.sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime());
|
|
42460
|
-
} catch {
|
|
42461
|
-
return [];
|
|
42462
|
-
}
|
|
42463
|
-
}
|
|
43432
|
+
var JSON_SETTINGS_TARGETS = ["claude", "gemini", "codex", "qwen"];
|
|
43433
|
+
var ALL_REMOVABLE_TARGETS = [...JSON_SETTINGS_TARGETS, ...ADAPTER_TARGETS];
|
|
42464
43434
|
|
|
42465
43435
|
// src/mcp/server.ts
|
|
42466
43436
|
init_manifest();
|
|
@@ -43018,13 +43988,13 @@ function coerceForSqlite(value) {
|
|
|
43018
43988
|
return String(value);
|
|
43019
43989
|
}
|
|
43020
43990
|
// src/mcp/server.ts
|
|
43021
|
-
var __dirname3 =
|
|
43991
|
+
var __dirname3 = dirname29(fileURLToPath4(import.meta.url));
|
|
43022
43992
|
var pkg = { name: "@hasna/hooks", version: "0.0.0" };
|
|
43023
43993
|
try {
|
|
43024
43994
|
for (const rel of ["../../package.json", "../package.json", "../../../package.json"]) {
|
|
43025
|
-
const p =
|
|
43026
|
-
if (
|
|
43027
|
-
pkg = JSON.parse(
|
|
43995
|
+
const p = join34(__dirname3, rel);
|
|
43996
|
+
if (existsSync16(p)) {
|
|
43997
|
+
pkg = JSON.parse(readFileSync18(p, "utf-8"));
|
|
43028
43998
|
break;
|
|
43029
43999
|
}
|
|
43030
44000
|
}
|
|
@@ -43033,10 +44003,10 @@ async function runVerifiedHook(name, stdin, timeoutMs) {
|
|
|
43033
44003
|
const { resolveHook: resolveHook2, resolveScriptPath: resolveScriptPath2 } = await Promise.resolve().then(() => (init_resolve(), exports_resolve));
|
|
43034
44004
|
const resolved = resolveHook2(name);
|
|
43035
44005
|
const hookScript = resolveScriptPath2(name);
|
|
43036
|
-
if (!resolved || !hookScript || !
|
|
44006
|
+
if (!resolved || !hookScript || !existsSync16(hookScript)) {
|
|
43037
44007
|
throw new Error(`Hook '${name}' not found`);
|
|
43038
44008
|
}
|
|
43039
|
-
const content =
|
|
44009
|
+
const content = readFileSync18(hookScript);
|
|
43040
44010
|
const sha7 = sha256Of(content);
|
|
43041
44011
|
const check2 = checkScriptHash(name, sha7);
|
|
43042
44012
|
if (!check2.ok) {
|
|
@@ -43248,7 +44218,7 @@ function createHooksServer() {
|
|
|
43248
44218
|
const settingsPath = getSettingsPath(scope);
|
|
43249
44219
|
const issues = [];
|
|
43250
44220
|
const healthy = [];
|
|
43251
|
-
const settingsExist =
|
|
44221
|
+
const settingsExist = existsSync16(settingsPath);
|
|
43252
44222
|
if (!settingsExist) {
|
|
43253
44223
|
issues.push({ hook: "(settings)", issue: `${settingsPath} not found`, severity: "warning" });
|
|
43254
44224
|
}
|
|
@@ -43258,7 +44228,7 @@ function createHooksServer() {
|
|
|
43258
44228
|
const meta3 = custom2 ? resolveHookMeta(name) : getHook(name);
|
|
43259
44229
|
let hookHealthy = true;
|
|
43260
44230
|
if (custom2) {
|
|
43261
|
-
if (!
|
|
44231
|
+
if (!existsSync16(custom2.scriptPath)) {
|
|
43262
44232
|
issues.push({ hook: name, issue: `Custom hook script missing: ${custom2.scriptPath}`, severity: "error" });
|
|
43263
44233
|
hookHealthy = false;
|
|
43264
44234
|
}
|
|
@@ -43268,14 +44238,14 @@ function createHooksServer() {
|
|
|
43268
44238
|
continue;
|
|
43269
44239
|
}
|
|
43270
44240
|
const hookDir = getHookPath(name);
|
|
43271
|
-
if (!
|
|
44241
|
+
if (!existsSync16(join34(hookDir, "src", "hook.ts"))) {
|
|
43272
44242
|
issues.push({ hook: name, issue: "Missing src/hook.ts in package", severity: "error" });
|
|
43273
44243
|
hookHealthy = false;
|
|
43274
44244
|
}
|
|
43275
44245
|
}
|
|
43276
44246
|
if (meta3 && settingsExist) {
|
|
43277
44247
|
try {
|
|
43278
|
-
const settings = JSON.parse(
|
|
44248
|
+
const settings = JSON.parse(readFileSync18(settingsPath, "utf-8"));
|
|
43279
44249
|
if (!hookRegisteredInSettings(settings, name, meta3.event, matcherFor(meta3, "claude"))) {
|
|
43280
44250
|
const eventName = meta3.event.split(":")[0];
|
|
43281
44251
|
issues.push({ hook: name, issue: `Not registered under correct event (${eventName})`, severity: "error" });
|
|
@@ -43312,10 +44282,10 @@ function createHooksServer() {
|
|
|
43312
44282
|
return { content: [{ type: "text", text: JSON.stringify({ error: `Hook '${name}' not found` }) }] };
|
|
43313
44283
|
}
|
|
43314
44284
|
const hookPath = getHookPath(name);
|
|
43315
|
-
const readmePath =
|
|
44285
|
+
const readmePath = join34(hookPath, "README.md");
|
|
43316
44286
|
let readme = "";
|
|
43317
|
-
if (
|
|
43318
|
-
readme =
|
|
44287
|
+
if (existsSync16(readmePath)) {
|
|
44288
|
+
readme = readFileSync18(readmePath, "utf-8");
|
|
43319
44289
|
}
|
|
43320
44290
|
if (verbose) {
|
|
43321
44291
|
return { content: [{ type: "text", text: JSON.stringify({ ...meta3, readme }) }] };
|
|
@@ -43498,7 +44468,7 @@ function createHooksServer() {
|
|
|
43498
44468
|
const ctx = {
|
|
43499
44469
|
scope,
|
|
43500
44470
|
settings_path: settingsPath,
|
|
43501
|
-
settings_exists:
|
|
44471
|
+
settings_exists: existsSync16(settingsPath),
|
|
43502
44472
|
registered_hooks: hooks,
|
|
43503
44473
|
hook_count: hooks.length,
|
|
43504
44474
|
healthy,
|