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.
Use multi-line TOML strings for prompt/template fields (#777)
Louis Choquel62044dc7c6ed
diff --git a/pipelex/cli/agent_cli/commands/concept_cmd.py b/pipelex/cli/agent_cli/commands/concept_cmd.pyindex 941307be8..5b51c3245 100644--- a/pipelex/cli/agent_cli/commands/concept_cmd.py+++ b/pipelex/cli/agent_cli/commands/concept_cmd.py@@ -11,6 +11,7 @@ from pydantic import ValidationErrorfrom pipelex.builder.concept.concept_spec import ConceptSpec, ConceptStructureSpecfrom pipelex.cli.agent_cli.commands.agent_output import agent_error, agent_success+from pipelex.language.toml_string_utils import format_toml_stringfrom pipelex.tools.typing.pydantic_utils import format_pydantic_validation_error_for_agent@@ -30,7 +31,7 @@ def _concept_spec_to_toml(concept_spec: ConceptSpec) -> str:concept_item_table = tomlkit.table()# Add description- concept_item_table.add("description", concept_spec.description)+ concept_item_table.add("description", format_toml_string(concept_spec.description))# Add refines if presentif concept_spec.refines:@@ -43,7 +44,7 @@ def _concept_spec_to_toml(concept_spec: ConceptSpec) -> str:field_dict = _structure_field_to_dict(field_spec)# If only description is present, use simple string formatif len(field_dict) == 1 and "description" in field_dict:- structure_table.add(field_name, field_dict["description"])+ structure_table.add(field_name, format_toml_string(field_dict["description"]))else:inline_table = tomlkit.inline_table()for key, value in field_dict.items():diff --git a/pipelex/cli/agent_cli/commands/pipe_cmd.py b/pipelex/cli/agent_cli/commands/pipe_cmd.pyindex c25c110b2..2fa6db90f 100644--- a/pipelex/cli/agent_cli/commands/pipe_cmd.py+++ b/pipelex/cli/agent_cli/commands/pipe_cmd.py@@ -29,6 +29,7 @@ from pipelex.builder.talents.img_gen_talent import ImgGenTalentfrom pipelex.builder.talents.llm_talent import LLMTalentfrom pipelex.cli.agent_cli.commands.agent_output import agent_error, agent_successfrom pipelex.core.pipes.pipe_blueprint import PipeType+from pipelex.language.toml_string_utils import format_toml_stringfrom pipelex.tools.typing.pydantic_utils import format_pydantic_validation_error_for_agent# Static talent-to-model preset mappings (from pipelex.toml defaults)@@ -99,7 +100,7 @@ def _pipe_spec_to_toml(pipe_spec: PipeSpec) -> str:pipe_item_table.add("type", pipe_spec.type)# Add description- pipe_item_table.add("description", pipe_spec.description)+ pipe_item_table.add("description", format_toml_string(pipe_spec.description))# Add inputs as inline tableif pipe_spec.inputs:@@ -133,9 +134,9 @@ def _add_type_specific_fields(pipe_spec: PipeSpec, pipe_table: tomlkit.TOMLDocummodel_preset = LLM_TALENT_TO_MODEL.get(pipe_spec.llm_talent, "$writing-creative")pipe_table.add("model", model_preset)if pipe_spec.system_prompt:- pipe_table.add("system_prompt", pipe_spec.system_prompt)+ pipe_table.add("system_prompt", format_toml_string(pipe_spec.system_prompt))if pipe_spec.prompt:- pipe_table.add("prompt", pipe_spec.prompt)+ pipe_table.add("prompt", format_toml_string(pipe_spec.prompt))elif isinstance(pipe_spec, PipeComposeSpec):if pipe_spec.construct_spec is not None:@@ -156,7 +157,7 @@ def _add_type_specific_fields(pipe_spec: PipeSpec, pipe_table: tomlkit.TOMLDocumif pipe_spec.target_format is not None:pipe_table.add("target_format", str(pipe_spec.target_format))if pipe_spec.template is not None:- pipe_table.add("template", pipe_spec.template)+ pipe_table.add("template", format_toml_string(pipe_spec.template))elif isinstance(pipe_spec, PipeSequenceSpec):steps_array = tomlkit.array()@@ -205,7 +206,7 @@ def _add_type_specific_fields(pipe_spec: PipeSpec, pipe_table: tomlkit.TOMLDocum# Convert img_gen_talent to model preset using static mappingsmodel_preset = IMG_GEN_TALENT_TO_MODEL.get(pipe_spec.img_gen_talent, "$gen-image")pipe_table.add("model", model_preset)- pipe_table.add("prompt", pipe_spec.prompt)+ pipe_table.add("prompt", format_toml_string(pipe_spec.prompt))elif isinstance(pipe_spec, PipeFuncSpec):pipe_table.add("function_name", pipe_spec.function_name)diff --git a/pipelex/language/mthds_factory.py b/pipelex/language/mthds_factory.pyindex 236a12e68..4501276b4 100644--- a/pipelex/language/mthds_factory.py+++ b/pipelex/language/mthds_factory.py@@ -4,10 +4,10 @@ from typing import TYPE_CHECKING, Any, Mapping, castimport tomlkitfrom tomlkit import array, document, inline_table, table-from tomlkit import string as tomlkit_stringfrom pipelex import logfrom pipelex.config import get_config+from pipelex.language.toml_string_utils import format_toml_stringfrom pipelex.tools.misc.json_utils import remove_none_values_from_dictfrom pipelex.types import StrEnum@@ -35,23 +35,18 @@ class MthdsFactory:@classmethoddef format_tomlkit_string(cls, text: str) -> Any: # Can't type this because of tomlkitr"""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.++ Delegates to the shared `format_toml_string` utility, passing config values."""strings_config = cls._mthds_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 += "\n"-- use_literal = strings_config.prefer_literal and ("'''" not in normalized)- return tomlkit_string(normalized, multiline=needs_multiline, literal=use_literal)+ return format_toml_string(+ text,+ force_multiline=strings_config.force_multiline,+ length_limit_to_multiline=strings_config.length_limit_to_multiline,+ ensure_trailing_newline=strings_config.ensure_trailing_newline,+ ensure_leading_blank_line=strings_config.ensure_leading_blank_line,+ prefer_literal=strings_config.prefer_literal,+ )@classmethoddef convert_dicts_to_inline_tables(cls, value: Any, field_ordering: list[str] | None = None) -> Any: # Can't type this because of tomlkitdiff --git a/pipelex/language/toml_string_utils.py b/pipelex/language/toml_string_utils.pynew file mode 100644index 000000000..121f1f1c6--- /dev/null+++ b/pipelex/language/toml_string_utils.py@@ -0,0 +1,43 @@+"""Shared TOML string formatting utilities.++Provides a pure function for building tomlkit string nodes with multi-line+support, used by both MthdsFactory (with config) and the agent CLI (with defaults).+"""++from __future__ import annotations++from typing import Any++from tomlkit import string as tomlkit_string+++def format_toml_string(+ text: str,+ *,+ force_multiline: bool = False,+ length_limit_to_multiline: int = 100,+ ensure_trailing_newline: bool = True,+ ensure_leading_blank_line: bool = True,+ prefer_literal: bool = False,+) -> Any: # Can't type this because of tomlkit+ r"""Build a tomlkit string node.++ - If `force_multiline` or the text contains '\n' or exceeds `length_limit_to_multiline`,+ 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.++ Default parameter values match pipelex.toml defaults so callers without+ config access get consistent behavior.+ """+ needs_multiline = force_multiline or ("\n" in text) or len(text) > length_limit_to_multiline+ 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 += "\n"++ use_literal = prefer_literal and ("'''" not in normalized)+ return tomlkit_string(normalized, multiline=needs_multiline, literal=use_literal)diff --git a/tests/unit/pipelex/language/test_toml_string_utils.py b/tests/unit/pipelex/language/test_toml_string_utils.pynew file mode 100644index 000000000..38e5f6d02--- /dev/null+++ b/tests/unit/pipelex/language/test_toml_string_utils.py@@ -0,0 +1,81 @@+"""Unit tests for format_toml_string — shared TOML string formatting utility."""++from __future__ import annotations++import pytest++from pipelex.language.toml_string_utils import format_toml_string+++class TestFormatTomlString:+ """Verify format_toml_string produces correct tomlkit string nodes."""++ def test_empty_string_stays_basic(self) -> None:+ """An empty string should remain a single-line basic string."""+ result = format_toml_string("")+ rendered = result.as_string()+ assert rendered == '""'++ def test_short_string_stays_basic(self) -> None:+ """A short string without newlines should remain a single-line basic string."""+ result = format_toml_string("hello world")+ rendered = result.as_string()+ assert rendered == '"hello world"'++ def test_newline_triggers_multiline(self) -> None:+ """A string containing newlines should become a multi-line basic string."""+ result = format_toml_string("line1\nline2")+ rendered = result.as_string()+ assert rendered.startswith('"""')+ assert "line1\nline2" in result.value++ def test_long_string_triggers_multiline(self) -> None:+ """A string exceeding length_limit_to_multiline should become multi-line."""+ long_text = "a" * 101+ result = format_toml_string(long_text)+ rendered = result.as_string()+ assert rendered.startswith('"""')++ def test_trailing_newline_added(self) -> None:+ """By default, a multi-line string gets a trailing newline."""+ result = format_toml_string("line1\nline2")+ assert result.value.endswith("\n")++ def test_leading_blank_line_added(self) -> None:+ """By default, a multi-line string gets a leading blank line."""+ result = format_toml_string("line1\nline2")+ assert result.value.startswith("\n")++ def test_no_trailing_newline_when_disabled(self) -> None:+ result = format_toml_string("line1\nline2", ensure_trailing_newline=False)+ assert result.value == "\nline1\nline2"++ def test_no_leading_blank_line_when_disabled(self) -> None:+ result = format_toml_string("line1\nline2", ensure_leading_blank_line=False)+ assert result.value == "line1\nline2\n"++ def test_force_multiline(self) -> None:+ """A short string should become multi-line when force_multiline=True."""+ result = format_toml_string("short", force_multiline=True)+ rendered = result.as_string()+ assert rendered.startswith('"""')+ assert result.value == "\nshort\n"++ def test_prefer_literal(self) -> None:+ """When prefer_literal=True, should use literal multi-line strings."""+ result = format_toml_string("line1\nline2", prefer_literal=True)+ rendered = result.as_string()+ assert rendered.startswith("'''")++ @pytest.mark.parametrize(+ "text",+ [+ "text with ''' inside",+ "'''leading triple quotes",+ ],+ )+ def test_literal_fallback_when_triple_quotes_present(self, text: str) -> None:+ """Falls back to basic multi-line when text contains triple single-quotes."""+ result = format_toml_string(text, prefer_literal=True, force_multiline=True)+ rendered = result.as_string()+ assert rendered.startswith('"""')