Suppress exceptions in generator to prevent AgentCore retry duplicates
This commit is contained in:
@@ -185,7 +185,7 @@ _current_actor_id: str = ''
|
||||
|
||||
|
||||
@app.entrypoint
|
||||
def main(payload: dict, context):
|
||||
async def main(payload: dict, context):
|
||||
"""Handle an invocation from agent-runner Lambda (streaming)."""
|
||||
global _current_actor_id
|
||||
|
||||
@@ -290,34 +290,21 @@ def main(payload: dict, context):
|
||||
tools=base_tools + list(workspace_tools),
|
||||
)
|
||||
|
||||
# Use callback handler to stream tokens directly to Telegram.
|
||||
# This avoids AgentCore's async-generator retry path (which causes duplicates).
|
||||
token_buffer = []
|
||||
|
||||
def _on_event(event: dict) -> None:
|
||||
"""Strands callback: accumulate text tokens and flush to Telegram at para breaks."""
|
||||
delta = event.get('event', {}).get('contentBlockDelta', {}).get('delta', {})
|
||||
token = delta.get('text', '') if isinstance(delta, dict) else ''
|
||||
if not token:
|
||||
return
|
||||
token_buffer.append(token)
|
||||
buf = ''.join(token_buffer)
|
||||
if buf.rstrip().endswith(('\n\n', '.\n', '!\n', '?\n')) or len(buf) > 800:
|
||||
stripped = buf.strip()
|
||||
if stripped:
|
||||
adapter.send(stripped)
|
||||
token_buffer.clear()
|
||||
|
||||
final_message = None
|
||||
try:
|
||||
result = agent(payload.get('prompt', ''), callback_handler=_on_event)
|
||||
# Flush remaining tokens
|
||||
remaining = ''.join(token_buffer).strip()
|
||||
if remaining:
|
||||
adapter.send(remaining)
|
||||
async for event in agent.stream_async(payload.get('prompt', '')):
|
||||
if 'result' in event:
|
||||
final_message = event['result'].message
|
||||
yield event
|
||||
except Exception as e:
|
||||
# Catch ALL exceptions including ReadTimeoutError to prevent AgentCore retry.
|
||||
# A retry re-runs the full agent loop causing duplicate Telegram messages.
|
||||
print(f'[main] Agent error (suppressed to prevent retry): {type(e).__name__}: {e}')
|
||||
if final_message:
|
||||
yield {'data': str(final_message), 'result': {'message': final_message}}
|
||||
finally:
|
||||
_typing_active = False
|
||||
session_manager.close()
|
||||
|
||||
return {'result': result.message}
|
||||
|
||||
app.run()
|
||||
|
||||
Reference in New Issue
Block a user