| 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("ollama"), |
| 50 | z.literal("cerebras"), |
| 51 | z.literal("kindo"), |
| 52 | z.literal("msty"), |
| 53 | z.literal("openrouter"), |
| 54 | z.literal("sambanova"), |
| 55 | z.literal("text-gen-webui"), |
| 56 | z.literal("vllm"), |
| 57 | z.literal("xAI"), |
| 58 | z.literal("scaleway"), |
| 59 | z.literal("ncompass"), |
| 60 | z.literal("relace"), |
| 61 | z.literal("huggingface-inference-api"), |
| 62 | ]), |
| 63 | }); |
| 64 | export type OpenAIConfig = z.infer<typeof OpenAIConfigSchema>; |
| 65 | |
| 66 | export const MoonshotConfigSchema = OpenAIConfigSchema.extend({ |
| 67 | provider: z.literal("moonshot"), |
| 68 | }); |
| 69 | export type MoonshotConfig = z.infer<typeof MoonshotConfigSchema>; |
| 70 | |
| 71 | export const DeepseekConfigSchema = OpenAIConfigSchema.extend({ |
| 72 | provider: z.literal("deepseek"), |
| 73 | }); |
| 74 | export type DeepseekConfig = z.infer<typeof DeepseekConfigSchema>; |
| 75 | |
| 76 | export const BedrockConfigSchema = OpenAIConfigSchema.extend({ |
| 77 | provider: z.literal("bedrock"), |
| 78 | // cacheBehavior: z.object({ |
| 79 | // cacheSystemMessage: z.boolean().optional(), |
| 80 | // cacheConversation: z.boolean().optional(), |
| 81 | // }).optional(), |
| 82 | env: z.object({ |
| 83 | region: z.string().optional(), |
| 84 | accessKeyId: z.string().optional(), |
| 85 | secretAccessKey: z.string().optional(), |
| 86 | profile: z.string().optional(), |
| 87 | }), |
| 88 | }); |
| 89 | export type BedrockConfig = z.infer<typeof BedrockConfigSchema>; |
| 90 | |
| 91 | export const LlamastackConfigSchema = OpenAIConfigSchema.extend({ |
| 92 | provider: z.literal("llamastack"), |
| 93 | }); |
| 94 | export type LlamastackConfig = z.infer<typeof LlamastackConfigSchema>; |
| 95 | |
| 96 | export const ContinueProxyConfigSchema = BasePlusConfig.extend({ |
| 97 | provider: z.literal("continue-proxy"), |
| 98 | env: z.object({ |
| 99 | apiKeyLocation: z.string().optional(), |
| 100 | envSecretLocations: z.record(z.string(), z.string()).optional(), |
| 101 | orgScopeId: z.string().nullable(), |
| 102 | proxyUrl: z.string().optional(), |
| 103 | }), |
| 104 | }); |
| 105 | export type ContinueProxyConfig = z.infer<typeof ContinueProxyConfigSchema>; |
| 106 | |
| 107 | export const MockConfigSchema = BasePlusConfig.extend({ |
| 108 | provider: z.literal("mock"), |
| 109 | }); |
| 110 | |
| 111 | export type MockConfig = z.infer<typeof MockConfigSchema>; |
| 112 | |
| 113 | // Other APIs |
| 114 | export const CohereConfigSchema = OpenAIConfigSchema.extend({ |
| 115 | provider: z.literal("cohere"), |
| 116 | }); |
| 117 | export type CohereConfig = z.infer<typeof CohereConfigSchema>; |
| 118 | |
| 119 | export const CometAPIConfigSchema = OpenAIConfigSchema.extend({ |
| 120 | provider: z.literal("cometapi"), |
| 121 | }); |
| 122 | export type CometAPIConfig = z.infer<typeof CometAPIConfigSchema>; |
| 123 | |
| 124 | export const AzureConfigSchema = OpenAIConfigSchema.extend({ |
| 125 | provider: z.literal("azure"), |
| 126 | env: z |
| 127 | .object({ |
| 128 | apiVersion: z.string().optional(), |
| 129 | apiType: z |
| 130 | .union([ |
| 131 | z.literal("azure-foundry"), |
| 132 | z.literal("azure-openai"), |
| 133 | z.literal("azure"), // Legacy |
| 134 | ]) |
| 135 | .optional(), |
| 136 | deployment: z.string().optional(), |
| 137 | }) |
| 138 | .optional(), |
| 139 | }); |
| 140 | export type AzureConfig = z.infer<typeof AzureConfigSchema>; |
| 141 | |
| 142 | export const GeminiConfigSchema = OpenAIConfigSchema.extend({ |
| 143 | provider: z.literal("gemini"), |
| 144 | apiKey: z.string(), |
| 145 | }); |
| 146 | export type GeminiConfig = z.infer<typeof GeminiConfigSchema>; |
| 147 | |
| 148 | export const AnthropicConfigSchema = OpenAIConfigSchema.extend({ |
| 149 | provider: z.literal("anthropic"), |
| 150 | apiKey: z.string(), |
| 151 | }); |
| 152 | export type AnthropicConfig = z.infer<typeof AnthropicConfigSchema>; |
| 153 | |
| 154 | export const WatsonXConfigSchema = BasePlusConfig.extend({ |
| 155 | provider: z.literal("watsonx"), |
| 156 | apiKey: z.string(), |
| 157 | env: z.object({ |
| 158 | apiVersion: z.string().optional(), |
| 159 | projectId: z.string().optional(), |
| 160 | deploymentId: z.string().optional(), |
| 161 | }), |
| 162 | }); |
| 163 | export type WatsonXConfig = z.infer<typeof WatsonXConfigSchema>; |
| 164 | |
| 165 | export const JinaConfigSchema = OpenAIConfigSchema.extend({ |
| 166 | provider: z.literal("jina"), |
| 167 | }); |
| 168 | export type JinaConfig = z.infer<typeof JinaConfigSchema>; |
| 169 | |
| 170 | export const InceptionConfigSchema = OpenAIConfigSchema.extend({ |
| 171 | provider: z.literal("inception"), |
| 172 | }); |
| 173 | export type InceptionConfig = z.infer<typeof InceptionConfigSchema>; |
| 174 | |
| 175 | export const VertexAIConfigSchema = BasePlusConfig.extend({ |
| 176 | provider: z.literal("vertexai"), |
| 177 | env: z |
| 178 | .object({ |
| 179 | region: z.string().optional(), |
| 180 | projectId: z.string().optional(), |
| 181 | keyFile: z.string().optional(), |
| 182 | keyJson: z.string().optional(), |
| 183 | }) |
| 184 | .optional(), |
| 185 | }); |
| 186 | export type VertexAIConfig = z.infer<typeof VertexAIConfigSchema>; |
| 187 | |
| 188 | // Discriminated union |
| 189 | export const LLMConfigSchema = z.discriminatedUnion("provider", [ |
| 190 | OpenAIConfigSchema, |
| 191 | BedrockConfigSchema, |
| 192 | MoonshotConfigSchema, |
| 193 | DeepseekConfigSchema, |
| 194 | CohereConfigSchema, |
| 195 | AzureConfigSchema, |
| 196 | GeminiConfigSchema, |
| 197 | AnthropicConfigSchema, |
| 198 | WatsonXConfigSchema, |
| 199 | JinaConfigSchema, |
| 200 | MockConfigSchema, |
| 201 | InceptionConfigSchema, |
| 202 | VertexAIConfigSchema, |
| 203 | LlamastackConfigSchema, |
| 204 | ContinueProxyConfigSchema, |
| 205 | CometAPIConfigSchema, |
| 206 | ]); |
| 207 | export type LLMConfig = z.infer<typeof LLMConfigSchema>; |