summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--logic/cartridge/parser.rb20
-rw-r--r--logic/helpers/hash.rb10
-rw-r--r--spec/data/cartridges/meta.md25
-rw-r--r--spec/logic/cartridge/parser_spec.rb24
-rw-r--r--spec/logic/helpers/hash_spec.rb9
5 files changed, 81 insertions, 7 deletions
diff --git a/logic/cartridge/parser.rb b/logic/cartridge/parser.rb
index f82b968..50b3dc5 100644
--- a/logic/cartridge/parser.rb
+++ b/logic/cartridge/parser.rb
@@ -44,9 +44,9 @@ module NanoBot
parsed.delete(:tools)
unless parsed.empty?
- yaml_source << YAML.dump(Logic::Helpers::Hash.stringify_keys(
- parsed
- )).gsub(/^---/, '') # TODO: Is this safe enough?
+ yaml_source << YAML.dump(
+ Logic::Helpers::Hash.stringify_keys(parsed)
+ ).gsub(/^---/, '') # TODO: Is this safe enough?
end
else
yaml_source << block[:source]
@@ -60,12 +60,18 @@ module NanoBot
end
unless tools.empty?
- yaml_source << YAML.dump(Logic::Helpers::Hash.stringify_keys(
- { tools: }
- )).gsub(/^---/, '') # TODO: Is this safe enough?
+ yaml_source << YAML.dump(
+ Logic::Helpers::Hash.stringify_keys({ tools: })
+ ).gsub(/^---/, '') # TODO: Is this safe enough?
end
- yaml(yaml_source.join("\n"))
+ cartridge = {}
+
+ yaml_source.each do |source|
+ cartridge = Logic::Helpers::Hash.deep_merge(cartridge, yaml(source))
+ end
+
+ cartridge
end
def self.yaml(raw)
diff --git a/logic/helpers/hash.rb b/logic/helpers/hash.rb
index 4cb44ac..66b6742 100644
--- a/logic/helpers/hash.rb
+++ b/logic/helpers/hash.rb
@@ -4,6 +4,16 @@ module NanoBot
module Logic
module Helpers
module Hash
+ def self.deep_merge(hash1, hash2)
+ hash1.merge(hash2) do |_key, old_val, new_val|
+ if old_val.is_a?(::Hash) && new_val.is_a?(::Hash)
+ deep_merge(old_val, new_val)
+ else
+ new_val
+ end
+ end
+ end
+
def self.symbolize_keys(object)
case object
when ::Hash
diff --git a/spec/data/cartridges/meta.md b/spec/data/cartridges/meta.md
new file mode 100644
index 0000000..68a0cbd
--- /dev/null
+++ b/spec/data/cartridges/meta.md
@@ -0,0 +1,25 @@
+Start by defining a meta section:
+
+```yaml
+meta:
+ symbol: 🤖
+ name: Nano Bot Name
+ author: Your Name
+ description: A helpful assistant.
+```
+
+You can also add version and license information:
+
+```yaml
+meta:
+ version: 1.0.0
+ license: CC0-1.0
+```
+
+Then, add a behavior section:
+
+```yaml
+behaviors:
+ interaction:
+ directive: You are a helpful assistant.
+```
diff --git a/spec/logic/cartridge/parser_spec.rb b/spec/logic/cartridge/parser_spec.rb
index e8bac0b..8297baa 100644
--- a/spec/logic/cartridge/parser_spec.rb
+++ b/spec/logic/cartridge/parser_spec.rb
@@ -30,6 +30,30 @@ RSpec.describe NanoBot::Logic::Cartridge::Parser do
end
end
+ context 'meta' do
+ let(:raw) { File.read('spec/data/cartridges/meta.md') }
+
+ it 'parses markdown cartridge' do
+ expect(described_class.parse(raw, format: 'md')).to eq(
+ {
+ meta: {
+ symbol: '🤖',
+ name: 'Nano Bot Name',
+ author: 'Your Name',
+ description: 'A helpful assistant.',
+ version: '1.0.0',
+ license: 'CC0-1.0'
+ },
+ behaviors: {
+ interaction: {
+ directive: 'You are a helpful assistant.'
+ }
+ }
+ }
+ )
+ end
+ end
+
context 'tools' do
let(:raw) { File.read('spec/data/cartridges/tools.md') }
diff --git a/spec/logic/helpers/hash_spec.rb b/spec/logic/helpers/hash_spec.rb
index 7c8ff58..0da92fb 100644
--- a/spec/logic/helpers/hash_spec.rb
+++ b/spec/logic/helpers/hash_spec.rb
@@ -9,6 +9,15 @@ RSpec.describe NanoBot::Logic::Helpers::Hash do
)
end
+ it 'deep merges' do
+ expect(described_class.deep_merge(
+ { a: { x: 1, y: 2 }, b: 3 },
+ { a: { y: 99, z: 4 }, c: 5 }
+ )).to eq(
+ { a: { x: 1, y: 99, z: 4 }, b: 3, c: 5 }
+ )
+ end
+
it 'stringify keys' do
expect(described_class.stringify_keys({ a: 'b', c: { d: [:e] } })).to eq(
{ 'a' => 'b', 'c' => { 'd' => [:e] } }