import {z} from 'zod' export type GetApiV2ArtifactsArtifactIdMessagesArgs = {limit?: number | undefined, before?: string | undefined, artifactId: string}; export type GetApiV2ArtifactsArtifactIdMessagesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2ArtifactsArtifactIdMessagesResponse = z.object({data: z.array(z.object({id: z.string(), artifact_id: z.string(), author_user_id: z.string().nullable().optional(), role: z.enum(["user", "assistant"]), content: z.string(), created_at: z.string(), revision_id: z.string().nullable().optional(), message: z.unknown(), selected_element: z.object({selector: z.string().min(1).max(2000), label: z.string().min(1).max(200), tag_name: z.string().min(1).max(50), text: z.string().max(500).optional(), outer_html: z.string().min(1).max(4000)}).nullable().optional()})), total: z.number().int().gte(0).lte(9007199254740991), has_more: 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 }; /** * List Pulse Dashboard Messages * * GET /v2/artifacts/{artifactId}/messages */ export const getApiV2ArtifactsArtifactIdMessages = async (args: GetApiV2ArtifactsArtifactIdMessagesArgs, options?: GetApiV2ArtifactsArtifactIdMessagesOptions) => { const {baseUrl = "https://api.duvo.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/artifacts/${encodeURIComponent(String(args.artifactId))}/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(getApiV2ArtifactsArtifactIdMessagesResponse, await res.json()) };