import {z} from 'zod' export type ParseRequest = {lines: Array, service?: (string | null) | undefined, source?: (string | null) | undefined}; export type CreateApiV1ParseArgs = {body: ParseRequest}; export type CreateApiV1ParseOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const templateOut = z.object({template_id: z.string(), raw_template: z.string()}); export const parsedLineOut = z.object({line: z.string(), template_id: z.string().nullable().optional(), raw_template: z.string().nullable().optional(), cache_hit: z.boolean().optional()}); export const parseStats = z.object({llm_calls: z.number().int(), cache_hits: z.number().int(), unmatched: z.number().int(), elapsed_ms: z.number().int(), incident_family_hits: z.number().int().optional(), incident_induced_templates: z.number().int().optional(), global_cache_hits: z.number().int().optional(), global_shadow_hits: z.number().int().optional(), global_shadow_agreements: z.number().int().optional(), global_shadow_disagreements: z.number().int().optional(), total_patterns: z.number().int().optional(), candidate_patterns: z.number().int().optional(), dropped_patterns: z.number().int().optional(), cache_pattern_hits: z.number().int().optional(), global_cache_pattern_hits: z.number().int().optional()}); export const parseResponse = z.object({templates: z.array(templateOut), parsed: z.array(parsedLineOut), stats: parseStats}); 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 }; /** * Parse Endpoint * * POST /v1/parse */ export const createApiV1Parse = async (args: CreateApiV1ParseArgs, options: CreateApiV1ParseOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/parse${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(parseResponse, await res.json()) };