-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(agents-api): Add stub for switch step
Signed-off-by: Diwank Tomer <[email protected]>
- Loading branch information
Diwank Tomer
committed
Aug 19, 2024
1 parent
9fdafaf
commit 69100c5
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
agents-api/agents_api/activities/task_steps/switch_step.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
|
||
from beartype import beartype | ||
from simpleeval import simple_eval | ||
from temporalio import activity | ||
|
||
from ...autogen.openapi_model import SwitchStep | ||
from ...common.protocol.tasks import ( | ||
StepContext, | ||
StepOutcome, | ||
) | ||
from ...env import testing | ||
|
||
|
||
@beartype | ||
async def switch_step(context: StepContext) -> StepOutcome: | ||
# NOTE: This activity is only for logging, so we just evaluate the expression | ||
# Hence, it's a local activity and SHOULD NOT fail | ||
try: | ||
assert isinstance(context.current_step, SwitchStep) | ||
|
||
# Assume that none of the cases evaluate to truthy | ||
output: int = -1 | ||
|
||
cases: list[str] = [c.case for c in context.current_step.switch] | ||
|
||
for i, case in enumerate(cases): | ||
result = simple_eval(case, names=context.model_dump()) | ||
|
||
if result: | ||
output = i | ||
break | ||
|
||
result = StepOutcome(output=output) | ||
return result | ||
|
||
except BaseException as e: | ||
logging.error(f"Error in switch_step: {e}") | ||
return StepOutcome(error=str(e)) | ||
|
||
|
||
# Note: This is here just for clarity. We could have just imported switch_step directly | ||
# They do the same thing, so we dont need to mock the switch_step function | ||
mock_switch_step = switch_step | ||
|
||
switch_step = activity.defn(name="switch_step")( | ||
switch_step if not testing else mock_switch_step | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters