Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-eyes-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

fix: don't wrap a method with an agent context if it's already wrapped
8 changes: 7 additions & 1 deletion packages/agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,13 @@ function withAgentContext<T extends (...args: any[]) => any>(
method: T
): (this: Agent<unknown, unknown>, ...args: Parameters<T>) => ReturnType<T> {
return function (...args: Parameters<T>): ReturnType<T> {
const { connection, request, email } = getCurrentAgent();
const { connection, request, email, agent } = getCurrentAgent();

if (agent === this) {
// already wrapped, so we can just call the method
return method.apply(this, args);
}
// not wrapped, so we need to wrap it
return agentContext.run({ agent: this, connection, request, email }, () => {
return method.apply(this, args);
});
Expand Down
Loading