-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: automatically record UMO names #9909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from collections import OrderedDict | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from astrbot import logger | ||
| from astrbot.core.umo_alias import get_event_auto_name | ||
|
|
||
| if TYPE_CHECKING: | ||
| from astrbot.core.db import BaseDatabase | ||
| from astrbot.core.platform.astr_message_event import AstrMessageEvent | ||
|
|
||
| MAX_UMO_AUTO_NAME_CACHE_SIZE = 10_000 | ||
|
|
||
|
|
||
| class UmoAutoNameRecorder: | ||
| """Persist changed UMO names without blocking the waking stage.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| db_helper: BaseDatabase | None, | ||
| config_id: str, | ||
| ) -> None: | ||
| """Initialize the bounded cache and background writer state. | ||
|
|
||
| Args: | ||
| db_helper: Database used to persist automatic names. | ||
| config_id: Pipeline configuration identifier used in the task name. | ||
| """ | ||
| self.db_helper = db_helper | ||
| self.config_id = config_id | ||
| self._cache: OrderedDict[str, str] = OrderedDict() | ||
| self._pending: OrderedDict[str, tuple[str, str]] = OrderedDict() | ||
| self._writer_task: asyncio.Task[None] | None = None | ||
|
|
||
| def schedule(self, event: AstrMessageEvent) -> None: | ||
| """Queue a changed automatic name from an awakened event. | ||
|
|
||
| Args: | ||
| event: Awakened event containing the UMO and display metadata. | ||
| """ | ||
| if self.db_helper is None: | ||
| return | ||
|
|
||
| umo = event.unified_msg_origin | ||
| auto_name = get_event_auto_name(event, fallback_to_id=False) | ||
| if not auto_name: | ||
| return | ||
| if self._cache.get(umo) == auto_name: | ||
| self._cache.move_to_end(umo) | ||
| return | ||
|
|
||
| self._cache[umo] = auto_name | ||
| self._cache.move_to_end(umo) | ||
| if len(self._cache) > MAX_UMO_AUTO_NAME_CACHE_SIZE: | ||
| self._cache.popitem(last=False) | ||
|
|
||
| self._pending[umo] = (str(event.get_sender_id() or ""), auto_name) | ||
| self._pending.move_to_end(umo) | ||
| if len(self._pending) > MAX_UMO_AUTO_NAME_CACHE_SIZE: | ||
| dropped_umo, (_, dropped_name) = self._pending.popitem(last=False) | ||
| if self._cache.get(dropped_umo) == dropped_name: | ||
| self._cache.pop(dropped_umo, None) | ||
|
|
||
| if self._writer_task is None or self._writer_task.done(): | ||
| task = asyncio.create_task( | ||
| self._flush(), | ||
| name=f"umo_auto_name_writer:{self.config_id}", | ||
| ) | ||
| self._writer_task = task | ||
| task.add_done_callback(self._on_writer_done) | ||
|
|
||
| async def _flush(self) -> None: | ||
| """Persist queued names sequentially, coalescing changes per UMO.""" | ||
| if self.db_helper is None: | ||
| return | ||
|
|
||
| try: | ||
| while self._pending: | ||
| umo, (creator_sender_id, auto_name) = self._pending.popitem(last=False) | ||
| try: | ||
| await self.db_helper.upsert_umo_auto_name( | ||
| umo=umo, | ||
| creator_sender_id=creator_sender_id, | ||
| auto_name=auto_name, | ||
| ) | ||
| except Exception as exc: | ||
| logger.warning( | ||
| "Failed to persist automatic UMO name for %s: %s", | ||
| umo, | ||
| exc, | ||
| ) | ||
| if umo not in self._pending and self._cache.get(umo) == auto_name: | ||
| self._cache.pop(umo, None) | ||
| finally: | ||
| self._writer_task = None | ||
|
|
||
| @staticmethod | ||
| def _on_writer_done(task: asyncio.Task[None]) -> None: | ||
| """Expose unexpected writer failures. | ||
|
|
||
| Args: | ||
| task: Completed automatic-name writer task. | ||
| """ | ||
| if task.cancelled(): | ||
| return | ||
| exc = task.exception() | ||
| if exc is not None: | ||
| logger.error("UMO automatic-name writer failed.", exc_info=exc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (broader_impact): The initialization migration drops
ix_umo_aliases_umowithout creating the new table-level unique constraint on existing databases. For databases created by the previous schema, that explicit index can be the only uniqueness enforcement forumo; after it is dropped, the newON CONFLICT (umo)upserts fail because SQLite has no matching unique constraint, and duplicate UMO rows can be inserted by other write paths.Triggers: When upgrading an existing database whose
umo_aliasestable was created by the previous schema.Suggested fix: Create or migrate the
uix_umo_alias_umounique constraint before dropping the legacy index, and only remove the old index after verifying that the constraint exists.