Skip to content

Commit c7b5093

Browse files
committed
report agent spend as yours and total
Metrics meters now carry both the caller's own aggregate and the agent-wide one, with the agent-wide value null when the caller may not see other users' sessions. Callers under an `all` access scope may read totals, since agents are not partitioned per user there. The agent library spend column shows "yours / total" and falls back to a dash when the total is withheld; single-number surfaces keep showing the total and fall back to the caller's own. Signed-off-by: Raman Tehlan <ramantehlan@gmail.com>
1 parent bc1b589 commit c7b5093

28 files changed

Lines changed: 578 additions & 184 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@truefoundry/trueforge': patch
3+
'@truefoundry/trueforge-assistant-ui-runtime': patch
4+
'@truefoundry/trueforge-ui': patch
5+
---
6+
7+
Report agent metrics as a pair: the caller's own figures plus the agent-wide ones, which are null when the caller may not see other users' sessions. The agent library spend column now shows "yours / total", with a dash when the total is withheld.

‎.github/fern/openapi/openapi.json‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4829,8 +4829,12 @@
48294829
"additionalProperties": false,
48304830
"properties": {
48314831
"aggregate_value": {
4832+
"description": "Value across every session on the agent, or null when the caller may not see other users sessions.",
48324833
"minimum": 0,
4833-
"type": "number"
4834+
"type": [
4835+
"number",
4836+
"null"
4837+
]
48344838
},
48354839
"description": {
48364840
"type": "string"
@@ -4854,11 +4858,17 @@
48544858
},
48554859
"unit": {
48564860
"$ref": "#/components/schemas/MetricsUnit"
4861+
},
4862+
"your_aggregate_value": {
4863+
"description": "Value across the sessions the caller created.",
4864+
"minimum": 0,
4865+
"type": "number"
48574866
}
48584867
},
48594868
"required": [
48604869
"name",
48614870
"aggregate_value",
4871+
"your_aggregate_value",
48624872
"description",
48634873
"unit"
48644874
],

‎docs/openapi.json‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4829,8 +4829,12 @@
48294829
"additionalProperties": false,
48304830
"properties": {
48314831
"aggregate_value": {
4832+
"description": "Value across every session on the agent, or null when the caller may not see other users sessions.",
48324833
"minimum": 0,
4833-
"type": "number"
4834+
"type": [
4835+
"number",
4836+
"null"
4837+
]
48344838
},
48354839
"description": {
48364840
"type": "string"
@@ -4854,11 +4858,17 @@
48544858
},
48554859
"unit": {
48564860
"$ref": "#/components/schemas/MetricsUnit"
4861+
},
4862+
"your_aggregate_value": {
4863+
"description": "Value across the sessions the caller created.",
4864+
"minimum": 0,
4865+
"type": "number"
48574866
}
48584867
},
48594868
"required": [
48604869
"name",
48614870
"aggregate_value",
4871+
"your_aggregate_value",
48624872
"description",
48634873
"unit"
48644874
],

‎packages/assistant-ui-runtime/src/server/types.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,10 @@ export interface AgentMetricChartDefinition {
14431443

14441444
export interface AgentMetricMeter {
14451445
name: string;
1446-
aggregateValue: number;
1446+
/** Across every session on the agent; null when the caller may not see other users' sessions. */
1447+
aggregateValue: number | null;
1448+
/** Across the sessions the caller created. */
1449+
yourAggregateValue: number;
14471450
description: string;
14481451
unit: string;
14491452
}

‎packages/trueforge-sdk/src/api/types/SessionMetricsMeter.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import type * as TrueForge from "../index.js";
44

55
export interface SessionMetricsMeter {
6-
aggregateValue: number;
6+
/** Value across every session on the agent, or null when the caller may not see other users sessions. */
7+
aggregateValue: number | null;
78
description: string;
89
name: TrueForge.SessionMetricsMeterName;
910
unit: TrueForge.MetricsUnit;
11+
/** Value across the sessions the caller created. */
12+
yourAggregateValue: number;
1013
}

‎packages/trueforge-sdk/src/serialization/types/SessionMetricsMeter.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ export const SessionMetricsMeter: core.serialization.ObjectSchema<
1010
serializers.SessionMetricsMeter.Raw,
1111
TrueForge.SessionMetricsMeter
1212
> = core.serialization.object({
13-
aggregateValue: core.serialization.property("aggregate_value", core.serialization.number()),
13+
aggregateValue: core.serialization.property("aggregate_value", core.serialization.number().nullable()),
1414
description: core.serialization.string(),
1515
name: SessionMetricsMeterName,
1616
unit: MetricsUnit,
17+
yourAggregateValue: core.serialization.property("your_aggregate_value", core.serialization.number()),
1718
});
1819

1920
export declare namespace SessionMetricsMeter {
2021
export interface Raw {
21-
aggregate_value: number;
22+
aggregate_value?: number | null;
2223
description: string;
2324
name: SessionMetricsMeterName.Raw;
2425
unit: MetricsUnit.Raw;
26+
your_aggregate_value: number;
2527
}
2628
}

‎packages/trueforge-sdk/tests/wire/internal/metrics.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ describe("MetricsClient", () => {
136136

137137
const rawResponseBody = {
138138
data: {
139-
meters: [{ aggregate_value: 1.1, description: "description", name: "total_sessions", unit: "count" }],
139+
meters: [
140+
{
141+
aggregate_value: null,
142+
description: "description",
143+
name: "total_sessions",
144+
unit: "count",
145+
your_aggregate_value: 1.1,
146+
},
147+
],
140148
},
141149
};
142150

@@ -157,10 +165,11 @@ describe("MetricsClient", () => {
157165
data: {
158166
meters: [
159167
{
160-
aggregateValue: 1.1,
168+
aggregateValue: null,
161169
description: "description",
162170
name: "total_sessions",
163171
unit: "count",
172+
yourAggregateValue: 1.1,
164173
},
165174
],
166175
},

‎packages/trueforge-ui/src/atoms/AgentLibraryUsage.tsx‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ function formatSparklineDay(iso: string): string {
3131

3232
export const agentLibraryUsageLabels = {
3333
sessions: `Sessions, ${String(AGENT_LIBRARY_METERS_WINDOW_DAYS)}d`,
34-
spend: `Spend, ${String(AGENT_LIBRARY_METERS_WINDOW_DAYS)}d`,
34+
spend: `Your / Total Spend, ${String(AGENT_LIBRARY_METERS_WINDOW_DAYS)}d`,
3535
daily: `Daily, ${String(AGENT_LIBRARY_SPARKLINE_DAYS)}d`,
3636
} as const;
3737

38+
/** Stands in for a value the server withheld or could not produce. */
39+
const UNAVAILABLE_VALUE = '—';
40+
41+
function spendTooltipContent(hasTotal: boolean): string {
42+
return hasTotal
43+
? "Your spend / spend across every user's sessions on this agent."
44+
: 'Your spend. The agent-wide total is only visible to people who manage this agent.';
45+
}
46+
3847
function nearestSparklineIndex({
3948
clientX,
4049
svg,
@@ -213,20 +222,40 @@ export function AgentSpendCell({
213222
</div>
214223
);
215224
}
216-
if (usage?.totalCostUsd == null) {
225+
if (usage?.yourCostUsd == null) {
217226
return (
218227
<span className="text-text-secondary text-sm" aria-label={`Spend unavailable for ${agentName}`}>
219-
—
228+
{UNAVAILABLE_VALUE}
220229
</span>
221230
);
222231
}
232+
const hasTotal = usage.totalCostUsd != null;
233+
const yourSpend = formatLibraryCostUsd(usage.yourCostUsd);
223234
return (
224-
<div className="flex min-w-0 flex-col gap-0.5">
225-
<span className="text-text-primary text-sm font-medium">{formatLibraryCostUsd(usage.totalCostUsd)}</span>
226-
{usage.costPerSessionUsd != null ? (
227-
<span className="text-text-secondary text-xs">{formatLibraryCostUsd(usage.costPerSessionUsd)} / session</span>
228-
) : null}
229-
</div>
235+
<Tooltip content={spendTooltipContent(hasTotal)} className="max-w-xs whitespace-normal text-left">
236+
<span className="flex min-w-0 flex-col gap-0.5">
237+
<span className="text-text-primary text-sm font-medium">
238+
<span aria-label={`Your spend ${yourSpend}`}>{yourSpend}</span>
239+
<span aria-hidden className="text-text-secondary">
240+
{' / '}
241+
</span>
242+
{usage.totalCostUsd == null ? (
243+
<span className="text-text-secondary" aria-label={`Total spend unavailable for ${agentName}`}>
244+
{UNAVAILABLE_VALUE}
245+
</span>
246+
) : (
247+
<span aria-label={`Total spend ${formatLibraryCostUsd(usage.totalCostUsd)}`}>
248+
{formatLibraryCostUsd(usage.totalCostUsd)}
249+
</span>
250+
)}
251+
</span>
252+
{usage.costPerSessionUsd != null ? (
253+
<span className="text-text-secondary text-xs">
254+
{formatLibraryCostUsd(usage.costPerSessionUsd)} per session
255+
</span>
256+
) : null}
257+
</span>
258+
</Tooltip>
230259
);
231260
}
232261

‎packages/trueforge-ui/src/atoms/agent-details/AgentMetricCard.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ComponentType } from 'react';
22

33
import { Icon } from '../../icons/Icon.js';
4+
import { visibleMeterValue } from '../../utils/agentMetricValues.js';
45
import { formatCostUsd, formatDurationMs } from '../../utils/sessionDisplayFormat.js';
56
import { cn } from '../lib/cn.js';
67
import type { AgentMetricCardProps } from './types.js';
@@ -39,7 +40,7 @@ export function AgentMetricCard({ meter }: AgentMetricCardProps) {
3940
</span>
4041
</div>
4142
<p className="text-4xl font-bold tabular-nums text-text-primary">
42-
{formatMeterValue(meter.aggregateValue, meter.unit)}
43+
{formatMeterValue(visibleMeterValue(meter), meter.unit)}
4344
</p>
4445
</section>
4546
);

‎packages/trueforge-ui/src/atoms/agent-details/AgentMetricsView.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ComponentType } from 'react';
44

55
import type { AgentMetricMeter } from '../../server/types.js';
66
import { useSlot } from '../../theme/SlotsProvider.js';
7+
import { visibleMeterValue } from '../../utils/agentMetricValues.js';
78
import { Skeleton } from '../primitives/Skeleton.js';
89
import type { AgentMetricChartProps, AgentMetricStatisticsProps, AgentMetricsViewProps } from './types.js';
910

@@ -28,7 +29,8 @@ function formatStatisticValue({
2829
durationInSeconds: boolean;
2930
}): string {
3031
if (meter == null) return '—';
31-
return statisticNumberFormatter.format(durationInSeconds ? meter.aggregateValue / 1_000 : meter.aggregateValue);
32+
const value = visibleMeterValue(meter);
33+
return statisticNumberFormatter.format(durationInSeconds ? value / 1_000 : value);
3234
}
3335

3436
export function AgentMetricStatistics({ meters }: AgentMetricStatisticsProps) {

0 commit comments

Comments
 (0)