Skip to content

Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.

Proper PlxFactory and config

Louis Choquel0a422ef771ea
diff --git a/Makefile b/Makefile
index d9b38e0e3..ab8a0cd3a 100644
--- a/Makefile
+++ b/Makefile
@@ -455,11 +455,11 @@ docs-deploy: env
### SHORTHANDS
##########################################################################################
-c: format lint pyright pylint mypy
+c: format lint pyright mypy pylint
@echo "> done: c = check"
cc: cleanderived c
- @echo "> done: cc = cleanderived format lint pyright pylint mypy"
+ @echo "> done: cc = cleanderived format lint pyright mypy pylint"
check: cc check-unused-imports
@echo "> done: check"
diff --git a/pipelex/cli/commands/build_cmd.py b/pipelex/cli/commands/build_cmd.py
index 617dc0339..567c83224 100644
--- a/pipelex/cli/commands/build_cmd.py
+++ b/pipelex/cli/commands/build_cmd.py
@@ -6,11 +6,11 @@ import typer
from pipelex import pretty_print
from pipelex.hub import get_report_delegate
+from pipelex.language.plx_factory import PlxFactory
from pipelex.libraries.pipelines.builder.builder import PipelexBundleSpec
from pipelex.pipelex import Pipelex
from pipelex.pipeline.execute import execute_pipeline
from pipelex.tools.misc.file_utils import ensure_directory_for_file_path, save_text_to_path
-from pipelex.tools.plx.plx_utils import make_plx_content
build_app = typer.Typer(help="Build artifacts like pipelines", no_args_is_help=True)
@@ -60,7 +60,7 @@ def build_pipe_cmd(
)
pretty_print(pipe_output, title="Pipe Output")
pipelex_bundle_spec = pipe_output.working_memory.get_stuff_as(name="pipelex_bundle_spec", content_type=PipelexBundleSpec)
- plx_content = make_plx_content(blueprint=pipelex_bundle_spec.to_blueprint())
+ plx_content = PlxFactory.make_plx_content(blueprint=pipelex_bundle_spec.to_blueprint())
# Save to file unless explicitly disabled with --no-output
if not no_output:
diff --git a/pipelex/config.py b/pipelex/config.py
index 53580d9f2..96d062275 100644
--- a/pipelex/config.py
+++ b/pipelex/config.py
@@ -7,13 +7,13 @@ from pipelex.cogt.config_cogt import Cogt
from pipelex.cogt.model_backends.prompting_target import PromptingTarget
from pipelex.exceptions import PipelexConfigError, StaticValidationErrorType
from pipelex.hub import get_required_config
+from pipelex.language.plx_config import PlxConfig
from pipelex.libraries.library_config import LibraryConfig
from pipelex.pipeline.track.tracker_config import TrackerConfig
from pipelex.tools.aws.aws_config import AwsConfig
from pipelex.tools.config.config_model import ConfigModel
from pipelex.tools.config.config_root import ConfigRoot
from pipelex.tools.log.log_config import LogConfig
-from pipelex.tools.plx.plx_config import PlxConfig
from pipelex.tools.templating.templating_models import PromptingStyle
from pipelex.types import StrEnum
diff --git a/pipelex/tools/plx/__init__.py b/pipelex/language/__init__.py
similarity index 100%
rename from pipelex/tools/plx/__init__.py
rename to pipelex/language/__init__.py
diff --git a/pipelex/language/plx_config.py b/pipelex/language/plx_config.py
new file mode 100644
index 000000000..f5a8f65ea
--- /dev/null
+++ b/pipelex/language/plx_config.py
@@ -0,0 +1,28 @@
+from pipelex.tools.config.config_model import ConfigModel
+
+
+class PlxConfigStrings(ConfigModel):
+ prefer_literal: bool
+ force_multiline: bool
+ length_limit_to_multiline: int
+ ensure_trailing_newline: bool
+ ensure_leading_blank_line: bool
+
+
+class PlxConfigInlineTables(ConfigModel):
+ spaces_inside_curly_braces: bool
+
+
+class PlxConfigForConcepts(ConfigModel):
+ structure_field_ordering: list[str]
+
+
+class PlxConfigForPipes(ConfigModel):
+ field_ordering: list[str]
+
+
+class PlxConfig(ConfigModel):
+ strings: PlxConfigStrings
+ inline_tables: PlxConfigInlineTables
+ concepts: PlxConfigForConcepts
+ pipes: PlxConfigForPipes
diff --git a/pipelex/language/plx_factory.py b/pipelex/language/plx_factory.py
new file mode 100644
index 000000000..84354ea99
--- /dev/null
+++ b/pipelex/language/plx_factory.py
@@ -0,0 +1,305 @@
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any, Mapping, cast
+
+import tomlkit
+from tomlkit import array, document, inline_table, table
+from tomlkit import string as tomlkit_string
+
+from pipelex import log
+from pipelex.config import PlxConfig, get_config
+from pipelex.tools.misc.json_utils import remove_none_values_from_dict
+from pipelex.types import StrEnum
+
+if TYPE_CHECKING:
+ from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
+
+
+class SectionKey(StrEnum):
+ PIPE = "pipe"
+ CONCEPT = "concept"
+
+
+CONCEPT_STRUCTURE_FIELD_KEY = "structure"
+
+
+class PlxFactory:
+ @classmethod
+ def _plx_config(cls) -> PlxConfig:
+ return get_config().pipelex.plx_config
+
+ @classmethod
+ def _format_tomlkit_string(cls, text: str) -> Any: # Can't type this because of tomlkit
+ r"""Build a tomlkit string node.
+ - If `force_multiline` or the text contains '\\n', we emit a triple-quoted multiline string.
+ - When multiline, `ensure_trailing_newline` puts the closing quotes on their own line.
+ - When multiline, `ensure_leading_blank_line` inserts a real blank line at the start of the string.
+ """
+ strings_config = cls._plx_config().strings
+
+ needs_multiline = strings_config.force_multiline or ("\n" in text) or len(text) > strings_config.length_limit_to_multiline
+ normalized = text
+
+ if needs_multiline:
+ if strings_config.ensure_leading_blank_line and not normalized.startswith("\n"):
+ normalized = "\n" + normalized
+ if strings_config.ensure_trailing_newline and not normalized.endswith("\n"):
+ normalized = normalized + "\n"
+
+ use_literal = strings_config.prefer_literal and ("'''" not in normalized)
+ return tomlkit_string(normalized, multiline=needs_multiline, literal=use_literal)
+
+ @classmethod
+ def _convert_dicts_to_inline_tables(cls, value: Any, field_ordering: list[str] | None = None) -> Any: # Can't type this because of tomlkit
+ """Recursively convert Python values; dicts -> inline tables; lists kept as arrays."""
+ if isinstance(value, Mapping):
+ value = cast("Mapping[str, Any]", value)
+ inline_table_obj = inline_table()
+ if field_ordering:
+ for key in field_ordering:
+ if key in value:
+ inline_table_obj[key] = cls._convert_dicts_to_inline_tables(value=value[key])
+ else:
+ for key, value_item in value.items():
+ inline_table_obj[key] = cls._convert_dicts_to_inline_tables(value=value_item)
+ return inline_table_obj
+
+ elif isinstance(value, list):
+ value = cast("list[Any]", value)
+ array_obj = array()
+ array_obj.multiline(True) # set to False for single-line arrays
+ for element in value:
+ if isinstance(element, Mapping):
+ element = cast("Mapping[str, Any]", element)
+ inline_element = inline_table()
+ for inner_key, inner_value in element.items():
+ inline_element[inner_key] = cls._convert_dicts_to_inline_tables(value=inner_value)
+ array_obj.append(inline_element) # pyright: ignore[reportUnknownMemberType]
+ else:
+ array_obj.append(cls._convert_dicts_to_inline_tables(value=element)) # pyright: ignore[reportUnknownMemberType]
+ return array_obj
+
+ elif isinstance(value, str):
+ return cls._format_tomlkit_string(text=value)
+ else:
+ return value
+
+ @classmethod
+ def _convert_mapping_to_table(
+ cls, mapping: Mapping[str, Any], field_ordering: list[str] | None = None
+ ) -> Any: # Can't type this because of tomlkit
+ """Convert a mapping into a TOML Table where any nested mappings (third level+)
+ are converted to inline tables.
+
+ This creates a second-level standard table, and only uses inline tables for
+ third level and deeper structures.
+ """
+ tbl = table()
+
+ # If field ordering is provided, add fields in the specified order first
+ if field_ordering:
+ for field_key in field_ordering:
+ if field_key in mapping and field_key != "category": # Skip category field
+ field_value = mapping[field_key]
+ if isinstance(field_value, Mapping):
+ # Third-level mapping -> inline table
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ else:
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+
+ # Add any remaining fields not in the ordering
+ for field_key, field_value in mapping.items():
+ if field_key not in field_ordering and field_key != "category":
+ if isinstance(field_value, Mapping):
+ # Third-level mapping -> inline table
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ else:
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ else:
+ # No field ordering provided, use original logic
+ for field_key, field_value in mapping.items():
+ # Skip the category field as it's not needed in PLX output
+ if field_key == "category":
+ continue
+
+ if isinstance(field_value, Mapping):
+ # Third-level mapping -> inline table
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ else:
+ tbl.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ return tbl
+
+ # TODO: replace this by a proper toml formatter utility
+ @classmethod
+ def _add_spaces_to_inline_tables(cls, toml_string: str) -> str:
+ """Add spaces inside inline table curly braces.
+
+ Converts {key = value} to { key = value }.
+ Only adds spaces if they're not already present.
+ Handles nested inline tables properly.
+ """
+
+ def find_and_replace_inline_tables(text: str) -> str:
+ """Find inline tables using proper brace matching and add spaces."""
+ result = ""
+ char_index = 0
+
+ while char_index < len(text):
+ if text[char_index] == "{":
+ # Check if this is a Jinja2 template (double braces)
+ if char_index + 1 < len(text) and text[char_index + 1] == "{":
+ # This is a Jinja2 template, find the closing }}
+ jinja_end = text.find("}}", char_index + 2)
+ if jinja_end != -1:
+ # Add the entire Jinja2 template as-is
+ result += text[char_index : jinja_end + 2]
+ char_index = jinja_end + 2
+ continue
+
+ # Found start of inline table, find the matching closing brace
+ brace_count = 1
+ start = char_index
+ char_index += 1
+
+ while char_index < len(text) and brace_count > 0:
+ if text[char_index] == "{":
+ brace_count += 1
+ elif text[char_index] == "}":
+ brace_count -= 1
+ char_index += 1
+
+ if brace_count == 0:
+ # Found complete inline table
+ content = text[start + 1 : char_index - 1] # Content between braces
+
+ # Recursively process nested inline tables first
+ content = find_and_replace_inline_tables(content)
+
+ # Add spaces if not already present
+ if content.startswith(" ") and content.endswith(" "):
+ result += "{" + content + "}"
+ elif content.startswith(" "):
+ result += "{" + content + " }"
+ elif content.endswith(" "):
+ result += "{ " + content + "}"
+ else:
+ result += "{ " + content + " }"
+ else:
+ # Unmatched brace, add as-is
+ result += text[start:char_index]
+ else:
+ result += text[char_index]
+ char_index += 1
+
+ return result
+
+ return find_and_replace_inline_tables(toml_string)
+
+ @classmethod
+ def _make_table_obj_for_pipe(cls, section_value: Mapping[str, Any]) -> Any:
+ """Make a table object for a pipe section."""
+ log.debug("******** Making table object for pipe section ********")
+ table_obj = table()
+ for field_key, field_value in section_value.items():
+ log.debug(f"------ Field {field_key} is a {type(field_value)}")
+ if not isinstance(field_value, Mapping):
+ log.debug(f"Field is not a mapping: key = {field_key}, value = {field_value}")
+ table_obj.add(field_key, cls._convert_dicts_to_inline_tables(field_value))
+ continue
+ log.debug(f"Field is a mapping: key = {field_key}, value = {field_value}")
+ field_value = cast("Mapping[str, Any]", field_value)
+ table_obj.add(field_key, cls._convert_mapping_to_table(field_value, field_ordering=cls._plx_config().pipes.field_ordering))
+ return table_obj
+
+ @classmethod
+ def _make_table_obj_for_concept(cls, section_value: Mapping[str, Any]) -> Any:
+ """Make a table object for a concept section."""
+ log.debug("******** Making table object for concept section ********")
+ table_obj = table()
+ for concept_key, concept_value in section_value.items():
+ if isinstance(concept_value, str):
+ log.debug(f"Concept '{concept_key}' is a string: {concept_value}")
+ table_obj.add(concept_key, concept_value)
+ continue
+ if not isinstance(concept_value, Mapping):
+ msg = f"Concept field value is not a mapping: key = {concept_key}, value = {concept_value}"
+ raise TypeError(msg)
+ log.debug(f"Concept '{concept_key}' is a mapping: {concept_value}")
+ concept_value = cast("Mapping[str, Any]", concept_value)
+ concept_table_obj = table()
+ for concept_field_key, concept_field_value in concept_value.items():
+ if concept_field_key == CONCEPT_STRUCTURE_FIELD_KEY:
+ if isinstance(concept_field_value, str):
+ log.debug(f"Structure for concept '{concept_key}' is a string: {concept_field_value}")
+ concept_table_obj.add("structure", concept_field_value)
+ continue
+ if not isinstance(concept_field_value, Mapping):
+ msg = f"Structure field value is not a mapping: key = {concept_field_key}, value = {concept_field_value}"
+ raise TypeError(msg)
+ log.debug(f"Structure for concept '{concept_key}' is a mapping: {concept_field_value}")
+ structure_value = cast("Mapping[str, Any]", concept_field_value)
+ structure_table_obj = table()
+ for structure_field_key, structure_field_value in structure_value.items():
+ if isinstance(structure_field_value, str):
+ log.debug(f"Structure '{structure_field_key}' is a string: {structure_field_value}")
+ structure_table_obj.add(structure_field_key, structure_field_value)
+ continue
+ if not isinstance(structure_field_value, Mapping):
+ msg = (
+ f"Structure field value is neither a string nor a mapping: "
+ f"key = {structure_field_key}, value = {structure_field_value}"
+ )
+ raise TypeError(msg)
+ log.debug(f"Structure for '{concept_key}' is a mapping: {structure_field_value}")
+ structure_table_obj.add(
+ structure_field_key,
+ cls._convert_dicts_to_inline_tables(
+ value=structure_field_value, field_ordering=cls._plx_config().concepts.structure_field_ordering
+ ),
+ )
+ concept_table_obj.add("structure", structure_table_obj)
+ else:
+ # sub_table = _convert_mapping_to_table(concept_field_value)
+ log.debug(f"{concept_key}/'{concept_field_key}' is inline: {concept_field_value}")
+ concept_table_obj.add(concept_field_key, cls._convert_dicts_to_inline_tables(concept_field_value))
+ table_obj.add(concept_key, concept_table_obj)
+ return table_obj
+
+ @classmethod
+ def dict_to_plx_styled_toml(cls, data: Mapping[str, Any]) -> str:
+ """Top-level keys become tables; second-level mappings become tables; inline tables start at third level."""
+ log.debug("=" * 100)
+ data = remove_none_values_from_dict(data=data)
+ document_root = document()
+ for root_key, root_value in data.items():
+ if not isinstance(root_value, Mapping):
+ log.debug(f"Root root_key is not a mapping: key = {root_key}, value = {root_value}")
+ document_root.add(root_key, cls._convert_dicts_to_inline_tables(root_value))
+ continue
+
+ # It's a mapping, therefore it's a section
+ log.debug(f"Root {root_key} is a section -------------------")
+
+ section_key = SectionKey(root_key)
+ section_value = cast("Mapping[str, Any]", root_value)
+ # Skip empty mappings (empty concept and pipe sections)
+ if not section_value:
+ log.debug(f"Section {section_key} is empty, skipping")
+ continue
+ match section_key:
+ case SectionKey.PIPE:
+ table_obj_for_pipe = cls._make_table_obj_for_pipe(section_value)
+ document_root.add(section_key, table_obj_for_pipe)
+ case SectionKey.CONCEPT:
+ table_obj_for_concept = cls._make_table_obj_for_concept(section_value)
+ document_root.add(section_key, table_obj_for_concept)
+
+ toml_output = tomlkit.dumps(document_root) # pyright: ignore[reportUnknownMemberType]
+ if cls._plx_config().inline_tables.spaces_inside_curly_braces:
+ return cls._add_spaces_to_inline_tables(toml_output)
+ return toml_output
+
+ @classmethod
+ def make_plx_content(cls, blueprint: PipelexBundleBlueprint) -> str:
+ blueprint_dict = blueprint.model_dump(serialize_as_any=True)
+ return cls.dict_to_plx_styled_toml(data=blueprint_dict)
diff --git a/pipelex/pipelex.toml b/pipelex/pipelex.toml
index 0a42477f9..352b8b794 100644
--- a/pipelex/pipelex.toml
+++ b/pipelex/pipelex.toml
@@ -219,6 +219,19 @@ image_urls = [
# PLX config
####################################################################################################
-[pipelex.plx_config]
-length_limit_to_multiline_strings = 100
+[pipelex.plx_config.inline_tables]
+spaces_inside_curly_braces = true
+
+[pipelex.plx_config.strings]
+prefer_literal = false
+force_multiline = false
+length_limit_to_multiline = 100
+ensure_trailing_newline = true
+ensure_leading_blank_line = true
+
+[pipelex.plx_config.concepts]
+structure_field_ordering = ["type", "definition", "choices", "required"]
+
+[pipelex.plx_config.pipes]
+field_ordering = ["type", "definition", "inputs", "output"]
diff --git a/pipelex/tools/plx/plx_config.py b/pipelex/tools/plx/plx_config.py
deleted file mode 100644
index a43aff1cd..000000000
--- a/pipelex/tools/plx/plx_config.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from pipelex.tools.config.config_model import ConfigModel
-
-
-class PlxConfig(ConfigModel):
- length_limit_to_multiline_strings: int
diff --git a/pipelex/tools/plx/plx_utils.py b/pipelex/tools/plx/plx_utils.py
deleted file mode 100644
index 9e2a0563d..000000000
--- a/pipelex/tools/plx/plx_utils.py
+++ /dev/null
@@ -1,307 +0,0 @@
-from __future__ import annotations
-
-from typing import TYPE_CHECKING, Any, Mapping, cast
-
-import tomlkit
-from tomlkit import array, document, inline_table, table
-from tomlkit import string as tomlkit_string
-
-from pipelex import log
-from pipelex.config import get_config
-from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
-from pipelex.tools.misc.json_utils import remove_none_values_from_dict
-from pipelex.types import StrEnum
-
-if TYPE_CHECKING:
- from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
-
-
-class SectionKey(StrEnum):
- PIPE = "pipe"
- CONCEPT = "concept"
-
-
-CONCEPT_STRUCTURE_FIELD_KEY = "structure"
-
-# Field ordering for specific keys
-FieldOrdering = list[str]
-
-# Field ordering for different pipe types
-PIPE_FIELD_ORDERING: FieldOrdering = [
- "type",
- "definition",
- "inputs",
- "output",
-]
-
-STRUCTURE_FIELD_ORDERING: FieldOrdering = ["type", "definition", "choices", "required"]
-
-
-def _format_tomlkit_string(
- text: str,
- prefer_literal: bool = False,
- force_multiline: bool = False,
- ensure_trailing_newline: bool = True,
- ensure_leading_blank_line: bool = True,
-) -> Any: # Can't type this because of tomlkit
- r"""Build a tomlkit string node.
- - If `force_multiline` or the text contains '\\n', we emit a triple-quoted multiline string.
- - When multiline, `ensure_trailing_newline` puts the closing quotes on their own line.
- - When multiline, `ensure_leading_blank_line` inserts a real blank line at the start of the string.
- """
- length_limit = get_config().pipelex.plx_config.length_limit_to_multiline_strings
- needs_multiline = force_multiline or ("\n" in text) or len(text) > length_limit
- normalized = text
-
- if needs_multiline:
- if ensure_leading_blank_line and not normalized.startswith("\n"):
- normalized = "\n" + normalized
- if ensure_trailing_newline and not normalized.endswith("\n"):
- normalized = normalized + "\n"
-
- use_literal = prefer_literal and ("'''" not in normalized)
- return tomlkit_string(normalized, multiline=needs_multiline, literal=use_literal)
-
-
-def _convert_dicts_to_inline_tables(value: Any, field_ordering: FieldOrdering | None = None) -> Any: # Can't type this because of tomlkit
- """Recursively convert Python values; dicts -> inline tables; lists kept as arrays."""
- if isinstance(value, Mapping):
- value = cast("Mapping[str, Any]", value)
- inline_table_obj = inline_table()
- if field_ordering:
- for key in field_ordering:
- if key in value:
- inline_table_obj[key] = _convert_dicts_to_inline_tables(value=value[key])
- else:
- for key, value_item in value.items():
- inline_table_obj[key] = _convert_dicts_to_inline_tables(value=value_item)
- return inline_table_obj
-
- if isinstance(value, list):
- value = cast("list[Any]", value)
- array_obj = array()
- array_obj.multiline(True) # set to False for single-line arrays
- for element in value:
- if isinstance(element, Mapping):
- element = cast("Mapping[str, Any]", element)
- inline_element = inline_table()
- for inner_key, inner_value in element.items():
- inline_element[inner_key] = _convert_dicts_to_inline_tables(value=inner_value)
- array_obj.append(inline_element) # pyright: ignore[reportUnknownMemberType]
- else:
- array_obj.append(_convert_dicts_to_inline_tables(value=element)) # pyright: ignore[reportUnknownMemberType]
- return array_obj
-
- if isinstance(value, str):
- return _format_tomlkit_string(text=value)
- return value
-
-
-def _convert_mapping_to_table(mapping: Mapping[str, Any], field_ordering: FieldOrdering | None = None) -> Any: # Can't type this because of tomlkit
- """Convert a mapping into a TOML Table where any nested mappings (third level+)
- are converted to inline tables.
-
- This creates a second-level standard table, and only uses inline tables for
- third level and deeper structures.
- """
- tbl = table()
-
- # If field ordering is provided, add fields in the specified order first
- if field_ordering:
- for field_key in field_ordering:
- if field_key in mapping and field_key != "category": # Skip category field
- field_value = mapping[field_key]
- if isinstance(field_value, Mapping):
- # Third-level mapping -> inline table
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
- else:
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
-
- # Add any remaining fields not in the ordering
- for field_key, field_value in mapping.items():
- if field_key not in field_ordering and field_key != "category":
- if isinstance(field_value, Mapping):
- # Third-level mapping -> inline table
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
- else:
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
- else:
- # No field ordering provided, use original logic
- for field_key, field_value in mapping.items():
- # Skip the category field as it's not needed in PLX output
- if field_key == "category":
- continue
-
- if isinstance(field_value, Mapping):
- # Third-level mapping -> inline table
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
- else:
- tbl.add(field_key, _convert_dicts_to_inline_tables(field_value))
- return tbl
-
-
-# TODO: replace this by a proper toml formatter utility
-def _add_spaces_to_inline_tables(toml_string: str) -> str:
- """Add spaces inside inline table curly braces.
-
- Converts {key = value} to { key = value }.
- Only adds spaces if they're not already present.
- Handles nested inline tables properly.
- """
-
- def find_and_replace_inline_tables(text: str) -> str:
- """Find inline tables using proper brace matching and add spaces."""
- result = ""
- char_index = 0
-
- while char_index < len(text):
- if text[char_index] == "{":
- # Check if this is a Jinja2 template (double braces)
- if char_index + 1 < len(text) and text[char_index + 1] == "{":
- # This is a Jinja2 template, find the closing }}
- jinja_end = text.find("}}", char_index + 2)
- if jinja_end != -1:
- # Add the entire Jinja2 template as-is
- result += text[char_index : jinja_end + 2]
- char_index = jinja_end + 2
- continue
-
- # Found start of inline table, find the matching closing brace
- brace_count = 1
- start = char_index
- char_index += 1
-
- while char_index < len(text) and brace_count > 0:
- if text[char_index] == "{":
- brace_count += 1
- elif text[char_index] == "}":
- brace_count -= 1
- char_index += 1
-
- if brace_count == 0:
- # Found complete inline table
- content = text[start + 1 : char_index - 1] # Content between braces
-
- # Recursively process nested inline tables first
- content = find_and_replace_inline_tables(content)
-
- # Add spaces if not already present
- if content.startswith(" ") and content.endswith(" "):
- result += "{" + content + "}"
- elif content.startswith(" "):
- result += "{" + content + " }"
- elif content.endswith(" "):
- result += "{ " + content + "}"
- else:
- result += "{ " + content + " }"
- else:
- # Unmatched brace, add as-is
- result += text[start:char_index]
- else:
- result += text[char_index]
- char_index += 1
-
- return result
-
- return find_and_replace_inline_tables(toml_string)
-
-
-def dict_to_plx_styled_toml(data: Mapping[str, Any]) -> str:
- """Top-level keys become tables; second-level mappings become tables; inline tables start at third level."""
- log.debug("=" * 100)
- data = remove_none_values_from_dict(data=data)
- document_root = document()
- for root_key, root_value in data.items():
- if not isinstance(root_value, Mapping):
- log.debug(f"Root root_key is not a mapping: key = {root_key}, value = {root_value}")
- document_root.add(root_key, _convert_dicts_to_inline_tables(root_value))
- continue
-
- # It's a mapping, therefore it's a section
- log.debug(f"Root {root_key} is a section -------------------")
-
- section_key = SectionKey(root_key)
- section_value = cast("Mapping[str, Any]", root_value)
- # Skip empty mappings (empty concept and pipe sections)
- if not section_value:
- log.debug(f"Section {section_key} is empty, skipping")
- continue
- match section_key:
- case SectionKey.PIPE:
- table_obj_for_pipe = _make_table_obj_for_pipe(section_value)
- document_root.add(section_key, table_obj_for_pipe)
- case SectionKey.CONCEPT:
- table_obj_for_concept = _make_table_obj_for_concept(section_value)
- document_root.add(section_key, table_obj_for_concept)
-
- toml_output = tomlkit.dumps(document_root) # pyright: ignore[reportUnknownMemberType]
- return _add_spaces_to_inline_tables(toml_output)
-
-
-def _make_table_obj_for_pipe(section_value: Mapping[str, Any]) -> Any:
- """Make a table object for a pipe section."""
- log.debug("******** Making table object for pipe section ********")
- table_obj = table()
- for field_key, field_value in section_value.items():
- log.debug(f"------ Field {field_key} is a {type(field_value)}")
- if not isinstance(field_value, Mapping):
- log.debug(f"Field is not a mapping: key = {field_key}, value = {field_value}")
- table_obj.add(field_key, _convert_dicts_to_inline_tables(field_value))
- continue
- log.debug(f"Field is a mapping: key = {field_key}, value = {field_value}")
- field_value = cast("Mapping[str, Any]", field_value)
- table_obj.add(field_key, _convert_mapping_to_table(field_value, field_ordering=PIPE_FIELD_ORDERING))
- return table_obj
-
-
-def _make_table_obj_for_concept(section_value: Mapping[str, Any]) -> Any:
- """Make a table object for a concept section."""
- log.debug("******** Making table object for concept section ********")
- table_obj = table()
- for concept_key, concept_value in section_value.items():
- if isinstance(concept_value, str):
- log.debug(f"Concept '{concept_key}' is a string: {concept_value}")
- table_obj.add(concept_key, concept_value)
- continue
- if not isinstance(concept_value, Mapping):
- msg = f"Concept field value is not a mapping: key = {concept_key}, value = {concept_value}"
- raise TypeError(msg)
- log.debug(f"Concept '{concept_key}' is a mapping: {concept_value}")
- concept_value = cast("Mapping[str, Any]", concept_value)
- concept_table_obj = table()
- for concept_field_key, concept_field_value in concept_value.items():
- if concept_field_key == CONCEPT_STRUCTURE_FIELD_KEY:
- if isinstance(concept_field_value, str):
- log.debug(f"Structure for concept '{concept_key}' is a string: {concept_field_value}")
- concept_table_obj.add("structure", concept_field_value)
- continue
- if not isinstance(concept_field_value, Mapping):
- msg = f"Structure field value is not a mapping: key = {concept_field_key}, value = {concept_field_value}"
- raise TypeError(msg)
- log.debug(f"Structure for concept '{concept_key}' is a mapping: {concept_field_value}")
- structure_value = cast("Mapping[str, Any]", concept_field_value)
- structure_table_obj = table()
- for structure_field_key, structure_field_value in structure_value.items():
- if isinstance(structure_field_value, str):
- log.debug(f"Structure '{structure_field_key}' is a string: {structure_field_value}")
- structure_table_obj.add(structure_field_key, structure_field_value)
- continue
- if not isinstance(structure_field_value, Mapping):
- msg = f"Structure field value is neither a string nor a mapping: key = {structure_field_key}, value = {structure_field_value}"
- raise TypeError(msg)
- log.debug(f"Structure for '{concept_key}' is a mapping: {structure_field_value}")
- structure_table_obj.add(
- structure_field_key, _convert_dicts_to_inline_tables(value=structure_field_value, field_ordering=STRUCTURE_FIELD_ORDERING)
- )
- concept_table_obj.add("structure", structure_table_obj)
- else:
- # sub_table = _convert_mapping_to_table(concept_field_value)
- log.debug(f"{concept_key}/'{concept_field_key}' is inline: {concept_field_value}")
- concept_table_obj.add(concept_field_key, _convert_dicts_to_inline_tables(concept_field_value))
- table_obj.add(concept_key, concept_table_obj)
- return table_obj
-
-
-def make_plx_content(blueprint: PipelexBundleBlueprint) -> str:
- blueprint_dict = blueprint.model_dump(serialize_as_any=True)
- return dict_to_plx_styled_toml(data=blueprint_dict)
diff --git a/tests/unit/pipelex/create/test_structured_output_generator.py b/tests/unit/pipelex/core/concepts/test_structured_output_generator.py
similarity index 100%
rename from tests/unit/pipelex/create/test_structured_output_generator.py
rename to tests/unit/pipelex/core/concepts/test_structured_output_generator.py
diff --git a/tests/unit/pipelex/core/interpreter/test_interpreter.py b/tests/unit/pipelex/core/interpreter/test_interpreter.py
index df959ddef..74bcade72 100644
--- a/tests/unit/pipelex/core/interpreter/test_interpreter.py
+++ b/tests/unit/pipelex/core/interpreter/test_interpreter.py
@@ -6,7 +6,6 @@ import pytest
from pipelex import log, pretty_print
from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
from pipelex.core.interpreter import PipelexInterpreter
-from pipelex.tools.plx.plx_utils import make_plx_content
from tests.unit.pipelex.core.test_data import InterpreterTestCases
@@ -31,14 +30,6 @@ class TestPipelexInterpreter:
pretty_print(expected_blueprint, title=f"Expected blueprint {test_name}")
assert blueprint == expected_blueprint
- @pytest.mark.parametrize("test_name,expected_plx_content,blueprint", InterpreterTestCases.VALID_TEST_CASES)
- def test_make_plx_content(self, test_name: str, expected_plx_content: str, blueprint: PipelexBundleBlueprint):
- """Test making blueprint from various valid PLX content."""
- plx_content = make_plx_content(blueprint=blueprint)
- pretty_print(plx_content, title=f"Plx content {test_name}")
- pretty_print(expected_plx_content, title=f"Expected PLX content {test_name}")
- assert plx_content == expected_plx_content
-
@pytest.mark.parametrize("test_name,invalid_plx_content,expected_exception", InterpreterTestCases.ERROR_TEST_CASES)
def test_invalid_plx_should_raise_exception(self, test_name: str, invalid_plx_content: str, expected_exception: Type[Exception]):
"""Test that invalid PLX content raises appropriate exceptions."""
diff --git a/tests/unit/pipelex/create/__init__.py b/tests/unit/pipelex/create/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/tests/unit/pipelex/language/test_plx_factory.py b/tests/unit/pipelex/language/test_plx_factory.py
new file mode 100644
index 000000000..ac5aa56d4
--- /dev/null
+++ b/tests/unit/pipelex/language/test_plx_factory.py
@@ -0,0 +1,22 @@
+from pathlib import Path
+from typing import Type
+
+import pytest
+
+from pipelex import log, pretty_print
+from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
+from pipelex.language.plx_factory import PlxFactory
+
+from tests.unit.pipelex.core.test_data import InterpreterTestCases
+
+
+class TestPlxFactory:
+
+ @pytest.mark.parametrize("test_name,expected_plx_content,blueprint", InterpreterTestCases.VALID_TEST_CASES)
+ def test_make_plx_content(self, test_name: str, expected_plx_content: str, blueprint: PipelexBundleBlueprint):
+ """Test making blueprint from various valid PLX content."""
+ plx_content = PlxFactory.make_plx_content(blueprint=blueprint)
+ pretty_print(plx_content, title=f"Plx content {test_name}")
+ pretty_print(expected_plx_content, title=f"Expected PLX content {test_name}")
+ assert plx_content == expected_plx_content
+