47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
import { tool } from "langchain"
|
|
import lunarLib from "lunar-javascript"
|
|
const { Lunar } = lunarLib
|
|
|
|
// 当年冬至在节气表中的 key 为英文(上一年冬至占用中文 key)
|
|
const EN_TERM_NAMES = { DONG_ZHI: "冬至" }
|
|
|
|
/**
|
|
* 年份节气列表工具
|
|
* 基于 lunar-javascript 计算给定年份的 24 节气精确日期
|
|
*/
|
|
export const getYearTermsTool = tool(
|
|
async ({ year }) => {
|
|
try {
|
|
const table = Lunar.fromYmd(year, 1, 1).getJieQiTable()
|
|
const terms = []
|
|
for (const [key, solar] of Object.entries(table)) {
|
|
// 只保留属于目标年份的节气(表首含上一年边界节气)
|
|
if (solar.getYear() !== year) continue
|
|
const name = EN_TERM_NAMES[key] ?? key
|
|
terms.push({ date: solar.toString(), name })
|
|
}
|
|
terms.sort((a, b) => a.date.localeCompare(b.date))
|
|
|
|
return JSON.stringify({ year, terms }, null, 2)
|
|
} catch (error) {
|
|
return JSON.stringify({ error: error.message }, null, 2)
|
|
}
|
|
},
|
|
{
|
|
name: "get_year_terms",
|
|
description: "查询给定年份节气所在日期列表",
|
|
schema: {
|
|
type: "object",
|
|
properties: {
|
|
year: {
|
|
type: "number",
|
|
description: "年份",
|
|
minimum: 1900,
|
|
maximum: 2100
|
|
}
|
|
},
|
|
required: ["year"]
|
|
}
|
|
}
|
|
)
|