Skip to content

Commit

Permalink
add page data to vector db
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhravya committed Apr 14, 2024
1 parent b86e832 commit 4390325
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
39 changes: 39 additions & 0 deletions apps/web/src/actions/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand Down
44 changes: 27 additions & 17 deletions apps/web/src/app/api/store/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export async function POST(req: NextRequest) {
};

const metadata = await getMetaData(data.url);

let storeToSpaces = data.spaces;

if (!storeToSpaces) {
Expand All @@ -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(
Expand All @@ -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()

await Promise.all([spaceData.forEach(async space => {
await db.insert(contentToSpace).values({ contentId: id, spaceId: space.id })
})])
.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 });
}),
]);

const res = (await Promise.race([
fetch("https://cf-ai-backend.dhravya.workers.dev/add", {
Expand Down

0 comments on commit 4390325

Please sign in to comment.