feat(utils): show weekday in date and datetime prompt variables (#9362)

* feat(utils): 优化日期时间变量替换格式

为 {{date}} 和 {{datetime}} 变量替换添加更详细的格式选项,包括星期、年月日和时间信息

* test(prompt): 更新测试中日期时间的本地化格式
This commit is contained in:
Phantom 2025-08-21 10:03:07 +08:00 committed by GitHub
parent 0e750c64db
commit a671f95bee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 5 deletions

View File

@ -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.

View File

@ -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)
}