Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { CHAT_REQUEST_FAILED, createChatResponse, HttpStatus } from '../server/index.js'
import { createChatResponse, HttpStatus, INVALID_REQUEST_JSON } from '../server/index.js'

export async function POST(request: Request) {
let body: unknown
try {
const body = await request.json()
return await createChatResponse(body)
} catch (error) {
const message = error instanceof Error ? error.message : CHAT_REQUEST_FAILED
return Response.json({ error: message }, { status: HttpStatus.InternalServerError })
body = await request.json()
} catch {
return Response.json({ error: INVALID_REQUEST_JSON }, { status: HttpStatus.BadRequest })
}

return createChatResponse(body)
}
1 change: 1 addition & 0 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { chatRequestSchema } from './chatRequest.js'
export {
CHAT_REQUEST_FAILED,
INVALID_CHAT_REQUEST,
INVALID_REQUEST_JSON,
OPENAI_API_KEY_MISSING,
} from './messages.js'
export { HttpStatus, type HttpStatusCode } from './httpStatus.js'
2 changes: 2 additions & 0 deletions server/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const OPENAI_API_KEY_MISSING =
'OpenAI API key is not configured. For local development, add OPENAI_API_KEY to a .env file in the project root and restart the dev server. On Vercel, add OPENAI_API_KEY under Project Settings → Environment Variables.'

export const INVALID_REQUEST_JSON = 'Request body must be valid JSON.'

export const INVALID_CHAT_REQUEST = 'Invalid chat request body.'

export const CHAT_REQUEST_FAILED = 'Chat request failed.'
Expand Down
13 changes: 12 additions & 1 deletion src/lib/getChatErrorMessage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { CHAT_CLIENT_ERROR } from '../../server/messages'

function parseApiError(text: string): string | null {
if (!text.trim().startsWith('{')) return null

try {
const error = (JSON.parse(text) as { error?: string }).error
return error?.trim() ?? null
} catch {
return null
}
}

export function getChatErrorMessage(
error: { message: string } | undefined,
status: string,
): string | null {
if (error?.message) {
return error.message
return parseApiError(error.message) ?? error.message
}

if (status === 'error') {
Expand Down
11 changes: 9 additions & 2 deletions vite/apiDevMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IncomingMessage, ServerResponse } from 'node:http'
import { CHAT_REQUEST_FAILED, createChatResponse, HttpStatus } from '../server'
import { CHAT_REQUEST_FAILED, createChatResponse, HttpStatus, INVALID_REQUEST_JSON } from '../server'

type ConnectNext = (error?: unknown) => void

Expand Down Expand Up @@ -43,7 +43,14 @@ async function sendWebResponse(webResponse: Response, res: ServerResponse): Prom
}

async function handleChatPost(req: IncomingMessage, res: ServerResponse): Promise<void> {
const body = await readJsonBody(req)
let body: unknown
try {
body = await readJsonBody(req)
} catch {
sendJson(res, HttpStatus.BadRequest, { error: INVALID_REQUEST_JSON })
return
}

const webResponse = await createChatResponse(body)
await sendWebResponse(webResponse, res)
}
Expand Down
Loading