fix(backend): remove duplicate session creation from execute_copilot

Session creation was moved to run() in the previous commit. Clean up
the now-redundant create_chat_session import and logic in execute_copilot.
This commit is contained in:
Zamil Majdy
2026-03-17 00:37:08 +07:00
parent 040d1e851c
commit 3e459c1235
4 changed files with 24 additions and 14 deletions

View File

@@ -165,6 +165,7 @@ class AutoPilotBlock(Block):
),
],
test_mock={
"create_session": lambda *args, **kwargs: "test-session-id",
"execute_copilot": lambda *args, **kwargs: (
"You have 2 agents: Agent A and Agent B.",
[],
@@ -179,6 +180,13 @@ class AutoPilotBlock(Block):
},
)
async def create_session(self, user_id: str) -> str:
"""Create a new chat session and return its ID (mockable for tests)."""
from backend.copilot.model import create_chat_session
session = await create_chat_session(user_id)
return session.session_id
async def execute_copilot(
self,
prompt: str,
@@ -193,7 +201,7 @@ class AutoPilotBlock(Block):
then let stream_chat_completion_sdk handle everything (session loading,
message append, lock, transcript, cleanup).
"""
from backend.copilot.model import create_chat_session, get_chat_session
from backend.copilot.model import get_chat_session
from backend.copilot.response_model import (
StreamError,
StreamTextDelta,
@@ -205,12 +213,6 @@ class AutoPilotBlock(Block):
tokens = _check_recursion(max_recursion_depth)
try:
# Create session if needed — same as the chat API route.
# If session_id is provided, stream_chat_completion_sdk loads it.
if not session_id:
session = await create_chat_session(user_id)
session_id = session.session_id
effective_prompt = prompt
if system_context:
effective_prompt = f"[System Context: {system_context}]\n\n{prompt}"
@@ -292,14 +294,11 @@ class AutoPilotBlock(Block):
yield "error", "Cannot run autopilot without an authenticated user."
return
from backend.copilot.model import create_chat_session
# Create session eagerly so the user always gets the session_id,
# even if the downstream stream fails (avoids orphaned sessions).
sid = input_data.session_id
if not sid:
session = await create_chat_session(execution_context.user_id)
sid = session.session_id
sid = await self.create_session(execution_context.user_id)
try:
response, tool_calls, history, _, usage = await self.execute_copilot(

View File

@@ -112,6 +112,11 @@ CATEGORY_FILE_MAP = {
}
_BRAND_NAMES: dict[str, str] = {
"AutoPilot": "AutoPilot",
}
def class_name_to_display_name(class_name: str) -> str:
"""Convert BlockClassName to 'Block Class Name'."""
# Remove 'Block' suffix (only at the end, not all occurrences)
@@ -120,7 +125,13 @@ def class_name_to_display_name(class_name: str) -> str:
name = re.sub(r"([a-z])([A-Z])", r"\1 \2", name)
# Handle consecutive capitals (e.g., 'HTTPRequest' -> 'HTTP Request')
name = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1 \2", name)
return name.strip()
name = name.strip()
# Restore brand names that shouldn't be split
for split_form, brand in _BRAND_NAMES.items():
# Build the split version (e.g., "AutoPilot" -> "Auto Pilot")
split = re.sub(r"([a-z])([A-Z])", r"\1 \2", split_form)
name = name.replace(split, brand)
return name
def type_to_readable(type_schema: dict[str, Any] | Any) -> str:

View File

@@ -579,7 +579,7 @@ Below is a comprehensive list of all available blocks, categorized by their prim
| Block Name | Description |
|------------|-------------|
| [Agent Executor](block-integrations/misc.md#agent-executor) | Executes an existing agent inside your agent |
| [Auto Pilot](block-integrations/misc.md#auto-pilot) | Execute tasks using AutoGPT AutoPilot with full access to platform tools (agent management, workspace files, web fetch, block execution, and more) |
| [AutoPilot](block-integrations/misc.md#autopilot) | Execute tasks using AutoGPT AutoPilot with full access to platform tools (agent management, workspace files, web fetch, block execution, and more) |
## CRM Services

View File

@@ -38,7 +38,7 @@ Input and output schemas define the expected data structure for communication be
---
## Auto Pilot
## AutoPilot
### What it is
Execute tasks using AutoGPT AutoPilot with full access to platform tools (agent management, workspace files, web fetch, block execution, and more). Enables sub-agent patterns and scheduled autopilot execution.