Skip to content

feat: add chat history pagination and reasoning blocks - #9667

Open
wcqqq1214 wants to merge 6 commits into
AstrBotDevs:masterfrom
wcqqq1214:feat/9652-chat-history-pagination
Open

feat: add chat history pagination and reasoning blocks#9667
wcqqq1214 wants to merge 6 commits into
AstrBotDevs:masterfrom
wcqqq1214:feat/9652-chat-history-pagination

Conversation

@wcqqq1214

@wcqqq1214 wcqqq1214 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #9652

This PR adds paginated loading for chat session history and lazy-loads reasoning/thinking content so long conversations stay responsive. Older messages load page-by-page as you scroll up instead of one large initial fetch, and the collapsed reasoning block only downloads its body when opened.

Modifications / 改动点

  • Backend: paginate session history (page / page_size / has_more) with boundary validation (default 1000; dashboard uses 50).

  • Strip reasoning bodies from the history payload; expose has_reasoning / reasoning_len markers plus a lazy reasoning-content endpoint.

  • Add a count interface to the DB layer for pagination totals.

  • Dashboard: infinite scroll-up loading for earlier messages in the main session.

  • Render collapsed reasoning entries (think / legacy reasoning part / top-level reasoning) with lazy load and multi-block support.

  • Sync OpenAPI spec, regenerate the TS client, and update scope docs (en / zh).

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

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

Automated

  • uv run pytest -q tests/test_chat_route.py tests/unit/test_chat_history_pagination.py tests/unit/test_openapi_scope_docs.py — 23 passed
  • uv run ruff format . / uv run ruff check . — clean
  • cd dashboard && npx vue-tsc --noEmit — clean

Manual (real SQLite DB, deployed with Docker on an ECS server)

  • History pagination: sessions with 3k / 10k / 50k messages return 50 messages on first load; scrolling to the top loads the previous 50 with stable scroll anchoring and no duplicate requests. page / page_size / total / has_more metadata is accurate at the boundaries (a page beyond the last returns empty, has_more flips to false).
  • Lazy reasoning: all three reasoning shapes (think part / legacy reasoning part / top-level reasoning) are stripped from list responses into has_reasoning / reasoning_len markers; the full body is fetched only on expand via GET /chat/messages/{id}, which returns the unstripped record; deepcopy leaves the stored ORM object untouched (unit-tested).
  • Auth & ownership: API-key and JWT users are isolated; GET /chat/messages/{id} returns a uniform 404 for missing or unauthorized records without leaking existence.
  • Parameter validation: page=0/-1 and page_size=0/1001 are rejected with 422; omitting the parameters keeps the legacy default page_size=1000.
  • Performance: first-screen payload drops from ~3.6 MB to ~182 KB (~20×); a 50k-message session loads page 1 in ~123 ms and page 1000 in ~252 ms — no linear degradation with offset.

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.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Add paginated chat history loading and lazy reasoning retrieval for sessions and threads in the dashboard and API, keeping long conversations responsive while preserving ownership and scope guarantees.

New Features:

  • Introduce page-based history pagination for chat sessions and side threads, including total counts and has_more metadata.
  • Add a dedicated chat message endpoint to fetch full, non-stripped history records with admin-sensitive scope handling.
  • Support lazy loading of reasoning/thinking content in chat messages, with UI hooks to request and display reasoning blocks on demand.

Enhancements:

  • Strip reasoning content from paginated history responses while exposing lightweight markers for client-side hydration.
  • Improve dashboard chat UI with infinite scroll to load earlier messages, anchored scrolling, and inline error/retry handling for history fetches.
  • Extend chat message rendering to handle collapsed reasoning blocks across multiple shapes, including streaming snapshots and media resolution.
  • Add database and manager support for counting platform message history rows per scope to power pagination metadata.
  • Update OpenAPI spec, generated TypeScript client, and scope documentation to cover pagination and the new chat message endpoint.

Tests:

  • Add unit tests covering history serializer stripping behavior, pagination defaults, message route ownership and scope rules, and real pagination/count behavior against SQLite.
  • Extend OpenAPI scope documentation tests to validate sensitive scopes and descriptions for the new chat message endpoint.

Summary by Sourcery

Add paginated chat history loading and lazy reasoning retrieval for dashboard sessions, along with a secure message detail endpoint and updated OpenAPI/docs.

New Features:

  • Introduce page-based history pagination for chat sessions with total counts, has_more flags, and infinite scroll-up loading in the dashboard.
  • Add a dedicated chat message API endpoint to fetch full, non-stripped history records for reasoning content after ownership validation.
  • Support lazy loading of reasoning/thinking blocks in chat messages, including UI affordances to request and expand reasoning on demand.

Enhancements:

  • Strip reasoning content from paginated history responses while exposing lightweight has_reasoning and reasoning_len markers for client-side hydration.
  • Improve dashboard chat behavior with anchored scroll when loading earlier messages, robust error handling, and message-level pagination state.
  • Extend message rendering to handle collapsed reasoning blocks and streaming snapshots while tracking reasoning load status in the UI.
  • Add database and manager support for counting platform message history rows per scope to drive pagination metadata.
  • Update OpenAPI spec, generated TypeScript client, and scope documentation (EN/ZH) to cover pagination and the new message endpoint.

Tests:

  • Add unit tests for history serializer stripping behavior, pagination defaults and boundaries, ownership and scope rules for the message endpoint, and real pagination/count behavior against SQLite.
  • Extend OpenAPI scope documentation tests to validate sensitive scopes and descriptions for the new chat message endpoint.

@wcqqq1214
wcqqq1214 force-pushed the feat/9652-chat-history-pagination branch from 319708e to 837b115 Compare August 14, 2026 13:23
@wcqqq1214
wcqqq1214 marked this pull request as ready for review August 14, 2026 14:40
@dosubot dosubot Bot added size:XL This PR changes 500-999 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 Aug 14, 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 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="dashboard/src/components/chat/Chat.vue" line_range="1739-1748" />
<code_context>
+  const sessionId = currSessionId.value;
+  if (!sessionId || activeSessionPagination.value?.loading) return;
+  const container = messagesContainer.value;
+  const firstMessage = activeMessages.value[0];
+  const firstId = firstMessage?.id == null ? "" : String(firstMessage.id);
+  const beforeTop = firstId
+    ? container?.querySelector<HTMLElement>(
+        `[data-message-id="${CSS.escape(firstId)}"]`,
+      )?.getBoundingClientRect().top
+    : undefined;
+  suppressAutoScroll.value = true;
+  try {
+    await loadEarlierMessages(sessionId);
+    if (currSessionId.value !== sessionId) return;
+    await nextTick();
+    if (beforeTop == null || !container || !firstId) return;
+    const row = container.querySelector<HTMLElement>(
+      `[data-message-id="${CSS.escape(firstId)}"]`,
+    );
</code_context>
<issue_to_address>
**suggestion:** Anchoring scroll to the first message can fail silently when the first row has no stable ID.

In `loadEarlierWithAnchor`, anchoring relies on `activeMessages.value[0].id`. If that message has `id == null` or its DOM row is missing, `beforeTop` remains undefined and the function exits after loading without restoring scroll, causing a jump when paging. You could instead fall back to the current `scrollTop` when `firstId` is falsy, or anchor on the first message in `activeMessages.value` with a non-null `id`.

Suggested implementation:

```
async function loadEarlierWithAnchor() {
  const sessionId = currSessionId.value;
  if (!sessionId || activeSessionPagination.value?.loading) return;
  const container = messagesContainer.value;

  const firstMessage = activeMessages.value[0];
  const anchorMessage =
    activeMessages.value.find((message) => message.id != null) ?? firstMessage;

  const firstId =
    anchorMessage?.id == null ? "" : String(anchorMessage.id);

  let beforeTop: number | undefined;
  if (firstId) {
    const row = container?.querySelector<HTMLElement>(
      `[data-message-id="${CSS.escape(firstId)}"]`,
    );
    beforeTop = row?.getBoundingClientRect().top;
  } else if (container) {
    // Fall back to the current scrollTop when the first row has no stable id
    beforeTop = container.scrollTop;
  }

  suppressAutoScroll.value = true;

  const target = activeReasoningTarget.value;

```

The existing logic after this snippet that:
1. Calls `await loadEarlierMessages(sessionId);`
2. Awaits `nextTick();`
3. Reads the DOM row for `firstId` and adjusts scroll based on the change in `getBoundingClientRect().top`

needs to be updated to:
- Use the `beforeTop` computed above (which may come from `scrollTop` when `firstId` is falsy).
- If `firstId` is truthy, continue to anchor using the corresponding `[data-message-id="..."]` row and adjust `scrollTop` by the delta in `top`.
- If `firstId` is falsy, skip querying by `data-message-id` and instead restore `container.scrollTop` to `beforeTop` after paging, ensuring scroll doesn’t jump when the first row has no stable id.
</issue_to_address>

### Comment 2
<location path="astrbot/dashboard/api/chat.py" line_range="157" />
<code_context>
+    )
+
+
+@router.get(
+    "/chat/messages/{message_id}",
+    openapi_extra={"x-astrbot-sensitive-scopes": [CHAT_ADMIN_SCOPE]},
</code_context>
<issue_to_address>
**issue (complexity):** Consider extracting the username resolution and admin-id filtering logic into dedicated helper functions to simplify the route’s control flow.

You can reduce the route’s complexity by extracting the username resolution and admin-id rejection into small helpers. This keeps behavior identical but makes the control flow easier to follow and test.

For example:

```python
def _resolve_effective_username(
    auth: AuthContext,
    username: str | None,
    service: ChatService,
) -> str:
    if auth.via != "api_key":
        return auth.username

    effective_username = str(username or "").strip()
    if not effective_username:
        raise ApiError("Message not found", status_code=404)

    if "*" in auth.scopes or CHAT_ADMIN_SCOPE in auth.scopes:
        return effective_username

    if _is_reserved_admin_id(effective_username, service):
        raise ApiError("Message not found", status_code=404)

    return effective_username


def _is_reserved_admin_id(effective_username: str, service: ChatService) -> bool:
    configs = getattr(
        getattr(service, "core_lifecycle", None),
        "astrbot_config_mgr",
        None,
    )
    for config in getattr(configs, "confs", {}).values():
        admin_ids = config.get("admins_id", []) if isinstance(config, dict) else []
        if any(str(admin_id) == effective_username for admin_id in admin_ids):
            return True
    return False
```

Then the route becomes a straight-line flow:

```python
@router.get(
    "/chat/messages/{message_id}",
    openapi_extra={"x-astrbot-sensitive-scopes": [CHAT_ADMIN_SCOPE]},
)
async def get_chat_message(
    message_id: int = Path(..., gt=0),
    username: str | None = Query(default=None),
    auth: AuthContext = Depends(require_chat_scope),
    service: ChatService = Depends(get_service),
):
    effective_username = _resolve_effective_username(auth, username, service)
    try:
        return ok(await service.get_message(effective_username, message_id))
    except ChatServiceError as exc:
        raise ApiError("Message not found", status_code=404) from exc
```

This keeps all current semantics (including the 404-on-any-failure behavior and admin ID restriction) but moves the nested logic out of the route, making it easier to understand and maintain.
</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 dashboard/src/components/chat/Chat.vue
Comment thread astrbot/dashboard/api/chat.py
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:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] webchat hang when meet long conversation

1 participant