Skip to content

Commit 6fdb523

Browse files
committed
resolve copilot comment: extracting this logic into a helper method _extract_session_info(data).
1 parent b32c10a commit 6fdb523

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/copilot-chat/src/copilot_agent/copilot_service.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,26 @@ def get_or_create_session(self, user_id, conv_id):
6363
self.sessions[session_key] = CoPilotConversation(LLMSession())
6464
return self.sessions[session_key]
6565

66+
def _extract_session_info(self, data):
67+
"""Extract and validate user_id and conv_id from request data."""
68+
if not data or 'data' not in data or 'messageInfo' not in data['data']:
69+
raise KeyError("Missing required fields: data.messageInfo")
70+
71+
message_info = data['data']['messageInfo']
72+
user_id = message_info.get('userId')
73+
conv_id = message_info.get('convId')
74+
75+
if not user_id or not conv_id:
76+
raise ValueError("Missing required fields: userId or convId")
77+
78+
return user_id, conv_id
79+
6680
def instance_operation(self):
6781
"""POST endpoint to handle copilot operations."""
6882
logger.info("Received request at /copilot/api/operation")
6983
try:
7084
data = request.get_json()
71-
# Validate required keys
72-
if not data or 'data' not in data or 'messageInfo' not in data['data']:
73-
return jsonify({"status": "error", "message": "Missing required fields: data.messageInfo"}), 400
74-
message_info = data['data']['messageInfo']
75-
user_id = message_info.get('userId')
76-
conv_id = message_info.get('convId')
77-
if not user_id or not conv_id:
78-
return jsonify({"status": "error", "message": "Missing required fields: userId or convId"}), 400
85+
user_id, conv_id = self._extract_session_info(data)
7986
copilot_conversation = self.get_or_create_session(user_id, conv_id)
8087

8188
in_parameters = copilot_conversation.build_in_parameters(data)
@@ -85,6 +92,8 @@ def instance_operation(self):
8592
"data": out_parameters.__dict__
8693
}
8794
return jsonify(response), 200
95+
except (KeyError, ValueError) as e:
96+
return jsonify({"status": "error", "message": str(e)}), 400
8897
except Exception as e:
8998
logger.info(f"Error handling copilot/api/operation: {e}")
9099
return jsonify({"status": "error", "message": str(e)}), 500
@@ -107,15 +116,14 @@ def on_chunk(chunk: str):
107116

108117
try:
109118
data = request.get_json()
110-
user_id = data['data']['messageInfo']['userId']
111-
conv_id = data['data']['messageInfo']['convId']
119+
user_id, conv_id = self._extract_session_info(data)
112120
copilot_conversation = self.get_or_create_session(user_id, conv_id)
113121
llm_session = copilot_conversation.llm_session
114122
# Attach streaming callback to existing session (no new session creation)
115123
llm_session.set_instance_stream_callback(on_chunk)
116-
except KeyError as e:
117-
logger.error(f"Missing key in JSON body for stream_operation: {e}")
118-
return jsonify({"status": "error", "message": f"Missing key: {e}"}), 400
124+
except (KeyError, ValueError) as e:
125+
logger.error(f"Missing or invalid fields in JSON body for stream_operation: {e}")
126+
return jsonify({"status": "error", "message": str(e)}), 400
119127
except Exception as e:
120128
logger.error(f"Failed to parse JSON body for stream_operation: {e}")
121129
return jsonify({"status": "error", "message": "invalid json"}), 400

0 commit comments

Comments
 (0)