import {z} from 'zod' export type PlacementScope = {page_id?: (string | null) | undefined, page_ids?: (Array | null) | undefined, project?: boolean | undefined, measurement_ids?: (Array | null) | undefined}; export type PlaceConditionSpec = {source_condition_id: string, target_condition_id: string, scope: PlacementScope, exemplar_measurement_id?: (string | null) | undefined}; export type StackProposalRequest = {conversation_id: string, spec: PlaceConditionSpec}; export type CreateApiApiV1ProjectsProjectIdStackProposalsArgs = {project_id: string, 'Idempotency-Key': string, body: StackProposalRequest}; export type CreateApiApiV1ProjectsProjectIdStackProposalsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const actionImpact = z.object({measurements: z.number().int().optional(), sheets: z.number().int().optional(), conditions: z.number().int().optional(), folders: z.number().int().optional()}); export const actionReceipt = z.object({ordinal: z.number().int(), action_id: z.string(), status: z.enum(["pending", "applied", "rejected", "undone", "failed"]), kind: z.enum(["create_condition", "create_folder", "update_condition", "update_folder", "move_conditions", "delete_condition", "delete_folder", "update_measurement", "reassign_measurement", "rename_measurement_group", "delete_measurement", "restore_measurement", "create_annotation", "update_annotation_status", "place_measurements", "flag_page_verification", "set_page_verification_note"]), entity_type: z.enum(["condition", "folder", "measurement", "annotation", "page"]), entity_id: z.string().nullable().optional(), summary: z.string(), impact: actionImpact.nullable().optional(), error: z.string().nullable().optional()}); export const writeBatchReceipt = z.object({batch_id: z.string(), items: z.array(actionReceipt)}); export const stackProposalResponse = z.object({outcome: z.enum(["proposed", "nothing_to_place"]), reason: z.enum(["no_source_markups", "already_placed"]).nullable().optional(), receipt: writeBatchReceipt.nullable().optional(), already_placed: z.number().int().nullable().optional(), source_name: z.string().nullable().optional(), target_name: z.string().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 }; /** * Create Stack Proposal * * POST /api/v1/projects/{project_id}/stack-proposals */ export const createApiApiV1ProjectsProjectIdStackProposals = async (args: CreateApiApiV1ProjectsProjectIdStackProposalsArgs, options: CreateApiApiV1ProjectsProjectIdStackProposalsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.project_id))}/stack-proposals${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(stackProposalResponse, await res.json()) };