-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathconnection.py
More file actions
129 lines (108 loc) · 4.09 KB
/
connection.py
File metadata and controls
129 lines (108 loc) · 4.09 KB
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
from __future__ import annotations
import asyncio
from collections.abc import Callable
from typing import Any
from ..connection import Connection, MethodHandler
from ..interfaces import Agent, Client
from ..meta import AGENT_METHODS
from ..schema import (
AuthenticateRequest,
AuthenticateResponse,
CancelNotification,
InitializeRequest,
InitializeResponse,
LoadSessionRequest,
LoadSessionResponse,
NewSessionRequest,
NewSessionResponse,
PromptRequest,
PromptResponse,
SetSessionModelRequest,
SetSessionModelResponse,
SetSessionModeRequest,
SetSessionModeResponse,
)
from ..utils import (
notify_model,
request_model,
request_model_from_dict,
)
from .router import build_client_router
__all__ = ["ClientSideConnection"]
_CLIENT_CONNECTION_ERROR = "ClientSideConnection requires asyncio StreamWriter/StreamReader"
class ClientSideConnection:
"""Client-side connection wrapper that dispatches JSON-RPC messages to an Agent implementation."""
def __init__(
self,
to_client: Callable[[Agent], Client],
input_stream: Any,
output_stream: Any,
) -> None:
if not isinstance(input_stream, asyncio.StreamWriter) or not isinstance(output_stream, asyncio.StreamReader):
raise TypeError(_CLIENT_CONNECTION_ERROR)
client = to_client(self) # type: ignore[arg-type]
handler = self._create_handler(client)
self._conn = Connection(handler, input_stream, output_stream)
def _create_handler(self, client: Client) -> MethodHandler:
router = build_client_router(client)
async def handler(method: str, params: Any | None, is_notification: bool) -> Any:
if is_notification:
await router.dispatch_notification(method, params)
return None
return await router.dispatch_request(method, params)
return handler
async def initialize(self, params: InitializeRequest) -> InitializeResponse:
return await request_model(
self._conn,
AGENT_METHODS["initialize"],
params,
InitializeResponse,
)
async def newSession(self, params: NewSessionRequest) -> NewSessionResponse:
return await request_model(
self._conn,
AGENT_METHODS["session_new"],
params,
NewSessionResponse,
)
async def loadSession(self, params: LoadSessionRequest) -> LoadSessionResponse:
return await request_model_from_dict(
self._conn,
AGENT_METHODS["session_load"],
params,
LoadSessionResponse,
)
async def setSessionMode(self, params: SetSessionModeRequest) -> SetSessionModeResponse:
return await request_model_from_dict(
self._conn,
AGENT_METHODS["session_set_mode"],
params,
SetSessionModeResponse,
)
async def setSessionModel(self, params: SetSessionModelRequest) -> SetSessionModelResponse:
return await request_model_from_dict(
self._conn,
AGENT_METHODS["session_set_model"],
params,
SetSessionModelResponse,
)
async def authenticate(self, params: AuthenticateRequest) -> AuthenticateResponse:
return await request_model_from_dict(
self._conn,
AGENT_METHODS["authenticate"],
params,
AuthenticateResponse,
)
async def prompt(self, params: PromptRequest) -> PromptResponse:
return await request_model(
self._conn,
AGENT_METHODS["session_prompt"],
params,
PromptResponse,
)
async def cancel(self, params: CancelNotification) -> None:
await notify_model(self._conn, AGENT_METHODS["session_cancel"], params)
async def extMethod(self, method: str, params: dict[str, Any]) -> dict[str, Any]:
return await self._conn.send_request(f"_{method}", params)
async def extNotification(self, method: str, params: dict[str, Any]) -> None:
await self._conn.send_notification(f"_{method}", params)