lfx-nightly 0.1.11.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lfx/__init__.py +0 -0
- lfx/__main__.py +25 -0
- lfx/base/__init__.py +0 -0
- lfx/base/agents/__init__.py +0 -0
- lfx/base/agents/agent.py +268 -0
- lfx/base/agents/callback.py +130 -0
- lfx/base/agents/context.py +109 -0
- lfx/base/agents/crewai/__init__.py +0 -0
- lfx/base/agents/crewai/crew.py +231 -0
- lfx/base/agents/crewai/tasks.py +12 -0
- lfx/base/agents/default_prompts.py +23 -0
- lfx/base/agents/errors.py +15 -0
- lfx/base/agents/events.py +346 -0
- lfx/base/agents/utils.py +205 -0
- lfx/base/astra_assistants/__init__.py +0 -0
- lfx/base/astra_assistants/util.py +171 -0
- lfx/base/chains/__init__.py +0 -0
- lfx/base/chains/model.py +19 -0
- lfx/base/composio/__init__.py +0 -0
- lfx/base/composio/composio_base.py +1291 -0
- lfx/base/compressors/__init__.py +0 -0
- lfx/base/compressors/model.py +60 -0
- lfx/base/constants.py +46 -0
- lfx/base/curl/__init__.py +0 -0
- lfx/base/curl/parse.py +188 -0
- lfx/base/data/__init__.py +5 -0
- lfx/base/data/base_file.py +685 -0
- lfx/base/data/docling_utils.py +245 -0
- lfx/base/data/utils.py +198 -0
- lfx/base/document_transformers/__init__.py +0 -0
- lfx/base/document_transformers/model.py +43 -0
- lfx/base/embeddings/__init__.py +0 -0
- lfx/base/embeddings/aiml_embeddings.py +62 -0
- lfx/base/embeddings/model.py +26 -0
- lfx/base/flow_processing/__init__.py +0 -0
- lfx/base/flow_processing/utils.py +86 -0
- lfx/base/huggingface/__init__.py +0 -0
- lfx/base/huggingface/model_bridge.py +133 -0
- lfx/base/io/__init__.py +0 -0
- lfx/base/io/chat.py +20 -0
- lfx/base/io/text.py +22 -0
- lfx/base/langchain_utilities/__init__.py +0 -0
- lfx/base/langchain_utilities/model.py +35 -0
- lfx/base/langchain_utilities/spider_constants.py +1 -0
- lfx/base/langwatch/__init__.py +0 -0
- lfx/base/langwatch/utils.py +18 -0
- lfx/base/mcp/__init__.py +0 -0
- lfx/base/mcp/constants.py +2 -0
- lfx/base/mcp/util.py +1398 -0
- lfx/base/memory/__init__.py +0 -0
- lfx/base/memory/memory.py +49 -0
- lfx/base/memory/model.py +38 -0
- lfx/base/models/__init__.py +3 -0
- lfx/base/models/aiml_constants.py +51 -0
- lfx/base/models/anthropic_constants.py +47 -0
- lfx/base/models/aws_constants.py +151 -0
- lfx/base/models/chat_result.py +76 -0
- lfx/base/models/google_generative_ai_constants.py +70 -0
- lfx/base/models/groq_constants.py +134 -0
- lfx/base/models/model.py +375 -0
- lfx/base/models/model_input_constants.py +307 -0
- lfx/base/models/model_metadata.py +41 -0
- lfx/base/models/model_utils.py +8 -0
- lfx/base/models/novita_constants.py +35 -0
- lfx/base/models/ollama_constants.py +49 -0
- lfx/base/models/openai_constants.py +122 -0
- lfx/base/models/sambanova_constants.py +18 -0
- lfx/base/processing/__init__.py +0 -0
- lfx/base/prompts/__init__.py +0 -0
- lfx/base/prompts/api_utils.py +224 -0
- lfx/base/prompts/utils.py +61 -0
- lfx/base/textsplitters/__init__.py +0 -0
- lfx/base/textsplitters/model.py +28 -0
- lfx/base/tools/__init__.py +0 -0
- lfx/base/tools/base.py +26 -0
- lfx/base/tools/component_tool.py +325 -0
- lfx/base/tools/constants.py +49 -0
- lfx/base/tools/flow_tool.py +132 -0
- lfx/base/tools/run_flow.py +224 -0
- lfx/base/vectorstores/__init__.py +0 -0
- lfx/base/vectorstores/model.py +193 -0
- lfx/base/vectorstores/utils.py +22 -0
- lfx/base/vectorstores/vector_store_connection_decorator.py +52 -0
- lfx/cli/__init__.py +5 -0
- lfx/cli/commands.py +319 -0
- lfx/cli/common.py +650 -0
- lfx/cli/run.py +441 -0
- lfx/cli/script_loader.py +247 -0
- lfx/cli/serve_app.py +546 -0
- lfx/cli/validation.py +69 -0
- lfx/components/FAISS/__init__.py +34 -0
- lfx/components/FAISS/faiss.py +111 -0
- lfx/components/Notion/__init__.py +19 -0
- lfx/components/Notion/add_content_to_page.py +269 -0
- lfx/components/Notion/create_page.py +94 -0
- lfx/components/Notion/list_database_properties.py +68 -0
- lfx/components/Notion/list_pages.py +122 -0
- lfx/components/Notion/list_users.py +77 -0
- lfx/components/Notion/page_content_viewer.py +93 -0
- lfx/components/Notion/search.py +111 -0
- lfx/components/Notion/update_page_property.py +114 -0
- lfx/components/__init__.py +411 -0
- lfx/components/_importing.py +42 -0
- lfx/components/agentql/__init__.py +3 -0
- lfx/components/agentql/agentql_api.py +151 -0
- lfx/components/agents/__init__.py +34 -0
- lfx/components/agents/agent.py +558 -0
- lfx/components/agents/mcp_component.py +501 -0
- lfx/components/aiml/__init__.py +37 -0
- lfx/components/aiml/aiml.py +112 -0
- lfx/components/aiml/aiml_embeddings.py +37 -0
- lfx/components/amazon/__init__.py +36 -0
- lfx/components/amazon/amazon_bedrock_embedding.py +109 -0
- lfx/components/amazon/amazon_bedrock_model.py +124 -0
- lfx/components/amazon/s3_bucket_uploader.py +211 -0
- lfx/components/anthropic/__init__.py +34 -0
- lfx/components/anthropic/anthropic.py +187 -0
- lfx/components/apify/__init__.py +5 -0
- lfx/components/apify/apify_actor.py +325 -0
- lfx/components/arxiv/__init__.py +3 -0
- lfx/components/arxiv/arxiv.py +163 -0
- lfx/components/assemblyai/__init__.py +46 -0
- lfx/components/assemblyai/assemblyai_get_subtitles.py +83 -0
- lfx/components/assemblyai/assemblyai_lemur.py +183 -0
- lfx/components/assemblyai/assemblyai_list_transcripts.py +95 -0
- lfx/components/assemblyai/assemblyai_poll_transcript.py +72 -0
- lfx/components/assemblyai/assemblyai_start_transcript.py +188 -0
- lfx/components/azure/__init__.py +37 -0
- lfx/components/azure/azure_openai.py +95 -0
- lfx/components/azure/azure_openai_embeddings.py +83 -0
- lfx/components/baidu/__init__.py +32 -0
- lfx/components/baidu/baidu_qianfan_chat.py +113 -0
- lfx/components/bing/__init__.py +3 -0
- lfx/components/bing/bing_search_api.py +61 -0
- lfx/components/cassandra/__init__.py +40 -0
- lfx/components/cassandra/cassandra.py +264 -0
- lfx/components/cassandra/cassandra_chat.py +92 -0
- lfx/components/cassandra/cassandra_graph.py +238 -0
- lfx/components/chains/__init__.py +3 -0
- lfx/components/chroma/__init__.py +34 -0
- lfx/components/chroma/chroma.py +167 -0
- lfx/components/cleanlab/__init__.py +40 -0
- lfx/components/cleanlab/cleanlab_evaluator.py +155 -0
- lfx/components/cleanlab/cleanlab_rag_evaluator.py +254 -0
- lfx/components/cleanlab/cleanlab_remediator.py +131 -0
- lfx/components/clickhouse/__init__.py +34 -0
- lfx/components/clickhouse/clickhouse.py +135 -0
- lfx/components/cloudflare/__init__.py +32 -0
- lfx/components/cloudflare/cloudflare.py +81 -0
- lfx/components/cohere/__init__.py +40 -0
- lfx/components/cohere/cohere_embeddings.py +81 -0
- lfx/components/cohere/cohere_models.py +46 -0
- lfx/components/cohere/cohere_rerank.py +51 -0
- lfx/components/composio/__init__.py +74 -0
- lfx/components/composio/composio_api.py +268 -0
- lfx/components/composio/dropbox_compnent.py +11 -0
- lfx/components/composio/github_composio.py +11 -0
- lfx/components/composio/gmail_composio.py +38 -0
- lfx/components/composio/googlecalendar_composio.py +11 -0
- lfx/components/composio/googlemeet_composio.py +11 -0
- lfx/components/composio/googletasks_composio.py +8 -0
- lfx/components/composio/linear_composio.py +11 -0
- lfx/components/composio/outlook_composio.py +11 -0
- lfx/components/composio/reddit_composio.py +11 -0
- lfx/components/composio/slack_composio.py +582 -0
- lfx/components/composio/slackbot_composio.py +11 -0
- lfx/components/composio/supabase_composio.py +11 -0
- lfx/components/composio/todoist_composio.py +11 -0
- lfx/components/composio/youtube_composio.py +11 -0
- lfx/components/confluence/__init__.py +3 -0
- lfx/components/confluence/confluence.py +84 -0
- lfx/components/couchbase/__init__.py +34 -0
- lfx/components/couchbase/couchbase.py +102 -0
- lfx/components/crewai/__init__.py +49 -0
- lfx/components/crewai/crewai.py +107 -0
- lfx/components/crewai/hierarchical_crew.py +46 -0
- lfx/components/crewai/hierarchical_task.py +44 -0
- lfx/components/crewai/sequential_crew.py +52 -0
- lfx/components/crewai/sequential_task.py +73 -0
- lfx/components/crewai/sequential_task_agent.py +143 -0
- lfx/components/custom_component/__init__.py +34 -0
- lfx/components/custom_component/custom_component.py +31 -0
- lfx/components/data/__init__.py +64 -0
- lfx/components/data/api_request.py +544 -0
- lfx/components/data/csv_to_data.py +95 -0
- lfx/components/data/directory.py +113 -0
- lfx/components/data/file.py +577 -0
- lfx/components/data/json_to_data.py +98 -0
- lfx/components/data/news_search.py +164 -0
- lfx/components/data/rss.py +69 -0
- lfx/components/data/sql_executor.py +101 -0
- lfx/components/data/url.py +311 -0
- lfx/components/data/web_search.py +112 -0
- lfx/components/data/webhook.py +56 -0
- lfx/components/datastax/__init__.py +70 -0
- lfx/components/datastax/astra_assistant_manager.py +306 -0
- lfx/components/datastax/astra_db.py +75 -0
- lfx/components/datastax/astra_vectorize.py +124 -0
- lfx/components/datastax/astradb.py +1285 -0
- lfx/components/datastax/astradb_cql.py +314 -0
- lfx/components/datastax/astradb_graph.py +330 -0
- lfx/components/datastax/astradb_tool.py +414 -0
- lfx/components/datastax/astradb_vectorstore.py +1285 -0
- lfx/components/datastax/cassandra.py +92 -0
- lfx/components/datastax/create_assistant.py +58 -0
- lfx/components/datastax/create_thread.py +32 -0
- lfx/components/datastax/dotenv.py +35 -0
- lfx/components/datastax/get_assistant.py +37 -0
- lfx/components/datastax/getenvvar.py +30 -0
- lfx/components/datastax/graph_rag.py +141 -0
- lfx/components/datastax/hcd.py +314 -0
- lfx/components/datastax/list_assistants.py +25 -0
- lfx/components/datastax/run.py +89 -0
- lfx/components/deactivated/__init__.py +15 -0
- lfx/components/deactivated/amazon_kendra.py +66 -0
- lfx/components/deactivated/chat_litellm_model.py +158 -0
- lfx/components/deactivated/code_block_extractor.py +26 -0
- lfx/components/deactivated/documents_to_data.py +22 -0
- lfx/components/deactivated/embed.py +16 -0
- lfx/components/deactivated/extract_key_from_data.py +46 -0
- lfx/components/deactivated/json_document_builder.py +57 -0
- lfx/components/deactivated/list_flows.py +20 -0
- lfx/components/deactivated/mcp_sse.py +61 -0
- lfx/components/deactivated/mcp_stdio.py +62 -0
- lfx/components/deactivated/merge_data.py +93 -0
- lfx/components/deactivated/message.py +37 -0
- lfx/components/deactivated/metal.py +54 -0
- lfx/components/deactivated/multi_query.py +59 -0
- lfx/components/deactivated/retriever.py +43 -0
- lfx/components/deactivated/selective_passthrough.py +77 -0
- lfx/components/deactivated/should_run_next.py +40 -0
- lfx/components/deactivated/split_text.py +63 -0
- lfx/components/deactivated/store_message.py +24 -0
- lfx/components/deactivated/sub_flow.py +124 -0
- lfx/components/deactivated/vectara_self_query.py +76 -0
- lfx/components/deactivated/vector_store.py +24 -0
- lfx/components/deepseek/__init__.py +34 -0
- lfx/components/deepseek/deepseek.py +136 -0
- lfx/components/docling/__init__.py +43 -0
- lfx/components/docling/chunk_docling_document.py +186 -0
- lfx/components/docling/docling_inline.py +231 -0
- lfx/components/docling/docling_remote.py +193 -0
- lfx/components/docling/export_docling_document.py +117 -0
- lfx/components/documentloaders/__init__.py +3 -0
- lfx/components/duckduckgo/__init__.py +3 -0
- lfx/components/duckduckgo/duck_duck_go_search_run.py +92 -0
- lfx/components/elastic/__init__.py +37 -0
- lfx/components/elastic/elasticsearch.py +267 -0
- lfx/components/elastic/opensearch.py +243 -0
- lfx/components/embeddings/__init__.py +37 -0
- lfx/components/embeddings/similarity.py +76 -0
- lfx/components/embeddings/text_embedder.py +64 -0
- lfx/components/exa/__init__.py +3 -0
- lfx/components/exa/exa_search.py +68 -0
- lfx/components/firecrawl/__init__.py +43 -0
- lfx/components/firecrawl/firecrawl_crawl_api.py +88 -0
- lfx/components/firecrawl/firecrawl_extract_api.py +136 -0
- lfx/components/firecrawl/firecrawl_map_api.py +89 -0
- lfx/components/firecrawl/firecrawl_scrape_api.py +73 -0
- lfx/components/git/__init__.py +4 -0
- lfx/components/git/git.py +262 -0
- lfx/components/git/gitextractor.py +196 -0
- lfx/components/glean/__init__.py +3 -0
- lfx/components/glean/glean_search_api.py +173 -0
- lfx/components/google/__init__.py +17 -0
- lfx/components/google/gmail.py +192 -0
- lfx/components/google/google_bq_sql_executor.py +157 -0
- lfx/components/google/google_drive.py +92 -0
- lfx/components/google/google_drive_search.py +152 -0
- lfx/components/google/google_generative_ai.py +147 -0
- lfx/components/google/google_generative_ai_embeddings.py +141 -0
- lfx/components/google/google_oauth_token.py +89 -0
- lfx/components/google/google_search_api_core.py +68 -0
- lfx/components/google/google_serper_api_core.py +74 -0
- lfx/components/groq/__init__.py +34 -0
- lfx/components/groq/groq.py +136 -0
- lfx/components/helpers/__init__.py +52 -0
- lfx/components/helpers/calculator_core.py +89 -0
- lfx/components/helpers/create_list.py +40 -0
- lfx/components/helpers/current_date.py +42 -0
- lfx/components/helpers/id_generator.py +42 -0
- lfx/components/helpers/memory.py +251 -0
- lfx/components/helpers/output_parser.py +45 -0
- lfx/components/helpers/store_message.py +90 -0
- lfx/components/homeassistant/__init__.py +7 -0
- lfx/components/homeassistant/home_assistant_control.py +152 -0
- lfx/components/homeassistant/list_home_assistant_states.py +137 -0
- lfx/components/huggingface/__init__.py +37 -0
- lfx/components/huggingface/huggingface.py +197 -0
- lfx/components/huggingface/huggingface_inference_api.py +106 -0
- lfx/components/ibm/__init__.py +34 -0
- lfx/components/ibm/watsonx.py +203 -0
- lfx/components/ibm/watsonx_embeddings.py +135 -0
- lfx/components/icosacomputing/__init__.py +5 -0
- lfx/components/icosacomputing/combinatorial_reasoner.py +84 -0
- lfx/components/input_output/__init__.py +38 -0
- lfx/components/input_output/chat.py +120 -0
- lfx/components/input_output/chat_output.py +200 -0
- lfx/components/input_output/text.py +27 -0
- lfx/components/input_output/text_output.py +29 -0
- lfx/components/jigsawstack/__init__.py +23 -0
- lfx/components/jigsawstack/ai_scrape.py +126 -0
- lfx/components/jigsawstack/ai_web_search.py +136 -0
- lfx/components/jigsawstack/file_read.py +115 -0
- lfx/components/jigsawstack/file_upload.py +94 -0
- lfx/components/jigsawstack/image_generation.py +205 -0
- lfx/components/jigsawstack/nsfw.py +60 -0
- lfx/components/jigsawstack/object_detection.py +124 -0
- lfx/components/jigsawstack/sentiment.py +112 -0
- lfx/components/jigsawstack/text_to_sql.py +90 -0
- lfx/components/jigsawstack/text_translate.py +77 -0
- lfx/components/jigsawstack/vocr.py +107 -0
- lfx/components/langchain_utilities/__init__.py +109 -0
- lfx/components/langchain_utilities/character.py +53 -0
- lfx/components/langchain_utilities/conversation.py +59 -0
- lfx/components/langchain_utilities/csv_agent.py +107 -0
- lfx/components/langchain_utilities/fake_embeddings.py +26 -0
- lfx/components/langchain_utilities/html_link_extractor.py +35 -0
- lfx/components/langchain_utilities/json_agent.py +45 -0
- lfx/components/langchain_utilities/langchain_hub.py +126 -0
- lfx/components/langchain_utilities/language_recursive.py +49 -0
- lfx/components/langchain_utilities/language_semantic.py +138 -0
- lfx/components/langchain_utilities/llm_checker.py +39 -0
- lfx/components/langchain_utilities/llm_math.py +42 -0
- lfx/components/langchain_utilities/natural_language.py +61 -0
- lfx/components/langchain_utilities/openai_tools.py +53 -0
- lfx/components/langchain_utilities/openapi.py +48 -0
- lfx/components/langchain_utilities/recursive_character.py +60 -0
- lfx/components/langchain_utilities/retrieval_qa.py +83 -0
- lfx/components/langchain_utilities/runnable_executor.py +137 -0
- lfx/components/langchain_utilities/self_query.py +80 -0
- lfx/components/langchain_utilities/spider.py +142 -0
- lfx/components/langchain_utilities/sql.py +40 -0
- lfx/components/langchain_utilities/sql_database.py +35 -0
- lfx/components/langchain_utilities/sql_generator.py +78 -0
- lfx/components/langchain_utilities/tool_calling.py +59 -0
- lfx/components/langchain_utilities/vector_store_info.py +49 -0
- lfx/components/langchain_utilities/vector_store_router.py +33 -0
- lfx/components/langchain_utilities/xml_agent.py +71 -0
- lfx/components/langwatch/__init__.py +3 -0
- lfx/components/langwatch/langwatch.py +278 -0
- lfx/components/link_extractors/__init__.py +3 -0
- lfx/components/lmstudio/__init__.py +34 -0
- lfx/components/lmstudio/lmstudioembeddings.py +89 -0
- lfx/components/lmstudio/lmstudiomodel.py +129 -0
- lfx/components/logic/__init__.py +52 -0
- lfx/components/logic/conditional_router.py +171 -0
- lfx/components/logic/data_conditional_router.py +125 -0
- lfx/components/logic/flow_tool.py +110 -0
- lfx/components/logic/listen.py +29 -0
- lfx/components/logic/loop.py +125 -0
- lfx/components/logic/notify.py +88 -0
- lfx/components/logic/pass_message.py +35 -0
- lfx/components/logic/run_flow.py +71 -0
- lfx/components/logic/sub_flow.py +114 -0
- lfx/components/maritalk/__init__.py +32 -0
- lfx/components/maritalk/maritalk.py +52 -0
- lfx/components/mem0/__init__.py +3 -0
- lfx/components/mem0/mem0_chat_memory.py +136 -0
- lfx/components/milvus/__init__.py +34 -0
- lfx/components/milvus/milvus.py +115 -0
- lfx/components/mistral/__init__.py +37 -0
- lfx/components/mistral/mistral.py +114 -0
- lfx/components/mistral/mistral_embeddings.py +58 -0
- lfx/components/models/__init__.py +34 -0
- lfx/components/models/embedding_model.py +114 -0
- lfx/components/models/language_model.py +144 -0
- lfx/components/mongodb/__init__.py +34 -0
- lfx/components/mongodb/mongodb_atlas.py +213 -0
- lfx/components/needle/__init__.py +3 -0
- lfx/components/needle/needle.py +104 -0
- lfx/components/notdiamond/__init__.py +34 -0
- lfx/components/notdiamond/notdiamond.py +228 -0
- lfx/components/novita/__init__.py +32 -0
- lfx/components/novita/novita.py +130 -0
- lfx/components/nvidia/__init__.py +57 -0
- lfx/components/nvidia/nvidia.py +157 -0
- lfx/components/nvidia/nvidia_embedding.py +77 -0
- lfx/components/nvidia/nvidia_ingest.py +317 -0
- lfx/components/nvidia/nvidia_rerank.py +63 -0
- lfx/components/nvidia/system_assist.py +65 -0
- lfx/components/olivya/__init__.py +3 -0
- lfx/components/olivya/olivya.py +116 -0
- lfx/components/ollama/__init__.py +37 -0
- lfx/components/ollama/ollama.py +330 -0
- lfx/components/ollama/ollama_embeddings.py +106 -0
- lfx/components/openai/__init__.py +37 -0
- lfx/components/openai/openai.py +100 -0
- lfx/components/openai/openai_chat_model.py +176 -0
- lfx/components/openrouter/__init__.py +32 -0
- lfx/components/openrouter/openrouter.py +202 -0
- lfx/components/output_parsers/__init__.py +3 -0
- lfx/components/perplexity/__init__.py +34 -0
- lfx/components/perplexity/perplexity.py +75 -0
- lfx/components/pgvector/__init__.py +34 -0
- lfx/components/pgvector/pgvector.py +72 -0
- lfx/components/pinecone/__init__.py +34 -0
- lfx/components/pinecone/pinecone.py +134 -0
- lfx/components/processing/__init__.py +117 -0
- lfx/components/processing/alter_metadata.py +108 -0
- lfx/components/processing/batch_run.py +205 -0
- lfx/components/processing/combine_text.py +39 -0
- lfx/components/processing/converter.py +159 -0
- lfx/components/processing/create_data.py +110 -0
- lfx/components/processing/data_operations.py +438 -0
- lfx/components/processing/data_to_dataframe.py +70 -0
- lfx/components/processing/dataframe_operations.py +313 -0
- lfx/components/processing/extract_key.py +53 -0
- lfx/components/processing/filter_data.py +42 -0
- lfx/components/processing/filter_data_values.py +88 -0
- lfx/components/processing/json_cleaner.py +103 -0
- lfx/components/processing/lambda_filter.py +154 -0
- lfx/components/processing/llm_router.py +499 -0
- lfx/components/processing/merge_data.py +90 -0
- lfx/components/processing/message_to_data.py +36 -0
- lfx/components/processing/parse_data.py +70 -0
- lfx/components/processing/parse_dataframe.py +68 -0
- lfx/components/processing/parse_json_data.py +90 -0
- lfx/components/processing/parser.py +143 -0
- lfx/components/processing/prompt.py +67 -0
- lfx/components/processing/python_repl_core.py +98 -0
- lfx/components/processing/regex.py +82 -0
- lfx/components/processing/save_file.py +225 -0
- lfx/components/processing/select_data.py +48 -0
- lfx/components/processing/split_text.py +141 -0
- lfx/components/processing/structured_output.py +202 -0
- lfx/components/processing/update_data.py +160 -0
- lfx/components/prototypes/__init__.py +34 -0
- lfx/components/prototypes/python_function.py +73 -0
- lfx/components/qdrant/__init__.py +34 -0
- lfx/components/qdrant/qdrant.py +109 -0
- lfx/components/redis/__init__.py +37 -0
- lfx/components/redis/redis.py +89 -0
- lfx/components/redis/redis_chat.py +43 -0
- lfx/components/sambanova/__init__.py +32 -0
- lfx/components/sambanova/sambanova.py +84 -0
- lfx/components/scrapegraph/__init__.py +40 -0
- lfx/components/scrapegraph/scrapegraph_markdownify_api.py +64 -0
- lfx/components/scrapegraph/scrapegraph_search_api.py +64 -0
- lfx/components/scrapegraph/scrapegraph_smart_scraper_api.py +71 -0
- lfx/components/searchapi/__init__.py +34 -0
- lfx/components/searchapi/search.py +79 -0
- lfx/components/serpapi/__init__.py +3 -0
- lfx/components/serpapi/serp.py +115 -0
- lfx/components/supabase/__init__.py +34 -0
- lfx/components/supabase/supabase.py +76 -0
- lfx/components/tavily/__init__.py +4 -0
- lfx/components/tavily/tavily_extract.py +117 -0
- lfx/components/tavily/tavily_search.py +212 -0
- lfx/components/textsplitters/__init__.py +3 -0
- lfx/components/toolkits/__init__.py +3 -0
- lfx/components/tools/__init__.py +72 -0
- lfx/components/tools/calculator.py +108 -0
- lfx/components/tools/google_search_api.py +45 -0
- lfx/components/tools/google_serper_api.py +115 -0
- lfx/components/tools/python_code_structured_tool.py +327 -0
- lfx/components/tools/python_repl.py +97 -0
- lfx/components/tools/search_api.py +87 -0
- lfx/components/tools/searxng.py +145 -0
- lfx/components/tools/serp_api.py +119 -0
- lfx/components/tools/tavily_search_tool.py +344 -0
- lfx/components/tools/wikidata_api.py +102 -0
- lfx/components/tools/wikipedia_api.py +49 -0
- lfx/components/tools/yahoo_finance.py +129 -0
- lfx/components/twelvelabs/__init__.py +52 -0
- lfx/components/twelvelabs/convert_astra_results.py +84 -0
- lfx/components/twelvelabs/pegasus_index.py +311 -0
- lfx/components/twelvelabs/split_video.py +291 -0
- lfx/components/twelvelabs/text_embeddings.py +57 -0
- lfx/components/twelvelabs/twelvelabs_pegasus.py +408 -0
- lfx/components/twelvelabs/video_embeddings.py +100 -0
- lfx/components/twelvelabs/video_file.py +179 -0
- lfx/components/unstructured/__init__.py +3 -0
- lfx/components/unstructured/unstructured.py +121 -0
- lfx/components/upstash/__init__.py +34 -0
- lfx/components/upstash/upstash.py +124 -0
- lfx/components/vectara/__init__.py +37 -0
- lfx/components/vectara/vectara.py +97 -0
- lfx/components/vectara/vectara_rag.py +164 -0
- lfx/components/vectorstores/__init__.py +40 -0
- lfx/components/vectorstores/astradb.py +1285 -0
- lfx/components/vectorstores/astradb_graph.py +319 -0
- lfx/components/vectorstores/cassandra.py +264 -0
- lfx/components/vectorstores/cassandra_graph.py +238 -0
- lfx/components/vectorstores/chroma.py +167 -0
- lfx/components/vectorstores/clickhouse.py +135 -0
- lfx/components/vectorstores/couchbase.py +102 -0
- lfx/components/vectorstores/elasticsearch.py +267 -0
- lfx/components/vectorstores/faiss.py +111 -0
- lfx/components/vectorstores/graph_rag.py +141 -0
- lfx/components/vectorstores/hcd.py +314 -0
- lfx/components/vectorstores/local_db.py +261 -0
- lfx/components/vectorstores/milvus.py +115 -0
- lfx/components/vectorstores/mongodb_atlas.py +213 -0
- lfx/components/vectorstores/opensearch.py +243 -0
- lfx/components/vectorstores/pgvector.py +72 -0
- lfx/components/vectorstores/pinecone.py +134 -0
- lfx/components/vectorstores/qdrant.py +109 -0
- lfx/components/vectorstores/supabase.py +76 -0
- lfx/components/vectorstores/upstash.py +124 -0
- lfx/components/vectorstores/vectara.py +97 -0
- lfx/components/vectorstores/vectara_rag.py +164 -0
- lfx/components/vectorstores/weaviate.py +89 -0
- lfx/components/vertexai/__init__.py +37 -0
- lfx/components/vertexai/vertexai.py +71 -0
- lfx/components/vertexai/vertexai_embeddings.py +67 -0
- lfx/components/weaviate/__init__.py +34 -0
- lfx/components/weaviate/weaviate.py +89 -0
- lfx/components/wikipedia/__init__.py +4 -0
- lfx/components/wikipedia/wikidata.py +86 -0
- lfx/components/wikipedia/wikipedia.py +53 -0
- lfx/components/wolframalpha/__init__.py +3 -0
- lfx/components/wolframalpha/wolfram_alpha_api.py +54 -0
- lfx/components/xai/__init__.py +32 -0
- lfx/components/xai/xai.py +167 -0
- lfx/components/yahoosearch/__init__.py +3 -0
- lfx/components/yahoosearch/yahoo.py +137 -0
- lfx/components/youtube/__init__.py +52 -0
- lfx/components/youtube/channel.py +227 -0
- lfx/components/youtube/comments.py +231 -0
- lfx/components/youtube/playlist.py +33 -0
- lfx/components/youtube/search.py +120 -0
- lfx/components/youtube/trending.py +285 -0
- lfx/components/youtube/video_details.py +263 -0
- lfx/components/youtube/youtube_transcripts.py +118 -0
- lfx/components/zep/__init__.py +3 -0
- lfx/components/zep/zep.py +44 -0
- lfx/constants.py +6 -0
- lfx/custom/__init__.py +7 -0
- lfx/custom/attributes.py +86 -0
- lfx/custom/code_parser/__init__.py +3 -0
- lfx/custom/code_parser/code_parser.py +361 -0
- lfx/custom/custom_component/__init__.py +0 -0
- lfx/custom/custom_component/base_component.py +128 -0
- lfx/custom/custom_component/component.py +1808 -0
- lfx/custom/custom_component/component_with_cache.py +8 -0
- lfx/custom/custom_component/custom_component.py +588 -0
- lfx/custom/dependency_analyzer.py +165 -0
- lfx/custom/directory_reader/__init__.py +3 -0
- lfx/custom/directory_reader/directory_reader.py +359 -0
- lfx/custom/directory_reader/utils.py +171 -0
- lfx/custom/eval.py +12 -0
- lfx/custom/schema.py +32 -0
- lfx/custom/tree_visitor.py +21 -0
- lfx/custom/utils.py +877 -0
- lfx/custom/validate.py +488 -0
- lfx/events/__init__.py +1 -0
- lfx/events/event_manager.py +110 -0
- lfx/exceptions/__init__.py +0 -0
- lfx/exceptions/component.py +15 -0
- lfx/field_typing/__init__.py +91 -0
- lfx/field_typing/constants.py +215 -0
- lfx/field_typing/range_spec.py +35 -0
- lfx/graph/__init__.py +6 -0
- lfx/graph/edge/__init__.py +0 -0
- lfx/graph/edge/base.py +277 -0
- lfx/graph/edge/schema.py +119 -0
- lfx/graph/edge/utils.py +0 -0
- lfx/graph/graph/__init__.py +0 -0
- lfx/graph/graph/ascii.py +202 -0
- lfx/graph/graph/base.py +2238 -0
- lfx/graph/graph/constants.py +63 -0
- lfx/graph/graph/runnable_vertices_manager.py +133 -0
- lfx/graph/graph/schema.py +52 -0
- lfx/graph/graph/state_model.py +66 -0
- lfx/graph/graph/utils.py +1024 -0
- lfx/graph/schema.py +75 -0
- lfx/graph/state/__init__.py +0 -0
- lfx/graph/state/model.py +237 -0
- lfx/graph/utils.py +200 -0
- lfx/graph/vertex/__init__.py +0 -0
- lfx/graph/vertex/base.py +823 -0
- lfx/graph/vertex/constants.py +0 -0
- lfx/graph/vertex/exceptions.py +4 -0
- lfx/graph/vertex/param_handler.py +264 -0
- lfx/graph/vertex/schema.py +26 -0
- lfx/graph/vertex/utils.py +19 -0
- lfx/graph/vertex/vertex_types.py +489 -0
- lfx/helpers/__init__.py +1 -0
- lfx/helpers/base_model.py +71 -0
- lfx/helpers/custom.py +13 -0
- lfx/helpers/data.py +167 -0
- lfx/helpers/flow.py +194 -0
- lfx/inputs/__init__.py +68 -0
- lfx/inputs/constants.py +2 -0
- lfx/inputs/input_mixin.py +328 -0
- lfx/inputs/inputs.py +714 -0
- lfx/inputs/validators.py +19 -0
- lfx/interface/__init__.py +6 -0
- lfx/interface/components.py +489 -0
- lfx/interface/importing/__init__.py +5 -0
- lfx/interface/importing/utils.py +39 -0
- lfx/interface/initialize/__init__.py +3 -0
- lfx/interface/initialize/loading.py +224 -0
- lfx/interface/listing.py +26 -0
- lfx/interface/run.py +16 -0
- lfx/interface/utils.py +111 -0
- lfx/io/__init__.py +63 -0
- lfx/io/schema.py +289 -0
- lfx/load/__init__.py +8 -0
- lfx/load/load.py +256 -0
- lfx/load/utils.py +99 -0
- lfx/log/__init__.py +5 -0
- lfx/log/logger.py +385 -0
- lfx/memory/__init__.py +90 -0
- lfx/memory/stubs.py +283 -0
- lfx/processing/__init__.py +1 -0
- lfx/processing/process.py +238 -0
- lfx/processing/utils.py +25 -0
- lfx/py.typed +0 -0
- lfx/schema/__init__.py +66 -0
- lfx/schema/artifact.py +83 -0
- lfx/schema/content_block.py +62 -0
- lfx/schema/content_types.py +91 -0
- lfx/schema/data.py +308 -0
- lfx/schema/dataframe.py +210 -0
- lfx/schema/dotdict.py +74 -0
- lfx/schema/encoders.py +13 -0
- lfx/schema/graph.py +47 -0
- lfx/schema/image.py +131 -0
- lfx/schema/json_schema.py +141 -0
- lfx/schema/log.py +61 -0
- lfx/schema/message.py +473 -0
- lfx/schema/openai_responses_schemas.py +74 -0
- lfx/schema/properties.py +41 -0
- lfx/schema/schema.py +171 -0
- lfx/schema/serialize.py +13 -0
- lfx/schema/table.py +140 -0
- lfx/schema/validators.py +114 -0
- lfx/serialization/__init__.py +5 -0
- lfx/serialization/constants.py +2 -0
- lfx/serialization/serialization.py +314 -0
- lfx/services/__init__.py +23 -0
- lfx/services/base.py +28 -0
- lfx/services/cache/__init__.py +6 -0
- lfx/services/cache/base.py +183 -0
- lfx/services/cache/service.py +166 -0
- lfx/services/cache/utils.py +169 -0
- lfx/services/chat/__init__.py +1 -0
- lfx/services/chat/config.py +2 -0
- lfx/services/chat/schema.py +10 -0
- lfx/services/deps.py +129 -0
- lfx/services/factory.py +19 -0
- lfx/services/initialize.py +19 -0
- lfx/services/interfaces.py +103 -0
- lfx/services/manager.py +172 -0
- lfx/services/schema.py +20 -0
- lfx/services/session.py +82 -0
- lfx/services/settings/__init__.py +3 -0
- lfx/services/settings/auth.py +130 -0
- lfx/services/settings/base.py +539 -0
- lfx/services/settings/constants.py +31 -0
- lfx/services/settings/factory.py +23 -0
- lfx/services/settings/feature_flags.py +12 -0
- lfx/services/settings/service.py +35 -0
- lfx/services/settings/utils.py +40 -0
- lfx/services/shared_component_cache/__init__.py +1 -0
- lfx/services/shared_component_cache/factory.py +30 -0
- lfx/services/shared_component_cache/service.py +9 -0
- lfx/services/storage/__init__.py +5 -0
- lfx/services/storage/local.py +155 -0
- lfx/services/storage/service.py +54 -0
- lfx/services/tracing/__init__.py +1 -0
- lfx/services/tracing/service.py +21 -0
- lfx/settings.py +6 -0
- lfx/template/__init__.py +6 -0
- lfx/template/field/__init__.py +0 -0
- lfx/template/field/base.py +257 -0
- lfx/template/field/prompt.py +15 -0
- lfx/template/frontend_node/__init__.py +6 -0
- lfx/template/frontend_node/base.py +212 -0
- lfx/template/frontend_node/constants.py +65 -0
- lfx/template/frontend_node/custom_components.py +79 -0
- lfx/template/template/__init__.py +0 -0
- lfx/template/template/base.py +100 -0
- lfx/template/utils.py +217 -0
- lfx/type_extraction/__init__.py +19 -0
- lfx/type_extraction/type_extraction.py +75 -0
- lfx/type_extraction.py +80 -0
- lfx/utils/__init__.py +1 -0
- lfx/utils/async_helpers.py +42 -0
- lfx/utils/component_utils.py +154 -0
- lfx/utils/concurrency.py +60 -0
- lfx/utils/connection_string_parser.py +11 -0
- lfx/utils/constants.py +205 -0
- lfx/utils/data_structure.py +212 -0
- lfx/utils/exceptions.py +22 -0
- lfx/utils/helpers.py +28 -0
- lfx/utils/image.py +73 -0
- lfx/utils/lazy_load.py +15 -0
- lfx/utils/request_utils.py +18 -0
- lfx/utils/schemas.py +139 -0
- lfx/utils/util.py +481 -0
- lfx/utils/util_strings.py +56 -0
- lfx/utils/version.py +24 -0
- lfx_nightly-0.1.11.dev0.dist-info/METADATA +293 -0
- lfx_nightly-0.1.11.dev0.dist-info/RECORD +699 -0
- lfx_nightly-0.1.11.dev0.dist-info/WHEEL +4 -0
- lfx_nightly-0.1.11.dev0.dist-info/entry_points.txt +2 -0
lfx/graph/graph/base.py
ADDED
@@ -0,0 +1,2238 @@
|
|
1
|
+
from __future__ import annotations
|
2
|
+
|
3
|
+
import asyncio
|
4
|
+
import contextlib
|
5
|
+
import contextvars
|
6
|
+
import copy
|
7
|
+
import json
|
8
|
+
import queue
|
9
|
+
import threading
|
10
|
+
import traceback
|
11
|
+
import uuid
|
12
|
+
from collections import defaultdict, deque
|
13
|
+
from datetime import datetime, timezone
|
14
|
+
from functools import partial
|
15
|
+
from itertools import chain
|
16
|
+
from typing import TYPE_CHECKING, Any, cast
|
17
|
+
|
18
|
+
from lfx.exceptions.component import ComponentBuildError
|
19
|
+
from lfx.graph.edge.base import CycleEdge, Edge
|
20
|
+
from lfx.graph.graph.constants import Finish, lazy_load_vertex_dict
|
21
|
+
from lfx.graph.graph.runnable_vertices_manager import RunnableVerticesManager
|
22
|
+
from lfx.graph.graph.schema import GraphData, GraphDump, StartConfigDict, VertexBuildResult
|
23
|
+
from lfx.graph.graph.state_model import create_state_model_from_graph
|
24
|
+
from lfx.graph.graph.utils import (
|
25
|
+
find_all_cycle_edges,
|
26
|
+
find_cycle_vertices,
|
27
|
+
find_start_component_id,
|
28
|
+
get_sorted_vertices,
|
29
|
+
process_flow,
|
30
|
+
should_continue,
|
31
|
+
)
|
32
|
+
from lfx.graph.schema import InterfaceComponentTypes, RunOutputs
|
33
|
+
from lfx.graph.utils import log_vertex_build
|
34
|
+
from lfx.graph.vertex.base import Vertex, VertexStates
|
35
|
+
from lfx.graph.vertex.schema import NodeData, NodeTypeEnum
|
36
|
+
from lfx.graph.vertex.vertex_types import ComponentVertex, InterfaceVertex, StateVertex
|
37
|
+
from lfx.log.logger import LogConfig, configure, logger
|
38
|
+
from lfx.schema.dotdict import dotdict
|
39
|
+
from lfx.schema.schema import INPUT_FIELD_NAME, InputType, OutputValue
|
40
|
+
from lfx.services.cache.utils import CacheMiss
|
41
|
+
from lfx.services.deps import get_chat_service, get_tracing_service
|
42
|
+
from lfx.utils.async_helpers import run_until_complete
|
43
|
+
|
44
|
+
if TYPE_CHECKING:
|
45
|
+
from collections.abc import Callable, Generator, Iterable
|
46
|
+
from typing import Any
|
47
|
+
|
48
|
+
from lfx.custom.custom_component.component import Component
|
49
|
+
from lfx.events.event_manager import EventManager
|
50
|
+
from lfx.graph.edge.schema import EdgeData
|
51
|
+
from lfx.graph.schema import ResultData
|
52
|
+
from lfx.schema.schema import InputValueRequest
|
53
|
+
from lfx.services.chat.schema import GetCache, SetCache
|
54
|
+
from lfx.services.tracing.service import TracingService
|
55
|
+
|
56
|
+
|
57
|
+
class Graph:
|
58
|
+
"""A class representing a graph of vertices and edges."""
|
59
|
+
|
60
|
+
def __init__(
|
61
|
+
self,
|
62
|
+
start: Component | None = None,
|
63
|
+
end: Component | None = None,
|
64
|
+
flow_id: str | None = None,
|
65
|
+
flow_name: str | None = None,
|
66
|
+
description: str | None = None,
|
67
|
+
user_id: str | None = None,
|
68
|
+
log_config: LogConfig | None = None,
|
69
|
+
context: dict[str, Any] | None = None,
|
70
|
+
) -> None:
|
71
|
+
"""Initializes a new Graph instance.
|
72
|
+
|
73
|
+
If both start and end components are provided, the graph is initialized and prepared for execution.
|
74
|
+
If only one is provided, a ValueError is raised. The context must be a dictionary if specified,
|
75
|
+
otherwise a TypeError is raised. Internal data structures for vertices, edges, state management,
|
76
|
+
run management, and tracing are set up during initialization.
|
77
|
+
"""
|
78
|
+
if log_config:
|
79
|
+
configure(**log_config)
|
80
|
+
|
81
|
+
self._start = start
|
82
|
+
self._state_model = None
|
83
|
+
self._end = end
|
84
|
+
self._prepared = False
|
85
|
+
self._runs = 0
|
86
|
+
self._updates = 0
|
87
|
+
self.flow_id = flow_id
|
88
|
+
self.flow_name = flow_name
|
89
|
+
self.description = description
|
90
|
+
self.user_id = user_id
|
91
|
+
self._is_input_vertices: list[str] = []
|
92
|
+
self._is_output_vertices: list[str] = []
|
93
|
+
self._is_state_vertices: list[str] | None = None
|
94
|
+
self.has_session_id_vertices: list[str] = []
|
95
|
+
self._sorted_vertices_layers: list[list[str]] = []
|
96
|
+
self._run_id = ""
|
97
|
+
self._session_id = ""
|
98
|
+
self._start_time = datetime.now(timezone.utc)
|
99
|
+
self.inactivated_vertices: set = set()
|
100
|
+
self.activated_vertices: list[str] = []
|
101
|
+
self.vertices_layers: list[list[str]] = []
|
102
|
+
self.vertices_to_run: set[str] = set()
|
103
|
+
self.stop_vertex: str | None = None
|
104
|
+
self.inactive_vertices: set = set()
|
105
|
+
self.edges: list[CycleEdge] = []
|
106
|
+
self.vertices: list[Vertex] = []
|
107
|
+
self.run_manager = RunnableVerticesManager()
|
108
|
+
self._vertices: list[NodeData] = []
|
109
|
+
self._edges: list[EdgeData] = []
|
110
|
+
|
111
|
+
self.top_level_vertices: list[str] = []
|
112
|
+
self.vertex_map: dict[str, Vertex] = {}
|
113
|
+
self.predecessor_map: dict[str, list[str]] = defaultdict(list)
|
114
|
+
self.successor_map: dict[str, list[str]] = defaultdict(list)
|
115
|
+
self.in_degree_map: dict[str, int] = defaultdict(int)
|
116
|
+
self.parent_child_map: dict[str, list[str]] = defaultdict(list)
|
117
|
+
self._run_queue: deque[str] = deque()
|
118
|
+
self._first_layer: list[str] = []
|
119
|
+
self._lock: asyncio.Lock | None = None
|
120
|
+
self.raw_graph_data: GraphData = {"nodes": [], "edges": []}
|
121
|
+
self._is_cyclic: bool | None = None
|
122
|
+
self._cycles: list[tuple[str, str]] | None = None
|
123
|
+
self._cycle_vertices: set[str] | None = None
|
124
|
+
self._call_order: list[str] = []
|
125
|
+
self._snapshots: list[dict[str, Any]] = []
|
126
|
+
self._end_trace_tasks: set[asyncio.Task] = set()
|
127
|
+
|
128
|
+
if context and not isinstance(context, dict):
|
129
|
+
msg = "Context must be a dictionary"
|
130
|
+
raise TypeError(msg)
|
131
|
+
self._context = dotdict(context or {})
|
132
|
+
# Lazy initialization - only get tracing service when needed
|
133
|
+
self._tracing_service: TracingService | None = None
|
134
|
+
self._tracing_service_initialized = False
|
135
|
+
if start is not None and end is not None:
|
136
|
+
self._set_start_and_end(start, end)
|
137
|
+
self.prepare(start_component_id=start.get_id())
|
138
|
+
if (start is not None and end is None) or (start is None and end is not None):
|
139
|
+
msg = "You must provide both input and output components"
|
140
|
+
raise ValueError(msg)
|
141
|
+
|
142
|
+
@property
|
143
|
+
def lock(self):
|
144
|
+
"""Lazy initialization of asyncio.Lock to avoid event loop binding issues."""
|
145
|
+
if self._lock is None:
|
146
|
+
self._lock = asyncio.Lock()
|
147
|
+
return self._lock
|
148
|
+
|
149
|
+
@property
|
150
|
+
def context(self) -> dotdict:
|
151
|
+
if isinstance(self._context, dotdict):
|
152
|
+
return self._context
|
153
|
+
return dotdict(self._context)
|
154
|
+
|
155
|
+
@context.setter
|
156
|
+
def context(self, value: dict[str, Any]):
|
157
|
+
if not isinstance(value, dict):
|
158
|
+
msg = "Context must be a dictionary"
|
159
|
+
raise TypeError(msg)
|
160
|
+
if isinstance(value, dict):
|
161
|
+
value = dotdict(value)
|
162
|
+
self._context = value
|
163
|
+
|
164
|
+
@property
|
165
|
+
def session_id(self):
|
166
|
+
return self._session_id
|
167
|
+
|
168
|
+
@session_id.setter
|
169
|
+
def session_id(self, value: str):
|
170
|
+
self._session_id = value
|
171
|
+
|
172
|
+
@property
|
173
|
+
def state_model(self):
|
174
|
+
if not self._state_model:
|
175
|
+
self._state_model = create_state_model_from_graph(self)
|
176
|
+
return self._state_model
|
177
|
+
|
178
|
+
def __add__(self, other):
|
179
|
+
if not isinstance(other, Graph):
|
180
|
+
msg = "Can only add Graph objects"
|
181
|
+
raise TypeError(msg)
|
182
|
+
# Add the vertices and edges from the other graph to this graph
|
183
|
+
new_instance = copy.deepcopy(self)
|
184
|
+
for vertex in other.vertices:
|
185
|
+
# This updates the edges as well
|
186
|
+
new_instance.add_vertex(vertex)
|
187
|
+
new_instance.build_graph_maps(new_instance.edges)
|
188
|
+
new_instance.define_vertices_lists()
|
189
|
+
return new_instance
|
190
|
+
|
191
|
+
def __iadd__(self, other):
|
192
|
+
if not isinstance(other, Graph):
|
193
|
+
msg = "Can only add Graph objects"
|
194
|
+
raise TypeError(msg)
|
195
|
+
# Add the vertices and edges from the other graph to this graph
|
196
|
+
for vertex in other.vertices:
|
197
|
+
# This updates the edges as well
|
198
|
+
self.add_vertex(vertex)
|
199
|
+
self.build_graph_maps(self.edges)
|
200
|
+
self.define_vertices_lists()
|
201
|
+
return self
|
202
|
+
|
203
|
+
@property
|
204
|
+
def tracing_service(self) -> TracingService | None:
|
205
|
+
"""Lazily initialize tracing service only when accessed."""
|
206
|
+
if not self._tracing_service_initialized:
|
207
|
+
try:
|
208
|
+
self._tracing_service = get_tracing_service()
|
209
|
+
except Exception: # noqa: BLE001
|
210
|
+
logger.exception("Error getting tracing service")
|
211
|
+
self._tracing_service = None
|
212
|
+
self._tracing_service_initialized = True
|
213
|
+
return self._tracing_service
|
214
|
+
|
215
|
+
def dumps(
|
216
|
+
self,
|
217
|
+
name: str | None = None,
|
218
|
+
description: str | None = None,
|
219
|
+
endpoint_name: str | None = None,
|
220
|
+
) -> str:
|
221
|
+
graph_dict = self.dump(name, description, endpoint_name)
|
222
|
+
return json.dumps(graph_dict, indent=4, sort_keys=True)
|
223
|
+
|
224
|
+
def dump(
|
225
|
+
self, name: str | None = None, description: str | None = None, endpoint_name: str | None = None
|
226
|
+
) -> GraphDump:
|
227
|
+
if self.raw_graph_data != {"nodes": [], "edges": []}:
|
228
|
+
data_dict = self.raw_graph_data
|
229
|
+
else:
|
230
|
+
# we need to convert the vertices and edges to json
|
231
|
+
nodes = [node.to_data() for node in self.vertices]
|
232
|
+
edges = [edge.to_data() for edge in self.edges]
|
233
|
+
self.raw_graph_data = {"nodes": nodes, "edges": edges}
|
234
|
+
data_dict = self.raw_graph_data
|
235
|
+
graph_dict: GraphDump = {
|
236
|
+
"data": data_dict,
|
237
|
+
"is_component": len(data_dict.get("nodes", [])) == 1 and data_dict["edges"] == [],
|
238
|
+
}
|
239
|
+
if name:
|
240
|
+
graph_dict["name"] = name
|
241
|
+
elif name is None and self.flow_name:
|
242
|
+
graph_dict["name"] = self.flow_name
|
243
|
+
if description:
|
244
|
+
graph_dict["description"] = description
|
245
|
+
elif description is None and self.description:
|
246
|
+
graph_dict["description"] = self.description
|
247
|
+
graph_dict["endpoint_name"] = str(endpoint_name)
|
248
|
+
return graph_dict
|
249
|
+
|
250
|
+
def add_nodes_and_edges(self, nodes: list[NodeData], edges: list[EdgeData]) -> None:
|
251
|
+
self._vertices = nodes
|
252
|
+
self._edges = edges
|
253
|
+
self.raw_graph_data = {"nodes": nodes, "edges": edges}
|
254
|
+
self.top_level_vertices = []
|
255
|
+
for vertex in self._vertices:
|
256
|
+
if vertex_id := vertex.get("id"):
|
257
|
+
self.top_level_vertices.append(vertex_id)
|
258
|
+
if vertex_id in self.cycle_vertices:
|
259
|
+
self.run_manager.add_to_cycle_vertices(vertex_id)
|
260
|
+
self._graph_data = process_flow(self.raw_graph_data)
|
261
|
+
|
262
|
+
self._vertices = self._graph_data["nodes"]
|
263
|
+
self._edges = self._graph_data["edges"]
|
264
|
+
self.initialize()
|
265
|
+
|
266
|
+
def add_component(self, component: Component, component_id: str | None = None) -> str:
|
267
|
+
component_id = component_id or component.get_id()
|
268
|
+
if component_id in self.vertex_map:
|
269
|
+
return component_id
|
270
|
+
component.set_id(component_id)
|
271
|
+
if component_id in self.vertex_map:
|
272
|
+
msg = f"Component ID {component_id} already exists"
|
273
|
+
raise ValueError(msg)
|
274
|
+
frontend_node = component.to_frontend_node()
|
275
|
+
self._vertices.append(frontend_node)
|
276
|
+
vertex = self._create_vertex(frontend_node)
|
277
|
+
vertex.add_component_instance(component)
|
278
|
+
self._add_vertex(vertex)
|
279
|
+
if component.get_edges():
|
280
|
+
for edge in component.get_edges():
|
281
|
+
self._add_edge(edge)
|
282
|
+
|
283
|
+
if component.get_components():
|
284
|
+
for _component in component.get_components():
|
285
|
+
self.add_component(_component)
|
286
|
+
|
287
|
+
return component_id
|
288
|
+
|
289
|
+
def _set_start_and_end(self, start: Component, end: Component) -> None:
|
290
|
+
if not hasattr(start, "to_frontend_node"):
|
291
|
+
msg = f"start must be a Component. Got {type(start)}"
|
292
|
+
raise TypeError(msg)
|
293
|
+
if not hasattr(end, "to_frontend_node"):
|
294
|
+
msg = f"end must be a Component. Got {type(end)}"
|
295
|
+
raise TypeError(msg)
|
296
|
+
self.add_component(start, start.get_id())
|
297
|
+
self.add_component(end, end.get_id())
|
298
|
+
|
299
|
+
def add_component_edge(self, source_id: str, output_input_tuple: tuple[str, str], target_id: str) -> None:
|
300
|
+
source_vertex = self.get_vertex(source_id)
|
301
|
+
if not isinstance(source_vertex, ComponentVertex):
|
302
|
+
msg = f"Source vertex {source_id} is not a component vertex."
|
303
|
+
raise TypeError(msg)
|
304
|
+
target_vertex = self.get_vertex(target_id)
|
305
|
+
if not isinstance(target_vertex, ComponentVertex):
|
306
|
+
msg = f"Target vertex {target_id} is not a component vertex."
|
307
|
+
raise TypeError(msg)
|
308
|
+
output_name, input_name = output_input_tuple
|
309
|
+
if source_vertex.custom_component is None:
|
310
|
+
msg = f"Source vertex {source_id} does not have a custom component."
|
311
|
+
raise ValueError(msg)
|
312
|
+
if target_vertex.custom_component is None:
|
313
|
+
msg = f"Target vertex {target_id} does not have a custom component."
|
314
|
+
raise ValueError(msg)
|
315
|
+
|
316
|
+
try:
|
317
|
+
input_field = target_vertex.get_input(input_name)
|
318
|
+
input_types = input_field.input_types
|
319
|
+
input_field_type = str(input_field.field_type)
|
320
|
+
except ValueError as e:
|
321
|
+
input_field = target_vertex.data.get("node", {}).get("template", {}).get(input_name)
|
322
|
+
if not input_field:
|
323
|
+
msg = f"Input field {input_name} not found in target vertex {target_id}"
|
324
|
+
raise ValueError(msg) from e
|
325
|
+
input_types = input_field.get("input_types", [])
|
326
|
+
input_field_type = input_field.get("type", "")
|
327
|
+
|
328
|
+
edge_data: EdgeData = {
|
329
|
+
"source": source_id,
|
330
|
+
"target": target_id,
|
331
|
+
"data": {
|
332
|
+
"sourceHandle": {
|
333
|
+
"dataType": source_vertex.custom_component.name
|
334
|
+
or source_vertex.custom_component.__class__.__name__,
|
335
|
+
"id": source_vertex.id,
|
336
|
+
"name": output_name,
|
337
|
+
"output_types": source_vertex.get_output(output_name).types,
|
338
|
+
},
|
339
|
+
"targetHandle": {
|
340
|
+
"fieldName": input_name,
|
341
|
+
"id": target_vertex.id,
|
342
|
+
"inputTypes": input_types,
|
343
|
+
"type": input_field_type,
|
344
|
+
},
|
345
|
+
},
|
346
|
+
}
|
347
|
+
self._add_edge(edge_data)
|
348
|
+
|
349
|
+
async def async_start(
|
350
|
+
self,
|
351
|
+
inputs: list[dict] | None = None,
|
352
|
+
max_iterations: int | None = None,
|
353
|
+
config: StartConfigDict | None = None,
|
354
|
+
event_manager: EventManager | None = None,
|
355
|
+
*,
|
356
|
+
reset_output_values: bool = True,
|
357
|
+
):
|
358
|
+
self.prepare()
|
359
|
+
if reset_output_values:
|
360
|
+
self._reset_all_output_values()
|
361
|
+
|
362
|
+
# The idea is for this to return a generator that yields the result of
|
363
|
+
# each step call and raise StopIteration when the graph is done
|
364
|
+
if config is not None:
|
365
|
+
self.__apply_config(config)
|
366
|
+
# I want to keep a counter of how many tyimes result.vertex.id
|
367
|
+
# has been yielded
|
368
|
+
yielded_counts: dict[str, int] = defaultdict(int)
|
369
|
+
|
370
|
+
while should_continue(yielded_counts, max_iterations):
|
371
|
+
result = await self.astep(event_manager=event_manager, inputs=inputs)
|
372
|
+
yield result
|
373
|
+
if isinstance(result, Finish):
|
374
|
+
return
|
375
|
+
if hasattr(result, "vertex"):
|
376
|
+
yielded_counts[result.vertex.id] += 1
|
377
|
+
|
378
|
+
msg = "Max iterations reached"
|
379
|
+
raise ValueError(msg)
|
380
|
+
|
381
|
+
def _snapshot(self):
|
382
|
+
return {
|
383
|
+
"_run_queue": self._run_queue.copy(),
|
384
|
+
"_first_layer": self._first_layer.copy(),
|
385
|
+
"vertices_layers": copy.deepcopy(self.vertices_layers),
|
386
|
+
"vertices_to_run": copy.deepcopy(self.vertices_to_run),
|
387
|
+
"run_manager": copy.deepcopy(self.run_manager.to_dict()),
|
388
|
+
}
|
389
|
+
|
390
|
+
def __apply_config(self, config: StartConfigDict) -> None:
|
391
|
+
for vertex in self.vertices:
|
392
|
+
if vertex.custom_component is None:
|
393
|
+
continue
|
394
|
+
for output in vertex.custom_component.get_outputs_map().values():
|
395
|
+
for key, value in config["output"].items():
|
396
|
+
setattr(output, key, value)
|
397
|
+
|
398
|
+
def _reset_all_output_values(self) -> None:
|
399
|
+
for vertex in self.vertices:
|
400
|
+
if vertex.custom_component is None:
|
401
|
+
continue
|
402
|
+
vertex.custom_component._reset_all_output_values()
|
403
|
+
|
404
|
+
def start(
|
405
|
+
self,
|
406
|
+
inputs: list[dict] | None = None,
|
407
|
+
max_iterations: int | None = None,
|
408
|
+
config: StartConfigDict | None = None,
|
409
|
+
event_manager: EventManager | None = None,
|
410
|
+
) -> Generator:
|
411
|
+
"""Starts the graph execution synchronously by creating a new event loop in a separate thread.
|
412
|
+
|
413
|
+
Args:
|
414
|
+
inputs: Optional list of input dictionaries
|
415
|
+
max_iterations: Optional maximum number of iterations
|
416
|
+
config: Optional configuration dictionary
|
417
|
+
event_manager: Optional event manager
|
418
|
+
|
419
|
+
Returns:
|
420
|
+
Generator yielding results from graph execution
|
421
|
+
"""
|
422
|
+
if self.is_cyclic and max_iterations is None:
|
423
|
+
msg = "You must specify a max_iterations if the graph is cyclic"
|
424
|
+
raise ValueError(msg)
|
425
|
+
|
426
|
+
if config is not None:
|
427
|
+
self.__apply_config(config)
|
428
|
+
|
429
|
+
# Create a queue for passing results and errors between threads
|
430
|
+
result_queue: queue.Queue[VertexBuildResult | Exception | None] = queue.Queue()
|
431
|
+
|
432
|
+
# Function to run async code in separate thread
|
433
|
+
def run_async_code():
|
434
|
+
# Create new event loop for this thread
|
435
|
+
loop = asyncio.new_event_loop()
|
436
|
+
asyncio.set_event_loop(loop)
|
437
|
+
|
438
|
+
try:
|
439
|
+
# Run the async generator
|
440
|
+
async_gen = self.async_start(inputs, max_iterations, event_manager)
|
441
|
+
|
442
|
+
while True:
|
443
|
+
try:
|
444
|
+
# Get next result from async generator
|
445
|
+
result = loop.run_until_complete(anext(async_gen))
|
446
|
+
result_queue.put(result)
|
447
|
+
|
448
|
+
if isinstance(result, Finish):
|
449
|
+
break
|
450
|
+
|
451
|
+
except StopAsyncIteration:
|
452
|
+
break
|
453
|
+
except ValueError as e:
|
454
|
+
# Put the exception in the queue
|
455
|
+
result_queue.put(e)
|
456
|
+
break
|
457
|
+
|
458
|
+
finally:
|
459
|
+
# Ensure all pending tasks are completed
|
460
|
+
pending = asyncio.all_tasks(loop)
|
461
|
+
if pending:
|
462
|
+
# Create a future to gather all pending tasks
|
463
|
+
cleanup_future = asyncio.gather(*pending, return_exceptions=True)
|
464
|
+
loop.run_until_complete(cleanup_future)
|
465
|
+
|
466
|
+
# Close the loop
|
467
|
+
loop.close()
|
468
|
+
# Signal completion
|
469
|
+
result_queue.put(None)
|
470
|
+
|
471
|
+
# Start thread for async execution
|
472
|
+
thread = threading.Thread(target=run_async_code)
|
473
|
+
thread.start()
|
474
|
+
|
475
|
+
# Yield results from queue
|
476
|
+
while True:
|
477
|
+
result = result_queue.get()
|
478
|
+
if result is None:
|
479
|
+
break
|
480
|
+
if isinstance(result, Exception):
|
481
|
+
raise result
|
482
|
+
yield result
|
483
|
+
|
484
|
+
# Wait for thread to complete
|
485
|
+
thread.join()
|
486
|
+
|
487
|
+
def _add_edge(self, edge: EdgeData) -> None:
|
488
|
+
self.add_edge(edge)
|
489
|
+
source_id = edge["data"]["sourceHandle"]["id"]
|
490
|
+
target_id = edge["data"]["targetHandle"]["id"]
|
491
|
+
self.predecessor_map[target_id].append(source_id)
|
492
|
+
self.successor_map[source_id].append(target_id)
|
493
|
+
self.in_degree_map[target_id] += 1
|
494
|
+
self.parent_child_map[source_id].append(target_id)
|
495
|
+
|
496
|
+
def add_node(self, node: NodeData) -> None:
|
497
|
+
self._vertices.append(node)
|
498
|
+
|
499
|
+
def add_edge(self, edge: EdgeData) -> None:
|
500
|
+
# Check if the edge already exists
|
501
|
+
if edge in self._edges:
|
502
|
+
return
|
503
|
+
self._edges.append(edge)
|
504
|
+
|
505
|
+
def initialize(self) -> None:
|
506
|
+
self._build_graph()
|
507
|
+
self.build_graph_maps(self.edges)
|
508
|
+
self.define_vertices_lists()
|
509
|
+
|
510
|
+
@property
|
511
|
+
def is_state_vertices(self) -> list[str]:
|
512
|
+
"""Returns a cached list of vertex IDs for vertices marked as state vertices.
|
513
|
+
|
514
|
+
The list is computed on first access by filtering vertices with `is_state` set to True and is
|
515
|
+
cached for future calls.
|
516
|
+
"""
|
517
|
+
if self._is_state_vertices is None:
|
518
|
+
self._is_state_vertices = [vertex.id for vertex in self.vertices if vertex.is_state]
|
519
|
+
return self._is_state_vertices
|
520
|
+
|
521
|
+
def activate_state_vertices(self, name: str, caller: str) -> None:
|
522
|
+
"""Activates vertices associated with a given state name.
|
523
|
+
|
524
|
+
Marks vertices with the specified state name, as well as their successors and related
|
525
|
+
predecessors. The state manager is then updated with the new state record.
|
526
|
+
"""
|
527
|
+
vertices_ids = set()
|
528
|
+
new_predecessor_map = {}
|
529
|
+
activated_vertices = []
|
530
|
+
for vertex_id in self.is_state_vertices:
|
531
|
+
caller_vertex = self.get_vertex(caller)
|
532
|
+
vertex = self.get_vertex(vertex_id)
|
533
|
+
if vertex_id == caller or vertex.display_name == caller_vertex.display_name:
|
534
|
+
continue
|
535
|
+
ctx_key = vertex.raw_params.get("context_key")
|
536
|
+
if isinstance(ctx_key, str) and name in ctx_key and vertex_id != caller and isinstance(vertex, StateVertex):
|
537
|
+
activated_vertices.append(vertex_id)
|
538
|
+
vertices_ids.add(vertex_id)
|
539
|
+
successors = self.get_all_successors(vertex, flat=True)
|
540
|
+
# Update run_manager.run_predecessors because we are activating vertices
|
541
|
+
# The run_prdecessors is the predecessor map of the vertices
|
542
|
+
# we remove the vertex_id from the predecessor map whenever we run a vertex
|
543
|
+
# So we need to get all edges of the vertex and successors
|
544
|
+
# and run self.build_adjacency_maps(edges) to get the new predecessor map
|
545
|
+
# that is not complete but we can use to update the run_predecessors
|
546
|
+
successors_predecessors = set()
|
547
|
+
for sucessor in successors:
|
548
|
+
successors_predecessors.update(self.get_all_predecessors(sucessor))
|
549
|
+
|
550
|
+
edges_set = set()
|
551
|
+
for _vertex in [vertex, *successors, *successors_predecessors]:
|
552
|
+
edges_set.update(_vertex.edges)
|
553
|
+
if _vertex.state == VertexStates.INACTIVE:
|
554
|
+
_vertex.set_state("ACTIVE")
|
555
|
+
|
556
|
+
vertices_ids.add(_vertex.id)
|
557
|
+
edges = list(edges_set)
|
558
|
+
predecessor_map, _ = self.build_adjacency_maps(edges)
|
559
|
+
new_predecessor_map.update(predecessor_map)
|
560
|
+
|
561
|
+
vertices_ids.update(new_predecessor_map.keys())
|
562
|
+
vertices_ids.update(v_id for value_list in new_predecessor_map.values() for v_id in value_list)
|
563
|
+
|
564
|
+
self.activated_vertices = activated_vertices
|
565
|
+
self.vertices_to_run.update(vertices_ids)
|
566
|
+
self.run_manager.update_run_state(
|
567
|
+
run_predecessors=new_predecessor_map,
|
568
|
+
vertices_to_run=self.vertices_to_run,
|
569
|
+
)
|
570
|
+
|
571
|
+
def reset_activated_vertices(self) -> None:
|
572
|
+
"""Resets the activated vertices in the graph."""
|
573
|
+
self.activated_vertices = []
|
574
|
+
|
575
|
+
def validate_stream(self) -> None:
|
576
|
+
"""Validates the stream configuration of the graph.
|
577
|
+
|
578
|
+
If there are two vertices in the same graph (connected by edges)
|
579
|
+
that have `stream=True` or `streaming=True`, raises a `ValueError`.
|
580
|
+
|
581
|
+
Raises:
|
582
|
+
ValueError: If two connected vertices have `stream=True` or `streaming=True`.
|
583
|
+
"""
|
584
|
+
for vertex in self.vertices:
|
585
|
+
if vertex.params.get("stream") or vertex.params.get("streaming"):
|
586
|
+
successors = self.get_all_successors(vertex)
|
587
|
+
for successor in successors:
|
588
|
+
if successor.params.get("stream") or successor.params.get("streaming"):
|
589
|
+
msg = (
|
590
|
+
f"Components {vertex.display_name} and {successor.display_name} "
|
591
|
+
"are connected and both have stream or streaming set to True"
|
592
|
+
)
|
593
|
+
raise ValueError(msg)
|
594
|
+
|
595
|
+
@property
|
596
|
+
def first_layer(self):
|
597
|
+
if self._first_layer is None:
|
598
|
+
msg = "Graph not prepared. Call prepare() first."
|
599
|
+
raise ValueError(msg)
|
600
|
+
return self._first_layer
|
601
|
+
|
602
|
+
@property
|
603
|
+
def is_cyclic(self):
|
604
|
+
"""Check if the graph has any cycles.
|
605
|
+
|
606
|
+
Returns:
|
607
|
+
bool: True if the graph has any cycles, False otherwise.
|
608
|
+
"""
|
609
|
+
if self._is_cyclic is None:
|
610
|
+
self._is_cyclic = bool(self.cycle_vertices)
|
611
|
+
return self._is_cyclic
|
612
|
+
|
613
|
+
@property
|
614
|
+
def run_id(self):
|
615
|
+
"""The ID of the current run.
|
616
|
+
|
617
|
+
Returns:
|
618
|
+
str: The run ID.
|
619
|
+
|
620
|
+
Raises:
|
621
|
+
ValueError: If the run ID is not set.
|
622
|
+
"""
|
623
|
+
if not self._run_id:
|
624
|
+
msg = "Run ID not set"
|
625
|
+
raise ValueError(msg)
|
626
|
+
return self._run_id
|
627
|
+
|
628
|
+
def set_run_id(self, run_id: uuid.UUID | str | None = None) -> None:
|
629
|
+
"""Sets the ID of the current run.
|
630
|
+
|
631
|
+
Args:
|
632
|
+
run_id (str): The run ID.
|
633
|
+
"""
|
634
|
+
if run_id is None:
|
635
|
+
run_id = uuid.uuid4()
|
636
|
+
|
637
|
+
self._run_id = str(run_id)
|
638
|
+
|
639
|
+
async def initialize_run(self) -> None:
|
640
|
+
if not self._run_id:
|
641
|
+
self.set_run_id()
|
642
|
+
if self.tracing_service:
|
643
|
+
run_name = f"{self.flow_name} - {self.flow_id}"
|
644
|
+
await self.tracing_service.start_tracers(
|
645
|
+
run_id=uuid.UUID(self._run_id),
|
646
|
+
run_name=run_name,
|
647
|
+
user_id=self.user_id,
|
648
|
+
session_id=self.session_id,
|
649
|
+
)
|
650
|
+
|
651
|
+
def _end_all_traces_async(self, outputs: dict[str, Any] | None = None, error: Exception | None = None) -> None:
|
652
|
+
task = asyncio.create_task(self.end_all_traces(outputs, error))
|
653
|
+
self._end_trace_tasks.add(task)
|
654
|
+
task.add_done_callback(self._end_trace_tasks.discard)
|
655
|
+
|
656
|
+
def end_all_traces_in_context(
|
657
|
+
self,
|
658
|
+
outputs: dict[str, Any] | None = None,
|
659
|
+
error: Exception | None = None,
|
660
|
+
) -> Callable:
|
661
|
+
# BackgroundTasks run in different context, so we need to copy the context
|
662
|
+
context = contextvars.copy_context()
|
663
|
+
|
664
|
+
async def async_end_traces_func():
|
665
|
+
await asyncio.create_task(self.end_all_traces(outputs, error), context=context)
|
666
|
+
|
667
|
+
return async_end_traces_func
|
668
|
+
|
669
|
+
async def end_all_traces(self, outputs: dict[str, Any] | None = None, error: Exception | None = None) -> None:
|
670
|
+
if not self.tracing_service:
|
671
|
+
return
|
672
|
+
self._end_time = datetime.now(timezone.utc)
|
673
|
+
if outputs is None:
|
674
|
+
outputs = {}
|
675
|
+
outputs |= self.metadata
|
676
|
+
await self.tracing_service.end_tracers(outputs, error)
|
677
|
+
|
678
|
+
@property
|
679
|
+
def sorted_vertices_layers(self) -> list[list[str]]:
|
680
|
+
"""Returns the sorted layers of vertex IDs by type.
|
681
|
+
|
682
|
+
Each layer in the returned list contains vertex IDs grouped by their classification,
|
683
|
+
such as input, output, session, or state vertices. Sorting is performed if not already done.
|
684
|
+
"""
|
685
|
+
if not self._sorted_vertices_layers:
|
686
|
+
self.sort_vertices()
|
687
|
+
return self._sorted_vertices_layers
|
688
|
+
|
689
|
+
def define_vertices_lists(self) -> None:
|
690
|
+
"""Populates internal lists of input, output, session ID, and state vertex IDs.
|
691
|
+
|
692
|
+
Iterates over all vertices and appends their IDs to the corresponding internal lists
|
693
|
+
based on their classification.
|
694
|
+
"""
|
695
|
+
for vertex in self.vertices:
|
696
|
+
if vertex.is_input:
|
697
|
+
self._is_input_vertices.append(vertex.id)
|
698
|
+
if vertex.is_output:
|
699
|
+
self._is_output_vertices.append(vertex.id)
|
700
|
+
if vertex.has_session_id:
|
701
|
+
self.has_session_id_vertices.append(vertex.id)
|
702
|
+
if vertex.is_state:
|
703
|
+
if self._is_state_vertices is None:
|
704
|
+
self._is_state_vertices = []
|
705
|
+
self._is_state_vertices.append(vertex.id)
|
706
|
+
|
707
|
+
def _set_inputs(self, input_components: list[str], inputs: dict[str, str], input_type: InputType | None) -> None:
|
708
|
+
"""Updates input vertices' parameters with the provided inputs, filtering by component list and input type.
|
709
|
+
|
710
|
+
Only vertices whose IDs or display names match the specified input components and whose IDs contain
|
711
|
+
the input type (unless input type is 'any' or None) are updated. Raises a ValueError if a specified
|
712
|
+
vertex is not found.
|
713
|
+
"""
|
714
|
+
for vertex_id in self._is_input_vertices:
|
715
|
+
vertex = self.get_vertex(vertex_id)
|
716
|
+
# If the vertex is not in the input_components list
|
717
|
+
if input_components and (vertex_id not in input_components and vertex.display_name not in input_components):
|
718
|
+
continue
|
719
|
+
# If the input_type is not any and the input_type is not in the vertex id
|
720
|
+
# Example: input_type = "chat" and vertex.id = "OpenAI-19ddn"
|
721
|
+
if input_type is not None and input_type != "any" and input_type not in vertex.id.lower():
|
722
|
+
continue
|
723
|
+
if vertex is None:
|
724
|
+
msg = f"Vertex {vertex_id} not found"
|
725
|
+
raise ValueError(msg)
|
726
|
+
vertex.update_raw_params(inputs, overwrite=True)
|
727
|
+
|
728
|
+
async def _run(
|
729
|
+
self,
|
730
|
+
*,
|
731
|
+
inputs: dict[str, str],
|
732
|
+
input_components: list[str],
|
733
|
+
input_type: InputType | None,
|
734
|
+
outputs: list[str],
|
735
|
+
stream: bool,
|
736
|
+
session_id: str,
|
737
|
+
fallback_to_env_vars: bool,
|
738
|
+
event_manager: EventManager | None = None,
|
739
|
+
) -> list[ResultData | None]:
|
740
|
+
"""Runs the graph with the given inputs.
|
741
|
+
|
742
|
+
Args:
|
743
|
+
inputs (Dict[str, str]): The input values for the graph.
|
744
|
+
input_components (list[str]): The components to run for the inputs.
|
745
|
+
input_type: (Optional[InputType]): The input type.
|
746
|
+
outputs (list[str]): The outputs to retrieve from the graph.
|
747
|
+
stream (bool): Whether to stream the results or not.
|
748
|
+
session_id (str): The session ID for the graph.
|
749
|
+
fallback_to_env_vars (bool): Whether to fallback to environment variables.
|
750
|
+
event_manager (EventManager | None): The event manager for the graph.
|
751
|
+
|
752
|
+
Returns:
|
753
|
+
List[Optional["ResultData"]]: The outputs of the graph.
|
754
|
+
"""
|
755
|
+
if input_components and not isinstance(input_components, list):
|
756
|
+
msg = f"Invalid components value: {input_components}. Expected list"
|
757
|
+
raise ValueError(msg)
|
758
|
+
if input_components is None:
|
759
|
+
input_components = []
|
760
|
+
|
761
|
+
if not isinstance(inputs.get(INPUT_FIELD_NAME, ""), str):
|
762
|
+
msg = f"Invalid input value: {inputs.get(INPUT_FIELD_NAME)}. Expected string"
|
763
|
+
raise TypeError(msg)
|
764
|
+
if inputs:
|
765
|
+
self._set_inputs(input_components, inputs, input_type)
|
766
|
+
# Update all the vertices with the session_id
|
767
|
+
for vertex_id in self.has_session_id_vertices:
|
768
|
+
vertex = self.get_vertex(vertex_id)
|
769
|
+
if vertex is None:
|
770
|
+
msg = f"Vertex {vertex_id} not found"
|
771
|
+
raise ValueError(msg)
|
772
|
+
vertex.update_raw_params({"session_id": session_id})
|
773
|
+
# Process the graph
|
774
|
+
try:
|
775
|
+
cache_service = get_chat_service()
|
776
|
+
if cache_service and self.flow_id:
|
777
|
+
await cache_service.set_cache(self.flow_id, self)
|
778
|
+
except Exception: # noqa: BLE001
|
779
|
+
logger.exception("Error setting cache")
|
780
|
+
|
781
|
+
try:
|
782
|
+
# Prioritize the webhook component if it exists
|
783
|
+
start_component_id = find_start_component_id(self._is_input_vertices)
|
784
|
+
await self.process(
|
785
|
+
start_component_id=start_component_id,
|
786
|
+
fallback_to_env_vars=fallback_to_env_vars,
|
787
|
+
event_manager=event_manager,
|
788
|
+
)
|
789
|
+
self.increment_run_count()
|
790
|
+
except Exception as exc:
|
791
|
+
self._end_all_traces_async(error=exc)
|
792
|
+
msg = f"Error running graph: {exc}"
|
793
|
+
raise ValueError(msg) from exc
|
794
|
+
|
795
|
+
self._end_all_traces_async()
|
796
|
+
# Get the outputs
|
797
|
+
vertex_outputs = []
|
798
|
+
for vertex in self.vertices:
|
799
|
+
if not vertex.built:
|
800
|
+
continue
|
801
|
+
if vertex is None:
|
802
|
+
msg = f"Vertex {vertex_id} not found"
|
803
|
+
raise ValueError(msg)
|
804
|
+
|
805
|
+
if not vertex.result and not stream and hasattr(vertex, "consume_async_generator"):
|
806
|
+
await vertex.consume_async_generator()
|
807
|
+
if (not outputs and vertex.is_output) or (vertex.display_name in outputs or vertex.id in outputs):
|
808
|
+
vertex_outputs.append(vertex.result)
|
809
|
+
|
810
|
+
return vertex_outputs
|
811
|
+
|
812
|
+
async def arun(
|
813
|
+
self,
|
814
|
+
inputs: list[dict[str, str]],
|
815
|
+
*,
|
816
|
+
inputs_components: list[list[str]] | None = None,
|
817
|
+
types: list[InputType | None] | None = None,
|
818
|
+
outputs: list[str] | None = None,
|
819
|
+
session_id: str | None = None,
|
820
|
+
stream: bool = False,
|
821
|
+
fallback_to_env_vars: bool = False,
|
822
|
+
event_manager: EventManager | None = None,
|
823
|
+
) -> list[RunOutputs]:
|
824
|
+
"""Runs the graph with the given inputs.
|
825
|
+
|
826
|
+
Args:
|
827
|
+
inputs (list[Dict[str, str]]): The input values for the graph.
|
828
|
+
inputs_components (Optional[list[list[str]]], optional): Components to run for the inputs. Defaults to None.
|
829
|
+
types (Optional[list[Optional[InputType]]], optional): The types of the inputs. Defaults to None.
|
830
|
+
outputs (Optional[list[str]], optional): The outputs to retrieve from the graph. Defaults to None.
|
831
|
+
session_id (Optional[str], optional): The session ID for the graph. Defaults to None.
|
832
|
+
stream (bool, optional): Whether to stream the results or not. Defaults to False.
|
833
|
+
fallback_to_env_vars (bool, optional): Whether to fallback to environment variables. Defaults to False.
|
834
|
+
event_manager (EventManager | None): The event manager for the graph.
|
835
|
+
|
836
|
+
Returns:
|
837
|
+
List[RunOutputs]: The outputs of the graph.
|
838
|
+
"""
|
839
|
+
# inputs is {"message": "Hello, world!"}
|
840
|
+
# we need to go through self.inputs and update the self.raw_params
|
841
|
+
# of the vertices that are inputs
|
842
|
+
# if the value is a list, we need to run multiple times
|
843
|
+
vertex_outputs = []
|
844
|
+
if not isinstance(inputs, list):
|
845
|
+
inputs = [inputs]
|
846
|
+
elif not inputs:
|
847
|
+
inputs = [{}]
|
848
|
+
# Length of all should be the as inputs length
|
849
|
+
# just add empty lists to complete the length
|
850
|
+
if inputs_components is None:
|
851
|
+
inputs_components = []
|
852
|
+
for _ in range(len(inputs) - len(inputs_components)):
|
853
|
+
inputs_components.append([])
|
854
|
+
if types is None:
|
855
|
+
types = []
|
856
|
+
if session_id:
|
857
|
+
self.session_id = session_id
|
858
|
+
for _ in range(len(inputs) - len(types)):
|
859
|
+
types.append("chat") # default to chat
|
860
|
+
for run_inputs, components, input_type in zip(inputs, inputs_components, types, strict=True):
|
861
|
+
run_outputs = await self._run(
|
862
|
+
inputs=run_inputs,
|
863
|
+
input_components=components,
|
864
|
+
input_type=input_type,
|
865
|
+
outputs=outputs or [],
|
866
|
+
stream=stream,
|
867
|
+
session_id=session_id or "",
|
868
|
+
fallback_to_env_vars=fallback_to_env_vars,
|
869
|
+
event_manager=event_manager,
|
870
|
+
)
|
871
|
+
run_output_object = RunOutputs(inputs=run_inputs, outputs=run_outputs)
|
872
|
+
await logger.adebug(f"Run outputs: {run_output_object}")
|
873
|
+
vertex_outputs.append(run_output_object)
|
874
|
+
return vertex_outputs
|
875
|
+
|
876
|
+
def next_vertex_to_build(self):
|
877
|
+
"""Returns the next vertex to be built.
|
878
|
+
|
879
|
+
Yields:
|
880
|
+
str: The ID of the next vertex to be built.
|
881
|
+
"""
|
882
|
+
yield from chain.from_iterable(self.vertices_layers)
|
883
|
+
|
884
|
+
@property
|
885
|
+
def metadata(self):
|
886
|
+
"""The metadata of the graph.
|
887
|
+
|
888
|
+
Returns:
|
889
|
+
dict: The metadata of the graph.
|
890
|
+
"""
|
891
|
+
time_format = "%Y-%m-%d %H:%M:%S %Z"
|
892
|
+
return {
|
893
|
+
"start_time": self._start_time.strftime(time_format),
|
894
|
+
"end_time": self._end_time.strftime(time_format),
|
895
|
+
"time_elapsed": f"{(self._end_time - self._start_time).total_seconds()} seconds",
|
896
|
+
"flow_id": self.flow_id,
|
897
|
+
"flow_name": self.flow_name,
|
898
|
+
}
|
899
|
+
|
900
|
+
def build_graph_maps(self, edges: list[CycleEdge] | None = None, vertices: list[Vertex] | None = None) -> None:
|
901
|
+
"""Builds the adjacency maps for the graph."""
|
902
|
+
if edges is None:
|
903
|
+
edges = self.edges
|
904
|
+
|
905
|
+
if vertices is None:
|
906
|
+
vertices = self.vertices
|
907
|
+
|
908
|
+
self.predecessor_map, self.successor_map = self.build_adjacency_maps(edges)
|
909
|
+
|
910
|
+
self.in_degree_map = self.build_in_degree(edges)
|
911
|
+
self.parent_child_map = self.build_parent_child_map(vertices)
|
912
|
+
|
913
|
+
def reset_inactivated_vertices(self) -> None:
|
914
|
+
"""Resets the inactivated vertices in the graph."""
|
915
|
+
for vertex_id in self.inactivated_vertices.copy():
|
916
|
+
self.mark_vertex(vertex_id, "ACTIVE")
|
917
|
+
self.inactivated_vertices = set()
|
918
|
+
self.inactivated_vertices = set()
|
919
|
+
|
920
|
+
def mark_all_vertices(self, state: str) -> None:
|
921
|
+
"""Marks all vertices in the graph."""
|
922
|
+
for vertex in self.vertices:
|
923
|
+
vertex.set_state(state)
|
924
|
+
|
925
|
+
def mark_vertex(self, vertex_id: str, state: str) -> None:
|
926
|
+
"""Marks a vertex in the graph."""
|
927
|
+
vertex = self.get_vertex(vertex_id)
|
928
|
+
vertex.set_state(state)
|
929
|
+
if state == VertexStates.INACTIVE:
|
930
|
+
self.run_manager.remove_from_predecessors(vertex_id)
|
931
|
+
|
932
|
+
def _mark_branch(
|
933
|
+
self, vertex_id: str, state: str, visited: set | None = None, output_name: str | None = None
|
934
|
+
) -> set:
|
935
|
+
"""Marks a branch of the graph."""
|
936
|
+
if visited is None:
|
937
|
+
visited = set()
|
938
|
+
else:
|
939
|
+
self.mark_vertex(vertex_id, state)
|
940
|
+
if vertex_id in visited:
|
941
|
+
return visited
|
942
|
+
visited.add(vertex_id)
|
943
|
+
|
944
|
+
for child_id in self.parent_child_map[vertex_id]:
|
945
|
+
# Only child_id that have an edge with the vertex_id through the output_name
|
946
|
+
# should be marked
|
947
|
+
if output_name:
|
948
|
+
edge = self.get_edge(vertex_id, child_id)
|
949
|
+
if edge and edge.source_handle.name != output_name:
|
950
|
+
continue
|
951
|
+
self._mark_branch(child_id, state, visited)
|
952
|
+
return visited
|
953
|
+
|
954
|
+
def mark_branch(self, vertex_id: str, state: str, output_name: str | None = None) -> None:
|
955
|
+
visited = self._mark_branch(vertex_id=vertex_id, state=state, output_name=output_name)
|
956
|
+
new_predecessor_map, _ = self.build_adjacency_maps(self.edges)
|
957
|
+
new_predecessor_map = {k: v for k, v in new_predecessor_map.items() if k in visited}
|
958
|
+
if vertex_id in self.cycle_vertices:
|
959
|
+
# Remove dependencies that are not in the cycle and have run at least once
|
960
|
+
new_predecessor_map = {
|
961
|
+
k: [dep for dep in v if dep in self.cycle_vertices and dep in self.run_manager.ran_at_least_once]
|
962
|
+
for k, v in new_predecessor_map.items()
|
963
|
+
}
|
964
|
+
self.run_manager.update_run_state(
|
965
|
+
run_predecessors=new_predecessor_map,
|
966
|
+
vertices_to_run=self.vertices_to_run,
|
967
|
+
)
|
968
|
+
|
969
|
+
def get_edge(self, source_id: str, target_id: str) -> CycleEdge | None:
|
970
|
+
"""Returns the edge between two vertices."""
|
971
|
+
for edge in self.edges:
|
972
|
+
if edge.source_id == source_id and edge.target_id == target_id:
|
973
|
+
return edge
|
974
|
+
return None
|
975
|
+
|
976
|
+
def build_parent_child_map(self, vertices: list[Vertex]):
|
977
|
+
parent_child_map = defaultdict(list)
|
978
|
+
for vertex in vertices:
|
979
|
+
parent_child_map[vertex.id] = [child.id for child in self.get_successors(vertex)]
|
980
|
+
return parent_child_map
|
981
|
+
|
982
|
+
def increment_run_count(self) -> None:
|
983
|
+
self._runs += 1
|
984
|
+
|
985
|
+
def increment_update_count(self) -> None:
|
986
|
+
self._updates += 1
|
987
|
+
|
988
|
+
def __getstate__(self):
|
989
|
+
# Get all attributes that are useful in runs.
|
990
|
+
# We don't need to save the state_manager because it is
|
991
|
+
# a singleton and it is not necessary to save it
|
992
|
+
return {
|
993
|
+
"vertices": self.vertices,
|
994
|
+
"edges": self.edges,
|
995
|
+
"flow_id": self.flow_id,
|
996
|
+
"flow_name": self.flow_name,
|
997
|
+
"description": self.description,
|
998
|
+
"user_id": self.user_id,
|
999
|
+
"raw_graph_data": self.raw_graph_data,
|
1000
|
+
"top_level_vertices": self.top_level_vertices,
|
1001
|
+
"inactivated_vertices": self.inactivated_vertices,
|
1002
|
+
"run_manager": self.run_manager.to_dict(),
|
1003
|
+
"_run_id": self._run_id,
|
1004
|
+
"in_degree_map": self.in_degree_map,
|
1005
|
+
"parent_child_map": self.parent_child_map,
|
1006
|
+
"predecessor_map": self.predecessor_map,
|
1007
|
+
"successor_map": self.successor_map,
|
1008
|
+
"activated_vertices": self.activated_vertices,
|
1009
|
+
"vertices_layers": self.vertices_layers,
|
1010
|
+
"vertices_to_run": self.vertices_to_run,
|
1011
|
+
"stop_vertex": self.stop_vertex,
|
1012
|
+
"_run_queue": self._run_queue,
|
1013
|
+
"_first_layer": self._first_layer,
|
1014
|
+
"_vertices": self._vertices,
|
1015
|
+
"_edges": self._edges,
|
1016
|
+
"_is_input_vertices": self._is_input_vertices,
|
1017
|
+
"_is_output_vertices": self._is_output_vertices,
|
1018
|
+
"has_session_id_vertices": self.has_session_id_vertices,
|
1019
|
+
"_sorted_vertices_layers": self._sorted_vertices_layers,
|
1020
|
+
}
|
1021
|
+
|
1022
|
+
def __deepcopy__(self, memo):
|
1023
|
+
# Check if we've already copied this instance
|
1024
|
+
if id(self) in memo:
|
1025
|
+
return memo[id(self)]
|
1026
|
+
|
1027
|
+
if self._start is not None and self._end is not None:
|
1028
|
+
# Deep copy start and end components
|
1029
|
+
start_copy = copy.deepcopy(self._start, memo)
|
1030
|
+
end_copy = copy.deepcopy(self._end, memo)
|
1031
|
+
new_graph = type(self)(
|
1032
|
+
start_copy,
|
1033
|
+
end_copy,
|
1034
|
+
copy.deepcopy(self.flow_id, memo),
|
1035
|
+
copy.deepcopy(self.flow_name, memo),
|
1036
|
+
copy.deepcopy(self.user_id, memo),
|
1037
|
+
)
|
1038
|
+
else:
|
1039
|
+
# Create a new graph without start and end, but copy flow_id, flow_name, and user_id
|
1040
|
+
new_graph = type(self)(
|
1041
|
+
None,
|
1042
|
+
None,
|
1043
|
+
copy.deepcopy(self.flow_id, memo),
|
1044
|
+
copy.deepcopy(self.flow_name, memo),
|
1045
|
+
copy.deepcopy(self.user_id, memo),
|
1046
|
+
)
|
1047
|
+
# Deep copy vertices and edges
|
1048
|
+
new_graph.add_nodes_and_edges(copy.deepcopy(self._vertices, memo), copy.deepcopy(self._edges, memo))
|
1049
|
+
|
1050
|
+
# Store the newly created object in memo
|
1051
|
+
memo[id(self)] = new_graph
|
1052
|
+
|
1053
|
+
return new_graph
|
1054
|
+
|
1055
|
+
def __setstate__(self, state):
|
1056
|
+
run_manager = state["run_manager"]
|
1057
|
+
if isinstance(run_manager, RunnableVerticesManager):
|
1058
|
+
state["run_manager"] = run_manager
|
1059
|
+
else:
|
1060
|
+
state["run_manager"] = RunnableVerticesManager.from_dict(run_manager)
|
1061
|
+
self.__dict__.update(state)
|
1062
|
+
self.vertex_map = {vertex.id: vertex for vertex in self.vertices}
|
1063
|
+
# Tracing service will be lazily initialized via property when needed
|
1064
|
+
self.set_run_id(self._run_id)
|
1065
|
+
|
1066
|
+
@classmethod
|
1067
|
+
def from_payload(
|
1068
|
+
cls,
|
1069
|
+
payload: dict,
|
1070
|
+
flow_id: str | None = None,
|
1071
|
+
flow_name: str | None = None,
|
1072
|
+
user_id: str | None = None,
|
1073
|
+
context: dict | None = None,
|
1074
|
+
) -> Graph:
|
1075
|
+
"""Creates a graph from a payload.
|
1076
|
+
|
1077
|
+
Args:
|
1078
|
+
payload: The payload to create the graph from.
|
1079
|
+
flow_id: The ID of the flow.
|
1080
|
+
flow_name: The flow name.
|
1081
|
+
user_id: The user ID.
|
1082
|
+
context: Optional context dictionary for request-specific data.
|
1083
|
+
|
1084
|
+
Returns:
|
1085
|
+
Graph: The created graph.
|
1086
|
+
"""
|
1087
|
+
if "data" in payload:
|
1088
|
+
payload = payload["data"]
|
1089
|
+
try:
|
1090
|
+
vertices = payload["nodes"]
|
1091
|
+
edges = payload["edges"]
|
1092
|
+
graph = cls(flow_id=flow_id, flow_name=flow_name, user_id=user_id, context=context)
|
1093
|
+
graph.add_nodes_and_edges(vertices, edges)
|
1094
|
+
except KeyError as exc:
|
1095
|
+
logger.exception(exc)
|
1096
|
+
if "nodes" not in payload and "edges" not in payload:
|
1097
|
+
msg = f"Invalid payload. Expected keys 'nodes' and 'edges'. Found {list(payload.keys())}"
|
1098
|
+
raise ValueError(msg) from exc
|
1099
|
+
|
1100
|
+
msg = f"Error while creating graph from payload: {exc}"
|
1101
|
+
raise ValueError(msg) from exc
|
1102
|
+
else:
|
1103
|
+
return graph
|
1104
|
+
|
1105
|
+
def __eq__(self, /, other: object) -> bool:
|
1106
|
+
if not isinstance(other, Graph):
|
1107
|
+
return False
|
1108
|
+
return self.__repr__() == other.__repr__()
|
1109
|
+
|
1110
|
+
# update this graph with another graph by comparing the __repr__ of each vertex
|
1111
|
+
# and if the __repr__ of a vertex is not the same as the other
|
1112
|
+
# then update the .data of the vertex to the self
|
1113
|
+
# both graphs have the same vertices and edges
|
1114
|
+
# but the data of the vertices might be different
|
1115
|
+
|
1116
|
+
def update_edges_from_vertex(self, other_vertex: Vertex) -> None:
|
1117
|
+
"""Updates the edges of a vertex in the Graph."""
|
1118
|
+
new_edges = []
|
1119
|
+
for edge in self.edges:
|
1120
|
+
if other_vertex.id in {edge.source_id, edge.target_id}:
|
1121
|
+
continue
|
1122
|
+
new_edges.append(edge)
|
1123
|
+
new_edges += other_vertex.edges
|
1124
|
+
self.edges = new_edges
|
1125
|
+
|
1126
|
+
def vertex_data_is_identical(self, vertex: Vertex, other_vertex: Vertex) -> bool:
|
1127
|
+
data_is_equivalent = vertex == other_vertex
|
1128
|
+
if not data_is_equivalent:
|
1129
|
+
return False
|
1130
|
+
return self.vertex_edges_are_identical(vertex, other_vertex)
|
1131
|
+
|
1132
|
+
@staticmethod
|
1133
|
+
def vertex_edges_are_identical(vertex: Vertex, other_vertex: Vertex) -> bool:
|
1134
|
+
same_length = len(vertex.edges) == len(other_vertex.edges)
|
1135
|
+
if not same_length:
|
1136
|
+
return False
|
1137
|
+
return all(edge in other_vertex.edges for edge in vertex.edges)
|
1138
|
+
|
1139
|
+
def update(self, other: Graph) -> Graph:
|
1140
|
+
# Existing vertices in self graph
|
1141
|
+
existing_vertex_ids = {vertex.id for vertex in self.vertices}
|
1142
|
+
# Vertex IDs in the other graph
|
1143
|
+
other_vertex_ids = set(other.vertex_map.keys())
|
1144
|
+
|
1145
|
+
# Find vertices that are in other but not in self (new vertices)
|
1146
|
+
new_vertex_ids = other_vertex_ids - existing_vertex_ids
|
1147
|
+
|
1148
|
+
# Find vertices that are in self but not in other (removed vertices)
|
1149
|
+
removed_vertex_ids = existing_vertex_ids - other_vertex_ids
|
1150
|
+
|
1151
|
+
# Remove vertices that are not in the other graph
|
1152
|
+
for vertex_id in removed_vertex_ids:
|
1153
|
+
with contextlib.suppress(ValueError):
|
1154
|
+
self.remove_vertex(vertex_id)
|
1155
|
+
|
1156
|
+
# The order here matters because adding the vertex is required
|
1157
|
+
# if any of them have edges that point to any of the new vertices
|
1158
|
+
# By adding them first, them adding the edges we ensure that the
|
1159
|
+
# edges have valid vertices to point to
|
1160
|
+
|
1161
|
+
# Add new vertices
|
1162
|
+
for vertex_id in new_vertex_ids:
|
1163
|
+
new_vertex = other.get_vertex(vertex_id)
|
1164
|
+
self._add_vertex(new_vertex)
|
1165
|
+
|
1166
|
+
# Now update the edges
|
1167
|
+
for vertex_id in new_vertex_ids:
|
1168
|
+
new_vertex = other.get_vertex(vertex_id)
|
1169
|
+
self._update_edges(new_vertex)
|
1170
|
+
# Graph is set at the end because the edges come from the graph
|
1171
|
+
# and the other graph is where the new edges and vertices come from
|
1172
|
+
new_vertex.graph = self
|
1173
|
+
|
1174
|
+
# Update existing vertices that have changed
|
1175
|
+
for vertex_id in existing_vertex_ids.intersection(other_vertex_ids):
|
1176
|
+
self_vertex = self.get_vertex(vertex_id)
|
1177
|
+
other_vertex = other.get_vertex(vertex_id)
|
1178
|
+
# If the vertices are not identical, update the vertex
|
1179
|
+
if not self.vertex_data_is_identical(self_vertex, other_vertex):
|
1180
|
+
self.update_vertex_from_another(self_vertex, other_vertex)
|
1181
|
+
|
1182
|
+
self.build_graph_maps()
|
1183
|
+
self.define_vertices_lists()
|
1184
|
+
self.increment_update_count()
|
1185
|
+
return self
|
1186
|
+
|
1187
|
+
def update_vertex_from_another(self, vertex: Vertex, other_vertex: Vertex) -> None:
|
1188
|
+
"""Updates a vertex from another vertex.
|
1189
|
+
|
1190
|
+
Args:
|
1191
|
+
vertex (Vertex): The vertex to be updated.
|
1192
|
+
other_vertex (Vertex): The vertex to update from.
|
1193
|
+
"""
|
1194
|
+
vertex.full_data = other_vertex.full_data
|
1195
|
+
vertex.parse_data()
|
1196
|
+
# Now we update the edges of the vertex
|
1197
|
+
self.update_edges_from_vertex(other_vertex)
|
1198
|
+
vertex.params = {}
|
1199
|
+
vertex.build_params()
|
1200
|
+
vertex.graph = self
|
1201
|
+
# If the vertex is frozen, we don't want
|
1202
|
+
# to reset the results nor the built attribute
|
1203
|
+
if not vertex.frozen:
|
1204
|
+
vertex.built = False
|
1205
|
+
vertex.result = None
|
1206
|
+
vertex.artifacts = {}
|
1207
|
+
vertex.set_top_level(self.top_level_vertices)
|
1208
|
+
self.reset_all_edges_of_vertex(vertex)
|
1209
|
+
|
1210
|
+
def reset_all_edges_of_vertex(self, vertex: Vertex) -> None:
|
1211
|
+
"""Resets all the edges of a vertex."""
|
1212
|
+
for edge in vertex.edges:
|
1213
|
+
for vid in [edge.source_id, edge.target_id]:
|
1214
|
+
if vid in self.vertex_map:
|
1215
|
+
vertex_ = self.vertex_map[vid]
|
1216
|
+
if not vertex_.frozen:
|
1217
|
+
vertex_.build_params()
|
1218
|
+
|
1219
|
+
def _add_vertex(self, vertex: Vertex) -> None:
|
1220
|
+
"""Adds a vertex to the graph."""
|
1221
|
+
self.vertices.append(vertex)
|
1222
|
+
self.vertex_map[vertex.id] = vertex
|
1223
|
+
|
1224
|
+
def add_vertex(self, vertex: Vertex) -> None:
|
1225
|
+
"""Adds a new vertex to the graph."""
|
1226
|
+
self._add_vertex(vertex)
|
1227
|
+
self._update_edges(vertex)
|
1228
|
+
|
1229
|
+
def _update_edges(self, vertex: Vertex) -> None:
|
1230
|
+
"""Updates the edges of a vertex."""
|
1231
|
+
# Vertex has edges, so we need to update the edges
|
1232
|
+
for edge in vertex.edges:
|
1233
|
+
if edge not in self.edges and edge.source_id in self.vertex_map and edge.target_id in self.vertex_map:
|
1234
|
+
self.edges.append(edge)
|
1235
|
+
|
1236
|
+
def _build_graph(self) -> None:
|
1237
|
+
"""Builds the graph from the vertices and edges."""
|
1238
|
+
self.vertices = self._build_vertices()
|
1239
|
+
self.vertex_map = {vertex.id: vertex for vertex in self.vertices}
|
1240
|
+
self.edges = self._build_edges()
|
1241
|
+
|
1242
|
+
# This is a hack to make sure that the LLM vertex is sent to
|
1243
|
+
# the toolkit vertex
|
1244
|
+
self._build_vertex_params()
|
1245
|
+
self._instantiate_components_in_vertices()
|
1246
|
+
self._set_cache_to_vertices_in_cycle()
|
1247
|
+
self._set_cache_if_listen_notify_components()
|
1248
|
+
for vertex in self.vertices:
|
1249
|
+
if vertex.id in self.cycle_vertices:
|
1250
|
+
self.run_manager.add_to_cycle_vertices(vertex.id)
|
1251
|
+
|
1252
|
+
def _get_edges_as_list_of_tuples(self) -> list[tuple[str, str]]:
|
1253
|
+
"""Returns the edges of the graph as a list of tuples.
|
1254
|
+
|
1255
|
+
Each tuple contains the source and target handle IDs from the edge data.
|
1256
|
+
|
1257
|
+
Returns:
|
1258
|
+
list[tuple[str, str]]: List of (source_id, target_id) tuples representing graph edges.
|
1259
|
+
"""
|
1260
|
+
return [(e["data"]["sourceHandle"]["id"], e["data"]["targetHandle"]["id"]) for e in self._edges]
|
1261
|
+
|
1262
|
+
def _set_cache_if_listen_notify_components(self) -> None:
|
1263
|
+
"""Disables caching for all vertices if Listen/Notify components are present.
|
1264
|
+
|
1265
|
+
If the graph contains any Listen or Notify components, caching is disabled for all vertices
|
1266
|
+
by setting cache=False on their outputs. This ensures proper handling of real-time
|
1267
|
+
communication between components.
|
1268
|
+
"""
|
1269
|
+
has_listen_or_notify_component = any(
|
1270
|
+
vertex.id.split("-")[0] in {"Listen", "Notify"} for vertex in self.vertices
|
1271
|
+
)
|
1272
|
+
if has_listen_or_notify_component:
|
1273
|
+
for vertex in self.vertices:
|
1274
|
+
vertex.apply_on_outputs(lambda output_object: setattr(output_object, "cache", False))
|
1275
|
+
|
1276
|
+
def _set_cache_to_vertices_in_cycle(self) -> None:
|
1277
|
+
"""Sets the cache to the vertices in cycle."""
|
1278
|
+
edges = self._get_edges_as_list_of_tuples()
|
1279
|
+
cycle_vertices = set(find_cycle_vertices(edges))
|
1280
|
+
for vertex in self.vertices:
|
1281
|
+
if vertex.id in cycle_vertices:
|
1282
|
+
vertex.apply_on_outputs(lambda output_object: setattr(output_object, "cache", False))
|
1283
|
+
|
1284
|
+
def _instantiate_components_in_vertices(self) -> None:
|
1285
|
+
"""Instantiates the components in the vertices."""
|
1286
|
+
for vertex in self.vertices:
|
1287
|
+
vertex.instantiate_component(self.user_id)
|
1288
|
+
|
1289
|
+
def remove_vertex(self, vertex_id: str) -> None:
|
1290
|
+
"""Removes a vertex from the graph."""
|
1291
|
+
vertex = self.get_vertex(vertex_id)
|
1292
|
+
if vertex is None:
|
1293
|
+
return
|
1294
|
+
self.vertices.remove(vertex)
|
1295
|
+
self.vertex_map.pop(vertex_id)
|
1296
|
+
self.edges = [edge for edge in self.edges if vertex_id not in {edge.source_id, edge.target_id}]
|
1297
|
+
|
1298
|
+
def _build_vertex_params(self) -> None:
|
1299
|
+
"""Identifies and handles the LLM vertex within the graph."""
|
1300
|
+
for vertex in self.vertices:
|
1301
|
+
vertex.build_params()
|
1302
|
+
|
1303
|
+
def _validate_vertex(self, vertex: Vertex) -> bool:
|
1304
|
+
"""Validates a vertex."""
|
1305
|
+
# All vertices that do not have edges are invalid
|
1306
|
+
return len(self.get_vertex_edges(vertex.id)) > 0
|
1307
|
+
|
1308
|
+
def get_vertex(self, vertex_id: str) -> Vertex:
|
1309
|
+
"""Returns a vertex by id."""
|
1310
|
+
try:
|
1311
|
+
return self.vertex_map[vertex_id]
|
1312
|
+
except KeyError as e:
|
1313
|
+
msg = f"Vertex {vertex_id} not found"
|
1314
|
+
raise ValueError(msg) from e
|
1315
|
+
|
1316
|
+
def get_root_of_group_node(self, vertex_id: str) -> Vertex:
|
1317
|
+
"""Returns the root of a group node."""
|
1318
|
+
if vertex_id in self.top_level_vertices:
|
1319
|
+
# Get all vertices with vertex_id as .parent_node_id
|
1320
|
+
# then get the one at the top
|
1321
|
+
vertices = [vertex for vertex in self.vertices if vertex.parent_node_id == vertex_id]
|
1322
|
+
# Now go through successors of the vertices
|
1323
|
+
# and get the one that none of its successors is in vertices
|
1324
|
+
for vertex in vertices:
|
1325
|
+
successors = self.get_all_successors(vertex, recursive=False)
|
1326
|
+
if not any(successor in vertices for successor in successors):
|
1327
|
+
return vertex
|
1328
|
+
msg = f"Vertex {vertex_id} is not a top level vertex or no root vertex found"
|
1329
|
+
raise ValueError(msg)
|
1330
|
+
|
1331
|
+
def get_next_in_queue(self):
|
1332
|
+
if not self._run_queue:
|
1333
|
+
return None
|
1334
|
+
return self._run_queue.popleft()
|
1335
|
+
|
1336
|
+
def extend_run_queue(self, vertices: list[str]) -> None:
|
1337
|
+
self._run_queue.extend(vertices)
|
1338
|
+
|
1339
|
+
async def astep(
|
1340
|
+
self,
|
1341
|
+
inputs: InputValueRequest | None = None,
|
1342
|
+
files: list[str] | None = None,
|
1343
|
+
user_id: str | None = None,
|
1344
|
+
event_manager: EventManager | None = None,
|
1345
|
+
):
|
1346
|
+
if not self._prepared:
|
1347
|
+
msg = "Graph not prepared. Call prepare() first."
|
1348
|
+
raise ValueError(msg)
|
1349
|
+
if not self._run_queue:
|
1350
|
+
self._end_all_traces_async()
|
1351
|
+
return Finish()
|
1352
|
+
vertex_id = self.get_next_in_queue()
|
1353
|
+
if not vertex_id:
|
1354
|
+
msg = "No vertex to run"
|
1355
|
+
raise ValueError(msg)
|
1356
|
+
chat_service = get_chat_service()
|
1357
|
+
|
1358
|
+
# Provide fallback cache functions if chat service is unavailable
|
1359
|
+
if chat_service is not None:
|
1360
|
+
get_cache_func = chat_service.get_cache
|
1361
|
+
set_cache_func = chat_service.set_cache
|
1362
|
+
else:
|
1363
|
+
# Fallback no-op cache functions for tests or when service unavailable
|
1364
|
+
async def get_cache_func(*args, **kwargs): # noqa: ARG001
|
1365
|
+
return None
|
1366
|
+
|
1367
|
+
async def set_cache_func(*args, **kwargs) -> bool: # noqa: ARG001
|
1368
|
+
return True
|
1369
|
+
|
1370
|
+
vertex_build_result = await self.build_vertex(
|
1371
|
+
vertex_id=vertex_id,
|
1372
|
+
user_id=user_id,
|
1373
|
+
inputs_dict=inputs.model_dump() if inputs and hasattr(inputs, "model_dump") else {},
|
1374
|
+
files=files,
|
1375
|
+
get_cache=get_cache_func,
|
1376
|
+
set_cache=set_cache_func,
|
1377
|
+
event_manager=event_manager,
|
1378
|
+
)
|
1379
|
+
|
1380
|
+
next_runnable_vertices = await self.get_next_runnable_vertices(
|
1381
|
+
self.lock, vertex=vertex_build_result.vertex, cache=False
|
1382
|
+
)
|
1383
|
+
if self.stop_vertex and self.stop_vertex in next_runnable_vertices:
|
1384
|
+
next_runnable_vertices = [self.stop_vertex]
|
1385
|
+
self.extend_run_queue(next_runnable_vertices)
|
1386
|
+
self.reset_inactivated_vertices()
|
1387
|
+
self.reset_activated_vertices()
|
1388
|
+
|
1389
|
+
if chat_service is not None:
|
1390
|
+
await chat_service.set_cache(str(self.flow_id or self._run_id), self)
|
1391
|
+
self._record_snapshot(vertex_id)
|
1392
|
+
return vertex_build_result
|
1393
|
+
|
1394
|
+
def get_snapshot(self):
|
1395
|
+
return copy.deepcopy(
|
1396
|
+
{
|
1397
|
+
"run_manager": self.run_manager.to_dict(),
|
1398
|
+
"run_queue": self._run_queue,
|
1399
|
+
"vertices_layers": self.vertices_layers,
|
1400
|
+
"first_layer": self.first_layer,
|
1401
|
+
"inactive_vertices": self.inactive_vertices,
|
1402
|
+
"activated_vertices": self.activated_vertices,
|
1403
|
+
}
|
1404
|
+
)
|
1405
|
+
|
1406
|
+
def _record_snapshot(self, vertex_id: str | None = None) -> None:
|
1407
|
+
self._snapshots.append(self.get_snapshot())
|
1408
|
+
if vertex_id:
|
1409
|
+
self._call_order.append(vertex_id)
|
1410
|
+
|
1411
|
+
def step(
|
1412
|
+
self,
|
1413
|
+
inputs: InputValueRequest | None = None,
|
1414
|
+
files: list[str] | None = None,
|
1415
|
+
user_id: str | None = None,
|
1416
|
+
):
|
1417
|
+
"""Runs the next vertex in the graph.
|
1418
|
+
|
1419
|
+
Note:
|
1420
|
+
This function is a synchronous wrapper around `astep`.
|
1421
|
+
It creates an event loop if one does not exist.
|
1422
|
+
|
1423
|
+
Args:
|
1424
|
+
inputs: The inputs for the vertex. Defaults to None.
|
1425
|
+
files: The files for the vertex. Defaults to None.
|
1426
|
+
user_id: The user ID. Defaults to None.
|
1427
|
+
"""
|
1428
|
+
return run_until_complete(self.astep(inputs, files, user_id))
|
1429
|
+
|
1430
|
+
async def build_vertex(
|
1431
|
+
self,
|
1432
|
+
vertex_id: str,
|
1433
|
+
*,
|
1434
|
+
get_cache: GetCache | None = None,
|
1435
|
+
set_cache: SetCache | None = None,
|
1436
|
+
inputs_dict: dict[str, str] | None = None,
|
1437
|
+
files: list[str] | None = None,
|
1438
|
+
user_id: str | None = None,
|
1439
|
+
fallback_to_env_vars: bool = False,
|
1440
|
+
event_manager: EventManager | None = None,
|
1441
|
+
) -> VertexBuildResult:
|
1442
|
+
"""Builds a vertex in the graph.
|
1443
|
+
|
1444
|
+
Args:
|
1445
|
+
vertex_id (str): The ID of the vertex to build.
|
1446
|
+
get_cache (GetCache): A coroutine to get the cache.
|
1447
|
+
set_cache (SetCache): A coroutine to set the cache.
|
1448
|
+
inputs_dict (Optional[Dict[str, str]]): Optional dictionary of inputs for the vertex. Defaults to None.
|
1449
|
+
files: (Optional[List[str]]): Optional list of files. Defaults to None.
|
1450
|
+
user_id (Optional[str]): Optional user ID. Defaults to None.
|
1451
|
+
fallback_to_env_vars (bool): Whether to fallback to environment variables. Defaults to False.
|
1452
|
+
event_manager (Optional[EventManager]): Optional event manager. Defaults to None.
|
1453
|
+
|
1454
|
+
Returns:
|
1455
|
+
Tuple: A tuple containing the next runnable vertices, top level vertices, result dictionary,
|
1456
|
+
parameters, validity flag, artifacts, and the built vertex.
|
1457
|
+
|
1458
|
+
Raises:
|
1459
|
+
ValueError: If no result is found for the vertex.
|
1460
|
+
"""
|
1461
|
+
vertex = self.get_vertex(vertex_id)
|
1462
|
+
self.run_manager.add_to_vertices_being_run(vertex_id)
|
1463
|
+
try:
|
1464
|
+
params = ""
|
1465
|
+
should_build = False
|
1466
|
+
if not vertex.frozen:
|
1467
|
+
should_build = True
|
1468
|
+
else:
|
1469
|
+
# Check the cache for the vertex
|
1470
|
+
if get_cache is not None:
|
1471
|
+
cached_result = await get_cache(key=vertex.id)
|
1472
|
+
else:
|
1473
|
+
cached_result = CacheMiss()
|
1474
|
+
if isinstance(cached_result, CacheMiss):
|
1475
|
+
should_build = True
|
1476
|
+
else:
|
1477
|
+
try:
|
1478
|
+
cached_vertex_dict = cached_result["result"]
|
1479
|
+
# Now set update the vertex with the cached vertex
|
1480
|
+
vertex.built = cached_vertex_dict["built"]
|
1481
|
+
vertex.artifacts = cached_vertex_dict["artifacts"]
|
1482
|
+
vertex.built_object = cached_vertex_dict["built_object"]
|
1483
|
+
vertex.built_result = cached_vertex_dict["built_result"]
|
1484
|
+
vertex.full_data = cached_vertex_dict["full_data"]
|
1485
|
+
vertex.results = cached_vertex_dict["results"]
|
1486
|
+
try:
|
1487
|
+
vertex.finalize_build()
|
1488
|
+
|
1489
|
+
if vertex.result is not None:
|
1490
|
+
vertex.result.used_frozen_result = True
|
1491
|
+
except Exception: # noqa: BLE001
|
1492
|
+
logger.debug("Error finalizing build", exc_info=True)
|
1493
|
+
should_build = True
|
1494
|
+
except KeyError:
|
1495
|
+
should_build = True
|
1496
|
+
|
1497
|
+
if should_build:
|
1498
|
+
await vertex.build(
|
1499
|
+
user_id=user_id,
|
1500
|
+
inputs=inputs_dict,
|
1501
|
+
fallback_to_env_vars=fallback_to_env_vars,
|
1502
|
+
files=files,
|
1503
|
+
event_manager=event_manager,
|
1504
|
+
)
|
1505
|
+
if set_cache is not None:
|
1506
|
+
vertex_dict = {
|
1507
|
+
"built": vertex.built,
|
1508
|
+
"results": vertex.results,
|
1509
|
+
"artifacts": vertex.artifacts,
|
1510
|
+
"built_object": vertex.built_object,
|
1511
|
+
"built_result": vertex.built_result,
|
1512
|
+
"full_data": vertex.full_data,
|
1513
|
+
}
|
1514
|
+
|
1515
|
+
await set_cache(key=vertex.id, data=vertex_dict)
|
1516
|
+
|
1517
|
+
except Exception as exc:
|
1518
|
+
if not isinstance(exc, ComponentBuildError):
|
1519
|
+
await logger.aexception("Error building Component")
|
1520
|
+
raise
|
1521
|
+
|
1522
|
+
if vertex.result is not None:
|
1523
|
+
params = f"{vertex.built_object_repr()}{params}"
|
1524
|
+
valid = True
|
1525
|
+
result_dict = vertex.result
|
1526
|
+
artifacts = vertex.artifacts
|
1527
|
+
else:
|
1528
|
+
msg = f"Error building Component: no result found for vertex {vertex_id}"
|
1529
|
+
raise ValueError(msg)
|
1530
|
+
|
1531
|
+
return VertexBuildResult(
|
1532
|
+
result_dict=result_dict, params=params, valid=valid, artifacts=artifacts, vertex=vertex
|
1533
|
+
)
|
1534
|
+
|
1535
|
+
def get_vertex_edges(
|
1536
|
+
self,
|
1537
|
+
vertex_id: str,
|
1538
|
+
*,
|
1539
|
+
is_target: bool | None = None,
|
1540
|
+
is_source: bool | None = None,
|
1541
|
+
) -> list[CycleEdge]:
|
1542
|
+
"""Returns a list of edges for a given vertex."""
|
1543
|
+
# The idea here is to return the edges that have the vertex_id as source or target
|
1544
|
+
# or both
|
1545
|
+
return [
|
1546
|
+
edge
|
1547
|
+
for edge in self.edges
|
1548
|
+
if (edge.source_id == vertex_id and is_source is not False)
|
1549
|
+
or (edge.target_id == vertex_id and is_target is not False)
|
1550
|
+
]
|
1551
|
+
|
1552
|
+
def get_vertices_with_target(self, vertex_id: str) -> list[Vertex]:
|
1553
|
+
"""Returns the vertices connected to a vertex."""
|
1554
|
+
vertices: list[Vertex] = []
|
1555
|
+
for edge in self.edges:
|
1556
|
+
if edge.target_id == vertex_id:
|
1557
|
+
vertex = self.get_vertex(edge.source_id)
|
1558
|
+
if vertex is None:
|
1559
|
+
continue
|
1560
|
+
vertices.append(vertex)
|
1561
|
+
return vertices
|
1562
|
+
|
1563
|
+
async def process(
|
1564
|
+
self,
|
1565
|
+
*,
|
1566
|
+
fallback_to_env_vars: bool,
|
1567
|
+
start_component_id: str | None = None,
|
1568
|
+
event_manager: EventManager | None = None,
|
1569
|
+
) -> Graph:
|
1570
|
+
"""Processes the graph with vertices in each layer run in parallel."""
|
1571
|
+
has_webhook_component = "webhook" in start_component_id.lower() if start_component_id else False
|
1572
|
+
first_layer = self.sort_vertices(start_component_id=start_component_id)
|
1573
|
+
vertex_task_run_count: dict[str, int] = {}
|
1574
|
+
to_process = deque(first_layer)
|
1575
|
+
layer_index = 0
|
1576
|
+
chat_service = get_chat_service()
|
1577
|
+
|
1578
|
+
# Provide fallback cache functions if chat service is unavailable
|
1579
|
+
if chat_service is not None:
|
1580
|
+
get_cache_func = chat_service.get_cache
|
1581
|
+
set_cache_func = chat_service.set_cache
|
1582
|
+
else:
|
1583
|
+
# Fallback no-op cache functions for tests or when service unavailable
|
1584
|
+
async def get_cache_func(*args, **kwargs): # noqa: ARG001
|
1585
|
+
return None
|
1586
|
+
|
1587
|
+
async def set_cache_func(*args, **kwargs):
|
1588
|
+
pass
|
1589
|
+
|
1590
|
+
await self.initialize_run()
|
1591
|
+
lock = asyncio.Lock()
|
1592
|
+
while to_process:
|
1593
|
+
current_batch = list(to_process) # Copy current deque items to a list
|
1594
|
+
to_process.clear() # Clear the deque for new items
|
1595
|
+
tasks = []
|
1596
|
+
for vertex_id in current_batch:
|
1597
|
+
vertex = self.get_vertex(vertex_id)
|
1598
|
+
task = asyncio.create_task(
|
1599
|
+
self.build_vertex(
|
1600
|
+
vertex_id=vertex_id,
|
1601
|
+
user_id=self.user_id,
|
1602
|
+
inputs_dict={},
|
1603
|
+
fallback_to_env_vars=fallback_to_env_vars,
|
1604
|
+
get_cache=get_cache_func,
|
1605
|
+
set_cache=set_cache_func,
|
1606
|
+
event_manager=event_manager,
|
1607
|
+
),
|
1608
|
+
name=f"{vertex.id} Run {vertex_task_run_count.get(vertex_id, 0)}",
|
1609
|
+
)
|
1610
|
+
tasks.append(task)
|
1611
|
+
vertex_task_run_count[vertex_id] = vertex_task_run_count.get(vertex_id, 0) + 1
|
1612
|
+
|
1613
|
+
await logger.adebug(f"Running layer {layer_index} with {len(tasks)} tasks, {current_batch}")
|
1614
|
+
try:
|
1615
|
+
next_runnable_vertices = await self._execute_tasks(
|
1616
|
+
tasks, lock=lock, has_webhook_component=has_webhook_component
|
1617
|
+
)
|
1618
|
+
except Exception:
|
1619
|
+
await logger.aexception(f"Error executing tasks in layer {layer_index}")
|
1620
|
+
raise
|
1621
|
+
if not next_runnable_vertices:
|
1622
|
+
break
|
1623
|
+
to_process.extend(next_runnable_vertices)
|
1624
|
+
layer_index += 1
|
1625
|
+
|
1626
|
+
await logger.adebug("Graph processing complete")
|
1627
|
+
return self
|
1628
|
+
|
1629
|
+
def find_next_runnable_vertices(self, vertex_successors_ids: list[str]) -> list[str]:
|
1630
|
+
"""Determines the next set of runnable vertices from a list of successor vertex IDs.
|
1631
|
+
|
1632
|
+
For each successor, if it is not runnable, recursively finds its runnable
|
1633
|
+
predecessors; otherwise, includes the successor itself. Returns a sorted list of all such vertex IDs.
|
1634
|
+
"""
|
1635
|
+
next_runnable_vertices = set()
|
1636
|
+
for v_id in sorted(vertex_successors_ids):
|
1637
|
+
if not self.is_vertex_runnable(v_id):
|
1638
|
+
next_runnable_vertices.update(self.find_runnable_predecessors_for_successor(v_id))
|
1639
|
+
else:
|
1640
|
+
next_runnable_vertices.add(v_id)
|
1641
|
+
|
1642
|
+
return sorted(next_runnable_vertices)
|
1643
|
+
|
1644
|
+
async def get_next_runnable_vertices(self, lock: asyncio.Lock, vertex: Vertex, *, cache: bool = True) -> list[str]:
|
1645
|
+
"""Determines the next set of runnable vertex IDs after a vertex completes execution.
|
1646
|
+
|
1647
|
+
If the completed vertex is a state vertex, any recently activated state vertices are also included.
|
1648
|
+
Updates the run manager to reflect the new runnable state and optionally caches the updated graph state.
|
1649
|
+
|
1650
|
+
Args:
|
1651
|
+
lock: An asyncio lock for thread-safe updates.
|
1652
|
+
vertex: The vertex that has just finished execution.
|
1653
|
+
cache: If True, caches the updated graph state.
|
1654
|
+
|
1655
|
+
Returns:
|
1656
|
+
A list of vertex IDs that are ready to be executed next.
|
1657
|
+
"""
|
1658
|
+
v_id = vertex.id
|
1659
|
+
v_successors_ids = vertex.successors_ids
|
1660
|
+
self.run_manager.ran_at_least_once.add(v_id)
|
1661
|
+
async with lock:
|
1662
|
+
self.run_manager.remove_vertex_from_runnables(v_id)
|
1663
|
+
next_runnable_vertices = self.find_next_runnable_vertices(v_successors_ids)
|
1664
|
+
|
1665
|
+
for next_v_id in set(next_runnable_vertices): # Use set to avoid duplicates
|
1666
|
+
if next_v_id == v_id:
|
1667
|
+
next_runnable_vertices.remove(v_id)
|
1668
|
+
else:
|
1669
|
+
self.run_manager.add_to_vertices_being_run(next_v_id)
|
1670
|
+
if cache and self.flow_id is not None:
|
1671
|
+
set_cache_coro = partial(get_chat_service().set_cache, key=self.flow_id)
|
1672
|
+
await set_cache_coro(data=self, lock=lock)
|
1673
|
+
if vertex.is_state:
|
1674
|
+
next_runnable_vertices.extend(self.activated_vertices)
|
1675
|
+
return next_runnable_vertices
|
1676
|
+
|
1677
|
+
async def _log_vertex_build_from_exception(self, vertex_id: str, result: Exception) -> None:
|
1678
|
+
"""Logs detailed information about a vertex build exception.
|
1679
|
+
|
1680
|
+
Formats the exception message and stack trace, constructs an error output,
|
1681
|
+
and records the failure using the vertex build logging system.
|
1682
|
+
"""
|
1683
|
+
if isinstance(result, ComponentBuildError):
|
1684
|
+
params = result.message
|
1685
|
+
tb = result.formatted_traceback
|
1686
|
+
else:
|
1687
|
+
from lfx.utils.exceptions import format_exception_message
|
1688
|
+
|
1689
|
+
tb = traceback.format_exc()
|
1690
|
+
await logger.aexception("Error building Component")
|
1691
|
+
|
1692
|
+
params = format_exception_message(result)
|
1693
|
+
message = {"errorMessage": params, "stackTrace": tb}
|
1694
|
+
vertex = self.get_vertex(vertex_id)
|
1695
|
+
output_label = vertex.outputs[0]["name"] if vertex.outputs else "output"
|
1696
|
+
outputs = {output_label: OutputValue(message=message, type="error")}
|
1697
|
+
result_data_response = {
|
1698
|
+
"results": {},
|
1699
|
+
"outputs": outputs,
|
1700
|
+
"logs": {},
|
1701
|
+
"message": {},
|
1702
|
+
"artifacts": {},
|
1703
|
+
"timedelta": None,
|
1704
|
+
"duration": None,
|
1705
|
+
"used_frozen_result": False,
|
1706
|
+
}
|
1707
|
+
|
1708
|
+
await log_vertex_build(
|
1709
|
+
flow_id=self.flow_id or "",
|
1710
|
+
vertex_id=vertex_id or "errors",
|
1711
|
+
valid=False,
|
1712
|
+
params=params,
|
1713
|
+
data=result_data_response,
|
1714
|
+
artifacts={},
|
1715
|
+
)
|
1716
|
+
|
1717
|
+
async def _execute_tasks(
|
1718
|
+
self, tasks: list[asyncio.Task], lock: asyncio.Lock, *, has_webhook_component: bool = False
|
1719
|
+
) -> list[str]:
|
1720
|
+
"""Executes tasks in parallel, handling exceptions for each task.
|
1721
|
+
|
1722
|
+
Args:
|
1723
|
+
tasks: List of tasks to execute
|
1724
|
+
lock: Async lock for synchronization
|
1725
|
+
has_webhook_component: Whether the graph has a webhook component
|
1726
|
+
"""
|
1727
|
+
results = []
|
1728
|
+
completed_tasks = await asyncio.gather(*tasks, return_exceptions=True)
|
1729
|
+
vertices: list[Vertex] = []
|
1730
|
+
|
1731
|
+
for i, result in enumerate(completed_tasks):
|
1732
|
+
task_name = tasks[i].get_name()
|
1733
|
+
vertex_id = tasks[i].get_name().split(" ")[0]
|
1734
|
+
|
1735
|
+
if isinstance(result, Exception):
|
1736
|
+
await logger.aerror(f"Task {task_name} failed with exception: {result}")
|
1737
|
+
if has_webhook_component:
|
1738
|
+
await self._log_vertex_build_from_exception(vertex_id, result)
|
1739
|
+
|
1740
|
+
# Cancel all remaining tasks
|
1741
|
+
for t in tasks[i + 1 :]:
|
1742
|
+
t.cancel()
|
1743
|
+
raise result
|
1744
|
+
if isinstance(result, VertexBuildResult):
|
1745
|
+
if self.flow_id is not None:
|
1746
|
+
await log_vertex_build(
|
1747
|
+
flow_id=self.flow_id,
|
1748
|
+
vertex_id=result.vertex.id,
|
1749
|
+
valid=result.valid,
|
1750
|
+
params=result.params,
|
1751
|
+
data=result.result_dict,
|
1752
|
+
artifacts=result.artifacts,
|
1753
|
+
)
|
1754
|
+
|
1755
|
+
vertices.append(result.vertex)
|
1756
|
+
else:
|
1757
|
+
msg = f"Invalid result from task {task_name}: {result}"
|
1758
|
+
raise TypeError(msg)
|
1759
|
+
|
1760
|
+
for v in vertices:
|
1761
|
+
# set all executed vertices as non-runnable to not run them again.
|
1762
|
+
# they could be calculated as predecessor or successors of parallel vertices
|
1763
|
+
# This could usually happen with input vertices like ChatInput
|
1764
|
+
self.run_manager.remove_vertex_from_runnables(v.id)
|
1765
|
+
|
1766
|
+
await logger.adebug(f"Vertex {v.id}, result: {v.built_result}, object: {v.built_object}")
|
1767
|
+
|
1768
|
+
for v in vertices:
|
1769
|
+
next_runnable_vertices = await self.get_next_runnable_vertices(lock, vertex=v, cache=False)
|
1770
|
+
results.extend(next_runnable_vertices)
|
1771
|
+
return list(set(results))
|
1772
|
+
|
1773
|
+
def topological_sort(self) -> list[Vertex]:
|
1774
|
+
"""Performs a topological sort of the vertices in the graph.
|
1775
|
+
|
1776
|
+
Returns:
|
1777
|
+
List[Vertex]: A list of vertices in topological order.
|
1778
|
+
|
1779
|
+
Raises:
|
1780
|
+
ValueError: If the graph contains a cycle.
|
1781
|
+
"""
|
1782
|
+
# States: 0 = unvisited, 1 = visiting, 2 = visited
|
1783
|
+
state = dict.fromkeys(self.vertices, 0)
|
1784
|
+
sorted_vertices = []
|
1785
|
+
|
1786
|
+
def dfs(vertex) -> None:
|
1787
|
+
if state[vertex] == 1:
|
1788
|
+
# We have a cycle
|
1789
|
+
msg = "Graph contains a cycle, cannot perform topological sort"
|
1790
|
+
raise ValueError(msg)
|
1791
|
+
if state[vertex] == 0:
|
1792
|
+
state[vertex] = 1
|
1793
|
+
for edge in vertex.edges:
|
1794
|
+
if edge.source_id == vertex.id:
|
1795
|
+
dfs(self.get_vertex(edge.target_id))
|
1796
|
+
state[vertex] = 2
|
1797
|
+
sorted_vertices.append(vertex)
|
1798
|
+
|
1799
|
+
# Visit each vertex
|
1800
|
+
for vertex in self.vertices:
|
1801
|
+
if state[vertex] == 0:
|
1802
|
+
dfs(vertex)
|
1803
|
+
|
1804
|
+
return list(reversed(sorted_vertices))
|
1805
|
+
|
1806
|
+
def generator_build(self) -> Generator[Vertex, None, None]:
|
1807
|
+
"""Builds each vertex in the graph and yields it."""
|
1808
|
+
sorted_vertices = self.topological_sort()
|
1809
|
+
logger.debug("There are %s vertices in the graph", len(sorted_vertices))
|
1810
|
+
yield from sorted_vertices
|
1811
|
+
|
1812
|
+
def get_predecessors(self, vertex):
|
1813
|
+
"""Returns the predecessors of a vertex."""
|
1814
|
+
return [self.get_vertex(source_id) for source_id in self.predecessor_map.get(vertex.id, [])]
|
1815
|
+
|
1816
|
+
def get_all_successors(self, vertex: Vertex, *, recursive=True, flat=True, visited=None):
|
1817
|
+
"""Returns all successors of a given vertex, optionally recursively and as a flat or nested list.
|
1818
|
+
|
1819
|
+
Args:
|
1820
|
+
vertex: The vertex whose successors are to be retrieved.
|
1821
|
+
recursive: If True, retrieves successors recursively; otherwise, only immediate successors.
|
1822
|
+
flat: If True, returns a flat list of successors; if False, returns a nested list structure.
|
1823
|
+
visited: Internal set used to track visited vertices and prevent cycles.
|
1824
|
+
|
1825
|
+
Returns:
|
1826
|
+
A list of successor vertices, either flat or nested depending on the `flat` parameter.
|
1827
|
+
"""
|
1828
|
+
if visited is None:
|
1829
|
+
visited = set()
|
1830
|
+
|
1831
|
+
# Prevent revisiting vertices to avoid infinite loops in cyclic graphs
|
1832
|
+
if vertex in visited:
|
1833
|
+
return []
|
1834
|
+
|
1835
|
+
visited.add(vertex)
|
1836
|
+
|
1837
|
+
successors = vertex.successors
|
1838
|
+
if not successors:
|
1839
|
+
return []
|
1840
|
+
|
1841
|
+
successors_result = []
|
1842
|
+
|
1843
|
+
for successor in successors:
|
1844
|
+
if recursive:
|
1845
|
+
next_successors = self.get_all_successors(successor, recursive=recursive, flat=flat, visited=visited)
|
1846
|
+
if flat:
|
1847
|
+
successors_result.extend(next_successors)
|
1848
|
+
else:
|
1849
|
+
successors_result.append(next_successors)
|
1850
|
+
if flat:
|
1851
|
+
successors_result.append(successor)
|
1852
|
+
else:
|
1853
|
+
successors_result.append([successor])
|
1854
|
+
|
1855
|
+
if not flat and successors_result:
|
1856
|
+
return [successors, *successors_result]
|
1857
|
+
|
1858
|
+
return successors_result
|
1859
|
+
|
1860
|
+
def get_successors(self, vertex: Vertex) -> list[Vertex]:
|
1861
|
+
"""Returns the immediate successor vertices of the given vertex.
|
1862
|
+
|
1863
|
+
Args:
|
1864
|
+
vertex: The vertex whose successors are to be retrieved.
|
1865
|
+
|
1866
|
+
Returns:
|
1867
|
+
A list of vertices that are direct successors of the specified vertex.
|
1868
|
+
"""
|
1869
|
+
return [self.get_vertex(target_id) for target_id in self.successor_map.get(vertex.id, set())]
|
1870
|
+
|
1871
|
+
def get_all_predecessors(self, vertex: Vertex, *, recursive: bool = True) -> list[Vertex]:
|
1872
|
+
"""Retrieves all predecessor vertices of a given vertex.
|
1873
|
+
|
1874
|
+
If `recursive` is True, returns both direct and indirect predecessors by
|
1875
|
+
traversing the graph recursively. If False, returns only the immediate predecessors.
|
1876
|
+
"""
|
1877
|
+
_predecessors = self.predecessor_map.get(vertex.id, [])
|
1878
|
+
predecessors = [self.get_vertex(v_id) for v_id in _predecessors]
|
1879
|
+
if recursive:
|
1880
|
+
for predecessor in _predecessors:
|
1881
|
+
predecessors.extend(self.get_all_predecessors(self.get_vertex(predecessor), recursive=recursive))
|
1882
|
+
else:
|
1883
|
+
predecessors.extend([self.get_vertex(predecessor) for predecessor in _predecessors])
|
1884
|
+
return predecessors
|
1885
|
+
|
1886
|
+
def get_vertex_neighbors(self, vertex: Vertex) -> dict[Vertex, int]:
|
1887
|
+
"""Returns a dictionary mapping each direct neighbor of a vertex to the count of connecting edges.
|
1888
|
+
|
1889
|
+
A neighbor is any vertex directly connected to the input vertex, either as a source or target.
|
1890
|
+
The count reflects the number of edges between the input vertex and each neighbor.
|
1891
|
+
"""
|
1892
|
+
neighbors: dict[Vertex, int] = {}
|
1893
|
+
for edge in self.edges:
|
1894
|
+
if edge.source_id == vertex.id:
|
1895
|
+
neighbor = self.get_vertex(edge.target_id)
|
1896
|
+
if neighbor is None:
|
1897
|
+
continue
|
1898
|
+
if neighbor not in neighbors:
|
1899
|
+
neighbors[neighbor] = 0
|
1900
|
+
neighbors[neighbor] += 1
|
1901
|
+
elif edge.target_id == vertex.id:
|
1902
|
+
neighbor = self.get_vertex(edge.source_id)
|
1903
|
+
if neighbor is None:
|
1904
|
+
continue
|
1905
|
+
if neighbor not in neighbors:
|
1906
|
+
neighbors[neighbor] = 0
|
1907
|
+
neighbors[neighbor] += 1
|
1908
|
+
return neighbors
|
1909
|
+
|
1910
|
+
@property
|
1911
|
+
def cycles(self):
|
1912
|
+
if self._cycles is None:
|
1913
|
+
if self._start is None:
|
1914
|
+
self._cycles = []
|
1915
|
+
else:
|
1916
|
+
entry_vertex = self._start.get_id()
|
1917
|
+
edges = [(e["data"]["sourceHandle"]["id"], e["data"]["targetHandle"]["id"]) for e in self._edges]
|
1918
|
+
self._cycles = find_all_cycle_edges(entry_vertex, edges)
|
1919
|
+
return self._cycles
|
1920
|
+
|
1921
|
+
@property
|
1922
|
+
def cycle_vertices(self):
|
1923
|
+
if self._cycle_vertices is None:
|
1924
|
+
edges = self._get_edges_as_list_of_tuples()
|
1925
|
+
self._cycle_vertices = set(find_cycle_vertices(edges))
|
1926
|
+
return self._cycle_vertices
|
1927
|
+
|
1928
|
+
def _build_edges(self) -> list[CycleEdge]:
|
1929
|
+
"""Builds the edges of the graph."""
|
1930
|
+
# Edge takes two vertices as arguments, so we need to build the vertices first
|
1931
|
+
# and then build the edges
|
1932
|
+
# if we can't find a vertex, we raise an error
|
1933
|
+
edges: set[CycleEdge | Edge] = set()
|
1934
|
+
for edge in self._edges:
|
1935
|
+
new_edge = self.build_edge(edge)
|
1936
|
+
edges.add(new_edge)
|
1937
|
+
if self.vertices and not edges:
|
1938
|
+
logger.warning("Graph has vertices but no edges")
|
1939
|
+
return list(cast("Iterable[CycleEdge]", edges))
|
1940
|
+
|
1941
|
+
def build_edge(self, edge: EdgeData) -> CycleEdge | Edge:
|
1942
|
+
source = self.get_vertex(edge["source"])
|
1943
|
+
target = self.get_vertex(edge["target"])
|
1944
|
+
|
1945
|
+
if source is None:
|
1946
|
+
msg = f"Source vertex {edge['source']} not found"
|
1947
|
+
raise ValueError(msg)
|
1948
|
+
if target is None:
|
1949
|
+
msg = f"Target vertex {edge['target']} not found"
|
1950
|
+
raise ValueError(msg)
|
1951
|
+
if any(v in self.cycle_vertices for v in [source.id, target.id]):
|
1952
|
+
new_edge: CycleEdge | Edge = CycleEdge(source, target, edge)
|
1953
|
+
else:
|
1954
|
+
new_edge = Edge(source, target, edge)
|
1955
|
+
return new_edge
|
1956
|
+
|
1957
|
+
@staticmethod
|
1958
|
+
def _get_vertex_class(node_type: str, node_base_type: str, node_id: str) -> type[Vertex]:
|
1959
|
+
"""Returns the node class based on the node type."""
|
1960
|
+
# First we check for the node_base_type
|
1961
|
+
node_name = node_id.split("-")[0]
|
1962
|
+
if node_name in InterfaceComponentTypes or node_type in InterfaceComponentTypes:
|
1963
|
+
return InterfaceVertex
|
1964
|
+
if node_name in {"SharedState", "Notify", "Listen"}:
|
1965
|
+
return StateVertex
|
1966
|
+
if node_base_type in lazy_load_vertex_dict.vertex_type_map:
|
1967
|
+
return lazy_load_vertex_dict.vertex_type_map[node_base_type]
|
1968
|
+
if node_name in lazy_load_vertex_dict.vertex_type_map:
|
1969
|
+
return lazy_load_vertex_dict.vertex_type_map[node_name]
|
1970
|
+
|
1971
|
+
if node_type in lazy_load_vertex_dict.vertex_type_map:
|
1972
|
+
return lazy_load_vertex_dict.vertex_type_map[node_type]
|
1973
|
+
return Vertex
|
1974
|
+
|
1975
|
+
def _build_vertices(self) -> list[Vertex]:
|
1976
|
+
"""Builds the vertices of the graph."""
|
1977
|
+
vertices: list[Vertex] = []
|
1978
|
+
for frontend_data in self._vertices:
|
1979
|
+
if frontend_data.get("type") == NodeTypeEnum.NoteNode:
|
1980
|
+
continue
|
1981
|
+
try:
|
1982
|
+
vertex_instance = self.get_vertex(frontend_data["id"])
|
1983
|
+
except ValueError:
|
1984
|
+
vertex_instance = self._create_vertex(frontend_data)
|
1985
|
+
vertices.append(vertex_instance)
|
1986
|
+
|
1987
|
+
return vertices
|
1988
|
+
|
1989
|
+
def _create_vertex(self, frontend_data: NodeData):
|
1990
|
+
vertex_data = frontend_data["data"]
|
1991
|
+
vertex_type: str = vertex_data["type"]
|
1992
|
+
vertex_base_type: str = vertex_data["node"]["template"]["_type"]
|
1993
|
+
if "id" not in vertex_data:
|
1994
|
+
msg = f"Vertex data for {vertex_data['display_name']} does not contain an id"
|
1995
|
+
raise ValueError(msg)
|
1996
|
+
|
1997
|
+
vertex_class = self._get_vertex_class(vertex_type, vertex_base_type, vertex_data["id"])
|
1998
|
+
|
1999
|
+
vertex_instance = vertex_class(frontend_data, graph=self)
|
2000
|
+
vertex_instance.set_top_level(self.top_level_vertices)
|
2001
|
+
return vertex_instance
|
2002
|
+
|
2003
|
+
def prepare(self, stop_component_id: str | None = None, start_component_id: str | None = None):
|
2004
|
+
self.initialize()
|
2005
|
+
if stop_component_id and start_component_id:
|
2006
|
+
msg = "You can only provide one of stop_component_id or start_component_id"
|
2007
|
+
raise ValueError(msg)
|
2008
|
+
|
2009
|
+
if stop_component_id or start_component_id:
|
2010
|
+
try:
|
2011
|
+
first_layer = self.sort_vertices(stop_component_id, start_component_id)
|
2012
|
+
except Exception: # noqa: BLE001
|
2013
|
+
logger.exception("Error sorting vertices")
|
2014
|
+
first_layer = self.sort_vertices()
|
2015
|
+
else:
|
2016
|
+
first_layer = self.sort_vertices()
|
2017
|
+
|
2018
|
+
for vertex_id in first_layer:
|
2019
|
+
self.run_manager.add_to_vertices_being_run(vertex_id)
|
2020
|
+
if vertex_id in self.cycle_vertices:
|
2021
|
+
self.run_manager.add_to_cycle_vertices(vertex_id)
|
2022
|
+
self._first_layer = sorted(first_layer)
|
2023
|
+
self._run_queue = deque(self._first_layer)
|
2024
|
+
self._prepared = True
|
2025
|
+
self._record_snapshot()
|
2026
|
+
return self
|
2027
|
+
|
2028
|
+
@staticmethod
|
2029
|
+
def get_children_by_vertex_type(vertex: Vertex, vertex_type: str) -> list[Vertex]:
|
2030
|
+
"""Returns the children of a vertex based on the vertex type."""
|
2031
|
+
children = []
|
2032
|
+
vertex_types = [vertex.data["type"]]
|
2033
|
+
if "node" in vertex.data:
|
2034
|
+
vertex_types += vertex.data["node"]["base_classes"]
|
2035
|
+
if vertex_type in vertex_types:
|
2036
|
+
children.append(vertex)
|
2037
|
+
return children
|
2038
|
+
|
2039
|
+
def __repr__(self) -> str:
|
2040
|
+
vertex_ids = [vertex.id for vertex in self.vertices]
|
2041
|
+
edges_repr = "\n".join([f" {edge.source_id} --> {edge.target_id}" for edge in self.edges])
|
2042
|
+
|
2043
|
+
return (
|
2044
|
+
f"Graph Representation:\n"
|
2045
|
+
f"----------------------\n"
|
2046
|
+
f"Vertices ({len(vertex_ids)}):\n"
|
2047
|
+
f" {', '.join(map(str, vertex_ids))}\n\n"
|
2048
|
+
f"Edges ({len(self.edges)}):\n"
|
2049
|
+
f"{edges_repr}"
|
2050
|
+
)
|
2051
|
+
|
2052
|
+
def __hash__(self) -> int:
|
2053
|
+
"""Return hash of the graph based on its string representation."""
|
2054
|
+
return hash(self.__repr__())
|
2055
|
+
|
2056
|
+
def get_vertex_predecessors_ids(self, vertex_id: str) -> list[str]:
|
2057
|
+
"""Get the predecessor IDs of a vertex."""
|
2058
|
+
return [v.id for v in self.get_predecessors(self.get_vertex(vertex_id))]
|
2059
|
+
|
2060
|
+
def get_vertex_successors_ids(self, vertex_id: str) -> list[str]:
|
2061
|
+
"""Get the successor IDs of a vertex."""
|
2062
|
+
return [v.id for v in self.get_vertex(vertex_id).successors]
|
2063
|
+
|
2064
|
+
def get_vertex_input_status(self, vertex_id: str) -> bool:
|
2065
|
+
"""Check if a vertex is an input vertex."""
|
2066
|
+
return self.get_vertex(vertex_id).is_input
|
2067
|
+
|
2068
|
+
def get_parent_map(self) -> dict[str, str | None]:
|
2069
|
+
"""Get the parent node map for all vertices."""
|
2070
|
+
return {vertex.id: vertex.parent_node_id for vertex in self.vertices}
|
2071
|
+
|
2072
|
+
def get_vertex_ids(self) -> list[str]:
|
2073
|
+
"""Get all vertex IDs in the graph."""
|
2074
|
+
return [vertex.id for vertex in self.vertices]
|
2075
|
+
|
2076
|
+
def sort_vertices(
|
2077
|
+
self,
|
2078
|
+
stop_component_id: str | None = None,
|
2079
|
+
start_component_id: str | None = None,
|
2080
|
+
) -> list[str]:
|
2081
|
+
"""Sorts the vertices in the graph."""
|
2082
|
+
self.mark_all_vertices("ACTIVE")
|
2083
|
+
|
2084
|
+
first_layer, remaining_layers = get_sorted_vertices(
|
2085
|
+
vertices_ids=self.get_vertex_ids(),
|
2086
|
+
cycle_vertices=self.cycle_vertices,
|
2087
|
+
stop_component_id=stop_component_id,
|
2088
|
+
start_component_id=start_component_id,
|
2089
|
+
graph_dict=self.__to_dict(),
|
2090
|
+
in_degree_map=self.in_degree_map,
|
2091
|
+
successor_map=self.successor_map,
|
2092
|
+
predecessor_map=self.predecessor_map,
|
2093
|
+
is_input_vertex=self.get_vertex_input_status,
|
2094
|
+
get_vertex_predecessors=self.get_vertex_predecessors_ids,
|
2095
|
+
get_vertex_successors=self.get_vertex_successors_ids,
|
2096
|
+
is_cyclic=self.is_cyclic,
|
2097
|
+
)
|
2098
|
+
|
2099
|
+
self.increment_run_count()
|
2100
|
+
self._sorted_vertices_layers = [first_layer, *remaining_layers]
|
2101
|
+
self.vertices_layers = remaining_layers
|
2102
|
+
self.vertices_to_run = set(chain.from_iterable([first_layer, *remaining_layers]))
|
2103
|
+
self.build_run_map()
|
2104
|
+
self._first_layer = first_layer
|
2105
|
+
return first_layer
|
2106
|
+
|
2107
|
+
@staticmethod
|
2108
|
+
def sort_interface_components_first(vertices_layers: list[list[str]]) -> list[list[str]]:
|
2109
|
+
"""Sorts the vertices in the graph so that vertices containing ChatInput or ChatOutput come first."""
|
2110
|
+
|
2111
|
+
def contains_interface_component(vertex):
|
2112
|
+
return any(component.value in vertex for component in InterfaceComponentTypes)
|
2113
|
+
|
2114
|
+
# Sort each inner list so that vertices containing ChatInput or ChatOutput come first
|
2115
|
+
return [
|
2116
|
+
sorted(
|
2117
|
+
inner_list,
|
2118
|
+
key=lambda vertex: not contains_interface_component(vertex),
|
2119
|
+
)
|
2120
|
+
for inner_list in vertices_layers
|
2121
|
+
]
|
2122
|
+
|
2123
|
+
def sort_by_avg_build_time(self, vertices_layers: list[list[str]]) -> list[list[str]]:
|
2124
|
+
"""Sorts the vertices in the graph so that vertices with the lowest average build time come first."""
|
2125
|
+
|
2126
|
+
def sort_layer_by_avg_build_time(vertices_ids: list[str]) -> list[str]:
|
2127
|
+
"""Sorts the vertices in the graph so that vertices with the lowest average build time come first."""
|
2128
|
+
if len(vertices_ids) == 1:
|
2129
|
+
return vertices_ids
|
2130
|
+
vertices_ids.sort(key=lambda vertex_id: self.get_vertex(vertex_id).avg_build_time)
|
2131
|
+
|
2132
|
+
return vertices_ids
|
2133
|
+
|
2134
|
+
return [sort_layer_by_avg_build_time(layer) for layer in vertices_layers]
|
2135
|
+
|
2136
|
+
def is_vertex_runnable(self, vertex_id: str) -> bool:
|
2137
|
+
"""Returns whether a vertex is runnable."""
|
2138
|
+
is_active = self.get_vertex(vertex_id).is_active()
|
2139
|
+
is_loop = self.get_vertex(vertex_id).is_loop
|
2140
|
+
return self.run_manager.is_vertex_runnable(vertex_id, is_active=is_active, is_loop=is_loop)
|
2141
|
+
|
2142
|
+
def build_run_map(self) -> None:
|
2143
|
+
"""Builds the run map for the graph.
|
2144
|
+
|
2145
|
+
This method is responsible for building the run map for the graph,
|
2146
|
+
which maps each node in the graph to its corresponding run function.
|
2147
|
+
"""
|
2148
|
+
self.run_manager.build_run_map(predecessor_map=self.predecessor_map, vertices_to_run=self.vertices_to_run)
|
2149
|
+
|
2150
|
+
def find_runnable_predecessors_for_successors(self, vertex_id: str) -> list[str]:
|
2151
|
+
"""For each successor of the current vertex, find runnable predecessors if any.
|
2152
|
+
|
2153
|
+
This checks the direct predecessors of each successor to identify any that are
|
2154
|
+
immediately runnable, expanding the search to ensure progress can be made.
|
2155
|
+
"""
|
2156
|
+
runnable_vertices = []
|
2157
|
+
for successor_id in self.run_manager.run_map.get(vertex_id, []):
|
2158
|
+
runnable_vertices.extend(self.find_runnable_predecessors_for_successor(successor_id))
|
2159
|
+
|
2160
|
+
return sorted(runnable_vertices)
|
2161
|
+
|
2162
|
+
def find_runnable_predecessors_for_successor(self, vertex_id: str) -> list[str]:
|
2163
|
+
runnable_vertices = []
|
2164
|
+
visited = set()
|
2165
|
+
|
2166
|
+
def find_runnable_predecessors(predecessor_id: str) -> None:
|
2167
|
+
if predecessor_id in visited:
|
2168
|
+
return
|
2169
|
+
visited.add(predecessor_id)
|
2170
|
+
predecessor_vertex = self.get_vertex(predecessor_id)
|
2171
|
+
is_active = predecessor_vertex.is_active()
|
2172
|
+
is_loop = predecessor_vertex.is_loop
|
2173
|
+
if self.run_manager.is_vertex_runnable(predecessor_id, is_active=is_active, is_loop=is_loop):
|
2174
|
+
runnable_vertices.append(predecessor_id)
|
2175
|
+
else:
|
2176
|
+
for pred_pred_id in self.run_manager.run_predecessors.get(predecessor_id, []):
|
2177
|
+
find_runnable_predecessors(pred_pred_id)
|
2178
|
+
|
2179
|
+
for predecessor_id in self.run_manager.run_predecessors.get(vertex_id, []):
|
2180
|
+
find_runnable_predecessors(predecessor_id)
|
2181
|
+
return runnable_vertices
|
2182
|
+
|
2183
|
+
def remove_from_predecessors(self, vertex_id: str) -> None:
|
2184
|
+
self.run_manager.remove_from_predecessors(vertex_id)
|
2185
|
+
|
2186
|
+
def remove_vertex_from_runnables(self, vertex_id: str) -> None:
|
2187
|
+
self.run_manager.remove_vertex_from_runnables(vertex_id)
|
2188
|
+
|
2189
|
+
def get_top_level_vertices(self, vertices_ids):
|
2190
|
+
"""Retrieves the top-level vertices from the given graph based on the provided vertex IDs.
|
2191
|
+
|
2192
|
+
Args:
|
2193
|
+
vertices_ids (list): A list of vertex IDs.
|
2194
|
+
|
2195
|
+
Returns:
|
2196
|
+
list: A list of top-level vertex IDs.
|
2197
|
+
|
2198
|
+
"""
|
2199
|
+
top_level_vertices = []
|
2200
|
+
for vertex_id in vertices_ids:
|
2201
|
+
vertex = self.get_vertex(vertex_id)
|
2202
|
+
if vertex.parent_is_top_level:
|
2203
|
+
top_level_vertices.append(vertex.parent_node_id)
|
2204
|
+
else:
|
2205
|
+
top_level_vertices.append(vertex_id)
|
2206
|
+
return top_level_vertices
|
2207
|
+
|
2208
|
+
def build_in_degree(self, edges: list[CycleEdge]) -> dict[str, int]:
|
2209
|
+
in_degree: dict[str, int] = defaultdict(int)
|
2210
|
+
|
2211
|
+
for edge in edges:
|
2212
|
+
# We don't need to count if a Component connects more than one
|
2213
|
+
# time to the same vertex.
|
2214
|
+
in_degree[edge.target_id] += 1
|
2215
|
+
for vertex in self.vertices:
|
2216
|
+
if vertex.id not in in_degree:
|
2217
|
+
in_degree[vertex.id] = 0
|
2218
|
+
return in_degree
|
2219
|
+
|
2220
|
+
@staticmethod
|
2221
|
+
def build_adjacency_maps(edges: list[CycleEdge]) -> tuple[dict[str, list[str]], dict[str, list[str]]]:
|
2222
|
+
"""Returns the adjacency maps for the graph."""
|
2223
|
+
predecessor_map: dict[str, list[str]] = defaultdict(list)
|
2224
|
+
successor_map: dict[str, list[str]] = defaultdict(list)
|
2225
|
+
for edge in edges:
|
2226
|
+
predecessor_map[edge.target_id].append(edge.source_id)
|
2227
|
+
successor_map[edge.source_id].append(edge.target_id)
|
2228
|
+
return predecessor_map, successor_map
|
2229
|
+
|
2230
|
+
def __to_dict(self) -> dict[str, dict[str, list[str]]]:
|
2231
|
+
"""Converts the graph to a dictionary."""
|
2232
|
+
result: dict = {}
|
2233
|
+
for vertex in self.vertices:
|
2234
|
+
vertex_id = vertex.id
|
2235
|
+
sucessors = [i.id for i in self.get_all_successors(vertex)]
|
2236
|
+
predecessors = [i.id for i in self.get_predecessors(vertex)]
|
2237
|
+
result |= {vertex_id: {"successors": sucessors, "predecessors": predecessors}}
|
2238
|
+
return result
|