import { eq } from 'drizzle-orm' import { z } from 'zod' const schema = z.object({ type: z.enum(['openai-compatible', 'anthropic', 'google']), baseUrl: z.string().default(''), apiKey: z.string().default(''), /** 编辑已有连接且未重新填写 Key 时,使用已保存的 Key */ providerId: z.string().optional() }) /** 用(可能尚未保存的)连接配置拉取模型列表 */ export default defineEventHandler(async (event) => { const body = await readJson(event, schema) let apiKey = body.apiKey.trim() if (!apiKey && body.providerId) { const p = useDb().select().from(tables.providers).where(eq(tables.providers.id, body.providerId)).get() if (p) apiKey = decryptSecret(p.apiKeyEnc) } if (!apiKey) throw createError({ statusCode: 400, message: '请先填写 API Key' }) try { return await listRemoteModels(body.type, body.baseUrl, apiKey) } catch (err) { throw createError({ statusCode: 502, message: `拉取模型列表失败:${describeError(err)}` }) } })