Files
wehub-resource-sync adc62957a7
CI / Multi-repo Path Gate (AST-grep) (windows-latest) (push) Has been cancelled
CI / Version Consistency Check (push) Has been cancelled
CI / npm pack + install test (push) Has been cancelled
CI / Lint & Type Check (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Test (Windows path suite) (push) Has been cancelled
CI / Build (push) Has been cancelled
CI / No Committed Build Artifacts (push) Has been cancelled
CI / Multi-repo Path Gate (AST-grep) (ubuntu-latest) (push) Has been cancelled
Upgrade Test / omc update + session-start hook (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:36:54 +08:00

81 lines
2.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* SessionStart Hook: Project Memory Detection
* Auto-detects project environment and injects context
*/
import { dirname, join } from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function getRuntimeBaseDir() {
return process.env.CLAUDE_PLUGIN_ROOT || join(__dirname, '..');
}
// Import timeout-protected stdin reader (prevents hangs on Linux/Windows, see issue #240, #524)
let readStdin;
try {
const mod = await import(pathToFileURL(join(__dirname, 'lib', 'stdin.mjs')).href);
readStdin = mod.readStdin;
} catch {
// Fallback: inline timeout-protected readStdin if lib module is missing
readStdin = (timeoutMs = 5000) => new Promise((resolve) => {
const chunks = [];
let settled = false;
const timeout = setTimeout(() => {
if (!settled) { settled = true; process.stdin.removeAllListeners(); process.stdin.destroy(); resolve(Buffer.concat(chunks).toString('utf-8')); }
}, timeoutMs);
process.stdin.on('data', (chunk) => { chunks.push(chunk); });
process.stdin.on('end', () => { if (!settled) { settled = true; clearTimeout(timeout); resolve(Buffer.concat(chunks).toString('utf-8')); } });
process.stdin.on('error', () => { if (!settled) { settled = true; clearTimeout(timeout); resolve(''); } });
if (process.stdin.readableEnded) { if (!settled) { settled = true; clearTimeout(timeout); resolve(Buffer.concat(chunks).toString('utf-8')); } }
});
}
// Dynamic import of project memory module (prevents crash if dist is missing, see issue #362)
let registerProjectMemoryContext;
try {
const mod = await import(pathToFileURL(join(getRuntimeBaseDir(), 'dist', 'hooks', 'project-memory', 'index.js')).href);
registerProjectMemoryContext = mod.registerProjectMemoryContext;
} catch {
// dist not built or missing - skip project memory detection silently
registerProjectMemoryContext = null;
}
/**
* Main hook execution
*/
async function main() {
try {
const input = await readStdin();
let data = {};
try { data = JSON.parse(input); } catch {}
// Extract directory and session ID
const directory = data.cwd || data.directory || process.cwd();
const sessionId = data.session_id || data.sessionId || '';
// Register project memory context (skip if module unavailable)
if (registerProjectMemoryContext) {
await registerProjectMemoryContext(sessionId, directory);
}
// Return success (context registered via contextCollector, not returned here)
console.log(JSON.stringify({
continue: true,
suppressOutput: true
}));
} catch (error) {
// Always continue on error
console.log(JSON.stringify({
continue: true,
suppressOutput: true
}));
}
}
main();