Skip to content

Commit 8d00d49

Browse files
committed
refactor to api route
1 parent 6f4fef8 commit 8d00d49

5 files changed

Lines changed: 82 additions & 171 deletions

File tree

app/(main)/actions.ts

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import prisma from "@/lib/prisma";
44
import { notFound } from "next/navigation";
55
import Together from "together-ai";
6-
// import { z } from "zod";
76
import {
87
getMainCodingPrompt,
98
screenshotToCodePrompt,
@@ -16,7 +15,6 @@ export async function createChat(
1615
quality: "high" | "low",
1716
screenshotUrl: string | undefined,
1817
) {
19-
console.log("-- CREATING CHAT");
2018
const chat = await prisma.chat.create({
2119
data: {
2220
model,
@@ -41,7 +39,6 @@ export async function createChat(
4139
const together = new Together(options);
4240

4341
async function fetchTitle() {
44-
console.log("-- FETCHING TITLE");
4542
const responseForChatTitle = await together.chat.completions.create({
4643
model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
4744
messages: [
@@ -61,7 +58,6 @@ export async function createChat(
6158
}
6259

6360
async function fetchTopExample() {
64-
console.log("-- FETCHING TOP EXAMPLE");
6561
const findSimilarExamples = await together.chat.completions.create({
6662
model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
6763
messages: [
@@ -147,7 +143,6 @@ export async function createChat(
147143
userMessage = prompt;
148144
}
149145

150-
console.log("-- UPDATING CHAT WITH MESSAGES");
151146
let newChat = await prisma.chat.update({
152147
where: {
153148
id: chat.id,
@@ -177,8 +172,6 @@ export async function createChat(
177172
.at(-1);
178173
if (!lastMessage) throw new Error("No new message");
179174

180-
console.log("-- RESPONDING");
181-
console.log({ chatId: chat.id, lastMessageId: lastMessage.id });
182175
return {
183176
chatId: chat.id,
184177
lastMessageId: lastMessage.id,
@@ -209,73 +202,3 @@ export async function createMessage(
209202

210203
return newMessage;
211204
}
212-
213-
export async function getNextCompletionStreamPromise(
214-
messageId: string,
215-
model: string,
216-
) {
217-
console.log("getNextCompletionStreamPromise: start");
218-
console.log({ messageId, model });
219-
let message;
220-
try {
221-
message = await prisma.message.findUniqueOrThrow({
222-
where: { id: messageId },
223-
});
224-
} catch (error) {
225-
console.log("IN ERROR BLOCK");
226-
console.log(error);
227-
}
228-
console.log("-- getNextCompletionStreamPromise: Found message ", message?.id);
229-
// if (!message) notFound();
230-
231-
// const messagesRes = await prisma.message.findMany({
232-
// where: { chatId: message.chatId, position: { lte: message.position } },
233-
// orderBy: { position: "asc" },
234-
// });
235-
// console.log("-- getNextCompletionStreamPromise: found messages");
236-
237-
// let messages = z
238-
// .array(
239-
// z.object({
240-
// role: z.enum(["system", "user", "assistant"]),
241-
// content: z.string(),
242-
// }),
243-
// )
244-
// .parse(messagesRes);
245-
// console.log("-- getNextCompletionStreamPromise: parsed messages");
246-
247-
// if (messages.length > 10) {
248-
// messages = [messages[0], messages[1], messages[2], ...messages.slice(-7)];
249-
// }
250-
251-
// let options: ConstructorParameters<typeof Together>[0] = {};
252-
// if (process.env.HELICONE_API_KEY) {
253-
// options.baseURL = "https://together.helicone.ai/v1";
254-
// options.defaultHeaders = {
255-
// "Helicone-Auth": `Bearer ${process.env.HELICONE_API_KEY}`,
256-
// "Helicone-Property-appname": "LlamaCoder",
257-
// "Helicone-Session-Id": message.chatId,
258-
// "Helicone-Session-Name": "LlamaCoder Chat",
259-
// };
260-
// }
261-
262-
// console.log("getNextCompletionStreamPromise: creating together client");
263-
// const together = new Together(options);
264-
265-
// console.log("getNextCompletionStreamPromise: returning stream");
266-
// return {
267-
// streamPromise: new Promise<ReadableStream>(async (resolve) => {
268-
// console.log("getNextCompletionStreamPromise: querying together");
269-
// const res = await together.chat.completions.create({
270-
// model,
271-
// messages: messages.map((m) => ({ role: m.role, content: m.content })),
272-
// stream: true,
273-
// temperature: 0.2,
274-
// max_tokens: 9000,
275-
// });
276-
277-
// console.log("getNextCompletionStreamPromise: resolving promise");
278-
// resolve(res.toReadableStream());
279-
// }),
280-
// };
281-
}

app/(main)/chats/[id]/chat-box.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import Spinner from "@/components/spinner";
55
import assert from "assert";
66
import { useRouter } from "next/navigation";
77
import { useEffect, useRef, useState, useTransition } from "react";
8-
// import TextareaAutosize from "react-textarea-autosize";
9-
import { createMessage, getNextCompletionStreamPromise } from "../../actions";
8+
import { createMessage } from "../../actions";
109
import { type Chat } from "./page";
1110

1211
export default function ChatBox({
@@ -50,11 +49,24 @@ export default function ChatBox({
5049
assert.ok(typeof prompt === "string");
5150

5251
const message = await createMessage(chat.id, prompt, "user");
53-
await getNextCompletionStreamPromise(message.id, chat.model);
54-
console.log(router);
55-
// onNewStreamPromise(streamPromise);
52+
const streamPromise = fetch(
53+
"/api/get-next-completion-stream-promise",
54+
{
55+
method: "POST",
56+
body: JSON.stringify({
57+
messageId: message.id,
58+
model: chat.model,
59+
}),
60+
},
61+
).then((res) => {
62+
if (!res.body) {
63+
throw "foo";
64+
}
65+
return res.body;
66+
});
5667

57-
// router.refresh();
68+
onNewStreamPromise(streamPromise);
69+
router.refresh();
5870
});
5971
}}
6072
>

app/(main)/page.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Image from "next/image";
1414
import Link from "next/link";
1515
import { useRouter } from "next/navigation";
1616
import { use, useState, useRef, useTransition } from "react";
17-
// import { createChat, getNextCompletionStreamPromise } from "./actions";
1817
import { createChat } from "./actions";
1918
import { Context } from "./providers";
2019
import Header from "@/components/header";
@@ -98,31 +97,28 @@ export default function Home() {
9897
assert.ok(typeof model === "string");
9998
assert.ok(quality === "high" || quality === "low");
10099

101-
console.log("-- CLIENT: createChat");
102100
const { chatId, lastMessageId } = await createChat(
103101
prompt,
104102
model,
105103
quality,
106104
screenshotUrl,
107105
);
108-
console.log("-- CLIENT: getNextCompletionStreamPromise");
109-
let res = await fetch(
106+
107+
let streamPromise = fetch(
110108
"/api/get-next-completion-stream-promise",
111109
{
112110
method: "POST",
113111
body: JSON.stringify({ messageId: lastMessageId, model }),
114112
},
115-
);
116-
let json = await res.json();
117-
console.log(json);
118-
// await getNextCompletionStreamPromise(lastMessageId, model);
119-
console.log(setStreamPromise);
120-
// const { streamPromise } = await getNextCompletionStreamPromise(
121-
// lastMessageId,
122-
// model,
123-
// );
113+
).then((res) => {
114+
if (!res.body) {
115+
throw "foo";
116+
}
117+
return res.body;
118+
});
119+
124120
startTransition(() => {
125-
// setStreamPromise(streamPromise);
121+
setStreamPromise(streamPromise);
126122
router.push(`/chats/${chatId}`);
127123
});
128124
});
Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,58 @@
11
import prisma from "@/lib/prisma";
2-
// import { z } from "zod";
3-
// import Together from "together-ai";
2+
import { z } from "zod";
3+
import Together from "together-ai";
44

55
export async function POST(req: Request) {
66
const { messageId, model } = await req.json();
77

8-
console.log("begin: ", messageId, model);
9-
108
const message = await prisma.message.findUniqueOrThrow({
119
where: { id: messageId },
1210
});
1311

14-
console.log("got message: ", message.id);
15-
16-
return Response.json({ ok: true });
17-
18-
// if (!message) {
19-
// new Response(null, { status: 404 });
20-
// }
21-
22-
// const messagesRes = await prisma.message.findMany({
23-
// where: { chatId: message.chatId, position: { lte: message.position } },
24-
// orderBy: { position: "asc" },
25-
// });
26-
27-
// let messages = z
28-
// .array(
29-
// z.object({
30-
// role: z.enum(["system", "user", "assistant"]),
31-
// content: z.string(),
32-
// }),
33-
// )
34-
// .parse(messagesRes);
12+
if (!message) {
13+
new Response(null, { status: 404 });
14+
}
3515

36-
// if (messages.length > 10) {
37-
// messages = [messages[0], messages[1], messages[2], ...messages.slice(-7)];
38-
// }
39-
40-
// let options: ConstructorParameters<typeof Together>[0] = {};
41-
// if (process.env.HELICONE_API_KEY) {
42-
// options.baseURL = "https://together.helicone.ai/v1";
43-
// options.defaultHeaders = {
44-
// "Helicone-Auth": `Bearer ${process.env.HELICONE_API_KEY}`,
45-
// "Helicone-Property-appname": "LlamaCoder",
46-
// "Helicone-Session-Id": message.chatId,
47-
// "Helicone-Session-Name": "LlamaCoder Chat",
48-
// };
49-
// }
50-
51-
// const together = new Together(options);
52-
53-
// const res = await together.chat.completions.create({
54-
// model,
55-
// messages: messages.map((m) => ({ role: m.role, content: m.content })),
56-
// stream: true,
57-
// temperature: 0.2,
58-
// max_tokens: 9000,
59-
// });
16+
const messagesRes = await prisma.message.findMany({
17+
where: { chatId: message.chatId, position: { lte: message.position } },
18+
orderBy: { position: "asc" },
19+
});
6020

61-
// return {
62-
// streamPromise: new Promise<ReadableStream>(async (resolve) => {
63-
// console.log("getNextCompletionStreamPromise: querying together");
64-
// const res = await together.chat.completions.create({
65-
// model,
66-
// messages: messages.map((m) => ({ role: m.role, content: m.content })),
67-
// stream: true,
68-
// temperature: 0.2,
69-
// max_tokens: 9000,
70-
// });
21+
let messages = z
22+
.array(
23+
z.object({
24+
role: z.enum(["system", "user", "assistant"]),
25+
content: z.string(),
26+
}),
27+
)
28+
.parse(messagesRes);
29+
30+
if (messages.length > 10) {
31+
messages = [messages[0], messages[1], messages[2], ...messages.slice(-7)];
32+
}
33+
34+
let options: ConstructorParameters<typeof Together>[0] = {};
35+
if (process.env.HELICONE_API_KEY) {
36+
options.baseURL = "https://together.helicone.ai/v1";
37+
options.defaultHeaders = {
38+
"Helicone-Auth": `Bearer ${process.env.HELICONE_API_KEY}`,
39+
"Helicone-Property-appname": "LlamaCoder",
40+
"Helicone-Session-Id": message.chatId,
41+
"Helicone-Session-Name": "LlamaCoder Chat",
42+
};
43+
}
44+
45+
const together = new Together(options);
46+
47+
const res = await together.chat.completions.create({
48+
model,
49+
messages: messages.map((m) => ({ role: m.role, content: m.content })),
50+
stream: true,
51+
temperature: 0.2,
52+
max_tokens: 9000,
53+
});
7154

72-
// console.log("getNextCompletionStreamPromise: resolving promise");
73-
// resolve(res.toReadableStream());
74-
// }),
75-
// };
55+
return new Response(res.toReadableStream());
7656
}
7757

7858
export const runtime = "edge";

next.config.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import bundleAnalyzer from "@next/bundle-analyzer";
33

44
const nextConfig: NextConfig = {
55
serverExternalPackages: ["@codesandbox/sdk"],
6-
// webpack: (config, options) => {
7-
// if (options.nextRuntime === "edge") {
8-
// if (!config.resolve.conditionNames) {
9-
// config.resolve.conditionNames = ["require", "node"];
10-
// }
11-
// if (!config.resolve.conditionNames.includes("worker")) {
12-
// config.resolve.conditionNames.push("worker");
13-
// }
14-
// }
15-
// return config;
16-
// },
6+
webpack: (config, options) => {
7+
if (options.nextRuntime === "edge") {
8+
if (!config.resolve.conditionNames) {
9+
config.resolve.conditionNames = ["require", "node"];
10+
}
11+
if (!config.resolve.conditionNames.includes("worker")) {
12+
config.resolve.conditionNames.push("worker");
13+
}
14+
}
15+
return config;
16+
},
1717
};
1818

1919
const withBundleAnalyzer = bundleAnalyzer({

0 commit comments

Comments
 (0)