File size: 10,800 Bytes
f647629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ec3391
 
 
 
 
 
 
 
f647629
1ec3391
 
 
 
f647629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ec3391
 
 
 
 
 
f647629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
"""
Weave MCP Server - Report creation and editing functionality for W&B reports.
"""

from pathlib import Path
from typing import Dict, List, Optional, Union
import re

import wandb_workspaces.reports.v2 as wr
from dotenv import load_dotenv

import wandb
from wandb_mcp_server.utils import get_rich_logger

# Load environment variables
load_dotenv(dotenv_path=Path(__file__).parent.parent.parent / ".env")

# Configure logging
logger = get_rich_logger(__name__)

CREATE_WANDB_REPORT_TOOL_DESCRIPTION = """Create a new Weights & Biases Report and add text and HTML-rendered charts. Useful to save/document analysis and other findings.

Only call this tool if the user explicitly asks to create a report or save to wandb/weights & biases. 

Always provide the returned report link to the user.

<plots_html_usage_guide>
- If the analsis has generated plots then they can be logged to a Weights & Biases report via converting them to html.
- All charts should be properly rendered in raw HTML, do not use any placeholders for any chart, render everything.
- All charts should be beautiful, tasteful and well proportioned.
- Plot html code should use SVG chart elements that should render properly in any modern browser.
- Include interactive hover effects where it makes sense.
- If the analysis contains multiple charts, break up the html into one section of html per chart.
- Ensure that the axis labels are properly set and aligned for each chart.
- Always use valid markdown for the report text.
</plots_html_usage_guide>

<plots_html_format_guide>
**IMPORTANT: plots_html Parameter Format**
- The plots_html parameter accepts either:
  1. A dictionary where keys are chart names and values are HTML strings: {"chart1": "<html>...</html>", "chart2": "<html>...</html>"}
  2. A single HTML string (will be automatically wrapped with key "chart")
- Do NOT pass raw HTML as a JSON string - pass it directly as an HTML string
- If you have multiple charts, use the dictionary format for better organization
- The tool will provide feedback about how your input was processed
</plots_html_format_guide>

Args:
    entity_name: str, The W&B entity (team or username) - required
    project_name: str, The W&B project name - required
    title: str, Title of the W&B Report - required
    description: str, Optional description of the W&B Report
    markdown_report_text: str, beuatifully formatted markdown text for the report body
    plots_html: str, Optional dict of plot name and html string of any charts created as part of an analysis

Returns:
    str, The url to the report

Example:
    ```python
    # Create a simple report
    report = create_report(
        entity_name="my-team",
        project_name="my-project",
        title="Model Analysis Report",
        description="Analysis of our latest model performance",
        markdown_report_text='''
            # Model Analysis Report
            [TOC]
            ## Performance Summary
            Our model achieved 95% accuracy on the test set.
            ### Key Metrics
            Precision: 0.92
            Recall: 0.89
        '''
    )
    ```
"""


def create_report(
    entity_name: str,
    project_name: str,
    title: str,
    description: Optional[str] = None,
    markdown_report_text: Optional[str] = None,
    plots_html: Optional[Union[Dict[str, str], str]] = None,
) -> Dict[str, str]:
    """
    Create a new Weights & Biases Report and add text and charts. Useful to save/document analysis and other findings.

    Args:
        entity_name: The W&B entity (team or username)
        project_name: The W&B project name
        title: Title of the W&B Report
        description: Optional description of the W&B Report
        markdown_report_text: Optional markdown text for the report body
        plots_html: Optional dict of plot name and html string, or single HTML string

    Returns:
        Dict with 'url' and 'processing_details' keys
    """
    import json
    
    # Process plots_html and collect warnings
    processed_plots_html = None
    processing_warnings = []
    
    if isinstance(plots_html, str):
        try:
            # First try to parse as JSON (dictionary)
            processed_plots_html = json.loads(plots_html)
            processing_warnings.append("Successfully parsed plots_html as JSON dictionary")
        except json.JSONDecodeError:
            # If it's not valid JSON, treat as raw HTML and wrap in dictionary
            if plots_html.strip():  # Only if not empty
                processed_plots_html = {"chart": plots_html}
                processing_warnings.append("plots_html was not valid JSON, treated as raw HTML and wrapped with key 'chart'")
            else:
                processed_plots_html = None
                processing_warnings.append("plots_html was empty string, no charts will be included")
    elif isinstance(plots_html, dict):
        processed_plots_html = plots_html
        processing_warnings.append(f"Successfully processed plots_html dictionary with {len(plots_html)} chart(s)")
    elif plots_html is None:
        processing_warnings.append("No plots_html provided, report will contain only text content")
    else:
        processing_warnings.append(f"Unexpected plots_html type: {type(plots_html)}, no charts will be included")
        processed_plots_html = None

    # Get the current API key from context
    from wandb_mcp_server.api_client import WandBApiManager
    api_key = WandBApiManager.get_api_key()
    
    # Store original environment key
    import os
    old_key = os.environ.get("WANDB_API_KEY")
    
    try:
        # Set API key temporarily for wandb.init()
        if api_key:
            os.environ["WANDB_API_KEY"] = api_key
        
        wandb.init(
            entity=entity_name, project=project_name, job_type="mcp_report_creation"
        )

        # Initialize the report
        report = wr.Report(
            entity=entity_name,
            project=project_name,
            title=title,
            description=description or "",
            width="fluid",
        )

        # Log plots
        plots_dict = {}
        if processed_plots_html:
            for plot_name, html in processed_plots_html.items():
                wandb.log({plot_name: wandb.Html(html)})
                plots_dict[plot_name] = html

            pg = []
            for k, v in plots_dict.items():
                pg.append(
                    wr.PanelGrid(
                        panels=[
                            wr.MediaBrowser(
                                media_keys=[k],
                                num_columns=1,
                                layout=wr.Layout(w=20, h=20),  # , x=5, y=5)
                            ),
                        ]
                    )
                )
        else:
            pg = None

        blocks = parse_report_content_enhanced(markdown_report_text or "")

        # Add blocks if provided
        if pg:
            report.blocks = blocks + pg
        else:
            report.blocks = blocks

        logger.info(f"Report blocks: {report.blocks}")

        # Save the report
        report.save()
        wandb.finish()
        logger.info(f"Created report: {title}")
        
        return {
            "url": report.url,
            "processing_details": processing_warnings
        }

    except Exception as e:
        logger.error(f"Error creating report: {e}")
        # Include processing details in the error for better debugging
        error_msg = f"Error creating report: {e}"
        if processing_warnings:
            error_msg += f"\n\nProcessing details: {'; '.join(processing_warnings)}"
        raise Exception(error_msg)
    finally:
        # Restore original environment variable
        if old_key:
            os.environ["WANDB_API_KEY"] = old_key
        elif "WANDB_API_KEY" in os.environ:
            del os.environ["WANDB_API_KEY"]


def edit_report(
    report_url: str,
    title: Optional[str] = None,
    description: Optional[str] = None,
    blocks: Optional[List[Union[wr.H1, wr.H2, wr.H3, wr.PanelGrid]]] = None,
    append_blocks: bool = False,
) -> wr.Report:
    """
    Edit an existing W&B report.

    Args:
        report_url: The URL of the report to edit
        title: Optional new title for the report
        description: Optional new description for the report
        blocks: Optional list of blocks to update or append
        append_blocks: If True, append new blocks to existing ones. If False, replace existing blocks.
    """
    try:
        # Load the existing report
        report = wr.Report.from_url(report_url)

        # Update title if provided
        if title:
            report.title = title

        # Update description if provided
        if description:
            report.description = description

        # Update blocks if provided
        if blocks:
            if append_blocks:
                # Append new blocks to existing ones
                report.blocks = (report.blocks or []) + blocks
            else:
                # Replace existing blocks
                report.blocks = blocks

        # Save the changes
        report.save()

        logger.info(f"Updated report: {report.title}")
        return report

    except Exception as e:
        logger.error(f"Error editing report: {e}")
        raise


def parse_report_content_enhanced(
    text: str,
) -> List[Union[wr.H1, wr.H2, wr.H3, wr.P, wr.TableOfContents]]:
    """
    Parse markdown-like text into W&B report blocks with paragraph grouping.
    """
    blocks = []
    lines = text.strip().split("\n")

    current_paragraph = []

    for line in lines:
        # Check if this is a special line (header or TOC)
        h1_match = re.match(r"^# (.+)$", line)
        h2_match = re.match(r"^## (.+)$", line)
        h3_match = re.match(r"^### (.+)$", line)
        is_toc = line.strip().lower() == "[toc]"

        # If we hit a special line and have paragraph content, finalize the paragraph
        if (h1_match or h2_match or h3_match or is_toc) and current_paragraph:
            blocks.append(wr.P("\n".join(current_paragraph)))
            current_paragraph = []

        # Handle the current line
        if h1_match:
            blocks.append(wr.H1(h1_match.group(1)))
        elif h2_match:
            blocks.append(wr.H2(h2_match.group(1)))
        elif h3_match:
            blocks.append(wr.H3(h3_match.group(1)))
        elif is_toc:
            blocks.append(wr.TableOfContents())
        else:
            if line.strip():  # Only add non-empty lines
                current_paragraph.append(line)

    # Don't forget any remaining paragraph content
    if current_paragraph:
        blocks.append(wr.P("\n".join(current_paragraph)))

    return blocks