import {z} from 'zod' export type Event = {type?: ('llm' | 'chain' | 'agent' | 'tool' | 'log' | 'embed' | 'retriever' | 'chat' | 'convo' | 'message' | 'thread') | undefined, event?: string | undefined, level?: string | undefined, runId?: string | undefined, parentRunId?: string | undefined, timestamp?: string | undefined, input?: Record | undefined, tags?: Array | undefined, name?: string | undefined, output?: Record | undefined, message?: (string | Record) | undefined, extra?: Record | undefined, feedback?: Record | undefined, templateId?: string | undefined, templateVersionId?: string | undefined, metadata?: Record | undefined, tokensUsage?: ({prompt?: number | undefined, completion?: number | undefined}) | undefined, error?: ({message?: string | undefined, stack?: string | undefined}) | undefined, appId?: string | undefined} | {[key: string]: unknown}; export type CreateApiV1RunsIngestArgs = {body: {events?: (Event | Array) | undefined}}; export type CreateApiV1RunsIngestOptions = {baseUrl?: string | undefined, ProjectPublicApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ingestResponse = z.object({results: z.array(z.object({id: z.string().optional(), success: z.boolean().optional(), error: z.string().optional()})).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 }; /** * Ingest run events * * POST /v1/runs/ingest */ export const createApiV1RunsIngest = async (args: CreateApiV1RunsIngestArgs, options?: CreateApiV1RunsIngestOptions) => { const {baseUrl = "https://api.lunary.ai", fetch: fetchFn = fetch, ProjectPublicApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ProjectPublicApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ProjectPublicApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/runs/ingest${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(ingestResponse, await res.json()) };