"""Lightweight DSPy text agent for written communication.""" import os import re import dspy def _clean(text: str) -> str: """Strip DSPy completion markers from output.""" return re.sub( r"\s*\[\[\s*##\s*completed\s*\]\]", "", text, ).strip() STYLE_GUIDE = ( "Write in the style of a senior government " "IT technical lead. Use numbered sections " "with bold headers. Be concise but thorough. " "Include specific dates, names, and action " "items with deadlines. Use markdown dashes " "for sub-bullets. End with a clear next step " "or offer to adjust. " "For emails: open with a warm one-liner, " "use a professional but approachable tone, " "close with signature block. " "For Teams: slightly more casual, use emoji " "sparingly, skip signature, keep same " "numbered structure but tighter wording." ) class ToneAnalyzer(dspy.Signature): """Analyze text tone. Return one of: casual, friendly, formal, urgent, neutral, professional, warm. """ text: str = dspy.InputField() tone: str = dspy.OutputField( desc="one of: casual, friendly, formal, " "urgent, neutral, professional, warm" ) class EmailDrafter(dspy.Signature): """Draft a structured professional email. Use numbered sections with clear headers. Include specific dates, action items with deadlines, and next steps. Open with a warm one-liner acknowledging the recipient. Close with a signature block. """ context: str = dspy.InputField( desc="situation, recipient, key facts, " "dates, and what needs to happen" ) tone: str = dspy.InputField( desc="casual, professional, urgent, " "friendly, or formal" ) style: str = dspy.InputField( desc="writing style guidance", default=STYLE_GUIDE, ) subject: str = dspy.OutputField( desc="email subject line with topic " "and specificity (e.g. Re: AI " "Governance — Finalizing Timeline)" ) body: str = dspy.OutputField( desc="full email body with numbered " "sections, sub-bullets, dates, " "action items, and signature block" ) class TeamsDrafter(dspy.Signature): """Draft a structured Teams message. Use numbered sections like email but tighter. Slightly more casual tone, emoji sparingly. No signature block. Include specific dates, action items, and next steps. """ context: str = dspy.InputField( desc="situation, recipient, key facts, " "dates, and what needs to happen" ) tone: str = dspy.InputField( desc="casual, professional, urgent, " "friendly, or formal" ) style: str = dspy.InputField( desc="writing style guidance", default=STYLE_GUIDE, ) message: str = dspy.OutputField( desc="Teams message with numbered " "sections, sub-bullets, dates, " "action items, no signature" ) def _load_if_exists(program, path): """Load optimized weights if file exists.""" if os.path.exists(path): program.load(path) return program class TextAgent: """Routes to the right drafter by channel. Automatically loads MIPRO-optimized prompts if optimized_*.json files exist alongside this module. """ def __init__(self, optimized_dir=None): if optimized_dir is None: optimized_dir = os.path.dirname( os.path.abspath(__file__) ) self._email = _load_if_exists( dspy.ChainOfThought(EmailDrafter), os.path.join( optimized_dir, "optimized_email.json", ), ) self._teams = _load_if_exists( dspy.ChainOfThought(TeamsDrafter), os.path.join( optimized_dir, "optimized_teams.json", ), ) self._tone = _load_if_exists( dspy.Predict(ToneAnalyzer), os.path.join( optimized_dir, "optimized_tone.json", ), ) def analyze_tone(self, text: str) -> str: return _clean( self._tone(text=text).tone ) def draft( self, context: str, channel: str = "email", tone: str = "professional", ) -> dict: if channel == "email": r = self._email( context=context, tone=tone, style=STYLE_GUIDE, ) return { "subject": _clean(r.subject), "body": _clean(r.body), } elif channel == "teams": r = self._teams( context=context, tone=tone, style=STYLE_GUIDE, ) return {"message": _clean(r.message)} else: raise ValueError( f"Unsupported channel: {channel}. " "Use 'email' or 'teams'." )