|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from collections.abc import Callable |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from ..connection import Connection, MethodHandler |
| 8 | +from ..interfaces import Agent |
| 9 | +from ..meta import CLIENT_METHODS |
| 10 | +from ..schema import ( |
| 11 | + CreateTerminalRequest, |
| 12 | + CreateTerminalResponse, |
| 13 | + KillTerminalCommandRequest, |
| 14 | + KillTerminalCommandResponse, |
| 15 | + ReadTextFileRequest, |
| 16 | + ReadTextFileResponse, |
| 17 | + ReleaseTerminalRequest, |
| 18 | + ReleaseTerminalResponse, |
| 19 | + RequestPermissionRequest, |
| 20 | + RequestPermissionResponse, |
| 21 | + SessionNotification, |
| 22 | + TerminalOutputRequest, |
| 23 | + TerminalOutputResponse, |
| 24 | + WaitForTerminalExitRequest, |
| 25 | + WaitForTerminalExitResponse, |
| 26 | + WriteTextFileRequest, |
| 27 | + WriteTextFileResponse, |
| 28 | +) |
| 29 | +from ..terminal import TerminalHandle |
| 30 | +from ..utils import notify_model, request_model, request_optional_model |
| 31 | +from .router import build_agent_router |
| 32 | + |
| 33 | +__all__ = ["AgentSideConnection"] |
| 34 | + |
| 35 | +_AGENT_CONNECTION_ERROR = "AgentSideConnection requires asyncio StreamWriter/StreamReader" |
| 36 | + |
| 37 | + |
| 38 | +class AgentSideConnection: |
| 39 | + """Agent-side connection wrapper that dispatches JSON-RPC messages to a Client implementation.""" |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + to_agent: Callable[[AgentSideConnection], Agent], |
| 44 | + input_stream: Any, |
| 45 | + output_stream: Any, |
| 46 | + ) -> None: |
| 47 | + agent = to_agent(self) |
| 48 | + handler = self._create_handler(agent) |
| 49 | + |
| 50 | + if not isinstance(input_stream, asyncio.StreamWriter) or not isinstance(output_stream, asyncio.StreamReader): |
| 51 | + raise TypeError(_AGENT_CONNECTION_ERROR) |
| 52 | + self._conn = Connection(handler, input_stream, output_stream) |
| 53 | + |
| 54 | + def _create_handler(self, agent: Agent) -> MethodHandler: |
| 55 | + router = build_agent_router(agent) |
| 56 | + |
| 57 | + async def handler(method: str, params: Any | None, is_notification: bool) -> Any: |
| 58 | + if is_notification: |
| 59 | + await router.dispatch_notification(method, params) |
| 60 | + return None |
| 61 | + return await router.dispatch_request(method, params) |
| 62 | + |
| 63 | + return handler |
| 64 | + |
| 65 | + async def sessionUpdate(self, params: SessionNotification) -> None: |
| 66 | + await notify_model(self._conn, CLIENT_METHODS["session_update"], params) |
| 67 | + |
| 68 | + async def requestPermission(self, params: RequestPermissionRequest) -> RequestPermissionResponse: |
| 69 | + return await request_model( |
| 70 | + self._conn, |
| 71 | + CLIENT_METHODS["session_request_permission"], |
| 72 | + params, |
| 73 | + RequestPermissionResponse, |
| 74 | + ) |
| 75 | + |
| 76 | + async def readTextFile(self, params: ReadTextFileRequest) -> ReadTextFileResponse: |
| 77 | + return await request_model( |
| 78 | + self._conn, |
| 79 | + CLIENT_METHODS["fs_read_text_file"], |
| 80 | + params, |
| 81 | + ReadTextFileResponse, |
| 82 | + ) |
| 83 | + |
| 84 | + async def writeTextFile(self, params: WriteTextFileRequest) -> WriteTextFileResponse | None: |
| 85 | + return await request_optional_model( |
| 86 | + self._conn, |
| 87 | + CLIENT_METHODS["fs_write_text_file"], |
| 88 | + params, |
| 89 | + WriteTextFileResponse, |
| 90 | + ) |
| 91 | + |
| 92 | + async def createTerminal(self, params: CreateTerminalRequest) -> TerminalHandle: |
| 93 | + create_response = await request_model( |
| 94 | + self._conn, |
| 95 | + CLIENT_METHODS["terminal_create"], |
| 96 | + params, |
| 97 | + CreateTerminalResponse, |
| 98 | + ) |
| 99 | + return TerminalHandle(create_response.terminalId, params.sessionId, self._conn) |
| 100 | + |
| 101 | + async def terminalOutput(self, params: TerminalOutputRequest) -> TerminalOutputResponse: |
| 102 | + return await request_model( |
| 103 | + self._conn, |
| 104 | + CLIENT_METHODS["terminal_output"], |
| 105 | + params, |
| 106 | + TerminalOutputResponse, |
| 107 | + ) |
| 108 | + |
| 109 | + async def releaseTerminal(self, params: ReleaseTerminalRequest) -> ReleaseTerminalResponse | None: |
| 110 | + return await request_optional_model( |
| 111 | + self._conn, |
| 112 | + CLIENT_METHODS["terminal_release"], |
| 113 | + params, |
| 114 | + ReleaseTerminalResponse, |
| 115 | + ) |
| 116 | + |
| 117 | + async def waitForTerminalExit(self, params: WaitForTerminalExitRequest) -> WaitForTerminalExitResponse: |
| 118 | + return await request_model( |
| 119 | + self._conn, |
| 120 | + CLIENT_METHODS["terminal_wait_for_exit"], |
| 121 | + params, |
| 122 | + WaitForTerminalExitResponse, |
| 123 | + ) |
| 124 | + |
| 125 | + async def killTerminal(self, params: KillTerminalCommandRequest) -> KillTerminalCommandResponse | None: |
| 126 | + return await request_optional_model( |
| 127 | + self._conn, |
| 128 | + CLIENT_METHODS["terminal_kill"], |
| 129 | + params, |
| 130 | + KillTerminalCommandResponse, |
| 131 | + ) |
| 132 | + |
| 133 | + async def extMethod(self, method: str, params: dict[str, Any]) -> dict[str, Any]: |
| 134 | + return await self._conn.send_request(f"_{method}", params) |
| 135 | + |
| 136 | + async def extNotification(self, method: str, params: dict[str, Any]) -> None: |
| 137 | + await self._conn.send_notification(f"_{method}", params) |
0 commit comments