Skip to content

Commit d04f1d2

Browse files
add pusblish to open ai for files
1 parent 494dac4 commit d04f1d2

File tree

15 files changed

+538
-320
lines changed

15 files changed

+538
-320
lines changed

app/(dashboard)/dashboard/chatbots/[chatbotId]/columns.tsx

Lines changed: 0 additions & 74 deletions
This file was deleted.

app/(dashboard)/dashboard/chatbots/[chatbotId]/page.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"use server"
2-
31
import { notFound, redirect } from "next/navigation"
42
import { Chatbot, User } from "@prisma/client"
53

@@ -56,11 +54,24 @@ export default async function ChatbotPage({ params }: ChatbotSettingsProps) {
5654
const crawlerFiles = await db.crawlerFile.findMany({
5755
where: {
5856
crawlerId: crawler.id,
59-
}
57+
},
6058
})
6159
files.push(...crawlerFiles)
6260
}
6361

62+
const openAIFiles = await db.openAIFile.findMany({
63+
select: {
64+
id: true,
65+
fileId: true,
66+
openAIFileId: true,
67+
},
68+
where: {
69+
fileId: {
70+
in: files.map((file) => file.id),
71+
}
72+
},
73+
})
74+
6475
return (
6576
<DashboardShell>
6677
<DashboardHeader heading="Chatbot" text="Configure your chatbot here">
@@ -88,7 +99,13 @@ export default async function ChatbotPage({ params }: ChatbotSettingsProps) {
8899
<p className="text-muted-foreground">
89100
Here&apos;s all of your files
90101
</p>
91-
{ /** <DataTable data={files} columns={columns} /> */}
102+
<div>
103+
{openAIFiles.map((file) => (
104+
<div key={file.id}>
105+
<p>{file.openAIFileId}</p>
106+
</div>
107+
))}
108+
</div>
92109
</div>
93110
</div>
94111
</>

app/(dashboard)/dashboard/settings/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getCurrentUser } from "@/lib/session"
66
import { DashboardHeader } from "@/components/header"
77
import { DashboardShell } from "@/components/shell"
88
import { UserNameForm } from "@/components/user-name-form"
9+
import { OpenAIForm } from "@/components/openai-config-form"
910

1011
export const metadata = {
1112
title: "Settings",
@@ -29,6 +30,10 @@ export default async function SettingsPage() {
2930
<div className="grid gap-10">
3031
<UserNameForm user={{ id: user.id, name: user.name || "" }} />
3132
</div>
33+
<div className="grid gap-10">
34+
<OpenAIForm user={{ id: user.id, name: user.name || "" }} />
35+
</div>
36+
3237
</div>
3338
</DashboardShell>
3439
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { getServerSession } from "next-auth/next"
2+
import { z } from "zod"
3+
4+
import { authOptions } from "@/lib/auth"
5+
import { db } from "@/lib/db"
6+
import OpenAI from "openai"
7+
8+
9+
10+
const routeContextSchema = z.object({
11+
params: z.object({
12+
crawlerId: z.string(),
13+
fileId: z.string(),
14+
}),
15+
})
16+
17+
async function verifyCurrentUserHasAccessToCrawler(crawlerId: string) {
18+
const session = await getServerSession(authOptions)
19+
20+
const count = await db.crawler.count({
21+
where: {
22+
userId: session?.user?.id,
23+
id: crawlerId,
24+
},
25+
})
26+
27+
return count > 0
28+
}
29+
30+
export async function POST(
31+
req: Request,
32+
context: z.infer<typeof routeContextSchema>
33+
) {
34+
try {
35+
const session = await getServerSession(authOptions)
36+
37+
// Validate the route params.
38+
const { params } = routeContextSchema.parse(context)
39+
40+
if (!(await verifyCurrentUserHasAccessToCrawler(params.crawlerId))) {
41+
return new Response(null, { status: 403 })
42+
}
43+
44+
const crawlerFile = await db.crawlerFile.findUnique({
45+
where: {
46+
id: params.fileId
47+
},
48+
select: {
49+
blobUrl: true
50+
}
51+
})
52+
53+
if (!crawlerFile) {
54+
return new Response(null, { status: 404 })
55+
}
56+
57+
const openAIConfig = await db.openAIConfig.findUnique({
58+
select: {
59+
globalAPIKey: true,
60+
id: true,
61+
},
62+
where: {
63+
userId: session?.user?.id
64+
}
65+
})
66+
const openai = new OpenAI({
67+
apiKey: openAIConfig?.globalAPIKey
68+
})
69+
70+
const file = await openai.files.create(
71+
{ file: await fetch(crawlerFile.blobUrl), purpose: 'assistants' }
72+
)
73+
74+
await db.openAIFile.create({
75+
data: {
76+
fileId: params.fileId,
77+
openAIFileId: file.id
78+
}
79+
})
80+
81+
return new Response(null, { status: 204 })
82+
} catch (error) {
83+
console.log(error)
84+
if (error instanceof z.ZodError) {
85+
return new Response(JSON.stringify(error.issues), { status: 422 })
86+
}
87+
88+
return new Response(null, { status: 500 })
89+
}
90+
}

app/api/crawlers/[crawlerId]/files/[fileId]/route.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { del } from '@vercel/blob';
55
import { authOptions } from "@/lib/auth"
66
import { db } from "@/lib/db"
77

8+
89
const routeContextSchema = z.object({
910
params: z.object({
1011
crawlerId: z.string(),
@@ -42,14 +43,42 @@ export async function DELETE(
4243
id: params.fileId
4344
},
4445
select: {
45-
blobUrl: true
46+
blobUrl: true,
47+
OpenAIFile: {
48+
select: {
49+
id: true,
50+
openAIFileId: true
51+
}
52+
}
4653
}
4754
})
4855

4956
if (!crawlerFile) {
5057
return new Response(null, { status: 404 })
5158
}
5259

60+
// TODO: Delete OpenAI file
61+
//import OpenAI from "openai"
62+
//if (crawlerFile.OpenAIFile) {
63+
// const session = await getServerSession(authOptions)
64+
// const clientConfig = await db.openAIConfig.findFirst({
65+
// where: {
66+
// userId: session?.user?.id
67+
// }
68+
// })
69+
70+
// const client = new OpenAI({
71+
// apiKey: clientConfig?.globalAPIKey,
72+
// })
73+
74+
// console.log(crawlerFile.OpenAIFile)
75+
76+
// await client.files.del(
77+
// fileId: crawlerFile.OpenAIFile.openAIFileId
78+
// )
79+
//}
80+
81+
5382
await del(crawlerFile.blobUrl)
5483

5584
await db.crawlerFile.delete({
@@ -60,6 +89,7 @@ export async function DELETE(
6089

6190
return new Response(null, { status: 204 })
6291
} catch (error) {
92+
console.log(error)
6393
if (error instanceof z.ZodError) {
6494
return new Response(JSON.stringify(error.issues), { status: 422 })
6595
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { getServerSession } from "next-auth/next"
2+
import { z } from "zod"
3+
4+
import { authOptions } from "@/lib/auth"
5+
import { db } from "@/lib/db"
6+
import { openAIConfigSchema } from "@/lib/validations/openaiConfig"
7+
8+
const routeContextSchema = z.object({
9+
params: z.object({
10+
userId: z.string(),
11+
}),
12+
})
13+
14+
export async function PATCH(
15+
req: Request,
16+
context: z.infer<typeof routeContextSchema>
17+
) {
18+
try {
19+
// Validate the route context.
20+
const { params } = routeContextSchema.parse(context)
21+
22+
// Ensure user is authentication and has access to this user.
23+
const session = await getServerSession(authOptions)
24+
if (!session?.user || params.userId !== session?.user.id) {
25+
return new Response(null, { status: 403 })
26+
}
27+
28+
// Get the request body and validate it.
29+
const body = await req.json()
30+
const payload = openAIConfigSchema.parse(body)
31+
32+
// Update the user.
33+
await db.openAIConfig.upsert({
34+
where: {
35+
userId: session.user.id,
36+
},
37+
create: {
38+
userId: session.user.id,
39+
...payload
40+
},
41+
update: {
42+
...payload
43+
}
44+
})
45+
46+
47+
return new Response(null, { status: 200 })
48+
} catch (error) {
49+
if (error instanceof z.ZodError) {
50+
return new Response(JSON.stringify(error.issues), { status: 422 })
51+
}
52+
console.error(error)
53+
54+
return new Response(null, { status: 500 })
55+
}
56+
}

0 commit comments

Comments
 (0)