Wire streaming: agent-runner processes chunks, remove send_message tool

This commit is contained in:
daniel
2026-05-07 16:32:02 -05:00
parent 40a942b506
commit 6adec991da
2 changed files with 35 additions and 4 deletions

View File

@@ -263,7 +263,7 @@ async def main(payload: dict, context):
region_name="us-east-1", region_name="us-east-1",
) )
base_tools = [send_message, web_search, web_fetch, read_workspace_file, write_workspace_file, base_tools = [web_search, web_fetch, read_workspace_file, write_workspace_file,
_code_interpreter.code_interpreter, home_assistant, connect_google_account, _code_interpreter.code_interpreter, home_assistant, connect_google_account,
manage_service] manage_service]

View File

@@ -187,10 +187,41 @@ def handler(event, context):
payload=json.dumps(payload).encode(), payload=json.dumps(payload).encode(),
) )
# Drain streaming response body (agent delivers to Telegram via send_message tool) # Process streaming response: buffer text chunks and send to Telegram as paragraphs arrive
bot_token = ''
bot_token_secret_arn = os.environ.get('TELEGRAM_BOT_TOKEN_SECRET_ARN', '')
if bot_token_secret_arn:
sm = boto3.client('secretsmanager', region_name='us-east-1')
try:
bot_token = sm.get_secret_value(SecretId=bot_token_secret_arn)['SecretString']
except Exception as e:
print(f'[agent-runner] Failed to get bot token: {e}')
body = response.get('response') body = response.get('response')
text_buffer = ''
if body is not None: if body is not None:
for _ in body.iter_chunks(): for chunk in body.iter_chunks():
if not chunk:
continue
try:
event = json.loads(chunk.decode('utf-8'))
# Strands streaming event: 'data' field contains text delta
delta = event.get('data', '') or event.get('text', '')
if delta:
text_buffer += delta
# Flush on paragraph or sentence break, or if buffer is large
flush = (
text_buffer.rstrip().endswith(('\n\n', '.\n', '!\n', '?\n'))
or len(text_buffer) > 800
)
if flush and text_buffer.strip():
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip())
text_buffer = ''
except (json.JSONDecodeError, UnicodeDecodeError):
pass pass
# Flush any remaining text
if text_buffer.strip() and bot_token:
send_telegram_direct(str(chat_id), bot_token, text_buffer.strip())
print(f"[agent-runner] Completed session={session_id} actor={actor_id}") print(f"[agent-runner] Completed session={session_id} actor={actor_id}")