from base_agent import BaseAgent, AgentData, RunContext_T
from modules.guiControll.gui_toolkit import press_key_combination, execute_type
from modules.guiControll.web_tool import WebTools
from modules.shellControll.shell_tool import ShellTools
from livekit.agents import AgentSession, JobContext, RoomInputOptions, cli, WorkerOptions
from livekit.plugins import google, noise_cancellation
from livekit.agents.llm import function_tool

class Cypher(BaseAgent):
    def __init__(self):
        super().__init__(
            instructions=f"""
                You are cypher which is responsible for controling desktop on the bases of user input.
                You have following tools:
                - `press_key`: For pressiing key combinations like `ctrl + c` as per user need.
                - `type`: For typing text as per user need remember it take english alphabets only.
                - `open_url`: For opening url in new or current window as per user need you have to locate the url in user input.
                - `web_search`: For searching anything in web as per user need give detail about it but in a formate way do not tell urls and dates like stuff.
                - `news_search`: For searching in news as per user need give detail about it but in a formate way do not tell urls and dates like stuff.
                - `run_command`: For running shell commands as per user need and you have to identify which command full fills user request.
                Rules:
                - If user input is not lie in these categories then respond then just talk to user in a respectfull way.
                - Use simple language while responding to user and use language which is used by user.
                - You don't have to tell users about tools and parameters just use them and send result to user.
                - Send input to tools as per their parameters.
            """
        )

        self.web_tools = WebTools()
        self.shell_tools = ShellTools()

    @function_tool
    async def press_key(self, target: str) -> str:
        return press_key_combination(target)
        
    @function_tool
    async def type(self, target: str) -> str:
        return execute_type(target)

    @function_tool
    async def open_url(self, url: str, new_window: bool = False) -> str:
        return await self.web_tools.open_page(url, new_window)
        

    @function_tool
    async def web_search(self, query: str, max_results: int = 5) -> str:
        return await self.web_tools.duckduckgo_search(query, max_results)

    @function_tool
    async def news_search(self, query: str, max_results: int = 5) -> str:
        return await self.web_tools.duckduckgo_news(query, max_results)
        

    @function_tool
    async def run_command(self, command: str) -> str:
        return self.shell_tools.run_shell_command(command)

    

async def entrypoint(ctx: JobContext):
    cypher = Cypher()
    agent_data = AgentData(ctx=ctx)

    agent_data.personas.update({
        "cypher": cypher
    })

    session = AgentSession[AgentData](
        llm=google.beta.realtime.RealtimeModel(
            voice='Charon'
        ),
        userdata=agent_data
    )

    await session.start(
        agent=cypher,
        room=ctx.room,
        room_input_options=RoomInputOptions(
            noise_cancellation=noise_cancellation.BVC(),
            video_enabled=True
        ),
    )


if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))