import {z} from 'zod' export type WorkspaceId = string; export type GetApiV1WorkspacesWorkspaceIdEntitlementArgs = {workspace_id: WorkspaceId}; export type GetApiV1WorkspacesWorkspaceIdEntitlementOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const runtimeLocation = z.enum(["local", "cloud"]); export const execution = z.object({location: runtimeLocation, capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional()}); export const usage = z.object({billable: z.boolean(), quantity: z.number().gte(0), unit: z.string(), credits: z.number().gte(0).optional()}); export const workspaceId = z.string(); export const entitledCapability = z.object({capability_id: z.string().min(1), allowed: z.boolean(), effective_from: z.string(), effective_until: z.string().nullable().optional()}); export const entitlementLimit = z.object({limit_id: z.string().min(1), value: z.number().gte(0), unit: z.string().min(1)}); export const creditReservationSummary = z.object({active_count: z.number().int().gte(0), credits_reserved: z.number().gte(0)}); export const workspaceEntitlement = z.object({workspace_id: workspaceId, plan_id: z.string().min(1), status: z.enum(["active", "past_due", "suspended", "canceled"]), capabilities: z.array(entitledCapability), limits: z.array(entitlementLimit), reservations: creditReservationSummary}); export const workspaceEntitlementResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: workspaceEntitlement}); 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 }; /** * Get the effective plan, capability grants, limits, and credit reservations * * GET /v1/workspaces/{workspace_id}/entitlement * * Also accepts auth: apiKeyAuth */ export const getApiV1WorkspacesWorkspaceIdEntitlement = async (args: GetApiV1WorkspacesWorkspaceIdEntitlementArgs, options?: GetApiV1WorkspacesWorkspaceIdEntitlementOptions) => { const {baseUrl = "https://api.cerul.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}/entitlement${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(workspaceEntitlementResponse, await res.json()) };