feat: strongly-typed AgentClient with call inference and stub proxy#1146
Merged
Conversation
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 detectedLatest commit: 55781c1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1144
callandstub:AgentClientnow 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 newstubproperty provides direct RPC-style proxy calls (client.stub.getValue()) matchinguseAgent's typed experience.onStateUpdateis also typed without needing a second generic parameter.AgentClient<State>,new AgentClient({...})) continues to work without changes.AgentMethods,AgentStub,RPCMethods, etc.) extracted toagents/clientand shared withuseAgentinagents/react, eliminating duplication.chunkDelayMsin twochat-turn-serializationtests from 40ms to 100ms to prevent timer-precision races in the Workers runtime.Breaking change
callis now an instance property (bound in constructor) instead of a prototype method.AgentClient.prototype.callno longer exists. Normal usage (client.call(...)) is unaffected; only code reflecting on the prototype or subclasses overridingcallas a method would need adjustment. Marked as a minor semver bump in the changeset.Test plan
chat-turn-serialization)call, typedstub, non-serializable method exclusion, base Agent method exclusion, backward compat forAgentClient<State>and untyped usagenpm run checkpasses (sherif, exports, oxfmt, oxlint, typecheck)Made with Cursor