import {z} from 'zod' export type PersonaTargetInput = {name: string, description?: (string | null) | undefined}; export type URLRequest = {url: string, target_prompts?: (Array | null) | undefined, target_personas?: (Array | null) | undefined, target_sources?: (Array | null) | undefined, custom_instructions?: (string | null) | undefined, override_suggestions?: (boolean | null) | undefined, retain_schema_types?: (Array | null) | undefined}; export type OrchestrationCreateRequest = {urls?: (Array | null) | undefined, requests?: (Array | null) | undefined, optimize?: boolean | undefined, deploy_axp?: boolean | undefined, stage_axp?: boolean | undefined, site_id?: (string | null) | undefined, target_prompts?: (Array | null) | undefined, target_personas?: (Array | null) | undefined, target_sources?: (Array | null) | undefined, override_suggestions?: boolean | undefined, webhook_url_override?: (string | null) | undefined}; export type CreateApiOrchestrationOptimizeAndDeployBrandIdArgs = {brand_id: number, body: OrchestrationCreateRequest}; export type CreateApiOrchestrationOptimizeAndDeployBrandIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const orchestrationRecord = z.object({token: z.string(), url: z.string(), status: z.string(), orchestration_status: z.enum(["audit", "optimizing", "deploying", "completed", "failed"]).nullable().optional()}); export const orchestrationCreateResponse = z.object({pipeline_id: z.string().nullable().optional(), tokens: z.array(orchestrationRecord), status: z.string().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 Optimize And Deploy Pipeline * * POST /orchestration/optimize-and-deploy/{brand_id} */ export const createApiOrchestrationOptimizeAndDeployBrandId = async (args: CreateApiOrchestrationOptimizeAndDeployBrandIdArgs, options?: CreateApiOrchestrationOptimizeAndDeployBrandIdOptions) => { const {baseUrl = "https://api.scrunchai.com/v2", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/orchestration/optimize-and-deploy/${encodeURIComponent(String(args.brand_id))}${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(orchestrationCreateResponse, await res.json()) };