File size: 1,807 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
# SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0

from typing import Any, override

from copilotj.plugin._base import Request, Response, Verbosity
from copilotj.plugin.awt import SnapshotDifference

__all__ = ["ScriptResult", "ScriptRequest"]

TIP_RESULT_NONE = """\
No result was returned, but the script likely ran successfully.\
"""

TIP_NO_OUTPUTS = """\
But due to the limitation of our ImageJ plugin, some outputs and errors may not be detected. If you are not sure, you \
can kindly ask user check the 'Debug' panel.\
"""


class ScriptResult(Response):
    outputs: dict[str, Any] | None
    err: str | None
    snapshot: SnapshotDifference | None = None

    @override
    def _describe(self, *, level: int, verbosity: Verbosity) -> list[str]:
        heading = "#" * level

        lines = []
        lines.append(f"{heading} Results")

        if self.outputs is not None and len(self.outputs) > 0:
            if len(self.outputs) == 1 and "result" in self.outputs:
                lines.append(self.outputs["result"] or TIP_RESULT_NONE)
            else:
                lines.append("Outputs:")
                for k, v in self.outputs.items():
                    lines.append(f"- {k}: {v}")

        elif self.err:
            lines.append(f"Error: {self.err}")

        else:
            tip = "No output detected."
            if (self.outputs is None or len(self.outputs) == 0) or (
                self.snapshot is None or not self.snapshot.any_changed
            ):
                tip += " " + TIP_NO_OUTPUTS

            lines.append(tip)

        return lines


class ScriptRequest(Request[ScriptResult], event="run_script", response_type=ScriptResult, timeout=16):
    language: str
    script: str
    timeout: int