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 LabelValue = {value: string, description?: string | undefined}; export type MapFieldInput = {type?: ('text' | 'multi-text' | 'value' | 'multi-values' | 'map') | undefined, description?: string | undefined, values?: Array | undefined, fields?: {[key: string]: MapFieldInput} | undefined}; export type LabelGroupInput = {key: string, description?: string | undefined, type?: ('value' | 'multi-values' | 'text' | 'multi-text' | 'map') | undefined, optional?: boolean | undefined, tag?: boolean | undefined, values?: Array | undefined, fields?: {[key: string]: MapFieldInput} | undefined}; export type DryRunExtractRequest = {content: Content, context?: string | undefined, timestamp?: (string | null) | undefined, agent_name?: (string | null) | undefined, strategy?: (string | null) | undefined, retain_mission?: (string | null) | undefined, retain_extraction_mode?: (string | null) | undefined, retain_custom_instructions?: (string | null) | undefined, retain_extract_causal_links?: (boolean | null) | undefined, retain_chunk_size?: (number | null) | undefined, entity_labels?: (Array | null) | undefined, entities_allow_free_form?: (boolean | null) | undefined, llm_output_language?: (string | null) | undefined}; export type CreateApiV1DefaultBanksBankIdMemoriesDryRunExtractArgs = {bank_id: string, authorization?: (string | null) | undefined, body: DryRunExtractRequest}; export type CreateApiV1DefaultBanksBankIdMemoriesDryRunExtractOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const extractedFactAttachment = z.object({block_index: z.number().int(), type: z.enum(["image", "file"]), media_type: z.string()}); export const extractedFact = z.object({text: z.string(), fact_type: z.string(), occurred_start: z.string().nullable().optional(), occurred_end: z.string().nullable().optional(), entities: z.array(z.string()).optional(), chunk_index: z.number().int().nullable().optional(), attachments: z.array(extractedFactAttachment).optional()}); export const extractionChunk = z.object({text: z.string(), fact_count: z.number().int()}); 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 dryRunExtractionResult = z.object({facts: z.array(extractedFact).optional(), chunks: z.array(extractionChunk).optional(), usage: tokenUsage.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 }; /** * Dry-run fact extraction (preview, no persistence) * * POST /v1/default/banks/{bank_id}/memories/dry-run-extract */ export const createApiV1DefaultBanksBankIdMemoriesDryRunExtract = async (args: CreateApiV1DefaultBanksBankIdMemoriesDryRunExtractArgs, options: CreateApiV1DefaultBanksBankIdMemoriesDryRunExtractOptions) => { 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/dry-run-extract${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(dryRunExtractionResult, await res.json()) };