-
Notifications
You must be signed in to change notification settings - Fork 41
Adding background task to create next state #175
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
Merged
NiveditJain
merged 10 commits into
exospherehost:main
from
NiveditJain:creating-next-state
Aug 10, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d5c7716
completed graph verification, testing pending
NiveditJain 0eb27cb
Update state-manager/app/tasks/verify_graph.py
NiveditJain aaa83fe
resolving issues by @gemini-code-assistant and @coderabbitai
NiveditJain 3186d03
Merge branch 'creating-next-state' of https://github.com/NiveditJain/…
NiveditJain 08f3dba
Merge branch 'main' into creating-next-state
NiveditJain d95eb51
Added process to create next state
NiveditJain 019904f
fixed error handling
NiveditJain fe7a85a
added timeout
NiveditJain 2a2f6c9
added multiple state bifercation
NiveditJain b2988d9
fixed parent_ids
NiveditJain 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
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,124 @@ | ||
| import asyncio | ||
| import time | ||
|
|
||
| from bson import ObjectId | ||
|
|
||
| from app.models.db.state import State | ||
| from app.models.db.graph_template_model import GraphTemplate | ||
| from app.models.graph_template_validation_status import GraphTemplateValidationStatus | ||
| from app.models.db.registered_node import RegisteredNode | ||
| from app.models.state_status_enum import StateStatusEnum | ||
|
|
||
| from json_schema_to_pydantic import create_model | ||
|
|
||
| async def create_next_state(state: State): | ||
| graph_template = None | ||
|
|
||
| try: | ||
| start_time = time.time() | ||
| timeout_seconds = 300 # 5 minutes | ||
|
|
||
| while True: | ||
| graph_template = await GraphTemplate.find_one(GraphTemplate.name == state.graph_name, GraphTemplate.namespace == state.namespace_name) | ||
| if not graph_template: | ||
| raise Exception(f"Graph template {state.graph_name} not found") | ||
| if graph_template.validation_status == GraphTemplateValidationStatus.VALID: | ||
| break | ||
|
|
||
| # Check if we've exceeded the timeout | ||
| if time.time() - start_time > timeout_seconds: | ||
| raise Exception(f"Timeout waiting for graph template {state.graph_name} to become valid after {timeout_seconds} seconds") | ||
|
|
||
| await asyncio.sleep(1) | ||
|
|
||
| node_template = graph_template.get_node_by_identifier(state.identifier) | ||
| if not node_template: | ||
| raise Exception(f"Node template {state.identifier} not found") | ||
|
|
||
| next_node_identifier = node_template.next_nodes | ||
| if not next_node_identifier: | ||
| raise Exception(f"Node template {state.identifier} has no next nodes") | ||
|
|
||
| cache_states = {} | ||
|
|
||
| for identifier in next_node_identifier: | ||
| next_node_template = graph_template.get_node_by_identifier(identifier) | ||
| if not next_node_template: | ||
| continue | ||
NiveditJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| registered_node = await RegisteredNode.find_one(RegisteredNode.name == next_node_template.node_name, RegisteredNode.namespace == next_node_template.namespace) | ||
|
|
||
| if not registered_node: | ||
| raise Exception(f"Registered node {next_node_template.node_name} not found") | ||
|
|
||
| next_node_input_model = create_model(registered_node.inputs_schema) | ||
| next_node_input_data = {} | ||
|
|
||
| for field_name, _ in next_node_input_model.model_fields.items(): | ||
| temporary_input = next_node_template.inputs[field_name] | ||
| splits = temporary_input.split("${{") | ||
|
|
||
| if len(splits) == 0: | ||
| next_node_input_data[field_name] = temporary_input | ||
| continue | ||
|
|
||
| constructed_string = "" | ||
| for split in splits: | ||
| if "}}" in split: | ||
| placeholder_content = split.split("}}")[0] | ||
| parts = [p.strip() for p in placeholder_content.split('.')] | ||
|
|
||
| if len(parts) != 3 or parts[1] != 'outputs': | ||
| raise Exception(f"Invalid input placeholder format: '{placeholder_content}' for field {field_name}") | ||
|
|
||
| input_identifier = parts[0] | ||
| input_field = parts[2] | ||
|
|
||
| parent_id = state.parents.get(input_identifier) | ||
|
|
||
| if not parent_id: | ||
| raise Exception(f"Parent identifier '{input_identifier}' not found in state parents.") | ||
|
|
||
| if parent_id not in cache_states: | ||
| dependent_state = await State.get(ObjectId(parent_id)) | ||
| if not dependent_state: | ||
| raise Exception(f"Dependent state {input_identifier} not found") | ||
| cache_states[parent_id] = dependent_state | ||
| else: | ||
NiveditJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dependent_state = cache_states[parent_id] | ||
|
|
||
| if input_field not in dependent_state.outputs: | ||
| raise Exception(f"Input field {input_field} not found in dependent state {input_identifier}") | ||
|
|
||
| constructed_string += dependent_state.outputs[input_field] + split.split("}}")[1] | ||
|
|
||
| else: | ||
| constructed_string += split | ||
|
|
||
| next_node_input_data[field_name] = constructed_string | ||
|
|
||
| new_state = State( | ||
| node_name=next_node_template.node_name, | ||
| namespace_name=next_node_template.namespace, | ||
| identifier=next_node_template.identifier, | ||
| graph_name=state.graph_name, | ||
| status=StateStatusEnum.CREATED, | ||
NiveditJain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| inputs=next_node_input_data, | ||
| outputs={}, | ||
| error=None, | ||
| parents={ | ||
| **state.parents, | ||
| next_node_template.identifier: ObjectId(state.id) | ||
| } | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| await new_state.save() | ||
|
|
||
| state.status = StateStatusEnum.SUCCESS | ||
| await state.save() | ||
|
|
||
| except Exception as e: | ||
| state.status = StateStatusEnum.ERRORED | ||
| state.error = str(e) | ||
| await state.save() | ||
| return | ||
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.
Uh oh!
There was an error while loading. Please reload this page.