-
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.
feat(agents-api): Add jinja templates support (#300)
* feat(agents-api): Add 'render_templates' field to sessions relation Signed-off-by: Diwank Singh Tomer <[email protected]> * feat(agents-api): Add jinja env Signed-off-by: Diwank Singh Tomer <[email protected]> * feat(agents-api): Add jinja templates support Signed-off-by: Diwank Singh Tomer <[email protected]> --------- Signed-off-by: Diwank Singh Tomer <[email protected]>
- Loading branch information
Showing
16 changed files
with
364 additions
and
156 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
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,38 @@ | ||
import arrow | ||
from jinja2.sandbox import ImmutableSandboxedEnvironment | ||
from jinja2schema import infer, to_json_schema | ||
from jsonschema import validate | ||
|
||
__all__ = [ | ||
"render_template", | ||
] | ||
|
||
# jinja environment | ||
jinja_env = ImmutableSandboxedEnvironment( | ||
autoescape=False, | ||
trim_blocks=True, | ||
lstrip_blocks=True, | ||
auto_reload=False, | ||
enable_async=True, | ||
loader=None, | ||
) | ||
|
||
# Add arrow to jinja | ||
jinja_env.globals["arrow"] = arrow | ||
|
||
|
||
# Funcs | ||
async def render_template( | ||
template_string: str, variables: dict, check: bool = False | ||
) -> str: | ||
# Parse template | ||
template = jinja_env.from_string(template_string) | ||
|
||
# If check is required, get required vars from template and validate variables | ||
if check: | ||
schema = to_json_schema(infer(template_string)) | ||
validate(instance=variables, schema=schema) | ||
|
||
# Render | ||
rendered = await template.render_async(**variables) | ||
return rendered |
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
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
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
67 changes: 67 additions & 0 deletions
67
agents-api/migrations/migrate_1714119679_session_render_templates.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,67 @@ | ||
# /usr/bin/env python3 | ||
|
||
MIGRATION_ID = "session_render_templates" | ||
CREATED_AT = 1714119679.493182 | ||
|
||
extend_sessions = { | ||
"up": """ | ||
?[render_templates, developer_id, session_id, updated_at, situation, summary, created_at, developer_id, metadata] := *sessions{ | ||
session_id, | ||
updated_at, | ||
situation, | ||
summary, | ||
created_at, | ||
developer_id | ||
}, | ||
metadata = {}, | ||
render_templates = false | ||
:replace sessions { | ||
developer_id: Uuid, | ||
session_id: Uuid, | ||
updated_at: Validity default [floor(now()), true], | ||
=> | ||
situation: String, | ||
summary: String? default null, | ||
created_at: Float default now(), | ||
metadata: Json default {}, | ||
render_templates: Bool default false, | ||
} | ||
""", | ||
"down": """ | ||
?[developer_id, session_id, updated_at, situation, summary, created_at, developer_id, metadata] := *sessions{ | ||
session_id, | ||
updated_at, | ||
situation, | ||
summary, | ||
created_at, | ||
developer_id | ||
}, metadata = {} | ||
:replace sessions { | ||
developer_id: Uuid, | ||
session_id: Uuid, | ||
updated_at: Validity default [floor(now()), true], | ||
=> | ||
situation: String, | ||
summary: String? default null, | ||
created_at: Float default now(), | ||
metadata: Json default {}, | ||
} | ||
""", | ||
} | ||
|
||
|
||
queries_to_run = [ | ||
extend_sessions, | ||
] | ||
|
||
|
||
def up(client): | ||
for q in queries_to_run: | ||
client.run(q["up"]) | ||
|
||
|
||
def down(client): | ||
for q in reversed(queries_to_run): | ||
client.run(q["down"]) |
Oops, something went wrong.