feat(api): implement conversation variable reset api.

pull/20699/head
QuantumGhost 12 months ago
parent c604659287
commit 9a26211cbf

@ -245,6 +245,41 @@ class VariableApi(Resource):
return Response("", 204)
class VariableResetApi(Resource):
@_api_prerequisite
@marshal_with(_WORKFLOW_DRAFT_VARIABLE_FIELDS)
def put(self, app_model: App, variable_id: str):
draft_var_srv = WorkflowDraftVariableService(
session=db.session(),
)
workflow_srv = WorkflowService()
draft_workflow = workflow_srv.get_draft_workflow(app_model)
if draft_workflow is None:
raise NotFoundError(
f"Draft workflow not found, app_id={app_model.id}",
)
variable = draft_var_srv.get_variable(variable_id=variable_id)
if variable is None:
raise NotFoundError(description=f"variable not found, id={variable_id}")
if variable.app_id != app_model.id:
raise NotFoundError(description=f"variable not found, id={variable_id}")
if variable.node_id != CONVERSATION_VARIABLE_NODE_ID:
error_msg = "variable is not a conversation variable, id={}, node_id={}, name={}".format(
variable.id,
variable.node_id,
variable.name,
)
raise InvalidArgumentError(error_msg)
resetted = draft_var_srv.reset_conversation_variable(draft_workflow, variable)
db.session.commit()
if resetted is None:
return Response("", 204)
else:
return variable
def _get_variable_list(app_model: App, node_id) -> WorkflowDraftVariableList:
with Session(bind=db.engine, expire_on_commit=False) as session:
draft_var_srv = WorkflowDraftVariableService(
@ -321,6 +356,7 @@ api.add_resource(
)
api.add_resource(NodeVariableCollectionApi, "/apps/<uuid:app_id>/workflows/draft/nodes/<string:node_id>/variables")
api.add_resource(VariableApi, "/apps/<uuid:app_id>/workflows/draft/variables/<uuid:variable_id>")
api.add_resource(VariableApi, "/apps/<uuid:app_id>/workflows/draft/variables/<uuid:variable_id>/reset")
api.add_resource(ConversationVariableCollectionApi, "/apps/<uuid:app_id>/workflows/draft/conversation-variables")
api.add_resource(SystemVariableCollectionApi, "/apps/<uuid:app_id>/workflows/draft/system-variables")

@ -1 +1,5 @@
# TODO(QuantumGhost): Refactor variable type identification. Instead of directly
# comparing `dify_model_identity` with constants throughout the codebase, extract
# this logic into a dedicated function. This would encapsulate the implementation
# details of how different variable types are identified.
FILE_MODEL_IDENTITY = "__dify__file__"

@ -32,6 +32,10 @@ class WorkflowDraftVariableList:
total: int | None = None
class _DraftVarServiceError(Exception):
pass
class DraftVarLoader(VariableLoader):
# This implements the VariableLoader interface for loading draft variables.
#
@ -200,6 +204,25 @@ class WorkflowDraftVariableService:
self._session.flush()
return variable
def reset_conversation_variable(
self, workflow: Workflow, variable: WorkflowDraftVariable
) -> WorkflowDraftVariable | None:
conv_var_by_name = {i.name: i for i in workflow.conversation_variables}
conv_var = conv_var_by_name.get(variable.name)
if conv_var is None:
self._session.delete(instance=variable)
self._session.flush()
_logger.warning(
"Conversation variable not found for draft variable, id=%s, name=%s", variable.id, variable.name
)
return None
variable.set_value(conv_var)
self._session.add(variable)
self._session.flush()
return variable
def delete_variable(self, variable: WorkflowDraftVariable):
self._session.delete(variable)

@ -1,5 +1,4 @@
import json
import logging
import time
import uuid
from collections.abc import Callable, Generator, Mapping, Sequence
@ -32,7 +31,6 @@ from core.workflow.workflow_entry import WorkflowEntry
from events.app_event import app_draft_workflow_was_synced, app_published_workflow_was_updated
from extensions.ext_database import db
from factories.variable_factory import segment_to_variable
from libs import gen_utils
from models.account import Account
from models.model import App, AppMode
from models.tools import WorkflowToolProvider

Loading…
Cancel
Save