import {z} from 'zod' export type TextContentBlock = {type: 'text', text: string}; export type Base64AttachmentSource = {type?: 'base64' | undefined, media_type: string, data: string}; export type ImageContentBlock = {type: 'image', source: Base64AttachmentSource}; export type FileContentBlock = {type: 'file', source: Base64AttachmentSource, filename?: (string | null) | undefined}; export type Content = string | Array; export type EntityInput = {text: string, type?: (string | null) | undefined}; export type MemoryItem = {content: Content, timestamp?: (string | string | null) | undefined, context?: (string | null) | undefined, metadata?: ({[key: string]: string} | null) | undefined, document_id?: (string | null) | undefined, entities?: (Array | null) | undefined, resolve_entities?: boolean | undefined, tags?: (Array | null) | undefined, observation_scopes?: ('per_tag' | 'combined' | 'all_combinations' | 'shared' | Array> | null) | undefined, strategy?: (string | null) | undefined, update_mode?: ('replace' | 'append' | null) | undefined}; export type RetainRequest = {items: Array, async?: boolean | undefined, document_tags?: (Array | null) | undefined, operation_id?: (string | null) | undefined}; export type CreateApiV1DefaultBanksBankIdMemoriesArgs = {bank_id: string, authorization?: (string | null) | undefined, body: RetainRequest}; export type CreateApiV1DefaultBanksBankIdMemoriesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const tokenUsage = z.object({input_tokens: z.number().int().optional(), output_tokens: z.number().int().optional(), total_tokens: z.number().int().optional(), cached_tokens: z.number().int().optional(), thoughts_tokens: z.number().int().optional()}); export const retainResponse = z.object({success: z.boolean(), bank_id: z.string(), items_count: z.number().int(), async: z.boolean(), operation_id: z.string().nullable().optional(), operation_ids: z.array(z.string()).nullable().optional(), usage: tokenUsage.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 }; /** * Retain memories * * POST /v1/default/banks/{bank_id}/memories */ export const createApiV1DefaultBanksBankIdMemories = async (args: CreateApiV1DefaultBanksBankIdMemoriesArgs, options: CreateApiV1DefaultBanksBankIdMemoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/default/banks/${encodeURIComponent(String(args.bank_id))}/memories${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(retainResponse, await res.json()) };