- VERSION file as authoritative version source - export.mjs reads VERSION with package.json fallback - bw-ota.ps1 DryRun mode for safe testing - auto-setup.ps1 bumped to v3.2.0 (Phase 8 OTA)
88 lines
3.2 KiB
JavaScript
88 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
||
// 幂等补丁: 注册 session-continuity MCP 的 5 个生命周期 Hook
|
||
// 使用 node 直跑 npx 缓存路径 (快 ~50ms vs npx ~3s)
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
const SENTINEL = 'session-continuity-hooks-v1';
|
||
const SETTINGS = path.join(require('os').homedir(), '.claude', 'settings.json');
|
||
const HOOKS_BASE = 'C:/Users/leesu/AppData/Local/npm-cache/_npx/41147f6a3b3ef0bb/node_modules/claude-session-continuity-mcp/dist/hooks';
|
||
|
||
// 验证 hook 文件存在
|
||
const hookFiles = ['session-start.js', 'user-prompt-submit.js', 'post-tool-use.js', 'pre-compact.js', 'session-end.js'];
|
||
for (const f of hookFiles) {
|
||
if (!fs.existsSync(path.join(HOOKS_BASE.replace(/\//g, path.sep), f))) {
|
||
console.error(`[patch] ABORT: hook file not found: ${f}`);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
// 读取 settings.json
|
||
const raw = fs.readFileSync(SETTINGS, 'utf8');
|
||
const settings = JSON.parse(raw);
|
||
|
||
// 幂等检查
|
||
const allCmds = JSON.stringify(settings.hooks || {});
|
||
if (allCmds.includes(SENTINEL) || allCmds.includes('claude-hook-session-start') || allCmds.includes('session-continuity-mcp/dist/hooks')) {
|
||
console.log('[patch] session-continuity hooks already registered, skipping');
|
||
process.exit(0);
|
||
}
|
||
|
||
// 备份
|
||
fs.writeFileSync(SETTINGS + '.bak.pre-sc-hooks', raw);
|
||
|
||
// 确保 hooks 对象存在
|
||
if (!settings.hooks) settings.hooks = {};
|
||
const hooks = settings.hooks;
|
||
|
||
function appendHook(event, entry) {
|
||
if (!hooks[event]) hooks[event] = [];
|
||
hooks[event].push(entry);
|
||
}
|
||
|
||
// 1. SessionStart — 会话开始自动加载上下文
|
||
appendHook('SessionStart', {
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/session-start.js` }]
|
||
});
|
||
|
||
// 2. UserPromptSubmit — 每次提示搜索相关记忆
|
||
appendHook('UserPromptSubmit', {
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/user-prompt-submit.js` }]
|
||
});
|
||
|
||
// 3. PostToolUse — Edit/Write 后追踪文件热路径 + Bash 错<><E99499>检测
|
||
appendHook('PostToolUse', {
|
||
matcher: 'Edit',
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/post-tool-use.js` }]
|
||
});
|
||
appendHook('PostToolUse', {
|
||
matcher: 'Write',
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/post-tool-use.js` }]
|
||
});
|
||
appendHook('PostToolUse', {
|
||
matcher: 'Bash',
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/post-tool-use.js` }]
|
||
});
|
||
|
||
// 4. PreCompact — 压缩前保存结构化交接
|
||
appendHook('PreCompact', {
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/pre-compact.js` }]
|
||
});
|
||
|
||
// 5. Stop — 会话结束自动提取 commit/决策/错误修复对
|
||
appendHook('Stop', {
|
||
hooks: [{ type: 'command', command: `node ${HOOKS_BASE}/session-end.js` }]
|
||
});
|
||
|
||
// 写入
|
||
fs.writeFileSync(SETTINGS, JSON.stringify(settings, null, 2));
|
||
|
||
console.log('[patch] session-continuity hooks registered:');
|
||
console.log(' + SessionStart → 自动加载项目<E9A1B9><E79BAE><EFBFBD>下文');
|
||
console.log(' + UserPromptSubmit → 搜索相关记忆');
|
||
console.log(' + PostToolUse (Edit/Write/Bash) → 文件追踪+错误检测');
|
||
console.log(' + PreCompact → 压缩前保存交接');
|
||
console.log(' + Stop → 会话结束自动保存');
|
||
console.log('[patch] backup: settings.json.bak.pre-sc-hooks');
|
||
console.log('[patch] 重启 Claude Code 生效');
|