microsoft-agents-hosting-core 0.4.0.dev18__py3-none-any.whl → 0.5.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- microsoft_agents/hosting/core/_oauth/_flow_state.py +2 -2
- microsoft_agents/hosting/core/_oauth/_oauth_flow.py +26 -23
- microsoft_agents/hosting/core/activity_handler.py +19 -17
- microsoft_agents/hosting/core/app/agent_application.py +37 -13
- microsoft_agents/hosting/core/app/input_file.py +15 -11
- microsoft_agents/hosting/core/app/oauth/_handlers/_authorization_handler.py +4 -4
- microsoft_agents/hosting/core/app/oauth/_handlers/_user_authorization.py +2 -2
- microsoft_agents/hosting/core/app/oauth/_handlers/agentic_user_authorization.py +9 -9
- microsoft_agents/hosting/core/app/oauth/authorization.py +38 -20
- microsoft_agents/hosting/core/app/state/state.py +50 -6
- microsoft_agents/hosting/core/app/typing_indicator.py +51 -27
- microsoft_agents/hosting/core/authorization/access_token_provider_base.py +1 -1
- microsoft_agents/hosting/core/authorization/anonymous_token_provider.py +1 -1
- microsoft_agents/hosting/core/authorization/jwt_token_validator.py +6 -4
- microsoft_agents/hosting/core/channel_adapter.py +9 -9
- microsoft_agents/hosting/core/channel_service_adapter.py +64 -6
- microsoft_agents/hosting/core/connector/client/connector_client.py +1 -1
- microsoft_agents/hosting/core/connector/client/user_token_client.py +40 -43
- microsoft_agents/hosting/core/connector/user_token_base.py +77 -1
- microsoft_agents/hosting/core/connector/user_token_client_base.py +3 -0
- microsoft_agents/hosting/core/state/agent_state.py +16 -20
- microsoft_agents/hosting/core/storage/transcript_file_store.py +1 -5
- microsoft_agents/hosting/core/turn_context.py +2 -1
- microsoft_agents_hosting_core-0.5.0.dist-info/METADATA +191 -0
- {microsoft_agents_hosting_core-0.4.0.dev18.dist-info → microsoft_agents_hosting_core-0.5.0.dist-info}/RECORD +28 -27
- microsoft_agents_hosting_core-0.5.0.dist-info/licenses/LICENSE +21 -0
- microsoft_agents_hosting_core-0.4.0.dev18.dist-info/METADATA +0 -16
- {microsoft_agents_hosting_core-0.4.0.dev18.dist-info → microsoft_agents_hosting_core-0.5.0.dist-info}/WHEEL +0 -0
- {microsoft_agents_hosting_core-0.4.0.dev18.dist-info → microsoft_agents_hosting_core-0.5.0.dist-info}/top_level.txt +0 -0
|
@@ -55,11 +55,11 @@ class Authorization:
|
|
|
55
55
|
only if auth_handlers is empty or None.
|
|
56
56
|
|
|
57
57
|
:param storage: The storage system to use for state management.
|
|
58
|
-
:type storage: Storage
|
|
58
|
+
:type storage: :class:`microsoft_agents.hosting.core.storage.Storage`
|
|
59
59
|
:param connection_manager: The connection manager for OAuth providers.
|
|
60
|
-
:type connection_manager: Connections
|
|
60
|
+
:type connection_manager: :class:`microsoft_agents.hosting.core.authorization.Connections`
|
|
61
61
|
:param auth_handlers: Configuration for OAuth providers.
|
|
62
|
-
:type auth_handlers: dict[str, AuthHandler],
|
|
62
|
+
:type auth_handlers: dict[str, :class:`microsoft_agents.hosting.core.app.oauth.auth_handler.AuthHandler`], Optional
|
|
63
63
|
:raises ValueError: When storage is None or no auth handlers provided.
|
|
64
64
|
"""
|
|
65
65
|
if not storage:
|
|
@@ -105,7 +105,7 @@ class Authorization:
|
|
|
105
105
|
it initializes an instance of each variant that is referenced.
|
|
106
106
|
|
|
107
107
|
:param auth_handlers: A dictionary of auth handler configurations.
|
|
108
|
-
:type auth_handlers: dict[str, AuthHandler]
|
|
108
|
+
:type auth_handlers: dict[str, :class:`microsoft_agents.hosting.core.app.oauth.auth_handler.AuthHandler`]
|
|
109
109
|
"""
|
|
110
110
|
for name, auth_handler in self._handler_settings.items():
|
|
111
111
|
auth_type = auth_handler.auth_type
|
|
@@ -126,26 +126,42 @@ class Authorization:
|
|
|
126
126
|
can be used to inspect or manipulate the state directly if needed.
|
|
127
127
|
|
|
128
128
|
:param context: The turn context for the current turn of conversation.
|
|
129
|
-
:type context: TurnContext
|
|
129
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
130
130
|
:return: A unique (across other values of channel_id and user_id) key for the sign-in state.
|
|
131
131
|
:rtype: str
|
|
132
132
|
"""
|
|
133
133
|
return f"auth:_SignInState:{context.activity.channel_id}:{context.activity.from_property.id}"
|
|
134
134
|
|
|
135
135
|
async def _load_sign_in_state(self, context: TurnContext) -> Optional[_SignInState]:
|
|
136
|
-
"""Load the sign-in state from storage for the given context.
|
|
136
|
+
"""Load the sign-in state from storage for the given context.
|
|
137
|
+
|
|
138
|
+
:param context: The turn context for the current turn of conversation.
|
|
139
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
140
|
+
:return: The sign-in state if found, None otherwise.
|
|
141
|
+
:rtype: Optional[:class:`microsoft_agents.hosting.core.app.oauth._sign_in_state._SignInState`]
|
|
142
|
+
"""
|
|
137
143
|
key = self._sign_in_state_key(context)
|
|
138
144
|
return (await self._storage.read([key], target_cls=_SignInState)).get(key)
|
|
139
145
|
|
|
140
146
|
async def _save_sign_in_state(
|
|
141
147
|
self, context: TurnContext, state: _SignInState
|
|
142
148
|
) -> None:
|
|
143
|
-
"""Save the sign-in state to storage for the given context.
|
|
149
|
+
"""Save the sign-in state to storage for the given context.
|
|
150
|
+
|
|
151
|
+
:param context: The turn context for the current turn of conversation.
|
|
152
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
153
|
+
:param state: The sign-in state to save.
|
|
154
|
+
:type state: :class:`microsoft_agents.hosting.core.app.oauth._sign_in_state._SignInState`
|
|
155
|
+
"""
|
|
144
156
|
key = self._sign_in_state_key(context)
|
|
145
157
|
await self._storage.write({key: state})
|
|
146
158
|
|
|
147
159
|
async def _delete_sign_in_state(self, context: TurnContext) -> None:
|
|
148
|
-
"""Delete the sign-in state from storage for the given context.
|
|
160
|
+
"""Delete the sign-in state from storage for the given context.
|
|
161
|
+
|
|
162
|
+
:param context: The turn context for the current turn of conversation.
|
|
163
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
164
|
+
"""
|
|
149
165
|
key = self._sign_in_state_key(context)
|
|
150
166
|
await self._storage.delete([key])
|
|
151
167
|
|
|
@@ -179,7 +195,7 @@ class Authorization:
|
|
|
179
195
|
:param handler_id: The ID of the auth handler to resolve.
|
|
180
196
|
:type handler_id: str
|
|
181
197
|
:return: The corresponding AuthorizationHandler instance.
|
|
182
|
-
:rtype:
|
|
198
|
+
:rtype: :class:`microsoft_agents.hosting.core.app.oauth._handlers._AuthorizationHandler`
|
|
183
199
|
:raises ValueError: If the handler ID is not recognized or not configured.
|
|
184
200
|
"""
|
|
185
201
|
if handler_id not in self._handlers:
|
|
@@ -200,13 +216,13 @@ class Authorization:
|
|
|
200
216
|
Storage is updated as needed with _SignInState data for caching purposes.
|
|
201
217
|
|
|
202
218
|
:param context: The turn context for the current turn of conversation.
|
|
203
|
-
:type context: TurnContext
|
|
219
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
204
220
|
:param state: The turn state for the current turn of conversation.
|
|
205
|
-
:type state: TurnState
|
|
221
|
+
:type state: :class:`microsoft_agents.hosting.core.app.state.turn_state.TurnState`
|
|
206
222
|
:param auth_handler_id: The ID of the auth handler to use for sign-in. If None, the first handler will be used.
|
|
207
223
|
:type auth_handler_id: str
|
|
208
224
|
:return: A _SignInResponse indicating the result of the sign-in attempt.
|
|
209
|
-
:rtype: _SignInResponse
|
|
225
|
+
:rtype: :class:`microsoft_agents.hosting.core.app.oauth._sign_in_response._SignInResponse`
|
|
210
226
|
"""
|
|
211
227
|
|
|
212
228
|
auth_handler_id = auth_handler_id or self._default_handler_id
|
|
@@ -250,7 +266,7 @@ class Authorization:
|
|
|
250
266
|
"""Attempts to sign out the user from a specified auth handler or the default handler.
|
|
251
267
|
|
|
252
268
|
:param context: The turn context for the current turn of conversation.
|
|
253
|
-
:type context: TurnContext
|
|
269
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
254
270
|
:param auth_handler_id: The ID of the auth handler to sign out from. If None, sign out from all handlers.
|
|
255
271
|
:type auth_handler_id: Optional[str]
|
|
256
272
|
:return: None
|
|
@@ -272,11 +288,11 @@ class Authorization:
|
|
|
272
288
|
from the cached _SignInState.
|
|
273
289
|
|
|
274
290
|
:param context: The context object for the current turn.
|
|
275
|
-
:type context: TurnContext
|
|
291
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
276
292
|
:param state: The turn state for the current turn.
|
|
277
|
-
:type state: TurnState
|
|
293
|
+
:type state: :class:`microsoft_agents.hosting.core.app.state.turn_state.TurnState`
|
|
278
294
|
:return: A tuple indicating whether the turn should be skipped and the continuation activity if applicable.
|
|
279
|
-
:rtype: tuple[bool, Optional[Activity]]
|
|
295
|
+
:rtype: tuple[bool, Optional[:class:`microsoft_agents.activity.Activity`]]
|
|
280
296
|
"""
|
|
281
297
|
sign_in_state = await self._load_sign_in_state(context)
|
|
282
298
|
|
|
@@ -306,11 +322,11 @@ class Authorization:
|
|
|
306
322
|
The token is taken from cache, so this does not initiate nor continue a sign-in flow.
|
|
307
323
|
|
|
308
324
|
:param context: The context object for the current turn.
|
|
309
|
-
:type context: TurnContext
|
|
325
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
310
326
|
:param auth_handler_id: The ID of the auth handler to get the token for.
|
|
311
327
|
:type auth_handler_id: str
|
|
312
328
|
:return: The token response from the OAuth provider.
|
|
313
|
-
:rtype: TokenResponse
|
|
329
|
+
:rtype: :class:`microsoft_agents.activity.TokenResponse`
|
|
314
330
|
"""
|
|
315
331
|
return await self.exchange_token(context, auth_handler_id=auth_handler_id)
|
|
316
332
|
|
|
@@ -324,7 +340,7 @@ class Authorization:
|
|
|
324
340
|
"""Exchanges or refreshes the token for a specific auth handler or the default handler.
|
|
325
341
|
|
|
326
342
|
:param context: The context object for the current turn.
|
|
327
|
-
:type context: TurnContext
|
|
343
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
328
344
|
:param scopes: The scopes to request during the token exchange or refresh. Defaults
|
|
329
345
|
to the list given in the AuthHandler configuration if None.
|
|
330
346
|
:type scopes: Optional[list[str]]
|
|
@@ -335,7 +351,7 @@ class Authorization:
|
|
|
335
351
|
the connection defined in the AuthHandler configuration will be used.
|
|
336
352
|
:type exchange_connection: Optional[str]
|
|
337
353
|
:return: The token response from the OAuth provider.
|
|
338
|
-
:rtype: TokenResponse
|
|
354
|
+
:rtype: :class:`microsoft_agents.activity.TokenResponse`
|
|
339
355
|
:raises ValueError: If the specified auth handler ID is not recognized or not configured.
|
|
340
356
|
"""
|
|
341
357
|
|
|
@@ -376,6 +392,7 @@ class Authorization:
|
|
|
376
392
|
Sets a handler to be called when sign-in is successfully completed.
|
|
377
393
|
|
|
378
394
|
:param handler: The handler function to call on successful sign-in.
|
|
395
|
+
:type handler: Callable[[:class:`microsoft_agents.hosting.core.turn_context.TurnContext`, :class:`microsoft_agents.hosting.core.app.state.turn_state.TurnState`, Optional[str]], Awaitable[None]]
|
|
379
396
|
"""
|
|
380
397
|
self._sign_in_success_handler = handler
|
|
381
398
|
|
|
@@ -387,5 +404,6 @@ class Authorization:
|
|
|
387
404
|
Sets a handler to be called when sign-in fails.
|
|
388
405
|
|
|
389
406
|
:param handler: The handler function to call on sign-in failure.
|
|
407
|
+
:type handler: Callable[[:class:`microsoft_agents.hosting.core.turn_context.TurnContext`, :class:`microsoft_agents.hosting.core.app.state.turn_state.TurnState`, Optional[str]], Awaitable[None]]
|
|
390
408
|
"""
|
|
391
409
|
self._sign_in_failure_handler = handler
|
|
@@ -97,9 +97,10 @@ class State(dict[str, StoreItem], ABC):
|
|
|
97
97
|
"""
|
|
98
98
|
Saves The State to Storage
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
:param _context: the turn context.
|
|
101
|
+
:type _context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
102
|
+
:param storage: storage to save to.
|
|
103
|
+
:type storage: Optional[:class:`microsoft_agents.hosting.core.storage.Storage`]
|
|
103
104
|
"""
|
|
104
105
|
|
|
105
106
|
if not storage or self.__key__ == "":
|
|
@@ -126,13 +127,24 @@ class State(dict[str, StoreItem], ABC):
|
|
|
126
127
|
"""
|
|
127
128
|
Loads The State from Storage
|
|
128
129
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
130
|
+
:param context: the turn context.
|
|
131
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
132
|
+
:param storage: storage to read from.
|
|
133
|
+
:type storage: Optional[:class:`microsoft_agents.hosting.core.storage.Storage`]
|
|
134
|
+
:return: The loaded state instance.
|
|
135
|
+
:rtype: :class:`microsoft_agents.hosting.core.app.state.state.State`
|
|
132
136
|
"""
|
|
133
137
|
return cls()
|
|
134
138
|
|
|
135
139
|
def create_property(self, name: str) -> _StatePropertyAccessor:
|
|
140
|
+
"""
|
|
141
|
+
Create a property accessor for the given name.
|
|
142
|
+
|
|
143
|
+
:param name: The name of the property.
|
|
144
|
+
:type name: str
|
|
145
|
+
:return: A state property accessor for the named property.
|
|
146
|
+
:rtype: :class:`microsoft_agents.hosting.core.state.state_property_accessor.StatePropertyAccessor`
|
|
147
|
+
"""
|
|
136
148
|
return StatePropertyAccessor(self, name)
|
|
137
149
|
|
|
138
150
|
def __setitem__(self, key: str, item: Any) -> None:
|
|
@@ -180,6 +192,14 @@ class StatePropertyAccessor(_StatePropertyAccessor):
|
|
|
180
192
|
_state: State
|
|
181
193
|
|
|
182
194
|
def __init__(self, state: State, name: str) -> None:
|
|
195
|
+
"""
|
|
196
|
+
Initialize the StatePropertyAccessor.
|
|
197
|
+
|
|
198
|
+
:param state: The state object to access properties from.
|
|
199
|
+
:type state: :class:`microsoft_agents.hosting.core.app.state.state.State`
|
|
200
|
+
:param name: The name of the property to access.
|
|
201
|
+
:type name: str
|
|
202
|
+
"""
|
|
183
203
|
self._name = name
|
|
184
204
|
self._state = state
|
|
185
205
|
|
|
@@ -190,6 +210,16 @@ class StatePropertyAccessor(_StatePropertyAccessor):
|
|
|
190
210
|
Union[Any, Callable[[], Optional[Any]]]
|
|
191
211
|
] = None,
|
|
192
212
|
) -> Optional[Any]:
|
|
213
|
+
"""
|
|
214
|
+
Get the property value from the state.
|
|
215
|
+
|
|
216
|
+
:param turn_context: The turn context.
|
|
217
|
+
:type turn_context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
218
|
+
:param default_value_or_factory: Default value or factory function to use if property doesn't exist.
|
|
219
|
+
:type default_value_or_factory: Optional[Union[Any, Callable[[], Optional[Any]]]]
|
|
220
|
+
:return: The property value or default value if not found.
|
|
221
|
+
:rtype: Optional[Any]
|
|
222
|
+
"""
|
|
193
223
|
value = self._state[self._name] if self._name in self._state else None
|
|
194
224
|
|
|
195
225
|
if value is None and default_value_or_factory is not None:
|
|
@@ -201,7 +231,21 @@ class StatePropertyAccessor(_StatePropertyAccessor):
|
|
|
201
231
|
return value
|
|
202
232
|
|
|
203
233
|
async def delete(self, turn_context: TurnContext) -> None:
|
|
234
|
+
"""
|
|
235
|
+
Delete the property from the state.
|
|
236
|
+
|
|
237
|
+
:param turn_context: The turn context.
|
|
238
|
+
:type turn_context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
239
|
+
"""
|
|
204
240
|
del self._state[self._name]
|
|
205
241
|
|
|
206
242
|
async def set(self, turn_context: TurnContext, value: Any) -> None:
|
|
243
|
+
"""
|
|
244
|
+
Set the property value in the state.
|
|
245
|
+
|
|
246
|
+
:param turn_context: The turn context.
|
|
247
|
+
:type turn_context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
248
|
+
:param value: The value to set for the property.
|
|
249
|
+
:type value: Any
|
|
250
|
+
"""
|
|
207
251
|
self._state[self._name] = value
|
|
@@ -4,9 +4,9 @@ Licensed under the MIT License.
|
|
|
4
4
|
"""
|
|
5
5
|
|
|
6
6
|
from __future__ import annotations
|
|
7
|
+
import asyncio
|
|
7
8
|
import logging
|
|
8
9
|
|
|
9
|
-
from threading import Timer
|
|
10
10
|
from typing import Optional
|
|
11
11
|
|
|
12
12
|
from microsoft_agents.hosting.core import TurnContext
|
|
@@ -20,36 +20,60 @@ class TypingIndicator:
|
|
|
20
20
|
Encapsulates the logic for sending "typing" activity to the user.
|
|
21
21
|
"""
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
self.
|
|
23
|
+
def __init__(self, intervalSeconds=1) -> None:
|
|
24
|
+
self._intervalSeconds = intervalSeconds
|
|
25
|
+
self._task: Optional[asyncio.Task] = None
|
|
26
|
+
self._running: bool = False
|
|
27
|
+
self._lock = asyncio.Lock()
|
|
28
28
|
|
|
29
29
|
async def start(self, context: TurnContext) -> None:
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
async with self._lock:
|
|
31
|
+
if self._running:
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
logger.debug(
|
|
35
|
+
f"Starting typing indicator with interval: {self._intervalSeconds} seconds"
|
|
36
|
+
)
|
|
37
|
+
self._running = True
|
|
38
|
+
self._task = asyncio.create_task(self._typing_loop(context))
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
await func()
|
|
40
|
+
async def stop(self) -> None:
|
|
41
|
+
async with self._lock:
|
|
42
|
+
if not self._running:
|
|
43
|
+
return
|
|
38
44
|
|
|
39
|
-
def stop(self) -> None:
|
|
40
|
-
if self._timer:
|
|
41
45
|
logger.debug("Stopping typing indicator")
|
|
42
|
-
self.
|
|
43
|
-
|
|
46
|
+
self._running = False
|
|
47
|
+
task = self._task
|
|
48
|
+
self._task = None
|
|
44
49
|
|
|
45
|
-
|
|
46
|
-
|
|
50
|
+
# Cancel outside the lock to avoid blocking
|
|
51
|
+
if task and not task.done():
|
|
52
|
+
task.cancel()
|
|
47
53
|
try:
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
await task
|
|
55
|
+
except asyncio.CancelledError:
|
|
56
|
+
pass
|
|
57
|
+
|
|
58
|
+
async def _typing_loop(self, context: TurnContext):
|
|
59
|
+
"""Continuously send typing indicators at the specified interval."""
|
|
60
|
+
try:
|
|
61
|
+
while True:
|
|
62
|
+
# Check running status under lock
|
|
63
|
+
async with self._lock:
|
|
64
|
+
if not self._running:
|
|
65
|
+
break
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
logger.debug("Sending typing activity")
|
|
69
|
+
await context.send_activity(Activity(type=ActivityTypes.typing))
|
|
70
|
+
except Exception as e:
|
|
71
|
+
logger.error(f"Error sending typing activity: {e}")
|
|
72
|
+
async with self._lock:
|
|
73
|
+
self._running = False
|
|
74
|
+
break
|
|
75
|
+
|
|
76
|
+
await asyncio.sleep(self._intervalSeconds)
|
|
77
|
+
except asyncio.CancelledError:
|
|
78
|
+
logger.debug("Typing indicator loop cancelled")
|
|
79
|
+
raise
|
|
@@ -43,6 +43,6 @@ class AccessTokenProviderBase(Protocol):
|
|
|
43
43
|
raise NotImplementedError()
|
|
44
44
|
|
|
45
45
|
async def get_agentic_user_token(
|
|
46
|
-
self, agent_app_instance_id: str,
|
|
46
|
+
self, agent_app_instance_id: str, agentic_user_id: str, scopes: list[str]
|
|
47
47
|
) -> Optional[str]:
|
|
48
48
|
raise NotImplementedError()
|
|
@@ -33,6 +33,6 @@ class AnonymousTokenProvider(AccessTokenProviderBase):
|
|
|
33
33
|
return "", ""
|
|
34
34
|
|
|
35
35
|
async def get_agentic_user_token(
|
|
36
|
-
self, agent_app_instance_id: str,
|
|
36
|
+
self, agent_app_instance_id: str, agentic_user_id: str, scopes: list[str]
|
|
37
37
|
) -> Optional[str]:
|
|
38
38
|
return ""
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
2
2
|
# Licensed under the MIT License.
|
|
3
3
|
|
|
4
|
+
import asyncio
|
|
4
5
|
import logging
|
|
5
6
|
import jwt
|
|
6
7
|
|
|
@@ -16,10 +17,10 @@ class JwtTokenValidator:
|
|
|
16
17
|
def __init__(self, configuration: AgentAuthConfiguration):
|
|
17
18
|
self.configuration = configuration
|
|
18
19
|
|
|
19
|
-
def validate_token(self, token: str) -> ClaimsIdentity:
|
|
20
|
+
async def validate_token(self, token: str) -> ClaimsIdentity:
|
|
20
21
|
|
|
21
22
|
logger.debug("Validating JWT token.")
|
|
22
|
-
key = self._get_public_key_or_secret(token)
|
|
23
|
+
key = await self._get_public_key_or_secret(token)
|
|
23
24
|
decoded_token = jwt.decode(
|
|
24
25
|
token,
|
|
25
26
|
key=key,
|
|
@@ -39,7 +40,7 @@ class JwtTokenValidator:
|
|
|
39
40
|
logger.debug("Returning anonymous claims identity.")
|
|
40
41
|
return ClaimsIdentity({}, False, authentication_type="Anonymous")
|
|
41
42
|
|
|
42
|
-
def _get_public_key_or_secret(self, token: str) -> PyJWK:
|
|
43
|
+
async def _get_public_key_or_secret(self, token: str) -> PyJWK:
|
|
43
44
|
header = get_unverified_header(token)
|
|
44
45
|
unverified_payload: dict = decode(token, options={"verify_signature": False})
|
|
45
46
|
|
|
@@ -50,5 +51,6 @@ class JwtTokenValidator:
|
|
|
50
51
|
)
|
|
51
52
|
jwks_client = PyJWKClient(jwksUri)
|
|
52
53
|
|
|
53
|
-
key = jwks_client.get_signing_key
|
|
54
|
+
key = await asyncio.to_thread(jwks_client.get_signing_key, header["kid"])
|
|
55
|
+
|
|
54
56
|
return key
|
|
@@ -40,7 +40,7 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
40
40
|
Sends a set of activities to the user. An array of responses from the server will be returned.
|
|
41
41
|
|
|
42
42
|
:param context: The context object for the turn.
|
|
43
|
-
:type context: :class:`TurnContext`
|
|
43
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
44
44
|
:param activities: The activities to send.
|
|
45
45
|
:type activities: list[Activity]
|
|
46
46
|
:return:
|
|
@@ -53,7 +53,7 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
53
53
|
Replaces an existing activity.
|
|
54
54
|
|
|
55
55
|
:param context: The context object for the turn.
|
|
56
|
-
:type context: :class:`TurnContext`
|
|
56
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
57
57
|
:param activity: New replacement activity.
|
|
58
58
|
:type activity: :class:`microsoft_agents.activity.Activity`
|
|
59
59
|
:return:
|
|
@@ -68,7 +68,7 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
68
68
|
Deletes an existing activity.
|
|
69
69
|
|
|
70
70
|
:param context: The context object for the turn.
|
|
71
|
-
:type context: :class:`TurnContext`
|
|
71
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
72
72
|
:param reference: Conversation reference for the activity to delete.
|
|
73
73
|
:type reference: :class:`microsoft_agents.activity.ConversationReference`
|
|
74
74
|
:return:
|
|
@@ -102,7 +102,7 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
102
102
|
:param reference: A reference to the conversation to continue.
|
|
103
103
|
:type reference: :class:`microsoft_agents.activity.ConversationReference`
|
|
104
104
|
:param callback: The method to call for the resulting agent turn.
|
|
105
|
-
:type callback: Callable[[TurnContext], Awaitable]
|
|
105
|
+
:type callback: Callable[[microsoft_agents.hosting.core.turn_context.TurnContext], Awaitable]
|
|
106
106
|
:param claims_identity: A :class:`microsoft_agents.hosting.core.ClaimsIdentity` for the conversation.
|
|
107
107
|
:type claims_identity: :class:`microsoft_agents.hosting.core.ClaimsIdentity`
|
|
108
108
|
:param audience:A value signifying the recipient of the proactive message.
|
|
@@ -124,11 +124,11 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
124
124
|
to the user.
|
|
125
125
|
|
|
126
126
|
:param claims_identity: A :class:`microsoft_agents.hosting.core.ClaimsIdentity` for the conversation.
|
|
127
|
-
:type claims_identity: :class:`microsoft_agents.hosting.core.ClaimsIdentity`
|
|
127
|
+
:type claims_identity: :class:`microsoft_agents.hosting.core.authorization.ClaimsIdentity`
|
|
128
128
|
:param continuation_activity: The activity to send.
|
|
129
129
|
:type continuation_activity: :class:`microsoft_agents.activity.Activity`
|
|
130
130
|
:param callback: The method to call for the resulting agent turn.
|
|
131
|
-
:type callback: Callable[[TurnContext], Awaitable]
|
|
131
|
+
:type callback: Callable[[microsoft_agents.hosting.core.turn_context.TurnContext], Awaitable]
|
|
132
132
|
:param audience: A value signifying the recipient of the proactive message.
|
|
133
133
|
:type audience: str
|
|
134
134
|
"""
|
|
@@ -155,9 +155,9 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
155
155
|
:param audience: A value signifying the recipient of the proactive message.
|
|
156
156
|
:type audience: str
|
|
157
157
|
:param conversation_parameters: The information to use to create the conversation
|
|
158
|
-
:type conversation_parameters: :class:`microsoft_agents.activity.
|
|
158
|
+
:type conversation_parameters: :class:`microsoft_agents.activity.ConversationParameters`
|
|
159
159
|
:param callback: The method to call for the resulting agent turn.
|
|
160
|
-
:type callback: Callable[[TurnContext], Awaitable]
|
|
160
|
+
:type callback: Callable[[microsoft_agents.hosting.core.turn_context.TurnContext], Awaitable]
|
|
161
161
|
|
|
162
162
|
:raises: Exception - Not implemented or when the implementation fails.
|
|
163
163
|
|
|
@@ -222,7 +222,7 @@ class ChannelAdapter(ABC, ChannelAdapterProtocol):
|
|
|
222
222
|
the end of the chain.
|
|
223
223
|
|
|
224
224
|
:param context: The context object for the turn.
|
|
225
|
-
:type context: :class:`TurnContext`
|
|
225
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
226
226
|
:param callback: A callback method to run at the end of the pipeline.
|
|
227
227
|
:type callback: Callable[[TurnContext], Awaitable]
|
|
228
228
|
:return:
|
|
@@ -44,12 +44,29 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
44
44
|
_AGENT_CONNECTOR_CLIENT_KEY = "ConnectorClient"
|
|
45
45
|
|
|
46
46
|
def __init__(self, channel_service_client_factory: ChannelServiceClientFactoryBase):
|
|
47
|
+
"""
|
|
48
|
+
Initialize the ChannelServiceAdapter.
|
|
49
|
+
|
|
50
|
+
:param channel_service_client_factory: The factory for creating channel service clients.
|
|
51
|
+
:type channel_service_client_factory: :class:`microsoft_agents.hosting.core.channel_service_client_factory_base.ChannelServiceClientFactoryBase`
|
|
52
|
+
"""
|
|
47
53
|
super().__init__()
|
|
48
54
|
self._channel_service_client_factory = channel_service_client_factory
|
|
49
55
|
|
|
50
56
|
async def send_activities(
|
|
51
57
|
self, context: TurnContext, activities: list[Activity]
|
|
52
58
|
) -> list[ResourceResponse]:
|
|
59
|
+
"""
|
|
60
|
+
Send a list of activities to the conversation.
|
|
61
|
+
|
|
62
|
+
:param context: The turn context for this conversation turn.
|
|
63
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
64
|
+
:param activities: The list of activities to send.
|
|
65
|
+
:type activities: list[:class:`microsoft_agents.activity.Activity`]
|
|
66
|
+
:return: List of resource responses for the sent activities.
|
|
67
|
+
:rtype: list[:class:`microsoft_agents.activity.ResourceResponse`]
|
|
68
|
+
:raises TypeError: If context or activities are None/invalid.
|
|
69
|
+
"""
|
|
53
70
|
if not context:
|
|
54
71
|
raise TypeError("Expected TurnContext but got None instead")
|
|
55
72
|
|
|
@@ -102,6 +119,17 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
102
119
|
return responses
|
|
103
120
|
|
|
104
121
|
async def update_activity(self, context: TurnContext, activity: Activity):
|
|
122
|
+
"""
|
|
123
|
+
Update an existing activity in the conversation.
|
|
124
|
+
|
|
125
|
+
:param context: The turn context for this conversation turn.
|
|
126
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
127
|
+
:param activity: The activity to update.
|
|
128
|
+
:type activity: :class:`microsoft_agents.activity.Activity`
|
|
129
|
+
:return: Resource response for the updated activity.
|
|
130
|
+
:rtype: :class:`microsoft_agents.activity.ResourceResponse`
|
|
131
|
+
:raises TypeError: If context or activity are None/invalid.
|
|
132
|
+
"""
|
|
105
133
|
if not context:
|
|
106
134
|
raise TypeError("Expected TurnContext but got None instead")
|
|
107
135
|
|
|
@@ -122,6 +150,15 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
122
150
|
async def delete_activity(
|
|
123
151
|
self, context: TurnContext, reference: ConversationReference
|
|
124
152
|
):
|
|
153
|
+
"""
|
|
154
|
+
Delete an activity from the conversation.
|
|
155
|
+
|
|
156
|
+
:param context: The turn context for this conversation turn.
|
|
157
|
+
:type context: :class:`microsoft_agents.hosting.core.turn_context.TurnContext`
|
|
158
|
+
:param reference: Reference to the conversation and activity to delete.
|
|
159
|
+
:type reference: :class:`microsoft_agents.activity.ConversationReference`
|
|
160
|
+
:raises TypeError: If context or reference are None/invalid.
|
|
161
|
+
"""
|
|
125
162
|
if not context:
|
|
126
163
|
raise TypeError("Expected TurnContext but got None instead")
|
|
127
164
|
|
|
@@ -155,9 +192,9 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
155
192
|
and is generally found in the `MicrosoftAppId` parameter in `config.py`.
|
|
156
193
|
:type agent_app_id: str
|
|
157
194
|
:param continuation_activity: The activity to continue the conversation with.
|
|
158
|
-
:type continuation_activity: Activity
|
|
195
|
+
:type continuation_activity: :class:`microsoft_agents.activity.Activity`
|
|
159
196
|
:param callback: The method to call for the resulting agent turn.
|
|
160
|
-
:type callback: Callable[[TurnContext], Awaitable]
|
|
197
|
+
:type callback: Callable[[:class:`microsoft_agents.hosting.core.turn_context.TurnContext`], Awaitable]
|
|
161
198
|
"""
|
|
162
199
|
if not callable:
|
|
163
200
|
raise TypeError(
|
|
@@ -182,6 +219,18 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
182
219
|
callback: Callable[[TurnContext], Awaitable],
|
|
183
220
|
audience: str = None,
|
|
184
221
|
):
|
|
222
|
+
"""
|
|
223
|
+
Continue a conversation with the provided claims identity.
|
|
224
|
+
|
|
225
|
+
:param claims_identity: The claims identity for the conversation.
|
|
226
|
+
:type claims_identity: :class:`microsoft_agents.hosting.core.authorization.ClaimsIdentity`
|
|
227
|
+
:param continuation_activity: The activity to continue the conversation with.
|
|
228
|
+
:type continuation_activity: :class:`microsoft_agents.activity.Activity`
|
|
229
|
+
:param callback: The method to call for the resulting agent turn.
|
|
230
|
+
:type callback: Callable[[:class:`microsoft_agents.hosting.core.turn_context.TurnContext`], Awaitable]
|
|
231
|
+
:param audience: The audience for the conversation.
|
|
232
|
+
:type audience: Optional[str]
|
|
233
|
+
"""
|
|
185
234
|
return await self.process_proactive(
|
|
186
235
|
claims_identity, continuation_activity, audience, callback
|
|
187
236
|
)
|
|
@@ -299,14 +348,15 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
299
348
|
"""
|
|
300
349
|
Creates a turn context and runs the middleware pipeline for an incoming activity.
|
|
301
350
|
|
|
302
|
-
:param
|
|
303
|
-
:type
|
|
351
|
+
:param claims_identity: The claims identity of the agent.
|
|
352
|
+
:type claims_identity: :class:`microsoft_agents.hosting.core.authorization.ClaimsIdentity`
|
|
304
353
|
:param activity: The incoming activity
|
|
305
|
-
:type activity: Activity
|
|
354
|
+
:type activity: :class:`microsoft_agents.activity.Activity`
|
|
306
355
|
:param callback: The callback to execute at the end of the adapter's middleware pipeline.
|
|
307
|
-
:type callback: Callable[[TurnContext], Awaitable]
|
|
356
|
+
:type callback: Callable[[:class:`microsoft_agents.hosting.core.turn_context.TurnContext`], Awaitable]
|
|
308
357
|
|
|
309
358
|
:return: A task that represents the work queued to execute.
|
|
359
|
+
:rtype: Optional[:class:`microsoft_agents.activity.InvokeResponse`]
|
|
310
360
|
|
|
311
361
|
.. remarks::
|
|
312
362
|
This class processes an activity received by the agents web server. This includes any messages
|
|
@@ -372,6 +422,14 @@ class ChannelServiceAdapter(ChannelAdapter, ABC):
|
|
|
372
422
|
return self._process_turn_results(context)
|
|
373
423
|
|
|
374
424
|
def create_claims_identity(self, agent_app_id: str = "") -> ClaimsIdentity:
|
|
425
|
+
"""
|
|
426
|
+
Create a claims identity for the given agent app ID.
|
|
427
|
+
|
|
428
|
+
:param agent_app_id: The agent application ID.
|
|
429
|
+
:type agent_app_id: str
|
|
430
|
+
:return: A claims identity for the agent.
|
|
431
|
+
:rtype: :class:`microsoft_agents.hosting.core.authorization.ClaimsIdentity`
|
|
432
|
+
"""
|
|
375
433
|
return ClaimsIdentity(
|
|
376
434
|
{
|
|
377
435
|
AuthenticationConstants.AUDIENCE_CLAIM: agent_app_id,
|
|
@@ -124,7 +124,7 @@ class ConversationsOperations(ConversationsBase):
|
|
|
124
124
|
|
|
125
125
|
def __init__(self, client: ClientSession, **kwargs):
|
|
126
126
|
self.client = client
|
|
127
|
-
self._max_conversation_id_length = kwargs.get("max_conversation_id_length",
|
|
127
|
+
self._max_conversation_id_length = kwargs.get("max_conversation_id_length", 150)
|
|
128
128
|
|
|
129
129
|
def _normalize_conversation_id(self, conversation_id: str) -> str:
|
|
130
130
|
return conversation_id[: self._max_conversation_id_length]
|