import {z} from 'zod' export type GetApiApiReposNamespaceRepoNameMetadataResourceArgs = {namespace: string, repo_name: string, resource: string}; export type GetApiApiReposNamespaceRepoNameMetadataResourceOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const entryDataType = z.enum(["dir", "text", "image", "video", "audio", "tabular", "binary"]); 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 dataTypeCount = z.object({count: z.number().int().gte(0), data_type: z.string()}); export const metadataDirImpl = z.object({data_types: z.array(dataTypeCount)}); export const metadataDir = z.object({dir: metadataDirImpl}); export const metadataTextImpl = z.object({num_chars: z.number().int().gte(0), num_lines: z.number().int().gte(0)}); export const metadataText = z.object({text: metadataTextImpl}); export const imgColorSpace = z.enum(["RGB", "RGBA", "Grayscale", "GrayscaleAlpha", "Rgb16", "Rgba16", "Grayscale16", "GrayscaleAlpha16", "Rgb32F", "Rgba32F", "Unknown"]); export const metadataImageImpl = z.object({color_space: imgColorSpace.nullable().optional(), height: z.number().int().gte(0), width: z.number().int().gte(0)}); export const metadataImage = z.object({image: metadataImageImpl}); export const metadataVideoImpl = z.object({height: z.number().int().gte(0), num_seconds: z.number(), width: z.number().int().gte(0)}); export const metadataVideo = z.object({video: metadataVideoImpl}); export const metadataAudioImpl = z.object({num_channels: z.number().int().gte(0), num_seconds: z.number(), sample_rate: z.number().int().gte(0)}); export const metadataAudio = z.object({audio: metadataAudioImpl}); 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 metadataTabularImpl = z.object({height: z.number().int().gte(0), schema: schema, width: z.number().int().gte(0)}); export const metadataTabular = z.object({tabular: metadataTabularImpl}); export const genericMetadata = z.union([metadataDir, metadataText, metadataImage, metadataVideo, metadataAudio, metadataTabular]); export const branch = z.object({commit_id: z.string(), name: z.string()}); export const workspaceView = z.object({commit: commit, created_at: z.string().nullable().optional(), id: z.string(), name: z.string().nullable().optional()}); export const parsedResourceView = z.object({branch: branch.nullable().optional(), commit: commit.nullable().optional(), path: z.string(), resource: z.string(), version: z.string(), workspace: workspaceView.nullable().optional()}); export const metadataEntry: z.ZodType = z.object({children: z.array(z.lazy(() => metadataEntry)).nullable().optional(), data_type: entryDataType, extension: z.string(), filename: z.string(), hash: z.string(), is_dir: z.boolean(), is_queryable: z.boolean().nullable().optional(), latest_commit: commit.nullable().optional(), metadata: genericMetadata.nullable().optional(), mime_type: z.string(), resource: parsedResourceView.nullable().optional(), size: z.number().int().gte(0)}); export const stagedEntryStatus = z.enum(["Added", "Modified", "Removed", "Unmodified"]); export const workspaceChanges = z.object({status: stagedEntryStatus}); export const workspaceMetadataEntry: z.ZodType = z.object({changes: workspaceChanges.nullable().optional(), children: z.array(z.lazy(() => workspaceMetadataEntry)).nullable().optional(), data_type: entryDataType, extension: z.string(), filename: z.string(), hash: z.string(), is_dir: z.boolean(), is_queryable: z.boolean().nullable().optional(), latest_commit: commit.nullable().optional(), metadata: genericMetadata.nullable().optional(), mime_type: z.string(), resource: parsedResourceView.nullable().optional(), size: z.number().int().gte(0)}); export const eMetadataEntry = z.union([metadataEntry, workspaceMetadataEntry]); export const eMetadataEntryResponseView = z.object({oxen_version: z.string().nullable().optional(), status: z.string(), status_message: z.string(), entry: eMetadataEntry}); 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 file metadata * * GET /api/repos/{namespace}/{repo_name}/metadata/{resource} */ export const getApiApiReposNamespaceRepoNameMetadataResource = async (args: GetApiApiReposNamespaceRepoNameMetadataResourceArgs, options?: GetApiApiReposNamespaceRepoNameMetadataResourceOptions) => { 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))}/metadata/${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(eMetadataEntryResponseView, await res.json()) };