Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: populate seq_id for ping messages (#8844)
cthomas6599aa3b4442
diff --git a/letta/server/rest_api/streaming_response.py b/letta/server/rest_api/streaming_response.pyindex 385caca9f..9869ff5cc 100644--- a/letta/server/rest_api/streaming_response.py+++ b/letta/server/rest_api/streaming_response.py@@ -4,6 +4,7 @@import asyncioimport json+import refrom collections.abc import AsyncIteratorfrom datetime import datetime, timezonefrom uuid import uuid4@@ -57,6 +58,7 @@ async def add_keepalive_to_stream(# A small maxsize prevents unbounded memory growth if the client is slowqueue = asyncio.Queue(maxsize=1)stream_exhausted = False+ last_seq_id = Noneasync def stream_reader():"""Read from the original stream and put items in the queue."""@@ -81,13 +83,19 @@ async def add_keepalive_to_stream(# Stream finishedbreakelif msg_type == "data":+ # Track seq_id from chunks for ping messages+ if isinstance(data, str):+ seq_id_match = re.search(r'"seq_id":(\d+)', data) # Look for "seq_id":<number> pattern in the SSE chunk+ if seq_id_match:+ last_seq_id = int(seq_id_match.group(1))+yield dataexcept asyncio.TimeoutError:# No data received within keepalive intervalif not stream_exhausted:- # Send keepalive ping in the same format as [DONE]- yield f"data: {LettaPing(id=f'ping-{uuid4()}', date=datetime.now(timezone.utc), run_id=run_id).model_dump_json()}\n\n"+ # Send keepalive ping with the last seq_id to allow clients to track progress+ yield f"data: {LettaPing(id=f'ping-{uuid4()}', date=datetime.now(timezone.utc), run_id=run_id, seq_id=last_seq_id).model_dump_json()}\n\n"else:# Stream is done but queue might be processing# Check if there's anything left