|
| 1 | +"""Decorator functions for exposing agent configuration fields. |
| 2 | +
|
| 3 | +Each decorator is a marker — it annotates a zero-argument function |
| 4 | +whose return value is the coded default for a configuration field. |
| 5 | +External tooling discovers these markers in source text and extracts |
| 6 | +their arguments and return values. |
| 7 | +
|
| 8 | +At decoration time only the *key* is validated; the function is |
| 9 | +returned unchanged. |
| 10 | +""" |
| 11 | + |
| 12 | +from typing import Any, Callable, Optional |
| 13 | + |
| 14 | +from .exceptions import AgentDecoratorError |
| 15 | + |
| 16 | + |
| 17 | +def _validate_key(key: str) -> None: |
| 18 | + """Validate a decorator key. |
| 19 | +
|
| 20 | + Raises: |
| 21 | + AgentDecoratorError: If the key is empty or whitespace-only. |
| 22 | + """ |
| 23 | + if not key or not key.strip(): |
| 24 | + raise AgentDecoratorError( |
| 25 | + f"Decorator key must be a non-empty string, got {key!r}" |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def prompt_section( |
| 30 | + key: str, |
| 31 | + label: str, |
| 32 | + description: str, |
| 33 | + validation: Optional[dict[str, Any]] = None, |
| 34 | +) -> Callable: |
| 35 | + """Expose a prompt section for editing. |
| 36 | +
|
| 37 | + Args: |
| 38 | + key: Unique identifier for this prompt (e.g. ``"prompts.system"``). |
| 39 | + label: Human-readable label shown in the UI. |
| 40 | + description: Help text explaining what this prompt does. |
| 41 | + validation: Optional validation rules as a dict |
| 42 | + (e.g. ``{"format": "text", "max_length": 500}``). |
| 43 | +
|
| 44 | + Returns: |
| 45 | + A decorator that validates the key and returns the function unchanged. |
| 46 | +
|
| 47 | + Raises: |
| 48 | + AgentDecoratorError: If the key is empty or whitespace-only. |
| 49 | +
|
| 50 | + Example:: |
| 51 | +
|
| 52 | + @prompt_section( |
| 53 | + key="prompts.identity", |
| 54 | + label="Agent Identity", |
| 55 | + description="Core identity and role definition", |
| 56 | + validation={"format": "text", "max_length": 500}, |
| 57 | + ) |
| 58 | + def get_identity_prompt() -> str: |
| 59 | + return "You are an expert assistant..." |
| 60 | + """ |
| 61 | + _validate_key(key) |
| 62 | + |
| 63 | + def decorator(fn: Callable[[], str]) -> Callable[[], str]: |
| 64 | + return fn |
| 65 | + |
| 66 | + return decorator |
| 67 | + |
| 68 | + |
| 69 | +def agent_config( |
| 70 | + key: str, |
| 71 | + label: str, |
| 72 | + description: str, |
| 73 | +) -> Callable: |
| 74 | + """Expose an agent configuration value for editing. |
| 75 | +
|
| 76 | + Args: |
| 77 | + key: Unique identifier (e.g. ``"config.temperature"``). |
| 78 | + label: Human-readable label. |
| 79 | + description: Help text. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + A decorator that validates the key and returns the function unchanged. |
| 83 | +
|
| 84 | + Raises: |
| 85 | + AgentDecoratorError: If the key is empty or whitespace-only. |
| 86 | +
|
| 87 | + Example:: |
| 88 | +
|
| 89 | + @agent_config( |
| 90 | + key="config.temperature", |
| 91 | + label="Temperature", |
| 92 | + description="The temperature setting for the language model", |
| 93 | + ) |
| 94 | + def get_temperature() -> float: |
| 95 | + return 0.7 |
| 96 | + """ |
| 97 | + _validate_key(key) |
| 98 | + |
| 99 | + def decorator(fn: Callable) -> Callable: |
| 100 | + return fn |
| 101 | + |
| 102 | + return decorator |
| 103 | + |
| 104 | + |
| 105 | +def agent_model( |
| 106 | + key: str, |
| 107 | + label: str, |
| 108 | + description: str = "", |
| 109 | +) -> Callable: |
| 110 | + """Expose an agent model selection for editing. |
| 111 | +
|
| 112 | + Args: |
| 113 | + key: Unique identifier (e.g. ``"config.model"``). |
| 114 | + label: Human-readable label. |
| 115 | + description: Help text (optional). |
| 116 | +
|
| 117 | + Returns: |
| 118 | + A decorator that validates the key and returns the function unchanged. |
| 119 | +
|
| 120 | + Raises: |
| 121 | + AgentDecoratorError: If the key is empty or whitespace-only. |
| 122 | +
|
| 123 | + Example:: |
| 124 | +
|
| 125 | + @agent_model( |
| 126 | + key="config.model", |
| 127 | + label="LLM Model", |
| 128 | + description="The language model powering this agent", |
| 129 | + ) |
| 130 | + def get_model_name() -> str: |
| 131 | + return "sap/gpt-4o" |
| 132 | + """ |
| 133 | + _validate_key(key) |
| 134 | + |
| 135 | + def decorator(fn: Callable) -> Callable: |
| 136 | + return fn |
| 137 | + |
| 138 | + return decorator |
0 commit comments