import {z} from 'zod' export type UpdateApiApiV1ModelProvidersConfigArgs = {body: {mode?: ('merge' | 'replace') | undefined, providers?: ({[key: string]: {providerTemplateId?: string | undefined, instanceId?: string | undefined, enabled?: boolean | undefined, auth?: ('api-key' | 'aws-sdk' | 'oauth' | 'token') | undefined, api?: ('openai-completions' | 'openai-responses' | 'openai-codex-responses' | 'anthropic-messages' | 'google-generative-ai' | 'github-copilot' | 'bedrock-converse-stream' | 'ollama') | undefined, apiKey?: (string | {source: 'env' | 'file' | 'exec', provider: string, id: string}) | undefined, baseUrl: string, oauthRegion?: ('global' | 'cn' | null) | undefined, oauthProfileRef?: string | undefined, displayName?: string | undefined, headers?: ({[key: string]: string | {source: 'env' | 'file' | 'exec', provider: string, id: string}}) | undefined, models?: (Array<{id: string, name: string, api?: ('openai-completions' | 'openai-responses' | 'openai-codex-responses' | 'anthropic-messages' | 'google-generative-ai' | 'github-copilot' | 'bedrock-converse-stream' | 'ollama') | undefined, reasoning?: boolean | undefined, input?: (Array<'text' | 'image'>) | undefined, cost?: {input: number, output: number, cacheRead: number, cacheWrite: number} | undefined, contextWindow?: number | undefined, maxTokens?: number | undefined, headers?: {[key: string]: string} | undefined, compat?: {[key: string]: unknown} | undefined}>) | undefined, metadata?: {[key: string]: unknown} | undefined}}) | undefined, bedrockDiscovery?: ({enabled?: boolean | undefined, region?: string | undefined, providerFilter?: Array | undefined, refreshInterval?: number | undefined, defaultContextWindow?: number | undefined, defaultMaxTokens?: number | undefined}) | undefined}}; export type UpdateApiApiV1ModelProvidersConfigOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const updateApiApiV1ModelProvidersConfigResponse = z.object({config: z.object({mode: z.enum(["merge", "replace"]).optional(), providers: z.record(z.string(), z.object({providerTemplateId: z.string().min(1).optional(), instanceId: z.string().min(1).optional(), enabled: z.boolean().optional(), auth: z.enum(["api-key", "aws-sdk", "oauth", "token"]).optional(), api: z.enum(["openai-completions", "openai-responses", "openai-codex-responses", "anthropic-messages", "google-generative-ai", "github-copilot", "bedrock-converse-stream", "ollama"]).optional(), apiKey: z.union([z.string().min(1), z.object({source: z.enum(["env", "file", "exec"]), provider: z.string().min(1), id: z.string().min(1)})]).optional(), baseUrl: z.string().min(1), oauthRegion: z.enum(["global", "cn"]).nullable().optional(), oauthProfileRef: z.string().min(1).optional(), displayName: z.string().min(1).optional(), headers: z.record(z.string(), z.union([z.string(), z.object({source: z.enum(["env", "file", "exec"]), provider: z.string().min(1), id: z.string().min(1)})])).optional(), models: z.array(z.object({id: z.string().min(1), name: z.string().min(1), api: z.enum(["openai-completions", "openai-responses", "openai-codex-responses", "anthropic-messages", "google-generative-ai", "github-copilot", "bedrock-converse-stream", "ollama"]).optional(), reasoning: z.boolean().optional(), input: z.array(z.enum(["text", "image"])).optional(), cost: z.object({input: z.number(), output: z.number(), cacheRead: z.number(), cacheWrite: z.number()}).optional(), contextWindow: z.number().optional(), maxTokens: z.number().optional(), headers: z.record(z.string(), z.string()).optional(), compat: z.record(z.string(), z.unknown()).optional()})).optional(), metadata: z.record(z.string(), z.unknown()).optional()})).optional(), bedrockDiscovery: z.object({enabled: z.boolean().optional(), region: z.string().optional(), providerFilter: z.array(z.string()).optional(), refreshInterval: z.number().optional(), defaultContextWindow: z.number().optional(), defaultMaxTokens: z.number().optional()}).optional()})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * PUT /api/v1/model-providers/config */ export const updateApiApiV1ModelProvidersConfig = async (args: UpdateApiApiV1ModelProvidersConfigArgs, options: UpdateApiApiV1ModelProvidersConfigOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/model-providers/config${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiApiV1ModelProvidersConfigResponse, await res.json()) };