Skip to content

Commit a9322d7

Browse files
committed
Some improvements to the example ai-chat
1 parent 11e3306 commit a9322d7

File tree

5 files changed

+198
-71
lines changed

5 files changed

+198
-71
lines changed

references/ai-chat/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ model User {
2121
model Chat {
2222
id String @id
2323
title String
24+
model String @default("gpt-4o-mini")
2425
messages Json @default("[]")
2526
userId String?
2627
user User? @relation(fields: [userId], references: [id])

references/ai-chat/src/app/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export const getChatToken = async () => chat.createAccessToken<typeof aiChat>("a
88

99
export async function getChatList() {
1010
const chats = await prisma.chat.findMany({
11-
select: { id: true, title: true, createdAt: true, updatedAt: true },
11+
select: { id: true, title: true, model: true, createdAt: true, updatedAt: true },
1212
orderBy: { updatedAt: "desc" },
1313
});
1414
return chats.map((c) => ({
1515
id: c.id,
1616
title: c.title,
17+
model: c.model,
1718
createdAt: c.createdAt.getTime(),
1819
updatedAt: c.updatedAt.getTime(),
1920
}));

references/ai-chat/src/components/chat-app.tsx

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { aiChat } from "@/trigger/chat";
77
import { useCallback, useEffect, useState } from "react";
88
import { Chat } from "@/components/chat";
99
import { ChatSidebar } from "@/components/chat-sidebar";
10+
import { DEFAULT_MODEL } from "@/lib/models";
1011
import {
1112
getChatToken,
1213
getChatList,
@@ -19,18 +20,22 @@ import {
1920
type ChatMeta = {
2021
id: string;
2122
title: string;
23+
model: string;
2224
createdAt: number;
2325
updatedAt: number;
2426
};
2527

28+
type SessionInfo = {
29+
runId: string;
30+
publicAccessToken: string;
31+
lastEventId?: string;
32+
};
33+
2634
type ChatAppProps = {
2735
initialChatList: ChatMeta[];
2836
initialActiveChatId: string | null;
2937
initialMessages: UIMessage[];
30-
initialSessions: Record<
31-
string,
32-
{ runId: string; publicAccessToken: string; lastEventId?: string }
33-
>;
38+
initialSessions: Record<string, SessionInfo>;
3439
};
3540

3641
export function ChatApp({
@@ -42,15 +47,21 @@ export function ChatApp({
4247
const [chatList, setChatList] = useState<ChatMeta[]>(initialChatList);
4348
const [activeChatId, setActiveChatId] = useState<string | null>(initialActiveChatId);
4449
const [messages, setMessages] = useState<UIMessage[]>(initialMessages);
50+
const [sessions, setSessions] = useState<Record<string, SessionInfo>>(initialSessions);
51+
52+
// Model for new chats (before first message is sent)
53+
const [newChatModel, setNewChatModel] = useState(DEFAULT_MODEL);
4554

4655
const handleSessionChange = useCallback(
47-
(
48-
chatId: string,
49-
session: { runId: string; publicAccessToken: string; lastEventId?: string } | null
50-
) => {
51-
// Session creation and token updates are handled server-side via onChatStart/onTurnComplete.
52-
// We only need to clean up when the run ends (session = null).
53-
if (!session) {
56+
(chatId: string, session: SessionInfo | null) => {
57+
if (session) {
58+
setSessions((prev) => ({ ...prev, [chatId]: session }));
59+
} else {
60+
setSessions((prev) => {
61+
const next = { ...prev };
62+
delete next[chatId];
63+
return next;
64+
});
5465
deleteSessionAction(chatId);
5566
}
5667
},
@@ -86,6 +97,7 @@ export function ChatApp({
8697
const id = generateId();
8798
setActiveChatId(id);
8899
setMessages([]);
100+
setNewChatModel(DEFAULT_MODEL);
89101
}
90102

91103
function handleSelectChat(id: string) {
@@ -119,6 +131,14 @@ export function ChatApp({
119131
setChatList(list);
120132
}, []);
121133

134+
// Determine the model for the active chat
135+
const activeChatMeta = chatList.find((c) => c.id === activeChatId);
136+
const isNewChat = activeChatId != null && !activeChatMeta;
137+
const activeModel = isNewChat ? newChatModel : (activeChatMeta?.model ?? DEFAULT_MODEL);
138+
139+
// Get session for the active chat
140+
const activeSession = activeChatId ? sessions[activeChatId] : undefined;
141+
122142
return (
123143
<main className="flex h-screen">
124144
<ChatSidebar
@@ -136,6 +156,11 @@ export function ChatApp({
136156
initialMessages={messages}
137157
transport={transport}
138158
resume={messages.length > 0}
159+
model={activeModel}
160+
isNewChat={isNewChat}
161+
onModelChange={isNewChat ? setNewChatModel : undefined}
162+
session={activeSession}
163+
dashboardUrl={process.env.NEXT_PUBLIC_TRIGGER_DASHBOARD_URL}
139164
onFirstMessage={handleFirstMessage}
140165
onMessagesChange={handleMessagesChange}
141166
/>

0 commit comments

Comments
 (0)