File size: 6,111 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | import { z } from 'zod';
import { PROVIDER_ID_PATTERN } from '@moonshot-ai/agent-core-v2';
import {
modelCatalogItemSchema,
providerCatalogItemSchema,
} from '@moonshot-ai/agent-core-v2/llm-adapter/model/catalog';
export const listModelsResponseSchema = z.object({
items: z.array(modelCatalogItemSchema),
});
export type ListModelsResponse = z.infer<typeof listModelsResponseSchema>;
export const listProvidersResponseSchema = z.object({
items: z.array(providerCatalogItemSchema),
});
export type ListProvidersResponse = z.infer<typeof listProvidersResponseSchema>;
export const getProviderResponseSchema = providerCatalogItemSchema.extend({
api_key: z.string().optional(),
});
export type GetProviderResponse = z.infer<typeof getProviderResponseSchema>;
export const providerWireTypeSchema = z.enum([
'kimi',
'openai',
'openai_responses',
'anthropic',
'google-genai',
'vertexai',
]);
export type ProviderWireType = z.infer<typeof providerWireTypeSchema>;
export const createProviderModelSchema = z.object({
model: z.string().min(1),
max_context_size: z.number().int().min(1),
display_name: z.string().min(1).optional(),
capabilities: z.array(z.string()).optional(),
max_output_size: z.number().int().min(1).optional(),
support_efforts: z.array(z.string().min(1)).optional(),
adaptive_thinking: z.boolean().optional(),
});
export type CreateProviderModel = z.infer<typeof createProviderModelSchema>;
function refineProviderForm(
value: { base_url?: string | undefined; models: Array<{ model: string }> },
ctx: z.RefinementCtx,
): void {
if (value.base_url !== undefined && value.base_url.includes('${')) {
ctx.addIssue({
code: 'custom',
message: 'base_url must not contain an environment variable placeholder',
path: ['base_url'],
});
}
const seen = new Set<string>();
for (const entry of value.models) {
if (seen.has(entry.model)) {
ctx.addIssue({
code: 'custom',
message: `duplicate model: ${entry.model}`,
path: ['models'],
});
return;
}
seen.add(entry.model);
}
}
export const providerIdSchema = z
.string()
.regex(
PROVIDER_ID_PATTERN,
'id must start with a letter or digit and may only contain letters, digits, "-", "_" and spaces',
);
export const createProviderRequestSchema = z
.object({
id: providerIdSchema,
type: providerWireTypeSchema,
api_key: z.string().optional(),
base_url: z.string().trim().optional(),
default_model: z.string().min(1).optional(),
models: z.array(createProviderModelSchema).min(1),
})
.superRefine((value, ctx) => {
refineProviderForm(value, ctx);
if (
value.default_model !== undefined &&
!value.models.some((entry) => entry.model === value.default_model)
) {
ctx.addIssue({
code: 'custom',
message: 'default_model must be one of models[].model',
path: ['default_model'],
});
}
});
export type CreateProviderRequest = z.infer<typeof createProviderRequestSchema>;
export const createProviderResponseSchema = providerCatalogItemSchema;
export type CreateProviderResponse = z.infer<typeof createProviderResponseSchema>;
export const replaceProviderRequestSchema = z
.object({
new_id: providerIdSchema.optional(),
type: providerWireTypeSchema,
api_key: z.string().optional(),
base_url: z.string().trim().optional(),
default_model: z.string().min(1).optional(),
models: z.array(createProviderModelSchema).min(1),
})
.superRefine((value, ctx) => {
refineProviderForm(value, ctx);
if (
value.default_model !== undefined &&
!value.models.some((entry) => entry.model === value.default_model)
) {
ctx.addIssue({
code: 'custom',
message: 'default_model must be one of models[].model',
path: ['default_model'],
});
}
});
export type ReplaceProviderRequest = z.infer<typeof replaceProviderRequestSchema>;
export const replaceProviderResponseSchema = z.object({
provider: providerCatalogItemSchema,
});
export type ReplaceProviderResponse = z.infer<typeof replaceProviderResponseSchema>;
export const catalogModelItemSchema = z.object({
id: z.string().min(1),
name: z.string().optional(),
max_context_size: z.number().int().min(1),
capabilities: z.array(z.string()).optional(),
reasoning: z.boolean(),
});
export type CatalogModelItem = z.infer<typeof catalogModelItemSchema>;
export const catalogProviderItemSchema = z.object({
id: z.string().min(1),
name: z.string().min(1),
wire_type: providerWireTypeSchema.nullable(),
guessed: z.boolean(),
needs_base_url: z.boolean(),
rejected: z.boolean(),
reject_reason: z.string().nullable(),
env_key: z.string().nullable(),
models: z.array(catalogModelItemSchema),
});
export type CatalogProviderItem = z.infer<typeof catalogProviderItemSchema>;
export const listCatalogProvidersResponseSchema = z.object({
items: z.array(catalogProviderItemSchema),
});
export type ListCatalogProvidersResponse = z.infer<typeof listCatalogProvidersResponseSchema>;
export const getCatalogProviderResponseSchema = catalogProviderItemSchema;
export type GetCatalogProviderResponse = z.infer<typeof getCatalogProviderResponseSchema>;
export const providerCollectionActionBodySchema = z.object({
catalog_id: z.string().min(1).optional(),
api_key: z.string().optional(),
base_url: z.string().optional(),
id: providerIdSchema.optional(),
url: z.string().min(1).optional(),
});
export type ProviderCollectionActionBody = z.infer<typeof providerCollectionActionBodySchema>;
export const importCatalogProviderResponseSchema = z.object({
provider: providerCatalogItemSchema,
models_imported: z.number().int().min(0),
});
export type ImportCatalogProviderResponse = z.infer<typeof importCatalogProviderResponseSchema>;
export const importCustomRegistryResponseSchema = z.object({
providers: z.array(providerCatalogItemSchema),
models_imported: z.number().int().min(0),
});
export type ImportCustomRegistryResponse = z.infer<typeof importCustomRegistryResponseSchema>;
|