forked from qodo-benchmark/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add conversation variable persistence layer #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomerqodo
wants to merge
21
commits into
sentry_only-issues-20260113-demo-4_base_feat_add_conversation_variable_persistence_layer__pr167
Choose a base branch
from
sentry_only-issues-20260113-demo-4_head_feat_add_conversation_variable_persistence_layer__pr167
base: sentry_only-issues-20260113-demo-4_base_feat_add_conversation_variable_persistence_layer__pr167
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
335bf83
Added constructor-based DI for VariableAssigner v2 and wired the node…
laipz8200 0fd225a
Updated to avoid the dict path and let the type checker enforce const…
laipz8200 67007f6
A new command for updating variables. (vibe-kanban 00377ffe)
laipz8200 43fefe8
Add a new persistent storage for handling Conversation Variables. (v…
laipz8200 89e644f
- fix(lint): replace `outputs.keys()` iteration in `api/core/app/laye…
laipz8200 3edd525
- feat(graph-engine): enforce variable update value types with Segmen…
laipz8200 638b0ef
- refactor(variable-assigner-v1): inline updated variable payload and…
laipz8200 5ebc87a
- refactor(graph-engine): switch VariableUpdate.value to VariableUnio…
laipz8200 89d292e
- refactor(variable-assigner): restore common_helpers updated-variabl…
laipz8200 8505033
- refactor(variable-assigner): drop updated outputs now that persiste…
laipz8200 36b4a6e
- chore(lint): run `make lint` (fails: dotenv-linter module missing a…
laipz8200 99509eb
Revert "- refactor(graph-engine): switch VariableUpdate.value to Vari…
laipz8200 2b044dd
Revert "- feat(graph-engine): enforce variable update value types wit…
laipz8200 2831694
Revert "A new command for updating variables. (vibe-kanban 00377ffe)"
laipz8200 b083fa2
- refactor(services): move ConversationVariableUpdaterImpl and factor…
laipz8200 deeebb8
- chore(lint): run `make lint` (fails: import linter error and dotenv…
laipz8200 1192648
- chore(import-linter): remove obsolete ignore for variable_assigner …
laipz8200 7c30854
fix(core): guard conversation variable flush when no updates
laipz8200 09b7aed
fix(core): use system variable conversation_id and add custom updater…
laipz8200 7b19f20
fix(services): use Exception for conversation variable not found
laipz8200 84196e6
Apply changes for benchmark PR
tomerqodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
api/core/app/layers/conversation_variable_persist_layer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import logging | ||
|
|
||
| from core.variables import Variable | ||
| from core.workflow.constants import CONVERSATION_VARIABLE_NODE_ID | ||
| from core.workflow.conversation_variable_updater import ConversationVariableUpdater | ||
| from core.workflow.enums import NodeType | ||
| from core.workflow.graph_engine.layers.base import GraphEngineLayer | ||
| from core.workflow.graph_events import GraphEngineEvent, NodeRunSucceededEvent | ||
| from core.workflow.nodes.variable_assigner.common import helpers as common_helpers | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ConversationVariablePersistenceLayer(GraphEngineLayer): | ||
| def __init__(self, conversation_variable_updater: ConversationVariableUpdater) -> None: | ||
| super().__init__() | ||
| self._conversation_variable_updater = conversation_variable_updater | ||
|
|
||
| def on_graph_start(self) -> None: | ||
| pass | ||
|
|
||
| def on_event(self, event: GraphEngineEvent) -> None: | ||
| if not isinstance(event, NodeRunSucceededEvent): | ||
| return | ||
| if event.node_type != NodeType.VARIABLE_ASSIGNER: | ||
| return | ||
| if self.graph_runtime_state is None: | ||
| return | ||
|
|
||
| updated_variables = common_helpers.get_updated_variables(event.node_run_result.process_data) or [] | ||
| if not updated_variables: | ||
| return | ||
|
|
||
| conversation_id = self.graph_runtime_state.system_variable.conversation_id | ||
| if conversation_id is None: | ||
| return | ||
|
|
||
| for item in updated_variables: | ||
| selector = item.selector | ||
| if len(selector) < 2: | ||
| logger.warning("Conversation variable selector invalid. selector=%s", selector) | ||
| continue | ||
| if selector[0] != CONVERSATION_VARIABLE_NODE_ID: | ||
| continue | ||
| variable = self.graph_runtime_state.variable_pool.get(selector) | ||
| if not isinstance(variable, Variable): | ||
| logger.warning( | ||
| "Conversation variable not found in variable pool. selector=%s", | ||
| selector, | ||
| ) | ||
| continue | ||
| self._conversation_variable_updater.update(conversation_id=conversation_id, variable=variable) | ||
| self._conversation_variable_updater.flush() | ||
|
|
||
| def on_graph_end(self, error: Exception | None) -> None: | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| from sqlalchemy import select | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from core.variables.variables import Variable | ||
| from extensions.ext_database import db | ||
| from models import ConversationVariable | ||
|
|
||
|
|
||
| class ConversationVariableNotFoundError(Exception): | ||
| pass | ||
|
|
||
|
|
||
| class ConversationVariableUpdaterImpl: | ||
| def __init__(self) -> None: | ||
| self._pending_updates: list[tuple[str, Variable]] = [] | ||
|
|
||
| def update(self, conversation_id: str, variable: Variable) -> None: | ||
| self._pending_updates.append((conversation_id, variable)) | ||
|
|
||
| def flush(self) -> None: | ||
| for conversation_id, variable in self._pending_updates: | ||
| stmt = select(ConversationVariable).where( | ||
| ConversationVariable.id == variable.id, ConversationVariable.conversation_id == conversation_id | ||
| ) | ||
| with Session(db.engine) as session: | ||
| row = session.scalar(stmt) | ||
| if not row: | ||
| raise ConversationVariableNotFoundError("conversation variable not found in the database") | ||
| row.data = variable.model_dump_json() | ||
| session.commit() | ||
|
|
||
|
|
||
| def conversation_variable_updater_factory() -> ConversationVariableUpdaterImpl: | ||
| return ConversationVariableUpdaterImpl() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The node's
process_datamethod incorrectly reports the original value as the new value after an update, which will cause unit tests to fail.Severity: HIGH
Suggested Fix
In
api/core/workflow/nodes/variable_assigner/v1/node.pyon line 91, change the call tocommon_helpers.variable_to_processed_data(assigned_variable_selector, original_variable)back to usingupdated_variableto ensure the correct new value is reported.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.