mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
Author labels in shared chats: bootstrap, stream prefix, route display name
This commit is contained in:
parent
d732bb7334
commit
48d442a387
4 changed files with 27 additions and 4 deletions
|
|
@ -12,6 +12,7 @@ These utilities help extract and transform content for different use cases.
|
|||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
|
||||
def extract_text_content(content: str | dict | list) -> str:
|
||||
|
|
@ -38,6 +39,7 @@ def extract_text_content(content: str | dict | list) -> str:
|
|||
async def bootstrap_history_from_db(
|
||||
session: AsyncSession,
|
||||
thread_id: int,
|
||||
thread_visibility: "ChatVisibility | None" = None,
|
||||
) -> list[HumanMessage | AIMessage]:
|
||||
"""
|
||||
Load message history from database and convert to LangChain format.
|
||||
|
|
@ -45,20 +47,28 @@ async def bootstrap_history_from_db(
|
|||
Used for cloned chats where the LangGraph checkpointer has no state,
|
||||
but we have messages in the database that should be used as context.
|
||||
|
||||
When thread_visibility is SEARCH_SPACE, user messages are prefixed with
|
||||
the author's display name so the LLM sees who said what.
|
||||
|
||||
Args:
|
||||
session: Database session
|
||||
thread_id: The chat thread ID
|
||||
thread_visibility: When SEARCH_SPACE, user messages get author prefix
|
||||
|
||||
Returns:
|
||||
List of LangChain messages (HumanMessage/AIMessage)
|
||||
"""
|
||||
from app.db import NewChatMessage
|
||||
from app.db import ChatVisibility, NewChatMessage
|
||||
|
||||
result = await session.execute(
|
||||
is_shared = thread_visibility == ChatVisibility.SEARCH_SPACE
|
||||
stmt = (
|
||||
select(NewChatMessage)
|
||||
.filter(NewChatMessage.thread_id == thread_id)
|
||||
.order_by(NewChatMessage.created_at)
|
||||
)
|
||||
if is_shared:
|
||||
stmt = stmt.options(selectinload(NewChatMessage.author))
|
||||
result = await session.execute(stmt)
|
||||
db_messages = result.scalars().all()
|
||||
|
||||
langchain_messages: list[HumanMessage | AIMessage] = []
|
||||
|
|
@ -68,6 +78,11 @@ async def bootstrap_history_from_db(
|
|||
if not text_content:
|
||||
continue
|
||||
if msg.role == "user":
|
||||
if is_shared:
|
||||
author_name = (
|
||||
(msg.author.display_name if msg.author else None) or "A team member"
|
||||
)
|
||||
text_content = f"**[{author_name}]:** {text_content}"
|
||||
langchain_messages.append(HumanMessage(content=text_content))
|
||||
elif msg.role == "assistant":
|
||||
langchain_messages.append(AIMessage(content=text_content))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue