Skip to content

Commit 6013712

Browse files
committed
og, optimizations
1 parent 779c90e commit 6013712

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+579
-489
lines changed

.env.example

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
# KV REST API config
2-
KV_REST_API_READ_ONLY_TOKEN=
3-
KV_REST_API_TOKEN=
4-
KV_REST_API_URL=
5-
KV_URL=
1+
# Production app url
2+
NEXT_PUBLIC_APP_URL=
63

7-
## Generate a random secret: https://generate-secret.vercel.app/32
8-
AUTH_SECRET=
9-
## Create a GitHub OAuth app here: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app
10-
AUTH_GITHUB_ID=
11-
AUTH_GITHUB_SECRET=
4+
# WalletConnect Project ID
5+
NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=
126

7+
# Deployer Private Key
8+
DEPLOYER_PRIVATE_KEY=
9+
10+
# API Keys
1311
OPENAI_API_KEY=
1412
STABILITY_API_KEY=
1513
NFT_STORAGE_API_KEY=
1614

17-
# WalletConnect Project ID
18-
NEXT_PUBLIC_WALLETCONNECT_PROJECT_ID=
19-
2015
# RPC API Keys
2116
NEXT_PUBLIC_ALCHEMY_API_KEY=
2217
QUICKNODE_API_KEY=
@@ -29,8 +24,16 @@ ARBITRUM_EXPLORER_API_KEY=
2924
OPTIMISM_EXPLORER_API_KEY=
3025
BASE_EXPLORER_API_KEY=
3126

32-
# Deployer Private Key
33-
DEPLOYER_PRIVATE_KEY=
34-
3527
# dev: http://localhost prod: https://w3gpt.ai
3628
NEXTAUTH_URL=
29+
## Generate a random secret: https://generate-secret.vercel.app/32
30+
AUTH_SECRET=
31+
## Create a GitHub OAuth app here: https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app
32+
AUTH_GITHUB_ID=
33+
AUTH_GITHUB_SECRET=
34+
35+
# KV REST API config
36+
KV_REST_API_READ_ONLY_TOKEN=
37+
KV_REST_API_TOKEN=
38+
KV_REST_API_URL=
39+
KV_URL=

LICENSE

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
Copyright 2023 Vercel, Inc.
1+
MIT License
22

3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
You may obtain a copy of the License at
3+
Copyright (c) 2023 W3GPT Corp
64

7-
http://www.apache.org/licenses/LICENSE-2.0
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
811

9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License.
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

app/api/assistants/threads/messages/route.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { NextRequest } from "next/server"
22

33
import { AssistantResponse, type ToolCall } from "ai"
4-
import { kv } from "@vercel/kv"
54

65
import { APP_URL } from "@/app/config"
76
import { openai } from "@/lib/openai"
87
import { auth } from "@/auth"
98
import deployContract from "@/lib/functions/deploy-contract/deploy-contract"
109
import { createAgent } from "@/lib/actions/ai"
1110
import type { DbChat } from "@/lib/types"
11+
import { storeChat } from "@/lib/actions/db"
1212

1313
export const runtime = "nodejs"
1414

@@ -41,26 +41,23 @@ export async function POST(request: NextRequest) {
4141
const title = message.slice(0, 50)
4242
const newChat: DbChat = {
4343
id: threadId,
44+
userId,
4445
title,
4546
agentId: assistantId,
46-
userId,
47-
createdAt: new Date(createdAt),
47+
createdAt: createdAt,
4848
avatarUrl: avatarUrl,
4949
published: false,
5050
messages: [{ id: messageId, role: "user", content: message }]
5151
}
52-
await kv.hmset(`chat:${threadId}`, newChat)
53-
await kv.zadd(`user:chat:${userId}`, {
54-
score: createdAt,
55-
member: `chat:${threadId}`
56-
})
52+
53+
await storeChat(newChat)
5754
}
5855

5956
return AssistantResponse({ threadId, messageId }, async ({ forwardStream, sendDataMessage }) => {
6057
// Run the assistant on the thread
6158
const runStream = openai.beta.threads.runs.stream(threadId, {
6259
assistant_id: assistantId,
63-
stream: true
60+
stream: true,
6461
})
6562

6663
// forward run status would stream message deltas
@@ -94,9 +91,9 @@ export async function POST(request: NextRequest) {
9491
}
9592
} catch (error) {
9693
const err = error as Error
97-
console.error(`Error in deployContract: ${err.message}`)
94+
console.error(`Error in deployContract tool: ${err.message}`)
9895
return {
99-
output: JSON.stringify({ error: `Error in deployContract: ${err.message}` }),
96+
output: JSON.stringify({ error: `Error in deployContract tool: ${err.message}` }),
10097
tool_call_id: toolCall.id
10198
}
10299
}

app/api/deploy-contract/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function POST(req: Request) {
1616
return new Response(JSON.stringify(deployResult))
1717
} catch (error) {
1818
const err = error as Error
19-
console.error(`Error in deployContract: ${err.message}`)
19+
console.error(`Error in deployContract API ROUTE: ${err.message}`)
2020
return new Response(JSON.stringify({ error: `Error in deployContract: ${err.message}` }), { status: 500 })
2121
}
2222
}

app/api/external-deploy/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ export async function POST(req: NextRequest) {
119119
return new Response(JSON.stringify(deployResult))
120120
} catch (error) {
121121
const err = error as Error
122-
console.error(`Error in deployContract: ${err.message}`)
123-
return new Response(JSON.stringify({ error: `Error in deployContract: ${err.message}` }), { status: 500 })
122+
console.error(`Error in deployContract external: ${err.message}`)
123+
return new Response(JSON.stringify({ error: `Error in deployContract external: ${err.message}` }), { status: 500 })
124124
}
125125
}
126126

app/api/og/route.tsx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { ImageResponse } from "next/og"
2+
import type { NextRequest } from "next/server"
3+
4+
import { APP_URL } from "@/app/config"
5+
import { getPublishedChat } from "@/lib/actions/db"
6+
7+
export const runtime = "edge"
8+
9+
const w3gptLogoUrl = `${APP_URL}/assets/w3gpt.png`
10+
11+
export async function GET(req: NextRequest) {
12+
const chatId = req.nextUrl.searchParams.get("id")
13+
const height = req.nextUrl.searchParams.get("h")
14+
15+
if (!chatId) {
16+
throw new Error("Chat ID is required")
17+
}
18+
19+
const chat = await getPublishedChat(chatId)
20+
21+
return new ImageResponse(
22+
<div tw="flex w-full items-center h-full flex-col bg-[#000000] text-white p-[80px]">
23+
<svg
24+
xmlns="http://www.w3.org/2000/svg"
25+
role="img"
26+
aria-label="w3gpt beta"
27+
width="362"
28+
height="59"
29+
viewBox="0 0 362 59"
30+
fill="none"
31+
>
32+
<path
33+
d="m124.608 27.128 5.856 13.392-.864.096 5.472-13.488h4.368L130.32 48.2l-6.384-13.632-5.808 13.632-9.216-21.072h4.368l6.192 14.016-1.536-.288 4.416-10.272-1.584-3.456zm36.712-11.712L152.872 29l-2.784-1.296a4.8 4.8 0 0 1 1.392-.48 8.3 8.3 0 0 1 1.536-.144 10.4 10.4 0 0 1 3.456.384 8.3 8.3 0 0 1 3.024 1.584q1.344 1.104 2.112 2.928.816 1.824.816 4.512 0 3.36-1.536 5.808a10.56 10.56 0 0 1-4.08 3.792q-2.496 1.296-5.568 1.296-2.304 0-4.608-.912-2.256-.96-3.744-2.496l2.352-3.84q.912 1.056 2.496 1.92 1.632.864 3.36.864 1.824 0 3.264-.816a6.03 6.03 0 0 0 2.352-2.304q.912-1.536.912-3.552 0-2.784-1.632-4.368-1.584-1.584-4.368-1.584-1.392 0-2.352.24-.96.192-1.728.48l-.144-.192 7.44-11.568 1.44.624h-11.568v-4.464zm17.589 41.424q-2.208 0-3.888-.528a11.2 11.2 0 0 1-3.072-1.392 22 22 0 0 1-2.592-2.064l2.592-3.024q1.584 1.488 3.216 2.304t3.648.816q2.256 0 3.792-.768 1.584-.72 2.4-2.064.816-1.296.816-3.024l.048-5.376.384.864q-.864 2.064-3.12 3.504-2.256 1.392-5.472 1.392a9.57 9.57 0 0 1-5.04-1.392q-2.256-1.392-3.648-3.744-1.344-2.4-1.344-5.376 0-3.12 1.44-5.472 1.44-2.4 3.792-3.792t5.136-1.392q2.64 0 4.8 1.104 2.208 1.056 3.216 2.736l-.24.816.48-3.84h4.08v20.016q0 2.688-1.392 4.896t-3.936 3.504-6.096 1.296m-6.816-19.968q0 1.92.912 3.504a6.8 6.8 0 0 0 2.448 2.496q1.584.912 3.504.912 1.776 0 3.168-.624 1.44-.672 2.4-1.776a6.7 6.7 0 0 0 1.344-2.64v-3.936a6.1 6.1 0 0 0-1.44-2.496 6.7 6.7 0 0 0-2.4-1.68q-1.392-.624-3.072-.624-1.92 0-3.504.864-1.536.864-2.448 2.448-.912 1.536-.912 3.552m37.663 10.608q-2.256 0-4.368-1.056-2.064-1.056-3.216-2.832l.288-1.392v14.352h-4.416V26.84h3.84l.624 5.088-.48-1.392q1.44-1.824 3.648-3.024t4.944-1.2q2.784 0 4.992 1.344 2.256 1.344 3.552 3.744t1.296 5.616-1.392 5.568a9.6 9.6 0 0 1-3.84 3.648q-2.4 1.248-5.472 1.248m-.864-3.552q1.92 0 3.504-.864a7.06 7.06 0 0 0 2.592-2.448q.96-1.584.96-3.552 0-2.016-.912-3.552a6.8 6.8 0 0 0-2.448-2.496q-1.488-.912-3.36-.912-1.728 0-3.12.672a6.3 6.3 0 0 0-2.304 1.872q-.912 1.152-1.296 2.688v3.312a6 6 0 0 0 1.152 2.736q.912 1.2 2.256 1.872 1.392.672 2.976.672m19.525-25.536h4.464v8.832h5.472v3.504h-5.472V47h-4.464V30.728h-3.648v-3.504h3.648zm13.074 26.064q0-1.056.768-1.776.816-.768 1.776-.768.864 0 1.632.768.816.72.816 1.776 0 1.152-.816 1.872-.768.672-1.632.672-.96 0-1.776-.672-.768-.72-.768-1.872m18.575 3.024q-2.784 0-5.088-1.2-2.256-1.2-3.6-3.504-1.296-2.352-1.296-5.712 0-3.312 1.392-5.712 1.392-2.448 3.696-3.744 2.352-1.296 5.136-1.296t4.704 1.296q1.92 1.248 2.928 2.976l-.288.768.432-4.224h4.128V47h-4.464v-5.184l.48 1.152q-.192.48-.864 1.248-.624.72-1.728 1.488-1.056.768-2.448 1.296-1.392.48-3.12.48m1.2-3.696q1.68 0 3.024-.624a5.9 5.9 0 0 0 2.208-1.728q.912-1.152 1.248-2.736v-3.888a5.9 5.9 0 0 0-1.344-2.496 6.3 6.3 0 0 0-2.304-1.68q-1.344-.624-2.976-.624a6.4 6.4 0 0 0-3.264.864 6.64 6.64 0 0 0-2.4 2.4q-.864 1.536-.864 3.6 0 1.92.912 3.504a6.8 6.8 0 0 0 2.448 2.496 6.4 6.4 0 0 0 3.312.912m18.669-16.656h4.464V47h-4.464zm-.336-7.2q0-1.056.816-1.776.864-.72 1.872-.72t1.776.72q.816.72.816 1.776 0 1.104-.816 1.824a2.62 2.62 0 0 1-1.776.672q-1.008 0-1.872-.72-.816-.72-.816-1.776m13.483 23.184h19.44V47h-19.44zm31.911-25.902q-1.197 0-2.121-.525a4.4 4.4 0 0 1-1.449-1.323l.231-.609V17h-1.953V.746h1.932v9.555l-.063-.735q.525-.714 1.491-1.155.987-.462 2.184-.462 1.176 0 2.142.567t1.533 1.596q.588 1.008.588 2.415 0 1.47-.63 2.52a4.23 4.23 0 0 1-1.638 1.617 4.7 4.7 0 0 1-2.247.546m-.399-1.659q.84 0 1.512-.399a2.86 2.86 0 0 0 1.05-1.071q.378-.693.378-1.575 0-.84-.399-1.512a2.8 2.8 0 0 0-1.05-1.092 2.8 2.8 0 0 0-1.491-.399q-.714 0-1.323.273a2.9 2.9 0 0 0-1.029.756q-.42.462-.588 1.092v1.743q.168.63.567 1.134.399.483 1.008.777.609.273 1.365.273m11.587 1.659q-1.491 0-2.562-.588a4.34 4.34 0 0 1-1.659-1.638q-.567-1.05-.567-2.394 0-1.281.651-2.331a4.9 4.9 0 0 1 1.743-1.68q1.092-.63 2.436-.63 1.722 0 2.856 1.008t1.533 2.772l-7.161 2.52-.462-1.155 5.88-2.142-.42.273a2.84 2.84 0 0 0-.84-1.197q-.588-.525-1.533-.525-.798 0-1.428.399-.63.378-.987 1.05t-.357 1.533q0 .903.378 1.596.378.672 1.029 1.071a3.03 3.03 0 0 0 1.512.378q.567 0 1.092-.21.546-.21 1.008-.546l.903 1.449a6 6 0 0 1-1.47.714 4.9 4.9 0 0 1-1.575.273m7.718-12.726h1.953v3.864h2.394v1.533h-2.394V17h-1.953V9.881h-1.596V8.348h1.596zm10.319 12.726a4.75 4.75 0 0 1-2.226-.525 3.95 3.95 0 0 1-1.575-1.533q-.567-1.029-.567-2.499 0-1.449.609-2.499.609-1.071 1.617-1.638a4.6 4.6 0 0 1 2.247-.567q1.218 0 2.058.567.84.546 1.281 1.302l-.126.336.189-1.848h1.806V17h-1.953v-2.268l.21.504q-.084.21-.378.546-.273.315-.756.651a4.6 4.6 0 0 1-1.071.567 4.2 4.2 0 0 1-1.365.21m.525-1.617q.735 0 1.323-.273t.966-.756a2.85 2.85 0 0 0 .546-1.197v-1.701a2.6 2.6 0 0 0-.588-1.092 2.76 2.76 0 0 0-1.008-.735 3.05 3.05 0 0 0-1.302-.273q-.777 0-1.428.378a2.9 2.9 0 0 0-1.05 1.05q-.378.672-.378 1.575a3.02 3.02 0 0 0 1.47 2.625 2.8 2.8 0 0 0 1.449.399M0 15h36v8H0zm36 16h32v8H36zM20 47h32v8H20zm28-32h36v8H48z"
34+
fill="#21DA03"
35+
/>
36+
</svg>
37+
38+
<div tw="flex flex-col w-full pt-[80px]">
39+
<div tw="flex w-full items-center">
40+
<div tw="flex h-[80px] w-[80px] items-center justify-center rounded-md border border-[#9b9ba4]">
41+
{chat?.avatarUrl ? (
42+
<img
43+
src={chat.avatarUrl}
44+
alt={chat.title}
45+
tw="w-full h-full object-cover rounded-md"
46+
height={48}
47+
width={48}
48+
/>
49+
) : (
50+
<svg
51+
role="img"
52+
aria-label="Chat"
53+
xmlns="http://www.w3.org/2000/svg"
54+
viewBox="0 0 256 256"
55+
fill="currentColor"
56+
width={48}
57+
height={48}
58+
>
59+
<path d="M230.92 212c-15.23-26.33-38.7-45.21-66.09-54.16a72 72 0 1 0-73.66 0c-27.39 8.94-50.86 27.82-66.09 54.16a8 8 0 1 0 13.85 8c18.84-32.56 52.14-52 89.07-52s70.23 19.44 89.07 52a8 8 0 1 0 13.85-8ZM72 96a56 56 0 1 1 56 56 56.06 56.06 0 0 1-56-56Z" />
60+
</svg>
61+
)}
62+
</div>
63+
<div tw="flex text-white font-bold text-4xl leading-normal ml-10">
64+
{chat?.title || "Create an NFT contract with staking and royalties"}
65+
</div>
66+
</div>
67+
<div tw="flex w-full mt-14 items-start">
68+
<div tw="flex h-[80px] w-[80px] items-center justify-center rounded-md border border-[#9b9ba4]">
69+
<img src={w3gptLogoUrl} alt={"w3gpt assistant logo"} tw="w-full h-full object-cover rounded-md" />
70+
</div>
71+
<div tw="flex text-white font-bold text-6xl leading-none ml-10">...</div>
72+
</div>
73+
</div>
74+
<div tw="flex items-center justify-between w-full mt-auto">
75+
<div tw="text-[1.8rem] ml-auto text-[#9b9ba4]">w3gpt.ai</div>
76+
</div>
77+
</div>,
78+
{
79+
width: 1200,
80+
height: height ? Number.parseInt(height) : 630
81+
}
82+
)
83+
}

app/chat/[id]/opengraph-image.png

78.4 KB
Loading

app/chat/[id]/page.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ export default async function ChatPage({ params, searchParams }: ChatPageProps)
1616
const chat = await getChat(params.id)
1717

1818
if (!chat) {
19+
redirect("/")
20+
}
21+
22+
if (chat?.userId !== session?.user?.id) {
1923
notFound()
2024
}
2125

2226
const agentId = chat.agentId || (searchParams?.a as string)
23-
const agent = (agentId && (await getAgent(agentId))) || undefined
24-
25-
const threadId = chat.id
26-
27-
const messages = await getAiThreadMessages(threadId)
27+
const [agent, messages] = await Promise.all([agentId ? getAgent(agentId) : undefined, getAiThreadMessages(params.id)])
2828

29-
return <Chat agent={agent} threadId={threadId} initialMessages={messages} session={session} />
29+
return <Chat agent={agent} threadId={chat.id} initialMessages={messages} session={session} />
3030
}

app/chat/[id]/twitter-image.png

78.8 KB
Loading

app/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const DEFAULT_GLOBAL_CONFIG: GlobalConfig = {
1717

1818
export const DEFAULT_AGENT: Agent = {
1919
id: "asst_Tgzrzv0VaSgTRMn8ufAULlZG",
20-
userId: 12901349,
20+
userId: "12901349",
2121
name: "W3GPT",
2222
description: "Develop smart contracts",
2323
creator: "soko.eth",

0 commit comments

Comments
 (0)