|
| 1 | +import * as fs from "fs" |
| 2 | +import * as path from "path" |
| 3 | +import * as os from "os" |
| 4 | + |
| 5 | +export interface Memory { |
| 6 | + id: string |
| 7 | + content: string |
| 8 | + timestamp: string |
| 9 | +} |
| 10 | + |
| 11 | +export interface APIMemory { |
| 12 | + id: string |
| 13 | + memory: string |
| 14 | + created_at: string |
| 15 | + updated_at: string |
| 16 | + total_memories: number |
| 17 | + owner: string |
| 18 | + organization: string |
| 19 | + metadata: Record<string, any> |
| 20 | + type: string |
| 21 | +} |
| 22 | + |
| 23 | +const MEMORIES_FILE = "pearai_memories.json" |
| 24 | + |
| 25 | +function convertToAPIMemory(localMemory: Memory): APIMemory { |
| 26 | + return { |
| 27 | + id: localMemory.id, |
| 28 | + memory: localMemory.content, |
| 29 | + created_at: localMemory.timestamp, |
| 30 | + updated_at: localMemory.timestamp, |
| 31 | + total_memories: 1, |
| 32 | + owner: "", |
| 33 | + organization: "", |
| 34 | + metadata: {}, |
| 35 | + type: "manual", |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function getMemoriesFilePath(): string { |
| 40 | + const pearaiPath = process.env.CONTINUE_GLOBAL_DIR ?? path.join(os.homedir(), ".pearai") |
| 41 | + return path.join(pearaiPath, MEMORIES_FILE) |
| 42 | +} |
| 43 | + |
| 44 | +export function initializeMemoriesFile(): void { |
| 45 | + const filePath = getMemoriesFilePath() |
| 46 | + if (!fs.existsSync(filePath)) { |
| 47 | + fs.writeFileSync(filePath, JSON.stringify([], null, 2)) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function readMemories(): APIMemory[] { |
| 52 | + try { |
| 53 | + initializeMemoriesFile() |
| 54 | + const content = fs.readFileSync(getMemoriesFilePath(), "utf8") |
| 55 | + const localMemories = JSON.parse(content) as Memory[] |
| 56 | + return localMemories.map(convertToAPIMemory) |
| 57 | + } catch (error) { |
| 58 | + console.error("Error reading memories:", error) |
| 59 | + return [] |
| 60 | + } |
| 61 | +} |
0 commit comments