-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathChatInterface.tsx
More file actions
331 lines (291 loc) · 10.3 KB
/
ChatInterface.tsx
File metadata and controls
331 lines (291 loc) · 10.3 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"use client"
import { useEffect, useRef, useState } from "react"
import { ChatHeader } from "./ChatHeader"
import { ChatInput } from "./ChatInput"
import { ChatMessages } from "./ChatMessages"
import { Message, MessageSegment, ToolCall } from "./types"
import { useGlobal } from "@/app/context/GlobalContext"
import { AgentCoreClient } from "@/lib/agentcore-client"
import type { AgentPattern } from "@/lib/agentcore-client"
import { submitFeedback } from "@/services/feedbackService"
import { useAuth } from "react-oidc-context"
import { useDefaultTool } from "@/hooks/useToolRenderer"
import { ToolCallDisplay } from "./ToolCallDisplay"
export default function ChatInterface() {
const [messages, setMessages] = useState<Message[]>([])
const [input, setInput] = useState("")
const [error, setError] = useState<string | null>(null)
const [client, setClient] = useState<AgentCoreClient | null>(null)
const [sessionId, setSessionId] = useState(() => crypto.randomUUID())
const { isLoading, setIsLoading } = useGlobal()
const auth = useAuth()
// Ref for message container to enable auto-scrolling
const messagesEndRef = useRef<HTMLDivElement>(null)
// Register default tool renderer (wildcard "*")
useDefaultTool(({ name, args, status, result }) => (
<ToolCallDisplay name={name} args={args} status={status} result={result} />
))
// Load agent configuration and create client on mount
useEffect(() => {
async function loadConfig() {
try {
const response = await fetch("/aws-exports.json")
if (!response.ok) {
throw new Error("Failed to load configuration")
}
const config = await response.json()
if (!config.agentRuntimeArn) {
throw new Error("Agent Runtime ARN not found in configuration")
}
const agentClient = new AgentCoreClient({
runtimeArn: config.agentRuntimeArn,
region: config.awsRegion || "us-east-1",
pattern: (config.agentPattern || "strands-single-agent") as AgentPattern,
})
setClient(agentClient)
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Unknown error"
setError(`Configuration error: ${errorMessage}`)
console.error("Failed to load agent configuration:", err)
}
}
loadConfig()
}, [])
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
}, [messages])
const sendMessage = async (userMessage: string) => {
if (!userMessage.trim() || !client) return
// Clear any previous errors
setError(null)
// Add user message to chat
const newUserMessage: Message = {
role: "user",
content: userMessage,
timestamp: new Date().toISOString(),
}
setMessages(prev => [...prev, newUserMessage])
setInput("")
setIsLoading(true)
// Create placeholder for assistant response
const assistantResponse: Message = {
role: "assistant",
content: "",
timestamp: new Date().toISOString(),
}
setMessages(prev => [...prev, assistantResponse])
try {
// Get auth token from react-oidc-context
const accessToken = auth.user?.access_token
if (!accessToken) {
throw new Error("Authentication required. Please log in again.")
}
const segments: MessageSegment[] = []
const toolCallMap = new Map<string, ToolCall>()
const updateMessage = () => {
// Build content from text segments for backward compat
const content = segments
.filter((s): s is Extract<MessageSegment, { type: "text" }> => s.type === "text")
.map(s => s.content)
.join("")
setMessages(prev => {
const updated = [...prev]
updated[updated.length - 1] = {
...updated[updated.length - 1],
content,
segments: [...segments],
}
return updated
})
}
// User identity is extracted server-side from the validated JWT token,
// not passed as a parameter — prevents impersonation via prompt injection.
await client.invoke(userMessage, sessionId, accessToken, event => {
switch (event.type) {
case "text": {
// If text arrives after a tool segment, mark all pending tools as complete
const prev = segments[segments.length - 1]
if (prev && prev.type === "tool") {
for (const tc of toolCallMap.values()) {
if (tc.status === "streaming" || tc.status === "executing") {
tc.status = "complete"
}
}
}
// Append to last text segment, or create new one
const last = segments[segments.length - 1]
if (last && last.type === "text") {
last.content += event.content
} else {
segments.push({ type: "text", content: event.content })
}
updateMessage()
break
}
case "tool_use_start": {
const tc: ToolCall = {
toolUseId: event.toolUseId,
name: event.name,
input: "",
status: "streaming",
}
toolCallMap.set(event.toolUseId, tc)
segments.push({ type: "tool", toolCall: tc })
updateMessage()
break
}
case "tool_use_delta": {
const tc = toolCallMap.get(event.toolUseId)
if (tc) {
tc.input += event.input
}
updateMessage()
break
}
case "tool_result": {
const tc = toolCallMap.get(event.toolUseId)
if (tc) {
tc.result = event.result
tc.status = "complete"
}
updateMessage()
break
}
case "message": {
if (event.role === "assistant") {
for (const tc of toolCallMap.values()) {
if (tc.status === "streaming") tc.status = "executing"
}
updateMessage()
}
break
}
}
})
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Unknown error"
setError(`Failed to get response: ${errorMessage}`)
console.error("Error invoking AgentCore:", err)
// Update the assistant message with error
setMessages(prev => {
const updated = [...prev]
updated[updated.length - 1] = {
...updated[updated.length - 1],
content:
"I apologize, but I encountered an error processing your request. Please try again.",
}
return updated
})
} finally {
setIsLoading(false)
}
}
// Handle form submission
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
sendMessage(input)
}
// Handle feedback submission
const handleFeedbackSubmit = async (
messageContent: string,
feedbackType: "positive" | "negative",
comment: string
) => {
try {
// Use ID token for API Gateway Cognito authorizer (not access token)
const idToken = auth.user?.id_token
if (!idToken) {
throw new Error("Authentication required. Please log in again.")
}
await submitFeedback(
{
sessionId,
message: messageContent,
feedbackType,
comment: comment || undefined,
},
idToken
)
console.log("Feedback submitted successfully")
} catch (err) {
const errorMessage = err instanceof Error ? err.message : "Unknown error"
console.error("Error submitting feedback:", err)
setError(`Failed to submit feedback: ${errorMessage}`)
}
}
// Start a new chat by clearing messages and generating a fresh session ID.
// A new UUID is required so the backend treats this as a distinct conversation context.
const startNewChat = () => {
setMessages([])
setInput("")
setError(null)
setSessionId(crypto.randomUUID())
}
// Check if this is the initial state (no messages)
const isInitialState = messages.length === 0
// Check if there are any assistant messages
const hasAssistantMessages = messages.some(message => message.role === "assistant")
return (
<div className="flex flex-col h-screen w-full">
{/* Fixed header */}
<div className="flex-none">
<ChatHeader onNewChat={startNewChat} canStartNewChat={hasAssistantMessages} />
{error && (
<div className="bg-red-50 border-l-4 border-red-500 p-4 mx-4 mt-2">
<p className="text-sm text-red-700">{error}</p>
</div>
)}
</div>
{/* Conditional layout based on whether there are messages */}
{isInitialState ? (
// Initial state - input in the middle
<>
{/* Empty space above */}
<div className="grow" />
{/* Centered welcome message */}
<div className="text-center mb-6">
<h2 className="text-2xl font-bold text-gray-800">Welcome to FAST Chat</h2>
<p className="text-gray-600 mt-2">Ask me anything to get started</p>
</div>
{/* Centered input */}
<div className="px-4 mb-16 max-w-4xl mx-auto w-full">
<ChatInput
input={input}
setInput={setInput}
handleSubmit={handleSubmit}
isLoading={isLoading}
/>
</div>
{/* Empty space below */}
<div className="grow" />
</>
) : (
// Chat in progress - normal layout
<>
{/* Scrollable message area */}
<div className="grow overflow-hidden">
<div className="max-w-4xl mx-auto w-full h-full">
<ChatMessages
messages={messages}
messagesEndRef={messagesEndRef}
sessionId={sessionId}
onFeedbackSubmit={handleFeedbackSubmit}
/>
</div>
</div>
{/* Fixed input area at bottom */}
<div className="flex-none">
<div className="max-w-4xl mx-auto w-full">
<ChatInput
input={input}
setInput={setInput}
handleSubmit={handleSubmit}
isLoading={isLoading}
/>
</div>
</div>
</>
)}
</div>
)
}