File size: 26,947 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0

import asyncio
import logging
import platform
import signal
import threading
import sys
import textwrap
import warnings
from pathlib import Path
from typing import Annotated

from copilotj.core import ImageMessage, TextMessage, new_vlm_model_client
from copilotj.core.config import Config
from copilotj.core.lifecycle import register_cleanup
from copilotj.plugin.api import ClientPluginAPI
from copilotj.util import JupyterNotebook

__all__ = [
    "PluginTools",
    "PyimagejRunner",
    "execute_python_script",
]

_log = logging.getLogger(__name__)


class PluginTools:
    def __init__(self, apis: ClientPluginAPI, *, cfg: Config):
        super().__init__()
        self.apis = apis
        self._cfg = cfg

    def _detect_timeout_from_script(self, script: str) -> float:
        script_lower = script.lower()
        if any(keyword in script_lower for keyword in ["batch", "for(", "for (", "getfilelist", "while"]):
            return 180.0
        return 15.0

    async def run_macro(
        self,
        script: Annotated[str, "Valid ImageJ macro script to execute"],
        timeout: Annotated[float | None, "Timeout in seconds (auto-detected: 15s normal, 180s for batch)"] = None,
        verify_result: Annotated[bool, "Enable visual verification of operation results (costs extra time)"] = False,
        operation_intent: Annotated[
            str | None, "Required if verify_result=True: describe what the operation should achieve"
        ] = None,
    ) -> str:
        if timeout is None:
            timeout = self._detect_timeout_from_script(script)
        try:
            script = script + "\n" + 'print("Macro executed.");'
            response = await self.apis.run_script("macro", script, timeout=timeout)
        except asyncio.TimeoutError:
            raise RuntimeError(
                f"Script execution timeout ({timeout}s). For batch processing, consider breaking down the script or manually setting a longer timeout."
            )

        if response.err or "Error" in str(response):
            # Get basic window info instead of full perception for errors
            window_info = await self.imagej_windowInfo()
            raise RuntimeError(
                f"Error during execution: {str(response)} \n Please check and adjust the script and try again. \n ImageJ window info: {window_info}"
            )

        # Perform state check if needed
        result = f"{script} has executed successfully.\n"

        # Perform result verification if requested
        if verify_result and operation_intent and self._cfg.vision_enabled:
            verification = await self.simple_result_verification(operation_intent)
            result += f"\nOperation verification: {verification}"

        return result

    async def capture_image(self, title: str | None = None) -> str:
        """Capture ImageJ Image."""
        return await self.apis.capture_image(title)

    async def label_image(self) -> str:
        """Execute the human-in-the-loop mask export macro for current image set."""
        macro_script = label_macro()
        await self.run_macro(macro_script, timeout=300.0)
        return "The label_image macro has been executed successfully."

    async def capture_screen(self, query: str | None = None) -> str:
        """Capture ImageJ Screen."""
        if not self._cfg.vision_enabled:
            window_info = await self.imagej_windowInfo()
            return f"[Vision disabled] Basic ImageJ window info:\n{window_info}"

        if not self._cfg.vision_available:
            window_info = await self.imagej_windowInfo()
            return (
                "[Vision not available] The configured model does not support image input. "
                "Please configure a vision-capable model (COPILOTJ_VLM_MODEL) "
                "or use a model with vision support.\n"
                f"Basic ImageJ window info:\n{window_info}"
            )

        vlm = new_vlm_model_client(self._cfg)
        imagejCapture_resp = await self.apis.capture_screen()
        vision_prompt = """
You are an ImageJ Vision Perception tool.  
Your task is to analyze the screenshot of the current ImageJ environment and return a 
detailed Markdown report describing the open images, window states, and biological content.

Your output MUST follow the exact Markdown structure below.

# 🖼️ Image Status
- **Has image:** yes/no  
- **Active window:** (window title or "none")  
- **File path:** (absolute path if visible; if not, write "unknown")  
- **Bit depth:** (8-bit / 16-bit / RGB / other)  
- **Channels detected:** e.g., C1=Green, C2=Red, Brightfield, etc.  
- **Is stack:** yes/no  
- **Number of slices:** (if known)  

# 🪟 Screen & Window Information
- **Open ImageJ windows:** list all visible windows  
- **Active ROI:** rectangle / polygon / line / none  
- **Scale bar present:** yes/no  
- **Z-position:** (if a stack)  
- **Frame index / slice index:** (if visible)  

# 🔬 Biological & Visual Content Interpretation
Provide a cautious but helpful interpretation of what is visible in the image:
- **Objects present:** e.g., "elongated bacteria-like rods", "nuclei", "fibrous structures"  
- **Possible cell types or organism types:** Macrophage / RBC / bacteria / neurons / yeast (if recognizable)  
- **Morphology summary:** e.g., “elongated rods”, “round cells”, “clustered nuclei”  
- **Fluorescence interpretation:** e.g., “green channel shows rod-like fluorescent structures suggesting GFP-tagged bacteria”  

Be cautious: **If uncertain, explicitly say uncertain.**

# 📉 Image Quality Assessment
Describe the technical quality of the image:
- **Brightness:** low / normal / high  
- **Contrast:** low / normal / high  
- **Noise level:** low / medium / high / severe  
- **Uneven background:** yes/no  
- **Focus quality:** sharp / soft / blurry  

# 🔧 Preprocessing & Analysis Recommendations
Provide recommendations for ImageJ preprocessing:
- **Should invert:** yes/no (based on foreground polarity)  
- **Recommended denoising:** Gaussian / Median / NLM / none  
- **Background correction needed:** yes/no + suggested radius  +
- **Recommended contrast enhancement:** e.g., CLAHE  
- **Suggested threshold method:** Otsu, Triangle, Yen, Moments, Auto, or user manual
- **Segmentation difficulty:** easy / moderate / difficult  
- **Suggested follow-up operations:** Open/Close, Fill Holes, Watershed, Analyze Particles  

# 📝 Notes
Provide any additional warnings or clarifications:
- e.g., “The image appears very noisy; segmentation will need preprocessing.”
- e.g., “Bacteria-like rods are dense; watershed may be unreliable.”

---

## Rules
1. All ImageJ metadata (window titles, file path, channel names, bit depth) must be correct.  
2. If an image is open, the **file path must appear** in the output when visible on screen.  
3. Biological interpretation must be reasonable and cautious.  
"""

        if query:
            vision_prompt += query
        imagejCapture = await vlm.create(
            [ImageMessage(role="user", image=i.image) for i in imagejCapture_resp.screenshots]
            + [TextMessage(role="user", text=vision_prompt)]
        )
        return imagejCapture.content or "no response"

    async def imagej_perception(self, query: str | None = None) -> str:
        """ImageJ Perception Tool."""
        perception = await self.imagej_windowInfo()

        if not self._cfg.vision_enabled:
            return f"[Vision disabled] ImageJ Window Information:\n{perception}"

        if not self._cfg.vision_available:
            return (
                "[Vision not available] The configured model does not support image input. "
                "Please configure a vision-capable model (COPILOTJ_VLM_MODEL) "
                "or use a model with vision support.\n"
                f"ImageJ Window Information:\n{perception}"
            )

        PROMPT_TEMPLATE = """

This is the leader agent's request: {{QUERY}}

For your reference, Following is the perception of ImageJ,
imagejStatus summarizes the status of ImageJ.
imagejOperation is the monitor event history of ImageJ.

{{PERCEPTION}}
"""
        prompt = PROMPT_TEMPLATE.replace("{{QUERY}}", str(query)).replace("{{PERCEPTION}}", perception)
        perception_str = await self.capture_screen(prompt)
        return perception_str

    async def imagej_windowInfo(self) -> str:
        """ImageJ Window Information Tool."""
        imagejStatus = await self.apis.take_snapshot()
        imagejOperation = await self.apis.get_operation_history()

        # Format as readable text instead of JSON to avoid string replacement issues
        result = f"""ImageJ Window Information:

=== ImageJ Status ===
{imagejStatus}

=== ImageJ Operation History ===
{imagejOperation}
"""
        return result

    async def simple_result_verification(self, operation_intent: str, expected_outcome: str = None) -> str:
        """Simple perception-based result verification."""
        if not self._cfg.vision_enabled:
            return "[Vision disabled] Visual verification skipped. Operation assumed successful."

        if not self._cfg.vision_available:
            return (
                "[Vision not available] Visual verification skipped — "
                "the configured model does not support image input. "
                "Operation assumed successful."
            )

        vlm = new_vlm_model_client(self._cfg)
        imagejCapture_resp = await self.apis.capture_screen()

        verification_prompt = f"""
You are an ImageJ Result Verification assistant. 

OPERATION INTENT: {operation_intent}
EXPECTED OUTCOME: {expected_outcome or "Not specified"}

Please analyze the current ImageJ screen and provide a BRIEF assessment:

1. **Operation Success**: Did the intended operation appear to complete successfully?
2. **Visual Result**: Briefly describe what you see (e.g., "threshold applied", "new window opened", "ROI selected")
3. **Potential Issues**: Any obvious problems or suboptimal results?
4. **Recommendation**: Should we proceed, retry, or adjust the approach?

Keep your response concise and focused on actionable feedback.
"""

        imagej_verification = await vlm.create(
            [ImageMessage(role="user", image=i.image) for i in imagejCapture_resp.screenshots]
            + [TextMessage(role="user", text=verification_prompt)]
        )
        return imagej_verification.content or "no verification response"


async def folder_summary(folder_path: str):
    if folder_path.strip() == ".":
        return "The current directory is not a valid folder path, try to ask the user to provide a specific folder path or confirm the current directory."

    folder = Path(folder_path)
    if not folder.exists() or not folder.is_dir():
        return f"The path '{folder_path}' is not a valid directory. Please ignore this tool and continue."

    summary = []
    MAX_FILES = 300

    for path in folder.rglob("*"):
        if len(summary) >= MAX_FILES:
            break
        relative_path = path.relative_to(folder)
        if path.is_file():
            summary.append(f"{relative_path}")
        elif path.is_dir():
            summary.append(f"Directory: {relative_path}")

    folder_summary = []
    for index, item in enumerate(summary, start=1):
        folder_summary.append(f"File {index}: {item}")

    files_summary = ",".join(folder_summary)

    total_files_msg = ""
    if len(summary) >= MAX_FILES:
        total_files_msg = f" (Showing first {MAX_FILES} items, more files may exist, need to ask user to check the folder directly if necessary)"

    prompt = f"""\
The folder path is set to: {folder_path}.\
The provided folder and uploaded files contain the following items{total_files_msg}:
{files_summary}.\
"""

    return prompt


async def system_info(add_python_info: bool = False):
    """
    Returns a string with information about the system: machine, operating system (all details), version of Python, etc.
    """

    info = {
        "Machine": f"{platform.machine()} ({platform.processor()} {platform.architecture()[0]})",
        "Version": platform.version().split(":")[0],
        "Platform": platform.platform(),
        "System": platform.system(),
    }

    if add_python_info:
        info |= {
            "Python Version": sys.version,
            "Python Compiler": platform.python_compiler(),
            "Python Implementation": platform.python_implementation(),
            "Python Build": platform.python_build(),
        }

    info_str = "```system_info\n"
    info_str += "\n".join([f"{key}: {value}" for key, value in info.items()])
    info_str += "\n```\n"

    return info_str


# Define tools
class PyimagejRunner:
    """Handles ImageJ execution within a Jupyter Notebook."""

    _notebook: JupyterNotebook

    def __init__(self, path_fiji: str | None = None):
        super().__init__()

        self._notebook = JupyterNotebook()
        mode = "interactive"

        if sys.platform == "darwin":
            warnings.warn("Running in headless mode on macOS.")
            mode = "headless"

        if path_fiji is not None:
            python_code = textwrap.dedent(f"""
                import imagej
                ij = imagej.init("{path_fiji}", mode="{mode}")
                ij.ui().showUI()
            """)
        else:
            python_code = textwrap.dedent(f"""
                import imagej
                ij = imagej.init(mode="{mode}")
                ij.ui().showUI()
            """)

        _, ok = self._notebook.add_and_run(python_code)
        if not ok:
            self._notebook.close()
            raise RuntimeError("Failed to initialize ImageJ.")

    def run_macro(self, script: str) -> str:
        """Runs an ImageJ macro inside Jupyter Notebook."""
        pyscript = rf"""ij.py.run_macro(r'''{script}''')"""
        result, ok = self._notebook.add_and_run(pyscript)

        if not ok:
            return f"Error during execution:\n{result}"

        return result

    def close(self) -> None:
        """Closes the notebook session."""
        self._notebook.close()


# execute_python_script tool
class JupyterClient:
    """Handles Python code execution using Jupyter client with proper lifecycle management."""

    def __init__(self):
        """Initialize Jupyter client."""
        self._notebook = None
        self._initialize_notebook()

        # Register cleanup via lifecycle (signal handler and server
        # on_shutdown both call run_cleanup()).  lifecycle also
        # registers run_cleanup with atexit as a safety net.
        self._register_signal_handlers()
        register_cleanup("jupyter_client", self.cleanup)

    def _register_signal_handlers(self):
        """Register signal handlers only if running in the main thread.

        In Appose managed mode, the server runs in a daemon thread while
        Appose's stdin loop occupies the main thread.  Python's signal
        module requires main-thread execution, so we skip registration
        when running outside the main thread.  Cleanup is still guaranteed
        via :mod:`copilotj.core.lifecycle` and ``atexit``.
        """
        if threading.current_thread() is not threading.main_thread():
            _log.info("Skipping Jupyter signal handlers outside the main thread")
            return
        signal.signal(signal.SIGTERM, self._signal_handler)
        signal.signal(signal.SIGINT, self._signal_handler)

    def _initialize_notebook(self):
        """Initialize or reinitialize the notebook."""
        try:
            if self._notebook:
                self._notebook.close()
        except Exception:
            pass

        self._notebook = JupyterNotebook()

    def _signal_handler(self, signum, frame):
        """Handle system signals."""
        _log.warning("Received signal %d, cleaning up Jupyter client", signum)
        self.cleanup()

    def execute_code(self, code: str) -> str:
        """Execute Python code using Jupyter client with auto-recovery.

        Args:
            code: Python code to execute

        Returns:
            str: Execution result
        """
        max_retries = 2
        for attempt in range(max_retries):
            try:
                if not self._notebook:
                    self._initialize_notebook()

                assert self._notebook is not None
                result, ok = self._notebook.add_and_run(code)
                if not ok:
                    # Check if it's a kernel issue
                    if "kernel" in result.lower() or "parent appears to have exited" in result.lower():
                        _log.warning("Kernel issue detected on attempt %d, reinitializing", attempt + 1)
                        self._initialize_notebook()
                        if attempt < max_retries - 1:
                            continue
                    return f"Error executing code: {result}"
                return result

            except Exception as e:
                error_msg = str(e)
                if attempt < max_retries - 1 and (
                    "kernel" in error_msg.lower() or "parent appears to have exited" in error_msg.lower()
                ):
                    _log.warning("Connection issue on attempt %d, retrying", attempt + 1)
                    self._initialize_notebook()
                    continue
                return f"Error executing code: {error_msg}"

        return "Error: Failed to execute code after multiple attempts"

    def cleanup(self):
        """Clean up Jupyter client resources."""
        if self._notebook:
            try:
                _log.info("Shutting down Jupyter kernel...")
                self._notebook.close()
                _log.info("Jupyter kernel shut down successfully")
            except Exception as e:
                _log.error("Error shutting down Jupyter kernel: %s", e)
            finally:
                self._notebook = None

    def __del__(self):
        """Destructor to ensure cleanup."""
        self.cleanup()


# Global Jupyter client instance with proper cleanup
_jupyter_client = None


def get_jupyter_client() -> JupyterClient:
    """Get or create Jupyter client."""
    global _jupyter_client
    if _jupyter_client is None:
        _jupyter_client = JupyterClient()
    return _jupyter_client


def cleanup_jupyter_client():
    """Cleanup global Jupyter client."""
    global _jupyter_client
    if _jupyter_client:
        _jupyter_client.cleanup()
        _jupyter_client = None


async def execute_python_script(script: Annotated[str, "Python script to execute"]) -> str:
    """
    Execute Python scripts for image processing with deep learning models.
    Enhanced with kernel recovery and better error handling.

    Args:
        script: Python script to execute

    Returns:
        str: Result of the script execution
    """
    try:
        # Get Jupyter client
        client = get_jupyter_client()

        # Add a small delay to ensure kernel is ready
        await asyncio.sleep(0.1)

        try:
            # Execute the script using Jupyter with timeout
            result = await asyncio.wait_for(
                asyncio.to_thread(client.execute_code, script),
                timeout=300,  # 5 minutes timeout
            )

            # Check for kernel shutdown warning
            if "parent appears to have exited" in result.lower():
                _log.warning("Jupyter kernel shutdown detected, attempting recovery...")
                # Try once more with a fresh client
                cleanup_jupyter_client()
                client = get_jupyter_client()
                await asyncio.sleep(0.5)  # Wait for initialization
                result = await asyncio.to_thread(client.execute_code, script)

            # Check if there's an error in the result
            if result.startswith("Error executing code:"):
                return f"❌ Script execution failed:\n{result}"

            # Additional output length limit protection
            max_output_length = 8000
            if len(result) > max_output_length:
                truncated_length = max_output_length - 200
                result = (
                    result[:truncated_length]
                    + f"\n\n... [Output truncated, original length: {len(result)} characters] ..."
                )

            # Return successful result
            return (
                f"✅ Script executed successfully:\n{result}"
                if result
                else "✅ Script executed successfully (no output)"
            )

        except asyncio.TimeoutError:
            return "❌ Error executing code: Timeout after 2 minutes"

        except Exception as e:
            error_msg = str(e)
            if "kernel" in error_msg.lower() or "parent appears to have exited" in error_msg.lower():
                _log.warning("Kernel issue detected, attempting recovery...")
                cleanup_jupyter_client()
                return "⚠️ Jupyter kernel restarted due to connection issue. Please try again."

            return f"❌ Error executing code: {error_msg}"

    except Exception as e:
        return f"❌ Failed to get Jupyter client: {str(e)}"


def label_macro():
    """Returns the macro script for human-in-the-loop mask export for current image set."""
    macro_script = textwrap.dedent("""
    macro "CopilotJ Save Each ROI Mask (English)" {

    Dialog.create("CopilotJ HITL Mask Export");
    Dialog.addDirectory("Images directory", "");
    Dialog.addDirectory("Masks output directory", "");
    Dialog.addCheckbox("Export full-size masks (Edit>Fill style)", true);
    Dialog.addCheckbox("Use ROI name for file naming (if available)", true);
    Dialog.show();

    imgsDir   = Dialog.getString();
    masksDir  = Dialog.getString();
    fullSize  = Dialog.getCheckbox();
    useRoiName= Dialog.getCheckbox();

    if (imgsDir=="") imgsDir=getDirectory("Choose images directory");
    if (masksDir=="") masksDir=getDirectory("Choose masks output directory");
    if (imgsDir=="" || masksDir=="") exit("Cancelled.");

    imgsDir  = ensureSlash(imgsDir);
    masksDir = ensureSlash(masksDir);

    File.makeDirectory(masksDir);
    list = getFileList(imgsDir);
    if (list.length==0) exit("No image files found in the directory.");

    exts = newArray(".tif",".tiff",".png",".jpg",".jpeg",
                    ".TIF",".TIFF",".PNG",".JPG",".JPEG");

    for (k=0; k<list.length; k++) {
        if (!endsWithAny(list[k], exts)) continue;

        inPath = imgsDir + list[k];
        if (!File.exists(inPath)) {
            print("⚠️ File not found: " + inPath);
            continue;
        }

        open(inPath);
        title = getTitle();
        getDimensions(W,H,C,Z,T);

        if (!isOpen("ROI Manager")) run("ROI Manager...");
        roiManager("reset");
        roiManager("Show All");

        waitForUser("Current image: " + title + 
            "\n\nUse ROI tools to annotate objects.\n" +
            "Click OK to save masks and continue to the next image.\n" +
            "Click Cancel to stop.");

        if (roiManager("count")==0) { close(); continue; }

        // --- Per-ROI export ---
        imgStem = stripExt(title);
        outDir  = masksDir + imgStem + File.separator;
        File.makeDirectory(outDir);

        n = roiManager("count");
        print("Exporting " + n + " ROI masks → " + outDir);

        selectWindow(title);
        for (i=0; i<n; i++) {
            roiManager("Select", i);

            // ---- filename: ROI name if available, else roi_XXX ----
            idxName = "roi_" + zeroPad3(i+1);
            rname   = Roi.getName();
            if (useRoiName && rname != "") {
                safe = sanitize(rname);
                if (safe != "") idxName = safe;
            }

            if (fullSize) {
                // --- Full-size mask (Edit>Fill on blank canvas) ---
                newImage("__tmp__", "8-bit black", W, H, 1);
                tmpID = getImageID();              // lock the new window
                selectImage(tmpID);
                run("Restore Selection");
                setForegroundColor(255,255,255);
                run("Fill", "slice");

                // enforce strict binary + fill interior on this exact window
                enforce_binary_and_fill_windowID(tmpID);

                // save & close
                selectImage(tmpID);
                saveAs("Tiff", outDir + idxName + ".tif");
                close();
            } else {
                // --- Cropped mask (Create Mask) ---
                run("Restore Selection");
                run("Create Mask");                // opens a new small window and activates it
                maskID = getImageID();             // lock the mask window by ID
                selectImage(maskID);
                rename("__roi_mask__" + zeroPad3(i+1));  // optional: stable name

                // enforce strict binary + fill interior on this exact window
                enforce_binary_and_fill_windowID(maskID);

                // save & close
                selectImage(maskID);
                saveAs("Tiff", outDir + idxName + ".tif");
                close();
            }
        }

        close(); // close original image
        print("✅ Saved ROI masks to: " + outDir);
    }

    showMessage("Done", "All ROI masks have been exported to:\n" + masksDir);
}

// ---- Utility functions ----
function stripExt(s) {
    dot = lastIndexOf(s, ".");
    if (dot>=0) return substring(s, 0, dot);
    else return s;
}
function endsWithAny(s, arr) {
    for (i=0; i<arr.length; i++)
        if (endsWith(s, arr[i])) return true;
    return false;
}
function ensureSlash(path) {
    if (!endsWith(path, "/") && !endsWith(path, "\\")) return path + File.separator;
    return path;
}
function zeroPad3(n) {
    if (n < 10)  return "00"+n;
    if (n < 100) return "0"+n;
    return ""+n;
}
function sanitize(s) {
    s = replace(s, " ", "_");
    s = replace(s, "[/\\\\:\\*\\?\"<>\\|]", "-");
    return s;
}

// Make a specific window strictly binary (0 background, 255 foreground) and solid-fill interior
function enforce_binary_and_fill_windowID(winID) {
    selectImage(winID);
    run("8-bit");
    setOption("BlackBackground", true);
    run("Make Binary");        // -> 0/255
    // run("Close");           // uncomment only if outlines have small gaps
    run("Fill Holes");         // solid white inside
    run("Grays");
}
""")
    return macro_script


if __name__ == "__main__":
    from copilotj.core import load_config

    load_config()

    # Run the test
    # async def test_deep_research():
    #     result = await deep_research("how to analyze time-lapse images of cell migration")
    #     print(result)

    # Run the async test
    # asyncio.run(test_deep_research())