Skip to content

Add retained streaming agent tools#1421

Merged
threepointone merged 8 commits intomainfrom
agent-tools-orchestration
Apr 30, 2026
Merged

Add retained streaming agent tools#1421
threepointone merged 8 commits intomainfrom
agent-tools-orchestration

Conversation

@threepointone
Copy link
Copy Markdown
Contributor

@threepointone threepointone commented Apr 30, 2026

Summary

  • Add the agent-tool orchestration surface: runAgentTool(), agentTool(), retained parent run registry, lifecycle/chunk event protocol, React event reducer/hook, and cleanup/replay helpers.
  • Implement child adapters for both @cloudflare/think and AIChatAgent, including streaming live-tail forwarding, cancellation, durable inspection, structured output persistence, and terminal error handling.
  • Rewrite examples/agents-as-tools to use the framework APIs, and update docs, READMEs, design notes, WIP history, and the changeset for the shipped Agent Tools feature.

Details

This PR promotes the previous agents-as-tools prototype into framework support. Parent agents now record retained runs in cf_agent_tool_runs, stream child UIMessageChunk bodies as agent-tool-event messages, and allow clients to replay or render child timelines via useAgentToolEvents().

Think children implement the adapter through their existing session-backed chat turn machinery. AIChatAgent children run headlessly through saveMessages(), with override hooks for formatAgentToolInput(), getAgentToolOutput(), and getAgentToolSummary(). Server-side tools work in those runs; browser-provided client tools remain a follow-up design issue: #1420.

The branch also hardens edge cases found during review: structured output is persisted for idempotent terminal reads, AIChatAgent stream errors become failed agent-tool runs, parent aborts are covered, reducer state creation avoids shared mutable empty state, and stale Think-only docs were updated.

Test plan

  • npm run test:workers -w @cloudflare/ai-chat -- --run src/tests/agent-tools.test.ts
  • npm run test -w agents -- src/tests/schema-and-state-optimization.test.ts
  • npm run build -w agents
  • npm run build -w @cloudflare/ai-chat
  • npm run typecheck
  • npm run check

Made with Cursor


Open in Devin Review

Introduce first-class agent tools for running chat-capable Think sub-agents from a parent agent. This adds the parent run registry, event replay, cleanup, cancellation wiring, the AI SDK `agentTool` wrapper, React event aggregation, and the Think child adapter needed to stream retained child timelines through the parent connection.

Rewrite the agents-as-tools example to consume the public APIs instead of the old helper-event prototype, and refresh docs, READMEs, design notes, tests, and release metadata so the feature is discoverable as the supported agent tools surface.

Made-with: Cursor
Extend the agent-tool child adapter contract to AIChatAgent so existing chat agents can run as retained, streaming tools with durable inspection, replay, and cancellation. Also update the shared live-tail transport for Durable Object RPC byte streams and document the headless client-tool limitation for follow-up work.

Made-with: Cursor
Persist structured agent-tool outputs, make AIChatAgent stream errors terminal, and expand cancellation/idempotency coverage so retained runs behave consistently across retries and replays. Refresh the docs and schema-version tests to reflect AIChatAgent support and the new parent registry column.

Made-with: Cursor
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

🦋 Changeset detected

Latest commit: 5ab3181

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
agents Minor
@cloudflare/think Minor
@cloudflare/ai-chat Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

devin-ai-integration[bot]

This comment was marked as resolved.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 30, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1421

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1421

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1421

hono-agents

npm i https://pkg.pr.new/hono-agents@1421

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1421

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1421

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1421

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1421

commit: 5ab3181

Clean up parent abort listeners after completed agent-tool runs and avoid acquiring stream readers when forwarding starts from an already-aborted signal. Add regression coverage for both edge cases so future cancellation changes preserve the resource cleanup behavior.

Made-with: Cursor
devin-ai-integration[bot]

This comment was marked as resolved.

Add expectRootKeepAliveRefCount helper that polls agent.getRootKeepAliveRefCount (up to 20 attempts with a short delay) and use it in sub-agent tests instead of ad-hoc setTimeout waits. This replaces fragile fixed delays with a deterministic polling assert to reduce test flakiness in packages/agents/src/tests/sub-agent.test.ts.
devin-ai-integration[bot]

This comment was marked as resolved.

Drop malformed or shape-invalid NDJSON frames during agent-tool stream forwarding so a corrupted display chunk does not fail an otherwise completed child run. Add regression coverage for the byte-stream forwarding path.

Made-with: Cursor
Add a unit test (packages/think/src/tests/agent-tools.test.ts) that verifies in-memory agent-tool bookkeeping is cleared after a run completes. Extend ThinkTestAgent with helpers to seed a last-error for a run and to inspect map sizes (seedAgentToolLastErrorForTest, getAgentToolCleanupMapSizesForTest). Fix cleanup logic in think.ts to remove entries from _agentToolLastErrors and _agentToolPreTurnAssistantIds when an agent-tool run is torn down to avoid retained in-memory state.
Introduce AgentToolInspection and ThinkAgentToolTestStub types and tighten test helpers' signatures. freshAgent now returns a Promise<ThinkAgentToolTestStub> (with a cast from getAgentByName) and waitForAgentToolRun accepts the stub and returns AgentToolInspection. These changes improve TypeScript safety for agent tool tests and make available explicit method shapes used in the tests (inspectAgentToolRun, seedAgentToolLastErrorForTest, startAgentToolRun, getAgentToolCleanupMapSizesForTest).
@threepointone threepointone merged commit 1b65ff5 into main Apr 30, 2026
3 checks passed
@threepointone threepointone deleted the agent-tools-orchestration branch April 30, 2026 11:05
@github-actions github-actions Bot mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant