import {z} from 'zod' export type MetadataLabels = {[key: string]: string}; export type Metadata = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, displayName?: string | undefined, externalId?: string | undefined, labels?: MetadataLabels | undefined, name: string, plan?: string | undefined, url?: string | undefined, workspace?: string | undefined}; export type Flavor = {name?: string | undefined, type?: ('cpu' | 'gpu') | undefined}; export type Flavors = Array; export type PolicyLocation = {name?: string | undefined, type?: ('location' | 'country' | 'continent') | undefined}; export type PolicyLocations = Array; export type PolicyMaxTokens = {granularity?: string | undefined, input?: number | undefined, output?: number | undefined, ratioInputOverOutput?: number | undefined, step?: number | undefined, total?: number | undefined}; export type PolicyResourceType = 'model' | 'function' | 'agent' | 'sandbox' | 'application'; export type PolicyResourceTypes = Array; export type PolicySpec = {flavors?: Flavors | undefined, locations?: PolicyLocations | undefined, maxTokens?: PolicyMaxTokens | undefined, resourceTypes?: PolicyResourceTypes | undefined, sandbox?: boolean | undefined, type?: ('location' | 'flavor' | 'maxToken') | undefined}; export type PolicyUsageCounts = {agents?: number | undefined, functions?: number | undefined, jobs?: number | undefined, models?: number | undefined, sandboxes?: number | undefined}; export type Policy = {metadata: Metadata, spec: PolicySpec, usage?: PolicyUsageCounts | undefined}; export type CreateApiPoliciesArgs = {body: Policy}; export type CreateApiPoliciesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const metadataLabels = z.record(z.string(), z.string()); export const metadata = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), displayName: z.string().optional(), externalId: z.string().optional(), labels: metadataLabels.optional(), name: z.string(), plan: z.string().optional(), url: z.string().optional(), workspace: z.string().optional()}); export const flavor = z.object({name: z.string().optional(), type: z.enum(["cpu", "gpu"]).optional()}); export const flavors = z.array(flavor); export const policyLocation = z.object({name: z.string().optional(), type: z.enum(["location", "country", "continent"]).optional()}); export const policyLocations = z.array(policyLocation); export const policyMaxTokens = z.object({granularity: z.string().optional(), input: z.number().int().optional(), output: z.number().int().optional(), ratioInputOverOutput: z.number().int().optional(), step: z.number().int().optional(), total: z.number().int().optional()}); export const policyResourceType = z.enum(["model", "function", "agent", "sandbox", "application"]); export const policyResourceTypes = z.array(policyResourceType); export const policySpec = z.object({flavors: flavors.optional(), locations: policyLocations.optional(), maxTokens: policyMaxTokens.optional(), resourceTypes: policyResourceTypes.optional(), sandbox: z.boolean().optional(), type: z.enum(["location", "flavor", "maxToken"]).optional()}); export const policyUsageCounts = z.object({agents: z.number().int().optional(), functions: z.number().int().optional(), jobs: z.number().int().optional(), models: z.number().int().optional(), sandboxes: z.number().int().optional()}); export const policy = z.object({metadata: metadata, spec: policySpec, usage: policyUsageCounts.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 }; /** * Create governance policy * * POST /policies * * Also accepts auth: ApiKeyAuth */ export const createApiPolicies = async (args: CreateApiPoliciesArgs, options?: CreateApiPoliciesOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/policies${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(policy, await res.json()) };