Skip to content

Commit 575727f

Browse files
committed
add CoreMemoryFunction,memory and test case
1 parent 969cf9d commit 575727f

File tree

3 files changed

+167
-78
lines changed

3 files changed

+167
-78
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe OpenAISwarm::Memories::CoreMemoryFunction do
4+
describe '.definition' do
5+
# Test with empty memory fields
6+
context 'when no memory fields are provided' do
7+
it 'returns a function definition with empty properties' do
8+
result = described_class.definition([])
9+
10+
expect(result).to be_a(Hash)
11+
expect(result[:type]).to eq('function')
12+
expect(result[:function][:name]).to eq('core_memory_save')
13+
expect(result[:function][:parameters][:properties]).to be_empty
14+
end
15+
end
16+
17+
# Test with memory fields
18+
context 'when memory fields are provided' do
19+
let(:memory_field) do
20+
field = OpenAISwarm::Memories::Field.new('name')
21+
field.tool_call_description = ' This is used to store the name.'
22+
field
23+
end
24+
25+
let(:memory_fields) { [memory_field] }
26+
27+
it 'returns a function definition with correct properties' do
28+
result = described_class.definition(memory_fields)
29+
30+
expect(result).to be_a(Hash)
31+
expect(result[:type]).to eq('function')
32+
expect(result[:function]).to include(
33+
name: 'core_memory_save',
34+
description: 'Save important information about you, the agent or the human you are chatting with.'
35+
)
36+
37+
# Check properties structure
38+
properties = result[:function][:parameters][:properties]
39+
expect(properties).to include('name')
40+
expect(properties['name']).to include(
41+
type: 'string',
42+
description: 'The name to remember. This is used to store the name.'
43+
)
44+
end
45+
end
46+
47+
# Test with multiple memory fields
48+
context 'when multiple memory fields are provided' do
49+
let(:memory_fields) do
50+
[
51+
OpenAISwarm::Memories::Field.new('name'),
52+
OpenAISwarm::Memories::Field.new('age')
53+
]
54+
end
55+
56+
it 'includes all fields in the properties' do
57+
result = described_class.definition(memory_fields)
58+
properties = result[:function][:parameters][:properties]
59+
60+
expect(properties.keys).to contain_exactly('name', 'age')
61+
expect(properties['name'][:type]).to eq('string')
62+
expect(properties['age'][:type]).to eq('string')
63+
end
64+
end
65+
end
66+
end

spec/lib/ruby-openai-swarm/memories/field_spec.rb

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,6 @@
11
require 'spec_helper'
22

33
RSpec.describe OpenAISwarm::Memories::Field do
4-
let(:field_name) { :test_field }
5-
let(:field_value) { "test value" }
6-
let(:field) { described_class.new(field_name, field_value) }
7-
8-
describe '#initialize' do
9-
it 'creates a field with name and value' do
10-
expect(field.name).to eq(field_name)
11-
expect(field.value).to eq(field_value)
12-
end
13-
14-
it 'accepts different types of values' do
15-
number_field = described_class.new(:number, 42)
16-
expect(number_field.value).to eq(42)
17-
18-
array_field = described_class.new(:array, [1, 2, 3])
19-
expect(array_field.value).to eq([1, 2, 3])
20-
21-
hash_field = described_class.new(:hash, { key: 'value' })
22-
expect(hash_field.value).to eq({ key: 'value' })
23-
end
24-
end
25-
26-
describe '#to_h' do
27-
it 'returns a hash representation of the field' do
28-
expect(field.to_h).to eq({
29-
name: field_name,
30-
value: field_value
31-
})
32-
end
33-
end
34-
35-
describe '#==' do
36-
it 'returns true when comparing identical fields' do
37-
field2 = described_class.new(field_name, field_value)
38-
expect(field).to eq(field2)
39-
end
40-
41-
it 'returns false when comparing fields with different names' do
42-
field2 = described_class.new(:other_field, field_value)
43-
expect(field).not_to eq(field2)
44-
end
45-
46-
it 'returns false when comparing fields with different values' do
47-
field2 = described_class.new(field_name, "other value")
48-
expect(field).not_to eq(field2)
49-
end
50-
end
51-
52-
describe '#clone' do
53-
it 'creates a deep copy of the field' do
54-
cloned_field = field.clone
55-
expect(cloned_field).to eq(field)
56-
expect(cloned_field.object_id).not_to eq(field.object_id)
57-
end
58-
59-
it 'creates independent copies of complex values' do
60-
array_field = described_class.new(:array, [1, 2, 3])
61-
cloned_field = array_field.clone
62-
cloned_field.value << 4
63-
expect(array_field.value).to eq([1, 2, 3])
64-
expect(cloned_field.value).to eq([1, 2, 3, 4])
65-
end
66-
end
67-
68-
describe 'validation' do
69-
it 'raises error when name is nil' do
70-
expect {
71-
described_class.new(nil, "value")
72-
}.to raise_error(ArgumentError, "Field name cannot be nil")
73-
end
74-
75-
it 'raises error when name is empty' do
76-
expect {
77-
described_class.new("", "value")
78-
}.to raise_error(ArgumentError, "Field name cannot be empty")
79-
end
80-
end
81-
824
# Test initialization with a string
835
describe '#initialize with string' do
846
it 'sets the field value when initialized with a string' do
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe OpenAISwarm::Memory do
4+
# Test initialization and memory field normalization
5+
describe '#initialize' do
6+
context 'when initialized with memory fields' do
7+
let(:memory_fields) { ['name', 'age'] }
8+
let(:memory) { described_class.new(memory_fields: memory_fields) }
9+
10+
it 'creates normalized memory fields' do
11+
expect(memory.instance_variable_get(:@memory_fields).size).to eq(2)
12+
expect(memory.instance_variable_get(:@memory_fields).first).to be_a(OpenAISwarm::Memories::Field)
13+
end
14+
end
15+
16+
context 'when initialized without memory fields' do
17+
let(:memory) { described_class.new }
18+
19+
it 'creates empty memory fields array' do
20+
expect(memory.instance_variable_get(:@memory_fields)).to be_empty
21+
end
22+
end
23+
end
24+
25+
# Test core memory save functionality
26+
describe '#core_memory_save' do
27+
let(:memory) { described_class.new(memory_fields: ['name']) }
28+
let(:entities) { [{ 'name' => 'John Doe' }] }
29+
30+
it 'delegates entity addition to entity store' do
31+
expect(memory.entity_store).to receive(:add_entities).with(entities)
32+
memory.core_memory_save(entities)
33+
end
34+
end
35+
36+
# Test prompt content generation
37+
describe '#prompt_content' do
38+
context 'when memories exist' do
39+
let(:memory_fields) { ['name', 'age'] }
40+
let(:memory) { described_class.new(memory_fields: memory_fields) }
41+
let(:memories_data) { "John Doe, 30" }
42+
43+
before do
44+
allow(memory).to receive(:get_memories_data).and_return(memories_data)
45+
end
46+
47+
it 'returns formatted prompt content with memories' do
48+
expected_content = "You have a section of your context called [MEMORY] " \
49+
"that contains the following information: name, age. " \
50+
"Here are the relevant details: [MEMORY]\n" \
51+
"John Doe, 30"
52+
expect(memory.prompt_content).to eq(expected_content)
53+
end
54+
end
55+
56+
context 'when no memories exist' do
57+
let(:memory) { described_class.new }
58+
59+
before do
60+
allow(memory).to receive(:get_memories_data).and_return(nil)
61+
end
62+
63+
it 'returns nil' do
64+
expect(memory.prompt_content).to be_nil
65+
end
66+
end
67+
end
68+
69+
# Test function generation
70+
describe '#function' do
71+
context 'when memory fields exist' do
72+
let(:memory) { described_class.new(memory_fields: ['name']) }
73+
74+
it 'returns a FunctionDescriptor instance' do
75+
expect(memory.function).to be_a(OpenAISwarm::FunctionDescriptor)
76+
end
77+
78+
it 'has correct target method' do
79+
expect(memory.function.target_method).to eq(memory.method(:core_memory_save))
80+
end
81+
end
82+
83+
context 'when no memory fields exist' do
84+
let(:memory) { described_class.new }
85+
86+
it 'returns nil' do
87+
expect(memory.function).to be_nil
88+
end
89+
end
90+
end
91+
92+
# Test memories data retrieval
93+
describe '#get_memories_data' do
94+
let(:memory) { described_class.new }
95+
96+
it 'delegates to entity store' do
97+
expect(memory.entity_store).to receive(:memories)
98+
memory.get_memories_data
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)