""" Visualize Capability ==================== Three-stage visualization pipeline: Analyze -> Generate -> Review. Produces SVG or Chart.js code from user requests and conversation context. """ from __future__ import annotations from typing import Any from deeptutor.capabilities.request_contracts import get_capability_request_schema from deeptutor.core.capability_protocol import BaseCapability, CapabilityManifest from deeptutor.core.context import UnifiedContext from deeptutor.core.stream_bus import StreamBus from deeptutor.core.trace import merge_trace_metadata class VisualizeCapability(BaseCapability): manifest = CapabilityManifest( name="visualize", description="Generate SVG, Chart.js, Mermaid, or interactive HTML visualizations.", stages=["analyzing", "generating", "reviewing"], tools_used=[], cli_aliases=["visualize", "viz"], request_schema=get_capability_request_schema("visualize"), ) async def run(self, context: UnifiedContext, stream: StreamBus) -> None: from deeptutor.agents.visualize.pipeline import VisualizePipeline from deeptutor.agents.visualize.utils import ( build_fallback_html, is_valid_html_document, ) from deeptutor.capabilities._answer_now import extract_answer_now_context from deeptutor.services.llm.config import get_llm_config answer_now_payload = extract_answer_now_context(context) if answer_now_payload is not None: await self._run_answer_now(context, stream, answer_now_payload) return llm_config = get_llm_config() history_context = str( context.metadata.get("conversation_context_text", "") or "" ).strip() render_mode = str( context.config_overrides.get("render_mode", "auto") or "auto" ).strip().lower() pipeline = VisualizePipeline( api_key=llm_config.api_key, base_url=llm_config.base_url, api_version=llm_config.api_version, language=context.language, trace_callback=self._build_trace_bridge(stream), ) # Stage 1: Analyze async with stream.stage("analyzing", source=self.name): await stream.thinking( "Analyzing visualization requirements...", source=self.name, stage="analyzing", ) analysis = await pipeline.run_analysis( user_input=context.user_message, history_context=history_context, render_mode=render_mode, ) await stream.progress( message=f"Render type: {analysis.render_type} — {analysis.description}", source=self.name, stage="analyzing", ) # Stage 2: Generate code async with stream.stage("generating", source=self.name): await stream.thinking( "Generating visualization code...", source=self.name, stage="generating", ) code = await pipeline.run_code_generation( user_input=context.user_message, history_context=history_context, analysis=analysis, ) await stream.progress( message="Code generated.", source=self.name, stage="generating", ) # Stage 3: Review & optimise async with stream.stage("reviewing", source=self.name): if analysis.render_type == "html": # Skip the LLM review pass for html — it would cost another # 30-60s on a 10k-token document with negligible quality gain. # Instead, do a local sanity check and fall back to a minimal # template if the model returned something unrenderable. from deeptutor.agents.visualize.models import ReviewResult if is_valid_html_document(code): final_code = code review = ReviewResult( optimized_code=final_code, changed=False, review_notes="Skipped LLM review for html render_type.", ) await stream.progress( message="HTML page ready (review skipped).", source=self.name, stage="reviewing", ) else: final_code = build_fallback_html( title=analysis.description or "Visualization", summary=analysis.data_description, note="The model did not return a renderable HTML document.", ) review = ReviewResult( optimized_code=final_code, changed=True, review_notes="Used fallback HTML template.", ) await stream.progress( message="HTML did not validate; using fallback template.", source=self.name, stage="reviewing", ) else: await stream.thinking( "Reviewing and optimizing code...", source=self.name, stage="reviewing", ) review = await pipeline.run_review( user_input=context.user_message, analysis=analysis, code=code, ) final_code = review.optimized_code if review.changed: await stream.progress( message=f"Code optimized: {review.review_notes}", source=self.name, stage="reviewing", ) else: await stream.progress( message="Code looks good — no changes needed.", source=self.name, stage="reviewing", ) # Emit final content as a fenced code block for the chat area if analysis.render_type == "svg": lang_tag = "svg" elif analysis.render_type == "mermaid": lang_tag = "mermaid" elif analysis.render_type == "html": lang_tag = "html" else: lang_tag = "javascript" content_md = f"```{lang_tag}\n{final_code}\n```" await stream.content(content_md, source=self.name, stage="reviewing") # Structured result for the frontend viewer await stream.result( { "response": content_md, "render_type": analysis.render_type, "code": { "language": lang_tag, "content": final_code, }, "analysis": analysis.model_dump(), "review": review.model_dump(), }, source=self.name, ) async def _run_answer_now( self, context: UnifiedContext, stream: StreamBus, payload: dict[str, Any], ) -> None: """ Fast-path for ``visualize``: skip analysis + review and emit final code in a single structured LLM call. The result envelope mirrors the standard pipeline so ``VisualizationViewer`` renders it directly. """ import json import re from deeptutor.capabilities._answer_now import ( build_answer_now_trace_metadata, format_trace_summary, join_chunks, labeled_block, make_skip_notice, stream_synthesis, ) is_zh = context.language.lower().startswith("zh") original = str(payload.get("original_user_message") or context.user_message).strip() partial = str(payload.get("partial_response") or "").strip() trace_summary = format_trace_summary(payload.get("events"), language=context.language) render_mode = str( context.config_overrides.get("render_mode", "auto") or "auto" ).strip().lower() if is_zh: system_prompt = ( "你是 DeepTutor 的可视化代码生成器。用户已经在等待," "请直接输出最终可渲染的代码。" "严格输出 JSON:{\"render_type\": \"svg|chartjs|mermaid|html\", " "\"code\": \"...\"}。" "code 字段是可直接渲染的源代码:" "SVG 标签 / Chart.js JS 配置 / Mermaid 文本 / " "完整的单文件 HTML(从 到 ," "CSS 内联在