45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
import { tool } from "@langchain/core/tools";
|
|
import { z } from "zod";
|
|
import lunarLib from "lunar-javascript";
|
|
const { Solar, HolidayUtil } = lunarLib
|
|
|
|
/**
|
|
* 年份节日列表工具
|
|
* 基于 lunar-javascript 计算全年节日(公历节日 + 农历传统节日)与法定节假日安排
|
|
*/
|
|
export const getYearHolidaysTool = tool(
|
|
async ({ year }) => {
|
|
try {
|
|
// 法定节假日安排(含调休补班)
|
|
const legal = HolidayUtil.getHolidays(year).map(h => ({
|
|
date: h.getTarget().toString(),
|
|
name: h.getName(),
|
|
type: h.isWork() ? "调休上班" : "放假"
|
|
}))
|
|
|
|
// 遍历全年日期,收集公历节日与农历传统节日(春节、中秋、端午等)
|
|
const festivals = []
|
|
const start = Solar.fromYmd(year, 1, 1)
|
|
const days = Solar.fromYmd(year + 1, 1, 1).subtract(start)
|
|
for (let i = 0; i < days; i++) {
|
|
const d = start.next(i)
|
|
const names = [...d.getFestivals(), ...d.getLunar().getFestivals()]
|
|
if (names.length) {
|
|
festivals.push({ date: d.toString(), name: names.join("、") })
|
|
}
|
|
}
|
|
|
|
return JSON.stringify({ year, legal, festivals }, null, 2)
|
|
} catch (error) {
|
|
return JSON.stringify({ error: error.message }, null, 2)
|
|
}
|
|
},
|
|
{
|
|
name: "get_year_holidays",
|
|
description: "查询给定年份节日所在日期列表,包括法定节假日安排(含调休)与传统节日",
|
|
schema: z.object({
|
|
year: z.number().int().min(1900).max(2100).describe("年份")
|
|
})
|
|
}
|
|
)
|