Skip to content

Commit 75f3b68

Browse files
authored
fix(msg):msg constructor method throw npe (#407)
1 parent 69e3268 commit 75f3b68

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

  • agentscope-core/src

agentscope-core/src/main/java/io/agentscope/core/message/Msg.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.Instant;
2828
import java.time.ZoneId;
2929
import java.time.format.DateTimeFormatter;
30+
import java.util.HashMap;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Objects;
@@ -88,8 +89,21 @@ private Msg(
8889
this.id = id;
8990
this.name = name;
9091
this.role = role;
91-
this.content = Objects.nonNull(content) ? List.copyOf(content) : List.of();
92-
this.metadata = Objects.nonNull(metadata) ? Map.copyOf(metadata) : Map.of();
92+
this.content =
93+
Objects.nonNull(content)
94+
? content.stream().filter(Objects::nonNull).toList()
95+
: List.of();
96+
this.metadata = new HashMap<>();
97+
if (Objects.nonNull(metadata)) {
98+
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
99+
String key = entry.getKey();
100+
Object value = entry.getValue();
101+
if (Objects.isNull(key) || Objects.isNull(value)) {
102+
continue;
103+
}
104+
this.metadata.put(key, value);
105+
}
106+
}
93107
this.timestamp = timestamp;
94108
}
95109

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024-2025 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.msg;
17+
18+
import io.agentscope.core.message.ContentBlock;
19+
import io.agentscope.core.message.Msg;
20+
import io.agentscope.core.message.MsgRole;
21+
import java.util.Map;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
public class MsgTests {
26+
27+
@Test
28+
void testConstructor() {
29+
Msg msg1 =
30+
Msg.builder()
31+
.name("test")
32+
.role(MsgRole.SYSTEM)
33+
.content(new ContentBlock())
34+
.timestamp(String.valueOf(System.currentTimeMillis()))
35+
.metadata(Map.of("key", "value"))
36+
.build();
37+
Assertions.assertNotNull(msg1);
38+
39+
Msg msg2 =
40+
Msg.builder()
41+
.name("test")
42+
.role(MsgRole.SYSTEM)
43+
.timestamp(String.valueOf(System.currentTimeMillis()))
44+
.build();
45+
Assertions.assertNotNull(msg2);
46+
}
47+
}

0 commit comments

Comments
 (0)