Skip to content

feat: add datetime prompt scope option#9190

Closed
911218sky wants to merge 3 commits into
AstrBotDevs:masterfrom
911218sky:datetime-system-prompt-scope
Closed

feat: add datetime prompt scope option#9190
911218sky wants to merge 3 commits into
AstrBotDevs:masterfrom
911218sky:datetime-system-prompt-scope

Conversation

@911218sky

@911218sky 911218sky commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Refs #9189

Modifications / 改动点

  • Add provider_settings.datetime_system_prompt_scope with two modes:

    • history: keep the existing behavior and write datetime reminders into chat history.
    • current: include the latest datetime only in the current request and mark it as temporary so it is not saved into history.
  • Keep the default as history to avoid changing existing behavior.

  • Add dashboard config metadata and zh-CN/en-US translations for the new option.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. labels Jul 8, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • When reading datetime_system_prompt_scope, consider providing an explicit default (e.g. cfg.get("datetime_system_prompt_scope", "history")) so older configs or missing keys keep the existing behavior instead of silently switching to temporary prompts.
  • The has_datetime flag currently drives whether the entire <system_reminder> block is temporary; if additional non-datetime reminders are added later, this may unintentionally make them temporary as well—consider scoping the temp behavior to just the datetime part or documenting this coupling clearly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- When reading `datetime_system_prompt_scope`, consider providing an explicit default (e.g. `cfg.get("datetime_system_prompt_scope", "history")`) so older configs or missing keys keep the existing behavior instead of silently switching to temporary prompts.
- The `has_datetime` flag currently drives whether the entire `<system_reminder>` block is temporary; if additional non-datetime reminders are added later, this may unintentionally make them temporary as well—consider scoping the temp behavior to just the datetime part or documenting this coupling clearly.

## Individual Comments

### Comment 1
<location path="astrbot/core/astr_main_agent.py" line_range="934-937" />
<code_context>
     timezone: str | None,
 ) -> None:
     system_parts: list[str] = []
+    has_datetime = False
     if cfg.get("identifier"):
         user_id = event.message_obj.sender.user_id
</code_context>
<issue_to_address>
**issue (bug_risk):** Temporary scope for datetime also affects other system reminders bundled in the same TextPart.

When `datetime_system_prompt_scope` is not `"history"`, the entire `<system_reminder>` is placed in a single temp `TextPart`. As a result, identifier and any other reminders become temporary too and are not persisted, even though the toggle is meant to control only the datetime. If only the datetime should be temporary, consider splitting it into its own `TextPart` (or otherwise separating system reminder components) so the temp flag applies only to the datetime content.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread astrbot/core/astr_main_agent.py Outdated

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new configuration option, datetime_system_prompt_scope, allowing users to choose whether the current datetime system prompt is saved to the chat history or only included in the current request to improve prompt caching. The review feedback correctly identifies a critical issue where setting the scope to 'current' inadvertently bundles all system reminders together and marks the entire block as temporary, which incorrectly excludes other persistent reminders (like user identifiers) from the chat history. The reviewer provides a solid suggestion to separate the datetime reminder from other system reminders so that only the datetime reminder is marked as temporary.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/astr_main_agent.py Outdated
timezone: str | None,
) -> None:
system_parts: list[str] = []
has_datetime = False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Initialize datetime_part as None instead of using a boolean flag has_datetime. This will allow us to handle the datetime reminder separately from other persistent system reminders (like user identifier and group name) when the scope is set to 'current'.

Suggested change
has_datetime = False
datetime_part: str | None = None

Comment on lines 960 to +974
current_time = now.strftime("%Y-%m-%d %H:%M (%Z)")
weekday = WEEKDAY_NAMES[now.weekday()]
system_parts.append(f"Current datetime: {current_time}, Weekday: {weekday}")
has_datetime = True

if system_parts:
system_content = (
"<system_reminder>" + "\n".join(system_parts) + "</system_reminder>"
)
req.extra_user_content_parts.append(TextPart(text=system_content))
system_part = TextPart(text=system_content)
req.extra_user_content_parts.append(
system_part
if not has_datetime or cfg.get("datetime_system_prompt_scope") == "history"
else system_part.mark_as_temp()
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When datetime_system_prompt_scope is set to 'current', the current implementation bundles all system reminders (including user identifier and group name display) into a single system_content block and marks the entire block as temporary (mark_as_temp()). This causes other persistent reminders to also be excluded from the chat history, which is unintended.

To fix this, keep the datetime reminder separate from other system reminders when the scope is 'current', and only mark the datetime reminder as temporary.

        current_time = now.strftime("%Y-%m-%d %H:%M (%Z)")
        weekday = WEEKDAY_NAMES[now.weekday()]
        datetime_part = f"Current datetime: {current_time}, Weekday: {weekday}"

    if datetime_part and cfg.get("datetime_system_prompt_scope") == "history":
        system_parts.append(datetime_part)
        datetime_part = None

    if system_parts:
        system_content = (
            "<system_reminder>" + "\n".join(system_parts) + "</system_reminder>"
        )
        req.extra_user_content_parts.append(TextPart(text=system_content))

    if datetime_part:
        datetime_content = (
            "<system_reminder>" + datetime_part + "</system_reminder>"
        )
        req.extra_user_content_parts.append(TextPart(text=datetime_content).mark_as_temp())

@911218sky 911218sky force-pushed the datetime-system-prompt-scope branch from 6ac5867 to 19279b6 Compare July 8, 2026 19:57
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 8, 2026
@911218sky

Copy link
Copy Markdown
Contributor Author

Addressed the review feedback in 19279b64d:

  • datetime_system_prompt_scope now uses cfg.get("datetime_system_prompt_scope", "history"), so older configs keep the existing history behavior.
  • datetime reminders are separated from other system reminders when scope is current; only the datetime reminder is marked temporary (_no_save). Identifier/group reminders remain persistent.
  • Re-ran py_compile, ruff, JSON validation, and a behavior check covering default/history/current+identifier cases.

@911218sky 911218sky closed this Jul 9, 2026
@911218sky 911218sky deleted the datetime-system-prompt-scope branch July 9, 2026 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant