import {z} from 'zod' export type GetApiApiV1ContextIdActionsArgs = {context_id: string, actor?: string | undefined, verb?: string | undefined, since?: string | undefined, until?: string | undefined, limit?: number | undefined, cursor?: string | undefined, count?: boolean | undefined}; export type GetApiApiV1ContextIdActionsOptions = {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 actionDetailJson = z.object({actor: z.string(), confidence: z.number(), createdAt: z.string(), id: z.string(), memoryCategory: memoryCategory, object: z.string().nullable().optional(), objectText: z.string().nullable().optional(), occurredAt: z.string().nullable().optional(), source: sourceRefJson.nullable().optional(), summary: z.string(), validFrom: z.string().nullable().optional(), validUntil: z.string().nullable().optional(), verb: 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 actionListResponseJson = z.object({actions: z.array(actionDetailJson), 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 actions (dated events), newest first by write time. `since`/`until` bound the event time. Paginated: follow `page.nextCursor`. Ordered on write time rather than event time because event time is revisable, and a revision under an event-time ordering would move a row across page boundaries mid-walk. * * GET /api/v1/{context_id}/actions */ export const getApiApiV1ContextIdActions = async (args: GetApiApiV1ContextIdActionsArgs, options: GetApiApiV1ContextIdActionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.actor != null) query.append("actor", String(args.actor)) if (args.verb != null) query.append("verb", String(args.verb)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) 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))}/actions${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(actionListResponseJson, await res.json()) };