From 0dd31c945809bfbdaff45c9429196a9f636fa0ff Mon Sep 17 00:00:00 2001 From: Jerrypluay Date: Mon, 24 Nov 2025 15:57:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(aiCaller.js):=20add=20method?= =?UTF-8?q?=20to=20calculate=20time=20difference=20for=20memory=20timestam?= =?UTF-8?q?ps=20in=20context=20introduction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ai/aiCaller.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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'; }