2025-09-18 04:49:10 +00:00
|
|
|
from sqlalchemy import select
|
2026-01-06 06:52:59 +00:00
|
|
|
from sqlalchemy.orm import Session, sessionmaker
|
2025-06-24 01:05:29 +00:00
|
|
|
|
2026-03-02 10:42:30 +00:00
|
|
|
from dify_graph.variables.variables import VariableBase
|
2025-09-18 04:49:10 +00:00
|
|
|
from models import ConversationVariable
|
2025-06-24 01:05:29 +00:00
|
|
|
|
2026-01-06 06:05:33 +00:00
|
|
|
|
|
|
|
|
class ConversationVariableNotFoundError(Exception):
|
|
|
|
|
pass
|
2025-06-24 01:05:29 +00:00
|
|
|
|
|
|
|
|
|
2026-01-06 06:52:59 +00:00
|
|
|
class ConversationVariableUpdater:
|
|
|
|
|
def __init__(self, session_maker: sessionmaker[Session]) -> None:
|
|
|
|
|
self._session_maker: sessionmaker[Session] = session_maker
|
|
|
|
|
|
2026-01-13 14:39:34 +00:00
|
|
|
def update(self, conversation_id: str, variable: VariableBase) -> None:
|
2025-06-24 01:05:29 +00:00
|
|
|
stmt = select(ConversationVariable).where(
|
|
|
|
|
ConversationVariable.id == variable.id, ConversationVariable.conversation_id == conversation_id
|
|
|
|
|
)
|
2026-01-06 06:52:59 +00:00
|
|
|
with self._session_maker() as session:
|
2025-06-24 01:05:29 +00:00
|
|
|
row = session.scalar(stmt)
|
|
|
|
|
if not row:
|
2026-01-06 06:05:33 +00:00
|
|
|
raise ConversationVariableNotFoundError("conversation variable not found in the database")
|
2025-06-24 01:05:29 +00:00
|
|
|
row.data = variable.model_dump_json()
|
|
|
|
|
session.commit()
|
|
|
|
|
|
2026-01-06 06:05:33 +00:00
|
|
|
def flush(self) -> None:
|
2025-06-24 01:05:29 +00:00
|
|
|
pass
|