mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-22 21:28:12 +02:00
Merge pull request #242 from CREDO23/feature/clickup-connector
[Feature] Add clickup connector
This commit is contained in:
commit
85fb6c6d39
13 changed files with 1101 additions and 3 deletions
|
|
@ -0,0 +1,63 @@
|
|||
"""Add ClickUp connector enums
|
||||
|
||||
Revision ID: '15'
|
||||
Revises: '14'
|
||||
Create Date: 2025-07-29 12:00:00.000000
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "15"
|
||||
down_revision: str | None = "14"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Safely add 'CLICKUP_CONNECTOR' to enum types if missing."""
|
||||
|
||||
# Add to searchsourceconnectortype enum
|
||||
op.execute(
|
||||
"""
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_type t
|
||||
JOIN pg_enum e ON t.oid = e.enumtypid
|
||||
WHERE t.typname = 'searchsourceconnectortype' AND e.enumlabel = 'CLICKUP_CONNECTOR'
|
||||
) THEN
|
||||
ALTER TYPE searchsourceconnectortype ADD VALUE 'CLICKUP_CONNECTOR';
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
"""
|
||||
)
|
||||
|
||||
# Add to documenttype enum
|
||||
op.execute(
|
||||
"""
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_type t
|
||||
JOIN pg_enum e ON t.oid = e.enumtypid
|
||||
WHERE t.typname = 'documenttype' AND e.enumlabel = 'CLICKUP_CONNECTOR'
|
||||
) THEN
|
||||
ALTER TYPE documenttype ADD VALUE 'CLICKUP_CONNECTOR';
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Remove 'CLICKUP_CONNECTOR' from enum types."""
|
||||
# Note: PostgreSQL doesn't support removing enum values directly
|
||||
# This would require recreating the enum type, which is complex
|
||||
# For now, we'll leave the enum values in place
|
||||
pass
|
||||
|
|
@ -945,6 +945,32 @@ async def fetch_relevant_documents(
|
|||
)
|
||||
}
|
||||
)
|
||||
elif connector == "CLICKUP_CONNECTOR":
|
||||
(
|
||||
source_object,
|
||||
clickup_chunks,
|
||||
) = await connector_service.search_clickup(
|
||||
user_query=reformulated_query,
|
||||
user_id=user_id,
|
||||
search_space_id=search_space_id,
|
||||
top_k=top_k,
|
||||
search_mode=search_mode,
|
||||
)
|
||||
|
||||
# Add to sources and raw documents
|
||||
if source_object:
|
||||
all_sources.append(source_object)
|
||||
all_raw_documents.extend(clickup_chunks)
|
||||
|
||||
# Stream found document count
|
||||
if streaming_service and writer:
|
||||
writer(
|
||||
{
|
||||
"yield_value": streaming_service.format_terminal_info_delta(
|
||||
f"📋 Found {len(clickup_chunks)} ClickUp tasks related to your query"
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
error_message = f"Error searching connector {connector}: {e!s}"
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ You are SurfSense, an advanced AI research assistant that provides detailed, wel
|
|||
- LINEAR_CONNECTOR: "Linear project issues and discussions" (personal project management)
|
||||
- JIRA_CONNECTOR: "Jira project issues, tickets, and comments" (personal project tracking)
|
||||
- CONFLUENCE_CONNECTOR: "Confluence pages and comments" (personal project documentation)
|
||||
- CLICKUP_CONNECTOR: "ClickUp tasks and project data" (personal task management)
|
||||
- DISCORD_CONNECTOR: "Discord server conversations and shared content" (personal community communications)
|
||||
- TAVILY_API: "Tavily search API results" (personalized search results)
|
||||
- LINKUP_API: "Linkup search API results" (personalized search results)
|
||||
|
|
|
|||
240
surfsense_backend/app/connectors/clickup_connector.py
Normal file
240
surfsense_backend/app/connectors/clickup_connector.py
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
"""
|
||||
ClickUp Connector Module
|
||||
|
||||
A module for retrieving data from ClickUp.
|
||||
Allows fetching tasks from workspaces and lists.
|
||||
"""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class ClickUpConnector:
|
||||
"""Class for retrieving data from ClickUp."""
|
||||
|
||||
def __init__(self, api_token: str | None = None):
|
||||
"""
|
||||
Initialize the ClickUpConnector class.
|
||||
|
||||
Args:
|
||||
api_token: ClickUp API token (optional)
|
||||
"""
|
||||
self.api_token = api_token
|
||||
self.base_url = "https://api.clickup.com/api/v2"
|
||||
|
||||
def set_api_token(self, api_token: str) -> None:
|
||||
"""
|
||||
Set the ClickUp API token.
|
||||
|
||||
Args:
|
||||
api_token: ClickUp API token
|
||||
"""
|
||||
self.api_token = api_token
|
||||
|
||||
def get_headers(self) -> dict[str, str]:
|
||||
"""
|
||||
Get headers for ClickUp API requests.
|
||||
|
||||
Returns:
|
||||
Dictionary of headers
|
||||
|
||||
Raises:
|
||||
ValueError: If api_token has not been set
|
||||
"""
|
||||
if not self.api_token:
|
||||
raise ValueError(
|
||||
"ClickUp API token not initialized. Call set_api_token() first."
|
||||
)
|
||||
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": self.api_token,
|
||||
}
|
||||
|
||||
def make_api_request(
|
||||
self, endpoint: str, params: dict[str, Any] | None = None
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Make a request to the ClickUp API.
|
||||
|
||||
Args:
|
||||
endpoint: API endpoint (without base URL)
|
||||
params: Query parameters for the request (optional)
|
||||
|
||||
Returns:
|
||||
Response data from the API
|
||||
|
||||
Raises:
|
||||
ValueError: If api_token has not been set
|
||||
Exception: If the API request fails
|
||||
"""
|
||||
if not self.api_token:
|
||||
raise ValueError(
|
||||
"ClickUp API token not initialized. Call set_api_token() first."
|
||||
)
|
||||
|
||||
url = f"{self.base_url}/{endpoint}"
|
||||
headers = self.get_headers()
|
||||
|
||||
response = requests.get(url, headers=headers, params=params, timeout=500)
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
raise Exception(
|
||||
f"API request failed with status code {response.status_code}: {response.text}"
|
||||
)
|
||||
|
||||
def get_authorized_workspaces(self) -> dict[str, Any]:
|
||||
"""
|
||||
Fetch authorized workspaces (teams) from ClickUp.
|
||||
|
||||
Returns:
|
||||
Dictionary containing teams data
|
||||
|
||||
Raises:
|
||||
ValueError: If credentials have not been set
|
||||
Exception: If the API request fails
|
||||
"""
|
||||
return self.make_api_request("team")
|
||||
|
||||
def get_workspace_tasks(
|
||||
self, workspace_id: str, include_closed: bool = False
|
||||
) -> list[dict[str, Any]]:
|
||||
"""
|
||||
Fetch all tasks from a ClickUp workspace using the filtered team tasks endpoint.
|
||||
|
||||
Args:
|
||||
workspace_id: ClickUp workspace (team) ID
|
||||
include_closed: Whether to include closed tasks (default: False)
|
||||
|
||||
Returns:
|
||||
List of task objects
|
||||
|
||||
Raises:
|
||||
ValueError: If credentials have not been set
|
||||
Exception: If the API request fails
|
||||
"""
|
||||
params = {
|
||||
"page": 0,
|
||||
"order_by": "created",
|
||||
"reverse": "true",
|
||||
"subtasks": "true",
|
||||
"include_closed": str(include_closed).lower(),
|
||||
}
|
||||
|
||||
all_tasks = []
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
params["page"] = page
|
||||
result = self.make_api_request(f"team/{workspace_id}/task", params)
|
||||
|
||||
if not isinstance(result, dict) or "tasks" not in result:
|
||||
break
|
||||
|
||||
tasks = result["tasks"]
|
||||
if not tasks:
|
||||
break
|
||||
|
||||
all_tasks.extend(tasks)
|
||||
|
||||
# Check if there are more pages
|
||||
if len(tasks) < 100: # ClickUp returns max 100 tasks per page
|
||||
break
|
||||
|
||||
page += 1
|
||||
|
||||
return all_tasks
|
||||
|
||||
def get_tasks_in_date_range(
|
||||
self,
|
||||
workspace_id: str,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
include_closed: bool = False,
|
||||
) -> tuple[list[dict[str, Any]], str | None]:
|
||||
"""
|
||||
Fetch tasks from ClickUp within a specific date range.
|
||||
|
||||
Args:
|
||||
workspace_id: ClickUp workspace (team) ID
|
||||
start_date: Start date in YYYY-MM-DD format
|
||||
end_date: End date in YYYY-MM-DD format
|
||||
include_closed: Whether to include closed tasks (default: False)
|
||||
|
||||
Returns:
|
||||
Tuple containing (tasks list, error message or None)
|
||||
"""
|
||||
try:
|
||||
# TODO : Include date range in api request
|
||||
|
||||
params = {
|
||||
"page": 0,
|
||||
"order_by": "created",
|
||||
"reverse": "true",
|
||||
"subtasks": "true",
|
||||
}
|
||||
|
||||
all_tasks = []
|
||||
page = 0
|
||||
|
||||
while True:
|
||||
params["page"] = page
|
||||
result = self.make_api_request(f"team/{workspace_id}/task", params)
|
||||
|
||||
if not isinstance(result, dict) or "tasks" not in result:
|
||||
return [], "Invalid response from ClickUp API"
|
||||
|
||||
tasks = result["tasks"]
|
||||
if not tasks:
|
||||
break
|
||||
|
||||
all_tasks.extend(tasks)
|
||||
|
||||
# Check if there are more pages
|
||||
if len(tasks) < 100: # ClickUp returns max 100 tasks per page
|
||||
break
|
||||
|
||||
page += 1
|
||||
|
||||
if not all_tasks:
|
||||
return [], "No tasks found in the specified date range."
|
||||
|
||||
return all_tasks, None
|
||||
|
||||
except Exception as e:
|
||||
return [], f"Error fetching tasks: {e!s}"
|
||||
|
||||
def get_task_details(self, task_id: str) -> dict[str, Any]:
|
||||
"""
|
||||
Fetch detailed information about a specific task.
|
||||
|
||||
Args:
|
||||
task_id: ClickUp task ID
|
||||
|
||||
Returns:
|
||||
Task details
|
||||
|
||||
Raises:
|
||||
ValueError: If credentials have not been set
|
||||
Exception: If the API request fails
|
||||
"""
|
||||
return self.make_api_request(f"task/{task_id}")
|
||||
|
||||
def get_task_comments(self, task_id: str) -> dict[str, Any]:
|
||||
"""
|
||||
Fetch comments for a specific task.
|
||||
|
||||
Args:
|
||||
task_id: ClickUp task ID
|
||||
|
||||
Returns:
|
||||
Task comments
|
||||
|
||||
Raises:
|
||||
ValueError: If credentials have not been set
|
||||
Exception: If the API request fails
|
||||
"""
|
||||
return self.make_api_request(f"task/{task_id}/comment")
|
||||
|
|
@ -44,6 +44,7 @@ class DocumentType(str, Enum):
|
|||
DISCORD_CONNECTOR = "DISCORD_CONNECTOR"
|
||||
JIRA_CONNECTOR = "JIRA_CONNECTOR"
|
||||
CONFLUENCE_CONNECTOR = "CONFLUENCE_CONNECTOR"
|
||||
CLICKUP_CONNECTOR = "CLICKUP_CONNECTOR"
|
||||
|
||||
|
||||
class SearchSourceConnectorType(str, Enum):
|
||||
|
|
@ -57,6 +58,7 @@ class SearchSourceConnectorType(str, Enum):
|
|||
DISCORD_CONNECTOR = "DISCORD_CONNECTOR"
|
||||
JIRA_CONNECTOR = "JIRA_CONNECTOR"
|
||||
CONFLUENCE_CONNECTOR = "CONFLUENCE_CONNECTOR"
|
||||
CLICKUP_CONNECTOR = "CLICKUP_CONNECTOR"
|
||||
|
||||
|
||||
class ChatType(str, Enum):
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@ from app.schemas import (
|
|||
SearchSourceConnectorUpdate,
|
||||
)
|
||||
from app.tasks.connectors_indexing_tasks import (
|
||||
index_clickup_tasks,
|
||||
index_confluence_pages,
|
||||
index_discord_messages,
|
||||
index_github_repos,
|
||||
|
|
@ -473,6 +474,21 @@ async def index_connector_content(
|
|||
)
|
||||
response_message = "Confluence indexing started in the background."
|
||||
|
||||
elif connector.connector_type == SearchSourceConnectorType.CLICKUP_CONNECTOR:
|
||||
# Run indexing in background
|
||||
logger.info(
|
||||
f"Triggering ClickUp indexing for connector {connector_id} into search space {search_space_id} from {indexing_from} to {indexing_to}"
|
||||
)
|
||||
background_tasks.add_task(
|
||||
run_clickup_indexing_with_new_session,
|
||||
connector_id,
|
||||
search_space_id,
|
||||
str(user.id),
|
||||
indexing_from,
|
||||
indexing_to,
|
||||
)
|
||||
response_message = "ClickUp indexing started in the background."
|
||||
|
||||
elif connector.connector_type == SearchSourceConnectorType.DISCORD_CONNECTOR:
|
||||
# Run indexing in background
|
||||
logger.info(
|
||||
|
|
@ -960,3 +976,61 @@ async def run_confluence_indexing(
|
|||
exc_info=True,
|
||||
)
|
||||
# Optionally update status in DB to indicate failure
|
||||
|
||||
|
||||
# Add new helper functions for ClickUp indexing
|
||||
async def run_clickup_indexing_with_new_session(
|
||||
connector_id: int,
|
||||
search_space_id: int,
|
||||
user_id: str,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
):
|
||||
"""Wrapper to run ClickUp indexing with its own database session."""
|
||||
logger.info(
|
||||
f"Background task started: Indexing ClickUp connector {connector_id} into space {search_space_id} from {start_date} to {end_date}"
|
||||
)
|
||||
async with async_session_maker() as session:
|
||||
await run_clickup_indexing(
|
||||
session, connector_id, search_space_id, user_id, start_date, end_date
|
||||
)
|
||||
logger.info(f"Background task finished: Indexing ClickUp connector {connector_id}")
|
||||
|
||||
|
||||
async def run_clickup_indexing(
|
||||
session: AsyncSession,
|
||||
connector_id: int,
|
||||
search_space_id: int,
|
||||
user_id: str,
|
||||
start_date: str,
|
||||
end_date: str,
|
||||
):
|
||||
"""Runs the ClickUp indexing task and updates the timestamp."""
|
||||
try:
|
||||
indexed_count, error_message = await index_clickup_tasks(
|
||||
session,
|
||||
connector_id,
|
||||
search_space_id,
|
||||
user_id,
|
||||
start_date,
|
||||
end_date,
|
||||
update_last_indexed=False,
|
||||
)
|
||||
if error_message:
|
||||
logger.error(
|
||||
f"ClickUp indexing failed for connector {connector_id}: {error_message}"
|
||||
)
|
||||
# Optionally update status in DB to indicate failure
|
||||
else:
|
||||
logger.info(
|
||||
f"ClickUp indexing successful for connector {connector_id}. Indexed {indexed_count} tasks."
|
||||
)
|
||||
# Update the last indexed timestamp only on success
|
||||
await update_connector_last_indexed(session, connector_id)
|
||||
await session.commit() # Commit timestamp update
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Critical error in run_clickup_indexing for connector {connector_id}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
# Optionally update status in DB to indicate failure
|
||||
|
|
|
|||
|
|
@ -167,6 +167,18 @@ class SearchSourceConnectorBase(BaseModel):
|
|||
if not config.get("CONFLUENCE_BASE_URL"):
|
||||
raise ValueError("CONFLUENCE_BASE_URL cannot be empty")
|
||||
|
||||
elif connector_type == SearchSourceConnectorType.CLICKUP_CONNECTOR:
|
||||
# For CLICKUP_CONNECTOR, only allow CLICKUP_API_TOKEN
|
||||
allowed_keys = ["CLICKUP_API_TOKEN"]
|
||||
if set(config.keys()) != set(allowed_keys):
|
||||
raise ValueError(
|
||||
f"For CLICKUP_CONNECTOR connector type, config must only contain these keys: {allowed_keys}"
|
||||
)
|
||||
|
||||
# Ensure the API token is not empty
|
||||
if not config.get("CLICKUP_API_TOKEN"):
|
||||
raise ValueError("CLICKUP_API_TOKEN cannot be empty")
|
||||
|
||||
return config
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1170,6 +1170,122 @@ class ConnectorService:
|
|||
|
||||
return result_object, confluence_chunks
|
||||
|
||||
async def search_clickup(
|
||||
self,
|
||||
user_query: str,
|
||||
user_id: str,
|
||||
search_space_id: int,
|
||||
top_k: int = 20,
|
||||
search_mode: SearchMode = SearchMode.CHUNKS,
|
||||
) -> tuple:
|
||||
"""
|
||||
Search for ClickUp tasks and return both the source information and langchain documents
|
||||
|
||||
Args:
|
||||
user_query: The user's query
|
||||
user_id: The user's ID
|
||||
search_space_id: The search space ID to search in
|
||||
top_k: Maximum number of results to return
|
||||
search_mode: Search mode (CHUNKS or DOCUMENTS)
|
||||
|
||||
Returns:
|
||||
tuple: (sources_info, langchain_documents)
|
||||
"""
|
||||
if search_mode == SearchMode.CHUNKS:
|
||||
clickup_chunks = await self.chunk_retriever.hybrid_search(
|
||||
query_text=user_query,
|
||||
top_k=top_k,
|
||||
user_id=user_id,
|
||||
search_space_id=search_space_id,
|
||||
document_type="CLICKUP_CONNECTOR",
|
||||
)
|
||||
elif search_mode == SearchMode.DOCUMENTS:
|
||||
clickup_chunks = await self.document_retriever.hybrid_search(
|
||||
query_text=user_query,
|
||||
top_k=top_k,
|
||||
user_id=user_id,
|
||||
search_space_id=search_space_id,
|
||||
document_type="CLICKUP_CONNECTOR",
|
||||
)
|
||||
# Transform document retriever results to match expected format
|
||||
clickup_chunks = self._transform_document_results(clickup_chunks)
|
||||
|
||||
# Early return if no results
|
||||
if not clickup_chunks:
|
||||
return {
|
||||
"id": 31,
|
||||
"name": "ClickUp Tasks",
|
||||
"type": "CLICKUP_CONNECTOR",
|
||||
"sources": [],
|
||||
}, []
|
||||
|
||||
sources_list = []
|
||||
|
||||
for chunk in clickup_chunks:
|
||||
# Extract document metadata
|
||||
document = chunk.get("document", {})
|
||||
metadata = document.get("metadata", {})
|
||||
|
||||
# Extract ClickUp task information from metadata
|
||||
task_name = metadata.get("task_name", "Unknown Task")
|
||||
task_id = metadata.get("task_id", "")
|
||||
task_url = metadata.get("task_url", "")
|
||||
task_status = metadata.get("task_status", "Unknown")
|
||||
task_priority = metadata.get("task_priority", "Unknown")
|
||||
task_assignees = metadata.get("task_assignees", [])
|
||||
task_due_date = metadata.get("task_due_date", "")
|
||||
task_list_name = metadata.get("task_list_name", "")
|
||||
task_space_name = metadata.get("task_space_name", "")
|
||||
|
||||
# Create description from task details
|
||||
description_parts = []
|
||||
if task_status:
|
||||
description_parts.append(f"Status: {task_status}")
|
||||
if task_priority:
|
||||
description_parts.append(f"Priority: {task_priority}")
|
||||
if task_assignees:
|
||||
assignee_names = [
|
||||
assignee.get("username", "Unknown") for assignee in task_assignees
|
||||
]
|
||||
description_parts.append(f"Assignees: {', '.join(assignee_names)}")
|
||||
if task_due_date:
|
||||
description_parts.append(f"Due: {task_due_date}")
|
||||
if task_list_name:
|
||||
description_parts.append(f"List: {task_list_name}")
|
||||
if task_space_name:
|
||||
description_parts.append(f"Space: {task_space_name}")
|
||||
|
||||
description = (
|
||||
" | ".join(description_parts) if description_parts else "ClickUp Task"
|
||||
)
|
||||
|
||||
source = {
|
||||
"id": document.get("id", self.source_id_counter),
|
||||
"title": task_name,
|
||||
"description": description,
|
||||
"url": task_url,
|
||||
"task_id": task_id,
|
||||
"status": task_status,
|
||||
"priority": task_priority,
|
||||
"assignees": task_assignees,
|
||||
"due_date": task_due_date,
|
||||
"list_name": task_list_name,
|
||||
"space_name": task_space_name,
|
||||
}
|
||||
|
||||
self.source_id_counter += 1
|
||||
sources_list.append(source)
|
||||
|
||||
# Create result object
|
||||
result_object = {
|
||||
"id": 31, # Assign a unique ID for the ClickUp connector
|
||||
"name": "ClickUp Tasks",
|
||||
"type": "CLICKUP_CONNECTOR",
|
||||
"sources": sources_list,
|
||||
}
|
||||
|
||||
return result_object, clickup_chunks
|
||||
|
||||
async def search_linkup(
|
||||
self, user_query: str, user_id: str, mode: str = "standard"
|
||||
) -> tuple:
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
|||
from sqlalchemy.future import select
|
||||
|
||||
from app.config import config
|
||||
from app.connectors.clickup_connector import ClickUpConnector
|
||||
from app.connectors.confluence_connector import ConfluenceConnector
|
||||
from app.connectors.discord_connector import DiscordConnector
|
||||
from app.connectors.github_connector import GitHubConnector
|
||||
|
|
@ -2698,3 +2699,316 @@ async def index_confluence_pages(
|
|||
)
|
||||
logger.error(f"Failed to index Confluence pages: {e!s}", exc_info=True)
|
||||
return 0, f"Failed to index Confluence pages: {e!s}"
|
||||
|
||||
|
||||
async def index_clickup_tasks(
|
||||
session: AsyncSession,
|
||||
connector_id: int,
|
||||
search_space_id: int,
|
||||
user_id: str,
|
||||
start_date: str | None = None,
|
||||
end_date: str | None = None,
|
||||
update_last_indexed: bool = True,
|
||||
) -> tuple[int, str | None]:
|
||||
"""
|
||||
Index tasks from ClickUp workspace.
|
||||
|
||||
Args:
|
||||
session: Database session
|
||||
connector_id: ID of the ClickUp connector
|
||||
search_space_id: ID of the search space
|
||||
user_id: ID of the user
|
||||
start_date: Start date for filtering tasks (YYYY-MM-DD format)
|
||||
end_date: End date for filtering tasks (YYYY-MM-DD format)
|
||||
update_last_indexed: Whether to update the last_indexed_at timestamp
|
||||
|
||||
Returns:
|
||||
Tuple of (number of indexed tasks, error message if any)
|
||||
"""
|
||||
task_logger = TaskLoggingService(session, search_space_id)
|
||||
|
||||
# Log task start
|
||||
log_entry = await task_logger.log_task_start(
|
||||
task_name="clickup_tasks_indexing",
|
||||
source="connector_indexing_task",
|
||||
message=f"Starting ClickUp tasks indexing for connector {connector_id}",
|
||||
metadata={
|
||||
"connector_id": connector_id,
|
||||
"start_date": start_date,
|
||||
"end_date": end_date,
|
||||
},
|
||||
)
|
||||
|
||||
try:
|
||||
# Get connector configuration
|
||||
result = await session.execute(
|
||||
select(SearchSourceConnector).filter(
|
||||
SearchSourceConnector.id == connector_id
|
||||
)
|
||||
)
|
||||
connector = result.scalars().first()
|
||||
|
||||
if not connector:
|
||||
error_msg = f"ClickUp connector with ID {connector_id} not found"
|
||||
await task_logger.log_task_failure(
|
||||
log_entry,
|
||||
f"Connector with ID {connector_id} not found or is not a ClickUp connector",
|
||||
"Connector not found",
|
||||
{"error_type": "ConnectorNotFound"},
|
||||
)
|
||||
return 0, error_msg
|
||||
|
||||
# Extract ClickUp configuration
|
||||
clickup_api_token = connector.config.get("CLICKUP_API_TOKEN")
|
||||
|
||||
if not clickup_api_token:
|
||||
error_msg = "ClickUp API token not found in connector configuration"
|
||||
await task_logger.log_task_failure(
|
||||
log_entry,
|
||||
f"ClickUp API token not found in connector config for connector {connector_id}",
|
||||
"Missing ClickUp token",
|
||||
{"error_type": "MissingToken"},
|
||||
)
|
||||
return 0, error_msg
|
||||
|
||||
await task_logger.log_task_progress(
|
||||
log_entry,
|
||||
f"Initializing ClickUp client for connector {connector_id}",
|
||||
{"stage": "client_initialization"},
|
||||
)
|
||||
|
||||
clickup_client = ClickUpConnector(api_token=clickup_api_token)
|
||||
|
||||
# Get authorized workspaces
|
||||
await task_logger.log_task_progress(
|
||||
log_entry,
|
||||
"Fetching authorized ClickUp workspaces",
|
||||
{"stage": "workspace_fetching"},
|
||||
)
|
||||
|
||||
workspaces_response = clickup_client.get_authorized_workspaces()
|
||||
workspaces = workspaces_response.get("teams", [])
|
||||
|
||||
if not workspaces:
|
||||
error_msg = "No authorized ClickUp workspaces found"
|
||||
await task_logger.log_task_failure(
|
||||
log_entry,
|
||||
f"No authorized ClickUp workspaces found for connector {connector_id}",
|
||||
"No workspaces found",
|
||||
{"error_type": "NoWorkspacesFound"},
|
||||
)
|
||||
return 0, error_msg
|
||||
|
||||
# Process and index each task
|
||||
documents_indexed = 0
|
||||
documents_skipped = 0
|
||||
|
||||
for workspace in workspaces:
|
||||
workspace_id = workspace.get("id")
|
||||
workspace_name = workspace.get("name", "Unknown Workspace")
|
||||
|
||||
if not workspace_id:
|
||||
continue
|
||||
|
||||
await task_logger.log_task_progress(
|
||||
log_entry,
|
||||
f"Processing workspace: {workspace_name}",
|
||||
{"stage": "workspace_processing", "workspace_id": workspace_id},
|
||||
)
|
||||
|
||||
# Fetch tasks from workspace
|
||||
if start_date and end_date:
|
||||
tasks, error = clickup_client.get_tasks_in_date_range(
|
||||
workspace_id=workspace_id,
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
include_closed=True,
|
||||
)
|
||||
if error:
|
||||
logger.warning(
|
||||
f"Error fetching tasks from workspace {workspace_name}: {error}"
|
||||
)
|
||||
continue
|
||||
else:
|
||||
tasks = clickup_client.get_workspace_tasks(
|
||||
workspace_id=workspace_id, include_closed=True
|
||||
)
|
||||
|
||||
await task_logger.log_task_progress(
|
||||
log_entry,
|
||||
f"Found {len(tasks)} tasks in workspace {workspace_name}",
|
||||
{"stage": "tasks_found", "task_count": len(tasks)},
|
||||
)
|
||||
|
||||
# Process each task
|
||||
for task in tasks:
|
||||
try:
|
||||
task_id = task.get("id")
|
||||
task_name = task.get("name", "Untitled Task")
|
||||
task_description = task.get("description", "")
|
||||
task_status = task.get("status", {}).get("status", "Unknown")
|
||||
task_priority = (
|
||||
task.get("priority", {}).get("priority", "Unknown")
|
||||
if task.get("priority")
|
||||
else "None"
|
||||
)
|
||||
task_assignees = task.get("assignees", [])
|
||||
task_due_date = task.get("due_date")
|
||||
task_created = task.get("date_created")
|
||||
task_updated = task.get("date_updated")
|
||||
|
||||
# Get list and space information
|
||||
task_list = task.get("list", {})
|
||||
task_list_name = task_list.get("name", "Unknown List")
|
||||
task_space = task.get("space", {})
|
||||
task_space_name = task_space.get("name", "Unknown Space")
|
||||
|
||||
# Create task content
|
||||
content_parts = [f"Task: {task_name}"]
|
||||
|
||||
if task_description:
|
||||
content_parts.append(f"Description: {task_description}")
|
||||
|
||||
content_parts.extend(
|
||||
[
|
||||
f"Status: {task_status}",
|
||||
f"Priority: {task_priority}",
|
||||
f"List: {task_list_name}",
|
||||
f"Space: {task_space_name}",
|
||||
]
|
||||
)
|
||||
|
||||
if task_assignees:
|
||||
assignee_names = [
|
||||
assignee.get("username", "Unknown")
|
||||
for assignee in task_assignees
|
||||
]
|
||||
content_parts.append(f"Assignees: {', '.join(assignee_names)}")
|
||||
|
||||
if task_due_date:
|
||||
content_parts.append(f"Due Date: {task_due_date}")
|
||||
|
||||
task_content = "\n".join(content_parts)
|
||||
|
||||
if not task_content.strip():
|
||||
logger.warning(f"Skipping task with no content: {task_name}")
|
||||
continue
|
||||
|
||||
# Generate content hash
|
||||
content_hash = generate_content_hash(task_content, search_space_id)
|
||||
|
||||
# Check if document already exists
|
||||
existing_doc_by_hash_result = await session.execute(
|
||||
select(Document).where(Document.content_hash == content_hash)
|
||||
)
|
||||
existing_document_by_hash = (
|
||||
existing_doc_by_hash_result.scalars().first()
|
||||
)
|
||||
|
||||
if existing_document_by_hash:
|
||||
logger.info(
|
||||
f"Document with content hash {content_hash} already exists for task {task_name}. Skipping processing."
|
||||
)
|
||||
documents_skipped += 1
|
||||
continue
|
||||
|
||||
# Generate embedding for the summary
|
||||
summary_embedding = config.embedding_model_instance.embed(
|
||||
task_content
|
||||
)
|
||||
|
||||
# Process chunks - using the full page content with comments
|
||||
chunks = [
|
||||
Chunk(
|
||||
content=chunk.text,
|
||||
embedding=config.embedding_model_instance.embed(chunk.text),
|
||||
)
|
||||
for chunk in config.chunker_instance.chunk(task_content)
|
||||
]
|
||||
|
||||
# Create and store new document
|
||||
logger.info(f"Creating new document for task {task_name}")
|
||||
|
||||
document = Document(
|
||||
search_space_id=search_space_id,
|
||||
title=f"Task - {task_name}",
|
||||
document_type=DocumentType.CLICKUP_CONNECTOR,
|
||||
document_metadata={
|
||||
"task_id": task_id,
|
||||
"task_name": task_name,
|
||||
"task_status": task_status,
|
||||
"task_priority": task_priority,
|
||||
"task_assignees": task_assignees,
|
||||
"task_due_date": task_due_date,
|
||||
"task_created": task_created,
|
||||
"task_updated": task_updated,
|
||||
"indexed_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
},
|
||||
content=task_content,
|
||||
content_hash=content_hash,
|
||||
embedding=summary_embedding,
|
||||
chunks=chunks,
|
||||
)
|
||||
|
||||
session.add(document)
|
||||
documents_indexed += 1
|
||||
logger.info(f"Successfully indexed new task {task_name}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Error processing task {task.get('name', 'Unknown')}: {e!s}",
|
||||
exc_info=True,
|
||||
)
|
||||
documents_skipped += 1
|
||||
|
||||
# Update the last_indexed_at timestamp for the connector only if requested
|
||||
total_processed = documents_indexed
|
||||
if update_last_indexed:
|
||||
connector.last_indexed_at = datetime.now()
|
||||
logger.info(f"Updated last_indexed_at to {connector.last_indexed_at}")
|
||||
|
||||
# Commit all changes
|
||||
await session.commit()
|
||||
logger.info(
|
||||
"Successfully committed all clickup document changes to database"
|
||||
)
|
||||
|
||||
# Log success
|
||||
await task_logger.log_task_success(
|
||||
log_entry,
|
||||
f"Successfully completed clickup indexing for connector {connector_id}",
|
||||
{
|
||||
"pages_processed": total_processed,
|
||||
"documents_indexed": documents_indexed,
|
||||
"documents_skipped": documents_skipped,
|
||||
},
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"clickup indexing completed: {documents_indexed} new tasks, {documents_skipped} skipped"
|
||||
)
|
||||
return (
|
||||
total_processed,
|
||||
None,
|
||||
) # Return None as the error message to indicate success
|
||||
|
||||
except SQLAlchemyError as db_error:
|
||||
await session.rollback()
|
||||
await task_logger.log_task_failure(
|
||||
log_entry,
|
||||
f"Database error during Cickup indexing for connector {connector_id}",
|
||||
str(db_error),
|
||||
{"error_type": "SQLAlchemyError"},
|
||||
)
|
||||
logger.error(f"Database error: {db_error!s}", exc_info=True)
|
||||
return 0, f"Database error: {db_error!s}"
|
||||
except Exception as e:
|
||||
await session.rollback()
|
||||
await task_logger.log_task_failure(
|
||||
log_entry,
|
||||
f"Failed to index ClickUp tasks for connector {connector_id}",
|
||||
str(e),
|
||||
{"error_type": type(e).__name__},
|
||||
)
|
||||
logger.error(f"Failed to index ClickUp tasks: {e!s}", exc_info=True)
|
||||
return 0, f"Failed to index ClickUp tasks: {e!s}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue