From a671f95bee1abb22e2ab0b62bb718295cd6cbb53 Mon Sep 17 00:00:00 2001 From: Phantom <59059173+EurFelux@users.noreply.github.com> Date: Thu, 21 Aug 2025 10:03:07 +0800 Subject: [PATCH] feat(utils): show weekday in date and datetime prompt variables (#9362) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(utils): 优化日期时间变量替换格式 为 {{date}} 和 {{datetime}} 变量替换添加更详细的格式选项,包括星期、年月日和时间信息 * test(prompt): 更新测试中日期时间的本地化格式 --- .../src/utils/__tests__/prompt.test.ts | 24 ++++++++++++++++--- src/renderer/src/utils/prompt.ts | 17 +++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/utils/__tests__/prompt.test.ts b/src/renderer/src/utils/__tests__/prompt.test.ts index ab4be618c9..6d990c16eb 100644 --- a/src/renderer/src/utils/__tests__/prompt.test.ts +++ b/src/renderer/src/utils/__tests__/prompt.test.ts @@ -133,7 +133,15 @@ describe('prompt', () => { const result = await replacePromptVariables(userPrompt, assistant.model?.name) const expectedPrompt = ` 以下是一些辅助信息: - - 日期和时间: ${mockDate.toLocaleString()}; + - 日期和时间: ${mockDate.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'numeric', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric' + })}; - 操作系统: macOS; - 中央处理器架构: darwin64; - 语言: zh-CN; @@ -176,7 +184,12 @@ describe('prompt', () => { basePrompt = await replacePromptVariables(initialPrompt, assistant.model?.name) expectedBasePrompt = ` System Information: - - Date: ${mockDate.toLocaleDateString()} + - Date: ${mockDate.toLocaleDateString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'numeric', + day: 'numeric' + })} - User: MockUser Instructions: Be helpful. @@ -239,7 +252,12 @@ describe('prompt', () => { const basePrompt = await replacePromptVariables(initialPrompt, assistant.model?.name) const expectedBasePrompt = ` System Information: - - Date: ${mockDate.toLocaleDateString()} + - Date: ${mockDate.toLocaleDateString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'numeric', + day: 'numeric' + })} - User: MockUser Instructions: Be helpful. diff --git a/src/renderer/src/utils/prompt.ts b/src/renderer/src/utils/prompt.ts index 4f7b9d5408..85766953ba 100644 --- a/src/renderer/src/utils/prompt.ts +++ b/src/renderer/src/utils/prompt.ts @@ -176,7 +176,12 @@ export const replacePromptVariables = async (userSystemPrompt: string, modelName const now = new Date() if (userSystemPrompt.includes('{{date}}')) { - const date = now.toLocaleDateString() + const date = now.toLocaleDateString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'numeric', + day: 'numeric' + }) userSystemPrompt = userSystemPrompt.replace(/{{date}}/g, date) } @@ -186,7 +191,15 @@ export const replacePromptVariables = async (userSystemPrompt: string, modelName } if (userSystemPrompt.includes('{{datetime}}')) { - const datetime = now.toLocaleString() + const datetime = now.toLocaleString(undefined, { + weekday: 'short', + year: 'numeric', + month: 'numeric', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric' + }) userSystemPrompt = userSystemPrompt.replace(/{{datetime}}/g, datetime) }