vellum-ai 0.9.16rc2__py3-none-any.whl → 0.10.0__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- vellum/plugins/__init__.py +0 -0
- vellum/plugins/pydantic.py +74 -0
- vellum/plugins/utils.py +19 -0
- vellum/plugins/vellum_mypy.py +639 -3
- vellum/workflows/README.md +90 -0
- vellum/workflows/__init__.py +5 -0
- vellum/workflows/constants.py +43 -0
- vellum/workflows/descriptors/__init__.py +0 -0
- vellum/workflows/descriptors/base.py +339 -0
- vellum/workflows/descriptors/tests/test_utils.py +83 -0
- vellum/workflows/descriptors/utils.py +90 -0
- vellum/workflows/edges/__init__.py +5 -0
- vellum/workflows/edges/edge.py +23 -0
- vellum/workflows/emitters/__init__.py +5 -0
- vellum/workflows/emitters/base.py +14 -0
- vellum/workflows/environment/__init__.py +5 -0
- vellum/workflows/environment/environment.py +7 -0
- vellum/workflows/errors/__init__.py +6 -0
- vellum/workflows/errors/types.py +20 -0
- vellum/workflows/events/__init__.py +31 -0
- vellum/workflows/events/node.py +125 -0
- vellum/workflows/events/tests/__init__.py +0 -0
- vellum/workflows/events/tests/test_event.py +216 -0
- vellum/workflows/events/types.py +52 -0
- vellum/workflows/events/utils.py +5 -0
- vellum/workflows/events/workflow.py +139 -0
- vellum/workflows/exceptions.py +15 -0
- vellum/workflows/expressions/__init__.py +0 -0
- vellum/workflows/expressions/accessor.py +52 -0
- vellum/workflows/expressions/and_.py +32 -0
- vellum/workflows/expressions/begins_with.py +31 -0
- vellum/workflows/expressions/between.py +38 -0
- vellum/workflows/expressions/coalesce_expression.py +41 -0
- vellum/workflows/expressions/contains.py +30 -0
- vellum/workflows/expressions/does_not_begin_with.py +31 -0
- vellum/workflows/expressions/does_not_contain.py +30 -0
- vellum/workflows/expressions/does_not_end_with.py +31 -0
- vellum/workflows/expressions/does_not_equal.py +25 -0
- vellum/workflows/expressions/ends_with.py +31 -0
- vellum/workflows/expressions/equals.py +25 -0
- vellum/workflows/expressions/greater_than.py +33 -0
- vellum/workflows/expressions/greater_than_or_equal_to.py +33 -0
- vellum/workflows/expressions/in_.py +31 -0
- vellum/workflows/expressions/is_blank.py +24 -0
- vellum/workflows/expressions/is_not_blank.py +24 -0
- vellum/workflows/expressions/is_not_null.py +21 -0
- vellum/workflows/expressions/is_not_undefined.py +22 -0
- vellum/workflows/expressions/is_null.py +21 -0
- vellum/workflows/expressions/is_undefined.py +22 -0
- vellum/workflows/expressions/less_than.py +33 -0
- vellum/workflows/expressions/less_than_or_equal_to.py +33 -0
- vellum/workflows/expressions/not_between.py +38 -0
- vellum/workflows/expressions/not_in.py +31 -0
- vellum/workflows/expressions/or_.py +32 -0
- vellum/workflows/graph/__init__.py +3 -0
- vellum/workflows/graph/graph.py +131 -0
- vellum/workflows/graph/tests/__init__.py +0 -0
- vellum/workflows/graph/tests/test_graph.py +437 -0
- vellum/workflows/inputs/__init__.py +5 -0
- vellum/workflows/inputs/base.py +55 -0
- vellum/workflows/logging.py +14 -0
- vellum/workflows/nodes/__init__.py +46 -0
- vellum/workflows/nodes/bases/__init__.py +7 -0
- vellum/workflows/nodes/bases/base.py +332 -0
- vellum/workflows/nodes/bases/base_subworkflow_node/__init__.py +5 -0
- vellum/workflows/nodes/bases/base_subworkflow_node/node.py +10 -0
- vellum/workflows/nodes/bases/tests/__init__.py +0 -0
- vellum/workflows/nodes/bases/tests/test_base_node.py +125 -0
- vellum/workflows/nodes/core/__init__.py +16 -0
- vellum/workflows/nodes/core/error_node/__init__.py +5 -0
- vellum/workflows/nodes/core/error_node/node.py +26 -0
- vellum/workflows/nodes/core/inline_subworkflow_node/__init__.py +5 -0
- vellum/workflows/nodes/core/inline_subworkflow_node/node.py +73 -0
- vellum/workflows/nodes/core/map_node/__init__.py +5 -0
- vellum/workflows/nodes/core/map_node/node.py +147 -0
- vellum/workflows/nodes/core/map_node/tests/__init__.py +0 -0
- vellum/workflows/nodes/core/map_node/tests/test_node.py +65 -0
- vellum/workflows/nodes/core/retry_node/__init__.py +5 -0
- vellum/workflows/nodes/core/retry_node/node.py +106 -0
- vellum/workflows/nodes/core/retry_node/tests/__init__.py +0 -0
- vellum/workflows/nodes/core/retry_node/tests/test_node.py +93 -0
- vellum/workflows/nodes/core/templating_node/__init__.py +5 -0
- vellum/workflows/nodes/core/templating_node/custom_filters.py +12 -0
- vellum/workflows/nodes/core/templating_node/exceptions.py +2 -0
- vellum/workflows/nodes/core/templating_node/node.py +123 -0
- vellum/workflows/nodes/core/templating_node/render.py +55 -0
- vellum/workflows/nodes/core/templating_node/tests/test_templating_node.py +21 -0
- vellum/workflows/nodes/core/try_node/__init__.py +5 -0
- vellum/workflows/nodes/core/try_node/node.py +110 -0
- vellum/workflows/nodes/core/try_node/tests/__init__.py +0 -0
- vellum/workflows/nodes/core/try_node/tests/test_node.py +82 -0
- vellum/workflows/nodes/displayable/__init__.py +31 -0
- vellum/workflows/nodes/displayable/api_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/api_node/node.py +44 -0
- vellum/workflows/nodes/displayable/bases/__init__.py +11 -0
- vellum/workflows/nodes/displayable/bases/api_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/bases/api_node/node.py +70 -0
- vellum/workflows/nodes/displayable/bases/base_prompt_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/bases/base_prompt_node/node.py +60 -0
- vellum/workflows/nodes/displayable/bases/inline_prompt_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/bases/inline_prompt_node/constants.py +13 -0
- vellum/workflows/nodes/displayable/bases/inline_prompt_node/node.py +118 -0
- vellum/workflows/nodes/displayable/bases/prompt_deployment_node.py +98 -0
- vellum/workflows/nodes/displayable/bases/search_node.py +90 -0
- vellum/workflows/nodes/displayable/code_execution_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/code_execution_node/node.py +197 -0
- vellum/workflows/nodes/displayable/code_execution_node/tests/__init__.py +0 -0
- vellum/workflows/nodes/displayable/code_execution_node/tests/fixtures/__init__.py +0 -0
- vellum/workflows/nodes/displayable/code_execution_node/tests/fixtures/main.py +3 -0
- vellum/workflows/nodes/displayable/code_execution_node/tests/test_code_execution_node.py +111 -0
- vellum/workflows/nodes/displayable/code_execution_node/utils.py +10 -0
- vellum/workflows/nodes/displayable/conditional_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/conditional_node/node.py +25 -0
- vellum/workflows/nodes/displayable/final_output_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/final_output_node/node.py +43 -0
- vellum/workflows/nodes/displayable/guardrail_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/guardrail_node/node.py +97 -0
- vellum/workflows/nodes/displayable/inline_prompt_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/inline_prompt_node/node.py +41 -0
- vellum/workflows/nodes/displayable/merge_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/merge_node/node.py +10 -0
- vellum/workflows/nodes/displayable/prompt_deployment_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/prompt_deployment_node/node.py +45 -0
- vellum/workflows/nodes/displayable/search_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/search_node/node.py +26 -0
- vellum/workflows/nodes/displayable/subworkflow_deployment_node/__init__.py +5 -0
- vellum/workflows/nodes/displayable/subworkflow_deployment_node/node.py +156 -0
- vellum/workflows/nodes/displayable/tests/__init__.py +0 -0
- vellum/workflows/nodes/displayable/tests/test_inline_text_prompt_node.py +148 -0
- vellum/workflows/nodes/displayable/tests/test_search_node_wth_text_output.py +134 -0
- vellum/workflows/nodes/displayable/tests/test_text_prompt_deployment_node.py +80 -0
- vellum/workflows/nodes/utils.py +27 -0
- vellum/workflows/outputs/__init__.py +6 -0
- vellum/workflows/outputs/base.py +196 -0
- vellum/workflows/ports/__init__.py +7 -0
- vellum/workflows/ports/node_ports.py +75 -0
- vellum/workflows/ports/port.py +75 -0
- vellum/workflows/ports/utils.py +40 -0
- vellum/workflows/references/__init__.py +17 -0
- vellum/workflows/references/environment_variable.py +20 -0
- vellum/workflows/references/execution_count.py +20 -0
- vellum/workflows/references/external_input.py +49 -0
- vellum/workflows/references/input.py +7 -0
- vellum/workflows/references/lazy.py +55 -0
- vellum/workflows/references/node.py +43 -0
- vellum/workflows/references/output.py +78 -0
- vellum/workflows/references/state_value.py +23 -0
- vellum/workflows/references/vellum_secret.py +15 -0
- vellum/workflows/references/workflow_input.py +41 -0
- vellum/workflows/resolvers/__init__.py +5 -0
- vellum/workflows/resolvers/base.py +15 -0
- vellum/workflows/runner/__init__.py +5 -0
- vellum/workflows/runner/runner.py +588 -0
- vellum/workflows/runner/types.py +18 -0
- vellum/workflows/state/__init__.py +5 -0
- vellum/workflows/state/base.py +327 -0
- vellum/workflows/state/context.py +18 -0
- vellum/workflows/state/encoder.py +57 -0
- vellum/workflows/state/store.py +28 -0
- vellum/workflows/state/tests/__init__.py +0 -0
- vellum/workflows/state/tests/test_state.py +113 -0
- vellum/workflows/types/__init__.py +0 -0
- vellum/workflows/types/core.py +91 -0
- vellum/workflows/types/generics.py +14 -0
- vellum/workflows/types/stack.py +39 -0
- vellum/workflows/types/tests/__init__.py +0 -0
- vellum/workflows/types/tests/test_utils.py +76 -0
- vellum/workflows/types/utils.py +164 -0
- vellum/workflows/utils/__init__.py +0 -0
- vellum/workflows/utils/names.py +13 -0
- vellum/workflows/utils/tests/__init__.py +0 -0
- vellum/workflows/utils/tests/test_names.py +15 -0
- vellum/workflows/utils/tests/test_vellum_variables.py +25 -0
- vellum/workflows/utils/vellum_variables.py +81 -0
- vellum/workflows/vellum_client.py +18 -0
- vellum/workflows/workflows/__init__.py +5 -0
- vellum/workflows/workflows/base.py +365 -0
- {vellum_ai-0.9.16rc2.dist-info → vellum_ai-0.10.0.dist-info}/METADATA +2 -1
- {vellum_ai-0.9.16rc2.dist-info → vellum_ai-0.10.0.dist-info}/RECORD +245 -7
- vellum_cli/__init__.py +72 -0
- vellum_cli/aliased_group.py +103 -0
- vellum_cli/config.py +96 -0
- vellum_cli/image_push.py +112 -0
- vellum_cli/logger.py +36 -0
- vellum_cli/pull.py +73 -0
- vellum_cli/push.py +121 -0
- vellum_cli/tests/test_config.py +100 -0
- vellum_cli/tests/test_pull.py +152 -0
- vellum_ee/workflows/__init__.py +0 -0
- vellum_ee/workflows/display/__init__.py +0 -0
- vellum_ee/workflows/display/base.py +73 -0
- vellum_ee/workflows/display/nodes/__init__.py +4 -0
- vellum_ee/workflows/display/nodes/base_node_display.py +116 -0
- vellum_ee/workflows/display/nodes/base_node_vellum_display.py +36 -0
- vellum_ee/workflows/display/nodes/get_node_display_class.py +25 -0
- vellum_ee/workflows/display/nodes/tests/__init__.py +0 -0
- vellum_ee/workflows/display/nodes/tests/test_base_node_display.py +47 -0
- vellum_ee/workflows/display/nodes/types.py +18 -0
- vellum_ee/workflows/display/nodes/utils.py +33 -0
- vellum_ee/workflows/display/nodes/vellum/__init__.py +32 -0
- vellum_ee/workflows/display/nodes/vellum/api_node.py +205 -0
- vellum_ee/workflows/display/nodes/vellum/code_execution_node.py +71 -0
- vellum_ee/workflows/display/nodes/vellum/conditional_node.py +217 -0
- vellum_ee/workflows/display/nodes/vellum/final_output_node.py +61 -0
- vellum_ee/workflows/display/nodes/vellum/guardrail_node.py +49 -0
- vellum_ee/workflows/display/nodes/vellum/inline_prompt_node.py +170 -0
- vellum_ee/workflows/display/nodes/vellum/inline_subworkflow_node.py +99 -0
- vellum_ee/workflows/display/nodes/vellum/map_node.py +100 -0
- vellum_ee/workflows/display/nodes/vellum/merge_node.py +48 -0
- vellum_ee/workflows/display/nodes/vellum/prompt_deployment_node.py +68 -0
- vellum_ee/workflows/display/nodes/vellum/search_node.py +193 -0
- vellum_ee/workflows/display/nodes/vellum/subworkflow_deployment_node.py +58 -0
- vellum_ee/workflows/display/nodes/vellum/templating_node.py +67 -0
- vellum_ee/workflows/display/nodes/vellum/tests/__init__.py +0 -0
- vellum_ee/workflows/display/nodes/vellum/tests/test_utils.py +106 -0
- vellum_ee/workflows/display/nodes/vellum/try_node.py +38 -0
- vellum_ee/workflows/display/nodes/vellum/utils.py +76 -0
- vellum_ee/workflows/display/tests/__init__.py +0 -0
- vellum_ee/workflows/display/tests/workflow_serialization/__init__.py +0 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_api_node_serialization.py +426 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_code_execution_node_serialization.py +607 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_conditional_node_serialization.py +1175 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_guardrail_node_serialization.py +235 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_inline_subworkflow_serialization.py +511 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_map_node_serialization.py +372 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_merge_node_serialization.py +272 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_prompt_deployment_serialization.py +289 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_subworkflow_deployment_serialization.py +354 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_terminal_node_serialization.py +123 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_basic_try_node_serialization.py +84 -0
- vellum_ee/workflows/display/tests/workflow_serialization/test_complex_terminal_node_serialization.py +233 -0
- vellum_ee/workflows/display/types.py +46 -0
- vellum_ee/workflows/display/utils/__init__.py +0 -0
- vellum_ee/workflows/display/utils/tests/__init__.py +0 -0
- vellum_ee/workflows/display/utils/tests/test_uuids.py +16 -0
- vellum_ee/workflows/display/utils/uuids.py +24 -0
- vellum_ee/workflows/display/utils/vellum.py +121 -0
- vellum_ee/workflows/display/vellum.py +357 -0
- vellum_ee/workflows/display/workflows/__init__.py +5 -0
- vellum_ee/workflows/display/workflows/base_workflow_display.py +302 -0
- vellum_ee/workflows/display/workflows/get_vellum_workflow_display_class.py +32 -0
- vellum_ee/workflows/display/workflows/vellum_workflow_display.py +386 -0
- {vellum_ai-0.9.16rc2.dist-info → vellum_ai-0.10.0.dist-info}/LICENSE +0 -0
- {vellum_ai-0.9.16rc2.dist-info → vellum_ai-0.10.0.dist-info}/WHEEL +0 -0
- {vellum_ai-0.9.16rc2.dist-info → vellum_ai-0.10.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,588 @@
|
|
1
|
+
from collections import defaultdict
|
2
|
+
from copy import deepcopy
|
3
|
+
import logging
|
4
|
+
from queue import Empty, Queue
|
5
|
+
from threading import Event as ThreadingEvent, Thread
|
6
|
+
from uuid import UUID, uuid4
|
7
|
+
from typing import TYPE_CHECKING, Any, Dict, Generic, Iterable, Iterator, Optional, Sequence, Set, Type, Union
|
8
|
+
|
9
|
+
from vellum.workflows.constants import UNDEF
|
10
|
+
from vellum.workflows.descriptors.base import BaseDescriptor
|
11
|
+
from vellum.workflows.edges.edge import Edge
|
12
|
+
from vellum.workflows.errors import VellumError, VellumErrorCode
|
13
|
+
from vellum.workflows.events import (
|
14
|
+
NodeExecutionFulfilledEvent,
|
15
|
+
NodeExecutionInitiatedEvent,
|
16
|
+
NodeExecutionRejectedEvent,
|
17
|
+
NodeExecutionStreamingEvent,
|
18
|
+
WorkflowEvent,
|
19
|
+
WorkflowEventStream,
|
20
|
+
WorkflowExecutionFulfilledEvent,
|
21
|
+
WorkflowExecutionInitiatedEvent,
|
22
|
+
WorkflowExecutionRejectedEvent,
|
23
|
+
WorkflowExecutionStreamingEvent,
|
24
|
+
)
|
25
|
+
from vellum.workflows.events.node import (
|
26
|
+
NodeExecutionFulfilledBody,
|
27
|
+
NodeExecutionInitiatedBody,
|
28
|
+
NodeExecutionRejectedBody,
|
29
|
+
NodeExecutionStreamingBody,
|
30
|
+
)
|
31
|
+
from vellum.workflows.events.types import BaseEvent
|
32
|
+
from vellum.workflows.events.utils import is_terminal_event
|
33
|
+
from vellum.workflows.events.workflow import (
|
34
|
+
WorkflowExecutionFulfilledBody,
|
35
|
+
WorkflowExecutionInitiatedBody,
|
36
|
+
WorkflowExecutionPausedBody,
|
37
|
+
WorkflowExecutionPausedEvent,
|
38
|
+
WorkflowExecutionRejectedBody,
|
39
|
+
WorkflowExecutionResumedBody,
|
40
|
+
WorkflowExecutionResumedEvent,
|
41
|
+
WorkflowExecutionStreamingBody,
|
42
|
+
)
|
43
|
+
from vellum.workflows.exceptions import NodeException
|
44
|
+
from vellum.workflows.nodes.bases import BaseNode
|
45
|
+
from vellum.workflows.outputs import BaseOutputs
|
46
|
+
from vellum.workflows.outputs.base import BaseOutput
|
47
|
+
from vellum.workflows.ports.port import Port
|
48
|
+
from vellum.workflows.references import ExternalInputReference, OutputReference
|
49
|
+
from vellum.workflows.runner.types import WorkItemEvent
|
50
|
+
from vellum.workflows.state.base import BaseState
|
51
|
+
from vellum.workflows.types.generics import OutputsType, StateType, WorkflowInputsType
|
52
|
+
|
53
|
+
if TYPE_CHECKING:
|
54
|
+
from vellum.workflows import BaseWorkflow
|
55
|
+
|
56
|
+
logger = logging.getLogger(__name__)
|
57
|
+
|
58
|
+
RunFromNodeArg = Sequence[Type[BaseNode]]
|
59
|
+
ExternalInputsArg = Dict[ExternalInputReference, Any]
|
60
|
+
BackgroundThreadItem = Union[BaseState, WorkflowEvent, None]
|
61
|
+
|
62
|
+
|
63
|
+
class WorkflowRunner(Generic[StateType]):
|
64
|
+
_entrypoints: Iterable[Type[BaseNode]]
|
65
|
+
|
66
|
+
def __init__(
|
67
|
+
self,
|
68
|
+
workflow: "BaseWorkflow[WorkflowInputsType, StateType]",
|
69
|
+
inputs: Optional[WorkflowInputsType] = None,
|
70
|
+
state: Optional[StateType] = None,
|
71
|
+
entrypoint_nodes: Optional[RunFromNodeArg] = None,
|
72
|
+
external_inputs: Optional[ExternalInputsArg] = None,
|
73
|
+
cancel_signal: Optional[ThreadingEvent] = None,
|
74
|
+
):
|
75
|
+
if state and external_inputs:
|
76
|
+
raise ValueError("Can only run a Workflow providing one of state or external inputs, not both")
|
77
|
+
|
78
|
+
self.workflow = workflow
|
79
|
+
if entrypoint_nodes:
|
80
|
+
if len(list(entrypoint_nodes)) > 1:
|
81
|
+
raise ValueError("Cannot resume from multiple nodes")
|
82
|
+
|
83
|
+
# TODO: Support resuming from multiple nodes
|
84
|
+
# https://app.shortcut.com/vellum/story/4408
|
85
|
+
node = next(iter(entrypoint_nodes))
|
86
|
+
if state:
|
87
|
+
self._initial_state = state
|
88
|
+
else:
|
89
|
+
self._initial_state = self.workflow.get_state_at_node(node)
|
90
|
+
self._entrypoints = entrypoint_nodes
|
91
|
+
elif external_inputs:
|
92
|
+
self._initial_state = self.workflow.get_most_recent_state()
|
93
|
+
for descriptor, value in external_inputs.items():
|
94
|
+
self._initial_state.meta.external_inputs[descriptor] = value
|
95
|
+
|
96
|
+
self._entrypoints = [
|
97
|
+
ei.inputs_class.__parent_class__
|
98
|
+
for ei in external_inputs
|
99
|
+
if issubclass(ei.inputs_class.__parent_class__, BaseNode)
|
100
|
+
]
|
101
|
+
else:
|
102
|
+
normalized_inputs = deepcopy(inputs) if inputs else self.workflow.get_default_inputs()
|
103
|
+
if state:
|
104
|
+
self._initial_state = deepcopy(state)
|
105
|
+
self._initial_state.meta.workflow_inputs = normalized_inputs
|
106
|
+
else:
|
107
|
+
self._initial_state = self.workflow.get_default_state(normalized_inputs)
|
108
|
+
self._entrypoints = self.workflow.get_entrypoints()
|
109
|
+
|
110
|
+
self._work_item_event_queue: Queue[WorkItemEvent[StateType]] = Queue()
|
111
|
+
self._workflow_event_queue: Queue[WorkflowEvent] = Queue()
|
112
|
+
self._background_thread_queue: Queue[BackgroundThreadItem] = Queue()
|
113
|
+
self._dependencies: Dict[Type[BaseNode], Set[Type[BaseNode]]] = defaultdict(set)
|
114
|
+
self._state_forks: Set[StateType] = {self._initial_state}
|
115
|
+
|
116
|
+
self._active_nodes_by_execution_id: Dict[UUID, BaseNode[StateType]] = {}
|
117
|
+
self._cancel_signal = cancel_signal
|
118
|
+
|
119
|
+
setattr(
|
120
|
+
self._initial_state,
|
121
|
+
"__snapshot_callback__",
|
122
|
+
lambda s: self._snapshot_state(s),
|
123
|
+
)
|
124
|
+
|
125
|
+
def _snapshot_state(self, state: StateType) -> StateType:
|
126
|
+
self.workflow._store.append_state_snapshot(state)
|
127
|
+
self._background_thread_queue.put(state)
|
128
|
+
return state
|
129
|
+
|
130
|
+
def _emit_event(self, event: WorkflowEvent) -> WorkflowEvent:
|
131
|
+
self.workflow._store.append_event(event)
|
132
|
+
self._background_thread_queue.put(event)
|
133
|
+
return event
|
134
|
+
|
135
|
+
def _run_work_item(self, node: BaseNode[StateType], span_id: UUID) -> None:
|
136
|
+
self._work_item_event_queue.put(
|
137
|
+
WorkItemEvent(
|
138
|
+
node=node,
|
139
|
+
event=NodeExecutionInitiatedEvent(
|
140
|
+
trace_id=node.state.meta.trace_id,
|
141
|
+
span_id=span_id,
|
142
|
+
body=NodeExecutionInitiatedBody(
|
143
|
+
node_definition=node.__class__,
|
144
|
+
inputs=node._inputs,
|
145
|
+
),
|
146
|
+
),
|
147
|
+
)
|
148
|
+
)
|
149
|
+
|
150
|
+
logger.debug(f"Started running node: {node.__class__.__name__}")
|
151
|
+
|
152
|
+
try:
|
153
|
+
node_run_response = node.run()
|
154
|
+
ports = node.Ports()
|
155
|
+
if not isinstance(node_run_response, (BaseOutputs, Iterator)):
|
156
|
+
raise NodeException(
|
157
|
+
message=f"Node {node.__class__.__name__} did not return a valid node run response",
|
158
|
+
code=VellumErrorCode.INVALID_OUTPUTS,
|
159
|
+
)
|
160
|
+
|
161
|
+
if isinstance(node_run_response, BaseOutputs):
|
162
|
+
if not isinstance(node_run_response, node.Outputs):
|
163
|
+
raise NodeException(
|
164
|
+
message=f"Node {node.__class__.__name__} did not return a valid outputs object",
|
165
|
+
code=VellumErrorCode.INVALID_OUTPUTS,
|
166
|
+
)
|
167
|
+
|
168
|
+
outputs = node_run_response
|
169
|
+
else:
|
170
|
+
streaming_output_queues: Dict[str, Queue] = {}
|
171
|
+
outputs = node.Outputs()
|
172
|
+
|
173
|
+
for output in node_run_response:
|
174
|
+
invoked_ports = output > ports
|
175
|
+
if not output.is_fulfilled:
|
176
|
+
if output.name not in streaming_output_queues:
|
177
|
+
streaming_output_queues[output.name] = Queue()
|
178
|
+
output_descriptor = OutputReference(
|
179
|
+
name=output.name,
|
180
|
+
types=(type(output.delta),),
|
181
|
+
instance=None,
|
182
|
+
outputs_class=node.Outputs,
|
183
|
+
)
|
184
|
+
node.state.meta.node_outputs[output_descriptor] = streaming_output_queues[output.name]
|
185
|
+
self._work_item_event_queue.put(
|
186
|
+
WorkItemEvent(
|
187
|
+
node=node,
|
188
|
+
event=NodeExecutionStreamingEvent(
|
189
|
+
trace_id=node.state.meta.trace_id,
|
190
|
+
span_id=span_id,
|
191
|
+
body=NodeExecutionStreamingBody(
|
192
|
+
node_definition=node.__class__,
|
193
|
+
output=BaseOutput(name=output.name),
|
194
|
+
),
|
195
|
+
),
|
196
|
+
invoked_ports=invoked_ports,
|
197
|
+
)
|
198
|
+
)
|
199
|
+
|
200
|
+
streaming_output_queues[output.name].put(output.delta)
|
201
|
+
self._work_item_event_queue.put(
|
202
|
+
WorkItemEvent(
|
203
|
+
node=node,
|
204
|
+
event=NodeExecutionStreamingEvent(
|
205
|
+
trace_id=node.state.meta.trace_id,
|
206
|
+
span_id=span_id,
|
207
|
+
body=NodeExecutionStreamingBody(
|
208
|
+
node_definition=node.__class__,
|
209
|
+
output=output,
|
210
|
+
),
|
211
|
+
),
|
212
|
+
invoked_ports=invoked_ports,
|
213
|
+
)
|
214
|
+
)
|
215
|
+
else:
|
216
|
+
if output.name in streaming_output_queues:
|
217
|
+
streaming_output_queues[output.name].put(UNDEF)
|
218
|
+
|
219
|
+
setattr(outputs, output.name, output.value)
|
220
|
+
self._work_item_event_queue.put(
|
221
|
+
WorkItemEvent(
|
222
|
+
node=node,
|
223
|
+
event=NodeExecutionStreamingEvent(
|
224
|
+
trace_id=node.state.meta.trace_id,
|
225
|
+
span_id=span_id,
|
226
|
+
body=NodeExecutionStreamingBody(
|
227
|
+
node_definition=node.__class__,
|
228
|
+
output=output,
|
229
|
+
),
|
230
|
+
),
|
231
|
+
invoked_ports=invoked_ports,
|
232
|
+
)
|
233
|
+
)
|
234
|
+
|
235
|
+
for descriptor, output_value in outputs:
|
236
|
+
node.state.meta.node_outputs[descriptor] = output_value
|
237
|
+
|
238
|
+
invoked_ports = ports(outputs, node.state)
|
239
|
+
node.state.meta.node_execution_cache.fulfill_node_execution(node.__class__, span_id)
|
240
|
+
|
241
|
+
self._work_item_event_queue.put(
|
242
|
+
WorkItemEvent(
|
243
|
+
node=node,
|
244
|
+
event=NodeExecutionFulfilledEvent(
|
245
|
+
trace_id=node.state.meta.trace_id,
|
246
|
+
span_id=span_id,
|
247
|
+
body=NodeExecutionFulfilledBody(
|
248
|
+
node_definition=node.__class__,
|
249
|
+
outputs=outputs,
|
250
|
+
),
|
251
|
+
),
|
252
|
+
invoked_ports=invoked_ports,
|
253
|
+
)
|
254
|
+
)
|
255
|
+
except NodeException as e:
|
256
|
+
self._work_item_event_queue.put(
|
257
|
+
WorkItemEvent(
|
258
|
+
node=node,
|
259
|
+
event=NodeExecutionRejectedEvent(
|
260
|
+
trace_id=node.state.meta.trace_id,
|
261
|
+
span_id=span_id,
|
262
|
+
body=NodeExecutionRejectedBody(
|
263
|
+
node_definition=node.__class__,
|
264
|
+
error=e.error,
|
265
|
+
),
|
266
|
+
),
|
267
|
+
)
|
268
|
+
)
|
269
|
+
except Exception as e:
|
270
|
+
logger.exception(f"An unexpected error occurred while running node {node.__class__.__name__}")
|
271
|
+
|
272
|
+
self._work_item_event_queue.put(
|
273
|
+
WorkItemEvent(
|
274
|
+
node=node,
|
275
|
+
event=NodeExecutionRejectedEvent(
|
276
|
+
trace_id=node.state.meta.trace_id,
|
277
|
+
span_id=span_id,
|
278
|
+
body=NodeExecutionRejectedBody(
|
279
|
+
node_definition=node.__class__,
|
280
|
+
error=VellumError(
|
281
|
+
message=str(e),
|
282
|
+
code=VellumErrorCode.INTERNAL_ERROR,
|
283
|
+
),
|
284
|
+
),
|
285
|
+
),
|
286
|
+
)
|
287
|
+
)
|
288
|
+
|
289
|
+
logger.debug(f"Finished running node: {node.__class__.__name__}")
|
290
|
+
|
291
|
+
def _handle_invoked_ports(self, state: StateType, ports: Optional[Iterable[Port]]) -> None:
|
292
|
+
if not ports:
|
293
|
+
return
|
294
|
+
|
295
|
+
for port in ports:
|
296
|
+
for edge in port.edges:
|
297
|
+
if port.fork_state:
|
298
|
+
next_state = deepcopy(state)
|
299
|
+
self._state_forks.add(next_state)
|
300
|
+
else:
|
301
|
+
next_state = state
|
302
|
+
|
303
|
+
self._run_node_if_ready(next_state, edge.to_node, edge)
|
304
|
+
|
305
|
+
def _run_node_if_ready(
|
306
|
+
self, state: StateType, node_class: Type[BaseNode], invoked_by: Optional[Edge] = None
|
307
|
+
) -> None:
|
308
|
+
with state.__lock__:
|
309
|
+
for descriptor in node_class.ExternalInputs:
|
310
|
+
if not isinstance(descriptor, ExternalInputReference):
|
311
|
+
continue
|
312
|
+
|
313
|
+
if state.meta.external_inputs.get(descriptor, UNDEF) is UNDEF:
|
314
|
+
state.meta.external_inputs[descriptor] = UNDEF
|
315
|
+
return
|
316
|
+
|
317
|
+
all_deps = self._dependencies[node_class]
|
318
|
+
if not node_class.Trigger.should_initiate(state, all_deps, invoked_by):
|
319
|
+
return
|
320
|
+
|
321
|
+
node = node_class(state=state, context=self.workflow.context)
|
322
|
+
node_span_id = uuid4()
|
323
|
+
state.meta.node_execution_cache.initiate_node_execution(node_class, node_span_id)
|
324
|
+
self._active_nodes_by_execution_id[node_span_id] = node
|
325
|
+
|
326
|
+
worker_thread = Thread(target=self._run_work_item, kwargs={"node": node, "span_id": node_span_id})
|
327
|
+
worker_thread.start()
|
328
|
+
|
329
|
+
def _handle_work_item_event(self, work_item_event: WorkItemEvent[StateType]) -> Optional[VellumError]:
|
330
|
+
node = work_item_event.node
|
331
|
+
event = work_item_event.event
|
332
|
+
invoked_ports = work_item_event.invoked_ports
|
333
|
+
|
334
|
+
if event.name == "node.execution.initiated":
|
335
|
+
return None
|
336
|
+
|
337
|
+
if event.name == "node.execution.rejected":
|
338
|
+
self._active_nodes_by_execution_id.pop(event.span_id)
|
339
|
+
return event.error
|
340
|
+
|
341
|
+
if event.name == "node.execution.streaming":
|
342
|
+
for workflow_output_descriptor in self.workflow.Outputs:
|
343
|
+
node_output_descriptor = workflow_output_descriptor.instance
|
344
|
+
if not isinstance(node_output_descriptor, OutputReference):
|
345
|
+
continue
|
346
|
+
if node_output_descriptor.outputs_class != event.node_definition.Outputs:
|
347
|
+
continue
|
348
|
+
if node_output_descriptor.name != event.output.name:
|
349
|
+
continue
|
350
|
+
|
351
|
+
self._workflow_event_queue.put(
|
352
|
+
self._stream_workflow_event(
|
353
|
+
BaseOutput(
|
354
|
+
name=workflow_output_descriptor.name,
|
355
|
+
value=event.output.value,
|
356
|
+
delta=event.output.delta,
|
357
|
+
)
|
358
|
+
)
|
359
|
+
)
|
360
|
+
|
361
|
+
self._handle_invoked_ports(node.state, invoked_ports)
|
362
|
+
|
363
|
+
return None
|
364
|
+
|
365
|
+
if event.name == "node.execution.fulfilled":
|
366
|
+
self._active_nodes_by_execution_id.pop(event.span_id)
|
367
|
+
self._handle_invoked_ports(node.state, invoked_ports)
|
368
|
+
|
369
|
+
return None
|
370
|
+
|
371
|
+
raise ValueError(f"Invalid event name: {event.name}")
|
372
|
+
|
373
|
+
def _initiate_workflow_event(self) -> WorkflowExecutionInitiatedEvent:
|
374
|
+
return WorkflowExecutionInitiatedEvent(
|
375
|
+
trace_id=self._initial_state.meta.trace_id,
|
376
|
+
span_id=self._initial_state.meta.span_id,
|
377
|
+
body=WorkflowExecutionInitiatedBody(
|
378
|
+
workflow_definition=self.workflow.__class__,
|
379
|
+
inputs=self._initial_state.meta.workflow_inputs,
|
380
|
+
),
|
381
|
+
)
|
382
|
+
|
383
|
+
def _stream_workflow_event(self, output: BaseOutput) -> WorkflowExecutionStreamingEvent:
|
384
|
+
return WorkflowExecutionStreamingEvent(
|
385
|
+
trace_id=self._initial_state.meta.trace_id,
|
386
|
+
span_id=self._initial_state.meta.span_id,
|
387
|
+
body=WorkflowExecutionStreamingBody(
|
388
|
+
workflow_definition=self.workflow.__class__,
|
389
|
+
output=output,
|
390
|
+
),
|
391
|
+
)
|
392
|
+
|
393
|
+
def _fulfill_workflow_event(self, outputs: OutputsType) -> WorkflowExecutionFulfilledEvent:
|
394
|
+
return WorkflowExecutionFulfilledEvent(
|
395
|
+
trace_id=self._initial_state.meta.trace_id,
|
396
|
+
span_id=self._initial_state.meta.span_id,
|
397
|
+
body=WorkflowExecutionFulfilledBody(
|
398
|
+
workflow_definition=self.workflow.__class__,
|
399
|
+
outputs=outputs,
|
400
|
+
),
|
401
|
+
)
|
402
|
+
|
403
|
+
def _reject_workflow_event(self, error: VellumError) -> WorkflowExecutionRejectedEvent:
|
404
|
+
return WorkflowExecutionRejectedEvent(
|
405
|
+
trace_id=self._initial_state.meta.trace_id,
|
406
|
+
span_id=self._initial_state.meta.span_id,
|
407
|
+
body=WorkflowExecutionRejectedBody(
|
408
|
+
workflow_definition=self.workflow.__class__,
|
409
|
+
error=error,
|
410
|
+
),
|
411
|
+
)
|
412
|
+
|
413
|
+
def _resume_workflow_event(self) -> WorkflowExecutionResumedEvent:
|
414
|
+
return WorkflowExecutionResumedEvent(
|
415
|
+
trace_id=self._initial_state.meta.trace_id,
|
416
|
+
span_id=self._initial_state.meta.span_id,
|
417
|
+
body=WorkflowExecutionResumedBody(
|
418
|
+
workflow_definition=self.workflow.__class__,
|
419
|
+
),
|
420
|
+
)
|
421
|
+
|
422
|
+
def _pause_workflow_event(self, external_inputs: Iterable[ExternalInputReference]) -> WorkflowExecutionPausedEvent:
|
423
|
+
return WorkflowExecutionPausedEvent(
|
424
|
+
trace_id=self._initial_state.meta.trace_id,
|
425
|
+
span_id=self._initial_state.meta.span_id,
|
426
|
+
body=WorkflowExecutionPausedBody(
|
427
|
+
workflow_definition=self.workflow.__class__,
|
428
|
+
external_inputs=external_inputs,
|
429
|
+
),
|
430
|
+
)
|
431
|
+
|
432
|
+
def _stream(self) -> None:
|
433
|
+
# TODO: We should likely handle this during initialization
|
434
|
+
# https://app.shortcut.com/vellum/story/4327
|
435
|
+
if not self._entrypoints:
|
436
|
+
self._workflow_event_queue.put(
|
437
|
+
self._reject_workflow_event(
|
438
|
+
VellumError(message="No entrypoints defined", code=VellumErrorCode.INVALID_WORKFLOW)
|
439
|
+
)
|
440
|
+
)
|
441
|
+
return
|
442
|
+
|
443
|
+
for edge in self.workflow.get_edges():
|
444
|
+
self._dependencies[edge.to_node].add(edge.from_port.node_class)
|
445
|
+
|
446
|
+
for node_cls in self._entrypoints:
|
447
|
+
try:
|
448
|
+
self._run_node_if_ready(self._initial_state, node_cls)
|
449
|
+
except NodeException as e:
|
450
|
+
self._workflow_event_queue.put(self._reject_workflow_event(e.error))
|
451
|
+
return
|
452
|
+
except Exception:
|
453
|
+
err_message = f"An unexpected error occurred while initializing node {node_cls.__name__}"
|
454
|
+
logger.exception(err_message)
|
455
|
+
self._workflow_event_queue.put(
|
456
|
+
self._reject_workflow_event(
|
457
|
+
VellumError(code=VellumErrorCode.INTERNAL_ERROR, message=err_message),
|
458
|
+
)
|
459
|
+
)
|
460
|
+
return
|
461
|
+
|
462
|
+
rejection_error: Optional[VellumError] = None
|
463
|
+
|
464
|
+
while True:
|
465
|
+
if not self._active_nodes_by_execution_id:
|
466
|
+
break
|
467
|
+
|
468
|
+
work_item_event = self._work_item_event_queue.get()
|
469
|
+
event = work_item_event.event
|
470
|
+
|
471
|
+
self._workflow_event_queue.put(event)
|
472
|
+
|
473
|
+
rejection_error = self._handle_work_item_event(work_item_event)
|
474
|
+
if rejection_error:
|
475
|
+
break
|
476
|
+
|
477
|
+
# Handle any remaining events
|
478
|
+
try:
|
479
|
+
while work_item_event := self._work_item_event_queue.get_nowait():
|
480
|
+
self._workflow_event_queue.put(work_item_event.event)
|
481
|
+
|
482
|
+
rejection_error = self._handle_work_item_event(work_item_event)
|
483
|
+
if rejection_error:
|
484
|
+
break
|
485
|
+
except Empty:
|
486
|
+
pass
|
487
|
+
|
488
|
+
final_state = self._state_forks.pop()
|
489
|
+
for other_state in self._state_forks:
|
490
|
+
final_state += other_state
|
491
|
+
|
492
|
+
unresolved_external_inputs = {
|
493
|
+
descriptor
|
494
|
+
for descriptor, node_input_value in final_state.meta.external_inputs.items()
|
495
|
+
if node_input_value is UNDEF
|
496
|
+
}
|
497
|
+
if unresolved_external_inputs:
|
498
|
+
self._workflow_event_queue.put(
|
499
|
+
self._pause_workflow_event(unresolved_external_inputs),
|
500
|
+
)
|
501
|
+
return
|
502
|
+
|
503
|
+
final_state.meta.is_terminated = True
|
504
|
+
if rejection_error:
|
505
|
+
self._workflow_event_queue.put(self._reject_workflow_event(rejection_error))
|
506
|
+
return
|
507
|
+
|
508
|
+
fulfilled_outputs = self.workflow.Outputs()
|
509
|
+
for descriptor, value in fulfilled_outputs:
|
510
|
+
if isinstance(value, BaseDescriptor):
|
511
|
+
setattr(fulfilled_outputs, descriptor.name, value.resolve(final_state))
|
512
|
+
elif isinstance(descriptor.instance, BaseDescriptor):
|
513
|
+
setattr(fulfilled_outputs, descriptor.name, descriptor.instance.resolve(final_state))
|
514
|
+
|
515
|
+
self._workflow_event_queue.put(self._fulfill_workflow_event(fulfilled_outputs))
|
516
|
+
|
517
|
+
def _run_background_thread(self) -> None:
|
518
|
+
state_class = self.workflow.get_state_class()
|
519
|
+
while True:
|
520
|
+
item = self._background_thread_queue.get()
|
521
|
+
if item is None:
|
522
|
+
break
|
523
|
+
|
524
|
+
if isinstance(item, state_class):
|
525
|
+
for emitter in self.workflow.emitters:
|
526
|
+
emitter.snapshot_state(item)
|
527
|
+
elif isinstance(item, BaseEvent):
|
528
|
+
for emitter in self.workflow.emitters:
|
529
|
+
emitter.emit_event(item)
|
530
|
+
|
531
|
+
def _run_cancel_thread(self) -> None:
|
532
|
+
if not self._cancel_signal:
|
533
|
+
return
|
534
|
+
|
535
|
+
self._cancel_signal.wait()
|
536
|
+
self._workflow_event_queue.put(
|
537
|
+
self._reject_workflow_event(
|
538
|
+
VellumError(code=VellumErrorCode.WORKFLOW_CANCELLED, message="Workflow run cancelled")
|
539
|
+
)
|
540
|
+
)
|
541
|
+
|
542
|
+
def stream(self) -> WorkflowEventStream:
|
543
|
+
background_thread = Thread(target=self._run_background_thread)
|
544
|
+
background_thread.start()
|
545
|
+
|
546
|
+
if self._cancel_signal:
|
547
|
+
cancel_thread = Thread(target=self._run_cancel_thread)
|
548
|
+
cancel_thread.start()
|
549
|
+
|
550
|
+
event: WorkflowEvent
|
551
|
+
if self._initial_state.meta.is_terminated or self._initial_state.meta.is_terminated is None:
|
552
|
+
event = self._initiate_workflow_event()
|
553
|
+
else:
|
554
|
+
event = self._resume_workflow_event()
|
555
|
+
|
556
|
+
yield self._emit_event(event)
|
557
|
+
self._initial_state.meta.is_terminated = False
|
558
|
+
|
559
|
+
# The extra level of indirection prevents the runner from waiting on the caller to consume the event stream
|
560
|
+
stream_thread = Thread(target=self._stream)
|
561
|
+
stream_thread.start()
|
562
|
+
|
563
|
+
while stream_thread.is_alive():
|
564
|
+
try:
|
565
|
+
event = self._workflow_event_queue.get(timeout=0.1)
|
566
|
+
except Empty:
|
567
|
+
continue
|
568
|
+
|
569
|
+
yield self._emit_event(event)
|
570
|
+
|
571
|
+
if is_terminal_event(event):
|
572
|
+
break
|
573
|
+
|
574
|
+
try:
|
575
|
+
while event := self._workflow_event_queue.get_nowait():
|
576
|
+
yield self._emit_event(event)
|
577
|
+
except Empty:
|
578
|
+
pass
|
579
|
+
|
580
|
+
if not is_terminal_event(event):
|
581
|
+
yield self._reject_workflow_event(
|
582
|
+
VellumError(
|
583
|
+
code=VellumErrorCode.INTERNAL_ERROR,
|
584
|
+
message="An unexpected error occurred while streaming Workflow events",
|
585
|
+
)
|
586
|
+
)
|
587
|
+
|
588
|
+
self._background_thread_queue.put(None)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"""Only intenral types and enums for WorkflowRunner should be defined in this module."""
|
2
|
+
|
3
|
+
from dataclasses import dataclass
|
4
|
+
from typing import TYPE_CHECKING, Generic, Iterable, Optional
|
5
|
+
|
6
|
+
from vellum.workflows.types.generics import StateType
|
7
|
+
|
8
|
+
if TYPE_CHECKING:
|
9
|
+
from vellum.workflows.events import NodeEvent
|
10
|
+
from vellum.workflows.nodes.bases import BaseNode
|
11
|
+
from vellum.workflows.ports import Port
|
12
|
+
|
13
|
+
|
14
|
+
@dataclass(frozen=True)
|
15
|
+
class WorkItemEvent(Generic[StateType]):
|
16
|
+
node: "BaseNode[StateType]"
|
17
|
+
event: "NodeEvent"
|
18
|
+
invoked_ports: Optional[Iterable["Port"]] = None
|