feat: add dev fallback for chat API (mock streaming response)
Build & Push Portfolio Image / build-push (push) Successful in 2m48s
Build & Push Portfolio Image / build-push (push) Successful in 2m48s
This commit is contained in:
+40
-1
@@ -35,8 +35,9 @@ Pre-screen context: Technical depth for platform engineer role. Cert rotation, q
|
|||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
const token = process.env.LLM_API_TOKEN
|
const token = process.env.LLM_API_TOKEN
|
||||||
|
const isDev = process.env.NODE_ENV === 'development'
|
||||||
|
|
||||||
if (!token) {
|
if (!token && !isDev) {
|
||||||
return new Response(
|
return new Response(
|
||||||
JSON.stringify({ error: 'LLM_API_TOKEN not configured' }),
|
JSON.stringify({ error: 'LLM_API_TOKEN not configured' }),
|
||||||
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
{ status: 500, headers: { 'Content-Type': 'application/json' } }
|
||||||
@@ -46,6 +47,44 @@ export async function POST(request: NextRequest) {
|
|||||||
try {
|
try {
|
||||||
const { message } = await request.json()
|
const { message } = await request.json()
|
||||||
|
|
||||||
|
// Dev fallback: mock streaming response
|
||||||
|
if (isDev && !token) {
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const mockReasoning = `Analyzing "${message}"...`
|
||||||
|
const mockContent = `Rock has 6+ years experience in Infrastructure, Backend, and LLM Systems. His expertise spans Kubernetes, Terraform, vLLM optimization (60% latency cut), AWS Distributed-Map across 57+ regions, and production homelab with 99.2% uptime.`
|
||||||
|
|
||||||
|
const stream = new ReadableStream({
|
||||||
|
async start(controller) {
|
||||||
|
let reasoning = ''
|
||||||
|
let content = ''
|
||||||
|
|
||||||
|
// Stream reasoning
|
||||||
|
for (const char of mockReasoning) {
|
||||||
|
reasoning += char
|
||||||
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: 'reasoning', text: reasoning })}\n\n`))
|
||||||
|
await new Promise(r => setTimeout(r, 5))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stream content
|
||||||
|
for (const char of mockContent) {
|
||||||
|
content += char
|
||||||
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: 'content', text: content })}\n\n`))
|
||||||
|
await new Promise(r => setTimeout(r, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify({ type: 'done' })}\n\n`))
|
||||||
|
controller.close()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return new Response(stream, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'text/event-stream',
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
'Connection': 'keep-alive',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const response = await fetch(LLM_API_URL, {
|
const response = await fetch(LLM_API_URL, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
Reference in New Issue
Block a user