import {z} from 'zod' export type GetApiV2PromptsPromptIdArgs = {prompt_id: string, version?: number | undefined, alias?: string | undefined, fields?: Array | undefined}; export type GetApiV2PromptsPromptIdOptions = {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 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 }; /** * GetPrompt * * GET /v2/prompts/{prompt_id} */ export const getApiV2PromptsPromptId = async (args: GetApiV2PromptsPromptIdArgs, options?: GetApiV2PromptsPromptIdOptions) => { 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.version != null) query.append("version", String(args.version)) if (args.alias != null) query.append("alias", String(args.alias)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/prompts/${encodeURIComponent(String(args.prompt_id))}${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(prompt, await res.json()) };