BAF Generator

besser.generators.agents.baf_generator._config_has_personalization_content(config) → bool[source]

Return True when config contains fields that warrant running the personalization pass (configure_agent + personalized_agent_model.py emission). A config with only system-level runtime settings does not.

besser.generators.agents.baf_generator.json_literal(value, indent: int = 2) → str[source]

JSON for a json.loads(r'''...''') literal in generated code.

Unlike Jinja’s tojson it keeps <, > and & readable (age > 65, not age \u003e 65); only ' is escaped, so the raw string cannot close early.

besser.generators.agents.baf_generator.extract_braced_vars(template: str) → list[str][source]

Return the unique {identifier} placeholders of template, in order of first appearance.

Used by the agent template to emit one session.get(...) substitution per session variable referenced in a message or prompt.

Parameters:

template (str) – Message or prompt text; None is treated as empty.

Returns:

Placeholder names without braces, e.g. ["name", "user_message"].

Return type:

list[str]

besser.generators.agents.baf_generator.sqlite_database_to_file(config_yaml: str) → str[source]

Rename database: to file: in sqlite db.sql entries of config_yaml.

BAF reads a sqlite path from file; older editor builds wrote database.

besser.generators.agents.baf_generator.workspace_rel_dir(path: str, name: str) → str[source]

Return the directory of a workspace relative to the generated agent’s folder.

Test sessions run the generated agent inside an isolated folder, so every declared workspace is created relative to it. A blank path falls back to the workspace name (as a safe identifier).

Parameters:
  • path (str) – The workspace path declared in the model (absolute or relative).

  • name (str) – The workspace name.

Returns:

A POSIX path relative to the output directory.

Return type:

str

Raises:

ValueError – If path would resolve outside the output directory (.. segments) or has no relative component (e.g. /).

besser.generators.agents.baf_generator.collect_gui_modules(agent: Agent) → dict[str, GUIReplyAction][source]

Map each guis/<module>.py module name to the GUIReplyAction it serves.

Walks the body and fallback body of every state and keeps the first GUIReplyAction per gui_id; the module name is safe_var_name(gui_id).

Parameters:

agent (Agent) – The agent model.

Returns:

Module name to action, in declaration order.

Return type:

dict[str, GUIReplyAction]

Raises:

ValueError – If two different gui_id values map to the same module name.

class besser.generators.agents.baf_generator.GenerationMode(*values)[source]

Bases: Enum

FULL = 'full'
PERSONALIZED_ONLY = 'personalized_only'
CODE_ONLY = 'code_only'
class besser.generators.agents.baf_generator.BAFGenerator(model: Agent, output_dir: str = None, config_path: str = None, config: dict = None, openai_api_key: str = None, generation_mode: GenerationMode | str = GenerationMode.FULL, config_yaml: str | None = None, test_mode: bool = False, a2a_descriptor: dict | None = None, deployment_component_metadata: bool = False, bind_host: str = 'localhost')[source]

Bases: GeneratorInterface

BAFGenerator is a class that implements the GeneratorInterface and is responsible for generating the agent code, using the BESSER Agent Framework (BAF), based on an input agent model.

Parameters:
  • model (Agent) – A agent model.

  • output_dir (str, optional) – The output directory where the generated code will be saved. Defaults to None.

  • generation_mode (GenerationMode | str, optional) – Controls which pipeline stages run. - GenerationMode.FULL (default): personalization (if config) + templated code. - GenerationMode.PERSONALIZED_ONLY: run personalization JSON/model export only. - GenerationMode.CODE_ONLY: skip personalization helpers, render templates immediately.

  • a2a_descriptor (dict, optional) – Resolved deployment topology and governance. Extends the authored agent graph when peers or an A2A server are present.

  • bind_host (str, optional) – Interface the websocket and Streamlit servers bind to in the default config.yaml. Defaults to "localhost" so a standalone agent is not exposed on the network; container builds (e.g. DockerComposeGenerator) pass "0.0.0.0" so the published ports reach the servers. A supplied config_yaml is written as-is.

_abc_impl = <_abc._abc_data object>
generate()[source]

Generates the BAF agent code and saves it to the specified output directory. If the output directory was not specified, the code generated will be stored in the <current directory>/output folder.

_generate_guis(env: Environment, gui_modules: dict[str, GUIReplyAction])[source]

Write the guis package: one module per GUIReplyAction exposing gui.

Each module holds the BUML code of the referenced Agent.gui_models entry (gui_model) followed by the AgentGUI wrapper rendered from agent_gui.py.j2.

Raises:

ValueError – If a GUIReplyAction references a gui_id that has no entry in Agent.gui_models.