From 4dc4e4e5a79f0208d0023a804624e6fe96f52add Mon Sep 17 00:00:00 2001 From: gu yan <51285201+Slipstream-Max@users.noreply.github.com> Date: Thu, 12 Feb 2026 00:38:12 +0800 Subject: [PATCH] add model list endpoint (#96) * add model list endpoint * fix: guard models list endpoint errors --- src/server/modules/proxy/proxy.controller.ts | 51 +++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/src/server/modules/proxy/proxy.controller.ts b/src/server/modules/proxy/proxy.controller.ts index 0bcfc21..d490609 100644 --- a/src/server/modules/proxy/proxy.controller.ts +++ b/src/server/modules/proxy/proxy.controller.ts @@ -1,6 +1,7 @@ import { Controller, Post, + Get, Body, Res, HttpStatus, @@ -22,6 +23,12 @@ import { GeminiResponse, } from './interfaces/request-interfaces'; import { ProxyGuard } from './proxy.guard'; +import { + getAllDynamicModels, + MODEL_LIST_CREATED_AT, + MODEL_LIST_OWNER, +} from '../../../lib/antigravity/ModelMapping'; +import { getServerConfig } from '../../server-config'; @@ -30,7 +37,37 @@ import { ProxyGuard } from './proxy.guard'; export class ProxyController { private readonly logger = new Logger(ProxyController.name); - constructor(@Inject(ProxyService) private readonly proxyService: ProxyService) {} + constructor(@Inject(ProxyService) private readonly proxyService: ProxyService) { } + + @Get('models') + listModels(@Res() res: FastifyReply) { + try { + const config = getServerConfig(); + const customMapping = config?.custom_mapping ?? {}; + const modelIds = getAllDynamicModels(customMapping); + + const data = modelIds.map((id) => ({ + id, + object: 'model', + created: MODEL_LIST_CREATED_AT, + owned_by: MODEL_LIST_OWNER, + })); + + res.status(HttpStatus.OK).send({ + object: 'list', + data, + }); + } catch (error) { + const message = error instanceof Error ? error.message : 'Failed to list models'; + this.logger.error(message, error instanceof Error ? error.stack : undefined); + res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({ + error: { + message, + type: 'server_error', + }, + }); + } + } @Post('chat/completions') async chatCompletions(@Body() body: OpenAIChatRequest, @Res() res: FastifyReply) { @@ -186,12 +223,12 @@ export class ProxyController { content: imageParts.length > 0 ? [ - { - type: 'text', - text: body.prompt ?? 'Please edit this image based on the provided instruction.', - }, - ...imageParts, - ] + { + type: 'text', + text: body.prompt ?? 'Please edit this image based on the provided instruction.', + }, + ...imageParts, + ] : (body.prompt ?? 'Please edit this image based on the provided instruction.'), }, ],