import {z} from 'zod' export type GetApiV1RunsArgs = {type?: ('llm' | 'trace' | 'thread') | undefined, search?: string | undefined, models?: Array | undefined, tags?: Array | undefined, tokens?: string | undefined, exportType?: ('csv' | 'jsonl') | undefined, minDuration?: string | undefined, maxDuration?: string | undefined, startTime?: string | undefined, endTime?: string | undefined, parentRunId?: string | undefined, limit?: string | undefined, page?: string | undefined, order?: string | undefined, sortField?: string | undefined, sortDirection?: ('asc' | 'desc') | undefined}; export type GetApiV1RunsOptions = {baseUrl?: string | undefined, ProjectPrivateApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const feedback = z.object({thumb: z.enum(["up", "down"]).optional(), comment: z.string().optional()}); export const run = z.object({id: z.string().optional(), projectId: z.string().optional(), isPublic: z.boolean().optional(), feedback: feedback.optional(), parentFeedback: feedback.optional(), type: z.string().optional(), name: z.string().optional(), createdAt: z.string().optional(), endedAt: z.string().optional(), duration: z.number().optional(), templateVersionId: z.string().optional(), templateSlug: z.string().optional(), cost: z.number().optional(), tokens: z.object({completion: z.number().optional(), prompt: z.number().optional(), total: z.number().optional()}).optional(), tags: z.array(z.string()).optional(), input: z.object({}).optional(), output: z.object({}).optional(), error: z.object({}).optional(), status: z.string().optional(), siblingRunId: z.string().optional(), params: z.object({}).optional(), metadata: z.object({}).optional(), firstMessage: z.object({}).optional(), messagesCount: z.number().int().optional(), user: z.object({id: z.string().optional(), externalId: z.string().optional(), createdAt: z.string().optional(), lastSeen: z.string().optional(), props: z.object({}).optional()}).optional()}); export const getApiV1RunsResponse = z.object({total: z.number().optional(), page: z.number().optional(), limit: z.number().optional(), data: z.array(run).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 runs * * GET /v1/runs */ export const getApiV1Runs = async (args: GetApiV1RunsArgs, options?: GetApiV1RunsOptions) => { const {baseUrl = "https://api.lunary.ai", fetch: fetchFn = fetch, ProjectPrivateApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.search != null) query.append("search", String(args.search)) for (const item of args.models ?? []) query.append("models", String(item)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.tokens != null) query.append("tokens", String(args.tokens)) if (args.exportType != null) query.append("exportType", String(args.exportType)) if (args.minDuration != null) query.append("minDuration", String(args.minDuration)) if (args.maxDuration != null) query.append("maxDuration", String(args.maxDuration)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.parentRunId != null) query.append("parentRunId", String(args.parentRunId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.order != null) query.append("order", String(args.order)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (ProjectPrivateApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ProjectPrivateApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/runs${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(getApiV1RunsResponse, await res.json()) };