fix: 添加 API 基础路径配置和环境变量支持
This commit is contained in:
31
src/app/api/test-integration/route.ts
Normal file
31
src/app/api/test-integration/route.ts
Normal file
@@ -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 });
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -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',
|
||||
|
||||
66
src/app/test-api/page.tsx
Normal file
66
src/app/test-api/page.tsx
Normal file
@@ -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 (
|
||||
<div className="min-h-screen p-8">
|
||||
<h1 className="mb-4 text-2xl font-bold">API 测试页面</h1>
|
||||
<div className="space-x-4">
|
||||
<Button onClick={testChatAPI} disabled={loading}>
|
||||
测试 Chat API
|
||||
</Button>
|
||||
<Button onClick={testAnalyzeAPI} disabled={loading}>
|
||||
测试 Analyze API
|
||||
</Button>
|
||||
</div>
|
||||
{result && (
|
||||
<div className="mt-8 rounded-lg border p-4">
|
||||
<pre className="whitespace-pre-wrap text-sm">{result}</pre>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
1
src/lib/api.ts
Normal file
1
src/lib/api.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || '';
|
||||
Reference in New Issue
Block a user