'use client' import { FormEvent, useMemo, useState } from 'react' import { usePathname } from 'next/navigation' import { getExplorerApiBase } from '@/services/api/blockscout' interface AgentMessage { role: 'assistant' | 'user' content: string } const QUICK_PROMPTS = [ 'Explain this page', 'Summarize the chain status', 'Help me inspect a contract', 'Find likely navigation issues', ] as const export default function ExplorerAgentTool() { const pathname = usePathname() ?? '/' const [open, setOpen] = useState(false) const [input, setInput] = useState('') const [submitting, setSubmitting] = useState(false) const [messages, setMessages] = useState([ { role: 'assistant', content: 'DBIS Explorer AI Assist is ready. I can explain this page, summarize what you are looking at, and help investigate transactions, contracts, routes, and system surfaces.', }, ]) const pageContext = useMemo( () => ({ path: pathname, view: 'explorer', }), [pathname], ) const sendMessage = async (content: string) => { const trimmed = content.trim() if (!trimmed || submitting) return const nextMessages: AgentMessage[] = [...messages, { role: 'user', content: trimmed }] setMessages(nextMessages) setInput('') setSubmitting(true) try { const response = await fetch(`${getExplorerApiBase()}/api/v1/ai/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ messages: nextMessages, pageContext, }), }) const payload = await response.json().catch(() => null) if (!response.ok) { throw new Error(payload?.error?.message || `HTTP ${response.status}`) } const reply = payload?.message?.content || payload?.reply || 'The agent did not return a readable reply.' setMessages((current) => [...current, { role: 'assistant', content: String(reply) }]) } catch (error) { setMessages((current) => [ ...current, { role: 'assistant', content: error instanceof Error ? `Agent tool is temporarily unavailable: ${error.message}` : 'Agent tool is temporarily unavailable.', }, ]) } finally { setSubmitting(false) } } const handleSubmit = async (event: FormEvent) => { event.preventDefault() await sendMessage(input) } return (
{open ? (

DBIS Explorer AI Assist

Page-aware guidance for the explorer. Helpful, read-only, and designed for quick investigation.

{QUICK_PROMPTS.map((prompt) => ( ))}
{messages.map((message, index) => (
{message.content}
))}