import {z} from 'zod' export type TestRunSampleBody = {input: unknown, metadata: unknown, rowId?: (string | null) | undefined}; export type CreateApiApiOrgIdTasksEntityIdTestRunArgs = {org_id: string, entity_id: string, body: {samples: Array}}; export type CreateApiApiOrgIdTasksEntityIdTestRunOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const testRunRowResultBody = z.object({attempts: z.number().int().gte(0), error: z.string().nullable().optional(), httpStatus: z.number().int().gte(0).nullable().optional(), input: z.unknown(), latencyMs: z.number().int(), parsedOutput: z.unknown(), rawRequest: z.string(), rawResponse: z.string(), rowId: z.string(), status: z.string()}); export const createApiApiOrgIdTasksEntityIdTestRunResponse = z.object({results: z.array(testRunRowResultBody)}); 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 }; /** * Try a remote task against a sample * * POST /api/{org_id}/tasks/{entity_id}/test_run */ export const createApiApiOrgIdTasksEntityIdTestRun = async (args: CreateApiApiOrgIdTasksEntityIdTestRunArgs, options: CreateApiApiOrgIdTasksEntityIdTestRunOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/tasks/${encodeURIComponent(String(args.entity_id))}/test_run${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(createApiApiOrgIdTasksEntityIdTestRunResponse, await res.json()) };