import {z} from 'zod' export type PromptMessage = {role: string, content: string}; export type EvaluationModelProvider = 'OPEN_AI' | 'ANTHROPIC'; export type ModelSettings = {provider?: EvaluationModelProvider | undefined, name?: string | undefined, temperature?: number | undefined, maxTokens?: number | undefined, topP?: number | undefined, frequencyPenalty?: number | undefined, presencePenalty?: number | undefined, stopSequence?: Array | undefined, reasoningEffort?: ('MINIMAL' | 'LOW' | 'MEDIUM' | 'HIGH') | undefined, verbosity?: ('LOW' | 'MEDIUM' | 'HIGH') | undefined}; export type OutputSchemaField = {id: string, name: string, type: string, required?: boolean | undefined, parentId?: string | undefined}; export type OutputSchema = {name: string, fields: Array}; export type Tool = {id?: string | undefined, name: string, description: string, mode: 'STRICT' | 'ADDITIONAL' | 'NO_ADDITIONAL', structuredSchema?: OutputSchema | undefined}; export type CreatePromptRequest = {alias: string, text?: string | undefined, messages?: Array | undefined, interpolationType?: ('MUSTACHE' | 'MUSTACHE_WITH_SPACE' | 'FSTRING' | 'DOLLAR_BRACKETS') | undefined, modelSettings?: ModelSettings | undefined, outputType?: ('TEXT' | 'JSON' | 'SCHEMA') | undefined, outputSchema?: OutputSchema | undefined, tools?: Array | undefined, branch?: string | undefined}; export type CreateApiV1PromptsArgs = {body: CreatePromptRequest}; export type CreateApiV1PromptsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createPromptResponse = z.object({success: z.boolean(), data: z.object({promptId: z.string(), hash: z.string()}), link: z.string(), deprecated: z.boolean()}); 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 }; /** * Push Prompts * * POST /v1/prompts */ export const createApiV1Prompts = async (args: CreateApiV1PromptsArgs, options?: CreateApiV1PromptsOptions) => { const {baseUrl = "https://api.confident-ai.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/prompts${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(createPromptResponse, await res.json()) };