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/plenty-tools-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

fix: use Object.getOwnPropertyDescriptor for property check
51 changes: 23 additions & 28 deletions packages/agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,42 +854,37 @@ export class Agent<Env = typeof env, State = unknown> extends Server<Env> {
while (proto && proto !== Object.prototype && depth < 10) {
const methodNames = Object.getOwnPropertyNames(proto);
for (const methodName of methodNames) {
// Skip if it's a private method or not a function or a getter
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);

// Skip if it's a private method, a base method, a getter, or not a function,
if (
baseMethods.has(methodName) ||
methodName.startsWith("_") ||
typeof this[methodName as keyof this] !== "function" ||
!!Object.getOwnPropertyDescriptor(proto, methodName)?.get
!descriptor ||
!!descriptor.get ||
typeof descriptor.value !== "function"
) {
continue;
}
// If the method doesn't exist in base prototypes, it's a custom method
if (!baseMethods.has(methodName)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was duplicated check of L859, so I removed it.

const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
if (descriptor && typeof descriptor.value === "function") {
// Wrap the custom method with context

const wrappedFunction = withAgentContext(
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
this[methodName as keyof this] as (...args: any[]) => any
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
) as any;

// if the method is callable, copy the metadata from the original method
if (this._isCallable(methodName)) {
callableMetadata.set(
wrappedFunction,
callableMetadata.get(
this[methodName as keyof this] as Function
)!
);
}

// set the wrapped function on the prototype
this.constructor.prototype[methodName as keyof this] =
wrappedFunction;
}
// Now, methodName is confirmed to be a custom method/function
// Wrap the custom method with context
const wrappedFunction = withAgentContext(
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
this[methodName as keyof this] as (...args: any[]) => any
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
) as any;

// if the method is callable, copy the metadata from the original method
if (this._isCallable(methodName)) {
callableMetadata.set(
wrappedFunction,
callableMetadata.get(this[methodName as keyof this] as Function)!
);
}

// set the wrapped function on the prototype
this.constructor.prototype[methodName as keyof this] = wrappedFunction;
}

proto = Object.getPrototypeOf(proto);
Expand Down
Loading