AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): persist user-context prefix to DB in both SDK and baseline paths
Zamil Majdyce0cb1e03545
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 154bc8a886..1f1fe42f59 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -27,6 +27,7 @@ from opentelemetry import trace as otel_tracefrom backend.copilot.config import CopilotModefrom backend.copilot.context import get_workspace_manager, set_execution_context+from backend.copilot.db import update_message_content_by_sequencefrom backend.copilot.graphiti.config import is_enabled_for_userfrom backend.copilot.model import (ChatMessage,@@ -1067,9 +1068,13 @@ async def stream_chat_completion_baseline(if prefixed is not None:# Persist the prefixed content so subsequent turns and --resume# retain the user context.- for session_msg in session.messages:+ # The user message was already saved to DB before context injection+ # (at ~line 932); update the DB record so the prefixed content+ # survives page reload.+ for idx, session_msg in enumerate(session.messages):if session_msg.role == "user":session_msg.content = prefixed+ await update_message_content_by_sequence(session_id, idx, prefixed)breakuser_message_for_transcript = prefixedelse:diff --git a/autogpt_platform/backend/backend/copilot/db.py b/autogpt_platform/backend/backend/copilot/db.pyindex a1dd93e752..6ab131beed 100644--- a/autogpt_platform/backend/backend/copilot/db.py+++ b/autogpt_platform/backend/backend/copilot/db.py@@ -498,6 +498,42 @@ async def update_tool_message_content(return False+async def update_message_content_by_sequence(+ session_id: str,+ sequence: int,+ new_content: str,+) -> bool:+ """Update the content of a specific message by its sequence number.++ Used to persist content modifications (e.g. user-context prefix injection)+ to a message that was already saved to the DB.++ Args:+ session_id: The chat session ID.+ sequence: The 0-based sequence number of the message to update.+ new_content: The new content to set.++ Returns:+ True if a message was updated, False otherwise.+ """+ try:+ result = await PrismaChatMessage.prisma().update_many(+ where={"sessionId": session_id, "sequence": sequence},+ data={"content": sanitize_string(new_content)},+ )+ if result == 0:+ logger.warning(+ f"No message found to update for session {session_id}, sequence {sequence}"+ )+ return False+ return True+ except Exception as e:+ logger.error(+ f"Failed to update message for session {session_id}, sequence {sequence}: {e}"+ )+ return False++async def set_turn_duration(session_id: str, duration_ms: int) -> None:"""Set durationMs on the last assistant message in a session.diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex d6359cbd2c..23f8041d53 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -62,6 +62,7 @@ from ..constants import (is_transient_api_error,)from ..context import encode_cwd_for_cli+from ..db import update_message_content_by_sequencefrom ..graphiti.config import is_enabled_for_userfrom ..model import (ChatMessage,@@ -2304,9 +2305,15 @@ async def stream_chat_completion_sdk(current_message = prefixed_messagequery_message = prefixed_message# Persist the prefixed content so resumed sessions retain the context.- for session_msg in session.messages:+ # The user message was already saved to DB before context injection;+ # update the DB record so the prefixed content survives page reload+ # and --resume (the save at line ~1926 used the un-prefixed content).+ for idx, session_msg in enumerate(session.messages):if session_msg.role == "user":session_msg.content = prefixed_message+ await update_message_content_by_sequence(+ session_id, idx, prefixed_message+ )break# If files are attached, prepare them: images become vision# content blocks in the user message, other files go to sdk_cwd.