Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/core/Cline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
Expand Down Expand Up @@ -606,11 +607,23 @@ export class Cline extends EventEmitter<ClineEvents> {

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: `<memories>\n<relevant user context from past, refer these if required/>\n${memoryContext}\n</memories>`,
})
}

await this.initiateTaskLoop([
{
type: "text",
text: `<task>\n${task}\n</task>`,
},
...memoryBlocks,
...imageBlocks,
])
}
Expand Down
61 changes: 61 additions & 0 deletions src/utils/memory.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>
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 []
}
}
Loading