/** * AI SDK UI - Chat with Tool Calling * * Demonstrates: * - Displaying tool calls in UI * - Rendering tool arguments and results * - Handling multi-step tool invocations * - Visual distinction between messages and tool calls * * Requires: * - API route with tools configured (see ai-sdk-core skill) * - Backend using `tool()` helper * * Usage: * 1. Set up API route with tools * 2. Copy this component * 3. Customize tool rendering as needed */ 'use client'; import { useChat } from 'ai/react'; import { useState, FormEvent } from 'react'; export default function ChatWithTools() { const { messages, sendMessage, isLoading, error } = useChat({ api: '/api/chat', }); const [input, setInput] = useState(''); const handleSubmit = (e: FormEvent) => { e.preventDefault(); if (!input.trim()) return; sendMessage({ content: input }); setInput(''); }; return (
Ask about weather, calculations, or search queries
{JSON.stringify(tool.args, null, 2)}
{JSON.stringify(tool.args, null, 2)}
Result:
{JSON.stringify(tool.result, null, 2)}