| 1 | import * as z from "zod"; |
| 2 | |
| 3 | export const ClientCertificateOptionsSchema = z.object({ |
| 4 | cert: z.string(), |
| 5 | key: z.string(), |
| 6 | passphrase: z.string().optional(), |
| 7 | }); |
| 8 | |
| 9 | export const RequestOptionsSchema = z.object({ |
| 10 | timeout: z.number().optional(), |
| 11 | verifySsl: z.boolean().optional(), |
| 12 | caBundlePath: z.union([z.string(), z.array(z.string())]).optional(), |
| 13 | proxy: z.string().optional(), |
| 14 | headers: z.record(z.string()).optional(), |
| 15 | extraBodyProperties: z.record(z.unknown()).optional(), |
| 16 | noProxy: z.array(z.string()).optional(), |
| 17 | clientCertificate: z.lazy(() => ClientCertificateOptionsSchema).optional(), |
| 18 | }); |
| 19 | |
| 20 | // Base config objects |
| 21 | export const BaseConfig = z.object({ |
| 22 | provider: z.string(), |
| 23 | requestOptions: RequestOptionsSchema.optional(), |
| 24 | }); |
| 25 | |
| 26 | export const BasePlusConfig = BaseConfig.extend({ |
| 27 | apiBase: z.string().optional(), |
| 28 | apiKey: z.string().optional(), |
| 29 | }); |
| 30 | |
| 31 | // OpenAI and compatible |
| 32 | export const OpenAIConfigSchema = BasePlusConfig.extend({ |
| 33 | provider: z.union([ |
| 34 | z.literal("openai"), |
| 35 | z.literal("mistral"), |
| 36 | z.literal("voyage"), |
| 37 | z.literal("deepinfra"), |
| 38 | z.literal("groq"), |
| 39 | z.literal("nvidia"), |
| 40 | z.literal("ovhcloud"), |
| 41 | z.literal("fireworks"), |
| 42 | z.literal("together"), |
| 43 | z.literal("novita"), |
| 44 | z.literal("nebius"), |
| 45 | z.literal("function-network"), |
| 46 | z.literal("llama.cpp"), |
| 47 | z.literal("llamafile"), |
| 48 | z.literal("lmstudio"), |
| 49 | z.literal("cerebras"), |
| 50 | z.literal("kindo"), |
| 51 | z.literal("msty"), |
| 52 | z.literal("openrouter"), |
| 53 | z.literal("sambanova"), |
| 54 | z.literal("text-gen-webui"), |
| 55 | z.literal("vllm"), |
| 56 | z.literal("x-ai"), |
| 57 | z.literal("scaleway"), |
| 58 | z.literal("ncompass"), |
| 59 | z.literal("relace"), |
| 60 | ]), |
| 61 | }); |
| 62 | export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>; |
| 63 | |
| 64 | export const MoonshotConfigSchema = OpenAIConfigSchema.extend({ |
| 65 | provider: z.literal("moonshot"), |
| 66 | }); |
| 67 | export type MoonshotConfig = z.infer<typeof MoonshotConfigSchema>; |
| 68 | |
| 69 | export const DeepseekConfigSchema = OpenAIConfigSchema.extend({ |
| 70 | provider: z.literal("deepseek"), |
| 71 | }); |
| 72 | export type DeepseekConfig = z.infer<typeof DeepseekConfigSchema>; |
| 73 | |
| 74 | export const LlamastackConfigSchema = OpenAIConfigSchema.extend({ |
| 75 | provider: z.literal("llamastack"), |
| 76 | }); |
| 77 | export type LlamastackConfig = z.infer<typeof LlamastackConfigSchema>; |
| 78 | |
| 79 | export const MockConfigSchema = BasePlusConfig.extend({ |
| 80 | provider: z.literal("mock"), |
| 81 | }); |
| 82 | |
| 83 | export type MockConfig = z.infer<typeof MockConfigSchema>; |
| 84 | |
| 85 | // Other APIs |
| 86 | export const CohereConfigSchema = OpenAIConfigSchema.extend({ |
| 87 | provider: z.literal("cohere"), |
| 88 | }); |
| 89 | export type CohereConfig = z.infer<typeof CohereConfigSchema>; |
| 90 | |
| 91 | export const AzureConfigSchema = OpenAIConfigSchema.extend({ |
| 92 | provider: z.literal("azure"), |
| 93 | env: z |
| 94 | .object({ |
| 95 | apiVersion: z.string().optional(), |
| 96 | apiType: z |
| 97 | .union([ |
| 98 | z.literal("azure-foundry"), |
| 99 | z.literal("azure-openai"), |
| 100 | z.literal("azure"), // Legacy |
| 101 | ]) |
| 102 | .optional(), |
| 103 | deployment: z.string().optional(), |
| 104 | }) |
| 105 | .optional(), |
| 106 | }); |
| 107 | export type AzureConfig = z.infer<typeof AzureConfigSchema>; |
| 108 | |
| 109 | export const GeminiConfigSchema = OpenAIConfigSchema.extend({ |
| 110 | provider: z.literal("gemini"), |
| 111 | apiKey: z.string(), |
| 112 | }); |
| 113 | export type GeminiConfig = z.infer<typeof GeminiConfigSchema>; |
| 114 | |
| 115 | export const AnthropicConfigSchema = OpenAIConfigSchema.extend({ |
| 116 | provider: z.literal("anthropic"), |
| 117 | apiKey: z.string(), |
| 118 | }); |
| 119 | export type AnthropicConfig = z.infer<typeof AnthropicConfigSchema>; |
| 120 | |
| 121 | export const WatsonXConfigSchema = BasePlusConfig.extend({ |
| 122 | provider: z.literal("watsonx"), |
| 123 | apiKey: z.string(), |
| 124 | env: z.object({ |
| 125 | apiVersion: z.string().optional(), |
| 126 | projectId: z.string().optional(), |
| 127 | deploymentId: z.string().optional(), |
| 128 | }), |
| 129 | }); |
| 130 | export type WatsonXConfig = z.infer<typeof WatsonXConfigSchema>; |
| 131 | |
| 132 | export const JinaConfigSchema = OpenAIConfigSchema.extend({ |
| 133 | provider: z.literal("jina"), |
| 134 | }); |
| 135 | export type JinaConfig = z.infer<typeof JinaConfigSchema>; |
| 136 | |
| 137 | export const InceptionConfigSchema = OpenAIConfigSchema.extend({ |
| 138 | provider: z.literal("inception"), |
| 139 | }); |
| 140 | export type InceptionConfig = z.infer<typeof InceptionConfigSchema>; |
| 141 | |
| 142 | // Discriminated union |
| 143 | export const LLMConfigSchema = z.discriminatedUnion("provider", [ |
| 144 | OpenAIConfigSchema, |
| 145 | MoonshotConfigSchema, |
| 146 | DeepseekConfigSchema, |
| 147 | CohereConfigSchema, |
| 148 | AzureConfigSchema, |
| 149 | GeminiConfigSchema, |
| 150 | AnthropicConfigSchema, |
| 151 | WatsonXConfigSchema, |
| 152 | JinaConfigSchema, |
| 153 | MockConfigSchema, |
| 154 | InceptionConfigSchema, |
| 155 | LlamastackConfigSchema, |
| 156 | ]); |
| 157 | export type LLMConfig = z.infer<typeof LLMConfigSchema>; |