如何为Copilot应用加上Token的限制

更新时间:2025-03-21 02:45:01

给 Copilot 增加问答 token 限制

关键技术点

image

  1. 发送消息配置执行脚本

  2. return 返回 true (继续发送消息), false(打断发送的消息)

具体搭建

内置数据库创建

image

copilot_token

user_id

text

token

integer

day

date

页面搭建

MAX_TOKEN
{{10000 * 4 * 500}}

image

find_user_token
SELECT SUM(token) FROM copilot_token WHERE user_id = {{mobi.currentUser.id}} AND day = {{now_day.value}}::date

image

add_token
INSERT INTO copilot_token(user_id,token,day)
VALUES({{mobi.currentUser.id}},{{token}},{{now_day.value}}::date)

image

now_day
function formatDateTime(date = new Date()) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
    const day = String(date.getDate()).padStart(2, '0');
    // 返回你想要的格式,这里以 "YYYY-MM-DD HH:mm:ss" 为例
    return `${year}-${month}-${day}`;
}

return formatDateTime();

image

发送消息事件
var token = await find_user_token.trigger();

if(token.data > MAX_TOKEN.value ){
  mobi.showMessage("您今日Token数已用完")
  return false;
}

return true;

image

  • 本页导读
  • 关键技术点
  • 具体搭建
  • 内置数据库创建
  • 页面搭建