-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtemplate.py
More file actions
42 lines (33 loc) · 1.25 KB
/
template.py
File metadata and controls
42 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Configuration for template nodes."""
from dataclasses import dataclass
from typing import Mapping, Any
from entity.configs.base import (
BaseConfig,
ConfigError,
ConfigFieldSpec,
require_mapping,
require_str,
)
@dataclass
class TemplateNodeConfig(BaseConfig):
"""Config describing the Jinja2 template used to format output."""
template: str = ""
@classmethod
def from_dict(cls, data: Mapping[str, Any], *, path: str) -> "TemplateNodeConfig":
mapping = require_mapping(data, path)
template = require_str(mapping, "template", path)
if not template:
raise ConfigError("template cannot be empty", f"{path}.template")
return cls(template=template, path=path)
def validate(self) -> None:
if not self.template:
raise ConfigError("template cannot be empty", f"{self.path}.template")
FIELD_SPECS = {
"template": ConfigFieldSpec(
name="template",
display_name="Jinja2 Template",
type_hint="text",
required=True,
description="Jinja2 template string for formatting output. Available context: {{ input }} (latest message content), {{ environment }} (execution environment variables).",
),
}