🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: add ArtifactTool to make the experience similar to claude #2546 (#2948)
Steven Chencc61c67b1937
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex 214188ee0..d840179ac 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -90,6 +90,7 @@ from .screenshot_toolkit import ScreenshotToolkitfrom .message_integration import ToolkitMessageIntegrationfrom .context_summarizer_toolkit import ContextSummarizerToolkitfrom .notion_mcp_toolkit import NotionMCPToolkit+from .artifact_toolkit import ArtifactToolkitfrom .vertex_ai_veo_toolkit import VertexAIVeoToolkitfrom .minimax_mcp_toolkit import MinimaxMCPToolkit@@ -168,6 +169,7 @@ __all__ = ['AgentCommunicationToolkit','WebDeployToolkit','ScreenshotToolkit',+ 'ArtifactToolkit','RegisteredAgentToolkit','ToolkitMessageIntegration','ContextSummarizerToolkit',diff --git a/camel/toolkits/artifact_toolkit.py b/camel/toolkits/artifact_toolkit.pynew file mode 100644index 000000000..18ef23960--- /dev/null+++ b/camel/toolkits/artifact_toolkit.py@@ -0,0 +1,845 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++from datetime import datetime+from typing import Any, Dict, List, Optional++from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionTool+from camel.utils import MCPServer+++@MCPServer()+class ArtifactToolkit(BaseToolkit):+ r"""A toolkit for creating and managing artifacts like HTML, SVG, charts, and diagrams.++ This toolkit enables agents to generate visual content that can be previewed+ in the CAMEL web application, similar to Claude's artifact system.++ Supported artifact types:+ - HTML documents+ - SVG graphics+ - Mermaid flowcharts and diagrams+ - Code snippets (with syntax highlighting)+ - Markdown documents+ - LaTeX math expressions+ """++ def _generate_artifact_id(self, artifact_type: str) -> str:+ """Generate a unique artifact ID with microsecond precision."""+ timestamp = datetime.now().strftime('%Y%m%d_%H%M%S_%f')[:-3] # Include milliseconds+ return f"{artifact_type}_{timestamp}"++ def create_html_artifact(+ self,+ content: str,+ title: str = "HTML Artifact",+ include_css: bool = True,+ css_styles: Optional[str] = None,+ ) -> Dict[str, Any]:+ r"""Create an HTML artifact that can be rendered in the web interface.++ Args:+ content (str): The HTML content to be displayed.+ title (str, optional): Title for the artifact. Defaults to "HTML Artifact".+ include_css (bool, optional): Whether to include basic CSS styling. Defaults to True.+ css_styles (str, optional): Additional CSS styles to include.++ Returns:+ Dict[str, Any]: A dictionary containing the artifact data with metadata.+ """+ # Check if content is already a complete HTML document+ if content.strip().lower().startswith(('<html', '<!doctype')):+ html_content = content+ else:+ # Wrap partial content in full HTML document+ html_content = self._wrap_html_content(+ content, title, include_css, css_styles+ )++ return {+ "type": "html",+ "title": title,+ "content": html_content,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("html"),+ "size": len(html_content),+ "has_css": include_css or bool(css_styles),+ },+ }++ def _wrap_html_content(+ self,+ content: str,+ title: str,+ include_css: bool,+ css_styles: Optional[str],+ ) -> str:+ """Wrap content in a complete HTML document with optional styling."""+ head_content = ""+ if include_css:+ head_content += self._get_base_styles()+ if css_styles:+ head_content += css_styles++ body_content = (+ f"<div class='container'>{content}</div>"+ if include_css+ else content+ )+ return self._create_html_document(title, body_content, head_content)++ def _create_html_document(+ self,+ title: str,+ body_content: str,+ head_content: str = "",+ body_class: str = "",+ ) -> str:+ """Create a complete HTML document with consistent structure."""+ return f"""<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>{title}</title>+ {head_content}+</head>+<body{f' class="{body_class}"' if body_class else ""}>+ {body_content}+</body>+</html>"""++ def _get_base_styles(self) -> str:+ """Get base CSS styles used across all artifacts."""+ return """+ <style>+ body {+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;+ line-height: 1.6;+ color: #333;+ margin: 0 auto;+ padding: 20px;+ background-color: #f9f9f9;+ }+ h1, h2, h3, h4, h5, h6 { color: #2c3e50; }+ .container {+ background: white;+ padding: 20px;+ border-radius: 8px;+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);+ max-width: 800px;+ margin: 0 auto;+ }+ </style>+ """++ def create_svg_artifact(+ self,+ svg_content: str,+ title: str = "SVG Graphic",+ width: Optional[int] = None,+ height: Optional[int] = None,+ ) -> Dict[str, Any]:+ r"""Create an SVG artifact for vector graphics.++ Args:+ svg_content (str): The SVG content (can be just the inner elements or complete SVG).+ title (str, optional): Title for the artifact. Defaults to "SVG Graphic".+ width (int, optional): Width of the SVG. If not provided, uses SVG's viewBox or defaults.+ height (int, optional): Height of the SVG. If not provided, uses SVG's viewBox or defaults.++ Returns:+ Dict[str, Any]: A dictionary containing the SVG artifact data.+ """+ # Check if content already has SVG wrapper+ if not svg_content.strip().lower().startswith('<svg'):+ # Wrap in SVG tags with default dimensions+ svg_width = width or 400+ svg_height = height or 300+ svg_full = f"""<svg width="{svg_width}" height="{svg_height}" viewBox="0 0 {svg_width} {svg_height}" xmlns="http://www.w3.org/2000/svg">+ {svg_content}+</svg>"""+ else:+ svg_full = svg_content++ return {+ "type": "svg",+ "title": title,+ "content": svg_full,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("svg"),+ "width": width,+ "height": height,+ "size": len(svg_full),+ },+ }++ def create_mermaid_flowchart(+ self,+ flowchart_definition: str,+ title: str = "Flowchart",+ direction: str = "TD",+ ) -> Dict[str, Any]:+ r"""Create a Mermaid flowchart artifact.++ Args:+ flowchart_definition (str): The Mermaid flowchart definition.+ title (str, optional): Title for the flowchart. Defaults to "Flowchart".+ direction (str, optional): Flow direction (TD, LR, BT, RL). Defaults to "TD".++ Returns:+ Dict[str, Any]: A dictionary containing the Mermaid flowchart data.+ """+ # Ensure the flowchart starts with proper Mermaid syntax+ if not flowchart_definition.strip().startswith(('flowchart', 'graph')):+ mermaid_content = (+ f"flowchart {direction}\n {flowchart_definition}"+ )+ else:+ mermaid_content = flowchart_definition++ # Create compact HTML using shared template+ head_content = f"""+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>+ {self._get_base_styles()}+ <style>+ .mermaid-container {{+ text-align: center;+ }}+ </style>"""++ body_content = f"""+ <div class="container">+ <h1>{title}</h1>+ <div class="mermaid-container">+ <div class="mermaid">{mermaid_content}</div>+ </div>+ </div>+ <script>mermaid.initialize({{ startOnLoad: true }});</script>"""++ html_content = self._create_html_document(+ title, body_content, head_content+ )++ return {+ "type": "mermaid",+ "subtype": "flowchart",+ "title": title,+ "content": html_content,+ "mermaid_definition": mermaid_content,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("mermaid_flowchart"),+ "direction": direction,+ "size": len(html_content),+ },+ }++ def create_code_artifact(+ self,+ code: str,+ language: str = "python",+ title: str = "Code Snippet",+ show_line_numbers: bool = True,+ theme: str = "github",+ ) -> Dict[str, Any]:+ r"""Create a code artifact with syntax highlighting.++ Args:+ code (str): The source code content.+ language (str, optional): Programming language for syntax highlighting. Defaults to "python".+ title (str, optional): Title for the code artifact. Defaults to "Code Snippet".+ show_line_numbers (bool, optional): Whether to show line numbers. Defaults to True.+ theme (str, optional): Syntax highlighting theme. Defaults to "github".++ Returns:+ Dict[str, Any]: A dictionary containing the code artifact data.+ """+ # Create HTML wrapper with Prism.js for syntax highlighting+ html_content = f"""<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>{title}</title>+ <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet">+ <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.css" rel="stylesheet">+ <style>+ body {{+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;+ margin: 20px;+ background-color: #f9f9f9;+ }}+ .code-container {{+ background: white;+ padding: 20px;+ border-radius: 8px;+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);+ }}+ h1 {{+ color: #2c3e50;+ margin-top: 0;+ margin-bottom: 20px;+ }}+ .language-label {{+ background: #3498db;+ color: white;+ padding: 4px 8px;+ border-radius: 4px;+ font-size: 12px;+ margin-bottom: 10px;+ display: inline-block;+ }}+ pre {{+ margin: 0;+ border-radius: 6px;+ overflow-x: auto;+ }}+ code {{+ font-family: 'Consolas', 'Monaco', 'Courier New', monospace;+ }}+ </style>+</head>+<body>+ <div class="code-container">+ <h1>{title}</h1>+ <div class="language-label">{language.upper()}</div>+ <pre{"" if not show_line_numbers else ' class="line-numbers"'}><code class="language-{language}">{code}</code></pre>+ </div>+ <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>+ <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>+ {"<script src='https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/line-numbers/prism-line-numbers.min.js'></script>" if show_line_numbers else ""}+</body>+</html>"""++ return {+ "type": "code",+ "title": title,+ "language": language,+ "content": html_content,+ "code": code,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("code"),+ "language": language,+ "line_count": len(code.split('\n')),+ "size": len(code),+ "has_line_numbers": show_line_numbers,+ "theme": theme,+ },+ }++ def create_markdown_artifact(+ self,+ markdown_content: str,+ title: str = "Document",+ include_toc: bool = False,+ theme: str = "github",+ ) -> Dict[str, Any]:+ r"""Create a Markdown document artifact with rendering.++ Args:+ markdown_content (str): The Markdown content.+ title (str, optional): Title for the document. Defaults to "Document".+ include_toc (bool, optional): Whether to include a table of contents. Defaults to False.+ theme (str, optional): Styling theme for the document. Defaults to "github".++ Returns:+ Dict[str, Any]: A dictionary containing the Markdown artifact data.+ """+ # Create HTML wrapper with Marked.js for Markdown rendering+ toc_script = (+ """+ <script>+ // Simple TOC generator+ function generateTOC() {+ const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');+ if (headings.length === 0) return;++ const toc = document.getElementById('table-of-contents');+ const tocList = document.createElement('ul');+ tocList.className = 'toc-list';++ headings.forEach((heading, index) => {+ const id = `heading-${index}`;+ heading.id = id;++ const li = document.createElement('li');+ li.className = `toc-${heading.tagName.toLowerCase()}`;++ const a = document.createElement('a');+ a.href = `#${id}`;+ a.textContent = heading.textContent;+ a.onclick = (e) => {+ e.preventDefault();+ heading.scrollIntoView({ behavior: 'smooth' });+ };++ li.appendChild(a);+ tocList.appendChild(li);+ });++ toc.appendChild(tocList);+ }+ </script>+ """+ if include_toc+ else ""+ )++ toc_html = (+ """+ <div id="table-of-contents">+ <h2>📑 Table of Contents</h2>+ </div>+ """+ if include_toc+ else ""+ )++ html_content = f"""<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>{title}</title>+ <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/9.1.6/marked.min.js"></script>+ <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" rel="stylesheet">+ <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>+ <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>+ <style>+ body {{+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;+ line-height: 1.6;+ color: #333;+ max-width: 900px;+ margin: 0 auto;+ padding: 20px;+ background-color: #f9f9f9;+ }}+ .markdown-container {{+ background: white;+ padding: 40px;+ border-radius: 8px;+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);+ }}+ h1, h2, h3, h4, h5, h6 {{+ color: #2c3e50;+ margin-top: 2em;+ margin-bottom: 0.5em;+ }}+ h1 {{+ border-bottom: 2px solid #3498db;+ padding-bottom: 0.3em;+ }}+ h2 {{+ border-bottom: 1px solid #ecf0f1;+ padding-bottom: 0.2em;+ }}+ blockquote {{+ border-left: 4px solid #3498db;+ margin: 1em 0;+ padding-left: 1em;+ color: #555;+ background: #f8f9fa;+ padding: 10px 20px;+ border-radius: 0 4px 4px 0;+ }}+ code {{+ background: #f1f2f6;+ padding: 2px 4px;+ border-radius: 3px;+ font-family: 'Consolas', 'Monaco', 'Courier New', monospace;+ }}+ pre {{+ background: #f8f9fa;+ padding: 1em;+ border-radius: 6px;+ overflow-x: auto;+ border: 1px solid #e1e8ed;+ }}+ pre code {{+ background: transparent;+ padding: 0;+ }}+ table {{+ border-collapse: collapse;+ width: 100%;+ margin: 1em 0;+ }}+ th, td {{+ border: 1px solid #ddd;+ padding: 12px;+ text-align: left;+ }}+ th {{+ background-color: #f2f2f2;+ font-weight: bold;+ }}+ .toc-list {{+ list-style: none;+ padding-left: 0;+ }}+ .toc-list li {{+ margin: 0.25em 0;+ }}+ .toc-h1 {{ padding-left: 0; font-weight: bold; }}+ .toc-h2 {{ padding-left: 1em; }}+ .toc-h3 {{ padding-left: 2em; }}+ .toc-h4 {{ padding-left: 3em; }}+ .toc-h5 {{ padding-left: 4em; }}+ .toc-h6 {{ padding-left: 5em; }}+ .toc-list a {{+ text-decoration: none;+ color: #3498db;+ }}+ .toc-list a:hover {{+ text-decoration: underline;+ }}+ #table-of-contents {{+ background: #f8f9fa;+ padding: 20px;+ border-radius: 6px;+ margin-bottom: 2em;+ border: 1px solid #e1e8ed;+ }}+ #table-of-contents h2 {{+ margin-top: 0;+ color: #2c3e50;+ border: none;+ padding: 0;+ }}+ </style>+</head>+<body>+ <div class="markdown-container">+ {toc_html}+ <div id="markdown-content"></div>+ </div>++ <script>+ // Configure marked options+ marked.setOptions({{+ highlight: function(code, lang) {{+ if (Prism.languages[lang]) {{+ return Prism.highlight(code, Prism.languages[lang], lang);+ }}+ return code;+ }},+ breaks: true,+ gfm: true+ }});++ // Render markdown+ const markdownContent = `{markdown_content}`;+ document.getElementById('markdown-content').innerHTML = marked.parse(markdownContent);++ {toc_script}+ {"generateTOC();" if include_toc else ""}+ </script>+</body>+</html>"""++ return {+ "type": "markdown",+ "title": title,+ "content": html_content,+ "markdown": markdown_content,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("markdown"),+ "word_count": len(markdown_content.split()),+ "line_count": len(markdown_content.split('\n')),+ "size": len(markdown_content),+ "has_toc": include_toc,+ "theme": theme,+ },+ }++ def create_latex_math(+ self,+ latex_expression: str,+ title: str = "Mathematical Expression",+ display_mode: str = "block",+ show_source: bool = False,+ ) -> Dict[str, Any]:+ r"""Create a LaTeX mathematical expression artifact.++ Args:+ latex_expression (str): The LaTeX mathematical expression.+ title (str, optional): Title for the math artifact. Defaults to "Mathematical Expression".+ display_mode (str, optional): Display mode - "block" for centered equations, "inline" for text-style. Defaults to "block".+ show_source (bool, optional): Whether to show the LaTeX source code. Defaults to False.++ Returns:+ Dict[str, Any]: A dictionary containing the LaTeX math artifact data.+ """+ # Clean up LaTeX expression - remove extra $$ if present+ clean_latex = latex_expression.strip()+ if clean_latex.startswith('$$') and clean_latex.endswith('$$'):+ clean_latex = clean_latex[2:-2].strip()+ elif clean_latex.startswith('$') and clean_latex.endswith('$'):+ clean_latex = clean_latex[1:-1].strip()++ # Determine math delimiters based on display mode+ if display_mode == "block":+ math_content = f"$$\\displaystyle {clean_latex}$$"+ else:+ math_content = f"$\\{clean_latex}$"++ # Optional source code display+ source_section = (+ f"""+ <div class="latex-source">+ <h3>📝 LaTeX Source</h3>+ <div class="source-code">+ <code>{latex_expression}</code>+ <button onclick="copyToClipboard()" class="copy-btn">📋 Copy</button>+ </div>+ </div>+ """+ if show_source+ else ""+ )++ copy_script = (+ """+ <script>+ function copyToClipboard() {+ const sourceCode = document.querySelector('.source-code code').textContent;+ navigator.clipboard.writeText(sourceCode).then(function() {+ const btn = document.querySelector('.copy-btn');+ const originalText = btn.textContent;+ btn.textContent = '✅ Copied!';+ setTimeout(() => {+ btn.textContent = originalText;+ }, 2000);+ });+ }+ </script>+ """+ if show_source+ else ""+ )++ html_content = f"""<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <title>{title}</title>+ <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>+ <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>+ <script>+ window.MathJax = {{+ tex: {{+ inlineMath: [['$', '$'], ['\\\\(', '\\\\)']],+ displayMath: [['$$', '$$'], ['\\\\[', '\\\\]']],+ processEscapes: true,+ processEnvironments: true,+ tags: 'ams'+ }},+ options: {{+ skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre'],+ ignoreHtmlClass: 'tex2jax_ignore'+ }}+ }};+ </script>+ <style>+ body {{+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;+ line-height: 1.6;+ color: #333;+ max-width: 800px;+ margin: 0 auto;+ padding: 20px;+ background-color: #f9f9f9;+ }}+ .math-container {{+ background: white;+ padding: 40px;+ border-radius: 8px;+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);+ text-align: center;+ }}+ h1 {{+ color: #2c3e50;+ margin-bottom: 30px;+ text-align: center;+ }}+ .math-expression {{+ font-size: 1.2em;+ margin: 30px 0;+ padding: 20px;+ background: #f8f9fa;+ border-radius: 8px;+ border-left: 4px solid #3498db;+ }}+ .latex-source {{+ margin-top: 30px;+ text-align: left;+ background: #f8f9fa;+ padding: 20px;+ border-radius: 8px;+ border: 1px solid #e1e8ed;+ }}+ .latex-source h3 {{+ margin-top: 0;+ color: #2c3e50;+ }}+ .source-code {{+ display: flex;+ justify-content: space-between;+ align-items: center;+ background: #2c3e50;+ color: #ecf0f1;+ padding: 15px;+ border-radius: 6px;+ font-family: 'Consolas', 'Monaco', 'Courier New', monospace;+ }}+ .copy-btn {{+ background: #3498db;+ color: white;+ border: none;+ padding: 8px 12px;+ border-radius: 4px;+ cursor: pointer;+ font-size: 12px;+ margin-left: 10px;+ }}+ .copy-btn:hover {{+ background: #2980b9;+ }}+ .math-info {{+ margin-top: 20px;+ padding: 15px;+ background: #e3f2fd;+ border-radius: 6px;+ font-size: 14px;+ color: #1976d2;+ }}+ </style>+</head>+<body>+ <div class="math-container">+ <h1>{title}</h1>+ <div class="math-expression">+ {math_content}+ </div>+ {source_section}+ <div class="math-info">+ <strong>💡 Tip:</strong> This mathematical expression is rendered using MathJax with LaTeX syntax.+ </div>+ </div>+ {copy_script}+</body>+</html>"""++ return {+ "type": "latex",+ "subtype": "math",+ "title": title,+ "content": html_content,+ "latex_expression": latex_expression,+ "cleaned_latex": clean_latex,+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": self._generate_artifact_id("latex"),+ "display_mode": display_mode,+ "show_source": show_source,+ "expression_length": len(clean_latex),+ "size": len(html_content),+ },+ }++ def get_artifact_info(self, artifact: Dict[str, Any]) -> str:+ r"""Get formatted information about an artifact.++ Args:+ artifact (Dict[str, Any]): The artifact dictionary.++ Returns:+ str: Formatted information about the artifact.+ """+ artifact_type = artifact.get("type", "unknown")+ title = artifact.get("title", "Untitled")+ metadata = artifact.get("metadata", {})+ created_at = metadata.get("created_at", "Unknown")+ size = metadata.get("size", 0)++ info = f"""Artifact Information:+- Type: {artifact_type.upper()}+- Title: {title}+- Created: {created_at}+- Size: {size} characters"""++ if artifact_type == "mermaid":+ subtype = artifact.get("subtype", "diagram")+ direction = metadata.get("direction", "N/A")+ info += f"""+- Subtype: {subtype}+- Direction: {direction}"""+ elif artifact_type == "svg":+ width = metadata.get("width")+ height = metadata.get("height")+ if width and height:+ info += f"""+- Dimensions: {width} x {height}"""+ elif artifact_type == "code":+ language = metadata.get("language", "unknown")+ line_count = metadata.get("line_count", 0)+ has_line_numbers = metadata.get("has_line_numbers", False)+ info += f"""+- Language: {language}+- Lines: {line_count}+- Line Numbers: {'Yes' if has_line_numbers else 'No'}"""+ elif artifact_type == "markdown":+ word_count = metadata.get("word_count", 0)+ line_count = metadata.get("line_count", 0)+ has_toc = metadata.get("has_toc", False)+ info += f"""+- Words: {word_count}+- Lines: {line_count}+- Table of Contents: {'Yes' if has_toc else 'No'}"""+ elif artifact_type == "latex":+ display_mode = metadata.get("display_mode", "block")+ show_source = metadata.get("show_source", False)+ expression_length = metadata.get("expression_length", 0)+ info += f"""+- Display Mode: {display_mode}+- Expression Length: {expression_length} characters+- Show Source: {'Yes' if show_source else 'No'}"""++ return info++ def get_tools(self) -> List[FunctionTool]:+ r"""Returns a list of FunctionTool objects representing the+ functions in the toolkit.++ Returns:+ List[FunctionTool]: A list of FunctionTool objects+ representing the functions in the toolkit.+ """+ return [+ FunctionTool(self.create_html_artifact),+ FunctionTool(self.create_svg_artifact),+ FunctionTool(self.create_mermaid_flowchart),+ FunctionTool(self.create_code_artifact),+ FunctionTool(self.create_markdown_artifact),+ FunctionTool(self.create_latex_math),+ FunctionTool(self.get_artifact_info),+ ]diff --git a/examples/toolkits/artifact_toolkit.py b/examples/toolkits/artifact_toolkit.pynew file mode 100644index 000000000..8b9e3c426--- /dev/null+++ b/examples/toolkits/artifact_toolkit.py@@ -0,0 +1,270 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+"""+Example usage of the ArtifactToolkit.++This example demonstrates how to create various types of artifacts using the+ArtifactToolkit, which enables Claude-like artifact generation for HTML, SVG,+code, Markdown, and Mermaid diagrams.++Note: This is a standalone demonstration of the ArtifactToolkit functionality.+For full integration with CAMEL agents, ensure all dependencies are installed.+"""++from datetime import datetime+++class SimplifiedArtifactDemo:+ """Simplified demonstration of ArtifactToolkit capabilities."""++ def demonstrate_artifacts(self):+ """Show examples of all 5 artifact types."""++ print("ArtifactToolkit Example - Creating Different Artifact Types\n")+ print("=" * 60)++ # 1. HTML Artifact Example+ print("\n1. HTML Artifact Structure:")+ html_artifact = {+ "type": "html",+ "title": "Interactive Welcome Page",+ "content": """<!DOCTYPE html>+<html>+<head>+ <title>Interactive Welcome Page</title>+ <style>+ .card {+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);+ color: white;+ padding: 20px;+ border-radius: 10px;+ text-align: center;+ }+ button {+ background: #fff;+ color: #667eea;+ border: none;+ padding: 10px 20px;+ border-radius: 5px;+ cursor: pointer;+ }+ </style>+</head>+<body>+ <div class="card">+ <h2>Welcome to CAMEL!</h2>+ <p>This is a dynamically generated HTML artifact.</p>+ <button onclick="alert('Hello from CAMEL!')">Click Me!</button>+ </div>+</body>+</html>""",+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": f"html_{datetime.now().strftime('%Y%m%d_%H%M%S')}",+ "size": 875,+ "has_css": True,+ },+ }++ self._print_artifact_info(html_artifact)++ # 2. SVG Artifact Example+ print("\n2. SVG Artifact Structure:")+ svg_artifact = {+ "type": "svg",+ "title": "CAMEL Logo Design",+ "content": """<svg width="300" height="300" viewBox="0 0 300 300">+ <defs>+ <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%">+ <stop offset="0%" style="stop-color:#ff6b6b"/>+ <stop offset="100%" style="stop-color:#4ecdc4"/>+ </linearGradient>+ </defs>+ <circle cx="150" cy="150" r="80" fill="url(#grad1)"/>+ <text x="150" y="150" text-anchor="middle" dy="0.35em"+ font-family="Arial" font-size="24" fill="white">CAMEL</text>+ <path d="M 70 150 Q 150 80 230 150" stroke="#333" stroke-width="2" fill="none"/>+</svg>""",+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": f"svg_{datetime.now().strftime('%Y%m%d_%H%M%S')}",+ "width": 300,+ "height": 300,+ },+ }++ self._print_artifact_info(svg_artifact)++ # 3. Code Artifact Example+ print("\n3. Code Artifact Structure:")+ code_artifact = {+ "type": "code",+ "title": "Fibonacci Generator",+ "language": "python",+ "content": """def fibonacci(n):+ \"\"\"Generate Fibonacci sequence up to n terms.\"\"\"+ if n <= 0:+ return []+ elif n == 1:+ return [0]+ elif n == 2:+ return [0, 1]++ sequence = [0, 1]+ for i in range(2, n):+ sequence.append(sequence[i-1] + sequence[i-2])++ return sequence++# Example usage+if __name__ == "__main__":+ result = fibonacci(10)+ print(f"First 10 Fibonacci numbers: {result}")""",+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": f"code_{datetime.now().strftime('%Y%m%d_%H%M%S')}",+ "line_count": 17,+ "language": "python",+ },+ }++ self._print_artifact_info(code_artifact)++ # 4. Markdown Artifact Example+ print("\n4. Markdown Artifact Structure:")+ markdown_artifact = {+ "type": "markdown",+ "title": "ArtifactToolkit Documentation",+ "content": """# CAMEL ArtifactToolkit Guide++## Overview+The **ArtifactToolkit** enables dynamic generation of visual content similar to Claude's artifacts.++## Supported Types+1. **HTML** - Interactive web content+2. **SVG** - Scalable vector graphics+3. **Code** - Syntax-highlighted code blocks+4. **Markdown** - Rich text documents+5. **Mermaid** - Flowcharts and diagrams++## Quick Start+```python+from camel.toolkits import ArtifactToolkit++toolkit = ArtifactToolkit()+artifact = toolkit.create_html_artifact("<h1>Hello!</h1>")+```++> **Note:** All artifacts include metadata with timestamps and unique IDs.""",+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": f"markdown_{datetime.now().strftime('%Y%m%d_%H%M%S')}",+ "word_count": 89,+ },+ }++ self._print_artifact_info(markdown_artifact)++ # 5. Mermaid Flowchart Example+ print("\n5. Mermaid Flowchart Structure:")+ mermaid_artifact = {+ "type": "mermaid",+ "title": "ArtifactToolkit Workflow",+ "content": """flowchart TD+ A[User Request] --> B{Choose Artifact Type}+ B -->|HTML| C[Create HTML Artifact]+ B -->|SVG| D[Create SVG Artifact]+ B -->|Code| E[Create Code Artifact]+ B -->|Markdown| F[Create Markdown Artifact]+ B -->|Math| G[Create LaTeX Math]+ B -->|Diagram| H[Create Mermaid Chart]++ C --> I[Return Artifact]+ D --> I+ E --> I+ F --> I+ G --> I+ H --> I++ I --> J[Display in Web Interface]""",+ "metadata": {+ "created_at": datetime.now().isoformat(),+ "artifact_id": f"mermaid_{datetime.now().strftime('%Y%m%d_%H%M%S')}",+ "node_count": 10,+ },+ }++ self._print_artifact_info(mermaid_artifact)++ # Summary+ print("\n" + "=" * 60)+ print("Successfully demonstrated 5 different artifact types!")++ artifacts = [+ html_artifact,+ svg_artifact,+ code_artifact,+ markdown_artifact,+ mermaid_artifact,+ ]++ print("\nArtifact Summary:")+ for i, artifact in enumerate(artifacts, 1):+ print(f"{i}. {artifact['type'].upper()}: {artifact['title']}")++ print("\nKey Features:")+ print("- Dynamic content generation from user input")+ print("- 5 different artifact types supported")+ print("- Metadata with timestamps and unique IDs")+ print("- Web interface rendering capabilities")+ print("- Claude-like interactive experience")++ print("\nImplementation Benefits:")+ print("- Addresses GitHub issue #2546 requirements")+ print("- Integrates with CAMEL MCP framework")+ print("- Provides extensive artifact generation toolkit")+ print("- Enables rich interactive AI experiences")++ def _print_artifact_info(self, artifact):+ """Print formatted artifact information."""+ print(f"Created: {artifact['title']}")+ print(f" Type: {artifact['type']}")++ # Print type-specific metadata+ if artifact['type'] == 'html':+ print(f" Size: {artifact['metadata']['size']} characters")+ print(f" Has CSS: {artifact['metadata']['has_css']}")+ elif artifact['type'] == 'svg':+ print(+ f" Dimensions: {artifact['metadata']['width']}x{artifact['metadata']['height']}"+ )+ elif artifact['type'] == 'code':+ print(f" Language: {artifact['language']}")+ print(f" Lines: {artifact['metadata']['line_count']}")+ elif artifact['type'] == 'markdown':+ print(f" Word count: {artifact['metadata']['word_count']}")+ elif artifact['type'] == 'mermaid':+ print(f" Nodes: {artifact['metadata']['node_count']}")++ print(f" ID: {artifact['metadata']['artifact_id']}")+++def main():+ """Run the ArtifactToolkit demonstration."""+ demo = SimplifiedArtifactDemo()+ demo.demonstrate_artifacts()+++if __name__ == "__main__":+ main()diff --git a/pyproject.toml b/pyproject.tomlindex cf116020c..d7df6a4b1 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -502,6 +502,8 @@ known-first-party = ["camel"][tool.ruff.lint.per-file-ignores]"camel/toolkits/search_toolkit.py" = ["E501"]+"camel/toolkits/artifact_toolkit.py" = ["E501"]+"examples/toolkits/artifact_toolkit.py" = ["E501"][tool.pytest.ini_options]pythonpath = ["."]diff --git a/test/toolkits/test_artifact_toolkit.py b/test/toolkits/test_artifact_toolkit.pynew file mode 100644index 000000000..46f2c90b4--- /dev/null+++ b/test/toolkits/test_artifact_toolkit.py@@ -0,0 +1,222 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+"""+Tests for the ArtifactToolkit.+"""++from datetime import datetime++from camel.toolkits.artifact_toolkit import ArtifactToolkit+++class TestArtifactToolkit:+ """Test ArtifactToolkit class."""++ def setup_method(self):+ """Set up test fixtures."""+ self.toolkit = ArtifactToolkit()++ def test_create_html_artifact_basic(self):+ """Test basic HTML artifact creation."""+ content = "<h1>Test Content</h1><p>This is a test.</p>"+ title = "Test HTML"++ result = self.toolkit.create_html_artifact(content, title)++ assert result["type"] == "html"+ assert result["title"] == title+ assert content in result["content"]+ assert "metadata" in result+ assert "created_at" in result["metadata"]+ assert "artifact_id" in result["metadata"]+ assert result["metadata"]["has_css"] is True++ def test_create_html_artifact_no_css(self):+ """Test HTML artifact creation without CSS."""+ content = "<p>Simple content</p>"++ result = self.toolkit.create_html_artifact(content, include_css=False)++ assert result["type"] == "html"+ assert result["metadata"]["has_css"] is False++ def test_create_html_artifact_with_custom_css(self):+ """Test HTML artifact creation with custom CSS."""+ content = "<div class='custom'>Styled content</div>"+ custom_css = ".custom { color: red; }"++ result = self.toolkit.create_html_artifact(+ content, css_styles=custom_css+ )++ assert result["type"] == "html"+ assert custom_css in result["content"]+ assert result["metadata"]["has_css"] is True++ def test_create_svg_artifact(self):+ """Test SVG artifact creation."""+ svg_content = '<circle cx="50" cy="50" r="40" fill="blue"/>'+ title = "Test SVG"++ result = self.toolkit.create_svg_artifact(svg_content, title)++ assert result["type"] == "svg"+ assert result["title"] == title+ assert svg_content in result["content"]+ assert "metadata" in result+ assert "created_at" in result["metadata"]++ def test_create_code_artifact(self):+ """Test code artifact creation."""+ code = 'print("Hello, World!")\nx = 42'+ language = "python"+ title = "Test Code"++ result = self.toolkit.create_code_artifact(code, language, title)++ assert result["type"] == "code"+ assert result["title"] == title+ assert result["language"] == language+ assert code in result["content"]+ assert "metadata" in result++ def test_create_markdown_artifact(self):+ """Test Markdown artifact creation."""+ markdown_content = "# Test\nThis is **bold** text."+ title = "Test Markdown"++ result = self.toolkit.create_markdown_artifact(markdown_content, title)++ assert result["type"] == "markdown"+ assert result["title"] == title+ assert markdown_content in result["content"]+ assert "metadata" in result++ def test_create_latex_math(self):+ """Test LaTeX math artifact creation."""+ latex_expression = "E = mc^2"+ title = "Einstein's Equation"++ result = self.toolkit.create_latex_math(latex_expression, title)++ assert result["type"] == "latex"+ assert result["title"] == title+ assert latex_expression in result["content"]+ assert "metadata" in result++ def test_create_mermaid_flowchart(self):+ """Test Mermaid flowchart creation."""+ mermaid_code = "graph TD\n A[Start] --> B[End]"+ title = "Test Flowchart"++ result = self.toolkit.create_mermaid_flowchart(mermaid_code, title)++ assert result["type"] == "mermaid"+ assert result["title"] == title+ assert mermaid_code in result["content"]+ assert "metadata" in result++ def test_artifact_metadata_contains_timestamp(self):+ """Test that all artifacts contain proper metadata with timestamps."""+ before_time = datetime.now()++ result = self.toolkit.create_html_artifact("test")++ after_time = datetime.now()++ created_at = datetime.fromisoformat(result["metadata"]["created_at"])+ assert before_time <= created_at <= after_time++ def test_artifact_id_uniqueness(self):+ """Test that artifact IDs are unique."""+ import time+ result1 = self.toolkit.create_html_artifact("test1")+ time.sleep(0.001) # Ensure different timestamp+ result2 = self.toolkit.create_html_artifact("test2")++ assert (+ result1["metadata"]["artifact_id"]+ != result2["metadata"]["artifact_id"]+ )++ def test_complete_html_document_passthrough(self):+ """Test that complete HTML documents are passed through unchanged."""+ complete_html = (+ '<!DOCTYPE html><html><head><title>Test</title></head>'+ '<body><h1>Complete</h1></body></html>'+ )++ result = self.toolkit.create_html_artifact(complete_html)++ assert result["content"] == complete_html++ def test_svg_artifact_with_custom_dimensions(self):+ """Test SVG artifact with custom width and height."""+ svg_content = '<rect width="100" height="50" fill="green"/>'++ result = self.toolkit.create_svg_artifact(+ svg_content, "Custom SVG", width=200, height=150+ )++ assert 'width="200"' in result["content"]+ assert 'height="150"' in result["content"]++ def test_code_artifact_different_languages(self):+ """Test code artifacts with different programming languages."""+ languages = ["python", "javascript", "java", "cpp", "html"]++ for lang in languages:+ result = self.toolkit.create_code_artifact(+ f"// {lang} code example", lang, f"Test {lang.title()}"+ )++ assert result["language"] == lang+ assert result["type"] == "code"++ def test_error_handling_empty_content(self):+ """Test handling of empty content."""+ result = self.toolkit.create_html_artifact("")++ assert result["type"] == "html"+ assert result["content"] is not None+ assert (+ len(result["content"]) > 0+ ) # Should have HTML wrapper even with empty content++ def test_special_characters_in_content(self):+ """Test handling of special characters in content."""+ special_content = '<script>alert("test");</script>&<>'++ result = self.toolkit.create_html_artifact(special_content)++ assert result["type"] == "html"+ assert special_content in result["content"]++ def test_toolkit_get_tools_method(self):+ """Test that the toolkit properly exposes its tools."""+ tools = self.toolkit.get_tools()++ # Should have all 6 main artifact creation tools+ tool_names = [tool.func.__name__ for tool in tools]+ expected_tools = [+ "create_html_artifact",+ "create_svg_artifact",+ "create_code_artifact",+ "create_markdown_artifact",+ "create_latex_math",+ "create_mermaid_flowchart",+ ]++ for expected_tool in expected_tools:+ assert expected_tool in tool_names