little_ghost 0.3.0 → 0.4.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 +4 -4
- data/README.md +68 -84
- data/docs/guides/assemblies.md +286 -0
- data/docs/guides/core_concepts.md +126 -231
- data/docs/guides/getting_started.md +114 -87
- data/docs/guides/production.md +187 -0
- data/docs/guides/prompt_views.md +132 -0
- data/lib/little_ghost/ag_ui/adapter.rb +3 -3
- data/lib/little_ghost/agent/delegation.rb +1 -1
- data/lib/little_ghost/agent.rb +167 -172
- data/lib/little_ghost/{agent_interruptions.rb → agent_interjections.rb} +12 -12
- data/lib/little_ghost/assembly.rb +55 -21
- data/lib/little_ghost/assembly_builder.rb +40 -2
- data/lib/little_ghost/assembly_execution.rb +87 -4
- data/lib/little_ghost/configuration.rb +263 -39
- data/lib/little_ghost/content.rb +5 -5
- data/lib/little_ghost/data_map.rb +209 -0
- data/lib/little_ghost/errors.rb +2 -2
- data/lib/little_ghost/execution.rb +32 -32
- data/lib/little_ghost/graph.rb +22 -3
- data/lib/little_ghost/message.rb +4 -4
- data/lib/little_ghost/model_resolver.rb +2 -2
- data/lib/little_ghost/prompt_resolver.rb +2 -0
- data/lib/little_ghost/run.rb +87 -49
- data/lib/little_ghost/run_context.rb +33 -20
- data/lib/little_ghost/runtime/hook.rb +3 -3
- data/lib/little_ghost/runtime.rb +71 -31
- data/lib/little_ghost/session.rb +12 -23
- data/lib/little_ghost/session_store.rb +9 -5
- data/lib/little_ghost/session_stores/agent_core_memory.rb +64 -56
- data/lib/little_ghost/session_stores/filesystem.rb +261 -0
- data/lib/little_ghost/session_stores/memory.rb +7 -0
- data/lib/little_ghost/subagents/manager.rb +42 -42
- data/lib/little_ghost/swarm.rb +13 -5
- data/lib/little_ghost/tool.rb +56 -14
- data/lib/little_ghost/tools/write_todos.rb +6 -1
- data/lib/little_ghost/tracing/open_telemetry.rb +1 -1
- data/lib/little_ghost/version.rb +1 -1
- data/lib/little_ghost/workflow.rb +30 -21
- data/lib/little_ghost.rb +29 -25
- metadata +7 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a2136594414b7f1c810eba51718916646506a4816ced53cf088034e091a0af5c
|
|
4
|
+
data.tar.gz: a7a2a279680b64f936096f70772374b3dcfc1ba21dfb2e428da91be22d385af4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3c78c760b8b9be5bb9aa3394fffdcc0735173366826b263534421c9755f0b0f458b4d14f75c65bee76bb8da839beb67917e6ce222d438073bef95f9258c23ec0
|
|
7
|
+
data.tar.gz: 6effec94de30f7b4e1ed5fcad776cd9579da8645a95ca510ebe5ee5a26921ded2c34eae8f50b70295e326c6874135cd7293de202874980da957ca73558852fe9
|
data/README.md
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
|
-
# Build AI features
|
|
1
|
+
# Build AI features that feel at home in Ruby
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
LittleGhost is a Ruby library for building AI features with agents and composable assemblies. With `OPENROUTER_API_KEY` set, start with one class, give it a prompt, and call it like the rest of your application code:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
```ruby
|
|
6
|
+
require "little_ghost"
|
|
7
|
+
|
|
8
|
+
class CustomerSupportAgent < LittleGhost::Agent
|
|
9
|
+
model "openrouter:openai/gpt-5.6-luna"
|
|
10
|
+
system_prompt "Answer customer questions clearly and concisely."
|
|
11
|
+
end
|
|
6
12
|
|
|
7
|
-
|
|
13
|
+
run = CustomerSupportAgent.ask("Draft a friendly greeting for a customer.")
|
|
14
|
+
run.response
|
|
15
|
+
# One possible response: Hi! How can I help today?
|
|
16
|
+
```
|
|
8
17
|
|
|
9
|
-
|
|
18
|
+
That small definition is already a complete agent. LittleGhost makes the model call, tracks usage, supports streaming, and closes the resources it creates for the request. Add a tool when the agent needs something from your application. Bring in more agents when the work grows.
|
|
10
19
|
|
|
11
|
-
|
|
20
|
+
Model requests may send system instructions, caller input, conversation history, tool results, and attachments to the selected external provider. Model wording can vary between runs. Choose providers and the data you send them with the same care as any other external service.
|
|
21
|
+
|
|
22
|
+
## Install the gem
|
|
23
|
+
|
|
24
|
+
LittleGhost requires Ruby 3.3 or newer. Add it to your bundle and provide a provider credential:
|
|
12
25
|
|
|
13
26
|
```ruby
|
|
14
|
-
|
|
27
|
+
gem "little_ghost"
|
|
28
|
+
```
|
|
15
29
|
|
|
30
|
+
```sh
|
|
31
|
+
$ bundle install
|
|
32
|
+
$ export OPENROUTER_API_KEY="..."
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
OpenRouter keeps the first setup to one credential. It is not required: LittleGhost also includes adapters for OpenAI-compatible APIs, Anthropic, Gemini, Vertex AI, and Bedrock. [Running in Production](docs/guides/production.md) shows how to configure providers and give model choices application-facing names.
|
|
36
|
+
|
|
37
|
+
LittleGhost runs inside your Ruby process. Use it from a controller, job, CLI, or service. If you want a conventional layout, start with `app/agents`, `app/assemblies`, `app/prompts`, and `app/tools`.
|
|
38
|
+
|
|
39
|
+
## Give an agent real capabilities
|
|
40
|
+
|
|
41
|
+
Tools let an agent call focused parts of your application:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
16
44
|
class HelpCenterLookupTool < LittleGhost::Tool
|
|
17
45
|
description "Look up a help center entry by topic."
|
|
18
46
|
input_schema(
|
|
@@ -29,39 +57,33 @@ class HelpCenterLookupTool < LittleGhost::Tool
|
|
|
29
57
|
end
|
|
30
58
|
|
|
31
59
|
class CustomerSupportAgent < LittleGhost::Agent
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
system_prompt "Answer clearly. Check the help center before stating company guidance."
|
|
60
|
+
model "openrouter:openai/gpt-5.6-luna"
|
|
61
|
+
system_prompt "Check the help center before stating company guidance."
|
|
35
62
|
tools HelpCenterLookupTool
|
|
36
63
|
end
|
|
37
|
-
|
|
38
|
-
run = CustomerSupportAgent.ask("Can I get a refund after two weeks?")
|
|
39
|
-
puts run.response
|
|
40
|
-
# One possible response:
|
|
41
|
-
# Refunds are available within 30 days, so your purchase is eligible.
|
|
42
64
|
```
|
|
43
65
|
|
|
44
|
-
The
|
|
66
|
+
The schema checks the shape of the input. Your Ruby code still decides whether the operation is allowed and safe. The result goes back to the model as context.
|
|
67
|
+
|
|
68
|
+
## Grow without changing the caller
|
|
69
|
+
|
|
70
|
+
An **agent** owns one model loop. An **assembly** is one or more agents working as a unit. You call either one the same way:
|
|
45
71
|
|
|
46
72
|
```ruby
|
|
47
|
-
CustomerSupportAgent.
|
|
48
|
-
|
|
49
|
-
|
|
73
|
+
CustomerSupportAgent.ask(question)
|
|
74
|
+
ResponseWorkflow.ask(question)
|
|
75
|
+
ProblemSolverSwarm.ask(question)
|
|
76
|
+
SupportFlowGraph.ask(question)
|
|
50
77
|
```
|
|
51
78
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
One agent is the smallest useful LittleGhost application. A request creates a run, the agent may call a tool or delegate to a subagent, and the run records the outcome:
|
|
79
|
+
Choose the coordination style that matches who should control the next step:
|
|
55
80
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
│
|
|
61
|
-
└────────> ResearchAgent subagent
|
|
62
|
-
```
|
|
81
|
+
- A **subagent** lets a model delegate an addressable task.
|
|
82
|
+
- A **workflow** uses ordinary Ruby for ordering and branching.
|
|
83
|
+
- A **swarm** lets configured agents choose permitted handoffs.
|
|
84
|
+
- A **graph** makes allowed routes explicit as nodes and edges.
|
|
63
85
|
|
|
64
|
-
|
|
86
|
+
A Workflow or Graph can contain agents, other assemblies, or both. Named classes are the clearest place to begin. Builders are there when your application discovers the participants or routes at runtime.
|
|
65
87
|
|
|
66
88
|
```text
|
|
67
89
|
request ──> CustomerSupportAgent
|
|
@@ -73,67 +95,29 @@ request ──> ProblemSolverSwarm ──> TriageAgent ──handoff──> Bill
|
|
|
73
95
|
request ──> SupportFlowGraph ──> TriageAgent ──edge──> ResponseAgent
|
|
74
96
|
```
|
|
75
97
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
Each top-level execution owns one run lifecycle. The run checkpoints session state, closes its resources, aggregates usage, and emits framework events regardless of which assembly type is the entrypoint.
|
|
81
|
-
|
|
82
|
-
## Installation and configuration
|
|
83
|
-
|
|
84
|
-
LittleGhost requires Ruby 3.3 or newer. Add it to your bundle:
|
|
85
|
-
|
|
86
|
-
```ruby
|
|
87
|
-
gem "little_ghost"
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
Then run `bundle install`. Built-in OpenAI-compatible, OpenRouter, Anthropic, Gemini, Vertex AI, and Amazon Bedrock integrations use Ruby's standard library and normalize responses into the same protocol.
|
|
91
|
-
|
|
92
|
-
Configuration does not require a particular directory layout. Provider connections and model profiles resolve independently in this order:
|
|
93
|
-
|
|
94
|
-
1. An inline `config.providers` or `config.models` declaration.
|
|
95
|
-
2. The corresponding explicit `config.providers_path` or `config.models_path`.
|
|
96
|
-
3. The optional conventional file under `config/little_ghost/`.
|
|
97
|
-
4. Environment-based provider selection and the built-in `default` profile.
|
|
98
|
-
|
|
99
|
-
An explicit path must exist. A missing conventional file is valid. The conventional form keeps connection policy separate from model roles:
|
|
100
|
-
|
|
101
|
-
```yaml
|
|
102
|
-
# config/little_ghost/providers.yml
|
|
103
|
-
providers:
|
|
104
|
-
openai:
|
|
105
|
-
adapter: openai
|
|
106
|
-
api_key: <%= ENV.fetch("OPENAI_API_KEY") %>
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
```yaml
|
|
110
|
-
# config/little_ghost/models.yml
|
|
111
|
-
default_model: customer_support
|
|
112
|
-
models:
|
|
113
|
-
customer_support:
|
|
114
|
-
target: openai:gpt-5.6-luna
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
By default, LittleGhost maps `default` to GPT-5.6 Luna. It configures conventional OpenRouter and OpenAI connections from nonblank `LITTLEGHOST_OPENROUTER_API_KEY`, `LITTLEGHOST_OPENAI_API_KEY`, `OPENROUTER_API_KEY`, and `OPENAI_API_KEY` values; that order determines the default when more than one provider is available. Model inputs—including prompts, history, tool data, and attachments—leave the application for the selected external provider. Configure providers explicitly when provider choice or data residency matters.
|
|
118
|
-
|
|
119
|
-
Applications that need custom routing can subclass `LittleGhost::ModelResolver` and install the class with `config.model_resolver`. A custom resolver owns its profiles and default role; configuring `models`, `models_path`, or `default_model` at the same time is an error. Provider configuration remains available to the resolver.
|
|
98
|
+
The result stays familiar too. Every call returns a `Run` with the response,
|
|
99
|
+
outcome, usage, and any final error. A coordinated assembly also records which
|
|
100
|
+
participants ran. Use `.stream_ask` to watch the work as it happens.
|
|
120
101
|
|
|
121
|
-
|
|
102
|
+
**Growing in public.** LittleGhost is under active development, and interfaces may evolve between releases. Pin the gem version and review release notes when upgrading.
|
|
122
103
|
|
|
123
|
-
##
|
|
104
|
+
## Keep going
|
|
124
105
|
|
|
125
|
-
- [Getting Started](docs/guides/getting_started.md)
|
|
126
|
-
- [Core Concepts](docs/guides/core_concepts.md)
|
|
127
|
-
- [
|
|
106
|
+
- [Getting Started](docs/guides/getting_started.md) takes you from installation to a tool-backed, streaming agent.
|
|
107
|
+
- [Core Concepts](docs/guides/core_concepts.md) builds the mental model from Agent to Assembly.
|
|
108
|
+
- [Compose Agents](docs/guides/assemblies.md) walks through workflows, swarms, graphs, nesting, and builders.
|
|
109
|
+
- [Prompts as Views](docs/guides/prompt_views.md) gives growing instructions, shared pieces, and application values a natural home.
|
|
110
|
+
- [Running in Production](docs/guides/production.md) covers configuration, sessions, execution, observability, and trust boundaries.
|
|
111
|
+
- [API reference](rdoc-ref:LittleGhost) provides exact signatures and lifecycle contracts.
|
|
128
112
|
|
|
129
|
-
|
|
113
|
+
### For contributors
|
|
130
114
|
|
|
131
115
|
See the [contributing guide](https://github.com/mattyr/little_ghost/blob/main/CONTRIBUTING.md), [Code of Conduct](https://github.com/mattyr/little_ghost/blob/main/CODE_OF_CONDUCT.md), and [security policy](https://github.com/mattyr/little_ghost/blob/main/SECURITY.md).
|
|
132
116
|
|
|
133
117
|
```sh
|
|
134
|
-
bundle install
|
|
135
|
-
bundle exec rake test
|
|
136
|
-
bundle exec standardrb --no-fix
|
|
118
|
+
$ bundle install
|
|
119
|
+
$ bundle exec rake test
|
|
120
|
+
$ bundle exec standardrb --no-fix
|
|
137
121
|
```
|
|
138
122
|
|
|
139
|
-
LittleGhost is
|
|
123
|
+
LittleGhost is available under the MIT License.
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# Compose Agents
|
|
2
|
+
|
|
3
|
+
An Assembly lets several participants answer through the same familiar calls as one Agent. This guide grows the customer-support example through each coordination style, then shows how to nest and construct assemblies dynamically.
|
|
4
|
+
|
|
5
|
+
## Start with the shared contract
|
|
6
|
+
|
|
7
|
+
Callers do not need a branch for each implementation:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
entrypoint = urgent? ? EscalationWorkflow : CustomerSupportAgent
|
|
11
|
+
run = entrypoint.ask(question)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`Agent`, `Workflow`, `Swarm`, and `Graph` all answer through the Assembly calling style. They differ in how they coordinate work, not in how your application calls them.
|
|
15
|
+
|
|
16
|
+
## Use a Workflow for explicit application logic
|
|
17
|
+
|
|
18
|
+
A Workflow's `perform` method is ordinary Ruby. Inside it, `invoke` prepares a child call. Read `.output` when you need an intermediate answer. Return the final `invoke` call untouched so its response can stream to the caller.
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
class ResponseWorkflow < LittleGhost::Workflow
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def perform
|
|
25
|
+
research = invoke(ResearchAgent).output
|
|
26
|
+
|
|
27
|
+
invoke CustomerSupportAgent, input: <<~PROMPT
|
|
28
|
+
#{input.text}
|
|
29
|
+
|
|
30
|
+
Verified research:
|
|
31
|
+
#{research}
|
|
32
|
+
PROMPT
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
run = ResponseWorkflow.ask("Why is transfer 481 pending?")
|
|
37
|
+
run.response
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Every participant passed to `invoke` can be an Agent or another Assembly. By default, each child receives the caller's history and application context. Pass `history: []`, `context: {}`, or redacted values when a child should see less.
|
|
41
|
+
|
|
42
|
+
Each child Agent keeps its own [prompt view](prompt_views.md). The Workflow supplies request-specific input; it does not replace that Agent's reusable system instructions.
|
|
43
|
+
|
|
44
|
+
The last child is special because its events become the Workflow's public stream. Return that `invoke` without consuming it:
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
# Wrong: this returns a String after consuming the final invocation.
|
|
48
|
+
def perform
|
|
49
|
+
invoke(CustomerSupportAgent).output
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Right: this returns the lazy invocation itself.
|
|
53
|
+
def perform
|
|
54
|
+
invoke CustomerSupportAgent
|
|
55
|
+
end
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The first version produces a failed top-level Run whose error is `ProtocolError`. Use `.output` only when Ruby needs an intermediate answer before choosing the next step.
|
|
59
|
+
|
|
60
|
+
### Choose a branch in Ruby
|
|
61
|
+
|
|
62
|
+
Each branch should end with its final unconsumed invocation:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
class RoutedResponseWorkflow < LittleGhost::Workflow
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def perform
|
|
69
|
+
route = invoke(TriageAgent, as: :triage).output
|
|
70
|
+
|
|
71
|
+
if route == "billing"
|
|
72
|
+
invoke BillingAgent, as: :billing_response
|
|
73
|
+
else
|
|
74
|
+
invoke CustomerSupportAgent, as: :general_response
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`as:` gives the child a readable participant name in steps, trajectories, and telemetry. It does not change which Assembly runs.
|
|
81
|
+
|
|
82
|
+
### Run independent work in parallel
|
|
83
|
+
|
|
84
|
+
Use `parallel` when several inputs can be processed independently:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
class InvestigationWorkflow < LittleGhost::Workflow
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def perform
|
|
91
|
+
findings = parallel(
|
|
92
|
+
invoke(LedgerResearchAgent),
|
|
93
|
+
invoke(PolicyResearchAgent),
|
|
94
|
+
max_concurrency: 2
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
invoke CustomerSupportAgent, input: <<~PROMPT
|
|
98
|
+
#{input.text}
|
|
99
|
+
|
|
100
|
+
Findings:
|
|
101
|
+
#{findings.join("\n")}
|
|
102
|
+
PROMPT
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`max_concurrency` limits how many calls run at once. Each one gets its own copy of the workflow context. Cancellation still depends on the provider or tool noticing its token or deadline.
|
|
108
|
+
|
|
109
|
+
## Use a Swarm for specialist handoffs
|
|
110
|
+
|
|
111
|
+
A Swarm keeps one Agent active at a time. You decide which specialists it may hand work to:
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
class ProblemSolverSwarm < LittleGhost::Swarm
|
|
115
|
+
member TriageAgent
|
|
116
|
+
member BillingAgent
|
|
117
|
+
member AccountAgent
|
|
118
|
+
|
|
119
|
+
start TriageAgent
|
|
120
|
+
handoff TriageAgent, to: [BillingAgent, AccountAgent]
|
|
121
|
+
handoff BillingAgent, to: TriageAgent
|
|
122
|
+
handoff AccountAgent, to: TriageAgent
|
|
123
|
+
|
|
124
|
+
max_steps 10
|
|
125
|
+
max_handoff_repeats 2
|
|
126
|
+
end
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The active model sees a handoff tool listing the members it may choose next. LittleGhost accepts only the routes you declared. `max_steps` limits total member executions. `max_handoff_repeats` limits how often the same directed handoff, such as triage to billing, may repeat.
|
|
130
|
+
|
|
131
|
+
Swarm members must be Agents, so each transition stays a direct model-to-model handoff. Caller history and application context are opt-in for each member. Handoff messages come from a model; never treat them as permission to read data or perform an action.
|
|
132
|
+
|
|
133
|
+
Opt in only for a member that needs the data:
|
|
134
|
+
|
|
135
|
+
```ruby
|
|
136
|
+
member AccountAgent, history: true, context: true
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Intermediate model text stays out of the caller-facing stream, leaving one coherent public answer. This is not a privacy boundary. The next member receives the handoff, and the result keeps a bounded summary of the journey.
|
|
140
|
+
|
|
141
|
+
## Use a Graph for guided routes
|
|
142
|
+
|
|
143
|
+
A Graph names the possible stops and the routes between them. Start with a conditional route before adding parallel branches:
|
|
144
|
+
|
|
145
|
+
```ruby
|
|
146
|
+
class SupportFlowGraph < LittleGhost::Graph
|
|
147
|
+
node :triage, TriageAgent
|
|
148
|
+
node :billing, BillingAgent
|
|
149
|
+
node :general, CustomerSupportAgent
|
|
150
|
+
node :respond, CustomerSupportAgent
|
|
151
|
+
|
|
152
|
+
start :triage
|
|
153
|
+
|
|
154
|
+
edge :triage, :billing do |state|
|
|
155
|
+
state.result(:triage).output == "billing"
|
|
156
|
+
end
|
|
157
|
+
edge :triage, :general
|
|
158
|
+
edge :billing, :respond
|
|
159
|
+
edge :general, :respond
|
|
160
|
+
finish :respond
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
SupportFlowGraph.validate!
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Conditions and input mappers read an immutable `Graph::State`. At most one conditional edge may match. If several match, LittleGhost raises `AssemblyRoutingError` instead of guessing which one wins. One unconditional edge can catch the request when none match.
|
|
167
|
+
|
|
168
|
+
Graph nodes start without caller history or application context. They still receive the original input or the output routed from an earlier node. Map or redact that data before it moves to a provider or participant that should see less.
|
|
169
|
+
|
|
170
|
+
Opt in for a trusted node when it needs caller context:
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
node :account_lookup, AccountLookupAgent, context: true
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Fork and join bounded parallel paths
|
|
177
|
+
|
|
178
|
+
Use a fork when one result should start several independent branches. A join brings their answers back together:
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
class InvestigationGraph < LittleGhost::Graph
|
|
182
|
+
node :triage, TriageAgent
|
|
183
|
+
node :ledger, LedgerResearchAgent
|
|
184
|
+
node :policy, PolicyResearchAgent
|
|
185
|
+
node :respond, CustomerSupportAgent
|
|
186
|
+
|
|
187
|
+
start :triage
|
|
188
|
+
fork :triage, to: [:ledger, :policy], max_concurrency: 2
|
|
189
|
+
join(
|
|
190
|
+
[:ledger, :policy],
|
|
191
|
+
to: :respond,
|
|
192
|
+
input: ->(state) { state.branch_results.transform_values(&:output) }
|
|
193
|
+
)
|
|
194
|
+
finish :respond
|
|
195
|
+
end
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
An error edge can send an expected failure to a recovery Assembly. Call `validate!` before the first run.
|
|
199
|
+
|
|
200
|
+
Once the topology grows, `InvestigationGraph.to_mermaid` returns Mermaid diagram source for the routes you declared. Render it in a Mermaid-aware editor or documentation page when a picture makes the graph easier to review.
|
|
201
|
+
|
|
202
|
+
## Make retries safe
|
|
203
|
+
|
|
204
|
+
Workflow calls, Swarm members, and Graph nodes can set timeouts and retries. Use them for work that can safely be attempted again:
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
invoke(
|
|
208
|
+
ResearchAgent,
|
|
209
|
+
timeout: 15,
|
|
210
|
+
retries: 2,
|
|
211
|
+
retry_on: [LittleGhost::ProviderError],
|
|
212
|
+
retry_delay: 0.25
|
|
213
|
+
)
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
A timeout asks the running code to stop; it cannot forcibly end arbitrary Ruby or provider work. A retry repeats the whole child step. Retry only selected failures, and make sure repeated external actions are safe.
|
|
217
|
+
|
|
218
|
+
Retries start at zero. When `retries` is greater than zero, `retry_on` must list the exception classes that are safe to try again. LittleGhost does not retry every failure by default.
|
|
219
|
+
|
|
220
|
+
## Inspect what the assembly did
|
|
221
|
+
|
|
222
|
+
A composite result remembers the steps it took. `trajectory` lets you explore them:
|
|
223
|
+
|
|
224
|
+
```ruby
|
|
225
|
+
run = InvestigationGraph.ask("Why is transfer 481 pending?")
|
|
226
|
+
trajectory = run.result.trajectory
|
|
227
|
+
|
|
228
|
+
trajectory.each { |step| puts "#{step.participant}: #{step.status}" }
|
|
229
|
+
trajectory.transitions
|
|
230
|
+
ledger = trajectory.find { |step| step.participant == "ledger" }
|
|
231
|
+
policy = trajectory.find { |step| step.participant == "policy" }
|
|
232
|
+
trajectory.concurrent?(ledger.id, policy.id)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Step outputs and buffered events have size limits. Use your application's instrumentation when trusted operators need deeper diagnostics.
|
|
236
|
+
|
|
237
|
+
## Compose assemblies inside assemblies
|
|
238
|
+
|
|
239
|
+
Workflow and Graph participants accept any Assembly definition:
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
class ResolutionGraph < LittleGhost::Graph
|
|
243
|
+
node :investigate, InvestigationWorkflow
|
|
244
|
+
node :resolve, ProblemSolverSwarm
|
|
245
|
+
|
|
246
|
+
start :investigate
|
|
247
|
+
edge :investigate, :resolve
|
|
248
|
+
finish :resolve
|
|
249
|
+
end
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
An Assembly can also become an Agent tool:
|
|
253
|
+
|
|
254
|
+
```ruby
|
|
255
|
+
class SupportCoordinatorAgent < LittleGhost::Agent
|
|
256
|
+
assembly_as_tool InvestigationGraph,
|
|
257
|
+
name: "investigate_support_request",
|
|
258
|
+
preserve_context: false
|
|
259
|
+
end
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
The nested assembly receives the parent Tool's current working state. That state may include values restored from a Session. `preserve_context` controls conversation history only: when it is false, working state still passes to the nested assembly. Any nested Tool doing privileged work must authorize with values the application established for the current request or checked again after loading.
|
|
263
|
+
|
|
264
|
+
## Reach for builders when definitions are dynamic
|
|
265
|
+
|
|
266
|
+
Classes are the preferred form in application code. Use a builder when trusted application configuration decides the nodes or routes:
|
|
267
|
+
|
|
268
|
+
```ruby
|
|
269
|
+
graph = LittleGhost::GraphBuilder.new(
|
|
270
|
+
id: "support_flow",
|
|
271
|
+
description: "Routes customer support requests"
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
graph.node :triage, TriageAgent
|
|
275
|
+
graph.node :respond, CustomerSupportAgent
|
|
276
|
+
graph.start :triage
|
|
277
|
+
graph.edge :triage, :respond
|
|
278
|
+
graph.finish :respond
|
|
279
|
+
graph.validate!
|
|
280
|
+
|
|
281
|
+
run = graph.ask("Where is my order?")
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Each builder uses the same declarations as its matching class. The builder stays editable, but each run gets a fixed copy of its current definition. Later edits affect later runs. Ruby callbacks still see any application objects they captured.
|
|
285
|
+
|
|
286
|
+
Continue with [Prompts as Views](prompt_views.md) to give each Agent's growing instructions and shared prompt pieces a natural home.
|