import { z } from "zod"; export const llmProviderTypeSchema = z.enum([ "openai-compatible", "anthropic", "bailian", ]); /** * Loose URL validation: we accept any non-empty string that parses as an * absolute URL. Trailing slashes are normalised by the server before use so * both `/v1` and `/v1/` work. */ const baseUrlSchema = z .string() .trim() .min(1, "请输入 Base URL") .refine((v) => { try { const url = new URL(v); return url.protocol === "http:" || url.protocol === "https:"; } catch { return false; } }, "Base URL 格式无效,需为 http(s) 完整地址"); export const createLLMProviderSchema = z.object({ name: z.string().trim().min(1, "请输入名称").max(64), type: llmProviderTypeSchema, baseUrl: baseUrlSchema, apiKey: z.string().trim().min(1, "请输入 API Key").max(512), defaultModel: z.string().trim().max(128).optional().or(z.literal("")), }); /** * Update schema: `apiKey` empty means "keep the existing encrypted value"; * all other fields are optional patches. */ export const updateLLMProviderSchema = z.object({ name: z.string().trim().min(1).max(64).optional(), type: llmProviderTypeSchema.optional(), baseUrl: baseUrlSchema.optional(), apiKey: z.string().trim().max(512).optional(), defaultModel: z.string().trim().max(128).optional().or(z.literal("")), }); export type CreateLLMProviderInput = z.infer; export type UpdateLLMProviderInput = z.infer;