优化了agent

This commit is contained in:
lik
2026-10-31 18:51:20 +08:00
parent ab584c5fa8
commit 02d57f82dc
22 changed files with 193 additions and 155 deletions
+3 -2
View File
@@ -1,6 +1,7 @@
import { tool } from "@langchain/core/tools"
import { z } from "zod"
import { Solar } from "lunar-javascript"
import logger from "../../../../utils/logger.js"
/**
* 日历工具
@@ -75,7 +76,7 @@ async function getBeijingNow() {
success: true
}
} catch (error) {
console.warn(`获取网络时间失败,使用本机时间: ${error.message}`)
logger.warn(`获取网络时间失败,使用本机时间: ${error.message}`)
const now = new Date()
return {
...getBeijingDateParts(now),
@@ -118,7 +119,7 @@ export const getCalendarInfoTool = tool(
const solar = Solar.fromYmd(now.year, now.month, now.day)
return JSON.stringify({ now, ...buildDayInfo(solar) }, null, 2)
} catch (error) {
console.error("Error in calendar tool:", error)
logger.error("Error in calendar tool:", error)
return JSON.stringify({ error: error.message }, null, 2)
}
},
+6 -14
View File
@@ -1,5 +1,6 @@
import { tool } from "langchain"
import lunarLib from "lunar-javascript"
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import lunarLib from "lunar-javascript";
const { Solar, HolidayUtil } = lunarLib
/**
@@ -36,17 +37,8 @@ export const getYearHolidaysTool = tool(
{
name: "get_year_holidays",
description: "查询给定年份节日所在日期列表,包括法定节假日安排(含调休)与传统节日",
schema: {
type: "object",
properties: {
year: {
type: "number",
description: "年份",
minimum: 1900,
maximum: 2100
}
},
required: ["year"]
}
schema: z.object({
year: z.number().int().min(1900).max(2100).describe("年份")
})
}
)
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import { DBModel } from "../../../../models/index.js";
import { escapeRegex } from "./utils.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import mongoose from "mongoose";
import { DBModel } from "../../../../models/index.js";
import { canTransition, STATUS_TRANSITIONS } from "../../../../utils/record_state.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import mongoose from "mongoose";
import { DBModel } from "../../../../models/index.js";
import { escapeRegex } from "./utils.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import mongoose from "mongoose";
import { DBModel } from "../../../../models/index.js";
import { genPinyin, genPinyinFL } from "../../../../models/schema/health_profile.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import { DBModel } from "../../../../models/index.js";
import { escapeRegex } from "./utils.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import mongoose from "mongoose";
import { DBModel } from "../../../../models/index.js";
import { escapeRegex } from "./utils.js";
+1 -1
View File
@@ -1,5 +1,5 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { z } from "zod";
import { DBModel } from "../../../../models/index.js";
import { escapeRegex } from "./utils.js";
+1 -2
View File
@@ -1,8 +1,7 @@
// Tool 模块统一导出入口
// Web 工具
export { webFetchTool } from './web/fetch.js';
export { webSearchTool } from './web/search.js';
export { webSearchTool, webFetchTool } from './web/tavily.js';
export { httpGetTool } from './web/http_get.js';
// 日历工具
-34
View File
@@ -1,34 +0,0 @@
import { TavilyExtract } from "@langchain/tavily";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import config from "../../../../config.js";
const webFetchTool = tool(
async ({
extractDepth = "basic",
includeImages = false,
format = "markdown",
urls = [],
}) => {
const tavilyExtract = new TavilyExtract({
tavilyApiKey: config.agent.tavily.apiKey,
extractDepth,
includeImages,
format
});
return await tavilyExtract._call({ urls });
},
{
name: "web_fetch",
description:
"抓取并提取指定网页的正文内容(默认 Markdown),适合阅读医院官网、政策公告、新闻等长文页面。",
schema: z.object({
urls: z.array(z.string()).min(1).max(5).describe("要抓取的网页 URL 列表(1-5 个)"),
extractDepth: z.enum(["basic", "advanced"]).default("basic").describe("提取深度:basic 更快,advanced 更完整"),
includeImages: z.boolean().default(false).describe("是否包含图片"),
format: z.enum(["markdown", "json"]).default("markdown").describe("返回格式"),
}),
},
);
export { webFetchTool };
+2 -2
View File
@@ -1,5 +1,5 @@
import { tool } from 'langchain';
import * as z from 'zod';
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import { safeFetch } from './http_client.js';
const BAIDU_MAP_HOST = 'api.map.baidu.com';
-49
View File
@@ -1,49 +0,0 @@
import { TavilySearch } from "@langchain/tavily";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import config from "../../../../config.js";
const webSearchTool = tool(
async ({
query,
maxResults = 5,
topic = "general",
includeRawContent = false,
}) => {
const tavilySearch = new TavilySearch({
maxResults,
tavilyApiKey: config.agent.tavily.apiKey,
includeRawContent,
topic,
});
return await tavilySearch._call({ query });
},
{
name: "web_search",
description:
"联网搜索公开网页信息,返回摘要与链接。适合查询医院公示、就医政策、停诊/号源公告等时效性信息。",
schema: z.object({
query: z.string().describe("搜索关键词或问题"),
maxResults: z
.number()
.int()
.min(1)
.max(10)
.optional()
.default(5)
.describe("返回结果数量(1-10),默认 5"),
topic: z
.enum(["general", "news", "finance"])
.optional()
.default("general")
.describe("搜索主题,政策/新闻类查询建议用 news"),
includeRawContent: z
.boolean()
.optional()
.default(false)
.describe("是否返回网页原文(内容较长,默认关闭)"),
}),
},
);
export { webSearchTool };
+76
View File
@@ -0,0 +1,76 @@
/**
* Tavily 联网工具:搜索 + 网页正文提取。
* 两个工具共用同一个 Tavily apiKey,合并在一个文件里便于维护。
*/
import { TavilySearch, TavilyExtract } from "@langchain/tavily";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import config from "../../../../config.js";
const webSearchTool = tool(
async ({ query, maxResults = 5, topic = "general", includeRawContent = false }) => {
const tavilySearch = new TavilySearch({
maxResults,
tavilyApiKey: config.agent.tavily.apiKey,
includeRawContent,
topic,
});
return await tavilySearch._call({ query });
},
{
name: "web_search",
description:
"联网搜索公开网页信息,返回摘要与链接。适合查询医院公示、就医政策、停诊/号源公告等时效性信息。",
schema: z.object({
query: z.string().describe("搜索关键词或问题"),
maxResults: z
.number()
.int()
.min(1)
.max(10)
.optional()
.default(5)
.describe("返回结果数量(1-10),默认 5"),
topic: z
.enum(["general", "news", "finance"])
.optional()
.default("general")
.describe("搜索主题,政策/新闻类查询建议用 news"),
includeRawContent: z
.boolean()
.optional()
.default(false)
.describe("是否返回网页原文(内容较长,默认关闭)"),
}),
},
);
const webFetchTool = tool(
async ({
extractDepth = "basic",
includeImages = false,
format = "markdown",
urls = [],
}) => {
const tavilyExtract = new TavilyExtract({
tavilyApiKey: config.agent.tavily.apiKey,
extractDepth,
includeImages,
format
});
return await tavilyExtract._call({ urls });
},
{
name: "web_fetch",
description:
"抓取并提取指定网页的正文内容(默认 Markdown),适合阅读医院官网、政策公告、新闻等长文页面。",
schema: z.object({
urls: z.array(z.string()).min(1).max(5).describe("要抓取的网页 URL 列表(1-5 个)"),
extractDepth: z.enum(["basic", "advanced"]).default("basic").describe("提取深度:basic 更快,advanced 更完整"),
includeImages: z.boolean().default(false).describe("是否包含图片"),
format: z.enum(["markdown", "json"]).default("markdown").describe("返回格式"),
}),
},
);
export { webSearchTool, webFetchTool };