import {z} from 'zod' export type LearningCreate = {learning_type: string, content: {[key: string]: unknown}, namespace?: (string | null) | undefined, user_id?: (string | null) | undefined, agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, session_id?: (string | null) | undefined, entity_id?: (string | null) | undefined, entity_type?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiLearningsArgs = {db_id?: (string | null) | undefined, table?: (string | null) | undefined, body: LearningCreate}; export type CreateApiLearningsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const learningResponse = z.object({learning_id: z.string(), learning_type: z.string(), namespace: z.string().nullable().optional(), user_id: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), session_id: z.string().nullable().optional(), entity_id: z.string().nullable().optional(), entity_type: z.string().nullable().optional(), content: z.record(z.string(), z.unknown()).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.number().int().nullable().optional(), updated_at: z.number().int().nullable().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 }; /** * Create Learning * * POST /learnings */ export const createApiLearnings = async (args: CreateApiLearningsArgs, options: CreateApiLearningsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/learnings${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(learningResponse, await res.json()) };