Browse Source

✨ feat: 增加乐跑时间修改MCP

Pchen. 1 month ago
parent
commit
95a809ef3a
2 changed files with 246 additions and 21 deletions
  1. 114 20
      apis/MCP/McpRPC.js
  2. 132 1
      lib/Lepao/Mcp.js

+ 114 - 20
apis/MCP/McpRPC.js

@@ -27,71 +27,159 @@ class McpRpc extends API {
                     "tools": [
                     "tools": [
                         {
                         {
                             "name": "bind_account",
                             "name": "bind_account",
-                            "description": "绑定账号",
+                            "description": "Bind a user account when the user provides a bind code",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": {
                                 "properties": {
-                                    "sender": { "type": "string" },
-                                    "bind_code": { "type": "string" }
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "bind_code": {
+                                        "type": "string",
+                                        "description": "Binding code provided by the user"
+                                    }
                                 },
                                 },
                                 "required": ["sender", "bind_code"]
                                 "required": ["sender", "bind_code"]
                             }
                             }
                         },
                         },
                         {
                         {
-                            "name": "get_account_info",
-                            "description": "获取账号信息,包含姓名学号、帐号状态、乐跑时间次数等",
+                            "name": "unbind_account",
+                            "description": "Unbind the currently linked user account",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
-                                "properties": { "sender": { "type": "string" } },
+                                "properties": {
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    }
+                                },
                                 "required": ["sender"]
                                 "required": ["sender"]
                             }
                             }
                         },
                         },
                         {
                         {
-                            "name": "unbind_account",
-                            "description": "解绑账号",
+                            "name": "get_account_info",
+                            "description": "Retrieve user account information such as name, student ID, account status, and running statistics",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
-                                "properties": { "sender": { "type": "string" } },
+                                "properties": {
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    }
+                                },
                                 "required": ["sender"]
                                 "required": ["sender"]
                             }
                             }
                         },
                         },
                         {
                         {
                             "name": "set_notification",
                             "name": "set_notification",
-                            "description": "消息通知设置,mode参数为:bot:智能机器人通知、email:邮件通知、none:关闭通知",
+                            "description": "Set notification preference when the user wants to enable or disable notifications",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": {
                                 "properties": {
-                                    "sender": { "type": "string" },
-                                    "mode": { "type": "string" }
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "mode": {
+                                        "type": "string",
+                                        "enum": ["bot", "email", "none"],
+                                        "description": "Notification mode: bot (chat bot), email (email notification), none (disable notifications)"
+                                    }
                                 },
                                 },
                                 "required": ["sender", "mode"]
                                 "required": ["sender", "mode"]
                             }
                             }
                         },
                         },
                         {
                         {
                             "name": "change_email",
                             "name": "change_email",
-                            "description": "更换通知邮箱,需传入合法邮箱地址:email",
+                            "description": "Update the user's notification email address",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": {
                                 "properties": {
-                                    "sender": { "type": "string" },
-                                    "email": { "type": "string" }
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "email": {
+                                        "type": "string",
+                                        "format": "email",
+                                        "description": "New email address for notifications"
+                                    }
                                 },
                                 },
                                 "required": ["sender", "email"]
                                 "required": ["sender", "email"]
                             }
                             }
                         },
                         },
                         {
                         {
                             "name": "create_work_order",
                             "name": "create_work_order",
-                            "description": "创建客服工单,遇到问题或建议可创建并等待人工处理,参数email为联系方式,工单标题title不超过20字,内容content不超过100字,标题和内容最好由你总结生成,且包含用户账号的基本信息",
+                            "description": "Create a customer support ticket when the user reports a problem or suggestion",
                             "inputSchema": {
                             "inputSchema": {
                                 "type": "object",
                                 "type": "object",
                                 "properties": {
                                 "properties": {
-                                    "sender": { "type": "string" },
-                                    "email": { "type": "string" },
-                                    "title": { "type": "string" },
-                                    "content": { "type": "string" }
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "email": {
+                                        "type": "string",
+                                        "format": "email",
+                                        "description": "User contact email"
+                                    },
+                                    "title": {
+                                        "type": "string",
+                                        "maxLength": 20,
+                                        "description": "Short title of the issue (max 20 characters)"
+                                    },
+                                    "content": {
+                                        "type": "string",
+                                        "maxLength": 100,
+                                        "description": "Detailed description of the issue (max 100 characters)"
+                                    }
                                 },
                                 },
                                 "required": ["sender", "email", "title", "content"]
                                 "required": ["sender", "email", "title", "content"]
                             }
                             }
+                        },
+                        {
+                            "name": "change_auto_time",
+                            "description": "Set the automatic running start hour when the user wants to schedule running time",
+                            "inputSchema": {
+                                "type": "object",
+                                "properties": {
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "auto_time": {
+                                        "type": "integer",
+                                        "minimum": 7,
+                                        "maximum": 23,
+                                        "description": "Hour of day to start running (7-23)"
+                                    }
+                                },
+                                "required": ["sender", "auto_time"]
+                            }
+                        },
+                        {
+                            "name": "change_auto_day",
+                            "description": "Set the days of the week for automatic running",
+                            "inputSchema": {
+                                "type": "object",
+                                "properties": {
+                                    "sender": {
+                                        "type": "string",
+                                        "description": "Unique user identifier from the chat platform"
+                                    },
+                                    "auto_day": {
+                                        "type": "array",
+                                        "items": {
+                                            "type": "integer",
+                                            "minimum": 0,
+                                            "maximum": 6
+                                        },
+                                        "description": "Days of week to run (0=Sunday, 6=Saturday)"
+                                    }
+                                },
+                                "required": ["sender", "auto_day"]
+                            }
                         }
                         }
                     ]
                     ]
                 }
                 }
@@ -119,6 +207,12 @@ class McpRpc extends API {
                     case "create_work_order":
                     case "create_work_order":
                         output = await MCP.create_work_order(args)
                         output = await MCP.create_work_order(args)
                         break
                         break
+                    case "change_auto_time":
+                        output = await MCP.change_auto_time(args)
+                        break
+                    case "change_auto_day":
+                        output = await MCP.change_auto_day(args)
+                        break
                     default:
                     default:
                         output = "未知工具"
                         output = "未知工具"
                 }
                 }

+ 132 - 1
lib/Lepao/Mcp.js

@@ -189,7 +189,7 @@ class Mcp {
             `
             `
 
 
             let insertRows = await db.query(insertSql, [email, sender])
             let insertRows = await db.query(insertSql, [email, sender])
-            if (!insertRows || insertRows.affectedRows !== 1)
+            if (!insertRows || insertRows.affectedRows === 0)
                 return '系统出错,请稍后再试'
                 return '系统出错,请稍后再试'
             return `更换成功`
             return `更换成功`
         } catch (error) {
         } catch (error) {
@@ -206,6 +206,25 @@ class Mcp {
             this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
             this.logger.info(`MCP接收设置通知请求:${sender},mode:${mode}`)
             if (mode !== 'email' && mode !== 'bot' && mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
             if (mode !== 'email' && mode !== 'bot' && mode !== 'none') return '通知type不合法,仅支持 email, bot, none三种模式'
 
 
+            let sql = `
+                SELECT 
+                    a.notice_type, a.email
+                FROM 
+                    lepao_extra f
+                LEFT JOIN 
+                    lepao_account a
+                ON 
+                    f.student_num = a.student_num
+                WHERE 
+                    f.bot_account = ?
+            `
+            const rows = await db.query(sql, [sender])
+            if (!rows) return '系统出错,请稍后再试'
+            if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
+
+            if (!rows[0].email) return '该账号还未设置邮箱,请先使用change_email设置邮箱'
+            if (rows[0].notice_type === 'mode') return `当前已是${mode}通知方式,无需修改`
+
             let insertSql = `
             let insertSql = `
                 UPDATE 
                 UPDATE 
                     lepao_account la
                     lepao_account la
@@ -228,6 +247,118 @@ class Mcp {
         }
         }
     }
     }
 
 
+    async change_auto_time({ sender, auto_time }) {
+        try {
+            if ([sender, auto_time].some(value => value === '' || value === null || value === undefined))
+                return '缺少参数'
+
+            this.logger.info(`MCP接收设置自动乐跑时间请求:${sender},auto_time:${auto_time}`)
+            if (!(Number.isInteger(auto_time) && ((auto_time >= 7 && auto_time <= 23) || auto_time === -1))) return '乐跑时间不合法,auto_time应为7~23或-1的整数'
+
+            let sql = `
+                SELECT 
+                    a.auto_run, a.auto_time, a.auto_day
+                FROM 
+                    lepao_extra f
+                LEFT JOIN 
+                    lepao_account a
+                ON 
+                    f.student_num = a.student_num
+                WHERE 
+                    f.bot_account = ?
+            `
+            const rows = await db.query(sql, [sender])
+            if (!rows) return '系统出错,请稍后再试'
+            if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
+
+            if (rows[0].auto_run !== 1) return '该账号未开启自动乐跑,请前往RunForge系统开启自动乐跑后再设置乐跑时间'
+            if (rows[0].auto_time === auto_time) return `乐跑时间与现有时间一致,无需修改`
+
+            let insertSql = `
+                UPDATE 
+                    lepao_account la
+                JOIN 
+                    lepao_extra le 
+                ON 
+                    la.student_num = le.student_num
+                SET 
+                    la.auto_time = ?
+                WHERE 
+                    le.bot_account = ?
+            `
+            let insertRows = await db.query(insertSql, [auto_time, sender])
+            if (!insertRows || insertRows.affectedRows === 0)
+                return '系统出错,请稍后再试'
+
+            const data = {
+                auto_time, auto_day: rows[0].auto_day
+            }
+
+            return `操作成功,现在自动乐跑时间为:${data.auto_day.slice().sort((a, b) => {
+                if (a === 0) return 1; if (b === 0) return -1; return a - b;
+            }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',')} ${this.autoTimeLabel(data).replace(/今日/g, '')}`
+        } catch (error) {
+            this.logger.error(`MCP更换设置自动乐跑时间出错:${error.message}`)
+            return '系统出错,请稍后再试'
+        }
+    }
+
+    async change_auto_day({ sender, auto_day }) {
+        try {
+            if ([sender, auto_day].some(value => value === '' || value === null || value === undefined))
+                return '缺少参数'
+
+            this.logger.info(`MCP接收设置自动乐跑请求:${sender},auto_day:${auto_day}`)
+            if (!Array.isArray(auto_day) || !auto_day.every(v => Number.isInteger(v) && v >= 0 && v <= 6))
+                return '乐跑星期不合法,auto_day应为仅包含整数0~6的数组'
+
+            let sql = `
+                SELECT 
+                    a.auto_run, a.auto_time, a.auto_day
+                FROM 
+                    lepao_extra f
+                LEFT JOIN 
+                    lepao_account a
+                ON 
+                    f.student_num = a.student_num
+                WHERE 
+                    f.bot_account = ?
+            `
+            const rows = await db.query(sql, [sender])
+            if (!rows) return '系统出错,请稍后再试'
+            if (rows.length == 0) return '您尚未绑定乐跑账号,请先绑定'
+
+            if (rows[0].auto_run !== 1) return '该账号未开启自动乐跑,请前往RunForge系统开启自动乐跑后再设置乐跑时间'
+
+            let insertSql = `
+                UPDATE 
+                    lepao_account la
+                JOIN 
+                    lepao_extra le 
+                ON 
+                    la.student_num = le.student_num
+                SET 
+                    la.auto_day = ?
+                WHERE 
+                    le.bot_account = ?
+            `
+            let insertRows = await db.query(insertSql, [JSON.stringify(auto_day), sender])
+            if (!insertRows || insertRows.affectedRows === 0)
+                return '系统出错,请稍后再试'
+
+            const data = {
+                auto_time: rows[0].auto_time, auto_day
+            }
+
+            return `操作成功,现在自动乐跑时间为:${data.auto_day.slice().sort((a, b) => {
+                if (a === 0) return 1; if (b === 0) return -1; return a - b;
+            }).map(day => this.auto_day.find(item => item.value === day)?.label).join(',')} ${this.autoTimeLabel(data).replace(/今日/g, '')}`
+        } catch (error) {
+            this.logger.error(`MCP更换设置自动乐跑时间出错:${error.message}`)
+            return '系统出错,请稍后再试'
+        }
+    }
+
     async create_work_order({ sender, email, title, content }) {
     async create_work_order({ sender, email, title, content }) {
         try {
         try {
             if ([sender, email, title, content].some(value => value === '' || value === null || value === undefined))
             if ([sender, email, title, content].some(value => value === '' || value === null || value === undefined))