Skip to content

Commit 4b9658c

Browse files
committed
Refactor plan_service.py for readability and formatting
Improved code readability by reformatting function signatures, argument lists, and multi-line statements. Enhanced exception logging and standardized code style, including consistent spacing and indentation. No functional changes were made.
1 parent 6354ec2 commit 4b9658c

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

src/backend/v3/common/services/plan_service.py

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77

88
from v3.models.models import MPlan
99
import v3.models.messages as messages
10-
from common.models.messages_kernel import AgentMessageData, AgentMessageType, AgentType, PlanStatus
10+
from common.models.messages_kernel import (
11+
AgentMessageData,
12+
AgentMessageType,
13+
AgentType,
14+
PlanStatus,
15+
)
1116
from v3.config.settings import orchestration_config
1217
from common.utils.event_utils import track_event_if_configured
1318
import uuid
1419
from semantic_kernel.kernel_pydantic import Field
1520

16-
21+
1722
logger = logging.getLogger(__name__)
23+
24+
1825
def build_agent_message_from_user_clarification(
19-
human_feedback: messages.UserClarificationResponse,
20-
user_id: str
26+
human_feedback: messages.UserClarificationResponse, user_id: str
2127
) -> AgentMessageData:
2228
"""
2329
Convert a UserClarificationResponse (human feedback) into an AgentMessageData.
@@ -26,17 +32,18 @@ def build_agent_message_from_user_clarification(
2632
# e.g. HUMAN_AGENT = "Human_Agent", -> value becomes ('Human_Agent',)
2733
# Consider fixing that enum (remove trailing commas) so .value is a string.
2834
return AgentMessageData(
29-
plan_id = human_feedback.plan_id or "",
30-
user_id = user_id,
31-
m_plan_id = human_feedback.m_plan_id or None,
32-
agent = AgentType.HUMAN.value, # or simply "Human_Agent"
33-
agent_type = AgentMessageType.HUMAN_AGENT, # will serialize per current enum definition
34-
content = human_feedback.answer or "",
35-
raw_data = json.dumps(asdict(human_feedback)),
36-
steps = [], # intentionally empty
37-
next_steps = [] # intentionally empty
35+
plan_id=human_feedback.plan_id or "",
36+
user_id=user_id,
37+
m_plan_id=human_feedback.m_plan_id or None,
38+
agent=AgentType.HUMAN.value, # or simply "Human_Agent"
39+
agent_type=AgentMessageType.HUMAN_AGENT, # will serialize per current enum definition
40+
content=human_feedback.answer or "",
41+
raw_data=json.dumps(asdict(human_feedback)),
42+
steps=[], # intentionally empty
43+
next_steps=[], # intentionally empty
3844
)
3945

46+
4047
def build_agent_message_from_agent_message_response(
4148
agent_response: messages.AgentMessageResponse,
4249
user_id: str,
@@ -47,7 +54,6 @@ def build_agent_message_from_agent_message_response(
4754
"""
4855
# Robust timestamp parsing (accepts seconds or ms or missing)
4956

50-
5157
# Raw data serialization
5258
raw = getattr(agent_response, "raw_data", None)
5359
try:
@@ -56,7 +62,13 @@ def build_agent_message_from_agent_message_response(
5662
try:
5763
raw_str = json.dumps(asdict(agent_response))
5864
except Exception:
59-
raw_str = json.dumps({k: getattr(agent_response, k) for k in dir(agent_response) if not k.startswith("_")})
65+
raw_str = json.dumps(
66+
{
67+
k: getattr(agent_response, k)
68+
for k in dir(agent_response)
69+
if not k.startswith("_")
70+
}
71+
)
6072
elif isinstance(raw, (dict, list)):
6173
raw_str = json.dumps(raw)
6274
else:
@@ -69,7 +81,11 @@ def build_agent_message_from_agent_message_response(
6981
next_steps = getattr(agent_response, "next_steps", []) or []
7082

7183
# Agent name and type
72-
agent_name = getattr(agent_response, "agent", "") or getattr(agent_response, "agent_name", "") or getattr(agent_response, "source", "")
84+
agent_name = (
85+
getattr(agent_response, "agent", "")
86+
or getattr(agent_response, "agent_name", "")
87+
or getattr(agent_response, "source", "")
88+
)
7389
# Try to infer agent_type, fallback to AI_AGENT
7490
agent_type_raw = getattr(agent_response, "agent_type", None)
7591
if isinstance(agent_type_raw, AgentMessageType):
@@ -83,7 +99,11 @@ def build_agent_message_from_agent_message_response(
8399
agent_type = AgentMessageType.AI_AGENT
84100

85101
# Content
86-
content = getattr(agent_response, "content", "") or getattr(agent_response, "text", "") or ""
102+
content = (
103+
getattr(agent_response, "content", "")
104+
or getattr(agent_response, "text", "")
105+
or ""
106+
)
87107

88108
# plan_id / user_id fallback
89109
plan_id_val = getattr(agent_response, "plan_id", "") or ""
@@ -105,7 +125,9 @@ def build_agent_message_from_agent_message_response(
105125
class PlanService:
106126

107127
@staticmethod
108-
async def handle_plan_approval(human_feedback: messages.PlanApprovalResponse, user_id: str) -> bool:
128+
async def handle_plan_approval(
129+
human_feedback: messages.PlanApprovalResponse, user_id: str
130+
) -> bool:
109131
"""
110132
Process a PlanApprovalResponse coming from the client.
111133
@@ -132,7 +154,7 @@ async def handle_plan_approval(human_feedback: messages.PlanApprovalResponse, us
132154
if human_feedback.approved:
133155
plan = await memory_store.get_plan(human_feedback.plan_id)
134156
mplan.plan_id = human_feedback.plan_id
135-
mplan.team_id = plan.team_id # just to keep consistency
157+
mplan.team_id = plan.team_id # just to keep consistency
136158
orchestration_config.plans[human_feedback.m_plan_id] = mplan
137159
if plan:
138160
plan.overall_status = PlanStatus.approved
@@ -149,15 +171,15 @@ async def handle_plan_approval(human_feedback: messages.PlanApprovalResponse, us
149171
else:
150172
print("Plan not found in memory store.")
151173
return False
152-
else: #reject plan
174+
else: # reject plan
153175
track_event_if_configured(
154-
"PlanRejected",
155-
{
156-
"m_plan_id": human_feedback.m_plan_id,
157-
"plan_id": human_feedback.plan_id,
158-
"user_id": user_id,
159-
},
160-
)
176+
"PlanRejected",
177+
{
178+
"m_plan_id": human_feedback.m_plan_id,
179+
"plan_id": human_feedback.plan_id,
180+
"user_id": user_id,
181+
},
182+
)
161183
await memory_store.delete_plan_by_plan_id(human_feedback.plan_id)
162184

163185
except Exception as e:
@@ -166,7 +188,9 @@ async def handle_plan_approval(human_feedback: messages.PlanApprovalResponse, us
166188
return True
167189

168190
@staticmethod
169-
async def handle_agent_messages(agent_message: messages.AgentMessageResponse, user_id: str) -> bool:
191+
async def handle_agent_messages(
192+
agent_message: messages.AgentMessageResponse, user_id: str
193+
) -> bool:
170194
"""
171195
Process an AgentMessage coming from the client.
172196
@@ -181,20 +205,25 @@ async def handle_agent_messages(agent_message: messages.AgentMessageResponse, us
181205
ValueError on invalid state
182206
"""
183207
try:
184-
agent_msg = build_agent_message_from_agent_message_response(agent_message, user_id)
208+
agent_msg = build_agent_message_from_agent_message_response(
209+
agent_message, user_id
210+
)
185211

186212
# Persist if your database layer supports it.
187213
# Look for or implement something like: memory_store.add_agent_message(agent_msg)
188214
memory_store = await DatabaseFactory.get_database(user_id=user_id)
189215
await memory_store.add_agent_message(agent_msg)
190216
return True
191217
except Exception as e:
192-
logger.exception("Failed to handle human clarification -> agent message: %s", e)
218+
logger.exception(
219+
"Failed to handle human clarification -> agent message: %s", e
220+
)
193221
return False
194222

195-
196223
@staticmethod
197-
async def handle_human_clarification(human_feedback: messages.UserClarificationResponse, user_id: str) -> bool:
224+
async def handle_human_clarification(
225+
human_feedback: messages.UserClarificationResponse, user_id: str
226+
) -> bool:
198227
"""
199228
Process a UserClarificationResponse coming from the client.
200229
@@ -209,7 +238,9 @@ async def handle_human_clarification(human_feedback: messages.UserClarificationR
209238
ValueError on invalid state
210239
"""
211240
try:
212-
agent_msg = build_agent_message_from_user_clarification(human_feedback, user_id)
241+
agent_msg = build_agent_message_from_user_clarification(
242+
human_feedback, user_id
243+
)
213244

214245
# Persist if your database layer supports it.
215246
# Look for or implement something like: memory_store.add_agent_message(agent_msg)
@@ -218,5 +249,7 @@ async def handle_human_clarification(human_feedback: messages.UserClarificationR
218249

219250
return True
220251
except Exception as e:
221-
logger.exception("Failed to handle human clarification -> agent message: %s", e)
252+
logger.exception(
253+
"Failed to handle human clarification -> agent message: %s", e
254+
)
222255
return False

0 commit comments

Comments
 (0)