import {z} from 'zod' export type CreateApiV2ArtifactsArtifactIdMessagesArgs = {artifactId: string, body: {message: string, from_revision_id?: string | undefined, selected_element?: ({selector: string, label: string, tag_name: string, text?: string | undefined, outer_html: string}) | undefined}}; export type CreateApiV2ArtifactsArtifactIdMessagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2ArtifactsArtifactIdMessagesResponse = z.object({user_message: 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()})}); 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 }; /** * Send Pulse Dashboard Message * * POST /v2/artifacts/{artifactId}/messages */ export const createApiV2ArtifactsArtifactIdMessages = async (args: CreateApiV2ArtifactsArtifactIdMessagesArgs, options?: CreateApiV2ArtifactsArtifactIdMessagesOptions) => { const {baseUrl = "https://api.duvo.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/artifacts/${encodeURIComponent(String(args.artifactId))}/messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV2ArtifactsArtifactIdMessagesResponse, await res.json()) };