import {z} from 'zod' export type Budget = 'low' | 'mid' | 'high'; export type FactsIncludeOptions = Record; export type ToolCallsIncludeOptions = {output?: boolean | undefined}; export type ReflectIncludeOptions = {facts?: (FactsIncludeOptions | null) | undefined, tool_calls?: (ToolCallsIncludeOptions | null) | undefined}; export type TagGroupLeaf = {tags: Array, match?: ('any' | 'all' | 'any_strict' | 'all_strict' | 'exact') | undefined, resolve?: ('exact' | 'fuzzy') | undefined}; export type TagGroupNotInput = {not: TagGroupLeaf | TagGroupAndInput | TagGroupOrInput | TagGroupNotInput}; export type TagGroupOrInput = {or: Array}; export type TagGroupAndInput = {and: Array}; export type ReflectRequest = {reflect_search_observations_max_tokens?: (number | null) | undefined, reflect_search_observations_include_entities?: (boolean | null) | undefined, reflect_search_mental_models_max_tokens?: (number | null) | undefined, query: string, budget?: Budget | undefined, context?: (string | null) | undefined, max_tokens?: number | undefined, include?: ReflectIncludeOptions | undefined, response_schema?: ({[key: string]: unknown} | null) | undefined, tags?: (Array | null) | undefined, tags_match?: ('any' | 'all' | 'any_strict' | 'all_strict' | 'exact') | undefined, tag_groups?: (Array | null) | undefined, apply_all_directives?: boolean | undefined, fact_types?: (Array<'world' | 'experience' | 'observation'> | null) | undefined, exclude_mental_models?: boolean | undefined, exclude_mental_model_ids?: (Array | null) | undefined}; export type CreateApiV1DefaultBanksBankIdReflectArgs = {bank_id: string, authorization?: (string | null) | undefined, body: ReflectRequest}; export type CreateApiV1DefaultBanksBankIdReflectOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const chunkAttachment = z.object({id: z.string(), hash: z.string(), kind: z.string(), media_type: z.string(), byte_size: z.number().int(), filename: z.string().nullable().optional(), url: z.string()}); export const reflectFact = z.object({id: z.string().nullable().optional(), text: z.string(), type: z.string().nullable().optional(), context: z.string().nullable().optional(), occurred_start: z.string().nullable().optional(), occurred_end: z.string().nullable().optional(), mentioned_at: z.string().nullable().optional(), document_id: z.string().nullable().optional(), chunk_id: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), attachments: z.array(chunkAttachment).nullable().optional()}); export const reflectMentalModel = z.object({id: z.string(), text: z.string(), context: z.string().nullable().optional()}); export const reflectDirective = z.object({id: z.string(), name: z.string(), content: z.string()}); export const reflectBasedOn = z.object({memories: z.array(reflectFact).optional(), mental_models: z.array(reflectMentalModel).optional(), directives: z.array(reflectDirective).optional()}); 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 reflectToolCall = z.object({tool: z.string(), input: z.record(z.string(), z.unknown()), output: z.record(z.string(), z.unknown()).nullable().optional(), duration_ms: z.number().int(), iteration: z.number().int().optional()}); export const reflectLLMCall = z.object({scope: z.string(), duration_ms: z.number().int()}); export const reflectTrace = z.object({tool_calls: z.array(reflectToolCall).optional(), llm_calls: z.array(reflectLLMCall).optional()}); export const reflectResponse = z.object({text: z.string(), based_on: reflectBasedOn.nullable().optional(), structured_output: z.record(z.string(), z.unknown()).nullable().optional(), structured_output_error: z.string().nullable().optional(), usage: tokenUsage.nullable().optional(), trace: reflectTrace.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 }; /** * Reflect and generate answer * * POST /v1/default/banks/{bank_id}/reflect */ export const createApiV1DefaultBanksBankIdReflect = async (args: CreateApiV1DefaultBanksBankIdReflectArgs, options: CreateApiV1DefaultBanksBankIdReflectOptions) => { 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))}/reflect${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(reflectResponse, await res.json()) };