import {z} from 'zod' export type GetApiDiV1LogsModelIdLoglinesArgs = {model_id: unknown, searchType?: ('TERM' | 'NESTED' | 'MATCH') | undefined, since_time?: unknown | undefined, pagesize?: number | undefined, pos?: number | undefined, version?: string | undefined}; export type GetApiDiV1LogsModelIdLoglinesOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const v1MetaInfo = z.object({training_id: z.string().optional(), user_id: z.string().optional(), time: z.number().optional(), rindex: z.number().optional()}); export const v1LogLine = z.object({meta: v1MetaInfo.optional(), line: z.string().optional()}); export const v1LogLinesList = z.object({models: z.array(v1LogLine).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 }; /** * Get loglines, based on query * * GET /di/v1/logs/{model_id}/loglines * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const getApiDiV1LogsModelIdLoglines = async (args: GetApiDiV1LogsModelIdLoglinesArgs, options?: GetApiDiV1LogsModelIdLoglinesOptions) => { const {baseUrl = "https://di.prophecis.com", fetch: fetchFn = fetch, basicAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.searchType != null) query.append("searchType", String(args.searchType)) if (args.since_time != null) query.append("since_time", String(args.since_time)) if (args.pagesize != null) query.append("pagesize", String(args.pagesize)) if (args.pos != null) query.append("pos", String(args.pos)) if (args.version != null) query.append("version", String(args.version)) if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v1/logs/${encodeURIComponent(String(args.model_id))}/loglines${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(v1LogLinesList, await res.json()) };