claude_swarm 1.0.1 → 1.0.4
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 +4 -4
- data/.claude/commands/release.md +1 -1
- data/.claude/hooks/lint-code-files.rb +65 -0
- data/.rubocop.yml +22 -2
- data/CHANGELOG.md +14 -1
- data/CLAUDE.md +1 -1
- data/CONTRIBUTING.md +69 -0
- data/README.md +27 -2
- data/Rakefile +71 -3
- data/analyze_coverage.rb +94 -0
- data/docs/v2/CHANGELOG.swarm_cli.md +43 -0
- data/docs/v2/CHANGELOG.swarm_memory.md +379 -0
- data/docs/v2/CHANGELOG.swarm_sdk.md +362 -0
- data/docs/v2/README.md +308 -0
- data/docs/v2/guides/claude-code-agents.md +262 -0
- data/docs/v2/guides/complete-tutorial.md +3088 -0
- data/docs/v2/guides/getting-started.md +1456 -0
- data/docs/v2/guides/memory-adapters.md +998 -0
- data/docs/v2/guides/plugins.md +816 -0
- data/docs/v2/guides/quick-start-cli.md +1745 -0
- data/docs/v2/guides/rails-integration.md +1902 -0
- data/docs/v2/guides/swarm-memory.md +599 -0
- data/docs/v2/reference/cli.md +729 -0
- data/docs/v2/reference/ruby-dsl.md +2154 -0
- data/docs/v2/reference/yaml.md +1835 -0
- data/docs-team-swarm.yml +2222 -0
- data/examples/learning-assistant/assistant.md +7 -0
- data/examples/learning-assistant/example-memories/concept-example.md +90 -0
- data/examples/learning-assistant/example-memories/experience-example.md +66 -0
- data/examples/learning-assistant/example-memories/fact-example.md +76 -0
- data/examples/learning-assistant/example-memories/memory-index.md +78 -0
- data/examples/learning-assistant/example-memories/skill-example.md +168 -0
- data/examples/learning-assistant/learning_assistant.rb +34 -0
- data/examples/learning-assistant/learning_assistant.yml +20 -0
- data/examples/v2/dsl/01_basic.rb +44 -0
- data/examples/v2/dsl/02_core_parameters.rb +59 -0
- data/examples/v2/dsl/03_capabilities.rb +71 -0
- data/examples/v2/dsl/04_llm_parameters.rb +56 -0
- data/examples/v2/dsl/05_advanced_flags.rb +73 -0
- data/examples/v2/dsl/06_permissions.rb +80 -0
- data/examples/v2/dsl/07_mcp_server.rb +62 -0
- data/examples/v2/dsl/08_swarm_hooks.rb +53 -0
- data/examples/v2/dsl/09_agent_hooks.rb +67 -0
- data/examples/v2/dsl/10_all_agents_hooks.rb +67 -0
- data/examples/v2/dsl/11_delegation.rb +60 -0
- data/examples/v2/dsl/12_complete_integration.rb +137 -0
- data/examples/v2/file_tools_swarm.yml +102 -0
- data/examples/v2/hooks/01_basic_hooks.rb +133 -0
- data/examples/v2/hooks/02_usage_tracking.rb +201 -0
- data/examples/v2/hooks/03_production_monitoring.rb +429 -0
- data/examples/v2/hooks/agent_stop_exit_0.yml +21 -0
- data/examples/v2/hooks/agent_stop_exit_1.yml +21 -0
- data/examples/v2/hooks/agent_stop_exit_2.yml +26 -0
- data/examples/v2/hooks/multiple_hooks_all_pass.yml +37 -0
- data/examples/v2/hooks/multiple_hooks_first_fails.yml +37 -0
- data/examples/v2/hooks/multiple_hooks_second_fails.yml +37 -0
- data/examples/v2/hooks/multiple_hooks_warnings.yml +37 -0
- data/examples/v2/hooks/post_tool_use_exit_0.yml +24 -0
- data/examples/v2/hooks/post_tool_use_exit_1.yml +24 -0
- data/examples/v2/hooks/post_tool_use_exit_2.yml +24 -0
- data/examples/v2/hooks/post_tool_use_multi_matcher_exit_0.yml +26 -0
- data/examples/v2/hooks/post_tool_use_multi_matcher_exit_1.yml +26 -0
- data/examples/v2/hooks/post_tool_use_multi_matcher_exit_2.yml +26 -0
- data/examples/v2/hooks/pre_tool_use_exit_0.yml +24 -0
- data/examples/v2/hooks/pre_tool_use_exit_1.yml +24 -0
- data/examples/v2/hooks/pre_tool_use_exit_2.yml +24 -0
- data/examples/v2/hooks/pre_tool_use_multi_matcher_exit_0.yml +26 -0
- data/examples/v2/hooks/pre_tool_use_multi_matcher_exit_1.yml +26 -0
- data/examples/v2/hooks/pre_tool_use_multi_matcher_exit_2.yml +27 -0
- data/examples/v2/hooks/swarm_summary.sh +44 -0
- data/examples/v2/hooks/user_prompt_exit_0.yml +21 -0
- data/examples/v2/hooks/user_prompt_exit_1.yml +21 -0
- data/examples/v2/hooks/user_prompt_exit_2.yml +21 -0
- data/examples/v2/hooks/validate_bash.rb +59 -0
- data/examples/v2/multi_directory_permissions.yml +221 -0
- data/examples/v2/node_context_demo.rb +127 -0
- data/examples/v2/node_workflow.rb +173 -0
- data/examples/v2/path_resolution_demo.rb +216 -0
- data/examples/v2/simple-swarm-v2.rb +90 -0
- data/examples/v2/simple-swarm-v2.yml +62 -0
- data/examples/v2/swarm.yml +71 -0
- data/examples/v2/swarm_with_hooks.yml +61 -0
- data/examples/v2/swarm_with_hooks_simple.yml +25 -0
- data/examples/v2/think_tool_demo.rb +62 -0
- data/exe/swarm +6 -0
- data/lib/claude_swarm/claude_mcp_server.rb +0 -6
- data/lib/claude_swarm/cli.rb +10 -3
- data/lib/claude_swarm/commands/ps.rb +19 -20
- data/lib/claude_swarm/commands/show.rb +1 -1
- data/lib/claude_swarm/configuration.rb +10 -12
- data/lib/claude_swarm/mcp_generator.rb +10 -1
- data/lib/claude_swarm/orchestrator.rb +73 -49
- data/lib/claude_swarm/system_utils.rb +37 -11
- data/lib/claude_swarm/version.rb +1 -1
- data/lib/claude_swarm/worktree_manager.rb +1 -0
- data/lib/claude_swarm/yaml_loader.rb +22 -0
- data/lib/claude_swarm.rb +7 -2
- data/lib/swarm_cli/cli.rb +201 -0
- data/lib/swarm_cli/command_registry.rb +61 -0
- data/lib/swarm_cli/commands/mcp_serve.rb +130 -0
- data/lib/swarm_cli/commands/mcp_tools.rb +148 -0
- data/lib/swarm_cli/commands/migrate.rb +55 -0
- data/lib/swarm_cli/commands/run.rb +173 -0
- data/lib/swarm_cli/config_loader.rb +97 -0
- data/lib/swarm_cli/formatters/human_formatter.rb +711 -0
- data/lib/swarm_cli/formatters/json_formatter.rb +51 -0
- data/lib/swarm_cli/interactive_repl.rb +918 -0
- data/lib/swarm_cli/mcp_serve_options.rb +44 -0
- data/lib/swarm_cli/mcp_tools_options.rb +59 -0
- data/lib/swarm_cli/migrate_options.rb +54 -0
- data/lib/swarm_cli/migrator.rb +132 -0
- data/lib/swarm_cli/options.rb +151 -0
- data/lib/swarm_cli/ui/components/agent_badge.rb +33 -0
- data/lib/swarm_cli/ui/components/content_block.rb +120 -0
- data/lib/swarm_cli/ui/components/divider.rb +57 -0
- data/lib/swarm_cli/ui/components/panel.rb +62 -0
- data/lib/swarm_cli/ui/components/usage_stats.rb +70 -0
- data/lib/swarm_cli/ui/formatters/cost.rb +49 -0
- data/lib/swarm_cli/ui/formatters/number.rb +58 -0
- data/lib/swarm_cli/ui/formatters/text.rb +77 -0
- data/lib/swarm_cli/ui/formatters/time.rb +73 -0
- data/lib/swarm_cli/ui/icons.rb +59 -0
- data/lib/swarm_cli/ui/renderers/event_renderer.rb +188 -0
- data/lib/swarm_cli/ui/state/agent_color_cache.rb +45 -0
- data/lib/swarm_cli/ui/state/depth_tracker.rb +40 -0
- data/lib/swarm_cli/ui/state/spinner_manager.rb +170 -0
- data/lib/swarm_cli/ui/state/usage_tracker.rb +62 -0
- data/lib/swarm_cli/version.rb +5 -0
- data/lib/swarm_cli.rb +44 -0
- data/lib/swarm_memory/adapters/base.rb +141 -0
- data/lib/swarm_memory/adapters/filesystem_adapter.rb +845 -0
- data/lib/swarm_memory/chat_extension.rb +34 -0
- data/lib/swarm_memory/cli/commands.rb +306 -0
- data/lib/swarm_memory/core/entry.rb +37 -0
- data/lib/swarm_memory/core/frontmatter_parser.rb +108 -0
- data/lib/swarm_memory/core/metadata_extractor.rb +68 -0
- data/lib/swarm_memory/core/path_normalizer.rb +75 -0
- data/lib/swarm_memory/core/semantic_index.rb +244 -0
- data/lib/swarm_memory/core/storage.rb +288 -0
- data/lib/swarm_memory/core/storage_read_tracker.rb +63 -0
- data/lib/swarm_memory/dsl/builder_extension.rb +40 -0
- data/lib/swarm_memory/dsl/memory_config.rb +113 -0
- data/lib/swarm_memory/embeddings/embedder.rb +36 -0
- data/lib/swarm_memory/embeddings/informers_embedder.rb +152 -0
- data/lib/swarm_memory/errors.rb +21 -0
- data/lib/swarm_memory/integration/cli_registration.rb +30 -0
- data/lib/swarm_memory/integration/configuration.rb +43 -0
- data/lib/swarm_memory/integration/registration.rb +31 -0
- data/lib/swarm_memory/integration/sdk_plugin.rb +531 -0
- data/lib/swarm_memory/optimization/analyzer.rb +244 -0
- data/lib/swarm_memory/optimization/defragmenter.rb +863 -0
- data/lib/swarm_memory/prompts/memory.md.erb +109 -0
- data/lib/swarm_memory/prompts/memory_assistant.md.erb +181 -0
- data/lib/swarm_memory/prompts/memory_researcher.md.erb +281 -0
- data/lib/swarm_memory/prompts/memory_retrieval.md.erb +78 -0
- data/lib/swarm_memory/search/semantic_search.rb +112 -0
- data/lib/swarm_memory/search/text_search.rb +42 -0
- data/lib/swarm_memory/search/text_similarity.rb +80 -0
- data/lib/swarm_memory/skills/meta/deep-learning.md +101 -0
- data/lib/swarm_memory/skills/meta/deep-learning.yml +14 -0
- data/lib/swarm_memory/tools/load_skill.rb +313 -0
- data/lib/swarm_memory/tools/memory_defrag.rb +382 -0
- data/lib/swarm_memory/tools/memory_delete.rb +99 -0
- data/lib/swarm_memory/tools/memory_edit.rb +185 -0
- data/lib/swarm_memory/tools/memory_glob.rb +160 -0
- data/lib/swarm_memory/tools/memory_grep.rb +247 -0
- data/lib/swarm_memory/tools/memory_multi_edit.rb +281 -0
- data/lib/swarm_memory/tools/memory_read.rb +123 -0
- data/lib/swarm_memory/tools/memory_write.rb +231 -0
- data/lib/swarm_memory/utils.rb +50 -0
- data/lib/swarm_memory/version.rb +5 -0
- data/lib/swarm_memory.rb +166 -0
- data/lib/swarm_sdk/agent/RETRY_LOGIC.md +127 -0
- data/lib/swarm_sdk/agent/builder.rb +461 -0
- data/lib/swarm_sdk/agent/chat/context_tracker.rb +314 -0
- data/lib/swarm_sdk/agent/chat/hook_integration.rb +372 -0
- data/lib/swarm_sdk/agent/chat/logging_helpers.rb +116 -0
- data/lib/swarm_sdk/agent/chat/system_reminder_injector.rb +152 -0
- data/lib/swarm_sdk/agent/chat.rb +1159 -0
- data/lib/swarm_sdk/agent/context.rb +112 -0
- data/lib/swarm_sdk/agent/context_manager.rb +309 -0
- data/lib/swarm_sdk/agent/definition.rb +556 -0
- data/lib/swarm_sdk/claude_code_agent_adapter.rb +205 -0
- data/lib/swarm_sdk/configuration.rb +296 -0
- data/lib/swarm_sdk/context_compactor/metrics.rb +147 -0
- data/lib/swarm_sdk/context_compactor/token_counter.rb +106 -0
- data/lib/swarm_sdk/context_compactor.rb +340 -0
- data/lib/swarm_sdk/hooks/adapter.rb +359 -0
- data/lib/swarm_sdk/hooks/context.rb +197 -0
- data/lib/swarm_sdk/hooks/definition.rb +80 -0
- data/lib/swarm_sdk/hooks/error.rb +29 -0
- data/lib/swarm_sdk/hooks/executor.rb +146 -0
- data/lib/swarm_sdk/hooks/registry.rb +147 -0
- data/lib/swarm_sdk/hooks/result.rb +150 -0
- data/lib/swarm_sdk/hooks/shell_executor.rb +254 -0
- data/lib/swarm_sdk/hooks/tool_call.rb +35 -0
- data/lib/swarm_sdk/hooks/tool_result.rb +62 -0
- data/lib/swarm_sdk/log_collector.rb +51 -0
- data/lib/swarm_sdk/log_stream.rb +69 -0
- data/lib/swarm_sdk/markdown_parser.rb +75 -0
- data/lib/swarm_sdk/model_aliases.json +5 -0
- data/lib/swarm_sdk/models.json +1 -0
- data/lib/swarm_sdk/models.rb +120 -0
- data/lib/swarm_sdk/node/agent_config.rb +49 -0
- data/lib/swarm_sdk/node/builder.rb +439 -0
- data/lib/swarm_sdk/node/transformer_executor.rb +248 -0
- data/lib/swarm_sdk/node_context.rb +170 -0
- data/lib/swarm_sdk/node_orchestrator.rb +384 -0
- data/lib/swarm_sdk/permissions/config.rb +239 -0
- data/lib/swarm_sdk/permissions/error_formatter.rb +121 -0
- data/lib/swarm_sdk/permissions/path_matcher.rb +35 -0
- data/lib/swarm_sdk/permissions/validator.rb +173 -0
- data/lib/swarm_sdk/permissions_builder.rb +122 -0
- data/lib/swarm_sdk/plugin.rb +147 -0
- data/lib/swarm_sdk/plugin_registry.rb +101 -0
- data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +243 -0
- data/lib/swarm_sdk/providers/openai_with_responses.rb +582 -0
- data/lib/swarm_sdk/result.rb +97 -0
- data/lib/swarm_sdk/swarm/agent_initializer.rb +334 -0
- data/lib/swarm_sdk/swarm/all_agents_builder.rb +140 -0
- data/lib/swarm_sdk/swarm/builder.rb +586 -0
- data/lib/swarm_sdk/swarm/mcp_configurator.rb +151 -0
- data/lib/swarm_sdk/swarm/tool_configurator.rb +419 -0
- data/lib/swarm_sdk/swarm.rb +982 -0
- data/lib/swarm_sdk/tools/bash.rb +274 -0
- data/lib/swarm_sdk/tools/clock.rb +44 -0
- data/lib/swarm_sdk/tools/delegate.rb +164 -0
- data/lib/swarm_sdk/tools/document_converters/base_converter.rb +83 -0
- data/lib/swarm_sdk/tools/document_converters/docx_converter.rb +99 -0
- data/lib/swarm_sdk/tools/document_converters/html_converter.rb +101 -0
- data/lib/swarm_sdk/tools/document_converters/pdf_converter.rb +78 -0
- data/lib/swarm_sdk/tools/document_converters/xlsx_converter.rb +194 -0
- data/lib/swarm_sdk/tools/edit.rb +150 -0
- data/lib/swarm_sdk/tools/glob.rb +158 -0
- data/lib/swarm_sdk/tools/grep.rb +228 -0
- data/lib/swarm_sdk/tools/image_extractors/docx_image_extractor.rb +43 -0
- data/lib/swarm_sdk/tools/image_extractors/pdf_image_extractor.rb +163 -0
- data/lib/swarm_sdk/tools/image_formats/tiff_builder.rb +65 -0
- data/lib/swarm_sdk/tools/multi_edit.rb +232 -0
- data/lib/swarm_sdk/tools/path_resolver.rb +43 -0
- data/lib/swarm_sdk/tools/read.rb +251 -0
- data/lib/swarm_sdk/tools/registry.rb +93 -0
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_list.rb +96 -0
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_read.rb +76 -0
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_write.rb +91 -0
- data/lib/swarm_sdk/tools/stores/read_tracker.rb +61 -0
- data/lib/swarm_sdk/tools/stores/scratchpad_storage.rb +224 -0
- data/lib/swarm_sdk/tools/stores/storage.rb +148 -0
- data/lib/swarm_sdk/tools/stores/todo_manager.rb +65 -0
- data/lib/swarm_sdk/tools/think.rb +95 -0
- data/lib/swarm_sdk/tools/todo_write.rb +216 -0
- data/lib/swarm_sdk/tools/web_fetch.rb +261 -0
- data/lib/swarm_sdk/tools/write.rb +117 -0
- data/lib/swarm_sdk/utils.rb +50 -0
- data/lib/swarm_sdk/version.rb +5 -0
- data/lib/swarm_sdk.rb +157 -0
- data/llm.v2.txt +13407 -0
- data/rubocop/cop/security/no_reflection_methods.rb +47 -0
- data/rubocop/cop/security/no_ruby_llm_logger.rb +32 -0
- data/swarm_cli.gemspec +57 -0
- data/swarm_memory.gemspec +28 -0
- data/swarm_sdk.gemspec +41 -0
- data/team.yml +1 -1
- data/team_full.yml +1875 -0
- data/{team_v2.yml → team_sdk.yml} +121 -52
- metadata +249 -6
- data/EXAMPLES.md +0 -164
|
@@ -0,0 +1,1456 @@
|
|
|
1
|
+
# Getting Started with SwarmSDK
|
|
2
|
+
|
|
3
|
+
## What You'll Learn
|
|
4
|
+
|
|
5
|
+
- What SwarmSDK is and why it's valuable for complex AI tasks
|
|
6
|
+
- How to install and configure SwarmSDK in your Ruby project
|
|
7
|
+
- The fundamental concepts: agents, delegation, tools, and swarms
|
|
8
|
+
- How to create your first agent using YAML, Ruby DSL, and Markdown
|
|
9
|
+
- Essential workflows for building single and multi-agent systems
|
|
10
|
+
- How to interpret results and debug common issues
|
|
11
|
+
|
|
12
|
+
## Prerequisites
|
|
13
|
+
|
|
14
|
+
- **Ruby 3.2.0 or higher** installed on your system
|
|
15
|
+
- **Basic Ruby knowledge** - familiarity with hashes, symbols, and blocks
|
|
16
|
+
- **LLM API access** - an API key for OpenAI, Anthropic, or another provider
|
|
17
|
+
- **15-20 minutes** to complete this guide
|
|
18
|
+
|
|
19
|
+
## What is SwarmSDK?
|
|
20
|
+
|
|
21
|
+
SwarmSDK is a Ruby framework for orchestrating **teams of AI agents** that collaborate to solve complex problems. Unlike traditional single-agent systems, SwarmSDK enables you to build specialized agents that work together, each focusing on what they do best.
|
|
22
|
+
|
|
23
|
+
### Why Multi-Agent Systems?
|
|
24
|
+
|
|
25
|
+
Complex tasks often require different types of expertise. Consider a software development project:
|
|
26
|
+
|
|
27
|
+
- A **planner** agent breaks down requirements into tasks
|
|
28
|
+
- A **coder** agent implements features
|
|
29
|
+
- A **reviewer** agent checks code quality and suggests improvements
|
|
30
|
+
- A **tester** agent validates functionality
|
|
31
|
+
|
|
32
|
+
With SwarmSDK, each agent specializes in one area and delegates work to others when needed. This mirrors how human teams collaborate, leading to better results than a single generalist AI trying to do everything.
|
|
33
|
+
|
|
34
|
+
### Core Benefits
|
|
35
|
+
|
|
36
|
+
**Agent Specialization**: Each agent has its own system prompt, model, tools, and working directory, optimized for specific tasks.
|
|
37
|
+
|
|
38
|
+
**Flexible Delegation**: Agents can delegate work to specialists through simple function calls, creating dynamic collaboration patterns.
|
|
39
|
+
|
|
40
|
+
**Comprehensive Tooling**: Built-in tools for file operations (Read, Write, Edit), code search (Grep, Glob), command execution (Bash), and shared memory (Scratchpad).
|
|
41
|
+
|
|
42
|
+
**Fine-Grained Control**: Permissions system restricts file access, hooks customize behavior at every step, and parameters tune each agent's LLM settings.
|
|
43
|
+
|
|
44
|
+
**Multiple Interfaces**: Define swarms using declarative YAML, fluent Ruby DSL, Markdown agent files, or direct API calls—whichever fits your workflow.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
Add SwarmSDK to your Ruby project's Gemfile:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
gem 'swarm_sdk'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then install:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
bundle install
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Or install directly:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
gem install swarm_sdk
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Configure Your API Key
|
|
67
|
+
|
|
68
|
+
SwarmSDK requires an LLM provider to run. Set your API key as an environment variable:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
# For OpenAI
|
|
72
|
+
export OPENAI_API_KEY="sk-your-key-here"
|
|
73
|
+
|
|
74
|
+
# For Anthropic Claude
|
|
75
|
+
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Tip**: Create a `.env` file in your project root for convenience:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# .env
|
|
82
|
+
OPENAI_API_KEY=sk-your-key-here
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Load it in your Ruby script:
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
require 'dotenv/load'
|
|
89
|
+
require 'swarm_sdk'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Verify Installation
|
|
93
|
+
|
|
94
|
+
Quick test to ensure everything works:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
ruby -e "require 'swarm_sdk'; puts 'SwarmSDK loaded successfully!'"
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Core Concepts
|
|
101
|
+
|
|
102
|
+
Before writing code, let's understand SwarmSDK's architecture.
|
|
103
|
+
|
|
104
|
+
### Swarms
|
|
105
|
+
|
|
106
|
+
A **swarm** is a team of agents working together. Every swarm has:
|
|
107
|
+
- A **name** (for logging and debugging)
|
|
108
|
+
- A **lead agent** (the entry point that receives your tasks)
|
|
109
|
+
- One or more **agents** (the team members)
|
|
110
|
+
|
|
111
|
+
When you call `swarm.execute("task")`, the task goes to the lead agent, which can either handle it directly or delegate to other agents.
|
|
112
|
+
|
|
113
|
+
### Agents
|
|
114
|
+
|
|
115
|
+
An **agent** is an AI with specific capabilities and constraints:
|
|
116
|
+
- **Description**: What the agent does (required)
|
|
117
|
+
- **Model**: Which LLM to use (e.g., "gpt-4", "claude-sonnet-4")
|
|
118
|
+
- **System Prompt**: Instructions defining the agent's behavior and expertise
|
|
119
|
+
- **Tools**: Functions the agent can call (Read, Write, Bash, etc.)
|
|
120
|
+
- **Directory**: The agent's working directory for file operations
|
|
121
|
+
- **Delegates To**: Other agents this agent can hand off work to
|
|
122
|
+
|
|
123
|
+
### Delegation
|
|
124
|
+
|
|
125
|
+
Delegation is how agents collaborate. When an agent is configured with `delegates_to: [other_agent]`, it gains a special delegation tool.
|
|
126
|
+
|
|
127
|
+
**Delegation tool naming**: Tools are named `DelegateTaskTo{AgentName}` where AgentName is capitalized. For example:
|
|
128
|
+
- `delegates_to: [reviewer]` → creates `DelegateTaskToReviewer` tool
|
|
129
|
+
- `delegates_to: [backend]` → creates `DelegateTaskToBackend` tool
|
|
130
|
+
- `delegates_to: [qa_tester]` → creates `DelegateTaskToQa_tester` tool
|
|
131
|
+
|
|
132
|
+
**Example flow**:
|
|
133
|
+
1. You send "Build a login page" to the **lead** agent
|
|
134
|
+
2. Lead delegates to **frontend** agent: "Create the React component"
|
|
135
|
+
3. Frontend delegates to **reviewer** agent: "Check this code"
|
|
136
|
+
4. Reviewer returns feedback to frontend
|
|
137
|
+
5. Frontend returns completed component to lead
|
|
138
|
+
6. Lead returns final result to you
|
|
139
|
+
|
|
140
|
+
Delegation creates flexible collaboration patterns without rigid workflows.
|
|
141
|
+
|
|
142
|
+
### Tools
|
|
143
|
+
|
|
144
|
+
Tools are functions agents call to interact with the world. SwarmSDK provides:
|
|
145
|
+
|
|
146
|
+
| Category | Tools | Purpose |
|
|
147
|
+
|----------|-------|---------|
|
|
148
|
+
| **File I/O** | Read, Write, Edit, MultiEdit | Read and modify files |
|
|
149
|
+
| **Search** | Grep, Glob | Find content and files |
|
|
150
|
+
| **Execution** | Bash | Run shell commands |
|
|
151
|
+
| **Web** | WebFetch | Fetch and process web content |
|
|
152
|
+
| **Task Management** | TodoWrite | Track progress on multi-step tasks |
|
|
153
|
+
| **Shared Scratchpad** | ScratchpadWrite, ScratchpadRead, ScratchpadList | Share work-in-progress between agents (volatile) |
|
|
154
|
+
| **Per-Agent Memory** | MemoryWrite, MemoryRead, MemoryEdit, MemoryMultiEdit, MemoryGlob, MemoryGrep, MemoryDelete | Persistent learning and knowledge storage (opt-in) |
|
|
155
|
+
| **Reasoning** | Think | Extended reasoning for complex problems |
|
|
156
|
+
|
|
157
|
+
**Default tools**: Every agent automatically gets Read, Grep, Glob, TodoWrite, Think, WebFetch, and scratchpad tools (ScratchpadWrite, ScratchpadRead, ScratchpadList) unless you explicitly disable them with `disable_default_tools: true`.
|
|
158
|
+
|
|
159
|
+
**Memory tools** are opt-in for learning agents that need persistent knowledge storage.
|
|
160
|
+
|
|
161
|
+
## Configuration Formats: YAML vs Ruby DSL vs Markdown
|
|
162
|
+
|
|
163
|
+
SwarmSDK supports three equally powerful configuration formats.
|
|
164
|
+
|
|
165
|
+
### YAML Configuration
|
|
166
|
+
|
|
167
|
+
**Use YAML when**:
|
|
168
|
+
- You want simple, declarative configuration files
|
|
169
|
+
- Your hooks are shell scripts (not Ruby code)
|
|
170
|
+
- You prefer separating configuration from code
|
|
171
|
+
- You're new to SwarmSDK
|
|
172
|
+
|
|
173
|
+
**Example**:
|
|
174
|
+
```yaml
|
|
175
|
+
version: 2
|
|
176
|
+
swarm:
|
|
177
|
+
name: "My First Swarm"
|
|
178
|
+
lead: assistant
|
|
179
|
+
agents:
|
|
180
|
+
assistant:
|
|
181
|
+
description: "A helpful assistant"
|
|
182
|
+
model: "gpt-4"
|
|
183
|
+
system_prompt: "You are a helpful assistant."
|
|
184
|
+
tools:
|
|
185
|
+
- Write
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Ruby DSL
|
|
189
|
+
|
|
190
|
+
**Use Ruby DSL when**:
|
|
191
|
+
- You need dynamic configuration (variables, conditionals, loops)
|
|
192
|
+
- You want to write hooks as Ruby blocks (inline logic)
|
|
193
|
+
- You prefer IDE autocomplete and type checking
|
|
194
|
+
- You're building reusable agent libraries
|
|
195
|
+
|
|
196
|
+
**Example - Inline Definition**:
|
|
197
|
+
```ruby
|
|
198
|
+
swarm = SwarmSDK.build do
|
|
199
|
+
name "My First Swarm"
|
|
200
|
+
lead :assistant
|
|
201
|
+
|
|
202
|
+
agent :assistant do
|
|
203
|
+
description "A helpful assistant"
|
|
204
|
+
model "gpt-4"
|
|
205
|
+
system_prompt "You are a helpful assistant."
|
|
206
|
+
tools :Write
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
**Example - Load from Markdown File**:
|
|
212
|
+
```ruby
|
|
213
|
+
swarm = SwarmSDK.build do
|
|
214
|
+
name "My First Swarm"
|
|
215
|
+
lead :assistant
|
|
216
|
+
|
|
217
|
+
# Load agent from markdown file content
|
|
218
|
+
# Name is always required (overrides any name in frontmatter)
|
|
219
|
+
agent :assistant, File.read("agents/assistant.md")
|
|
220
|
+
end
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**Example - Mix Inline and Markdown**:
|
|
224
|
+
```ruby
|
|
225
|
+
swarm = SwarmSDK.build do
|
|
226
|
+
name "Dev Team"
|
|
227
|
+
lead :frontend
|
|
228
|
+
|
|
229
|
+
# Inline DSL agent
|
|
230
|
+
agent :frontend do
|
|
231
|
+
description "Frontend developer"
|
|
232
|
+
model "gpt-4"
|
|
233
|
+
system_prompt "You build UIs"
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# Load backend from markdown file
|
|
237
|
+
agent :backend, File.read("agents/backend.md")
|
|
238
|
+
end
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**Note**: Use `system_prompt` in the Ruby DSL to set the agent's instructions.
|
|
242
|
+
|
|
243
|
+
### Markdown Agent Files
|
|
244
|
+
|
|
245
|
+
**Use Markdown when**:
|
|
246
|
+
- You want to define agents in separate, readable files
|
|
247
|
+
- You prefer writing prompts in a documentation format
|
|
248
|
+
- You want to version control agent definitions independently
|
|
249
|
+
- You're sharing agent configurations with non-developers
|
|
250
|
+
|
|
251
|
+
**Example**: Create `agents/assistant.md`:
|
|
252
|
+
|
|
253
|
+
```markdown
|
|
254
|
+
---
|
|
255
|
+
description: "A helpful assistant"
|
|
256
|
+
model: "gpt-4"
|
|
257
|
+
tools:
|
|
258
|
+
- Write
|
|
259
|
+
- Edit
|
|
260
|
+
delegates_to:
|
|
261
|
+
- reviewer
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
# Assistant Agent
|
|
265
|
+
|
|
266
|
+
You are a helpful assistant who writes clear, maintainable code.
|
|
267
|
+
|
|
268
|
+
## Your Responsibilities
|
|
269
|
+
|
|
270
|
+
- Write clean, well-documented code
|
|
271
|
+
- Follow best practices and style guides
|
|
272
|
+
- Delegate code reviews to the reviewer agent
|
|
273
|
+
|
|
274
|
+
## Best Practices
|
|
275
|
+
|
|
276
|
+
- Always add comments for complex logic
|
|
277
|
+
- Use descriptive variable names
|
|
278
|
+
- Test your code before finalizing
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Then reference it in your YAML configuration:
|
|
282
|
+
|
|
283
|
+
```yaml
|
|
284
|
+
# swarm.yml
|
|
285
|
+
version: 2
|
|
286
|
+
swarm:
|
|
287
|
+
name: "My Swarm"
|
|
288
|
+
lead: assistant
|
|
289
|
+
agents:
|
|
290
|
+
assistant: "agents/assistant.md"
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Note**: When the agent value is a string, it's treated as a file path. You can also use the hash format if you need to override settings from the markdown file:
|
|
294
|
+
|
|
295
|
+
```yaml
|
|
296
|
+
agents:
|
|
297
|
+
assistant:
|
|
298
|
+
agent_file: "agents/assistant.md"
|
|
299
|
+
timeout: 120 # Override timeout from file
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
```ruby
|
|
303
|
+
require 'swarm_sdk'
|
|
304
|
+
|
|
305
|
+
# Load swarm from YAML (which references the Markdown file)
|
|
306
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
307
|
+
result = swarm.execute("Your task here")
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**Note**: Markdown agent definitions can be used in multiple ways:
|
|
311
|
+
- **YAML**: Reference file path: `assistant: "agents/assistant.md"`
|
|
312
|
+
- **Ruby DSL**: Pass file content: `agent :assistant, File.read("agents/assistant.md")`
|
|
313
|
+
- **Ruby DSL**: Or inline markdown: `agent :assistant, <<~MD ... MD`
|
|
314
|
+
|
|
315
|
+
In the Ruby DSL, the agent name is **always required** and will override any `name` field in the markdown frontmatter.
|
|
316
|
+
|
|
317
|
+
**All three approaches are powerful**—choose what fits your workflow:
|
|
318
|
+
- **YAML**: Best for simple, declarative configs with file references
|
|
319
|
+
- **Ruby DSL**: Best for dynamic configs, inline hooks, and programmatic agent definitions
|
|
320
|
+
- **Markdown**: Best for reusable agent definitions that can be version controlled independently
|
|
321
|
+
|
|
322
|
+
The rest of this guide shows examples in all three formats.
|
|
323
|
+
|
|
324
|
+
## Your First Agent
|
|
325
|
+
|
|
326
|
+
Let's create the simplest possible agent: one that answers questions.
|
|
327
|
+
|
|
328
|
+
### Understanding What We're Building
|
|
329
|
+
|
|
330
|
+
We'll create:
|
|
331
|
+
1. A swarm named "My First Swarm"
|
|
332
|
+
2. One agent called "assistant"
|
|
333
|
+
3. The agent will use GPT-4 and have basic file reading capability
|
|
334
|
+
|
|
335
|
+
### YAML Approach
|
|
336
|
+
|
|
337
|
+
Create a file called `swarm.yml`:
|
|
338
|
+
|
|
339
|
+
```yaml
|
|
340
|
+
version: 2
|
|
341
|
+
swarm:
|
|
342
|
+
name: "My First Swarm"
|
|
343
|
+
lead: assistant
|
|
344
|
+
|
|
345
|
+
agents:
|
|
346
|
+
assistant:
|
|
347
|
+
description: "A helpful assistant"
|
|
348
|
+
model: "gpt-4"
|
|
349
|
+
system_prompt: |
|
|
350
|
+
You are a helpful assistant.
|
|
351
|
+
Answer questions clearly and concisely.
|
|
352
|
+
tools:
|
|
353
|
+
- Write
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
Create a file called `run.rb`:
|
|
357
|
+
|
|
358
|
+
```ruby
|
|
359
|
+
require 'swarm_sdk'
|
|
360
|
+
|
|
361
|
+
# Load swarm from YAML
|
|
362
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
363
|
+
|
|
364
|
+
# Execute a task
|
|
365
|
+
result = swarm.execute("What is 2 + 2?")
|
|
366
|
+
|
|
367
|
+
puts "Response: #{result.content}"
|
|
368
|
+
puts "Success: #{result.success?}"
|
|
369
|
+
puts "Duration: #{result.duration}s"
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### Ruby DSL Approach
|
|
373
|
+
|
|
374
|
+
Create a file called `run.rb`:
|
|
375
|
+
|
|
376
|
+
```ruby
|
|
377
|
+
require 'swarm_sdk'
|
|
378
|
+
|
|
379
|
+
# Build swarm with DSL
|
|
380
|
+
swarm = SwarmSDK.build do
|
|
381
|
+
name "My First Swarm"
|
|
382
|
+
lead :assistant
|
|
383
|
+
|
|
384
|
+
agent :assistant do
|
|
385
|
+
description "A helpful assistant"
|
|
386
|
+
model "gpt-4"
|
|
387
|
+
system_prompt "You are a helpful assistant. Answer questions clearly and concisely."
|
|
388
|
+
tools :Write
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
# Execute a task
|
|
393
|
+
result = swarm.execute("What is 2 + 2?")
|
|
394
|
+
|
|
395
|
+
puts "Response: #{result.content}"
|
|
396
|
+
puts "Success: #{result.success?}"
|
|
397
|
+
puts "Duration: #{result.duration}s"
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### Markdown Approach
|
|
401
|
+
|
|
402
|
+
Create `agents/assistant.md`:
|
|
403
|
+
|
|
404
|
+
```markdown
|
|
405
|
+
---
|
|
406
|
+
description: "A helpful assistant"
|
|
407
|
+
model: "gpt-4"
|
|
408
|
+
tools:
|
|
409
|
+
- Write
|
|
410
|
+
---
|
|
411
|
+
|
|
412
|
+
# Helpful Assistant
|
|
413
|
+
|
|
414
|
+
You are a helpful assistant.
|
|
415
|
+
|
|
416
|
+
Answer questions clearly and concisely.
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
Create `swarm.yml`:
|
|
420
|
+
|
|
421
|
+
```yaml
|
|
422
|
+
version: 2
|
|
423
|
+
swarm:
|
|
424
|
+
name: "My First Swarm"
|
|
425
|
+
lead: assistant
|
|
426
|
+
agents:
|
|
427
|
+
assistant: "agents/assistant.md"
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
Create `run.rb`:
|
|
431
|
+
|
|
432
|
+
```ruby
|
|
433
|
+
require 'swarm_sdk'
|
|
434
|
+
|
|
435
|
+
# Load swarm from YAML (which references the Markdown file)
|
|
436
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
437
|
+
|
|
438
|
+
# Execute a task
|
|
439
|
+
result = swarm.execute("What is 2 + 2?")
|
|
440
|
+
|
|
441
|
+
puts "Response: #{result.content}"
|
|
442
|
+
puts "Success: #{result.success?}"
|
|
443
|
+
puts "Duration: #{result.duration}s"
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
### Run Your Agent
|
|
447
|
+
|
|
448
|
+
```bash
|
|
449
|
+
ruby run.rb
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
**Expected output**:
|
|
453
|
+
```
|
|
454
|
+
Response: The answer is 4.
|
|
455
|
+
Success: true
|
|
456
|
+
Duration: 1.23s
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
### What's Happening?
|
|
460
|
+
|
|
461
|
+
Let's break down each part:
|
|
462
|
+
|
|
463
|
+
1. **`version: 2`** (YAML only): Specifies SwarmSDK configuration version
|
|
464
|
+
2. **`name "My First Swarm"`**: Names your swarm (useful for logging)
|
|
465
|
+
3. **`lead :assistant`**: Designates which agent receives your tasks
|
|
466
|
+
4. **`agent :assistant do`**: Defines an agent with a unique name
|
|
467
|
+
5. **`description`**: Explains the agent's role (required field)
|
|
468
|
+
6. **`model "gpt-4"`**: Specifies which LLM to use
|
|
469
|
+
7. **`system_prompt`**: Instructions that guide the agent's behavior
|
|
470
|
+
8. **`tools :Write`**: Grants the agent Write capability (Read is already included by default)
|
|
471
|
+
9. **`swarm.execute("...")`**: Sends a task to the lead agent and returns the result
|
|
472
|
+
|
|
473
|
+
## Understanding Results
|
|
474
|
+
|
|
475
|
+
The `execute` method returns a `Result` object with detailed information:
|
|
476
|
+
|
|
477
|
+
```ruby
|
|
478
|
+
result = swarm.execute("What is 2 + 2?")
|
|
479
|
+
|
|
480
|
+
# Access response content
|
|
481
|
+
result.content # => "The answer is 4."
|
|
482
|
+
|
|
483
|
+
# Check success status
|
|
484
|
+
result.success? # => true
|
|
485
|
+
|
|
486
|
+
# Get performance metrics
|
|
487
|
+
result.duration # => 1.23 (seconds)
|
|
488
|
+
result.total_cost # => 0.0015 (USD)
|
|
489
|
+
result.total_tokens # => 450
|
|
490
|
+
|
|
491
|
+
# Get involved agents (useful in multi-agent swarms)
|
|
492
|
+
result.agents_involved # => [:assistant]
|
|
493
|
+
|
|
494
|
+
# Check for errors
|
|
495
|
+
result.error # => nil (or exception object if failed)
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
### Result Object Reference
|
|
499
|
+
|
|
500
|
+
| Property | Type | Description |
|
|
501
|
+
|----------|------|-------------|
|
|
502
|
+
| `content` | String | The final response from the lead agent |
|
|
503
|
+
| `success?` | Boolean | Whether execution completed without errors |
|
|
504
|
+
| `duration` | Float | Total execution time in seconds |
|
|
505
|
+
| `total_cost` | Float | Total cost in USD (if provider reports it) |
|
|
506
|
+
| `total_tokens` | Integer | Total tokens used across all agents |
|
|
507
|
+
| `agents_involved` | Array[Symbol] | List of agents that participated |
|
|
508
|
+
| `error` | Exception \| nil | Error object if execution failed |
|
|
509
|
+
|
|
510
|
+
### Common Result Patterns
|
|
511
|
+
|
|
512
|
+
```ruby
|
|
513
|
+
# Check if successful before using result
|
|
514
|
+
if result.success?
|
|
515
|
+
puts result.content
|
|
516
|
+
else
|
|
517
|
+
puts "Error: #{result.error.message}"
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
# Log cost and performance
|
|
521
|
+
puts "Cost: $#{result.total_cost.round(4)}"
|
|
522
|
+
puts "Time: #{result.duration.round(2)}s"
|
|
523
|
+
puts "Tokens: #{result.total_tokens}"
|
|
524
|
+
|
|
525
|
+
# Check which agents were involved
|
|
526
|
+
puts "Agents used: #{result.agents_involved.join(', ')}"
|
|
527
|
+
|
|
528
|
+
# Handle errors gracefully
|
|
529
|
+
begin
|
|
530
|
+
result = swarm.execute("Complex task")
|
|
531
|
+
|
|
532
|
+
# Check result success instead of catching exceptions
|
|
533
|
+
unless result.success?
|
|
534
|
+
puts "Execution failed: #{result.error.message}"
|
|
535
|
+
end
|
|
536
|
+
rescue SwarmSDK::ConfigurationError => e
|
|
537
|
+
puts "Configuration error: #{e.message}"
|
|
538
|
+
end
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
## Common Workflows
|
|
542
|
+
|
|
543
|
+
Now that you understand the basics, let's explore common patterns.
|
|
544
|
+
|
|
545
|
+
### Single Agent with File Operations
|
|
546
|
+
|
|
547
|
+
**Use case**: An agent that reads a file and summarizes it.
|
|
548
|
+
|
|
549
|
+
**YAML** (`file-reader.yml`):
|
|
550
|
+
```yaml
|
|
551
|
+
version: 2
|
|
552
|
+
swarm:
|
|
553
|
+
name: "File Reader"
|
|
554
|
+
lead: reader
|
|
555
|
+
|
|
556
|
+
agents:
|
|
557
|
+
reader:
|
|
558
|
+
description: "Reads and summarizes files"
|
|
559
|
+
model: "gpt-4"
|
|
560
|
+
system_prompt: |
|
|
561
|
+
You are a file analysis expert.
|
|
562
|
+
Read files and provide concise, insightful summaries.
|
|
563
|
+
# Read is included by default - no tools needed!
|
|
564
|
+
directory: "."
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
**Ruby DSL**:
|
|
568
|
+
```ruby
|
|
569
|
+
swarm = SwarmSDK.build do
|
|
570
|
+
name "File Reader"
|
|
571
|
+
lead :reader
|
|
572
|
+
|
|
573
|
+
agent :reader do
|
|
574
|
+
description "Reads and summarizes files"
|
|
575
|
+
model "gpt-4"
|
|
576
|
+
system_prompt "You are a file analysis expert. Read files and provide concise, insightful summaries."
|
|
577
|
+
directory "."
|
|
578
|
+
end
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
# Use it
|
|
582
|
+
result = swarm.execute("Read README.md and summarize its key points")
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
**Markdown** (`agents/reader.md`):
|
|
586
|
+
```markdown
|
|
587
|
+
---
|
|
588
|
+
description: "Reads and summarizes files"
|
|
589
|
+
model: "gpt-4"
|
|
590
|
+
directory: "."
|
|
591
|
+
---
|
|
592
|
+
|
|
593
|
+
# File Analysis Expert
|
|
594
|
+
|
|
595
|
+
You are a file analysis expert.
|
|
596
|
+
|
|
597
|
+
Read files and provide concise, insightful summaries.
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
**Why this works**: The agent has Read tool by default and can access files in the specified directory.
|
|
601
|
+
|
|
602
|
+
### Multi-Agent Collaboration
|
|
603
|
+
|
|
604
|
+
**Use case**: A code review system with a coder and reviewer.
|
|
605
|
+
|
|
606
|
+
**YAML** (`code-review.yml`):
|
|
607
|
+
```yaml
|
|
608
|
+
version: 2
|
|
609
|
+
swarm:
|
|
610
|
+
name: "Code Review Team"
|
|
611
|
+
lead: coder
|
|
612
|
+
|
|
613
|
+
agents:
|
|
614
|
+
coder:
|
|
615
|
+
description: "Writes clean, maintainable code"
|
|
616
|
+
model: "gpt-4"
|
|
617
|
+
system_prompt: |
|
|
618
|
+
You are an expert programmer.
|
|
619
|
+
Write code and delegate to the reviewer for feedback.
|
|
620
|
+
tools:
|
|
621
|
+
- Write
|
|
622
|
+
- Edit
|
|
623
|
+
delegates_to:
|
|
624
|
+
- reviewer
|
|
625
|
+
|
|
626
|
+
reviewer:
|
|
627
|
+
description: "Reviews code for quality and issues"
|
|
628
|
+
model: "claude-sonnet-4"
|
|
629
|
+
system_prompt: |
|
|
630
|
+
You are a code review expert.
|
|
631
|
+
Analyze code for bugs, style issues, and improvements.
|
|
632
|
+
# Only needs Read (included by default)
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
**Ruby DSL**:
|
|
636
|
+
```ruby
|
|
637
|
+
swarm = SwarmSDK.build do
|
|
638
|
+
name "Code Review Team"
|
|
639
|
+
lead :coder
|
|
640
|
+
|
|
641
|
+
agent :coder do
|
|
642
|
+
description "Writes clean, maintainable code"
|
|
643
|
+
model "gpt-4"
|
|
644
|
+
system_prompt "You are an expert programmer. Write code and delegate to the reviewer for feedback."
|
|
645
|
+
tools :Write, :Edit
|
|
646
|
+
delegates_to :reviewer
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
agent :reviewer do
|
|
650
|
+
description "Reviews code for quality and issues"
|
|
651
|
+
model "claude-sonnet-4"
|
|
652
|
+
system_prompt "You are a code review expert. Analyze code for bugs, style issues, and improvements."
|
|
653
|
+
end
|
|
654
|
+
end
|
|
655
|
+
|
|
656
|
+
# Use it
|
|
657
|
+
result = swarm.execute("Write a function to validate email addresses and get it reviewed")
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
**Markdown** (`agents/coder.md` + `agents/reviewer.md` + `swarm.yml`):
|
|
661
|
+
|
|
662
|
+
`agents/coder.md`:
|
|
663
|
+
```markdown
|
|
664
|
+
---
|
|
665
|
+
description: "Writes clean, maintainable code"
|
|
666
|
+
model: "gpt-4"
|
|
667
|
+
tools:
|
|
668
|
+
- Write
|
|
669
|
+
- Edit
|
|
670
|
+
delegates_to:
|
|
671
|
+
- reviewer
|
|
672
|
+
---
|
|
673
|
+
|
|
674
|
+
# Expert Programmer
|
|
675
|
+
|
|
676
|
+
You are an expert programmer.
|
|
677
|
+
|
|
678
|
+
Write code and delegate to the reviewer for feedback.
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
`agents/reviewer.md`:
|
|
682
|
+
```markdown
|
|
683
|
+
---
|
|
684
|
+
description: "Reviews code for quality and issues"
|
|
685
|
+
model: "claude-sonnet-4"
|
|
686
|
+
---
|
|
687
|
+
|
|
688
|
+
# Code Review Expert
|
|
689
|
+
|
|
690
|
+
You are a code review expert.
|
|
691
|
+
|
|
692
|
+
Analyze code for bugs, style issues, and improvements.
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
`swarm.yml`:
|
|
696
|
+
```yaml
|
|
697
|
+
version: 2
|
|
698
|
+
swarm:
|
|
699
|
+
name: "Code Review Team"
|
|
700
|
+
lead: coder
|
|
701
|
+
agents:
|
|
702
|
+
coder: "agents/coder.md"
|
|
703
|
+
reviewer: "agents/reviewer.md"
|
|
704
|
+
```
|
|
705
|
+
|
|
706
|
+
Then load and use:
|
|
707
|
+
```ruby
|
|
708
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
709
|
+
result = swarm.execute("Write a function to validate email addresses and get it reviewed")
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
**How delegation works**: The coder writes code, then calls the `DelegateTaskToReviewer` tool to get feedback. The reviewer analyzes and returns suggestions. The coder can iterate based on feedback.
|
|
713
|
+
|
|
714
|
+
### Using Multiple Tools
|
|
715
|
+
|
|
716
|
+
**Use case**: An agent that searches, reads, and modifies files.
|
|
717
|
+
|
|
718
|
+
**YAML** (`refactor-assistant.yml`):
|
|
719
|
+
```yaml
|
|
720
|
+
version: 2
|
|
721
|
+
swarm:
|
|
722
|
+
name: "Refactor Assistant"
|
|
723
|
+
lead: refactorer
|
|
724
|
+
|
|
725
|
+
agents:
|
|
726
|
+
refactorer:
|
|
727
|
+
description: "Finds and refactors code patterns"
|
|
728
|
+
model: "gpt-4"
|
|
729
|
+
system_prompt: |
|
|
730
|
+
You are a refactoring expert.
|
|
731
|
+
Use Grep to find code patterns, Read to understand context,
|
|
732
|
+
and Edit to make improvements.
|
|
733
|
+
tools:
|
|
734
|
+
- Edit
|
|
735
|
+
- Bash
|
|
736
|
+
# Grep and Read included by default
|
|
737
|
+
directory: "./src"
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
**Ruby DSL**:
|
|
741
|
+
```ruby
|
|
742
|
+
swarm = SwarmSDK.build do
|
|
743
|
+
name "Refactor Assistant"
|
|
744
|
+
lead :refactorer
|
|
745
|
+
|
|
746
|
+
agent :refactorer do
|
|
747
|
+
description "Finds and refactors code patterns"
|
|
748
|
+
model "gpt-4"
|
|
749
|
+
system_prompt "You are a refactoring expert. Use Grep to find code patterns, Read to understand context, and Edit to make improvements."
|
|
750
|
+
tools :Edit, :Bash
|
|
751
|
+
directory "./src"
|
|
752
|
+
end
|
|
753
|
+
end
|
|
754
|
+
|
|
755
|
+
# Use it
|
|
756
|
+
result = swarm.execute("Find all functions longer than 50 lines and suggest refactorings")
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
**Tool workflow**:
|
|
760
|
+
1. Agent uses Grep to find long functions: `Grep(pattern: "def .*", path: ".")`
|
|
761
|
+
2. Agent uses Read to examine each file: `Read(file_path: "file.rb")`
|
|
762
|
+
3. Agent uses Edit to refactor: `Edit(file_path: "file.rb", old_string: "...", new_string: "...")`
|
|
763
|
+
|
|
764
|
+
### Customizing Agent Behavior
|
|
765
|
+
|
|
766
|
+
**Use case**: Different agents need different LLM parameters.
|
|
767
|
+
|
|
768
|
+
**YAML** (`customized-agents.yml`):
|
|
769
|
+
```yaml
|
|
770
|
+
version: 2
|
|
771
|
+
swarm:
|
|
772
|
+
name: "Customized Team"
|
|
773
|
+
lead: creative
|
|
774
|
+
|
|
775
|
+
agents:
|
|
776
|
+
creative:
|
|
777
|
+
description: "Creative brainstormer"
|
|
778
|
+
model: "gpt-4"
|
|
779
|
+
system_prompt: "Generate creative ideas."
|
|
780
|
+
parameters:
|
|
781
|
+
temperature: 1.5 # More creative/random
|
|
782
|
+
top_p: 0.95
|
|
783
|
+
|
|
784
|
+
analytical:
|
|
785
|
+
description: "Analytical reviewer"
|
|
786
|
+
model: "claude-sonnet-4"
|
|
787
|
+
system_prompt: "Analyze ideas critically."
|
|
788
|
+
parameters:
|
|
789
|
+
temperature: 0.3 # More focused/deterministic
|
|
790
|
+
```
|
|
791
|
+
|
|
792
|
+
**Ruby DSL**:
|
|
793
|
+
```ruby
|
|
794
|
+
swarm = SwarmSDK.build do
|
|
795
|
+
name "Customized Team"
|
|
796
|
+
lead :creative
|
|
797
|
+
|
|
798
|
+
agent :creative do
|
|
799
|
+
description "Creative brainstormer"
|
|
800
|
+
model "gpt-4"
|
|
801
|
+
system_prompt "Generate creative ideas."
|
|
802
|
+
parameters temperature: 1.5, top_p: 0.95
|
|
803
|
+
end
|
|
804
|
+
|
|
805
|
+
agent :analytical do
|
|
806
|
+
description "Analytical reviewer"
|
|
807
|
+
model "claude-sonnet-4"
|
|
808
|
+
system_prompt "Analyze ideas critically."
|
|
809
|
+
parameters temperature: 0.3
|
|
810
|
+
end
|
|
811
|
+
end
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
**Why customize parameters**:
|
|
815
|
+
- **Temperature**: Controls randomness (0.0-2.0). Higher = more creative, lower = more focused.
|
|
816
|
+
- **Top P**: Controls diversity (0.0-1.0). Higher = more diverse outputs.
|
|
817
|
+
- **Max tokens**: Limits response length.
|
|
818
|
+
|
|
819
|
+
### Using Scratchpad for Agent Communication
|
|
820
|
+
|
|
821
|
+
**Use case**: Agents sharing data without explicit delegation.
|
|
822
|
+
|
|
823
|
+
**YAML** (`research-team.yml`):
|
|
824
|
+
```yaml
|
|
825
|
+
version: 2
|
|
826
|
+
swarm:
|
|
827
|
+
name: "Research Team"
|
|
828
|
+
lead: coordinator
|
|
829
|
+
|
|
830
|
+
agents:
|
|
831
|
+
coordinator:
|
|
832
|
+
description: "Coordinates research tasks"
|
|
833
|
+
model: "gpt-4"
|
|
834
|
+
system_prompt: |
|
|
835
|
+
Coordinate research. Use ScratchpadWrite to share findings.
|
|
836
|
+
delegates_to:
|
|
837
|
+
- researcher
|
|
838
|
+
- analyst
|
|
839
|
+
|
|
840
|
+
researcher:
|
|
841
|
+
description: "Gathers raw data"
|
|
842
|
+
model: "gpt-4"
|
|
843
|
+
system_prompt: |
|
|
844
|
+
Research topics and write findings to scratchpad using file path "research/data".
|
|
845
|
+
|
|
846
|
+
analyst:
|
|
847
|
+
description: "Analyzes research data"
|
|
848
|
+
model: "claude-sonnet-4"
|
|
849
|
+
system_prompt: |
|
|
850
|
+
Read "research/data" from scratchpad and provide analysis.
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
**Ruby DSL**:
|
|
854
|
+
```ruby
|
|
855
|
+
swarm = SwarmSDK.build do
|
|
856
|
+
name "Research Team"
|
|
857
|
+
lead :coordinator
|
|
858
|
+
|
|
859
|
+
agent :coordinator do
|
|
860
|
+
description "Coordinates research tasks"
|
|
861
|
+
model "gpt-4"
|
|
862
|
+
system_prompt "Coordinate research. Use ScratchpadWrite to share findings."
|
|
863
|
+
delegates_to :researcher, :analyst
|
|
864
|
+
end
|
|
865
|
+
|
|
866
|
+
agent :researcher do
|
|
867
|
+
description "Gathers raw data"
|
|
868
|
+
model "gpt-4"
|
|
869
|
+
system_prompt 'Research topics and write findings to scratchpad using file path "research/data".'
|
|
870
|
+
end
|
|
871
|
+
|
|
872
|
+
agent :analyst do
|
|
873
|
+
description "Analyzes research data"
|
|
874
|
+
model "claude-sonnet-4"
|
|
875
|
+
system_prompt 'Read "research/data" from scratchpad and provide analysis.'
|
|
876
|
+
end
|
|
877
|
+
end
|
|
878
|
+
|
|
879
|
+
# Use it
|
|
880
|
+
result = swarm.execute("Research Ruby performance optimization techniques and analyze them")
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
**How it works**:
|
|
884
|
+
1. Coordinator delegates to researcher: "Gather data on Ruby performance"
|
|
885
|
+
2. Researcher uses `ScratchpadWrite(file_path: "research/data", content: "...", title: "Research Data")`
|
|
886
|
+
3. Coordinator delegates to analyst: "Analyze the research"
|
|
887
|
+
4. Analyst uses `ScratchpadRead(file_path: "research/data")` to get the data
|
|
888
|
+
5. Analyst provides analysis
|
|
889
|
+
|
|
890
|
+
## Common Pitfalls and Solutions
|
|
891
|
+
|
|
892
|
+
### Pitfall 1: Missing Lead Agent
|
|
893
|
+
|
|
894
|
+
**Error**:
|
|
895
|
+
```ruby
|
|
896
|
+
# ❌ No lead agent specified
|
|
897
|
+
swarm = SwarmSDK.build do
|
|
898
|
+
name "My Swarm"
|
|
899
|
+
agent :assistant do
|
|
900
|
+
description "Helper"
|
|
901
|
+
model "gpt-4"
|
|
902
|
+
end
|
|
903
|
+
end
|
|
904
|
+
# => ConfigurationError: No lead agent set
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
**Solution**:
|
|
908
|
+
```ruby
|
|
909
|
+
# ✅ Always specify lead
|
|
910
|
+
swarm = SwarmSDK.build do
|
|
911
|
+
name "My Swarm"
|
|
912
|
+
lead :assistant # Don't forget this!
|
|
913
|
+
agent :assistant do
|
|
914
|
+
description "Helper"
|
|
915
|
+
model "gpt-4"
|
|
916
|
+
end
|
|
917
|
+
end
|
|
918
|
+
```
|
|
919
|
+
|
|
920
|
+
### Pitfall 2: Missing Required Description
|
|
921
|
+
|
|
922
|
+
**Error**:
|
|
923
|
+
```ruby
|
|
924
|
+
# ❌ Description required
|
|
925
|
+
agent :assistant do
|
|
926
|
+
model "gpt-4"
|
|
927
|
+
system_prompt "You are helpful."
|
|
928
|
+
end
|
|
929
|
+
# => ConfigurationError: missing required 'description'
|
|
930
|
+
```
|
|
931
|
+
|
|
932
|
+
**Solution**:
|
|
933
|
+
```ruby
|
|
934
|
+
# ✅ Always include description
|
|
935
|
+
agent :assistant do
|
|
936
|
+
description "A helpful assistant" # Required!
|
|
937
|
+
model "gpt-4"
|
|
938
|
+
system_prompt "You are helpful."
|
|
939
|
+
end
|
|
940
|
+
```
|
|
941
|
+
|
|
942
|
+
### Pitfall 3: Invalid Model Names
|
|
943
|
+
|
|
944
|
+
**Error**:
|
|
945
|
+
```ruby
|
|
946
|
+
# ❌ Model doesn't exist
|
|
947
|
+
agent :assistant do
|
|
948
|
+
description "Helper"
|
|
949
|
+
model "gpt-99" # Typo or non-existent model
|
|
950
|
+
end
|
|
951
|
+
# => May fail at runtime when trying to call LLM
|
|
952
|
+
```
|
|
953
|
+
|
|
954
|
+
**Solution**:
|
|
955
|
+
```ruby
|
|
956
|
+
# ✅ Use valid model identifiers
|
|
957
|
+
agent :assistant do
|
|
958
|
+
description "Helper"
|
|
959
|
+
model "gpt-4" # Valid: gpt-4, gpt-4-turbo, claude-sonnet-4, etc.
|
|
960
|
+
end
|
|
961
|
+
```
|
|
962
|
+
|
|
963
|
+
**Valid model examples**:
|
|
964
|
+
- OpenAI: `"gpt-4"`, `"gpt-4-turbo"`, `"gpt-3.5-turbo"`
|
|
965
|
+
- Anthropic: `"claude-sonnet-4"`, `"claude-opus-4"`, `"claude-haiku-4"`
|
|
966
|
+
- Others: Depends on your provider configuration
|
|
967
|
+
|
|
968
|
+
### Pitfall 4: Delegation Without Configuration
|
|
969
|
+
|
|
970
|
+
**Error**:
|
|
971
|
+
```ruby
|
|
972
|
+
# ❌ Trying to delegate to unconfigured agent
|
|
973
|
+
swarm = SwarmSDK.build do
|
|
974
|
+
name "Team"
|
|
975
|
+
lead :leader
|
|
976
|
+
|
|
977
|
+
agent :leader do
|
|
978
|
+
description "Leader"
|
|
979
|
+
model "gpt-4"
|
|
980
|
+
# Missing: delegates_to :helper
|
|
981
|
+
end
|
|
982
|
+
|
|
983
|
+
agent :helper do
|
|
984
|
+
description "Helper"
|
|
985
|
+
model "gpt-4"
|
|
986
|
+
end
|
|
987
|
+
end
|
|
988
|
+
# Leader won't have DelegateTaskToHelper tool!
|
|
989
|
+
```
|
|
990
|
+
|
|
991
|
+
**Solution**:
|
|
992
|
+
```ruby
|
|
993
|
+
# ✅ Configure delegation explicitly
|
|
994
|
+
agent :leader do
|
|
995
|
+
description "Leader"
|
|
996
|
+
model "gpt-4"
|
|
997
|
+
delegates_to :helper # Now leader can delegate!
|
|
998
|
+
end
|
|
999
|
+
```
|
|
1000
|
+
|
|
1001
|
+
### Pitfall 5: Tool Not Available
|
|
1002
|
+
|
|
1003
|
+
**Error**:
|
|
1004
|
+
```ruby
|
|
1005
|
+
# Agent tries to use Write but doesn't have it
|
|
1006
|
+
agent :reader do
|
|
1007
|
+
description "Reader"
|
|
1008
|
+
model "gpt-4"
|
|
1009
|
+
disable_default_tools true # Disables all default tools
|
|
1010
|
+
tools :Read # Only Read available
|
|
1011
|
+
end
|
|
1012
|
+
# If agent tries to write: ToolNotFoundError
|
|
1013
|
+
```
|
|
1014
|
+
|
|
1015
|
+
**Solution**:
|
|
1016
|
+
```ruby
|
|
1017
|
+
# ✅ Add tools you need
|
|
1018
|
+
agent :reader do
|
|
1019
|
+
description "Reader"
|
|
1020
|
+
model "gpt-4"
|
|
1021
|
+
tools :Read, :Write # Explicitly add Write
|
|
1022
|
+
end
|
|
1023
|
+
```
|
|
1024
|
+
|
|
1025
|
+
### Pitfall 6: Incorrect Gem Name
|
|
1026
|
+
|
|
1027
|
+
**Error**:
|
|
1028
|
+
```ruby
|
|
1029
|
+
# ❌ Wrong gem name
|
|
1030
|
+
gem 'swarm-sdk' # Wrong!
|
|
1031
|
+
gem 'swarmcore' # Wrong!
|
|
1032
|
+
|
|
1033
|
+
# ❌ Wrong require
|
|
1034
|
+
require 'swarm-sdk' # Wrong!
|
|
1035
|
+
```
|
|
1036
|
+
|
|
1037
|
+
**Solution**:
|
|
1038
|
+
```ruby
|
|
1039
|
+
# ✅ Correct gem name and require
|
|
1040
|
+
# In Gemfile:
|
|
1041
|
+
gem 'swarm_sdk'
|
|
1042
|
+
|
|
1043
|
+
# In Ruby code:
|
|
1044
|
+
require 'swarm_sdk'
|
|
1045
|
+
|
|
1046
|
+
# Installation:
|
|
1047
|
+
# gem install swarm_sdk
|
|
1048
|
+
```
|
|
1049
|
+
|
|
1050
|
+
### Pitfall 7: Wrong Ruby Version
|
|
1051
|
+
|
|
1052
|
+
**Error**:
|
|
1053
|
+
```bash
|
|
1054
|
+
# Using Ruby 3.1 or earlier
|
|
1055
|
+
ruby -v
|
|
1056
|
+
# => ruby 3.1.0
|
|
1057
|
+
|
|
1058
|
+
gem install swarm_sdk
|
|
1059
|
+
# => ERROR: swarm_sdk requires Ruby >= 3.2.0
|
|
1060
|
+
```
|
|
1061
|
+
|
|
1062
|
+
**Solution**:
|
|
1063
|
+
```bash
|
|
1064
|
+
# ✅ Use Ruby 3.2.0 or higher
|
|
1065
|
+
# Check version
|
|
1066
|
+
ruby -v
|
|
1067
|
+
# => ruby 3.2.0 (or higher)
|
|
1068
|
+
|
|
1069
|
+
# Install/upgrade Ruby if needed:
|
|
1070
|
+
rbenv install 3.2.0
|
|
1071
|
+
rbenv global 3.2.0
|
|
1072
|
+
# or
|
|
1073
|
+
rvm install 3.2.0
|
|
1074
|
+
rvm use 3.2.0
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
## Testing Your Setup
|
|
1078
|
+
|
|
1079
|
+
Here's a comprehensive test script to verify SwarmSDK is working:
|
|
1080
|
+
|
|
1081
|
+
```ruby
|
|
1082
|
+
require 'swarm_sdk'
|
|
1083
|
+
|
|
1084
|
+
puts "Testing SwarmSDK installation..."
|
|
1085
|
+
puts "Ruby version: #{RUBY_VERSION}"
|
|
1086
|
+
puts ""
|
|
1087
|
+
|
|
1088
|
+
begin
|
|
1089
|
+
# Test 1: Basic swarm creation
|
|
1090
|
+
puts "Test 1: Creating swarm..."
|
|
1091
|
+
swarm = SwarmSDK.build do
|
|
1092
|
+
name "Setup Test"
|
|
1093
|
+
lead :test_agent
|
|
1094
|
+
|
|
1095
|
+
agent :test_agent do
|
|
1096
|
+
description "Test agent"
|
|
1097
|
+
model "gpt-4"
|
|
1098
|
+
system_prompt "Reply with exactly: 'SwarmSDK is working!'"
|
|
1099
|
+
end
|
|
1100
|
+
end
|
|
1101
|
+
puts "✅ Swarm created successfully"
|
|
1102
|
+
puts ""
|
|
1103
|
+
|
|
1104
|
+
# Test 2: Simple execution
|
|
1105
|
+
puts "Test 2: Executing task..."
|
|
1106
|
+
result = swarm.execute("Test")
|
|
1107
|
+
|
|
1108
|
+
if result.success?
|
|
1109
|
+
puts "✅ Execution successful"
|
|
1110
|
+
puts " Response: #{result.content}"
|
|
1111
|
+
puts " Duration: #{result.duration.round(2)}s"
|
|
1112
|
+
puts " Cost: $#{result.total_cost.round(4)}"
|
|
1113
|
+
puts " Agents: #{result.agents_involved.inspect}"
|
|
1114
|
+
else
|
|
1115
|
+
puts "❌ Execution failed: #{result.error.message}"
|
|
1116
|
+
puts " Check your API key and network connection"
|
|
1117
|
+
end
|
|
1118
|
+
puts ""
|
|
1119
|
+
|
|
1120
|
+
# Test 3: Multi-agent delegation
|
|
1121
|
+
puts "Test 3: Testing delegation..."
|
|
1122
|
+
swarm2 = SwarmSDK.build do
|
|
1123
|
+
name "Delegation Test"
|
|
1124
|
+
lead :primary
|
|
1125
|
+
|
|
1126
|
+
agent :primary do
|
|
1127
|
+
description "Primary agent"
|
|
1128
|
+
model "gpt-4"
|
|
1129
|
+
system_prompt "Delegate to helper with message: 'Hello!'"
|
|
1130
|
+
delegates_to :helper
|
|
1131
|
+
end
|
|
1132
|
+
|
|
1133
|
+
agent :helper do
|
|
1134
|
+
description "Helper agent"
|
|
1135
|
+
model "gpt-4"
|
|
1136
|
+
system_prompt "Reply with: 'Delegation works!'"
|
|
1137
|
+
end
|
|
1138
|
+
end
|
|
1139
|
+
|
|
1140
|
+
result2 = swarm2.execute("Test delegation")
|
|
1141
|
+
if result2.success? && result2.agents_involved.length > 1
|
|
1142
|
+
puts "✅ Delegation works"
|
|
1143
|
+
puts " Agents involved: #{result2.agents_involved.inspect}"
|
|
1144
|
+
else
|
|
1145
|
+
puts "❌ Delegation failed"
|
|
1146
|
+
end
|
|
1147
|
+
|
|
1148
|
+
rescue StandardError => e
|
|
1149
|
+
puts "❌ Error: #{e.class.name}"
|
|
1150
|
+
puts " Message: #{e.message}"
|
|
1151
|
+
puts " Backtrace: #{e.backtrace.first(3).join("\n ")}"
|
|
1152
|
+
puts ""
|
|
1153
|
+
puts "Common fixes:"
|
|
1154
|
+
puts " - Verify API key is set: echo $OPENAI_API_KEY"
|
|
1155
|
+
puts " - Check network connection"
|
|
1156
|
+
puts " - Ensure Ruby >= 3.2.0"
|
|
1157
|
+
end
|
|
1158
|
+
```
|
|
1159
|
+
|
|
1160
|
+
**Expected output**:
|
|
1161
|
+
```
|
|
1162
|
+
Testing SwarmSDK installation...
|
|
1163
|
+
Ruby version: 3.2.0
|
|
1164
|
+
|
|
1165
|
+
Test 1: Creating swarm...
|
|
1166
|
+
✅ Swarm created successfully
|
|
1167
|
+
|
|
1168
|
+
Test 2: Executing task...
|
|
1169
|
+
✅ Execution successful
|
|
1170
|
+
Response: SwarmSDK is working!
|
|
1171
|
+
Duration: 1.23s
|
|
1172
|
+
Cost: $0.0015
|
|
1173
|
+
Agents: [:test_agent]
|
|
1174
|
+
|
|
1175
|
+
Test 3: Testing delegation...
|
|
1176
|
+
✅ Delegation works
|
|
1177
|
+
Agents involved: [:primary, :helper]
|
|
1178
|
+
```
|
|
1179
|
+
|
|
1180
|
+
## Next Steps
|
|
1181
|
+
|
|
1182
|
+
Congratulations! You've learned the fundamentals of SwarmSDK.
|
|
1183
|
+
|
|
1184
|
+
### Continue Learning
|
|
1185
|
+
|
|
1186
|
+
**Core Tutorials** (in recommended order):
|
|
1187
|
+
1. **[Complete Tutorial](complete-tutorial.md)** - Comprehensive guide building a real application
|
|
1188
|
+
2. **[Quick Start CLI](quick-start-cli.md)** - Using SwarmSDK from the command line
|
|
1189
|
+
3. **[Rails Integration](rails-integration.md)** - Integrating SwarmSDK with Rails applications
|
|
1190
|
+
|
|
1191
|
+
**Advanced Topics**:
|
|
1192
|
+
- **[Permissions Guide](permissions.md)** - Control file and command access securely
|
|
1193
|
+
- **[Hooks Complete Guide](hooks-complete-guide.md)** - Customize behavior at every step
|
|
1194
|
+
- **[Scratchpad Guide](how-to-use-scratchpad.md)** - Share data between agents
|
|
1195
|
+
- **[Performance Tuning](performance-tuning.md)** - Optimize for speed and cost
|
|
1196
|
+
|
|
1197
|
+
### Key Concepts to Explore
|
|
1198
|
+
|
|
1199
|
+
**Agent Delegation Patterns**: Learn advanced collaboration strategies like hierarchical delegation, peer collaboration, and specialist chains.
|
|
1200
|
+
|
|
1201
|
+
**Permissions System**: Discover how to restrict agents to specific directories, prevent dangerous commands, and create secure multi-agent systems.
|
|
1202
|
+
|
|
1203
|
+
**Hooks and Customization**: Master pre/post hooks for tool calls, prompt injection, and workflow automation.
|
|
1204
|
+
|
|
1205
|
+
**MCP Server Integration**: Connect to external tools and services using the Model Context Protocol.
|
|
1206
|
+
|
|
1207
|
+
**Node-Based Workflows**: Build multi-stage pipelines where different agent teams handle each stage.
|
|
1208
|
+
|
|
1209
|
+
### Real-World Examples
|
|
1210
|
+
|
|
1211
|
+
Check out complete working examples in the `examples/` directory:
|
|
1212
|
+
|
|
1213
|
+
- **Code Review Bot** - Multi-agent code review system
|
|
1214
|
+
- **Documentation Generator** - Automated documentation from code
|
|
1215
|
+
- **Test Suite Builder** - Generate comprehensive test suites
|
|
1216
|
+
- **Refactoring Assistant** - Find and fix code smells
|
|
1217
|
+
|
|
1218
|
+
## Where to Get Help
|
|
1219
|
+
|
|
1220
|
+
- **Documentation**: [SwarmSDK Guides](../README.md)
|
|
1221
|
+
- **API Reference**: [API Documentation](../../api/)
|
|
1222
|
+
- **Examples**: [Example Swarms](../../../examples/v2/)
|
|
1223
|
+
- **Issues**: [GitHub Issues](https://github.com/parruda/claude-swarm/issues)
|
|
1224
|
+
|
|
1225
|
+
## Summary
|
|
1226
|
+
|
|
1227
|
+
You've learned:
|
|
1228
|
+
|
|
1229
|
+
✅ **What SwarmSDK is** - A framework for building collaborative AI agent teams
|
|
1230
|
+
|
|
1231
|
+
✅ **Installation** - How to add SwarmSDK to your project and configure API keys
|
|
1232
|
+
|
|
1233
|
+
✅ **Core concepts** - Swarms, agents, delegation, and tools
|
|
1234
|
+
|
|
1235
|
+
✅ **Configuration formats** - YAML, Ruby DSL, and Markdown approaches
|
|
1236
|
+
|
|
1237
|
+
✅ **Creating agents** - How to define agents with models, prompts, and tools
|
|
1238
|
+
|
|
1239
|
+
✅ **Understanding results** - How to interpret execution results and metrics
|
|
1240
|
+
|
|
1241
|
+
✅ **Common workflows** - Single agent, multi-agent, and tool usage patterns
|
|
1242
|
+
|
|
1243
|
+
✅ **Avoiding pitfalls** - Common mistakes and how to fix them
|
|
1244
|
+
|
|
1245
|
+
✅ **Testing setup** - How to verify everything works correctly
|
|
1246
|
+
|
|
1247
|
+
**Next**: [Complete Tutorial →](complete-tutorial.md)
|
|
1248
|
+
|
|
1249
|
+
---
|
|
1250
|
+
|
|
1251
|
+
## Quick Reference Card
|
|
1252
|
+
|
|
1253
|
+
### Minimal Working Swarm (Ruby DSL)
|
|
1254
|
+
|
|
1255
|
+
```ruby
|
|
1256
|
+
require 'swarm_sdk'
|
|
1257
|
+
|
|
1258
|
+
swarm = SwarmSDK.build do
|
|
1259
|
+
name "My Swarm"
|
|
1260
|
+
lead :agent_name
|
|
1261
|
+
|
|
1262
|
+
agent :agent_name do
|
|
1263
|
+
description "What it does"
|
|
1264
|
+
model "gpt-4"
|
|
1265
|
+
system_prompt "Instructions"
|
|
1266
|
+
tools :Write, :Edit
|
|
1267
|
+
end
|
|
1268
|
+
end
|
|
1269
|
+
|
|
1270
|
+
result = swarm.execute("Your task here")
|
|
1271
|
+
puts result.content if result.success?
|
|
1272
|
+
```
|
|
1273
|
+
|
|
1274
|
+
### Minimal Working Swarm (YAML)
|
|
1275
|
+
|
|
1276
|
+
```yaml
|
|
1277
|
+
# swarm.yml
|
|
1278
|
+
version: 2
|
|
1279
|
+
swarm:
|
|
1280
|
+
name: "My Swarm"
|
|
1281
|
+
lead: agent_name
|
|
1282
|
+
agents:
|
|
1283
|
+
agent_name:
|
|
1284
|
+
description: "What it does"
|
|
1285
|
+
model: "gpt-4"
|
|
1286
|
+
system_prompt: "Instructions"
|
|
1287
|
+
tools:
|
|
1288
|
+
- Write
|
|
1289
|
+
- Edit
|
|
1290
|
+
```
|
|
1291
|
+
|
|
1292
|
+
```ruby
|
|
1293
|
+
# run.rb
|
|
1294
|
+
require 'swarm_sdk'
|
|
1295
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
1296
|
+
result = swarm.execute("Your task here")
|
|
1297
|
+
puts result.content if result.success?
|
|
1298
|
+
```
|
|
1299
|
+
|
|
1300
|
+
### Minimal Working Agent (Markdown)
|
|
1301
|
+
|
|
1302
|
+
```markdown
|
|
1303
|
+
<!-- agents/agent_name.md -->
|
|
1304
|
+
---
|
|
1305
|
+
description: "What it does"
|
|
1306
|
+
model: "gpt-4"
|
|
1307
|
+
tools:
|
|
1308
|
+
- Write
|
|
1309
|
+
- Edit
|
|
1310
|
+
---
|
|
1311
|
+
|
|
1312
|
+
# Agent Name
|
|
1313
|
+
|
|
1314
|
+
Instructions for the agent.
|
|
1315
|
+
```
|
|
1316
|
+
|
|
1317
|
+
```yaml
|
|
1318
|
+
# swarm.yml
|
|
1319
|
+
version: 2
|
|
1320
|
+
swarm:
|
|
1321
|
+
name: "My Swarm"
|
|
1322
|
+
lead: agent_name
|
|
1323
|
+
agents:
|
|
1324
|
+
agent_name: "agents/agent_name.md"
|
|
1325
|
+
```
|
|
1326
|
+
|
|
1327
|
+
```ruby
|
|
1328
|
+
# run.rb
|
|
1329
|
+
require 'swarm_sdk'
|
|
1330
|
+
swarm = SwarmSDK::Swarm.load('swarm.yml')
|
|
1331
|
+
result = swarm.execute("Your task here")
|
|
1332
|
+
```
|
|
1333
|
+
|
|
1334
|
+
### Essential Fields
|
|
1335
|
+
|
|
1336
|
+
**Required**:
|
|
1337
|
+
- `name` - Swarm name (for logging)
|
|
1338
|
+
- `lead` - Entry point agent (symbol)
|
|
1339
|
+
- `description` - Agent's role (required for each agent)
|
|
1340
|
+
|
|
1341
|
+
**Common**:
|
|
1342
|
+
- `model` - LLM to use (e.g., "gpt-4", "claude-sonnet-4")
|
|
1343
|
+
- `system_prompt` - Agent instructions
|
|
1344
|
+
- `tools` - Additional tools (Write, Edit, Bash, etc.)
|
|
1345
|
+
- `delegates_to` - Agents this agent can delegate to
|
|
1346
|
+
- `directory` - Working directory for file operations
|
|
1347
|
+
|
|
1348
|
+
### Default Tools (Always Available)
|
|
1349
|
+
|
|
1350
|
+
Unless `disable_default_tools: true`:
|
|
1351
|
+
- **Read** - Read files
|
|
1352
|
+
- **Grep** - Search file contents
|
|
1353
|
+
- **Glob** - Find files by pattern
|
|
1354
|
+
- **TodoWrite** - Track tasks
|
|
1355
|
+
- **Think** - Extended reasoning
|
|
1356
|
+
- **WebFetch** - Fetch and process web content
|
|
1357
|
+
- **ScratchpadWrite, ScratchpadRead, ScratchpadList** - Shared scratchpad (volatile)
|
|
1358
|
+
|
|
1359
|
+
### Common Tools (Add Explicitly)
|
|
1360
|
+
|
|
1361
|
+
```ruby
|
|
1362
|
+
tools :Write # Create/overwrite files
|
|
1363
|
+
tools :Edit # Modify existing files
|
|
1364
|
+
tools :MultiEdit # Batch file edits
|
|
1365
|
+
tools :Bash # Run shell commands
|
|
1366
|
+
```
|
|
1367
|
+
|
|
1368
|
+
### Delegation Pattern
|
|
1369
|
+
|
|
1370
|
+
```ruby
|
|
1371
|
+
agent :leader do
|
|
1372
|
+
description "Coordinates work"
|
|
1373
|
+
model "gpt-4"
|
|
1374
|
+
delegates_to :worker # Creates DelegateTaskToWorker tool
|
|
1375
|
+
end
|
|
1376
|
+
|
|
1377
|
+
agent :worker do
|
|
1378
|
+
description "Does the work"
|
|
1379
|
+
model "gpt-4"
|
|
1380
|
+
end
|
|
1381
|
+
```
|
|
1382
|
+
|
|
1383
|
+
### Result Object Methods
|
|
1384
|
+
|
|
1385
|
+
```ruby
|
|
1386
|
+
result = swarm.execute("task")
|
|
1387
|
+
|
|
1388
|
+
result.content # Response text
|
|
1389
|
+
result.success? # Boolean
|
|
1390
|
+
result.duration # Float (seconds)
|
|
1391
|
+
result.total_cost # Float (USD)
|
|
1392
|
+
result.total_tokens # Integer
|
|
1393
|
+
result.agents_involved # Array[Symbol]
|
|
1394
|
+
result.error # Exception | nil
|
|
1395
|
+
```
|
|
1396
|
+
|
|
1397
|
+
### Model Examples
|
|
1398
|
+
|
|
1399
|
+
**OpenAI**:
|
|
1400
|
+
- `"gpt-4"` - Most capable
|
|
1401
|
+
- `"gpt-4-turbo"` - Faster, cheaper
|
|
1402
|
+
- `"gpt-3.5-turbo"` - Fast, economical
|
|
1403
|
+
|
|
1404
|
+
**Anthropic**:
|
|
1405
|
+
- `"claude-opus-4"` - Most capable
|
|
1406
|
+
- `"claude-sonnet-4"` - Balanced
|
|
1407
|
+
- `"claude-haiku-4"` - Fast, economical
|
|
1408
|
+
|
|
1409
|
+
### Parameters for Tuning
|
|
1410
|
+
|
|
1411
|
+
```ruby
|
|
1412
|
+
parameters temperature: 0.7 # 0.0 (focused) to 2.0 (creative)
|
|
1413
|
+
parameters top_p: 0.9 # 0.0 to 1.0 (diversity)
|
|
1414
|
+
parameters max_tokens: 2000 # Response length limit
|
|
1415
|
+
```
|
|
1416
|
+
|
|
1417
|
+
### Common Patterns
|
|
1418
|
+
|
|
1419
|
+
**Single agent**:
|
|
1420
|
+
```ruby
|
|
1421
|
+
swarm = SwarmSDK.build do
|
|
1422
|
+
name "Solo"
|
|
1423
|
+
lead :worker
|
|
1424
|
+
agent :worker do
|
|
1425
|
+
description "Does everything"
|
|
1426
|
+
model "gpt-4"
|
|
1427
|
+
end
|
|
1428
|
+
end
|
|
1429
|
+
```
|
|
1430
|
+
|
|
1431
|
+
**Delegation chain**:
|
|
1432
|
+
```ruby
|
|
1433
|
+
swarm = SwarmSDK.build do
|
|
1434
|
+
name "Chain"
|
|
1435
|
+
lead :first
|
|
1436
|
+
agent :first { description "First"; model "gpt-4"; delegates_to :second }
|
|
1437
|
+
agent :second { description "Second"; model "gpt-4"; delegates_to :third }
|
|
1438
|
+
agent :third { description "Third"; model "gpt-4" }
|
|
1439
|
+
end
|
|
1440
|
+
```
|
|
1441
|
+
|
|
1442
|
+
**Parallel team**:
|
|
1443
|
+
```ruby
|
|
1444
|
+
swarm = SwarmSDK.build do
|
|
1445
|
+
name "Team"
|
|
1446
|
+
lead :coordinator
|
|
1447
|
+
agent :coordinator do
|
|
1448
|
+
description "Coordinates"
|
|
1449
|
+
model "gpt-4"
|
|
1450
|
+
delegates_to :frontend, :backend, :testing
|
|
1451
|
+
end
|
|
1452
|
+
agent :frontend { description "Frontend"; model "gpt-4" }
|
|
1453
|
+
agent :backend { description "Backend"; model "gpt-4" }
|
|
1454
|
+
agent :testing { description "Testing"; model "gpt-4" }
|
|
1455
|
+
end
|
|
1456
|
+
```
|