import { tool } from "@langchain/core/tools"; import { z } from "zod"; import { hospitalKnowledgeStore, MAX_READ_CHARS } from "./store.js"; const hospitalKnowledgeReadTool = tool( async ({ hospital, section }) => { try { const result = await hospitalKnowledgeStore.read(hospital, { section }); if (!result.found) { return { success: false, error: result.error || `医院知识库中没有「${hospital}」的记录`, candidates: result.candidates || [], }; } if (result.sectionMissing) { return { success: false, error: `「${result.name}」档案中没有「${section}」章节`, hospital: result.name, sections: result.sections, }; } return { success: true, hospital: result.name, aliases: result.aliases, city: result.city, file: result.file, sections: result.sections, content: result.content, truncated: result.truncated, note: result.note || undefined, }; } catch (error) { return { success: false, error: error.message }; } }, { name: "hospital_knowledge_read", description: `读取一家医院的完整知识档案(Markdown 文本)。可用医院正式名称或别名定位;未收录时返回候选医院供确认。` + `档案较长时用 section 只读指定章节;单次返回上限 ${MAX_READ_CHARS} 字符,超限会截断并提示按章节读取。`, schema: z.object({ hospital: z.string().describe("医院名称或别名,例如「北京协和医院」「协和」"), section: z.string().optional().describe("只读指定章节,例如「联系电话」「医生与排班」;不传时返回整个档案"), }), }, ); export { hospitalKnowledgeReadTool };