File size: 13,536 Bytes
edcd2ef | 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | """
Chat Orchestrator API - Phase 5
Main chat endpoint that orchestrates AI agents, MCP tools, and event publishing.
This is the heart of the AI-native todo application.
"""
import json
import uuid
from typing import Dict, Any
from fastapi import APIRouter, HTTPException, status, Depends
from sqlalchemy.orm import Session
from src.orchestrator import IntentDetector, SkillDispatcher, EventPublisher, Intent
from src.models.base import get_db
from src.models.task import Task
from src.models.conversation import Conversation
from src.models.message import Message, MessageRole
from src.utils.logging import get_logger
from src.utils.errors import ValidationError
logger = get_logger(__name__)
router = APIRouter(prefix="/chat", tags=["chat"])
# Initialize orchestrator components
intent_detector = IntentDetector()
skill_dispatcher = SkillDispatcher()
event_publisher = EventPublisher()
@router.post("/command")
async def chat_command(
request: Dict[str, Any],
db: Session = Depends(get_db)
) -> Dict[str, Any]:
"""
Process chat command through AI orchestrator flow.
Orchestrator Flow:
1. Load conversation context (if exists)
2. Detect user intent
3. Dispatch to appropriate skill agent
4. Validate skill output
5. Execute business logic (via MCP tools)
6. Publish Kafka events
7. Save conversation to database
8. Return response to user
Args:
request: Chat request with user_input, conversation_id, user_id
db: Database session
Returns:
Chat response with intent, confidence, and result
"""
user_input = request.get("user_input", "").strip()
conversation_id = request.get("conversation_id")
user_id = request.get("user_id") # From Phase III auth
if not user_input:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="user_input is required"
)
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="user_id is required"
)
correlation_id = str(uuid.uuid4())
logger.info(
"chat_command_start",
user_id=user_id,
conversation_id=conversation_id,
input_length=len(user_input),
correlation_id=correlation_id
)
try:
# Step 1: Load or create conversation
if conversation_id:
conversation = db.query(Conversation).filter(
Conversation.id == conversation_id
).first()
if not conversation:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Conversation not found"
)
else:
# Create new conversation
conversation = Conversation(
user_id=user_id,
dapr_state_key=f"conversation:{uuid.uuid4()}"
)
db.add(conversation)
db.commit()
db.refresh(conversation)
conversation_id = str(conversation.id)
# Step 2: Save user message to database
user_message = Message(
conversation_id=conversation.id,
role=MessageRole.USER,
content=user_input
)
db.add(user_message)
db.commit()
# Step 3: Detect intent
intent, confidence, metadata = intent_detector.detect_with_context(
user_input,
context={"user_id": user_id, "conversation_id": conversation_id}
)
logger.info(
"intent_detected",
intent=intent.value,
confidence=confidence,
correlation_id=correlation_id
)
# Step 4: Dispatch to skill agent
context = {
"user_id": user_id,
"conversation_id": conversation_id,
"correlation_id": correlation_id
}
skill_result = await skill_dispatcher.dispatch(intent, user_input, context)
# Step 5: Validate skill output
if skill_result.get("confidence", 0) < 0.7:
# Low confidence - ask for clarification
clarification_response = await _handle_low_confidence(
user_input,
intent,
confidence,
skill_result
)
# Save assistant message
assistant_message = Message(
conversation_id=conversation.id,
role=MessageRole.ASSISTANT,
content=clarification_response["response"],
intent_detected=intent.value,
confidence_score=confidence
)
db.add(assistant_message)
db.commit()
return clarification_response
# Step 6: Execute business logic based on intent
result = await _execute_intent(
intent,
skill_result,
user_id,
db,
correlation_id
)
# Step 7: Generate response message
response_text = result.get("message", _generate_default_response(intent, result))
# Step 8: Save assistant message with AI metadata
assistant_message = Message(
conversation_id=conversation.id,
role=MessageRole.ASSISTANT,
content=response_text,
intent_detected=intent.value,
skill_agent_used=skill_result.get("agent", "TaskAgent"),
confidence_score=confidence
)
db.add(assistant_message)
# Update conversation last_message_at
conversation.last_message_at = assistant_message.created_at
db.commit()
logger.info(
"chat_command_success",
intent=intent.value,
result_keys=list(result.keys()),
correlation_id=correlation_id
)
return {
"response": response_text,
"conversation_id": str(conversation_id),
"intent_detected": intent.value,
"skill_agent_used": skill_result.get("agent", "TaskAgent"),
"confidence_score": confidence,
**result
}
except HTTPException:
raise
except Exception as e:
logger.error(
"chat_command_error",
error=str(e),
correlation_id=correlation_id,
exc_info=True
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="An error occurred processing your request"
)
async def _handle_low_confidence(
user_input: str,
intent: Intent,
confidence: float,
skill_result: Dict[str, Any]
) -> Dict[str, Any]:
"""Handle low confidence detections with clarification."""
clarification_messages = {
Intent.CREATE_TASK: (
f"I think you want to create a task, but I'm not sure about the details. "
f"You said: '{user_input}'. Could you provide the task title?"
),
Intent.UPDATE_TASK: (
f"I'd like to help update your task, but I'm not sure which task or what changes. "
f"Could you clarify?"
),
Intent.UNKNOWN: (
f"I'm not sure what you'd like to do. You said: '{user_input}'. "
f"Could you rephrase that? I can help you create, update, complete, or list tasks."
)
}
message = clarification_messages.get(
intent,
f"I'm not sure I understood correctly. You said: '{user_input}'. Could you clarify?"
)
return {
"response": message,
"intent_detected": intent.value,
"confidence_score": confidence,
"clarification_needed": True
}
async def _execute_intent(
intent: Intent,
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Execute business logic based on intent."""
if intent == Intent.CREATE_TASK:
return await _create_task(skill_result, user_id, db, correlation_id)
elif intent == Intent.UPDATE_TASK:
return await _update_task(skill_result, user_id, db, correlation_id)
elif intent == Intent.COMPLETE_TASK:
return await _complete_task(skill_result, user_id, db, correlation_id)
elif intent == Intent.DELETE_TASK:
return await _delete_task(skill_result, user_id, db, correlation_id)
elif intent == Intent.QUERY_TASKS:
return await _query_tasks(skill_result, user_id, db)
elif intent == Intent.SET_REMINDER:
return await _set_reminder(skill_result, user_id, db, correlation_id)
else:
return {
"message": "I'm not sure how to help with that. Could you try rephrasing?",
"suggestion": "Try: 'Create a task to buy milk tomorrow'"
}
async def _create_task(
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Create task from skill result."""
try:
# Create task
task = Task(
title=skill_result["title"],
description=skill_result.get("description"),
due_date=skill_result.get("due_date"),
priority=skill_result.get("priority", "medium"),
tags=skill_result.get("tags", []),
reminder_config=skill_result.get("reminder_config"),
recurrence_rule=skill_result.get("recurrence_rule"),
user_id=user_id
)
db.add(task)
db.commit()
db.refresh(task)
# Publish events
await event_publisher.publish_task_event(
"task.created",
str(task.id),
task.to_dict(),
correlation_id
)
await event_publisher.publish_task_update(
str(task.id),
"created",
task.to_dict(),
correlation_id
)
await event_publisher.publish_audit_event(
"Task",
str(task.id),
"CREATE",
"user",
user_id,
new_values=task.to_dict(),
correlation_id=correlation_id
)
return {
"message": f"I've created a task '{task.title}' for you.",
"task_created": {
"task_id": str(task.id),
"title": task.title,
"due_date": task.due_date.isoformat() if task.due_date else None,
"priority": task.priority.value if task.priority else None
}
}
except Exception as e:
logger.error("create_task_failed", error=str(e))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create task"
)
async def _update_task(
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Update task."""
# TODO: Implement task update logic
return {
"message": "Task updates are coming soon!",
"skill_result": skill_result
}
async def _complete_task(
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Complete task."""
# TODO: Implement task completion logic
return {
"message": "Task completion is coming soon!",
"skill_result": skill_result
}
async def _delete_task(
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Delete task."""
# TODO: Implement task deletion logic
return {
"message": "Task deletion is coming soon!",
"skill_result": skill_result
}
async def _query_tasks(
skill_result: Dict[str, Any],
user_id: str,
db: Session
) -> Dict[str, Any]:
"""Query tasks."""
try:
# Build query filters
query = db.query(Task).filter(Task.user_id == user_id)
# Apply status filter
if skill_result.get("status"):
query = query.filter(Task.status == skill_result["status"])
# Apply priority filter
if skill_result.get("priority"):
query = query.filter(Task.priority == skill_result["priority"])
# Execute query
tasks = query.limit(20).all()
return {
"message": f"Found {len(tasks)} task(s)",
"tasks": [task.to_dict() for task in tasks]
}
except Exception as e:
logger.error("query_tasks_failed", error=str(e))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to query tasks"
)
async def _set_reminder(
skill_result: Dict[str, Any],
user_id: str,
db: Session,
correlation_id: str
) -> Dict[str, Any]:
"""Set reminder."""
# TODO: Implement reminder creation logic
return {
"message": "Reminder creation is coming soon!",
"skill_result": skill_result
}
def _generate_default_response(intent: Intent, result: Dict[str, Any]) -> str:
"""Generate default response for intent."""
responses = {
Intent.CREATE_TASK: "Task created successfully!",
Intent.UPDATE_TASK: "Task updated successfully!",
Intent.COMPLETE_TASK: "Great job! Task completed.",
Intent.DELETE_TASK: "Task deleted.",
Intent.QUERY_TASKS: f"Found {result.get('task_count', 0)} tasks.",
Intent.SET_REMINDER: "Reminder set!"
}
return responses.get(intent, "Done!")
|