39 lines
No EOL
1.1 KiB
TypeScript
39 lines
No EOL
1.1 KiB
TypeScript
'use server'
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
|
|
const OPENCODE_BASE_URL = 'https://opencode.ai/zen/v1'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { apiKey, model, messages, max_tokens } = body
|
|
|
|
if (!apiKey) {
|
|
return NextResponse.json({ error: 'API key required' }, { status: 400 })
|
|
}
|
|
|
|
const response = await fetch(`${OPENCODE_BASE_URL}/chat/completions`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${apiKey}`,
|
|
},
|
|
body: JSON.stringify({
|
|
model,
|
|
messages,
|
|
max_tokens: max_tokens || 500,
|
|
}),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const error = await response.text()
|
|
return NextResponse.json({ error: `OpenCode API error: ${response.status} - ${error}` }, { status: response.status })
|
|
}
|
|
|
|
const data = await response.json()
|
|
return NextResponse.json(data)
|
|
} catch (error: any) {
|
|
return NextResponse.json({ error: error.message || 'Internal server error' }, { status: 500 })
|
|
}
|
|
} |