-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.tsx
More file actions
179 lines (161 loc) · 5.63 KB
/
chat.tsx
File metadata and controls
179 lines (161 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use client';
import { useChat } from '@ai-sdk/react';
import type * as AI from 'ai';
import { useClientTransport, useActiveTurns, useView, useAblyMessages } from '@ably/ai-transport/react';
import { useChatTransport, useMessageSync } from '@ably/ai-transport/vercel/react';
import { useState, useEffect, useCallback } from 'react';
import { MessageList } from './components/message-list';
import { DebugPane } from './components/debug-pane';
import type { CallbackLogEntry } from './components/debug-pane';
import { useClientTools } from './hooks/use-client-tools';
// ---------------------------------------------------------------------------
// Chat component
// ---------------------------------------------------------------------------
export function Chat({ chatId, clientId, historyLimit }: { chatId: string; clientId?: string; historyLimit?: number }) {
// Transport is created by TransportProvider in page.tsx
const transport = useClientTransport<AI.UIMessageChunk, AI.UIMessage>({ channelName: chatId });
const chatTransport = useChatTransport(transport);
// -- Callback & status logging for debug pane ----------------------------
const [callbackLog, setCallbackLog] = useState<CallbackLogEntry[]>([]);
const [statusLog, setStatusLog] = useState<{ time: number; status: string }[]>([]);
const clearLogs = useCallback(() => {
setCallbackLog([]);
setStatusLog([]);
}, []);
const {
setMessages,
sendMessage,
stop,
status,
regenerate,
addToolResult,
messages: chatMessages,
} = useChat({
id: chatId,
transport: chatTransport,
onToolCall: ({ toolCall }) => {
setCallbackLog((prev) => [
...prev,
{
time: Date.now(),
type: 'onToolCall',
summary: `${toolCall.toolName}(${JSON.stringify(toolCall.input)})`,
},
]);
},
onFinish: ({ message, finishReason }) => {
setCallbackLog((prev) => [
...prev,
{
time: Date.now(),
type: 'onFinish',
summary: `reason=${String(finishReason)}, parts=${String(message.parts.length)}`,
},
]);
},
});
useMessageSync(transport, setMessages, chatTransport);
// Track status transitions
useEffect(() => {
setStatusLog((prev) => [...prev, { time: Date.now(), status }]);
}, [status]);
const activeTurns = useActiveTurns({ transport });
const hasAnyTurns = activeTurns.size > 0;
// Auto-loads first page on mount
const { nodes, hasOlder, loading, loadOlder } = useView({ transport, limit: historyLimit ?? 30 });
useClientTools(chatMessages, addToolResult, nodes, clientId);
const ablyMessages = useAblyMessages({ transport });
return (
<div className="flex h-dvh">
<div className="flex flex-1 flex-col">
<Header clientId={clientId} />
<MessageList
nodes={nodes}
hasOlder={hasOlder}
loading={loading}
onLoadOlder={loadOlder}
onRegenerate={(messageId) => regenerate({ messageId })}
onEdit={(messageId, newText) => sendMessage({ text: newText, messageId })}
/>
<InputBar
onSend={(text) => sendMessage({ text })}
onStop={stop}
hasAnyTurns={hasAnyTurns}
/>
</div>
<DebugPane
messages={nodes.map((n) => n.message)}
ablyMessages={ablyMessages}
activeTurns={activeTurns}
status={status}
callbackLog={callbackLog}
statusLog={statusLog}
onClearLogs={clearLogs}
/>
</div>
);
}
// ---------------------------------------------------------------------------
// Header
// ---------------------------------------------------------------------------
function Header({ clientId }: { clientId?: string }) {
return (
<header className="flex items-center gap-2 border-b border-zinc-800 px-4 py-3">
<div className="h-2 w-2 rounded-full bg-emerald-500" />
<h1 className="text-sm font-medium text-zinc-300">Ably AI — Vercel UI SDK</h1>
{clientId && <span className="ml-auto text-xs text-zinc-600 font-mono">{clientId}</span>}
</header>
);
}
// ---------------------------------------------------------------------------
// Input bar — single Stop button when streaming, Send button otherwise
// ---------------------------------------------------------------------------
function InputBar({
onSend,
onStop,
hasAnyTurns,
}: {
onSend: (text: string) => void;
onStop: () => void;
hasAnyTurns: boolean;
}) {
const [input, setInput] = useState('');
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const text = input.trim();
if (!text) return;
setInput('');
onSend(text);
};
return (
<form
onSubmit={handleSubmit}
className="border-t border-zinc-800 px-4 py-3 flex gap-2"
>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Type a message..."
className="flex-1 rounded-md bg-zinc-900 border border-zinc-700 px-3 py-2 text-sm text-zinc-200 placeholder-zinc-600 outline-none focus:border-zinc-500"
autoFocus
/>
{hasAnyTurns ? (
<button
type="button"
onClick={onStop}
className="rounded-md bg-red-900/60 px-4 py-2 text-sm font-medium text-red-300 hover:bg-red-900/80 transition-colors"
>
Stop
</button>
) : (
<button
type="submit"
disabled={!input.trim()}
className="rounded-md bg-zinc-700 px-4 py-2 text-sm font-medium text-zinc-200 hover:bg-zinc-600 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
>
Send
</button>
)}
</form>
);
}