Formatter for `KEY:\\nvalue` paragraph-style message payloads.
| 3 | from .base import StatelessFormatter |
| 4 | |
| 5 | class ParagraphMessageFormatter(StatelessFormatter): |
| 6 | """Formatter for `KEY:\\nvalue` paragraph-style message payloads.""" |
| 7 | |
| 8 | def __init__(self): |
| 9 | super().__init__() |
| 10 | self._is_input_formatter = True |
| 11 | self._is_output_formatter = True |
| 12 | self._agent_introducer = """ |
| 13 | Your response must strictly and only follow the format below. |
| 14 | 1. Your response is a collection of key-value pairs. |
| 15 | 2. For each key-value pair, the key firstly comes and then follows a colon and then the next line follows the value. |
| 16 | 3. A key must write in one line and must not contain any newline. |
| 17 | 4. A value can contain multiple lines and end with ".". |
| 18 | FORMAT EXAMPLE: |
| 19 | KEY1: |
| 20 | value1 |
| 21 | KEY2: |
| 22 | value2 |
| 23 | KEY3: |
| 24 | value3 |
| 25 | ... |
| 26 | """ |
| 27 | |
| 28 | def format(self, message: str) -> dict: |
| 29 | """Parse paragraph text into key-value dictionary.""" |
| 30 | |
| 31 | if isinstance(message, dict): |
| 32 | return message |
| 33 | |
| 34 | result = {} |
| 35 | lines = message.split("\n") |
| 36 | current_key = None |
| 37 | current_content = [] |
| 38 | |
| 39 | for line in lines: |
| 40 | line = line.rstrip() |
| 41 | if ":" in line and not line.startswith(" "): |
| 42 | parts = line.split(":", 1) |
| 43 | if len(parts) == 2: |
| 44 | if current_key is not None and current_content: |
| 45 | content = "\n".join(current_content).strip() |
| 46 | if current_key in result: |
| 47 | result[current_key] += "\n" + content |
| 48 | else: |
| 49 | result[current_key] = content |
| 50 | |
| 51 | current_key = parts[0].strip() |
| 52 | current_content = [parts[1].strip()] if parts[1].strip() else [] |
| 53 | elif current_key is not None: |
| 54 | current_content.append(line) |
| 55 | |
| 56 | if current_key is not None and current_content: |
| 57 | content = "\n".join(current_content).strip() |
| 58 | if current_key in result: |
| 59 | result[current_key] += "\n" + content |
| 60 | else: |
| 61 | result[current_key] = content |
| 62 |
no outgoing calls
no test coverage detected