File size: 3,386 Bytes
64648ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | import { and, desc, eq } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import { agentMemories } from "@paperclipai/db";
export function memoryLoaderService(db: Db) {
return {
async loadMemories(
agentId: string,
projectId?: string | null,
opts?: { scope?: "global" | "project"; category?: string },
) {
const conditions = [eq(agentMemories.agentId, agentId)];
if (opts?.scope) {
conditions.push(eq(agentMemories.scope, opts.scope));
}
if (opts?.category) {
conditions.push(
eq(
agentMemories.category,
opts.category as "pattern" | "preference" | "decision" | "learning" | "feedback",
),
);
}
if (projectId) {
conditions.push(eq(agentMemories.projectId, projectId));
}
// Return global + project-scoped memories
const memories = await db
.select()
.from(agentMemories)
.where(and(...conditions))
.orderBy(desc(agentMemories.createdAt));
// If projectId specified and no scope filter, also include global memories
if (projectId && !opts?.scope) {
const globalMemories = await db
.select()
.from(agentMemories)
.where(
and(eq(agentMemories.agentId, agentId), eq(agentMemories.scope, "global")),
)
.orderBy(desc(agentMemories.createdAt));
// Deduplicate by id
const seen = new Set(memories.map((m) => m.id));
for (const m of globalMemories) {
if (!seen.has(m.id)) {
memories.push(m);
}
}
}
return memories;
},
async saveMemory(data: {
agentId: string;
companyId: string;
scope: "global" | "project";
projectId?: string | null;
category: "pattern" | "preference" | "decision" | "learning" | "feedback";
title: string;
content: string;
source: "self" | "ceo" | "board" | "human";
confidence?: number;
}) {
const [memory] = await db
.insert(agentMemories)
.values({
agentId: data.agentId,
companyId: data.companyId,
scope: data.scope,
projectId: data.projectId ?? null,
category: data.category,
title: data.title,
content: data.content,
source: data.source,
confidence: data.confidence ?? 0.5,
})
.returning();
return memory;
},
async updateMemory(
id: string,
data: {
title?: string;
content?: string;
category?: "pattern" | "preference" | "decision" | "learning" | "feedback";
confidence?: number;
},
) {
const [updated] = await db
.update(agentMemories)
.set({
...data,
updatedAt: new Date(),
})
.where(eq(agentMemories.id, id))
.returning();
return updated;
},
async deleteMemory(id: string) {
const [deleted] = await db
.delete(agentMemories)
.where(eq(agentMemories.id, id))
.returning();
return deleted;
},
async getMemory(id: string) {
const [memory] = await db
.select()
.from(agentMemories)
.where(eq(agentMemories.id, id))
.limit(1);
return memory ?? null;
},
};
}
|