|
1 | 1 | import type { NextRequest } from "next/server" |
2 | 2 |
|
3 | 3 | import { AssistantResponse, type ToolCall } from "ai" |
4 | | -import { kv } from "@vercel/kv" |
5 | 4 |
|
6 | 5 | import { APP_URL } from "@/app/config" |
7 | 6 | import { openai } from "@/lib/openai" |
8 | 7 | import { auth } from "@/auth" |
9 | 8 | import deployContract from "@/lib/functions/deploy-contract/deploy-contract" |
10 | 9 | import { createAgent } from "@/lib/actions/ai" |
11 | 10 | import type { DbChat } from "@/lib/types" |
| 11 | +import { storeChat } from "@/lib/actions/db" |
12 | 12 |
|
13 | 13 | export const runtime = "nodejs" |
14 | 14 |
|
@@ -41,26 +41,23 @@ export async function POST(request: NextRequest) { |
41 | 41 | const title = message.slice(0, 50) |
42 | 42 | const newChat: DbChat = { |
43 | 43 | id: threadId, |
| 44 | + userId, |
44 | 45 | title, |
45 | 46 | agentId: assistantId, |
46 | | - userId, |
47 | | - createdAt: new Date(createdAt), |
| 47 | + createdAt: createdAt, |
48 | 48 | avatarUrl: avatarUrl, |
49 | 49 | published: false, |
50 | 50 | messages: [{ id: messageId, role: "user", content: message }] |
51 | 51 | } |
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) |
57 | 54 | } |
58 | 55 |
|
59 | 56 | return AssistantResponse({ threadId, messageId }, async ({ forwardStream, sendDataMessage }) => { |
60 | 57 | // Run the assistant on the thread |
61 | 58 | const runStream = openai.beta.threads.runs.stream(threadId, { |
62 | 59 | assistant_id: assistantId, |
63 | | - stream: true |
| 60 | + stream: true, |
64 | 61 | }) |
65 | 62 |
|
66 | 63 | // forward run status would stream message deltas |
@@ -94,9 +91,9 @@ export async function POST(request: NextRequest) { |
94 | 91 | } |
95 | 92 | } catch (error) { |
96 | 93 | const err = error as Error |
97 | | - console.error(`Error in deployContract: ${err.message}`) |
| 94 | + console.error(`Error in deployContract tool: ${err.message}`) |
98 | 95 | return { |
99 | | - output: JSON.stringify({ error: `Error in deployContract: ${err.message}` }), |
| 96 | + output: JSON.stringify({ error: `Error in deployContract tool: ${err.message}` }), |
100 | 97 | tool_call_id: toolCall.id |
101 | 98 | } |
102 | 99 | } |
|
0 commit comments