import {z} from 'zod' export type ListSortField = 'list_sort_field_unspecified' | 'list_sort_field_created_at' | 'list_sort_field_last_modified_at' | 'list_sort_field_name' | 'list_sort_field_title'; export type ListSortDirection = 'list_sort_direction_unspecified' | 'list_sort_direction_asc' | 'list_sort_direction_desc'; export type GetApiV2PromptsArgs = {pageSize?: number | undefined, pageToken?: string | undefined, alias?: string | undefined, fields?: Array | undefined, 'sort.field'?: ListSortField | undefined, 'sort.direction'?: ListSortDirection | undefined, sort_by?: string | undefined, sort_direction?: string | undefined}; export type GetApiV2PromptsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const promptVariable = z.object({name: z.string().optional()}); export const promptDefinition = z.object({content: z.string(), variables: z.array(promptVariable).optional()}); export const registrySharingScope = z.enum(["sharing_scope_unspecified", "private", "workspace"]); export const timestamp = z.string(); export const prompt = z.object({id: z.string().optional(), name: z.string().optional(), definition: promptDefinition.optional(), version: z.number().int().optional(), notes: z.string().optional(), aliases: z.array(z.string()).optional(), sharingScope: registrySharingScope.optional(), createdAt: timestamp.optional(), updatedAt: timestamp.optional(), latestVersion: z.number().int().optional(), title: z.string().optional(), description: z.string().optional(), createdBy: z.string().optional(), versionCreatedAt: timestamp.optional()}); export const listPromptsResponse = z.object({data: z.array(prompt).optional(), nextPageToken: z.string().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 }; /** * ListPrompts * * GET /v2/prompts */ export const getApiV2Prompts = async (args: GetApiV2PromptsArgs, options?: GetApiV2PromptsOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.alias != null) query.append("alias", String(args.alias)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (args['sort.field'] != null) query.append("sort.field", String(args['sort.field'])) if (args['sort.direction'] != null) query.append("sort.direction", String(args['sort.direction'])) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/prompts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listPromptsResponse, await res.json()) };