chorus-llm 0.2.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/LICENSE.txt +21 -0
- data/README.md +173 -0
- data/chorus-llm.gemspec +28 -0
- data/lib/chorus/agent.rb +27 -0
- data/lib/chorus/agents/coder_agent.rb +22 -0
- data/lib/chorus/agents/research_agent.rb +22 -0
- data/lib/chorus/client.rb +88 -0
- data/lib/chorus/context.rb +122 -0
- data/lib/chorus/orchestrator.rb +52 -0
- data/lib/chorus/router.rb +33 -0
- data/lib/chorus/version.rb +5 -0
- data/lib/chorus.rb +17 -0
- metadata +59 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e9aaa6c14d9fe4b979246f8fbf95ae878c1e05178ebdffd6c90ad1f9d73c1d17
|
|
4
|
+
data.tar.gz: c4d5473d68812262c3670d972ed42791d758990a204745855d87cc55c94c66c7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f3e1b1df26ef99d02da3bcf161088fe5650d4d959061ad229015b2a95b98687fc5864d34b13038905673353b7ac2fd2b14a4724865432f14fc9f7a3f0752c19b
|
|
7
|
+
data.tar.gz: 62f5a01e91cbc78bc6adcd05e7649ac06a1300067bb37a4d3ff95e16f23044683d314b9fce3a07c96de1982ad1cc5e65c5f58ea834c1be0a1c4711934c0b2c18
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Amayyas
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# Chorus
|
|
2
|
+
|
|
3
|
+
[](https://github.com/Amayyas/Chorus/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/chorus-llm)
|
|
5
|
+
|
|
6
|
+
Chorus is a Ruby framework for orchestrating multiple specialized LLM agents.
|
|
7
|
+
|
|
8
|
+
## The problem
|
|
9
|
+
|
|
10
|
+
Most multi-agent frameworks replay the entire conversation history to every
|
|
11
|
+
agent on every call. It works, but it wastes tokens (money and latency) and
|
|
12
|
+
pollutes each agent's context with exchanges that aren't relevant to it.
|
|
13
|
+
|
|
14
|
+
Chorus takes a different approach: a **router** dynamically decides which
|
|
15
|
+
agent should handle a message, and a **shared context** hands that agent only
|
|
16
|
+
a **relevant slice** of the history:
|
|
17
|
+
|
|
18
|
+
- the current message (the task at hand),
|
|
19
|
+
- its own past exchanges (role-based continuity of memory),
|
|
20
|
+
- a short summary of what other agents have been doing (visibility without pollution).
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
Add this line to your Gemfile:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
gem "chorus-llm"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then `bundle install`. The library loads via `require "chorus"`:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
require "chorus"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To work on this repo:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
bundle install
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Chorus needs an Anthropic API key to make real calls (the test suite never
|
|
43
|
+
makes network calls):
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
export ANTHROPIC_API_KEY="sk-ant-..."
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Minimal usage example
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
require "chorus"
|
|
53
|
+
|
|
54
|
+
orchestrator = Chorus::Orchestrator.new
|
|
55
|
+
|
|
56
|
+
result = orchestrator.handle("There's a bug in my sort function")
|
|
57
|
+
# => { agent: :coder, response: "..." }
|
|
58
|
+
|
|
59
|
+
result = orchestrator.handle("What is the capital of France?")
|
|
60
|
+
# => { agent: :research, response: "..." }
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Under the hood, on every call:
|
|
64
|
+
|
|
65
|
+
1. `Chorus::Router#route` picks the agent (`:coder` or `:research`) based on
|
|
66
|
+
keywords in the message.
|
|
67
|
+
2. `Chorus::Context#slice_for` builds the relevant context slice for that
|
|
68
|
+
agent.
|
|
69
|
+
3. The agent (`Chorus::Agents::CoderAgent` or `Chorus::Agents::ResearchAgent`)
|
|
70
|
+
sends that slice to the Anthropic API via `Chorus::Client`.
|
|
71
|
+
4. The response is recorded in `Chorus::Context`, tagged with the name of the
|
|
72
|
+
agent that produced it.
|
|
73
|
+
|
|
74
|
+
## Running the tests
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
bundle exec rspec # tests only
|
|
78
|
+
bundle exec rubocop # lint only
|
|
79
|
+
bundle exec rake # both (Rakefile default task)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
No test makes a network call — `Chorus::Client` is always mocked.
|
|
83
|
+
|
|
84
|
+
## Running the demo
|
|
85
|
+
|
|
86
|
+
`examples/demo.rb` simulates a 5-message conversation alternating between
|
|
87
|
+
coding and research tasks, and makes REAL API calls (unlike the test suite).
|
|
88
|
+
Requires `ANTHROPIC_API_KEY`:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
ANTHROPIC_API_KEY="sk-ant-..." ruby examples/demo.rb
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
For each message, the script prints the message received, the agent chosen
|
|
95
|
+
by the router, the size of the context slice sent, and the response.
|
|
96
|
+
|
|
97
|
+
## Architecture
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
lib/chorus/
|
|
101
|
+
├── client.rb # sole point of contact with the Anthropic API (net/http)
|
|
102
|
+
├── agent.rb # base class: factors out the call to Client
|
|
103
|
+
├── agents/
|
|
104
|
+
│ ├── coder_agent.rb # :coder — code, debugging, explaining code
|
|
105
|
+
│ └── research_agent.rb # :research — research, synthesis, factual Q&A
|
|
106
|
+
├── context.rb # full history + slice_for (the core of the concept)
|
|
107
|
+
├── router.rb # route(message) -> :coder | :research
|
|
108
|
+
└── orchestrator.rb # entry point: handle(user_message)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## CI/CD
|
|
112
|
+
|
|
113
|
+
### Continuous integration (`.github/workflows/ci.yml`)
|
|
114
|
+
|
|
115
|
+
On every push and pull request:
|
|
116
|
+
|
|
117
|
+
- **Tests** — `bundle exec rspec` across a Ruby 3.2 / 3.3 / 3.4 matrix.
|
|
118
|
+
- **RuboCop** — lint (`.rubocop.yml`, with `rubocop-rspec` and `rubocop-performance`).
|
|
119
|
+
- **bundler-audit** — scans dependencies against the RubyGems advisory database.
|
|
120
|
+
- **Gem build** — `gem build chorus-llm.gemspec` must succeed (catches
|
|
121
|
+
gemspec errors before they break a release).
|
|
122
|
+
|
|
123
|
+
### Automated releases (`.github/workflows/release.yml`)
|
|
124
|
+
|
|
125
|
+
Versioning follows [Conventional Commits](https://www.conventionalcommits.org/)
|
|
126
|
+
via [release-please](https://github.com/googleapis/release-please):
|
|
127
|
+
|
|
128
|
+
1. Every commit on `main` prefixed with `feat:`, `fix:`, `feat!:`, etc. updates
|
|
129
|
+
(or creates) a Release PR that bumps `lib/chorus/version.rb` and generates
|
|
130
|
+
`CHANGELOG.md`.
|
|
131
|
+
2. Merging that PR automatically:
|
|
132
|
+
- creates the Git tag (`vX.Y.Z`) and a GitHub Release,
|
|
133
|
+
- publishes to [RubyGems.org](https://rubygems.org/gems/chorus-llm) via
|
|
134
|
+
[Trusted Publishing](https://guides.rubygems.org/trusted-publishing/)
|
|
135
|
+
(OIDC — no RubyGems API key stored as a GitHub secret).
|
|
136
|
+
|
|
137
|
+
**One-time manual configuration** (not automatable from code):
|
|
138
|
+
|
|
139
|
+
- On RubyGems.org, a trusted publisher links the `chorus-llm` gem to the
|
|
140
|
+
`Amayyas/Chorus` repo, the `release.yml` workflow, and the `release`
|
|
141
|
+
environment.
|
|
142
|
+
- In the repo's GitHub settings, a `release` environment exists
|
|
143
|
+
(Settings → Environments) — used by the publish job.
|
|
144
|
+
- In Settings → Actions → General, *"Allow GitHub Actions to create and
|
|
145
|
+
approve pull requests"* is enabled so release-please can open its Release
|
|
146
|
+
PRs.
|
|
147
|
+
|
|
148
|
+
### Expected commit format
|
|
149
|
+
|
|
150
|
+
release-please needs [Conventional Commits](https://www.conventionalcommits.org/) to know what to bump:
|
|
151
|
+
|
|
152
|
+
| Prefix | Effect |
|
|
153
|
+
|---|---|
|
|
154
|
+
| `fix: ...` | patch (0.1.0 → 0.1.1) |
|
|
155
|
+
| `feat: ...` | minor (0.1.0 → 0.2.0) |
|
|
156
|
+
| `feat!: ...` or `BREAKING CHANGE:` in the body | major (0.1.0 → 1.0.0) |
|
|
157
|
+
| `chore:`, `docs:`, `test:`, `refactor:` | no bump, but listed in the CHANGELOG depending on config |
|
|
158
|
+
|
|
159
|
+
## Roadmap — what's NOT in the MVP (v0.1.0)
|
|
160
|
+
|
|
161
|
+
This version proves the concept with a minimal but complete foundation. It
|
|
162
|
+
intentionally does not include:
|
|
163
|
+
|
|
164
|
+
- **LLM-based router** — routing is currently keyword matching, not a call to
|
|
165
|
+
a classification model.
|
|
166
|
+
- **Persistent long-term memory** — all state lives in the Ruby process's
|
|
167
|
+
memory; nothing is saved to disk or a database.
|
|
168
|
+
- **More than 2 agents** — only `CoderAgent` and `ResearchAgent` exist.
|
|
169
|
+
- **Explicit agent handoff** — an agent cannot delegate to another mid-response;
|
|
170
|
+
each message is handled by a single agent from start to finish.
|
|
171
|
+
- **LLM-generated summaries** — the other-agents summary in `slice_for` is a
|
|
172
|
+
simple concatenation of truncated subjects, not an API call.
|
|
173
|
+
- **Advanced error handling** (retries, backoff, etc.) on API calls.
|
data/chorus-llm.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/chorus/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "chorus-llm"
|
|
7
|
+
spec.version = Chorus::VERSION
|
|
8
|
+
spec.authors = ["Amayyas"]
|
|
9
|
+
spec.email = ["amayyasadn@gmail.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Ruby framework for multi-agent LLM orchestration with contextual routing."
|
|
12
|
+
spec.description = <<~DESC
|
|
13
|
+
Chorus routes each incoming task to the right specialized LLM agent and hands it
|
|
14
|
+
only the relevant slice of shared context, instead of replaying the full
|
|
15
|
+
conversation history to every agent.
|
|
16
|
+
DESC
|
|
17
|
+
spec.homepage = "https://github.com/Amayyas/Chorus"
|
|
18
|
+
spec.license = "MIT"
|
|
19
|
+
spec.required_ruby_version = ">= 3.2.0"
|
|
20
|
+
|
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
22
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
|
24
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
25
|
+
|
|
26
|
+
spec.files = Dir["lib/**/*.rb", "README.md", "LICENSE.txt", "chorus-llm.gemspec"]
|
|
27
|
+
spec.require_paths = ["lib"]
|
|
28
|
+
end
|
data/lib/chorus/agent.rb
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Chorus
|
|
4
|
+
# Base class for every Chorus agent. A concrete agent only needs to define
|
|
5
|
+
# its `name` and `system_prompt` — the call to the LLM is factored out here.
|
|
6
|
+
class Agent
|
|
7
|
+
# @return [Symbol] the agent's identifier, e.g. :coder
|
|
8
|
+
attr_reader :name
|
|
9
|
+
|
|
10
|
+
# @return [String] the system prompt describing this agent's role and limits
|
|
11
|
+
attr_reader :system_prompt
|
|
12
|
+
|
|
13
|
+
# @param client [Chorus::Client] the API client used to talk to Claude
|
|
14
|
+
def initialize(client:)
|
|
15
|
+
@client = client
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Sends a context slice to the LLM and returns its text response.
|
|
19
|
+
#
|
|
20
|
+
# @param context_slice [Array<Hash>] messages formatted as `{role:, content:}`,
|
|
21
|
+
# as produced by `Chorus::Context#slice_for`.
|
|
22
|
+
# @return [String] the agent's response text
|
|
23
|
+
def call(context_slice)
|
|
24
|
+
@client.chat(system_prompt: system_prompt, messages: context_slice)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../agent"
|
|
4
|
+
|
|
5
|
+
module Chorus
|
|
6
|
+
module Agents
|
|
7
|
+
# Handles code-related tasks: writing, debugging, and explaining code.
|
|
8
|
+
class CoderAgent < Chorus::Agent
|
|
9
|
+
SYSTEM_PROMPT = <<~PROMPT
|
|
10
|
+
You are Chorus's coding agent. You write, debug, and explain code.
|
|
11
|
+
Be precise and give runnable examples when relevant. If a task is not
|
|
12
|
+
related to code, say so briefly instead of improvising an answer.
|
|
13
|
+
PROMPT
|
|
14
|
+
|
|
15
|
+
def initialize(client:)
|
|
16
|
+
super
|
|
17
|
+
@name = :coder
|
|
18
|
+
@system_prompt = SYSTEM_PROMPT
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../agent"
|
|
4
|
+
|
|
5
|
+
module Chorus
|
|
6
|
+
module Agents
|
|
7
|
+
# Handles research/factual/explanatory tasks: information lookup and synthesis.
|
|
8
|
+
class ResearchAgent < Chorus::Agent
|
|
9
|
+
SYSTEM_PROMPT = <<~PROMPT
|
|
10
|
+
You are Chorus's research agent. You answer questions that require
|
|
11
|
+
factual explanation, synthesis of information, or general knowledge.
|
|
12
|
+
Be concise and cite the reasoning behind your answer when useful.
|
|
13
|
+
PROMPT
|
|
14
|
+
|
|
15
|
+
def initialize(client:)
|
|
16
|
+
super
|
|
17
|
+
@name = :research
|
|
18
|
+
@system_prompt = SYSTEM_PROMPT
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "json"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Chorus
|
|
8
|
+
# Raised when ANTHROPIC_API_KEY is not set in the environment.
|
|
9
|
+
class MissingAPIKeyError < StandardError
|
|
10
|
+
def initialize(msg = "ANTHROPIC_API_KEY is not set. Export it before using Chorus.")
|
|
11
|
+
super
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Raised when the Anthropic API returns a non-2xx response.
|
|
16
|
+
class APIError < StandardError; end
|
|
17
|
+
|
|
18
|
+
# Thin wrapper around the Anthropic Messages API. This is the ONLY class in
|
|
19
|
+
# Chorus allowed to perform HTTP calls — every agent goes through it.
|
|
20
|
+
class Client
|
|
21
|
+
API_URL = "https://api.anthropic.com/v1/messages"
|
|
22
|
+
ANTHROPIC_VERSION = "2023-06-01"
|
|
23
|
+
DEFAULT_MODEL = "claude-opus-4-8"
|
|
24
|
+
DEFAULT_MAX_TOKENS = 4096
|
|
25
|
+
|
|
26
|
+
# @param api_key [String, nil] Anthropic API key. Defaults to ENV["ANTHROPIC_API_KEY"].
|
|
27
|
+
# @param model [String] model id to use for every call made by this client.
|
|
28
|
+
def initialize(api_key: ENV.fetch("ANTHROPIC_API_KEY", nil), model: DEFAULT_MODEL)
|
|
29
|
+
raise MissingAPIKeyError if api_key.nil? || api_key.empty?
|
|
30
|
+
|
|
31
|
+
@api_key = api_key
|
|
32
|
+
@model = model
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sends a single-turn (or multi-turn) request to the Messages API.
|
|
36
|
+
#
|
|
37
|
+
# @param system_prompt [String] the system prompt describing the agent's role.
|
|
38
|
+
# @param messages [Array<Hash>] conversation turns, each `{role:, content:}`.
|
|
39
|
+
# @param max_tokens [Integer] maximum tokens to generate.
|
|
40
|
+
# @return [String] the text of Claude's response.
|
|
41
|
+
def chat(system_prompt:, messages:, max_tokens: DEFAULT_MAX_TOKENS)
|
|
42
|
+
response = post_message(system_prompt: system_prompt, messages: messages, max_tokens: max_tokens)
|
|
43
|
+
extract_text(response)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def post_message(system_prompt:, messages:, max_tokens:)
|
|
49
|
+
uri = URI(API_URL)
|
|
50
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
51
|
+
http.use_ssl = true
|
|
52
|
+
|
|
53
|
+
request = build_request(uri, system_prompt: system_prompt, messages: messages, max_tokens: max_tokens)
|
|
54
|
+
response = http.request(request)
|
|
55
|
+
|
|
56
|
+
# The interpolated error message is clearer on one line than split
|
|
57
|
+
# across a modifier-if, even though it runs right up against the
|
|
58
|
+
# line-length limit.
|
|
59
|
+
# rubocop:disable Style/IfUnlessModifier
|
|
60
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
61
|
+
raise APIError, "Anthropic API error (#{response.code}): #{response.body}"
|
|
62
|
+
end
|
|
63
|
+
# rubocop:enable Style/IfUnlessModifier
|
|
64
|
+
|
|
65
|
+
JSON.parse(response.body)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def build_request(uri, system_prompt:, messages:, max_tokens:)
|
|
69
|
+
request = Net::HTTP::Post.new(uri)
|
|
70
|
+
request["Content-Type"] = "application/json"
|
|
71
|
+
request["x-api-key"] = @api_key
|
|
72
|
+
request["anthropic-version"] = ANTHROPIC_VERSION
|
|
73
|
+
request.body = JSON.generate(
|
|
74
|
+
model: @model,
|
|
75
|
+
max_tokens: max_tokens,
|
|
76
|
+
system: system_prompt,
|
|
77
|
+
messages: messages
|
|
78
|
+
)
|
|
79
|
+
request
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def extract_text(response)
|
|
83
|
+
content = response.fetch("content", [])
|
|
84
|
+
text_block = content.find { |block| block["type"] == "text" }
|
|
85
|
+
text_block ? text_block["text"] : ""
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Chorus
|
|
4
|
+
# Represents the full shared state of a conversation, and knows how to carve
|
|
5
|
+
# out the slice of it that is relevant to a given agent and task.
|
|
6
|
+
#
|
|
7
|
+
# This class is the heart of Chorus's concept: instead of replaying the
|
|
8
|
+
# entire conversation to every agent, `slice_for` builds a small, targeted
|
|
9
|
+
# view of the context. See the comments inside `slice_for` for the exact
|
|
10
|
+
# rules — this logic is intentionally simple in v0.1.0 (no LLM calls) and
|
|
11
|
+
# is the primary thing to refine in v0.2.0.
|
|
12
|
+
class Context
|
|
13
|
+
Message = Struct.new(:role, :content, :agent, :timestamp, keyword_init: true)
|
|
14
|
+
|
|
15
|
+
# How many characters of a task we keep when summarizing another agent's
|
|
16
|
+
# activity for a teammate. Kept short on purpose — it's a pointer, not a
|
|
17
|
+
# transcript.
|
|
18
|
+
SUBJECT_LENGTH = 60
|
|
19
|
+
|
|
20
|
+
def initialize
|
|
21
|
+
@messages = []
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @param role [Symbol] :user or :assistant
|
|
25
|
+
# @param content [String] the message text
|
|
26
|
+
# @param agent [Symbol, nil] which agent produced this message (nil for user messages)
|
|
27
|
+
# @return [void]
|
|
28
|
+
def add_message(role:, content:, agent: nil)
|
|
29
|
+
@messages << Message.new(role: role, content: content, agent: agent, timestamp: Time.now)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @return [Array<Message>] the complete, unfiltered history — for debugging/logging only.
|
|
33
|
+
def full_history
|
|
34
|
+
@messages.dup
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Builds the context slice for `agent_name` working on `task`.
|
|
38
|
+
#
|
|
39
|
+
# The slice always contains, in order:
|
|
40
|
+
# 1. A one-line summary of what OTHER agents have been doing, so this
|
|
41
|
+
# agent has situational awareness without the full transcript of
|
|
42
|
+
# their conversations. Built by concatenating short subjects
|
|
43
|
+
# (truncated prior task text) — no LLM call involved in v0.1.0.
|
|
44
|
+
# 2. This agent's own past (task, response) pairs, so it remembers what
|
|
45
|
+
# it has already said — continuity of memory per role.
|
|
46
|
+
# 3. The current task, which is always the final entry.
|
|
47
|
+
#
|
|
48
|
+
# The Anthropic API only requires the conversation to start with a `user`
|
|
49
|
+
# turn; consecutive same-role turns are merged server-side, so we don't
|
|
50
|
+
# need to strictly alternate roles here.
|
|
51
|
+
#
|
|
52
|
+
# @param agent_name [Symbol] the agent about to handle `task`, e.g. :coder
|
|
53
|
+
# @param task [String] the current user message
|
|
54
|
+
# @return [Array<Hash>] messages shaped as `{role:, content:}`, ready for `Chorus::Client#chat`
|
|
55
|
+
def slice_for(agent_name, task)
|
|
56
|
+
slice = []
|
|
57
|
+
|
|
58
|
+
summary = other_agents_summary(agent_name)
|
|
59
|
+
slice << { role: "user", content: "[Other agents' context] #{summary}" } if summary
|
|
60
|
+
|
|
61
|
+
slice.concat(own_history_for(agent_name))
|
|
62
|
+
|
|
63
|
+
slice << { role: "user", content: task }
|
|
64
|
+
slice
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
# Reconstructs (task, response) pairs previously produced by `agent_name`,
|
|
70
|
+
# so it can pick up where it left off instead of starting cold each time.
|
|
71
|
+
def own_history_for(agent_name)
|
|
72
|
+
pairs = []
|
|
73
|
+
|
|
74
|
+
@messages.each_with_index do |message, index|
|
|
75
|
+
next unless message.role == :assistant && message.agent == agent_name
|
|
76
|
+
|
|
77
|
+
preceding_task = preceding_user_message(index)
|
|
78
|
+
pairs << { role: "user", content: preceding_task } if preceding_task
|
|
79
|
+
pairs << { role: "assistant", content: message.content }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
pairs
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Short, no-LLM summary of what agents OTHER than `agent_name` have been
|
|
86
|
+
# asked to do, so `agent_name` has visibility without full pollution.
|
|
87
|
+
def other_agents_summary(agent_name)
|
|
88
|
+
subjects = @messages.each_with_index.filter_map do |message, index|
|
|
89
|
+
next unless other_agent_response?(message, agent_name)
|
|
90
|
+
|
|
91
|
+
preceding_task = preceding_user_message(index)
|
|
92
|
+
next unless preceding_task
|
|
93
|
+
|
|
94
|
+
"#{message.agent}: #{truncate(preceding_task)}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
return nil if subjects.empty?
|
|
98
|
+
|
|
99
|
+
subjects.uniq.join("; ")
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def other_agent_response?(message, agent_name)
|
|
103
|
+
message.role == :assistant && message.agent && message.agent != agent_name
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @return [String, nil] the content of the user message right before
|
|
107
|
+
# `@messages[index]`, or nil if there isn't one (e.g. this is the first
|
|
108
|
+
# message, or the previous entry wasn't a user turn).
|
|
109
|
+
def preceding_user_message(index)
|
|
110
|
+
return nil unless index.positive?
|
|
111
|
+
|
|
112
|
+
preceding = @messages[index - 1]
|
|
113
|
+
preceding.content if preceding.role == :user
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def truncate(text)
|
|
117
|
+
return text if text.length <= SUBJECT_LENGTH
|
|
118
|
+
|
|
119
|
+
"#{text[0, SUBJECT_LENGTH]}…"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "context"
|
|
4
|
+
require_relative "router"
|
|
5
|
+
require_relative "agents/coder_agent"
|
|
6
|
+
require_relative "agents/research_agent"
|
|
7
|
+
|
|
8
|
+
module Chorus
|
|
9
|
+
# Main entry point of Chorus. Ties together the Router, Context and Agents:
|
|
10
|
+
# for every incoming user message it picks an agent, builds that agent's
|
|
11
|
+
# context slice, calls it, and records the response.
|
|
12
|
+
class Orchestrator
|
|
13
|
+
# @return [Chorus::Context] the shared conversation state
|
|
14
|
+
attr_reader :context
|
|
15
|
+
|
|
16
|
+
# @param client [Chorus::Client] API client shared by all agents
|
|
17
|
+
# @param router [Chorus::Router] decides which agent handles a message
|
|
18
|
+
# @param context [Chorus::Context] shared conversation state
|
|
19
|
+
# @param agents [Hash{Symbol => Chorus::Agent}] agent name => agent instance
|
|
20
|
+
def initialize(client: Chorus::Client.new, router: Chorus::Router.new, context: Chorus::Context.new, agents: nil)
|
|
21
|
+
@client = client
|
|
22
|
+
@router = router
|
|
23
|
+
@context = context
|
|
24
|
+
@agents = agents || default_agents
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @param user_message [String] the incoming message to handle
|
|
28
|
+
# @return [Hash] `{agent: Symbol, response: String}`
|
|
29
|
+
def handle(user_message)
|
|
30
|
+
agent_name = @router.route(user_message)
|
|
31
|
+
agent = @agents.fetch(agent_name) { raise ArgumentError, "Unknown agent: #{agent_name}" }
|
|
32
|
+
|
|
33
|
+
@context.add_message(role: :user, content: user_message)
|
|
34
|
+
|
|
35
|
+
context_slice = @context.slice_for(agent_name, user_message)
|
|
36
|
+
response = agent.call(context_slice)
|
|
37
|
+
|
|
38
|
+
@context.add_message(role: :assistant, content: response, agent: agent_name)
|
|
39
|
+
|
|
40
|
+
{ agent: agent_name, response: response }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def default_agents
|
|
46
|
+
{
|
|
47
|
+
coder: Chorus::Agents::CoderAgent.new(client: @client),
|
|
48
|
+
research: Chorus::Agents::ResearchAgent.new(client: @client)
|
|
49
|
+
}
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Chorus
|
|
4
|
+
# Decides which agent should handle a given user message.
|
|
5
|
+
#
|
|
6
|
+
# v0.1.0 uses simple keyword matching (no LLM call). The public interface
|
|
7
|
+
# is a single method, `route`, so this class can be swapped for an
|
|
8
|
+
# LLM-based router later without touching `Orchestrator`.
|
|
9
|
+
class Router
|
|
10
|
+
# Words that, when present in a message, indicate a coding task.
|
|
11
|
+
CODER_KEYWORDS = %w[code bug function error debug script class method variable
|
|
12
|
+
exception refactor].freeze
|
|
13
|
+
|
|
14
|
+
DEFAULT_AGENT = :research
|
|
15
|
+
|
|
16
|
+
# @param message [String] the raw user message
|
|
17
|
+
# @return [Symbol] the agent name that should handle this message (:coder or :research)
|
|
18
|
+
def route(message)
|
|
19
|
+
coding_task?(message) ? :coder : DEFAULT_AGENT
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def coding_task?(message)
|
|
25
|
+
normalized = message.downcase
|
|
26
|
+
# rubocop:disable Style/ArrayIntersect -- `normalized` is a String, not
|
|
27
|
+
# an Array; Array#intersect? would raise a TypeError here. This is a
|
|
28
|
+
# substring check on each keyword, not an array-element intersection.
|
|
29
|
+
CODER_KEYWORDS.any? { |keyword| normalized.include?(keyword) }
|
|
30
|
+
# rubocop:enable Style/ArrayIntersect
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
data/lib/chorus.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "chorus/version"
|
|
4
|
+
require_relative "chorus/client"
|
|
5
|
+
require_relative "chorus/agent"
|
|
6
|
+
require_relative "chorus/agents/coder_agent"
|
|
7
|
+
require_relative "chorus/agents/research_agent"
|
|
8
|
+
require_relative "chorus/context"
|
|
9
|
+
require_relative "chorus/router"
|
|
10
|
+
require_relative "chorus/orchestrator"
|
|
11
|
+
|
|
12
|
+
# Chorus is a Ruby framework for orchestrating multiple specialized LLM
|
|
13
|
+
# agents. A Router decides which agent should handle an incoming message, and
|
|
14
|
+
# a Context builds a targeted slice of shared conversation state for that
|
|
15
|
+
# agent — instead of replaying the full history to every call.
|
|
16
|
+
module Chorus
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chorus-llm
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Amayyas
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: |
|
|
13
|
+
Chorus routes each incoming task to the right specialized LLM agent and hands it
|
|
14
|
+
only the relevant slice of shared context, instead of replaying the full
|
|
15
|
+
conversation history to every agent.
|
|
16
|
+
email:
|
|
17
|
+
- amayyasadn@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- LICENSE.txt
|
|
23
|
+
- README.md
|
|
24
|
+
- chorus-llm.gemspec
|
|
25
|
+
- lib/chorus.rb
|
|
26
|
+
- lib/chorus/agent.rb
|
|
27
|
+
- lib/chorus/agents/coder_agent.rb
|
|
28
|
+
- lib/chorus/agents/research_agent.rb
|
|
29
|
+
- lib/chorus/client.rb
|
|
30
|
+
- lib/chorus/context.rb
|
|
31
|
+
- lib/chorus/orchestrator.rb
|
|
32
|
+
- lib/chorus/router.rb
|
|
33
|
+
- lib/chorus/version.rb
|
|
34
|
+
homepage: https://github.com/Amayyas/Chorus
|
|
35
|
+
licenses:
|
|
36
|
+
- MIT
|
|
37
|
+
metadata:
|
|
38
|
+
homepage_uri: https://github.com/Amayyas/Chorus
|
|
39
|
+
source_code_uri: https://github.com/Amayyas/Chorus
|
|
40
|
+
changelog_uri: https://github.com/Amayyas/Chorus/blob/main/CHANGELOG.md
|
|
41
|
+
rubygems_mfa_required: 'true'
|
|
42
|
+
rdoc_options: []
|
|
43
|
+
require_paths:
|
|
44
|
+
- lib
|
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - ">="
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: 3.2.0
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
requirements: []
|
|
56
|
+
rubygems_version: 4.0.10
|
|
57
|
+
specification_version: 4
|
|
58
|
+
summary: Ruby framework for multi-agent LLM orchestration with contextual routing.
|
|
59
|
+
test_files: []
|