import {z} from 'zod' export type GetApiIncidentsArgs = {sensorId?: string | undefined, place?: string | undefined, objectId?: string | undefined, fromTimestamp?: string | undefined, toTimestamp?: string | undefined, category?: string | undefined, queryString?: string | undefined, vlmVerified?: boolean | undefined, vlmVerdict?: ('all' | 'confirmed' | 'rejected' | 'verification-failed' | 'not-confirmed') | undefined, maxResultSize?: number | undefined}; export type GetApiIncidentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const place = z.object({name: z.string().max(10000), info: z.record(z.string(), z.string().max(10000)).optional()}); export const analyticsModule = z.object({id: z.string().max(10000).optional(), description: z.string().max(10000).optional(), version: z.string().max(10000).optional(), source: z.string().max(10000).optional(), info: z.record(z.string(), z.string().max(10000)).optional()}); export const incident = z.object({timestamp: z.string().max(24), Id: z.string().max(10000), end: z.string().max(24), category: z.string().max(10000).optional(), sensorId: z.string().max(10000).optional(), place: place, objectIds: z.array(z.string().max(10000)).optional(), frameIds: z.array(z.string().max(10000)).optional(), info: z.record(z.string(), z.string().max(10000)).optional(), analyticsModule: analyticsModule.optional()}); export const incidents = z.object({incidents: z.array(incident)}); 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 }; /** * Get incidents * * GET /incidents */ export const getApiIncidents = async (args: GetApiIncidentsArgs, options?: GetApiIncidentsOptions) => { const {baseUrl = "https://ai.nvidia.com/api/v3.3/analytics", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sensorId != null) query.append("sensorId", String(args.sensorId)) if (args.place != null) query.append("place", String(args.place)) if (args.objectId != null) query.append("objectId", String(args.objectId)) if (args.fromTimestamp != null) query.append("fromTimestamp", String(args.fromTimestamp)) if (args.toTimestamp != null) query.append("toTimestamp", String(args.toTimestamp)) if (args.category != null) query.append("category", String(args.category)) if (args.queryString != null) query.append("queryString", String(args.queryString)) if (args.vlmVerified != null) query.append("vlmVerified", String(args.vlmVerified)) if (args.vlmVerdict != null) query.append("vlmVerdict", String(args.vlmVerdict)) if (args.maxResultSize != null) query.append("maxResultSize", String(args.maxResultSize)) const url = `${baseUrl.replace(/\/+$/, '')}/incidents${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(incidents, await res.json()) };