import {z} from 'zod' export type Credential = {key: string, value?: string | undefined, secret?: boolean | undefined}; export type ProviderAuthConfig = {token_url?: string | undefined, send_as?: ('form' | 'json' | 'basic') | undefined, credentials?: Array | undefined, token_field?: string | undefined, expires_field?: string | undefined, fallback_ttl_seconds?: number | undefined}; export type ProviderAuthCheck = {provider_id?: string | undefined, auth_config?: ProviderAuthConfig | undefined}; export type CreateApiV1PrivateLlmProviderKeyAuthConfigTestArgs = {body: ProviderAuthCheck}; export type CreateApiV1PrivateLlmProviderKeyAuthConfigTestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const result = z.object({name: z.string().optional(), current: z.number().optional(), previous: z.number().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 }; /** * Test a provider's dynamic token auth * * POST /v1/private/llm-provider-key/auth-config/test */ export const createApiV1PrivateLlmProviderKeyAuthConfigTest = async (args: CreateApiV1PrivateLlmProviderKeyAuthConfigTestArgs, options?: CreateApiV1PrivateLlmProviderKeyAuthConfigTestOptions) => { const {baseUrl = "http://localhost:5173/api", 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(/\/+$/, '')}/v1/private/llm-provider-key/auth-config/test${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(result, await res.json()) };