import {z} from 'zod' export type GetApiApiV1WatchesWatchIdChangesChangeIdDiffArgs = {watch_id: string, change_id: number, 'Idempotency-Key'?: (string | null) | undefined}; export type GetApiApiV1WatchesWatchIdChangesChangeIdDiffOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const documentSideResponse = z.object({chunkCount: z.number().int(), expectedChunks: z.number().int().nullable().optional(), wordCount: z.number().int(), expectedWords: z.number().int().nullable().optional(), isComplete: z.boolean(), versionSha1: z.string().nullable().optional(), reconstructedFromCurrent: z.boolean().optional(), reconstructedChars: z.number().int().optional()}); export const changeDiffResponse = z.object({changeId: z.number().int(), changeKind: z.string(), beforeText: z.string().nullable().optional(), afterText: z.string().nullable().optional(), hasBefore: z.boolean(), hasAfter: z.boolean(), diffAvailable: z.boolean(), unavailableReason: z.enum(["neither_side_available", "before_text_unavailable", "after_text_unavailable", "sides_identical"]).nullable().optional(), before: documentSideResponse.nullable().optional(), after: documentSideResponse.nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional()}); export const responseMeta = z.object({processingTimeMs: z.number(), creditsConsumed: z.number(), cursor: z.number().int().nullable().optional(), hasMore: z.boolean().nullable().optional()}); export const changeDiffEnvelope = z.object({data: changeDiffResponse, meta: responseMeta}); 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 }; /** * Old-vs-new text for one change * * GET /api/v1/watches/{watch_id}/changes/{change_id}/diff * * Also accepts auth: ApiKeyHeader */ export const getApiApiV1WatchesWatchIdChangesChangeIdDiff = async (args: GetApiApiV1WatchesWatchIdChangesChangeIdDiffArgs, options?: GetApiApiV1WatchesWatchIdChangesChangeIdDiffOptions) => { const {baseUrl = "https://api.vaquill.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/watches/${encodeURIComponent(String(args.watch_id))}/changes/${encodeURIComponent(String(args.change_id))}/diff${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(changeDiffEnvelope, await res.json()) };