diff --git a/lib/ai/aiCaller.js b/lib/ai/aiCaller.js index c9b46ba..44e2464 100644 --- a/lib/ai/aiCaller.js +++ b/lib/ai/aiCaller.js @@ -96,6 +96,32 @@ class AiCaller { return fullPrompt; } + /** + * 计算时间差 + * @param pastTime 过去时间戳 + * @returns {string} 时间差字符串 + */ + calculateTimeDifference(pastTime) { + const now = Date.now(); + const diff = now - pastTime; + + const days = Math.floor(diff / (1000 * 60 * 60 * 24)); + const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); + + let result = ''; + if (days > 0) { + result += `${days}天`; + } + if (hours > 0) { + result += `${hours}小时`; + } + if (minutes > 0) { + result += `${minutes}分钟`; + } + return result || '刚刚'; + } + /** * 获取系统提示词 * @param {object} e 上下文事件对象 @@ -140,7 +166,8 @@ class AiCaller { if (memories && memories.length > 0) { contextIntro += '你可能会用到的记忆,请按情况使用,如果不合语境请忽略,请结合记忆时间和当前时间智能判断:\n'; memories.forEach((memory, index) => { - contextIntro += `${index + 1}. 关键词:${memory.keywords},内容:${memory.data},记忆创建时间:${memory.createdAt}\n`; + const timeDiff = this.calculateTimeDifference(memory.createdAt); + contextIntro += `${index + 1}. 关键词:${memory.keywords},内容:${memory.data},记忆创建时间:${memory.createdAt},距离现在:${timeDiff}\\n`; }); contextIntro += '\n'; }