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 GetApiV2SkillsArgs = {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 GetApiV2SkillsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const skillAssetContent = z.union([z.object({rawContent: z.string(), isExecutable: z.boolean().optional()}), z.object({textContent: z.string(), isExecutable: z.boolean().optional()})]); export const skillDefinition = z.object({description: z.string().optional(), body: z.string().optional(), assets: z.record(z.string(), skillAssetContent).optional()}); export const registrySharingScope = z.enum(["sharing_scope_unspecified", "private", "workspace"]); export const timestamp = z.string(); export const skill = z.object({id: z.string().optional(), name: z.string().optional(), definition: skillDefinition.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()}); export const listSkillsResponse = z.object({data: z.array(skill).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 }; /** * ListSkills * * GET /v2/skills */ export const getApiV2Skills = async (args: GetApiV2SkillsArgs, options?: GetApiV2SkillsOptions) => { 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/skills${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(listSkillsResponse, await res.json()) };