import {z} from 'zod' export type GetApiApiPreviewCapsulesIdVersionsCompareArgs = {id: string, from: string, to?: string | undefined}; export type GetApiApiPreviewCapsulesIdVersionsCompareOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiPreviewCapsulesIdVersionsCompareResponse = z.object({capsule_id: z.string().max(36), changes: z.object({body: z.union([z.object({additions: z.number().int().gte(0).lte(9007199254740991), deletions: z.number().int().gte(0).lte(9007199254740991), rows: z.array(z.union([z.object({kind: z.enum(["add", "remove", "context"]), text: z.string().max(100000)}).and(z.record(z.string(), z.unknown())), z.object({count: z.number().int().gte(0).lte(9007199254740991), kind: z.literal("skipped")}).and(z.record(z.string(), z.unknown()))])), status: z.literal("diffed")}).and(z.record(z.string(), z.unknown())), z.object({after_line_count: z.number().int().gte(0).lte(9007199254740991), before_line_count: z.number().int().gte(0).lte(9007199254740991), status: z.literal("too_large")}).and(z.record(z.string(), z.unknown()))]).nullable(), fields: z.array(z.object({after: z.string().max(100000), before: z.string().max(100000), change: z.enum(["added", "removed", "changed"]), name: z.string().max(200)}).and(z.record(z.string(), z.unknown()))), meta: z.union([z.object({changes: z.array(z.object({after: z.string().max(100000), before: z.string().max(100000), change: z.enum(["added", "removed", "changed"]), group: z.enum(["prompt", "agent"]), name: z.string().max(200)}).and(z.record(z.string(), z.unknown()))), status: z.literal("compared")}).and(z.record(z.string(), z.unknown())), z.object({may_have_changed: z.boolean(), sides: z.array(z.enum(["from", "to"])), status: z.literal("not_captured")}).and(z.record(z.string(), z.unknown()))]), summary: z.object({after: z.string().max(100000).nullable(), before: z.string().max(100000).nullable()}).and(z.record(z.string(), z.unknown())).nullable(), title: z.object({after: z.string().max(100000), before: z.string().max(100000)}).and(z.record(z.string(), z.unknown())).nullable()}).and(z.record(z.string(), z.unknown())), from_version_num: z.number().int().gt(0).lte(9007199254740991), to_version_num: z.number().int().gt(0).lte(9007199254740991), unchanged: z.boolean()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Compare two versions of a capsule: title, summary, body lines and field values. `to` defaults to the newest version you can see. Needs a plan with revision history. * * GET /api/preview/capsules/{id}/versions/compare * * Also accepts auth: cookieAuth, oauth2 */ export const getApiApiPreviewCapsulesIdVersionsCompare = async (args: GetApiApiPreviewCapsulesIdVersionsCompareArgs, options: GetApiApiPreviewCapsulesIdVersionsCompareOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/capsules/${encodeURIComponent(String(args.id))}/versions/compare${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiPreviewCapsulesIdVersionsCompareResponse, await res.json()) };