Skip to content

Commit 6d5a1f3

Browse files
authored
refactor(tools): rename date param to start_date and slim holiday API model (#107)
1 parent 1c5afd7 commit 6d5a1f3

2 files changed

Lines changed: 185 additions & 32 deletions

File tree

‎data-agent-backend/src/main/java/io/github/malonetalk/agent/tools/DateInfoTool.java‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.time.temporal.ChronoUnit;
3636
import java.util.Locale;
3737
import java.util.Optional;
38+
import lombok.Builder;
3839
import org.springframework.beans.factory.annotation.Autowired;
3940
import org.springframework.stereotype.Component;
4041
import org.springframework.util.StringUtils;
@@ -69,17 +70,18 @@ public DateInfoTool(ObjectMapper objectMapper) {
6970
date. Use this whenever the user asks about today, a date, weekday, whether a \
7071
day is a holiday, Chinese holiday schedule, or date difference. For relative \
7172
dates, convert them using the system prompt's current date before passing \
72-
date; omit date for today. Pass end_date only when a calendar-day difference \
73-
is needed. Date difference counts start date inclusive and end date exclusive.\
73+
start_date; omit start_date for today. Pass end_date only when a calendar-day \
74+
difference is needed. Date difference counts start_date inclusive and \
75+
end_date exclusive.\
7476
""")
7577
public String getDateInfo(
7678
@ToolParam(
77-
name = "date",
79+
name = "start_date",
7880
description =
79-
"Date in yyyy-MM-dd format. Defaults to today. Also acts as"
80-
+ " start date when end_date is provided.",
81+
"Date in yyyy-MM-dd format. Defaults to today. When end_date"
82+
+ " is provided, this acts as the start of the range.",
8183
required = false)
82-
String date,
84+
String startDate,
8385
@ToolParam(
8486
name = "end_date",
8587
description =
@@ -94,39 +96,39 @@ public String getDateInfo(
9496
required = false)
9597
String timezone) {
9698
try {
97-
return objectMapper.writeValueAsString(resolve(date, endDate, timezone));
99+
return objectMapper.writeValueAsString(resolve(startDate, endDate, timezone));
98100
} catch (Exception e) {
99101
return "Error: failed to get date info: " + e.getMessage();
100102
}
101103
}
102104

103-
DateInfo resolve(String dateText, String timezoneText) {
104-
return resolve(dateText, null, timezoneText);
105-
}
106-
107-
DateInfo resolve(String dateText, String endDateText, String timezoneText) {
105+
DateInfo resolve(String startDateText, String endDateText, String timezoneText) {
108106
ZoneId zoneId =
109107
ZoneId.of(StringUtils.hasText(timezoneText) ? timezoneText : DEFAULT_TIMEZONE);
110-
LocalDate date =
111-
StringUtils.hasText(dateText)
112-
? LocalDate.parse(dateText)
108+
LocalDate startDate =
109+
StringUtils.hasText(startDateText)
110+
? LocalDate.parse(startDateText)
113111
: LocalDate.now(clock.withZone(zoneId));
114-
Optional<HolidayApiResponse> holidayApiResponse = queryHolidayApi(date);
112+
Optional<HolidayApiResponse> holidayApiResponse = queryHolidayApi(startDate);
115113
HolidayDetail holiday = holidayApiResponse.map(HolidayApiResponse::holiday).orElse(null);
116114

117115
boolean legalHoliday = holiday != null && Boolean.TRUE.equals(holiday.holiday());
116+
// API returns holiday={holiday:false} only for adjusted workdays (调休补班);
117+
// regular non-holiday days return holiday=null.
118118
boolean adjustedWorkday = holiday != null && Boolean.FALSE.equals(holiday.holiday());
119-
boolean weekend = date.getDayOfWeek().getValue() >= 6;
120-
121-
return new DateInfo(
122-
date.toString(),
123-
date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA),
124-
holiday != null ? holiday.name() : null,
125-
dayType(legalHoliday, adjustedWorkday, weekend),
126-
holidayApiResponse.isPresent(),
127-
StringUtils.hasText(endDateText)
128-
? resolveDateDiff(date, LocalDate.parse(endDateText))
129-
: null);
119+
boolean weekend = startDate.getDayOfWeek().getValue() >= 6;
120+
121+
return DateInfo.builder()
122+
.date(startDate.toString())
123+
.weekday(startDate.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA))
124+
.holidayName(holiday != null ? holiday.name() : null)
125+
.dayType(dayType(legalHoliday, adjustedWorkday, weekend))
126+
.holidayDataAvailable(holidayApiResponse.isPresent())
127+
.dateDiff(
128+
StringUtils.hasText(endDateText)
129+
? resolveDateDiff(startDate, LocalDate.parse(endDateText))
130+
: null)
131+
.build();
130132
}
131133

132134
DateDiff resolveDateDiff(LocalDate startDate, LocalDate endDate) {
@@ -174,6 +176,7 @@ private String dayType(boolean legalHoliday, boolean adjustedWorkday, boolean we
174176
return weekend ? "WEEKEND" : "WORKDAY";
175177
}
176178

179+
@Builder
177180
record DateInfo(
178181
String date,
179182
String weekday,
@@ -185,11 +188,8 @@ record DateInfo(
185188
record DateDiff(long calendarDays, long absoluteCalendarDays) {}
186189

187190
@JsonIgnoreProperties(ignoreUnknown = true)
188-
record HolidayApiResponse(int code, HolidayType type, HolidayDetail holiday) {}
189-
190-
@JsonIgnoreProperties(ignoreUnknown = true)
191-
record HolidayType(Integer type, String name, Integer week) {}
191+
record HolidayApiResponse(int code, HolidayDetail holiday) {}
192192

193193
@JsonIgnoreProperties(ignoreUnknown = true)
194-
record HolidayDetail(Boolean holiday, String name, String target, String date) {}
194+
record HolidayDetail(Boolean holiday, String name) {}
195195
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
package io.github.malonetalk.agent.tools;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
27+
28+
import com.fasterxml.jackson.databind.ObjectMapper;
29+
import java.net.http.HttpClient;
30+
import java.net.http.HttpResponse;
31+
import java.time.Clock;
32+
import java.time.Instant;
33+
import java.time.LocalDate;
34+
import java.time.ZoneId;
35+
import org.junit.jupiter.api.Test;
36+
37+
class DateInfoToolTest {
38+
39+
private final ObjectMapper objectMapper = new ObjectMapper();
40+
private final Clock fixedClock =
41+
Clock.fixed(Instant.parse("2026-08-12T00:00:00Z"), ZoneId.of("Asia/Shanghai"));
42+
43+
// --- Date diff ---
44+
45+
@Test
46+
void dateDiff_positive() {
47+
DateInfoTool tool = new DateInfoTool(objectMapper, fixedClock, mock(HttpClient.class));
48+
var diff = tool.resolveDateDiff(LocalDate.of(2026, 8, 12), LocalDate.of(2026, 8, 15));
49+
assertEquals(3, diff.calendarDays());
50+
assertEquals(3, diff.absoluteCalendarDays());
51+
}
52+
53+
@Test
54+
void dateDiff_negativeWhenEndBeforeStart() {
55+
DateInfoTool tool = new DateInfoTool(objectMapper, fixedClock, mock(HttpClient.class));
56+
var diff = tool.resolveDateDiff(LocalDate.of(2026, 8, 15), LocalDate.of(2026, 8, 12));
57+
assertEquals(-3, diff.calendarDays());
58+
assertEquals(3, diff.absoluteCalendarDays());
59+
}
60+
61+
// --- Day classification: holiday API available ---
62+
63+
@Test
64+
void publicHoliday() {
65+
var tool =
66+
toolWithHolidayApi("{\"code\":0,\"holiday\":{\"holiday\":true,\"name\":\"国庆节\"}}");
67+
var info = tool.resolve("2026-10-01", null, "Asia/Shanghai");
68+
assertEquals("2026-10-01", info.date());
69+
assertEquals("PUBLIC_HOLIDAY", info.dayType());
70+
assertEquals("国庆节", info.holidayName());
71+
assertTrue(info.holidayDataAvailable());
72+
}
73+
74+
@Test
75+
void adjustedWorkday() {
76+
var tool =
77+
toolWithHolidayApi("{\"code\":0,\"holiday\":{\"holiday\":false,\"name\":\"调休\"}}");
78+
var info = tool.resolve("2026-10-10", null, "Asia/Shanghai");
79+
assertEquals("ADJUSTED_WORKDAY", info.dayType());
80+
}
81+
82+
@Test
83+
void regularWorkday() {
84+
var tool =
85+
toolWithHolidayApi(
86+
"{\"code\":0,\"holiday\":null}"); // API returns null holiday for regular
87+
// days
88+
var info = tool.resolve("2026-08-12", null, "Asia/Shanghai");
89+
assertEquals("WORKDAY", info.dayType());
90+
assertNull(info.holidayName());
91+
assertTrue(info.holidayDataAvailable());
92+
}
93+
94+
@Test
95+
void weekend() {
96+
var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}");
97+
var info = tool.resolve("2026-08-15", null, "Asia/Shanghai"); // Saturday
98+
assertEquals("WEEKEND", info.dayType());
99+
}
100+
101+
// --- Holiday API unavailable → graceful degradation ---
102+
103+
@Test
104+
@SuppressWarnings("unchecked")
105+
void holidayApiUnavailable_fallsBackToWeekdayClassification() throws Exception {
106+
HttpClient httpClient = mock(HttpClient.class);
107+
HttpResponse<String> mockResponse = mock(HttpResponse.class);
108+
when(httpClient.send(any(), any(HttpResponse.BodyHandler.class))).thenReturn(mockResponse);
109+
when(mockResponse.statusCode()).thenReturn(503);
110+
111+
var tool = new DateInfoTool(objectMapper, fixedClock, httpClient);
112+
var info = tool.resolve("2026-08-12", null, "Asia/Shanghai");
113+
114+
assertFalse(info.holidayDataAvailable());
115+
assertEquals("WORKDAY", info.dayType());
116+
assertNull(info.holidayName());
117+
}
118+
119+
// --- Resolve defaults to today when start_date omitted ---
120+
121+
@Test
122+
void omittedStartDate_defaultsToToday() {
123+
var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}");
124+
var info = tool.resolve(null, null, "Asia/Shanghai");
125+
assertEquals("2026-08-12", info.date()); // fixedClock is 2026-08-12
126+
}
127+
128+
// --- Date diff integrated ---
129+
130+
@Test
131+
void withEndDate_includesDiff() {
132+
var tool = toolWithHolidayApi("{\"code\":0,\"holiday\":null}");
133+
var info = tool.resolve("2026-08-12", "2026-08-15", "Asia/Shanghai");
134+
assertEquals(3, info.dateDiff().calendarDays());
135+
}
136+
137+
// --- helper ---
138+
139+
@SuppressWarnings("unchecked")
140+
private DateInfoTool toolWithHolidayApi(String jsonBody) {
141+
try {
142+
HttpClient httpClient = mock(HttpClient.class);
143+
HttpResponse<String> mockResponse = mock(HttpResponse.class);
144+
when(httpClient.send(any(), any(HttpResponse.BodyHandler.class)))
145+
.thenReturn(mockResponse);
146+
when(mockResponse.statusCode()).thenReturn(200);
147+
when(mockResponse.body()).thenReturn(jsonBody);
148+
return new DateInfoTool(objectMapper, fixedClock, httpClient);
149+
} catch (Exception e) {
150+
throw new RuntimeException(e);
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)