Add content conversion helper methods for Azure request handling

- Introduced `_content_to_text` method in `request_adapter.py` to convert various content types into a string format suitable for Azure.
- Updated existing logic to utilize this new method for processing message content.
- Added `_content_to_string` method in `logging.py` for similar content conversion, enhancing display capabilities for message content.
This commit is contained in:
dat-nguyen96
2026-03-12 13:18:15 +01:00
parent cb83b80709
commit e371af9cd2
2 changed files with 49 additions and 4 deletions
+25 -4
View File
@@ -27,6 +27,27 @@ class RequestAdapter:
self.adapter = adapter # AzureAdapter instance for shared config/env
# ---- Helpers (kept local to minimize cross-module coupling) ----
def _content_to_text(self, content: Any) -> str:
"""Convert message content (string or list of parts) to a string for Azure."""
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for part in content:
if isinstance(part, dict):
if part.get("type") == "text":
parts.append(part.get("text", ""))
elif part.get("type") == "image_url":
parts.append("[image]")
else:
parts.append(f"[{part.get('type', 'unknown')}]")
else:
parts.append(str(part))
return "\n".join(parts) if parts else ""
return str(content)
def _copy_request_headers_for_azure(
self, src: Request, *, api_key: str
) -> Dict[str, str]:
@@ -47,26 +68,26 @@ class RequestAdapter:
role = m.get("role")
content = m.get("content")
if role in {"system", "developer"}:
instructions_parts.append(content)
instructions_parts.append(self._content_to_text(content))
continue
# For user/assistant/tools as inputs
if role == "tool":
call_id = m.get("tool_call_id")
item = {
"type": "function_call_output",
"output": content,
"output": self._content_to_text(content),
"status": "completed",
"call_id": call_id,
}
input_items.append(item)
else:
text_content = self._content_to_text(content)
item = {
"role": role or "user",
"content": [
{
"type": "input_text" if role == "user" else "output_text",
"text": content,
"text": text_content,
},
],
}
+24
View File
@@ -100,11 +100,35 @@ def _capture_request_details(req: Request, request_id: str) -> Dict[str, Any]:
def escape_tags(text: str) -> str:
"""Escapes xml-like tags in text so that they are visible when rendered as Markdown."""
if text is None:
return ""
if not isinstance(text, str):
return str(text)
return re.sub(
"(<[^<\n]+?)(>)", "\\1>`\n", re.sub("(<)([^>\n]+?>)", "\n`<\\2", text)
).replace(">`\n\n\n`<", ">`\n\n`<")
def _content_to_string(content: Any) -> str:
"""Convert message content (string or list of parts) to a string for display."""
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for part in content:
if isinstance(part, dict):
if part.get("type") == "text":
parts.append(part.get("text", ""))
else:
parts.append(f"[{part.get('type', 'unknown')}]")
else:
parts.append(str(part))
return "\n".join(parts) if parts else ""
return str(content)
def create_message_panel(msg: Dict[str, Any], idx: int, total: int) -> Panel:
"""Create a Rich Panel for displaying a message.