import {z} from 'zod' export type CreateApiApiPreviewCapsulesIdSkillPublishArgs = {id: string, body: {name?: (string | null) | undefined, requestId?: string | undefined, review?: ({candidateDigest: string, confirmedChanges: number, reviewerUserId?: string | undefined, servedGeneration: number | null}) | undefined}}; export type CreateApiApiPreviewCapsulesIdSkillPublishOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiPreviewCapsulesIdSkillPublishResponse = z.object({status: z.object({blockedCode: z.string().max(128).nullable(), blockedReason: z.string().max(4000).nullable(), blockers: z.array(z.record(z.string(), z.unknown())), delivery: z.record(z.string(), z.unknown()).nullable(), derivedName: z.string().max(200).nullable(), drifted: z.boolean(), enabled: z.boolean(), inMarketplace: z.boolean(), nameFrozenMismatch: z.boolean(), nameSource: z.enum(["chosen", "derived"]), publishable: z.boolean(), publishedVersionNum: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), republishRefused: z.string().max(4000).nullable().optional(), skillName: z.string().max(200).nullable(), state: z.enum(["draft", "published", "stale", "pending", "failed", "unknown"]).nullable(), warnings: z.array(z.string().max(4000))}).and(z.record(z.string(), z.unknown()))}).and(z.record(z.string(), z.unknown())); 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 }; /** * Publish a capsule as a skill (enabling it when needed), optionally under a chosen name * * POST /api/preview/capsules/{id}/skill/publish * * Also accepts auth: cookieAuth, oauth2 */ export const createApiApiPreviewCapsulesIdSkillPublish = async (args: CreateApiApiPreviewCapsulesIdSkillPublishArgs, options: CreateApiApiPreviewCapsulesIdSkillPublishOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/preview/capsules/${encodeURIComponent(String(args.id))}/skill/publish${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(createApiApiPreviewCapsulesIdSkillPublishResponse, await res.json()) };