import {z} from 'zod' export type TokenomicsTimeRange = {start_date: string, end_date: string}; export type TokenomicsInsightsFilters = {departments?: (Array | null) | undefined, job_functions?: (Array | null) | undefined, job_families?: (Array | null) | undefined, regions?: (Array | null) | undefined, countries?: (Array | null) | undefined, activity_status?: ('active' | 'inactive' | 'all' | null) | undefined, time_range?: (TokenomicsTimeRange | null) | undefined, providers?: (Array | null) | undefined, models?: (Array | null) | undefined, leader_user_key?: (string | null) | undefined}; export type TokenomicsInsightsRequest = {filters?: TokenomicsInsightsFilters | undefined, view?: ('individual' | 'team' | 'organization' | null) | undefined, limit?: number | undefined}; export type CreateApiTokenomicsV1InsightsArgs = {body: TokenomicsInsightsRequest}; export type CreateApiTokenomicsV1InsightsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const tokenomicsDateRange = z.object({start_date: z.string(), end_date: z.string()}); export const tokenomicsDataFreshness = z.object({last_updated_at: z.string().nullable().optional()}); export const tokenomicsInsightSubject = z.object({type: z.enum(["organization", "provider", "model"]), provider: z.string().nullable().optional(), model: z.string().nullable().optional()}); export const tokenomicsInsight = z.object({template_id: z.string(), template_version: z.string(), title: z.string(), suggestion: z.string().nullable().optional(), source_metrics: z.array(z.enum(["usage_metrics", "adoption"])), metric: z.enum(["usage_cost_usd", "active_users", "cost_per_million_tokens_usd", "cost_per_active_user_usd", "usage_cost_pct"]), direction: z.enum(["increase", "decrease"]).nullable().optional(), subject: tokenomicsInsightSubject, current_value: z.number(), baseline_value: z.number().nullable().optional(), prior_value: z.number().nullable().optional(), delta_pct: z.number().nullable().optional()}); export const tokenomicsInsightsResponse = z.object({date_range: tokenomicsDateRange, comparison_date_range: tokenomicsDateRange, data_freshness: tokenomicsDataFreshness, material_change_threshold_pct: z.number(), template_catalog_version: z.string(), template_coverage: z.record(z.string(), z.enum(["complete", "partial", "unavailable"])).optional(), insights: z.array(tokenomicsInsight)}); 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 Tokenomics Insights * * POST /tokenomics/v1/insights * * Also accepts auth: ClassicAPIKeyHeader, OAuth2PasswordBearer, HTTPBasic */ export const createApiTokenomicsV1Insights = async (args: CreateApiTokenomicsV1InsightsArgs, options?: CreateApiTokenomicsV1InsightsOptions) => { const {baseUrl = "https://api.galileo.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("Splunk-AO-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tokenomics/v1/insights${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(tokenomicsInsightsResponse, await res.json()) };