Files
paseo/plugin-examples/provider-direct/server/provider.test.ts
T
Mohamed Boudra eda4597aab 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.
2026-11-22 08:25:42 +01:00

242 lines
8.2 KiB
TypeScript

import type { ProviderEvent } from "@getpaseo/plugin/server/provider";
import { describe, expect, it } from "vitest";
import { createDirectExampleProvider } from "./provider.js";
describe("direct provider example", () => {
it("fails steering when no turn is active without creating a turn", async () => {
const connection = await createDirectExampleProvider().connect({
versions: [1],
capabilities: ["prompt.command", "prompt.message", "prompt.steer"],
});
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
await connection.send({
type: "session.open",
requestId: "open-1",
sessionId: "session-1",
config: { cwd: "/repo", env: {}, mcpServers: {}, settings: {}, persist: false },
history: "skip",
});
const turnEventsBefore = events.filter((event) => event.type === "session.turn").length;
await connection.send({
type: "session.prompt",
sessionId: "session-1",
prompt: {
clientMessageId: "steer-1",
delivery: "steer",
input: { type: "command", name: "reset", arguments: "" },
},
});
expect(events).toContainEqual({
type: "session.prompt_result",
sessionId: "session-1",
clientMessageId: "steer-1",
result: { type: "failed", error: { message: "There is no active turn to steer" } },
});
expect(events.filter((event) => event.type === "session.turn")).toHaveLength(turnEventsBefore);
expect(events).not.toContainEqual(expect.objectContaining({ type: "session.notice" }));
await connection.close();
});
it("demonstrates option controls, commands, persistence, and provider-rendered timeline items", async () => {
const connection = await createDirectExampleProvider().connect({
versions: [1],
capabilities: [
"prompt.message",
"prompt.command",
"session.configure",
"session.metrics",
"session.persistence",
"session.subsession",
"timeline.plugin",
],
});
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
await connection.send({
type: "session.open",
requestId: "open-complete",
sessionId: "session-complete",
config: {
cwd: "/repo",
env: { EXAMPLE: "1" },
systemPrompt: "Be useful",
mcpServers: {},
model: "example-1",
mode: "build",
settings: {},
providerOptions: { concise: true, voice: "calm", privateValue: 1 },
persist: true,
},
history: "skip",
});
expect(events).toContainEqual(
expect.objectContaining({
type: "session.config",
config: expect.objectContaining({
controls: [
expect.objectContaining({ key: "concise", icon: "Zap" }),
expect.objectContaining({ key: "voice", icon: "Mic", defaultValue: "calm" }),
],
providerOptions: { concise: true, voice: "calm" },
}),
}),
);
expect(events).toContainEqual(expect.objectContaining({ type: "session.commands" }));
await connection.send({
type: "session.configure",
requestId: "configure-complete",
sessionId: "session-complete",
changes: { providerOptions: { voice: "direct", concise: null } },
});
expect(events.findLast((event) => event.type === "session.config")).toMatchObject({
config: { providerOptions: { voice: "direct" } },
});
await connection.send({
type: "session.configure",
requestId: "configure-refused",
sessionId: "session-complete",
changes: { model: "other-model", providerOptions: { voice: "loud" } },
});
expect(events).toContainEqual(
expect.objectContaining({ type: "request.failed", requestId: "configure-refused" }),
);
// A refused request changes nothing, the model it named included.
await connection.send({
type: "session.configure",
requestId: "configure-after-refused",
sessionId: "session-complete",
changes: {},
});
expect(events.findLast((event) => event.type === "session.config")).not.toMatchObject({
config: { model: "other-model" },
});
await connection.send({
type: "session.prompt",
sessionId: "session-complete",
prompt: {
clientMessageId: "command-1",
delivery: "auto",
input: { type: "command", name: "reset", arguments: "" },
},
});
await connection.send({
type: "session.prompt",
sessionId: "session-complete",
prompt: {
clientMessageId: "message-1",
delivery: "auto",
input: { type: "message", content: [{ type: "text", text: "hello" }] },
},
});
expect(events).toContainEqual(
expect.objectContaining({
type: "session.prompt_result",
clientMessageId: "command-1",
result: { type: "completed" },
}),
);
expect(events).toContainEqual(
expect.objectContaining({
type: "timeline.item",
item: expect.objectContaining({ type: "user_message", clientMessageId: "message-1" }),
}),
);
expect(events).toContainEqual(
expect.objectContaining({
type: "timeline.item",
sessionId: "session-complete",
item: expect.objectContaining({ type: "plugin", kind: "provider-result" }),
}),
);
const child = events.find(
(event): event is Extract<ProviderEvent, { type: "session.opened" }> =>
event.type === "session.opened" && event.parentSessionId === "session-complete",
);
expect(child?.sessionId).toMatch(/^[0-9a-f-]{36}$/u);
expect(events).toContainEqual(
expect.objectContaining({
type: "timeline.item",
sessionId: child?.sessionId,
item: expect.objectContaining({ type: "plugin", kind: "provider-result" }),
}),
);
expect(events).toContainEqual(expect.objectContaining({ type: "session.persistence" }));
expect(events).toContainEqual({
type: "session.turn",
sessionId: "session-complete",
turnId: "turn-1",
state: "completed",
metrics: [
{ label: "Model", value: "example-1" },
{ label: "Reply", value: "11 characters" },
],
});
expect(events).toContainEqual({
type: "session.metrics",
sessionId: "session-complete",
metrics: [{ label: "Turns", value: "1" }],
});
await connection.close();
});
it("keeps session metrics from a Paseo that does not offer them", async () => {
const connection = await createDirectExampleProvider().connect({
versions: [1],
capabilities: ["prompt.message"],
});
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
await connection.send({
type: "session.open",
requestId: "open-older",
sessionId: "session-older",
config: { cwd: "/repo", env: {}, mcpServers: {}, settings: {}, persist: false },
history: "skip",
});
await connection.send({
type: "session.prompt",
sessionId: "session-older",
prompt: {
clientMessageId: "message-older",
delivery: "auto",
input: { type: "message", content: [{ type: "text", text: "hello" }] },
},
});
await new Promise<void>((resolve) => queueMicrotask(resolve));
expect(events).toContainEqual(
expect.objectContaining({ type: "session.turn", state: "completed" }),
);
expect(events.filter((event) => event.type === "session.metrics")).toEqual([]);
await connection.close();
});
it("rejects calls after the example connection closes", async () => {
const connection = await createDirectExampleProvider().connect({
versions: [1],
capabilities: ["prompt.message"],
});
await connection.close();
await expect(
connection.send({ type: "catalog", requestId: "catalog-after-close" }),
).rejects.toThrow("Provider connection is closed");
await expect(
connection.send({
type: "session.open",
requestId: "open-after-close",
sessionId: "closed-session",
config: { cwd: "/repo", env: {}, mcpServers: {}, settings: {}, persist: false },
history: "skip",
}),
).rejects.toThrow("Provider connection is closed");
});
});