import {z} from 'zod' export type GetApiApiReposNamespaceRepoNameDataFramesResourceArgs = {namespace: string, repo_name: string, resource: string, columns: string | null, delimiter: string | null, find_embedding_where: string | null, filter: string | null, output: string | null, output_column: string | null, page_size: number | null, page: number | null, row: number | null, randomize: boolean | null, reverse: boolean | null, slice: string | null, sort_by: string | null, sort_by_similarity_to: string | null, sql: string | null, take: string | null}; export type GetApiApiReposNamespaceRepoNameDataFramesResourceOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commit = z.object({author: z.string(), email: z.string(), id: z.string(), message: z.string(), parent_ids: z.array(z.string()), timestamp: z.string()}); export const previousField = z.object({dtype: z.string(), metadata: z.unknown(), name: z.string()}); export const changes = z.object({previous: previousField.nullable().optional(), status: z.string()}); export const field = z.object({changes: changes.nullable().optional(), dtype: z.string(), metadata: z.unknown(), name: z.string()}); export const schema = z.object({fields: z.array(field), hash: z.string(), metadata: z.unknown()}); export const dataFrameSize = z.object({height: z.number().int().gte(0), width: z.number().int().gte(0)}); export const dataFrameSchemaSize = z.object({schema: schema, size: dataFrameSize}); export const dFOptView = z.object({name: z.string(), value: z.unknown()}); export const pagination = z.object({page_number: z.number().int().gte(0), page_size: z.number().int().gte(0), total_entries: z.number().int().gte(0), total_pages: z.number().int().gte(0)}); export const jsonDataFrameView = z.object({opts: z.array(dFOptView), data: z.unknown(), pagination: pagination, schema: schema, size: dataFrameSize}); export const jsonDataFrameViews = z.object({source: dataFrameSchemaSize, view: jsonDataFrameView}); export const dFResourceType = z.enum(["compare", "diff", "query"]); export const derivedDFResource = z.object({path: z.string(), resource_id: z.string(), resource_type: dFResourceType}); export const resourceVersion = z.object({path: z.string(), version: z.string()}); export const jsonDataFrameViewResponse = z.object({oxen_version: z.string().nullable().optional(), status: z.string(), status_message: z.string(), commit: commit.nullable().optional(), data_frame: jsonDataFrameViews, derived_resource: derivedDFResource.nullable().optional(), resource: resourceVersion.nullable().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 data frame slice * * GET /api/repos/{namespace}/{repo_name}/data_frames/{resource} */ export const getApiApiReposNamespaceRepoNameDataFramesResource = async (args: GetApiApiReposNamespaceRepoNameDataFramesResourceArgs, options?: GetApiApiReposNamespaceRepoNameDataFramesResourceOptions) => { const {baseUrl = "https://hub.oxen.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/repos/${encodeURIComponent(String(args.namespace))}/${encodeURIComponent(String(args.repo_name))}/data_frames/${encodeURIComponent(String(args.resource))}${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(jsonDataFrameViewResponse, await res.json()) };