import {z} from 'zod' export type GetApiV2LiveActionChatsChatIdMessagesArgs = {chatId: string, cursor?: string | undefined, limit?: number | undefined}; export type GetApiV2LiveActionChatsChatIdMessagesOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const attachmentDto = z.object({id: z.string(), messageId: z.string(), status: z.enum(["IN_QUEUE", "IN_PROGRESS", "COMPLETED", "FAILED"]), type: z.string(), url: z.string(), videoUrl: z.string().nullable(), blurredUrl: z.string().nullable(), explicit: z.boolean(), mimeType: z.string(), prompt: z.string().nullable(), rephrasedPrompt: z.string().nullable(), source: z.enum(["chat_ask", "scene_maker", "chat_animate_image", "chat_gift", "user_photo", "voice_over", "voice_message", "ai_initiated", "studio", "demo_subscription_reward", "upscale"]).nullable(), durationSeconds: z.number().nullable(), canRetry: z.boolean(), retryCount: z.number()}); export const messageDto = z.object({id: z.string(), chatId: z.string(), text: z.string(), status: z.enum(["IN_PROGRESS", "COMPLETED", "FAILED"]), isFromUser: z.boolean(), isFromSystem: z.boolean(), isRead: z.boolean(), createdAt: z.string(), attachments: z.array(attachmentDto), messageType: z.enum(["TEXT", "IMAGE_REQUEST", "VIDEO_REQUEST", "ATTACHMENT", "ANIMATE_REQUEST"]), moderationStatus: z.enum(["NONE", "ALLOWED", "BLOCKED", "FAILED_OPEN", "FLAGGED"]), moderationFeedbackSent: z.boolean(), isDeleted: z.boolean().optional(), idempotencyKey: z.string(), suggestions: z.array(z.string()).optional(), giftSuggestion: z.object({}).optional(), actions: z.array(z.string()).optional()}); export const getMessagesResponseDto = z.object({items: z.array(messageDto), nextCursor: z.string().optional(), prevCursor: z.string().optional(), hasNext: z.boolean(), hasPrev: z.boolean()}); 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 messages for a live-action chat * * GET /v2/live-action-chats/{chatId}/messages */ export const getApiV2LiveActionChatsChatIdMessages = async (args: GetApiV2LiveActionChatsChatIdMessagesArgs, options: GetApiV2LiveActionChatsChatIdMessagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/live-action-chats/${encodeURIComponent(String(args.chatId))}/messages${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(getMessagesResponseDto, await res.json()) };