From da218b72b4c4ddc4f9de4beea10784419532f911 Mon Sep 17 00:00:00 2001 From: jaystar <1783853055750436-jaystar@noreply.coze.cn> Date: Thu, 22 Jan 2026 10:09:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=20API=20=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E8=B7=AF=E5=BE=84=E9=85=8D=E7=BD=AE=E5=92=8C=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/test-integration/route.ts | 31 +++++++++++++ src/app/chat/page.tsx | 3 +- src/app/recognition/page.tsx | 3 +- src/app/test-api/page.tsx | 66 +++++++++++++++++++++++++++ src/lib/api.ts | 1 + 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/app/api/test-integration/route.ts create mode 100644 src/app/test-api/page.tsx create mode 100644 src/lib/api.ts 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 ( +
{result}
+