import {z} from 'zod' export type GetApiV2SkillsArgs = {limit?: number | undefined, starting_after?: string | undefined, ending_before?: string | undefined}; export type GetApiV2SkillsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const skillFile = z.object({path: z.string(), size_bytes: z.string(), sha256: z.string(), content_type: z.string()}); export const skillRepositorySource = z.object({connection_id: z.string().optional(), repository_url: z.string().optional(), ref: z.string().optional(), subdirectory: z.string().optional(), commit_sha: z.string().optional()}); export const skill = z.object({skill_id: z.string(), display_name: z.string(), description: z.string(), tags: z.array(z.string()), project_id: z.string(), path: z.string(), created_at: z.string(), updated_at: z.string(), created_by_id: z.string(), updated_by_id: z.string(), instructions: z.string(), version: z.string(), files: z.array(skillFile).optional(), has_files: z.boolean().optional(), repository_source: skillRepositorySource.optional()}); export const listSkillsResponse = z.object({object: z.string(), data: z.array(skill), has_more: 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 }; /** * List all skills * * GET /v2/skills */ export const getApiV2Skills = async (args: GetApiV2SkillsArgs, options?: GetApiV2SkillsOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) 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()) };