Stop creating empty conversations that clutter sidebar with 'New Chat'

- agent.new_conversation() now just resets conv_id to None; DB row is
  created lazily by ensure_conversation() when user sends first message
- on_app_load no longer eagerly creates a conversation on page load
- list_conversations filters out conversations with no messages so
  orphaned empty rows don't appear in the sidebar

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
cora-start
PeninsulaInd 2026-02-23 11:08:34 -06:00
parent 603878e095
commit d771dd5c80
3 changed files with 19 additions and 11 deletions

View File

@ -100,10 +100,9 @@ class Agent:
self.db.create_conversation(self.conv_id, agent_name=self.name) self.db.create_conversation(self.conv_id, agent_name=self.name)
return self.conv_id return self.conv_id
def new_conversation(self) -> str: def new_conversation(self):
self.conv_id = uuid.uuid4().hex[:12] """Reset conversation state. DB row is created lazily by ensure_conversation()."""
self.db.create_conversation(self.conv_id, agent_name=self.name) self.conv_id = None
return self.conv_id
def load_conversation(self, conv_id: str) -> list[dict]: def load_conversation(self, conv_id: str) -> list[dict]:
"""Load an existing conversation by ID. Returns message list.""" """Load an existing conversation by ID. Returns message list."""

View File

@ -95,20 +95,32 @@ class Database:
return conv_id return conv_id
def list_conversations(self, limit: int = 50, agent_name: str | None = None) -> list[dict]: def list_conversations(self, limit: int = 50, agent_name: str | None = None) -> list[dict]:
# Only return conversations that have at least one message
if agent_name: if agent_name:
rows = self._conn.execute( rows = self._conn.execute(
"SELECT id, title, updated_at, agent_name FROM conversations" "SELECT c.id, c.title, c.updated_at, c.agent_name"
" WHERE agent_name = ? ORDER BY updated_at DESC LIMIT ?", " FROM conversations c"
" WHERE c.agent_name = ?"
" AND EXISTS (SELECT 1 FROM messages m WHERE m.conv_id = c.id)"
" ORDER BY c.updated_at DESC LIMIT ?",
(agent_name, limit), (agent_name, limit),
).fetchall() ).fetchall()
else: else:
rows = self._conn.execute( rows = self._conn.execute(
"SELECT id, title, updated_at, agent_name FROM conversations" "SELECT c.id, c.title, c.updated_at, c.agent_name"
" ORDER BY updated_at DESC LIMIT ?", " FROM conversations c"
" WHERE EXISTS (SELECT 1 FROM messages m WHERE m.conv_id = c.id)"
" ORDER BY c.updated_at DESC LIMIT ?",
(limit,), (limit,),
).fetchall() ).fetchall()
return [dict(r) for r in rows] return [dict(r) for r in rows]
def get_conversation_title(self, conv_id: str) -> str | None:
row = self._conn.execute(
"SELECT title FROM conversations WHERE id = ?", (conv_id,)
).fetchone()
return row["title"] if row else None
def update_conversation_title(self, conv_id: str, title: str): def update_conversation_title(self, conv_id: str, title: str):
self._conn.execute( self._conn.execute(
"UPDATE conversations SET title = ? WHERE id = ?", (title, conv_id) "UPDATE conversations SET title = ? WHERE id = ?", (title, conv_id)

View File

@ -442,9 +442,6 @@ def create_ui(
if conv_id and agent: if conv_id and agent:
messages = agent.load_conversation(conv_id) messages = agent.load_conversation(conv_id)
chatbot_msgs = _messages_to_chatbot(messages) chatbot_msgs = _messages_to_chatbot(messages)
elif agent:
agent.ensure_conversation()
conv_id = agent.conv_id
convs = _refresh_conv_list(agent_name) convs = _refresh_conv_list(agent_name)
new_browser = {"agent_name": agent_name, "conv_id": conv_id} new_browser = {"agent_name": agent_name, "conv_id": conv_id}