import {z} from 'zod' export type TextQuery = {q: string, field?: (string | null) | undefined}; export type SimilarityOrder = 'most_similar' | 'least_similar'; export type VectorQuery = {name: string, record_id?: (string | null) | undefined, value?: (Array | null) | undefined, order?: SimilarityOrder | undefined}; export type Query = {text?: (TextQuery | null) | undefined, vector?: (VectorQuery | null) | undefined}; export type RecordFilterScope = {entity: 'record', property: 'id' | 'external_id' | 'inserted_at' | 'updated_at' | 'status'}; export type ResponseFilterScope = {entity: 'response', question?: (string | null) | undefined, property?: ('status' | null) | undefined}; export type SuggestionFilterScope = {entity: 'suggestion', question: string, property?: ('value' | 'score' | 'agent' | 'type' | null) | undefined}; export type MetadataFilterScope = {entity: 'metadata', metadata_property: string}; export type TermsFilter = {type: 'terms', scope: RecordFilterScope | ResponseFilterScope | SuggestionFilterScope | MetadataFilterScope, values: Array}; export type RangeFilter = {type: 'range', scope: RecordFilterScope | ResponseFilterScope | SuggestionFilterScope | MetadataFilterScope, ge?: (number | string | null) | undefined, le?: (number | string | null) | undefined}; export type Filters = {and?: (Array | null) | undefined}; export type SortOrder = 'asc' | 'desc'; export type Order = {scope: RecordFilterScope | ResponseFilterScope | SuggestionFilterScope | MetadataFilterScope, order: SortOrder}; export type SearchRecordsQuery = {query?: (Query | null) | undefined, filters?: (Filters | null) | undefined, sort?: (Array | null) | undefined}; export type CreateApiMeDatasetsDatasetIdRecordsSearchArgs = {dataset_id: string, offset?: number | undefined, limit?: number | undefined, include?: (Array | null) | undefined, body: SearchRecordsQuery}; export type CreateApiMeDatasetsDatasetIdRecordsSearchOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const recordStatus = z.enum(["pending", "completed"]); export const responseValue = z.object({value: z.unknown()}); export const responseStatus = z.enum(["draft", "submitted", "discarded"]); export const response = z.object({id: z.string(), values: z.record(z.string(), responseValue).nullable().optional(), status: responseStatus, record_id: z.string(), user_id: z.string(), inserted_at: z.string(), updated_at: z.string()}); export const suggestionType = z.enum(["model", "human", "selection"]); export const suggestion = z.object({question_id: z.string(), type: suggestionType.nullable().optional(), value: z.unknown(), agent: z.string().nullable().optional(), score: z.union([z.number(), z.array(z.number())]).nullable().optional(), id: z.string(), inserted_at: z.string(), updated_at: z.string()}); export const record = z.object({id: z.string(), status: recordStatus, fields: z.record(z.string(), z.unknown()), metadata: z.record(z.string(), z.unknown()).nullable().optional(), external_id: z.string().nullable().optional(), reference: z.string().nullable().optional(), responses: z.array(response).nullable().optional(), suggestions: z.array(suggestion).nullable().optional(), vectors: z.record(z.string(), z.array(z.number())).nullable().optional(), dataset_id: z.string(), inserted_at: z.string(), updated_at: z.string()}); export const searchRecord = z.object({record: record, query_score: z.number().nullable().optional()}); export const searchRecordsResult = z.object({items: z.array(searchRecord), total: z.number().int().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 }; /** * Search Current User Dataset Records * * POST /me/datasets/{dataset_id}/records/search * * Also accepts auth: HTTPBearer */ export const createApiMeDatasetsDatasetIdRecordsSearch = async (args: CreateApiMeDatasetsDatasetIdRecordsSearchArgs, options: CreateApiMeDatasetsDatasetIdRecordsSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.include ?? []) query.append("include", String(item)) if (APIKeyHeader !== undefined) headers.set("X-Extralit-Api-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/me/datasets/${encodeURIComponent(String(args.dataset_id))}/records/search${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(searchRecordsResult, await res.json()) };