import {z} from 'zod' export type PromptPreviewRequest = {operation?: ('retain' | 'consolidation' | 'reflect') | undefined, strategy?: (string | null) | undefined}; export type CreateApiV1DefaultBanksBankIdPromptsPreviewArgs = {bank_id: string, authorization?: (string | null) | undefined, body: PromptPreviewRequest}; export type CreateApiV1DefaultBanksBankIdPromptsPreviewOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const promptBlockModel = z.object({text: z.string(), source: z.enum(["config", "builtin"]), field: z.string().optional(), section: z.string().optional(), heading: z.string().optional(), active: z.boolean().optional(), value: z.string().nullable().optional(), kind: z.enum(["text", "boolean", "choice", "complex"]), choices: z.array(z.string()).nullable().optional(), editable: z.boolean().optional()}); export const promptMessageModel = z.object({role: z.enum(["system", "user"]), blocks: z.array(promptBlockModel).optional()}); export const runSettingModel = z.object({field: z.string(), value: z.string().nullable().optional(), kind: z.enum(["text", "boolean", "choice", "complex"]), editable: z.boolean().optional()}); export const promptPreviewResponse = z.object({messages: z.array(promptMessageModel).optional(), strategy: z.string().nullable().optional(), strategies: z.array(z.string()).optional(), run_settings: z.array(runSettingModel).optional(), response_schema: z.record(z.string(), z.unknown()).nullable().optional(), skipped_reason: z.string().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 }; /** * Preview an operation's prompts (no LLM call) * * POST /v1/default/banks/{bank_id}/prompts/preview */ export const createApiV1DefaultBanksBankIdPromptsPreview = async (args: CreateApiV1DefaultBanksBankIdPromptsPreviewArgs, options: CreateApiV1DefaultBanksBankIdPromptsPreviewOptions) => { 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))}/prompts/preview${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(promptPreviewResponse, await res.json()) };