from __future__ import annotations import re from typing import Any COPY_NAME_INSTRUCTION = " Include attached digits and trailing numeric segments that are part of the printed name." _ROLE = re.compile( "\\b(?:seller|merchant|vendor|customer|bill[- ]?to|recipient|payee|payer|guest|organization|business|company|legal)\\b", re.IGNORECASE, ) _OTHER_VALUE = re.compile( "\\b(?:address|number|identifier|code|date|row|label|amount)\\b", re.IGNORECASE ) def prepare( fields: dict[str, dict[str, Any]], *, has_media: bool = False ) -> dict[str, dict[str, Any]]: if not has_media: return fields result = dict(fields) for name, spec in fields.items(): if spec.get("type") != "text" or spec.get("knowledge"): continue instructions = spec.get("instructions", "") if not isinstance(instructions, str): continue requested = re.split("[.\\n]", instructions, maxsplit=1)[0] if ( re.search("\\bname\\b", requested, re.IGNORECASE) and _ROLE.search(requested) and (not _OTHER_VALUE.search(requested)) ): result[name] = { **spec, "instructions": instructions + COPY_NAME_INSTRUCTION, } return result