import {z} from 'zod' export type GetApiSnapshotsSnapshotIdHealthDocDriftArgs = {snapshot_id: string, min_confidence?: number | undefined, kind?: (string | null) | undefined, document?: (string | null) | undefined, limit?: number | undefined}; export type GetApiSnapshotsSnapshotIdHealthDocDriftOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const docDriftFindingResponse = z.object({id: z.string(), file_path: z.string(), line_number: z.number().int(), kind: z.string(), target: z.string(), confidence: z.number(), origin: z.string(), reason: z.string(), raw: z.string(), context: z.string(), evidence: z.array(z.string()), fingerprint: z.string(), suggestion: z.string().nullable().optional(), suggestion_basis: z.string().nullable().optional(), first_seen_at: z.string().nullable().optional(), is_new: z.boolean().optional()}); export const docDriftSummaryResponse = z.object({findings_total: z.number().int(), documents: z.number().int(), confidence: z.record(z.string(), z.number().int()), by_kind: z.record(z.string(), z.number().int()), findings_basis: z.string(), new_since_last_update: z.number().int().nullable().optional()}); export const docDriftResponse = z.object({findings: z.array(docDriftFindingResponse), findings_emitted: z.number().int(), summary: docDriftSummaryResponse.nullable(), unavailable: z.enum(["not_computed", "index_predates_doc_drift", "drift_read_failed"]).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 }; /** * List Doc Drift * * GET /snapshots/{snapshot_id}/health/doc-drift */ export const getApiSnapshotsSnapshotIdHealthDocDrift = async (args: GetApiSnapshotsSnapshotIdHealthDocDriftArgs, options: GetApiSnapshotsSnapshotIdHealthDocDriftOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.min_confidence != null) query.append("min_confidence", String(args.min_confidence)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.document != null) query.append("document", String(args.document)) if (args.limit != null) query.append("limit", String(args.limit)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/snapshots/${encodeURIComponent(String(args.snapshot_id))}/health/doc-drift${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(docDriftResponse, await res.json()) };