diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 6cb26fbbc61..4ffbb924770 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -82,6 +82,7 @@ import { validateToolUse, isToolAllowedForMode, ToolName } from "./mode-validato import { parseXml } from "../utils/xml" import { readLines } from "../integrations/misc/read-lines" import { getWorkspacePath } from "../utils/path" +import { readMemories } from "../utils/memory" import { isBinaryFile } from "isbinaryfile" type ToolResponse = string | Array @@ -606,11 +607,23 @@ export class Cline extends EventEmitter { console.log(`[subtasks] task ${this.taskId}.${this.instanceId} starting`) + // PearAI memories + const memories = readMemories() + let memoryBlocks: Anthropic.TextBlockParam[] = [] + if (memories.length > 0) { + const memoryContext = memories.map((m) => m.memory).join("\n\n") + memoryBlocks.push({ + type: "text", + text: `\n\n${memoryContext}\n`, + }) + } + await this.initiateTaskLoop([ { type: "text", text: `\n${task}\n`, }, + ...memoryBlocks, ...imageBlocks, ]) } diff --git a/src/utils/memory.ts b/src/utils/memory.ts new file mode 100644 index 00000000000..ada7c2ae258 --- /dev/null +++ b/src/utils/memory.ts @@ -0,0 +1,61 @@ +import * as fs from "fs" +import * as path from "path" +import * as os from "os" + +export interface Memory { + id: string + content: string + timestamp: string +} + +export interface APIMemory { + id: string + memory: string + created_at: string + updated_at: string + total_memories: number + owner: string + organization: string + metadata: Record + type: string +} + +const MEMORIES_FILE = "pearai_memories.json" + +function convertToAPIMemory(localMemory: Memory): APIMemory { + return { + id: localMemory.id, + memory: localMemory.content, + created_at: localMemory.timestamp, + updated_at: localMemory.timestamp, + total_memories: 1, + owner: "", + organization: "", + metadata: {}, + type: "manual", + } +} + +export function getMemoriesFilePath(): string { + const pearaiPath = process.env.CONTINUE_GLOBAL_DIR ?? path.join(os.homedir(), ".pearai") + return path.join(pearaiPath, MEMORIES_FILE) +} + +export function initializeMemoriesFile(): void { + const filePath = getMemoriesFilePath() + if (!fs.existsSync(filePath)) { + fs.writeFileSync(filePath, JSON.stringify([], null, 2)) + } +} + +export function readMemories(): APIMemory[] { + try { + initializeMemoriesFile() + const content = fs.readFileSync(getMemoriesFilePath(), "utf8") + const localMemories = JSON.parse(content) as Memory[] + return localMemories.map(convertToAPIMemory) + } catch (error) { + console.error("Error reading memories:", error) + return [] + } +}