|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.agentscope.core.formatter.gemini; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import io.agentscope.core.formatter.gemini.dto.GeminiContent; |
| 23 | +import io.agentscope.core.message.Base64Source; |
| 24 | +import io.agentscope.core.message.ImageBlock; |
| 25 | +import io.agentscope.core.message.Msg; |
| 26 | +import io.agentscope.core.message.MsgRole; |
| 27 | +import io.agentscope.core.message.TextBlock; |
| 28 | +import io.agentscope.core.message.ThinkingBlock; |
| 29 | +import io.agentscope.core.message.ToolResultBlock; |
| 30 | +import java.util.Base64; |
| 31 | +import java.util.List; |
| 32 | +import org.junit.jupiter.api.DisplayName; |
| 33 | +import org.junit.jupiter.api.Tag; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +@Tag("unit") |
| 37 | +@DisplayName("GeminiConversationMerger Unit Tests") |
| 38 | +class GeminiConversationMergerTest { |
| 39 | + |
| 40 | + private static final String HISTORY_PROMPT = |
| 41 | + "# Conversation History\nThe content between <history></history> tags contains your" |
| 42 | + + " conversation history\n"; |
| 43 | + |
| 44 | + private final GeminiConversationMerger merger = new GeminiConversationMerger(HISTORY_PROMPT); |
| 45 | + |
| 46 | + @Test |
| 47 | + @DisplayName("Should merge text conversation with history tags and skip thinking block") |
| 48 | + void testMergeTextConversation() { |
| 49 | + Msg user = |
| 50 | + Msg.builder() |
| 51 | + .name("user") |
| 52 | + .role(MsgRole.USER) |
| 53 | + .content(List.of(TextBlock.builder().text("Hello").build())) |
| 54 | + .build(); |
| 55 | + Msg assistantThinking = |
| 56 | + Msg.builder() |
| 57 | + .name("assistant") |
| 58 | + .role(MsgRole.ASSISTANT) |
| 59 | + .content(List.of(ThinkingBlock.builder().thinking("internal").build())) |
| 60 | + .build(); |
| 61 | + Msg toolResult = |
| 62 | + Msg.builder() |
| 63 | + .name("system") |
| 64 | + .role(MsgRole.SYSTEM) |
| 65 | + .content( |
| 66 | + List.of( |
| 67 | + ToolResultBlock.builder() |
| 68 | + .id("call-1") |
| 69 | + .name("search") |
| 70 | + .output(List.of(TextBlock.builder().text("ok").build())) |
| 71 | + .build())) |
| 72 | + .build(); |
| 73 | + |
| 74 | + GeminiContent merged = |
| 75 | + merger.mergeToContent( |
| 76 | + List.of(user, assistantThinking, toolResult), |
| 77 | + m -> m.getName() != null ? m.getName() : m.getRole().name().toLowerCase(), |
| 78 | + blocks -> "tool output", |
| 79 | + HISTORY_PROMPT); |
| 80 | + |
| 81 | + assertEquals("user", merged.getRole()); |
| 82 | + assertEquals(1, merged.getParts().size()); |
| 83 | + String text = merged.getParts().get(0).getText(); |
| 84 | + assertTrue(text.startsWith(HISTORY_PROMPT + "<history>")); |
| 85 | + assertTrue(text.contains("user: Hello")); |
| 86 | + assertTrue(text.contains("Tool: search\ntool output")); |
| 87 | + assertTrue(text.endsWith("</history>")); |
| 88 | + assertTrue(!text.contains("internal")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @DisplayName("Should insert history tags when first and last parts are media") |
| 93 | + void testMergeMediaOnlyConversation() { |
| 94 | + String base64 = Base64.getEncoder().encodeToString("img".getBytes()); |
| 95 | + ImageBlock image = |
| 96 | + ImageBlock.builder() |
| 97 | + .source(Base64Source.builder().mediaType("image/png").data(base64).build()) |
| 98 | + .build(); |
| 99 | + |
| 100 | + Msg mediaMsg = |
| 101 | + Msg.builder().name("user").role(MsgRole.USER).content(List.of(image)).build(); |
| 102 | + |
| 103 | + GeminiContent merged = |
| 104 | + merger.mergeToContent( |
| 105 | + List.of(mediaMsg), |
| 106 | + m -> m.getName() != null ? m.getName() : "user", |
| 107 | + blocks -> "", |
| 108 | + ""); |
| 109 | + |
| 110 | + assertEquals("user", merged.getRole()); |
| 111 | + assertEquals(3, merged.getParts().size()); |
| 112 | + assertEquals("<history>", merged.getParts().get(0).getText()); |
| 113 | + assertNotNull(merged.getParts().get(1).getInlineData()); |
| 114 | + assertEquals("</history>", merged.getParts().get(2).getText()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("Should append closing tag as separate part when last part is media") |
| 119 | + void testMergeTextThenMediaConversation() { |
| 120 | + String base64 = Base64.getEncoder().encodeToString("img".getBytes()); |
| 121 | + ImageBlock image = |
| 122 | + ImageBlock.builder() |
| 123 | + .source(Base64Source.builder().mediaType("image/png").data(base64).build()) |
| 124 | + .build(); |
| 125 | + |
| 126 | + Msg msg = |
| 127 | + Msg.builder() |
| 128 | + .name("agent") |
| 129 | + .role(MsgRole.USER) |
| 130 | + .content(List.of(TextBlock.builder().text("Look").build(), image)) |
| 131 | + .build(); |
| 132 | + |
| 133 | + GeminiContent merged = |
| 134 | + merger.mergeToContent( |
| 135 | + List.of(msg), m -> "agent", blocks -> "", "# Prompt\n"); |
| 136 | + |
| 137 | + assertEquals(3, merged.getParts().size()); |
| 138 | + assertTrue(merged.getParts().get(0).getText().startsWith("# Prompt\n<history>agent: Look")); |
| 139 | + assertNotNull(merged.getParts().get(1).getInlineData()); |
| 140 | + assertEquals("</history>", merged.getParts().get(2).getText()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @DisplayName("Should return empty parts for empty conversation input") |
| 145 | + void testMergeEmptyConversation() { |
| 146 | + GeminiContent merged = |
| 147 | + merger.mergeToContent( |
| 148 | + List.of(), |
| 149 | + m -> m.getName() != null ? m.getName() : "unknown", |
| 150 | + blocks -> "", |
| 151 | + HISTORY_PROMPT); |
| 152 | + |
| 153 | + assertEquals("user", merged.getRole()); |
| 154 | + assertNotNull(merged.getParts()); |
| 155 | + assertTrue(merged.getParts().isEmpty()); |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments