omniai-tools 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbc1dae9ab7fe238fadd4c48d660f342e851b1803e3e33e22c487ceabd3bf32b
4
- data.tar.gz: cf37310371a1711adaedea7609ce340552c7858faab4d9b60c6d26919864490a
3
+ metadata.gz: f361e26584723aa8d30ba41edaefafb44a7e08c5c6345656dbf25e37c965448d
4
+ data.tar.gz: c718feeb39dfdd1c2c5ff9a2976ece26eec48aae12f52405724fc21209101d32
5
5
  SHA512:
6
- metadata.gz: 766b80d9ab2b3bf459eeee00473c69f352fd4e61b70a4be9531a76b164d1e1fbc7373fe634033f19c519dc56098b77e536431d53a46bd91bde017887ed91f61a
7
- data.tar.gz: 1fc747fda798976e4c13e4c0fd208a83253b61d8f1b473029e085308f7603f7d20e616f32c9b0e564952e6f48cf4e1eb10cc5570d8d6525c6c4a9728449e5b08
6
+ metadata.gz: fa14b3333edc0b235e046d9f408f75f201ea37be76435d897873fb7585999cd0aeac3d36c86c16e72a772ab6a28f48dd40560074767720736583de79a80d8847
7
+ data.tar.gz: 6ec511fc27bae83abdf0d827572ad2784419039b0d37d128d5670b19d58645a8e6efd82bfb7132397473181b56897f966314396a76172aaec33883826ff14f12
data/Gemfile CHANGED
@@ -16,4 +16,5 @@ gem "rubocop-factory_bot"
16
16
  gem "rubocop-rake"
17
17
  gem "rubocop-rspec"
18
18
  gem "simplecov"
19
+ gem "sqlite3"
19
20
  gem "yard"
data/README.md CHANGED
@@ -8,6 +8,54 @@
8
8
 
9
9
  `OmniAI::Tools` is a library of pre-built tools to simplify integrating common tasks with [OmniAI](https://github.com/ksylvest/omniai).
10
10
 
11
+ ## Database
12
+
13
+ Database tools are focused on running SQL statements:
14
+
15
+ ```ruby
16
+ require "omniai/openai"
17
+ require "omniai/tools"
18
+
19
+ require "sqlite3"
20
+
21
+ db = SQLite3::Database.new(":memory:")
22
+
23
+ client = OmniAI::OpenAI::Client.new
24
+ logger = Logger.new($stdout)
25
+
26
+ tools = [
27
+ OmniAI::Tools::Database::SqliteTool.new(logger:, db:),
28
+ ]
29
+
30
+ puts "Type 'exit' or 'quit' to leave."
31
+
32
+ prompt = OmniAI::Chat::Prompt.build do |builder|
33
+ builder.system "Use tools to manage a database as requested."
34
+ end
35
+
36
+ loop do
37
+ print "# "
38
+ text = gets.strip
39
+ break if %w[exit quit].include?(text)
40
+
41
+ prompt.user(text)
42
+ response = client.chat(prompt, stream: $stdout, tools:)
43
+ prompt.assistant(response.text)
44
+ end
45
+ ```
46
+
47
+ ```
48
+ Type 'exit' or 'quit' to leave.
49
+ # Generate tables for artists, albums, songs, bands, members and populate it with data surrounding the Beatles.
50
+
51
+ CREATE TABLE artists (id INTEGER PRIMARY KEY, name TEXT NOT NULL)
52
+ CREATE TABLE bands (id INTEGER PRIMARY KEY, name TEXT NOT NULL)
53
+ CREATE TABLE members (id INTEGER PRIMARY KEY, artist_id INTEGER, band_id INTEGER, FOREIGN KEY(artist_id) REFERENCES artists(id), FOREIGNKEY(band_id) REFERENCES bands(id))"
54
+ CREATE TABLE albums (id INTEGER PRIMARY KEY, title TEXT NOT NULL, band_id INTEGER, FOREIGN KEY(band_id) REFERENCES bands(id))
55
+ CREATE TABLE songs (id INTEGER PRIMARY KEY, title TEXT NOT NULL, album_id INTEGER, FOREIGN KEY(album_id) REFERENCES albums(id))
56
+ ...
57
+ ```
58
+
11
59
  ## Disk
12
60
 
13
61
  Disk tools are focused on creating, updating, and deleting files and directories within a "root":
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+
5
+ module OmniAI
6
+ module Tools
7
+ module Database
8
+ # @example
9
+ # tool = OmniAI::Tools::Database::SqliteTool.new
10
+ # tool.execute(path: "./foo/bar")
11
+ class BaseTool < OmniAI::Tool
12
+ # @param logger [IO] An optional logger for debugging executed commands.
13
+ def initialize(logger: Logger.new(IO::NULL))
14
+ super()
15
+ @logger = logger
16
+ end
17
+
18
+ # @example
19
+ # tool = OmniAI::Tools::Database::BaseTool.new
20
+ # tool.execute(statements: ["SELECT * FROM people"])
21
+ #
22
+ # @param statements [Array<String>]
23
+ #
24
+ # @return [Array<Hash>]
25
+ def execute(statements:)
26
+ [].tap do |executions|
27
+ statements.map do |statement|
28
+ execution = perform(statement:)
29
+ executions << execution
30
+ break unless execution[:status].eql?(:ok)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+
5
+ module OmniAI
6
+ module Tools
7
+ module Database
8
+ # @example
9
+ # tool = OmniAI::Tools::Database::SqliteTool.new
10
+ # tool.execute(path: "./foo/bar")
11
+ class SqliteTool < BaseTool
12
+ description <<~TEXT
13
+ Executes SQL commands (INSERT / UPDATE / SELECT / etc) on a database.
14
+
15
+ Example:
16
+
17
+ STATEMENTS:
18
+
19
+ [
20
+ 'CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL)',
21
+ 'INSERT INTO people (name) VALUES ('John')',
22
+ 'INSERT INTO people (name) VALUES ('Paul')',
23
+ 'SELECT * FROM people',
24
+ 'DROP TABLE people'
25
+ ]
26
+
27
+ RESULT:
28
+
29
+ [
30
+ {
31
+ "status": "OK",
32
+ "statement": "CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
33
+ "result": "..."
34
+ },
35
+ {
36
+ "status": "OK",
37
+ "statement": "INSERT INTO people (name) VALUES ('John')"
38
+ "result": "..."
39
+ },
40
+ {
41
+ "status": "OK",
42
+ "statement": "INSERT INTO people (name) VALUES ('Paul')",
43
+ "result": "..."
44
+ },
45
+ {
46
+ "status": "OK",
47
+ "statement": "SELECT * FROM people",
48
+ "result": "..."
49
+ },
50
+ {
51
+ "status": "OK",
52
+ "statement": "DROP TABLE people",
53
+ "result": "..."
54
+ }
55
+ ]
56
+ TEXT
57
+
58
+ parameter(
59
+ :statements,
60
+ :array,
61
+ description: "A list of SQL statements to run sequentially.",
62
+ items: OmniAI::Schema.string(description: 'A SQL statement to run (e.g. "SELECT * FROM ...").')
63
+ )
64
+
65
+ required %i[statements]
66
+
67
+ # @param logger [IO] An optional logger for debugging executed commands.
68
+ # @param db [SQLite3::Database] A sqlite database.
69
+ def initialize(db:, logger: Logger.new(IO::NULL))
70
+ super(logger:)
71
+ @db = db
72
+ end
73
+
74
+ # @example
75
+ # tool = OmniAI::Tools::Database::BaseTool.new
76
+ # tool.execute(statements: ["SELECT * FROM people"])
77
+ #
78
+ # @param statements [Array<String>]
79
+ #
80
+ # @return [Array<Hash>]
81
+ def execute(statements:)
82
+ [].tap do |executions|
83
+ statements.map do |statement|
84
+ execution = perform(statement:)
85
+ executions << execution
86
+ break unless execution[:status].eql?(:ok)
87
+ end
88
+ end
89
+ end
90
+
91
+ protected
92
+
93
+ # @param statement [String]
94
+ #
95
+ # @return [Hash]
96
+ def perform(statement:)
97
+ @logger.info(%(#{self.class.name}#perform statement=#{statement.inspect}))
98
+
99
+ result = @db.execute2(statement)
100
+
101
+ { status: :ok, statement:, result: }
102
+ rescue ::SQLite3::Exception => e
103
+ @logger.warn("ERROR: #{e.message}")
104
+
105
+ { status: :error, statement:, result: e.message }
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAI
4
+ module Tools
5
+ module Disk
6
+ # @example
7
+ # tool = OmniAI::Tools::Disk::DirectoryCreateTool.new(root: "./project")
8
+ # tool.execute(path: "./foo/bar")
9
+ class DirectoryCreateTool < BaseTool
10
+ description "Creates a directory."
11
+
12
+ parameter :old_path, :string, description: "a path (e.g. `./old`)"
13
+ parameter :new_path, :string, description: "a path (e.g. `./new`)"
14
+
15
+ required %i[old_path new_path]
16
+
17
+ # @param old_path [String]
18
+ # @param new_path [String]
19
+ #
20
+ # @return [String]
21
+ def execute(old_path:, new_path:)
22
+ @logger.info("#{self.class.name}#execute path=#{path.inspect}")
23
+
24
+ FileUtils.mv(old_path, new_path)
25
+ rescue SecurityError => e
26
+ @logger.error(e.message)
27
+ raise e
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -14,7 +14,7 @@ module OmniAI
14
14
  parameter :service, :string, description: "The service to run the command on (e.g. `app`)."
15
15
  parameter :command, :string, description: "The command to run (e.g. `rspec`)."
16
16
  parameter :args, :array, description: "The arguments for the command.",
17
- items: OmniAI::Tool::Property.string(description: "An argument for the command (e.g. `spec/main_spec.rb`).")
17
+ items: OmniAI::Schema.string(description: "An argument for the command (e.g. `spec/main_spec.rb`).")
18
18
 
19
19
  # @param service [String]
20
20
  # @param command [String]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module OmniAI
4
4
  module Tools
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-04-23 00:00:00.000000000 Z
10
+ date: 2025-05-14 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: omniai
@@ -49,9 +49,12 @@ files:
49
49
  - bin/console
50
50
  - bin/setup
51
51
  - lib/omniai/tools.rb
52
+ - lib/omniai/tools/database/base_tool.rb
53
+ - lib/omniai/tools/database/sqlite_tool.rb
52
54
  - lib/omniai/tools/disk/base_tool.rb
53
55
  - lib/omniai/tools/disk/directory_create_tool.rb
54
56
  - lib/omniai/tools/disk/directory_delete_tool.rb
57
+ - lib/omniai/tools/disk/directory_move_tool.rb
55
58
  - lib/omniai/tools/disk/file_create_tool.rb
56
59
  - lib/omniai/tools/disk/file_delete_tool.rb
57
60
  - lib/omniai/tools/disk/file_move_tool.rb