import {z} from 'zod' export type SkillFileWrite = {path?: string | undefined, content?: unknown | undefined}; export type SaveSkillFilesRequest = {files?: Array | undefined, display_name?: string | undefined, description?: string | undefined, instructions?: string | undefined}; export type CreateApiV2SkillsSkillIdFilesSaveArgs = {skill_id: string, body: SaveSkillFilesRequest}; export type CreateApiV2SkillsSkillIdFilesSaveOptions = {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 saveSkillFilesResponse = z.object({skill: skill.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 }; /** * Save skill files atomically * * POST /v2/skills/{skill_id}/files:save */ export const createApiV2SkillsSkillIdFilesSave = async (args: CreateApiV2SkillsSkillIdFilesSaveArgs, options?: CreateApiV2SkillsSkillIdFilesSaveOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/skills/${encodeURIComponent(String(args.skill_id))}/files:save${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(saveSkillFilesResponse, await res.json()) };