Skip to content

Commit 35aad02

Browse files
authored
Merge pull request #125 from MuhamadAjiW/feat/discord-connector
Feat/Discord Connector
2 parents 093eb6f + a3c9148 commit 35aad02

File tree

20 files changed

+2987
-1714
lines changed

20 files changed

+2987
-1714
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""Add DISCORD_CONNECTOR to SearchSourceConnectorType and DocumentType enums
2+
3+
Revision ID: 9
4+
Revises: 8
5+
"""
6+
7+
from typing import Sequence, Union
8+
9+
from alembic import op
10+
import sqlalchemy as sa
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision: str = "9"
15+
down_revision: Union[str, None] = "8"
16+
branch_labels: Union[str, Sequence[str], None] = None
17+
depends_on: Union[str, Sequence[str], None] = None
18+
19+
# Define the ENUM type name and the new value
20+
CONNECTOR_ENUM = "searchsourceconnectortype"
21+
CONNECTOR_NEW_VALUE = "DISCORD_CONNECTOR"
22+
DOCUMENT_ENUM = "documenttype"
23+
DOCUMENT_NEW_VALUE = "DISCORD_CONNECTOR"
24+
25+
26+
def upgrade() -> None:
27+
"""Upgrade schema - add DISCORD_CONNECTOR to connector and document enum."""
28+
# Add DISCORD_CONNECTOR to searchsourceconnectortype
29+
op.execute(f"ALTER TYPE {CONNECTOR_ENUM} ADD VALUE '{CONNECTOR_NEW_VALUE}'")
30+
# Add DISCORD_CONNECTOR to documenttype
31+
op.execute(f"ALTER TYPE {DOCUMENT_ENUM} ADD VALUE '{DOCUMENT_NEW_VALUE}'")
32+
33+
34+
def downgrade() -> None:
35+
"""Downgrade schema - remove DISCORD_CONNECTOR from connector and document enum."""
36+
37+
# Old enum name
38+
old_connector_enum_name = f"{CONNECTOR_ENUM}_old"
39+
old_document_enum_name = f"{DOCUMENT_ENUM}_old"
40+
41+
old_connector_values = (
42+
"SERPER_API",
43+
"TAVILY_API",
44+
"LINKUP_API",
45+
"SLACK_CONNECTOR",
46+
"NOTION_CONNECTOR",
47+
"GITHUB_CONNECTOR",
48+
"LINEAR_CONNECTOR",
49+
)
50+
old_document_values = (
51+
"EXTENSION",
52+
"CRAWLED_URL",
53+
"FILE",
54+
"SLACK_CONNECTOR",
55+
"NOTION_CONNECTOR",
56+
"YOUTUBE_VIDEO",
57+
"GITHUB_CONNECTOR",
58+
"LINEAR_CONNECTOR",
59+
)
60+
61+
old_connector_values_sql = ", ".join([f"'{v}'" for v in old_connector_values])
62+
old_document_values_sql = ", ".join([f"'{v}'" for v in old_document_values])
63+
64+
# Table and column names (adjust if different)
65+
connector_table_name = "search_source_connectors"
66+
connector_column_name = "connector_type"
67+
document_table_name = "documents"
68+
document_column_name = "document_type"
69+
70+
# Connector Enum Downgrade Steps
71+
# 1. Rename the current connector enum type
72+
op.execute(f"ALTER TYPE {CONNECTOR_ENUM} RENAME TO {old_connector_enum_name}")
73+
74+
# 2. Create the new connector enum type with the old values
75+
op.execute(f"CREATE TYPE {CONNECTOR_ENUM} AS ENUM({old_connector_values_sql})")
76+
77+
# 3. Update the connector table:
78+
op.execute(
79+
f"ALTER TABLE {connector_table_name} "
80+
f"ALTER COLUMN {connector_column_name} "
81+
f"TYPE {CONNECTOR_ENUM} "
82+
f"USING {connector_column_name}::text::{CONNECTOR_ENUM}"
83+
)
84+
85+
# 4. Drop the old connector enum type
86+
op.execute(f"DROP TYPE {old_connector_enum_name}")
87+
88+
89+
# Document Enum Downgrade Steps
90+
# 1. Rename the current document enum type
91+
op.execute(f"ALTER TYPE {DOCUMENT_ENUM} RENAME TO {old_document_enum_name}")
92+
93+
# 2. Create the new document enum type with the old values
94+
op.execute(f"CREATE TYPE {DOCUMENT_ENUM} AS ENUM({old_document_values_sql})")
95+
96+
# 3. Delete rows with the new value from the documents table
97+
op.execute(
98+
f"DELETE FROM {document_table_name} WHERE {document_column_name}::text = '{DOCUMENT_NEW_VALUE}'"
99+
)
100+
101+
# 4. Alter the document table to use the new enum type (casting old values)
102+
op.execute(
103+
f"ALTER TABLE {document_table_name} "
104+
f"ALTER COLUMN {document_column_name} "
105+
f"TYPE {DOCUMENT_ENUM} "
106+
f"USING {document_column_name}::text::{DOCUMENT_ENUM}"
107+
)
108+
109+
# 5. Drop the old enum types
110+
op.execute(f"DROP TYPE {old_document_enum_name}")
111+
112+
# ### end Alembic commands ###

surfsense_backend/app/agents/researcher/nodes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ async def fetch_relevant_documents(
400400
if streaming_service and writer:
401401
streaming_service.only_update_terminal(f"🔗 Found {len(linkup_chunks)} Linkup results related to your query")
402402
writer({"yeild_value": streaming_service._format_annotations()})
403+
404+
elif connector == "DISCORD_CONNECTOR":
405+
source_object, discord_chunks = await connector_service.search_discord(
406+
user_query=reformulated_query,
407+
user_id=user_id,
408+
search_space_id=search_space_id,
409+
top_k=top_k,
410+
search_mode=search_mode
411+
)
412+
# Add to sources and raw documents
413+
if source_object:
414+
all_sources.append(source_object)
415+
all_raw_documents.extend(discord_chunks)
416+
# Stream found document count
417+
if streaming_service and writer:
418+
streaming_service.only_update_terminal(f"🗨️ Found {len(discord_chunks)} Discord messages related to your query")
419+
writer({"yeild_value": streaming_service._format_annotations()})
403420

404421

405422
except Exception as e:

surfsense_backend/app/agents/researcher/sub_section_writer/prompts.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def get_citation_system_prompt():
1515
- YOUTUBE_VIDEO: "YouTube video transcripts and metadata" (personally saved videos)
1616
- GITHUB_CONNECTOR: "GitHub repository content and issues" (personal repositories and interactions)
1717
- LINEAR_CONNECTOR: "Linear project issues and discussions" (personal project management)
18+
- DISCORD_CONNECTOR: "Discord server messages and channels" (personal community interactions)
1819
- TAVILY_API: "Tavily search API results" (personalized search results)
1920
- LINKUP_API: "Linkup search API results" (personalized search results)
2021
</knowledge_sources>

0 commit comments

Comments
 (0)