| 1 | import { 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 | export type ClientCertificateOptions = z.infer< |
| 9 | typeof clientCertificateOptionsSchema |
| 10 | >; |
| 11 | |
| 12 | export const requestOptionsSchema = z.object({ |
| 13 | timeout: z.number().optional(), |
| 14 | verifySsl: z.boolean().optional(), |
| 15 | caBundlePath: z.union([z.string(), z.array(z.string())]).optional(), |
| 16 | proxy: z.string().optional(), |
| 17 | headers: z.record(z.string()).optional(), |
| 18 | extraBodyProperties: z.record(z.any()).optional(), |
| 19 | noProxy: z.array(z.string()).optional(), |
| 20 | clientCertificate: clientCertificateOptionsSchema.optional(), |
| 21 | }); |
| 22 | export type RequestOptions = z.infer<typeof requestOptionsSchema>; |
| 23 | export const modelRolesSchema = z.enum([ |
| 24 | "chat", |
| 25 | "autocomplete", |
| 26 | "embed", |
| 27 | "rerank", |
| 28 | "edit", |
| 29 | "apply", |
| 30 | "summarize", |
| 31 | ]); |
| 32 | export type ModelRole = z.infer<typeof modelRolesSchema>; |
| 33 | |
| 34 | // TODO consider just using array of strings for model capabilities |
| 35 | // To allow more dynamic string parsing |
| 36 | export const modelCapabilitySchema = z.union([ |
| 37 | z.literal("tool_use"), |
| 38 | z.literal("image_input"), |
| 39 | ]); |
| 40 | export type ModelCapability = z.infer<typeof modelCapabilitySchema>; |
| 41 | |
| 42 | export const completionOptionsSchema = z.object({ |
| 43 | contextLength: z.number().optional(), |
| 44 | maxTokens: z.number().optional(), |
| 45 | temperature: z.number().optional(), |
| 46 | topP: z.number().optional(), |
| 47 | topK: z.number().optional(), |
| 48 | stop: z.array(z.string()).optional(), |
| 49 | n: z.number().optional(), |
| 50 | reasoning: z.boolean().optional(), |
| 51 | reasoningBudgetTokens: z.number().optional(), |
| 52 | promptCaching: z.boolean().optional(), |
| 53 | stream: z.boolean().optional(), |
| 54 | }); |
| 55 | export type CompletionOptions = z.infer<typeof completionOptionsSchema>; |
| 56 | |
| 57 | export const embeddingTasksSchema = z.union([ |
| 58 | z.literal("chunk"), |
| 59 | z.literal("query"), |
| 60 | ]); |
| 61 | export type EmbeddingTasks = z.infer<typeof embeddingTasksSchema>; |
| 62 | |
| 63 | export const embeddingPrefixesSchema = z.record( |
| 64 | embeddingTasksSchema, |
| 65 | z.string(), |
| 66 | ); |
| 67 | export type EmbeddingPrefixes = z.infer<typeof embeddingPrefixesSchema>; |
| 68 | |
| 69 | export const cacheBehaviorSchema = z.object({ |
| 70 | cacheSystemMessage: z.boolean().optional(), |
| 71 | cacheConversation: z.boolean().optional(), |
| 72 | }); |
| 73 | export type CacheBehavior = z.infer<typeof cacheBehaviorSchema>; |
| 74 | |
| 75 | export const embedOptionsSchema = z.object({ |
| 76 | maxChunkSize: z.number().optional(), |
| 77 | maxBatchSize: z.number().optional(), |
| 78 | embeddingPrefixes: embeddingPrefixesSchema.optional(), |
| 79 | }); |
| 80 | export type EmbedOptions = z.infer<typeof embedOptionsSchema>; |
| 81 | |
| 82 | export const chatOptionsSchema = z.object({ |
| 83 | baseSystemMessage: z.string().optional(), |
| 84 | baseAgentSystemMessage: z.string().optional(), |
| 85 | }); |
| 86 | export type ChatOptions = z.infer<typeof chatOptionsSchema>; |
| 87 | |
| 88 | const templateSchema = z.enum([ |
| 89 | "llama2", |
| 90 | "alpaca", |
| 91 | "zephyr", |
| 92 | "phi2", |
| 93 | "phind", |
| 94 | "anthropic", |
| 95 | "chatml", |
| 96 | "none", |
| 97 | "openchat", |
| 98 | "deepseek", |
| 99 | "xwin-coder", |
| 100 | "neural-chat", |
| 101 | "codellama-70b", |
| 102 | "llava", |
| 103 | "gemma", |
| 104 | "granite", |
| 105 | "llama3", |
| 106 | "codestral", |
| 107 | ]); |
| 108 | |
| 109 | export const autocompleteOptionsSchema = z.object({ |
| 110 | disable: z.boolean().optional(), |
| 111 | maxPromptTokens: z.number().optional(), |
| 112 | debounceDelay: z.number().optional(), |
| 113 | modelTimeout: z.number().optional(), |
| 114 | maxSuffixPercentage: z.number().optional(), |
| 115 | prefixPercentage: z.number().optional(), |
| 116 | template: z.string().optional(), |
| 117 | onlyMyCode: z.boolean().optional(), |
| 118 | }); |
| 119 | |
| 120 | /** Prompt templates use Handlebars syntax */ |
| 121 | const promptTemplatesSchema = z.object({ |
| 122 | apply: z.string().optional(), |
| 123 | chat: templateSchema.optional(), |
| 124 | edit: z.string().optional(), |
| 125 | autocomplete: z.string().optional(), |
| 126 | }); |
| 127 | export type PromptTemplates = z.infer<typeof promptTemplatesSchema>; |
| 128 | |
| 129 | const baseModelFields = { |
| 130 | name: z.string(), |
| 131 | model: z.string(), |
| 132 | apiKey: z.string().optional(), |
| 133 | apiBase: z.string().optional(), |
| 134 | maxStopWords: z.number().optional(), |
| 135 | roles: modelRolesSchema.array().optional(), |
| 136 | capabilities: modelCapabilitySchema.array().optional(), |
| 137 | defaultCompletionOptions: completionOptionsSchema.optional(), |
| 138 | cacheBehavior: cacheBehaviorSchema.optional(), |
| 139 | requestOptions: requestOptionsSchema.optional(), |
| 140 | embedOptions: embedOptionsSchema.optional(), |
| 141 | chatOptions: chatOptionsSchema.optional(), |
| 142 | promptTemplates: promptTemplatesSchema.optional(), |
| 143 | useLegacyCompletionsEndpoint: z.boolean().optional(), |
| 144 | env: z |
| 145 | .record(z.string(), z.union([z.string(), z.boolean(), z.number()])) |
| 146 | .optional(), |
| 147 | autocompleteOptions: autocompleteOptionsSchema.optional(), |
| 148 | }; |
| 149 | |
| 150 | export const modelSchema = z.union([ |
| 151 | z.object({ |
| 152 | ...baseModelFields, |
| 153 | provider: z.literal("continue-proxy"), |
| 154 | apiKeyLocation: z.string(), |
| 155 | orgScopeId: z.string().nullable(), |
| 156 | onPremProxyUrl: z.string().nullable(), |
| 157 | }), |
| 158 | z.object({ |
| 159 | ...baseModelFields, |
| 160 | provider: z.string().refine((val) => val !== "continue-proxy"), |
| 161 | }), |
| 162 | ]); |
| 163 | |
| 164 | export const partialModelSchema = z.union([ |
| 165 | z |
| 166 | .object({ |
| 167 | ...baseModelFields, |
| 168 | provider: z.literal("continue-proxy"), |
| 169 | apiKeyLocation: z.string(), |
| 170 | }) |
| 171 | .partial(), |
| 172 | z |
| 173 | .object({ |
| 174 | ...baseModelFields, |
| 175 | provider: z.string().refine((val) => val !== "continue-proxy"), |
| 176 | }) |
| 177 | .partial(), |
| 178 | ]); |
| 179 | |
| 180 | export type ModelConfig = z.infer<typeof modelSchema>; |