Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: patch `use_assistant_message` flag on the server (#704)
Charles Packer208933b79859
diff --git a/letta/client/streaming.py b/letta/client/streaming.pyindex c87225340..9c9a04e70 100644--- a/letta/client/streaming.py+++ b/letta/client/streaming.py@@ -7,7 +7,7 @@ from httpx_sse import SSEError, connect_ssefrom letta.constants import OPENAI_CONTEXT_WINDOW_ERROR_SUBSTRINGfrom letta.errors import LLMErrorfrom letta.schemas.enums import MessageStreamStatus-from letta.schemas.letta_message import ReasoningMessage, ToolCallMessage, ToolReturnMessage+from letta.schemas.letta_message import AssistantMessage, ReasoningMessage, ToolCallMessage, ToolReturnMessagefrom letta.schemas.letta_response import LettaStreamingResponsefrom letta.schemas.usage import LettaUsageStatistics@@ -50,6 +50,8 @@ def _sse_post(url: str, data: dict, headers: dict) -> Generator[LettaStreamingRechunk_data = json.loads(sse.data)if "reasoning" in chunk_data:yield ReasoningMessage(**chunk_data)+ elif "assistant_message" in chunk_data:+ yield AssistantMessage(**chunk_data)elif "tool_call" in chunk_data:yield ToolCallMessage(**chunk_data)elif "tool_return" in chunk_data:diff --git a/letta/server/rest_api/app.py b/letta/server/rest_api/app.pyindex 5ae3fb509..223871ad5 100644--- a/letta/server/rest_api/app.py+++ b/letta/server/rest_api/app.py@@ -290,8 +290,8 @@ def start_server(server_logger.addHandler(stream_handler)if (os.getenv("LOCAL_HTTPS") == "true") or "--localhttps" in sys.argv:- print(f"▶ Server running at: https://{host or 'localhost'}:{port or REST_DEFAULT_PORT}\n")- print(f"▶ View using ADE at: https://app.letta.com/development-servers/local/dashboard")+ print(f"▶ Server running at: https://{host or 'localhost'}:{port or REST_DEFAULT_PORT}")+ print(f"▶ View using ADE at: https://app.letta.com/development-servers/local/dashboard\n")uvicorn.run(app,host=host or "localhost",@@ -300,8 +300,8 @@ def start_server(ssl_certfile="certs/localhost.pem",)else:- print(f"▶ Server running at: http://{host or 'localhost'}:{port or REST_DEFAULT_PORT}\n")- print(f"▶ View using ADE at: https://app.letta.com/development-servers/local/dashboard")+ print(f"▶ Server running at: http://{host or 'localhost'}:{port or REST_DEFAULT_PORT}")+ print(f"▶ View using ADE at: https://app.letta.com/development-servers/local/dashboard\n")uvicorn.run(app,host=host or "localhost",diff --git a/letta/server/rest_api/interface.py b/letta/server/rest_api/interface.pyindex 995593aad..933703308 100644--- a/letta/server/rest_api/interface.py+++ b/letta/server/rest_api/interface.py@@ -264,6 +264,7 @@ class StreamingServerInterface(AgentChunkStreamingInterface):self,multi_step=True,# Related to if we want to try and pass back the AssistantMessage as a special case function+ use_assistant_message=False,assistant_message_tool_name=DEFAULT_MESSAGE_TOOL,assistant_message_tool_kwarg=DEFAULT_MESSAGE_TOOL_KWARG,# Related to if we expect inner_thoughts to be in the kwargs@@ -295,9 +296,10 @@ class StreamingServerInterface(AgentChunkStreamingInterface):# self.multi_step_gen_indicator = MessageStreamStatus.done_generation# Support for AssistantMessage- self.use_assistant_message = False # TODO: Remove this+ self.use_assistant_message = use_assistant_message # TODO: Remove this (actually? @charles)self.assistant_message_tool_name = assistant_message_tool_nameself.assistant_message_tool_kwarg = assistant_message_tool_kwarg+ self.prev_assistant_message_id = None # Used to skip tool call response receipts for `send_message`# Support for inner_thoughts_in_kwargsself.inner_thoughts_in_kwargs = inner_thoughts_in_kwargs@@ -308,6 +310,8 @@ class StreamingServerInterface(AgentChunkStreamingInterface):self.function_name_buffer = Noneself.function_args_buffer = Noneself.function_id_buffer = None+ # A buffer used to store the last flushed function name+ self.last_flushed_function_name = None# extra printsself.debug = False@@ -434,7 +438,8 @@ class StreamingServerInterface(AgentChunkStreamingInterface):# TODO(charles) merge into logic for internal_monologue# special case for trapping `send_message`- if self.use_assistant_message and tool_call.function:+ # if self.use_assistant_message and tool_call.function:+ if not self.inner_thoughts_in_kwargs and self.use_assistant_message and tool_call.function:if self.inner_thoughts_in_kwargs:raise NotImplementedError("inner_thoughts_in_kwargs with use_assistant_message not yet supported")@@ -535,15 +540,28 @@ class StreamingServerInterface(AgentChunkStreamingInterface):# however the frontend may expect name first, then args, so to be# safe we'll output name first in a separate chunkif self.function_name_buffer:- processed_chunk = ToolCallMessage(- id=message_id,- date=message_date,- tool_call=ToolCallDelta(- name=self.function_name_buffer,- arguments=None,- tool_call_id=self.function_id_buffer,- ),- )++ # use_assisitant_message means that we should also not release main_json raw, and instead should only release the contents of "message": "..."+ if self.use_assistant_message and self.function_name_buffer == self.assistant_message_tool_name:+ processed_chunk = None++ # Store the ID of the tool call so allow skipping the corresponding response+ if self.function_id_buffer:+ self.prev_assistant_message_id = self.function_id_buffer++ else:+ processed_chunk = ToolCallMessage(+ id=message_id,+ date=message_date,+ tool_call=ToolCallDelta(+ name=self.function_name_buffer,+ arguments=None,+ tool_call_id=self.function_id_buffer,+ ),+ )++ # Record what the last function name we flushed was+ self.last_flushed_function_name = self.function_name_buffer# Clear the bufferself.function_name_buffer = Noneself.function_id_buffer = None@@ -559,35 +577,94 @@ class StreamingServerInterface(AgentChunkStreamingInterface):# If there was nothing in the name buffer, we can proceed to# output the arguments chunk as a ToolCallMessageelse:- # There may be a buffer from a previous chunk, for example- # if the previous chunk had arguments but we needed to flush name- if self.function_args_buffer:- # In this case, we should release the buffer + new data at once- combined_chunk = self.function_args_buffer + updates_main_json- processed_chunk = ToolCallMessage(- id=message_id,- date=message_date,- tool_call=ToolCallDelta(- name=None,- arguments=combined_chunk,- tool_call_id=self.function_id_buffer,- ),- )- # clear buffer- self.function_args_buffer = None- self.function_id_buffer = None++ # use_assisitant_message means that we should also not release main_json raw, and instead should only release the contents of "message": "..."+ if self.use_assistant_message and (+ self.last_flushed_function_name is not None+ and self.last_flushed_function_name == self.assistant_message_tool_name+ ):+ # do an additional parse on the updates_main_json+ if self.function_args_buffer:++ updates_main_json = self.function_args_buffer + updates_main_json+ self.function_args_buffer = None++ # Pretty gross hardcoding that assumes that if we're toggling into the keywords, we have the full prefix+ match_str = '{"' + self.assistant_message_tool_kwarg + '":"'+ if updates_main_json == match_str:+ updates_main_json = None++ else:+ # Some hardcoding to strip off the trailing "}"+ if updates_main_json in ["}", '"}']:+ updates_main_json = None+ if updates_main_json and len(updates_main_json) > 0 and updates_main_json[-1:] == '"':+ updates_main_json = updates_main_json[:-1]++ if not updates_main_json:+ # early exit to turn into content mode+ return None++ # There may be a buffer from a previous chunk, for example+ # if the previous chunk had arguments but we needed to flush name+ if self.function_args_buffer:+ # In this case, we should release the buffer + new data at once+ combined_chunk = self.function_args_buffer + updates_main_json+ processed_chunk = AssistantMessage(+ id=message_id,+ date=message_date,+ assistant_message=combined_chunk,+ )+ # Store the ID of the tool call so allow skipping the corresponding response+ if self.function_id_buffer:+ self.prev_assistant_message_id = self.function_id_buffer+ # clear buffer+ self.function_args_buffer = None+ self.function_id_buffer = None++ else:+ # If there's no buffer to clear, just output a new chunk with new data+ processed_chunk = AssistantMessage(+ id=message_id,+ date=message_date,+ assistant_message=updates_main_json,+ )+ # Store the ID of the tool call so allow skipping the corresponding response+ if self.function_id_buffer:+ self.prev_assistant_message_id = self.function_id_buffer+ # clear buffers+ self.function_id_buffer = Noneelse:- # If there's no buffer to clear, just output a new chunk with new data- processed_chunk = ToolCallMessage(- id=message_id,- date=message_date,- tool_call=ToolCallDelta(- name=None,- arguments=updates_main_json,- tool_call_id=self.function_id_buffer,- ),- )- self.function_id_buffer = None++ # There may be a buffer from a previous chunk, for example+ # if the previous chunk had arguments but we needed to flush name+ if self.function_args_buffer:+ # In this case, we should release the buffer + new data at once+ combined_chunk = self.function_args_buffer + updates_main_json+ processed_chunk = ToolCallMessage(+ id=message_id,+ date=message_date,+ tool_call=ToolCallDelta(+ name=None,+ arguments=combined_chunk,+ tool_call_id=self.function_id_buffer,+ ),+ )+ # clear buffer+ self.function_args_buffer = None+ self.function_id_buffer = None+ else:+ # If there's no buffer to clear, just output a new chunk with new data+ processed_chunk = ToolCallMessage(+ id=message_id,+ date=message_date,+ tool_call=ToolCallDelta(+ name=None,+ arguments=updates_main_json,+ tool_call_id=self.function_id_buffer,+ ),+ )+ self.function_id_buffer = None# # If there's something in the main_json buffer, we should add if to the arguments and release it together# tool_call_delta = {}@@ -906,6 +983,8 @@ class StreamingServerInterface(AgentChunkStreamingInterface):date=msg_obj.created_at,assistant_message=func_args[self.assistant_message_tool_kwarg],)+ # Store the ID of the tool call so allow skipping the corresponding response+ self.prev_assistant_message_id = function_call.idelse:processed_chunk = ToolCallMessage(id=msg_obj.id,@@ -938,13 +1017,23 @@ class StreamingServerInterface(AgentChunkStreamingInterface):msg = msg.replace("Success: ", "")# new_message = {"function_return": msg, "status": "success"}assert msg_obj.tool_call_id is not None- new_message = ToolReturnMessage(- id=msg_obj.id,- date=msg_obj.created_at,- tool_return=msg,- status="success",- tool_call_id=msg_obj.tool_call_id,- )++ print(f"YYY printing the function call - {msg_obj.tool_call_id} == {self.prev_assistant_message_id} ???")++ # Skip this is use_assistant_message is on+ if self.use_assistant_message and msg_obj.tool_call_id == self.prev_assistant_message_id:+ # Wipe the cache+ self.prev_assistant_message_id = None+ # Skip this tool call receipt+ return+ else:+ new_message = ToolReturnMessage(+ id=msg_obj.id,+ date=msg_obj.created_at,+ tool_return=msg,+ status="success",+ tool_call_id=msg_obj.tool_call_id,+ )elif msg.startswith("Error: "):msg = msg.replace("Error: ", "", 1)diff --git a/letta/server/server.py b/letta/server/server.pyindex 352b982e2..8e01fc31b 100644--- a/letta/server/server.py+++ b/letta/server/server.py@@ -1215,7 +1215,6 @@ class SyncServer(Server):stream_tokens: bool,# related to whether or not we return `LettaMessage`s or `Message`schat_completion_mode: bool = False,- timestamp: Optional[datetime] = None,# Support for AssistantMessageuse_assistant_message: bool = True,assistant_message_tool_name: str = constants.DEFAULT_MESSAGE_TOOL,@@ -1249,7 +1248,16 @@ class SyncServer(Server):stream_tokens = False# Create a new interface per request- letta_agent.interface = StreamingServerInterface(use_assistant_message)+ letta_agent.interface = StreamingServerInterface(+ # multi_step=True, # would we ever want to disable this?+ use_assistant_message=use_assistant_message,+ assistant_message_tool_name=assistant_message_tool_name,+ assistant_message_tool_kwarg=assistant_message_tool_kwarg,+ inner_thoughts_in_kwargs=(+ llm_config.put_inner_thoughts_in_kwargs if llm_config.put_inner_thoughts_in_kwargs is not None else False+ ),+ # inner_thoughts_kwarg=INNER_THOUGHTS_KWARG,+ )streaming_interface = letta_agent.interfaceif not isinstance(streaming_interface, StreamingServerInterface):raise ValueError(f"Agent has wrong type of interface: {type(streaming_interface)}")@@ -1263,13 +1271,14 @@ class SyncServer(Server):# streaming_interface.function_call_legacy_mode = stream# Allow AssistantMessage is desired by client- streaming_interface.assistant_message_tool_name = assistant_message_tool_name- streaming_interface.assistant_message_tool_kwarg = assistant_message_tool_kwarg+ # streaming_interface.use_assistant_message = use_assistant_message+ # streaming_interface.assistant_message_tool_name = assistant_message_tool_name+ # streaming_interface.assistant_message_tool_kwarg = assistant_message_tool_kwarg# Related to JSON buffer reader- streaming_interface.inner_thoughts_in_kwargs = (- llm_config.put_inner_thoughts_in_kwargs if llm_config.put_inner_thoughts_in_kwargs is not None else False- )+ # streaming_interface.inner_thoughts_in_kwargs = (+ # llm_config.put_inner_thoughts_in_kwargs if llm_config.put_inner_thoughts_in_kwargs is not None else False+ # )# Offload the synchronous message_func to a separate threadstreaming_interface.stream_start()diff --git a/letta/streaming_utils.py b/letta/streaming_utils.pyindex 61b6fa7a1..650e66438 100644--- a/letta/streaming_utils.py+++ b/letta/streaming_utils.py@@ -1,4 +1,4 @@-from typing import Optional+from typing import Optional, Tuplefrom letta.constants import DEFAULT_MESSAGE_TOOL_KWARG@@ -48,7 +48,7 @@ class JSONInnerThoughtsExtractor:self.hold_main_json = wait_for_first_keyself.main_json_held_buffer = ""- def process_fragment(self, fragment):+ def process_fragment(self, fragment: str) -> Tuple[str, str]:updates_main_json = ""updates_inner_thoughts = ""i = 0@@ -263,8 +263,10 @@ class FunctionArgumentsStreamHandler:self.key_buffer = ""self.accumulating = Truereturn None+if chunk.strip() == "}":self.in_message = Falseself.message_started = Falsereturn None+return Nonediff --git a/tests/test_client_legacy.py b/tests/test_client_legacy.pyindex 51fe6d54a..a7675c682 100644--- a/tests/test_client_legacy.py+++ b/tests/test_client_legacy.py@@ -262,6 +262,8 @@ def test_streaming_send_message(mock_e2b_api_key_none, client: RESTClient, agentinner_thoughts_count += 1if isinstance(chunk, ToolCallMessage) and chunk.tool_call and chunk.tool_call.name == "send_message":send_message_ran = True+ if isinstance(chunk, AssistantMessage):+ send_message_ran = Trueif isinstance(chunk, MessageStreamStatus):if chunk == MessageStreamStatus.done:assert not done, "Message stream already done"@@ -531,16 +533,15 @@ def test_sources(client: Union[LocalClient, RESTClient], agent: AgentState):def test_message_update(client: Union[LocalClient, RESTClient], agent: AgentState):"""Test that we can update the details of a message"""- import json# create a messagemessage_response = client.send_message(agent_id=agent.id, message="Test message", role="user")print("Messages=", message_response)assert isinstance(message_response, LettaResponse)- assert isinstance(message_response.messages[-1], ToolReturnMessage)+ assert isinstance(message_response.messages[-1], AssistantMessage)message = message_response.messages[-1]- new_text = json.dumps({"message": "This exact string would never show up in the message???"})+ new_text = "this is a secret message"new_message = client.update_message(message_id=message.id, text=new_text, agent_id=agent.id)assert new_message.text == new_text