This commit is contained in:
lik
2026-06-01 15:48:50 +08:00
parent 24b56b1875
commit 40c45c6db7
51 changed files with 2079 additions and 9 deletions

View File

@@ -0,0 +1,42 @@
import { tool } from "langchain"
import lunar from "lunar-javascript"
const { Solar, Lunar, HolidayUtil, TermUtil } = lunar
import { getAccurateTime } from "./utils.js"
/**
* 日历工具
* 提供准确时间、农历信息、节气、节日和老黄历等信息
*/
export const getCalendarInfoTool = tool(
async () => {
try {
// 获取准确时间
const timeInfo = await getAccurateTime()
// 获取农历信息
const date = new Date(timeInfo.timestamp)
const solar = Solar.fromDate(date)
const lunar = solar.getLunar()
// 组合返回结果
const result = {
time: timeInfo,
weekday: solar.getWeek(),
lunar: {
lunar: lunar.toString(),
solar: solar.toString(),
}
}
return JSON.stringify(result, null, 2)
} catch (error) {
console.error('Error in calendar tool:', error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
{
name: "get_calendar_info",
description: "获取当前准确日期时间,以及对应的农历日期"
}
)

View File

@@ -0,0 +1,53 @@
import { tool } from "langchain"
/**
* 特定日期日历工具
* 提供指定日期的日历信息
*/
export const getLunarCalendarInfoTool = tool(
async ({ year, month, day }) => {
try {
const date = new Date(year, month - 1, day)
if (isNaN(date.getTime())) {
return JSON.stringify({ error: '无效的日期' }, null, 2)
}
// 获取农历信息
const lunarInfo = {}
return JSON.stringify(lunarInfo, null, 2)
} catch (error) {
console.error('Error in date calendar tool:', error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
{
name: "get_lunar_calendar_info",
description: "获取指定日期的日历信息,包括农历信息、老黄历、当年生肖、今日宜忌、喜神方位、财神方位、今日节气、今日节日等",
schema: {
type: "object",
properties: {
year: {
type: "number",
description: "年份",
minimum: 1900,
maximum: 2100
},
month: {
type: "number",
description: "月份",
minimum: 1,
maximum: 12
},
day: {
type: "number",
description: "日期",
minimum: 1,
maximum: 31
}
},
required: ["year", "month", "day"]
}
}
)

View File

@@ -0,0 +1,86 @@
import * as z from "zod"
import { tool } from "langchain"
import lunar from "lunar-javascript"
const { Solar, Lunar, HolidayUtil, TermUtil } = lunar
/**
* 获取准确时间工具
* 通过国内权威时间源获取当前准确时间
* @returns {string} - 返回当前准确时间的JSON字符串
*/
export async function getAccurateTime() {
const timeSources = [
{
name: 'timeapi.io',
url: 'https://timeapi.io/api/Time/current/zone?timeZone=Asia/Shanghai',
method: 'json'
},
{
name: '国家授时中心',
url: 'http://www.ntsc.ac.cn',
method: 'header'
},
{
name: '阿里云',
url: 'https://www.aliyun.com',
method: 'header'
},
{
name: '腾讯云',
url: 'https://cloud.tencent.com',
method: 'header'
},
{
name: '华为云',
url: 'https://www.huaweicloud.com',
method: 'header'
}
]
for (const source of timeSources) {
try {
const startTime = Date.now()
const res = await fetch(source.url)
const latency = Date.now() - startTime
let timestamp = null
let beijingTime = null
if (source.method === 'json') {
const data = await res.json()
timestamp = new Date(data.dateTime).getTime()
beijingTime = data.dateTime
} else if (source.method === 'header') {
const dateHeader = res.headers.get('Date')
if (dateHeader) {
timestamp = new Date(dateHeader).getTime()
beijingTime = new Date(timestamp).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' })
}
}
if (timestamp) {
return {
timestamp,
date: new Date(timestamp).toISOString(),
localTime: beijingTime,
source: source.name,
latency: latency,
success: true
}
}
} catch (error) {
console.log(`${source.name} 获取时间失败: ${error.message}`)
continue
}
}
const date = new Date()
return {
timestamp: date.getTime(),
date: date.toISOString(),
localTime: date.toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
success: false,
message: '使用本地时间作为备份'
}
}

View File

@@ -0,0 +1,41 @@
import { tool } from "langchain"
/**
* 年份节日列表工具
* 查询给定年份节日所在日期列表
*/
export const getYearHolidaysTool = tool(
async ({ year }) => {
try {
// 简化实现,返回基本节日信息
const holidays = [
{ date: `${year}-01-01`, name: '元旦' },
{ date: `${year}-02-14`, name: '情人节' },
{ date: `${year}-05-01`, name: '劳动节' },
{ date: `${year}-06-01`, name: '儿童节' },
{ date: `${year}-10-01`, name: '国庆节' }
]
return JSON.stringify({ year, holidays }, null, 2)
} catch (error) {
console.error('Error in year holidays tool:', error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
{
name: "get_year_holidays",
description: "查询给定年份节日所在日期列表",
schema: {
type: "object",
properties: {
year: {
type: "number",
description: "年份",
minimum: 1900,
maximum: 2100
}
},
required: ["year"]
}
}
)

View File

@@ -0,0 +1,60 @@
import { tool } from "langchain"
/**
* 年份节气列表工具
* 查询给定年份节气所在日期列表
*/
export const getYearTermsTool = tool(
async ({ year }) => {
try {
// 简化实现返回24节气信息
const terms = [
{ date: `${year}-02-04`, name: '立春' },
{ date: `${year}-02-19`, name: '雨水' },
{ date: `${year}-03-05`, name: '惊蛰' },
{ date: `${year}-03-20`, name: '春分' },
{ date: `${year}-04-04`, name: '清明' },
{ date: `${year}-04-19`, name: '谷雨' },
{ date: `${year}-05-05`, name: '立夏' },
{ date: `${year}-05-20`, name: '小满' },
{ date: `${year}-06-05`, name: '芒种' },
{ date: `${year}-06-21`, name: '夏至' },
{ date: `${year}-07-07`, name: '小暑' },
{ date: `${year}-07-22`, name: '大暑' },
{ date: `${year}-08-07`, name: '立秋' },
{ date: `${year}-08-23`, name: '处暑' },
{ date: `${year}-09-07`, name: '白露' },
{ date: `${year}-09-23`, name: '秋分' },
{ date: `${year}-10-08`, name: '寒露' },
{ date: `${year}-10-23`, name: '霜降' },
{ date: `${year}-11-07`, name: '立冬' },
{ date: `${year}-11-22`, name: '小雪' },
{ date: `${year}-12-07`, name: '大雪' },
{ date: `${year}-12-21`, name: '冬至' },
{ date: `${year + 1}-01-05`, name: '小寒' },
{ date: `${year + 1}-01-20`, name: '大寒' }
]
return JSON.stringify({ year, terms }, null, 2)
} catch (error) {
console.error('Error in year terms tool:', 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"]
}
}
)