Spaces:
No application file
No application file
File size: 7,395 Bytes
bce4c09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | """
Message Factory Service
Handles creation of AgentMessage objects with proper formatting and validation.
"""
import json
import uuid
from datetime import datetime
from typing import Dict, List, Optional, Any
from dataclasses import dataclass, asdict
from enum import Enum
class MessageType(Enum):
"""Types of messages Agent Brown can send"""
GENERATION_REQUEST = "generation_request"
REFINEMENT_REQUEST = "refinement_request"
VALIDATION_ERROR = "validation_error"
FINAL_APPROVAL = "final_approval"
@dataclass
class AgentMessage:
"""Schema for inter-agent communication following tech_specs.md"""
message_id: str
timestamp: str
sender: str
recipient: str
message_type: str
payload: Dict[str, Any]
context: Dict[str, Any]
def to_dict(self) -> Dict[str, Any]:
return asdict(self)
def to_json(self) -> str:
return json.dumps(self.to_dict(), indent=2)
class MessageFactory:
"""Factory for creating standardized AgentMessage objects"""
def __init__(self, session_id: str, conversation_id: str):
self.session_id = session_id
self.conversation_id = conversation_id
def create_generation_request(
self,
enhanced_prompt: str,
original_prompt: str,
dialogues: List[str],
style_tags: List[str],
panels: int,
language: str,
extras: List[str],
style_config: Dict[str, Any],
validation_score: float,
iteration: int,
) -> AgentMessage:
"""Create a generation request message for Agent Bayko"""
payload = {
"prompt": enhanced_prompt,
"original_prompt": original_prompt,
"style_tags": style_tags,
"panels": panels,
"language": language,
"extras": extras,
"style_config": style_config,
"generation_params": {
"quality": "high",
"aspect_ratio": "16:9",
"panel_layout": "sequential",
},
}
return AgentMessage(
message_id=f"msg_{uuid.uuid4().hex[:8]}",
timestamp=datetime.utcnow().isoformat() + "Z",
sender="agent_brown",
recipient="agent_bayko",
message_type=MessageType.GENERATION_REQUEST.value,
payload=payload,
context={
"conversation_id": self.conversation_id,
"session_id": self.session_id,
"iteration": iteration,
"previous_feedback": None,
"validation_score": validation_score,
},
)
def create_error_message(
self, issues: List[str], suggestions: List[str]
) -> AgentMessage:
"""Create error message for validation failures"""
return AgentMessage(
message_id=f"msg_{uuid.uuid4().hex[:8]}",
timestamp=datetime.utcnow().isoformat() + "Z",
sender="agent_brown",
recipient="user_interface",
message_type=MessageType.VALIDATION_ERROR.value,
payload={
"error": "Input validation failed",
"issues": issues,
"suggestions": suggestions,
},
context={
"conversation_id": self.conversation_id or "error",
"session_id": self.session_id or "error",
"iteration": 0,
"error_type": "validation",
},
)
def create_rejection_message(
self,
bayko_response: Dict[str, Any],
evaluation: Dict[str, Any],
iteration: int,
) -> AgentMessage:
"""Create rejection message for auto-rejected content"""
return AgentMessage(
message_id=f"msg_{uuid.uuid4().hex[:8]}",
timestamp=datetime.utcnow().isoformat() + "Z",
sender="agent_brown",
recipient="user_interface",
message_type=MessageType.VALIDATION_ERROR.value,
payload={
"error": "Content rejected",
"reason": evaluation["reason"],
"rejected_content": bayko_response,
"auto_rejection": True,
},
context={
"conversation_id": self.conversation_id,
"session_id": self.session_id,
"iteration": iteration,
"rejection_type": "quality",
},
)
def create_refinement_message(
self,
bayko_response: Dict[str, Any],
feedback: Dict[str, Any],
iteration: int,
) -> AgentMessage:
"""Create refinement request message"""
return AgentMessage(
message_id=f"msg_{uuid.uuid4().hex[:8]}",
timestamp=datetime.utcnow().isoformat() + "Z",
sender="agent_brown",
recipient="agent_bayko",
message_type=MessageType.REFINEMENT_REQUEST.value,
payload={
"original_content": bayko_response,
"feedback": feedback,
"specific_improvements": feedback.get(
"improvement_suggestions", []
),
"focus_areas": [
area
for area, score in [
("adherence", feedback.get("adherence_score", 0)),
(
"style_consistency",
feedback.get("style_consistency", 0),
),
("narrative_flow", feedback.get("narrative_flow", 0)),
(
"technical_quality",
feedback.get("technical_quality", 0),
),
]
if score < 0.7
],
"iteration": iteration,
},
context={
"conversation_id": self.conversation_id,
"session_id": self.session_id,
"iteration": iteration,
"previous_feedback": feedback,
"refinement_reason": "Quality below threshold",
},
)
def create_approval_message(
self,
bayko_response: Dict[str, Any],
feedback: Dict[str, Any],
iteration: int,
) -> AgentMessage:
"""Create final approval message"""
return AgentMessage(
message_id=f"msg_{uuid.uuid4().hex[:8]}",
timestamp=datetime.utcnow().isoformat() + "Z",
sender="agent_brown",
recipient="user_interface",
message_type=MessageType.FINAL_APPROVAL.value,
payload={
"approved_content": bayko_response,
"final_feedback": feedback,
"session_summary": {
"total_iterations": iteration,
"final_score": feedback.get("overall_score", 0),
"processing_complete": True,
},
},
context={
"conversation_id": self.conversation_id,
"session_id": self.session_id,
"iteration": iteration,
"final_approval": True,
"completion_timestamp": datetime.utcnow().isoformat() + "Z",
},
)
|