Working through the request.
" card_html = ( f"" ) return {"id": record.get("id", ""), "role": role, "html": card_html} def _render_message_blocks(record: dict[str, Any]) -> tuple[str, set[str]]: blocks = _ensure_message_blocks(record) if len(blocks) == 0: return "", set() rendered = [] rendered_attachment_keys = set() reasoning_total = sum(1 for block in blocks if isinstance(block, dict) and block.get("type") == "reasoning" and len(str(block.get("text", "")).strip()) > 0) reasoning_no = 0 for block in blocks: if not isinstance(block, dict): continue block_type = str(block.get("type", "markdown")).strip().lower() or "markdown" if block_type == "markdown": content_source, attachments = _extract_attachments_from_markdown(block.get("text", "")) content_html = _markdown_to_html(content_source) if len(content_html) > 0: rendered.append(content_html) attachment_html = _render_attachments(_dedupe_attachments(attachments, rendered_attachment_keys)) if len(attachment_html) > 0: rendered.append(attachment_html) continue if block_type == "reasoning": reasoning_text = str(block.get("text", "")).strip() if len(reasoning_text) == 0: continue reasoning_no += 1 rendered.append(_render_reasoning_block(block, reasoning_no, reasoning_total)) continue if block_type == "tool": rendered.append(_render_tool_block(block)) attachment_html = _render_attachments(_dedupe_attachments([block.get("attachment")] if isinstance(block.get("attachment"), dict) else [], rendered_attachment_keys)) if len(attachment_html) > 0: rendered.append(attachment_html) return "".join(rendered), rendered_attachment_keys def _dedupe_attachments(attachments: list[dict[str, Any]], rendered_attachment_keys: set[str]) -> list[dict[str, Any]]: unique = [] for attachment in attachments: if not isinstance(attachment, dict): continue dedupe_key = attachment.get("path_key", "") or attachment.get("href", "") if len(dedupe_key) == 0 or dedupe_key in rendered_attachment_keys: continue rendered_attachment_keys.add(dedupe_key) unique.append(attachment) return unique def _render_reasoning_block(block: dict[str, Any], block_no: int, total_blocks: int) -> str: label = "Thought process" return ( f"{arguments_text}{result_text or html.escape('Pending...')}