Class: Stellwerk::Flow
- Inherits:
-
Object
- Object
- Stellwerk::Flow
- Defined in:
- lib/stellwerk/flow.rb
Overview
Convenience wrapper for loading and executing compiled flows
Instance Attribute Summary collapse
-
#compiled_json ⇒ Object
readonly
Returns the value of attribute compiled_json.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.load(path) ⇒ Stellwerk::Flow
Load a flow from a JSON file.
Instance Method Summary collapse
-
#compiled_at ⇒ Object
Get compilation timestamp.
-
#entry_node_ids ⇒ Object
Get entry node IDs.
-
#execute(params = {}, sub_flows: {}, metadata: {}) ⇒ Stellwerk::Result
Execute the flow with the given parameters.
-
#initialize(compiled_json, path: nil) ⇒ Flow
constructor
Create a flow from a parsed JSON hash.
-
#node_ids ⇒ Object
Get all node IDs.
-
#version ⇒ Object
Get the flow version from compiled JSON.
Constructor Details
#initialize(compiled_json, path: nil) ⇒ Flow
Create a flow from a parsed JSON hash
32 33 34 35 36 |
# File 'lib/stellwerk/flow.rb', line 32 def initialize(compiled_json, path: nil) @compiled_json = compiled_json @path = path validate! end |
Instance Attribute Details
#compiled_json ⇒ Object (readonly)
Returns the value of attribute compiled_json.
8 9 10 |
# File 'lib/stellwerk/flow.rb', line 8 def compiled_json @compiled_json end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/stellwerk/flow.rb', line 8 def path @path end |
Class Method Details
.load(path) ⇒ Stellwerk::Flow
Load a flow from a JSON file
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/stellwerk/flow.rb', line 15 def self.load(path) unless File.exist?(path) raise InvalidFlowError, "Flow file not found: #{path}" end content = File.read(path) json = JSON.parse(content) new(json, path: path) rescue JSON::ParserError => e raise InvalidFlowError, "Invalid JSON in flow file: #{e.}" end |
Instance Method Details
#compiled_at ⇒ Object
Get compilation timestamp
78 79 80 |
# File 'lib/stellwerk/flow.rb', line 78 def compiled_at @compiled_json["compiled_at"] || @compiled_json[:compiled_at] end |
#entry_node_ids ⇒ Object
Get entry node IDs
83 84 85 |
# File 'lib/stellwerk/flow.rb', line 83 def entry_node_ids @compiled_json["entry_node_ids"] || @compiled_json[:entry_node_ids] || [] end |
#execute(params = {}, sub_flows: {}, metadata: {}) ⇒ Stellwerk::Result
Execute the flow with the given parameters
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stellwerk/flow.rb', line 46 def execute(params = {}, sub_flows: {}, metadata: {}) enforce_license! check_quota! start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) status = "success" begin result = Evaluator.call( compiled_json: @compiled_json, params: params, sub_flows: sub_flows, metadata: ) status = "error" unless result.success? result rescue StandardError status = "error" raise ensure duration_ms = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - start_time track_execution(duration_ms, status) end end |
#node_ids ⇒ Object
Get all node IDs
88 89 90 91 |
# File 'lib/stellwerk/flow.rb', line 88 def node_ids nodes = @compiled_json["nodes"] || @compiled_json[:nodes] || {} nodes.keys end |
#version ⇒ Object
Get the flow version from compiled JSON
73 74 75 |
# File 'lib/stellwerk/flow.rb', line 73 def version @compiled_json["version"] || @compiled_json[:version] end |