Skip to content

Commit e7fc70b

Browse files
authored
Merge pull request #52 from trypear/memory
pearai memories
2 parents 0f9022c + 0bf56eb commit e7fc70b

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/core/Cline.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { validateToolUse, isToolAllowedForMode, ToolName } from "./mode-validato
8282
import { parseXml } from "../utils/xml"
8383
import { readLines } from "../integrations/misc/read-lines"
8484
import { getWorkspacePath } from "../utils/path"
85+
import { readMemories } from "../utils/memory"
8586
import { isBinaryFile } from "isbinaryfile"
8687

8788
type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
@@ -606,11 +607,23 @@ export class Cline extends EventEmitter<ClineEvents> {
606607

607608
console.log(`[subtasks] task ${this.taskId}.${this.instanceId} starting`)
608609

610+
// PearAI memories
611+
const memories = readMemories()
612+
let memoryBlocks: Anthropic.TextBlockParam[] = []
613+
if (memories.length > 0) {
614+
const memoryContext = memories.map((m) => m.memory).join("\n\n")
615+
memoryBlocks.push({
616+
type: "text",
617+
text: `<memories>\n<relevant user context from past, refer these if required/>\n${memoryContext}\n</memories>`,
618+
})
619+
}
620+
609621
await this.initiateTaskLoop([
610622
{
611623
type: "text",
612624
text: `<task>\n${task}\n</task>`,
613625
},
626+
...memoryBlocks,
614627
...imageBlocks,
615628
])
616629
}

src/utils/memory.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)