'use client'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport, getToolName, isToolUIPart } from 'ai'; import { tools } from '../api/use-chat-human-in-the-loop/tools'; import { APPROVAL, getToolsRequiringConfirmation, } from '../api/use-chat-human-in-the-loop/utils'; import { useState } from 'react'; import { HumanInTheLoopUIMessage, MyTools, } from '../api/use-chat-human-in-the-loop/types'; export default function Chat() { const [input, setInput] = useState(''); const { messages, sendMessage, addToolResult } = useChat({ transport: new DefaultChatTransport({ api: '/api/use-chat-human-in-the-loop', }), }); const toolsRequiringConfirmation = getToolsRequiringConfirmation(tools); const pendingToolCallConfirmation = messages.some(m => m.parts?.some( part => isToolUIPart(part) && part.state === 'input-available' && toolsRequiringConfirmation.includes(getToolName(part)), ), ); return (
{messages?.map(m => (
{`${m.role}: `} {m.parts?.map((part, i) => { if (part.type === 'text') { return
{part.text}
; } if (isToolUIPart(part)) { const toolInvocation = part; const toolName = getToolName(toolInvocation); const toolCallId = toolInvocation.toolCallId; const dynamicInfoStyles = 'font-mono bg-zinc-100 p-1 text-sm'; // render confirmation tool (client-side tool with user interaction) if ( toolsRequiringConfirmation.includes(toolName) && toolInvocation.state === 'input-available' ) { return (
Run {toolName}{' '} with args:
{JSON.stringify(toolInvocation.input, null, 2)}
); } return (
call {toolInvocation.state === 'output-available' ? 'ed' : 'ing'}{' '} {toolName} {part.output && (
{JSON.stringify(part.output, null, 2)}
)}
); } })}
))}
{ e.preventDefault(); sendMessage({ text: input }); setInput(''); }} > setInput(e.target.value)} />
); }