zephira 0.1.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 +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +36 -0
- data/Dockerfile +39 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +145 -0
- data/README.md +230 -0
- data/Rakefile +12 -0
- data/exe/zephira +6 -0
- data/lib/zephira/agent/status.rb +20 -0
- data/lib/zephira/agent.rb +312 -0
- data/lib/zephira/backends/open_ai_compatible.rb +74 -0
- data/lib/zephira/backends.rb +17 -0
- data/lib/zephira/cli.rb +41 -0
- data/lib/zephira/commands/about.rb +27 -0
- data/lib/zephira/commands/bye.rb +22 -0
- data/lib/zephira/commands/clear.rb +32 -0
- data/lib/zephira/commands/compact.rb +22 -0
- data/lib/zephira/commands/help.rb +22 -0
- data/lib/zephira/commands/history.rb +25 -0
- data/lib/zephira/commands/model.rb +51 -0
- data/lib/zephira/commands.rb +31 -0
- data/lib/zephira/completions/file_names.rb +22 -0
- data/lib/zephira/completions/slash_commands.rb +17 -0
- data/lib/zephira/completions.rb +22 -0
- data/lib/zephira/config.rb +17 -0
- data/lib/zephira/formatter.rb +68 -0
- data/lib/zephira/history.rb +117 -0
- data/lib/zephira/logger.rb +46 -0
- data/lib/zephira/models/base_model.rb +143 -0
- data/lib/zephira/models/chat_gpt41.rb +15 -0
- data/lib/zephira/models/chat_gpt41_mini.rb +15 -0
- data/lib/zephira/models/claude_35_sonnet.rb +15 -0
- data/lib/zephira/models/gpt_5_4.rb +15 -0
- data/lib/zephira/models/gpt_5_5.rb +15 -0
- data/lib/zephira/models/gpt_o4_mini.rb +15 -0
- data/lib/zephira/models/llama4.rb +15 -0
- data/lib/zephira/models.rb +19 -0
- data/lib/zephira/sandbox.rb +193 -0
- data/lib/zephira/tokens.rb +16 -0
- data/lib/zephira/tools/base_tool.rb +105 -0
- data/lib/zephira/tools/code_search.rb +135 -0
- data/lib/zephira/tools/delete_file.rb +47 -0
- data/lib/zephira/tools/http_request.rb +112 -0
- data/lib/zephira/tools/list_directory.rb +57 -0
- data/lib/zephira/tools/memory_delete.rb +39 -0
- data/lib/zephira/tools/memory_list.rb +37 -0
- data/lib/zephira/tools/memory_read.rb +43 -0
- data/lib/zephira/tools/memory_store.rb +51 -0
- data/lib/zephira/tools/memory_write.rb +39 -0
- data/lib/zephira/tools/read_file.rb +90 -0
- data/lib/zephira/tools/shell.rb +82 -0
- data/lib/zephira/tools/update_file.rb +46 -0
- data/lib/zephira/tools/web_search.rb +113 -0
- data/lib/zephira/tools.rb +70 -0
- data/lib/zephira/version.rb +5 -0
- data/lib/zephira.rb +24 -0
- data/license.txt +21 -0
- data/standard.yml +3 -0
- metadata +243 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class MemoryDelete < BaseTool
|
|
6
|
+
class << self
|
|
7
|
+
def name
|
|
8
|
+
"memory_delete"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def description
|
|
12
|
+
"Delete a named key from persistent memory."
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def parameters
|
|
16
|
+
{
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
key: {type: "string", description: "Memory key to delete"},
|
|
20
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
21
|
+
},
|
|
22
|
+
required: ["key", "intent"]
|
|
23
|
+
}
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def run
|
|
28
|
+
key = validate(arg(:key), arg_path: "key", type: String)
|
|
29
|
+
|
|
30
|
+
unless MemoryStore.delete(key)
|
|
31
|
+
return error_result(message: "Key not found: #{key}")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
agent.status.verbose(" • Memory deleted: '#{key}'")
|
|
35
|
+
success_result("Memory deleted: '#{key}'")
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class MemoryList < BaseTool
|
|
6
|
+
class << self
|
|
7
|
+
def name
|
|
8
|
+
"memory_list"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def description
|
|
12
|
+
"List all stored memory keys."
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def read_only?
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def parameters
|
|
20
|
+
{
|
|
21
|
+
type: "object",
|
|
22
|
+
properties: {
|
|
23
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
24
|
+
},
|
|
25
|
+
required: ["intent"]
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def run
|
|
31
|
+
keys = MemoryStore.keys
|
|
32
|
+
agent.status.verbose(" • Memory list: #{keys.size} keys")
|
|
33
|
+
success_result(keys)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class MemoryRead < BaseTool
|
|
6
|
+
class << self
|
|
7
|
+
def name
|
|
8
|
+
"memory_read"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def description
|
|
12
|
+
"Read a named value from persistent memory."
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def read_only?
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def parameters
|
|
20
|
+
{
|
|
21
|
+
type: "object",
|
|
22
|
+
properties: {
|
|
23
|
+
key: {type: "string", description: "Memory key to read"},
|
|
24
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
25
|
+
},
|
|
26
|
+
required: ["key", "intent"]
|
|
27
|
+
}
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def run
|
|
32
|
+
key = validate(arg(:key), arg_path: "key", type: String)
|
|
33
|
+
|
|
34
|
+
unless MemoryStore.key?(key)
|
|
35
|
+
return error_result(message: "Key not found: #{key}")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
agent.status.verbose(" • Memory read: '#{key}'")
|
|
39
|
+
success_result(MemoryStore.read(key))
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "yaml"
|
|
5
|
+
|
|
6
|
+
module Zephira
|
|
7
|
+
class Tools
|
|
8
|
+
# Shared YAML-backed key/value store used by the memory_* tools. Values are
|
|
9
|
+
# always strings; loaded with safe_load to refuse arbitrary object
|
|
10
|
+
# deserialization since memory is agent/user-supplied content.
|
|
11
|
+
class MemoryStore
|
|
12
|
+
PATH = ".zephira/memory.yml"
|
|
13
|
+
|
|
14
|
+
def self.load
|
|
15
|
+
return {} unless ::File.exist?(PATH)
|
|
16
|
+
YAML.safe_load_file(PATH) || {}
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.save(memory)
|
|
20
|
+
::FileUtils.mkdir_p(::File.dirname(PATH))
|
|
21
|
+
::File.write(PATH, memory.to_yaml)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.read(key)
|
|
25
|
+
load[key]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.write(key, value)
|
|
29
|
+
memory = load
|
|
30
|
+
memory[key] = value
|
|
31
|
+
save(memory)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.delete(key)
|
|
35
|
+
memory = load
|
|
36
|
+
return false unless memory.key?(key)
|
|
37
|
+
memory.delete(key)
|
|
38
|
+
save(memory)
|
|
39
|
+
true
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.key?(key)
|
|
43
|
+
load.key?(key)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.keys
|
|
47
|
+
load.keys
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class MemoryWrite < BaseTool
|
|
6
|
+
class << self
|
|
7
|
+
def name
|
|
8
|
+
"memory_write"
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def description
|
|
12
|
+
"Write a named value to persistent memory."
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def parameters
|
|
16
|
+
{
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
key: {type: "string", description: "Memory key"},
|
|
20
|
+
value: {type: "string", description: "Value to store"},
|
|
21
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
22
|
+
},
|
|
23
|
+
required: ["key", "value", "intent"]
|
|
24
|
+
}
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def run
|
|
29
|
+
key = validate(arg(:key), arg_path: "key", type: String)
|
|
30
|
+
value = validate(arg(:value), arg_path: "value", type: String, allow_empty: true)
|
|
31
|
+
|
|
32
|
+
MemoryStore.write(key, value)
|
|
33
|
+
|
|
34
|
+
agent.status.verbose(" • Memory written: '#{key}'")
|
|
35
|
+
success_result("Memory written: '#{key}'")
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class ReadFile < BaseTool
|
|
6
|
+
DEFAULT_MAX_BYTES = 20 * 1024
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def name
|
|
10
|
+
"read_file"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def description
|
|
14
|
+
"Read the contents of one or more files (up to a size threshold)."
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def read_only?
|
|
18
|
+
true
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def parameters
|
|
22
|
+
{
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: {
|
|
25
|
+
intent: {
|
|
26
|
+
type: "string",
|
|
27
|
+
description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."
|
|
28
|
+
},
|
|
29
|
+
file_paths: {
|
|
30
|
+
type: "array",
|
|
31
|
+
items: {type: "string"},
|
|
32
|
+
description: "Paths to the files to read"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
required: ["file_paths", "intent"]
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def run
|
|
41
|
+
paths = validate(arg(:file_paths), arg_path: "file_paths", type: Array, allow_empty: false)
|
|
42
|
+
|
|
43
|
+
results = paths.map do |file_path|
|
|
44
|
+
if file_path.nil? || file_path.strip.empty?
|
|
45
|
+
agent.status.warn("`file_path` was empty or missing for entry")
|
|
46
|
+
{"path" => file_path, "content" => "", "error" => "`file_path` was empty or missing"}
|
|
47
|
+
else
|
|
48
|
+
expanded_path = ::File.expand_path(file_path)
|
|
49
|
+
begin
|
|
50
|
+
size = ::File.size(expanded_path)
|
|
51
|
+
agent.status.verbose(" • Reading file: '#{file_path}' (max #{DEFAULT_MAX_BYTES} bytes)")
|
|
52
|
+
|
|
53
|
+
data = if size > DEFAULT_MAX_BYTES
|
|
54
|
+
agent.status.verbose(" • File size #{size} bytes exceeds limit of #{DEFAULT_MAX_BYTES} bytes, truncating content")
|
|
55
|
+
::File.open(expanded_path, "rb") { |file| file.read(DEFAULT_MAX_BYTES) }
|
|
56
|
+
else
|
|
57
|
+
::File.binread(expanded_path)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
content = normalize_content(data)
|
|
61
|
+
agent.status.verbose(" • Read file: '#{file_path}'")
|
|
62
|
+
agent.logger.info("Read file: '#{file_path}'")
|
|
63
|
+
{"path" => file_path, "content" => content}
|
|
64
|
+
rescue Errno::ENOENT
|
|
65
|
+
agent.status.warn(" • File not found: '#{file_path}'")
|
|
66
|
+
agent.logger.error("File not found: '#{file_path}'")
|
|
67
|
+
{"path" => file_path, "content" => "", "error" => "No such file or directory: #{file_path}"}
|
|
68
|
+
rescue Errno::EACCES
|
|
69
|
+
agent.status.warn(" • Permission denied: '#{file_path}'")
|
|
70
|
+
agent.logger.error("Permission denied: '#{file_path}'")
|
|
71
|
+
{"path" => file_path, "content" => "", "error" => "Permission denied: #{file_path}"}
|
|
72
|
+
rescue Errno::EISDIR
|
|
73
|
+
agent.status.warn(" • Is a directory: '#{file_path}'")
|
|
74
|
+
agent.logger.error("Is a directory: '#{file_path}'")
|
|
75
|
+
{"path" => file_path, "content" => "", "error" => "Is a directory: #{file_path}"}
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
success_result(results)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def normalize_content(data)
|
|
86
|
+
data.force_encoding(Encoding::UTF_8).scrub("?")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
|
|
5
|
+
module Zephira
|
|
6
|
+
class Tools
|
|
7
|
+
class Shell < BaseTool
|
|
8
|
+
OUTPUT_TRUNCATION_WIDTH = 200
|
|
9
|
+
TRUNCATION_OVERHEAD = 23
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def name
|
|
13
|
+
"shell"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def description
|
|
17
|
+
"Run a shell command"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def parameters
|
|
21
|
+
{
|
|
22
|
+
type: "object",
|
|
23
|
+
properties: {
|
|
24
|
+
command: {type: "string", description: "Command to run"},
|
|
25
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
26
|
+
},
|
|
27
|
+
required: ["command", "intent"]
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def run
|
|
33
|
+
cmd = validate(arg(:command), arg_path: "command", type: String, allow_empty: false)
|
|
34
|
+
|
|
35
|
+
agent.status.verbose(" • Running shell command: '#{cmd}'")
|
|
36
|
+
stdout_str, stderr_str, status_obj = Open3.capture3(cmd, chdir: Dir.pwd)
|
|
37
|
+
|
|
38
|
+
unless stdout_str.to_s.empty?
|
|
39
|
+
message = truncate_string_to_fit(
|
|
40
|
+
prefix: " • Shell command stdout: ",
|
|
41
|
+
text_array: stdout_str.lines,
|
|
42
|
+
max_characters: OUTPUT_TRUNCATION_WIDTH
|
|
43
|
+
)
|
|
44
|
+
agent.status.verbose(message)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
unless stderr_str.to_s.empty?
|
|
48
|
+
message = truncate_string_to_fit(
|
|
49
|
+
prefix: " • \e[91mShell command stderr:\e[0m ",
|
|
50
|
+
text_array: stderr_str.lines,
|
|
51
|
+
max_characters: OUTPUT_TRUNCATION_WIDTH
|
|
52
|
+
)
|
|
53
|
+
agent.status.verbose(message)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
agent.status.verbose(" • Shell command completed with exit status: #{status_obj.exitstatus}")
|
|
57
|
+
success_result(status: status_obj.exitstatus, stdout: stdout_str, stderr: stderr_str)
|
|
58
|
+
rescue Errno::ENOENT
|
|
59
|
+
error_result(message: "Command not found: #{arg(:command)}")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def truncate_string_to_fit(prefix:, text_array:, max_characters:)
|
|
65
|
+
postfix = " ... (~#{text_array.size - 1} more lines)"
|
|
66
|
+
overhead = prefix.length + postfix.length + TRUNCATION_OVERHEAD
|
|
67
|
+
available_length = max_characters - overhead
|
|
68
|
+
|
|
69
|
+
sanitized = text_array.join
|
|
70
|
+
.gsub(/\e\[[\d;?]*[@-~]/, "")
|
|
71
|
+
.delete("\e")
|
|
72
|
+
.gsub(/\r?\n/, " ")
|
|
73
|
+
|
|
74
|
+
full = prefix + sanitized
|
|
75
|
+
return full if full.length <= max_characters
|
|
76
|
+
|
|
77
|
+
truncated = sanitized[0, available_length]
|
|
78
|
+
prefix + truncated + postfix
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
|
|
5
|
+
module Zephira
|
|
6
|
+
class Tools
|
|
7
|
+
class UpdateFile < BaseTool
|
|
8
|
+
class << self
|
|
9
|
+
def name
|
|
10
|
+
"update_file"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def description
|
|
14
|
+
"Update or create a file by providing the full replacement content."
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def parameters
|
|
18
|
+
{
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: {
|
|
21
|
+
content: {type: "string", description: "Full replacement file text"},
|
|
22
|
+
file_path: {type: "string", description: "Path to the file to be updated or created"},
|
|
23
|
+
intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
|
|
24
|
+
},
|
|
25
|
+
required: ["content", "file_path", "intent"]
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def run
|
|
31
|
+
content = validate(arg(:content), arg_path: "content", type: String, allow_empty: true)
|
|
32
|
+
file_path = validate(arg(:file_path), arg_path: "file_path", type: String)
|
|
33
|
+
|
|
34
|
+
agent.status.verbose(" • Updating file: '#{file_path}'")
|
|
35
|
+
|
|
36
|
+
::FileUtils.mkdir_p(::File.dirname(file_path))
|
|
37
|
+
::File.write(file_path, content)
|
|
38
|
+
|
|
39
|
+
msg = "Updated file: '#{file_path}'"
|
|
40
|
+
agent.status.verbose(" • #{msg}")
|
|
41
|
+
agent.logger.info(msg)
|
|
42
|
+
success_result(msg)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "json"
|
|
6
|
+
|
|
7
|
+
module Zephira
|
|
8
|
+
class Tools
|
|
9
|
+
class WebSearch < BaseTool
|
|
10
|
+
class << self
|
|
11
|
+
def name
|
|
12
|
+
"web_search"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def description
|
|
16
|
+
"Performs a web search using the Brave Search API."
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def read_only?
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def parameters
|
|
24
|
+
{
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
intent: {
|
|
28
|
+
type: "string",
|
|
29
|
+
description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."
|
|
30
|
+
},
|
|
31
|
+
queries: {
|
|
32
|
+
type: "array",
|
|
33
|
+
description: "An array of search query instructions",
|
|
34
|
+
items: {
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
query: {
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "The string to search for"
|
|
40
|
+
},
|
|
41
|
+
num_results: {
|
|
42
|
+
type: "integer",
|
|
43
|
+
description: "Maximum number of results to return (1-50)",
|
|
44
|
+
minimum: 1,
|
|
45
|
+
maximum: 50
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
required: ["query", "num_results"],
|
|
49
|
+
additionalProperties: false
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
required: ["intent", "queries"]
|
|
54
|
+
}
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def run
|
|
59
|
+
queries = validate(arg(:queries), arg_path: "queries", type: Array, allow_empty: false)
|
|
60
|
+
|
|
61
|
+
api_key = Config.read("ZEPHIRA_BRAVE_SEARCH_API_KEY").to_s
|
|
62
|
+
if api_key.strip.empty?
|
|
63
|
+
return error_result(message: "ZEPHIRA_BRAVE_SEARCH_API_KEY not set (env var or .zephira.yml)")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
results = queries.map { |query| run_query(query, api_key) }
|
|
67
|
+
success_result(results)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def run_query(query_args, api_key)
|
|
73
|
+
query = query_args["query"] || query_args[:query]
|
|
74
|
+
num_results = query_args["num_results"] || query_args[:num_results]
|
|
75
|
+
|
|
76
|
+
unless query.is_a?(String) && !query.strip.empty?
|
|
77
|
+
return error_result(message: "`query` must be a non-empty string")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
if num_results && (!num_results.is_a?(Integer) || !(1..50).include?(num_results))
|
|
81
|
+
return error_result(message: "`num_results` must be an integer between 1 and 50")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
agent.update_status(" Web search: '#{query}'")
|
|
85
|
+
|
|
86
|
+
uri = URI("https://api.search.brave.com/res/v1/web/search")
|
|
87
|
+
params = {"q" => query}
|
|
88
|
+
params["count"] = num_results if num_results
|
|
89
|
+
uri.query = URI.encode_www_form(params)
|
|
90
|
+
|
|
91
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
92
|
+
http.use_ssl = true
|
|
93
|
+
response = http.get(uri.request_uri, {"Accept" => "application/json", "X-Subscription-Token" => api_key})
|
|
94
|
+
|
|
95
|
+
if response.code.to_i >= 300
|
|
96
|
+
agent.status.warn(" • ERROR: '#{query}' search failed (#{response.code})")
|
|
97
|
+
agent.logger.error("Search failed: '#{query}' (#{response.code})")
|
|
98
|
+
return error_result(message: "Search failed with status #{response.code}")
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
begin
|
|
102
|
+
data = JSON.parse(response.body)
|
|
103
|
+
agent.status.verbose(" • Search complete: '#{query}'")
|
|
104
|
+
agent.logger.info("Search complete: '#{query}'")
|
|
105
|
+
success_result(data)
|
|
106
|
+
rescue JSON::ParserError => error
|
|
107
|
+
agent.status.warn(" • ERROR: Invalid JSON for '#{query}'")
|
|
108
|
+
error_result(message: "Invalid JSON: #{error.message}")
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Zephira
|
|
4
|
+
class Tools
|
|
5
|
+
class ToolNotFoundError < StandardError; end
|
|
6
|
+
class ToolExecutionError < StandardError; end
|
|
7
|
+
class ToolResultError < StandardError; end
|
|
8
|
+
|
|
9
|
+
def self.load(paths:)
|
|
10
|
+
paths.each do |path|
|
|
11
|
+
Dir.glob(File.join(path, "**", "*.rb")).each do |file|
|
|
12
|
+
require File.expand_path(file)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def constants
|
|
19
|
+
@constants ||= ::Zephira::Tools.constants(false)
|
|
20
|
+
.map { |const_sym| ::Zephira::Tools.const_get(const_sym) }
|
|
21
|
+
.select { |const| const.is_a?(Class) && const < ::Zephira::Tools::BaseTool }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def to_h
|
|
25
|
+
constants.map do |tool|
|
|
26
|
+
{
|
|
27
|
+
name: tool.name,
|
|
28
|
+
description: tool.description,
|
|
29
|
+
parameters: tool.parameters
|
|
30
|
+
}
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def run(name:, args:, agent:)
|
|
35
|
+
tool = find!(name)
|
|
36
|
+
|
|
37
|
+
result = begin
|
|
38
|
+
tool.run(args: args, agent: agent)
|
|
39
|
+
rescue => exception
|
|
40
|
+
raise ToolExecutionError, "Encountered an error when executing tool #{name}: #{exception.message}"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
validate_result(result)
|
|
44
|
+
result
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def read_only?(name)
|
|
48
|
+
tool = constants.find { |candidate| candidate.name == name }
|
|
49
|
+
tool && tool.respond_to?(:read_only?) && tool.read_only?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def find!(name)
|
|
53
|
+
tool = constants.find { |candidate| candidate.name == name }
|
|
54
|
+
raise ToolNotFoundError, "Tool not found: #{name}" if tool.nil?
|
|
55
|
+
tool
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def validate_result(result)
|
|
61
|
+
unless result.is_a?(Hash) && result.key?(:outcome) && result.key?(:error) && result.key?(:data)
|
|
62
|
+
raise ToolResultError, "Tool result must be a hash with keys :outcome, :error, and :data. Got: #{result.inspect}"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
unless %w[success error].include?(result[:outcome])
|
|
66
|
+
raise ToolResultError, "Tool result :outcome must be 'success' or 'error'. Got: #{result[:outcome].inspect}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
data/lib/zephira.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "zephira/version"
|
|
4
|
+
require_relative "zephira/config"
|
|
5
|
+
require_relative "zephira/sandbox"
|
|
6
|
+
require_relative "zephira/formatter"
|
|
7
|
+
require_relative "zephira/tokens"
|
|
8
|
+
require_relative "zephira/logger"
|
|
9
|
+
require_relative "zephira/backends"
|
|
10
|
+
require_relative "zephira/models"
|
|
11
|
+
require_relative "zephira/tools"
|
|
12
|
+
require_relative "zephira/tools/base_tool"
|
|
13
|
+
Dir[File.join(__dir__, "zephira/tools/*.rb")].each { |file| require file }
|
|
14
|
+
require_relative "zephira/history"
|
|
15
|
+
require_relative "zephira/commands"
|
|
16
|
+
Dir[File.join(__dir__, "zephira/commands/*.rb")].each { |file| require file }
|
|
17
|
+
require_relative "zephira/completions"
|
|
18
|
+
Dir[File.join(__dir__, "zephira/completions/*.rb")].each { |file| require file }
|
|
19
|
+
require_relative "zephira/agent"
|
|
20
|
+
require_relative "zephira/agent/status"
|
|
21
|
+
require_relative "zephira/cli"
|
|
22
|
+
|
|
23
|
+
module Zephira
|
|
24
|
+
end
|
data/license.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Aaron Gough
|
|
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/standard.yml
ADDED