From 3faaa25af620d4090a68962e3c64819c7156cd59 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Thu, 28 May 2026 13:11:05 +0530 Subject: [PATCH] refactor(database): update default source values to 'surfsense' for chat threads and messages --- .../alembic/versions/144_add_gateway_tables.py | 4 ++-- surfsense_backend/app/db.py | 8 ++++---- surfsense_backend/app/gateway/agent_invoke.py | 5 ++++- surfsense_web/content/docs/manual-installation.mdx | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/surfsense_backend/alembic/versions/144_add_gateway_tables.py b/surfsense_backend/alembic/versions/144_add_gateway_tables.py index 35eb662c8..91d06f815 100644 --- a/surfsense_backend/alembic/versions/144_add_gateway_tables.py +++ b/surfsense_backend/alembic/versions/144_add_gateway_tables.py @@ -424,7 +424,7 @@ def upgrade() -> None: if not _column_exists(conn, "new_chat_threads", "source"): op.add_column( "new_chat_threads", - sa.Column("source", sa.Text(), nullable=False, server_default="web"), + sa.Column("source", sa.Text(), nullable=False, server_default="surfsense"), ) op.alter_column("new_chat_threads", "source", type_=sa.Text()) if not _column_exists(conn, "new_chat_threads", "external_chat_binding_id"): @@ -454,7 +454,7 @@ def upgrade() -> None: if not _column_exists(conn, "new_chat_messages", "source"): op.add_column( "new_chat_messages", - sa.Column("source", sa.Text(), nullable=False, server_default="web"), + sa.Column("source", sa.Text(), nullable=False, server_default="surfsense"), ) op.alter_column("new_chat_messages", "source", type_=sa.Text()) if not _column_exists(conn, "new_chat_messages", "platform_metadata"): diff --git a/surfsense_backend/app/db.py b/surfsense_backend/app/db.py index de7792627..e9b301ece 100644 --- a/surfsense_backend/app/db.py +++ b/surfsense_backend/app/db.py @@ -696,9 +696,9 @@ class NewChatThread(BaseModel, TimestampMixin): # agent_llm_id changes). Unindexed: all reads are by primary key. pinned_llm_config_id = Column(Integer, nullable=True) - # Surface metadata for web and external chat threads. Zero publishes all - # chat-message sources; the UI can decide which surfaces to render. - source = Column(Text, nullable=False, default="web", server_default="web") + # Surface metadata for first-party SurfSense and external chat threads. + # Zero publishes all chat-message sources; the UI can decide which surfaces to render. + source = Column(Text, nullable=False, default="surfsense", server_default="surfsense") external_chat_binding_id = Column( BigInteger, ForeignKey("external_chat_bindings.id", ondelete="SET NULL"), @@ -786,7 +786,7 @@ class NewChatMessage(BaseModel, TimestampMixin): # Mirrors the parent thread source for publication-level filtering. # This denormalization avoids join-dependent logical replication rules. - source = Column(Text, nullable=False, default="web", server_default="web") + source = Column(Text, nullable=False, default="surfsense", server_default="surfsense") platform_metadata = Column(JSONB, nullable=True) # Relationships diff --git a/surfsense_backend/app/gateway/agent_invoke.py b/surfsense_backend/app/gateway/agent_invoke.py index 39d1fb299..b195f3bce 100644 --- a/surfsense_backend/app/gateway/agent_invoke.py +++ b/surfsense_backend/app/gateway/agent_invoke.py @@ -88,7 +88,10 @@ async def call_agent_for_gateway( await stream.aclose() await session.execute( update(NewChatMessage) - .where(NewChatMessage.thread_id == thread.id, NewChatMessage.source == "web") + .where( + NewChatMessage.thread_id == thread.id, + NewChatMessage.source == "surfsense", + ) .values(source="telegram") ) await session.commit() diff --git a/surfsense_web/content/docs/manual-installation.mdx b/surfsense_web/content/docs/manual-installation.mdx index f977ef11d..602c9a30b 100644 --- a/surfsense_web/content/docs/manual-installation.mdx +++ b/surfsense_web/content/docs/manual-installation.mdx @@ -41,7 +41,7 @@ The backend is the core of SurfSense. Follow these steps to set it up: ### Optional: Telegram External Chat Surface -SurfSense can expose the same canonical chat agent through Telegram. The `external_chat_*` tables store adapter identity, delivery configuration, and durable inbox rows. The actual chat thread and messages remain in `new_chat_threads` and `new_chat_messages`, and all chat-message sources are eligible for Zero replication so a future SurfSense UI layer can render external chat surfaces. The web app initially shows pairing, health, revoke, and resume controls under **User Settings > Messaging Channels**. +SurfSense can expose the same canonical chat agent through Telegram. The `external_chat_*` tables store adapter identity, delivery configuration, and durable inbox rows. The actual chat thread and messages remain in `new_chat_threads` and `new_chat_messages`, with first-party web/desktop chats marked as `source="surfsense"` and external surfaces marked by platform, such as `source="telegram"`. All chat-message sources are eligible for Zero replication so a future SurfSense UI layer can render external chat surfaces. The web app initially shows pairing, health, revoke, and resume controls under **User Settings > Messaging Channels**. Add these variables to `surfsense_backend/.env` when enabling the Telegram surface: