import {z} from 'zod' export type GetApiV1MachinesPricingOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const machinePricingResponse = z.object({pricing: z.object({currency: z.literal("usd"), credit_usd_cents: z.number().int().gte(1), runtime: z.record(z.string(), z.unknown()), one_time: z.record(z.string(), z.unknown()), eligibility_gates: z.object({provision_minimum_wallet_balance_credits: z.number().int().gte(0), provision_minimum_wallet_balance_cents: z.number().int().gte(0), provision_minimum_wallet_balance_usd: z.number().gte(0), note: z.literal("Eligibility check only; provisioning does not debit this amount.")}), external_machines: z.object({enrollment_credits: z.literal(0), runtime_credits_per_hour: z.literal(0), direct_action_credits: z.literal(0), frame_transport_credits: z.literal(0), task_and_workflow_steps: z.literal("managed key: charged at the selected CUA version's normal per-step rate; BYOK: zero Coasty platform credits"), note: z.string()}), billing_granularity: z.string(), billed_to: z.string(), model: z.string(), insufficient_funds_behavior: z.string(), ttl: z.record(z.string(), z.unknown())}), request_id: z.string()}); 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 live machine runtime rate card * * GET /v1/machines/pricing * * Also accepts auth: bearerAuth */ export const getApiV1MachinesPricing = async (options?: GetApiV1MachinesPricingOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/pricing${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(machinePricingResponse, await res.json()) };