forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
457 lines (390 loc) · 16.6 KB
/
Copy pathmain.py
File metadata and controls
457 lines (390 loc) · 16.6 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Copyright (c) Microsoft. All rights reserved.
"""Run a Telegram bot as a Foundry Hosted Agent using Invocations 2.0.
API Management authenticates Telegram, stamps ``channel="telegram"`` and a
trusted ingress secret, and supplies the Telegram chat id as the Foundry
``agent_session_id``. The hosted agent keeps conversation history in Cosmos DB
and sends streamed responses back through the Telegram Bot API.
"""
from __future__ import annotations
import asyncio
import base64
import hmac
import logging
import os
import time
from collections.abc import AsyncIterator, Awaitable, Callable, Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import httpx
from agent_framework import Agent, AgentResponse, AgentResponseUpdate, AgentRunInputs, Message, ResponseStream
from agent_framework.foundry import FoundryChatClient
from agent_framework.observability import enable_instrumentation
from agent_framework_azure_cosmos import CosmosHistoryProvider
from agent_framework_hosting_telegram import (
TelegramOperation,
telegram_callback_query_id,
telegram_chat_id,
telegram_command,
telegram_from_streaming_run,
telegram_media_file_id,
telegram_to_run,
)
from azure.ai.agentserver.core import get_request_context
from azure.ai.agentserver.invocations import InvocationAgentServerHost
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient
from dotenv import load_dotenv
from starlette.requests import Request
from starlette.responses import Response
load_dotenv()
LOGGER = logging.getLogger(__name__)
EDIT_INTERVAL_SECONDS = 0.4
MAX_MEDIA_BYTES = 1024 * 1024
PLACEHOLDER_TEXT = "..."
BOT_TOKEN_SECRET_NAME = "telegram-bot-token"
WEBHOOK_SECRET_NAME = "telegram-webhook-secret"
INGRESS_SECRET_HEADER = "X-Agent-Framework-Ingress-Secret"
ENABLE_SENSITIVE_DATA = os.getenv("ENABLE_SENSITIVE_DATA", "true").casefold() in {"1", "true", "yes", "on"}
AGENT_INSTRUCTIONS = (Path(__file__).parent / "instructions.md").read_text(encoding="utf-8").strip()
MODEL_MEDIA_TYPES = {
"application/pdf": "application/pdf",
"audio/mp3": "audio/mp3",
"audio/mpeg": "audio/mp3",
"audio/wav": "audio/wav",
"audio/wave": "audio/wav",
"audio/x-wav": "audio/wav",
"image/gif": "image/gif",
"image/jpeg": "image/jpeg",
"image/png": "image/png",
"image/webp": "image/webp",
}
# Message bodies and Telegram file URLs can contain user content or the bot
# token, so keep dependency INFO logs out of non-sensitive telemetry.
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
logging.getLogger("agent_framework").setLevel(logging.WARNING)
@dataclass
class Runtime:
"""Hold long-lived clients used by the hosted process."""
credential: DefaultAzureCredential
history: CosmosHistoryProvider
agent: Agent
secrets: SecretClient
http: httpx.AsyncClient
bot_token: str | None = None
_runtime: Runtime | None = None
_runtime_lock = asyncio.Lock()
async def get_runtime() -> Runtime:
"""Create and cache the Azure, Agent Framework, and HTTP clients."""
global _runtime
if _runtime is not None:
return _runtime
async with _runtime_lock:
if _runtime is not None:
return _runtime
credential = DefaultAzureCredential()
secrets = SecretClient(vault_url=os.environ["KEY_VAULT_URL"], credential=credential)
history = CosmosHistoryProvider(
endpoint=os.environ["AZURE_COSMOS_ENDPOINT"],
database_name=os.environ["AZURE_COSMOS_DATABASE_NAME"],
container_name=os.environ["AZURE_COSMOS_CONTAINER_NAME"],
credential=credential,
)
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
credential=credential,
)
await client.configure_azure_monitor(
enable_sensitive_data=ENABLE_SENSITIVE_DATA,
enable_live_metrics=True,
)
agent = Agent(
client=client,
name="TelegramAssistant",
instructions=AGENT_INSTRUCTIONS,
context_providers=[history],
default_options={"store": False},
)
_runtime = Runtime(
credential=credential,
history=history,
agent=agent,
secrets=secrets,
http=httpx.AsyncClient(timeout=httpx.Timeout(30.0), follow_redirects=True),
)
return _runtime
async def get_bot_token(runtime: Runtime) -> str:
"""Return the cached Telegram bot token from Key Vault."""
if runtime.bot_token is not None:
return runtime.bot_token
secret = await runtime.secrets.get_secret(BOT_TOKEN_SECRET_NAME)
value = secret.value
if not isinstance(value, str) or not value:
raise RuntimeError("The Telegram bot token secret is empty")
runtime.bot_token = value
return value
async def authenticate_ingress(request: Request, runtime: Runtime) -> bool:
"""Validate the secret stamped by API Management."""
provided_secret = request.headers.get(INGRESS_SECRET_HEADER)
if not provided_secret:
return False
secret = await runtime.secrets.get_secret(WEBHOOK_SECRET_NAME)
expected_secret = secret.value
if not isinstance(expected_secret, str) or not expected_secret:
raise RuntimeError("The Telegram webhook secret is empty")
return hmac.compare_digest(provided_secret, expected_secret)
def _telegram_result(response: httpx.Response, method: str) -> dict[str, Any]:
"""Validate a Telegram Bot API response without exposing its request URL."""
try:
payload: dict[str, Any] = response.json()
except ValueError as exc:
raise RuntimeError(f"Telegram returned invalid JSON for {method}") from exc
if not isinstance(payload, Mapping):
raise RuntimeError(f"Telegram returned an invalid response for {method}")
description = payload.get("description")
if response.is_error or payload.get("ok") is not True:
if (
method == "editMessageText"
and isinstance(description, str)
and "message is not modified" in description.lower()
):
LOGGER.debug("Telegram ignored an edit whose rendered content was unchanged")
return {}
safe_description = description if isinstance(description, str) else "unknown error"
raise RuntimeError(f"Telegram rejected {method} with status {response.status_code}: {safe_description}")
result = payload.get("result")
return result if isinstance(result, dict) else {}
async def execute_telegram_operation(runtime: Runtime, operation: TelegramOperation) -> dict[str, Any]:
"""Execute one operation produced by the Telegram hosting helper."""
token = await get_bot_token(runtime)
method = operation["method"]
try:
response = await runtime.http.post(
f"https://api.telegram.org/bot{token}/{method}",
json=operation["payload"],
)
except httpx.HTTPError:
# httpx exceptions include the token-bearing request URL.
raise RuntimeError(f"Telegram request failed for {method}") from None
return _telegram_result(response, method)
async def resolve_telegram_file(
file_id: str,
runtime: Runtime,
*,
media_type: str = "application/octet-stream",
) -> str | None:
"""Resolve and download a bounded Telegram file as a token-safe data URI."""
try:
metadata = await execute_telegram_operation(
runtime,
TelegramOperation(method="getFile", payload={"file_id": file_id}),
)
except RuntimeError:
LOGGER.warning("Telegram media metadata could not be resolved")
return None
file_path = metadata.get("file_path")
file_size = metadata.get("file_size")
if not isinstance(file_path, str) or (isinstance(file_size, int) and file_size > MAX_MEDIA_BYTES):
return None
token = await get_bot_token(runtime)
try:
async with runtime.http.stream("GET", f"https://api.telegram.org/file/bot{token}/{file_path}") as response:
if response.is_error:
LOGGER.warning("Telegram media download returned status %s", response.status_code)
return None
content_length = response.headers.get("content-length")
if content_length is not None:
try:
if int(content_length) > MAX_MEDIA_BYTES:
return None
except ValueError:
LOGGER.debug("Telegram media response had an invalid content-length")
content = bytearray()
async for chunk in response.aiter_bytes():
content.extend(chunk)
if len(content) > MAX_MEDIA_BYTES:
return None
except httpx.HTTPError:
LOGGER.warning("Telegram media download failed")
return None
encoded = base64.b64encode(content).decode("ascii")
return f"data:{media_type};base64,{encoded}"
def _normalize_run_media_type(messages: AgentRunInputs, source_media_type: str, model_media_type: str) -> None:
"""Align converted URI content with the model serializer's supported media type."""
if isinstance(messages, Message):
normalized_messages = (messages,)
elif isinstance(messages, Sequence) and not isinstance(messages, (str, bytes)):
normalized_messages = messages
else:
return
for message in normalized_messages:
if not isinstance(message, Message):
continue
for content in message.contents:
if (
getattr(content, "type", None) in {"data", "uri"}
and getattr(content, "media_type", None) == source_media_type
):
content.media_type = model_media_type
async def _send_command_response(
update: Mapping[str, Any],
command: str,
session_id: str,
runtime: Runtime,
) -> bool:
"""Handle sample-owned commands and return whether one matched."""
chat_id = telegram_chat_id(update)
if chat_id is None:
raise ValueError("Telegram update does not contain a supported chat")
name = command.partition(" ")[0]
if name == "/start":
text = "Hi! I am a Telegram assistant. Send text or media, or use /help to see available commands."
elif name == "/help":
text = "/new - clear this conversation\n/help - show this message\n/start - show the welcome message"
elif name == "/new":
await runtime.history.clear(session_id)
text = "New conversation started. Your next message begins with empty history."
else:
return False
await execute_telegram_operation(
runtime,
TelegramOperation(method="sendMessage", payload={"chat_id": chat_id, "text": text}),
)
return True
async def _stream_operations(
stream: ResponseStream[AgentResponseUpdate, AgentResponse[Any]],
*,
chat_id: int,
message_id: int,
) -> AsyncIterator[TelegramOperation]:
"""Render the agent response stream into Telegram operations."""
async for operation in telegram_from_streaming_run(
stream,
chat_id=chat_id,
message_id=message_id,
initial_text=PLACEHOLDER_TEXT,
):
yield operation
async def deliver_stream(
runtime: Runtime,
stream: ResponseStream[AgentResponseUpdate, AgentResponse[Any]],
*,
chat_id: int,
message_id: int,
clock: Callable[[], float] = time.monotonic,
sleep: Callable[[float], Awaitable[None]] = asyncio.sleep,
) -> None:
"""Deliver cumulative stream edits with a bounded Telegram edit rate."""
last_edit_at = 0.0
async for operation in _stream_operations(stream, chat_id=chat_id, message_id=message_id):
if operation["method"] == "editMessageText":
delay = EDIT_INTERVAL_SECONDS - (clock() - last_edit_at)
if delay > 0:
await sleep(delay)
last_edit_at = clock()
await execute_telegram_operation(runtime, operation)
async def handle_telegram_update(update: Mapping[str, Any], session_id: str, runtime: Runtime) -> None:
"""Handle one Telegram update using durable history for the supplied session."""
chat_id = telegram_chat_id(update)
if chat_id is None:
raise ValueError("Telegram update does not contain a supported chat")
if str(chat_id) != session_id:
raise ValueError("Telegram chat id does not match agent_session_id")
callback_query_id = telegram_callback_query_id(update)
if callback_query_id is not None:
await execute_telegram_operation(
runtime,
TelegramOperation(method="answerCallbackQuery", payload={"callback_query_id": callback_query_id}),
)
command = telegram_command(update)
if command is not None and await _send_command_response(update, command, session_id, runtime):
return
media = telegram_media_file_id(update)
model_media_type = MODEL_MEDIA_TYPES.get(media[1].lower()) if media is not None else None
if media is not None and model_media_type is None:
await execute_telegram_operation(
runtime,
TelegramOperation(
method="sendMessage",
payload={
"chat_id": chat_id,
"text": "I can process photos, PDF documents, and MP3 or WAV audio up to 1 MiB.",
},
),
)
return
resolved_media_type = model_media_type or "application/octet-stream"
async def resolve_file(file_id: str) -> str | None:
media_type = resolved_media_type if media is not None and media[0] == file_id else "application/octet-stream"
return await resolve_telegram_file(file_id, runtime, media_type=media_type)
try:
run = await telegram_to_run(
update,
resolve_file_url=resolve_file,
stream=True,
)
except ValueError:
await execute_telegram_operation(
runtime,
TelegramOperation(
method="sendMessage",
payload={
"chat_id": chat_id,
"text": "I could not process that update. Try sending text or a supported file up to 1 MiB.",
},
),
)
return
if media is not None and resolved_media_type != media[1]:
_normalize_run_media_type(run["messages"], media[1], resolved_media_type)
placeholder = await execute_telegram_operation(
runtime,
TelegramOperation(method="sendMessage", payload={"chat_id": chat_id, "text": PLACEHOLDER_TEXT}),
)
message_id = placeholder.get("message_id")
if not isinstance(message_id, int):
raise RuntimeError("Telegram did not return a message id for the streaming placeholder")
session = runtime.agent.create_session(session_id=session_id)
response = runtime.agent.run(
run["messages"],
session=session,
options=run["options"],
stream=True,
)
if not isinstance(response, ResponseStream):
raise RuntimeError("Agent did not return a response stream")
await deliver_stream(runtime, response, chat_id=chat_id, message_id=message_id)
async def dispatch_channel(payload: Mapping[str, Any], session_id: str, runtime: Runtime) -> None:
"""Validate the ingress channel and dispatch its unmodified payload."""
channel = payload.get("channel")
if not isinstance(channel, str):
raise ValueError("Missing channel")
update = {key: value for key, value in payload.items() if key != "channel"}
match channel:
case "telegram":
await handle_telegram_update(update, session_id, runtime)
case _:
raise ValueError(f"Unsupported channel: {channel}")
app = InvocationAgentServerHost()
enable_instrumentation(enable_sensitive_data=ENABLE_SENSITIVE_DATA, force=True)
@app.invoke_handler
async def handle_invoke(request: Request) -> Response:
"""Process an update forwarded by API Management."""
runtime = await get_runtime()
if not await authenticate_ingress(request, runtime):
return Response("Unauthorized ingress", status_code=401)
session_id = get_request_context().session_id
if not session_id:
return Response("Missing agent_session_id", status_code=400)
try:
payload: dict[str, Any] = await request.json()
if not isinstance(payload, dict):
raise ValueError("Request body must be a JSON object")
await dispatch_channel(payload, session_id, runtime)
except ValueError as exc:
return Response(str(exc), status_code=400)
return Response(status_code=200)
if __name__ == "__main__":
app.run()