import {z} from 'zod' export type PromotionCleanupStrategyV1 = 'KEEP' | 'SCALE_TO_ZERO' | 'DEACTIVATE'; export type RollingDeployStrategyV1 = 'REPLICA'; export type UpdateRollingDeployConfigV1 = {rolling_deploy_strategy?: (RollingDeployStrategyV1 | null) | undefined, max_surge_percent?: (number | null) | undefined, max_unavailable_percent?: (number | null) | undefined, stabilization_time_seconds?: (number | null) | undefined, replica_overhead_percent?: (number | null) | undefined}; export type UpdatePromotionSettingsV1 = {redeploy_on_promotion?: (boolean | null) | undefined, rolling_deploy?: (boolean | null) | undefined, promotion_cleanup_strategy?: (PromotionCleanupStrategyV1 | null) | undefined, rolling_deploy_config?: (UpdateRollingDeployConfigV1 | null) | undefined, ramp_up_while_promoting?: (boolean | null) | undefined, ramp_up_duration_seconds?: (number | null) | undefined}; export type UpdateAutoscalingSettingsV1 = {min_replica?: (number | null) | undefined, max_replica?: (number | null) | undefined, autoscaling_window?: (number | null) | undefined, scale_down_delay?: (number | null) | undefined, concurrency_target?: (number | null) | undefined, target_utilization_percentage?: (number | null) | undefined, target_in_flight_tokens?: (number | null) | undefined, max_scale_down_rate?: (number | null) | undefined}; export type ChainletEnvironmentSettingsRequestV1 = {chainlet_name: string, autoscaling_settings?: (UpdateAutoscalingSettingsV1 | null) | undefined, instance_type_id?: string | undefined}; export type CreateChainEnvironmentRequestV1 = {name: string, promotion_settings?: (UpdatePromotionSettingsV1 | null) | undefined, chainlet_settings?: (Array | null) | undefined}; export type CreateApiV1ChainsChainIdEnvironmentsArgs = {body: CreateChainEnvironmentRequestV1}; export type CreateApiV1ChainsChainIdEnvironmentsOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const promotionCleanupStrategyV1 = z.enum(["KEEP", "SCALE_TO_ZERO", "DEACTIVATE"]); export const rollingDeployStrategyV1 = z.literal("REPLICA"); export const rollingDeployConfigV1 = z.object({rolling_deploy_strategy: rollingDeployStrategyV1.optional(), max_surge_percent: z.number().int().optional(), max_unavailable_percent: z.number().int().optional(), stabilization_time_seconds: z.number().int().optional(), replica_overhead_percent: z.number().int().optional()}); export const promotionSettingsV1 = z.object({redeploy_on_promotion: z.boolean().nullable().optional(), rolling_deploy: z.boolean().nullable().optional(), promotion_cleanup_strategy: promotionCleanupStrategyV1.nullable().optional(), rolling_deploy_config: rollingDeployConfigV1.nullable().optional(), ramp_up_while_promoting: z.boolean().nullable().optional(), ramp_up_duration_seconds: z.number().int().nullable().optional()}); export const autoscalingSettingsV1 = z.object({min_replica: z.number().int(), max_replica: z.number().int(), autoscaling_window: z.number().int().nullable(), scale_down_delay: z.number().int().nullable(), concurrency_target: z.number().int(), target_utilization_percentage: z.number().int().nullable(), target_in_flight_tokens: z.number().int().nullable().optional(), max_scale_down_rate: z.number().int().nullable().optional()}); export const instanceTypeV1 = z.object({id: z.string(), name: z.string(), memory_limit_mib: z.number().int(), millicpu_limit: z.number().int(), gpu_count: z.number().int(), gpu_type: z.string().nullable(), gpu_memory_limit_mib: z.number().int().nullable()}); export const chainletEnvironmentSettingsV1 = z.object({chainlet_name: z.string(), autoscaling_settings: autoscalingSettingsV1.nullable(), instance_type: instanceTypeV1}); export const deploymentStatusV1 = z.enum(["BUILDING", "DEPLOYING", "DEPLOY_FAILED", "LOADING_MODEL", "ACTIVE", "UNHEALTHY", "BUILD_FAILED", "BUILD_STOPPED", "DEACTIVATING", "INACTIVE", "FAILED", "UPDATING", "SCALED_TO_ZERO", "WAKING_UP"]); export const chainletV1 = z.object({id: z.string(), name: z.string(), autoscaling_settings: autoscalingSettingsV1.nullable(), instance_type_name: z.string(), active_replica_count: z.number().int(), status: deploymentStatusV1}); export const chainDeploymentV1 = z.object({id: z.string(), created_at: z.string(), chain_id: z.string(), environment: z.string().nullable(), chainlets: z.array(chainletV1), status: deploymentStatusV1}); export const chainEnvironmentV1 = z.object({name: z.string(), created_at: z.string(), chain_id: z.string(), promotion_settings: promotionSettingsV1, chainlet_settings: z.array(chainletEnvironmentSettingsV1), current_deployment: chainDeploymentV1.nullable(), candidate_deployment: chainDeploymentV1.nullable().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 }; /** * Creates a chain environment * * POST /v1/chains/{chain_id}/environments */ export const createApiV1ChainsChainIdEnvironments = async (args: CreateApiV1ChainsChainIdEnvironmentsArgs, options: CreateApiV1ChainsChainIdEnvironmentsOptions) => { 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(/\/+$/, '')}/v1/chains/${encodeURIComponent(String(args.chain_id))}/environments${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(chainEnvironmentV1, await res.json()) };