2025-12-18 23:57:57 -08:00
|
|
|
"""
|
2025-12-19 20:40:10 +02:00
|
|
|
SurfSense deep agent implementation.
|
2025-12-18 23:57:57 -08:00
|
|
|
|
2025-12-19 20:40:10 +02:00
|
|
|
This module provides the factory function for creating SurfSense deep agents
|
2025-12-21 19:07:46 +05:30
|
|
|
with knowledge base search and podcast generation capabilities.
|
2025-12-18 23:57:57 -08:00
|
|
|
"""
|
|
|
|
|
|
2025-12-19 20:21:39 +02:00
|
|
|
from collections.abc import Sequence
|
2025-12-18 23:57:57 -08:00
|
|
|
|
|
|
|
|
from deepagents import create_deep_agent
|
2025-12-19 20:21:39 +02:00
|
|
|
from langchain_core.tools import BaseTool
|
2025-12-18 23:57:57 -08:00
|
|
|
from langchain_litellm import ChatLiteLLM
|
2025-12-21 03:30:10 -08:00
|
|
|
from langgraph.types import Checkpointer
|
2025-12-18 23:57:57 -08:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
2025-12-19 20:40:10 +02:00
|
|
|
from app.agents.new_chat.context import SurfSenseContextSchema
|
2025-12-23 01:11:56 +05:30
|
|
|
from app.agents.new_chat.display_image import create_display_image_tool
|
2025-12-19 20:40:10 +02:00
|
|
|
from app.agents.new_chat.knowledge_base import create_search_knowledge_base_tool
|
2025-12-23 00:58:27 +05:30
|
|
|
from app.agents.new_chat.link_preview import create_link_preview_tool
|
2025-12-21 19:07:46 +05:30
|
|
|
from app.agents.new_chat.podcast import create_generate_podcast_tool
|
2025-12-19 20:40:10 +02:00
|
|
|
from app.agents.new_chat.system_prompt import build_surfsense_system_prompt
|
2025-12-18 23:57:57 -08:00
|
|
|
from app.services.connector_service import ConnectorService
|
|
|
|
|
|
|
|
|
|
# =============================================================================
|
|
|
|
|
# Deep Agent Factory
|
|
|
|
|
# =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_surfsense_deep_agent(
|
|
|
|
|
llm: ChatLiteLLM,
|
|
|
|
|
search_space_id: int,
|
|
|
|
|
db_session: AsyncSession,
|
|
|
|
|
connector_service: ConnectorService,
|
2025-12-21 03:30:10 -08:00
|
|
|
checkpointer: Checkpointer,
|
2025-12-21 19:07:46 +05:30
|
|
|
user_id: str | None = None,
|
2025-12-19 19:11:44 +02:00
|
|
|
user_instructions: str | None = None,
|
2025-12-19 19:18:32 +02:00
|
|
|
enable_citations: bool = True,
|
2025-12-21 19:07:46 +05:30
|
|
|
enable_podcast: bool = True,
|
2025-12-23 00:58:27 +05:30
|
|
|
enable_link_preview: bool = True,
|
2025-12-23 01:11:56 +05:30
|
|
|
enable_display_image: bool = True,
|
2025-12-19 20:21:39 +02:00
|
|
|
additional_tools: Sequence[BaseTool] | None = None,
|
2025-12-18 23:57:57 -08:00
|
|
|
):
|
|
|
|
|
"""
|
2025-12-21 19:07:46 +05:30
|
|
|
Create a SurfSense deep agent with knowledge base search and podcast generation capabilities.
|
2025-12-18 23:57:57 -08:00
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
llm: ChatLiteLLM instance
|
|
|
|
|
search_space_id: The user's search space ID
|
|
|
|
|
db_session: Database session
|
|
|
|
|
connector_service: Initialized connector service
|
2025-12-21 03:30:10 -08:00
|
|
|
checkpointer: LangGraph checkpointer for conversation state persistence.
|
|
|
|
|
Use AsyncPostgresSaver for production or MemorySaver for testing.
|
2025-12-21 19:07:46 +05:30
|
|
|
user_id: The user's ID (required for podcast generation)
|
2025-12-19 19:11:44 +02:00
|
|
|
user_instructions: Optional user instructions to inject into the system prompt.
|
|
|
|
|
These will be added to the system prompt to customize agent behavior.
|
2025-12-19 19:18:32 +02:00
|
|
|
enable_citations: Whether to include citation instructions in the system prompt (default: True).
|
|
|
|
|
When False, the agent will not be instructed to add citations to responses.
|
2025-12-21 19:07:46 +05:30
|
|
|
enable_podcast: Whether to include the podcast generation tool (default: True).
|
|
|
|
|
When True and user_id is provided, the agent can generate podcasts.
|
2025-12-23 00:58:27 +05:30
|
|
|
enable_link_preview: Whether to include the link preview tool (default: True).
|
|
|
|
|
When True, the agent can fetch and display rich link previews.
|
2025-12-23 01:11:56 +05:30
|
|
|
enable_display_image: Whether to include the display image tool (default: True).
|
|
|
|
|
When True, the agent can display images with metadata.
|
2025-12-19 20:21:39 +02:00
|
|
|
additional_tools: Optional sequence of additional tools to inject into the agent.
|
|
|
|
|
The search_knowledge_base tool will always be included.
|
2025-12-18 23:57:57 -08:00
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
CompiledStateGraph: The configured deep agent
|
|
|
|
|
"""
|
|
|
|
|
# Create the search tool with injected dependencies
|
|
|
|
|
search_tool = create_search_knowledge_base_tool(
|
|
|
|
|
search_space_id=search_space_id,
|
|
|
|
|
db_session=db_session,
|
|
|
|
|
connector_service=connector_service,
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-19 20:21:39 +02:00
|
|
|
# Combine search tool with any additional tools
|
|
|
|
|
tools = [search_tool]
|
2025-12-21 19:07:46 +05:30
|
|
|
|
|
|
|
|
# Add podcast tool if enabled and user_id is provided
|
|
|
|
|
if enable_podcast and user_id:
|
|
|
|
|
podcast_tool = create_generate_podcast_tool(
|
|
|
|
|
search_space_id=search_space_id,
|
|
|
|
|
db_session=db_session,
|
|
|
|
|
user_id=str(user_id),
|
|
|
|
|
)
|
|
|
|
|
tools.append(podcast_tool)
|
|
|
|
|
|
2025-12-23 00:58:27 +05:30
|
|
|
# Add link preview tool if enabled
|
|
|
|
|
if enable_link_preview:
|
|
|
|
|
link_preview_tool = create_link_preview_tool()
|
|
|
|
|
tools.append(link_preview_tool)
|
|
|
|
|
|
2025-12-23 01:11:56 +05:30
|
|
|
# Add display image tool if enabled
|
|
|
|
|
if enable_display_image:
|
|
|
|
|
display_image_tool = create_display_image_tool()
|
|
|
|
|
tools.append(display_image_tool)
|
|
|
|
|
|
2025-12-19 20:21:39 +02:00
|
|
|
if additional_tools:
|
|
|
|
|
tools.extend(additional_tools)
|
|
|
|
|
|
2025-12-21 03:30:10 -08:00
|
|
|
# Create the deep agent with user-configurable system prompt and checkpointer
|
2025-12-18 23:57:57 -08:00
|
|
|
agent = create_deep_agent(
|
|
|
|
|
model=llm,
|
2025-12-19 20:21:39 +02:00
|
|
|
tools=tools,
|
2025-12-19 19:11:44 +02:00
|
|
|
system_prompt=build_surfsense_system_prompt(
|
2025-12-19 19:18:32 +02:00
|
|
|
user_instructions=user_instructions,
|
|
|
|
|
enable_citations=enable_citations,
|
2025-12-19 19:11:44 +02:00
|
|
|
),
|
2025-12-18 23:57:57 -08:00
|
|
|
context_schema=SurfSenseContextSchema,
|
2025-12-21 03:30:10 -08:00
|
|
|
checkpointer=checkpointer, # Enable conversation memory via thread_id
|
2025-12-18 23:57:57 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return agent
|