aigency 0.3.0 → 0.5.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/CHANGELOG.md +34 -0
- data/README.md +7 -15
- data/lib/aigency/aigent/current_time.rb +33 -0
- data/lib/aigency/aigent/shell.rb +124 -0
- data/lib/aigency/aigent.rb +110 -36
- data/lib/aigency/delegator.rb +11 -0
- data/lib/aigency/plugin/shell.rb +11 -0
- data/lib/aigency/plugin.rb +6 -2
- data/lib/aigency/rake.rb +13 -0
- data/lib/aigency/sandbox/copy_from_tempdir.rb +30 -0
- data/lib/aigency/sandbox/copy_to_tempdir.rb +30 -0
- data/lib/aigency/sandbox/exec.rb +2 -2
- data/lib/aigency/sandbox/helpers.rb +15 -0
- data/lib/aigency/sandbox.rb +9 -5
- data/lib/aigency/shell.rb +4 -11
- data/lib/aigency/tempdir/helpers.rb +15 -0
- data/lib/aigency/tempdir/list.rb +18 -0
- data/lib/aigency/tempdir/read.rb +28 -0
- data/lib/aigency/tempdir/size.rb +27 -0
- data/lib/aigency/tempdir/write.rb +28 -0
- data/lib/aigency/tempdir.rb +19 -0
- data/lib/aigency/tool.rb +5 -0
- data/lib/aigency/usability.rb +1 -0
- data/lib/aigency/version.rb +1 -1
- data/lib/aigency/workspace/cat.rb +2 -2
- data/lib/aigency/workspace/{shell.rb → cmd.rb} +3 -3
- data/lib/aigency/workspace/copy_from_tempdir.rb +29 -0
- data/lib/aigency/workspace/copy_to_tempdir.rb +29 -0
- data/lib/aigency/workspace/cp.rb +2 -2
- data/lib/aigency/workspace/diff.rb +2 -2
- data/lib/aigency/workspace/grep.rb +2 -2
- data/lib/aigency/workspace/head.rb +2 -2
- data/lib/aigency/workspace/ls.rb +2 -2
- data/lib/aigency/workspace/mkdir.rb +2 -2
- data/lib/aigency/workspace/mv.rb +2 -2
- data/lib/aigency/workspace/rm.rb +2 -2
- data/lib/aigency/workspace/tail.rb +2 -2
- data/lib/aigency/workspace/wc.rb +2 -2
- data/lib/aigency/workspace/write.rb +2 -2
- data/lib/aigency/workspace.rb +4 -4
- data/lib/aigency.rb +16 -14
- data/scaffold/{Rakefile → aigent/aigent.rake} +1 -10
- data/scaffold/{.gitignore → enlist/.gitignore} +1 -0
- data/scaffold/{README.md → enlist/README.md} +7 -3
- data/scaffold/enlist/Rakefile +9 -0
- data/scaffold/enlist/bin/aigent +11 -0
- data/scaffold/{config → enlist/config}/schedule.rb +5 -0
- data/scaffold/sandbox/Dockerfile +37 -0
- data/scaffold/sandbox/docker-compose.yml +37 -0
- data/{lib/aigency/sandbox/squid.conf.tpl → scaffold/sandbox/squid.conf} +4 -2
- data/scaffold/sandbox/ssh_config +2 -0
- metadata +49 -17
- data/lib/aigency/sandbox/Dockerfile.tpl +0 -13
- data/lib/aigency/sandbox/docker-compose.yml.tpl +0 -26
- data/scaffold/config/aigent.yml +0 -9
- /data/scaffold/{.env.example → enlist/.env.example} +0 -0
- /data/scaffold/{Gemfile → enlist/Gemfile} +0 -0
- /data/scaffold/{IDENTITY.md → enlist/IDENTITY.md} +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 873660a498f3b272c6ed8e29c970a90f2d68efea0581de2f73fae64f3c400c8e
|
|
4
|
+
data.tar.gz: 830594c5ce2d4b37ab09e678c93633716ce6ee1e0937a8c14f12e42063ee464e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7af922b9e3a2cc281f8696dd778a69f696a58193578bae541bb7a24fb1ac93e07e6ab5071d8fe160a6fed801c278be7cb58b4bd78e054c5cb2d8a5ae4af5281
|
|
7
|
+
data.tar.gz: 7f20a0f6fa6e7de82aa1df2069d5fe397c797793790d772f1961c88b353301fad2ed6cd242cecac9190f900aaff5cc7ada36a2399c319d5474654ead6db853d9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
## 0.5.0 - November 28, 2026
|
|
2
|
+
- Sandbox configuration updates
|
|
3
|
+
- Tempdir tools
|
|
4
|
+
- Aigent plugin
|
|
5
|
+
- Upgrade to RubyLLM 2
|
|
6
|
+
- New aigent CLI
|
|
7
|
+
|
|
8
|
+
## 0.4.0 - October 21, 2026
|
|
9
|
+
- Config file directory
|
|
10
|
+
- Add openssh to sandbox
|
|
11
|
+
- Sandbox configuration
|
|
12
|
+
- Tempdir tools
|
|
13
|
+
- Aigent plugin
|
|
14
|
+
- Proxy environment variables
|
|
15
|
+
- apk update && apk upgrade
|
|
16
|
+
- Plugins add rake files
|
|
17
|
+
- Aigent setup in README
|
|
18
|
+
- Rearrange sandbox scaffold
|
|
19
|
+
- Add bin/aigent to aigent directory
|
|
20
|
+
- Fix grep spec for BusyBox
|
|
21
|
+
- CurrentTime tool
|
|
22
|
+
- Dockerfile packages and mise setup
|
|
23
|
+
- Instructions subcommand
|
|
24
|
+
- Aigent#stream errors write to error.log
|
|
25
|
+
- Aigent stream command
|
|
26
|
+
- Remove adduser recommendation
|
|
27
|
+
- Clarify Docker requirement
|
|
28
|
+
- Plugin shells
|
|
29
|
+
- Tempdir file size management
|
|
30
|
+
- CopyTempdirFile tool
|
|
31
|
+
- Aigent plugin setup
|
|
32
|
+
- Update chat calls for RubyLLM 2.0
|
|
33
|
+
- Use the chat completions protocol
|
|
34
|
+
|
|
1
35
|
## 0.3.0 - October 9, 2026
|
|
2
36
|
- New plugin architecture
|
|
3
37
|
- Workspace and Sandbox plugins
|
data/README.md
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
A framework for creating AI agents with RubyLLM.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Requirements
|
|
6
6
|
|
|
7
7
|
- Ruby 3.2+
|
|
8
|
-
- Docker 29+
|
|
8
|
+
- Docker 29+ (optional; required for the sandbox plugin)
|
|
9
9
|
|
|
10
10
|
(Note: Aigency is not tested on Windows. Some features are known to be Windows-incompatible.)
|
|
11
11
|
|
|
@@ -27,28 +27,20 @@ gem 'aigency'
|
|
|
27
27
|
|
|
28
28
|
### Creating a New Aigent
|
|
29
29
|
|
|
30
|
-
The recommended way to create an aigent is to give it a dedicated system user.
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
useradd aigent_user
|
|
34
|
-
sudo su - aigent_user
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Once you're logged in as the aigent user, create the aigent directory:
|
|
38
|
-
|
|
39
30
|
```bash
|
|
40
31
|
aigency enlist
|
|
41
32
|
cd aigent
|
|
42
33
|
bundle install
|
|
43
34
|
rake setup
|
|
35
|
+
cp .env.example .env
|
|
44
36
|
```
|
|
45
37
|
|
|
46
|
-
The bare minimum configuration you need to do is setting `
|
|
38
|
+
The bare minimum configuration you need to do is setting `OPENAI_API_BASE` and `OPENAI_API_KEY` in `.env`.
|
|
47
39
|
|
|
48
|
-
Test the aigent with the `
|
|
40
|
+
Test the aigent with the `say` command.
|
|
49
41
|
|
|
50
42
|
```bash
|
|
51
|
-
|
|
43
|
+
bin/aigent say "How many fibers are intertwined in a shredded wheat biscuit?"
|
|
52
44
|
```
|
|
53
45
|
|
|
54
|
-
See the aigent's README (scaffold/README.md in this codebase)
|
|
46
|
+
See the aigent's README for more information (`README.md` in your aigent directory or `scaffold/enlist/README.md` in this codebase).
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'date'
|
|
4
|
+
|
|
5
|
+
module Aigency
|
|
6
|
+
class Aigent
|
|
7
|
+
class CurrentTime < Aigency::Tool
|
|
8
|
+
description 'Get the current time'
|
|
9
|
+
|
|
10
|
+
parameters do
|
|
11
|
+
any_of :format, description: "Date/time format" do
|
|
12
|
+
string enum: %w[iso8601 unix]
|
|
13
|
+
'iso8601'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def execute(format: 'iso8601')
|
|
18
|
+
aigent.log.puts "[Aigent::CurrentTime] #{format}"
|
|
19
|
+
now = DateTime.now
|
|
20
|
+
case format
|
|
21
|
+
when 'iso8601'
|
|
22
|
+
{ success: true, current_time: now.iso8601 }
|
|
23
|
+
when 'unix'
|
|
24
|
+
{ success: true, current_time: now.to_time.to_i }
|
|
25
|
+
else
|
|
26
|
+
{ success: false, error: "Invalid format: #{format}" }
|
|
27
|
+
end
|
|
28
|
+
rescue StandardError => e
|
|
29
|
+
{ success: false, error: "[#{e.class}] #{e.message}"}
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'thor'
|
|
4
|
+
|
|
5
|
+
module Aigency
|
|
6
|
+
class Aigent
|
|
7
|
+
# Commands that can be executed in aigent directories.
|
|
8
|
+
#
|
|
9
|
+
class Shell < Thor
|
|
10
|
+
include Delegator
|
|
11
|
+
|
|
12
|
+
desc "say PROMPT", "Send a prompt to the aigent"
|
|
13
|
+
def say(prompt)
|
|
14
|
+
aigent.stream prompt
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
desc "load FILE", "Load a prompt from a file"
|
|
18
|
+
def load(file)
|
|
19
|
+
aigent.load filename
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
desc "setup", "Initialize aigent plugins"
|
|
23
|
+
def setup
|
|
24
|
+
aigent.setup
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
desc "tools", "Get a list of available tools"
|
|
28
|
+
def tools
|
|
29
|
+
puts aigent.tools.map(&:to_s).sort
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
desc "plugins", "Get a list of installed plugins"
|
|
33
|
+
def plugins
|
|
34
|
+
puts aigent.plugins.map(&:name).sort
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
desc "status", "Get the aigent's current status"
|
|
38
|
+
def status
|
|
39
|
+
puts "Name: #{aigent.config('aigent')['name']}"
|
|
40
|
+
puts "Busy: #{aigent.busy?}"
|
|
41
|
+
if aigent.busy?
|
|
42
|
+
puts "Process: #{aigent.lock_owner || 'unknown'}"
|
|
43
|
+
end
|
|
44
|
+
puts "Plugins: #{aigent.plugins.length}"
|
|
45
|
+
puts "Tools: #{aigent.tools.length}"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
desc "instructions", "List base instructions"
|
|
49
|
+
long_desc <<~DESC
|
|
50
|
+
Show all instructions that will be given to the aigent before the user
|
|
51
|
+
prompt. They typically include the IDENTITY.md file and any prompts injected
|
|
52
|
+
by installed plugins.
|
|
53
|
+
DESC
|
|
54
|
+
def instructions
|
|
55
|
+
puts aigent.instructions
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
desc "stream", "Tail the currently active chat stream"
|
|
59
|
+
option :follow, type: :boolean, aliases: :f, desc: "Follow the stream (tail -f)"
|
|
60
|
+
option :lines, type: :numeric, aliases: :n, desc: "Number of lines (tail -n)", default: 10
|
|
61
|
+
long_desc <<~DESC
|
|
62
|
+
This is useful for monitoring non-interactive chat streams, e.g., chat sessions
|
|
63
|
+
that were started by cron jobs or rake tasks. It runs `tail` on the current chat
|
|
64
|
+
file in the aigent's data/chats folder.
|
|
65
|
+
DESC
|
|
66
|
+
def stream
|
|
67
|
+
if aigent.busy?
|
|
68
|
+
file = Dir.glob(aigent.data('chats', '*.txt')).max { |file| File.mtime(file).to_i }
|
|
69
|
+
cmd = ['tail', file, '-n', options[:lines].to_s]
|
|
70
|
+
cmd.push '-f' if options[:follow]
|
|
71
|
+
puts "(#{file})"
|
|
72
|
+
exec *cmd
|
|
73
|
+
else
|
|
74
|
+
puts "No active chat stream."
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
desc "kill", "Kill a blocking process"
|
|
79
|
+
long_desc <<~DESC
|
|
80
|
+
Kill the process that owns an exclusive lock on the aigent's data/lock file.
|
|
81
|
+
(This is the process that flags the aigent as `busy`.)
|
|
82
|
+
DESC
|
|
83
|
+
def kill
|
|
84
|
+
unless aigent.busy?
|
|
85
|
+
puts "No blocking process exists."
|
|
86
|
+
return
|
|
87
|
+
end
|
|
88
|
+
pid = aigent.lock_owner
|
|
89
|
+
if pid.nil?
|
|
90
|
+
puts "Could not determine the PID of the blocking process."
|
|
91
|
+
return
|
|
92
|
+
end
|
|
93
|
+
puts "Killing process #{pid}"
|
|
94
|
+
Process.kill 'TERM', pid
|
|
95
|
+
rescue Errno::ESRCH
|
|
96
|
+
puts "Process #{pid} no longer exists."
|
|
97
|
+
rescue Errno::EPERM
|
|
98
|
+
puts "Permission denied to kill process #{pid}."
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
class << self
|
|
102
|
+
def mounted? = false
|
|
103
|
+
|
|
104
|
+
def start(args = ARGV, config = {})
|
|
105
|
+
return super if mounted?
|
|
106
|
+
build('.').start(args, config)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def build(directory)
|
|
110
|
+
Class.new(self) do
|
|
111
|
+
def self.mounted? = true
|
|
112
|
+
Aigent.new(directory).plugins.each do |plugin|
|
|
113
|
+
next unless plugin.shell
|
|
114
|
+
if all_commands.key?(plugin.name)
|
|
115
|
+
raise "Plugin '#{plugin.name}' cannot register CLI command '#{plugin.name}': already taken"
|
|
116
|
+
end
|
|
117
|
+
register(plugin.shell, plugin.name, "#{plugin.name} [SUBCOMMAND ...ARGS]", "#{plugin.name} plugin commands")
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
data/lib/aigency/aigent.rb
CHANGED
|
@@ -6,13 +6,16 @@ require 'set'
|
|
|
6
6
|
|
|
7
7
|
module Aigency
|
|
8
8
|
class Aigent
|
|
9
|
+
require 'aigency/aigent/current_time'
|
|
10
|
+
require 'aigency/aigent/shell'
|
|
11
|
+
|
|
9
12
|
# @return [IO]
|
|
10
13
|
attr_reader :log
|
|
11
14
|
|
|
12
15
|
def initialize(directory)
|
|
13
16
|
@base_directory = File.absolute_path(directory)
|
|
14
17
|
@log = $stdout
|
|
15
|
-
|
|
18
|
+
preload
|
|
16
19
|
end
|
|
17
20
|
|
|
18
21
|
# @return [String]
|
|
@@ -42,10 +45,10 @@ module Aigency
|
|
|
42
45
|
# @param except [Array<Class<Tool>>] Tools to exclude from this chat
|
|
43
46
|
# @return [RubyLLM::Chat]
|
|
44
47
|
def brief(*extra, except: [])
|
|
45
|
-
chat = rubyllm_context.chat(provider: :openai)
|
|
46
|
-
(tools - except).each { |klass| chat.
|
|
47
|
-
chat.with_instructions(build_instructions(extra), append: true)
|
|
48
|
-
chat.
|
|
48
|
+
chat = rubyllm_context.chat(provider: :openai, protocol: 'chat_completions').with_thinking(effort: :medium)
|
|
49
|
+
(tools - except).each { |klass| chat.with_tools(klass.new(self)) }
|
|
50
|
+
chat.with_instructions(build_instructions(extra).join, append: true)
|
|
51
|
+
chat.with_provider_options(max_tokens: max_tokens)
|
|
49
52
|
end
|
|
50
53
|
|
|
51
54
|
# Prompt a chat with a file.
|
|
@@ -71,14 +74,17 @@ module Aigency
|
|
|
71
74
|
@log = out
|
|
72
75
|
response = brief(*extra, except: except).ask text, with: attach do |chunk|
|
|
73
76
|
@log.print chunk.thinking&.text
|
|
74
|
-
@log.print chunk.content
|
|
75
77
|
end
|
|
78
|
+
@log.puts response.content
|
|
76
79
|
@log.puts "Input tokens: #{response.tokens.input}"
|
|
77
80
|
@log.puts "Output tokens: #{response.tokens.output}"
|
|
81
|
+
@log.puts "Cache read: #{response.tokens.cache_read}"
|
|
82
|
+
@log.puts "Cache write: #{response.tokens.cache_write}"
|
|
78
83
|
response
|
|
79
84
|
rescue StandardError => e
|
|
80
|
-
|
|
81
|
-
@log.puts
|
|
85
|
+
message = "[#{e.class}] #{e.message}\n" + e.backtrace.map { |line| " #{line}\n" }.join
|
|
86
|
+
@log.puts message
|
|
87
|
+
File.write data('log', 'error.log'), "#{DateTime.now.iso8601} #{message}", mode: 'a'
|
|
82
88
|
ensure
|
|
83
89
|
@log = original_log
|
|
84
90
|
end
|
|
@@ -86,7 +92,7 @@ module Aigency
|
|
|
86
92
|
# @param name [String]
|
|
87
93
|
# @return [Hash]
|
|
88
94
|
def config(name)
|
|
89
|
-
configs[name] ||= read_config(name)
|
|
95
|
+
configs[File.basename(name, '.*')] ||= read_config(name)
|
|
90
96
|
end
|
|
91
97
|
|
|
92
98
|
# Initialize the aigent's plugin configurations.
|
|
@@ -98,21 +104,27 @@ module Aigency
|
|
|
98
104
|
|
|
99
105
|
# Run code in an exclusive lock
|
|
100
106
|
#
|
|
101
|
-
def
|
|
102
|
-
File.open(data('lock'),
|
|
107
|
+
def block(&block)
|
|
108
|
+
File.open(data('lock'), File::RDWR | File::CREAT, 0o644) do |file|
|
|
103
109
|
file.flock(File::LOCK_EX)
|
|
110
|
+
file.truncate(0)
|
|
111
|
+
file.rewind
|
|
104
112
|
file.write(Process.pid)
|
|
105
113
|
file.flush
|
|
106
114
|
block&.call
|
|
107
115
|
end
|
|
108
116
|
end
|
|
117
|
+
alias lock block
|
|
118
|
+
alias flock block
|
|
109
119
|
|
|
110
120
|
# Run code if an exclusive lock is available or skip if a lock exists
|
|
111
121
|
#
|
|
112
122
|
def try_with_lock(&block)
|
|
113
|
-
File.open(data('lock'),
|
|
123
|
+
File.open(data('lock'), File::RDWR | File::CREAT, 0o644) do |file|
|
|
114
124
|
acquired = file.flock(File::LOCK_EX|File::LOCK_NB)
|
|
115
125
|
return unless acquired
|
|
126
|
+
file.truncate(0)
|
|
127
|
+
file.rewind
|
|
116
128
|
file.write(Process.pid)
|
|
117
129
|
file.flush
|
|
118
130
|
block&.call
|
|
@@ -130,33 +142,48 @@ module Aigency
|
|
|
130
142
|
end
|
|
131
143
|
alias busy? locked?
|
|
132
144
|
|
|
145
|
+
# Get the PID of the process holding the exclusive lock (if any)
|
|
146
|
+
#
|
|
147
|
+
# Reads the PID from the lockfile and verifies that the process exists.
|
|
148
|
+
# If the lockfile PID is missing or stale, falls back to inspecting the
|
|
149
|
+
# OS lock table (Linux /proc/locks) for the flock owner.
|
|
150
|
+
#
|
|
151
|
+
# @return [Integer, nil]
|
|
152
|
+
def lock_owner
|
|
153
|
+
return nil unless locked?
|
|
154
|
+
pid = File.read(data('lock')).to_i
|
|
155
|
+
return pid if pid > 0 && process_exists?(pid)
|
|
156
|
+
proc_locks_owner
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @return [String]
|
|
133
160
|
def instructions
|
|
134
|
-
@instructions ||= build_instructions([])
|
|
161
|
+
@instructions ||= build_instructions([]).join("\n\n")
|
|
135
162
|
end
|
|
136
163
|
|
|
137
164
|
# @return [Array<Plugin>]
|
|
138
165
|
def plugins
|
|
139
166
|
config('aigent').fetch('plugins', [])
|
|
140
|
-
.
|
|
167
|
+
.unshift('aigent')
|
|
168
|
+
.uniq
|
|
169
|
+
.filter_map { |name| Plugin.registry[name] }
|
|
141
170
|
end
|
|
142
171
|
|
|
143
172
|
def tools
|
|
144
|
-
plugins.flat_map(&:tools)
|
|
173
|
+
plugins.flat_map(&:tools).uniq
|
|
145
174
|
end
|
|
146
175
|
|
|
147
176
|
private
|
|
148
177
|
|
|
178
|
+
# Eager load plugin configurations. This is useful in situations where the
|
|
179
|
+
# environment variables might change after the aigent was instantiated.
|
|
180
|
+
#
|
|
181
|
+
def preload
|
|
182
|
+
plugins.each { |plugin| config(plugin.name) }
|
|
183
|
+
end
|
|
184
|
+
|
|
149
185
|
# @param plugin [Plugin]
|
|
150
186
|
def boot(plugin)
|
|
151
|
-
unless plugin.config.empty?
|
|
152
|
-
config_file = File.absolute_path("#{plugin.name}.yml", directory)
|
|
153
|
-
if File.exist?(config_file)
|
|
154
|
-
log.puts "File #{plugin.name}.yml exists, skipping"
|
|
155
|
-
else
|
|
156
|
-
log.puts "Creating #{plugin.name}.yml"
|
|
157
|
-
File.write config_file, plugin.config
|
|
158
|
-
end
|
|
159
|
-
end
|
|
160
187
|
plugin.directories.each do |dir|
|
|
161
188
|
absolute = File.absolute_path(dir, directory)
|
|
162
189
|
if File.directory?(absolute)
|
|
@@ -166,6 +193,15 @@ module Aigency
|
|
|
166
193
|
FileUtils.mkdir_p absolute
|
|
167
194
|
end
|
|
168
195
|
end
|
|
196
|
+
unless plugin.config.empty?
|
|
197
|
+
config_file = File.absolute_path(File.join('config', "#{plugin.name}.yml"), directory)
|
|
198
|
+
if File.exist?(config_file)
|
|
199
|
+
log.puts "File config/#{plugin.name}.yml exists, skipping"
|
|
200
|
+
else
|
|
201
|
+
log.puts "Creating config/#{plugin.name}.yml"
|
|
202
|
+
File.write config_file, plugin.config
|
|
203
|
+
end
|
|
204
|
+
end
|
|
169
205
|
plugin.files.each do |file, content|
|
|
170
206
|
absolute = File.absolute_path(file, directory)
|
|
171
207
|
if File.exist?(absolute)
|
|
@@ -211,11 +247,10 @@ module Aigency
|
|
|
211
247
|
@base_instructions ||= identity_file? ? File.read(identity_file) : "You are a helpful AI assistant."
|
|
212
248
|
end
|
|
213
249
|
|
|
250
|
+
# @return [Array<String>]
|
|
214
251
|
def build_instructions(extra)
|
|
215
|
-
[base_instructions].concat
|
|
216
|
-
.
|
|
217
|
-
.concat extra
|
|
218
|
-
.join("\n\n")
|
|
252
|
+
[base_instructions].concat(tool_prompts)
|
|
253
|
+
.concat(extra)
|
|
219
254
|
end
|
|
220
255
|
|
|
221
256
|
def tool_prompts
|
|
@@ -225,14 +260,6 @@ module Aigency
|
|
|
225
260
|
.compact
|
|
226
261
|
end
|
|
227
262
|
|
|
228
|
-
def load_requires
|
|
229
|
-
read_requires.each { |name| require name }
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
def read_requires
|
|
233
|
-
config('aigent')['require'] || []
|
|
234
|
-
end
|
|
235
|
-
|
|
236
263
|
def max_tokens
|
|
237
264
|
@max_tokens ||= (config('aigent')['max_tokens'] || -1).to_i
|
|
238
265
|
end
|
|
@@ -243,5 +270,52 @@ module Aigency
|
|
|
243
270
|
config.openai_api_key = config('aigent')['openai_api_key']
|
|
244
271
|
end
|
|
245
272
|
end
|
|
273
|
+
|
|
274
|
+
# @return [Boolean] True if a process with the given PID exists
|
|
275
|
+
def process_exists?(pid)
|
|
276
|
+
Process.kill 0, pid
|
|
277
|
+
true
|
|
278
|
+
rescue Errno::ESRCH
|
|
279
|
+
false
|
|
280
|
+
rescue Errno::EPERM
|
|
281
|
+
true
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# Parse the Linux /proc/locks table for the PID holding a flock on the lockfile
|
|
285
|
+
#
|
|
286
|
+
# @return [Integer, nil]
|
|
287
|
+
def proc_locks_owner
|
|
288
|
+
return nil unless File.exist?("/proc/locks")
|
|
289
|
+
stat = File.stat(data('lock'))
|
|
290
|
+
File.foreach("/proc/locks") do |line|
|
|
291
|
+
fields = line.split
|
|
292
|
+
next unless fields[1] == "FLOCK"
|
|
293
|
+
major, minor, ino = fields[5].to_s.split(":")
|
|
294
|
+
next unless major.to_i(16) == ((stat.dev >> 8) & 0xfff)
|
|
295
|
+
next unless minor.to_i(16) == ((stat.dev & 0xff) | ((stat.dev >> 12) & 0xffffff00))
|
|
296
|
+
next unless ino.to_i == stat.ino # inode is decimal in /proc/locks
|
|
297
|
+
owner = fields[4].to_i
|
|
298
|
+
return owner if owner > 0 && owner != Process.pid
|
|
299
|
+
end
|
|
300
|
+
nil
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
Plugin.register Plugin.new(
|
|
304
|
+
'aigent',
|
|
305
|
+
tools: [CurrentTime],
|
|
306
|
+
directories: ['config', 'data/chats', 'data/log'],
|
|
307
|
+
rakefile: File.join(GEM_PATH, 'scaffold', 'aigent', 'aigent.rake'),
|
|
308
|
+
config: <<~FILE
|
|
309
|
+
name: Aigent
|
|
310
|
+
openai_api_base: ${OPENAI_API_BASE}
|
|
311
|
+
openai_api_key: ${OPENAI_API_KEY}
|
|
312
|
+
# max_tokens: 65536
|
|
313
|
+
plugins:
|
|
314
|
+
# You should only need either a workspace or a sandbox, not both. If you
|
|
315
|
+
# enable one, it's recommended that you disable the other.
|
|
316
|
+
# - sandbox
|
|
317
|
+
- workspace
|
|
318
|
+
FILE
|
|
319
|
+
)
|
|
246
320
|
end
|
|
247
321
|
end
|
data/lib/aigency/plugin.rb
CHANGED
|
@@ -2,20 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
module Aigency
|
|
4
4
|
class Plugin
|
|
5
|
-
|
|
5
|
+
require 'aigency/plugin/shell'
|
|
6
|
+
|
|
7
|
+
attr_reader :name, :config, :tools, :directories, :files, :rakefile, :shell
|
|
6
8
|
|
|
7
9
|
# @param name [String]
|
|
8
10
|
# @param tools [Array<Class<Tool>>]
|
|
9
11
|
# @param config [String]
|
|
10
12
|
# @param directories [Array<String>]
|
|
11
13
|
# @param files [Hash{String => String}]
|
|
12
|
-
def initialize name, tools: [], config: '', directories: [], files: {}
|
|
14
|
+
def initialize name, tools: [], config: '', directories: [], files: {}, rakefile: nil, shell: nil
|
|
13
15
|
raise "A plugin named '#{name}' already exists" if self.class.registry.key?(name)
|
|
14
16
|
@name = name
|
|
15
17
|
@config = config
|
|
16
18
|
@tools = tools
|
|
17
19
|
@directories = directories
|
|
18
20
|
@files = files
|
|
21
|
+
@rakefile = rakefile
|
|
22
|
+
@shell = shell
|
|
19
23
|
self.class.registry[name] = self
|
|
20
24
|
end
|
|
21
25
|
|
data/lib/aigency/rake.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'aigency/tempdir'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
|
|
6
|
+
module Aigency
|
|
7
|
+
module Sandbox
|
|
8
|
+
class CopyFromTempdir < Tool
|
|
9
|
+
include Helpers
|
|
10
|
+
include Tempdir::Helpers
|
|
11
|
+
|
|
12
|
+
description 'Copy a file from tempdir to sandbox'
|
|
13
|
+
|
|
14
|
+
parameters do
|
|
15
|
+
string :tempdir_source, description: "A destination filename in tempdir", required: true
|
|
16
|
+
string :sandbox_destination, description: "A relative path to a sandbox file", required: true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def execute(tempdir_source:, sandbox_destination:)
|
|
20
|
+
aigent.log.puts "[Sandbox::CopyToTempdir] #{tempdir_source} #{sandbox_destination}"
|
|
21
|
+
abs_tempdir = expand_in_tempdir(tempdir_source)
|
|
22
|
+
abs_sandbox = expand_in_sandbox(sandbox_destination)
|
|
23
|
+
FileUtils.copy_file abs_tempdir, abs_sandbox
|
|
24
|
+
{ success: true, file: abs_sandbox[aigent.data('sandbox').length..]}
|
|
25
|
+
rescue StandardError => e
|
|
26
|
+
{ success: false, error: "[#{e.class}] #{e.message}"}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'aigency/tempdir'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
|
|
6
|
+
module Aigency
|
|
7
|
+
module Sandbox
|
|
8
|
+
class CopyToTempdir < Tool
|
|
9
|
+
include Helpers
|
|
10
|
+
include Tempdir::Helpers
|
|
11
|
+
|
|
12
|
+
description 'Copy a file from sandbox to tempdir'
|
|
13
|
+
|
|
14
|
+
parameters do
|
|
15
|
+
string :sandbox_source, description: "A relative path to a sandbox file", required: true
|
|
16
|
+
string :tempdir_destination, description: "A destination filename in tempdir", required: true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def execute(sandbox_source:, tempdir_destination:)
|
|
20
|
+
aigent.log.puts "[Sandbox::CopyToTempdir] #{sandbox_source} #{tempdir_destination}"
|
|
21
|
+
abs_sandbox = expand_in_sandbox(sandbox_source)
|
|
22
|
+
abs_tempdir = expand_in_tempdir(tempdir_destination)
|
|
23
|
+
FileUtils.copy_file abs_sandbox, abs_tempdir
|
|
24
|
+
{ success: true, file: abs_sandbox }
|
|
25
|
+
rescue StandardError => e
|
|
26
|
+
{ success: false, error: "[#{e.class}] #{e.message}"}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/lib/aigency/sandbox/exec.rb
CHANGED
|
@@ -5,9 +5,9 @@ require 'open3'
|
|
|
5
5
|
module Aigency
|
|
6
6
|
module Sandbox
|
|
7
7
|
class Exec < Aigency::Tool
|
|
8
|
-
|
|
8
|
+
description 'Run a shell command in your sandbox'
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
parameters do
|
|
11
11
|
string :command, description: "The complete command to execute", required: true
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Aigency
|
|
4
|
+
module Sandbox
|
|
5
|
+
module Helpers
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
def expand_in_sandbox path
|
|
9
|
+
absolute = File.absolute_path(path, aigent.data('sandbox'))
|
|
10
|
+
raise PathError, "#{path} is not relative to tempdir" unless absolute.start_with?(aigent.data('sandbox'))
|
|
11
|
+
absolute
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|