|
1 | | - |
| 1 | +# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. ========= |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ========= Copyright 2023-2026 @ CAMEL-AI.org. All Rights Reserved. ========= |
2 | 14 | import json |
3 | 15 | import os |
4 | 16 | import subprocess |
|
15 | 27 |
|
16 | 28 | @MCPServer() |
17 | 29 | class PptxNodeToolkit(BaseToolkit): |
18 | | - r"""A toolkit for creating PowerPoint presentations using PptxGenJS (Node.js). |
19 | | - """ |
| 30 | + r"""A toolkit for creating PowerPoint presentations using PptxGenJS (Node.js).""" |
20 | 31 |
|
21 | 32 | def __init__( |
22 | 33 | self, |
@@ -56,15 +67,15 @@ def create_presentation( |
56 | 67 | filename: str, |
57 | 68 | ) -> str: |
58 | 69 | r"""Create a PowerPoint presentation (PPTX) file using PptxGenJS. |
59 | | - |
| 70 | +
|
60 | 71 | The filename MUST end with ".pptx". If it does not, the toolkit will |
61 | 72 | automatically append it, but the agent should strive to provide the |
62 | 73 | correct extension. |
63 | 74 |
|
64 | 75 | Args: |
65 | 76 | content (str): The content to write to the PPTX file as a JSON |
66 | 77 | string. It must be a list of dictionaries representing slides. |
67 | | - |
| 78 | +
|
68 | 79 | JSON Schema Example: |
69 | 80 | [ |
70 | 81 | { |
@@ -99,41 +110,41 @@ def create_presentation( |
99 | 110 | script_path = Path(__file__).parent / "scripts" / "generate_pptx.js" |
100 | 111 |
|
101 | 112 | try: |
102 | | - # Ensure content is a valid JSON string |
103 | | - try: |
104 | | - if not isinstance(content, str): |
105 | | - content_str = json.dumps(content) |
106 | | - else: |
107 | | - # Validate JSON |
108 | | - json_obj = json.loads(content) |
109 | | - content_str = json.dumps(json_obj) |
110 | | - except json.JSONDecodeError: |
111 | | - return "Error: Content must be valid JSON string representing slides." |
112 | | - |
113 | | - # Run node script |
114 | | - result = subprocess.run( |
115 | | - ["node", str(script_path), str(file_path), content_str], |
116 | | - capture_output=True, |
117 | | - text=True, |
118 | | - check=True |
119 | | - ) |
120 | | - |
121 | | - # Parse JSON output from script |
122 | | - try: |
123 | | - script_output = json.loads(result.stdout.strip()) |
124 | | - if script_output.get("success"): |
125 | | - return f"Presentation created successfully. Path: {script_output.get('path')}, Slides: {script_output.get('slides')}" |
126 | | - else: |
127 | | - return f"Error creating presentation: {script_output.get('error')}" |
128 | | - except json.JSONDecodeError: |
129 | | - return f"Error parsing script output: {result.stdout.strip()}" |
| 113 | + # Ensure content is a valid JSON string |
| 114 | + try: |
| 115 | + if not isinstance(content, str): |
| 116 | + content_str = json.dumps(content) |
| 117 | + else: |
| 118 | + # Validate JSON |
| 119 | + json_obj = json.loads(content) |
| 120 | + content_str = json.dumps(json_obj) |
| 121 | + except json.JSONDecodeError: |
| 122 | + return "Error: Content must be valid JSON string representing slides." |
| 123 | + |
| 124 | + # Run node script |
| 125 | + result = subprocess.run( |
| 126 | + ["node", str(script_path), str(file_path), content_str], |
| 127 | + capture_output=True, |
| 128 | + text=True, |
| 129 | + check=True, |
| 130 | + ) |
| 131 | + |
| 132 | + # Parse JSON output from script |
| 133 | + try: |
| 134 | + script_output = json.loads(result.stdout.strip()) |
| 135 | + if script_output.get("success"): |
| 136 | + return f"Presentation created successfully. Path: {script_output.get('path')}, Slides: {script_output.get('slides')}" |
| 137 | + else: |
| 138 | + return f"Error creating presentation: {script_output.get('error')}" |
| 139 | + except json.JSONDecodeError: |
| 140 | + return f"Error parsing script output: {result.stdout.strip()}" |
130 | 141 |
|
131 | 142 | except subprocess.CalledProcessError as e: |
132 | 143 | logger.error(f"Error creating presentation: {e.stderr}") |
133 | 144 | return f"Error creating presentation subprocess: {e.stderr}" |
134 | 145 | except Exception as e: |
135 | | - logger.error(f"Error creating presentation: {str(e)}") |
136 | | - return f"Error creating presentation: {str(e)}" |
| 146 | + logger.error(f"Error creating presentation: {e!s}") |
| 147 | + return f"Error creating presentation: {e!s}" |
137 | 148 |
|
138 | 149 | def get_tools(self) -> list[FunctionTool]: |
139 | 150 | r"""Returns a list of FunctionTool objects representing the |
|
0 commit comments