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
73 changes: 73 additions & 0 deletions samples/conversational-agent-app/src/components/ChatArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

import { useState, useRef, useEffect, useCallback } from 'react'
import { useConversationalAgent } from '../context/ConversationalAgentContext'
import type { AvailableConnectionsResponse } from '@uipath/uipath-typescript/conversational-agent'
import { MessageBubble } from './MessageBubble'
import { WelcomeScreen } from './WelcomeScreen'
import { ChatInput } from './ChatInput'
import { ConnectionReadinessCard, type ConnectorReadiness } from './ConnectionReadinessCard'
import { Spinner } from './Spinner'

export function ChatArea() {
const {
conversationalAgent,
messages,
currentConversation,
selectedAgent,
Expand All @@ -23,10 +26,67 @@ export function ChatArea() {
resolveInterrupt,
} = useConversationalAgent()

const [connectionReadiness, setConnectionReadiness] = useState<ConnectorReadiness[] | null>(null)

const [pendingMessage, setPendingMessage] = useState<string | null>(null)
const [isLoadingMore, setIsLoadingMore] = useState(false)
const messagesEndRef = useRef<HTMLDivElement>(null)

// Fetch connection readiness when agent changes
useEffect(() => {
if (!conversationalAgent || !selectedAgent) {
setConnectionReadiness(null)
return
}
let cancelled = false

const mapItems = (items: AvailableConnectionsResponse) => {
if (cancelled) return
if (items.length === 0) { setConnectionReadiness(null); return }
const readiness: ConnectorReadiness[] = items.map(item => ({
connectorKey: item.connectorKey,
connectorName: item.connectorName ?? item.connectorKey,
connectorImage: item.connectorImage,
isConfigurable: item.isConfigurable !== false,
currentConnectionId: item.currentConnectionId,
currentConnectionName: item.currentConnectionName,
currentConnectionState: (item.connections?.find(c => c.id === item.currentConnectionId)?.state as ConnectorReadiness['currentConnectionState'])
?? (item.currentConnectionId ? 'Expired' : undefined),
connectionsUrl: item.connectionsUrl,
}))
const hasUnresolved = readiness.some(
c => c.isConfigurable && (!c.currentConnectionId || c.currentConnectionState !== 'Enabled'),
)
setConnectionReadiness(hasUnresolved ? readiness : null)
}

let timerRef: ReturnType<typeof setTimeout> | undefined

// Initial fetch, then sequential re-fetch after delay to pick up auto-bind results
const fetchReadiness = async () => {
try {
const items = await conversationalAgent.getAvailableConnections(selectedAgent.id, selectedAgent.folderId)
mapItems(items)
} catch (error) {
console.warn('Failed to fetch connection readiness:', error)
if (!cancelled) setConnectionReadiness(null)
return
}
if (cancelled) return
await new Promise<void>((resolve) => { timerRef = setTimeout(resolve, 1500) })
if (cancelled) return
try {
const items = await conversationalAgent.getAvailableConnections(selectedAgent.id, selectedAgent.folderId)
mapItems(items)
} catch (error) {
console.warn('Failed to refresh connection readiness:', error)
}
}
fetchReadiness()

return () => { cancelled = true; if (timerRef) clearTimeout(timerRef) }
}, [conversationalAgent, selectedAgent])

// Auto-scroll to bottom when new messages arrive
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' })
Expand All @@ -47,6 +107,7 @@ export function ChatArea() {
}, [selectedAgent, createConversation])

const handleSubmit = useCallback(async (content: string, attachments: File[]) => {
setConnectionReadiness(null)
await sendMessage(content, attachments)
}, [sendMessage])

Expand Down Expand Up @@ -90,6 +151,18 @@ export function ChatArea() {
</div>
</div>

{/* Connection readiness card */}
{connectionReadiness && conversationalAgent && selectedAgent && (
<ConnectionReadinessCard
connectors={connectionReadiness}
conversationalAgent={conversationalAgent}
agentId={selectedAgent.id}
folderId={selectedAgent.folderId}
onAllConnected={() => setConnectionReadiness(null)}
defaultCollapsed={messages.length > 0}
/>
)}

{/* Messages */}
<div className="flex-1 overflow-y-auto">
{messages.length === 0 ? (
Expand Down
Loading
Loading