flow_nodes 0.1.0
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.
- checksums.yaml +7 -0
- data/.qlty.yml +40 -0
- data/.rspec +3 -0
- data/.rubocop.yml +53 -0
- data/CHANGELOG.md +59 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +315 -0
- data/Rakefile +12 -0
- data/examples/advanced_workflow.rb +299 -0
- data/examples/batch_processing.rb +108 -0
- data/examples/chatbot.rb +91 -0
- data/examples/llm_calendar_parser.rb +429 -0
- data/examples/llm_content_processor.rb +603 -0
- data/examples/llm_document_analyzer.rb +276 -0
- data/examples/simple_llm_example.rb +166 -0
- data/examples/workflow.rb +158 -0
- data/lib/flow_nodes/async_batch_flow.rb +16 -0
- data/lib/flow_nodes/async_batch_node.rb +15 -0
- data/lib/flow_nodes/async_flow.rb +49 -0
- data/lib/flow_nodes/async_node.rb +48 -0
- data/lib/flow_nodes/async_parallel_batch_flow.rb +17 -0
- data/lib/flow_nodes/async_parallel_batch_node.rb +18 -0
- data/lib/flow_nodes/base_node.rb +117 -0
- data/lib/flow_nodes/batch_flow.rb +16 -0
- data/lib/flow_nodes/batch_node.rb +15 -0
- data/lib/flow_nodes/conditional_transition.rb +17 -0
- data/lib/flow_nodes/flow.rb +65 -0
- data/lib/flow_nodes/node.rb +54 -0
- data/lib/flow_nodes/version.rb +5 -0
- data/lib/flow_nodes.rb +20 -0
- data/sig/flow_nodes.rbs +4 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1d43ed2ccf1e7333f3e4f7f0f61b12c35adeeb020bfc4af99c52e8f3b37a93e0
|
4
|
+
data.tar.gz: 69d609ccf71175c6a67eb92c70757bf6c122c76b2738eb25eeedb5289e3be6ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1c5df694422c2337a346c54595ad1694247f6756c59c7d2352dd7e44c68f01039b214d626100b83f7a372184b11380abd2fe329830ef8528727921c169b583c2
|
7
|
+
data.tar.gz: 5f8cd50d2ea00c2bbcf735851c3aa4cfc5a79b71afa1870cec1a06de7d9ebefb726e2e8f3769d9b235c3a4750710231e1fcd0760ac2a0337e0cf64ff62bbe38a
|
data/.qlty.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Qlty configuration for FlowNodes
|
2
|
+
version: 1
|
3
|
+
|
4
|
+
# Coverage configuration
|
5
|
+
coverage:
|
6
|
+
exclude_paths:
|
7
|
+
- "spec/**/*"
|
8
|
+
- "examples/**/*"
|
9
|
+
- "vendor/**/*"
|
10
|
+
- "tmp/**/*"
|
11
|
+
- "bin/**/*"
|
12
|
+
|
13
|
+
# Minimum coverage thresholds
|
14
|
+
thresholds:
|
15
|
+
total: 90
|
16
|
+
changed: 80
|
17
|
+
|
18
|
+
# Code quality tools
|
19
|
+
tools:
|
20
|
+
rubocop:
|
21
|
+
enabled: true
|
22
|
+
config: .rubocop.yml
|
23
|
+
|
24
|
+
reek:
|
25
|
+
enabled: false # Disable reek for now
|
26
|
+
|
27
|
+
bundler_audit:
|
28
|
+
enabled: true
|
29
|
+
|
30
|
+
# Issue tracking
|
31
|
+
issues:
|
32
|
+
exclude_paths:
|
33
|
+
- "spec/**/*"
|
34
|
+
- "examples/**/*"
|
35
|
+
- "vendor/**/*"
|
36
|
+
|
37
|
+
# Formatting
|
38
|
+
formatting:
|
39
|
+
exclude_paths:
|
40
|
+
- "examples/**/*" # Allow examples to be more relaxed
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# RuboCop configuration for FlowNodes
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
NewCops: enable
|
5
|
+
TargetRubyVersion: 3.1
|
6
|
+
SuggestExtensions: false
|
7
|
+
Exclude:
|
8
|
+
- 'vendor/**/*'
|
9
|
+
- 'tmp/**/*'
|
10
|
+
- 'bin/**/*'
|
11
|
+
- 'examples/**/*'
|
12
|
+
|
13
|
+
# Layout
|
14
|
+
Layout/LineLength:
|
15
|
+
Max: 120
|
16
|
+
Exclude:
|
17
|
+
- 'spec/**/*'
|
18
|
+
|
19
|
+
# Style
|
20
|
+
Style/Documentation:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/StringLiterals:
|
24
|
+
EnforcedStyle: double_quotes
|
25
|
+
|
26
|
+
Style/HashSyntax:
|
27
|
+
EnforcedStyle: ruby19
|
28
|
+
|
29
|
+
Style/TrailingCommaInArrayLiteral:
|
30
|
+
EnforcedStyleForMultiline: consistent_comma
|
31
|
+
|
32
|
+
Style/TrailingCommaInHashLiteral:
|
33
|
+
EnforcedStyleForMultiline: consistent_comma
|
34
|
+
|
35
|
+
# Metrics
|
36
|
+
Metrics/ClassLength:
|
37
|
+
Max: 150
|
38
|
+
|
39
|
+
Metrics/MethodLength:
|
40
|
+
Max: 20
|
41
|
+
Exclude:
|
42
|
+
- 'spec/**/*'
|
43
|
+
|
44
|
+
Metrics/BlockLength:
|
45
|
+
Max: 30
|
46
|
+
Exclude:
|
47
|
+
- 'spec/**/*'
|
48
|
+
- 'Rakefile'
|
49
|
+
- '*.gemspec'
|
50
|
+
|
51
|
+
# Naming
|
52
|
+
Naming/MethodParameterName:
|
53
|
+
MinNameLength: 1
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
### Added
|
11
|
+
- Initial release of FlowNodes gem
|
12
|
+
- Core node and flow architecture
|
13
|
+
- BaseNode class with parameter management and connections
|
14
|
+
- Node class with retry logic and lifecycle hooks
|
15
|
+
- Flow class for orchestrating node execution
|
16
|
+
- BatchNode for sequential batch processing
|
17
|
+
- AsyncNode for asynchronous execution
|
18
|
+
- AsyncBatchNode for async sequential batch processing
|
19
|
+
- AsyncParallelBatchNode for parallel batch processing
|
20
|
+
- AsyncFlow for orchestrating async and sync nodes
|
21
|
+
- AsyncBatchFlow for async batch processing
|
22
|
+
- AsyncParallelBatchFlow for parallel batch processing
|
23
|
+
- ConditionalTransition for conditional flow routing
|
24
|
+
- Comprehensive test suite with 181 tests
|
25
|
+
- Thread-safe parameter isolation with deep copying
|
26
|
+
- Retry mechanisms with customizable fallbacks
|
27
|
+
- Lifecycle hooks (prep, exec, post)
|
28
|
+
- Examples directory with practical use cases
|
29
|
+
- GitHub Actions CI/CD workflow
|
30
|
+
- RuboCop configuration for code quality
|
31
|
+
- SimpleCov for code coverage tracking
|
32
|
+
- Qlty integration for code quality and coverage monitoring
|
33
|
+
- YARD documentation support
|
34
|
+
- Comprehensive README with usage examples
|
35
|
+
|
36
|
+
### Changed
|
37
|
+
- Refactored monolithic structure to use individual class files
|
38
|
+
- Fixed parameter passing issues in async execution
|
39
|
+
- Improved array handling in batch processing
|
40
|
+
- Enhanced error handling in async flows
|
41
|
+
- Optimized performance for I/O-bound tasks
|
42
|
+
|
43
|
+
### Fixed
|
44
|
+
- Parameter flow through prep() → _run() → _orch() → _exec() → exec()
|
45
|
+
- Array handling to prevent hash-to-array conversion issues
|
46
|
+
- Post hook parameter passing in async operations
|
47
|
+
- Conditional flow routing in async contexts
|
48
|
+
- Thread safety issues in parallel processing
|
49
|
+
- Retry logic consistency across all node types
|
50
|
+
|
51
|
+
## [0.1.0] - 2024-01-XX
|
52
|
+
|
53
|
+
### Added
|
54
|
+
- Initial release with core FlowNodes framework
|
55
|
+
- Ruby port of PocketFlow's minimalist LLM architecture
|
56
|
+
- Production-ready architecture with clean separation of concerns
|
57
|
+
- Support for Ruby 3.1+ with proper dependency management
|
58
|
+
- MIT license for open source usage
|
59
|
+
- Full compatibility with PocketFlow's design patterns and philosophy
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 RJ Robinson
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,315 @@
|
|
1
|
+
# FlowNodes
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/flow_nodes)
|
4
|
+
[](https://github.com/rjrobinson/flow_nodes/actions)
|
5
|
+
[](https://qlty.sh/)
|
6
|
+
[](https://github.com/rubocop/rubocop)
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
8
|
+
|
9
|
+
**A Ruby port of the minimalist LLM framework, [PocketFlow](https://github.com/The-Pocket/PocketFlow).**
|
10
|
+
|
11
|
+
FlowNodes is a Ruby gem that brings the lightweight, expressive power of [PocketFlow](https://github.com/The-Pocket/PocketFlow) to the Ruby ecosystem. It provides a minimal, graph-based core for building powerful LLM applications like Agents, Workflows, and RAG, without the bloat of larger frameworks.
|
12
|
+
|
13
|
+
## Design Philosophy
|
14
|
+
|
15
|
+
FlowNodes is inspired by and based on [PocketFlow](https://github.com/The-Pocket/PocketFlow), a Python framework created by [The Pocket](https://github.com/The-Pocket). We've adapted PocketFlow's elegant, minimalist approach to LLM application development for the Ruby ecosystem.
|
16
|
+
|
17
|
+
**Core principles:**
|
18
|
+
- **Minimalist Design**: Core functionality in under 500 lines of code
|
19
|
+
- **Graph-based Architecture**: Connect nodes to create complex workflows
|
20
|
+
- **LLM-First**: Built specifically for Large Language Model applications
|
21
|
+
- **Extensible**: Easy to extend with custom nodes and flows
|
22
|
+
|
23
|
+
FlowNodes maintains the same philosophy and API patterns as PocketFlow while providing a native Ruby experience. This ensures Ruby developers can leverage the proven design patterns that make PocketFlow so effective.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'flow_nodes'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
```bash
|
35
|
+
$ bundle install
|
36
|
+
```
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
```bash
|
40
|
+
$ gem install flow_nodes
|
41
|
+
```
|
42
|
+
|
43
|
+
## Features
|
44
|
+
|
45
|
+
- **Minimal & Lightweight**: Core framework in under 500 lines of code
|
46
|
+
- **Graph-based**: Build complex workflows with simple node connections
|
47
|
+
- **Async Support**: Built-in async and parallel processing capabilities
|
48
|
+
- **Batch Processing**: Sequential and parallel batch operations
|
49
|
+
- **Retry Logic**: Built-in retry mechanisms with customizable fallbacks
|
50
|
+
- **Thread Safety**: Proper isolation and deep copying for concurrent execution
|
51
|
+
- **Extensible**: Easy to extend with custom nodes and flows
|
52
|
+
- **Production Ready**: Comprehensive test coverage and clean architecture
|
53
|
+
|
54
|
+
## Quick Start
|
55
|
+
|
56
|
+
### Basic Node and Flow
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
require 'flow_nodes'
|
60
|
+
|
61
|
+
class GreetingNode < FlowNodes::Node
|
62
|
+
def exec(params)
|
63
|
+
puts "Hello, #{params[:name]}!"
|
64
|
+
"greeted"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class FarewellNode < FlowNodes::Node
|
69
|
+
def exec(params)
|
70
|
+
puts "Goodbye, #{params[:name]}!"
|
71
|
+
nil # End the flow
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Create nodes
|
76
|
+
greeting = GreetingNode.new
|
77
|
+
farewell = FarewellNode.new
|
78
|
+
|
79
|
+
# Connect nodes: greeting -> farewell
|
80
|
+
greeting - :greeted >> farewell
|
81
|
+
|
82
|
+
# Create and run flow
|
83
|
+
flow = FlowNodes::Flow.new(start: greeting)
|
84
|
+
flow.set_params(name: "World")
|
85
|
+
flow.run(nil)
|
86
|
+
```
|
87
|
+
|
88
|
+
### Conditional Flows
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class ValidationNode < FlowNodes::Node
|
92
|
+
def exec(params)
|
93
|
+
params[:email]&.include?("@") ? "valid" : "invalid"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class ProcessNode < FlowNodes::Node
|
98
|
+
def exec(params)
|
99
|
+
puts "Processing #{params[:email]}"
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class ErrorNode < FlowNodes::Node
|
105
|
+
def exec(params)
|
106
|
+
puts "Error: Invalid email #{params[:email]}"
|
107
|
+
nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
validator = ValidationNode.new
|
112
|
+
processor = ProcessNode.new
|
113
|
+
error_handler = ErrorNode.new
|
114
|
+
|
115
|
+
# Branch based on validation result
|
116
|
+
validator - :valid >> processor
|
117
|
+
validator - :invalid >> error_handler
|
118
|
+
|
119
|
+
flow = FlowNodes::Flow.new(start: validator)
|
120
|
+
flow.set_params(email: "user@example.com")
|
121
|
+
flow.run(nil)
|
122
|
+
```
|
123
|
+
|
124
|
+
### Batch Processing
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
class DataProcessor < FlowNodes::BatchNode
|
128
|
+
def exec(item)
|
129
|
+
puts "Processing: #{item}"
|
130
|
+
item.upcase
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
processor = DataProcessor.new
|
135
|
+
processor.set_params(["hello", "world", "ruby"])
|
136
|
+
results = processor.run(nil)
|
137
|
+
# => ["HELLO", "WORLD", "RUBY"]
|
138
|
+
```
|
139
|
+
|
140
|
+
### Async and Parallel Processing
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
class AsyncProcessor < FlowNodes::AsyncParallelBatchNode
|
144
|
+
def exec_async(item)
|
145
|
+
puts "Processing #{item} on thread #{Thread.current.object_id}"
|
146
|
+
sleep(0.1) # Simulate I/O
|
147
|
+
item.upcase
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
processor = AsyncProcessor.new
|
152
|
+
processor.set_params(["hello", "world", "ruby"])
|
153
|
+
results = processor.run_async(nil)
|
154
|
+
# Processes all items in parallel
|
155
|
+
```
|
156
|
+
|
157
|
+
### Lifecycle Hooks
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
class LoggingNode < FlowNodes::Node
|
161
|
+
def prep(state)
|
162
|
+
puts "Preparing to process"
|
163
|
+
{ prepared_at: Time.now }
|
164
|
+
end
|
165
|
+
|
166
|
+
def exec(params)
|
167
|
+
puts "Processing: #{params}"
|
168
|
+
"success"
|
169
|
+
end
|
170
|
+
|
171
|
+
def post(state, params, result)
|
172
|
+
puts "Completed with result: #{result}"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
### Error Handling and Retries
|
178
|
+
|
179
|
+
```ruby
|
180
|
+
class RetryNode < FlowNodes::Node
|
181
|
+
def initialize
|
182
|
+
super(max_retries: 3, wait: 1)
|
183
|
+
end
|
184
|
+
|
185
|
+
def exec(params)
|
186
|
+
# Simulate unreliable operation
|
187
|
+
raise "Network error" if rand < 0.7
|
188
|
+
"success"
|
189
|
+
end
|
190
|
+
|
191
|
+
def exec_fallback(params, exception)
|
192
|
+
puts "All retries failed: #{exception.message}"
|
193
|
+
"failed"
|
194
|
+
end
|
195
|
+
end
|
196
|
+
```
|
197
|
+
|
198
|
+
## Architecture
|
199
|
+
|
200
|
+
FlowNodes is built around several core classes:
|
201
|
+
|
202
|
+
- **`BaseNode`**: Foundation class with parameter management and connections
|
203
|
+
- **`Node`**: Adds retry logic and lifecycle hooks
|
204
|
+
- **`Flow`**: Orchestrates node execution and state management
|
205
|
+
- **`BatchNode`**: Processes arrays of items sequentially
|
206
|
+
- **`AsyncNode`**: Enables asynchronous execution
|
207
|
+
- **`AsyncBatchNode`**: Async sequential batch processing
|
208
|
+
- **`AsyncParallelBatchNode`**: Async parallel batch processing
|
209
|
+
- **`AsyncFlow`**: Orchestrates async and sync nodes together
|
210
|
+
|
211
|
+
## Examples
|
212
|
+
|
213
|
+
See the [`examples/`](examples/) directory for complete examples:
|
214
|
+
|
215
|
+
- [`examples/chatbot.rb`](examples/chatbot.rb) - Interactive chatbot with self-loops
|
216
|
+
- [`examples/workflow.rb`](examples/workflow.rb) - Data validation and processing workflow
|
217
|
+
- [`examples/batch_processing.rb`](examples/batch_processing.rb) - Sequential and parallel batch operations
|
218
|
+
|
219
|
+
## Development
|
220
|
+
|
221
|
+
After checking out the repo, run `bundle install` to install dependencies.
|
222
|
+
|
223
|
+
### Running Tests
|
224
|
+
|
225
|
+
```bash
|
226
|
+
bundle exec rspec
|
227
|
+
```
|
228
|
+
|
229
|
+
### Code Quality
|
230
|
+
|
231
|
+
```bash
|
232
|
+
# Run RuboCop for style checking
|
233
|
+
bundle exec rubocop
|
234
|
+
|
235
|
+
# Auto-fix issues
|
236
|
+
bundle exec rubocop -a
|
237
|
+
|
238
|
+
# Generate documentation
|
239
|
+
bundle exec yard doc
|
240
|
+
```
|
241
|
+
|
242
|
+
### CI/CD Setup
|
243
|
+
|
244
|
+
The project uses GitHub Actions for CI/CD with the following secrets required:
|
245
|
+
|
246
|
+
- `QLTY_COVERAGE_TOKEN`: Token for Qlty code coverage reporting
|
247
|
+
- `RUBYGEMS_AUTH_TOKEN`: Token for publishing to RubyGems (optional)
|
248
|
+
|
249
|
+
### Running Examples
|
250
|
+
|
251
|
+
```bash
|
252
|
+
# Run the chatbot example
|
253
|
+
ruby examples/chatbot.rb
|
254
|
+
|
255
|
+
# Run the workflow example
|
256
|
+
ruby examples/workflow.rb
|
257
|
+
|
258
|
+
# Run the batch processing example
|
259
|
+
ruby examples/batch_processing.rb
|
260
|
+
```
|
261
|
+
|
262
|
+
## API Reference
|
263
|
+
|
264
|
+
For detailed API documentation, visit the [YARD documentation](https://rubydoc.info/gems/flow_nodes) or generate it locally:
|
265
|
+
|
266
|
+
```bash
|
267
|
+
bundle exec yard doc
|
268
|
+
open doc/index.html
|
269
|
+
```
|
270
|
+
|
271
|
+
## Performance Considerations
|
272
|
+
|
273
|
+
- **Async Operations**: Use Ruby threads, subject to the GVL. Best for I/O-bound tasks.
|
274
|
+
- **Parallel Processing**: `AsyncParallelBatchNode` provides concurrency for I/O operations.
|
275
|
+
- **Memory**: Deep copying ensures thread safety but uses more memory.
|
276
|
+
- **Batch Size**: Consider batch sizes for memory usage vs. processing efficiency.
|
277
|
+
|
278
|
+
## Thread Safety
|
279
|
+
|
280
|
+
FlowNodes is designed to be thread-safe:
|
281
|
+
|
282
|
+
- Parameters are deep-copied using `Marshal` to prevent state bleed
|
283
|
+
- Each flow execution operates on isolated node instances
|
284
|
+
- Shared state objects should be managed carefully in multi-threaded environments
|
285
|
+
|
286
|
+
## Contributing
|
287
|
+
|
288
|
+
1. Fork the repository
|
289
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
290
|
+
3. Make your changes
|
291
|
+
4. Add tests for your changes
|
292
|
+
5. Ensure all tests pass (`bundle exec rspec`)
|
293
|
+
6. Run RuboCop (`bundle exec rubocop`)
|
294
|
+
7. Commit your changes (`git commit -am 'Add amazing feature'`)
|
295
|
+
8. Push to the branch (`git push origin feature/amazing-feature`)
|
296
|
+
9. Open a Pull Request
|
297
|
+
|
298
|
+
### Code of Conduct
|
299
|
+
|
300
|
+
This project follows the [Contributor Covenant](https://www.contributor-covenant.org/) code of conduct.
|
301
|
+
|
302
|
+
## Acknowledgments
|
303
|
+
|
304
|
+
This project is a Ruby port of the excellent [PocketFlow](https://github.com/The-Pocket/PocketFlow) Python framework created by [The Pocket](https://github.com/The-Pocket).
|
305
|
+
|
306
|
+
**Special thanks to:**
|
307
|
+
- The original PocketFlow team for pioneering the minimalist LLM framework approach
|
308
|
+
- The Python community for inspiring clean, expressive API design
|
309
|
+
- The Ruby community for providing excellent tools and libraries that made this port possible
|
310
|
+
|
311
|
+
FlowNodes would not exist without the groundbreaking work of the PocketFlow team. We encourage users to also check out the [original Python PocketFlow](https://github.com/The-Pocket/PocketFlow) and its excellent [documentation](https://the-pocket.github.io/PocketFlow/).
|
312
|
+
|
313
|
+
## License
|
314
|
+
|
315
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|