import {z} from 'zod' export type AiProviderCredentialsOpenAi = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsAzureOpenAi = {resourceName: string, apiKey: string, deploymentName: string, apiVersion: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsGoogleGemini = {project: string, region: string, serviceAccountKey?: Record | undefined, workloadIdentityFederationAudience?: string | undefined, serviceAccountEmail?: string | undefined, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsGoogleGeminiAiStudio = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsMistralAi = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsAnthropic = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsXAi = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsWatsonx = {apiKey: string, projectId: string, region: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsDeepseek = {apiKey: string, baseUrl?: string | undefined, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsMicrosoftFoundry = {deployments: Array<{deploymentName: string, targetUri: string, apiKey: string}>, headers?: {[key: string]: string} | undefined, sendCustomHeadersOnly?: boolean | undefined}; export type AiProviderCredentialsCustomAi = {identifier: string, key: string}; export type AiProviderCrowdinConfigurationActionRule = {action?: ('pre_translate' | 'qa_check') | undefined, availableAiModelIds?: Array | undefined}; export type CrowdinAiProviderCreateForm = {name: string, type: 'open_ai' | 'azure_open_ai' | 'google_gemini' | 'google_gemini_ai_studio' | 'mistral_ai' | 'anthropic' | 'x_ai' | 'watsonx' | 'deepseek' | 'microsoft_foundry' | 'custom_ai', credentials?: (AiProviderCredentialsOpenAi | AiProviderCredentialsAzureOpenAi | AiProviderCredentialsGoogleGemini | AiProviderCredentialsGoogleGeminiAiStudio | AiProviderCredentialsMistralAi | AiProviderCredentialsAnthropic | AiProviderCredentialsXAi | AiProviderCredentialsWatsonx | AiProviderCredentialsDeepseek | AiProviderCredentialsMicrosoftFoundry | AiProviderCredentialsCustomAi) | undefined, config?: ({actionRules?: Array | undefined}) | undefined, isEnabled?: boolean | undefined, useSystemCredentials?: boolean | undefined}; export type CreateApiUsersUserIdAiProvidersArgs = {userId: number, body: CrowdinAiProviderCreateForm}; export type CreateApiUsersUserIdAiProvidersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const aiProviderCrowdinConfigurationActionRule = z.object({action: z.enum(["pre_translate", "qa_check"]).optional(), availableAiModelIds: z.array(z.string()).optional()}); export const crowdinAiProvider = z.object({id: z.number().int().optional(), name: z.string().optional(), type: z.string().optional(), credentials: z.union([z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({resourceName: z.string(), apiKey: z.string(), deploymentName: z.string(), apiVersion: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({project: z.string(), region: z.string(), serviceAccountKey: z.object({}).optional(), workloadIdentityFederationAudience: z.string().optional(), serviceAccountEmail: z.string().optional(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), projectId: z.string(), region: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({apiKey: z.string(), baseUrl: z.string().optional(), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({deployments: z.array(z.object({deploymentName: z.string(), targetUri: z.string(), apiKey: z.string()})), headers: z.record(z.string(), z.string()).optional(), sendCustomHeadersOnly: z.boolean().optional()}), z.object({identifier: z.string(), key: z.string()})]).nullable().optional(), config: z.object({actionRules: z.array(aiProviderCrowdinConfigurationActionRule).optional()}).optional(), isEnabled: z.boolean().optional(), useSystemCredentials: z.boolean().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), promptsCount: z.number().optional()}); export const crowdinAiProviderResource = z.object({data: crowdinAiProvider}); 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 }; /** * Add AI Provider * * POST /users/{userId}/ai/providers */ export const createApiUsersUserIdAiProviders = async (args: CreateApiUsersUserIdAiProvidersArgs, options?: CreateApiUsersUserIdAiProvidersOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", 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(/\/+$/, '')}/users/${encodeURIComponent(String(args.userId))}/ai/providers${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(crowdinAiProviderResource, await res.json()) };