import {z} from 'zod' export type PromptVariablesConfiguration = {mode?: ('inline' | 'call_context') | undefined, call_context_note?: (string | null) | undefined, include_weekday?: boolean | undefined}; export type PromptPreviewRequest = {prompt_variables?: PromptVariablesConfiguration | undefined, sample_values?: ({[key: string]: unknown} | null) | undefined, sample_run_id?: (number | null) | undefined, workflow_definition?: ({[key: string]: unknown} | null) | undefined}; export type CreateApiApiV1WorkflowWorkflowIdPromptPreviewArgs = {workflow_id: number, authorization?: (string | null) | undefined, 'X-API-Key'?: (string | null) | undefined, body: PromptPreviewRequest}; export type CreateApiApiV1WorkflowWorkflowIdPromptPreviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const nodePromptPreview = z.object({node_id: z.string(), node_name: z.string(), system_prompt: z.string(), tools: z.array(z.record(z.string(), z.unknown())), variables: z.array(z.string()), unresolved_variables: z.array(z.string()), static_tokens_estimate: z.number().int(), per_call_tokens_estimate: z.number().int(), cache_status: z.string().nullable().optional()}); export const promptPreviewResponse = z.object({nodes: z.array(nodePromptPreview), call_context: z.string().nullable(), provider: z.string().nullable(), model: z.string().nullable(), explicit_cache_available: z.boolean(), minimum_tokens: z.number().int().nullable().optional(), notices: z.array(z.string()), default_note: z.string()}); 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 Workflow Prompts * * POST /api/v1/workflow/{workflow_id}/prompt-preview */ export const createApiApiV1WorkflowWorkflowIdPromptPreview = async (args: CreateApiApiV1WorkflowWorkflowIdPromptPreviewArgs, options?: CreateApiApiV1WorkflowWorkflowIdPromptPreviewOptions) => { const {baseUrl = "https://app.dograh.com", 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)) if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflow/${encodeURIComponent(String(args.workflow_id))}/prompt-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()) };