feat(api): utilize `ConversationVariableUpdater` in variable assigner nodes
Removed the original logicpull/20699/head
parent
1b234de81f
commit
d720287504
@ -0,0 +1,39 @@
|
|||||||
|
import abc
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
|
from core.variables import Variable
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationVariableUpdater(Protocol):
|
||||||
|
"""
|
||||||
|
ConversationVariableUpdater defines an abstraction for updating conversation variable values.
|
||||||
|
|
||||||
|
It is intended for use by `v1.VariableAssignerNode` and `v2.VariableAssignerNode` when updating
|
||||||
|
conversation variables.
|
||||||
|
|
||||||
|
Implementations may choose to batch updates. If batching is used, the `flush` method
|
||||||
|
should be implemented to persist buffered changes, and `update_conversation_variable`
|
||||||
|
should handle buffering accordingly.
|
||||||
|
|
||||||
|
Note: Since implementations may buffer updates, instances of ConversationVariableUpdater
|
||||||
|
are not thread-safe. Each VariableAssignerNode should create its own instance during execution.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def update(self, conversation_id: str, variable: "Variable") -> None:
|
||||||
|
"""
|
||||||
|
Updates the value of the specified conversation variable in the underlying storage.
|
||||||
|
|
||||||
|
:param conversation_id: The ID of the conversation to update. Typically references `ConversationVariable.id`.
|
||||||
|
:param variable: The `Variable` instance containing the updated value.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def flush(self):
|
||||||
|
"""
|
||||||
|
Flushes all pending updates to the underlying storage system.
|
||||||
|
|
||||||
|
If the implementation does not buffer updates, this method can be a no-op.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
from sqlalchemy import Engine, select
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from core.variables.variables import Variable
|
||||||
|
from models.engine import db
|
||||||
|
from models.workflow import ConversationVariable
|
||||||
|
|
||||||
|
from .exc import VariableOperatorNodeError
|
||||||
|
|
||||||
|
|
||||||
|
class ConversationVariableUpdaterImpl:
|
||||||
|
_engine: Engine | None
|
||||||
|
|
||||||
|
def __init__(self, engine: Engine | None = None) -> None:
|
||||||
|
self._engine = engine
|
||||||
|
|
||||||
|
def _get_engine(self) -> Engine:
|
||||||
|
if self._engine:
|
||||||
|
return self._engine
|
||||||
|
return db.engine
|
||||||
|
|
||||||
|
def update(self, conversation_id: str, variable: Variable):
|
||||||
|
stmt = select(ConversationVariable).where(
|
||||||
|
ConversationVariable.id == variable.id, ConversationVariable.conversation_id == conversation_id
|
||||||
|
)
|
||||||
|
with Session(self._get_engine()) as session:
|
||||||
|
row = session.scalar(stmt)
|
||||||
|
if not row:
|
||||||
|
raise VariableOperatorNodeError("conversation variable not found in the database")
|
||||||
|
row.data = variable.model_dump_json()
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def conversation_variable_updater_factory() -> ConversationVariableUpdaterImpl:
|
||||||
|
return ConversationVariableUpdaterImpl()
|
||||||
Loading…
Reference in New Issue