import {z} from 'zod' export type SavingsEstimateRequest = {upload_id: string}; export type CreateApiV1OrgOrgIdSavingsEstimateArgs = {body: SavingsEstimateRequest}; export type CreateApiV1OrgOrgIdSavingsEstimateOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const extractionAssumption = z.object({field: z.string(), value: z.string(), reason: z.string(), services: z.array(z.string())}); export const extractionReport = z.object({period_basis: z.enum(["monthly", "annual", "source_period", "null"]).nullable().optional(), engine_version: z.string(), assumptions: z.array(extractionAssumption), gaps: z.array(z.record(z.string(), z.unknown())), readiness: z.record(z.string(), z.number().int())}); export const valueRange = z.object({estimate: z.number(), lower_bound: z.number().nullable(), upper_bound: z.number().nullable()}); export const costLineItem = z.object({service: z.string(), reservable_service: z.string().nullable(), service_code: z.string().nullable().optional(), resource_type: z.string().nullable(), usage_quantity: valueRange.nullable(), usage_unit: z.string().nullable(), cost: valueRange, region: z.string().nullable(), spend_type: z.enum(["on_demand", "reserved", "unreservable", "unknown"]), savings_estimate: valueRange.nullable()}); export const savingsEstimateDetail = z.object({provider: z.enum(["aws", "azure", "gcp", "anthropic", "openai", "null"]).nullable(), source_format: z.string(), period_start: z.string().nullable(), period_end: z.string().nullable(), account_id: z.string().nullable(), currency: z.string(), is_projected: z.boolean(), extraction_status: z.enum(["complete", "partial", "unverified"]), line_items: z.array(costLineItem), total_cost: valueRange, total_savings_estimate: valueRange}); export const savingsEstimate = z.object({extraction_report: extractionReport.nullable().optional(), id: z.string(), status: z.enum(["queued", "processing", "complete", "error"]), error: z.string().nullable().optional(), upload_id: z.string(), result: savingsEstimateDetail.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 }; /** * Estimate savings for an uploaded billing file * * POST /v1/org/{org_id}/savings-estimate */ export const createApiV1OrgOrgIdSavingsEstimate = async (args: CreateApiV1OrgOrgIdSavingsEstimateArgs, options: CreateApiV1OrgOrgIdSavingsEstimateOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/org/${encodeURIComponent(String(args.org_id))}/savings-estimate${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(savingsEstimate, await res.json()) };