From 6adec991da157cef8cfabad9ad48da32e137d735 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 7 May 2026 16:32:02 -0500 Subject: [PATCH] Wire streaming: agent-runner processes chunks, remove send_message tool --- agentclaw/app/agent_claw_main/main.py | 2 +- src/lambdas/agent-runner/handler.py | 37 ++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/agentclaw/app/agent_claw_main/main.py b/agentclaw/app/agent_claw_main/main.py index 8185ee2..bda8b1c 100644 --- a/agentclaw/app/agent_claw_main/main.py +++ b/agentclaw/app/agent_claw_main/main.py @@ -263,7 +263,7 @@ async def main(payload: dict, context): 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, manage_service] diff --git a/src/lambdas/agent-runner/handler.py b/src/lambdas/agent-runner/handler.py index 8eb39d8..20c3998 100644 --- a/src/lambdas/agent-runner/handler.py +++ b/src/lambdas/agent-runner/handler.py @@ -187,10 +187,41 @@ def handler(event, context): 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') + text_buffer = '' if body is not None: - for _ in body.iter_chunks(): - pass + 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 + + # 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}")