import {z} from 'zod' export type GetApiV1WorkspacesWorkspaceDashboardArgs = {workspace: string}; export type GetApiV1WorkspacesWorkspaceDashboardOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardTask = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), cost_usd: z.string().nullable(), finished_at: z.string().nullable(), started_at: z.string().nullable(), status: z.string(), task_id: z.string(), title: z.string()}); export const hitlBlocker = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), created_at: z.string(), description: z.string(), status: z.string(), task_id: z.string()}); export const blockedTaskBlocker = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), description: z.string(), error: z.string().nullable(), occurred_at: z.string(), task_id: z.string()}); export const failedTaskBlocker = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), error: z.string().nullable(), occurred_at: z.string(), task_id: z.string()}); export const walletExhaustedBlocker = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), budget_usd: z.string(), period: z.string()}); export const blockers = z.object({awaiting_continuation: z.array(hitlBlocker), blocked_24h: z.array(blockedTaskBlocker), failed_24h: z.array(failedTaskBlocker), hitl: z.array(hitlBlocker), wallet_exhausted: z.array(walletExhaustedBlocker)}); export const dailySpendPoint = z.object({date: z.string(), usd: z.string()}); export const brokenConnection = z.object({at: z.string().nullable(), error: z.string().nullable(), instance_id: z.string(), name: z.string(), reason: z.enum(["verification_failed", "last_call_failed"])}); export const brokenTrigger = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), consecutive_failures: z.number().int(), failure_threshold: z.number().int(), last_execution_at: z.string().nullable(), name: z.string(), reason: z.enum(["failing", "stopped_after_failures", "needs_owner"]), trigger_id: z.string()}); export const health = z.object({connections: z.array(brokenConnection), triggers: z.array(brokenTrigger)}); export const frequentSchedule = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), cron_expression: z.string(), next_run_at: z.string(), runs_per_day: z.number().int(), title: z.string(), trigger_id: z.string()}); export const scheduledRun = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), fires_at: z.string(), task_id: z.string().nullable().optional(), title: z.string(), trigger_id: z.string().nullable().optional()}); export const schedule = z.object({frequent: z.array(frequentSchedule), horizon_days: z.number().int(), runs: z.array(scheduledRun)}); export const currencySpend = z.object({amount: z.string(), currency: z.string()}); export const spendCard = z.object({cap_usd: z.string().nullable(), mtd_estimate_usd: z.string().optional(), mtd_usd: z.string(), not_covered: z.array(currencySpend).optional(), pct_of_cap: z.number().nullable(), projected_eom_usd: z.string().nullable(), projection_method: z.string().optional(), today_usd: z.string()}); export const agentSpend = z.object({agent_id: z.string(), agent_name: z.string().nullable().optional(), usd: z.string()}); export const runtimeUsage = z.object({cpu_core_hours: z.string(), memory_gib_hours: z.string(), storage_gib: z.string()}); export const dashboardResponse = z.object({active_tasks: z.array(dashboardTask), blockers: blockers, daily_spend: z.array(dailySpendPoint), health: health, recent_tasks: z.array(dashboardTask), schedule: schedule, spend: spendCard, top_agents: z.array(agentSpend), usage: runtimeUsage.nullable()}); 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 Dashboard * * GET /v1/workspaces/{workspace}/dashboard */ export const getApiV1WorkspacesWorkspaceDashboard = async (args: GetApiV1WorkspacesWorkspaceDashboardArgs, options: GetApiV1WorkspacesWorkspaceDashboardOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/dashboard${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(dashboardResponse, await res.json()) };