diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index 7f1c5fd14..1a056de45 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -306,6 +306,11 @@ export interface PaseoAgentTimelineRefetchOptions { cursor?: FetchAgentTimelineCursor; limit?: number; projection?: FetchAgentTimelineProjection; + /** + * Extends a `tail` or `before` page back to the start of the oldest turn it touches, so + * `{ direction: "tail", limit: 1, wholeTurns: true }` returns the latest turn. + */ + wholeTurns?: boolean; requestId?: string; } diff --git a/packages/plugin/src/boundaries.test.ts b/packages/plugin/src/boundaries.test.ts index 7b78940ba..8b8cae663 100644 --- a/packages/plugin/src/boundaries.test.ts +++ b/packages/plugin/src/boundaries.test.ts @@ -393,6 +393,7 @@ describe("lifecycle hook SDK types", () => { expect( typeErrors(` import type { PluginServerContext, PluginHooks, PluginHookPayload, PluginHookResult } from "@getpaseo/plugin/server"; + import type { AgentTimelineItem } from "@getpaseo/protocol/agent-types"; export default function contribute(server: PluginServerContext) { server.addHook("worktree.create.before", async ({ request }) => { if (request.kind === "checkout-branch") { @@ -426,8 +427,17 @@ describe("lifecycle hook SDK types", () => { const { metadata, ...withoutMetadata } = request; return withoutMetadata; }); - server.addHook("agent.turn.after", async ({ agent, outcome, timeline }, { paseo, signal }) => { + server.addHook("agent.turn.after", async (event, { paseo, signal }) => { + const { agent, outcome } = event; const kind: "completed" | "failed" | "canceled" = outcome.kind; + // @ts-expect-error Turn payloads carry no history; read it from the agent's timeline. + event.timeline; + const turn = await paseo.agents + .ref(agent.id) + .timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); + const items: readonly AgentTimelineItem[] = turn.entries + .filter((entry) => entry.turnId === event.turnId) + .map(({ item }) => item); // @ts-expect-error Notification payloads have no creation request. outcome.request; }); diff --git a/packages/plugin/src/server/lifecycle.ts b/packages/plugin/src/server/lifecycle.ts index a048d9370..f1ee1a979 100644 --- a/packages/plugin/src/server/lifecycle.ts +++ b/packages/plugin/src/server/lifecycle.ts @@ -2,7 +2,6 @@ import type { AgentToolDefinition } from "./agent-tools.js"; import type { AgentPermissionRequest, AgentPermissionResponse, - AgentTimelineItem, AgentSessionConfig, JsonValue, } from "@getpaseo/protocol/agent-types"; @@ -104,12 +103,13 @@ export interface PluginHookEvents { "agent.turn.after": { agent: PluginHookAgent; turnId: string | null; - outcome: PluginTurnOutcome; /** - * @deprecated Always empty. Fetch history on demand with - * context.paseo.agents.ref(agent.id).timeline.refetch({ direction: "tail", limit: 20 }). + * The payload carries no history. Read the turn with + * `paseo.agents.ref(agent.id).timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true })` + * and keep the entries whose `turnId` equals this `turnId`. The result is empty when a newer turn + * started before the read, or when `turnId` is `null`. */ - timeline: readonly AgentTimelineItem[]; + outcome: PluginTurnOutcome; }; "agent.permission.before": { agent: PluginHookAgent; request: AgentPermissionRequest }; "agent.permission.after": { diff --git a/packages/server/src/server/agent/agent-manager.test.ts b/packages/server/src/server/agent/agent-manager.test.ts index 0590bae58..700ee87c4 100644 --- a/packages/server/src/server/agent/agent-manager.test.ts +++ b/packages/server/src/server/agent/agent-manager.test.ts @@ -13334,7 +13334,6 @@ test.each([ }, turnId: "hook-turn", outcome, - timeline: [], }, }, ]); diff --git a/packages/server/src/server/plugins/lifecycle.e2e.test.ts b/packages/server/src/server/plugins/lifecycle.e2e.test.ts index 92fb21443..d08089ad3 100644 --- a/packages/server/src/server/plugins/lifecycle.e2e.test.ts +++ b/packages/server/src/server/plugins/lifecycle.e2e.test.ts @@ -163,8 +163,14 @@ export default function contribute(server) { server.addHook("agent.turn.before", async (event) => { console.log(JSON.stringify({ hook: "agent.turn.before", event })); }); - server.addHook("agent.turn.after", async (event) => { - console.log(JSON.stringify({ hook: "agent.turn.after", event })); + server.addHook("agent.turn.after", async (event, context) => { + const page = await context.paseo.agents + .ref(event.agent.id) + .timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); + const turn = page.entries + .filter((entry) => entry.turnId === event.turnId) + .map(({ item }) => item.type); + console.log(JSON.stringify({ hook: "agent.turn.after", event, turn })); }); server.addHook("agent.permission.before", async (event, context) => { console.log(JSON.stringify({ hook: "agent.permission.before", event })); @@ -220,6 +226,7 @@ export default function contribute(server) { { hook: "agent.turn.after", event: { outcome: { kind: "completed" }, timeline: [] }, + turn: ["user_message", "tool_call"], }, { hook: "agent.turn.before", event: { agent: { id: agent.id } } }, ]); diff --git a/packages/server/src/server/plugins/lifecycle.real.e2e.test.ts b/packages/server/src/server/plugins/lifecycle.real.e2e.test.ts index 40de1215d..5223e547e 100644 --- a/packages/server/src/server/plugins/lifecycle.real.e2e.test.ts +++ b/packages/server/src/server/plugins/lifecycle.real.e2e.test.ts @@ -157,8 +157,7 @@ test.skipIf(process.platform !== "linux")( "This is a hook integration test. Reply exactly: out of credits. If the next user message is Try again., reply exactly RETRY_OK instead. Do not use tools.", ); await waitForTurns(client, agent.id, 2); - const retryTurns = await endedTurns(client, agent.id); - expect(assistantText(retryTurns[1].data.timeline)).toBe("RETRY_OK"); + expect(await latestReplyText(client, agent.id)).toBe("RETRY_OK"); proof.followup = "RETRY_OK"; await client.sendMessage( @@ -207,8 +206,7 @@ test.skipIf(process.platform !== "linux")( const envPermission = await waitForPermission(client, agent.id, envCommand); await client.respondToPermission(agent.id, envPermission, { behavior: "allow" }); await waitForTurns(client, agent.id, 5); - const envTurns = await endedTurns(client, agent.id); - expect(assistantText(envTurns[4].data.timeline)).toBe("created|opened"); + expect(await latestReplyText(client, agent.id)).toBe("created|opened"); proof.environmentOutput = "created|opened"; await client.refreshAgent(agent.id); @@ -332,6 +330,16 @@ test.skipIf(process.platform !== "linux")( 600_000, ); +// Turn hooks carry no history; read the reply the way plugins do. +async function latestReplyText(client: DaemonClient, agentId: string): Promise { + const page = await client.fetchAgentTimeline(agentId, { + direction: "tail", + limit: 1, + wholeTurns: true, + }); + return assistantText(page.entries.map((entry) => entry.item)); +} + function assistantText(timeline: readonly AgentTimelineItem[]): string { let text = ""; for (const item of timeline) { diff --git a/packages/server/src/server/plugins/lifecycle/index.ts b/packages/server/src/server/plugins/lifecycle/index.ts index 5e552c011..b291bf372 100644 --- a/packages/server/src/server/plugins/lifecycle/index.ts +++ b/packages/server/src/server/plugins/lifecycle/index.ts @@ -195,21 +195,18 @@ export function publishAgentStream( lifecycle.emit("agent.turn.after", { agent, turnId: event.turnId ?? null, - timeline: [], outcome: { kind: "completed" }, }); } else if (event.type === "turn_failed") { lifecycle.emit("agent.turn.after", { agent, turnId: event.turnId ?? null, - timeline: [], outcome: { kind: "failed", error: { message: event.error, code: event.code } }, }); } else if (event.type === "turn_canceled") { lifecycle.emit("agent.turn.after", { agent, turnId: event.turnId ?? null, - timeline: [], outcome: { kind: "canceled", reason: event.reason }, }); } else if (event.type === "permission_requested") { diff --git a/packages/server/src/server/plugins/runtime.ts b/packages/server/src/server/plugins/runtime.ts index f8fe8a91b..fad1b78bd 100644 --- a/packages/server/src/server/plugins/runtime.ts +++ b/packages/server/src/server/plugins/runtime.ts @@ -564,7 +564,7 @@ export class PluginRuntime { type: "hook", requestId: randomUUID(), name, - input: event, + input: wireEvent(name, event), }) .then(() => undefined) .catch((error) => { @@ -1482,3 +1482,11 @@ function readConnectionId(value: unknown): string | null { const connectionId = Reflect.get(value, "connectionId"); return typeof connectionId === "string" ? connectionId : null; } + +// COMPAT(pluginTurnTimeline): added in v0.16.0-beta.3; plugins built before the field left the SDK type read `timeline`; remove after 2027-05-31 +function wireEvent( + name: Name, + event: PluginHookEvents[Name], +): PluginHookEvents[Name] { + return name === "agent.turn.after" ? { ...event, timeline: [] } : event; +} diff --git a/plugin-examples/lifecycle-actions/index.server.ts b/plugin-examples/lifecycle-actions/index.server.ts index 906cd2d56..a89c7aa82 100644 --- a/plugin-examples/lifecycle-actions/index.server.ts +++ b/plugin-examples/lifecycle-actions/index.server.ts @@ -23,8 +23,9 @@ export default function contribute(server: PluginServerContext) { return; } const agent = context.paseo.agents.ref(event.agent.id); - const page = await agent.timeline.refetch({ direction: "tail", limit: 20 }); - const text = latestOutputText(page.entries.map(({ item }) => item)); + const page = await agent.timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); + const turn = page.entries.filter((entry) => entry.turnId === event.turnId); + const text = latestOutputText(turn.map(({ item }) => item)); if (/out of credits/i.test(text)) { console.log("Sending a follow-up after out of credits", event.agent.id); await agent.send("Try again."); diff --git a/public-docs/plugins/hooks.md b/public-docs/plugins/hooks.md index c93da33e0..29be2ce0d 100644 --- a/public-docs/plugins/hooks.md +++ b/public-docs/plugins/hooks.md @@ -62,7 +62,7 @@ Paseo only sees the returned promise, so detached work gets no error reporting a | `agent.close.after` | no | `{ agent }` | | | `agent.archive.after` | no | `{ agent, archivedAt }` | | | `agent.turn.before` | no | `{ agent, turnId }` | | -| `agent.turn.after` | no | `{ agent, turnId, outcome, timeline }` | | +| `agent.turn.after` | no | `{ agent, turnId, outcome }` | | | `agent.permission.before` | no | `{ agent, request }` | | | `agent.permission.after` | no | `{ agent, requestId, resolution }` | | @@ -525,7 +525,7 @@ server.addHook("agent.turn.before", async ({ agent, turnId }) => { ## agent.turn.after -A live turn completes, fails, or is canceled. Receives `{ agent, turnId, outcome, timeline }`. `outcome.kind` is `completed`, `failed`, or `canceled`. `timeline` is deprecated and always empty; fetch history through the SDK as below. +A live turn completes, fails, or is canceled. Receives `{ agent, turnId, outcome }`. `outcome.kind` is `completed`, `failed`, or `canceled`. The payload carries no history. Read the turn from the agent's timeline: `refetch({ direction: "tail", limit: 1, wholeTurns: true })` returns the latest turn whole, as below. Keep the entries whose `turnId` equals `event.turnId`, because a turn that starts before your read, such as a follow-up another plugin sent, replaces this one as the latest. The filtered list is empty in that case, and when `event.turnId` is `null`. Plugins built against an older SDK that read `event.timeline` get an empty array. ### Send a follow-up when a turn ends @@ -545,8 +545,9 @@ export default function contribute(server: PluginServerContext) { } const agent = context.paseo.agents.ref(event.agent.id); - const page = await agent.timeline.refetch({ direction: "tail", limit: 20 }); - const text = latestOutputText(page.entries.map(({ item }) => item)); + const page = await agent.timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); + const turn = page.entries.filter((entry) => entry.turnId === event.turnId); + const text = latestOutputText(turn.map(({ item }) => item)); if (/out of credits/i.test(text)) { await agent.send("Try again."); } @@ -562,9 +563,6 @@ Turn ends: "out of credits" → a new turn starts ``` -The handler fetches only the last 20 timeline items. Output before that page is not inspected. -Use `cursor` and `direction` to page further when your plugin needs more history. - This sends a new message with the existing SDK. Persistent matches keep sending follow-ups; add limits or delays in your plugin when needed. Attachments and tool effects are not replayed. diff --git a/public-docs/plugins/hooks.zh-CN.md b/public-docs/plugins/hooks.zh-CN.md index d78bccd6d..af6c66bd7 100644 --- a/public-docs/plugins/hooks.zh-CN.md +++ b/public-docs/plugins/hooks.zh-CN.md @@ -60,7 +60,7 @@ Paseo 只观察返回的 Promise,因此独立任务没有错误报告,也没 | `agent.close.after` | 否 | `{ agent }` | | | `agent.archive.after` | 否 | `{ agent, archivedAt }` | | | `agent.turn.before` | 否 | `{ agent, turnId }` | | -| `agent.turn.after` | 否 | `{ agent, turnId, outcome, timeline }` | | +| `agent.turn.after` | 否 | `{ agent, turnId, outcome }` | | | `agent.permission.before` | 否 | `{ agent, request }` | | | `agent.permission.after` | 否 | `{ agent, requestId, resolution }` | | @@ -514,7 +514,7 @@ server.addHook("agent.turn.before", async ({ agent, turnId }) => { ## agent.turn.after {#agentturnafter} -实时轮次完成、失败或取消后触发,收到 `{ agent, turnId, outcome, timeline }`。`outcome.kind` 为 `completed`、`failed` 或 `canceled`。`timeline` 已弃用且始终为空;请按下面的示例通过 SDK 获取历史。 +实时轮次完成、失败或取消后触发,收到 `{ agent, turnId, outcome }`。`outcome.kind` 为 `completed`、`failed` 或 `canceled`。载荷不含历史。请从 Agent 的时间线读取本轮:`refetch({ direction: "tail", limit: 1, wholeTurns: true })` 会完整返回最近一轮,见下面的示例。只保留 `turnId` 等于 `event.turnId` 的条目:如果在读取前已有新一轮开始(例如另一个插件发送的后续消息),最近一轮就不再是本轮。此时过滤后的列表为空;`event.turnId` 为 `null` 时也为空。基于旧版 SDK 构建、仍读取 `event.timeline` 的插件只会拿到空数组。 ### 在轮次结束时发送后续消息 {#send-a-follow-up-when-a-turn-ends} @@ -531,8 +531,9 @@ export default function contribute(server: PluginServerContext) { } const agent = context.paseo.agents.ref(event.agent.id); - const page = await agent.timeline.refetch({ direction: "tail", limit: 20 }); - const text = latestOutputText(page.entries.map(({ item }) => item)); + const page = await agent.timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); + const turn = page.entries.filter((entry) => entry.turnId === event.turnId); + const text = latestOutputText(turn.map(({ item }) => item)); if (/out of credits/i.test(text)) { await agent.send("Try again."); } @@ -548,8 +549,6 @@ Turn ends: "out of credits" → a new turn starts ``` -该处理函数只获取最近 20 条时间线条目,不检查这一页之前的输出。插件需要更多历史时,请用 `cursor` 和 `direction` 继续翻页。 - 这会用现有的 SDK 发送一条新消息。持续匹配会不断发送后续消息;需要时请在插件中添加次数限制或延迟。附件和工具的效果不会被重放。 ## agent.permission.before {#agentpermissionbefore} diff --git a/public-docs/plugins/migration.md b/public-docs/plugins/migration.md index 836a8e57b..a2cd723ff 100644 --- a/public-docs/plugins/migration.md +++ b/public-docs/plugins/migration.md @@ -207,6 +207,22 @@ setting `id` as an option key and each `value` as that option, and sends changes | `ProviderSetting` type | `ProviderOptionControl` | | `agent.create.before` `config.featureValues` | `config.providerOptions`; a returned `featureValues` edit is still applied | +### Turn history in v0.16 + +`agent.turn.after` has sent an empty `timeline` since v0.13, and the SDK type no longer declares it. +A plugin that reads `event.timeline` now fails typecheck. Read the turn from the agent instead: + +```ts +const turn = await context.paseo.agents + .ref(event.agent.id) + .timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); +const items = turn.entries.filter((entry) => entry.turnId === event.turnId).map(({ item }) => item); +``` + +The filter drops entries from a turn that started after this one ended, such as a follow-up another plugin sent. The list is empty when such a turn has already started, or when `event.turnId` is `null`. + +Plugins built against an older SDK keep receiving `timeline: []` until removal after 2027-05-31. + ## 5. Recognize half-migration errors | Compiler or load error | Meaning and fix | diff --git a/public-docs/plugins/migration.zh-CN.md b/public-docs/plugins/migration.zh-CN.md index 216f2437b..df74652d4 100644 --- a/public-docs/plugins/migration.zh-CN.md +++ b/public-docs/plugins/migration.zh-CN.md @@ -170,6 +170,21 @@ Provider 插件只有一个配置记录 `providerOptions`,并用控件描述 | `ProviderSetting` 类型 | `ProviderOptionControl` | | `agent.create.before` 的 `config.featureValues` | `config.providerOptions`;返回的 `featureValues` 修改仍会被应用 | +### v0.16 中的轮次历史 {#turn-history-in-v016} + +自 v0.13 起,`agent.turn.after` 发送的 `timeline` 始终为空,SDK 类型也不再声明它。读取 `event.timeline` 的插件现在无法通过类型检查。请改为从 Agent 读取本轮: + +```ts +const turn = await context.paseo.agents + .ref(event.agent.id) + .timeline.refetch({ direction: "tail", limit: 1, wholeTurns: true }); +const items = turn.entries.filter((entry) => entry.turnId === event.turnId).map(({ item }) => item); +``` + +过滤会去掉本轮结束后才开始的新一轮的条目,例如另一个插件发送的后续消息。如果这样的新一轮已经开始,或 `event.turnId` 为 `null`,列表为空。 + +基于旧版 SDK 构建的插件在 2027-05-31 之后移除兼容层之前仍会收到 `timeline: []`。 + ## 5. 识别只迁移了一半的错误 {#5-recognize-half-migration-errors} | 编译器或加载错误 | 含义与修复方法 | diff --git a/public-docs/sdk/reference.md b/public-docs/sdk/reference.md index 5a095b2c2..4db6277f8 100644 --- a/public-docs/sdk/reference.md +++ b/public-docs/sdk/reference.md @@ -118,7 +118,7 @@ workspace the call creates is background too. With a `parent` and no value, the ### Timeline handle -`agent.timeline.refetch(options?)` fetches a page. Options are `direction`, `cursor`, `limit`, `projection`, and `requestId`. +`agent.timeline.refetch(options?)` fetches a page. Options are `direction`, `cursor`, `limit`, `projection`, `wholeTurns`, and `requestId`. `wholeTurns: true` extends a `tail` or `before` page back to the start of the oldest turn it reaches, so `{ direction: "tail", limit: 1, wholeTurns: true }` returns the latest turn. It requires `features.wholeTurnTimeline`; older hosts ignore it and return a page of `limit` items. `agent.timeline.subscribe(handler)` establishes network demand for this agent and restores it after reconnect. Its unsubscribe function releases that demand; await `unsubscribe.ready` for initial daemon acknowledgement before starting work. Delivery is live-only. Reconnect emits a local `subscription_restored` event; request missed history explicitly with `refetch()`. A live replacement invalidates the previous epoch. See the callback shapes, paging and failure behavior in [timeline events](./events.md#follow-timeline-events). diff --git a/public-docs/sdk/reference.zh-CN.md b/public-docs/sdk/reference.zh-CN.md index 5879e9cab..87166ff76 100644 --- a/public-docs/sdk/reference.zh-CN.md +++ b/public-docs/sdk/reference.zh-CN.md @@ -114,7 +114,7 @@ nav: API 参考 ### 时间线句柄 {#timeline-handle} -`agent.timeline.refetch(options?)` 获取一页时间线。选项有 `direction`、`cursor`、`limit`、`projection` 和 `requestId`。 +`agent.timeline.refetch(options?)` 获取一页时间线。选项有 `direction`、`cursor`、`limit`、`projection`、`wholeTurns` 和 `requestId`。`wholeTurns: true` 会把 `tail` 或 `before` 页向前扩展到它所触及的最早一轮的开头,因此 `{ direction: "tail", limit: 1, wholeTurns: true }` 返回最近一轮。它需要 `features.wholeTurnTimeline`;旧版主机会忽略它,返回 `limit` 条记录。 `agent.timeline.subscribe(handler)` 为该 Agent 建立网络订阅需求,并在重连后恢复它。它返回的取消订阅函数会释放这项需求;开始工作之前,请 await `unsubscribe.ready` 以等待 daemon 的初始确认。投递只包含实时内容。重连时会发出一个本地的 `subscription_restored` 事件;错过的历史请用 `refetch()` 显式请求。实时的替换会使之前的 epoch 失效。回调的形状、分页和失败行为见[时间线事件](./events.md#follow-timeline-events)。