Skip to content

feat: strongly-typed AgentClient with call inference and stub proxy#1146

Merged
threepointone merged 1 commit into
mainfrom
tyoed-agent-client
Mar 22, 2026
Merged

feat: strongly-typed AgentClient with call inference and stub proxy#1146
threepointone merged 1 commit into
mainfrom
tyoed-agent-client

Conversation

@threepointone
Copy link
Copy Markdown
Contributor

@threepointone threepointone commented Mar 22, 2026

Summary

Closes #1144

  • Typed call and stub: AgentClient now accepts an optional agent type parameter (AgentClient<MyAgent>) for full type inference on RPC calls — method names autocomplete, argument types and return types are inferred. A new stub property provides direct RPC-style proxy calls (client.stub.getValue()) matching useAgent's typed experience.
  • State inference: State type is automatically inferred from the agent type, so onStateUpdate is also typed without needing a second generic parameter.
  • Backward compatible: Existing untyped usage (AgentClient<State>, new AgentClient({...})) continues to work without changes.
  • Shared type utilities: RPC type utilities (AgentMethods, AgentStub, RPCMethods, etc.) extracted to agents/client and shared with useAgent in agents/react, eliminating duplication.
  • Flaky test fix: Increased chunkDelayMs in two chat-turn-serialization tests from 40ms to 100ms to prevent timer-precision races in the Workers runtime.

Breaking change

call is now an instance property (bound in constructor) instead of a prototype method. AgentClient.prototype.call no longer exists. Normal usage (client.call(...)) is unaffected; only code reflecting on the prototype or subclasses overriding call as a method would need adjustment. Marked as a minor semver bump in the changeset.

Test plan

  • All 745 agents workers tests pass
  • All 310 ai-chat tests pass (including previously flaky chat-turn-serialization)
  • All 65 projects typecheck successfully
  • New type-level tests verify typed call, typed stub, non-serializable method exclusion, base Agent method exclusion, backward compat for AgentClient<State> and untyped usage
  • npm run check passes (sherif, exports, oxfmt, oxlint, typecheck)

Made with Cursor


Open with Devin

Introduce a strongly-typed AgentClient generic (AgentT) with inferred call and stub APIs, plus shared RPC type utilities exported from packages/agents/src/client. Move call to a per-instance bound implementation (_callImpl) to support conditional typing and expose a createStubProxy helper to provide a direct RPC-style stub. Remove duplicate RPC typing from the React hook and import the new shared types instead. Add TypeScript declaration tests for typed client and stub, add a changeset, and tweak ai-chat test timings (increase chunkDelayMs) to stabilize timing-sensitive tests. Note: this is backward-compatible for untyped usage, but AgentClient.prototype.call no longer exists (call is assigned per-instance).
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 22, 2026

🦋 Changeset detected

Latest commit: 55781c1

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

This PR includes changesets to release 1 package
Name Type
agents 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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 22, 2026

Open in StackBlitz

agents

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

@cloudflare/ai-chat

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

@cloudflare/codemode

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

hono-agents

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

@cloudflare/shell

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

@cloudflare/think

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

@cloudflare/voice

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

@cloudflare/worker-bundler

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

commit: 55781c1

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

@threepointone threepointone merged commit b74e108 into main Mar 22, 2026
2 checks passed
@threepointone threepointone deleted the tyoed-agent-client branch March 22, 2026 12:49
@github-actions github-actions Bot mentioned this pull request Mar 22, 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.

AgentClient.prototype.call is not strongly typed

1 participant