Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | 2209x 2209x 7x 7x 7x 2209x 4x 4x 4x 2209x 9x 9x 9x 9x 9x 9x 2209x 3x 3x 3x 2209x 23x 23x 23x 23x 23x 23x 23x 23x 23x 2209x 4x 4x 4x 4x 4x 2209x 18x 18x 18x 18x 18x 18x 18x 18x 2209x 6x 6x 6x 6x 6x 6x | import {
CostPeriod,
CostSource,
ResourceCost,
StepUsageBreakdown,
} from "@/objects/Cost";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { APIRouter } from "../ApiRouter";
export interface UsagePeriod {
input_tokens: number;
output_tokens: number;
total_tokens: number;
invocation_count: number;
error_count: number;
avg_latency_ms: number | null;
estimated_cost_usd: number | null;
}
export interface UsageStats {
this_month: UsagePeriod;
all_time: UsagePeriod;
}
export interface TracePeriod {
llm_invocation_count: number;
tool_invocation_count: number;
avg_llm_per_conversation: number | null;
stddev_llm_per_conversation: number | null;
avg_tool_per_conversation: number | null;
stddev_tool_per_conversation: number | null;
}
export interface TraceStats {
this_month: TracePeriod;
all_time: TracePeriod;
}
export interface LLMInvocationSummary {
id: string;
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
latency_ms: number | null;
status: string;
estimated_cost_usd: number | null;
created_at: string | null;
}
export interface ToolInvocationSummary {
id: string;
tool_name: string;
status: string;
latency_ms: number | null;
execution_context: Record<string, unknown> | null;
created_at: string | null;
}
export interface TaskRunTrace {
task_run_id: string;
llm_invocations: LLMInvocationSummary[];
tool_invocations: ToolInvocationSummary[];
}
/** One model call within an invocation — the model that actually made it. */
export interface LLMInvocationCall {
/** Order within the invocation, from 0. */
sequence: number;
provider: string;
/** The exact model identifier the call was made with. */
model_name: string;
/** The catalogue's name for it, when the catalogue lists the model. */
model_display_name: string | null;
model_id: string | null;
/** chat | embedding */
kind: string;
/** The graph node that made the call; null outside a graph. */
node: string | null;
/** Every prompt token, and every generated token (reasoning included). */
prompt_tokens: number;
completion_tokens: number;
/** Parts of the two totals above, as the provider reported them. */
cache_read_tokens?: number;
cache_write_tokens?: number;
reasoning_tokens?: number;
latency_ms: number | null;
/** success | error — a refused call is kept against the model it went to. */
status: string;
error_detail: string | null;
/** Null when the call could not be priced (or failed), never 0 for that. */
estimated_cost_usd: number | null;
/** The rates behind the cost — or `unpriced_reason`. */
pricing?: {
source?: string;
rates?: Record<string, string>;
long_context?: { threshold: number };
unpriced_reason?: string;
} | null;
}
/** One traced invocation — an execution or platform flow — and its calls. */
export interface LLMInvocationRecord {
id: string;
created_at: string | null;
/** model | agent | skill | worker | graph | pipeline | agent_generation |
* evaluation | evaluation_judge — the last two are an evaluation's cells
* and the judge grading them, stamped by svc-core so they never read as
* a chat. */
purpose: string | null;
conversation_id: string | null;
/** The model the invocation asked for (or the one model its calls used),
* by exact identifier. Null when it named none and its calls used
* several — `calls` names each — or for a trace older than call records.
* Read through `util/invocationModels`, never directly as a label. */
provider: string | null;
model_name: string | null;
model_display_name?: string | null;
model_id?: string | null;
/** Every model call it made, in order; empty for older traces. */
calls?: LLMInvocationCall[];
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
latency_ms: number | null;
status: string;
error_detail: string | null;
estimated_cost_usd: number | null;
request_config: Record<string, unknown> | null;
/** Truncated snapshot of what the call produced. */
output_preview: string | null;
}
/** How often a domain was surfaced by web_search, and its trust tier. */
export interface WebSearchDomainStat {
domain: string;
visit_count: number;
/** 0.0-1.0 domain quality/trust score, averaged across citations. */
avg_trust_score: number | null;
last_visited: string | null;
}
export interface WebSearchDomainStats {
domains: WebSearchDomainStat[];
}
/** One slice of a breakdown's axis. */
export interface BreakdownBucket {
key: string;
label: string;
value: number | null;
/** Fraction of the total this slice accounts for, 0-1. Null for a metric
* that cannot be summed, where a share would be meaningless. */
share: number | null;
/** The named resource no longer exists. The figure still stands — deleting
* an agent does not unspend its budget. */
missing: boolean;
}
/** One metric, over one window, optionally sliced by one dimension. */
export interface Breakdown {
metric: string;
/** Which record the figure was counted from. Derived from the metric — a
* request never states it, so a tile cannot name a metric and a table
* that disagree. */
source: string;
dimension: string | null;
period: string;
since: string | null;
until: string;
/** count | tokens | usd | ms | percent */
unit: string;
/** Whether summing the buckets is meaningful. False for averages and rates. */
additive: boolean;
/** Whether an absent figure should read as zero. False where a null means
* "never measured" — unpriced models, untimed invocations. */
zero_is_meaningful: boolean;
total: number | null;
/** How many slices the axis has, before truncation. */
bucket_count: number;
buckets: BreakdownBucket[];
}
export interface StatsCatalogueEntry {
key: string;
label: string;
description: string;
}
export interface StatsMetricEntry extends StatsCatalogueEntry {
unit: string;
additive: boolean;
zero_is_meaningful: boolean;
/** The source this is counted from — `usage`, `governance`, `redactions`
* or `reviews`. Used to group the picker and to decide which dimensions
* may be offered beside it. */
source: string;
}
export interface StatsDimensionEntry extends StatsCatalogueEntry {
plural: string;
resolves_names: boolean;
/** Which sources this axis can slice. More than one where the same column
* exists on both: a governance event and its redaction expansion share
* every column, so "by rule" is one axis rather than two. */
sources: string[];
}
/** What can be measured, sliced by what, over what window.
*
* Served from svc-core's registries rather than restated here, so a metric
* added as a class on the backend appears in the pickers with no release of
* this app — the same contract the rule builder's catalogue works to.
*/
export interface StatsCatalogue {
sources: StatsCatalogueEntry[];
metrics: StatsMetricEntry[];
dimensions: StatsDimensionEntry[];
periods: StatsCatalogueEntry[];
max_buckets: number;
}
export interface BreakdownQuery {
metric: string;
dimension?: string | null;
period?: string;
/** Written as `<dimension>:<id>` — narrows the rows before aggregation. */
filters?: string[];
limit?: number;
}
export class StatsRouter extends APIRouter {
constructor(
host: string,
token: string | null = null,
default_config: AxiosRequestConfig | null = null,
headers: Dictionary<string> = {},
) {
super("stats", host, token, default_config, headers);
}
getUsage = async (): Promise<UsageStats | null> => {
const res = await this.api.get<UsageStats>(this.endpoint("usage"));
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
getTraces = async (): Promise<TraceStats | null> => {
const res = await this.api.get<TraceStats>(this.endpoint("traces"));
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
listInvocations = async (
purpose?: string,
limit = 200,
/** Rows to skip, newest first — how the activity feed pages back. */
offset = 0,
): Promise<LLMInvocationRecord[] | null> => {
const params = new URLSearchParams({ limit: String(limit) });
if (purpose) params.set("purpose", purpose);
Iif (offset > 0) params.set("offset", String(offset));
const res = await this.api.get<LLMInvocationRecord[]>(
this.endpoint(`invocations?${params.toString()}`),
);
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
getCatalogue = async (): Promise<StatsCatalogue | null> => {
const res = await this.api.get<StatsCatalogue>(this.endpoint("catalogue"));
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
/** What one resource (or one kind, or the whole account) has cost.
* `mine` counts only the caller's own work in the workspace ("your
* spend"); `since` / `until` (ISO) replace the period with a range. */
getCost = async (query: {
source_type?: CostSource;
source_id?: string;
period?: CostPeriod;
mine?: boolean;
since?: string;
until?: string;
}): Promise<ResourceCost | null> => {
const params = new URLSearchParams();
if (query.source_type) params.set("source_type", query.source_type);
if (query.source_id) params.set("source_id", query.source_id);
if (query.period) params.set("period", query.period);
if (query.mine) params.set("mine", "true");
if (query.since) params.set("since", query.since);
Iif (query.until) params.set("until", query.until);
const res = await this.api.get<ResourceCost>(
this.endpoint(`cost?${params.toString()}`),
);
return super.validRes(res) ? res.data : null;
};
/** Where a resource's tokens go: its newest turns' model calls, summed
* by the step of the pipeline that made them. */
getSteps = async (query: {
source_type: CostSource;
source_id?: string;
period?: CostPeriod;
}): Promise<StepUsageBreakdown | null> => {
const params = new URLSearchParams({ source_type: query.source_type });
if (query.source_id) params.set("source_id", query.source_id);
if (query.period) params.set("period", query.period);
const res = await this.api.get<StepUsageBreakdown>(
this.endpoint(`steps?${params.toString()}`),
);
return super.validRes(res) ? res.data : null;
};
getBreakdown = async (query: BreakdownQuery): Promise<Breakdown | null> => {
const params = new URLSearchParams({ metric: query.metric });
if (query.dimension) params.set("dimension", query.dimension);
if (query.period) params.set("period", query.period);
if (query.limit) params.set("limit", String(query.limit));
// Repeated rather than comma-joined: a filter value is an id, and the
// backend refuses a malformed pair rather than dropping it, so the two
// have to agree on the shape exactly.
(query.filters ?? []).forEach((entry) => params.append("filter", entry));
const res = await this.api.get<Breakdown>(
this.endpoint(`breakdown?${params.toString()}`),
);
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
getWebSearchDomains = async (
limit = 20,
since?: string,
until?: string,
): Promise<WebSearchDomainStats | null> => {
const params = new URLSearchParams({ limit: String(limit) });
if (since) params.set("since", since);
Iif (until) params.set("until", until);
const res = await this.api.get<WebSearchDomainStats>(
this.endpoint(`web-search-domains?${params.toString()}`),
);
Eif (super.validRes(res)) {
return res.data;
}
return null;
};
}
|