const API = require('../../../lib/API') const MCP = require('../../../lib/AIChat/AiAssistantMcp').AI_ASSISTANT_MCP const sender = { type: 'string', description: '当前会话中的 RunForge 用户 uuid。涉及用户私有数据的工具必须传入该字段,并且只能读取或修改该用户拥有的数据。' } const pageProps = { current: { type: 'integer', minimum: 1, description: '页码。未传时默认为 1。' }, pagesize: { type: 'integer', minimum: 1, maximum: 20, description: '每页数量。未传时默认为 10,最大 20。' } } const tools = [ { name: 'list_lepao_accounts', description: '查询当前 RunForge 用户绑定的乐跑账号列表。用户询问账号状态、跑区、自动乐跑设置、目标次数、乐跑时间、通知方式或绑定邮箱时,优先使用此工具。支持模糊筛选和分页返回,响应包含 list 和 pagination。', inputSchema: { type: 'object', properties: { sender, student_num: { type: 'string', description: '学号模糊筛选。' }, name: { type: 'string', description: '姓名模糊筛选。' }, area: { type: 'string', description: '跑区筛选。用户指定跑区时使用精确跑区名称;用户说随机跑区时使用空值。' }, state: { type: 'integer', description: '-1 或不传表示全部;0 表示需重新登录,1 表示正常,2 表示异常。' }, auto_run: { type: 'integer', enum: [0, 1], description: '按自动乐跑开关筛选:1 开启,0 关闭。' }, ...pageProps }, required: ['sender'] } }, { name: 'list_lepao_records', description: '查询当前用户拥有的乐跑记录。用户询问乐跑历史、成功或失败结果、乐跑模式、乐跑时间,或某个绑定账号的记录时使用。支持账号、姓名、结果文本模糊筛选,支持毫秒时间戳范围筛选和分页返回。', inputSchema: { type: 'object', properties: { sender, lepao_account: { type: 'string', description: '乐跑学号/账号模糊筛选。' }, name: { type: 'string', description: '姓名模糊筛选。' }, result: { type: 'string', description: '结果文本模糊筛选,适合查询成功、失败原因等。' }, start_time: { type: 'integer', description: '开始时间戳,毫秒,包含该时间。' }, end_time: { type: 'integer', description: '结束时间戳,毫秒,不包含该时间。' }, ...pageProps }, required: ['sender'] } }, { name: 'list_lepao_count_ledger', description: '查询当前用户的乐跑次数余额变动流水。用户询问乐跑次数为什么变化、如何扣减/返还/充值,或想看次数交易历史时使用。支持业务类型、备注关键词、毫秒时间戳范围和分页查询,不暴露内部 id。', inputSchema: { type: 'object', properties: { sender, biz_type: { type: 'string', description: '业务类型精确筛选,例如消费、返还、充值或流水表定义的其它类型。' }, remark: { type: 'string', description: '备注关键词模糊筛选。' }, start_time: { type: 'integer', description: '开始时间戳,毫秒,包含该时间。' }, end_time: { type: 'integer', description: '结束时间戳,毫秒,包含该时间。' }, ...pageProps }, required: ['sender'] } }, { name: 'list_goods', description: '查询当前上架且可购买的商品列表。用户询问价格、套餐、库存、分类、权益或购买选项前使用。支持关键词、分类筛选和分页返回,响应不包含内部 id 和不必要的媒体字段。', inputSchema: { type: 'object', properties: { keyword: { type: 'string', description: '商品名称关键词模糊筛选。' }, category: { type: 'string', description: '商品分类精确筛选。' }, ...pageProps } } }, { name: 'list_my_orders', description: '查询当前用户创建的订单列表。用户询问支付状态、订单历史、退款状态,或需要定位公开订单号时使用。支持订单状态、订单号筛选和分页返回。', inputSchema: { type: 'object', properties: { sender, state: { type: 'integer', description: '-1 或不传表示全部;0 待支付,1 已支付,2 已取消,3 异常,4 已退款。' }, order_id: { type: 'string', description: '公开订单号精确筛选。这里是用户可见的 order_no/orderId,不是数据库内部 id。' }, ...pageProps }, required: ['sender'] } }, { name: 'save_lepao_account', description: '新增或部分更新当前用户拥有的一个乐跑账号。使用 student_num 识别账号。只传用户明确要设置的字段,未传字段会保持不变。已有账号可更新邮箱、跑区、自动乐跑开关/时间/星期、目标次数、通知方式或备注。不确定账号现状时先调用 list_lepao_accounts,不要用此工具读取账号数据。', inputSchema: { type: 'object', properties: { sender, student_num: { type: 'string', description: '乐跑学号/账号。必填,用于识别账号。' }, email: { type: 'string', description: '该乐跑账号的通知邮箱。notice_type 为 email 时必填,且不能使用 icloud.com 域名。' }, area: { type: 'string', description: '要使用的跑区。取值只能是:["兰花湖校区跑区", "主校区北跑区", "主校区南跑区", "重庆工商大学茶园校区"]。用户要求随机跑区时使用空值。' }, auto_time: { type: 'integer', minimum: -1, maximum: 23, description: '自动乐跑开始小时,范围 7-21;使用 -1 表示随机时间。' }, auto_run: { type: 'integer', enum: [0, 1], description: '自动乐跑开关:1 开启,0 关闭。' }, target_count: { type: 'number', minimum: 0, maximum: 99, description: '目标总乐跑次数,范围 0-99。开启自动乐跑时,目标次数不能小于或等于账号已累计完成次数,除非设置为 0。' }, auto_day: { type: 'array', items: { type: 'integer', minimum: 0, maximum: 6 }, description: '自动乐跑星期。0 周日,1 周一,依此类推,6 周六。' }, notice_type: { type: 'string', enum: ['email', 'none', 'bot'], description: '乐跑后的通知渠道:email 邮件,bot 机器人,none 不通知。' }, notes: { type: 'string', description: '该账号的用户可见备注。' } }, required: ['sender', 'student_num'] } }, // { // name: 'save_power_task', // description: '新增或部分更新当前用户的电费余额监控任务。已有任务可用 task_id 兼容识别,也可用 area + building + room 识别。只传用户要设置的字段,未传字段保持不变。新增任务时必须提供 area、building、room 和 lowest 阈值。', // inputSchema: { // type: 'object', // properties: { // sender, // task_id: { type: 'integer', description: '已有任务 id,主要用于兼容。用户提供区域、楼栋、房间时优先使用 area + building + room。' }, // area: { type: 'string', description: '校区或电费区域名称。' }, // building: { type: 'string', description: '电费系统中的楼栋标识或名称。' }, // room: { type: 'string', description: '电费系统中的房间标识或名称。' }, // email: { type: 'string', description: '用于接收低余额提醒的邮箱。' }, // lowest: { type: 'number', description: '低余额提醒阈值,余额低于该值时后端可能发送提醒。' }, // notes: { type: 'string', description: '该电费任务的用户可见备注。' } // }, // required: ['sender'] // } // }, { name: 'list_power_bills', description: '查询当前用户的电费监控任务列表。可按校区/电费区域、楼栋、房间筛选,返回分页任务列表;只用于查看任务本身和当前余额信息,不返回账单变动记录。查询某寝室电费账单记录请使用 list_power_bill_records。', inputSchema: { type: 'object', properties: { sender, area: { type: 'string', description: '校区或电费区域精确筛选。' }, building: { type: 'string', description: '楼栋精确筛选。' }, room: { type: 'string', description: '房间精确筛选。' }, ...pageProps }, required: ['sender'] } }, { name: 'list_power_bill_records', description: '查询某个寝室/房间的电费账单或余额变动记录。必须提供 sender、area、building、room 精确定位当前用户拥有的电费任务,建议先调用 list_power_bills 获取准确的寝室信息;返回匹配任务和分页 records。不暴露内部任务 id 或记录 id。', inputSchema: { type: 'object', properties: { sender, area: { type: 'string', description: '校区或电费区域,必填,需精确匹配。' }, building: { type: 'string', description: '楼栋,必填,需精确匹配。' }, room: { type: 'string', description: '寝室/房间,必填,需精确匹配。' }, ...pageProps }, required: ['sender', 'area', 'building', 'room'] } }, { name: 'send_lepao_count', description: '向其他用户赠送乐跑次数,系统审核通过后到账。需提供接收方用户名和赠送次数,次数范围为 1-9999。', inputSchema: { type: 'object', properties: { sender, username: { type: 'string', description: '接收方用户名,需精确匹配。' }, count: { type: 'integer', minimum: 1, maximum: 9999, description: '赠送的乐跑次数,范围 1-9999。' } }, required: ['sender', 'username', 'count'] } }, { name: 'list_send_count_requests', description: '查询当前用户的乐跑次数赠送记录。可按赠送方向和审核状态筛选,返回分页列表。', inputSchema: { type: 'object', properties: { sender, direction: { type: 'string', enum: ['sent', 'received'], description: '赠送方向筛选:sent 我发出的,received 我收到的。不传表示全部。' }, status: { type: 'string', enum: ['pending', 'approved', 'rejected'], description: '审核状态筛选:pending 待审核,approved 已通过,rejected 已拒绝。不传表示全部。' }, ...pageProps }, required: ['sender'] } }, { name: 'list_work_orders', description: '查询当前用户提交的客服工单列表。返回分页列表,包含工单编号、标题、状态、邮箱、创建时间和更新时间。', inputSchema: { type: 'object', properties: { sender, ...pageProps }, required: ['sender'] } }, { name: 'get_work_order_detail', description: '查询当前用户某个客服工单详情。必须提供 work_order_id,只能查询当前 sender 自己创建的工单;返回消息列表和消息发送者 userInfo。', inputSchema: { type: 'object', properties: { sender, work_order_id: { type: 'integer', minimum: 1, description: '工单编号,即 list_work_orders 返回的 work_order_id。' } }, required: ['sender', 'work_order_id'] } }, { name: 'query_qxs_book_list', description: '使用用户在当前会话中提供的趣选书账号密码查询教材/书单。仅在用户明确要求查询教材、课程用书信息,并提供 username/password 时使用。查询到的结果会分页返回。', inputSchema: { type: 'object', properties: { sender, username: { type: 'string', description: '用户提供的趣选书用户名。' }, password: { type: 'string', description: '用户为本次查询提供的趣选书密码。' }, ...pageProps }, required: ['sender', 'username', 'password'] } }, { name: 'submit_work_order', description: '为当前用户提交客服工单。当其它工具无法解决问题,或问题需要人工处理时使用。标题应简洁,内容应包含必要上下文。用户可在工单列表查看处理进度。', inputSchema: { type: 'object', properties: { sender, title: { type: 'string', maxLength: 80, description: '简短工单标题,最多 80 个字符。' }, content: { type: 'string', maxLength: 4000, description: '详细问题描述和相关上下文,最多 4000 个字符。' }, email: { type: 'string', description: '该工单的可选联系邮箱。' }, files: { type: 'array', items: { type: 'string' }, maxItems: 6, description: '与问题相关的可选上传文件路径或 URL,最多 6 个。' } }, required: ['sender', 'title', 'content'] } } ] class McpRpc extends API { constructor() { super() this.noEncrypt() this.setPath('/aiAssistantMcp') this.setMethod('POST') } async onRequest(req, res) { const { method, params = {}, id: reqId } = req.body || {} try { let result if (method === 'initialize') { result = { protocolVersion: '2024-11-05', capabilities: { tools: {} }, serverInfo: { name: 'runforgeAiAssistant', version: '1.0' } } } else if (method === 'tools/list') { result = { tools } } else if (method === 'tools/call') { const { name, arguments: args = {} } = params if (!MCP[name] || typeof MCP[name] !== 'function') { result = { content: [{ type: 'text', text: '未知工具' }] } } else { const output = await MCP[name](args) result = { content: [{ type: 'text', text: output }] } } } else { result = { error: `未知方法: ${method}` } } return res.json({ jsonrpc: '2.0', id: reqId ?? null, result }) } catch (err) { return res.json({ jsonrpc: '2.0', id: reqId ?? null, error: { code: -32000, message: err.message } }) } } } module.exports.McpRpc = McpRpc