Skip to content

Commit 31368f6

Browse files
committed
Add Constructor.__post_call__() to support post-call processing
1 parent ea917b2 commit 31368f6

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

coagent/core/types.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,23 @@ def decode_json(cls, json_data: str | bytes) -> RawMessage:
118118

119119
class Constructor:
120120
def __init__(self, typ: Type, *args: Any, **kwargs: Any) -> None:
121-
self.type = typ
122-
self.args = args
123-
self.kwargs = kwargs
121+
self.type: Type = typ
122+
self.args: tuple[Any] = args
123+
self.kwargs: dict[str, Any] = kwargs
124+
125+
# When a `__post_call__()` method is defined on the class, it will be
126+
# called by `__call__()`, normally as `self.__post_call__(agent)`.
127+
self._post_call_fn: Callable[[Agent], Awaitable[None]] | None = getattr(
128+
self, "__post_call__", None
129+
)
124130

125131
async def __call__(self, channel: Channel, address: Address) -> Agent:
126132
agent = self.type(*self.args, **self.kwargs)
127133
agent.init(channel, address)
134+
135+
if self._post_call_fn is not None:
136+
await self._post_call_fn(agent)
137+
128138
return agent
129139

130140

0 commit comments

Comments
 (0)