import {z} from 'zod' export type GetApiApiV1betaSkillsArgs = {scope?: ('user' | 'project') | undefined, client?: string | undefined, project_root?: string | undefined, group?: string | undefined}; export type GetApiApiV1betaSkillsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const githubComStacklokToolhivePkgSkillsDependency = z.object({digest: z.string().optional(), name: z.string().optional(), reference: z.string().optional()}); export const githubComStacklokToolhivePkgSkillsSkillMetadata = z.object({author: z.string().optional(), description: z.string().optional(), name: z.string().optional(), tags: z.array(z.string()).optional(), version: z.string().optional()}); export const githubComStacklokToolhivePkgSkillsScope = z.enum(["user", "project"]); export const githubComStacklokToolhivePkgSkillsInstallStatus = z.enum(["installed", "pending", "failed"]); export const githubComStacklokToolhivePkgSkillsInstalledSkill = z.object({clients: z.array(z.string()).optional(), dependencies: z.array(githubComStacklokToolhivePkgSkillsDependency).optional(), digest: z.string().optional(), installed_at: z.string().optional(), managed: z.boolean().optional(), metadata: githubComStacklokToolhivePkgSkillsSkillMetadata.optional(), project_root: z.string().optional(), reference: z.string().optional(), scope: githubComStacklokToolhivePkgSkillsScope.optional(), status: githubComStacklokToolhivePkgSkillsInstallStatus.optional(), tag: z.string().optional()}); export const pkgApiV1SkillListResponse = z.object({skills: z.array(githubComStacklokToolhivePkgSkillsInstalledSkill).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 }; /** * List all installed skills * * GET /api/v1beta/skills */ export const getApiApiV1betaSkills = async (args: GetApiApiV1betaSkillsArgs, options: GetApiApiV1betaSkillsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.scope != null) query.append("scope", String(args.scope)) if (args.client != null) query.append("client", String(args.client)) if (args.project_root != null) query.append("project_root", String(args.project_root)) if (args.group != null) query.append("group", String(args.group)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1beta/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(pkgApiV1SkillListResponse, await res.json()) };