import {z} from 'zod' export type GetApiV1SpeechToTextTranscriptsTranscriptionIdArgs = {transcription_id: string, 'xi-api-key'?: (string | null) | undefined}; export type GetApiV1SpeechToTextTranscriptsTranscriptionIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const speechToTextCharacterResponseModel = z.object({text: z.string(), start: z.number().nullable().optional(), end: z.number().nullable().optional()}); export const speechToTextWordResponseModel = z.object({text: z.string(), start: z.number().nullable().optional(), end: z.number().nullable().optional(), type: z.enum(["word", "spacing", "audio_event"]), speaker_id: z.string().nullable().optional(), logprob: z.number(), characters: z.array(speechToTextCharacterResponseModel).nullable().optional(), channel_index: z.number().int().nullable().optional()}); export const additionalFormatResponseModel = z.object({requested_format: z.string(), file_extension: z.string(), content_type: z.string(), is_base64_encoded: z.boolean(), content: z.string()}); export const detectedEntity = z.object({text: z.string(), entity_type: z.string(), start_char: z.number().int(), end_char: z.number().int()}); export const editedTranscript = z.object({kind: z.literal("transcript"), text: z.string(), edited_text: z.string()}); export const transcriptEditError = z.object({kind: z.literal("error"), error_type: z.literal("edit_failed"), message: z.string()}); export const speechToTextChunkResponseModel = z.object({language_code: z.string(), language_probability: z.number(), text: z.string(), words: z.array(speechToTextWordResponseModel), channel_index: z.number().int().nullable().optional(), additional_formats: z.array(additionalFormatResponseModel.nullable()).nullable().optional(), transcription_id: z.string().nullable().optional(), entities: z.array(detectedEntity).nullable().optional(), audio_duration_secs: z.number().nullable().optional(), edited_transcript: z.union([editedTranscript, transcriptEditError]).nullable().optional()}); export const getApiV1SpeechToTextTranscriptsTranscriptionIdResponse = z.union([z.union([z.object({language_code: z.string(), language_probability: z.number(), text: z.string(), words: z.array(speechToTextWordResponseModel), channel_index: z.number().int().nullable().optional(), additional_formats: z.array(additionalFormatResponseModel.nullable()).nullable().optional(), transcription_id: z.string().nullable().optional(), entities: z.array(detectedEntity).nullable().optional(), audio_duration_secs: z.number().nullable().optional(), edited_transcript: z.union([editedTranscript, transcriptEditError]).nullable().optional()}), z.object({transcripts: z.array(speechToTextChunkResponseModel), transcription_id: z.string().nullable().optional(), audio_duration_secs: z.number().nullable().optional(), language_code: z.string(), language_probability: z.number(), text: z.string(), words: z.array(speechToTextWordResponseModel), channel_index: z.number().int().nullable().optional(), additional_formats: z.array(additionalFormatResponseModel.nullable()).nullable().optional(), entities: z.array(detectedEntity).nullable().optional(), edited_transcript: z.union([editedTranscript, transcriptEditError]).nullable().optional()})]), z.union([z.object({language_code: z.string(), language_probability: z.number(), text: z.string(), words: z.array(speechToTextWordResponseModel), channel_index: z.number().int().nullable().optional(), additional_formats: z.array(additionalFormatResponseModel.nullable()).nullable().optional(), transcription_id: z.string().nullable().optional(), entities: z.array(detectedEntity).nullable().optional(), audio_duration_secs: z.number().nullable().optional(), edited_transcript: z.union([editedTranscript, transcriptEditError]).nullable().optional(), transcripts: z.array(speechToTextChunkResponseModel)}), z.object({transcripts: z.array(speechToTextChunkResponseModel), transcription_id: z.string().nullable().optional(), audio_duration_secs: z.number().nullable().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 Transcript By Id * * GET /v1/speech-to-text/transcripts/{transcription_id} */ export const getApiV1SpeechToTextTranscriptsTranscriptionId = async (args: GetApiV1SpeechToTextTranscriptsTranscriptionIdArgs, options: GetApiV1SpeechToTextTranscriptsTranscriptionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['xi-api-key'] != null) headers.set("xi-api-key", String(args['xi-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/speech-to-text/transcripts/${encodeURIComponent(String(args.transcription_id))}${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(getApiV1SpeechToTextTranscriptsTranscriptionIdResponse, await res.json()) };