Spaces:
Runtime error
Runtime error
Commit ·
ff75f4d
1
Parent(s): 2a85837
basic working version is final now
Browse files
app/services/agents/__pycache__/agent_service.cpython-312.pyc
CHANGED
|
Binary files a/app/services/agents/__pycache__/agent_service.cpython-312.pyc and b/app/services/agents/__pycache__/agent_service.cpython-312.pyc differ
|
|
|
app/services/agents/__pycache__/ai_extractor.cpython-312.pyc
CHANGED
|
Binary files a/app/services/agents/__pycache__/ai_extractor.cpython-312.pyc and b/app/services/agents/__pycache__/ai_extractor.cpython-312.pyc differ
|
|
|
app/services/agents/agent_service.py
CHANGED
|
@@ -517,24 +517,20 @@ class AgentService:
|
|
| 517 |
) -> Dict[str, Any]:
|
| 518 |
"""
|
| 519 |
Complete agent session and execute actions
|
| 520 |
-
|
| 521 |
-
Args:
|
| 522 |
-
session: Agent session
|
| 523 |
-
agent: Agent configuration
|
| 524 |
-
state_machine: State machine with collected data
|
| 525 |
-
|
| 526 |
-
Returns:
|
| 527 |
-
Completion response
|
| 528 |
"""
|
| 529 |
try:
|
| 530 |
session.status = SessionStatus.COMPLETED
|
| 531 |
session.completed_at = datetime.utcnow()
|
| 532 |
session.collected_data = state_machine.collected_data
|
| 533 |
-
|
| 534 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
actions = agent.config.get("actions", [])
|
| 536 |
action_results = []
|
| 537 |
-
|
| 538 |
for action_config in actions:
|
| 539 |
if action_config.get("trigger") == "on_completion" and action_config.get("enabled", True):
|
| 540 |
try:
|
|
@@ -542,9 +538,10 @@ class AgentService:
|
|
| 542 |
session=session,
|
| 543 |
action_type=action_config["type"],
|
| 544 |
action_config=action_config.get("config", {}),
|
| 545 |
-
session_data=
|
| 546 |
)
|
| 547 |
action_results.append(result)
|
|
|
|
| 548 |
except Exception as e:
|
| 549 |
logger.error(
|
| 550 |
f"Action {action_config['type']} failed for session {session.id}: {str(e)}",
|
|
@@ -555,9 +552,52 @@ class AgentService:
|
|
| 555 |
"success": False,
|
| 556 |
"error": str(e)
|
| 557 |
})
|
| 558 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
await self.db.commit()
|
| 560 |
-
|
| 561 |
logger.info(
|
| 562 |
f"Completed session {session.id} for agent '{agent.name}'",
|
| 563 |
extra={
|
|
@@ -566,30 +606,26 @@ class AgentService:
|
|
| 566 |
"actions_executed": len(action_results)
|
| 567 |
}
|
| 568 |
)
|
| 569 |
-
|
| 570 |
-
return {
|
| 571 |
-
"message": (
|
| 572 |
-
agent.completion_message
|
| 573 |
-
or "Thank you! Your submission is complete."
|
| 574 |
-
),
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
"current_step": None,
|
| 577 |
"step_prompt": None,
|
| 578 |
-
|
| 579 |
"is_completed": True,
|
| 580 |
-
|
| 581 |
-
"collected_data": state_machine.collected_data,
|
| 582 |
-
|
| 583 |
"actions_executed": action_results,
|
| 584 |
-
|
| 585 |
"session_id": session.id
|
| 586 |
}
|
| 587 |
-
|
| 588 |
except Exception as e:
|
| 589 |
await self.db.rollback()
|
| 590 |
logger.exception(f"Failed to complete session {session.id}: {str(e)}")
|
| 591 |
raise ValueError(f"Failed to complete session: {str(e)}")
|
| 592 |
-
|
| 593 |
async def _execute_action(
|
| 594 |
self,
|
| 595 |
session: AgentSession,
|
|
|
|
| 517 |
) -> Dict[str, Any]:
|
| 518 |
"""
|
| 519 |
Complete agent session and execute actions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
"""
|
| 521 |
try:
|
| 522 |
session.status = SessionStatus.COMPLETED
|
| 523 |
session.completed_at = datetime.utcnow()
|
| 524 |
session.collected_data = state_machine.collected_data
|
| 525 |
+
|
| 526 |
+
collected_data = state_machine.collected_data
|
| 527 |
+
|
| 528 |
+
# ==============================
|
| 529 |
+
# 1. EXECUTE COMPLETION ACTIONS
|
| 530 |
+
# ==============================
|
| 531 |
actions = agent.config.get("actions", [])
|
| 532 |
action_results = []
|
| 533 |
+
|
| 534 |
for action_config in actions:
|
| 535 |
if action_config.get("trigger") == "on_completion" and action_config.get("enabled", True):
|
| 536 |
try:
|
|
|
|
| 538 |
session=session,
|
| 539 |
action_type=action_config["type"],
|
| 540 |
action_config=action_config.get("config", {}),
|
| 541 |
+
session_data=collected_data
|
| 542 |
)
|
| 543 |
action_results.append(result)
|
| 544 |
+
|
| 545 |
except Exception as e:
|
| 546 |
logger.error(
|
| 547 |
f"Action {action_config['type']} failed for session {session.id}: {str(e)}",
|
|
|
|
| 552 |
"success": False,
|
| 553 |
"error": str(e)
|
| 554 |
})
|
| 555 |
+
|
| 556 |
+
# ==============================
|
| 557 |
+
# 2. GENERATE AI SUMMARY (NEW)
|
| 558 |
+
# ==============================
|
| 559 |
+
summary = ""
|
| 560 |
+
|
| 561 |
+
try:
|
| 562 |
+
summary = await self.ai_extractor.generate_summary(
|
| 563 |
+
agent_name=agent.name,
|
| 564 |
+
collected_data=collected_data
|
| 565 |
+
)
|
| 566 |
+
except Exception as e:
|
| 567 |
+
logger.warning(f"Summary generation failed: {str(e)}")
|
| 568 |
+
|
| 569 |
+
# fallback safe summary (no hardcoding fields)
|
| 570 |
+
summary = "Summary could not be generated, but your data has been saved successfully."
|
| 571 |
+
|
| 572 |
+
# ==============================
|
| 573 |
+
# 3. SAVE TO CHAT HISTORY
|
| 574 |
+
# ==============================
|
| 575 |
+
if session.conversation_id:
|
| 576 |
+
# final assistant message
|
| 577 |
+
await self.conv_service.add_message(
|
| 578 |
+
conversation_id=int(session.conversation_id),
|
| 579 |
+
role="assistant",
|
| 580 |
+
content=agent.completion_message or "Thank you! Your submission is complete.",
|
| 581 |
+
metadata={
|
| 582 |
+
"agent_session_id": session.id,
|
| 583 |
+
"is_completion": True
|
| 584 |
+
}
|
| 585 |
+
)
|
| 586 |
+
|
| 587 |
+
# system-style structured summary message
|
| 588 |
+
await self.conv_service.add_message(
|
| 589 |
+
conversation_id=int(session.conversation_id),
|
| 590 |
+
role="system",
|
| 591 |
+
content=f"Final Summary:\n{summary}",
|
| 592 |
+
metadata={
|
| 593 |
+
"agent_session_id": session.id,
|
| 594 |
+
"is_summary": True,
|
| 595 |
+
"collected_data": collected_data
|
| 596 |
+
}
|
| 597 |
+
)
|
| 598 |
+
|
| 599 |
await self.db.commit()
|
| 600 |
+
|
| 601 |
logger.info(
|
| 602 |
f"Completed session {session.id} for agent '{agent.name}'",
|
| 603 |
extra={
|
|
|
|
| 606 |
"actions_executed": len(action_results)
|
| 607 |
}
|
| 608 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
|
| 610 |
+
# ==============================
|
| 611 |
+
# 4. RESPONSE
|
| 612 |
+
# ==============================
|
| 613 |
+
return {
|
| 614 |
+
"message": agent.completion_message or "Thank you! Your submission is complete.",
|
| 615 |
"current_step": None,
|
| 616 |
"step_prompt": None,
|
|
|
|
| 617 |
"is_completed": True,
|
| 618 |
+
"collected_data": collected_data,
|
|
|
|
|
|
|
| 619 |
"actions_executed": action_results,
|
| 620 |
+
"summary": summary,
|
| 621 |
"session_id": session.id
|
| 622 |
}
|
| 623 |
+
|
| 624 |
except Exception as e:
|
| 625 |
await self.db.rollback()
|
| 626 |
logger.exception(f"Failed to complete session {session.id}: {str(e)}")
|
| 627 |
raise ValueError(f"Failed to complete session: {str(e)}")
|
| 628 |
+
|
| 629 |
async def _execute_action(
|
| 630 |
self,
|
| 631 |
session: AgentSession,
|
app/services/agents/ai_extractor.py
CHANGED
|
@@ -98,6 +98,59 @@ class AIValueExtractor:
|
|
| 98 |
|
| 99 |
return None
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
async def _ai_extract(
|
| 102 |
self,
|
| 103 |
user_message: str,
|
|
|
|
| 98 |
|
| 99 |
return None
|
| 100 |
|
| 101 |
+
async def generate_summary(self, agent_name: str, collected_data: dict) -> str:
|
| 102 |
+
"""
|
| 103 |
+
Generate a clean human-readable summary using Groq
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
prompt = f"""
|
| 108 |
+
You are a professional assistant that summarizes structured form data.
|
| 109 |
+
|
| 110 |
+
Agent: {agent_name}
|
| 111 |
+
|
| 112 |
+
Convert this into a short human-readable summary.
|
| 113 |
+
|
| 114 |
+
Rules:
|
| 115 |
+
- Do NOT output JSON
|
| 116 |
+
- Do NOT repeat keys like "customer_name:"
|
| 117 |
+
- Write natural language
|
| 118 |
+
- Keep it 3–6 lines max
|
| 119 |
+
- Make it professional and clear
|
| 120 |
+
|
| 121 |
+
DATA:
|
| 122 |
+
{json.dumps(collected_data, indent=2)}
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
from app.services.ai.groq_provider import GroqProvider
|
| 126 |
+
|
| 127 |
+
provider = GroqProvider()
|
| 128 |
+
|
| 129 |
+
response = await provider.chat(
|
| 130 |
+
model=self.model,
|
| 131 |
+
messages=[
|
| 132 |
+
{
|
| 133 |
+
"role": "system",
|
| 134 |
+
"content": "You convert structured data into clean human summaries."
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
"role": "user",
|
| 138 |
+
"content": prompt
|
| 139 |
+
}
|
| 140 |
+
],
|
| 141 |
+
temperature=0.3,
|
| 142 |
+
max_tokens=300
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
if not response or not getattr(response, "content", None):
|
| 146 |
+
raise ValueError("Empty summary response from Groq")
|
| 147 |
+
|
| 148 |
+
return response.content.strip()
|
| 149 |
+
|
| 150 |
+
except Exception as e:
|
| 151 |
+
logger.error(f"Summary generation failed: {str(e)}")
|
| 152 |
+
raise
|
| 153 |
+
|
| 154 |
async def _ai_extract(
|
| 155 |
self,
|
| 156 |
user_message: str,
|