nexo_ai 0.11.0 → 0.13.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/.rdoc_options +5 -0
- data/CHANGELOG.md +95 -0
- data/README.md +13 -14
- data/Rakefile +5 -0
- data/docs/concurrency.md +1 -2
- data/docs/durable-workflows.md +5 -3
- data/docs/getting-started.md +1 -1
- data/docs/loops.md +62 -89
- data/docs/mcp.md +57 -51
- data/docs/rails.md +1 -1
- data/docs/sessions.md +7 -3
- data/docs/skills.md +20 -11
- data/docs/workflows.md +2 -2
- data/examples/mcp_filesystem.rb +1 -1
- data/lib/generators/nexo/artifacts/artifacts_generator.rb +9 -7
- data/lib/generators/nexo/artifacts/templates/add_artifacts_to_nexo_workflow_runs.rb +1 -1
- data/lib/generators/nexo/install/install_generator.rb +7 -5
- data/lib/generators/nexo/skill/skill_generator.rb +10 -8
- data/lib/generators/nexo/state/state_generator.rb +9 -7
- data/lib/generators/nexo/state/templates/add_state_to_nexo_workflow_runs.rb +1 -1
- data/lib/generators/nexo/workflows/templates/create_nexo_workflow_runs.rb +3 -3
- data/lib/generators/nexo/workflows/workflows_generator.rb +7 -5
- data/lib/nexo/agent.rb +159 -169
- data/lib/nexo/concurrent.rb +18 -16
- data/lib/nexo/configuration.rb +21 -21
- data/lib/nexo/engine.rb +1 -1
- data/lib/nexo/loop.rb +12 -14
- data/lib/nexo/loops/ruby_llm.rb +43 -34
- data/lib/nexo/mcp/gated_tool.rb +31 -27
- data/lib/nexo/mcp.rb +73 -68
- data/lib/nexo/output_truncator.rb +9 -9
- data/lib/nexo/permissions.rb +44 -46
- data/lib/nexo/read_tracker.rb +4 -4
- data/lib/nexo/run_store.rb +30 -28
- data/lib/nexo/sandbox.rb +29 -27
- data/lib/nexo/sandboxes/container.rb +75 -75
- data/lib/nexo/sandboxes/local.rb +15 -15
- data/lib/nexo/sandboxes/remote.rb +25 -23
- data/lib/nexo/sandboxes/virtual.rb +2 -2
- data/lib/nexo/sandboxes.rb +9 -9
- data/lib/nexo/session.rb +31 -29
- data/lib/nexo/skills/skill.rb +89 -0
- data/lib/nexo/skills.rb +33 -58
- data/lib/nexo/tools/fetch.rb +21 -21
- data/lib/nexo/tools/glob.rb +6 -6
- data/lib/nexo/tools/read_file.rb +8 -8
- data/lib/nexo/tools/shell.rb +7 -7
- data/lib/nexo/tools/web_search.rb +16 -16
- data/lib/nexo/tools/write_file.rb +11 -11
- data/lib/nexo/turbo_broadcaster.rb +1 -1
- data/lib/nexo/version.rb +2 -2
- data/lib/nexo/workflow.rb +207 -193
- data/lib/nexo/workflow_job.rb +6 -6
- data/lib/nexo/workflow_run.rb +10 -10
- data/lib/nexo.rb +28 -26
- data/sig/nexo/agent.rbs +1 -1
- metadata +7 -34
- data/lib/nexo/loops/agent_sdk.rb +0 -68
data/lib/nexo/session.rb
CHANGED
|
@@ -2,48 +2,50 @@
|
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
4
|
# A continuing, addressable session: a *remembering* instance of an Agent,
|
|
5
|
-
# keyed by
|
|
5
|
+
# keyed by `(agent_name, instance_id)` and resumable across separate
|
|
6
6
|
# invocations. It is the memory half of Nexo's model — the mirror of the
|
|
7
7
|
# fire-and-finish Workflow.
|
|
8
8
|
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
9
|
+
# ```ruby
|
|
10
|
+
# session = Nexo::Session.resume(Assistant, "user-42")
|
|
11
|
+
# session.prompt("My name is Mac.")
|
|
12
|
+
# # ... a later request / process ...
|
|
13
|
+
# Nexo::Session.resume(Assistant, "user-42").prompt("What is my name?")
|
|
14
|
+
# # => "...Mac..." — the persisted thread carried the earlier turn
|
|
15
|
+
# ```
|
|
14
16
|
#
|
|
15
17
|
# A Session adds ONLY memory + addressability. Message persistence is RubyLLM's
|
|
16
|
-
#
|
|
18
|
+
# `acts_as_chat` (a Session defines no message table and serializes nothing);
|
|
17
19
|
# the agent supplies the sandbox, permissions, skills, MCP servers, and fetch
|
|
18
20
|
# allow-list unchanged. Opening or resuming a session therefore never widens
|
|
19
21
|
# what the agent is allowed to do.
|
|
20
22
|
#
|
|
21
|
-
#
|
|
23
|
+
# ## Rails-optional
|
|
22
24
|
#
|
|
23
25
|
# Durable persistence requires ActiveRecord. Backend selection guards on
|
|
24
|
-
#
|
|
26
|
+
# `defined?(::ActiveRecord::Base)`, mirroring RunStore.default:
|
|
25
27
|
#
|
|
26
|
-
# * *Rails* — the thread is a row on the host
|
|
28
|
+
# * *Rails* — the thread is a row on the host `acts_as_chat` model
|
|
27
29
|
# (Nexo.config.session_chat_model, a String class name, constantized here at
|
|
28
|
-
# resume time so no compile-time AR reference exists).
|
|
29
|
-
#
|
|
30
|
+
# resume time so no compile-time AR reference exists). `find_or_create_by` on
|
|
31
|
+
# `(agent_name, instance_id)` loads-or-creates it; `acts_as_chat`'s callbacks
|
|
30
32
|
# persist every new message. The host app owns the schema (via
|
|
31
|
-
#
|
|
33
|
+
# `rails g ruby_llm:install`) plus the two addressing columns and their unique
|
|
32
34
|
# composite index.
|
|
33
35
|
# * *Plain Ruby* — no AR reference at all. A process-wide MemoryStore holds a
|
|
34
|
-
# live
|
|
36
|
+
# live `RubyLLM::Chat` per pair; the thread lives only for the process and is
|
|
35
37
|
# documented as non-durable.
|
|
36
38
|
#
|
|
37
|
-
#
|
|
39
|
+
# ## Lifecycle
|
|
38
40
|
#
|
|
39
41
|
# A long-lived session accumulates context and any MCP/stdio/SSE connections the
|
|
40
42
|
# agent holds. Call #close when done to tear those down; see the README's
|
|
41
43
|
# retention/PII note.
|
|
42
44
|
class Session
|
|
43
|
-
# Loads-or-creates the persisted thread for
|
|
45
|
+
# Loads-or-creates the persisted thread for `(agent_class.name, instance_id)`
|
|
44
46
|
# and returns a Session whose #prompt appends to it. Extra keyword arguments
|
|
45
|
-
# are forwarded to
|
|
46
|
-
# inputs an agent does where they don't conflict with addressing (e.g.
|
|
47
|
+
# are forwarded to `agent_class.new` so a session accepts the same construction
|
|
48
|
+
# inputs an agent does where they don't conflict with addressing (e.g. `cwd:`).
|
|
47
49
|
def self.resume(agent_class, instance_id, **agent_opts)
|
|
48
50
|
new(agent_class.new(**agent_opts), instance_id)
|
|
49
51
|
end
|
|
@@ -52,8 +54,8 @@ module Nexo
|
|
|
52
54
|
# caller-supplied instance id.
|
|
53
55
|
attr_reader :agent_name, :instance_id
|
|
54
56
|
|
|
55
|
-
# Wraps a built
|
|
56
|
-
# the persisted thread for the
|
|
57
|
+
# Wraps a built `agent` and `instance_id`, then hydrates (finds-or-creates)
|
|
58
|
+
# the persisted thread for the `(agent_name, instance_id)` pair. Prefer the
|
|
57
59
|
# Session.resume entry point over calling this directly.
|
|
58
60
|
def initialize(agent, instance_id)
|
|
59
61
|
@agent = agent
|
|
@@ -62,11 +64,11 @@ module Nexo
|
|
|
62
64
|
@chat = hydrate
|
|
63
65
|
end
|
|
64
66
|
|
|
65
|
-
# Appends
|
|
66
|
-
# and returns the response (respond to
|
|
67
|
-
# by
|
|
68
|
-
# Ruby). The optional
|
|
69
|
-
#
|
|
67
|
+
# Appends `text` to the thread, runs the agent's loop over the hydrated chat,
|
|
68
|
+
# and returns the response (respond to `#content`). New messages are persisted
|
|
69
|
+
# by `acts_as_chat` (Rails) or accumulate on the live in-memory chat (plain
|
|
70
|
+
# Ruby). The optional `&on_event` block receives the same
|
|
71
|
+
# `(:tool_call | :tool_result | :done, payload)` events as Loops::RubyLLM.
|
|
70
72
|
def prompt(text, max_turns: 25, &on_event)
|
|
71
73
|
@agent.loop.run(agent: @agent, prompt: text, max_turns: max_turns, chat: @chat, &on_event)
|
|
72
74
|
end
|
|
@@ -109,10 +111,10 @@ module Nexo
|
|
|
109
111
|
|
|
110
112
|
# The durable (Rails) path applies only when BOTH ActiveRecord and the
|
|
111
113
|
# configured host chat model are present — mirroring how RunStore.default
|
|
112
|
-
# requires
|
|
114
|
+
# requires `::ActiveRecord::Base` AND `Nexo::WorkflowRun`. Checking the host
|
|
113
115
|
# model (rather than AR alone) keeps the plain-Ruby fallback robust even in a
|
|
114
116
|
# process where some other component has loaded ActiveRecord but no
|
|
115
|
-
#
|
|
117
|
+
# `acts_as_chat` host model exists (e.g. Nexo's own offline test suite). The
|
|
116
118
|
# host model String is only *referenced* — never constantized — when
|
|
117
119
|
# ActiveRecord is absent, so plain Ruby stays AR-free.
|
|
118
120
|
def durable?
|
|
@@ -127,7 +129,7 @@ module Nexo
|
|
|
127
129
|
end
|
|
128
130
|
|
|
129
131
|
# Process-wide, in-memory backend for the plain-Ruby path. Holds one
|
|
130
|
-
#
|
|
132
|
+
# `{agent:, chat:}` entry per `[agent_name, instance_id]` pair so a later
|
|
131
133
|
# Session.resume in the same process resumes the very same thread AND reuses
|
|
132
134
|
# the agent that owns its live tools/connections. Not durable — a fresh
|
|
133
135
|
# process starts empty. Mirrors RunStore::Memory's class-level singleton so
|
|
@@ -136,7 +138,7 @@ module Nexo
|
|
|
136
138
|
@threads = {}
|
|
137
139
|
|
|
138
140
|
class << self
|
|
139
|
-
# Returns the stored
|
|
141
|
+
# Returns the stored `{agent:, chat:}` entry for the pair, building it once
|
|
140
142
|
# (via the block) on first resume and reusing it thereafter.
|
|
141
143
|
def fetch(agent_name, instance_id)
|
|
142
144
|
@threads[[agent_name, instance_id]] ||= yield
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module Nexo
|
|
6
|
+
module Skills
|
|
7
|
+
# One skill package read from disk: a folder holding a `SKILL.md` (YAML front
|
|
8
|
+
# matter + Markdown body) and, optionally, `scripts/`, `assets/` and
|
|
9
|
+
# `references/`.
|
|
10
|
+
#
|
|
11
|
+
# This is a deliberately small reader. `ruby_llm` ships its own
|
|
12
|
+
# `RubyLLM::Skill`, but it enforces the Agent Skills naming rule (lowercase
|
|
13
|
+
# words joined by hyphens, equal to the folder name), which rejects the
|
|
14
|
+
# underscore names Nexo has always resolved from a symbol (`skills
|
|
15
|
+
# :email_triage`). Nexo only ever needs the body text and the file listings,
|
|
16
|
+
# so it reads those itself rather than force every host to rename its skills.
|
|
17
|
+
class Skill
|
|
18
|
+
# The file that makes a folder a skill.
|
|
19
|
+
FILE = "SKILL.md"
|
|
20
|
+
|
|
21
|
+
# The bundled-file folders a skill may ship.
|
|
22
|
+
KINDS = %w[scripts assets references].freeze
|
|
23
|
+
|
|
24
|
+
FRONT_MATTER = /\A---[ \t]*\r?\n(.*?)^---[ \t]*(?:\r?\n|\z)/m
|
|
25
|
+
private_constant :FRONT_MATTER
|
|
26
|
+
|
|
27
|
+
# The skill's folder, as an absolute path String.
|
|
28
|
+
attr_reader :path
|
|
29
|
+
|
|
30
|
+
# The front matter as a Hash with String keys.
|
|
31
|
+
attr_reader :metadata
|
|
32
|
+
|
|
33
|
+
# The `SKILL.md` body: the instructions injected into the chat.
|
|
34
|
+
attr_reader :content
|
|
35
|
+
|
|
36
|
+
# Reads the skill in the folder `path`. Raises Nexo::Error naming the file
|
|
37
|
+
# when `SKILL.md` is missing, has no front matter, or its front matter is
|
|
38
|
+
# not a YAML mapping.
|
|
39
|
+
def initialize(path)
|
|
40
|
+
@path = File.expand_path(path.to_s)
|
|
41
|
+
file = File.join(@path, FILE)
|
|
42
|
+
raise Error, "skill not found: #{file}" unless File.file?(file)
|
|
43
|
+
|
|
44
|
+
@metadata, @content = parse(File.read(file, encoding: "UTF-8"), file)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# The skill's declared name.
|
|
48
|
+
def name = metadata["name"]
|
|
49
|
+
|
|
50
|
+
# The one-line summary of when to use the skill.
|
|
51
|
+
def description = metadata["description"]
|
|
52
|
+
|
|
53
|
+
# What the skill needs from its environment, or nil when it declares nothing.
|
|
54
|
+
def compatibility = metadata["compatibility"]
|
|
55
|
+
|
|
56
|
+
# Host paths of the files under `scripts/`, sorted.
|
|
57
|
+
def scripts = files("scripts")
|
|
58
|
+
|
|
59
|
+
# Host paths of the files under `assets/`, sorted.
|
|
60
|
+
def assets = files("assets")
|
|
61
|
+
|
|
62
|
+
# Host paths of the files under `references/`, sorted.
|
|
63
|
+
def references = files("references")
|
|
64
|
+
|
|
65
|
+
private
|
|
66
|
+
|
|
67
|
+
# Splits `text` into its front matter Hash and its Markdown body.
|
|
68
|
+
def parse(text, file)
|
|
69
|
+
match = FRONT_MATTER.match(text.delete_prefix("\u{FEFF}"))
|
|
70
|
+
raise Error, "#{file} must start with YAML front matter between --- lines" unless match
|
|
71
|
+
|
|
72
|
+
front_matter = YAML.safe_load(match[1], permitted_classes: [Date, Time], aliases: false)
|
|
73
|
+
raise Error, "#{file} front matter must be a YAML mapping" unless front_matter.is_a?(Hash)
|
|
74
|
+
|
|
75
|
+
[front_matter.transform_keys(&:to_s), match.post_match.strip]
|
|
76
|
+
rescue Psych::Exception => e
|
|
77
|
+
raise Error, "#{file} has invalid YAML front matter: #{e.message}"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# The files under one of the skill's folders, as sorted host paths.
|
|
81
|
+
def files(kind)
|
|
82
|
+
dir = File.join(path, kind)
|
|
83
|
+
return [] unless File.directory?(dir)
|
|
84
|
+
|
|
85
|
+
Dir.glob(File.join(dir, "**", "*")).select { |f| File.file?(f) && File.basename(f) != ".keep" }.sort
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
data/lib/nexo/skills.rb
CHANGED
|
@@ -1,88 +1,63 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
# class macro — no loader wiring.
|
|
7
|
-
#
|
|
8
|
-
# +ruby_llm-skills+ is a SOFT (optional) runtime dependency: it is required
|
|
9
|
-
# lazily by load! the first time a skill is used. With the gem absent,
|
|
10
|
-
# +require "nexo"+ still loads cleanly; only touching a skill raises
|
|
11
|
-
# MissingDependencyError with install guidance.
|
|
4
|
+
# Skills: drop a SKILL.md package into `app/skills/<name>/` and attach it to an
|
|
5
|
+
# agent with a single `skills` class macro — no loader wiring, and no extra gem.
|
|
12
6
|
#
|
|
13
7
|
# A loaded skill contributes its **instructions** (the SKILL.md body) to a chat.
|
|
14
8
|
# It ships no independent tools, so attaching a skill never widens the agent's
|
|
15
9
|
# effective capabilities.
|
|
16
10
|
#
|
|
17
|
-
# Its
|
|
18
|
-
# own: a skill lives under
|
|
11
|
+
# Its `scripts/`, `assets/` and `references/` files are NOT reachable on their
|
|
12
|
+
# own: a skill lives under `skills_path`, every sandbox confines file access to
|
|
19
13
|
# its own working directory, and nothing bridges the two. To let an agent read or
|
|
20
14
|
# run a skill's bundled files, copy them into the sandbox first — through the
|
|
21
|
-
# sandbox's own
|
|
22
|
-
# Nexo's permission-gated tools like any other workspace file.
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
15
|
+
# sandbox's own `#write`, so it works on every tier. They are then reached through
|
|
16
|
+
# Nexo's permission-gated tools like any other workspace file.
|
|
17
|
+
#
|
|
18
|
+
# `ruby_llm` has its own skills (`Chat#with_skills`, which adds a `load_skill`
|
|
19
|
+
# tool). Nexo does not attach it: skill text goes in as instructions, and a
|
|
20
|
+
# skill's files are reached only through the gated sandbox tools.
|
|
26
21
|
module Skills
|
|
27
22
|
class << self
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
def load!
|
|
32
|
-
require "ruby_llm/skills"
|
|
33
|
-
rescue LoadError
|
|
34
|
-
raise MissingDependencyError,
|
|
35
|
-
'Skills require the `ruby_llm-skills` gem. Add `gem "ruby_llm-skills"` to your Gemfile.'
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# Resolves a skill +name+ (symbol or string) to a loaded skill object read
|
|
39
|
-
# from the filesystem under Nexo.config.skills_path. The filesystem is the
|
|
40
|
-
# only skill source in v1 — zip/DB/remote loading is deferred entirely to
|
|
41
|
-
# +ruby_llm-skills+.
|
|
23
|
+
# Resolves a skill `name` (symbol or string) to a Skills::Skill read from
|
|
24
|
+
# `<skills_path>/<name>/SKILL.md` under Nexo.config.skills_path. The
|
|
25
|
+
# filesystem is the only skill source.
|
|
42
26
|
#
|
|
43
|
-
#
|
|
44
|
-
# then resolves +<skills_path>/<name>/SKILL.md+. A missing file raises
|
|
45
|
-
# Error whose message names the resolved path.
|
|
46
|
-
#
|
|
47
|
-
# @return [RubyLLM::Skills::Skill] the loaded skill (exposes +#content+,
|
|
48
|
-
# +#name+, +#description+).
|
|
27
|
+
# A missing file raises Error whose message names the resolved path.
|
|
49
28
|
def find(name)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
dir = File.join(Nexo.config.skills_path.to_s, name.to_s)
|
|
53
|
-
skill_md = File.join(dir, "SKILL.md")
|
|
54
|
-
raise Error, "skill not found: #{skill_md}" unless File.exist?(skill_md)
|
|
55
|
-
|
|
56
|
-
RubyLLM::Skills.load(dir)
|
|
29
|
+
Skill.new(File.join(Nexo.config.skills_path.to_s, name.to_s))
|
|
57
30
|
end
|
|
58
31
|
|
|
59
32
|
# Copies a skill's bundled files INTO a sandbox, so an agent can read or run
|
|
60
33
|
# them through its permission-gated tools.
|
|
61
34
|
#
|
|
62
|
-
# A skill lives under
|
|
63
|
-
# confines file access to its own working directory — so
|
|
64
|
-
# and
|
|
35
|
+
# A skill lives under `skills_path`, outside every sandbox, and each sandbox
|
|
36
|
+
# confines file access to its own working directory — so `scripts/`, `assets/`
|
|
37
|
+
# and `references/` are unreachable until they are staged. This is that step.
|
|
65
38
|
#
|
|
66
|
-
# It goes through the sandbox's own
|
|
67
|
-
# on every tier:
|
|
68
|
-
#
|
|
39
|
+
# It goes through the sandbox's own `#write`, which is the ONLY route that works
|
|
40
|
+
# on every tier: `Local` writes to the filesystem, `Container` streams over
|
|
41
|
+
# `docker exec` / `container exec`, and `Remote` hands off to the injected
|
|
69
42
|
# client. Nothing here knows or cares which one it is.
|
|
70
43
|
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
44
|
+
# ```ruby
|
|
45
|
+
# Nexo::Skills.materialize(:dashboard_designer, into: agent.sandbox)
|
|
46
|
+
# # => { scripts: ["scripts/render_dashboard.rb"],
|
|
47
|
+
# # assets: ["assets/dashboard-template.html"] }
|
|
48
|
+
# ```
|
|
74
49
|
#
|
|
75
50
|
# Returns the SANDBOX-RELATIVE path of every file written, grouped by kind, so a
|
|
76
51
|
# caller can build a command without knowing where the sandbox lives. Build
|
|
77
52
|
# commands from these relative paths — a host-absolute path is meaningless
|
|
78
53
|
# inside a container and on another machine.
|
|
79
54
|
#
|
|
80
|
-
# Kinds a skill ships nothing for are omitted.
|
|
81
|
-
#
|
|
55
|
+
# Kinds a skill ships nothing for are omitted. `kinds:` narrows the copy;
|
|
56
|
+
# `overwrite: false` skips files already present, which is what you want against
|
|
82
57
|
# an image that already bakes the skill in.
|
|
83
58
|
#
|
|
84
|
-
# SECURITY: staged files land in WRITABLE space. An agent holding
|
|
85
|
-
#
|
|
59
|
+
# SECURITY: staged files land in WRITABLE space. An agent holding `:write` and
|
|
60
|
+
# `:shell` can rewrite a script before executing it — where the same file, left
|
|
86
61
|
# outside the sandbox, could not be touched at all. Re-materializing on every run
|
|
87
62
|
# bounds tampering to a single turn; if that is not enough, keep the resource out
|
|
88
63
|
# of the sandbox and feed the agent its contents another way.
|
|
@@ -99,8 +74,8 @@ module Nexo
|
|
|
99
74
|
|
|
100
75
|
private
|
|
101
76
|
|
|
102
|
-
# A skill's files of one kind, as host paths.
|
|
103
|
-
#
|
|
77
|
+
# A skill's files of one kind, as host paths. An object that does not answer
|
|
78
|
+
# the kind (a skill double with no bundled files) yields an empty list.
|
|
104
79
|
def resources(skill, kind)
|
|
105
80
|
return [] unless %i[scripts assets references].include?(kind.to_sym)
|
|
106
81
|
return [] unless skill.respond_to?(kind)
|
|
@@ -108,7 +83,7 @@ module Nexo
|
|
|
108
83
|
Array(skill.public_send(kind))
|
|
109
84
|
end
|
|
110
85
|
|
|
111
|
-
# Writes one host file into the sandbox under
|
|
86
|
+
# Writes one host file into the sandbox under `<kind>/<basename>`, preserving the
|
|
112
87
|
# layout a skill's own prose refers to ("run scripts/render.rb").
|
|
113
88
|
def copy_in(host_path, kind, sandbox, overwrite)
|
|
114
89
|
relative = File.join(kind.to_s, File.basename(host_path))
|
data/lib/nexo/tools/fetch.rb
CHANGED
|
@@ -7,22 +7,22 @@ require "resolv"
|
|
|
7
7
|
|
|
8
8
|
module Nexo
|
|
9
9
|
module Tools
|
|
10
|
-
# Read-only HTTP(S) GET, gated by the
|
|
11
|
-
# like
|
|
12
|
-
#
|
|
13
|
-
#
|
|
10
|
+
# Read-only HTTP(S) GET, gated by the `:fetch` capability (denied by default,
|
|
11
|
+
# like `:shell`) and a host allow-list (SSRF/egress guard). Follows the
|
|
12
|
+
# `ReadFile`/`WriteFile` shape exactly: authorize first, act, and rescue
|
|
13
|
+
# `Permissions::Denied` into `{ error: ... }` so the loop never crashes.
|
|
14
14
|
#
|
|
15
15
|
# Deliberately minimal: a single stdlib GET. No POST/PUT/DELETE, no crawler,
|
|
16
16
|
# no cache, no rate limiter, no redirect-following. The only request header the
|
|
17
|
-
# model influences is a fixed
|
|
17
|
+
# model influences is a fixed `User-Agent`. Fetched bodies are UNTRUSTED model
|
|
18
18
|
# input (prompt-injection risk) and are returned raw (truncated), never parsed.
|
|
19
19
|
#
|
|
20
|
-
# Return shape differs from
|
|
21
|
-
# is
|
|
22
|
-
#
|
|
20
|
+
# Return shape differs from `ReadFile` (which returns a bare String): success
|
|
21
|
+
# is `{ body: <string truncated to MAX_BYTES> }`; any denial or error is
|
|
22
|
+
# `{ error: <message> }`.
|
|
23
23
|
class Fetch < RubyLLM::Tool
|
|
24
24
|
description "Fetch the text of a web page by URL (HTTP/HTTPS GET only)."
|
|
25
|
-
|
|
25
|
+
parameter :url, type: :string, required: true, description: "Absolute http(s) URL to fetch"
|
|
26
26
|
|
|
27
27
|
# Bodies are truncated to this many bytes (byteslice) before returning.
|
|
28
28
|
MAX_BYTES = 200_000
|
|
@@ -35,8 +35,8 @@ module Nexo
|
|
|
35
35
|
IPAddr.new("::/128") # unspecified IPv6
|
|
36
36
|
].freeze
|
|
37
37
|
|
|
38
|
-
#
|
|
39
|
-
#
|
|
38
|
+
# `allow_hosts` scopes which hosts the GET may reach (subdomain-aware);
|
|
39
|
+
# `permissions` gates the `:fetch` capability. Both locks must open.
|
|
40
40
|
def initialize(sandbox:, permissions:, allow_hosts: [])
|
|
41
41
|
@sandbox = sandbox
|
|
42
42
|
@permissions = permissions
|
|
@@ -44,10 +44,10 @@ module Nexo
|
|
|
44
44
|
super()
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
# Order of checks (every denial returns
|
|
47
|
+
# Order of checks (every denial returns `{ error: }`, never raises into the
|
|
48
48
|
# loop): capability gate → scheme → host allow-list → private-address guard →
|
|
49
49
|
# perform the GET. The private-address guard runs AFTER the allow-list and is
|
|
50
|
-
# never bypassed, so an allow-listed
|
|
50
|
+
# never bypassed, so an allow-listed `localhost` is still refused.
|
|
51
51
|
def execute(url:)
|
|
52
52
|
@permissions.authorize!(:fetch, url)
|
|
53
53
|
|
|
@@ -83,10 +83,10 @@ module Nexo
|
|
|
83
83
|
|
|
84
84
|
private
|
|
85
85
|
|
|
86
|
-
# Subdomain-aware host match (not exact):
|
|
87
|
-
#
|
|
88
|
-
# permits itself and
|
|
89
|
-
#
|
|
86
|
+
# Subdomain-aware host match (not exact): `host` matches an allow-list entry
|
|
87
|
+
# `h` when it equals `h` or ends with +"." + h+. So `news.example.com`
|
|
88
|
+
# permits itself and `www.news.example.com`, but never `notexample.com` or
|
|
89
|
+
# `example.com.evil.org`. Case-insensitive on the host.
|
|
90
90
|
def host_allowed?(host)
|
|
91
91
|
return false if host.nil?
|
|
92
92
|
|
|
@@ -97,7 +97,7 @@ module Nexo
|
|
|
97
97
|
end
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
-
# Resolves
|
|
100
|
+
# Resolves `host` to IPAddr objects. Resolution failure yields `[]` so the
|
|
101
101
|
# caller fails open to a plain hostname connect (an invalid host then errors
|
|
102
102
|
# naturally as a fetch error) — matching the prior guard's fail-open posture.
|
|
103
103
|
def resolve(host)
|
|
@@ -106,7 +106,7 @@ module Nexo
|
|
|
106
106
|
[]
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
-
# Whether
|
|
109
|
+
# Whether `addr` (an IPAddr) is loopback, private (RFC1918 / ULA), link-local
|
|
110
110
|
# (incl. the 169.254.169.254 metadata address), or in one of the extra
|
|
111
111
|
# UNSAFE_RANGES the stdlib predicates miss.
|
|
112
112
|
def unsafe_ip?(addr)
|
|
@@ -114,9 +114,9 @@ module Nexo
|
|
|
114
114
|
UNSAFE_RANGES.any? { |range| range.include?(addr) }
|
|
115
115
|
end
|
|
116
116
|
|
|
117
|
-
# GETs
|
|
117
|
+
# GETs `uri`, pinned to the pre-vetted `ip` when one was resolved (so the
|
|
118
118
|
# socket connects to the exact address the guard approved — no re-resolution,
|
|
119
|
-
# no DNS-rebinding window).
|
|
119
|
+
# no DNS-rebinding window). `ip` may be nil for an unresolvable host, in which
|
|
120
120
|
# case Net::HTTP resolves the hostname itself and fails naturally.
|
|
121
121
|
def get(uri, ip = nil)
|
|
122
122
|
http = Net::HTTP.new(uri.host, uri.port)
|
data/lib/nexo/tools/glob.rb
CHANGED
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
4
|
module Tools
|
|
5
|
-
# Lists workspace files matching a glob pattern. Authorizes
|
|
6
|
-
# a denial is returned as
|
|
5
|
+
# Lists workspace files matching a glob pattern. Authorizes `:glob` first;
|
|
6
|
+
# a denial is returned as `{ error: ... }`.
|
|
7
7
|
class Glob < RubyLLM::Tool
|
|
8
8
|
description "List files in the workspace matching a glob pattern."
|
|
9
|
-
|
|
9
|
+
parameter :pattern, type: :string, required: true, description: "Glob pattern, e.g. **/*.rb"
|
|
10
10
|
|
|
11
|
-
#
|
|
11
|
+
# `sandbox` performs the match; `permissions` gates the `:glob` capability.
|
|
12
12
|
def initialize(sandbox:, permissions:)
|
|
13
13
|
@sandbox = sandbox
|
|
14
14
|
@permissions = permissions
|
|
15
15
|
super()
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
-
# Authorizes
|
|
19
|
-
# or
|
|
18
|
+
# Authorizes `:glob`, then returns `{ files: [...] }` matching `pattern` —
|
|
19
|
+
# or `{ error: ... }` on a denial.
|
|
20
20
|
def execute(pattern:)
|
|
21
21
|
@permissions.authorize!(:glob, pattern)
|
|
22
22
|
{files: @sandbox.glob(pattern)}
|
data/lib/nexo/tools/read_file.rb
CHANGED
|
@@ -3,23 +3,23 @@
|
|
|
3
3
|
module Nexo
|
|
4
4
|
# The sandbox-backed, permission-gated tools Nexo attaches to an agent's chat:
|
|
5
5
|
# ReadFile, WriteFile, Shell, Glob, and Fetch. Each authorizes a capability
|
|
6
|
-
# against the agent's Permissions before acting and returns
|
|
6
|
+
# against the agent's Permissions before acting and returns `{ error: ... }` on
|
|
7
7
|
# a denial rather than raising into the tool loop.
|
|
8
8
|
module Tools
|
|
9
|
-
# Reads a file from the agent's sandbox. Authorizes
|
|
10
|
-
# the sandbox; a denial or a missing file is returned as
|
|
9
|
+
# Reads a file from the agent's sandbox. Authorizes `:read` before touching
|
|
10
|
+
# the sandbox; a denial or a missing file is returned as `{ error: ... }` so
|
|
11
11
|
# the model can adapt instead of the loop crashing.
|
|
12
12
|
class ReadFile < RubyLLM::Tool
|
|
13
13
|
description "Read a file from the workspace."
|
|
14
|
-
|
|
14
|
+
parameter :path, type: :string, required: true, description: "Path to the file to read"
|
|
15
15
|
|
|
16
16
|
# Returned content is capped to this many bytes (head), matching Tools::Fetch,
|
|
17
17
|
# so a single huge file can't blow a small context window. The read-before-write
|
|
18
18
|
# guard is unaffected — it records the file's mtime, not its content.
|
|
19
19
|
MAX_BYTES = 200_000
|
|
20
20
|
|
|
21
|
-
#
|
|
22
|
-
# for the read-before-write + stale guard. Default
|
|
21
|
+
# `tracker:` is an optional ReadTracker shared with Tools::WriteFile
|
|
22
|
+
# for the read-before-write + stale guard. Default `nil` ⇒ nothing is
|
|
23
23
|
# recorded, preserving direct-construction behavior.
|
|
24
24
|
def initialize(sandbox:, permissions:, tracker: nil)
|
|
25
25
|
@sandbox = sandbox
|
|
@@ -28,8 +28,8 @@ module Nexo
|
|
|
28
28
|
super()
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
-
# Authorizes
|
|
32
|
-
# on the tracker (if any), and returns the content — or
|
|
31
|
+
# Authorizes `:read`, reads `path` from the sandbox, records `(path, mtime)`
|
|
32
|
+
# on the tracker (if any), and returns the content — or `{ error: ... }` on a
|
|
33
33
|
# denied read or a missing file.
|
|
34
34
|
def execute(path:)
|
|
35
35
|
@permissions.authorize!(:read, path)
|
data/lib/nexo/tools/shell.rb
CHANGED
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
4
|
module Tools
|
|
5
|
-
# Runs a shell command in the agent's sandbox. Authorizes
|
|
5
|
+
# Runs a shell command in the agent's sandbox. Authorizes `:shell` first.
|
|
6
6
|
# A denial, or a sandbox with no shell (e.g. Sandboxes::Virtual), is
|
|
7
|
-
# returned as
|
|
7
|
+
# returned as `{ error: ... }` rather than raised into the loop.
|
|
8
8
|
class Shell < RubyLLM::Tool
|
|
9
9
|
description "Run a shell command in the agent's workspace."
|
|
10
|
-
|
|
10
|
+
parameter :command, type: :string, required: true, description: "The command to execute"
|
|
11
11
|
|
|
12
|
-
#
|
|
12
|
+
# `sandbox` runs the command; `permissions` gates the `:shell` capability.
|
|
13
13
|
def initialize(sandbox:, permissions:)
|
|
14
14
|
@sandbox = sandbox
|
|
15
15
|
@permissions = permissions
|
|
16
16
|
super()
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
# Authorizes
|
|
20
|
-
# status: }
|
|
21
|
-
#
|
|
19
|
+
# Authorizes `:shell`, runs `command`, and returns
|
|
20
|
+
# `{ stdout:, stderr:, status: }` (stdout/stderr truncated via OutputTruncator) — or
|
|
21
|
+
# `{ error: ... }` on a denial or a sandbox with no shell.
|
|
22
22
|
def execute(command:)
|
|
23
23
|
@permissions.authorize!(:shell, command)
|
|
24
24
|
out = @sandbox.shell(command)
|
|
@@ -2,34 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
module Nexo
|
|
4
4
|
module Tools
|
|
5
|
-
# Vendor-neutral web search, gated by the
|
|
6
|
-
# default, like
|
|
7
|
-
# authorize first, act, and rescue
|
|
5
|
+
# Vendor-neutral web search, gated by the `:search` capability (denied by
|
|
6
|
+
# default, like `:fetch`/`:shell`). Follows the `Tools::Fetch` shape exactly:
|
|
7
|
+
# authorize first, act, and rescue `Permissions::Denied` into `{ error: ... }`
|
|
8
8
|
# so the loop never crashes.
|
|
9
9
|
#
|
|
10
10
|
# Nexo ships NO search provider. The query is delegated to a host-injected
|
|
11
|
-
#
|
|
12
|
-
# of
|
|
11
|
+
# `backend` responding to `#search(query, **opts)` that returns an Enumerable
|
|
12
|
+
# of `{title:, url:, snippet:}` rows (Hashes or objects responding to `#to_h`).
|
|
13
13
|
# Results are count- and snippet-capped so untrusted provider output can't blow
|
|
14
|
-
# the context window. It pairs with
|
|
14
|
+
# the context window. It pairs with `Tools::Fetch`: search finds URLs, fetch
|
|
15
15
|
# reads one.
|
|
16
16
|
#
|
|
17
17
|
# Backend results are UNTRUSTED model input (prompt-injection risk in snippets);
|
|
18
18
|
# the backend is trust-bearing and runs in the host process, not the sandbox.
|
|
19
19
|
#
|
|
20
|
-
# Return shape: success is
|
|
21
|
-
# rows); any denial or error is
|
|
20
|
+
# Return shape: success is `{ results: [{title:, url:, snippet:}, …] }` (≤8
|
|
21
|
+
# rows); any denial or error is `{ error: <message> }`.
|
|
22
22
|
class WebSearch < RubyLLM::Tool
|
|
23
23
|
description "Search the web for a query; returns titles, URLs, and snippets."
|
|
24
|
-
|
|
24
|
+
parameter :query, type: :string, required: true, description: "The search query"
|
|
25
25
|
|
|
26
26
|
# At most this many result rows are returned.
|
|
27
27
|
MAX_RESULTS = 8
|
|
28
28
|
# Each result snippet is truncated to this many characters.
|
|
29
29
|
MAX_SNIPPET = 300
|
|
30
30
|
|
|
31
|
-
#
|
|
32
|
-
# host-owned provider.
|
|
31
|
+
# `permissions` gates the `:search` capability; `backend` is the injected,
|
|
32
|
+
# host-owned provider. `sandbox:` is accepted for signature parity with the
|
|
33
33
|
# other tools even though search runs in the host process, not the sandbox.
|
|
34
34
|
def initialize(sandbox:, permissions:, backend:)
|
|
35
35
|
@permissions = permissions
|
|
@@ -37,7 +37,7 @@ module Nexo
|
|
|
37
37
|
super()
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
# Order of operations (every denial/error returns
|
|
40
|
+
# Order of operations (every denial/error returns `{ error: }`, never raises
|
|
41
41
|
# into the loop): capability gate → delegate to the backend (query-only) →
|
|
42
42
|
# normalize and cap. The gate runs before the backend, so a denied search
|
|
43
43
|
# never touches it.
|
|
@@ -59,9 +59,9 @@ module Nexo
|
|
|
59
59
|
|
|
60
60
|
private
|
|
61
61
|
|
|
62
|
-
# Accepts a Hash or any object responding to
|
|
63
|
-
# string-keyed fields (a backend that returns
|
|
64
|
-
# same as
|
|
62
|
+
# Accepts a Hash or any object responding to `#to_h`, with either symbol- or
|
|
63
|
+
# string-keyed fields (a backend that returns `{"title" => …}` works the
|
|
64
|
+
# same as `{title: …}`); returns a row with stringified fields and the
|
|
65
65
|
# snippet truncated to MAX_SNIPPET.
|
|
66
66
|
def normalize(r)
|
|
67
67
|
h = r.respond_to?(:to_h) ? r.to_h : r
|
|
@@ -72,7 +72,7 @@ module Nexo
|
|
|
72
72
|
}
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
# Reads
|
|
75
|
+
# Reads `key` from a row tolerating symbol OR string keys.
|
|
76
76
|
def field(hash, key)
|
|
77
77
|
hash[key] || hash[key.to_s]
|
|
78
78
|
end
|