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

from typing import Any, Literal

from copilotj.core.agent import Agent
from copilotj.core.runtime import Runtime
from copilotj.core.ui import UI, DialogChange

__all__ = ["Pattern"]


class Pattern:
    def __init__(self, name: str, *, ui: UI | None = None) -> None:
        super().__init__()
        self._runtime = Runtime(name, ui=ui)
        self._agents: dict[str, Agent] = {}

    #########################
    #      Rigistration     #
    #########################

    def register(self, agent: Agent) -> None:
        """Register an agent with the pattern.

        Args:
            agent: The agent to register

        Raises:
            ValueError: If an agent with the same name is already registered
        """
        name = agent.name
        if name in self._agents and self._agents[name] is not agent:
            raise ValueError(f"Agent '{name}' already registered")

        agent._set_runtime(self._runtime)
        self._agents[name] = agent

    def _agent(self, name: str) -> Agent:
        """Get a registered agent by name."""
        if name in self._agents:
            raise ValueError(f"Agent '{name}' not registered")

        return self._agents[name]

    def __setattr__(self, name: str, value: object) -> None:
        if isinstance(value, Agent):
            self.register(value)

        super().__setattr__(name, value)

    #########################
    #          UI           #
    #########################

    async def request_user_confirm(self, message: str) -> bool:
        """Request user confirmation through the UI.

        Args:
            message: The confirmation message to display

        Returns:
            approval: True if user confirms, False otherwise
        """
        return await self._runtime.request_user_confirm(message)

    async def dialog_changed(self, dialog_id: int, state: Literal["completed"]) -> None:
        await self._runtime.print_dialog_change(DialogChange(id=str(dialog_id), state=state))

    def log_info(self, message: str) -> None:
        self._runtime.log_info(message)

    def log_error(self, message: str) -> None:
        self._runtime.log_error(message)

    #########################
    #       Messaging       #
    #########################

    async def _send_message(self, name: str, message: Any) -> None:
        """Send a message to a specific agent by name.

        Args:
            name: The name of the agent to send the message to
            message: The message to send (type should match what the agent expects)

        Raise:
            ValueError: If no agent with the given name is registered
        """
        if name not in self._agents:
            raise ValueError(f"No agent registered with name: {name}")

        agent = self._agents[name]
        await agent.on_message(message)
        raise NotImplementedError("Not implemented")