Skip to content

Commit 5aa3b6d

Browse files
committed
Improve error handling in chat.py: include conversation and message IDs in streaming errors and metadata. Update smoke_test.py to use sys.executable for Python path.
1 parent f0c5125 commit 5aa3b6d

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

smoke_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def get_client_session(self):
9696

9797
# Prepare server parameters
9898
server_params = StdioServerParameters(
99-
command="python",
99+
command=sys.executable,
100100
args=[server_script],
101101
env={
102102
**os.environ,

src/tools/chat.py

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,42 @@ async def codebase_consultant(
131131
full_response = ""
132132
conversation_metadata = {}
133133

134-
async for line in response.aiter_lines():
135-
if not line:
136-
continue
137-
138-
# Handle metadata events
139-
if line.startswith("event: message"):
140-
continue
141-
142-
if line.startswith("data: "):
143-
data = line[6:] # Remove "data: " prefix
144-
if data == "[DONE]":
145-
break
146-
try:
147-
chunk = json.loads(data)
148-
149-
# Capture metadata with conversation ID
150-
if chunk.get("event") == "metadata" and "conversationId" in chunk:
151-
conversation_metadata = chunk
152-
await ctx.info(f"Conversation ID: {chunk['conversationId']}")
153-
continue
154-
155-
# Process content chunks
156-
if "choices" in chunk and len(chunk["choices"]) > 0:
157-
delta = chunk["choices"][0].get("delta", {})
158-
if delta and "content" in delta and delta["content"] is not None:
159-
full_response += delta["content"]
160-
except json.JSONDecodeError:
161-
pass
134+
try:
135+
async for line in response.aiter_lines():
136+
if not line:
137+
continue
138+
139+
# Handle metadata events
140+
if line.startswith("event: message"):
141+
continue
142+
143+
if line.startswith("data: "):
144+
data = line[6:] # Remove "data: " prefix
145+
if data == "[DONE]":
146+
break
147+
try:
148+
chunk = json.loads(data)
149+
150+
# Capture metadata with conversation ID and message ID
151+
if chunk.get("event") == "metadata":
152+
conv_id = chunk.get("conversationId")
153+
msg_id = chunk.get("messageId")
154+
if conv_id or msg_id:
155+
conversation_metadata = chunk
156+
await ctx.info(f"Conversation ID: {conv_id}, Message ID: {msg_id}")
157+
continue
158+
159+
# Process content chunks
160+
if "choices" in chunk and len(chunk["choices"]) > 0:
161+
delta = chunk["choices"][0].get("delta", {})
162+
if delta and "content" in delta and delta["content"] is not None:
163+
full_response += delta["content"]
164+
except json.JSONDecodeError:
165+
pass
166+
except Exception as streaming_error:
167+
# Include conversation and message IDs in streaming error response
168+
error_context = _format_metadata_context(conversation_metadata)
169+
return f"Error during streaming: {str(streaming_error)} {error_context}"
162170

163171
# Append conversation ID info to the response if we got one and it's a new conversation
164172
if conversation_metadata.get("conversationId") and not conversation_id:
@@ -171,4 +179,20 @@ async def codebase_consultant(
171179
error_msg = await handle_api_error(ctx, e, "chat completion")
172180
if isinstance(e, httpx.HTTPStatusError) and e.response.status_code == 404:
173181
return "Error: Not found (404): The requested resource could not be found. Check your conversation_id or data_sources."
174-
return error_msg
182+
return error_msg
183+
184+
185+
def _format_metadata_context(metadata: Dict) -> str:
186+
"""Format conversation metadata for error messages."""
187+
if not metadata:
188+
return ""
189+
190+
parts = []
191+
if metadata.get("conversationId"):
192+
parts.append(f"Conversation ID: {metadata['conversationId']}")
193+
if metadata.get("messageId"):
194+
parts.append(f"Message ID: {metadata['messageId']}")
195+
196+
if parts:
197+
return f"\n\n---\n**Debug Info:**\n" + "\n".join(f"- {p}" for p in parts)
198+
return ""

0 commit comments

Comments
 (0)