From f5775aea9d4ef0bd98137d82c02b225133955c04 Mon Sep 17 00:00:00 2001 From: Himanshu Date: Tue, 8 Apr 2025 16:18:22 +0530 Subject: [PATCH 1/2] pearai memories --- src/core/Cline.ts | 13 ++++++++++ src/utils/memory.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/utils/memory.ts diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 21ad1ae0a85..4b7185a3517 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" type ToolResponse = string | Array type UserContent = Array @@ -605,11 +606,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..96fef67a89b --- /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 = "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 [] + } +} From 6a971916f92f7fe58efb6f22a0ae1b8a2646c06c Mon Sep 17 00:00:00 2001 From: Himanshu Date: Mon, 28 Apr 2025 09:37:42 +0530 Subject: [PATCH 2/2] memory file name --- src/utils/memory.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/memory.ts b/src/utils/memory.ts index 96fef67a89b..ada7c2ae258 100644 --- a/src/utils/memory.ts +++ b/src/utils/memory.ts @@ -20,7 +20,7 @@ export interface APIMemory { type: string } -const MEMORIES_FILE = "memories.json" +const MEMORIES_FILE = "pearai_memories.json" function convertToAPIMemory(localMemory: Memory): APIMemory { return {