-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.ts
More file actions
50 lines (43 loc) · 1.35 KB
/
memory.ts
File metadata and controls
50 lines (43 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
const MEMORY_DIR = "./memory";
const MEMORY_FILE = `${MEMORY_DIR}/MEMORY.md`;
export function ensureMemoryDir() {
if (!existsSync(MEMORY_DIR)) {
mkdirSync(MEMORY_DIR, { recursive: true });
}
}
export function readMemory(): string {
ensureMemoryDir();
if (!existsSync(MEMORY_FILE)) return "";
return readFileSync(MEMORY_FILE, "utf-8");
}
export function writeMemory(content: string) {
ensureMemoryDir();
writeFileSync(MEMORY_FILE, content, "utf-8");
}
export function readDayLog(date: string): string {
ensureMemoryDir();
const path = `${MEMORY_DIR}/${date}.md`;
if (!existsSync(path)) return "";
return readFileSync(path, "utf-8");
}
export function appendDayLog(date: string, entry: string) {
ensureMemoryDir();
const path = `${MEMORY_DIR}/${date}.md`;
const existing = existsSync(path) ? readFileSync(path, "utf-8") : "";
writeFileSync(path, existing + entry + "\n", "utf-8");
}
export function getRecentDayLogs(days: number): string {
const logs: string[] = [];
const now = new Date();
for (let i = 0; i < days; i++) {
const d = new Date(now);
d.setDate(d.getDate() - i);
const dateStr = d.toISOString().slice(0, 10);
const log = readDayLog(dateStr);
if (log) {
logs.push(`## ${dateStr}\n${log}`);
}
}
return logs.join("\n\n");
}