|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { OpenAIWebSearchMessage } from '@/agent/openai-web-search-agent'; |
| 4 | +import { Response } from '@/components/ai-elements/response'; |
| 5 | +import ChatInput from '@/components/chat-input'; |
| 6 | +import { ReasoningView } from '@/components/reasoning-view'; |
| 7 | +import SourcesView from '@/components/sources-view'; |
| 8 | +import OpenAIWebSearchView from '@/components/tool/openai-web-search-view'; |
| 9 | +import { useChat } from '@ai-sdk/react'; |
| 10 | +import { DefaultChatTransport } from 'ai'; |
| 11 | + |
| 12 | +export default function TestOpenAIWebSearch() { |
| 13 | + const { error, status, sendMessage, messages, regenerate } = |
| 14 | + useChat<OpenAIWebSearchMessage>({ |
| 15 | + transport: new DefaultChatTransport({ |
| 16 | + api: '/api/chat-openai-web-search', |
| 17 | + }), |
| 18 | + }); |
| 19 | + |
| 20 | + return ( |
| 21 | + <div className="flex flex-col py-24 mx-auto w-full max-w-md stretch"> |
| 22 | + <h1 className="mb-4 text-xl font-bold">OpenAI Web Search Test</h1> |
| 23 | + |
| 24 | + {messages.map(message => ( |
| 25 | + <div key={message.id} className="whitespace-pre-wrap"> |
| 26 | + {message.role === 'user' ? 'User: ' : 'AI: '} |
| 27 | + {message.parts.map((part, index) => { |
| 28 | + switch (part.type) { |
| 29 | + case 'text': { |
| 30 | + return <Response key={index}>{part.text}</Response>; |
| 31 | + } |
| 32 | + case 'reasoning': { |
| 33 | + return <ReasoningView part={part} key={index} />; |
| 34 | + } |
| 35 | + case 'tool-web_search': { |
| 36 | + return <OpenAIWebSearchView invocation={part} key={index} />; |
| 37 | + } |
| 38 | + } |
| 39 | + })} |
| 40 | + |
| 41 | + <SourcesView |
| 42 | + sources={message.parts.filter(part => part.type === 'source-url')} |
| 43 | + /> |
| 44 | + </div> |
| 45 | + ))} |
| 46 | + |
| 47 | + {error && ( |
| 48 | + <div className="mt-4"> |
| 49 | + <div className="text-red-500">An error occurred.</div> |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + className="px-4 py-2 mt-4 text-blue-500 rounded-md border border-blue-500" |
| 53 | + onClick={() => regenerate()} |
| 54 | + > |
| 55 | + Retry |
| 56 | + </button> |
| 57 | + </div> |
| 58 | + )} |
| 59 | + |
| 60 | + <ChatInput status={status} onSubmit={text => sendMessage({ text })} /> |
| 61 | + </div> |
| 62 | + ); |
| 63 | +} |
0 commit comments