feat(providers): let providers report session and turn metrics

Providers know things about a session and a turn that Paseo cannot derive:
timing, token flow, cache use, cost, the model that answered. They can now
report them as label/value rows that Paseo carries to clients unchanged, so
each provider decides which metrics make sense and how they read.

Session metrics live on the agent record and snapshot. Turn metrics are
stamped on the turn's footer row (last assistant message, else last tool
call) so they survive reload, pagination and a daemon restart, and so the
live turn_completed event and a later timeline fetch agree. Provider history
replay restores both.

Metrics are sanitized once where they enter the agent manager. The wire
schemas validate whole messages, so one malformed row from a provider would
otherwise make clients drop an entire snapshot, turn event or timeline page.

A live completed turn with no Model metric gets the agent's model, so every
provider shows model attribution before it is instrumented. Replayed turns do
not: they may have run on a different model than the agent has now.

Plugin providers publish session metrics behind a negotiated
`session.metrics` capability because an older daemon closes a connection
that sends an event type it does not know. Turn metrics ride an existing
event that older daemons strip, so they need no gate.

The wire additions are optional fields on existing schemas only.
This commit is contained in:
Mohamed Boudra
2026-11-22 08:25:42 +01:00
parent 08ad7c7271
commit eda4597aab
33 changed files with 1616 additions and 62 deletions
+48
View File
@@ -427,6 +427,54 @@ export default function contribute(client: PluginClientContext) {
The same renderer can display items emitted by a provider, a client timeline transformer, or a
daemon timeline append. A renderer does not require a provider implementation.
## Report metrics
Report what your provider measured as `label` and `value` pairs. Paseo shows each value as given,
so format it for reading: `12.4s`, not `12431`. You choose which metrics make sense for your agent.
Add `metrics` to the `completed` turn event. Paseo shows them from the footer of that turn:
```ts
emit({
type: "session.turn",
sessionId,
turnId,
state: "completed",
metrics: [
{ label: "Model", value: "example-1" },
{ label: "Elapsed", value: "12.4s" },
{ label: "Tokens", value: "18.9K ↑ · 2.3K ↓" },
{ label: "Cache", value: "82%", tone: "ok" },
],
});
```
Publish `session.metrics` for totals that cover the whole session. Paseo shows them in the context
window details. Each event replaces the previous list, and an empty list clears it. Add
`session.metrics` to your supported capabilities and publish the event only when it was negotiated:
```ts
if (capabilities.includes("session.metrics")) {
emit({
type: "session.metrics",
sessionId,
metrics: [
{ label: "Turns", value: "7" },
{ label: "Cost", value: "$0.42" },
],
});
}
```
A Paseo from before session metrics does not offer the capability and closes a connection that
sends the event. It ignores `metrics` on a turn, so turn metrics need no check.
Set `tone` to `"ok"`, `"warning"`, or `"danger"` to color a value. When a completed turn has no
`Model` metric, Paseo adds the session's model.
When `history` is `"replay"`, publish each old turn's `completed` event after that turn's items.
Paseo puts the metrics back on the turn's footer.
## Report installation and updates
Add `installation` to your registration and the provider's settings show an **Installation**
+42
View File
@@ -351,6 +351,48 @@ export default function contribute(client: PluginClientContext) {
同一个渲染器可以显示由 Provider、客户端时间线转换器或 daemon 时间线追加发出的条目。渲染器并不需要 Provider 实现。
## 报告指标 {#report-metrics}
把 Provider 测量到的数据以 `label` 和 `value` 的形式报告。Paseo 会原样显示每个值,所以请把它格式化成便于阅读的形式:写 `12.4s`,不要写 `12431`。哪些指标适合你的 Agent,由你决定。
在 `completed` 轮次事件中添加 `metrics`。Paseo 会在该轮次的页脚显示它们:
```ts
emit({
type: "session.turn",
sessionId,
turnId,
state: "completed",
metrics: [
{ label: "Model", value: "example-1" },
{ label: "Elapsed", value: "12.4s" },
{ label: "Tokens", value: "18.9K ↑ · 2.3K ↓" },
{ label: "Cache", value: "82%", tone: "ok" },
],
});
```
对于覆盖整个会话的总计,发布 `session.metrics`。Paseo 会在上下文窗口详情中显示它们。每个事件都会替换之前的列表,空列表会清除它。把 `session.metrics` 加入你支持的能力,并且只在协商成功时发布该事件:
```ts
if (capabilities.includes("session.metrics")) {
emit({
type: "session.metrics",
sessionId,
metrics: [
{ label: "Turns", value: "7" },
{ label: "Cost", value: "$0.42" },
],
});
}
```
不支持会话指标的旧版 Paseo 不会提供这项能力,并且会关闭发送该事件的连接。它会忽略轮次上的 `metrics`,所以轮次指标不需要检查。
把 `tone` 设为 `"ok"`、`"warning"` 或 `"danger"`,可以给值着色。如果已完成的轮次没有 `Model` 指标,Paseo 会添加会话的模型。
当 `history` 为 `"replay"` 时,请在每个旧轮次的条目之后发布该轮次的 `completed` 事件。Paseo 会把指标放回该轮次的页脚。
## 报告安装与更新 {#report-installation-and-updates}
在注册中添加 `installation`,Provider 的设置中就会出现 **安装** 部分。用户可以看到你的 CLI 是否已安装、是哪个版本、是否有可用更新,以及一个 **更新** 按钮,它会运行你的更新程序并显示其输出。每个成员都是可选的,只声明你的 CLI 支持的部分:
+2
View File
@@ -419,6 +419,8 @@ configuration, persistence, permissions, and failures are events.
Use the single `session.prompt` input for messages, structured commands, steering, and command side
effects. Repeat `clientMessageId` on the live user timeline item and publish exactly one matching
`session.prompt_result`. Publish provider-created children as sessions with `parentSessionId`.
Report measurements as `metrics` on a completed `session.turn` and through `session.metrics`. See
[Report metrics](/docs/plugins/providers#report-metrics).
Provider options are one record, `ProviderSessionConfig.providerOptions` on `session.open`. It
includes configured defaults and per-agent overrides; validate and apply it in your provider. Report
+1 -1
View File
@@ -357,7 +357,7 @@ Paseo 为服务端代码提供 `@getpaseo/plugin`、`@getpaseo/plugin/server`、
使用来自 `@getpaseo/plugin/server/provider` 的 `ProviderRegistration` 调用 `server.registerProvider()`。它的连接通过 `send()` 接收输入,并通过 `onEvent()` 发出完整的状态快照。`send()` 只报告是否接受;提示词的处理结果、轮次、配置、持久化、权限和失败都是事件。
消息、结构化命令、引导和命令副作用都使用同一个 `session.prompt` 输入。在实时的用户时间线条目上重复 `clientMessageId`,并恰好发布一个与之匹配的 `session.prompt_result`。把 Provider 创建的子会话发布为带有 `parentSessionId` 的会话。
消息、结构化命令、引导和命令副作用都使用同一个 `session.prompt` 输入。在实时的用户时间线条目上重复 `clientMessageId`,并恰好发布一个与之匹配的 `session.prompt_result`。把 Provider 创建的子会话发布为带有 `parentSessionId` 的会话。通过已完成的 `session.turn` 上的 `metrics` 和 `session.metrics` 报告测量数据,见[报告指标](/docs/plugins/providers#report-metrics)。
Provider 选项是一个记录,即 `session.open` 中的 `ProviderSessionConfig.providerOptions`。它包含配置的默认值和针对单个 Agent 的覆盖值;请在你的 Provider 中校验并应用它。在 `session.config` 中为需要在 Composer 里显示开关或选择器的顶层键报告 `controls`,并在 `providerOptions` 下报告会话正在使用的值。运行中会话的修改会以 `session.configure` 的形式到达,内容在 `changes.providerOptions` 中。配置和合并语义见 [Provider 选项](/docs/sdk/provider-options)。宿主工具以 MCP 服务器的形式出现在完整的会话配置中。