aboutsummaryrefslogtreecommitdiff
path: root/apps/web/src/actions
diff options
context:
space:
mode:
authoryxshv <[email protected]>2024-04-14 14:29:23 +0530
committeryxshv <[email protected]>2024-04-14 14:29:23 +0530
commitfa39265142a7aa452a273e4290d58757af2786bb (patch)
tree52e2e07d2a20009d650ed0b3ebe60aaab87d81ff /apps/web/src/actions
parentfixed notes vectorize (diff)
downloadsupermemory-fa39265142a7aa452a273e4290d58757af2786bb.tar.xz
supermemory-fa39265142a7aa452a273e4290d58757af2786bb.zip
new modals
Diffstat (limited to 'apps/web/src/actions')
-rw-r--r--apps/web/src/actions/db.ts268
1 files changed, 229 insertions, 39 deletions
diff --git a/apps/web/src/actions/db.ts b/apps/web/src/actions/db.ts
index cd1a0f1d..20aa4de6 100644
--- a/apps/web/src/actions/db.ts
+++ b/apps/web/src/actions/db.ts
@@ -4,14 +4,12 @@ import { db } from "@/server/db";
import {
contentToSpace,
sessions,
- StoredContent,
storedContent,
- StoredSpace,
users,
space,
} from "@/server/db/schema";
import { SearchResult } from "@/contexts/MemoryContext";
-import { like, eq, and, sql, exists, asc, notExists } from "drizzle-orm";
+import { like, eq, and, sql, exists, asc, notExists, inArray, notInArray } from "drizzle-orm";
import { union } from "drizzle-orm/sqlite-core";
import { env } from "@/env";
@@ -82,6 +80,22 @@ export async function searchMemoriesAndSpaces(
}
}
+export async function getMemoriesFromUrl(urls: string[]) {
+
+ const user = await getUser();
+
+ if (!user) {
+ return [];
+ }
+
+ return urls.length > 0 ? await db.select()
+ .from(storedContent)
+ .where(and(
+ inArray(storedContent.url, urls),
+ eq(storedContent.user, user.id)
+ )).all() : []
+}
+
async function getUser() {
const token =
cookies().get("next-auth.session-token")?.value ??
@@ -167,6 +181,38 @@ export async function addSpace(name: string, memories: number[]) {
};
}
+export async function fetchContent(id: number) {
+
+
+ const user = await getUser();
+
+ if (!user) {
+ return null;
+ }
+
+ const fetchedMemory = await db.select()
+ .from(storedContent)
+ .where(and(
+ eq(storedContent.id, id),
+ eq(storedContent.user, user.id)
+ ));
+
+ const memory = fetchedMemory.length > 0 ? fetchedMemory[0] : null
+
+ const spaces = memory ? await db.select()
+ .from(contentToSpace)
+ .where(
+ eq(contentToSpace.contentId, memory.id)
+ ) : []
+
+
+ return {
+ memory,
+ spaces: spaces.map(s => s.spaceId)
+ }
+
+}
+
export async function fetchContentForSpace(
spaceId: number,
range?: {
@@ -174,6 +220,13 @@ export async function fetchContentForSpace(
limit: number;
},
) {
+
+ const user = await getUser();
+
+ if (!user) {
+ return null;
+ }
+
const query = db
.select()
.from(storedContent)
@@ -184,9 +237,19 @@ export async function fetchContentForSpace(
.from(contentToSpace)
.where(
and(
- eq(contentToSpace.spaceId, spaceId),
- eq(contentToSpace.contentId, storedContent.id),
- ),
+ and(
+ eq(contentToSpace.spaceId, spaceId),
+ eq(contentToSpace.contentId, storedContent.id),
+ ),
+ exists(
+ db.select()
+ .from(space)
+ .where(and(
+ eq(space.user, user.id),
+ eq(space.id, contentToSpace.spaceId)
+ ))
+ )
+ )
),
),
)
@@ -207,25 +270,30 @@ export async function fetchFreeMemories(range?: {
return [];
}
- const query = db
- .select()
- .from(storedContent)
- .where(
- and(
- notExists(
- db
- .select()
- .from(contentToSpace)
- .where(eq(contentToSpace.contentId, storedContent.id)),
- ),
- eq(storedContent.user, user.id),
- ),
- )
- .orderBy(asc(storedContent.savedAt));
+ try {
+ const query = db
+ .select()
+ .from(storedContent)
+ .where(
+ and(
+ notExists(
+ db
+ .select()
+ .from(contentToSpace)
+ .where(eq(contentToSpace.contentId, storedContent.id)),
+ ),
+ eq(storedContent.user, user.id),
+ ),
+ )
+ .orderBy(asc(storedContent.savedAt));
+
+ return range
+ ? await query.limit(range.limit).offset(range.offset)
+ : await query.all();
+ } catch {
+ return []
+ }
- return range
- ? await query.limit(range.limit).offset(range.offset)
- : await query.all();
}
export async function addMemory(
@@ -238,7 +306,7 @@ export async function addMemory(
return null;
}
- if (!content.content || content.content == "") {
+ if (!content.content || content.content.trim() === "") {
const resp = await fetch(
`https://cf-ai-backend.dhravya.workers.dev/getPageContent?url=${content.url}`,
{
@@ -259,9 +327,36 @@ export async function addMemory(
return null;
}
- console.log(content);
+ let [addedMemory] = await db
+ .insert(storedContent)
+ .values({
+ user: user.id,
+ ...content,
+ })
+ .returning();
+
+ const addedToSpaces =
+ spaces.length > 0
+ ? await db
+ .insert(contentToSpace)
+ .values(
+ spaces.map((s) => ({
+ contentId: addedMemory.id,
+ spaceId: s,
+ })),
+ )
+ .returning()
+ : [];
+
+ if (content.type === 'note') {
+ addedMemory = (await db.update(storedContent)
+ .set({
+ url: addedMemory.url + addedMemory.id
+ })
+ .where(eq(storedContent.id, addedMemory.id))
+ .returning())[0]
+ }
- console.log({ ...content, user: user.email });
// Add to vectorDB
const res = (await Promise.race([
@@ -271,9 +366,9 @@ export async function addMemory(
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
},
body: JSON.stringify({
- pageContent: content.content,
- title: content.title,
- url: content.url,
+ pageContent: addedMemory.content,
+ title: addedMemory.title,
+ url: addedMemory.url,
user: user.email,
}),
}),
@@ -282,30 +377,110 @@ export async function addMemory(
),
])) as Response;
- const [addedMemory] = await db
- .insert(storedContent)
- .values({
- user: user.id,
- ...content,
- })
+ return {
+ memory: addedMemory,
+ addedToSpaces,
+ };
+}
+
+
+export async function updateMemory(
+ id: number,
+ { title, content, spaces }: {
+ title?: string;
+ content?: string;
+ spaces?: number[]
+ }
+) {
+ const user = await getUser();
+
+ if (!user) {
+ return null;
+ }
+
+ console.log("updating")
+
+ const [prev] = await db.select()
+ .from(storedContent)
+ .where(and(
+ eq(storedContent.user, user.id),
+ eq(storedContent.id, id)
+ ));
+
+ if (!prev) {
+ return null
+ }
+
+ const newContent = {
+ ...(title ? { title }: {}),
+ ...(content ? { content }: {}),
+ }
+
+ const updated = {
+ ...newContent,
+ ...prev
+ }
+
+ // Add to vectorDB
+ const res = (await Promise.race([
+ fetch("https://cf-ai-backend.dhravya.workers.dev/edit?uniqueUrl="+updated.url , {
+ method: "POST",
+ headers: {
+ "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
+ },
+ body: JSON.stringify({
+ pageContent: updated.content,
+ title: updated.title,
+ url: updated.url,
+ user: user.email,
+ }),
+ }),
+ new Promise((_, reject) =>
+ setTimeout(() => reject(new Error("Request timed out")), 40000),
+ ),
+ ])) as Response;
+
+ const [updatedMemory] = await db
+ .update(storedContent)
+ .set(newContent)
+ .where(
+ eq(storedContent.id, id)
+ )
.returning();
+
+ console.log(updatedMemory, newContent)
+
+ const removedFromSpaces = spaces ?
+ spaces.length > 0 ?
+ await db.delete(contentToSpace)
+ .where(and(
+ notInArray(contentToSpace.spaceId, spaces),
+ eq(contentToSpace.contentId, id)
+ )).returning()
+ : await db.delete(contentToSpace)
+ .where(
+ eq(contentToSpace.contentId, id)
+ )
+ : [];
const addedToSpaces =
- spaces.length > 0
+ (spaces && spaces.length > 0)
? await db
.insert(contentToSpace)
.values(
spaces.map((s) => ({
- contentId: addedMemory.id,
+ contentId: id,
spaceId: s,
})),
)
+ .onConflictDoNothing()
.returning()
: [];
return {
- memory: addedMemory,
+ memory: updatedMemory,
addedToSpaces,
+ removedFromSpaces
};
}
@@ -340,5 +515,20 @@ export async function deleteMemory(id: number) {
.where(and(eq(storedContent.user, user.id), eq(storedContent.id, id)))
.returning();
+ if (deleted) {
+
+ const res = (await Promise.race([
+ fetch(`https://cf-ai-backend.dhravya.workers.dev/delete?websiteUrl=${deleted.url}&user=${user.email}` , {
+ method: "DELETE",
+ headers: {
+ "X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
+ }
+ }),
+ new Promise((_, reject) =>
+ setTimeout(() => reject(new Error("Request timed out")), 40000),
+ ),
+ ])) as Response;
+ }
+
return deleted;
}