elelem-builtins 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e9add550f4e090bdfedad12f0ace36276ab00db2f2c1c1af51d0dd2c2dfaef8c
4
+ data.tar.gz: 3e1dc2c8c59c56b273161578ac5628fdfac317f2a0cbca3566ea878255d4610a
5
+ SHA512:
6
+ metadata.gz: '08a66e554544c36454bdc6f99e0987ddbda1072a4216acd9b34a3b9201cd73c204a40504e6e0ceef8c37bafca13086530ac5213116954b5268b334f362d376f2'
7
+ data.tar.gz: 8cae217a61971f7f9b572ee88e58969b2b1cdfd2b6020dd03f0192406059350556649498823be467f874cf5535be2579f393d01d23151f714c1ae645297884e1
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 mo khan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: %i[spec]
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:builtins) do |agent|
5
+ agent.commands.register("exit", description: "Exit elelem") { exit(0) }
6
+
7
+ agent.commands.register("clear", description: "Clear conversation history") do
8
+ agent.conversation.clear!
9
+ agent.output.say " → context cleared"
10
+ end
11
+
12
+ agent.commands.register("help", description: "Show available commands") do
13
+ agent.output.say agent.commands.map { |name, description| "#{name.ljust(12)} #{description}" }.join("\n")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:context) do |agent|
5
+ agent.commands.register("context", description: "Show conversation context") do |args|
6
+ messages = agent.context
7
+
8
+ case args
9
+ when nil, ""
10
+ messages.each_with_index do |msg, i|
11
+ role = msg[:role]
12
+ preview = msg[:content].to_s.lines.first&.strip&.slice(0, 60) || ""
13
+ preview += "..." if msg[:content].to_s.length > 60
14
+ agent.output.say " #{i + 1}. #{role}: #{preview}"
15
+ end
16
+ when "json"
17
+ agent.output.say JSON.pretty_generate(messages)
18
+ when /^\d+$/
19
+ index = args.to_i - 1
20
+ if index >= 0 && index < messages.length
21
+ content = messages[index][:content].to_s
22
+ agent.output.say(content, as: :markdown)
23
+ else
24
+ agent.output.say " Invalid index: #{args}"
25
+ end
26
+ else
27
+ agent.output.say " Usage: /context [json|<number>]"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:execute) do |agent|
5
+ agent.toolbox.add("execute",
6
+ description: "Run shell command (supports pipes and redirections)",
7
+ params: { command: { type: "string" } },
8
+ required: ["command"],
9
+ aliases: ["bash", "sh", "exec", "execute<|channel|>"]
10
+ ) do |a|
11
+ Elelem::Builtins.sh("bash", args: ["-c", a["command"]], timeout: Elelem::Builtins.command_timeout) { |x| agent.output.print(x) }
12
+ end
13
+
14
+ agent.toolbox.after("execute") do |args, result|
15
+ next if result[:exit_status] == 0
16
+
17
+ agent.output.doing("execute", args, state: "x")
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:provider) do |agent|
5
+ agent.commands.register("provider", description: "Switch provider", completions: -> { Elelem::Config.names }) do |name|
6
+ if name.nil? || name.empty?
7
+ agent.output.say " → available: #{Elelem::Config.names.join(", ")}"
8
+ else
9
+ agent.client = Elelem::Config.build_client(name)
10
+ agent.output.say " → switched to #{name}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:read) do |agent|
5
+ agent.toolbox.add("read",
6
+ description: "Read file",
7
+ params: { path: { type: "string" } },
8
+ required: ["path"],
9
+ aliases: ["open"]
10
+ ) do |a|
11
+ path = Pathname.new(a["path"]).expand_path
12
+ path.exist? ? { content: path.read, path: a["path"] } : { error: "not found" }
13
+ end
14
+
15
+ agent.toolbox.after("read") do |_, result|
16
+ if result[:error]
17
+ agent.output.say " ! #{result[:error]}"
18
+ else
19
+ agent.output.display_file(result[:path], fallback: result[:content])
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:reload) do |agent|
5
+ agent.commands.register("reload", description: "Reload plugin code from disk") do
6
+ Elelem::Config.reload(agent)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Builtins
5
+ class Terminal < Elelem::Terminal
6
+ def display_file(path, fallback: nil)
7
+ system("bat", "--paging=never", path) || say(fallback || path)
8
+ end
9
+
10
+ private
11
+
12
+ def render(text, as)
13
+ return super unless as == :markdown
14
+
15
+ gap
16
+ width = @stream.winsize[1] rescue 80
17
+ IO.popen(["glow", "-s", "dark", "-w", width.to_s, "-"], "r+") do |io|
18
+ io.write(text)
19
+ io.close_write
20
+ io.read
21
+ end
22
+ rescue Errno::ENOENT
23
+ text
24
+ end
25
+
26
+ def write(text)
27
+ super
28
+ (@transcript ||= open_transcript).print text
29
+ end
30
+
31
+ def open_transcript
32
+ state_dir = File.join(ENV.fetch("XDG_STATE_HOME", File.expand_path("~/.local/state")), "elelem", "sessions")
33
+ FileUtils.mkdir_p(state_dir)
34
+ session_id = "#{Time.now.utc.strftime("%Y%m%dT%H%M%SZ")}-#{Process.pid}"
35
+ File.open(File.join(state_dir, "#{session_id}.transcript"), "a").tap { |f| f.sync = true }
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ Elelem.configure { |config| config.output { Elelem::Builtins::Terminal.new } }
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Builtins
5
+ module ToolMarkdown
6
+ def self.summary(tool)
7
+ "| #{tool.name} | #{tool.description.lines.first.chomp} |"
8
+ end
9
+
10
+ def self.to_markdown(tool)
11
+ lines = ["## #{tool.name}", "", tool.description, "", "### Parameters", ""]
12
+ tool.params.each do |param, spec|
13
+ req = tool.required.include?(param.to_s) ? ", required" : ""
14
+ desc = spec[:description] ? " - #{spec[:description]}" : ""
15
+ lines << "- `#{param}` (#{spec[:type]}#{req})#{desc}"
16
+ end
17
+ lines << "" << "*aliases: #{tool.aliases.join(", ")}*" if tool.aliases.any?
18
+ lines.join("\n")
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ Elelem.configure do |config|
25
+ config.setup(:tools) do |agent|
26
+ completions = -> { agent.toolbox.tools.keys }
27
+
28
+ agent.commands.register("tools", description: "List available tools", completions: completions) do |arg|
29
+ if arg && !arg.empty?
30
+ tool = agent.toolbox.tools[arg]
31
+ unless tool
32
+ agent.output.say "Unknown tool: #{arg}"
33
+ next
34
+ end
35
+
36
+ agent.output.say Elelem::Builtins::ToolMarkdown.to_markdown(tool), as: :markdown
37
+ else
38
+ rows = agent.toolbox.tools.each_value.map { |tool| Elelem::Builtins::ToolMarkdown.summary(tool) }
39
+
40
+ md = String.new("| Tool | Description |\n")
41
+ md << "|------|-------------|\n"
42
+ md << rows.join("\n")
43
+
44
+ agent.output.say md, as: :markdown
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Elelem
4
+ module Builtins
5
+ VERSION = "0.2.0"
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ Elelem.configure do |config|
4
+ config.setup(:write) do |agent|
5
+ agent.toolbox.add("write",
6
+ description: "Write file",
7
+ params: { path: { type: "string" }, content: { type: "string" } },
8
+ required: ["path", "content"],
9
+ aliases: ["write<|channel|>"]
10
+ ) do |a|
11
+ path = Pathname.new(a["path"]).expand_path
12
+ FileUtils.mkdir_p(path.dirname)
13
+ { bytes: path.write(a["content"]), path: a["path"] }
14
+ end
15
+
16
+ agent.toolbox.before("write") do |args|
17
+ path = Pathname.new(args["path"]).expand_path
18
+ next unless path.exist? && $stdin.tty?
19
+
20
+ Tempfile.create(["elelem", File.extname(path)]) do |t|
21
+ t.write(args["content"])
22
+ t.flush
23
+ system("diff", "--color=always", "-u", path.to_s, t.path)
24
+ end
25
+ end
26
+
27
+ agent.toolbox.after("write") do |_, result|
28
+ if result[:error]
29
+ agent.output.say " ! #{result[:error]}"
30
+ else
31
+ agent.output.display_file(result[:path], fallback: " -> #{result[:path]}")
32
+ agent.toolbox.run("verify", { "path" => result[:path] }) if agent.toolbox.tools.key?("verify")
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "elelem"
4
+ require "fileutils"
5
+ require "json"
6
+ require "open3"
7
+ require "pathname"
8
+ require "stringio"
9
+ require "tempfile"
10
+
11
+ require_relative "builtins/version"
12
+
13
+ module Elelem
14
+ module Builtins
15
+ def self.sh(cmd, args: [], cwd: Dir.pwd, env: {}, timeout: nil)
16
+ output = StringIO.new
17
+ options = { chdir: cwd }
18
+ options[:pgroup] = true if timeout
19
+
20
+ Open3.popen2e(env, cmd, *args, **options) do |stdin, out, wait_thr|
21
+ stdin.close
22
+ timed_out = false
23
+ watchdog = timeout && Thread.new do
24
+ sleep(timeout)
25
+ timed_out = true
26
+ terminate(wait_thr.pid)
27
+ end
28
+
29
+ out.each_line do |line|
30
+ yield line if block_given?
31
+ output.write(line)
32
+ end
33
+ timed_out ? watchdog&.join : watchdog&.kill
34
+
35
+ status = wait_thr.value
36
+ note = timed_out ? "\n[command timed out after #{timeout}s]" : ""
37
+ { exit_status: status.exitstatus || (timed_out ? 124 : 1), content: truncate(output.string) + note }
38
+ end
39
+ end
40
+
41
+ def self.truncate(content, limit: 10_000)
42
+ return content if content.length <= limit
43
+
44
+ half = limit / 2
45
+ elided = content.length - limit
46
+ "#{content[0, half]}\n[#{elided} characters elided]\n#{content[-half, half]}"
47
+ end
48
+
49
+ def self.terminate(pid)
50
+ Process.kill("TERM", -pid)
51
+ sleep 0.5
52
+ Process.kill("KILL", -pid)
53
+ rescue Errno::ESRCH
54
+ nil
55
+ end
56
+
57
+ def self.command_timeout
58
+ value = ENV["ELELEM_CMD_TIMEOUT"]
59
+ value && !value.empty? ? Integer(value) : nil
60
+ end
61
+ end
62
+ end
63
+
64
+ require_relative "builtins/provider"
65
+ require_relative "builtins/reload"
66
+ require_relative "builtins/builtins"
67
+ require_relative "builtins/context"
68
+ require_relative "builtins/tools"
69
+ require_relative "builtins/read"
70
+ require_relative "builtins/write"
71
+ require_relative "builtins/execute"
72
+ require_relative "builtins/terminal"
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Elelem::Builtins::ToolMarkdown do
4
+ subject(:tool) do
5
+ Elelem::Tool.new("greet",
6
+ description: "Greets someone.\nMore detail.",
7
+ params: { name: { type: "string", description: "who to greet" } },
8
+ required: ["name"],
9
+ aliases: ["hi"]
10
+ ) { |args| { greeting: "hi #{args["name"]}" } }
11
+ end
12
+
13
+ describe ".summary" do
14
+ it "renders a single table row with the first description line" do
15
+ expect(described_class.summary(tool)).to eq("| greet | Greets someone. |")
16
+ end
17
+ end
18
+
19
+ describe ".to_markdown" do
20
+ it "renders name, description, params, and aliases" do
21
+ markdown = described_class.to_markdown(tool)
22
+
23
+ expect(markdown).to include("## greet")
24
+ expect(markdown).to include("Greets someone.\nMore detail.")
25
+ expect(markdown).to include("- `name` (string, required) - who to greet")
26
+ expect(markdown).to include("*aliases: hi*")
27
+ end
28
+
29
+ it "omits the aliases line when there are none" do
30
+ plain_tool = Elelem::Tool.new("greet", description: "Greets someone.") { }
31
+
32
+ expect(described_class.to_markdown(plain_tool)).not_to include("aliases:")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/elelem/builtins"
4
+
5
+ RSpec.configure do |config|
6
+ config.disable_monkey_patching!
7
+
8
+ config.expect_with :rspec do |c|
9
+ c.syntax = :expect
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elelem-builtins
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - mo khan
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: elelem
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '0.13'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '0.13'
26
+ - !ruby/object:Gem::Dependency
27
+ name: fileutils
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.8'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.8'
40
+ - !ruby/object:Gem::Dependency
41
+ name: json
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: open3
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.2'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.2'
68
+ - !ruby/object:Gem::Dependency
69
+ name: stringio
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.1'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '3.1'
82
+ - !ruby/object:Gem::Dependency
83
+ name: tempfile
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.3'
89
+ type: :runtime
90
+ prerelease: false
91
+ version_requirements: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.3'
96
+ description: Bundles the execute, read, write, tools, context, provider, reload, and
97
+ builtins plugins that make a freshly-installed elelem agent useful.
98
+ email:
99
+ - mo@mokhan.ca
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".rspec"
105
+ - LICENSE.txt
106
+ - Rakefile
107
+ - lib/elelem/builtins.rb
108
+ - lib/elelem/builtins/builtins.rb
109
+ - lib/elelem/builtins/context.rb
110
+ - lib/elelem/builtins/execute.rb
111
+ - lib/elelem/builtins/provider.rb
112
+ - lib/elelem/builtins/read.rb
113
+ - lib/elelem/builtins/reload.rb
114
+ - lib/elelem/builtins/terminal.rb
115
+ - lib/elelem/builtins/tools.rb
116
+ - lib/elelem/builtins/version.rb
117
+ - lib/elelem/builtins/write.rb
118
+ - spec/elelem/builtins/tool_markdown_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://src.mokhan.ca/elelem/builtins
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ allowed_push_host: https://rubygems.org
125
+ homepage_uri: https://src.mokhan.ca/elelem/builtins
126
+ source_code_uri: https://src.mokhan.ca/elelem/builtins
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 4.0.0
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 4.0.20
142
+ specification_version: 4
143
+ summary: Default tools and commands for elelem.
144
+ test_files: []