|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from abc import ABC, abstractmethod |
| 3 | +from abc import abstractmethod |
4 | 4 | from dataclasses import dataclass |
5 | | -from typing import TYPE_CHECKING, Generic, Literal, TypeAlias, TypeVar |
| 5 | +from typing import TYPE_CHECKING, Any |
6 | 6 |
|
7 | 7 |
|
8 | 8 | if TYPE_CHECKING: |
9 | 9 | from a2a.client.client import ClientCallContext |
10 | 10 |
|
11 | 11 | from a2a.types.a2a_pb2 import ( # noqa: TC001 |
12 | 12 | AgentCard, |
13 | | - CancelTaskRequest, |
14 | | - DeleteTaskPushNotificationConfigRequest, |
15 | | - GetExtendedAgentCardRequest, |
16 | | - GetTaskPushNotificationConfigRequest, |
17 | | - GetTaskRequest, |
18 | | - ListTaskPushNotificationConfigsRequest, |
19 | | - ListTaskPushNotificationConfigsResponse, |
20 | | - ListTasksRequest, |
21 | | - ListTasksResponse, |
22 | | - SendMessageRequest, |
23 | | - SendMessageResponse, |
24 | | - StreamResponse, |
25 | | - SubscribeToTaskRequest, |
26 | | - Task, |
27 | | - TaskPushNotificationConfig, |
28 | 13 | ) |
29 | 14 |
|
30 | 15 |
|
31 | | -M = TypeVar('M') |
32 | | -P = TypeVar('P') |
33 | | -R = TypeVar('R') |
34 | | - |
35 | | - |
36 | 16 | @dataclass |
37 | | -class ClientCallInput(Generic[M, P]): |
| 17 | +class ClientCallInput: |
38 | 18 | """Represents the method and its associated input arguments payload.""" |
39 | 19 |
|
40 | | - method: M |
41 | | - value: P |
| 20 | + method: str |
| 21 | + value: Any |
42 | 22 |
|
43 | 23 |
|
44 | 24 | @dataclass |
45 | | -class ClientCallResult(Generic[M, R]): |
| 25 | +class ClientCallResult: |
46 | 26 | """Represents the method and its associated result payload.""" |
47 | 27 |
|
48 | | - method: M |
49 | | - value: R |
| 28 | + method: str |
| 29 | + value: Any |
50 | 30 |
|
51 | 31 |
|
52 | 32 | @dataclass |
53 | | -class BeforeArgs(Generic[M, P, R]): |
| 33 | +class BeforeArgs: |
54 | 34 | """Arguments passed to the interceptor before a method call.""" |
55 | 35 |
|
56 | | - input: ClientCallInput[M, P] |
| 36 | + input: ClientCallInput |
57 | 37 | agent_card: AgentCard |
58 | 38 | context: ClientCallContext | None = None |
59 | | - early_return: ClientCallResult[M, R] | None = None |
| 39 | + early_return: ClientCallResult | None = None |
60 | 40 |
|
61 | 41 |
|
62 | 42 | @dataclass |
63 | | -class AfterArgs(Generic[M, R]): |
| 43 | +class AfterArgs: |
64 | 44 | """Arguments passed to the interceptor after a method call completes.""" |
65 | 45 |
|
66 | | - result: ClientCallResult[M, R] |
| 46 | + result: ClientCallResult |
67 | 47 | agent_card: AgentCard |
68 | 48 | context: ClientCallContext | None = None |
69 | 49 | early_return: bool = False |
70 | 50 |
|
71 | 51 |
|
72 | | -class ClientCallInterceptor(ABC, Generic[M, P, R]): |
| 52 | +class ClientCallInterceptor: |
73 | 53 | """An abstract base class for client-side call interceptors. |
74 | 54 |
|
75 | 55 | Interceptors can inspect and modify requests before they are sent, |
76 | 56 | which is ideal for concerns like authentication, logging, or tracing. |
77 | 57 | """ |
78 | 58 |
|
79 | 59 | @abstractmethod |
80 | | - async def before(self, args: UnionBeforeArgs) -> None: |
| 60 | + async def before(self, args: BeforeArgs) -> None: |
81 | 61 | """Invoked before transport method.""" |
82 | 62 |
|
83 | 63 | @abstractmethod |
84 | | - async def after(self, args: UnionAfterArgs) -> None: |
| 64 | + async def after(self, args: AfterArgs) -> None: |
85 | 65 | """Invoked after transport method.""" |
86 | | - |
87 | | - |
88 | | -UnionBeforeArgs: TypeAlias = ( |
89 | | - BeforeArgs[ |
90 | | - Literal['send_message'], 'SendMessageRequest', 'SendMessageResponse' |
91 | | - ] |
92 | | - | BeforeArgs[ |
93 | | - Literal['send_message_streaming'], |
94 | | - 'SendMessageRequest', |
95 | | - 'StreamResponse', |
96 | | - ] |
97 | | - | BeforeArgs[Literal['get_task'], 'GetTaskRequest', 'Task'] |
98 | | - | BeforeArgs[Literal['list_tasks'], 'ListTasksRequest', 'ListTasksResponse'] |
99 | | - | BeforeArgs[Literal['cancel_task'], 'CancelTaskRequest', 'Task'] |
100 | | - | BeforeArgs[ |
101 | | - Literal['create_task_push_notification_config'], |
102 | | - 'TaskPushNotificationConfig', |
103 | | - 'TaskPushNotificationConfig', |
104 | | - ] |
105 | | - | BeforeArgs[ |
106 | | - Literal['get_task_push_notification_config'], |
107 | | - 'GetTaskPushNotificationConfigRequest', |
108 | | - 'TaskPushNotificationConfig', |
109 | | - ] |
110 | | - | BeforeArgs[ |
111 | | - Literal['list_task_push_notification_configs'], |
112 | | - 'ListTaskPushNotificationConfigsRequest', |
113 | | - 'ListTaskPushNotificationConfigsResponse', |
114 | | - ] |
115 | | - | BeforeArgs[ |
116 | | - Literal['delete_task_push_notification_config'], |
117 | | - 'DeleteTaskPushNotificationConfigRequest', |
118 | | - None, |
119 | | - ] |
120 | | - | BeforeArgs[ |
121 | | - Literal['subscribe'], 'SubscribeToTaskRequest', 'StreamResponse' |
122 | | - ] |
123 | | - | BeforeArgs[ |
124 | | - Literal['get_extended_agent_card'], |
125 | | - 'GetExtendedAgentCardRequest', |
126 | | - 'AgentCard', |
127 | | - ] |
128 | | -) |
129 | | - |
130 | | -UnionAfterArgs: TypeAlias = ( |
131 | | - AfterArgs[Literal['send_message'], 'SendMessageResponse'] |
132 | | - | AfterArgs[Literal['send_message_streaming'], 'StreamResponse'] |
133 | | - | AfterArgs[Literal['get_task'], 'Task'] |
134 | | - | AfterArgs[Literal['list_tasks'], 'ListTasksResponse'] |
135 | | - | AfterArgs[Literal['cancel_task'], 'Task'] |
136 | | - | AfterArgs[ |
137 | | - Literal['create_task_push_notification_config'], |
138 | | - 'TaskPushNotificationConfig', |
139 | | - ] |
140 | | - | AfterArgs[ |
141 | | - Literal['get_task_push_notification_config'], |
142 | | - 'TaskPushNotificationConfig', |
143 | | - ] |
144 | | - | AfterArgs[ |
145 | | - Literal['list_task_push_notification_configs'], |
146 | | - 'ListTaskPushNotificationConfigsResponse', |
147 | | - ] |
148 | | - | AfterArgs[Literal['delete_task_push_notification_config'], None] |
149 | | - | AfterArgs[Literal['subscribe'], 'StreamResponse'] |
150 | | - | AfterArgs[Literal['get_extended_agent_card'], 'AgentCard'] |
151 | | -) |
0 commit comments