File size: 1,967 Bytes
20f108c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { homedir } from "os";
import { resolve } from "path";
import { AgentMessage } from "../agents/types.js";

const BASE_DIR = resolve(homedir(), ".config", "phantom");
const MEMORY_DIR = resolve(BASE_DIR, "memory");
const KNOWLEDGE_DIR = resolve(BASE_DIR, "knowledge");

function ensureDirs(): void {
  if (!existsSync(MEMORY_DIR)) {
    mkdirSync(MEMORY_DIR, { recursive: true });
  }
  if (!existsSync(KNOWLEDGE_DIR)) {
    mkdirSync(KNOWLEDGE_DIR, { recursive: true });
  }
}

export function saveMemory(agentId: string, memory: AgentMessage[]): void {
  try {
    ensureDirs();
    const filePath = resolve(MEMORY_DIR, `${agentId}.json`);
    writeFileSync(filePath, JSON.stringify(memory, null, 2), "utf-8");
  } catch (e: any) {
    console.error(`Failed to save memory for agent ${agentId}: ${e.message}`);
  }
}

export function loadMemory(agentId: string): AgentMessage[] {
  try {
    ensureDirs();
    const filePath = resolve(MEMORY_DIR, `${agentId}.json`);
    if (!existsSync(filePath)) return [];
    const raw = readFileSync(filePath, "utf-8");
    return JSON.parse(raw);
  } catch (e: any) {
    console.error(`Failed to load memory for agent ${agentId}: ${e.message}`);
    return [];
  }
}

export function saveKnowledge(agentId: string, knowledge: string): void {
  try {
    ensureDirs();
    const filePath = resolve(KNOWLEDGE_DIR, `${agentId}.txt`);
    writeFileSync(filePath, knowledge, "utf-8");
  } catch (e: any) {
    console.error(`Failed to save knowledge for agent ${agentId}: ${e.message}`);
  }
}

export function loadKnowledge(agentId: string): string {
  try {
    ensureDirs();
    const filePath = resolve(KNOWLEDGE_DIR, `${agentId}.txt`);
    if (!existsSync(filePath)) return "";
    return readFileSync(filePath, "utf-8");
  } catch (e: any) {
    console.error(`Failed to load knowledge for agent ${agentId}: ${e.message}`);
    return "";
  }
}