import {z} from 'zod' export type GetApiSttV1AsyncTranscriptsRidOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSttV1AsyncTranscriptsRidResponse = z.object({rid: z.string().optional(), status: z.enum(["ai_requested", "uploaded", "file_processing", "transcribing", "post_processing", "transcribed", "input_error", "transcript_error", "file_error"]).optional(), progress: z.number().int().optional(), sttResults: z.array(z.object({transcript: z.string(), words: z.array(z.object({word: z.string().optional(), startTime: z.object({seconds: z.string().optional(), nanos: z.number().int().optional()}).optional(), endTime: z.object({seconds: z.string().optional(), nanos: z.number().int().optional()}).optional(), hasKeyword: z.boolean().optional(), speaker: z.string().optional()})).optional(), keywords: z.array(z.string()).optional()})).optional(), message: z.string().optional(), custom: z.object({}).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 }; /** * Get long audio transcription * * GET /stt/v1/async/transcripts/{rid} */ export const getApiSttV1AsyncTranscriptsRid = async (options: GetApiSttV1AsyncTranscriptsRidOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/stt/v1/async/transcripts/${encodeURIComponent(String(args.rid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiSttV1AsyncTranscriptsRidResponse, await res.json()) };