import { useTools } from '@frontmcp/react/ai';
import OpenAI from 'openai';
function OpenAIChat() {
const { tools, processToolCalls } = useTools('openai');
// ⚠️ dangerouslyAllowBrowser exposes your API key in client-side code.
// In production, proxy requests through your backend instead.
const openai = new OpenAI({ apiKey: '...', dangerouslyAllowBrowser: true });
async function chat(message: string) {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: message }],
tools: tools ?? undefined,
});
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
const results = await processToolCalls(toolCalls);
// Continue the conversation with tool results...
}
}
}