|
| 1 | +const fs = require('node:fs'); |
| 2 | +const os = require('node:os'); |
| 3 | +const path = require('node:path'); |
| 4 | + |
| 5 | +const SESSION_FILE_ENV_KEYS = [ |
| 6 | + 'CODEX_SESSION_FILE', |
| 7 | + 'CODEX_SESSION_FILE_PATH', |
| 8 | + 'CODEX_TRANSCRIPT_PATH', |
| 9 | + 'OPENAI_CODEX_SESSION_FILE', |
| 10 | + 'OPENAI_CODEX_SESSION_FILE_PATH', |
| 11 | +]; |
| 12 | + |
| 13 | +const SESSION_FILE_INPUT_KEYS = [ |
| 14 | + 'session_file', |
| 15 | + 'sessionFile', |
| 16 | + 'session_file_path', |
| 17 | + 'sessionFilePath', |
| 18 | + 'transcript_path', |
| 19 | + 'transcriptPath', |
| 20 | +]; |
| 21 | + |
| 22 | +const SESSION_ID_ENV_KEYS = [ |
| 23 | + 'CODEX_THREAD_ID', |
| 24 | + 'CODEX_SESSION_ID', |
| 25 | + 'OPENAI_CODEX_THREAD_ID', |
| 26 | + 'OPENAI_CODEX_SESSION_ID', |
| 27 | +]; |
| 28 | + |
| 29 | +const SESSION_ID_INPUT_KEYS = [ |
| 30 | + 'session_id', |
| 31 | + 'sessionId', |
| 32 | + 'thread_id', |
| 33 | + 'threadId', |
| 34 | + 'conversation_id', |
| 35 | + 'conversationId', |
| 36 | +]; |
| 37 | + |
| 38 | +const CODEX_PID_ENV_KEYS = [ |
| 39 | + 'CODEX_PID', |
| 40 | + 'CODEX_PROCESS_PID', |
| 41 | + 'OPENAI_CODEX_PID', |
| 42 | + 'OPENAI_CODEX_PROCESS_PID', |
| 43 | + 'AI_DEVKIT_CODEX_PID', |
| 44 | +]; |
| 45 | + |
| 46 | +const CODEX_PID_INPUT_KEYS = [ |
| 47 | + 'codex_pid', |
| 48 | + 'codexPid', |
| 49 | + 'codex_process_pid', |
| 50 | + 'codexProcessPid', |
| 51 | + 'pid', |
| 52 | +]; |
| 53 | + |
| 54 | +function readHookInput() { |
| 55 | + if (process.stdin.isTTY) return {}; |
| 56 | + |
| 57 | + try { |
| 58 | + const raw = fs.readFileSync(0, 'utf8').trim(); |
| 59 | + return raw ? JSON.parse(raw) : {}; |
| 60 | + } catch { |
| 61 | + return {}; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function normalizeValue(value) { |
| 66 | + if (typeof value === 'string' && value.trim()) return value.trim(); |
| 67 | + if (typeof value === 'number' && Number.isSafeInteger(value) && value > 0) return String(value); |
| 68 | + return undefined; |
| 69 | +} |
| 70 | + |
| 71 | +function inputSources(input) { |
| 72 | + return [input, input?.payload, input?.session, input?.thread].filter( |
| 73 | + (source) => source && typeof source === 'object', |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +function inputValue(input, keys) { |
| 78 | + for (const source of inputSources(input)) { |
| 79 | + const value = firstValue(keys, (key) => source[key]); |
| 80 | + if (value) return value; |
| 81 | + } |
| 82 | + |
| 83 | + return undefined; |
| 84 | +} |
| 85 | + |
| 86 | +function envValue(keys) { |
| 87 | + return firstValue(keys, (key) => process.env[key]); |
| 88 | +} |
| 89 | + |
| 90 | +function firstValue(keys, getValue) { |
| 91 | + for (const key of keys) { |
| 92 | + const value = normalizeValue(getValue(key)); |
| 93 | + if (value) return value; |
| 94 | + } |
| 95 | + |
| 96 | + return undefined; |
| 97 | +} |
| 98 | + |
| 99 | +function readRegistry(registryFile) { |
| 100 | + let existing; |
| 101 | + |
| 102 | + try { |
| 103 | + existing = JSON.parse(fs.readFileSync(registryFile, 'utf8')); |
| 104 | + } catch { |
| 105 | + return {}; |
| 106 | + } |
| 107 | + |
| 108 | + return existing && typeof existing === 'object' && !Array.isArray(existing) ? existing : {}; |
| 109 | +} |
| 110 | + |
| 111 | +function writeRegistry(registryFile, registry) { |
| 112 | + fs.mkdirSync(path.dirname(registryFile), { recursive: true }); |
| 113 | + fs.writeFileSync(registryFile, JSON.stringify(registry, null, 2), 'utf8'); |
| 114 | +} |
| 115 | + |
| 116 | +function findSessionFileById(homeDir, sessionId) { |
| 117 | + const codexSessionsDir = path.join(homeDir, '.codex', 'sessions'); |
| 118 | + const pendingDirs = [codexSessionsDir]; |
| 119 | + |
| 120 | + while (pendingDirs.length > 0) { |
| 121 | + const dir = pendingDirs.pop(); |
| 122 | + let entries; |
| 123 | + |
| 124 | + try { |
| 125 | + entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 126 | + } catch { |
| 127 | + continue; |
| 128 | + } |
| 129 | + |
| 130 | + for (const entry of entries) { |
| 131 | + const entryPath = path.join(dir, entry.name); |
| 132 | + if (entry.isDirectory()) { |
| 133 | + pendingDirs.push(entryPath); |
| 134 | + } else if (entry.isFile() && entry.name.endsWith('.jsonl') && entry.name.includes(sessionId)) { |
| 135 | + return entryPath; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return undefined; |
| 141 | +} |
| 142 | + |
| 143 | +function resolveSessionFile(homeDir, input) { |
| 144 | + const explicitPath = envValue(SESSION_FILE_ENV_KEYS) || inputValue(input, SESSION_FILE_INPUT_KEYS); |
| 145 | + |
| 146 | + if (explicitPath) return explicitPath; |
| 147 | + |
| 148 | + const sessionId = envValue(SESSION_ID_ENV_KEYS) || inputValue(input, SESSION_ID_INPUT_KEYS); |
| 149 | + |
| 150 | + return sessionId ? findSessionFileById(homeDir, sessionId) : undefined; |
| 151 | +} |
| 152 | + |
| 153 | +function resolveCodexPid(input) { |
| 154 | + const pid = envValue(CODEX_PID_ENV_KEYS) || inputValue(input, CODEX_PID_INPUT_KEYS); |
| 155 | + |
| 156 | + return pid || String(process.ppid || process.pid); |
| 157 | +} |
| 158 | + |
| 159 | +const input = readHookInput(); |
| 160 | +const homeDir = os.homedir(); |
| 161 | +const registryFile = path.join(homeDir, '.codex', 'ai-devkit', 'sessions.json'); |
| 162 | +const registry = readRegistry(registryFile); |
| 163 | + |
| 164 | +registry[resolveCodexPid(input)] = resolveSessionFile(homeDir, input) || 'ephemeral'; |
| 165 | +writeRegistry(registryFile, registry); |
0 commit comments