import {z} from 'zod' export type GetApiV1OrgsOrgIdCreditsArgs = {include?: ('bundles' | Array<'bundles'>) | undefined, orgId: string, Authorization?: string | undefined}; export type GetApiV1OrgsOrgIdCreditsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1OrgsOrgIdCreditsResponse = z.object({links: z.object({self: z.string()}), data: z.object({id: z.string(), type: z.literal("credit-account"), attributes: z.object({credits: z.union([z.object({state: z.union([z.literal("ok"), z.literal("over_budget")]), limit: z.number(), used: z.number(), usedByType: z.object({usage: z.number(), platform: z.number(), ai: z.number()}), additionalCredits: z.number().gte(0).optional(), monthlyCreditsRemaining: z.number().gte(0).optional(), expiringCredits: z.object({credits: z.number().gte(0), expiresAt: z.string()}).nullable().optional(), creditSources: z.array(z.object({kind: z.union([z.literal("grant"), z.literal("monthly_allowance"), z.literal("paid")]), source: z.string().nullable(), name: z.string().nullable(), credits: z.number().gte(0), used: z.number().gte(0), remaining: z.number().gte(0), expiresAt: z.string().nullable()})), remaining: z.number(), windowStart: z.string().nullable(), resetsAt: z.string().nullable(), overLimit: z.boolean(), blocked: z.boolean(), overageAllowed: z.boolean()}), z.object({state: z.literal("unlimited")}), z.object({state: z.literal("unavailable")})]), plan: z.object({code: z.union([z.literal("free"), z.literal("free-legacy-v1"), z.literal("team-v1"), z.literal("business-v1"), z.literal("enterprise-v1")]), displayName: z.string()}), prices: z.object({skillReview: z.number(), skillImproveIteration: z.number(), evalScenarioRun: z.number(), evalScenarioGeneration: z.number(), cloudLaunch: z.number(), agentDocRuleConflict: z.number(), securityScan: z.number()}), bundles: z.array(z.object({code: z.union([z.literal("bundle-5k-v1"), z.literal("bundle-25k-v1")]), credits: z.number(), priceUsd: z.number(), displayName: z.string()})).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 }; /** * The org's credit account: current-window usage against the advertised limit and the plan. Pass `?include=bundles` to also include the catalogue of purchasable top-up bundles in the response. * * GET /v1/orgs/{orgId}/credits */ export const getApiV1OrgsOrgIdCredits = async (args: GetApiV1OrgsOrgIdCreditsArgs, options?: GetApiV1OrgsOrgIdCreditsOptions) => { const {baseUrl = "https://api.tessl.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include != null) query.append("include", String(args.include)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orgs/${encodeURIComponent(String(args.orgId))}/credits${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(getApiV1OrgsOrgIdCreditsResponse, await res.json()) };