Skip to content

Commit

Permalink
fix: Use ruamel.yaml instead of pyyaml (#651)
Browse files Browse the repository at this point in the history
Signed-off-by: Diwank Singh Tomer <[email protected]>

<!-- ELLIPSIS_HIDDEN -->

----

> [!IMPORTANT]
> Replace `pyyaml` with `ruamel.yaml` for YAML processing and perform
minor code cleanups.
> 
>   - **YAML Processing**:
> - Replace `pyyaml` with `ruamel.yaml` in `utils.py`, `template.py`,
and `routers/tasks/router.py`.
> - Add `yaml.py` in `common/utils` to define `dump` and `load`
functions using `ruamel.yaml`.
>   - **Code Cleanup**:
> - Remove unused imports in `excecute_api_call.py` and
`execute_system.py`.
>     - Fix `None` comparison in `execute_system.py`.
>     - Remove unused exception variable in `update_execution.py`.
>   - **Dependencies**:
>     - Remove `pyyaml` and add `ruamel-yaml` in `pyproject.toml`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup>
for 0854ead. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->

Signed-off-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
creatorrr authored Oct 15, 2024
1 parent 24c2bd5 commit 0855438
Show file tree
Hide file tree
Showing 15 changed files with 6,605 additions and 155 deletions.
4 changes: 1 addition & 3 deletions agents-api/agents_api/activities/excecute_api_call.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import base64
from typing import Annotated, Any, Optional, TypedDict, Union
from typing import Any, Optional, TypedDict, Union

import httpx
from beartype import beartype
from pydantic import Field
from temporalio import activity

from ..autogen.openapi_model import ApiCallDef

# from ..clients import integrations
from ..common.protocol.tasks import StepContext
from ..env import testing

# from ..models.tools import get_tool_args_from_metadata
Expand Down
4 changes: 2 additions & 2 deletions agents-api/agents_api/activities/execute_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def execute_system(
)

# NO SUBRESOURCE
elif system.subresource == None:
elif system.subresource is None:
if system.operation == "list":
return list_agents_query(**arguments)
elif system.operation == "get":
Expand Down Expand Up @@ -196,7 +196,7 @@ async def execute_system(
)

# NO SUBRESOURCE
elif system.subresource == None:
elif system.subresource is None:
if system.operation == "list":
return list_users_query(**arguments)
elif system.operation == "get":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
async def get_value_step(
context: StepContext,
) -> StepOutcome:
key: str = context.current_step.get
key: str = context.current_step.get # noqa: F841
raise NotImplementedError("Not implemented yet")


Expand Down
16 changes: 7 additions & 9 deletions agents-api/agents_api/activities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
import string
import time
import urllib.parse
from typing import Any, Callable, ParamSpec, Type, TypeVar, cast
from typing import Any, Callable, ParamSpec, TypeVar

import re2
import yaml
import zoneinfo
from beartype import beartype
from simpleeval import EvalWithCompoundTypes, SimpleEval
from yaml import CSafeDumper, CSafeLoader

T = TypeVar("T")

from ..common.utils import yaml

P = ParamSpec("P")
T = TypeVar("T")
R = TypeVar("R")
P = ParamSpec("P")


# TODO: We need to make sure that we dont expose any security issues
Expand Down Expand Up @@ -51,7 +49,7 @@
"zip": zip,
"search_regex": lambda pattern, string: re2.search(pattern, string),
"load_json": json.loads,
"load_yaml": lambda string: yaml.load(string, Loader=CSafeLoader),
"load_yaml": yaml.load,
"match_regex": lambda pattern, string: bool(re2.fullmatch(pattern, string)),
}

Expand All @@ -74,8 +72,8 @@ class stdlib_json:


class stdlib_yaml:
load = lambda string: yaml.load(string, Loader=CSafeLoader) # noqa: E731
dump = lambda value: yaml.dump(value, Dumper=CSafeDumper) # noqa: E731
load = yaml.load
dump = yaml.dump


class stdlib_time:
Expand Down
2 changes: 1 addition & 1 deletion agents-api/agents_api/common/protocol/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Annotated, Any, Type
from typing import Annotated, Any
from uuid import UUID

from pydantic import BaseModel, Field, computed_field
Expand Down
3 changes: 2 additions & 1 deletion agents-api/agents_api/common/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import arrow
import re2
import yaml
from beartype import beartype
from jinja2.sandbox import ImmutableSandboxedEnvironment
from jinja2schema import infer, to_json_schema
from jsonschema import validate

from . import yaml

__all__: List[str] = [
"render_template",
]
Expand Down
18 changes: 18 additions & 0 deletions agents-api/agents_api/common/utils/yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from io import StringIO
from typing import Any

from ruamel.yaml import YAML

yaml = YAML(typ="safe", pure=True) # pure is needed for yaml 1.2 support
yaml.version = (1, 2)


def dump(value: Any) -> str:
stream = StringIO()
yaml.dump(value, stream)

return stream.getvalue()


def load(string: str) -> Any:
return yaml.load(string)
2 changes: 1 addition & 1 deletion agents-api/agents_api/routers/internal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .router import router
from .router import router as router
2 changes: 1 addition & 1 deletion agents-api/agents_api/routers/internal/router.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Request, Response
from fastapi import APIRouter, Request
from google.protobuf import json_format
from temporalio.api.common.v1 import Payloads

Expand Down
3 changes: 2 additions & 1 deletion agents-api/agents_api/routers/tasks/router.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from typing import Callable

import yaml
from fastapi import APIRouter, Request, Response
from fastapi.routing import APIRoute

from ...common.utils import yaml


class YamlRequest(Request):
async def body(self) -> bytes:
Expand Down
4 changes: 2 additions & 2 deletions agents-api/agents_api/routers/tasks/update_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def update_execution(
*get_temporal_workflow_data(execution_id=execution_id)
)
await wf_handle.cancel()
except Exception as e:
except Exception:
raise HTTPException(status_code=500, detail="Failed to stop execution")

case ResumeExecutionRequest():
Expand All @@ -59,7 +59,7 @@ async def update_execution(
)
try:
await act_handle.complete(data.input)
except Exception as e:
except Exception:
raise HTTPException(
status_code=500, detail="Failed to resume execution"
)
Expand Down
Loading

0 comments on commit 0855438

Please sign in to comment.