forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
182 lines (152 loc) · 6.25 KB
/
context.py
File metadata and controls
182 lines (152 loc) · 6.25 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import uuid
from typing import Any
from a2a.server.context import ServerCallContext
from a2a.types import (
InvalidParamsError,
Message,
MessageSendConfiguration,
MessageSendParams,
Task,
)
from a2a.utils import get_message_text
from a2a.utils.errors import ServerError
class RequestContext:
"""Request Context.
Holds information about the current request being processed by the server,
including the incoming message, task and context identifiers, and related
tasks.
"""
def __init__( # noqa: PLR0913
self,
request: MessageSendParams | None = None,
task_id: str | None = None,
context_id: str | None = None,
task: Task | None = None,
related_tasks: list[Task] | None = None,
call_context: ServerCallContext | None = None,
):
"""Initializes the RequestContext.
Args:
request: The incoming `MessageSendParams` request payload.
task_id: The ID of the task explicitly provided in the request or path.
context_id: The ID of the context explicitly provided in the request or path.
task: The existing `Task` object retrieved from the store, if any.
related_tasks: A list of other tasks related to the current request (e.g., for tool use).
call_context: The server call context associated with this request.
"""
if related_tasks is None:
related_tasks = []
self._params = request
self._task_id = task_id
self._context_id = context_id
self._current_task = task
self._related_tasks = related_tasks
self._call_context = call_context
# If the task id and context id were provided, make sure they
# match the request. Otherwise, create them
if self._params:
if task_id:
self._params.message.task_id = task_id
if task and task.id != task_id:
raise ServerError(InvalidParamsError(message='bad task id'))
else:
self._check_or_generate_task_id()
if context_id:
self._params.message.context_id = context_id
if task and task.context_id != context_id:
raise ServerError(
InvalidParamsError(message='bad context id')
)
else:
self._check_or_generate_context_id()
def get_user_input(self, delimiter: str = '\n') -> str:
"""Extracts text content from the user's message parts.
Args:
delimiter: The string to use when joining multiple text parts.
Returns:
A single string containing all text content from the user message,
joined by the specified delimiter. Returns an empty string if no
user message is present or if it contains no text parts.
"""
if not self._params:
return ''
return get_message_text(self._params.message, delimiter)
def attach_related_task(self, task: Task) -> None:
"""Attaches a related task to the context.
This is useful for scenarios like tool execution where a new task
might be spawned.
Args:
task: The `Task` object to attach.
"""
self._related_tasks.append(task)
@property
def message(self) -> Message | None:
"""The incoming `Message` object from the request, if available."""
return self._params.message if self._params else None
@property
def related_tasks(self) -> list[Task]:
"""A list of tasks related to the current request."""
return self._related_tasks
@property
def current_task(self) -> Task | None:
"""The current `Task` object being processed."""
return self._current_task
@current_task.setter
def current_task(self, task: Task) -> None:
"""Sets the current task object."""
self._current_task = task
@property
def task_id(self) -> str | None:
"""The ID of the task associated with this context."""
return self._task_id
@property
def context_id(self) -> str | None:
"""The ID of the conversation context associated with this task."""
return self._context_id
@property
def configuration(self) -> MessageSendConfiguration | None:
"""The `MessageSendConfiguration` from the request, if available."""
if not self._params:
return None
return self._params.configuration
@property
def call_context(self) -> ServerCallContext | None:
"""The server call context associated with this request."""
return self._call_context
@property
def metadata(self) -> dict[str, Any]:
"""Metadata associated with the request, if available."""
if not self._params:
return {}
return self._params.metadata or {}
def add_activated_extension(self, uri: str) -> None:
"""Add an extension to the set of activated extensions for this request.
This causes the extension to be indicated back to the client in the
response.
"""
if self._call_context:
self._call_context.activated_extensions.add(uri)
@property
def requested_extensions(self) -> set[str]:
"""Extensions that the client requested to activate."""
return (
self._call_context.requested_extensions
if self._call_context
else set()
)
def _check_or_generate_task_id(self) -> None:
"""Ensures a task ID is present, generating one if necessary."""
if not self._params:
return
if not self._task_id and not self._params.message.task_id:
self._params.message.task_id = str(uuid.uuid4())
if self._params.message.task_id:
self._task_id = self._params.message.task_id
def _check_or_generate_context_id(self) -> None:
"""Ensures a context ID is present, generating one if necessary."""
if not self._params:
return
if not self._context_id and not self._params.message.context_id:
self._params.message.context_id = str(uuid.uuid4())
if self._params.message.context_id:
self._context_id = self._params.message.context_id