import {z} from 'zod' export type GetApiApiV1ContextIdAttributesArgs = {context_id: string, entity?: string | undefined, key?: string | undefined, limit?: number | undefined, cursor?: string | undefined, count?: boolean | undefined}; export type GetApiApiV1ContextIdAttributesOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const memoryCategory = z.enum(["identity", "knowledge", "context"]); export const sourceRefJson = z.object({kind: z.string(), ref: z.string().nullable().optional(), sessionId: z.string().nullable().optional()}); export const attributeDetailJson = z.object({createdAt: z.string(), entity: z.string(), id: z.string(), importance: z.number(), key: z.string(), memoryCategory: memoryCategory, source: sourceRefJson.nullable().optional(), supersededBy: z.string().nullable().optional(), supersedes: z.string().nullable().optional(), validFrom: z.string().nullable().optional(), validUntil: z.string().nullable().optional(), value: 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 attributeListResponseJson = z.object({attributes: z.array(attributeDetailJson), 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 live attributes, newest first. Live means not superseded and inside its validity window; superseded values are reachable through the entity history endpoint. Paginated: follow `page.nextCursor`. * * GET /api/v1/{context_id}/attributes */ export const getApiApiV1ContextIdAttributes = async (args: GetApiApiV1ContextIdAttributesArgs, options: GetApiApiV1ContextIdAttributesOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.entity != null) query.append("entity", String(args.entity)) if (args.key != null) query.append("key", String(args.key)) 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 (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/${encodeURIComponent(String(args.context_id))}/attributes${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(attributeListResponseJson, await res.json()) };