diff --git a/src/app/api/test-integration/route.ts b/src/app/api/test-integration/route.ts new file mode 100644 index 0000000..35146cb --- /dev/null +++ b/src/app/api/test-integration/route.ts @@ -0,0 +1,31 @@ +import { NextResponse } from 'next/server'; +import { LLMClient, Config } from 'coze-coding-dev-sdk'; + +export const runtime = 'nodejs'; + +export async function GET() { + try { + const config = new Config(); + const client = new LLMClient(config); + + const response = await client.invoke([ + { + role: 'user', + content: 'Hello', + }, + ], { + temperature: 0.7, + }); + + return NextResponse.json({ + success: true, + response: response.content, + }); + } catch (error) { + console.error('Integration test error:', error); + return NextResponse.json({ + success: false, + error: error instanceof Error ? error.message : String(error), + }, { status: 500 }); + } +} diff --git a/src/app/chat/page.tsx b/src/app/chat/page.tsx index 4c3c016..183dda0 100644 --- a/src/app/chat/page.tsx +++ b/src/app/chat/page.tsx @@ -5,6 +5,7 @@ import { Send, MessageSquare, ArrowLeft } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import Link from 'next/link'; +import { API_BASE_URL } from '@/lib/api'; interface Message { role: 'user' | 'assistant'; @@ -40,7 +41,7 @@ export default function ChatPage() { setIsLoading(true); try { - const response = await fetch('/api/chat', { + const response = await fetch(`${API_BASE_URL}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/app/recognition/page.tsx b/src/app/recognition/page.tsx index e3baacb..a40c6f9 100644 --- a/src/app/recognition/page.tsx +++ b/src/app/recognition/page.tsx @@ -5,6 +5,7 @@ import { Camera, Upload, X, ArrowLeft, Loader2, Info } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import Link from 'next/link'; +import { API_BASE_URL } from '@/lib/api'; interface NutritionInfo { foodName: string; @@ -69,7 +70,7 @@ export default function RecognitionPage() { setError(null); try { - const response = await fetch('/api/analyze-food', { + const response = await fetch(`${API_BASE_URL}/api/analyze-food`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/src/app/test-api/page.tsx b/src/app/test-api/page.tsx new file mode 100644 index 0000000..3263f2d --- /dev/null +++ b/src/app/test-api/page.tsx @@ -0,0 +1,66 @@ +'use client'; + +import { useState } from 'react'; +import { Button } from '@/components/ui/button'; + +export default function TestAPIPage() { + const [result, setResult] = useState(''); + const [loading, setLoading] = useState(false); + + const testChatAPI = async () => { + setLoading(true); + setResult(''); + try { + const response = await fetch('/api/chat', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ message: '测试一下', history: [] }), + }); + + const text = await response.text(); + setResult(`Chat API Status: ${response.status}\nResponse:\n${text}`); + } catch (error) { + setResult(`Error: ${error}`); + } finally { + setLoading(false); + } + }; + + const testAnalyzeAPI = async () => { + setLoading(true); + setResult(''); + try { + const response = await fetch('/api/analyze-food', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ image: '' }), + }); + + const text = await response.text(); + setResult(`Analyze API Status: ${response.status}\nResponse:\n${text}`); + } catch (error) { + setResult(`Error: ${error}`); + } finally { + setLoading(false); + } + }; + + return ( +
+

API 测试页面

+
+ + +
+ {result && ( +
+
{result}
+
+ )} +
+ ); +} diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 0000000..46c2b85 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1 @@ +export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || '';