|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +function createPromptHistoryController(deps = {}) { |
| 4 | + const { |
| 5 | + fs, |
| 6 | + path, |
| 7 | + CONFIG_DIR, |
| 8 | + MAX_HISTORY = 20 |
| 9 | + } = deps; |
| 10 | + |
| 11 | + if (!fs) throw new Error('createPromptHistoryController 缺少 fs'); |
| 12 | + if (!path) throw new Error('createPromptHistoryController 缺少 path'); |
| 13 | + if (typeof CONFIG_DIR !== 'string' || !CONFIG_DIR) throw new Error('createPromptHistoryController 缺少 CONFIG_DIR'); |
| 14 | + |
| 15 | + const HISTORY_ROOT = path.join(CONFIG_DIR, 'codexmate-prompt-history'); |
| 16 | + const STAMP_RE = /^(\d{8})-(\d{6})-(\d{3})\.bak$/; |
| 17 | + |
| 18 | + function sanitizeBucket(bucket) { |
| 19 | + const raw = typeof bucket === 'string' ? bucket.trim() : ''; |
| 20 | + if (!raw) throw new Error('prompt history bucket 不能为空'); |
| 21 | + const safe = raw.replace(/[^A-Za-z0-9_.-]/g, '_'); |
| 22 | + if (!safe) throw new Error('prompt history bucket 无效: ' + bucket); |
| 23 | + return safe; |
| 24 | + } |
| 25 | + |
| 26 | + function resolveBucketDir(bucket) { |
| 27 | + const safe = sanitizeBucket(bucket); |
| 28 | + return path.join(HISTORY_ROOT, safe); |
| 29 | + } |
| 30 | + |
| 31 | + function buildTimestamp(d = new Date()) { |
| 32 | + const pad2 = (n) => String(n).padStart(2, '0'); |
| 33 | + const pad3 = (n) => String(n).padStart(3, '0'); |
| 34 | + return `${d.getFullYear()}${pad2(d.getMonth() + 1)}${pad2(d.getDate())}` |
| 35 | + + `-${pad2(d.getHours())}${pad2(d.getMinutes())}${pad2(d.getSeconds())}` |
| 36 | + + `-${pad3(d.getMilliseconds())}`; |
| 37 | + } |
| 38 | + |
| 39 | + function parseStamp(name) { |
| 40 | + return STAMP_RE.test(name) ? name.slice(0, name.length - 4) : null; |
| 41 | + } |
| 42 | + |
| 43 | + function ensureDirSync(dir) { |
| 44 | + fs.mkdirSync(dir, { recursive: true }); |
| 45 | + } |
| 46 | + |
| 47 | + function rmtreeSync(dir) { |
| 48 | + try { |
| 49 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 50 | + } catch (_) {} |
| 51 | + } |
| 52 | + |
| 53 | + function backupPromptBeforeWrite(bucket, currentFilePath) { |
| 54 | + const dir = resolveBucketDir(bucket); |
| 55 | + const exists = fs.existsSync(currentFilePath); |
| 56 | + if (!exists) return { backedUp: false, entries: listPromptHistory(bucket) }; |
| 57 | + let raw; |
| 58 | + try { |
| 59 | + raw = fs.readFileSync(currentFilePath, 'utf-8'); |
| 60 | + } catch (e) { |
| 61 | + return { backedUp: false, error: '读取当前文件失败: ' + e.message }; |
| 62 | + } |
| 63 | + ensureDirSync(dir); |
| 64 | + const stamp = buildTimestamp(); |
| 65 | + const dest = path.join(dir, stamp + '.bak'); |
| 66 | + try { |
| 67 | + fs.writeFileSync(dest, raw, 'utf-8'); |
| 68 | + } catch (e) { |
| 69 | + return { backedUp: false, error: '写入备份失败: ' + e.message }; |
| 70 | + } |
| 71 | + trimHistory(dir); |
| 72 | + return { backedUp: true, entries: listPromptHistory(bucket) }; |
| 73 | + } |
| 74 | + |
| 75 | + function listPromptHistory(bucket) { |
| 76 | + const dir = resolveBucketDir(bucket); |
| 77 | + if (!fs.existsSync(dir)) return []; |
| 78 | + let names; |
| 79 | + try { |
| 80 | + names = fs.readdirSync(dir); |
| 81 | + } catch (_) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + const items = []; |
| 85 | + for (const name of names) { |
| 86 | + const stamp = parseStamp(name); |
| 87 | + if (!stamp) continue; |
| 88 | + let stat; |
| 89 | + try { |
| 90 | + stat = fs.statSync(path.join(dir, name)); |
| 91 | + } catch (_) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + items.push({ |
| 95 | + id: stamp, |
| 96 | + bucket: sanitizeBucket(bucket), |
| 97 | + size: Number(stat.size) || 0, |
| 98 | + mtimeMs: Number(stat.mtimeMs) || 0 |
| 99 | + }); |
| 100 | + } |
| 101 | + items.sort((a, b) => b.mtimeMs - a.mtimeMs); |
| 102 | + return items; |
| 103 | + } |
| 104 | + |
| 105 | + function readPromptHistory(bucket, id) { |
| 106 | + const dir = resolveBucketDir(bucket); |
| 107 | + const stamp = typeof id === 'string' ? id.trim() : ''; |
| 108 | + const fullName = stamp + '.bak'; |
| 109 | + if (!stamp || !STAMP_RE.test(fullName)) { |
| 110 | + return { error: 'history id invalid' }; |
| 111 | + } |
| 112 | + const file = path.join(dir, fullName); |
| 113 | + if (!fs.existsSync(file)) return { error: 'history entry 不存在' }; |
| 114 | + try { |
| 115 | + const content = fs.readFileSync(file, 'utf-8'); |
| 116 | + return { id: stamp, content, bucket: sanitizeBucket(bucket) }; |
| 117 | + } catch (e) { |
| 118 | + return { error: '读取 history 失败: ' + e.message }; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + function trimHistory(dir) { |
| 123 | + let names; |
| 124 | + try { |
| 125 | + names = fs.readdirSync(dir); |
| 126 | + } catch (_) { |
| 127 | + return; |
| 128 | + } |
| 129 | + const stamped = []; |
| 130 | + for (const name of names) { |
| 131 | + const stamp = parseStamp(name); |
| 132 | + if (!stamp) continue; |
| 133 | + let stat; |
| 134 | + try { |
| 135 | + stat = fs.statSync(path.join(dir, name)); |
| 136 | + } catch (_) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + stamped.push({ name, mtimeMs: Number(stat.mtimeMs) || 0 }); |
| 140 | + } |
| 141 | + stamped.sort((a, b) => b.mtimeMs - a.mtimeMs); |
| 142 | + if (stamped.length <= MAX_HISTORY) return; |
| 143 | + for (let i = MAX_HISTORY; i < stamped.length; i++) { |
| 144 | + try { |
| 145 | + fs.unlinkSync(path.join(dir, stamped[i].name)); |
| 146 | + } catch (_) {} |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + function clearPromptHistory(bucket) { |
| 151 | + if (!bucket) { |
| 152 | + rmtreeSync(HISTORY_ROOT); |
| 153 | + return { cleared: true }; |
| 154 | + } |
| 155 | + const dir = resolveBucketDir(bucket); |
| 156 | + rmtreeSync(dir); |
| 157 | + return { cleared: true }; |
| 158 | + } |
| 159 | + |
| 160 | + return { |
| 161 | + sanitizeBucket, |
| 162 | + backupPromptBeforeWrite, |
| 163 | + listPromptHistory, |
| 164 | + readPromptHistory, |
| 165 | + clearPromptHistory |
| 166 | + }; |
| 167 | +} |
| 168 | + |
| 169 | +module.exports = { |
| 170 | + createPromptHistoryController |
| 171 | +}; |
0 commit comments