import {z} from 'zod' export type GetApiV2BudgetsBudgetIdArgs = {budget_id: string}; export type GetApiV2BudgetsBudgetIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceBudgetScope = z.object({}); export const projectBudgetScopeRestResponse = z.object({projectId: z.string().optional()}); export const identityBudgetScopeRestResponse = z.object({identityExternalId: z.string().optional()}); export const apiKeyBudgetScopeRestResponse = z.object({apiKeyId: z.string().optional()}); export const providerBudgetScope = z.object({provider: z.string().optional()}); export const modelBudgetScopeRestResponse = z.object({modelId: z.string()}); export const budgetScopeRestResponse = z.object({workspace: workspaceBudgetScope.optional(), project: projectBudgetScopeRestResponse.optional(), identity: identityBudgetScopeRestResponse.optional(), apiKey: apiKeyBudgetScopeRestResponse.optional(), provider: providerBudgetScope.optional(), model: modelBudgetScopeRestResponse.optional()}); export const budgetMatch = z.object({cel: z.string().optional()}); export const budgetPeriod = z.enum(["BUDGET_PERIOD_DAILY", "BUDGET_PERIOD_WEEKLY", "BUDGET_PERIOD_MONTHLY", "BUDGET_PERIOD_YEARLY", "BUDGET_PERIOD_ONE_TIME"]); export const budgetLimitsRestResponse = z.object({period: budgetPeriod.optional(), amount: z.number().optional(), tokenLimit: z.number().optional()}); export const rateLimitRestResponse = z.object({requestsPerMinute: z.number().int().optional()}); export const budgetUsage = z.object({amount: z.number().optional(), tokens: z.number().optional(), requests: z.number().int().optional()}); export const budgetAlertDimension = z.enum(["BUDGET_ALERT_DIMENSION_COST", "BUDGET_ALERT_DIMENSION_TOKENS"]); export const budgetAlertRestResponse = z.object({id: z.string().optional(), thresholdPercent: z.number().int(), notifierIds: z.array(z.string()), dimension: budgetAlertDimension.optional()}); export const budgetRestResponse = z.object({budgetId: z.string(), scope: budgetScopeRestResponse.optional(), match: budgetMatch.optional(), limits: budgetLimitsRestResponse, rateLimit: rateLimitRestResponse.optional(), isActive: z.boolean().optional(), expiresAt: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), usage: budgetUsage.optional(), alerts: z.array(budgetAlertRestResponse).optional()}); export const getBudgetResponse = z.object({budget: budgetRestResponse}); 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 }; /** * Retrieve a budget * * GET /v2/budgets/{budget_id} */ export const getApiV2BudgetsBudgetId = async (args: GetApiV2BudgetsBudgetIdArgs, options?: GetApiV2BudgetsBudgetIdOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/budgets/${encodeURIComponent(String(args.budget_id))}${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(getBudgetResponse, await res.json()) };