import {z} from 'zod' export type ScenarioSource = 'manual' | 'drafted'; export type ScenarioCreate = {name: string, intent: string, journey?: string | undefined, source?: ScenarioSource | undefined, source_brief?: (string | null) | undefined, source_agent_id?: (string | null) | undefined, source_persona_id?: (string | null) | undefined, tags?: Array | undefined, agent_ids?: Array | undefined}; export type CreateApiV1ScenariosArgs = {body: ScenarioCreate}; export type CreateApiV1ScenariosOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const scenarioSource = z.enum(["manual", "drafted"]); export const scenarioAgentRef = z.object({id: z.string(), name: z.string()}); export const scenarioResponse = z.object({id: z.string(), organization_id: z.string(), name: z.string(), intent: z.string(), journey: z.string(), source: scenarioSource, source_brief: z.string().nullable().optional(), source_agent_id: z.string().nullable().optional(), source_agent_name: z.string().nullable().optional(), source_persona_id: z.string().nullable().optional(), source_persona_name: z.string().nullable().optional(), tags: z.array(z.string()), agents: z.array(scenarioAgentRef).optional(), created_by: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); 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 a scenario * * POST /v1/scenarios */ export const createApiV1Scenarios = async (args: CreateApiV1ScenariosArgs, options: CreateApiV1ScenariosOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/scenarios${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(scenarioResponse, await res.json()) };