import {z} from 'zod' export type CustomHarnessCreate = {id?: (string | null) | undefined, name: string, description?: (string | null) | undefined, agent_id: string, persona_ids?: Array | undefined, policy_ids?: Array | undefined, system_prompt?: (string | null) | undefined}; export type CreateApiV1CustomHarnessesArgs = {team_id?: (string | null) | undefined, body: CustomHarnessCreate}; export type CreateApiV1CustomHarnessesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const customHarnessStatus = z.enum(["draft", "active", "archived", "failed"]); export const customHarness = z.object({id: z.string(), team_id: z.string(), name: z.string(), description: z.string().nullable().optional(), agent_id: z.string(), persona_ids: z.array(z.string()).optional(), policy_ids: z.array(z.string()).optional(), status: customHarnessStatus.optional(), workflow_id: z.string().nullable().optional(), s3_key: z.string().nullable().optional(), generated_at: z.number().int().nullable().optional(), prompt_count: z.number().int().nullable().optional(), created_at: z.number().int(), updated_at: z.number().int(), created_by: z.string(), creation_error: 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 Custom Harness * * POST /v1/custom-harnesses/ */ export const createApiV1CustomHarnesses = async (args: CreateApiV1CustomHarnessesArgs, options: CreateApiV1CustomHarnessesOptions) => { 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.team_id != null) query.append("team_id", String(args.team_id)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/custom-harnesses/${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(customHarness, await res.json()) };