import {z} from 'zod' export type GetApiBotsBotIdUsageArgs = {bot_id: string}; export type GetApiBotsBotIdUsageOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageReport = z.object({bot_id: z.string(), meetingbaas_bot_id: z.string().nullable().optional(), stt_provider: z.string(), tts_provider: z.string(), llm_provider: z.string().nullable().optional(), llm_model: z.string().nullable().optional(), key_sources: z.record(z.string(), z.string()).optional(), started_at: z.number(), ended_at: z.number().nullable().optional(), duration_seconds: z.number(), stt_audio_seconds: z.number(), tts_characters: z.number().int(), tts_requests: z.number().int(), llm_turns: z.number().int(), llm_prompt_tokens: z.number().int(), llm_cached_prompt_tokens: z.number().int(), llm_completion_tokens: z.number().int(), llm_total_tokens: z.number().int()}).and(z.record(z.string(), z.unknown())); 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 Bot Usage * * GET /bots/{bot_id}/usage */ export const getApiBotsBotIdUsage = async (args: GetApiBotsBotIdUsageArgs, options?: GetApiBotsBotIdUsageOptions) => { const {baseUrl = "https://speaking.meetingbaas.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-meeting-baas-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/bots/${encodeURIComponent(String(args.bot_id))}/usage${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(usageReport, await res.json()) };