import {z} from 'zod' export type GetApiApiV1UsersEventSearchUserIdArgs = {user_id: unknown | unknown, query: string, topk?: number | undefined, similarity_threshold?: number | undefined, time_range_in_days?: number | undefined}; export type GetApiApiV1UsersEventSearchUserIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const profileDelta = z.object({content: z.string(), attributes: z.record(z.string(), z.unknown()).nullable()}); export const eventTag = z.object({tag: z.string(), value: z.string()}); export const eventData = z.object({profile_delta: z.array(profileDelta).nullable().optional(), event_tip: z.string().nullable().optional(), event_tags: z.array(eventTag).nullable().optional()}); export const userEventData = z.object({id: z.union([z.string(), z.string()]), event_data: eventData.optional(), created_at: z.string().optional(), updated_at: z.string().optional(), similarity: z.number().nullable().optional()}); export const eventGistData = z.object({content: z.string()}); export const userEventGistData = z.object({id: z.union([z.string(), z.string()]), gist_data: eventGistData.optional(), created_at: z.string().optional(), updated_at: z.string().optional(), similarity: z.number().nullable().optional()}); export const userEventsData = z.object({events: z.array(userEventData), gists: z.array(userEventGistData).optional()}); export const cODE = z.union([z.literal(0), z.literal(400), z.literal(401), z.literal(403), z.literal(404), z.literal(405), z.literal(409), z.literal(422), z.literal(500), z.literal(501), z.literal(502), z.literal(503), z.literal(504), z.literal(520)]); export const userEventsDataResponse = z.object({data: userEventsData.nullable().optional(), errno: cODE.optional(), errmsg: z.string().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 User Events * * GET /api/v1/users/event/search/{user_id} */ export const getApiApiV1UsersEventSearchUserId = async (args: GetApiApiV1UsersEventSearchUserIdArgs, options?: GetApiApiV1UsersEventSearchUserIdOptions) => { const {baseUrl = "https://api.memobase.dev", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.query != null) query.append("query", String(args.query)) if (args.topk != null) query.append("topk", String(args.topk)) if (args.similarity_threshold != null) query.append("similarity_threshold", String(args.similarity_threshold)) if (args.time_range_in_days != null) query.append("time_range_in_days", String(args.time_range_in_days)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/users/event/search/${encodeURIComponent(String(args.user_id))}${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(userEventsDataResponse, await res.json()) };