Skip to content

Commit

Permalink
Merge pull request #972 from julep-ai/x/miscellaneous-fixes
Browse files Browse the repository at this point in the history
fix(agents-api): Session & system tool fixes
  • Loading branch information
HamadaSalhab authored Dec 18, 2024
2 parents 75abf54 + 669f3a8 commit 1d4b2c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
29 changes: 27 additions & 2 deletions agents-api/agents_api/activities/execute_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SystemDef,
TextOnlyDocSearchRequest,
UpdateSessionRequest,
UpdateUserRequest,
VectorDocSearchRequest,
)
from ..common.protocol.tasks import ExecutionInput, StepContext
Expand Down Expand Up @@ -110,7 +111,7 @@ async def execute_system(
await bg_runner()
return res

# Handle create operations
# Handle create session
if system.operation == "create" and system.resource == "session":
developer_id = arguments.pop("developer_id")
session_id = arguments.pop("session_id", None)
Expand All @@ -132,7 +133,7 @@ async def execute_system(
),
)

# Handle update operations
# Handle update session
if system.operation == "update" and system.resource == "session":
developer_id = arguments.pop("developer_id")
session_id = arguments.pop("session_id")
Expand All @@ -154,11 +155,35 @@ async def execute_system(
),
)

# Handle update user
if system.operation == "update" and system.resource == "user":
developer_id = arguments.pop("developer_id")
user_id = arguments.pop("user_id")
update_user_request = UpdateUserRequest(**arguments)

# In case users.update becomes asynchronous in the future
if asyncio.iscoroutinefunction(handler):
return await handler()

# Run the synchronous function in another process
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
process_pool_executor,
partial(
handler,
developer_id=developer_id,
user_id=user_id,
data=update_user_request,
),
)

# Handle regular operations
if asyncio.iscoroutinefunction(handler):
return await handler(**arguments)

# Run the synchronous function in another process
# FIXME: When the handler throws an exception, the process dies and the error is not captured. Instead it throws:
# "concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending."
loop = asyncio.get_running_loop()
return await loop.run_in_executor(
process_pool_executor, partial(handler, **arguments)
Expand Down
1 change: 1 addition & 0 deletions agents-api/agents_api/common/protocol/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def get_chat_environment(self) -> dict[str, dict | list[dict]]:
"agents": [agent.model_dump() for agent in self.agents],
"current_agent": current_agent.model_dump(),
"agent": current_agent.model_dump(),
"user": self.users[0].model_dump() if len(self.users) > 0 else None,
"users": [user.model_dump() for user in self.users],
"settings": settings,
"tools": [tool.model_dump() for tool in tools],
Expand Down

0 comments on commit 1d4b2c8

Please sign in to comment.