import {z} from 'zod' export type UpdateConfigVariablesBody = {variables: Record}; export type PatchApiV1AgentsAgentIdRealTimeConfigsClientEnvVariablesArgs = {body: UpdateConfigVariablesBody}; export type PatchApiV1AgentsAgentIdRealTimeConfigsClientEnvVariablesOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getConfigPageResponse = z.object({clientEnv: z.string(), variables: z.object({}).optional(), schema: z.object({}).optional(), lastUpdated: z.string().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 }; /** * Update config variables for an environment * * PATCH /v1/agents/{agentId}/real-time-configs/{clientEnv}/variables */ export const patchApiV1AgentsAgentIdRealTimeConfigsClientEnvVariables = async (args: PatchApiV1AgentsAgentIdRealTimeConfigsClientEnvVariablesArgs, options?: PatchApiV1AgentsAgentIdRealTimeConfigsClientEnvVariablesOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/agents/${encodeURIComponent(String(args.agentId))}/real-time-configs/${encodeURIComponent(String(args.clientEnv))}/variables${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(getConfigPageResponse, await res.json()) };