mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-07 02:58:11 +00:00
add page data to vector db
This commit is contained in:
parent
b86e8323fc
commit
439032568a
2 changed files with 65 additions and 16 deletions
|
|
@ -13,6 +13,7 @@ import {
|
|||
import { SearchResult } from "@/contexts/MemoryContext";
|
||||
import { like, eq, and, sql, exists, asc, notExists } from "drizzle-orm";
|
||||
import { union } from "drizzle-orm/sqlite-core";
|
||||
import { env } from "@/env";
|
||||
|
||||
// @todo: (future) pagination not yet needed
|
||||
export async function searchMemoriesAndSpaces(
|
||||
|
|
@ -237,6 +238,44 @@ export async function addMemory(
|
|||
return null;
|
||||
}
|
||||
|
||||
if (!content.content || content.content == "") {
|
||||
const resp = await fetch(
|
||||
`https://cf-ai-backend.dhravya.workers.dev/getPageContent?url=${content.url}`,
|
||||
{
|
||||
headers: {
|
||||
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const data = await resp.text();
|
||||
|
||||
content.content = data;
|
||||
}
|
||||
|
||||
if (!content.content || content.content == "") {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add to vectorDB
|
||||
const res = (await Promise.race([
|
||||
fetch("https://cf-ai-backend.dhravya.workers.dev/add", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
|
||||
},
|
||||
body: JSON.stringify({ ...content, user: user.email }),
|
||||
}),
|
||||
new Promise((_, reject) =>
|
||||
setTimeout(() => reject(new Error("Request timed out")), 40000),
|
||||
),
|
||||
])) as Response;
|
||||
|
||||
if (res.status !== 200) {
|
||||
console.log(res.status, res.statusText);
|
||||
return null;
|
||||
}
|
||||
|
||||
const [addedMemory] = await db
|
||||
.insert(storedContent)
|
||||
.values({
|
||||
|
|
|
|||
|
|
@ -71,7 +71,6 @@ export async function POST(req: NextRequest) {
|
|||
};
|
||||
|
||||
const metadata = await getMetaData(data.url);
|
||||
|
||||
let storeToSpaces = data.spaces;
|
||||
|
||||
if (!storeToSpaces) {
|
||||
|
|
@ -87,16 +86,21 @@ export async function POST(req: NextRequest) {
|
|||
|
||||
console.log("count", count[0].count);
|
||||
|
||||
const { id } = (await db.insert(storedContent).values({
|
||||
content: data.pageContent,
|
||||
title: metadata.title,
|
||||
description: metadata.description,
|
||||
url: data.url,
|
||||
baseUrl: metadata.baseUrl,
|
||||
image: metadata.image,
|
||||
savedAt: new Date(),
|
||||
user: session.user.id,
|
||||
}).returning({ id: storedContent.id }))[0];
|
||||
const { id } = (
|
||||
await db
|
||||
.insert(storedContent)
|
||||
.values({
|
||||
content: data.pageContent,
|
||||
title: metadata.title,
|
||||
description: metadata.description,
|
||||
url: data.url,
|
||||
baseUrl: metadata.baseUrl,
|
||||
image: metadata.image,
|
||||
savedAt: new Date(),
|
||||
user: session.user.id,
|
||||
})
|
||||
.returning({ id: storedContent.id })
|
||||
)[0];
|
||||
|
||||
if (!id) {
|
||||
return NextResponse.json(
|
||||
|
|
@ -108,12 +112,18 @@ export async function POST(req: NextRequest) {
|
|||
const spaceData = await db
|
||||
.select()
|
||||
.from(space)
|
||||
.where(and(inArray(space.name, storeToSpaces), eq(space.user, session.user.id)))
|
||||
.all()
|
||||
.where(
|
||||
and(inArray(space.name, storeToSpaces), eq(space.user, session.user.id)),
|
||||
)
|
||||
.all();
|
||||
|
||||
await Promise.all([spaceData.forEach(async space => {
|
||||
await db.insert(contentToSpace).values({ contentId: id, spaceId: space.id })
|
||||
})])
|
||||
await Promise.all([
|
||||
spaceData.forEach(async (space) => {
|
||||
await db
|
||||
.insert(contentToSpace)
|
||||
.values({ contentId: id, spaceId: space.id });
|
||||
}),
|
||||
]);
|
||||
|
||||
const res = (await Promise.race([
|
||||
fetch("https://cf-ai-backend.dhravya.workers.dev/add", {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue