import {z} from 'zod' export type GetApiApiV1ContextIdEntitiesArgs = {context_id: string, type?: string | undefined, limit?: number | undefined, cursor?: string | undefined, count?: boolean | undefined, offset?: number | undefined}; export type GetApiApiV1ContextIdEntitiesOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const memoryCategory = z.enum(["identity", "knowledge", "context"]); export const entityDetailJson = z.object({createdAt: z.string(), entityType: z.string(), id: z.string(), importance: z.number(), memoryCategory: memoryCategory, name: z.string(), updatedAt: z.string()}); export const pageMeta = z.object({hasMore: z.boolean(), nextCursor: z.string().nullable().optional(), totalSize: z.number().int().gte(0).nullable().optional()}); export const entityListResponseJson = z.object({entities: z.array(entityDetailJson), page: pageMeta}); 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 }; /** * List entities (optionally filtered by type), ordered by type then name. Paginated: `limit` defaults to 100 and is capped at 500; follow `page.nextCursor` to walk the Context. * * GET /api/v1/{context_id}/entities */ export const getApiApiV1ContextIdEntities = async (args: GetApiApiV1ContextIdEntitiesArgs, options: GetApiApiV1ContextIdEntitiesOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.context_id))}/entities${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(entityListResponseJson, await res.json()) };