import {z} from 'zod' export type GetApiWorkspacesWorkspaceIdExperimentsExperimentIdArgs = {experiment_id: string, workspace_id: string}; export type GetApiWorkspacesWorkspaceIdExperimentsExperimentIdOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentVariantDetail = z.object({id: z.string(), position: z.number().int(), name: z.string().nullable().optional(), color: z.string().nullable().optional(), experiment_id: z.string(), headline: z.string().nullable().optional(), attachments: z.array(z.record(z.string(), z.unknown())), created_at: z.string()}); export const experimentPopulationRef = z.object({id: z.string(), name: z.string(), subtitle: z.string().nullable().optional()}); export const experimentEvaluationItem = z.object({id: z.string(), experiment_id: z.string(), persona_id: z.string(), variant_id: z.string(), rationale: z.string().nullable().optional(), is_winner: z.boolean().nullable().optional(), created_at: z.string()}); export const experimentPersonaFailureItem = z.object({id: z.string(), experiment_id: z.string(), persona_id: z.string(), error_message: z.string(), created_at: z.string()}); export const experimentDetail = z.object({id: z.string(), experiment_id: z.string(), name: z.string(), owner_id: z.string(), workspace_id: z.string(), population_id: z.string(), context: z.string().nullable().optional(), type: z.literal("ab_test"), category: z.enum(["creatives", "messaging", "products", "websites"]).nullable().optional(), status: z.enum(["draft", "queued", "running", "completed", "failed"]), started_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), insights: z.record(z.string(), z.unknown()).nullable().optional(), demographic_filter: z.record(z.string(), z.unknown()).nullable().optional(), flow: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string(), updated_at: z.string(), experiment_variants: z.array(experimentVariantDetail), populations: experimentPopulationRef.nullable().optional(), evaluations: z.array(experimentEvaluationItem), persona_failures: z.array(experimentPersonaFailureItem)}); export const readExperimentResponse = z.object({status: z.literal("completed"), experiment: experimentDetail}); 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 }; /** * Read Experiment * * GET /workspaces/{workspace_id}/experiments/{experiment_id} */ export const getApiWorkspacesWorkspaceIdExperimentsExperimentId = async (args: GetApiWorkspacesWorkspaceIdExperimentsExperimentIdArgs, options: GetApiWorkspacesWorkspaceIdExperimentsExperimentIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/workspaces/${encodeURIComponent(String(args.workspace_id))}/experiments/${encodeURIComponent(String(args.experiment_id))}${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(readExperimentResponse, await res.json()) };