File size: 10,654 Bytes
2c41dce | 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | """
Orchestrator Module
Central controller that coordinates agent execution in a fixed sequence.
"""
from typing import Dict, Any, Optional
import logging
from core.agent_base import Agent
from core.errors import ProofSystemError
from agents.input_validator import InputValidatorAgent
from agents.text_extraction_agent import TextExtractionAgent
from agents.hashing_agent import HashingAgent
from agents.metadata_agent import MetadataAgent
from agents.proof_builder import ProofBuilderAgent
from agents.storage_agent import SupabaseStorageAgent
from agents.verification_agent import VerificationAgent
from models.proof import Proof, VerificationResult
from sidecar.gemini_sidecar import GeminiSidecar
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class Orchestrator:
"""
Central orchestrator that manages the proof generation pipeline.
Coordinates agent execution and handles failures gracefully.
"""
def __init__(self):
"""Initialize all agents in the pipeline."""
self.input_validator = InputValidatorAgent()
self.text_extraction_agent = TextExtractionAgent()
self.hashing_agent = HashingAgent()
self.metadata_agent = MetadataAgent()
self.proof_builder = ProofBuilderAgent()
self.storage_agent = SupabaseStorageAgent()
self.verification_agent = VerificationAgent(self.storage_agent)
# AI Sidecar (optional, non-authoritative)
self.ai_sidecar = GeminiSidecar()
logger.info("Orchestrator initialized with all agents")
def create_proof(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Execute the full proof creation pipeline.
Args:
input_data: {
"type": "file" | "text",
"content": bytes | str,
"filename": str (optional)
}
Returns:
{
"success": bool,
"proof_id": str,
"proof": Proof,
"message": str
}
"""
try:
logger.info("Starting proof creation pipeline")
# Step 1: Validate input
logger.info("Step 1/6: Validating input")
validated_data = self.input_validator.execute(input_data)
# Step 2: Extract text (OCR if applicable)
logger.info("Step 2/6: Text extraction (OCR)")
ocr_data = self.text_extraction_agent.execute(validated_data)
# Step 3: Generate hash (ALWAYS on raw content, never OCR output)
logger.info("Step 3/6: Generating hash from raw content")
hashed_data = self.hashing_agent.execute(ocr_data)
# Step 4: Generate metadata (includes OCR results)
logger.info("Step 4/6: Generating metadata")
metadata_data = self.metadata_agent.execute(hashed_data)
# Step 5: Build proof
logger.info("Step 5/6: Building proof object")
proof_data = self.proof_builder.execute(metadata_data)
# Step 6: Save proof
logger.info("Step 6/6: Saving proof to storage")
storage_result = self.storage_agent.save_proof(proof_data["proof"])
logger.info(f"Proof created successfully: {proof_data['proof_id']}")
# OPTIONAL: AI Sidecar explains the proof (non-blocking)
# This does NOT affect the core response
result = {
"success": True,
"proof_id": proof_data["proof_id"],
"proof": proof_data["proof"],
"message": "Proof created and stored successfully"
}
# Add AI explanation if available (optional, non-authoritative)
if self.ai_sidecar.enabled:
try:
assistant_response = self.ai_sidecar.explain_proof(
proof_data["proof"]
)
result["assistant"] = assistant_response.to_dict()
logger.info("AI explanation added to response")
except Exception as e:
logger.warning(f"AI explanation failed (non-critical): {str(e)}")
# AI failure does not affect core response
return result
except ProofSystemError as e:
logger.error(f"Proof creation failed: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": e.__class__.__name__,
"message": "Proof creation failed"
}
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": "UnexpectedError",
"message": "An unexpected error occurred"
}
def ask_assistant(
self,
question: str,
proof_id: Optional[str] = None
) -> Dict[str, Any]:
"""
Ask the AI assistant a question about a proof.
This is a separate, optional endpoint - not part of core flow.
Args:
question: User's question
proof_id: Optional proof ID for context
Returns:
{
"success": bool,
"assistant": AssistantResponse dict,
"message": str
}
"""
if not self.ai_sidecar.enabled:
return {
"success": False,
"message": "AI assistant is not enabled. Set AI_ENABLED=true and configure GEMINI_API_KEY."
}
try:
logger.info(f"AI assistant query: {question[:50]}...")
# Get proof if provided
proof = None
if proof_id:
proof = self.storage_agent.get_proof(proof_id)
assistant_response = self.ai_sidecar.answer_question(
question,
proof
)
return {
"success": True,
"assistant": assistant_response.to_dict(),
"message": "Question answered"
}
except Exception as e:
logger.error(f"AI assistant query failed: {str(e)}")
return {
"success": False,
"error": str(e),
"message": "AI assistant query failed"
}
def verify_proof(self, proof_id: str, content: bytes) -> Dict[str, Any]:
"""
Verify an existing proof.
Args:
proof_id: Unique proof identifier
content: Original content to verify
Returns:
{
"success": bool,
"verification_result": VerificationResult,
"message": str
}
"""
try:
logger.info(f"Starting proof verification: {proof_id}")
result = self.verification_agent.execute({
"proof_id": proof_id,
"content": content
})
verification_result = result["verification_result"]
logger.info(f"Verification completed: {verification_result.message}")
result = {
"success": True,
"verification_result": verification_result,
"message": verification_result.message
}
# OPTIONAL: AI Sidecar explains verification (non-blocking)
if self.ai_sidecar.enabled:
try:
# Get original proof for context
proof = self.storage_agent.get_proof(proof_id)
assistant_response = self.ai_sidecar.explain_verification(
verification_result,
proof
)
result["assistant"] = assistant_response.to_dict()
logger.info("AI verification explanation added")
except Exception as e:
logger.warning(f"AI explanation failed (non-critical): {str(e)}")
# AI failure does not affect core response
return result
except ProofSystemError as e:
logger.error(f"Verification failed: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": e.__class__.__name__,
"message": "Verification failed"
}
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": "UnexpectedError",
"message": "An unexpected error occurred"
}
def get_proof(self, proof_id: str) -> Dict[str, Any]:
"""
Retrieve a proof from storage.
Args:
proof_id: Unique proof identifier
Returns:
{
"success": bool,
"proof": Proof | None,
"message": str
}
"""
try:
logger.info(f"Retrieving proof: {proof_id}")
proof = self.storage_agent.get_proof(proof_id)
if not proof:
return {
"success": False,
"proof": None,
"message": f"Proof not found: {proof_id}"
}
return {
"success": True,
"proof": proof,
"message": "Proof retrieved successfully"
}
except ProofSystemError as e:
logger.error(f"Proof retrieval failed: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": e.__class__.__name__,
"message": "Proof retrieval failed"
}
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return {
"success": False,
"error": str(e),
"error_type": "UnexpectedError",
"message": "An unexpected error occurred"
} |