import {z} from 'zod' export type StoreCopyParams = {name: string, description?: (string | null) | undefined, metadata: unknown, tags?: (Array | null) | undefined}; export type CreateApiV1StoresStoreIdentifierCopyArgs = {store_identifier: string | string, body: StoreCopyParams}; export type CreateApiV1StoresStoreIdentifierCopyOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const contextualizationConfig = z.object({with_metadata: z.union([z.boolean(), z.array(z.string())]).optional(), with_file_context: z.boolean().optional()}); export const lsfStoreConfig = z.object({}); export const storeConfig = z.object({contextualization: z.union([z.boolean(), contextualizationConfig]).optional(), lsf: lsfStoreConfig.nullable().optional()}); export const fileCounts = z.object({pending: z.number().int().optional(), in_progress: z.number().int().optional(), cancelled: z.number().int().optional(), completed: z.number().int().optional(), failed: z.number().int().optional(), total: z.number().int().optional()}); export const expiresAfter = z.object({anchor: z.literal("last_active_at").optional(), days: z.number().int().optional()}); export const storeStatus = z.enum(["expired", "in_progress", "completed", "failed"]); export const storeCopyInfo = z.object({role: z.enum(["source", "target"]), status: z.enum(["in_progress", "failed"]), peer_store_id: z.string(), started_at: z.string(), error: z.string().nullable().optional()}); export const store = z.object({id: z.string(), name: z.string().min(1).max(128), description: z.string().max(256).nullable().optional(), is_public: z.boolean().optional(), license: z.string().nullable().optional(), metadata: z.unknown(), tags: z.array(z.string()).optional(), config: storeConfig.nullable().optional(), bucket_id: z.string().nullable().optional(), file_counts: fileCounts.optional(), expires_after: expiresAfter.nullable().optional(), status: storeStatus.optional(), created_at: z.string(), updated_at: z.string(), last_active_at: z.string().nullable().optional(), usage_bytes: z.number().int().optional(), usage_tokens: z.number().int().optional(), expires_at: z.string().nullable().optional(), copy_state: storeCopyInfo.nullable().optional(), object: z.literal("store").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 }; /** * Copy a store * * POST /v1/stores/{store_identifier}/copy */ export const createApiV1StoresStoreIdentifierCopy = async (args: CreateApiV1StoresStoreIdentifierCopyArgs, options?: CreateApiV1StoresStoreIdentifierCopyOptions) => { const {baseUrl = "https://api.mixedbread.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stores/${encodeURIComponent(String(args.store_identifier))}/copy${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(store, await res.json()) };