import {z} from 'zod' export type RetryConfig = {max_retries?: number | undefined, base_delay_seconds?: number | undefined, max_delay_seconds?: number | undefined, jitter?: boolean | undefined}; export type KbPiiSettings = {enabled?: boolean | undefined}; export type CallWindowSettings = {enabled?: boolean | undefined, timezone?: string | undefined, window_start?: string | undefined, window_end?: string | undefined, days_of_week?: Array | undefined}; export type CallExpirySettings = {enabled?: boolean | undefined, days?: number | undefined, hours?: number | undefined}; export type HolidayEntry = {name?: (string | null) | undefined, start_date: string, end_date?: (string | null) | undefined, recurring?: boolean | undefined}; export type HolidayCalendarSettings = {enabled?: boolean | undefined, holidays?: Array | undefined}; export type VoiceEntitlementSettings = {max_active_calls?: (number | null) | undefined, outbound_cps?: (number | null) | undefined, queue_weight?: (number | null) | undefined, priority_class?: (number | null) | undefined, priority_aging_max_wait_seconds?: (number | null) | undefined, inbound_reserved_calls?: (number | null) | undefined}; export type MemoryOrgSettings = {min_user_turns?: (number | null) | undefined, fact_cap?: (number | null) | undefined, retention_days?: (number | null) | undefined}; export type CurrentTimeSettings = {enabled?: (boolean | null) | undefined, timezone?: (string | null) | undefined}; export type OrgSettings = {retry?: (RetryConfig | null) | undefined, kb_pii?: (KbPiiSettings | null) | undefined, kb_role_clearance_map?: ({[key: string]: number} | null) | undefined, kb_role_audience_map?: ({[key: string]: Array} | null) | undefined, kb_classification_mode?: ('auto' | 'kb_default' | null) | undefined, kb_acl_oversample_factor?: (number | null) | undefined, call_window?: (CallWindowSettings | null) | undefined, call_expiry?: (CallExpirySettings | null) | undefined, holiday_calendar?: (HolidayCalendarSettings | null) | undefined, voice_entitlements?: (VoiceEntitlementSettings | null) | undefined, memory?: (MemoryOrgSettings | null) | undefined, inline_kb_sources?: (boolean | null) | undefined, current_time?: (CurrentTimeSettings | null) | undefined} | {[key: string]: unknown}; export type OrganizationUpdate = {settings?: (OrgSettings | null) | undefined}; export type UpdateApiV1IdentityOrgArgs = {body: OrganizationUpdate}; export type UpdateApiV1IdentityOrgOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const retryConfig = z.object({max_retries: z.number().int().gte(0).lte(5).optional(), base_delay_seconds: z.number().gte(0.1).lte(30).optional(), max_delay_seconds: z.number().gte(1).lte(120).optional(), jitter: z.boolean().optional()}); export const kbPiiSettings = z.object({enabled: z.boolean().optional()}); export const callWindowSettings = z.object({enabled: z.boolean().optional(), timezone: z.string().optional(), window_start: z.string().optional(), window_end: z.string().optional(), days_of_week: z.array(z.number().int()).optional()}); export const callExpirySettings = z.object({enabled: z.boolean().optional(), days: z.number().int().gte(0).lte(30).optional(), hours: z.number().int().gte(0).lte(23).optional()}); export const holidayEntry = z.object({name: z.string().max(100).nullable().optional(), start_date: z.string(), end_date: z.string().nullable().optional(), recurring: z.boolean().optional()}); export const holidayCalendarSettings = z.object({enabled: z.boolean().optional(), holidays: z.array(holidayEntry).optional()}); export const voiceEntitlementSettings = z.object({max_active_calls: z.number().int().gte(1).lte(10000).nullable().optional(), outbound_cps: z.number().gt(0).lte(10000).nullable().optional(), queue_weight: z.number().gt(0).lte(1000).nullable().optional(), priority_class: z.number().int().gte(0).nullable().optional(), priority_aging_max_wait_seconds: z.number().int().gte(1).lte(86400).nullable().optional(), inbound_reserved_calls: z.number().int().gte(0).lte(10000).nullable().optional()}); export const memoryOrgSettings = z.object({min_user_turns: z.number().int().gte(0).nullable().optional(), fact_cap: z.number().int().gte(1).lte(100).nullable().optional(), retention_days: z.number().int().gte(1).nullable().optional()}); export const currentTimeSettings = z.object({enabled: z.boolean().nullable().optional(), timezone: z.string().nullable().optional()}); export const orgSettings = z.object({retry: retryConfig.nullable().optional(), kb_pii: kbPiiSettings.nullable().optional(), kb_role_clearance_map: z.record(z.string(), z.number().int()).nullable().optional(), kb_role_audience_map: z.record(z.string(), z.array(z.string())).nullable().optional(), kb_classification_mode: z.enum(["auto", "kb_default"]).nullable().optional(), kb_acl_oversample_factor: z.number().int().gte(1).nullable().optional(), call_window: callWindowSettings.nullable().optional(), call_expiry: callExpirySettings.nullable().optional(), holiday_calendar: holidayCalendarSettings.nullable().optional(), voice_entitlements: voiceEntitlementSettings.nullable().optional(), memory: memoryOrgSettings.nullable().optional(), inline_kb_sources: z.boolean().nullable().optional(), current_time: currentTimeSettings.nullable().optional()}).and(z.record(z.string(), z.unknown())); export const organizationResponse = z.object({id: z.string(), clerk_org_id: z.string(), name: z.string(), slug: z.string().nullable(), settings: orgSettings, is_active: z.boolean(), created_at: z.string(), updated_at: z.string()}); 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 current organization * * PUT /v1/identity/org */ export const updateApiV1IdentityOrg = async (args: UpdateApiV1IdentityOrgArgs, options: UpdateApiV1IdentityOrgOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/identity/org${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(organizationResponse, await res.json()) };