Skip to content

Stub for Agent RPC#274

Merged
threepointone merged 2 commits intocloudflare:mainfrom
ruifigueira:stub
May 13, 2025
Merged

Stub for Agent RPC#274
threepointone merged 2 commits intocloudflare:mainfrom
ruifigueira:stub

Conversation

@ruifigueira
Copy link
Copy Markdown
Contributor

A proposal for a stub object for Agent RPC that better mimics method calls.

Usage example:

const { stub } = useAgent<MyAgent, {}>({ agent: "my-agent" });

// return type is promisified
stub.sayHello() satisfies Promise<string>;

// @ts-expect-error first argument is not a string
await stub.sayHello(1);

await stub.perform("some task", 1);
await stub.perform("another task");
// @ts-expect-error requires parameters
await stub.perform();

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented May 12, 2025

🦋 Changeset detected

Latest commit: f823d11

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

This PR includes changesets to release 2 packages
Name Type
agents Patch
hono-agents Patch

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

f6: () => Promise<void>;
}

const { stub } = useAgent<A, {}>({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

love this idea. how about instead of calling this "stub", we call it rpc. so it'll be agent.rpc.f1()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, that even was the initial name but I changed it before submitting the PR 😅 I'll do it at the end of the day

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

well, the nice thing about calling it "stub" is that's what we call it eslewhere in workers already. so maybe we it's fine to call it stub here. lemme review and land this!

Comment on lines +266 to +275
agent.stub = new Proxy<any>(
{},
{
get: (target, method) => {
return (...args: unknown[]) => {
return call(method as string, args);
};
},
}
) as AgentStub<unknown>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

great implementation, so simple

Copy link
Copy Markdown
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

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

another banger, great PR

@threepointone threepointone merged commit 93ccdbd into cloudflare:main May 13, 2025
1 check passed
@threepointone threepointone mentioned this pull request May 13, 2025
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.

2 participants