mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
MCP server checks the gate at startup and exits before opening stdio if the data directory requires a newer runtime. Desktop HTTP API maps DataDirCompatibilityError (including wrapped causes) to a 409 response so the renderer can surface a clear upgrade prompt. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
/**
|
|
* ChatLab API — Fastify server instance
|
|
*/
|
|
|
|
import Fastify, { type FastifyInstance, type FastifyError } from 'fastify'
|
|
import { authHook } from './auth'
|
|
import { ApiError, ApiErrorCode, apiErrorFromUnknown, errorResponse, serverError } from './errors'
|
|
import { apiLogger } from './logger'
|
|
|
|
const JSON_BODY_LIMIT = 50 * 1024 * 1024 // 50MB
|
|
|
|
export function createServer(): FastifyInstance {
|
|
const server = Fastify({
|
|
logger: false,
|
|
bodyLimit: JSON_BODY_LIMIT,
|
|
})
|
|
|
|
server.addHook('onRequest', authHook)
|
|
|
|
server.setErrorHandler((error: FastifyError, _request, reply) => {
|
|
if (error instanceof ApiError) {
|
|
reply.code(error.statusCode).send(errorResponse(error))
|
|
return
|
|
}
|
|
|
|
const apiError = apiErrorFromUnknown(error)
|
|
if (apiError) {
|
|
reply.code(apiError.statusCode).send(errorResponse(apiError))
|
|
return
|
|
}
|
|
|
|
if (error.statusCode === 413) {
|
|
const bodyErr = new ApiError(ApiErrorCode.BODY_TOO_LARGE, 'Request body exceeds 50MB limit')
|
|
reply.code(413).send(errorResponse(bodyErr))
|
|
return
|
|
}
|
|
|
|
apiLogger.error('Unhandled error', error)
|
|
const err = serverError(error.message)
|
|
reply.code(err.statusCode).send(errorResponse(err))
|
|
})
|
|
|
|
return server
|
|
}
|