import {z} from 'zod' export type TelemetryContentMode = 'TELEMETRY_CONTENT_MODE_METADATA_ONLY' | 'TELEMETRY_CONTENT_MODE_FULL_CONTENT'; export type TelemetryProjectScope = 'TELEMETRY_PROJECT_SCOPE_ALL' | 'TELEMETRY_PROJECT_SCOPE_SELECTED' | 'TELEMETRY_PROJECT_SCOPE_ALL_EXCEPT'; export type TelemetryDestination = {id?: string | undefined, workspace_id?: string | undefined, display_name?: string | undefined, provider?: string | undefined, endpoint_url?: string | undefined, request_headers?: {[key: string]: string} | undefined, settings?: {[key: string]: string} | undefined, content_mode?: TelemetryContentMode | undefined, project_scope?: TelemetryProjectScope | undefined, project_ids?: Array | undefined, enabled?: boolean | undefined, created_at?: string | undefined, updated_at?: string | undefined, failure_count?: number | undefined, last_error?: string | undefined, last_status_code?: number | undefined, last_attempt_at?: string | undefined, disabled_reason?: string | undefined}; export type CreateTelemetryDestinationRequest = {destination?: TelemetryDestination | undefined}; export type CreateApiV3TelemetryDestinationsArgs = {body: CreateTelemetryDestinationRequest}; export type CreateApiV3TelemetryDestinationsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const telemetryContentMode = z.enum(["TELEMETRY_CONTENT_MODE_METADATA_ONLY", "TELEMETRY_CONTENT_MODE_FULL_CONTENT"]); export const telemetryProjectScope = z.enum(["TELEMETRY_PROJECT_SCOPE_ALL", "TELEMETRY_PROJECT_SCOPE_SELECTED", "TELEMETRY_PROJECT_SCOPE_ALL_EXCEPT"]); export const telemetryDestination = z.object({id: z.string().optional(), workspace_id: z.string().optional(), display_name: z.string().optional(), provider: z.string().optional(), endpoint_url: z.string().optional(), request_headers: z.record(z.string(), z.string()).optional(), settings: z.record(z.string(), z.string()).optional(), content_mode: telemetryContentMode.optional(), project_scope: telemetryProjectScope.optional(), project_ids: z.array(z.string()).optional(), enabled: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), failure_count: z.number().int().optional(), last_error: z.string().optional(), last_status_code: z.number().int().optional(), last_attempt_at: z.string().optional(), disabled_reason: z.string().optional()}); export const telemetryDestinationResponse = z.object({destination: telemetryDestination.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 }; /** * Create an OpenTelemetry destination * * POST /v3/telemetry-destinations */ export const createApiV3TelemetryDestinations = async (args: CreateApiV3TelemetryDestinationsArgs, options?: CreateApiV3TelemetryDestinationsOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/telemetry-destinations${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(telemetryDestinationResponse, await res.json()) };