|
6 | 6 | end |
7 | 7 |
|
8 | 8 | describe "#to_hash" do |
9 | | - it "returns function" do |
10 | | - message = described_class.new(role: "tool_result", content: "4.0", tool_call_id: "toolu_014eSx9oBA5DMe8gZqaqcJ3H") |
11 | | - expect(message.to_hash).to eq( |
12 | | - { |
13 | | - role: "user", |
| 9 | + context "when role is assistant" do |
| 10 | + let(:role) { "assistant" } |
| 11 | + |
| 12 | + it "returns assistant_hash" do |
| 13 | + message = described_class.new(role: role, content: "Hello, how can I help you?") |
| 14 | + expect(message).to receive(:assistant_hash).and_call_original |
| 15 | + expect(message.to_hash).to eq( |
| 16 | + role: role, |
| 17 | + content: "Hello, how can I help you?" |
| 18 | + ) |
| 19 | + end |
| 20 | + |
| 21 | + it "returns assistant_hash with tool_calls" do |
| 22 | + message = described_class.new( |
| 23 | + role: role, |
| 24 | + tool_calls: [ |
| 25 | + { |
| 26 | + "type" => "tool_use", |
| 27 | + "id" => "toolu_01UEciZACvRZ6S4rqAwD1syH", |
| 28 | + "name" => "news_retriever__get_everything", |
| 29 | + "input" => { |
| 30 | + "q" => "Google I/O 2024", |
| 31 | + "sort_by" => "publishedAt", |
| 32 | + "language" => "en" |
| 33 | + } |
| 34 | + } |
| 35 | + ] |
| 36 | + ) |
| 37 | + expect(message.to_hash).to eq( |
| 38 | + role: role, |
14 | 39 | content: [ |
15 | 40 | { |
16 | | - type: "tool_result", |
17 | | - tool_use_id: "toolu_014eSx9oBA5DMe8gZqaqcJ3H", |
18 | | - content: "4.0" |
| 41 | + "type" => "tool_use", |
| 42 | + "id" => "toolu_01UEciZACvRZ6S4rqAwD1syH", |
| 43 | + "name" => "news_retriever__get_everything", |
| 44 | + "input" => { |
| 45 | + "q" => "Google I/O 2024", |
| 46 | + "sort_by" => "publishedAt", |
| 47 | + "language" => "en" |
| 48 | + } |
19 | 49 | } |
20 | 50 | ] |
21 | | - } |
22 | | - ) |
| 51 | + ) |
| 52 | + end |
23 | 53 | end |
24 | 54 |
|
25 | | - it "returns tool_calls" do |
26 | | - message = described_class.new( |
27 | | - role: "assistant", |
28 | | - tool_calls: [ |
| 55 | + context "when role is tool_result" do |
| 56 | + let(:message) { described_class.new(role: "tool_result", content: "4.0", tool_call_id: "toolu_014eSx9oBA5DMe8gZqaqcJ3H") } |
| 57 | + |
| 58 | + it "returns tool_hash" do |
| 59 | + expect(message).to receive(:tool_hash).and_call_original |
| 60 | + expect(message.to_hash).to eq( |
29 | 61 | { |
30 | | - "type" => "tool_use", |
31 | | - "id" => "toolu_01UEciZACvRZ6S4rqAwD1syH", |
32 | | - "name" => "news_retriever__get_everything", |
33 | | - "input" => { |
34 | | - "q" => "Google I/O 2024", |
35 | | - "sort_by" => "publishedAt", |
36 | | - "language" => "en" |
37 | | - } |
| 62 | + role: "user", |
| 63 | + content: [ |
| 64 | + { |
| 65 | + type: "tool_result", |
| 66 | + tool_use_id: "toolu_014eSx9oBA5DMe8gZqaqcJ3H", |
| 67 | + content: "4.0" |
| 68 | + } |
| 69 | + ] |
38 | 70 | } |
39 | | - ] |
40 | | - ) |
41 | | - expect(message.to_hash).to eq( |
42 | | - role: "assistant", |
43 | | - content: [ |
44 | | - { |
45 | | - "type" => "tool_use", |
46 | | - "id" => "toolu_01UEciZACvRZ6S4rqAwD1syH", |
47 | | - "name" => "news_retriever__get_everything", |
48 | | - "input" => { |
49 | | - "q" => "Google I/O 2024", |
50 | | - "sort_by" => "publishedAt", |
51 | | - "language" => "en" |
| 71 | + ) |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + context "when role is user" do |
| 76 | + let(:role) { "user" } |
| 77 | + |
| 78 | + it "returns user_hash" do |
| 79 | + message = described_class.new(role: role, content: "Hello, how can I help you?") |
| 80 | + expect(message).to receive(:user_hash).and_call_original |
| 81 | + expect(message.to_hash).to eq( |
| 82 | + role: role, |
| 83 | + content: [ |
| 84 | + { |
| 85 | + type: "text", |
| 86 | + text: "Hello, how can I help you?" |
52 | 87 | } |
53 | | - } |
54 | | - ] |
55 | | - ) |
| 88 | + ] |
| 89 | + ) |
| 90 | + end |
| 91 | + |
| 92 | + it "returns user_hash with image_url" do |
| 93 | + message = described_class.new(role: role, image_url: "https://example.com/image.jpg") |
| 94 | + allow(message).to receive(:image).and_return(double(base64: "base64_data", mime_type: "image/jpeg")) |
| 95 | + |
| 96 | + expect(message.to_hash).to eq( |
| 97 | + role: role, |
| 98 | + content: [ |
| 99 | + { |
| 100 | + type: "image", |
| 101 | + source: { |
| 102 | + type: "base64", |
| 103 | + data: "base64_data", |
| 104 | + media_type: "image/jpeg" |
| 105 | + } |
| 106 | + } |
| 107 | + ] |
| 108 | + ) |
| 109 | + end |
56 | 110 | end |
57 | 111 | end |
58 | 112 | end |
0 commit comments