Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/(main)/chats/[id]/chat-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ArrowRightIcon from "@/components/icons/arrow-right";
import Spinner from "@/components/spinner";
import assert from "assert";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState, useTransition } from "react";
import { createMessage } from "../../actions";
Expand Down
3 changes: 3 additions & 0 deletions app/(main)/chats/[id]/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export default function PageClient({ chat }: { chat: Chat }) {
setActiveTab("preview");
}
})
.on("error", (error) => {
console.error("Stream error:", error);
})
.on("finalContent", async (finalText) => {
startTransition(async () => {
const message = await createMessage(
Expand Down
44 changes: 41 additions & 3 deletions app/api/get-next-completion-stream-promise/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { PrismaNeon } from "@prisma/adapter-neon";
import { Pool } from "@neondatabase/serverless";
import { z } from "zod";
import Together from "together-ai";
import { ChatCompletionStream } from "together-ai/lib/ChatCompletionStream.mjs";
import { after } from "next/server";

export async function POST(req: Request) {
const neon = new Pool({ connectionString: process.env.DATABASE_URL });
Expand Down Expand Up @@ -57,8 +59,44 @@ export async function POST(req: Request) {
max_tokens: 9000,
});

return new Response(res.toReadableStream());
const stream = res.toReadableStream();

const [s1, s2] = stream.tee();

let unlock: () => void;
const promise = new Promise<void>((resolve) => {
unlock = () => {
resolve();
};
});

console.log("last message:", messages.at(-1));

ChatCompletionStream.fromReadableStream(s1)
.on("content", (delta) => {
// console.log("Stream content:", delta);
})
.on("error", (error) => {
console.error("Stream error:", error);
unlock();
})
.on("finalChatCompletion", (finalChunk) => {
console.log("Final Chat Completion:", finalChunk);
})
.on("finalContent", (finalText) => {
console.log("Final content hook called");
})
.on("end", () => {
console.log("Stream ended");
unlock();
});

after(async () => {
await promise;
console.log("exiting after hook");
});

return new Response(s2);
}

export const runtime = "edge";
export const maxDuration = 45;
export const maxDuration = 240;
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"sonner": "^1.7.2",
"tailwind-merge": "^2.6.0",
"tailwindcss-animate": "^1.0.7",
"together-ai": "^0.11.1",
"together-ai": "^0.20.0",
"use-stick-to-bottom": "^1.0.43",
"vaul": "^1.1.2",
"zod": "^3.24.1"
Expand Down