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.
feat(backend): Extract backend copilot/chat enhancements from hackathon (#11719)
Swifty3b1b2fe30cc4
diff --git a/autogpt_platform/Makefile b/autogpt_platform/Makefileindex 2ff454e392..d53be570bc 100644--- a/autogpt_platform/Makefile+++ b/autogpt_platform/Makefile@@ -6,9 +6,10 @@ start-core:# Stop core servicesstop-core:- docker compose stop deps+ docker compose stopreset-db:+ docker compose stop dbrm -rf db/docker/volumes/db/datacd backend && poetry run prisma migrate deploycd backend && poetry run prisma generate@@ -60,4 +61,4 @@ help:@echo " run-backend - Run the backend FastAPI server"@echo " run-frontend - Run the frontend Next.js development server"@echo " test-data - Run the test data creator"- @echo " load-store-agents - Load store agents from agents/ folder into test database"\ No newline at end of file+ @echo " load-store-agents - Load store agents from agents/ folder into test database"diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex a0004633ca..49689d7ad6 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -58,6 +58,13 @@ V0_API_KEY=OPEN_ROUTER_API_KEY=NVIDIA_API_KEY=+# Langfuse Prompt Management+# Used for managing the CoPilot system prompt externally+# Get credentials from https://cloud.langfuse.com or your self-hosted instance+LANGFUSE_PUBLIC_KEY=+LANGFUSE_SECRET_KEY=+LANGFUSE_HOST=https://cloud.langfuse.com+# OAuth Credentials# For the OAuth callback URL, use <your_frontend_url>/auth/integrations/oauth_callback,# e.g. http://localhost:3000/auth/integrations/oauth_callbackdiff --git a/autogpt_platform/backend/backend/api/external/v1/tools.py b/autogpt_platform/backend/backend/api/external/v1/tools.pyindex 9e362fb32c..07734dd0c9 100644--- a/autogpt_platform/backend/backend/api/external/v1/tools.py+++ b/autogpt_platform/backend/backend/api/external/v1/tools.py@@ -70,7 +70,7 @@ class RunAgentRequest(BaseModel):)-def _create_ephemeral_session(user_id: str | None) -> ChatSession:+def _create_ephemeral_session(user_id: str) -> ChatSession:"""Create an ephemeral session for stateless API requests."""return ChatSession.new(user_id)diff --git a/autogpt_platform/backend/backend/api/features/chat/config.py b/autogpt_platform/backend/backend/api/features/chat/config.pyindex 5b8f16298e..95aef7f2ed 100644--- a/autogpt_platform/backend/backend/api/features/chat/config.py+++ b/autogpt_platform/backend/backend/api/features/chat/config.py@@ -1,7 +1,6 @@"""Configuration management for chat system."""import os-from pathlib import Pathfrom pydantic import Field, field_validatorfrom pydantic_settings import BaseSettings@@ -12,7 +11,11 @@ class ChatConfig(BaseSettings):# OpenAI API Configurationmodel: str = Field(- default="qwen/qwen3-235b-a22b-2507", description="Default model to use"+ default="anthropic/claude-opus-4.5", description="Default model to use"+ )+ title_model: str = Field(+ default="openai/gpt-4o-mini",+ description="Model to use for generating session titles (should be fast/cheap)",)api_key: str | None = Field(default=None, description="OpenAI API key")base_url: str | None = Field(@@ -23,12 +26,6 @@ class ChatConfig(BaseSettings):# Session TTL Configuration - 12 hourssession_ttl: int = Field(default=43200, description="Session TTL in seconds")- # System Prompt Configuration- system_prompt_path: str = Field(- default="prompts/chat_system.md",- description="Path to system prompt file relative to chat module",- )-# Streaming Configurationmax_context_messages: int = Field(default=50, ge=1, le=200, description="Maximum context messages"@@ -41,6 +38,13 @@ class ChatConfig(BaseSettings):default=3, description="Maximum number of agent schedules")+ # Langfuse Prompt Management Configuration+ # Note: Langfuse credentials are in Settings().secrets (settings.py)+ langfuse_prompt_name: str = Field(+ default="CoPilot Prompt",+ description="Name of the prompt in Langfuse to fetch",+ )+@field_validator("api_key", mode="before")@classmethoddef get_api_key(cls, v):@@ -72,43 +76,11 @@ class ChatConfig(BaseSettings):v = "https://openrouter.ai/api/v1"return v- def get_system_prompt(self, **template_vars) -> str:- """Load and render the system prompt from file.-- Args:- **template_vars: Variables to substitute in the template-- Returns:- Rendered system prompt string-- """- # Get the path relative to this module- module_dir = Path(__file__).parent- prompt_path = module_dir / self.system_prompt_path-- # Check for .j2 extension first (Jinja2 template)- j2_path = Path(str(prompt_path) + ".j2")- if j2_path.exists():- try:- from jinja2 import Template-- template = Template(j2_path.read_text())- return template.render(**template_vars)- except ImportError:- # Jinja2 not installed, fall back to reading as plain text- return j2_path.read_text()-- # Check for markdown file- if prompt_path.exists():- content = prompt_path.read_text()-- # Simple variable substitution if Jinja2 is not available- for key, value in template_vars.items():- placeholder = f"{{{key}}}"- content = content.replace(placeholder, str(value))-- return content- raise FileNotFoundError(f"System prompt file not found: {prompt_path}")+ # Prompt paths for different contexts+ PROMPT_PATHS: dict[str, str] = {+ "default": "prompts/chat_system.md",+ "onboarding": "prompts/onboarding_system.md",+ }class Config:"""Pydantic config."""diff --git a/autogpt_platform/backend/backend/api/features/chat/db.py b/autogpt_platform/backend/backend/api/features/chat/db.pynew file mode 100644index 0000000000..05a3553cc8--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/chat/db.py@@ -0,0 +1,249 @@+"""Database operations for chat sessions."""++import asyncio+import logging+from datetime import UTC, datetime+from typing import Any, cast++from prisma.models import ChatMessage as PrismaChatMessage+from prisma.models import ChatSession as PrismaChatSession+from prisma.types import (+ ChatMessageCreateInput,+ ChatSessionCreateInput,+ ChatSessionUpdateInput,+ ChatSessionWhereInput,+)++from backend.data.db import transaction+from backend.util.json import SafeJson++logger = logging.getLogger(__name__)+++async def get_chat_session(session_id: str) -> PrismaChatSession | None:+ """Get a chat session by ID from the database."""+ session = await PrismaChatSession.prisma().find_unique(+ where={"id": session_id},+ include={"Messages": True},+ )+ if session and session.Messages:+ # Sort messages by sequence in Python - Prisma Python client doesn't support+ # order_by in include clauses (unlike Prisma JS), so we sort after fetching+ session.Messages.sort(key=lambda m: m.sequence)+ return session+++async def create_chat_session(+ session_id: str,+ user_id: str,+) -> PrismaChatSession:+ """Create a new chat session in the database."""+ data = ChatSessionCreateInput(+ id=session_id,+ userId=user_id,+ credentials=SafeJson({}),+ successfulAgentRuns=SafeJson({}),+ successfulAgentSchedules=SafeJson({}),+ )+ return await PrismaChatSession.prisma().create(+ data=data,+ include={"Messages": True},+ )+++async def update_chat_session(+ session_id: str,+ credentials: dict[str, Any] | None = None,+ successful_agent_runs: dict[str, Any] | None = None,+ successful_agent_schedules: dict[str, Any] | None = None,+ total_prompt_tokens: int | None = None,+ total_completion_tokens: int | None = None,+ title: str | None = None,+) -> PrismaChatSession | None:+ """Update a chat session's metadata."""+ data: ChatSessionUpdateInput = {"updatedAt": datetime.now(UTC)}++ if credentials is not None:+ data["credentials"] = SafeJson(credentials)+ if successful_agent_runs is not None:+ data["successfulAgentRuns"] = SafeJson(successful_agent_runs)+ if successful_agent_schedules is not None:+ data["successfulAgentSchedules"] = SafeJson(successful_agent_schedules)+ if total_prompt_tokens is not None:+ data["totalPromptTokens"] = total_prompt_tokens+ if total_completion_tokens is not None:+ data["totalCompletionTokens"] = total_completion_tokens+ if title is not None:+ data["title"] = title++ session = await PrismaChatSession.prisma().update(+ where={"id": session_id},+ data=data,+ include={"Messages": True},+ )+ if session and session.Messages:+ # Sort in Python - Prisma Python doesn't support order_by in include clauses+ session.Messages.sort(key=lambda m: m.sequence)+ return session+++async def add_chat_message(+ session_id: str,+ role: str,+ sequence: int,+ content: str | None = None,+ name: str | None = None,+ tool_call_id: str | None = None,+ refusal: str | None = None,+ tool_calls: list[dict[str, Any]] | None = None,+ function_call: dict[str, Any] | None = None,+) -> PrismaChatMessage:+ """Add a message to a chat session."""+ # Build input dict dynamically rather than using ChatMessageCreateInput directly+ # because Prisma's TypedDict validation rejects optional fields set to None.+ # We only include fields that have values, then cast at the end.+ data: dict[str, Any] = {+ "Session": {"connect": {"id": session_id}},+ "role": role,+ "sequence": sequence,+ }++ # Add optional string fields+ if content is not None:+ data["content"] = content+ if name is not None:+ data["name"] = name+ if tool_call_id is not None:+ data["toolCallId"] = tool_call_id+ if refusal is not None:+ data["refusal"] = refusal++ # Add optional JSON fields only when they have values+ if tool_calls is not None:+ data["toolCalls"] = SafeJson(tool_calls)+ if function_call is not None:+ data["functionCall"] = SafeJson(function_call)++ # Run message create and session timestamp update in parallel for lower latency+ _, message = await asyncio.gather(+ PrismaChatSession.prisma().update(+ where={"id": session_id},+ data={"updatedAt": datetime.now(UTC)},+ ),+ PrismaChatMessage.prisma().create(data=cast(ChatMessageCreateInput, data)),+ )+ return message+++async def add_chat_messages_batch(+ session_id: str,+ messages: list[dict[str, Any]],+ start_sequence: int,+) -> list[PrismaChatMessage]:+ """Add multiple messages to a chat session in a batch.++ Uses a transaction for atomicity - if any message creation fails,+ the entire batch is rolled back.+ """+ if not messages:+ return []++ created_messages = []++ async with transaction() as tx:+ for i, msg in enumerate(messages):+ # Build input dict dynamically rather than using ChatMessageCreateInput+ # directly because Prisma's TypedDict validation rejects optional fields+ # set to None. We only include fields that have values, then cast.+ data: dict[str, Any] = {+ "Session": {"connect": {"id": session_id}},+ "role": msg["role"],+ "sequence": start_sequence + i,+ }++ # Add optional string fields+ if msg.get("content") is not None:+ data["content"] = msg["content"]+ if msg.get("name") is not None:+ data["name"] = msg["name"]+ if msg.get("tool_call_id") is not None:+ data["toolCallId"] = msg["tool_call_id"]+ if msg.get("refusal") is not None:+ data["refusal"] = msg["refusal"]++ # Add optional JSON fields only when they have values+ if msg.get("tool_calls") is not None:+ data["toolCalls"] = SafeJson(msg["tool_calls"])+ if msg.get("function_call") is not None:+ data["functionCall"] = SafeJson(msg["function_call"])++ created = await PrismaChatMessage.prisma(tx).create(+ data=cast(ChatMessageCreateInput, data)+ )+ created_messages.append(created)++ # Update session's updatedAt timestamp within the same transaction.+ # Note: Token usage (total_prompt_tokens, total_completion_tokens) is updated+ # separately via update_chat_session() after streaming completes.+ await PrismaChatSession.prisma(tx).update(+ where={"id": session_id},+ data={"updatedAt": datetime.now(UTC)},+ )++ return created_messages+++async def get_user_chat_sessions(+ user_id: str,+ limit: int = 50,+ offset: int = 0,+) -> list[PrismaChatSession]:+ """Get chat sessions for a user, ordered by most recent."""+ return await PrismaChatSession.prisma().find_many(+ where={"userId": user_id},+ order={"updatedAt": "desc"},+ take=limit,+ skip=offset,+ )+++async def get_user_session_count(user_id: str) -> int:+ """Get the total number of chat sessions for a user."""+ return await PrismaChatSession.prisma().count(where={"userId": user_id})+++async def delete_chat_session(session_id: str, user_id: str | None = None) -> bool:+ """Delete a chat session and all its messages.++ Args:+ session_id: The session ID to delete.+ user_id: If provided, validates that the session belongs to this user+ before deletion. This prevents unauthorized deletion of other+ users' sessions.++ Returns:+ True if deleted successfully, False otherwise.+ """+ try:+ # Build typed where clause with optional user_id validation+ where_clause: ChatSessionWhereInput = {"id": session_id}+ if user_id is not None:+ where_clause["userId"] = user_id++ result = await PrismaChatSession.prisma().delete_many(where=where_clause)+ if result == 0:+ logger.warning(+ f"No session deleted for {session_id} "+ f"(user_id validation: {user_id is not None})"+ )+ return False+ return True+ except Exception as e:+ logger.error(f"Failed to delete chat session {session_id}: {e}")+ return False+++async def get_chat_session_message_count(session_id: str) -> int:+ """Get the number of messages in a chat session."""+ count = await PrismaChatMessage.prisma().count(where={"sessionId": session_id})+ return countdiff --git a/autogpt_platform/backend/backend/api/features/chat/model.py b/autogpt_platform/backend/backend/api/features/chat/model.pyindex b8aea5a334..ec4cf1fc8b 100644--- a/autogpt_platform/backend/backend/api/features/chat/model.py+++ b/autogpt_platform/backend/backend/api/features/chat/model.py@@ -1,6 +1,9 @@+import asyncioimport loggingimport uuidfrom datetime import UTC, datetime+from typing import Any+from weakref import WeakValueDictionaryfrom openai.types.chat import (ChatCompletionAssistantMessageParam,@@ -16,17 +19,63 @@ from openai.types.chat.chat_completion_message_tool_call_param import (ChatCompletionMessageToolCallParam,Function,)+from prisma.models import ChatMessage as PrismaChatMessage+from prisma.models import ChatSession as PrismaChatSessionfrom pydantic import BaseModelfrom backend.data.redis_client import get_redis_async-from backend.util.exceptions import RedisError+from backend.util import json+from backend.util.exceptions import DatabaseError, RedisError+from . import db as chat_dbfrom .config import ChatConfiglogger = logging.getLogger(__name__)config = ChatConfig()+def _parse_json_field(value: str | dict | list | None, default: Any = None) -> Any:+ """Parse a JSON field that may be stored as string or already parsed."""+ if value is None:+ return default+ if isinstance(value, str):+ return json.loads(value)+ return value+++# Redis cache key prefix for chat sessions+CHAT_SESSION_CACHE_PREFIX = "chat:session:"+++def _get_session_cache_key(session_id: str) -> str:+ """Get the Redis cache key for a chat session."""+ return f"{CHAT_SESSION_CACHE_PREFIX}{session_id}"+++# Session-level locks to prevent race conditions during concurrent upserts.+# Uses WeakValueDictionary to automatically garbage collect locks when no longer referenced,+# preventing unbounded memory growth while maintaining lock semantics for active sessions.+# Invalidation: Locks are auto-removed by GC when no coroutine holds a reference (after+# async with lock: completes). Explicit cleanup also occurs in delete_chat_session().+_session_locks: WeakValueDictionary[str, asyncio.Lock] = WeakValueDictionary()+_session_locks_mutex = asyncio.Lock()+++async def _get_session_lock(session_id: str) -> asyncio.Lock:+ """Get or create a lock for a specific session to prevent concurrent upserts.++ Uses WeakValueDictionary for automatic cleanup: locks are garbage collected+ when no coroutine holds a reference to them, preventing memory leaks from+ unbounded growth of session locks.+ """+ async with _session_locks_mutex:+ lock = _session_locks.get(session_id)+ if lock is None:+ lock = asyncio.Lock()+ _session_locks[session_id] = lock+ return lock++class ChatMessage(BaseModel):role: strcontent: str | None = None@@ -45,7 +94,8 @@ class Usage(BaseModel):class ChatSession(BaseModel):session_id: str- user_id: str | None+ user_id: str+ title: str | None = Nonemessages: list[ChatMessage]usage: list[Usage]credentials: dict[str, dict] = {} # Map of provider -> credential metadata@@ -55,10 +105,11 @@ class ChatSession(BaseModel):successful_agent_schedules: dict[str, int] = {}@staticmethod- def new(user_id: str | None) -> "ChatSession":+ def new(user_id: str) -> "ChatSession":return ChatSession(session_id=str(uuid.uuid4()),user_id=user_id,+ title=None,messages=[],usage=[],credentials={},@@ -66,6 +117,61 @@ class ChatSession(BaseModel):updated_at=datetime.now(UTC),)+ @staticmethod+ def from_db(+ prisma_session: PrismaChatSession,+ prisma_messages: list[PrismaChatMessage] | None = None,+ ) -> "ChatSession":+ """Convert Prisma models to Pydantic ChatSession."""+ messages = []+ if prisma_messages:+ for msg in prisma_messages:+ messages.append(+ ChatMessage(+ role=msg.role,+ content=msg.content,+ name=msg.name,+ tool_call_id=msg.toolCallId,+ refusal=msg.refusal,+ tool_calls=_parse_json_field(msg.toolCalls),+ function_call=_parse_json_field(msg.functionCall),+ )+ )++ # Parse JSON fields from Prisma+ credentials = _parse_json_field(prisma_session.credentials, default={})+ successful_agent_runs = _parse_json_field(+ prisma_session.successfulAgentRuns, default={}+ )+ successful_agent_schedules = _parse_json_field(+ prisma_session.successfulAgentSchedules, default={}+ )++ # Calculate usage from token counts+ usage = []+ if prisma_session.totalPromptTokens or prisma_session.totalCompletionTokens:+ usage.append(+ Usage(+ prompt_tokens=prisma_session.totalPromptTokens or 0,+ completion_tokens=prisma_session.totalCompletionTokens or 0,+ total_tokens=(prisma_session.totalPromptTokens or 0)+ + (prisma_session.totalCompletionTokens or 0),+ )+ )++ return ChatSession(+ session_id=prisma_session.id,+ user_id=prisma_session.userId,+ title=prisma_session.title,+ messages=messages,+ usage=usage,+ credentials=credentials,+ started_at=prisma_session.createdAt,+ updated_at=prisma_session.updatedAt,+ successful_agent_runs=successful_agent_runs,+ successful_agent_schedules=successful_agent_schedules,+ )+def to_openai_messages(self) -> list[ChatCompletionMessageParam]:messages = []for message in self.messages:@@ -155,50 +261,337 @@ class ChatSession(BaseModel):return messages-async def get_chat_session(- session_id: str,- user_id: str | None,-) -> ChatSession | None:- """Get a chat session by ID."""- redis_key = f"chat:session:{session_id}"+async def _get_session_from_cache(session_id: str) -> ChatSession | None:+ """Get a chat session from Redis cache."""+ redis_key = _get_session_cache_key(session_id)async_redis = await get_redis_async()-raw_session: bytes | None = await async_redis.get(redis_key)if raw_session is None:- logger.warning(f"Session {session_id} not found in Redis")return Nonetry:session = ChatSession.model_validate_json(raw_session)+ logger.info(+ f"Loading session {session_id} from cache: "+ f"message_count={len(session.messages)}, "+ f"roles={[m.role for m in session.messages]}"+ )+ return sessionexcept Exception as e:logger.error(f"Failed to deserialize session {session_id}: {e}", exc_info=True)raise RedisError(f"Corrupted session data for {session_id}") from e- if session.user_id is not None and session.user_id != user_id:++async def _cache_session(session: ChatSession) -> None:+ """Cache a chat session in Redis."""+ redis_key = _get_session_cache_key(session.session_id)+ async_redis = await get_redis_async()+ await async_redis.setex(redis_key, config.session_ttl, session.model_dump_json())+++async def _get_session_from_db(session_id: str) -> ChatSession | None:+ """Get a chat session from the database."""+ prisma_session = await chat_db.get_chat_session(session_id)+ if not prisma_session:+ return None++ messages = prisma_session.Messages+ logger.info(+ f"Loading session {session_id} from DB: "+ f"has_messages={messages is not None}, "+ f"message_count={len(messages) if messages else 0}, "+ f"roles={[m.role for m in messages] if messages else []}"+ )++ return ChatSession.from_db(prisma_session, messages)+++async def _save_session_to_db(+ session: ChatSession, existing_message_count: int+) -> None:+ """Save or update a chat session in the database."""+ # Check if session exists in DB+ existing = await chat_db.get_chat_session(session.session_id)++ if not existing:+ # Create new session+ await chat_db.create_chat_session(+ session_id=session.session_id,+ user_id=session.user_id,+ )+ existing_message_count = 0++ # Calculate total tokens from usage+ total_prompt = sum(u.prompt_tokens for u in session.usage)+ total_completion = sum(u.completion_tokens for u in session.usage)++ # Update session metadata+ await chat_db.update_chat_session(+ session_id=session.session_id,+ credentials=session.credentials,+ successful_agent_runs=session.successful_agent_runs,+ successful_agent_schedules=session.successful_agent_schedules,+ total_prompt_tokens=total_prompt,+ total_completion_tokens=total_completion,+ )++ # Add new messages (only those after existing count)+ new_messages = session.messages[existing_message_count:]+ if new_messages:+ messages_data = []+ for msg in new_messages:+ messages_data.append(+ {+ "role": msg.role,+ "content": msg.content,+ "name": msg.name,+ "tool_call_id": msg.tool_call_id,+ "refusal": msg.refusal,+ "tool_calls": msg.tool_calls,+ "function_call": msg.function_call,+ }+ )+ logger.info(+ f"Saving {len(new_messages)} new messages to DB for session {session.session_id}: "+ f"roles={[m['role'] for m in messages_data]}, "+ f"start_sequence={existing_message_count}"+ )+ await chat_db.add_chat_messages_batch(+ session_id=session.session_id,+ messages=messages_data,+ start_sequence=existing_message_count,+ )+++async def get_chat_session(+ session_id: str,+ user_id: str | None = None,+) -> ChatSession | None:+ """Get a chat session by ID.++ Checks Redis cache first, falls back to database if not found.+ Caches database results back to Redis.++ Args:+ session_id: The session ID to fetch.+ user_id: If provided, validates that the session belongs to this user.+ If None, ownership is not validated (admin/system access).+ """+ # Try cache first+ try:+ session = await _get_session_from_cache(session_id)+ if session:+ # Verify user ownership if user_id was provided for validation+ if user_id is not None and session.user_id != user_id:+ logger.warning(+ f"Session {session_id} user id mismatch: {session.user_id} != {user_id}"+ )+ return None+ return session+ except RedisError:+ logger.warning(f"Cache error for session {session_id}, trying database")+ except Exception as e:+ logger.warning(f"Unexpected cache error for session {session_id}: {e}")++ # Fall back to database+ logger.info(f"Session {session_id} not in cache, checking database")+ session = await _get_session_from_db(session_id)++ if session is None:+ logger.warning(f"Session {session_id} not found in cache or database")+ return None++ # Verify user ownership if user_id was provided for validation+ if user_id is not None and session.user_id != user_id:logger.warning(f"Session {session_id} user id mismatch: {session.user_id} != {user_id}")return None+ # Cache the session from DB+ try:+ await _cache_session(session)+ logger.info(f"Cached session {session_id} from database")+ except Exception as e:+ logger.warning(f"Failed to cache session {session_id}: {e}")+return sessionasync def upsert_chat_session(session: ChatSession,) -> ChatSession:- """Update a chat session with the given messages."""+ """Update a chat session in both cache and database.- redis_key = f"chat:session:{session.session_id}"+ Uses session-level locking to prevent race conditions when concurrent+ operations (e.g., background title update and main stream handler)+ attempt to upsert the same session simultaneously.- async_redis = await get_redis_async()- resp = await async_redis.setex(- redis_key, config.session_ttl, session.model_dump_json()- )+ Raises:+ DatabaseError: If the database write fails. The cache is still updated+ as a best-effort optimization, but the error is propagated to ensure+ callers are aware of the persistence failure.+ RedisError: If the cache write fails (after successful DB write).+ """+ # Acquire session-specific lock to prevent concurrent upserts+ lock = await _get_session_lock(session.session_id)- if not resp:- raise RedisError(- f"Failed to persist chat session {session.session_id} to Redis: {resp}"+ async with lock:+ # Get existing message count from DB for incremental saves+ existing_message_count = await chat_db.get_chat_session_message_count(+ session.session_id)+ db_error: Exception | None = None++ # Save to database (primary storage)+ try:+ await _save_session_to_db(session, existing_message_count)+ except Exception as e:+ logger.error(+ f"Failed to save session {session.session_id} to database: {e}"+ )+ db_error = e++ # Save to cache (best-effort, even if DB failed)+ try:+ await _cache_session(session)+ except Exception as e:+ # If DB succeeded but cache failed, raise cache error+ if db_error is None:+ raise RedisError(+ f"Failed to persist chat session {session.session_id} to Redis: {e}"+ ) from e+ # If both failed, log cache error but raise DB error (more critical)+ logger.warning(+ f"Cache write also failed for session {session.session_id}: {e}"+ )++ # Propagate DB error after attempting cache (prevents data loss)+ if db_error is not None:+ raise DatabaseError(+ f"Failed to persist chat session {session.session_id} to database"+ ) from db_error++ return session+++async def create_chat_session(user_id: str) -> ChatSession:+ """Create a new chat session and persist it.++ Raises:+ DatabaseError: If the database write fails. We fail fast to ensure+ callers never receive a non-persisted session that only exists+ in cache (which would be lost when the cache expires).+ """+ session = ChatSession.new(user_id)++ # Create in database first - fail fast if this fails+ try:+ await chat_db.create_chat_session(+ session_id=session.session_id,+ user_id=user_id,+ )+ except Exception as e:+ logger.error(f"Failed to create session {session.session_id} in database: {e}")+ raise DatabaseError(+ f"Failed to create chat session {session.session_id} in database"+ ) from e++ # Cache the session (best-effort optimization, DB is source of truth)+ try:+ await _cache_session(session)+ except Exception as e:+ logger.warning(f"Failed to cache new session {session.session_id}: {e}")+return session+++async def get_user_sessions(+ user_id: str,+ limit: int = 50,+ offset: int = 0,+) -> tuple[list[ChatSession], int]:+ """Get chat sessions for a user from the database with total count.++ Returns:+ A tuple of (sessions, total_count) where total_count is the overall+ number of sessions for the user (not just the current page).+ """+ prisma_sessions = await chat_db.get_user_chat_sessions(user_id, limit, offset)+ total_count = await chat_db.get_user_session_count(user_id)++ sessions = []+ for prisma_session in prisma_sessions:+ # Convert without messages for listing (lighter weight)+ sessions.append(ChatSession.from_db(prisma_session, None))++ return sessions, total_count+++async def delete_chat_session(session_id: str, user_id: str | None = None) -> bool:+ """Delete a chat session from both cache and database.++ Args:+ session_id: The session ID to delete.+ user_id: If provided, validates that the session belongs to this user+ before deletion. This prevents unauthorized deletion.++ Returns:+ True if deleted successfully, False otherwise.+ """+ # Delete from database first (with optional user_id validation)+ # This confirms ownership before invalidating cache+ deleted = await chat_db.delete_chat_session(session_id, user_id)++ if not deleted:+ return False++ # Only invalidate cache and clean up lock after DB confirms deletion+ try:+ redis_key = _get_session_cache_key(session_id)+ async_redis = await get_redis_async()+ await async_redis.delete(redis_key)+ except Exception as e:+ logger.warning(f"Failed to delete session {session_id} from cache: {e}")++ # Clean up session lock (belt-and-suspenders with WeakValueDictionary)+ async with _session_locks_mutex:+ _session_locks.pop(session_id, None)++ return True+++async def update_session_title(session_id: str, title: str) -> bool:+ """Update only the title of a chat session.++ This is a lightweight operation that doesn't touch messages, avoiding+ race conditions with concurrent message updates. Use this for background+ title generation instead of upsert_chat_session.++ Args:+ session_id: The session ID to update.+ title: The new title to set.++ Returns:+ True if updated successfully, False otherwise.+ """+ try:+ result = await chat_db.update_chat_session(session_id=session_id, title=title)+ if result is None:+ logger.warning(f"Session {session_id} not found for title update")+ return False++ # Invalidate cache so next fetch gets updated title+ try:+ redis_key = _get_session_cache_key(session_id)+ async_redis = await get_redis_async()+ await async_redis.delete(redis_key)+ except Exception as e:+ logger.warning(f"Failed to invalidate cache for session {session_id}: {e}")++ return True+ except Exception as e:+ logger.error(f"Failed to update title for session {session_id}: {e}")+ return Falsediff --git a/autogpt_platform/backend/backend/api/features/chat/model_test.py b/autogpt_platform/backend/backend/api/features/chat/model_test.pyindex b7f4c8a493..c230b00f9c 100644--- a/autogpt_platform/backend/backend/api/features/chat/model_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/model_test.py@@ -43,9 +43,9 @@ async def test_chatsession_serialization_deserialization():@pytest.mark.asyncio(loop_scope="session")-async def test_chatsession_redis_storage():+async def test_chatsession_redis_storage(setup_test_user, test_user_id):- s = ChatSession.new(user_id=None)+ s = ChatSession.new(user_id=test_user_id)s.messages = messagess = await upsert_chat_session(s)@@ -59,12 +59,61 @@ async def test_chatsession_redis_storage():@pytest.mark.asyncio(loop_scope="session")-async def test_chatsession_redis_storage_user_id_mismatch():+async def test_chatsession_redis_storage_user_id_mismatch(+ setup_test_user, test_user_id+):- s = ChatSession.new(user_id="abc123")+ s = ChatSession.new(user_id=test_user_id)s.messages = messagess = await upsert_chat_session(s)- s2 = await get_chat_session(s.session_id, None)+ s2 = await get_chat_session(s.session_id, "different_user_id")assert s2 is None++[email protected](loop_scope="session")+async def test_chatsession_db_storage(setup_test_user, test_user_id):+ """Test that messages are correctly saved to and loaded from DB (not cache)."""+ from backend.data.redis_client import get_redis_async++ # Create session with messages including assistant message+ s = ChatSession.new(user_id=test_user_id)+ s.messages = messages # Contains user, assistant, and tool messages+ assert s.session_id is not None, "Session id is not set"+ # Upsert to save to both cache and DB+ s = await upsert_chat_session(s)++ # Clear the Redis cache to force DB load+ redis_key = f"chat:session:{s.session_id}"+ async_redis = await get_redis_async()+ await async_redis.delete(redis_key)++ # Load from DB (cache was cleared)+ s2 = await get_chat_session(+ session_id=s.session_id,+ user_id=s.user_id,+ )++ assert s2 is not None, "Session not found after loading from DB"+ assert len(s2.messages) == len(+ s.messages+ ), f"Message count mismatch: expected {len(s.messages)}, got {len(s2.messages)}"++ # Verify all roles are present+ roles = [m.role for m in s2.messages]+ assert "user" in roles, f"User message missing. Roles found: {roles}"+ assert "assistant" in roles, f"Assistant message missing. Roles found: {roles}"+ assert "tool" in roles, f"Tool message missing. Roles found: {roles}"++ # Verify message content+ for orig, loaded in zip(s.messages, s2.messages):+ assert orig.role == loaded.role, f"Role mismatch: {orig.role} != {loaded.role}"+ assert (+ orig.content == loaded.content+ ), f"Content mismatch for {orig.role}: {orig.content} != {loaded.content}"+ if orig.tool_calls:+ assert (+ loaded.tool_calls is not None+ ), f"Tool calls missing for {orig.role} message"+ assert len(orig.tool_calls) == len(loaded.tool_calls)diff --git a/autogpt_platform/backend/backend/api/features/chat/prompts/chat_system.md b/autogpt_platform/backend/backend/api/features/chat/prompts/chat_system.mddeleted file mode 100644index a660ca805e..0000000000--- a/autogpt_platform/backend/backend/api/features/chat/prompts/chat_system.md+++ /dev/null@@ -1,104 +0,0 @@-You are Otto, an AI Co-Pilot and Forward Deployed Engineer for AutoGPT, an AI Business Automation tool. Your mission is to help users quickly find and set up AutoGPT agents to solve their business problems.--Here are the functions available to you:--<functions>-1. **find_agent** - Search for agents that solve the user's problem-2. **run_agent** - Run or schedule an agent (automatically handles setup)-</functions>--## HOW run_agent WORKS--The `run_agent` tool automatically handles the entire setup flow:--1. **First call** (no inputs) → Returns available inputs so user can decide what values to use-2. **Credentials check** → If missing, UI automatically prompts user to add them (you don't need to mention this)-3. **Execution** → Runs when you provide `inputs` OR set `use_defaults=true`--Parameters:-- `username_agent_slug` (required): Agent identifier like "creator/agent-name"-- `inputs`: Object with input values for the agent-- `use_defaults`: Set to `true` to run with default values (only after user confirms)-- `schedule_name` + `cron`: For scheduled execution--## WORKFLOW--1. **find_agent** - Search for agents that solve the user's problem-2. **run_agent** (first call, no inputs) - Get available inputs for the agent-3. **Ask user** what values they want to use OR if they want to use defaults-4. **run_agent** (second call) - Either with `inputs={...}` or `use_defaults=true`--## YOUR APPROACH--**Step 1: Understand the Problem**-- Ask maximum 1-2 targeted questions-- Focus on: What business problem are they solving?-- Move quickly to searching for solutions--**Step 2: Find Agents**-- Use `find_agent` immediately with relevant keywords-- Suggest the best option from search results-- Explain briefly how it solves their problem--**Step 3: Get Agent Inputs**-- Call `run_agent(username_agent_slug="creator/agent-name")` without inputs-- This returns the available inputs (required and optional)-- Present these to the user and ask what values they want--**Step 4: Run with User's Choice**-- If user provides values: `run_agent(username_agent_slug="...", inputs={...})`-- If user says "use defaults": `run_agent(username_agent_slug="...", use_defaults=true)`-- On success, share the agent link with the user--**For Scheduled Execution:**-- Add `schedule_name` and `cron` parameters-- Example: `run_agent(username_agent_slug="...", inputs={...}, schedule_name="Daily Report", cron="0 9 * * *")`--## FUNCTION CALL FORMAT--To call a function, use this exact format:-`<function_call>function_name(parameter="value")</function_call>`--Examples:-- `<function_call>find_agent(query="social media automation")</function_call>`-- `<function_call>run_agent(username_agent_slug="creator/agent-name")</function_call>` (get inputs)-- `<function_call>run_agent(username_agent_slug="creator/agent-name", inputs={"topic": "AI news"})</function_call>`-- `<function_call>run_agent(username_agent_slug="creator/agent-name", use_defaults=true)</function_call>`--## KEY RULES--**What You DON'T Do:**-- Don't help with login (frontend handles this)-- Don't mention or explain credentials to the user (frontend handles this automatically)-- Don't run agents without first showing available inputs to the user-- Don't use `use_defaults=true` without user explicitly confirming-- Don't write responses longer than 3 sentences--**What You DO:**-- Always call run_agent first without inputs to see what's available-- Ask user what values they want OR if they want to use defaults-- Keep all responses to maximum 3 sentences-- Include the agent link in your response after successful execution--**Error Handling:**-- Authentication needed → "Please sign in via the interface"-- Credentials missing → The UI handles this automatically. Focus on asking the user about input values instead.--## RESPONSE STRUCTURE--Before responding, wrap your analysis in <thinking> tags to systematically plan your approach:-- Extract the key business problem or request from the user's message-- Determine what function call (if any) you need to make next-- Plan your response to stay under the 3-sentence maximum--Example interaction:-```-User: "Run the AI news agent for me"-Otto: <function_call>run_agent(username_agent_slug="autogpt/ai-news")</function_call>-[Tool returns: Agent accepts inputs - Required: topic. Optional: num_articles (default: 5)]-Otto: The AI News agent needs a topic. What topic would you like news about, or should I use the defaults?-User: "Use defaults"-Otto: <function_call>run_agent(username_agent_slug="autogpt/ai-news", use_defaults=true)</function_call>-```--KEEP ANSWERS TO 3 SENTENCESdiff --git a/autogpt_platform/backend/backend/api/features/chat/response_model.py b/autogpt_platform/backend/backend/api/features/chat/response_model.pyindex 2d38820bd5..49a9b38e8f 100644--- a/autogpt_platform/backend/backend/api/features/chat/response_model.py+++ b/autogpt_platform/backend/backend/api/features/chat/response_model.py@@ -1,3 +1,10 @@+"""+Response models for Vercel AI SDK UI Stream Protocol.++This module implements the AI SDK UI Stream Protocol (v1) for streaming chat responses.+See: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol+"""+from enum import Enumfrom typing import Any@@ -5,97 +12,133 @@ from pydantic import BaseModel, Fieldclass ResponseType(str, Enum):- """Types of streaming responses."""+ """Types of streaming responses following AI SDK protocol."""++ # Message lifecycle+ START = "start"+ FINISH = "finish"++ # Text streaming+ TEXT_START = "text-start"+ TEXT_DELTA = "text-delta"+ TEXT_END = "text-end"++ # Tool interaction+ TOOL_INPUT_START = "tool-input-start"+ TOOL_INPUT_AVAILABLE = "tool-input-available"+ TOOL_OUTPUT_AVAILABLE = "tool-output-available"- TEXT_CHUNK = "text_chunk"- TEXT_ENDED = "text_ended"- TOOL_CALL = "tool_call"- TOOL_CALL_START = "tool_call_start"- TOOL_RESPONSE = "tool_response"+ # OtherERROR = "error"USAGE = "usage"- STREAM_END = "stream_end"class StreamBaseResponse(BaseModel):"""Base response model for all streaming responses."""type: ResponseType- timestamp: str | None = Nonedef to_sse(self) -> str:"""Convert to SSE format."""return f"data: {self.model_dump_json()}\n\n"-class StreamTextChunk(StreamBaseResponse):- """Streaming text content from the assistant."""+# ========== Message Lifecycle ==========+++class StreamStart(StreamBaseResponse):+ """Start of a new message."""++ type: ResponseType = ResponseType.START+ messageId: str = Field(..., description="Unique message ID")+++class StreamFinish(StreamBaseResponse):+ """End of message/stream."""++ type: ResponseType = ResponseType.FINISH+++# ========== Text Streaming ==========+++class StreamTextStart(StreamBaseResponse):+ """Start of a text block."""++ type: ResponseType = ResponseType.TEXT_START+ id: str = Field(..., description="Text block ID")+- type: ResponseType = ResponseType.TEXT_CHUNK- content: str = Field(..., description="Text content chunk")+class StreamTextDelta(StreamBaseResponse):+ """Streaming text content delta."""+ type: ResponseType = ResponseType.TEXT_DELTA+ id: str = Field(..., description="Text block ID")+ delta: str = Field(..., description="Text content delta")-class StreamToolCallStart(StreamBaseResponse):++class StreamTextEnd(StreamBaseResponse):+ """End of a text block."""++ type: ResponseType = ResponseType.TEXT_END+ id: str = Field(..., description="Text block ID")+++# ========== Tool Interaction ==========+++class StreamToolInputStart(StreamBaseResponse):"""Tool call started notification."""- type: ResponseType = ResponseType.TOOL_CALL_START- tool_name: str = Field(..., description="Name of the tool that was executed")- tool_id: str = Field(..., description="Unique tool call ID")+ type: ResponseType = ResponseType.TOOL_INPUT_START+ toolCallId: str = Field(..., description="Unique tool call ID")+ toolName: str = Field(..., description="Name of the tool being called")-class StreamToolCall(StreamBaseResponse):- """Tool invocation notification."""+class StreamToolInputAvailable(StreamBaseResponse):+ """Tool input is ready for execution."""- type: ResponseType = ResponseType.TOOL_CALL- tool_id: str = Field(..., description="Unique tool call ID")- tool_name: str = Field(..., description="Name of the tool being called")- arguments: dict[str, Any] = Field(- default_factory=dict, description="Tool arguments"+ type: ResponseType = ResponseType.TOOL_INPUT_AVAILABLE+ toolCallId: str = Field(..., description="Unique tool call ID")+ toolName: str = Field(..., description="Name of the tool being called")+ input: dict[str, Any] = Field(+ default_factory=dict, description="Tool input arguments")-class StreamToolExecutionResult(StreamBaseResponse):+class StreamToolOutputAvailable(StreamBaseResponse):"""Tool execution result."""- type: ResponseType = ResponseType.TOOL_RESPONSE- tool_id: str = Field(..., description="Tool call ID this responds to")- tool_name: str = Field(..., description="Name of the tool that was executed")- result: str | dict[str, Any] = Field(..., description="Tool execution result")+ type: ResponseType = ResponseType.TOOL_OUTPUT_AVAILABLE+ toolCallId: str = Field(..., description="Tool call ID this responds to")+ output: str | dict[str, Any] = Field(..., description="Tool execution output")+ # Additional fields for internal use (not part of AI SDK spec but useful)+ toolName: str | None = Field(+ default=None, description="Name of the tool that was executed"+ )success: bool = Field(default=True, description="Whether the tool execution succeeded")+# ========== Other ==========++class StreamUsage(StreamBaseResponse):"""Token usage statistics."""type: ResponseType = ResponseType.USAGE- prompt_tokens: int- completion_tokens: int- total_tokens: int+ promptTokens: int = Field(..., description="Number of prompt tokens")+ completionTokens: int = Field(..., description="Number of completion tokens")+ totalTokens: int = Field(..., description="Total number of tokens")class StreamError(StreamBaseResponse):"""Error response."""type: ResponseType = ResponseType.ERROR- message: str = Field(..., description="Error message")+ errorText: str = Field(..., description="Error message text")code: str | None = Field(default=None, description="Error code")details: dict[str, Any] | None = Field(default=None, description="Additional error details")---class StreamTextEnded(StreamBaseResponse):- """Text streaming completed marker."""-- type: ResponseType = ResponseType.TEXT_ENDED---class StreamEnd(StreamBaseResponse):- """End of stream marker."""-- type: ResponseType = ResponseType.STREAM_END- summary: dict[str, Any] | None = Field(- default=None, description="Stream summary statistics"- )diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 667335d048..58b017ad5e 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -13,12 +13,25 @@ from backend.util.exceptions import NotFoundErrorfrom . import service as chat_servicefrom .config import ChatConfig+from .model import ChatSession, create_chat_session, get_chat_session, get_user_sessionsconfig = ChatConfig()logger = logging.getLogger(__name__)++async def _validate_and_get_session(+ session_id: str,+ user_id: str | None,+) -> ChatSession:+ """Validate session exists and belongs to user."""+ session = await get_chat_session(session_id, user_id)+ if not session:+ raise NotFoundError(f"Session {session_id} not found.")+ return session++router = APIRouter(tags=["chat"],)@@ -26,6 +39,14 @@ router = APIRouter(# ========== Request/Response Models ==========+class StreamChatRequest(BaseModel):+ """Request model for streaming chat with optional context."""++ message: str+ is_user_message: bool = True+ context: dict[str, str] | None = None # {url: str, content: str}++class CreateSessionResponse(BaseModel):"""Response model containing information on a newly created chat session."""@@ -44,22 +65,77 @@ class SessionDetailResponse(BaseModel):messages: list[dict]+class SessionSummaryResponse(BaseModel):+ """Response model for a session summary (without messages)."""++ id: str+ created_at: str+ updated_at: str+ title: str | None = None+++class ListSessionsResponse(BaseModel):+ """Response model for listing chat sessions."""++ sessions: list[SessionSummaryResponse]+ total: int++# ========== Routes ==========+ "/sessions",+ dependencies=[Security(auth.requires_user)],+)+async def list_sessions(+ user_id: Annotated[str, Security(auth.get_user_id)],+ limit: int = Query(default=50, ge=1, le=100),+ offset: int = Query(default=0, ge=0),+) -> ListSessionsResponse:+ """+ List chat sessions for the authenticated user.++ Returns a paginated list of chat sessions belonging to the current user,+ ordered by most recently updated.++ Args:+ user_id: The authenticated user's ID.+ limit: Maximum number of sessions to return (1-100).+ offset: Number of sessions to skip for pagination.++ Returns:+ ListSessionsResponse: List of session summaries and total count.+ """+ sessions, total_count = await get_user_sessions(user_id, limit, offset)++ return ListSessionsResponse(+ sessions=[+ SessionSummaryResponse(+ id=session.session_id,+ created_at=session.started_at.isoformat(),+ updated_at=session.updated_at.isoformat(),+ title=session.title,+ )+ for session in sessions+ ],+ total=total_count,+ )++@router.post("/sessions",)async def create_session(- user_id: Annotated[str | None, Depends(auth.get_user_id)],+ user_id: Annotated[str, Depends(auth.get_user_id)],) -> CreateSessionResponse:"""Create a new chat session.- Initiates a new chat session for either an authenticated or anonymous user.+ Initiates a new chat session for the authenticated user.Args:- user_id: The optional authenticated user ID parsed from the JWT. If missing, creates an anonymous session.+ user_id: The authenticated user ID parsed from the JWT (required).Returns:CreateSessionResponse: Details of the created session.@@ -67,15 +143,15 @@ async def create_session("""logger.info(f"Creating session with user_id: "- f"...{user_id[-8:] if user_id and len(user_id) > 8 else '<redacted>'}"+ f"...{user_id[-8:] if len(user_id) > 8 else '<redacted>'}")- session = await chat_service.create_chat_session(user_id)+ session = await create_chat_session(user_id)return CreateSessionResponse(id=session.session_id,created_at=session.started_at.isoformat(),- user_id=session.user_id or None,+ user_id=session.user_id,)@@ -99,29 +175,88 @@ async def get_session(SessionDetailResponse: Details for the requested session; raises NotFoundError if not found."""- session = await chat_service.get_session(session_id, user_id)+ session = await get_chat_session(session_id, user_id)if not session:raise NotFoundError(f"Session {session_id} not found")++ messages = [message.model_dump() for message in session.messages]+ logger.info(+ f"Returning session {session_id}: "+ f"message_count={len(messages)}, "+ f"roles={[m.get('role') for m in messages]}"+ )+return SessionDetailResponse(id=session.session_id,created_at=session.started_at.isoformat(),updated_at=session.updated_at.isoformat(),user_id=session.user_id or None,- messages=[message.model_dump() for message in session.messages],+ messages=messages,+ )+++ "/sessions/{session_id}/stream",+)+async def stream_chat_post(+ session_id: str,+ request: StreamChatRequest,+ user_id: str | None = Depends(auth.get_user_id),+):+ """+ Stream chat responses for a session (POST with context support).++ Streams the AI/completion responses in real time over Server-Sent Events (SSE), including:+ - Text fragments as they are generated+ - Tool call UI elements (if invoked)+ - Tool execution results++ Args:+ session_id: The chat session identifier to associate with the streamed messages.+ request: Request body containing message, is_user_message, and optional context.+ user_id: Optional authenticated user ID.+ Returns:+ StreamingResponse: SSE-formatted response chunks.++ """+ session = await _validate_and_get_session(session_id, user_id)++ async def event_generator() -> AsyncGenerator[str, None]:+ async for chunk in chat_service.stream_chat_completion(+ session_id,+ request.message,+ is_user_message=request.is_user_message,+ user_id=user_id,+ session=session, # Pass pre-fetched session to avoid double-fetch+ context=request.context,+ ):+ yield chunk.to_sse()+ # AI SDK protocol termination+ yield "data: [DONE]\n\n"++ return StreamingResponse(+ event_generator(),+ media_type="text/event-stream",+ headers={+ "Cache-Control": "no-cache",+ "Connection": "keep-alive",+ "X-Accel-Buffering": "no", # Disable nginx buffering+ "x-vercel-ai-ui-message-stream": "v1", # AI SDK protocol header+ },)@router.get("/sessions/{session_id}/stream",)-async def stream_chat(+async def stream_chat_get(session_id: str,message: Annotated[str, Query(min_length=1, max_length=10000)],user_id: str | None = Depends(auth.get_user_id),is_user_message: bool = Query(default=True),):"""- Stream chat responses for a session.+ Stream chat responses for a session (GET - legacy endpoint).Streams the AI/completion responses in real time over Server-Sent Events (SSE), including:- Text fragments as they are generated@@ -137,14 +272,7 @@ async def stream_chat(StreamingResponse: SSE-formatted response chunks."""- # Validate session exists before starting the stream- # This prevents errors after the response has already started- session = await chat_service.get_session(session_id, user_id)-- if not session:- raise NotFoundError(f"Session {session_id} not found. ")- if session.user_id is None and user_id is not None:- session = await chat_service.assign_user_to_session(session_id, user_id)+ session = await _validate_and_get_session(session_id, user_id)async def event_generator() -> AsyncGenerator[str, None]:async for chunk in chat_service.stream_chat_completion(@@ -155,6 +283,8 @@ async def stream_chat(session=session, # Pass pre-fetched session to avoid double-fetch):yield chunk.to_sse()+ # AI SDK protocol termination+ yield "data: [DONE]\n\n"return StreamingResponse(event_generator(),@@ -163,6 +293,7 @@ async def stream_chat("Cache-Control": "no-cache","Connection": "keep-alive","X-Accel-Buffering": "no", # Disable nginx buffering+ "x-vercel-ai-ui-message-stream": "v1", # AI SDK protocol header},)@@ -201,16 +332,28 @@ async def health_check() -> dict:"""Health check endpoint for the chat service.- Performs a full cycle test of session creation, assignment, and retrieval. Should always return healthy+ Performs a full cycle test of session creation and retrieval. Should always return healthyif the service and data layer are operational.Returns:dict: A status dictionary indicating health, service name, and API version."""- session = await chat_service.create_chat_session(None)- await chat_service.assign_user_to_session(session.session_id, "test_user")- await chat_service.get_session(session.session_id, "test_user")+ from backend.data.user import get_or_create_user++ # Ensure health check user exists (required for FK constraint)+ health_check_user_id = "health-check-user"+ await get_or_create_user(+ {+ "sub": health_check_user_id,+ "email": "[email protected]",+ "user_metadata": {"name": "Health Check User"},+ }+ )++ # Create and retrieve session to verify full data layer+ session = await create_chat_session(health_check_user_id)+ await get_chat_session(session.session_id, health_check_user_id)return {"status": "healthy",diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.pyindex 2d96d4abcd..b0271f8a80 100644--- a/autogpt_platform/backend/backend/api/features/chat/service.py+++ b/autogpt_platform/backend/backend/api/features/chat/service.py@@ -1,31 +1,47 @@+import asyncioimport loggingfrom collections.abc import AsyncGenerator-from datetime import UTC, datetimefrom typing import Anyimport orjson-from openai import AsyncOpenAI+from langfuse import Langfuse+from openai import (+ APIConnectionError,+ APIError,+ APIStatusError,+ AsyncOpenAI,+ RateLimitError,+)from openai.types.chat import ChatCompletionChunk, ChatCompletionToolParam+from backend.data.understanding import (+ format_understanding_for_prompt,+ get_business_understanding,+)from backend.util.exceptions import NotFoundError+from backend.util.settings import Settings+from . import db as chat_dbfrom .config import ChatConfigfrom .model import (ChatMessage,ChatSession,Usage,get_chat_session,+ update_session_title,upsert_chat_session,)from .response_model import (StreamBaseResponse,- StreamEnd,StreamError,- StreamTextChunk,- StreamTextEnded,- StreamToolCall,- StreamToolCallStart,- StreamToolExecutionResult,+ StreamFinish,+ StreamStart,+ StreamTextDelta,+ StreamTextEnd,+ StreamTextStart,+ StreamToolInputAvailable,+ StreamToolInputStart,+ StreamToolOutputAvailable,StreamUsage,)from .tools import execute_tool, tools@@ -33,28 +49,155 @@ from .tools import execute_tool, toolslogger = logging.getLogger(__name__)config = ChatConfig()+settings = Settings()client = AsyncOpenAI(api_key=config.api_key, base_url=config.base_url)+# Langfuse client (lazy initialization)+_langfuse_client: Langfuse | None = None-async def create_chat_session(- user_id: str | None = None,-) -> ChatSession:++class LangfuseNotConfiguredError(Exception):+ """Raised when Langfuse is required but not configured."""++ pass+++def _is_langfuse_configured() -> bool:+ """Check if Langfuse credentials are configured."""+ return bool(+ settings.secrets.langfuse_public_key and settings.secrets.langfuse_secret_key+ )+++def _get_langfuse_client() -> Langfuse:+ """Get or create the Langfuse client for prompt management and tracing."""+ global _langfuse_client+ if _langfuse_client is None:+ if not _is_langfuse_configured():+ raise LangfuseNotConfiguredError(+ "Langfuse is not configured. The chat feature requires Langfuse for prompt management. "+ "Please set the LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY environment variables."+ )+ _langfuse_client = Langfuse(+ public_key=settings.secrets.langfuse_public_key,+ secret_key=settings.secrets.langfuse_secret_key,+ host=settings.secrets.langfuse_host or "https://cloud.langfuse.com",+ )+ return _langfuse_client+++def _get_environment() -> str:+ """Get the current environment name for Langfuse tagging."""+ return settings.config.app_env.value+++def _get_langfuse_prompt() -> str:+ """Fetch the latest production prompt from Langfuse.++ Returns:+ The compiled prompt text from Langfuse.++ Raises:+ Exception: If Langfuse is unavailable or prompt fetch fails."""- Create a new chat session and persist it to the database.+ try:+ langfuse = _get_langfuse_client()+ # cache_ttl_seconds=0 disables SDK caching to always get the latest prompt+ prompt = langfuse.get_prompt(config.langfuse_prompt_name, cache_ttl_seconds=0)+ compiled = prompt.compile()+ logger.info(+ f"Fetched prompt '{config.langfuse_prompt_name}' from Langfuse "+ f"(version: {prompt.version})"+ )+ return compiled+ except Exception as e:+ logger.error(f"Failed to fetch prompt from Langfuse: {e}")+ raise+++async def _is_first_session(user_id: str) -> bool:+ """Check if this is the user's first chat session.++ Returns True if the user has 1 or fewer sessions (meaning this is their first)."""- session = ChatSession.new(user_id)- # Persist the session immediately so it can be used for streaming- return await upsert_chat_session(session)+ try:+ session_count = await chat_db.get_user_session_count(user_id)+ return session_count <= 1+ except Exception as e:+ logger.warning(f"Failed to check session count for user {user_id}: {e}")+ return False # Default to non-onboarding if we can't check-async def get_session(- session_id: str,- user_id: str | None = None,-) -> ChatSession | None:+async def _build_system_prompt(user_id: str | None) -> tuple[str, Any]:+ """Build the full system prompt including business understanding if available.++ Args:+ user_id: The user ID for fetching business understanding+ If "default" and this is the user's first session, will use "onboarding" instead.++ Returns:+ Tuple of (compiled prompt string, Langfuse prompt object for tracing)"""- Get a chat session by ID.++ langfuse = _get_langfuse_client()++ # cache_ttl_seconds=0 disables SDK caching to always get the latest prompt+ prompt = langfuse.get_prompt(config.langfuse_prompt_name, cache_ttl_seconds=0)++ # If user is authenticated, try to fetch their business understanding+ understanding = None+ if user_id:+ try:+ understanding = await get_business_understanding(user_id)+ except Exception as e:+ logger.warning(f"Failed to fetch business understanding: {e}")+ understanding = None+ if understanding:+ context = format_understanding_for_prompt(understanding)+ else:+ context = "This is the first time you are meeting the user. Greet them and introduce them to the platform"++ compiled = prompt.compile(users_information=context)+ return compiled, prompt+++async def _generate_session_title(message: str) -> str | None:+ """Generate a concise title for a chat session based on the first message.++ Args:+ message: The first user message in the session++ Returns:+ A short title (3-6 words) or None if generation fails"""- return await get_chat_session(session_id, user_id)+ try:+ response = await client.chat.completions.create(+ model=config.title_model,+ messages=[+ {+ "role": "system",+ "content": (+ "Generate a very short title (3-6 words) for a chat conversation "+ "based on the user's first message. The title should capture the "+ "main topic or intent. Return ONLY the title, no quotes or punctuation."+ ),+ },+ {"role": "user", "content": message[:500]}, # Limit input length+ ],+ max_tokens=20,+ )+ title = response.choices[0].message.content+ if title:+ # Clean up the title+ title = title.strip().strip("\"'")+ # Limit length+ if len(title) > 50:+ title = title[:47] + "..."+ return title+ return None+ except Exception as e:+ logger.warning(f"Failed to generate session title: {e}")+ return Noneasync def assign_user_to_session(@@ -78,6 +221,7 @@ async def stream_chat_completion(user_id: str | None = None,retry_count: int = 0,session: ChatSession | None = None,+ context: dict[str, str] | None = None, # {url: str, content: str}) -> AsyncGenerator[StreamBaseResponse, None]:"""Main entry point for streaming chat completions with database handling.@@ -102,6 +246,21 @@ async def stream_chat_completion(f"Streaming chat completion for session {session_id} for message {message} and user id {user_id}. Message is user message: {is_user_message}")+ # Check if Langfuse is configured - required for chat functionality+ if not _is_langfuse_configured():+ logger.error("Chat request failed: Langfuse is not configured")+ yield StreamError(+ errorText="Chat service is not available. Langfuse must be configured "+ "with LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY environment variables."+ )+ yield StreamFinish()+ return++ # Langfuse observations will be created after session is loaded (need messages for input)+ # Initialize to None so finally block can safely check and end them+ trace = None+ generation = None+# Only fetch from Redis if session not provided (initial call)if session is None:session = await get_chat_session(session_id, user_id)@@ -121,9 +280,18 @@ async def stream_chat_completion()if message:+ # Build message content with context if provided+ message_content = message+ if context and context.get("url") and context.get("content"):+ context_text = f"Page URL: {context['url']}\n\nPage Content:\n{context['content']}\n\n---\n\nUser Message: {message}"+ message_content = context_text+ logger.info(+ f"Including page context: URL={context['url']}, content_length={len(context['content'])}"+ )+session.messages.append(ChatMessage(- role="user" if is_user_message else "assistant", content=message+ role="user" if is_user_message else "assistant", content=message_content))logger.info(@@ -141,206 +309,370 @@ async def stream_chat_completion(session = await upsert_chat_session(session)assert session, "Session not found"+ # Generate title for new sessions on first user message (non-blocking)+ # Check: is_user_message, no title yet, and this is the first user message+ if is_user_message and message and not session.title:+ user_messages = [m for m in session.messages if m.role == "user"]+ if len(user_messages) == 1:+ # First user message - generate title in background+ import asyncio++ # Capture only the values we need (not the session object) to avoid+ # stale data issues when the main flow modifies the session+ captured_session_id = session_id+ captured_message = message++ async def _update_title():+ try:+ title = await _generate_session_title(captured_message)+ if title:+ # Use dedicated title update function that doesn't+ # touch messages, avoiding race conditions+ await update_session_title(captured_session_id, title)+ logger.info(+ f"Generated title for session {captured_session_id}: {title}"+ )+ except Exception as e:+ logger.warning(f"Failed to update session title: {e}")++ # Fire and forget - don't block the chat response+ asyncio.create_task(_update_title())++ # Build system prompt with business understanding+ system_prompt, langfuse_prompt = await _build_system_prompt(user_id)++ # Build input messages including system prompt for complete Langfuse logging+ trace_input_messages = [{"role": "system", "content": system_prompt}] + [+ m.model_dump() for m in session.messages+ ]++ # Create Langfuse trace for this LLM call (each call gets its own trace, grouped by session_id)+ # Using v3 SDK: start_observation creates a root span, update_trace sets trace-level attributes+ try:+ langfuse = _get_langfuse_client()+ env = _get_environment()+ trace = langfuse.start_observation(+ name="chat_completion",+ input={"messages": trace_input_messages},+ metadata={+ "environment": env,+ "model": config.model,+ "message_count": len(session.messages),+ "prompt_name": langfuse_prompt.name if langfuse_prompt else None,+ "prompt_version": langfuse_prompt.version if langfuse_prompt else None,+ },+ )+ # Set trace-level attributes (session_id, user_id, tags)+ trace.update_trace(+ session_id=session_id,+ user_id=user_id,+ tags=[env, "copilot"],+ )+ except Exception as e:+ logger.warning(f"Failed to create Langfuse trace: {e}")++ # Initialize variables that will be used in finally block (must be defined before try)assistant_response = ChatMessage(role="assistant",content="",)-- has_yielded_end = False- has_yielded_error = False- has_done_tool_call = False- has_received_text = False- text_streaming_ended = False- tool_response_messages: list[ChatMessage] = []accumulated_tool_calls: list[dict[str, Any]] = []- should_retry = False+ # Wrap main logic in try/finally to ensure Langfuse observations are always endedtry:- async for chunk in _stream_chat_chunks(- session=session,- tools=tools,- ):-- if isinstance(chunk, StreamTextChunk):- content = chunk.content or ""- assert assistant_response.content is not None- assistant_response.content += content- has_received_text = True- yield chunk- elif isinstance(chunk, StreamToolCallStart):- # Emit text_ended before first tool call, but only if we've received text- if has_received_text and not text_streaming_ended:- yield StreamTextEnded()- text_streaming_ended = True- yield chunk- elif isinstance(chunk, StreamToolCall):- # Accumulate tool calls in OpenAI format- accumulated_tool_calls.append(- {- "id": chunk.tool_id,- "type": "function",- "function": {- "name": chunk.tool_name,- "arguments": orjson.dumps(chunk.arguments).decode("utf-8"),- },- }- )- elif isinstance(chunk, StreamToolExecutionResult):- result_content = (- chunk.result- if isinstance(chunk.result, str)- else orjson.dumps(chunk.result).decode("utf-8")- )- tool_response_messages.append(- ChatMessage(- role="tool",- content=result_content,- tool_call_id=chunk.tool_id,+ has_yielded_end = False+ has_yielded_error = False+ has_done_tool_call = False+ has_received_text = False+ text_streaming_ended = False+ tool_response_messages: list[ChatMessage] = []+ should_retry = False++ # Generate unique IDs for AI SDK protocol+ import uuid as uuid_module++ message_id = str(uuid_module.uuid4())+ text_block_id = str(uuid_module.uuid4())++ # Yield message start+ yield StreamStart(messageId=message_id)++ # Create Langfuse generation for each LLM call, linked to the prompt+ # Using v3 SDK: start_observation with as_type="generation"+ generation = (+ trace.start_observation(+ as_type="generation",+ name="llm_call",+ model=config.model,+ input={"messages": trace_input_messages},+ prompt=langfuse_prompt,+ )+ if trace+ else None+ )++ try:+ async for chunk in _stream_chat_chunks(+ session=session,+ tools=tools,+ system_prompt=system_prompt,+ text_block_id=text_block_id,+ ):++ if isinstance(chunk, StreamTextStart):+ # Emit text-start before first text delta+ if not has_received_text:+ yield chunk+ elif isinstance(chunk, StreamTextDelta):+ delta = chunk.delta or ""+ assert assistant_response.content is not None+ assistant_response.content += delta+ has_received_text = True+ yield chunk+ elif isinstance(chunk, StreamTextEnd):+ # Emit text-end after text completes+ if has_received_text and not text_streaming_ended:+ text_streaming_ended = True+ yield chunk+ elif isinstance(chunk, StreamToolInputStart):+ # Emit text-end before first tool call, but only if we've received text+ if has_received_text and not text_streaming_ended:+ yield StreamTextEnd(id=text_block_id)+ text_streaming_ended = True+ yield chunk+ elif isinstance(chunk, StreamToolInputAvailable):+ # Accumulate tool calls in OpenAI format+ accumulated_tool_calls.append(+ {+ "id": chunk.toolCallId,+ "type": "function",+ "function": {+ "name": chunk.toolName,+ "arguments": orjson.dumps(chunk.input).decode("utf-8"),+ },+ })- )- has_done_tool_call = True- # Track if any tool execution failed- if not chunk.success:- logger.warning(- f"Tool {chunk.tool_name} (ID: {chunk.tool_id}) execution failed"+ elif isinstance(chunk, StreamToolOutputAvailable):+ result_content = (+ chunk.output+ if isinstance(chunk.output, str)+ else orjson.dumps(chunk.output).decode("utf-8"))- yield chunk- elif isinstance(chunk, StreamEnd):- if not has_done_tool_call:- has_yielded_end = True+ tool_response_messages.append(+ ChatMessage(+ role="tool",+ content=result_content,+ tool_call_id=chunk.toolCallId,+ )+ )+ has_done_tool_call = True+ # Track if any tool execution failed+ if not chunk.success:+ logger.warning(+ f"Tool {chunk.toolName} (ID: {chunk.toolCallId}) execution failed"+ )yield chunk- elif isinstance(chunk, StreamError):- has_yielded_error = True- elif isinstance(chunk, StreamUsage):- session.usage.append(- Usage(- prompt_tokens=chunk.prompt_tokens,- completion_tokens=chunk.completion_tokens,- total_tokens=chunk.total_tokens,+ elif isinstance(chunk, StreamFinish):+ if not has_done_tool_call:+ # Emit text-end before finish if we received text but haven't closed it+ if has_received_text and not text_streaming_ended:+ yield StreamTextEnd(id=text_block_id)+ text_streaming_ended = True+ has_yielded_end = True+ yield chunk+ elif isinstance(chunk, StreamError):+ has_yielded_error = True+ elif isinstance(chunk, StreamUsage):+ session.usage.append(+ Usage(+ prompt_tokens=chunk.promptTokens,+ completion_tokens=chunk.completionTokens,+ total_tokens=chunk.totalTokens,+ ))- )- else:- logger.error(f"Unknown chunk type: {type(chunk)}", exc_info=True)- except Exception as e:- logger.error(f"Error during stream: {e!s}", exc_info=True)+ else:+ logger.error(f"Unknown chunk type: {type(chunk)}", exc_info=True)+ except Exception as e:+ logger.error(f"Error during stream: {e!s}", exc_info=True)- # Check if this is a retryable error (JSON parsing, incomplete tool calls, etc.)- is_retryable = isinstance(e, (orjson.JSONDecodeError, KeyError, TypeError))+ # Check if this is a retryable error (JSON parsing, incomplete tool calls, etc.)+ is_retryable = isinstance(e, (orjson.JSONDecodeError, KeyError, TypeError))- if is_retryable and retry_count < config.max_retries:+ if is_retryable and retry_count < config.max_retries:+ logger.info(+ f"Retryable error encountered. Attempt {retry_count + 1}/{config.max_retries}"+ )+ should_retry = True+ else:+ # Non-retryable error or max retries exceeded+ # Save any partial progress before reporting error+ messages_to_save: list[ChatMessage] = []++ # Add assistant message if it has content or tool calls+ if accumulated_tool_calls:+ assistant_response.tool_calls = accumulated_tool_calls+ if assistant_response.content or assistant_response.tool_calls:+ messages_to_save.append(assistant_response)++ # Add tool response messages after assistant message+ messages_to_save.extend(tool_response_messages)++ session.messages.extend(messages_to_save)+ await upsert_chat_session(session)++ if not has_yielded_error:+ error_message = str(e)+ if not is_retryable:+ error_message = f"Non-retryable error: {error_message}"+ elif retry_count >= config.max_retries:+ error_message = f"Max retries ({config.max_retries}) exceeded: {error_message}"++ error_response = StreamError(errorText=error_message)+ yield error_response+ if not has_yielded_end:+ yield StreamFinish()+ return++ # Handle retry outside of exception handler to avoid nesting+ if should_retry and retry_count < config.max_retries:logger.info(- f"Retryable error encountered. Attempt {retry_count + 1}/{config.max_retries}"+ f"Retrying stream_chat_completion for session {session_id}, attempt {retry_count + 1}")- should_retry = True- else:- # Non-retryable error or max retries exceeded- # Save any partial progress before reporting error- messages_to_save: list[ChatMessage] = []-- # Add assistant message if it has content or tool calls- if accumulated_tool_calls:- assistant_response.tool_calls = accumulated_tool_calls- if assistant_response.content or assistant_response.tool_calls:- messages_to_save.append(assistant_response)-- # Add tool response messages after assistant message- messages_to_save.extend(tool_response_messages)-- session.messages.extend(messages_to_save)- await upsert_chat_session(session)-- if not has_yielded_error:- error_message = str(e)- if not is_retryable:- error_message = f"Non-retryable error: {error_message}"- elif retry_count >= config.max_retries:- error_message = (- f"Max retries ({config.max_retries}) exceeded: {error_message}"- )-- error_response = StreamError(- message=error_message,- timestamp=datetime.now(UTC).isoformat(),- )- yield error_response- if not has_yielded_end:- yield StreamEnd(- timestamp=datetime.now(UTC).isoformat(),- )- return+ async for chunk in stream_chat_completion(+ session_id=session.session_id,+ user_id=user_id,+ retry_count=retry_count + 1,+ session=session,+ context=context,+ ):+ yield chunk+ return # Exit after retry to avoid double-saving in finally block- # Handle retry outside of exception handler to avoid nesting- if should_retry and retry_count < config.max_retries:+ # Normal completion path - save session and handle tool call continuationlogger.info(- f"Retrying stream_chat_completion for session {session_id}, attempt {retry_count + 1}"+ f"Normal completion path: session={session.session_id}, "+ f"current message_count={len(session.messages)}")- async for chunk in stream_chat_completion(- session_id=session.session_id,- user_id=user_id,- retry_count=retry_count + 1,- session=session,- ):- yield chunk- return # Exit after retry to avoid double-saving in finally block- # Normal completion path - save session and handle tool call continuation- logger.info(- f"Normal completion path: session={session.session_id}, "- f"current message_count={len(session.messages)}"- )+ # Build the messages list in the correct order+ messages_to_save: list[ChatMessage] = []- # Build the messages list in the correct order- messages_to_save: list[ChatMessage] = []+ # Add assistant message with tool_calls if any+ if accumulated_tool_calls:+ assistant_response.tool_calls = accumulated_tool_calls+ logger.info(+ f"Added {len(accumulated_tool_calls)} tool calls to assistant message"+ )+ if assistant_response.content or assistant_response.tool_calls:+ messages_to_save.append(assistant_response)+ logger.info(+ f"Saving assistant message with content_len={len(assistant_response.content or '')}, tool_calls={len(assistant_response.tool_calls or [])}"+ )- # Add assistant message with tool_calls if any- if accumulated_tool_calls:- assistant_response.tool_calls = accumulated_tool_calls+ # Add tool response messages after assistant message+ messages_to_save.extend(tool_response_messages)logger.info(- f"Added {len(accumulated_tool_calls)} tool calls to assistant message"+ f"Saving {len(tool_response_messages)} tool response messages, "+ f"total_to_save={len(messages_to_save)}")- if assistant_response.content or assistant_response.tool_calls:- messages_to_save.append(assistant_response)++ session.messages.extend(messages_to_save)logger.info(- f"Saving assistant message with content_len={len(assistant_response.content or '')}, tool_calls={len(assistant_response.tool_calls or [])}"+ f"Extended session messages, new message_count={len(session.messages)}")+ await upsert_chat_session(session)- # Add tool response messages after assistant message- messages_to_save.extend(tool_response_messages)- logger.info(- f"Saving {len(tool_response_messages)} tool response messages, "- f"total_to_save={len(messages_to_save)}"- )-- session.messages.extend(messages_to_save)- logger.info(f"Extended session messages, new message_count={len(session.messages)}")- await upsert_chat_session(session)+ # If we did a tool call, stream the chat completion again to get the next response+ if has_done_tool_call:+ logger.info(+ "Tool call executed, streaming chat completion again to get assistant response"+ )+ async for chunk in stream_chat_completion(+ session_id=session.session_id,+ user_id=user_id,+ session=session, # Pass session object to avoid Redis refetch+ context=context,+ ):+ yield chunk- # If we did a tool call, stream the chat completion again to get the next response- if has_done_tool_call:- logger.info(- "Tool call executed, streaming chat completion again to get assistant response"- )- async for chunk in stream_chat_completion(- session_id=session.session_id,- user_id=user_id,- session=session, # Pass session object to avoid Redis refetch- ):- yield chunk+ finally:+ # Always end Langfuse observations to prevent resource leaks+ # Guard against None and catch errors to avoid masking original exceptions+ if generation is not None:+ try:+ latest_usage = session.usage[-1] if session.usage else None+ generation.update(+ model=config.model,+ output={+ "content": assistant_response.content,+ "tool_calls": accumulated_tool_calls or None,+ },+ usage_details=(+ {+ "input": latest_usage.prompt_tokens,+ "output": latest_usage.completion_tokens,+ "total": latest_usage.total_tokens,+ }+ if latest_usage+ else None+ ),+ )+ generation.end()+ except Exception as e:+ logger.warning(f"Failed to end Langfuse generation: {e}")++ if trace is not None:+ try:+ if accumulated_tool_calls:+ trace.update_trace(output={"tool_calls": accumulated_tool_calls})+ else:+ trace.update_trace(output={"response": assistant_response.content})+ trace.end()+ except Exception as e:+ logger.warning(f"Failed to end Langfuse trace: {e}")+++# Retry configuration for OpenAI API calls+MAX_RETRIES = 3+BASE_DELAY_SECONDS = 1.0+MAX_DELAY_SECONDS = 30.0+++def _is_retryable_error(error: Exception) -> bool:+ """Determine if an error is retryable."""+ if isinstance(error, RateLimitError):+ return True+ if isinstance(error, APIConnectionError):+ return True+ if isinstance(error, APIStatusError):+ # APIStatusError has a response with status_code+ # Retry on 5xx status codes (server errors)+ if error.response.status_code >= 500:+ return True+ if isinstance(error, APIError):+ # Retry on overloaded errors or 500 errors (may not have status code)+ error_message = str(error).lower()+ if "overloaded" in error_message or "internal server error" in error_message:+ return True+ return Falseasync def _stream_chat_chunks(session: ChatSession,tools: list[ChatCompletionToolParam],+ system_prompt: str | None = None,+ text_block_id: str | None = None,) -> AsyncGenerator[StreamBaseResponse, None]:"""Pure streaming function for OpenAI chat completions with tool calling.This function is database-agnostic and focuses only on streaming logic.+ Implements exponential backoff retry for transient API errors.Args:- messages: Conversation context as ChatCompletionMessageParam list- session_id: Session ID- user_id: User ID for tool execution+ session: Chat session with conversation history+ tools: Available tools for the model+ system_prompt: System prompt to prepend to messagesYields:SSE formatted JSON response objects@@ -350,140 +682,186 @@ async def _stream_chat_chunks(logger.info("Starting pure chat stream")+ # Build messages with system prompt prepended+ messages = session.to_openai_messages()+ if system_prompt:+ from openai.types.chat import ChatCompletionSystemMessageParam++ system_message = ChatCompletionSystemMessageParam(+ role="system",+ content=system_prompt,+ )+ messages = [system_message] + messages+# Loop to handle tool calls and continue conversationwhile True:- try:- logger.info("Creating OpenAI chat completion stream...")-- # Create the stream with proper types- stream = await client.chat.completions.create(- model=model,- messages=session.to_openai_messages(),- tools=tools,- tool_choice="auto",- stream=True,- )+ retry_count = 0+ last_error: Exception | None = None++ while retry_count <= MAX_RETRIES:+ try:+ logger.info(+ f"Creating OpenAI chat completion stream..."+ f"{f' (retry {retry_count}/{MAX_RETRIES})' if retry_count > 0 else ''}"+ )- # Variables to accumulate tool calls- tool_calls: list[dict[str, Any]] = []- active_tool_call_idx: int | None = None- finish_reason: str | None = None- # Track which tool call indices have had their start event emitted- emitted_start_for_idx: set[int] = set()-- # Process the stream- chunk: ChatCompletionChunk- async for chunk in stream:- if chunk.usage:- yield StreamUsage(- prompt_tokens=chunk.usage.prompt_tokens,- completion_tokens=chunk.usage.completion_tokens,- total_tokens=chunk.usage.total_tokens,- )+ # Create the stream with proper types+ stream = await client.chat.completions.create(+ model=model,+ messages=messages,+ tools=tools,+ tool_choice="auto",+ stream=True,+ stream_options={"include_usage": True},+ )- if chunk.choices:- choice = chunk.choices[0]- delta = choice.delta-- # Capture finish reason- if choice.finish_reason:- finish_reason = choice.finish_reason- logger.info(f"Finish reason: {finish_reason}")-- # Handle content streaming- if delta.content:- # Stream the text chunk- text_response = StreamTextChunk(- content=delta.content,- timestamp=datetime.now(UTC).isoformat(),+ # Variables to accumulate tool calls+ tool_calls: list[dict[str, Any]] = []+ active_tool_call_idx: int | None = None+ finish_reason: str | None = None+ # Track which tool call indices have had their start event emitted+ emitted_start_for_idx: set[int] = set()++ # Track if we've started the text block+ text_started = False++ # Process the stream+ chunk: ChatCompletionChunk+ async for chunk in stream:+ if chunk.usage:+ yield StreamUsage(+ promptTokens=chunk.usage.prompt_tokens,+ completionTokens=chunk.usage.completion_tokens,+ totalTokens=chunk.usage.total_tokens,)- yield text_response-- # Handle tool calls- if delta.tool_calls:- for tc_chunk in delta.tool_calls:- idx = tc_chunk.index-- # Update active tool call index if needed- if (- active_tool_call_idx is None- or active_tool_call_idx != idx- ):- active_tool_call_idx = idx-- # Ensure we have a tool call object at this index- while len(tool_calls) <= idx:- tool_calls.append(- {- "id": "",- "type": "function",- "function": {- "name": "",- "arguments": "",++ if chunk.choices:+ choice = chunk.choices[0]+ delta = choice.delta++ # Capture finish reason+ if choice.finish_reason:+ finish_reason = choice.finish_reason+ logger.info(f"Finish reason: {finish_reason}")++ # Handle content streaming+ if delta.content:+ # Emit text-start on first text content+ if not text_started and text_block_id:+ yield StreamTextStart(id=text_block_id)+ text_started = True+ # Stream the text delta+ text_response = StreamTextDelta(+ id=text_block_id or "",+ delta=delta.content,+ )+ yield text_response++ # Handle tool calls+ if delta.tool_calls:+ for tc_chunk in delta.tool_calls:+ idx = tc_chunk.index++ # Update active tool call index if needed+ if (+ active_tool_call_idx is None+ or active_tool_call_idx != idx+ ):+ active_tool_call_idx = idx++ # Ensure we have a tool call object at this index+ while len(tool_calls) <= idx:+ tool_calls.append(+ {+ "id": "",+ "type": "function",+ "function": {+ "name": "",+ "arguments": "",+ },},- },- )-- # Accumulate the tool call data- if tc_chunk.id:- tool_calls[idx]["id"] = tc_chunk.id- if tc_chunk.function:- if tc_chunk.function.name:- tool_calls[idx]["function"][- "name"- ] = tc_chunk.function.name- if tc_chunk.function.arguments:- tool_calls[idx]["function"][- "arguments"- ] += tc_chunk.function.arguments-- # Emit StreamToolCallStart only after we have the tool call ID- if (- idx not in emitted_start_for_idx- and tool_calls[idx]["id"]- and tool_calls[idx]["function"]["name"]- ):- yield StreamToolCallStart(- tool_id=tool_calls[idx]["id"],- tool_name=tool_calls[idx]["function"]["name"],- timestamp=datetime.now(UTC).isoformat(),- )- emitted_start_for_idx.add(idx)- logger.info(f"Stream complete. Finish reason: {finish_reason}")-- # Yield all accumulated tool calls after the stream is complete- # This ensures all tool call arguments have been fully received- for idx, tool_call in enumerate(tool_calls):- try:- async for tc in _yield_tool_call(tool_calls, idx, session):- yield tc- except (orjson.JSONDecodeError, KeyError, TypeError) as e:+ )++ # Accumulate the tool call data+ if tc_chunk.id:+ tool_calls[idx]["id"] = tc_chunk.id+ if tc_chunk.function:+ if tc_chunk.function.name:+ tool_calls[idx]["function"][+ "name"+ ] = tc_chunk.function.name+ if tc_chunk.function.arguments:+ tool_calls[idx]["function"][+ "arguments"+ ] += tc_chunk.function.arguments++ # Emit StreamToolInputStart only after we have the tool call ID+ if (+ idx not in emitted_start_for_idx+ and tool_calls[idx]["id"]+ and tool_calls[idx]["function"]["name"]+ ):+ yield StreamToolInputStart(+ toolCallId=tool_calls[idx]["id"],+ toolName=tool_calls[idx]["function"]["name"],+ )+ emitted_start_for_idx.add(idx)+ logger.info(f"Stream complete. Finish reason: {finish_reason}")++ # Yield all accumulated tool calls after the stream is complete+ # This ensures all tool call arguments have been fully received+ for idx, tool_call in enumerate(tool_calls):+ try:+ async for tc in _yield_tool_call(tool_calls, idx, session):+ yield tc+ except (orjson.JSONDecodeError, KeyError, TypeError) as e:+ logger.error(+ f"Failed to parse tool call {idx}: {e}",+ exc_info=True,+ extra={"tool_call": tool_call},+ )+ yield StreamError(+ errorText=f"Invalid tool call arguments for tool {tool_call.get('function', {}).get('name', 'unknown')}: {e}",+ )+ # Re-raise to trigger retry logic in the parent function+ raise++ yield StreamFinish()+ return+ except Exception as e:+ last_error = e+ if _is_retryable_error(e) and retry_count < MAX_RETRIES:+ retry_count += 1+ # Calculate delay with exponential backoff+ delay = min(+ BASE_DELAY_SECONDS * (2 ** (retry_count - 1)),+ MAX_DELAY_SECONDS,+ )+ logger.warning(+ f"Retryable error in stream: {e!s}. "+ f"Retrying in {delay:.1f}s (attempt {retry_count}/{MAX_RETRIES})"+ )+ await asyncio.sleep(delay)+ continue # Retry the stream+ else:+ # Non-retryable error or max retries exceededlogger.error(- f"Failed to parse tool call {idx}: {e}",+ f"Error in stream (not retrying): {e!s}",exc_info=True,- extra={"tool_call": tool_call},- )- yield StreamError(- message=f"Invalid tool call arguments for tool {tool_call.get('function', {}).get('name', 'unknown')}: {e}",- timestamp=datetime.now(UTC).isoformat(),)- # Re-raise to trigger retry logic in the parent function- raise-- yield StreamEnd(- timestamp=datetime.now(UTC).isoformat(),- )- return- except Exception as e:- logger.error(f"Error in stream: {e!s}", exc_info=True)- error_response = StreamError(- message=str(e),- timestamp=datetime.now(UTC).isoformat(),- )- yield error_response- yield StreamEnd(- timestamp=datetime.now(UTC).isoformat(),+ error_response = StreamError(errorText=str(e))+ yield error_response+ yield StreamFinish()+ return++ # If we exit the retry loop without returning, it means we exhausted retries+ if last_error:+ logger.error(+ f"Max retries ({MAX_RETRIES}) exceeded. Last error: {last_error!s}",+ exc_info=True,)+ yield StreamError(errorText=f"Max retries exceeded: {last_error!s}")+ yield StreamFinish()return@@ -500,39 +878,30 @@ async def _yield_tool_call(KeyError: If expected tool call fields are missingTypeError: If tool call structure is invalid"""+ tool_name = tool_calls[yield_idx]["function"]["name"]+ tool_call_id = tool_calls[yield_idx]["id"]logger.info(f"Yielding tool call: {tool_calls[yield_idx]}")- # Parse tool call arguments - exceptions will propagate to caller- arguments = orjson.loads(tool_calls[yield_idx]["function"]["arguments"])+ # Parse tool call arguments - handle empty arguments gracefully+ raw_arguments = tool_calls[yield_idx]["function"]["arguments"]+ if raw_arguments:+ arguments = orjson.loads(raw_arguments)+ else:+ arguments = {}- yield StreamToolCall(- tool_id=tool_calls[yield_idx]["id"],- tool_name=tool_calls[yield_idx]["function"]["name"],- arguments=arguments,- timestamp=datetime.now(UTC).isoformat(),+ yield StreamToolInputAvailable(+ toolCallId=tool_call_id,+ toolName=tool_name,+ input=arguments,)- tool_execution_response: StreamToolExecutionResult = await execute_tool(- tool_name=tool_calls[yield_idx]["function"]["name"],+ tool_execution_response: StreamToolOutputAvailable = await execute_tool(+ tool_name=tool_name,parameters=arguments,- tool_call_id=tool_calls[yield_idx]["id"],+ tool_call_id=tool_call_id,user_id=session.user_id,session=session,)+logger.info(f"Yielding Tool execution response: {tool_execution_response}")yield tool_execution_response---if __name__ == "__main__":- import asyncio-- async def main():- session = await create_chat_session()- async for chunk in stream_chat_completion(- session.session_id,- "Please find me an agent that can help me with my business. Call the tool twice once with the query 'money printing agent' and once with the query 'money generating agent'",- user_id=session.user_id,- ):- print(chunk)-- asyncio.run(main())diff --git a/autogpt_platform/backend/backend/api/features/chat/service_test.py b/autogpt_platform/backend/backend/api/features/chat/service_test.pyindex d1af22a71a..70f27af14f 100644--- a/autogpt_platform/backend/backend/api/features/chat/service_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/service_test.py@@ -4,18 +4,19 @@ from os import getenvimport pytestfrom . import service as chat_service+from .model import create_chat_session, get_chat_session, upsert_chat_sessionfrom .response_model import (- StreamEnd,StreamError,- StreamTextChunk,- StreamToolExecutionResult,+ StreamFinish,+ StreamTextDelta,+ StreamToolOutputAvailable,)logger = logging.getLogger(__name__)@pytest.mark.asyncio(loop_scope="session")-async def test_stream_chat_completion():+async def test_stream_chat_completion(setup_test_user, test_user_id):"""Test the stream_chat_completion function."""@@ -23,7 +24,7 @@ async def test_stream_chat_completion():if not api_key:return pytest.skip("OPEN_ROUTER_API_KEY is not set, skipping test")- session = await chat_service.create_chat_session()+ session = await create_chat_session(test_user_id)has_errors = Falsehas_ended = False@@ -34,9 +35,9 @@ async def test_stream_chat_completion():logger.info(chunk)if isinstance(chunk, StreamError):has_errors = True- if isinstance(chunk, StreamTextChunk):- assistant_message += chunk.content- if isinstance(chunk, StreamEnd):+ if isinstance(chunk, StreamTextDelta):+ assistant_message += chunk.delta+ if isinstance(chunk, StreamFinish):has_ended = Trueassert has_ended, "Chat completion did not end"@@ -45,7 +46,7 @@ async def test_stream_chat_completion():@pytest.mark.asyncio(loop_scope="session")-async def test_stream_chat_completion_with_tool_calls():+async def test_stream_chat_completion_with_tool_calls(setup_test_user, test_user_id):"""Test the stream_chat_completion function."""@@ -53,8 +54,8 @@ async def test_stream_chat_completion_with_tool_calls():if not api_key:return pytest.skip("OPEN_ROUTER_API_KEY is not set, skipping test")- session = await chat_service.create_chat_session()- session = await chat_service.upsert_chat_session(session)+ session = await create_chat_session(test_user_id)+ session = await upsert_chat_session(session)has_errors = Falsehas_ended = False@@ -68,14 +69,14 @@ async def test_stream_chat_completion_with_tool_calls():if isinstance(chunk, StreamError):has_errors = True- if isinstance(chunk, StreamEnd):+ if isinstance(chunk, StreamFinish):has_ended = True- if isinstance(chunk, StreamToolExecutionResult):+ if isinstance(chunk, StreamToolOutputAvailable):had_tool_calls = Trueassert has_ended, "Chat completion did not end"assert not has_errors, "Error occurred while streaming chat completion"assert had_tool_calls, "Tool calls did not occur"- session = await chat_service.get_session(session.session_id)+ session = await get_chat_session(session.session_id)assert session, "Session not found"assert session.usage, "Usage is empty"diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/__init__.py b/autogpt_platform/backend/backend/api/features/chat/tools/__init__.pyindex 5b9b8649a8..2a4401d551 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/__init__.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/__init__.py@@ -4,21 +4,32 @@ from openai.types.chat import ChatCompletionToolParamfrom backend.api.features.chat.model import ChatSession+from .add_understanding import AddUnderstandingTool+from .agent_output import AgentOutputToolfrom .base import BaseToolfrom .find_agent import FindAgentTool+from .find_library_agent import FindLibraryAgentToolfrom .run_agent import RunAgentToolif TYPE_CHECKING:- from backend.api.features.chat.response_model import StreamToolExecutionResult--# Initialize tool instances-find_agent_tool = FindAgentTool()-run_agent_tool = RunAgentTool()--# Export tools as OpenAI format+ from backend.api.features.chat.response_model import StreamToolOutputAvailable++# Single source of truth for all tools+TOOL_REGISTRY: dict[str, BaseTool] = {+ "add_understanding": AddUnderstandingTool(),+ "find_agent": FindAgentTool(),+ "find_library_agent": FindLibraryAgentTool(),+ "run_agent": RunAgentTool(),+ "agent_output": AgentOutputTool(),+}++# Export individual tool instances for backwards compatibility+find_agent_tool = TOOL_REGISTRY["find_agent"]+run_agent_tool = TOOL_REGISTRY["run_agent"]++# Generated from registry for OpenAI APItools: list[ChatCompletionToolParam] = [- find_agent_tool.as_openai_tool(),- run_agent_tool.as_openai_tool(),+ tool.as_openai_tool() for tool in TOOL_REGISTRY.values()]@@ -28,14 +39,9 @@ async def execute_tool(user_id: str | None,session: ChatSession,tool_call_id: str,-) -> "StreamToolExecutionResult":-- tool_map: dict[str, BaseTool] = {- "find_agent": find_agent_tool,- "run_agent": run_agent_tool,- }- if tool_name not in tool_map:+) -> "StreamToolOutputAvailable":+ """Execute a tool by name."""+ tool = TOOL_REGISTRY.get(tool_name)+ if not tool:raise ValueError(f"Tool {tool_name} not found")- return await tool_map[tool_name].execute(- user_id, session, tool_call_id, **parameters- )+ return await tool.execute(user_id, session, tool_call_id, **parameters)diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/_test_data.py b/autogpt_platform/backend/backend/api/features/chat/tools/_test_data.pyindex f75b7bb0d0..a8f208ebb0 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/_test_data.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/_test_data.py@@ -3,6 +3,7 @@ from datetime import UTC, datetimefrom os import getenvimport pytest+from prisma.types import ProfileCreateInputfrom pydantic import SecretStrfrom backend.api.features.chat.model import ChatSession@@ -17,7 +18,7 @@ from backend.data.user import get_or_create_userfrom backend.integrations.credentials_store import IntegrationCredentialsStore-def make_session(user_id: str | None = None):+def make_session(user_id: str):return ChatSession(session_id=str(uuid.uuid4()),user_id=user_id,@@ -49,13 +50,13 @@ async def setup_test_data():# 1b. Create a profile with username for the user (required for store agent lookup)username = user.email.split("@")[0]await prisma.profile.create(- data={- "userId": user.id,- "username": username,- "name": f"Test User {username}",- "description": "Test user profile",- "links": [], # Required field - empty array for test profiles- }+ data=ProfileCreateInput(+ userId=user.id,+ username=username,+ name=f"Test User {username}",+ description="Test user profile",+ links=[], # Required field - empty array for test profiles+ ))# 2. Create a test graph with agent input -> agent output@@ -172,13 +173,13 @@ async def setup_llm_test_data():# 1b. Create a profile with username for the user (required for store agent lookup)username = user.email.split("@")[0]await prisma.profile.create(- data={- "userId": user.id,- "username": username,- "name": f"Test User {username}",- "description": "Test user profile for LLM tests",- "links": [], # Required field - empty array for test profiles- }+ data=ProfileCreateInput(+ userId=user.id,+ username=username,+ name=f"Test User {username}",+ description="Test user profile for LLM tests",+ links=[], # Required field - empty array for test profiles+ ))# 2. Create test OpenAI credentials for the user@@ -332,13 +333,13 @@ async def setup_firecrawl_test_data():# 1b. Create a profile with username for the user (required for store agent lookup)username = user.email.split("@")[0]await prisma.profile.create(- data={- "userId": user.id,- "username": username,- "name": f"Test User {username}",- "description": "Test user profile for Firecrawl tests",- "links": [], # Required field - empty array for test profiles- }+ data=ProfileCreateInput(+ userId=user.id,+ username=username,+ name=f"Test User {username}",+ description="Test user profile for Firecrawl tests",+ links=[], # Required field - empty array for test profiles+ ))# NOTE: We deliberately do NOT create Firecrawl credentials for this userdiff --git a/autogpt_platform/backend/backend/api/features/chat/tools/add_understanding.py b/autogpt_platform/backend/backend/api/features/chat/tools/add_understanding.pynew file mode 100644index 0000000000..fe3d5e8984--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/chat/tools/add_understanding.py@@ -0,0 +1,119 @@+"""Tool for capturing user business understanding incrementally."""++import logging+from typing import Any++from backend.api.features.chat.model import ChatSession+from backend.data.understanding import (+ BusinessUnderstandingInput,+ upsert_business_understanding,+)++from .base import BaseTool+from .models import ErrorResponse, ToolResponseBase, UnderstandingUpdatedResponse++logger = logging.getLogger(__name__)+++class AddUnderstandingTool(BaseTool):+ """Tool for capturing user's business understanding incrementally."""++ @property+ def name(self) -> str:+ return "add_understanding"++ @property+ def description(self) -> str:+ return """Capture and store information about the user's business context,+workflows, pain points, and automation goals. Call this tool whenever the user+shares information about their business. Each call incrementally adds to the+existing understanding - you don't need to provide all fields at once.++Use this to build a comprehensive profile that helps recommend better agents+and automations for the user's specific needs."""++ @property+ def parameters(self) -> dict[str, Any]:+ # Auto-generate from Pydantic model schema+ schema = BusinessUnderstandingInput.model_json_schema()+ properties = {}+ for field_name, field_schema in schema.get("properties", {}).items():+ prop: dict[str, Any] = {"description": field_schema.get("description", "")}+ # Handle anyOf for Optional types+ if "anyOf" in field_schema:+ for option in field_schema["anyOf"]:+ if option.get("type") != "null":+ prop["type"] = option.get("type", "string")+ if "items" in option:+ prop["items"] = option["items"]+ break+ else:+ prop["type"] = field_schema.get("type", "string")+ if "items" in field_schema:+ prop["items"] = field_schema["items"]+ properties[field_name] = prop+ return {"type": "object", "properties": properties, "required": []}++ @property+ def requires_auth(self) -> bool:+ """Requires authentication to store user-specific data."""+ return True++ async def _execute(+ self,+ user_id: str | None,+ session: ChatSession,+ **kwargs,+ ) -> ToolResponseBase:+ """+ Capture and store business understanding incrementally.++ Each call merges new data with existing understanding:+ - String fields are overwritten if provided+ - List fields are appended (with deduplication)+ """+ session_id = session.session_id++ if not user_id:+ return ErrorResponse(+ message="Authentication required to save business understanding.",+ session_id=session_id,+ )++ # Check if any data was provided+ if not any(v is not None for v in kwargs.values()):+ return ErrorResponse(+ message="Please provide at least one field to update.",+ session_id=session_id,+ )++ # Build input model from kwargs (only include fields defined in the model)+ valid_fields = set(BusinessUnderstandingInput.model_fields.keys())+ input_data = BusinessUnderstandingInput(+ **{k: v for k, v in kwargs.items() if k in valid_fields}+ )++ # Track which fields were updated+ updated_fields = [+ k for k, v in kwargs.items() if k in valid_fields and v is not None+ ]++ # Upsert with merge+ understanding = await upsert_business_understanding(user_id, input_data)++ # Build current understanding summary (filter out empty values)+ current_understanding = {+ k: v+ for k, v in understanding.model_dump(+ exclude={"id", "user_id", "created_at", "updated_at"}+ ).items()+ if v is not None and v != [] and v != ""+ }++ return UnderstandingUpdatedResponse(+ message=f"Updated understanding with: {', '.join(updated_fields)}. "+ "I now have a better picture of your business context.",+ session_id=session_id,+ updated_fields=updated_fields,+ current_understanding=current_understanding,+ )diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/agent_output.py b/autogpt_platform/backend/backend/api/features/chat/tools/agent_output.pynew file mode 100644index 0000000000..d81a11362b--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/chat/tools/agent_output.py@@ -0,0 +1,446 @@+"""Tool for retrieving agent execution outputs from user's library."""++import logging+import re+from datetime import datetime, timedelta, timezone+from typing import Any++from pydantic import BaseModel, field_validator++from backend.api.features.chat.model import ChatSession+from backend.api.features.library import db as library_db+from backend.api.features.library.model import LibraryAgent+from backend.data import execution as execution_db+from backend.data.execution import ExecutionStatus, GraphExecution, GraphExecutionMeta++from .base import BaseTool+from .models import (+ AgentOutputResponse,+ ErrorResponse,+ ExecutionOutputInfo,+ NoResultsResponse,+ ToolResponseBase,+)+from .utils import fetch_graph_from_store_slug++logger = logging.getLogger(__name__)+++class AgentOutputInput(BaseModel):+ """Input parameters for the agent_output tool."""++ agent_name: str = ""+ library_agent_id: str = ""+ store_slug: str = ""+ execution_id: str = ""+ run_time: str = "latest"++ @field_validator(+ "agent_name",+ "library_agent_id",+ "store_slug",+ "execution_id",+ "run_time",+ mode="before",+ )+ @classmethod+ def strip_strings(cls, v: Any) -> Any:+ """Strip whitespace from string fields."""+ return v.strip() if isinstance(v, str) else v+++def parse_time_expression(+ time_expr: str | None,+) -> tuple[datetime | None, datetime | None]:+ """+ Parse time expression into datetime range (start, end).++ Supports: "latest", "yesterday", "today", "last week", "last 7 days",+ "last month", "last 30 days", ISO date "YYYY-MM-DD", ISO datetime.+ """+ if not time_expr or time_expr.lower() == "latest":+ return None, None++ now = datetime.now(timezone.utc)+ today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)+ expr = time_expr.lower().strip()++ # Relative time expressions lookup+ relative_times: dict[str, tuple[datetime, datetime]] = {+ "yesterday": (today_start - timedelta(days=1), today_start),+ "today": (today_start, now),+ "last week": (now - timedelta(days=7), now),+ "last 7 days": (now - timedelta(days=7), now),+ "last month": (now - timedelta(days=30), now),+ "last 30 days": (now - timedelta(days=30), now),+ }+ if expr in relative_times:+ return relative_times[expr]++ # Try ISO date format (YYYY-MM-DD)+ date_match = re.match(r"^(\d{4})-(\d{2})-(\d{2})$", expr)+ if date_match:+ try:+ year, month, day = map(int, date_match.groups())+ start = datetime(year, month, day, 0, 0, 0, tzinfo=timezone.utc)+ return start, start + timedelta(days=1)+ except ValueError:+ # Invalid date components (e.g., month=13, day=32)+ pass++ # Try ISO datetime+ try:+ parsed = datetime.fromisoformat(expr.replace("Z", "+00:00"))+ if parsed.tzinfo is None:+ parsed = parsed.replace(tzinfo=timezone.utc)+ return parsed - timedelta(hours=1), parsed + timedelta(hours=1)+ except ValueError:+ return None, None+++class AgentOutputTool(BaseTool):+ """Tool for retrieving execution outputs from user's library agents."""++ @property+ def name(self) -> str:+ return "agent_output"++ @property+ def description(self) -> str:+ return """Retrieve execution outputs from agents in the user's library.++ Identify the agent using one of:+ - agent_name: Fuzzy search in user's library+ - library_agent_id: Exact library agent ID+ - store_slug: Marketplace format 'username/agent-name'++ Select which run to retrieve using:+ - execution_id: Specific execution ID+ - run_time: 'latest' (default), 'yesterday', 'last week', or ISO date 'YYYY-MM-DD'+ """++ @property+ def parameters(self) -> dict[str, Any]:+ return {+ "type": "object",+ "properties": {+ "agent_name": {+ "type": "string",+ "description": "Agent name to search for in user's library (fuzzy match)",+ },+ "library_agent_id": {+ "type": "string",+ "description": "Exact library agent ID",+ },+ "store_slug": {+ "type": "string",+ "description": "Marketplace identifier: 'username/agent-slug'",+ },+ "execution_id": {+ "type": "string",+ "description": "Specific execution ID to retrieve",+ },+ "run_time": {+ "type": "string",+ "description": (+ "Time filter: 'latest', 'yesterday', 'last week', or 'YYYY-MM-DD'"+ ),+ },+ },+ "required": [],+ }++ @property+ def requires_auth(self) -> bool:+ return True++ async def _resolve_agent(+ self,+ user_id: str,+ agent_name: str | None,+ library_agent_id: str | None,+ store_slug: str | None,+ ) -> tuple[LibraryAgent | None, str | None]:+ """+ Resolve agent from provided identifiers.+ Returns (library_agent, error_message).+ """+ # Priority 1: Exact library agent ID+ if library_agent_id:+ try:+ agent = await library_db.get_library_agent(library_agent_id, user_id)+ return agent, None+ except Exception as e:+ logger.warning(f"Failed to get library agent by ID: {e}")+ return None, f"Library agent '{library_agent_id}' not found"++ # Priority 2: Store slug (username/agent-name)+ if store_slug and "/" in store_slug:+ username, agent_slug = store_slug.split("/", 1)+ graph, _ = await fetch_graph_from_store_slug(username, agent_slug)+ if not graph:+ return None, f"Agent '{store_slug}' not found in marketplace"++ # Find in user's library by graph_id+ agent = await library_db.get_library_agent_by_graph_id(user_id, graph.id)+ if not agent:+ return (+ None,+ f"Agent '{store_slug}' is not in your library. "+ "Add it first to see outputs.",+ )+ return agent, None++ # Priority 3: Fuzzy name search in library+ if agent_name:+ try:+ response = await library_db.list_library_agents(+ user_id=user_id,+ search_term=agent_name,+ page_size=5,+ )+ if not response.agents:+ return (+ None,+ f"No agents matching '{agent_name}' found in your library",+ )++ # Return best match (first result from search)+ return response.agents[0], None+ except Exception as e:+ logger.error(f"Error searching library agents: {e}")+ return None, f"Error searching for agent: {e}"++ return (+ None,+ "Please specify an agent name, library_agent_id, or store_slug",+ )++ async def _get_execution(+ self,+ user_id: str,+ graph_id: str,+ execution_id: str | None,+ time_start: datetime | None,+ time_end: datetime | None,+ ) -> tuple[GraphExecution | None, list[GraphExecutionMeta], str | None]:+ """+ Fetch execution(s) based on filters.+ Returns (single_execution, available_executions_meta, error_message).+ """+ # If specific execution_id provided, fetch it directly+ if execution_id:+ execution = await execution_db.get_graph_execution(+ user_id=user_id,+ execution_id=execution_id,+ include_node_executions=False,+ )+ if not execution:+ return None, [], f"Execution '{execution_id}' not found"+ return execution, [], None++ # Get completed executions with time filters+ executions = await execution_db.get_graph_executions(+ graph_id=graph_id,+ user_id=user_id,+ statuses=[ExecutionStatus.COMPLETED],+ created_time_gte=time_start,+ created_time_lte=time_end,+ limit=10,+ )++ if not executions:+ return None, [], None # No error, just no executions++ # If only one execution, fetch full details+ if len(executions) == 1:+ full_execution = await execution_db.get_graph_execution(+ user_id=user_id,+ execution_id=executions[0].id,+ include_node_executions=False,+ )+ return full_execution, [], None++ # Multiple executions - return latest with full details, plus list of available+ full_execution = await execution_db.get_graph_execution(+ user_id=user_id,+ execution_id=executions[0].id,+ include_node_executions=False,+ )+ return full_execution, executions, None++ def _build_response(+ self,+ agent: LibraryAgent,+ execution: GraphExecution | None,+ available_executions: list[GraphExecutionMeta],+ session_id: str | None,+ ) -> AgentOutputResponse:+ """Build the response based on execution data."""+ library_agent_link = f"/library/agents/{agent.id}"++ if not execution:+ return AgentOutputResponse(+ message=f"No completed executions found for agent '{agent.name}'",+ session_id=session_id,+ agent_name=agent.name,+ agent_id=agent.graph_id,+ library_agent_id=agent.id,+ library_agent_link=library_agent_link,+ total_executions=0,+ )++ execution_info = ExecutionOutputInfo(+ execution_id=execution.id,+ status=execution.status.value,+ started_at=execution.started_at,+ ended_at=execution.ended_at,+ outputs=dict(execution.outputs),+ inputs_summary=execution.inputs if execution.inputs else None,+ )++ available_list = None+ if len(available_executions) > 1:+ available_list = [+ {+ "id": e.id,+ "status": e.status.value,+ "started_at": e.started_at.isoformat() if e.started_at else None,+ }+ for e in available_executions[:5]+ ]++ message = f"Found execution outputs for agent '{agent.name}'"+ if len(available_executions) > 1:+ message += (+ f". Showing latest of {len(available_executions)} matching executions."+ )++ return AgentOutputResponse(+ message=message,+ session_id=session_id,+ agent_name=agent.name,+ agent_id=agent.graph_id,+ library_agent_id=agent.id,+ library_agent_link=library_agent_link,+ execution=execution_info,+ available_executions=available_list,+ total_executions=len(available_executions) if available_executions else 1,+ )++ async def _execute(+ self,+ user_id: str | None,+ session: ChatSession,+ **kwargs,+ ) -> ToolResponseBase:+ """Execute the agent_output tool."""+ session_id = session.session_id++ # Parse and validate input+ try:+ input_data = AgentOutputInput(**kwargs)+ except Exception as e:+ logger.error(f"Invalid input: {e}")+ return ErrorResponse(+ message="Invalid input parameters",+ error=str(e),+ session_id=session_id,+ )++ # Ensure user_id is present (should be guaranteed by requires_auth)+ if not user_id:+ return ErrorResponse(+ message="User authentication required",+ session_id=session_id,+ )++ # Check if at least one identifier is provided+ if not any(+ [+ input_data.agent_name,+ input_data.library_agent_id,+ input_data.store_slug,+ input_data.execution_id,+ ]+ ):+ return ErrorResponse(+ message=(+ "Please specify at least one of: agent_name, "+ "library_agent_id, store_slug, or execution_id"+ ),+ session_id=session_id,+ )++ # If only execution_id provided, we need to find the agent differently+ if (+ input_data.execution_id+ and not input_data.agent_name+ and not input_data.library_agent_id+ and not input_data.store_slug+ ):+ # Fetch execution directly to get graph_id+ execution = await execution_db.get_graph_execution(+ user_id=user_id,+ execution_id=input_data.execution_id,+ include_node_executions=False,+ )+ if not execution:+ return ErrorResponse(+ message=f"Execution '{input_data.execution_id}' not found",+ session_id=session_id,+ )++ # Find library agent by graph_id+ agent = await library_db.get_library_agent_by_graph_id(+ user_id, execution.graph_id+ )+ if not agent:+ return NoResultsResponse(+ message=(+ f"Execution found but agent not in your library. "+ f"Graph ID: {execution.graph_id}"+ ),+ session_id=session_id,+ suggestions=["Add the agent to your library to see more details"],+ )++ return self._build_response(agent, execution, [], session_id)++ # Resolve agent from identifiers+ agent, error = await self._resolve_agent(+ user_id=user_id,+ agent_name=input_data.agent_name or None,+ library_agent_id=input_data.library_agent_id or None,+ store_slug=input_data.store_slug or None,+ )++ if error or not agent:+ return NoResultsResponse(+ message=error or "Agent not found",+ session_id=session_id,+ suggestions=[+ "Check the agent name or ID",+ "Make sure the agent is in your library",+ ],+ )++ # Parse time expression+ time_start, time_end = parse_time_expression(input_data.run_time)++ # Fetch execution(s)+ execution, available_executions, exec_error = await self._get_execution(+ user_id=user_id,+ graph_id=agent.graph_id,+ execution_id=input_data.execution_id or None,+ time_start=time_start,+ time_end=time_end,+ )++ if exec_error:+ return ErrorResponse(+ message=exec_error,+ session_id=session_id,+ )++ return self._build_response(agent, execution, available_executions, session_id)diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/agent_search.py b/autogpt_platform/backend/backend/api/features/chat/tools/agent_search.pynew file mode 100644index 0000000000..5fa74ba04e--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/chat/tools/agent_search.py@@ -0,0 +1,151 @@+"""Shared agent search functionality for find_agent and find_library_agent tools."""++import logging+from typing import Literal++from backend.api.features.library import db as library_db+from backend.api.features.store import db as store_db+from backend.util.exceptions import DatabaseError, NotFoundError++from .models import (+ AgentInfo,+ AgentsFoundResponse,+ ErrorResponse,+ NoResultsResponse,+ ToolResponseBase,+)++logger = logging.getLogger(__name__)++SearchSource = Literal["marketplace", "library"]+++async def search_agents(+ query: str,+ source: SearchSource,+ session_id: str | None,+ user_id: str | None = None,+) -> ToolResponseBase:+ """+ Search for agents in marketplace or user library.++ Args:+ query: Search query string+ source: "marketplace" or "library"+ session_id: Chat session ID+ user_id: User ID (required for library search)++ Returns:+ AgentsFoundResponse, NoResultsResponse, or ErrorResponse+ """+ if not query:+ return ErrorResponse(+ message="Please provide a search query", session_id=session_id+ )++ if source == "library" and not user_id:+ return ErrorResponse(+ message="User authentication required to search library",+ session_id=session_id,+ )++ agents: list[AgentInfo] = []+ try:+ if source == "marketplace":+ logger.info(f"Searching marketplace for: {query}")+ results = await store_db.get_store_agents(search_query=query, page_size=5)+ for agent in results.agents:+ agents.append(+ AgentInfo(+ id=f"{agent.creator}/{agent.slug}",+ name=agent.agent_name,+ description=agent.description or "",+ source="marketplace",+ in_library=False,+ creator=agent.creator,+ category="general",+ rating=agent.rating,+ runs=agent.runs,+ is_featured=False,+ )+ )+ else: # library+ logger.info(f"Searching user library for: {query}")+ results = await library_db.list_library_agents(+ user_id=user_id, # type: ignore[arg-type]+ search_term=query,+ page_size=10,+ )+ for agent in results.agents:+ agents.append(+ AgentInfo(+ id=agent.id,+ name=agent.name,+ description=agent.description or "",+ source="library",+ in_library=True,+ creator=agent.creator_name,+ status=agent.status.value,+ can_access_graph=agent.can_access_graph,+ has_external_trigger=agent.has_external_trigger,+ new_output=agent.new_output,+ graph_id=agent.graph_id,+ )+ )+ logger.info(f"Found {len(agents)} agents in {source}")+ except NotFoundError:+ pass+ except DatabaseError as e:+ logger.error(f"Error searching {source}: {e}", exc_info=True)+ return ErrorResponse(+ message=f"Failed to search {source}. Please try again.",+ error=str(e),+ session_id=session_id,+ )++ if not agents:+ suggestions = (+ [+ "Try more general terms",+ "Browse categories in the marketplace",+ "Check spelling",+ ]+ if source == "marketplace"+ else [+ "Try different keywords",+ "Use find_agent to search the marketplace",+ "Check your library at /library",+ ]+ )+ no_results_msg = (+ f"No agents found matching '{query}'. Try different keywords or browse the marketplace."+ if source == "marketplace"+ else f"No agents matching '{query}' found in your library."+ )+ return NoResultsResponse(+ message=no_results_msg, session_id=session_id, suggestions=suggestions+ )++ title = f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} "+ title += (+ f"for '{query}'"+ if source == "marketplace"+ else f"in your library for '{query}'"+ )++ message = (+ "Now you have found some options for the user to choose from. "+ "You can add a link to a recommended agent at: /marketplace/agent/agent_id "+ "Please ask the user if they would like to use any of these agents."+ if source == "marketplace"+ else "Found agents in the user's library. You can provide a link to view an agent at: "+ "/library/agents/{agent_id}. Use agent_output to get execution results, or run_agent to execute."+ )++ return AgentsFoundResponse(+ message=message,+ title=title,+ agents=agents,+ count=len(agents),+ session_id=session_id,+ )diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/base.py b/autogpt_platform/backend/backend/api/features/chat/tools/base.pyindex b4c9d8d731..1dc40c18c7 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/base.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/base.py@@ -6,7 +6,7 @@ from typing import Anyfrom openai.types.chat import ChatCompletionToolParamfrom backend.api.features.chat.model import ChatSession-from backend.api.features.chat.response_model import StreamToolExecutionResult+from backend.api.features.chat.response_model import StreamToolOutputAvailablefrom .models import ErrorResponse, NeedLoginResponse, ToolResponseBase@@ -53,7 +53,7 @@ class BaseTool:session: ChatSession,tool_call_id: str,**kwargs,- ) -> StreamToolExecutionResult:+ ) -> StreamToolOutputAvailable:"""Execute the tool with authentication check.Args:@@ -69,10 +69,10 @@ class BaseTool:logger.error(f"Attempted tool call for {self.name} but user not authenticated")- return StreamToolExecutionResult(- tool_id=tool_call_id,- tool_name=self.name,- result=NeedLoginResponse(+ return StreamToolOutputAvailable(+ toolCallId=tool_call_id,+ toolName=self.name,+ output=NeedLoginResponse(message=f"Please sign in to use {self.name}",session_id=session.session_id,).model_dump_json(),@@ -81,17 +81,17 @@ class BaseTool:try:result = await self._execute(user_id, session, **kwargs)- return StreamToolExecutionResult(- tool_id=tool_call_id,- tool_name=self.name,- result=result.model_dump_json(),+ return StreamToolOutputAvailable(+ toolCallId=tool_call_id,+ toolName=self.name,+ output=result.model_dump_json(),)except Exception as e:logger.error(f"Error in {self.name}: {e}", exc_info=True)- return StreamToolExecutionResult(- tool_id=tool_call_id,- tool_name=self.name,- result=ErrorResponse(+ return StreamToolOutputAvailable(+ toolCallId=tool_call_id,+ toolName=self.name,+ output=ErrorResponse(message=f"An error occurred while executing {self.name}",error=str(e),session_id=session.session_id,diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/find_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/find_agent.pyindex 3ad071f412..477522757d 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/find_agent.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/find_agent.py@@ -1,26 +1,16 @@-"""Tool for discovering agents from marketplace and user library."""+"""Tool for discovering agents from marketplace."""-import loggingfrom typing import Anyfrom backend.api.features.chat.model import ChatSession-from backend.api.features.store import db as store_db-from backend.util.exceptions import DatabaseError, NotFoundError+from .agent_search import search_agentsfrom .base import BaseTool-from .models import (- AgentCarouselResponse,- AgentInfo,- ErrorResponse,- NoResultsResponse,- ToolResponseBase,-)--logger = logging.getLogger(__name__)+from .models import ToolResponseBaseclass FindAgentTool(BaseTool):- """Tool for discovering agents based on user needs."""+ """Tool for discovering agents from the marketplace."""@propertydef name(self) -> str:@@ -46,84 +36,11 @@ class FindAgentTool(BaseTool):}async def _execute(- self,- user_id: str | None,- session: ChatSession,- **kwargs,+ self, user_id: str | None, session: ChatSession, **kwargs) -> ToolResponseBase:- """Search for agents in the marketplace.-- Args:- user_id: User ID (may be anonymous)- session_id: Chat session ID- query: Search query-- Returns:- AgentCarouselResponse: List of agents found in the marketplace- NoResultsResponse: No agents found in the marketplace- ErrorResponse: Error message- """- query = kwargs.get("query", "").strip()- session_id = session.session_id- if not query:- return ErrorResponse(- message="Please provide a search query",- session_id=session_id,- )- agents = []- try:- logger.info(f"Searching marketplace for: {query}")- store_results = await store_db.get_store_agents(- search_query=query,- page_size=5,- )-- logger.info(f"Find agents tool found {len(store_results.agents)} agents")- for agent in store_results.agents:- agent_id = f"{agent.creator}/{agent.slug}"- logger.info(f"Building agent ID = {agent_id}")- agents.append(- AgentInfo(- id=agent_id,- name=agent.agent_name,- description=agent.description or "",- source="marketplace",- in_library=False,- creator=agent.creator,- category="general",- rating=agent.rating,- runs=agent.runs,- is_featured=False,- ),- )- except NotFoundError:- pass- except DatabaseError as e:- logger.error(f"Error searching agents: {e}", exc_info=True)- return ErrorResponse(- message="Failed to search for agents. Please try again.",- error=str(e),- session_id=session_id,- )- if not agents:- return NoResultsResponse(- message=f"No agents found matching '{query}'. Try different keywords or browse the marketplace. If you have 3 consecutive find_agent tool calls results and found no agents. Please stop trying and ask the user if there is anything else you can help with.",- session_id=session_id,- suggestions=[- "Try more general terms",- "Browse categories in the marketplace",- "Check spelling",- ],- )-- # Return formatted carousel- title = (- f"Found {len(agents)} agent{'s' if len(agents) != 1 else ''} for '{query}'"- )- return AgentCarouselResponse(- message="Now you have found some options for the user to choose from. You can add a link to a recommended agent at: /marketplace/agent/agent_id Please ask the user if they would like to use any of these agents. If they do, please call the get_agent_details tool for this agent.",- title=title,- agents=agents,- count=len(agents),- session_id=session_id,+ return await search_agents(+ query=kwargs.get("query", "").strip(),+ source="marketplace",+ session_id=session.session_id,+ user_id=user_id,)diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/find_library_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/find_library_agent.pynew file mode 100644index 0000000000..108fba75ae--- /dev/null+++ b/autogpt_platform/backend/backend/api/features/chat/tools/find_library_agent.py@@ -0,0 +1,52 @@+"""Tool for searching agents in the user's library."""++from typing import Any++from backend.api.features.chat.model import ChatSession++from .agent_search import search_agents+from .base import BaseTool+from .models import ToolResponseBase+++class FindLibraryAgentTool(BaseTool):+ """Tool for searching agents in the user's library."""++ @property+ def name(self) -> str:+ return "find_library_agent"++ @property+ def description(self) -> str:+ return (+ "Search for agents in the user's library. Use this to find agents "+ "the user has already added to their library, including agents they "+ "created or added from the marketplace."+ )++ @property+ def parameters(self) -> dict[str, Any]:+ return {+ "type": "object",+ "properties": {+ "query": {+ "type": "string",+ "description": "Search query to find agents by name or description.",+ },+ },+ "required": ["query"],+ }++ @property+ def requires_auth(self) -> bool:+ return True++ async def _execute(+ self, user_id: str | None, session: ChatSession, **kwargs+ ) -> ToolResponseBase:+ return await search_agents(+ query=kwargs.get("query", "").strip(),+ source="library",+ session_id=session.session_id,+ user_id=user_id,+ )diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/models.py b/autogpt_platform/backend/backend/api/features/chat/tools/models.pyindex a3fbbe025c..8d3871c694 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/models.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/models.py@@ -1,5 +1,6 @@"""Pydantic models for tool responses."""+from datetime import datetimefrom enum import Enumfrom typing import Any@@ -11,14 +12,15 @@ from backend.data.model import CredentialsMetaInputclass ResponseType(str, Enum):"""Types of tool responses."""- AGENT_CAROUSEL = "agent_carousel"+ AGENTS_FOUND = "agents_found"AGENT_DETAILS = "agent_details"SETUP_REQUIREMENTS = "setup_requirements"EXECUTION_STARTED = "execution_started"NEED_LOGIN = "need_login"ERROR = "error"NO_RESULTS = "no_results"- SUCCESS = "success"+ AGENT_OUTPUT = "agent_output"+ UNDERSTANDING_UPDATED = "understanding_updated"# Base response model@@ -51,14 +53,14 @@ class AgentInfo(BaseModel):graph_id: str | None = None-class AgentCarouselResponse(ToolResponseBase):+class AgentsFoundResponse(ToolResponseBase):"""Response for find_agent tool."""- type: ResponseType = ResponseType.AGENT_CAROUSEL+ type: ResponseType = ResponseType.AGENTS_FOUNDtitle: str = "Available Agents"agents: list[AgentInfo]count: int- name: str = "agent_carousel"+ name: str = "agents_found"class NoResultsResponse(ToolResponseBase):@@ -173,3 +175,37 @@ class ErrorResponse(ToolResponseBase):type: ResponseType = ResponseType.ERRORerror: str | None = Nonedetails: dict[str, Any] | None = None+++# Agent output models+class ExecutionOutputInfo(BaseModel):+ """Summary of a single execution's outputs."""++ execution_id: str+ status: str+ started_at: datetime | None = None+ ended_at: datetime | None = None+ outputs: dict[str, list[Any]]+ inputs_summary: dict[str, Any] | None = None+++class AgentOutputResponse(ToolResponseBase):+ """Response for agent_output tool."""++ type: ResponseType = ResponseType.AGENT_OUTPUT+ agent_name: str+ agent_id: str+ library_agent_id: str | None = None+ library_agent_link: str | None = None+ execution: ExecutionOutputInfo | None = None+ available_executions: list[dict[str, Any]] | None = None+ total_executions: int = 0+++# Business understanding models+class UnderstandingUpdatedResponse(ToolResponseBase):+ """Response for add_understanding tool."""++ type: ResponseType = ResponseType.UNDERSTANDING_UPDATED+ updated_fields: list[str] = Field(default_factory=list)+ current_understanding: dict[str, Any] = Field(default_factory=dict)diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.pyindex 931e075021..1f0a836543 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent.py@@ -7,6 +7,7 @@ from pydantic import BaseModel, Field, field_validatorfrom backend.api.features.chat.config import ChatConfigfrom backend.api.features.chat.model import ChatSession+from backend.api.features.library import db as library_dbfrom backend.data.graph import GraphModelfrom backend.data.model import CredentialsMetaInputfrom backend.data.user import get_user_by_id@@ -57,6 +58,7 @@ class RunAgentInput(BaseModel):"""Input parameters for the run_agent tool."""username_agent_slug: str = ""+ library_agent_id: str = ""inputs: dict[str, Any] = Field(default_factory=dict)use_defaults: bool = Falseschedule_name: str = ""@@ -64,7 +66,12 @@ class RunAgentInput(BaseModel):timezone: str = "UTC"@field_validator(- "username_agent_slug", "schedule_name", "cron", "timezone", mode="before"+ "username_agent_slug",+ "library_agent_id",+ "schedule_name",+ "cron",+ "timezone",+ mode="before",)@classmethoddef strip_strings(cls, v: Any) -> Any:@@ -90,7 +97,7 @@ class RunAgentTool(BaseTool):@propertydef description(self) -> str:- return """Run or schedule an agent from the marketplace.+ return """Run or schedule an agent from the marketplace or user's library.The tool automatically handles the setup flow:- Returns missing inputs if required fields are not provided@@ -98,6 +105,10 @@ class RunAgentTool(BaseTool):- Executes immediately if all requirements are met- Schedules execution if cron expression is provided+ Identify the agent using either:+ - username_agent_slug: Marketplace format 'username/agent-name'+ - library_agent_id: ID of an agent in the user's library+For scheduled execution, provide: schedule_name, cron, and optionally timezone."""@property@@ -109,6 +120,10 @@ class RunAgentTool(BaseTool):"type": "string","description": "Agent identifier in format 'username/agent-name'",},+ "library_agent_id": {+ "type": "string",+ "description": "Library agent ID from user's library",+ },"inputs": {"type": "object","description": "Input values for the agent",@@ -131,7 +146,7 @@ class RunAgentTool(BaseTool):"description": "IANA timezone for schedule (default: UTC)",},},- "required": ["username_agent_slug"],+ "required": [],}@property@@ -149,10 +164,16 @@ class RunAgentTool(BaseTool):params = RunAgentInput(**kwargs)session_id = session.session_id- # Validate agent slug format- if not params.username_agent_slug or "/" not in params.username_agent_slug:+ # Validate at least one identifier is provided+ has_slug = params.username_agent_slug and "/" in params.username_agent_slug+ has_library_id = bool(params.library_agent_id)++ if not has_slug and not has_library_id:return ErrorResponse(- message="Please provide an agent slug in format 'username/agent-name'",+ message=(+ "Please provide either a username_agent_slug "+ "(format 'username/agent-name') or a library_agent_id"+ ),session_id=session_id,)@@ -167,13 +188,41 @@ class RunAgentTool(BaseTool):is_schedule = bool(params.schedule_name or params.cron)try:- # Step 1: Fetch agent details (always happens first)- username, agent_name = params.username_agent_slug.split("/", 1)- graph, store_agent = await fetch_graph_from_store_slug(username, agent_name)+ # Step 1: Fetch agent details+ graph: GraphModel | None = None+ library_agent = None++ # Priority: library_agent_id if provided+ if has_library_id:+ library_agent = await library_db.get_library_agent(+ params.library_agent_id, user_id+ )+ if not library_agent:+ return ErrorResponse(+ message=f"Library agent '{params.library_agent_id}' not found",+ session_id=session_id,+ )+ # Get the graph from the library agent+ from backend.data.graph import get_graph++ graph = await get_graph(+ library_agent.graph_id,+ library_agent.graph_version,+ user_id=user_id,+ )+ else:+ # Fetch from marketplace slug+ username, agent_name = params.username_agent_slug.split("/", 1)+ graph, _ = await fetch_graph_from_store_slug(username, agent_name)if not graph:+ identifier = (+ params.library_agent_id+ if has_library_id+ else params.username_agent_slug+ )return ErrorResponse(- message=f"Agent '{params.username_agent_slug}' not found in marketplace",+ message=f"Agent '{identifier}' not found",session_id=session_id,)diff --git a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent_test.py b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent_test.pyindex cd4e7d04ba..9e10304429 100644--- a/autogpt_platform/backend/backend/api/features/chat/tools/run_agent_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/tools/run_agent_test.py@@ -58,11 +58,11 @@ async def test_run_agent(setup_test_data):# Verify the responseassert response is not None- assert hasattr(response, "result")+ assert hasattr(response, "output")# Parse the result JSON to verify the execution started- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)assert "execution_id" in result_dataassert "graph_id" in result_dataassert result_data["graph_id"] == graph.id@@ -98,11 +98,11 @@ async def test_run_agent_missing_inputs(setup_test_data):# Verify that we get an error responseassert response is not None- assert hasattr(response, "result")+ assert hasattr(response, "output")# The tool should return an ErrorResponse when setup info indicates not ready- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)assert "message" in result_data@@ -130,10 +130,10 @@ async def test_run_agent_invalid_agent_id(setup_test_data):# Verify that we get an error responseassert response is not None- assert hasattr(response, "result")+ assert hasattr(response, "output")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)assert "message" in result_data# Should get an error about failed setup or not foundassert any(@@ -170,12 +170,12 @@ async def test_run_agent_with_llm_credentials(setup_llm_test_data):# Verify the responseassert response is not None- assert hasattr(response, "result")+ assert hasattr(response, "output")# Parse the result JSON to verify the execution started- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should successfully start execution since credentials are availableassert "execution_id" in result_data@@ -207,9 +207,9 @@ async def test_run_agent_shows_available_inputs_when_none_provided(setup_test_da)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should return agent_details type showing available inputsassert result_data.get("type") == "agent_details"@@ -242,9 +242,9 @@ async def test_run_agent_with_use_defaults(setup_test_data):)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should execute successfullyassert "execution_id" in result_data@@ -272,9 +272,9 @@ async def test_run_agent_missing_credentials(setup_firecrawl_test_data):)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should return setup_requirements type with missing credentialsassert result_data.get("type") == "setup_requirements"@@ -304,9 +304,9 @@ async def test_run_agent_invalid_slug_format(setup_test_data):)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should return errorassert result_data.get("type") == "error"@@ -317,9 +317,10 @@ async def test_run_agent_invalid_slug_format(setup_test_data):async def test_run_agent_unauthenticated():"""Test that run_agent returns need_login for unauthenticated users."""tool = RunAgentTool()- session = make_session(user_id=None)+ # Session has a user_id (session owner), but we test tool execution without user_id+ session = make_session(user_id="test-session-owner")- # Execute without user_id+ # Execute without user_id to test unauthenticated behaviorresponse = await tool.execute(user_id=None,session_id=str(uuid.uuid4()),@@ -330,9 +331,9 @@ async def test_run_agent_unauthenticated():)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Base tool returns need_login type for unauthenticated usersassert result_data.get("type") == "need_login"@@ -362,9 +363,9 @@ async def test_run_agent_schedule_without_cron(setup_test_data):)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should return error about missing cronassert result_data.get("type") == "error"@@ -394,9 +395,9 @@ async def test_run_agent_schedule_without_name(setup_test_data):)assert response is not None- assert hasattr(response, "result")- assert isinstance(response.result, str)- result_data = orjson.loads(response.result)+ assert hasattr(response, "output")+ assert isinstance(response.output, str)+ result_data = orjson.loads(response.output)# Should return error about missing schedule_nameassert result_data.get("type") == "error"diff --git a/autogpt_platform/backend/backend/data/understanding.py b/autogpt_platform/backend/backend/data/understanding.pynew file mode 100644index 0000000000..eb63d719ca--- /dev/null+++ b/autogpt_platform/backend/backend/data/understanding.py@@ -0,0 +1,404 @@+"""Data models and access layer for user business understanding."""++import logging+from datetime import datetime+from typing import Any, Optional, cast++import pydantic+from prisma.models import CoPilotUnderstanding++from backend.data.redis_client import get_redis_async+from backend.util.json import SafeJson++logger = logging.getLogger(__name__)++# Cache configuration+CACHE_KEY_PREFIX = "understanding"+CACHE_TTL_SECONDS = 48 * 60 * 60 # 48 hours+++def _cache_key(user_id: str) -> str:+ """Generate cache key for user business understanding."""+ return f"{CACHE_KEY_PREFIX}:{user_id}"+++def _json_to_list(value: Any) -> list[str]:+ """Convert Json field to list[str], handling None."""+ if value is None:+ return []+ if isinstance(value, list):+ return cast(list[str], value)+ return []+++class BusinessUnderstandingInput(pydantic.BaseModel):+ """Input model for updating business understanding - all fields optional for incremental updates."""++ # User info+ user_name: Optional[str] = pydantic.Field(None, description="The user's name")+ job_title: Optional[str] = pydantic.Field(None, description="The user's job title")++ # Business basics+ business_name: Optional[str] = pydantic.Field(+ None, description="Name of the user's business"+ )+ industry: Optional[str] = pydantic.Field(None, description="Industry or sector")+ business_size: Optional[str] = pydantic.Field(+ None, description="Company size (e.g., '1-10', '11-50')"+ )+ user_role: Optional[str] = pydantic.Field(+ None,+ description="User's role in the organization (e.g., 'decision maker', 'implementer')",+ )++ # Processes & activities+ key_workflows: Optional[list[str]] = pydantic.Field(+ None, description="Key business workflows"+ )+ daily_activities: Optional[list[str]] = pydantic.Field(+ None, description="Daily activities performed"+ )++ # Pain points & goals+ pain_points: Optional[list[str]] = pydantic.Field(+ None, description="Current pain points"+ )+ bottlenecks: Optional[list[str]] = pydantic.Field(+ None, description="Process bottlenecks"+ )+ manual_tasks: Optional[list[str]] = pydantic.Field(+ None, description="Manual/repetitive tasks"+ )+ automation_goals: Optional[list[str]] = pydantic.Field(+ None, description="Desired automation goals"+ )++ # Current tools+ current_software: Optional[list[str]] = pydantic.Field(+ None, description="Software/tools currently used"+ )+ existing_automation: Optional[list[str]] = pydantic.Field(+ None, description="Existing automations"+ )++ # Additional context+ additional_notes: Optional[str] = pydantic.Field(+ None, description="Any additional context"+ )+++class BusinessUnderstanding(pydantic.BaseModel):+ """Full business understanding model returned from database."""++ id: str+ user_id: str+ created_at: datetime+ updated_at: datetime++ # User info+ user_name: Optional[str] = None+ job_title: Optional[str] = None++ # Business basics+ business_name: Optional[str] = None+ industry: Optional[str] = None+ business_size: Optional[str] = None+ user_role: Optional[str] = None++ # Processes & activities+ key_workflows: list[str] = pydantic.Field(default_factory=list)+ daily_activities: list[str] = pydantic.Field(default_factory=list)++ # Pain points & goals+ pain_points: list[str] = pydantic.Field(default_factory=list)+ bottlenecks: list[str] = pydantic.Field(default_factory=list)+ manual_tasks: list[str] = pydantic.Field(default_factory=list)+ automation_goals: list[str] = pydantic.Field(default_factory=list)++ # Current tools+ current_software: list[str] = pydantic.Field(default_factory=list)+ existing_automation: list[str] = pydantic.Field(default_factory=list)++ # Additional context+ additional_notes: Optional[str] = None++ @classmethod+ def from_db(cls, db_record: CoPilotUnderstanding) -> "BusinessUnderstanding":+ """Convert database record to Pydantic model."""+ data = db_record.data if isinstance(db_record.data, dict) else {}+ business = (+ data.get("business", {}) if isinstance(data.get("business"), dict) else {}+ )+ return cls(+ id=db_record.id,+ user_id=db_record.userId,+ created_at=db_record.createdAt,+ updated_at=db_record.updatedAt,+ user_name=data.get("name"),+ job_title=business.get("job_title"),+ business_name=business.get("business_name"),+ industry=business.get("industry"),+ business_size=business.get("business_size"),+ user_role=business.get("user_role"),+ key_workflows=_json_to_list(business.get("key_workflows")),+ daily_activities=_json_to_list(business.get("daily_activities")),+ pain_points=_json_to_list(business.get("pain_points")),+ bottlenecks=_json_to_list(business.get("bottlenecks")),+ manual_tasks=_json_to_list(business.get("manual_tasks")),+ automation_goals=_json_to_list(business.get("automation_goals")),+ current_software=_json_to_list(business.get("current_software")),+ existing_automation=_json_to_list(business.get("existing_automation")),+ additional_notes=business.get("additional_notes"),+ )+++def _merge_lists(existing: list | None, new: list | None) -> list | None:+ """Merge two lists, removing duplicates while preserving order."""+ if new is None:+ return existing+ if existing is None:+ return new+ # Preserve order, add new items that don't exist+ merged = list(existing)+ for item in new:+ if item not in merged:+ merged.append(item)+ return merged+++async def _get_from_cache(user_id: str) -> Optional[BusinessUnderstanding]:+ """Get business understanding from Redis cache."""+ try:+ redis = await get_redis_async()+ cached_data = await redis.get(_cache_key(user_id))+ if cached_data:+ return BusinessUnderstanding.model_validate_json(cached_data)+ except Exception as e:+ logger.warning(f"Failed to get understanding from cache: {e}")+ return None+++async def _set_cache(user_id: str, understanding: BusinessUnderstanding) -> None:+ """Set business understanding in Redis cache with TTL."""+ try:+ redis = await get_redis_async()+ await redis.setex(+ _cache_key(user_id),+ CACHE_TTL_SECONDS,+ understanding.model_dump_json(),+ )+ except Exception as e:+ logger.warning(f"Failed to set understanding in cache: {e}")+++async def _delete_cache(user_id: str) -> None:+ """Delete business understanding from Redis cache."""+ try:+ redis = await get_redis_async()+ await redis.delete(_cache_key(user_id))+ except Exception as e:+ logger.warning(f"Failed to delete understanding from cache: {e}")+++async def get_business_understanding(+ user_id: str,+) -> Optional[BusinessUnderstanding]:+ """Get the business understanding for a user.++ Checks cache first, falls back to database if not cached.+ Results are cached for 48 hours.+ """+ # Try cache first+ cached = await _get_from_cache(user_id)+ if cached:+ logger.debug(f"Business understanding cache hit for user {user_id}")+ return cached++ # Cache miss - load from database+ logger.debug(f"Business understanding cache miss for user {user_id}")+ record = await CoPilotUnderstanding.prisma().find_unique(where={"userId": user_id})+ if record is None:+ return None++ understanding = BusinessUnderstanding.from_db(record)++ # Store in cache for next time+ await _set_cache(user_id, understanding)++ return understanding+++async def upsert_business_understanding(+ user_id: str,+ input_data: BusinessUnderstandingInput,+) -> BusinessUnderstanding:+ """+ Create or update business understanding with incremental merge strategy.++ - String fields: new value overwrites if provided (not None)+ - List fields: new items are appended to existing (deduplicated)++ Data is stored as: {name: ..., business: {version: 1, ...}}+ """+ # Get existing record for merge+ existing = await CoPilotUnderstanding.prisma().find_unique(+ where={"userId": user_id}+ )++ # Get existing data structure or start fresh+ existing_data: dict[str, Any] = {}+ if existing and isinstance(existing.data, dict):+ existing_data = dict(existing.data)++ existing_business: dict[str, Any] = {}+ if isinstance(existing_data.get("business"), dict):+ existing_business = dict(existing_data["business"])++ # Business fields (stored inside business object)+ business_string_fields = [+ "job_title",+ "business_name",+ "industry",+ "business_size",+ "user_role",+ "additional_notes",+ ]+ business_list_fields = [+ "key_workflows",+ "daily_activities",+ "pain_points",+ "bottlenecks",+ "manual_tasks",+ "automation_goals",+ "current_software",+ "existing_automation",+ ]++ # Handle top-level name field+ if input_data.user_name is not None:+ existing_data["name"] = input_data.user_name++ # Business string fields - overwrite if provided+ for field in business_string_fields:+ value = getattr(input_data, field)+ if value is not None:+ existing_business[field] = value++ # Business list fields - merge with existing+ for field in business_list_fields:+ value = getattr(input_data, field)+ if value is not None:+ existing_list = _json_to_list(existing_business.get(field))+ merged = _merge_lists(existing_list, value)+ existing_business[field] = merged++ # Set version and nest business data+ existing_business["version"] = 1+ existing_data["business"] = existing_business++ # Upsert with the merged data+ record = await CoPilotUnderstanding.prisma().upsert(+ where={"userId": user_id},+ data={+ "create": {"userId": user_id, "data": SafeJson(existing_data)},+ "update": {"data": SafeJson(existing_data)},+ },+ )++ understanding = BusinessUnderstanding.from_db(record)++ # Update cache with new understanding+ await _set_cache(user_id, understanding)++ return understanding+++async def clear_business_understanding(user_id: str) -> bool:+ """Clear/delete business understanding for a user from both DB and cache."""+ # Delete from cache first+ await _delete_cache(user_id)++ try:+ await CoPilotUnderstanding.prisma().delete(where={"userId": user_id})+ return True+ except Exception:+ # Record might not exist+ return False+++def format_understanding_for_prompt(understanding: BusinessUnderstanding) -> str:+ """Format business understanding as text for system prompt injection."""+ sections = []++ # User info section+ user_info = []+ if understanding.user_name:+ user_info.append(f"Name: {understanding.user_name}")+ if understanding.job_title:+ user_info.append(f"Job Title: {understanding.job_title}")+ if user_info:+ sections.append("## User\n" + "\n".join(user_info))++ # Business section+ business_info = []+ if understanding.business_name:+ business_info.append(f"Company: {understanding.business_name}")+ if understanding.industry:+ business_info.append(f"Industry: {understanding.industry}")+ if understanding.business_size:+ business_info.append(f"Size: {understanding.business_size}")+ if understanding.user_role:+ business_info.append(f"Role Context: {understanding.user_role}")+ if business_info:+ sections.append("## Business\n" + "\n".join(business_info))++ # Processes section+ processes = []+ if understanding.key_workflows:+ processes.append(f"Key Workflows: {', '.join(understanding.key_workflows)}")+ if understanding.daily_activities:+ processes.append(+ f"Daily Activities: {', '.join(understanding.daily_activities)}"+ )+ if processes:+ sections.append("## Processes\n" + "\n".join(processes))++ # Pain points section+ pain_points = []+ if understanding.pain_points:+ pain_points.append(f"Pain Points: {', '.join(understanding.pain_points)}")+ if understanding.bottlenecks:+ pain_points.append(f"Bottlenecks: {', '.join(understanding.bottlenecks)}")+ if understanding.manual_tasks:+ pain_points.append(f"Manual Tasks: {', '.join(understanding.manual_tasks)}")+ if pain_points:+ sections.append("## Pain Points\n" + "\n".join(pain_points))++ # Goals section+ if understanding.automation_goals:+ sections.append(+ "## Automation Goals\n"+ + "\n".join(f"- {goal}" for goal in understanding.automation_goals)+ )++ # Current tools section+ tools_info = []+ if understanding.current_software:+ tools_info.append(+ f"Current Software: {', '.join(understanding.current_software)}"+ )+ if understanding.existing_automation:+ tools_info.append(+ f"Existing Automation: {', '.join(understanding.existing_automation)}"+ )+ if tools_info:+ sections.append("## Current Tools\n" + "\n".join(tools_info))++ # Additional notes+ if understanding.additional_notes:+ sections.append(f"## Additional Context\n{understanding.additional_notes}")++ if not sections:+ return ""++ return "# User Business Context\n\n" + "\n\n".join(sections)diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 7a51200eaf..4448aeb77e 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -658,6 +658,14 @@ class Secrets(UpdateTrackingModel["Secrets"], BaseSettings):ayrshare_api_key: str = Field(default="", description="Ayrshare API Key")ayrshare_jwt_key: str = Field(default="", description="Ayrshare private Key")++ # Langfuse prompt management+ langfuse_public_key: str = Field(default="", description="Langfuse public key")+ langfuse_secret_key: str = Field(default="", description="Langfuse secret key")+ langfuse_host: str = Field(+ default="https://cloud.langfuse.com", description="Langfuse host URL"+ )+# Add more secret fields as neededmodel_config = SettingsConfigDict(env_file=".env",diff --git a/autogpt_platform/backend/migrations/20260115081736_add_chat_tables/migration.sql b/autogpt_platform/backend/migrations/20260115081736_add_chat_tables/migration.sqlnew file mode 100644index 0000000000..2c964858e6--- /dev/null+++ b/autogpt_platform/backend/migrations/20260115081736_add_chat_tables/migration.sql@@ -0,0 +1,64 @@+-- CreateTable+CREATE TABLE "CoPilotUnderstanding" (+ "id" TEXT NOT NULL,+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "userId" TEXT NOT NULL,+ "data" JSONB,++ CONSTRAINT "CoPilotUnderstanding_pkey" PRIMARY KEY ("id")+);++-- CreateTable+CREATE TABLE "ChatSession" (+ "id" TEXT NOT NULL,+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "userId" TEXT NOT NULL,+ "title" TEXT,+ "credentials" JSONB NOT NULL DEFAULT '{}',+ "successfulAgentRuns" JSONB NOT NULL DEFAULT '{}',+ "successfulAgentSchedules" JSONB NOT NULL DEFAULT '{}',+ "totalPromptTokens" INTEGER NOT NULL DEFAULT 0,+ "totalCompletionTokens" INTEGER NOT NULL DEFAULT 0,++ CONSTRAINT "ChatSession_pkey" PRIMARY KEY ("id")+);++-- CreateTable+CREATE TABLE "ChatMessage" (+ "id" TEXT NOT NULL,+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "sessionId" TEXT NOT NULL,+ "role" TEXT NOT NULL,+ "content" TEXT,+ "name" TEXT,+ "toolCallId" TEXT,+ "refusal" TEXT,+ "toolCalls" JSONB,+ "functionCall" JSONB,+ "sequence" INTEGER NOT NULL,++ CONSTRAINT "ChatMessage_pkey" PRIMARY KEY ("id")+);++-- CreateIndex+CREATE UNIQUE INDEX "CoPilotUnderstanding_userId_key" ON "CoPilotUnderstanding"("userId");++-- CreateIndex+CREATE INDEX "CoPilotUnderstanding_userId_idx" ON "CoPilotUnderstanding"("userId");++-- CreateIndex+CREATE INDEX "ChatSession_userId_updatedAt_idx" ON "ChatSession"("userId", "updatedAt");++-- CreateIndex+CREATE UNIQUE INDEX "ChatMessage_sessionId_sequence_key" ON "ChatMessage"("sessionId", "sequence");++-- AddForeignKey+ALTER TABLE "CoPilotUnderstanding" ADD CONSTRAINT "CoPilotUnderstanding_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "ChatSession" ADD CONSTRAINT "ChatSession_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "ChatMessage" ADD CONSTRAINT "ChatMessage_sessionId_fkey" FOREIGN KEY ("sessionId") REFERENCES "ChatSession"("id") ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lockindex 5e3b07a145..31664fa216 100644--- a/autogpt_platform/backend/poetry.lock+++ b/autogpt_platform/backend/poetry.lock@@ -2777,6 +2777,30 @@ enabler = ["pytest-enabler (>=2.2)"]test = ["pyfakefs", "pytest (>=6,!=8.1.*)"]type = ["pygobject-stubs", "pytest-mypy", "shtab", "types-pywin32"]+[[package]]+name = "langfuse"+version = "3.11.2"+description = "A client library for accessing langfuse"+optional = false+python-versions = "<4.0,>=3.10"+groups = ["main"]+files = [+ {file = "langfuse-3.11.2-py3-none-any.whl", hash = "sha256:84faea9f909694023cc7f0eb45696be190248c8790424f22af57ca4cd7a29f2d"},+ {file = "langfuse-3.11.2.tar.gz", hash = "sha256:ab5f296a8056815b7288c7f25bc308a5e79f82a8634467b25daffdde99276e09"},+]++[package.dependencies]+backoff = ">=1.10.0"+httpx = ">=0.15.4,<1.0"+openai = ">=0.27.8"+opentelemetry-api = ">=1.33.1,<2.0.0"+opentelemetry-exporter-otlp-proto-http = ">=1.33.1,<2.0.0"+opentelemetry-sdk = ">=1.33.1,<2.0.0"+packaging = ">=23.2,<26.0"+pydantic = ">=1.10.7,<3.0"+requests = ">=2,<3"+wrapt = ">=1.14,<2.0"+[[package]]name = "launchdarkly-eventsource"version = "1.3.0"@@ -3468,6 +3492,90 @@ files = [importlib-metadata = ">=6.0,<8.8.0"typing-extensions = ">=4.5.0"+[[package]]+name = "opentelemetry-exporter-otlp-proto-common"+version = "1.35.0"+description = "OpenTelemetry Protobuf encoding"+optional = false+python-versions = ">=3.9"+groups = ["main"]+files = [+ {file = "opentelemetry_exporter_otlp_proto_common-1.35.0-py3-none-any.whl", hash = "sha256:863465de697ae81279ede660f3918680b4480ef5f69dcdac04f30722ed7b74cc"},+ {file = "opentelemetry_exporter_otlp_proto_common-1.35.0.tar.gz", hash = "sha256:6f6d8c39f629b9fa5c79ce19a2829dbd93034f8ac51243cdf40ed2196f00d7eb"},+]++[package.dependencies]+opentelemetry-proto = "1.35.0"++[[package]]+name = "opentelemetry-exporter-otlp-proto-http"+version = "1.35.0"+description = "OpenTelemetry Collector Protobuf over HTTP Exporter"+optional = false+python-versions = ">=3.9"+groups = ["main"]+files = [+ {file = "opentelemetry_exporter_otlp_proto_http-1.35.0-py3-none-any.whl", hash = "sha256:9a001e3df3c7f160fb31056a28ed7faa2de7df68877ae909516102ae36a54e1d"},+ {file = "opentelemetry_exporter_otlp_proto_http-1.35.0.tar.gz", hash = "sha256:cf940147f91b450ef5f66e9980d40eb187582eed399fa851f4a7a45bb880de79"},+]++[package.dependencies]+googleapis-common-protos = ">=1.52,<2.0"+opentelemetry-api = ">=1.15,<2.0"+opentelemetry-exporter-otlp-proto-common = "1.35.0"+opentelemetry-proto = "1.35.0"+opentelemetry-sdk = ">=1.35.0,<1.36.0"+requests = ">=2.7,<3.0"+typing-extensions = ">=4.5.0"++[[package]]+name = "opentelemetry-proto"+version = "1.35.0"+description = "OpenTelemetry Python Proto"+optional = false+python-versions = ">=3.9"+groups = ["main"]+files = [+ {file = "opentelemetry_proto-1.35.0-py3-none-any.whl", hash = "sha256:98fffa803164499f562718384e703be8d7dfbe680192279a0429cb150a2f8809"},+ {file = "opentelemetry_proto-1.35.0.tar.gz", hash = "sha256:532497341bd3e1c074def7c5b00172601b28bb83b48afc41a4b779f26eb4ee05"},+]++[package.dependencies]+protobuf = ">=5.0,<7.0"++[[package]]+name = "opentelemetry-sdk"+version = "1.35.0"+description = "OpenTelemetry Python SDK"+optional = false+python-versions = ">=3.9"+groups = ["main"]+files = [+ {file = "opentelemetry_sdk-1.35.0-py3-none-any.whl", hash = "sha256:223d9e5f5678518f4842311bb73966e0b6db5d1e0b74e35074c052cd2487f800"},+ {file = "opentelemetry_sdk-1.35.0.tar.gz", hash = "sha256:2a400b415ab68aaa6f04e8a6a9f6552908fb3090ae2ff78d6ae0c597ac581954"},+]++[package.dependencies]+opentelemetry-api = "1.35.0"+opentelemetry-semantic-conventions = "0.56b0"+typing-extensions = ">=4.5.0"++[[package]]+name = "opentelemetry-semantic-conventions"+version = "0.56b0"+description = "OpenTelemetry Semantic Conventions"+optional = false+python-versions = ">=3.9"+groups = ["main"]+files = [+ {file = "opentelemetry_semantic_conventions-0.56b0-py3-none-any.whl", hash = "sha256:df44492868fd6b482511cc43a942e7194be64e94945f572db24df2e279a001a2"},+ {file = "opentelemetry_semantic_conventions-0.56b0.tar.gz", hash = "sha256:c114c2eacc8ff6d3908cb328c811eaf64e6d68623840be9224dc829c4fd6c2ea"},+]++[package.dependencies]+opentelemetry-api = "1.35.0"+typing-extensions = ">=4.5.0"+[[package]]name = "orjson"version = "3.11.3"@@ -6922,6 +7030,97 @@ files = [{file = "websockets-15.0.1.tar.gz", hash = "sha256:82544de02076bafba038ce055ee6412d68da13ab47f0c60cab827346de828dee"},]+[[package]]+name = "wrapt"+version = "1.17.3"+description = "Module for decorators, wrappers and monkey patching."+optional = false+python-versions = ">=3.8"+groups = ["main"]+files = [+ {file = "wrapt-1.17.3-cp310-cp310-macosx_10_9_universal2.whl", hash = "sha256:88bbae4d40d5a46142e70d58bf664a89b6b4befaea7b2ecc14e03cedb8e06c04"},+ {file = "wrapt-1.17.3-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:e6b13af258d6a9ad602d57d889f83b9d5543acd471eee12eb51f5b01f8eb1bc2"},+ {file = "wrapt-1.17.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:fd341868a4b6714a5962c1af0bd44f7c404ef78720c7de4892901e540417111c"},+ {file = "wrapt-1.17.3-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:f9b2601381be482f70e5d1051a5965c25fb3625455a2bf520b5a077b22afb775"},+ {file = "wrapt-1.17.3-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:343e44b2a8e60e06a7e0d29c1671a0d9951f59174f3709962b5143f60a2a98bd"},+ {file = "wrapt-1.17.3-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:33486899acd2d7d3066156b03465b949da3fd41a5da6e394ec49d271baefcf05"},+ {file = "wrapt-1.17.3-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:e6f40a8aa5a92f150bdb3e1c44b7e98fb7113955b2e5394122fa5532fec4b418"},+ {file = "wrapt-1.17.3-cp310-cp310-win32.whl", hash = "sha256:a36692b8491d30a8c75f1dfee65bef119d6f39ea84ee04d9f9311f83c5ad9390"},+ {file = "wrapt-1.17.3-cp310-cp310-win_amd64.whl", hash = "sha256:afd964fd43b10c12213574db492cb8f73b2f0826c8df07a68288f8f19af2ebe6"},+ {file = "wrapt-1.17.3-cp310-cp310-win_arm64.whl", hash = "sha256:af338aa93554be859173c39c85243970dc6a289fa907402289eeae7543e1ae18"},+ {file = "wrapt-1.17.3-cp311-cp311-macosx_10_9_universal2.whl", hash = "sha256:273a736c4645e63ac582c60a56b0acb529ef07f78e08dc6bfadf6a46b19c0da7"},+ {file = "wrapt-1.17.3-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:5531d911795e3f935a9c23eb1c8c03c211661a5060aab167065896bbf62a5f85"},+ {file = "wrapt-1.17.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:0610b46293c59a3adbae3dee552b648b984176f8562ee0dba099a56cfbe4df1f"},+ {file = "wrapt-1.17.3-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:b32888aad8b6e68f83a8fdccbf3165f5469702a7544472bdf41f582970ed3311"},+ {file = "wrapt-1.17.3-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8cccf4f81371f257440c88faed6b74f1053eef90807b77e31ca057b2db74edb1"},+ {file = "wrapt-1.17.3-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:d8a210b158a34164de8bb68b0e7780041a903d7b00c87e906fb69928bf7890d5"},+ {file = "wrapt-1.17.3-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:79573c24a46ce11aab457b472efd8d125e5a51da2d1d24387666cd85f54c05b2"},+ {file = "wrapt-1.17.3-cp311-cp311-win32.whl", hash = "sha256:c31eebe420a9a5d2887b13000b043ff6ca27c452a9a22fa71f35f118e8d4bf89"},+ {file = "wrapt-1.17.3-cp311-cp311-win_amd64.whl", hash = "sha256:0b1831115c97f0663cb77aa27d381237e73ad4f721391a9bfb2fe8bc25fa6e77"},+ {file = "wrapt-1.17.3-cp311-cp311-win_arm64.whl", hash = "sha256:5a7b3c1ee8265eb4c8f1b7d29943f195c00673f5ab60c192eba2d4a7eae5f46a"},+ {file = "wrapt-1.17.3-cp312-cp312-macosx_10_13_universal2.whl", hash = "sha256:ab232e7fdb44cdfbf55fc3afa31bcdb0d8980b9b95c38b6405df2acb672af0e0"},+ {file = "wrapt-1.17.3-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:9baa544e6acc91130e926e8c802a17f3b16fbea0fd441b5a60f5cf2cc5c3deba"},+ {file = "wrapt-1.17.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:6b538e31eca1a7ea4605e44f81a48aa24c4632a277431a6ed3f328835901f4fd"},+ {file = "wrapt-1.17.3-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:042ec3bb8f319c147b1301f2393bc19dba6e176b7da446853406d041c36c7828"},+ {file = "wrapt-1.17.3-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3af60380ba0b7b5aeb329bc4e402acd25bd877e98b3727b0135cb5c2efdaefe9"},+ {file = "wrapt-1.17.3-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:0b02e424deef65c9f7326d8c19220a2c9040c51dc165cddb732f16198c168396"},+ {file = "wrapt-1.17.3-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:74afa28374a3c3a11b3b5e5fca0ae03bef8450d6aa3ab3a1e2c30e3a75d023dc"},+ {file = "wrapt-1.17.3-cp312-cp312-win32.whl", hash = "sha256:4da9f45279fff3543c371d5ababc57a0384f70be244de7759c85a7f989cb4ebe"},+ {file = "wrapt-1.17.3-cp312-cp312-win_amd64.whl", hash = "sha256:e71d5c6ebac14875668a1e90baf2ea0ef5b7ac7918355850c0908ae82bcb297c"},+ {file = "wrapt-1.17.3-cp312-cp312-win_arm64.whl", hash = "sha256:604d076c55e2fdd4c1c03d06dc1a31b95130010517b5019db15365ec4a405fc6"},+ {file = "wrapt-1.17.3-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:a47681378a0439215912ef542c45a783484d4dd82bac412b71e59cf9c0e1cea0"},+ {file = "wrapt-1.17.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:54a30837587c6ee3cd1a4d1c2ec5d24e77984d44e2f34547e2323ddb4e22eb77"},+ {file = "wrapt-1.17.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:16ecf15d6af39246fe33e507105d67e4b81d8f8d2c6598ff7e3ca1b8a37213f7"},+ {file = "wrapt-1.17.3-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:6fd1ad24dc235e4ab88cda009e19bf347aabb975e44fd5c2fb22a3f6e4141277"},+ {file = "wrapt-1.17.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:0ed61b7c2d49cee3c027372df5809a59d60cf1b6c2f81ee980a091f3afed6a2d"},+ {file = "wrapt-1.17.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:423ed5420ad5f5529db9ce89eac09c8a2f97da18eb1c870237e84c5a5c2d60aa"},+ {file = "wrapt-1.17.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:e01375f275f010fcbf7f643b4279896d04e571889b8a5b3f848423d91bf07050"},+ {file = "wrapt-1.17.3-cp313-cp313-win32.whl", hash = "sha256:53e5e39ff71b3fc484df8a522c933ea2b7cdd0d5d15ae82e5b23fde87d44cbd8"},+ {file = "wrapt-1.17.3-cp313-cp313-win_amd64.whl", hash = "sha256:1f0b2f40cf341ee8cc1a97d51ff50dddb9fcc73241b9143ec74b30fc4f44f6cb"},+ {file = "wrapt-1.17.3-cp313-cp313-win_arm64.whl", hash = "sha256:7425ac3c54430f5fc5e7b6f41d41e704db073309acfc09305816bc6a0b26bb16"},+ {file = "wrapt-1.17.3-cp314-cp314-macosx_10_13_universal2.whl", hash = "sha256:cf30f6e3c077c8e6a9a7809c94551203c8843e74ba0c960f4a98cd80d4665d39"},+ {file = "wrapt-1.17.3-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:e228514a06843cae89621384cfe3a80418f3c04aadf8a3b14e46a7be704e4235"},+ {file = "wrapt-1.17.3-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:5ea5eb3c0c071862997d6f3e02af1d055f381b1d25b286b9d6644b79db77657c"},+ {file = "wrapt-1.17.3-cp314-cp314-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:281262213373b6d5e4bb4353bc36d1ba4084e6d6b5d242863721ef2bf2c2930b"},+ {file = "wrapt-1.17.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:dc4a8d2b25efb6681ecacad42fca8859f88092d8732b170de6a5dddd80a1c8fa"},+ {file = "wrapt-1.17.3-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:373342dd05b1d07d752cecbec0c41817231f29f3a89aa8b8843f7b95992ed0c7"},+ {file = "wrapt-1.17.3-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:d40770d7c0fd5cbed9d84b2c3f2e156431a12c9a37dc6284060fb4bec0b7ffd4"},+ {file = "wrapt-1.17.3-cp314-cp314-win32.whl", hash = "sha256:fbd3c8319de8e1dc79d346929cd71d523622da527cca14e0c1d257e31c2b8b10"},+ {file = "wrapt-1.17.3-cp314-cp314-win_amd64.whl", hash = "sha256:e1a4120ae5705f673727d3253de3ed0e016f7cd78dc463db1b31e2463e1f3cf6"},+ {file = "wrapt-1.17.3-cp314-cp314-win_arm64.whl", hash = "sha256:507553480670cab08a800b9463bdb881b2edeed77dc677b0a5915e6106e91a58"},+ {file = "wrapt-1.17.3-cp314-cp314t-macosx_10_13_universal2.whl", hash = "sha256:ed7c635ae45cfbc1a7371f708727bf74690daedc49b4dba310590ca0bd28aa8a"},+ {file = "wrapt-1.17.3-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:249f88ed15503f6492a71f01442abddd73856a0032ae860de6d75ca62eed8067"},+ {file = "wrapt-1.17.3-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:5a03a38adec8066d5a37bea22f2ba6bbf39fcdefbe2d91419ab864c3fb515454"},+ {file = "wrapt-1.17.3-cp314-cp314t-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:5d4478d72eb61c36e5b446e375bbc49ed002430d17cdec3cecb36993398e1a9e"},+ {file = "wrapt-1.17.3-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:223db574bb38637e8230eb14b185565023ab624474df94d2af18f1cdb625216f"},+ {file = "wrapt-1.17.3-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:e405adefb53a435f01efa7ccdec012c016b5a1d3f35459990afc39b6be4d5056"},+ {file = "wrapt-1.17.3-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:88547535b787a6c9ce4086917b6e1d291aa8ed914fdd3a838b3539dc95c12804"},+ {file = "wrapt-1.17.3-cp314-cp314t-win32.whl", hash = "sha256:41b1d2bc74c2cac6f9074df52b2efbef2b30bdfe5f40cb78f8ca22963bc62977"},+ {file = "wrapt-1.17.3-cp314-cp314t-win_amd64.whl", hash = "sha256:73d496de46cd2cdbdbcce4ae4bcdb4afb6a11234a1df9c085249d55166b95116"},+ {file = "wrapt-1.17.3-cp314-cp314t-win_arm64.whl", hash = "sha256:f38e60678850c42461d4202739f9bf1e3a737c7ad283638251e79cc49effb6b6"},+ {file = "wrapt-1.17.3-cp38-cp38-macosx_10_9_universal2.whl", hash = "sha256:70d86fa5197b8947a2fa70260b48e400bf2ccacdcab97bb7de47e3d1e6312225"},+ {file = "wrapt-1.17.3-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:df7d30371a2accfe4013e90445f6388c570f103d61019b6b7c57e0265250072a"},+ {file = "wrapt-1.17.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:caea3e9c79d5f0d2c6d9ab96111601797ea5da8e6d0723f77eabb0d4068d2b2f"},+ {file = "wrapt-1.17.3-cp38-cp38-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:758895b01d546812d1f42204bd443b8c433c44d090248bf22689df673ccafe00"},+ {file = "wrapt-1.17.3-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:02b551d101f31694fc785e58e0720ef7d9a10c4e62c1c9358ce6f63f23e30a56"},+ {file = "wrapt-1.17.3-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:656873859b3b50eeebe6db8b1455e99d90c26ab058db8e427046dbc35c3140a5"},+ {file = "wrapt-1.17.3-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:a9a2203361a6e6404f80b99234fe7fb37d1fc73487b5a78dc1aa5b97201e0f22"},+ {file = "wrapt-1.17.3-cp38-cp38-win32.whl", hash = "sha256:55cbbc356c2842f39bcc553cf695932e8b30e30e797f961860afb308e6b1bb7c"},+ {file = "wrapt-1.17.3-cp38-cp38-win_amd64.whl", hash = "sha256:ad85e269fe54d506b240d2d7b9f5f2057c2aa9a2ea5b32c66f8902f768117ed2"},+ {file = "wrapt-1.17.3-cp39-cp39-macosx_10_9_universal2.whl", hash = "sha256:30ce38e66630599e1193798285706903110d4f057aab3168a34b7fdc85569afc"},+ {file = "wrapt-1.17.3-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:65d1d00fbfb3ea5f20add88bbc0f815150dbbde3b026e6c24759466c8b5a9ef9"},+ {file = "wrapt-1.17.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:a7c06742645f914f26c7f1fa47b8bc4c91d222f76ee20116c43d5ef0912bba2d"},+ {file = "wrapt-1.17.3-cp39-cp39-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl", hash = "sha256:7e18f01b0c3e4a07fe6dfdb00e29049ba17eadbc5e7609a2a3a4af83ab7d710a"},+ {file = "wrapt-1.17.3-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:0f5f51a6466667a5a356e6381d362d259125b57f059103dd9fdc8c0cf1d14139"},+ {file = "wrapt-1.17.3-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:59923aa12d0157f6b82d686c3fd8e1166fa8cdfb3e17b42ce3b6147ff81528df"},+ {file = "wrapt-1.17.3-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:46acc57b331e0b3bcb3e1ca3b421d65637915cfcd65eb783cb2f78a511193f9b"},+ {file = "wrapt-1.17.3-cp39-cp39-win32.whl", hash = "sha256:3e62d15d3cfa26e3d0788094de7b64efa75f3a53875cdbccdf78547aed547a81"},+ {file = "wrapt-1.17.3-cp39-cp39-win_amd64.whl", hash = "sha256:1f23fa283f51c890eda8e34e4937079114c74b4c81d2b2f1f1d94948f5cc3d7f"},+ {file = "wrapt-1.17.3-cp39-cp39-win_arm64.whl", hash = "sha256:24c2ed34dc222ed754247a2702b1e1e89fdbaa4016f324b4b8f1a802d4ffe87f"},+ {file = "wrapt-1.17.3-py3-none-any.whl", hash = "sha256:7171ae35d2c33d326ac19dd8facb1e82e5fd04ef8c6c0e394d7af55a55051c22"},+ {file = "wrapt-1.17.3.tar.gz", hash = "sha256:f66eb08feaa410fe4eebd17f2a2c8e2e46d3476e9f8c783daa8e09e0faa666d0"},+]+[[package]]name = "xattr"version = "1.2.0"@@ -7295,4 +7494,4 @@ cffi = ["cffi (>=1.11)"][metadata]lock-version = "2.1"python-versions = ">=3.10,<3.14"-content-hash = "a93ba0cea3b465cb6ec3e3f258b383b09f84ea352ccfdbfa112902cde5653fc6"+content-hash = "86838b5ae40d606d6e01a14dad8a56c389d890d7a6a0c274a6602cca80f0df84"diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.tomlindex 21bf15e776..29b40709e5 100644--- a/autogpt_platform/backend/pyproject.toml+++ b/autogpt_platform/backend/pyproject.toml@@ -33,6 +33,7 @@ html2text = "^2024.2.26"jinja2 = "^3.1.6"jsonref = "^1.1.0"jsonschema = "^4.25.0"+langfuse = "^3.11.0"launchdarkly-server-sdk = "^9.12.0"mem0ai = "^0.1.115"moviepy = "^2.1.2"diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 0efca1bf3d..6e1a02f367 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -48,12 +48,13 @@ model User {AnalyticsMetrics AnalyticsMetrics[]CreditTransactions CreditTransaction[]UserBalance UserBalance?-- AgentPresets AgentPreset[]- LibraryAgents LibraryAgent[]+ ChatSessions ChatSession[]+ AgentPresets AgentPreset[]+ LibraryAgents LibraryAgent[]Profile Profile[]UserOnboarding UserOnboarding?+ CoPilotUnderstanding CoPilotUnderstanding?BuilderSearchHistory BuilderSearchHistory[]StoreListings StoreListing[]StoreListingReviews StoreListingReview[]@@ -122,6 +123,19 @@ model UserOnboarding {User User @relation(fields: [userId], references: [id], onDelete: Cascade)}+model CoPilotUnderstanding {+ id String @id @default(uuid())+ createdAt DateTime @default(now())+ updatedAt DateTime @default(now()) @updatedAt++ userId String @unique+ User User @relation(fields: [userId], references: [id], onDelete: Cascade)++ data Json?++ @@index([userId])+}+model BuilderSearchHistory {id String @id @default(uuid())createdAt DateTime @default(now())@@ -135,6 +149,58 @@ model BuilderSearchHistory {User User @relation(fields: [userId], references: [id], onDelete: Cascade)}+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////+//////////////// CHAT SESSION TABLES ///////////////////+////////////////////////////////////////////////////////////+////////////////////////////////////////////////////////////++model ChatSession {+ id String @id @default(uuid())+ createdAt DateTime @default(now())+ updatedAt DateTime @default(now()) @updatedAt++ userId String+ User User @relation(fields: [userId], references: [id], onDelete: Cascade)+ // Session metadata+ title String?+ credentials Json @default("{}") // Map of provider -> credential metadata++ // Rate limiting counters (stored as JSON maps)+ successfulAgentRuns Json @default("{}") // Map of graph_id -> count+ successfulAgentSchedules Json @default("{}") // Map of graph_id -> count++ // Usage tracking+ totalPromptTokens Int @default(0)+ totalCompletionTokens Int @default(0)++ Messages ChatMessage[]++ @@index([userId, updatedAt])+}++model ChatMessage {+ id String @id @default(uuid())+ createdAt DateTime @default(now())++ sessionId String+ Session ChatSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)++ // Message content+ role String // "user", "assistant", "system", "tool", "function"+ content String?+ name String?+ toolCallId String?+ refusal String?+ toolCalls Json? // List of tool calls for assistant messages+ functionCall Json? // Deprecated but kept for compatibility++ // Ordering within session+ sequence Int++ @@unique([sessionId, sequence])+}+// This model describes the Agent Graph/Flow (Multi Agent System).model AgentGraph {id String @default(uuid())diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 249d67e749..e601be6626 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -921,7 +921,7 @@"get": {"tags": ["v2", "chat", "chat"],"summary": "Health Check",- "description": "Health check endpoint for the chat service.\n\nPerforms a full cycle test of session creation, assignment, and retrieval. Should always return healthy\nif the service and data layer are operational.\n\nReturns:\n dict: A status dictionary indicating health, service name, and API version.",+ "description": "Health check endpoint for the chat service.\n\nPerforms a full cycle test of session creation and retrieval. Should always return healthy\nif the service and data layer are operational.\n\nReturns:\n dict: A status dictionary indicating health, service name, and API version.","operationId": "getV2HealthCheck","responses": {"200": {@@ -940,11 +940,67 @@}},"/api/chat/sessions": {+ "get": {+ "tags": ["v2", "chat", "chat"],+ "summary": "List Sessions",+ "description": "List chat sessions for the authenticated user.\n\nReturns a paginated list of chat sessions belonging to the current user,\nordered by most recently updated.\n\nArgs:\n user_id: The authenticated user's ID.\n limit: Maximum number of sessions to return (1-100).\n offset: Number of sessions to skip for pagination.\n\nReturns:\n ListSessionsResponse: List of session summaries and total count.",+ "operationId": "getV2ListSessions",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "limit",+ "in": "query",+ "required": false,+ "schema": {+ "type": "integer",+ "maximum": 100,+ "minimum": 1,+ "default": 50,+ "title": "Limit"+ }+ },+ {+ "name": "offset",+ "in": "query",+ "required": false,+ "schema": {+ "type": "integer",+ "minimum": 0,+ "default": 0,+ "title": "Offset"+ }+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/ListSessionsResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ },"post": {"tags": ["v2", "chat", "chat"],"summary": "Create Session",- "description": "Create a new chat session.\n\nInitiates a new chat session for either an authenticated or anonymous user.\n\nArgs:\n user_id: The optional authenticated user ID parsed from the JWT. If missing, creates an anonymous session.\n\nReturns:\n CreateSessionResponse: Details of the created session.",+ "description": "Create a new chat session.\n\nInitiates a new chat session for the authenticated user.\n\nArgs:\n user_id: The authenticated user ID parsed from the JWT (required).\n\nReturns:\n CreateSessionResponse: Details of the created session.","operationId": "postV2CreateSession",+ "security": [{ "HTTPBearerJWT": [] }],"responses": {"200": {"description": "Successful Response",@@ -959,8 +1015,7 @@"401": {"$ref": "#/components/responses/HTTP401NotAuthenticatedError"}- },- "security": [{ "HTTPBearerJWT": [] }]+ }}},"/api/chat/sessions/{session_id}": {@@ -1048,9 +1103,9 @@"/api/chat/sessions/{session_id}/stream": {"get": {"tags": ["v2", "chat", "chat"],- "summary": "Stream Chat",- "description": "Stream chat responses for a session.\n\nStreams the AI/completion responses in real time over Server-Sent Events (SSE), including:\n - Text fragments as they are generated\n - Tool call UI elements (if invoked)\n - Tool execution results\n\nArgs:\n session_id: The chat session identifier to associate with the streamed messages.\n message: The user's new message to process.\n user_id: Optional authenticated user ID.\n is_user_message: Whether the message is a user message.\nReturns:\n StreamingResponse: SSE-formatted response chunks.",- "operationId": "getV2StreamChat",+ "summary": "Stream Chat Get",+ "description": "Stream chat responses for a session (GET - legacy endpoint).\n\nStreams the AI/completion responses in real time over Server-Sent Events (SSE), including:\n - Text fragments as they are generated\n - Tool call UI elements (if invoked)\n - Tool execution results\n\nArgs:\n session_id: The chat session identifier to associate with the streamed messages.\n message: The user's new message to process.\n user_id: Optional authenticated user ID.\n is_user_message: Whether the message is a user message.\nReturns:\n StreamingResponse: SSE-formatted response chunks.",+ "operationId": "getV2StreamChatGet","security": [{ "HTTPBearerJWT": [] }],"parameters": [{@@ -1098,6 +1153,46 @@}}}+ },+ "post": {+ "tags": ["v2", "chat", "chat"],+ "summary": "Stream Chat Post",+ "description": "Stream chat responses for a session (POST with context support).\n\nStreams the AI/completion responses in real time over Server-Sent Events (SSE), including:\n - Text fragments as they are generated\n - Tool call UI elements (if invoked)\n - Tool execution results\n\nArgs:\n session_id: The chat session identifier to associate with the streamed messages.\n request: Request body containing message, is_user_message, and optional context.\n user_id: Optional authenticated user ID.\nReturns:\n StreamingResponse: SSE-formatted response chunks.",+ "operationId": "postV2StreamChatPost",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "session_id",+ "in": "path",+ "required": true,+ "schema": { "type": "string", "title": "Session Id" }+ }+ ],+ "requestBody": {+ "required": true,+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/StreamChatRequest" }+ }+ }+ },+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": { "application/json": { "schema": {} } }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }}},"/api/credits": {@@ -8019,6 +8114,20 @@"required": ["source_id", "sink_id", "source_name", "sink_name"],"title": "Link"},+ "ListSessionsResponse": {+ "properties": {+ "sessions": {+ "items": { "$ref": "#/components/schemas/SessionSummaryResponse" },+ "type": "array",+ "title": "Sessions"+ },+ "total": { "type": "integer", "title": "Total" }+ },+ "type": "object",+ "required": ["sessions", "total"],+ "title": "ListSessionsResponse",+ "description": "Response model for listing chat sessions."+ },"LogRawMetricRequest": {"properties": {"metric_name": {@@ -9348,6 +9457,21 @@"title": "SessionDetailResponse","description": "Response model providing complete details for a chat session, including messages."},+ "SessionSummaryResponse": {+ "properties": {+ "id": { "type": "string", "title": "Id" },+ "created_at": { "type": "string", "title": "Created At" },+ "updated_at": { "type": "string", "title": "Updated At" },+ "title": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Title"+ }+ },+ "type": "object",+ "required": ["id", "created_at", "updated_at"],+ "title": "SessionSummaryResponse",+ "description": "Response model for a session summary (without messages)."+ },"SetGraphActiveVersion": {"properties": {"active_graph_version": {@@ -9899,6 +10023,30 @@"required": ["submissions", "pagination"],"title": "StoreSubmissionsResponse"},+ "StreamChatRequest": {+ "properties": {+ "message": { "type": "string", "title": "Message" },+ "is_user_message": {+ "type": "boolean",+ "title": "Is User Message",+ "default": true+ },+ "context": {+ "anyOf": [+ {+ "additionalProperties": { "type": "string" },+ "type": "object"+ },+ { "type": "null" }+ ],+ "title": "Context"+ }+ },+ "type": "object",+ "required": ["message"],+ "title": "StreamChatRequest",+ "description": "Request model for streaming chat with optional context."+ },"SubmissionStatus": {"type": "string","enum": ["DRAFT", "PENDING", "APPROVED", "REJECTED"],