import {z} from 'zod' export type FieldWrapper = {empty?: boolean | undefined}; export type CreateApiKgV3EnhanceBulkArgs = {name?: string | undefined, size?: number | undefined, threshold?: number | undefined, refresh?: boolean | undefined, search?: boolean | undefined, nonCanonicalFacts?: boolean | undefined, jsonmode?: (' ' | 'extended') | undefined, webhookurl?: string | undefined, body: Array<{type?: string | undefined, location?: string | undefined, ip?: string | undefined, phone?: string | undefined, description?: string | undefined} | {type?: string | undefined, location?: string | undefined, ip?: string | undefined, phone?: string | undefined, description?: string | undefined, email?: FieldWrapper | undefined, employer?: string | undefined, title?: string | undefined, school?: string | undefined} | {id?: string | undefined}>}; export type CreateApiKgV3EnhanceBulkOptions = {baseUrl?: string | undefined, tokenscheme?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkjobAccepted = z.object({job_id: z.string().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 }; /** * Create a Bulkjob * * POST /kg/v3/enhance/bulk */ export const createApiKgV3EnhanceBulk = async (args: CreateApiKgV3EnhanceBulkArgs, options?: CreateApiKgV3EnhanceBulkOptions) => { const {baseUrl = "https://llm.diffbot.com/api/v1/web_search", fetch: fetchFn = fetch, tokenscheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.size != null) query.append("size", String(args.size)) if (args.threshold != null) query.append("threshold", String(args.threshold)) if (args.refresh != null) query.append("refresh", String(args.refresh)) if (args.search != null) query.append("search", String(args.search)) if (args.nonCanonicalFacts != null) query.append("nonCanonicalFacts", String(args.nonCanonicalFacts)) if (args.jsonmode != null) query.append("jsonmode", String(args.jsonmode)) if (args.webhookurl != null) query.append("webhookurl", String(args.webhookurl)) if (tokenscheme !== undefined) query.set("token", tokenscheme) const url = `${baseUrl.replace(/\/+$/, '')}/kg/v3/enhance/bulk${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(bulkjobAccepted, await res.json()) };