Skip to content

feat(visualize): replace free-form HTML generation with standardized JSON widget spec framework#488

Open
sagnikonly wants to merge 1 commit into
HKUDS:devfrom
sagnikonly:feat/visualize-framework-dev
Open

feat(visualize): replace free-form HTML generation with standardized JSON widget spec framework#488
sagnikonly wants to merge 1 commit into
HKUDS:devfrom
sagnikonly:feat/visualize-framework-dev

Conversation

@sagnikonly
Copy link
Copy Markdown
Contributor

Description

This PR introduces a standardized widget framework for the visualize capability, replacing free-form LLM-generated HTML with a structured, template-driven architecture that guarantees consistent, high-quality interactive widgets regardless of which LLM is used.

Problem: The previous approach asked LLMs to generate a complete HTML document for every visualization. This caused layout inconsistency across models, dark mode breakage (hardcoded white backgrounds), LLM reasoning text leaking into the rendered widget, and content overflowing the iframe height.

Solution: The LLM now outputs a structured JSON widget spec (metrics, SVG/canvas, controls, JS update function). A fixed Python shell build_widget_from_spec() in utils.py assembles this into a guaranteed 3-zone layout — Metric Strip → Diagram Canvas → Controls — with a full CSS design system that cannot drift with LLM output. The iframe is made transparent so widgets inherit the app's background, and @media (prefers-color-scheme) tokens ensure automatic light/dark adaptation. A graceful HTML fallback is preserved for resilience.

Related Issues

Module(s) Affected

  • agents
  • api
  • config
  • core
  • knowledge
  • logging
  • services
  • tools
  • utils
  • web (Frontend)
  • docs (Documentation)
  • scripts
  • tests
  • Other: ...

Checklist

  • I have read and followed the contribution guidelines.
  • My code follows the project's coding standards.
  • I have run pre-commit run --all-files and fixed any issues.
  • I have added relevant tests for my changes.
  • I have updated the documentation (if necessary).
  • My changes do not introduce any new security vulnerabilities.

Additional Notes

Files changed (8 files, 865 insertions, 114 deletions):

  • deeptutor/agents/visualize/utils.py — Added build_widget_from_spec() fixed shell + is_widget_spec() detection
  • deeptutor/agents/visualize/agents/code_generator_agent.py — JSON spec routing with HTML fallback
  • deeptutor/agents/visualize/prompts/en|zh/code_generator_agent.yaml — Spec-first prompt with strict output schema
  • deeptutor/agents/visualize/prompts/en|zh/answer_now.yaml — Fast-path updated to request JSON spec
  • web/components/visualize/VisualizationViewer.tsx — Transparent iframe, reasoning leak guard
  • web/lib/iframe-html.ts — Transparent dark mode injection, inline-style white/black overrides

All 14 pre-commit hooks passed (ruff, ruff-format, prettier, bandit, mypy, detect-secrets, trailing-whitespace, end-of-file-fixer, check-yaml, check-json, check-toml, check-merge-conflicts, check-case-conflict, check-added-large-files).

New Design
Screenshot 2026-05-17 at 8 09 10 AM
Old Design
Screenshot 2026-05-17 at 8 09 39 AM

…yle design

- Introduced build_widget_from_spec() in utils.py: a fixed Python HTML shell
  that guarantees consistent 3-zone layout (metric strip → diagram → controls)
  regardless of which LLM generates the content. LLM now outputs a structured
  JSON spec (metrics, canvas_html, controls, update_js) instead of raw HTML.

- CodeGeneratorAgent: detects JSON widget spec and routes through the fixed
  shell; falls back to raw HTML for resilience.

- VisualizationViewer.tsx: fixed iframe to be fully transparent (allowtransparency,
  background:transparent) so the widget inherits the app's background.

- iframe-html.ts: dark mode injection now uses transparent backgrounds,
  overrides LLM-injected inline white backgrounds and black text.

- Prompts (EN/ZH code_generator_agent + answer_now): updated to request JSON
  widget spec. Explicit bans on titles, step-by-step solutions inside widget.

- Widget shell CSS uses CSS custom properties with @media (prefers-color-scheme)
  for full light/dark mode support — all colors adapt automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant