/* ┌──────────────────────────────────────────────────────────────────────────────┐ │ @author: Davidson Gomes │ │ @file: /app/documentation/components/FrontendImplementationSection.tsx │ │ Developed by: Davidson Gomes │ │ Creation date: May 13, 2025 │ │ Contact: contato@evolution-api.com │ ├──────────────────────────────────────────────────────────────────────────────┤ │ @copyright © Evolution API 2025. All rights reserved. │ │ Licensed under the Apache License, Version 2.0 │ │ │ │ You may not use this file except in compliance with the License. │ │ You may obtain a copy of the License at │ │ │ │ http://www.apache.org/licenses/LICENSE-2.0 │ │ │ │ Unless required by applicable law or agreed to in writing, software │ │ distributed under the License is distributed on an "AS IS" BASIS, │ │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ │ See the License for the specific language governing permissions and │ │ limitations under the License. │ ├──────────────────────────────────────────────────────────────────────────────┤ │ @important │ │ For any future changes to the code in this file, it is recommended to │ │ include, together with the modification, the information of the developer │ │ who changed it and the date of modification. │ └──────────────────────────────────────────────────────────────────────────────┘ */ import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { ClipboardCopy } from "lucide-react"; import { CodeBlock } from "@/app/documentation/components/CodeBlock"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; interface FrontendImplementationSectionProps { copyToClipboard: (text: string) => void; } export function FrontendImplementationSection({ copyToClipboard }: FrontendImplementationSectionProps) { return ( Frontend implementation Practical examples for implementation in React applications Standard HTTP Streaming SSE React component

Implementation of message/send

Example of standard implementation in JavaScript/React:

part.type === 'text') .map(part => part.text) .join(''); // Here you would update your React state // setResponse(responseText); return responseText; } return task; } catch (error) { console.error('Error sending task:', error); return null; } finally { // Finalize loading state setIsLoading(false); } }`} language="javascript" />

Implementation of message/stream (Streaming)

Example of implementation of streaming with Server-Sent Events (SSE):

{ try { // Process data from the event const data = JSON.parse(event.data); // Process the event if (data.result) { // Process status if available if (data.result.status) { const status = data.result.status; // Extract text if available let currentText = ''; if (status.message && status.message.parts) { const parts = status.message.parts.filter(part => part.type === 'text'); if (parts.length > 0) { currentText = parts.map(part => part.text).join(''); } } // Call callback with updates onUpdateCallback({ text: currentText, state: status.state, complete: data.result.final === true }); // If it's the final event, close the connection if (data.result.final === true) { eventSource.close(); onUpdateCallback({ complete: true, state: status.state }); } } // Process artifact if available if (data.result.artifact) { const artifact = data.result.artifact; if (artifact.parts && artifact.parts.length > 0) { const parts = artifact.parts.filter(part => part.type === 'text'); if (parts.length > 0) { const artifactText = parts.map(part => part.text).join(''); // Call callback with artifact onUpdateCallback({ text: artifactText, state: 'artifact', complete: artifact.lastChunk === true }); // If it's the last chunk, close the connection if (artifact.lastChunk === true) { eventSource.close(); } } } } } } catch (error) { console.error('Error processing event:', error); onUpdateCallback({ error: error.message }); } }; // Handler for errors eventSource.onerror = (error) => { console.error('Error in EventSource:', error); eventSource.close(); onUpdateCallback({ error: 'Connection with server interrupted', complete: true, state: 'error' }); }; } } catch (error) { console.error('Error in streaming:', error); // Notify error through callback onUpdateCallback({ error: error.message, complete: true, state: 'error' }); return null; } }`} language="javascript" />

React component with streaming support:

{ if (update.error) { // Handle error setStatus('error'); return; } // Update text setResponse(update.text); // Update status setStatus(update.state); // If it's complete, finish streaming if (update.complete) { setIsStreaming(false); } }; const handleSubmit = async (e) => { e.preventDefault(); if (!message.trim()) return; // Clear previous response setResponse(''); setStatus('submitted'); // Start streaming setIsStreaming(true); try { // Start stream with the agent await initAgentStream(agentId, message, handleStreamUpdate); // Clear message field after sending setMessage(''); } catch (error) { console.error('Error:', error); setStatus('error'); setIsStreaming(false); } }; // Render status indicator based on status const renderStatusIndicator = () => { switch (status) { case 'submitted': return Sent; case 'working': return Processing; case 'completed': return Completed; case 'error': return Error; default: return null; } }; return (
{response && (
A2A Agent
{renderStatusIndicator()}
{response} {status === 'working' && !response && (
)}
)}
setMessage(e.target.value)} placeholder="Type your message..." disabled={isStreaming} className="chat-input" />
); }`} language="javascript" />
); }`)} >
); }