import {z} from 'zod' export type EvalBacktestContinuation = {cursor: string, sourceHash: string, reservationId: string, draftHash: string}; export type EvalBacktestDraft = {continuation?: EvalBacktestContinuation | undefined, assertions: {mode: 'replace' | 'extend' | 'inherit', list: Array>}, matchOptions?: (Record | null) | undefined}; export type CreateApiProjectsProjectIdEvalRunsRunIdBacktestArgs = {'x-mcpjam-eval-vocabulary'?: ('1' | '2') | undefined, projectId: string, runId: string, body: EvalBacktestDraft}; export type CreateApiProjectsProjectIdEvalRunsRunIdBacktestOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const evalBacktestContinuation = z.object({cursor: z.string().min(1).max(8192), sourceHash: z.string().min(1).max(256), reservationId: z.string().min(1).max(256), draftHash: z.string().min(1).max(256)}); export const evalBacktestReport = z.object({schemaVersion: z.unknown(), sourceRunId: z.string(), sourceHash: z.string(), draftHash: z.string(), configRevision: z.unknown(), complete: z.boolean(), continuationAvailable: z.boolean(), continuation: evalBacktestContinuation.optional(), counts: z.object({iterations: z.number().int().gte(0).optional(), comparable: z.number().int().gte(0).optional(), ungradable: z.number().int().gte(0).optional(), flipped: z.number().int().gte(0).optional()}), differences: z.array(z.object({})), modelUse: z.literal("none")}); 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 }; /** * Preview deterministic draft evaluators against stored evidence * * POST /projects/{projectId}/eval-runs/{runId}/backtest */ export const createApiProjectsProjectIdEvalRunsRunIdBacktest = async (args: CreateApiProjectsProjectIdEvalRunsRunIdBacktestArgs, options?: CreateApiProjectsProjectIdEvalRunsRunIdBacktestOptions) => { const {baseUrl = "https://app.mcpjam.com/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-mcpjam-eval-vocabulary'] != null) headers.set("x-mcpjam-eval-vocabulary", String(args['x-mcpjam-eval-vocabulary'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.projectId))}/eval-runs/${encodeURIComponent(String(args.runId))}/backtest${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(evalBacktestReport, await res.json()) };