generated from exteraGramPlugins/pluginBase
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b3a4bece3 | ||
|
|
f58e157b70 | ||
|
|
b885f2179f | ||
|
|
af23d2e682 | ||
|
|
16bf8de84c | ||
|
|
b2105ca753 |
No files matched your search
@@ -1,151 +1,44 @@
|
|||||||
# exteraGram Plugin Base
|
<p align="center">
|
||||||
|
<img src="images/icon.svg" width="128" height="128" alt="Chat Summaries icon">
|
||||||
|
</p>
|
||||||
|
|
||||||
A build toolchain and ADB automation for writing [exteraGram](https://exteragram.app) plugins
|
<h1 align="center">Chat Summaries</h1>
|
||||||
as ordinary multi-file Python projects instead of hand-maintaining one giant `.py` file.
|
|
||||||
|
|
||||||
## How exteraGram plugins actually work
|
**TL;DR:** an [exteraGram](https://exteragram.app) plugin that summarizes the last N
|
||||||
|
messages of any chat, group, channel or topic with AI, right inside the chat.
|
||||||
|
|
||||||
A plugin, as exteraGram loads it, is exactly **one Python file** on the device:
|
## Description
|
||||||
`/data/user/0/com.exteragram.messenger/files/plugins/<plugin_id>.py`. The distribution
|
|
||||||
convention names the same content `<id>.plugin`.
|
|
||||||
|
|
||||||
The app reads plugin metadata **statically via AST** — it looks for top-level constant
|
Chat Summaries sends a chat's recent history to any OpenAI-compatible endpoint (OpenAI,
|
||||||
assignments (`__id__`, `__name__`, `__description__`, `__author__`, `__version__`,
|
OpenRouter, a local Ollama, …) and streams the answer into a summary card placed after the
|
||||||
`__icon__`, `__app_version__`, `__sdk_version__`, `__requirements__`) without executing the
|
newest summarized message. Summaries are saved on the device and come back whenever the chat
|
||||||
file. Only `__id__` and `__name__` are required.
|
is reopened.
|
||||||
|
|
||||||
The file must define exactly one class inheriting `BasePlugin` (`from base_plugin import
|
## Features
|
||||||
BasePlugin`). Lifecycle methods (`on_plugin_load`, `on_plugin_unload`, `create_settings`) and
|
|
||||||
hooks (`on_send_message_hook`, `pre_request_hook`, `post_request_hook`, `on_update_hook(s)`,
|
|
||||||
`on_app_event`) are all called by the host; hooks must be registered inside
|
|
||||||
`on_plugin_load` (e.g. `self.add_on_send_message_hook()`). A hook returns a
|
|
||||||
`HookResult(strategy=HookStrategy.MODIFY, params=params)` to apply changes, or a bare
|
|
||||||
`HookResult()` to pass through. Settings rows returned from `create_settings` come from
|
|
||||||
`ui.settings` (`Header`, `Input`, `Switch`, `Text`, `Selector`, `Divider`) and persist through
|
|
||||||
`self.get_setting`/`self.set_setting`. Logging goes through `android_utils.log`; Chaquopy also
|
|
||||||
redirects `sys.stdout`/`sys.stderr` to the logcat tags `python.stdout`/`python.stderr`.
|
|
||||||
|
|
||||||
This repo does not use the official Elyx/ElyxBuilder archive format — see
|
- **Summarize any chat.** ⋮ → Plugins → Summarize, or hold the unread button to summarize
|
||||||
`docs/sdk-notes.md` for why.
|
the unread messages. Pick how many messages (1–1000) and an offset to skip the newest ones.
|
||||||
|
- **Streaming summary card.** Reasoning and answer stream into a card in the chat, which
|
||||||
|
jumps to it once. The finished summary ends with an `#aisummaries` hashtag.
|
||||||
|
- **Any OpenAI-compatible endpoint.** Set the base URL, API key (optional, e.g. for local
|
||||||
|
Ollama) and model in the settings.
|
||||||
|
- **Custom prompts.** A default prompt, with per-chat overrides.
|
||||||
|
- **Anonymized transcript.** Senders are sent to the model as numbers, not names; attached
|
||||||
|
file names are included only if you enable it.
|
||||||
|
- **Saved locally.** Summaries are restored when the chat is reopened. Delete a card like any
|
||||||
|
message to forget it; the settings show how many are saved and how much space they take,
|
||||||
|
and offer *Clear all summaries*.
|
||||||
|
- **Clean failures.** If anything fails before the answer starts, you only get a bulletin
|
||||||
|
and the chat is left untouched.
|
||||||
|
|
||||||
## Repo layout
|
Summaries exist only on this device, so Telegram's search, which runs on the server, does
|
||||||
|
not find them, not even by `#aisummaries`.
|
||||||
|
|
||||||
```
|
## Documentation
|
||||||
plugin.json single source of truth for plugin metadata
|
|
||||||
src/ the plugin, written as an ordinary Python package
|
|
||||||
main.py entry module — exactly one BasePlugin subclass, absolute imports only
|
|
||||||
config.py setting keys, defaults and tunables
|
|
||||||
diagnostics.py bounded in-memory log, mirrored to a file (logcat is restricted here)
|
|
||||||
providers/ endpoint settings, prompt assembly and SSE streaming
|
|
||||||
services/ history fetch, anonymized transcript, markdown→RichMessage, injection, SQLite store
|
|
||||||
hooks/ Xposed hooks (unread-button entry points, summary restore on chat open)
|
|
||||||
ui/ summarize dialog, bulletin and theme helpers
|
|
||||||
tools/
|
|
||||||
bundle.py compiles plugin.json + src/ into dist/<id>.plugin
|
|
||||||
devclient.py speaks the on-device dev-server protocol (127.0.0.1:42690)
|
|
||||||
verify_bundle.py offline harness that proves a built bundle behaves correctly, no phone needed
|
|
||||||
release.py publishes a built bundle to a Gitea release (used by CI)
|
|
||||||
.gitea/workflows/ CI that builds, verifies and publishes the .plugin to Releases
|
|
||||||
scripts/ bash automation for the ADB debug loop (see below)
|
|
||||||
docs/sdk-notes.md condensed SDK API surface gathered from the official docs
|
|
||||||
```
|
|
||||||
|
|
||||||
## The three commands that matter
|
- [How Chat Summaries works](docs/how-it-works.md): the summarize flow step by step
|
||||||
|
- [Development](docs/development.md): getting started, repo layout, building and verifying, icon export
|
||||||
```bash
|
- [How exteraGram plugins work](docs/how-plugins-work.md): the plugin format and lifecycle
|
||||||
python3 tools/bundle.py --release # compile src/ -> dist/<id>.plugin
|
- [Device setup and ADB automation](docs/device-and-adb.md): deploy, watch, logs, debugger
|
||||||
python3 tools/verify_bundle.py dist/<id>.plugin # offline proof the bundle works, no device needed
|
- [Versioning and releases](docs/versioning-and-releases.md): git hooks, auto-bump, CI publishing
|
||||||
bash scripts/deploy.sh --enable # build (--dev) + push to a connected device + enable + reload
|
- [SDK notes](docs/sdk-notes.md): condensed exteraGram SDK API surface
|
||||||
```
|
|
||||||
|
|
||||||
`bundle.py` walks `src/` recursively, embeds every `.py` file's source and dotted module name
|
|
||||||
into a compressed, base64-encoded payload, and appends a small runtime preamble that installs
|
|
||||||
a private import hook (scoped to a per-module `__builtins__` dict, never touching
|
|
||||||
`sys.modules`) so cross-module and cross-package imports resolve exactly as they would if the
|
|
||||||
files were still on disk. `--dev` (the default) embeds absolute local paths so a debugger
|
|
||||||
resolves breakpoints against your real source files; `--release` embeds repo-relative paths
|
|
||||||
for distribution. Both modes produce byte-reproducible output for a given `src/` + `plugin.json`.
|
|
||||||
|
|
||||||
## Device prerequisites
|
|
||||||
|
|
||||||
- exteraGram 12.5.1+.
|
|
||||||
- In-app: Preferences → Plugins → enable the plugin engine, then enable developer mode.
|
|
||||||
- USB debugging enabled, device authorized (`adb devices`).
|
|
||||||
|
|
||||||
Release-build APKs disallow `adb run-as`, so you cannot `adb push` a file straight into the
|
|
||||||
app's private plugin directory. The on-device **dev server** (`tools/devclient.py`,
|
|
||||||
`scripts/deploy.sh`) is the actual install path — it talks to the running app over a TCP
|
|
||||||
socket forwarded with `adb forward`, and the app writes the file into its own storage.
|
|
||||||
|
|
||||||
## ADB automation
|
|
||||||
|
|
||||||
All scripts live in `scripts/`, are runnable from any working directory, and honor the
|
|
||||||
standard `ANDROID_SERIAL` env var for device selection.
|
|
||||||
|
|
||||||
- `scripts/adb-connect.sh [--debug]` — starts adb, waits for the device, (re)creates the
|
|
||||||
`tcp:42690` port forward (and `tcp:5678` with `--debug`), pings the dev server.
|
|
||||||
- `scripts/deploy.sh [--release] [--enable] [--no-connect]` — bundles, connects, writes the
|
|
||||||
plugin to the device, optionally enables it, reloads it.
|
|
||||||
- `scripts/watch.sh [deploy args…]` — watches `src/` and `plugin.json`, redeploys on change
|
|
||||||
(uses `inotifywait` if available, otherwise polls).
|
|
||||||
- `scripts/logs.sh [-c] [-t] [-g PATTERN]` — streams logcat for the running app (`-t` for the
|
|
||||||
raw Chaquopy stdio tags, `-g` to grep, `-c` to clear first).
|
|
||||||
- `scripts/debug.sh start|stop [--platform vscode|pycharm]` — forwards the debugger port and
|
|
||||||
tells the dev server to start/stop the in-app debugger. Requires `debugpy` (vscode) or
|
|
||||||
`pydevd-pycharm` (pycharm) present in the app's Python runtime — add it to `requirements` in
|
|
||||||
`plugin.json` if it's missing.
|
|
||||||
- `scripts/fetch-sdk-stubs.sh [--beta]` — downloads the latest SDK type stubs into `stubs/`
|
|
||||||
for editor autocomplete.
|
|
||||||
|
|
||||||
## Publishing a release
|
|
||||||
|
|
||||||
`.gitea/workflows/release.yml` builds, verifies and publishes the `.plugin` to the repo's
|
|
||||||
Releases tab on every push to `main`. `dist/` is gitignored, so the artifact is always built
|
|
||||||
by CI rather than committed.
|
|
||||||
|
|
||||||
The `version` field in `plugin.json` is the single source of truth:
|
|
||||||
|
|
||||||
| Push to `main` with… | Result |
|
|
||||||
| --- | --- |
|
|
||||||
| a new `version` | creates tag `v<version>` and a new release |
|
|
||||||
| the same `version` | rebuilds the existing release, replacing its attachment |
|
|
||||||
|
|
||||||
So every push leaves a downloadable build attached to the current version, and bumping
|
|
||||||
`version` is what cuts a new release. Pushing a `v*` tag by hand still works and uses that
|
|
||||||
tag verbatim, as does a manual run (Actions → Publish plugin → Run workflow).
|
|
||||||
|
|
||||||
Every path runs `--check`, builds with `--release`, and gates the upload on
|
|
||||||
`verify_bundle.py`, so a bundle that fails verification is never published. A rebuild
|
|
||||||
replaces the attachment rather than piling up duplicates, and refreshes the release notes to
|
|
||||||
name the commit it came from. The tag itself is never moved: it keeps pointing at the commit
|
|
||||||
that first introduced that version.
|
|
||||||
|
|
||||||
Authentication uses `secrets.API_TOKEN`, an organisation-level Actions secret holding a user
|
|
||||||
token with the `write:repository` scope (which covers releases). Set it once under
|
|
||||||
**Org → Settings → Actions → Secrets** and every repo in the organisation inherits it; a
|
|
||||||
repo-level secret of the same name takes precedence if one repo ever needs different
|
|
||||||
credentials.
|
|
||||||
|
|
||||||
The workflow passes it to `release.py` as `RELEASE_TOKEN`, **not** `GITEA_TOKEN`. The Actions
|
|
||||||
runner injects its own `GITEA_TOKEN` into every step, and that value overrides a step-level
|
|
||||||
`env:` entry of the same name, so the org secret would be silently ignored.
|
|
||||||
|
|
||||||
If the org secret does not reach a repository the publish still works: `release.py` tries
|
|
||||||
`RELEASE_TOKEN` first and then falls back to the runner's `GITEA_TOKEN`, which can publish
|
|
||||||
releases in its own repository, and prints which one it used. An organisation secret is only
|
|
||||||
exposed to a repository that has Actions enabled, so check **Repo → Settings → Actions** if a
|
|
||||||
repo unexpectedly falls back.
|
|
||||||
|
|
||||||
If you would rather not manage a token at all, that injected `GITEA_TOKEN` can create
|
|
||||||
releases in its own repo: pass `--token-env GITEA_TOKEN` and drop the org secret. It is
|
|
||||||
scoped to the single job and expires with it, so it is the safer default; `API_TOKEN` is what
|
|
||||||
you want when publishing needs to reach beyond this repo.
|
|
||||||
|
|
||||||
## Writing your own plugin
|
|
||||||
|
|
||||||
1. Edit `plugin.json` (id, name, description, etc.).
|
|
||||||
2. Write your plugin under `src/`, with `src/main.py` as the entry module (absolute imports
|
|
||||||
only — it's executed at plugin-module top level, not inside a package).
|
|
||||||
3. `python3 tools/bundle.py --release && python3 tools/verify_bundle.py dist/<id>.plugin`
|
|
||||||
(substitute your `id` from `plugin.json`).
|
|
||||||
4. `bash scripts/deploy.sh --enable` with a device connected.
|
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# Development
|
||||||
|
|
||||||
|
## Writing your own plugin
|
||||||
|
|
||||||
|
1. Run `python3 tools/bundle.py --check` once. It enables the git hooks that auto-bump the
|
||||||
|
version on commit (see [Git hooks and versioning](versioning-and-releases.md#git-hooks-and-versioning)).
|
||||||
|
2. Edit `plugin.json` (id, name, description, etc.).
|
||||||
|
3. Write your plugin under `src/`, with `src/main.py` as the entry module (absolute imports
|
||||||
|
only — it's executed at plugin-module top level, not inside a package).
|
||||||
|
4. `python3 tools/bundle.py --release && python3 tools/verify_bundle.py dist/<id>.plugin`
|
||||||
|
(substitute your `id` from `plugin.json`).
|
||||||
|
5. `bash scripts/deploy.sh --enable` with a device connected.
|
||||||
|
|
||||||
|
## Repo layout
|
||||||
|
|
||||||
|
```
|
||||||
|
plugin.json single source of truth for plugin metadata
|
||||||
|
src/ the plugin, written as an ordinary Python package
|
||||||
|
main.py entry module — exactly one BasePlugin subclass, absolute imports only
|
||||||
|
config.py setting keys, defaults and tunables
|
||||||
|
diagnostics.py bounded in-memory log, mirrored to a file (logcat is restricted here)
|
||||||
|
providers/ endpoint settings, prompt assembly and SSE streaming
|
||||||
|
services/ history fetch, anonymized transcript, markdown→RichMessage, injection, SQLite store
|
||||||
|
hooks/ Xposed hooks (unread-button entry points, summary restore on chat open,
|
||||||
|
forgetting a summary when its card is deleted)
|
||||||
|
ui/ summarize dialog, bulletin and theme helpers
|
||||||
|
tools/
|
||||||
|
bundle.py compiles plugin.json + src/ into dist/<id>.plugin
|
||||||
|
devclient.py speaks the on-device dev-server protocol (127.0.0.1:42690)
|
||||||
|
verify_bundle.py offline harness that proves a built bundle behaves correctly, no phone needed
|
||||||
|
release.py publishes a built bundle to a Gitea release (used by CI)
|
||||||
|
icon_png.py renders images/icon.svg to a square PNG
|
||||||
|
images/icon.svg the plugin icon, source of truth for any PNG export
|
||||||
|
.gitea/workflows/ CI that builds, verifies and publishes the .plugin to Releases
|
||||||
|
scripts/ ADB debug-loop automation and the git hooks
|
||||||
|
docs/ these notes + sdk-notes.md, the condensed SDK API surface
|
||||||
|
```
|
||||||
|
|
||||||
|
## The three commands that matter
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 tools/bundle.py --release # compile src/ -> dist/<id>.plugin
|
||||||
|
python3 tools/verify_bundle.py dist/<id>.plugin # offline proof the bundle works, no device needed
|
||||||
|
bash scripts/deploy.sh --enable # build (--dev) + push to a connected device + enable + reload
|
||||||
|
```
|
||||||
|
|
||||||
|
`bundle.py` walks `src/` recursively, embeds every `.py` file's source and dotted module name
|
||||||
|
into a compressed, base64-encoded payload, and appends a small runtime preamble that installs
|
||||||
|
a private import hook (scoped to a per-module `__builtins__` dict, never touching
|
||||||
|
`sys.modules`) so cross-module and cross-package imports resolve exactly as they would if the
|
||||||
|
files were still on disk. `--dev` (the default) embeds absolute local paths so a debugger
|
||||||
|
resolves breakpoints against your real source files; `--release` embeds repo-relative paths
|
||||||
|
for distribution. Both modes produce byte-reproducible output for a given `src/` + `plugin.json`.
|
||||||
|
Its first run in a clone also enables the repo's git hooks (see
|
||||||
|
[Git hooks and versioning](versioning-and-releases.md#git-hooks-and-versioning)).
|
||||||
|
|
||||||
|
## Exporting the icon as PNG
|
||||||
|
|
||||||
|
`tools/icon_png.py` renders `images/icon.svg` to `images/icon.png`, 512x512 by default.
|
||||||
|
`--resolution` sets the size of both sides, so `--resolution 1024` gives 1024x1024:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 tools/icon_png.py # images/icon.png, 512x512
|
||||||
|
python3 tools/icon_png.py --resolution 1024 # images/icon.png, 1024x1024
|
||||||
|
python3 tools/icon_png.py some.svg -o out.png
|
||||||
|
```
|
||||||
|
|
||||||
|
The output is always PNG; any other `-o` extension is rejected. The script itself is stdlib
|
||||||
|
only and hands the rendering to the first renderer it finds: the `cairosvg` Python package,
|
||||||
|
`rsvg-convert` (librsvg), ImageMagick or Inkscape. Install any one of them.
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# Device setup and ADB automation
|
||||||
|
|
||||||
|
## Device prerequisites
|
||||||
|
|
||||||
|
- exteraGram 12.5.1+.
|
||||||
|
- In-app: Preferences → Plugins → enable the plugin engine, then enable developer mode.
|
||||||
|
- USB debugging enabled, device authorized (`adb devices`).
|
||||||
|
|
||||||
|
Release-build APKs disallow `adb run-as`, so you cannot `adb push` a file straight into the
|
||||||
|
app's private plugin directory. The on-device **dev server** (`tools/devclient.py`,
|
||||||
|
`scripts/deploy.sh`) is the actual install path — it talks to the running app over a TCP
|
||||||
|
socket forwarded with `adb forward`, and the app writes the file into its own storage.
|
||||||
|
|
||||||
|
## ADB automation
|
||||||
|
|
||||||
|
All scripts live in `scripts/`, are runnable from any working directory, and honor the
|
||||||
|
standard `ANDROID_SERIAL` env var for device selection.
|
||||||
|
|
||||||
|
- `scripts/adb-connect.sh [--debug]` — starts adb, waits for the device, (re)creates the
|
||||||
|
`tcp:42690` port forward (and `tcp:5678` with `--debug`), pings the dev server.
|
||||||
|
- `scripts/deploy.sh [--release] [--enable] [--no-connect]` — bundles, connects, writes the
|
||||||
|
plugin to the device, optionally enables it, reloads it.
|
||||||
|
- `scripts/watch.sh [deploy args…]` — watches `src/` and `plugin.json`, redeploys on change
|
||||||
|
(uses `inotifywait` if available, otherwise polls).
|
||||||
|
- `scripts/logs.sh [-c] [-t] [-g PATTERN]` — streams logcat for the running app (`-t` for the
|
||||||
|
raw Chaquopy stdio tags, `-g` to grep, `-c` to clear first).
|
||||||
|
- `scripts/debug.sh start|stop [--platform vscode|pycharm]` — forwards the debugger port and
|
||||||
|
tells the dev server to start/stop the in-app debugger. Requires `debugpy` (vscode) or
|
||||||
|
`pydevd-pycharm` (pycharm) present in the app's Python runtime — add it to `requirements` in
|
||||||
|
`plugin.json` if it's missing.
|
||||||
|
- `scripts/fetch-sdk-stubs.sh [--beta]` — downloads the latest SDK type stubs into `stubs/`
|
||||||
|
for editor autocomplete.
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# How Chat Summaries works
|
||||||
|
|
||||||
|
## Summarize flow
|
||||||
|
|
||||||
|
**Summarize** (⋮ → Plugins → Summarize, or hold the unread button) runs in this order:
|
||||||
|
|
||||||
|
1. A bulletin confirms the press, then the history is fetched.
|
||||||
|
2. The transcript streams to the configured OpenAI-compatible endpoint.
|
||||||
|
3. Only when the first chunk arrives is a summary card injected, directly after the newest
|
||||||
|
analyzed message. If that message is not loaded (an offset, or history scrolled away),
|
||||||
|
the chat is asked to load it first. The chat then jumps to the card once.
|
||||||
|
4. Reasoning and answer stream into the card. The finished summary ends with a native
|
||||||
|
`#aisummaries` hashtag and is saved to a local SQLite store, from which it is restored
|
||||||
|
whenever the chat is reopened.
|
||||||
|
|
||||||
|
Any failure before step 3 (fetch error, empty history, unreachable host, bad key, HTTP
|
||||||
|
error) is reported as a bulletin only: the chat is not modified and the view does not move.
|
||||||
|
A failure mid-stream is written onto the card, and a summary saved earlier for the same
|
||||||
|
range is kept.
|
||||||
|
|
||||||
|
Deleting a card like any other message removes its saved row. The settings page shows how
|
||||||
|
many summaries are saved and roughly how much space they take, and offers **Clear all
|
||||||
|
summaries** (with a confirmation step).
|
||||||
|
|
||||||
|
Summaries exist only on this device, so Telegram's own search, which runs on the server,
|
||||||
|
does not find them, including a search for `#aisummaries`.
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# How exteraGram plugins work
|
||||||
|
|
||||||
|
## How exteraGram plugins actually work
|
||||||
|
|
||||||
|
A plugin, as exteraGram loads it, is exactly **one Python file** on the device:
|
||||||
|
`/data/user/0/com.exteragram.messenger/files/plugins/<plugin_id>.py`. The distribution
|
||||||
|
convention names the same content `<id>.plugin`.
|
||||||
|
|
||||||
|
The app reads plugin metadata **statically via AST** — it looks for top-level constant
|
||||||
|
assignments (`__id__`, `__name__`, `__description__`, `__author__`, `__version__`,
|
||||||
|
`__icon__`, `__app_version__`, `__sdk_version__`, `__requirements__`) without executing the
|
||||||
|
file. Only `__id__` and `__name__` are required.
|
||||||
|
|
||||||
|
The file must define exactly one class inheriting `BasePlugin` (`from base_plugin import
|
||||||
|
BasePlugin`). Lifecycle methods (`on_plugin_load`, `on_plugin_unload`, `create_settings`) and
|
||||||
|
hooks (`on_send_message_hook`, `pre_request_hook`, `post_request_hook`, `on_update_hook(s)`,
|
||||||
|
`on_app_event`) are all called by the host; hooks must be registered inside
|
||||||
|
`on_plugin_load` (e.g. `self.add_on_send_message_hook()`). A hook returns a
|
||||||
|
`HookResult(strategy=HookStrategy.MODIFY, params=params)` to apply changes, or a bare
|
||||||
|
`HookResult()` to pass through. Settings rows returned from `create_settings` come from
|
||||||
|
`ui.settings` (`Header`, `Input`, `Switch`, `Text`, `Selector`, `Divider`) and persist through
|
||||||
|
`self.get_setting`/`self.set_setting`. Logging goes through `android_utils.log`; Chaquopy also
|
||||||
|
redirects `sys.stdout`/`sys.stderr` to the logcat tags `python.stdout`/`python.stderr`.
|
||||||
|
|
||||||
|
This repo does not use the official Elyx/ElyxBuilder archive format — see
|
||||||
|
[`sdk-notes.md`](sdk-notes.md) for why.
|
||||||
@@ -78,6 +78,41 @@ plugins that need to reach beyond the public SDK surface.
|
|||||||
- `intents.IntentsManager` — Android intent integration, **exteraGram 12.6.4+ only**.
|
- `intents.IntentsManager` — Android intent integration, **exteraGram 12.6.4+ only**.
|
||||||
<https://plugins.exteragram.app/docs/intents>
|
<https://plugins.exteragram.app/docs/intents>
|
||||||
|
|
||||||
|
## Host internals relied on by the summary card
|
||||||
|
|
||||||
|
Verified against exteraGram's `ChatActivity`/`MessagesController` sources and the 12.5.x dex.
|
||||||
|
|
||||||
|
- **Scrolling.** `ChatActivity.scrollToMessageId(id, 0, false, 0, true, 0)`: for an id that
|
||||||
|
is not loaded it requests history around it and jumps there when it arrives; for a loaded
|
||||||
|
one it only `smoothScrollBy`s from the current cell positions, which are stale right after
|
||||||
|
an insert. To land on a row, set the layout manager's pending position instead:
|
||||||
|
`chatLayoutManager.scrollToPositionWithOffset(chatAdapter.messagesStartRow + index,
|
||||||
|
offset, false)` (`bottom=false` counts `offset` from the top edge). `messagesStartRow` is
|
||||||
|
public and current after `notifyDataSetChanged()`, which runs `updateRowsInternal()`.
|
||||||
|
- **Deleting.** Every delete goes through `MessagesController.deleteMessages` (4 overloads,
|
||||||
|
all starting `(messages, randoms, encryptedChat, dialogId, …)`). Only ids `> 0` are sent to
|
||||||
|
the server; the rest are deleted from the local cache and posted in
|
||||||
|
`NotificationCenter.messagesDeleted`. `ChatActivity` removes deleted rows by looking them up
|
||||||
|
in `messagesDict`, which injected cards are never added to, so the plugin removes them.
|
||||||
|
- **Hashtags in rich text.** `TL_iv.textHashtag` (single field `text`) is rendered by
|
||||||
|
`RichMessageLayout.formatText` exactly like a `TL_messageEntityHashtag` in a normal
|
||||||
|
message (`URLSpanNoUnderline`). Long-pressing it offers Open / Copy hashtag. A `textUrl`
|
||||||
|
whose URL starts with `#` is not a hashtag: it goes through the external-URL path and does
|
||||||
|
nothing.
|
||||||
|
- **Search.** In-chat and hashtag search are server-side (`messages.search`); local results
|
||||||
|
exist only for Saved Messages tags. A locally injected message cannot appear in them.
|
||||||
|
|
||||||
|
## SDK UI details used here
|
||||||
|
|
||||||
|
- `ui.settings.Text(text, subtext=, icon=, accent=, red=, on_click=)`, where `on_click`
|
||||||
|
receives the clicked `View`. `set_setting(key, value, reload_settings=True)` re-renders
|
||||||
|
the settings page.
|
||||||
|
- `AlertDialogBuilder(activity)`; button listeners receive `(builder, which)`;
|
||||||
|
`make_button_red(AlertDialogBuilder.BUTTON_POSITIVE)` after `show()`. A dismissed builder
|
||||||
|
cannot be shown again.
|
||||||
|
- `BulletinHelper.show_*` post to the UI thread themselves.
|
||||||
|
- `android_utils.run_on_ui_thread(fn, delay)`, where `delay` is in milliseconds.
|
||||||
|
|
||||||
## On-device dev server protocol
|
## On-device dev server protocol
|
||||||
|
|
||||||
<https://plugins.exteragram.app/docs/dev-server>
|
<https://plugins.exteragram.app/docs/dev-server>
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
# Versioning and releases
|
||||||
|
|
||||||
|
## Git hooks and versioning
|
||||||
|
|
||||||
|
`scripts/pre-commit` bumps the patch number of `version` in `plugin.json` on every commit
|
||||||
|
(`1.0.0` → `1.0.1`) and stages it into that same commit. `scripts/post-commit` re-stages
|
||||||
|
`plugin.json` afterwards, because commits of a file selection (`git commit -- <paths>`, which
|
||||||
|
some IDEs use) are built from a temporary index and would otherwise leave it looking modified.
|
||||||
|
|
||||||
|
**The hooks enable themselves.** Git never activates hooks from repository content, so a
|
||||||
|
fresh clone, or a repo created from this template, starts without them. The first run of
|
||||||
|
`tools/bundle.py` fixes that by running `git config core.hooksPath scripts`, and every
|
||||||
|
workflow runs it: directly, or through `scripts/deploy.sh` and `scripts/watch.sh`. That
|
||||||
|
includes `--check`, so after cloning run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python3 tools/bundle.py --check
|
||||||
|
```
|
||||||
|
|
||||||
|
once, before your first commit. A commit made before any build runs without the hooks.
|
||||||
|
`bundle.py` leaves `core.hooksPath` alone when it is already set (so pointing it elsewhere on
|
||||||
|
purpose sticks), skips the step in CI (whenever `CI` is set) and outside a git work tree, and
|
||||||
|
never fails a build over it. To turn the hooks off for a clone, run
|
||||||
|
`git config core.hooksPath .git/hooks`; `git config --unset core.hooksPath` only lasts
|
||||||
|
until the next build.
|
||||||
|
|
||||||
|
The bump is skipped when:
|
||||||
|
|
||||||
|
- the commit message contains `[no bump]`, `[skip bump]`, `[no version bump]` or
|
||||||
|
`[skip version]` (case-insensitive; `nobump`, `no-bump` and `no_bump` also work),
|
||||||
|
- `NO_BUMP=1` (or `GIT_NO_BUMP=1`) is set, e.g. `NO_BUMP=1 git commit`, which is the only way
|
||||||
|
for commits written in an editor, since their message doesn't exist yet when the hook runs,
|
||||||
|
- `version` in `plugin.json` already differs from `HEAD` (you bumped it by hand, e.g. for a
|
||||||
|
minor or major release),
|
||||||
|
- a merge, rebase, cherry-pick or revert is in progress.
|
||||||
|
|
||||||
|
Because every version change publishes a release (see [Publishing a release](#publishing-a-release)), a normal commit + push to
|
||||||
|
`main` ships a new patch release. Use a stop word for commits that should not.
|
||||||
|
|
||||||
|
## Publishing a release
|
||||||
|
|
||||||
|
`.gitea/workflows/release.yml` builds, verifies and publishes the `.plugin` to the repo's
|
||||||
|
Releases tab on every push to `main`. `dist/` is gitignored, so the artifact is always built
|
||||||
|
by CI rather than committed.
|
||||||
|
|
||||||
|
The `version` field in `plugin.json` is the single source of truth:
|
||||||
|
|
||||||
|
| Push to `main` with… | Result |
|
||||||
|
| --- | --- |
|
||||||
|
| a new `version` | creates tag `v<version>` and a new release |
|
||||||
|
| the same `version` | rebuilds the existing release, replacing its attachment |
|
||||||
|
|
||||||
|
So every push leaves a downloadable build attached to the current version, and bumping
|
||||||
|
`version` is what cuts a new release. With the git hooks enabled every commit bumps it,
|
||||||
|
so every push of a normal commit cuts a new patch release. Pushing a `v*` tag by hand still works and uses that
|
||||||
|
tag verbatim, as does a manual run (Actions → Publish plugin → Run workflow).
|
||||||
|
|
||||||
|
Every path runs `--check`, builds with `--release`, and gates the upload on
|
||||||
|
`verify_bundle.py`, so a bundle that fails verification is never published. A rebuild
|
||||||
|
replaces the attachment rather than piling up duplicates, and refreshes the release notes to
|
||||||
|
name the commit it came from. The tag itself is never moved: it keeps pointing at the commit
|
||||||
|
that first introduced that version.
|
||||||
|
|
||||||
|
Authentication uses `secrets.API_TOKEN`, an organisation-level Actions secret holding a user
|
||||||
|
token with the `write:repository` scope (which covers releases). Set it once under
|
||||||
|
**Org → Settings → Actions → Secrets** and every repo in the organisation inherits it; a
|
||||||
|
repo-level secret of the same name takes precedence if one repo ever needs different
|
||||||
|
credentials.
|
||||||
|
|
||||||
|
The workflow passes it to `release.py` as `RELEASE_TOKEN`, **not** `GITEA_TOKEN`. The Actions
|
||||||
|
runner injects its own `GITEA_TOKEN` into every step, and that value overrides a step-level
|
||||||
|
`env:` entry of the same name, so the org secret would be silently ignored.
|
||||||
|
|
||||||
|
If the org secret does not reach a repository the publish still works: `release.py` tries
|
||||||
|
`RELEASE_TOKEN` first and then falls back to the runner's `GITEA_TOKEN`, which can publish
|
||||||
|
releases in its own repository, and prints which one it used. An organisation secret is only
|
||||||
|
exposed to a repository that has Actions enabled, so check **Repo → Settings → Actions** if a
|
||||||
|
repo unexpectedly falls back.
|
||||||
|
|
||||||
|
If you would rather not manage a token at all, that injected `GITEA_TOKEN` can create
|
||||||
|
releases in its own repo: pass `--token-env GITEA_TOKEN` and drop the org secret. It is
|
||||||
|
scoped to the single job and expires with it, so it is the safer default; `API_TOKEN` is what
|
||||||
|
you want when publishing needs to reach beyond this repo.
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="640" height="640" viewBox="0 0 640 640">
|
||||||
|
<defs>
|
||||||
|
<linearGradient id="bg" x1="0.3" y1="0" x2="0.7" y2="1">
|
||||||
|
<stop offset="0" stop-color="#F89B2E"/>
|
||||||
|
<stop offset="0.5" stop-color="#F7724A"/>
|
||||||
|
<stop offset="1" stop-color="#F5517A"/>
|
||||||
|
</linearGradient>
|
||||||
|
<radialGradient id="glow" cx="0.5" cy="0.45" r="0.5">
|
||||||
|
<stop offset="0" stop-color="#FF5A3C" stop-opacity="0.18"/>
|
||||||
|
<stop offset="1" stop-color="#FF5A3C" stop-opacity="0"/>
|
||||||
|
</radialGradient>
|
||||||
|
<linearGradient id="paper" x1="0" y1="0" x2="0" y2="1">
|
||||||
|
<stop offset="0" stop-color="#F7F7F7"/>
|
||||||
|
<stop offset="1" stop-color="#E2E2E2"/>
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient id="star" x1="0" y1="0" x2="0" y2="1">
|
||||||
|
<stop offset="0" stop-color="#FFE27A"/>
|
||||||
|
<stop offset="1" stop-color="#FFC53D"/>
|
||||||
|
</linearGradient>
|
||||||
|
<filter id="sh" x="-20%" y="-20%" width="140%" height="150%">
|
||||||
|
<feDropShadow dx="0" dy="10" stdDeviation="10" flood-color="#7A1E2E" flood-opacity="0.28"/>
|
||||||
|
</filter>
|
||||||
|
</defs>
|
||||||
|
<rect width="640" height="640" fill="url(#bg)"/>
|
||||||
|
<rect width="640" height="640" fill="url(#glow)"/>
|
||||||
|
|
||||||
|
<!-- back bubble: the chat being summarized -->
|
||||||
|
<path filter="url(#sh)" d="M250,128 H452 A44,44 0 0 1 496,172 V300 A44,44 0 0 1 452,344 H446 L462,392 L398,344 H250 A44,44 0 0 1 206,300 V172 A44,44 0 0 1 250,128 Z"
|
||||||
|
fill="#FFFFFF" fill-opacity="0.45" stroke="#FFFFFF" stroke-opacity="0.7" stroke-width="5" stroke-linejoin="round"/>
|
||||||
|
|
||||||
|
<!-- front bubble: the summary -->
|
||||||
|
<g filter="url(#sh)">
|
||||||
|
<path d="M190,196 H418 A48,48 0 0 1 466,244 V420 A48,48 0 0 1 418,468 H262 L178,526 L196,468 H190 A48,48 0 0 1 142,420 V244 A48,48 0 0 1 190,196 Z"
|
||||||
|
fill="url(#paper)" stroke="#B9B9B9" stroke-width="6" stroke-linejoin="round"/>
|
||||||
|
</g>
|
||||||
|
<g stroke-linecap="round">
|
||||||
|
<line x1="186" y1="258" x2="372" y2="258" stroke="#555555" stroke-width="18"/>
|
||||||
|
<circle cx="194" cy="320" r="10" fill="#F7724A"/>
|
||||||
|
<line x1="224" y1="320" x2="420" y2="320" stroke="#8C8C8C" stroke-width="13"/>
|
||||||
|
<circle cx="194" cy="370" r="10" fill="#F7724A"/>
|
||||||
|
<line x1="224" y1="370" x2="392" y2="370" stroke="#8C8C8C" stroke-width="13"/>
|
||||||
|
<circle cx="194" cy="420" r="10" fill="#F7724A"/>
|
||||||
|
<line x1="224" y1="420" x2="336" y2="420" stroke="#8C8C8C" stroke-width="13"/>
|
||||||
|
</g>
|
||||||
|
|
||||||
|
<!-- sparkles -->
|
||||||
|
<g filter="url(#sh)" stroke="#F59A2E" stroke-width="7" stroke-linejoin="round" fill="url(#star)">
|
||||||
|
<path d="M470,150 C476,196 488,208 534,214 C488,220 476,232 470,278 C464,232 452,220 406,214 C452,208 464,196 470,150 Z"/>
|
||||||
|
<path d="M532,282 C535,304 541,310 563,313 C541,316 535,322 532,344 C529,322 523,316 501,313 C523,310 529,304 532,282 Z"/>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 2.7 KiB |
+6
-3
@@ -3,9 +3,12 @@
|
|||||||
"name": "Chat Summaries",
|
"name": "Chat Summaries",
|
||||||
"description": "Summarize the last N messages (with offset) in any chat, group, channel, or topic using AI.",
|
"description": "Summarize the last N messages (with offset) in any chat, group, channel, or topic using AI.",
|
||||||
"author": "@lyuksovannyy",
|
"author": "@lyuksovannyy",
|
||||||
"version": "1.0.3",
|
"version": "1.1.2",
|
||||||
"icon": "exteraPlugins/1",
|
"icon": "exteraGramPluginIcons/2",
|
||||||
"app_version": ">=12.5.1",
|
"app_version": ">=12.5.1",
|
||||||
"sdk_version": ">=1.4.4.3",
|
"sdk_version": ">=1.4.4.3",
|
||||||
"requirements": ["requests", "debugpy"]
|
"requirements": [
|
||||||
|
"requests",
|
||||||
|
"debugpy"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# Companion to scripts/pre-commit.
|
# Companion to scripts/pre-commit. Both are enabled per clone by tools/bundle.py
|
||||||
|
# (it sets `core.hooksPath scripts` on its first run).
|
||||||
#
|
#
|
||||||
# `git commit -- <pathspec>` (also how some IDEs commit a file selection) builds
|
# `git commit -- <pathspec>` (also how some IDEs commit a file selection) builds
|
||||||
# the commit from a temporary index. The pre-commit bump therefore lands in the
|
# the commit from a temporary index. The pre-commit bump therefore lands in the
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Increment the patch component of plugin.json's version as part of the commit.
|
"""Increment the patch component of plugin.json's version as part of the commit.
|
||||||
|
|
||||||
|
Enabled per clone by tools/bundle.py, which sets `core.hooksPath scripts` on its
|
||||||
|
first run (git never enables hooks from repository content by itself).
|
||||||
|
|
||||||
Runs as a pre-commit hook, which is the only commit hook whose `git add` is
|
Runs as a pre-commit hook, which is the only commit hook whose `git add` is
|
||||||
picked up by the commit currently being created:
|
picked up by the commit currently being created:
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ SETTING_DEFAULT_COUNT = "default_count"
|
|||||||
SETTING_DEFAULT_OFFSET = "default_offset"
|
SETTING_DEFAULT_OFFSET = "default_offset"
|
||||||
SETTING_GLOBAL_PROMPT = "global_prompt"
|
SETTING_GLOBAL_PROMPT = "global_prompt"
|
||||||
SETTING_INCLUDE_ATTACHMENTS = "include_attachments"
|
SETTING_INCLUDE_ATTACHMENTS = "include_attachments"
|
||||||
|
# Not a user setting: writing it with reload_settings=True is how the
|
||||||
|
# settings page is re-rendered after the store changes.
|
||||||
|
SETTING_STORAGE_REVISION = "storage_revision"
|
||||||
|
|
||||||
DEFAULT_PROMPT = (
|
DEFAULT_PROMPT = (
|
||||||
"Summarize the following chat conversation. Focus on key points, "
|
"Summarize the following chat conversation. Focus on key points, "
|
||||||
@@ -46,3 +49,22 @@ LONG_PRESS_SECONDS = 0.4
|
|||||||
# rebuilds the RichMessage blocks and rebinds the cell, so pushing on every SSE
|
# rebuilds the RichMessage blocks and rebinds the cell, so pushing on every SSE
|
||||||
# chunk would burn the UI thread for no visible gain.
|
# chunk would burn the UI thread for no visible gain.
|
||||||
STREAM_UPDATE_INTERVAL = 0.25
|
STREAM_UPDATE_INTERVAL = 0.25
|
||||||
|
|
||||||
|
# Appended to every finished summary. Lets a reader tell a local AI summary
|
||||||
|
# from a real message at a glance, and gives copied or forwarded text a
|
||||||
|
# searchable marker.
|
||||||
|
SUMMARY_HASHTAG = "#aisummaries"
|
||||||
|
|
||||||
|
# A summary card can only be placed directly after its newest analyzed
|
||||||
|
# message, which with an offset may not be loaded yet. Placement asks the chat
|
||||||
|
# to load it, then retries this many times, this many ms apart (~10 s), asking
|
||||||
|
# again every PLACEMENT_RELOAD_EVERY retries in case a request was dropped.
|
||||||
|
PLACEMENT_RETRIES = 80
|
||||||
|
PLACEMENT_RETRY_DELAY_MS = 125
|
||||||
|
PLACEMENT_RELOAD_EVERY = 16
|
||||||
|
|
||||||
|
# After jumping to a fresh card, check at these delays (ms) that it is still on
|
||||||
|
# screen and jump again if not. Loading a far anchor makes ChatActivity run its
|
||||||
|
# own postponed scroll, and a cache-then-server reload can re-scroll it, either
|
||||||
|
# of which can land after the jump and carry the view away from the card.
|
||||||
|
JUMP_SETTLE_DELAYS_MS = (250, 700, 1500, 3000)
|
||||||
@@ -0,0 +1,276 @@
|
|||||||
|
"""Keeps the page-down button's unread counter right away from the newest messages.
|
||||||
|
|
||||||
|
ChatActivity shows `newUnreadMessageCount` on the page-down badge. It seeds it
|
||||||
|
from the dialog's unread count (everything after the read marker) and only
|
||||||
|
decrements it as messages are marked read. Away from the newest messages that
|
||||||
|
number is wrong: after a jump into history (a summary anchor, a reply, a
|
||||||
|
pinned message, search) messages between the read marker and the jump target
|
||||||
|
stay counted, so the badge can say 5k while only 3k messages lie below what is
|
||||||
|
on screen.
|
||||||
|
|
||||||
|
What the badge should show is how many messages are newer than the newest
|
||||||
|
visible one. The server answers that exactly: messages.getHistory with
|
||||||
|
offset_id=<that id>, add_offset=-1, limit=1 returns `offset_id_offset`, the
|
||||||
|
number of messages newer than offset_id. The hook asks while the chat is not
|
||||||
|
showing the newest messages, at most once per REFRESH_SECONDS per chat and only
|
||||||
|
after the visible position moved, then writes the result through the same
|
||||||
|
setter the app uses. At the bottom of the chat it never interferes.
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
|
import diagnostics
|
||||||
|
from hooks.restore_hook import _find_methods_by_name
|
||||||
|
|
||||||
|
# The badge only needs to be right, not instant: one tiny history request per
|
||||||
|
# chat every few seconds while scrolling history is negligible next to what
|
||||||
|
# scrolling itself loads.
|
||||||
|
REFRESH_SECONDS = 3.0
|
||||||
|
|
||||||
|
|
||||||
|
def _get(obj: Any, name: str) -> Any:
|
||||||
|
try:
|
||||||
|
from hook_utils import get_private_field
|
||||||
|
|
||||||
|
return get_private_field(obj, name)
|
||||||
|
except Exception:
|
||||||
|
return getattr(obj, name, None)
|
||||||
|
|
||||||
|
|
||||||
|
def _set_int(obj: Any, name: str, value: int) -> bool:
|
||||||
|
"""Writes a Java int field through reflection.
|
||||||
|
|
||||||
|
hook_utils.set_private_field returns without error here but leaves the
|
||||||
|
field unchanged (verified on device), so the field is written directly.
|
||||||
|
"""
|
||||||
|
cls = obj.getClass()
|
||||||
|
while cls is not None:
|
||||||
|
try:
|
||||||
|
field = cls.getDeclaredField(name)
|
||||||
|
field.setAccessible(True)
|
||||||
|
field.setInt(obj, int(value))
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
cls = cls.getSuperclass()
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _first(value: Any) -> Any:
|
||||||
|
"""First element of a Java array exposed through Chaquopy, or None."""
|
||||||
|
try:
|
||||||
|
return value[0]
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _newest_visible_id(chat_activity: Any) -> int:
|
||||||
|
"""Largest real message id among the rows on screen, or 0."""
|
||||||
|
try:
|
||||||
|
from services.rich_injector import get_chat_adapter, get_messages_list
|
||||||
|
|
||||||
|
layout_manager = _get(chat_activity, "chatLayoutManager")
|
||||||
|
adapter = get_chat_adapter(chat_activity)
|
||||||
|
messages = get_messages_list(chat_activity)
|
||||||
|
if layout_manager is None or adapter is None or messages is None:
|
||||||
|
return 0
|
||||||
|
first = int(layout_manager.findFirstVisibleItemPosition())
|
||||||
|
last = int(layout_manager.findLastVisibleItemPosition())
|
||||||
|
if first < 0 or last < 0:
|
||||||
|
return 0
|
||||||
|
start = int(adapter.messagesStartRow)
|
||||||
|
size = int(messages.size())
|
||||||
|
best = 0
|
||||||
|
for position in range(min(first, last), max(first, last) + 1):
|
||||||
|
index = position - start
|
||||||
|
if 0 <= index < size:
|
||||||
|
owner = getattr(messages.get(index), "messageOwner", None)
|
||||||
|
msg_id = int(getattr(owner, "id", 0) or 0) if owner is not None else 0
|
||||||
|
# Injected summary cards are negative, date rows are 0.
|
||||||
|
if msg_id > best:
|
||||||
|
best = msg_id
|
||||||
|
return best
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.visible_id", exc)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def _read_inbox_max_id(dialog_id: int) -> int:
|
||||||
|
try:
|
||||||
|
from client_utils import get_messages_controller
|
||||||
|
|
||||||
|
controller = get_messages_controller()
|
||||||
|
dialog = controller.dialogs_dict.get(dialog_id) if controller else None
|
||||||
|
return int(getattr(dialog, "read_inbox_max_id", 0) or 0) if dialog is not None else 0
|
||||||
|
except Exception:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
class BadgeHook:
|
||||||
|
def __init__(self, plugin):
|
||||||
|
self.plugin = plugin
|
||||||
|
self._unhooks: List[Any] = []
|
||||||
|
# dialog_id -> (monotonic time, visible id) of the last server query.
|
||||||
|
self._asked_at: Dict[int, tuple] = {}
|
||||||
|
# dialog_id -> True while a query is in flight.
|
||||||
|
self._in_flight: Dict[int, bool] = {}
|
||||||
|
# Strong references to RequestCallback wrappers until they fire.
|
||||||
|
self._pending: List[Any] = []
|
||||||
|
|
||||||
|
def install(self) -> None:
|
||||||
|
try:
|
||||||
|
from base_plugin import MethodHook
|
||||||
|
from hook_utils import find_class
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.install:imports", exc)
|
||||||
|
return
|
||||||
|
|
||||||
|
chat_activity_class = find_class("org.telegram.ui.ChatActivity")
|
||||||
|
if not chat_activity_class:
|
||||||
|
diagnostics.note("badge_hook.install: ChatActivity not found")
|
||||||
|
return
|
||||||
|
methods = _find_methods_by_name(chat_activity_class, "updateMessagesVisiblePart")
|
||||||
|
diagnostics.note(f"badge_hook.install: updateMessagesVisiblePart methods found={len(methods)}")
|
||||||
|
|
||||||
|
outer = self
|
||||||
|
|
||||||
|
class _VisiblePartHook(MethodHook):
|
||||||
|
def after_hooked_method(self, param):
|
||||||
|
try:
|
||||||
|
outer._check(param.thisObject)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.check", exc)
|
||||||
|
|
||||||
|
for method in methods:
|
||||||
|
try:
|
||||||
|
ref = self.plugin.hook_method(method, _VisiblePartHook())
|
||||||
|
if ref:
|
||||||
|
self._unhooks.append(ref)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.install:hook_method", exc)
|
||||||
|
|
||||||
|
def uninstall(self) -> None:
|
||||||
|
for ref in self._unhooks:
|
||||||
|
try:
|
||||||
|
self.plugin.unhook_method(ref)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.uninstall", exc)
|
||||||
|
self._unhooks = []
|
||||||
|
self._pending = []
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _check(self, chat_activity: Any) -> None:
|
||||||
|
if chat_activity is None:
|
||||||
|
return
|
||||||
|
# Topics, threads, scheduled and saved views keep their own counters.
|
||||||
|
for flag in ("isTopic", "isThreadChat", "isComments"):
|
||||||
|
if bool(_get(chat_activity, flag) or False):
|
||||||
|
return
|
||||||
|
if int(_get(chat_activity, "chatMode") or 0) != 0:
|
||||||
|
return
|
||||||
|
if int(_get(chat_activity, "newUnreadMessageCount") or 0) <= 0:
|
||||||
|
return
|
||||||
|
# Showing the newest messages: the app's own count is accurate.
|
||||||
|
if bool(_first(_get(chat_activity, "forwardEndReached")) or False):
|
||||||
|
return
|
||||||
|
|
||||||
|
dialog_id = int(chat_activity.getDialogId())
|
||||||
|
if self._in_flight.get(dialog_id):
|
||||||
|
return
|
||||||
|
visible_id = _newest_visible_id(chat_activity)
|
||||||
|
if visible_id <= 0:
|
||||||
|
return
|
||||||
|
# Viewing a message marks everything up to it read, so the backlog
|
||||||
|
# starts after whichever is newer: the read marker or the screen.
|
||||||
|
visible_id = max(visible_id, _read_inbox_max_id(dialog_id))
|
||||||
|
now = time.monotonic()
|
||||||
|
last_at, last_id = self._asked_at.get(dialog_id, (0.0, 0))
|
||||||
|
if visible_id == last_id or now - last_at < REFRESH_SECONDS:
|
||||||
|
return
|
||||||
|
if len(self._asked_at) > 64:
|
||||||
|
self._asked_at.clear()
|
||||||
|
self._asked_at[dialog_id] = (now, visible_id)
|
||||||
|
self._query(chat_activity, dialog_id, visible_id)
|
||||||
|
|
||||||
|
def _query(self, chat_activity: Any, dialog_id: int, visible_id: int) -> None:
|
||||||
|
try:
|
||||||
|
from client_utils import RequestCallback, get_messages_controller, send_request
|
||||||
|
from org.telegram.tgnet import TLRPC
|
||||||
|
from services.message_fetcher import MessageFetcher
|
||||||
|
|
||||||
|
controller = get_messages_controller()
|
||||||
|
peer = MessageFetcher._resolve_input_peer(controller, TLRPC, dialog_id) if controller else None
|
||||||
|
if peer is None:
|
||||||
|
return
|
||||||
|
req = TLRPC.TL_messages_getHistory()
|
||||||
|
req.peer = peer
|
||||||
|
req.offset_id = visible_id
|
||||||
|
req.offset_date = 0
|
||||||
|
# -1 returns the anchor itself; offset_id_offset then counts the
|
||||||
|
# messages strictly newer than it.
|
||||||
|
req.add_offset = -1
|
||||||
|
req.limit = 1
|
||||||
|
req.max_id = 0
|
||||||
|
req.min_id = 0
|
||||||
|
req.hash = 0
|
||||||
|
|
||||||
|
box: List[Any] = []
|
||||||
|
|
||||||
|
def on_complete(response, error):
|
||||||
|
try:
|
||||||
|
self._in_flight.pop(dialog_id, None)
|
||||||
|
for proxy in box:
|
||||||
|
if proxy in self._pending:
|
||||||
|
self._pending.remove(proxy)
|
||||||
|
if error is not None or response is None:
|
||||||
|
return
|
||||||
|
newer = getattr(response, "offset_id_offset", None)
|
||||||
|
if newer is None:
|
||||||
|
return
|
||||||
|
self._apply(chat_activity, dialog_id, int(newer), visible_id)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.on_complete", exc)
|
||||||
|
|
||||||
|
proxy = RequestCallback(on_complete)
|
||||||
|
box.append(proxy)
|
||||||
|
self._pending.append(proxy)
|
||||||
|
self._in_flight[dialog_id] = True
|
||||||
|
send_request(req, proxy)
|
||||||
|
except Exception as exc:
|
||||||
|
self._in_flight.pop(dialog_id, None)
|
||||||
|
diagnostics.record_error("badge_hook.query", exc)
|
||||||
|
|
||||||
|
def _apply(self, chat_activity: Any, dialog_id: int, newer: int, visible_id: int) -> None:
|
||||||
|
def run():
|
||||||
|
try:
|
||||||
|
if int(chat_activity.getDialogId()) != dialog_id:
|
||||||
|
return
|
||||||
|
# Never raise the badge: when the user already read past what
|
||||||
|
# is on screen, the app's lower count is the right one.
|
||||||
|
current = int(_get(chat_activity, "newUnreadMessageCount") or 0)
|
||||||
|
if current <= 0:
|
||||||
|
return
|
||||||
|
value = max(0, min(newer, current))
|
||||||
|
if value == current:
|
||||||
|
return
|
||||||
|
if not _set_int(chat_activity, "newUnreadMessageCount", value):
|
||||||
|
return
|
||||||
|
_set_int(chat_activity, "prevSetUnreadCount", value)
|
||||||
|
layout = _get(chat_activity, "sideControlsButtonsLayout")
|
||||||
|
if layout is not None:
|
||||||
|
# Button 1 is the page-down button; the app animates only
|
||||||
|
# when the button is already on screen.
|
||||||
|
layout.setButtonCount(1, value, True)
|
||||||
|
diagnostics.note(
|
||||||
|
f"badge_hook: dialog {dialog_id} badge {current} -> {value} "
|
||||||
|
f"({newer} messages newer than visible {visible_id})"
|
||||||
|
)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("badge_hook.apply", exc)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from android_utils import run_on_ui_thread
|
||||||
|
|
||||||
|
run_on_ui_thread(run)
|
||||||
|
except Exception:
|
||||||
|
run()
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
"""Forgets a summary when the user deletes its card.
|
||||||
|
|
||||||
|
Deleting goes through MessagesController.deleteMessages, whichever menu it
|
||||||
|
starts from. Injected cards have negative ids, which that method never sends
|
||||||
|
to the server (only ids > 0 are), so letting the call through is harmless; it
|
||||||
|
just never touches the plugin's own store. This hook does: any deleted id that
|
||||||
|
is a stored summary's virtual id removes that row, so the card does not come
|
||||||
|
back the next time the chat is opened.
|
||||||
|
|
||||||
|
Every deleteMessages overload is hooked. They delegate to one another, so one
|
||||||
|
delete can be seen several times; handling is idempotent and only the call
|
||||||
|
that actually removed a row reports it.
|
||||||
|
"""
|
||||||
|
from typing import Any, List
|
||||||
|
|
||||||
|
import diagnostics
|
||||||
|
from hooks.restore_hook import _find_methods_by_name
|
||||||
|
|
||||||
|
|
||||||
|
def _java_ints(value: Any) -> List[int]:
|
||||||
|
"""ArrayList<Integer> as Chaquopy exposes it: size()/get(), not iterable."""
|
||||||
|
if value is None:
|
||||||
|
return []
|
||||||
|
try:
|
||||||
|
return [int(value.get(i)) for i in range(value.size())]
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
return [int(v) for v in value]
|
||||||
|
except Exception:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteHook:
|
||||||
|
def __init__(self, plugin):
|
||||||
|
self.plugin = plugin
|
||||||
|
self._unhooks: List[Any] = []
|
||||||
|
|
||||||
|
def install(self) -> None:
|
||||||
|
try:
|
||||||
|
from base_plugin import MethodHook
|
||||||
|
from hook_utils import find_class
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("delete_hook.install:imports", exc)
|
||||||
|
return
|
||||||
|
|
||||||
|
controller_class = find_class("org.telegram.messenger.MessagesController")
|
||||||
|
if not controller_class:
|
||||||
|
diagnostics.note("delete_hook.install: MessagesController not found")
|
||||||
|
return
|
||||||
|
methods = _find_methods_by_name(controller_class, "deleteMessages")
|
||||||
|
diagnostics.note(f"delete_hook.install: deleteMessages overloads found={len(methods)}")
|
||||||
|
|
||||||
|
plugin = self.plugin
|
||||||
|
|
||||||
|
class _OnDelete(MethodHook):
|
||||||
|
def before_hooked_method(self, param):
|
||||||
|
try:
|
||||||
|
args = list(getattr(param, "args", None) or [])
|
||||||
|
# Every overload starts (messages, randoms, encryptedChat,
|
||||||
|
# dialogId, ...).
|
||||||
|
if len(args) < 4:
|
||||||
|
return
|
||||||
|
ids = [i for i in _java_ints(args[0]) if i < 0]
|
||||||
|
if not ids:
|
||||||
|
return
|
||||||
|
plugin.on_cards_deleted(int(args[3] or 0), ids)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("delete_hook.before_hooked_method", exc)
|
||||||
|
|
||||||
|
for method in methods:
|
||||||
|
try:
|
||||||
|
ref = self.plugin.hook_method(method, _OnDelete())
|
||||||
|
if ref:
|
||||||
|
self._unhooks.append(ref)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("delete_hook.install:hook_method", exc)
|
||||||
|
|
||||||
|
def uninstall(self) -> None:
|
||||||
|
for ref in self._unhooks:
|
||||||
|
try:
|
||||||
|
ref.unhook()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
self._unhooks = []
|
||||||
+212
-92
@@ -21,8 +21,11 @@ from config import (
|
|||||||
SETTING_DEFAULT_OFFSET,
|
SETTING_DEFAULT_OFFSET,
|
||||||
SETTING_GLOBAL_PROMPT,
|
SETTING_GLOBAL_PROMPT,
|
||||||
SETTING_INCLUDE_ATTACHMENTS,
|
SETTING_INCLUDE_ATTACHMENTS,
|
||||||
|
SETTING_STORAGE_REVISION,
|
||||||
STREAM_UPDATE_INTERVAL,
|
STREAM_UPDATE_INTERVAL,
|
||||||
)
|
)
|
||||||
|
from hooks.badge_hook import BadgeHook
|
||||||
|
from hooks.delete_hook import DeleteHook
|
||||||
from hooks.restore_hook import RestoreHook
|
from hooks.restore_hook import RestoreHook
|
||||||
from hooks.unread_hook import UnreadHook
|
from hooks.unread_hook import UnreadHook
|
||||||
from providers.dispatcher import Dispatcher
|
from providers.dispatcher import Dispatcher
|
||||||
@@ -37,6 +40,8 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
self.dispatcher = Dispatcher(self)
|
self.dispatcher = Dispatcher(self)
|
||||||
self.unread_hook = UnreadHook(self)
|
self.unread_hook = UnreadHook(self)
|
||||||
self.restore_hook = RestoreHook(self)
|
self.restore_hook = RestoreHook(self)
|
||||||
|
self.delete_hook = DeleteHook(self)
|
||||||
|
self.badge_hook = BadgeHook(self)
|
||||||
self.summary_db = None
|
self.summary_db = None
|
||||||
|
|
||||||
def _get_summary_db(self):
|
def _get_summary_db(self):
|
||||||
@@ -83,10 +88,20 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
self.restore_hook.install()
|
self.restore_hook.install()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("on_plugin_load:restore_hook.install", exc)
|
diagnostics.record_error("on_plugin_load:restore_hook.install", exc)
|
||||||
|
try:
|
||||||
|
self.delete_hook.install()
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("on_plugin_load:delete_hook.install", exc)
|
||||||
|
try:
|
||||||
|
self.badge_hook.install()
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("on_plugin_load:badge_hook.install", exc)
|
||||||
|
|
||||||
def on_plugin_unload(self) -> None:
|
def on_plugin_unload(self) -> None:
|
||||||
self.unread_hook.uninstall()
|
self.unread_hook.uninstall()
|
||||||
self.restore_hook.uninstall()
|
self.restore_hook.uninstall()
|
||||||
|
self.delete_hook.uninstall()
|
||||||
|
self.badge_hook.uninstall()
|
||||||
log("chat_summaries unloaded")
|
log("chat_summaries unloaded")
|
||||||
|
|
||||||
def create_settings(self):
|
def create_settings(self):
|
||||||
@@ -140,9 +155,22 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
"summary can mention shared files. Message text is sent either way.",
|
"summary can mention shared files. Message text is sent either way.",
|
||||||
icon="msg_photos",
|
icon="msg_photos",
|
||||||
),
|
),
|
||||||
|
Divider(text="Storage"),
|
||||||
|
Text(
|
||||||
|
text="Saved summaries",
|
||||||
|
subtext=self._storage_summary(),
|
||||||
|
icon="msg_storage_usage",
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
text="Clear all summaries",
|
||||||
|
icon="msg_delete",
|
||||||
|
red=True,
|
||||||
|
on_click=self._on_clear_all_click,
|
||||||
|
),
|
||||||
|
Divider(),
|
||||||
Text(
|
Text(
|
||||||
text="About",
|
text="About",
|
||||||
subtext="Summarize via ⋮ → Plugins → Summarize, or hold the unread button to auto-fill unread messages. Diagnostics are also available from that menu.",
|
subtext="Summarize via ⋮ → Plugins → Summarize, or hold the unread button to auto-fill unread messages. Diagnostics are also available from that menu. Delete a summary card like any message to forget it.",
|
||||||
icon="msg_info",
|
icon="msg_info",
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
@@ -245,117 +273,101 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
offset: int,
|
offset: int,
|
||||||
prompt: str,
|
prompt: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Shows the summary card immediately, then drives it through every
|
"""Fetches history, then streams the summary into a card.
|
||||||
stage: fetching history, contacting the endpoint, streaming reasoning,
|
|
||||||
streaming the answer.
|
|
||||||
|
|
||||||
The card is injected before the fetch so the click has instant visible
|
Nothing is injected into the chat until the endpoint has actually
|
||||||
feedback. Its anchor is provisional until the fetch returns (the newest
|
started answering. Every failure before that point -- history fetch,
|
||||||
analyzed message id is not known yet) and is corrected via reanchor().
|
unreachable host, bad key, HTTP error, empty history -- is reported as a
|
||||||
|
bulletin and leaves the chat exactly as the user left it: no error
|
||||||
|
bubble to dismiss, no jump away from where they were reading.
|
||||||
"""
|
"""
|
||||||
from services.rich_injector import SummaryInjection, virtual_id_for
|
diagnostics.note(
|
||||||
|
f"summary: dialog={dialog_id} topic={topic_id} count={count} offset={offset}"
|
||||||
account = self._get_account()
|
|
||||||
# With a non-zero offset the analyzed range ends partway up the
|
|
||||||
# history, so skip that many messages when guessing where the card
|
|
||||||
# goes. reanchor() corrects it exactly once the fetch returns.
|
|
||||||
provisional_anchor = self._newest_visible_message_id(fragment, skip=offset)
|
|
||||||
|
|
||||||
injection = SummaryInjection(
|
|
||||||
fragment,
|
|
||||||
account,
|
|
||||||
dialog_id,
|
|
||||||
topic_id,
|
|
||||||
provisional_anchor,
|
|
||||||
virtual_id_for(0, provisional_anchor or 1),
|
|
||||||
)
|
)
|
||||||
if not injection.insert(status="Fetching messages…", streaming=True):
|
BulletinHelper.show_info(f"Chat Summaries: reading {count} messages…", fragment)
|
||||||
BulletinHelper.show_error("Chat Summaries: cannot inject into this chat", fragment)
|
|
||||||
return
|
|
||||||
|
|
||||||
def fail(message: str) -> None:
|
|
||||||
injection.update(answer_markdown=f"**Error:** {message}", streaming=False)
|
|
||||||
BulletinHelper.show_error(f"Chat Summaries: {message}", fragment)
|
|
||||||
|
|
||||||
def on_fetched(messages, error):
|
def on_fetched(messages, error):
|
||||||
if error:
|
if error:
|
||||||
fail(str(error))
|
self._report_error(str(error), fragment)
|
||||||
return
|
return
|
||||||
if not messages:
|
if not messages:
|
||||||
fail("no messages found")
|
self._report_error("no messages found", fragment)
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self._stream_into(injection, fragment, dialog_id, topic_id, prompt, messages)
|
self._stream_into(fragment, dialog_id, topic_id, prompt, messages)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("on_fetched:_stream_into", exc)
|
diagnostics.record_error("on_fetched:_stream_into", exc)
|
||||||
fail(str(exc))
|
self._report_error(str(exc), fragment)
|
||||||
|
|
||||||
self.fetcher.fetch_history(dialog_id, count, offset, topic_id, on_fetched)
|
self.fetcher.fetch_history(dialog_id, count, offset, topic_id, on_fetched)
|
||||||
|
|
||||||
def _newest_visible_message_id(self, chat_activity: Any, skip: int = 0) -> int:
|
def _report_error(self, message: str, fragment: Any) -> None:
|
||||||
"""Provisional anchor: the newest real message the analysis will cover.
|
diagnostics.note(f"summary failed: {message}")
|
||||||
|
BulletinHelper.show_error(f"Chat Summaries: {message}", fragment)
|
||||||
`skip` mirrors the summarize offset, so with an offset the guess lands
|
|
||||||
where the analyzed range actually ends instead of at the bottom of the
|
|
||||||
chat. Injected cards carry negative ids and are ignored.
|
|
||||||
|
|
||||||
When the offset reaches past the loaded window there is no message to
|
|
||||||
skip to, so fall back to the oldest loaded message rather than 0: the
|
|
||||||
card only needs somewhere valid to appear while "Fetching messages…" is
|
|
||||||
shown, and reanchor() moves it to the exact spot once the range is
|
|
||||||
known. Returning 0 would make the insert fail and the user would click
|
|
||||||
Summarize and see nothing happen.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
from services.rich_injector import get_messages_list
|
|
||||||
|
|
||||||
messages_list = get_messages_list(chat_activity)
|
|
||||||
if messages_list is None:
|
|
||||||
return 0
|
|
||||||
remaining = max(0, int(skip))
|
|
||||||
oldest = 0
|
|
||||||
for i in range(messages_list.size()):
|
|
||||||
owner = getattr(messages_list.get(i), "messageOwner", None)
|
|
||||||
msg_id = int(getattr(owner, "id", 0) or 0) if owner is not None else 0
|
|
||||||
if msg_id <= 0:
|
|
||||||
continue
|
|
||||||
oldest = msg_id
|
|
||||||
if remaining:
|
|
||||||
remaining -= 1
|
|
||||||
continue
|
|
||||||
return msg_id
|
|
||||||
return oldest
|
|
||||||
except Exception as exc:
|
|
||||||
diagnostics.record_error("_newest_visible_message_id", exc)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def _stream_into(
|
def _stream_into(
|
||||||
self,
|
self,
|
||||||
injection: Any,
|
|
||||||
fragment: Any,
|
fragment: Any,
|
||||||
dialog_id: int,
|
dialog_id: int,
|
||||||
topic_id: int,
|
topic_id: int,
|
||||||
prompt: str,
|
prompt: str,
|
||||||
messages: list,
|
messages: list,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Streams reasoning, then the answer, into the already-visible card."""
|
"""Streams reasoning, then the answer, into a card created on the
|
||||||
from services.rich_injector import virtual_id_for
|
first chunk.
|
||||||
|
|
||||||
anchor_msg_id = max(int(getattr(m, "id", 0) or 0) for m in messages)
|
The card goes directly after the newest analyzed message and the chat
|
||||||
|
jumps to it once, when it is placed.
|
||||||
|
"""
|
||||||
|
from services.rich_injector import SummaryInjection, virtual_id_for
|
||||||
|
|
||||||
|
ids = [int(getattr(m, "id", 0) or 0) for m in messages]
|
||||||
|
anchor_msg_id = max(ids)
|
||||||
|
diagnostics.note(
|
||||||
|
f"summary: fetched {len(messages)} messages, ids {min(ids)}..{anchor_msg_id}"
|
||||||
|
)
|
||||||
db = self._get_summary_db()
|
db = self._get_summary_db()
|
||||||
# Reserve the row now so the virtual id is stable for the whole stream.
|
state = {
|
||||||
summary_id = 0
|
"answer": "",
|
||||||
|
"thinking": "",
|
||||||
|
"last_push": 0.0,
|
||||||
|
"mapping": None,
|
||||||
|
"injection": None,
|
||||||
|
"summary_id": 0,
|
||||||
|
"created_row": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
def open_card():
|
||||||
|
"""Creates and places the card. Runs once, on the first chunk."""
|
||||||
|
if state["injection"] is not None:
|
||||||
|
return state["injection"]
|
||||||
|
# Reserve the row now so the virtual id is stable for the whole
|
||||||
|
# stream, and matches the card the restore hook may already show
|
||||||
|
# for this range.
|
||||||
if db is not None:
|
if db is not None:
|
||||||
try:
|
try:
|
||||||
summary_id = db.save(dialog_id, topic_id, anchor_msg_id, "", "", len(messages))
|
state["summary_id"], state["created_row"] = db.reserve(
|
||||||
|
dialog_id, topic_id, anchor_msg_id, len(messages)
|
||||||
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("_stream_into:db.reserve", exc)
|
diagnostics.record_error("_stream_into:db.reserve", exc)
|
||||||
|
injection = SummaryInjection(
|
||||||
injection.reanchor(anchor_msg_id, virtual_id_for(summary_id, anchor_msg_id))
|
fragment,
|
||||||
injection.update(status=f"Summarizing {len(messages)} messages…", streaming=True)
|
self._get_account(),
|
||||||
|
dialog_id,
|
||||||
state = {"answer": "", "thinking": "", "last_push": 0.0, "mapping": None}
|
topic_id,
|
||||||
|
anchor_msg_id,
|
||||||
|
virtual_id_for(state["summary_id"], anchor_msg_id),
|
||||||
|
)
|
||||||
|
state["injection"] = injection
|
||||||
|
if not injection.insert(
|
||||||
|
answer_markdown=rendered("answer"), thinking=rendered("thinking"), streaming=True
|
||||||
|
):
|
||||||
|
# The summary is still saved when it finishes, and the restore
|
||||||
|
# hook shows it once the chat can take it.
|
||||||
|
BulletinHelper.show_error("Chat Summaries: cannot show the summary in this chat", fragment)
|
||||||
|
state["last_push"] = _now()
|
||||||
|
return injection
|
||||||
|
|
||||||
def rendered(key: str, final: bool = False) -> str:
|
def rendered(key: str, final: bool = False) -> str:
|
||||||
"""Placeholder-substituted view of `answer` or `thinking`.
|
"""Placeholder-substituted view of `answer` or `thinking`.
|
||||||
@@ -378,18 +390,17 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
return text
|
return text
|
||||||
return deanonymize(text, mapping, dialog_id)
|
return deanonymize(text, mapping, dialog_id)
|
||||||
|
|
||||||
def push(force: bool = False) -> None:
|
def push(force: bool = False, tagged: bool = False) -> None:
|
||||||
"""Rate-limits re-renders; each one rebuilds blocks and rebinds cells."""
|
"""Rate-limits re-renders; each one rebuilds blocks and rebinds cells."""
|
||||||
import time
|
now = _now()
|
||||||
|
|
||||||
now = time.monotonic()
|
|
||||||
if not force and now - state["last_push"] < STREAM_UPDATE_INTERVAL:
|
if not force and now - state["last_push"] < STREAM_UPDATE_INTERVAL:
|
||||||
return
|
return
|
||||||
state["last_push"] = now
|
state["last_push"] = now
|
||||||
injection.update(
|
state["injection"].update(
|
||||||
answer_markdown=rendered("answer", final=force),
|
answer_markdown=rendered("answer", final=force),
|
||||||
thinking=rendered("thinking", final=force),
|
thinking=rendered("thinking", final=force),
|
||||||
streaming=not force,
|
streaming=not force,
|
||||||
|
tagged=tagged,
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_mapping(mapping) -> None:
|
def on_mapping(mapping) -> None:
|
||||||
@@ -400,18 +411,32 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
state["thinking"] += text
|
state["thinking"] += text
|
||||||
else:
|
else:
|
||||||
state["answer"] += text
|
state["answer"] += text
|
||||||
|
if state["injection"] is None:
|
||||||
|
open_card()
|
||||||
|
else:
|
||||||
push()
|
push()
|
||||||
|
|
||||||
def on_done(answer: Optional[str], thinking: Optional[str], error: Optional[str]) -> None:
|
def on_done(answer: Optional[str], thinking: Optional[str], error: Optional[str]) -> None:
|
||||||
if error:
|
if error:
|
||||||
|
self._report_error(error, fragment)
|
||||||
|
if state["injection"] is None:
|
||||||
|
# Failed before the first chunk: the chat was never touched.
|
||||||
|
return
|
||||||
state["answer"] = (state["answer"] or "") + f"\n\n**Error:** {error}"
|
state["answer"] = (state["answer"] or "") + f"\n\n**Error:** {error}"
|
||||||
push(force=True)
|
push(force=True)
|
||||||
BulletinHelper.show_error(f"Chat Summaries: {error}", fragment)
|
# A row created for this stream would never get text; don't
|
||||||
|
# leave it behind. A pre-existing summary keeps its text.
|
||||||
|
if db is not None and state["created_row"] and state["summary_id"]:
|
||||||
|
try:
|
||||||
|
db.delete(state["summary_id"])
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("_stream_into:db.delete", exc)
|
||||||
return
|
return
|
||||||
state["answer"] = answer or state["answer"]
|
state["answer"] = answer or state["answer"]
|
||||||
state["thinking"] = thinking or state["thinking"]
|
state["thinking"] = thinking or state["thinking"]
|
||||||
push(force=True)
|
open_card()
|
||||||
if db is not None and summary_id:
|
push(force=True, tagged=True)
|
||||||
|
if db is not None and state["summary_id"]:
|
||||||
try:
|
try:
|
||||||
# Persist real names and links, not placeholders: the
|
# Persist real names and links, not placeholders: the
|
||||||
# mapping is per-request and would be gone by the time the
|
# mapping is per-request and would be gone by the time the
|
||||||
@@ -425,10 +450,7 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
# The restore gate skips scopes it has already handled, so
|
# The restore gate skips scopes it has already handled, so
|
||||||
# the newly saved row has to invalidate that cache or it
|
# the newly saved row has to invalidate that cache or it
|
||||||
# would not be restored until the chat was reopened.
|
# would not be restored until the chat was reopened.
|
||||||
try:
|
|
||||||
self.restore_hook.invalidate()
|
self.restore_hook.invalidate()
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("_stream_into:db.save", exc)
|
diagnostics.record_error("_stream_into:db.save", exc)
|
||||||
|
|
||||||
@@ -442,6 +464,90 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
topic_id=topic_id,
|
topic_id=topic_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
# Deleting a card / clearing the store
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
|
||||||
|
def on_cards_deleted(self, dialog_id: int, deleted_ids: list) -> None:
|
||||||
|
"""Called by DeleteHook with the negative ids being deleted in a chat."""
|
||||||
|
from client_utils import get_last_fragment
|
||||||
|
from services.rich_injector import remove_cards, virtual_id_for
|
||||||
|
|
||||||
|
db = self._get_summary_db()
|
||||||
|
if db is None or not dialog_id:
|
||||||
|
return
|
||||||
|
wanted = {int(i) for i in deleted_ids}
|
||||||
|
removed = []
|
||||||
|
try:
|
||||||
|
for summary in db.get_for_dialog(dialog_id):
|
||||||
|
virtual_id = virtual_id_for(summary.id, summary.anchor_msg_id)
|
||||||
|
if virtual_id in wanted and db.delete(summary.id):
|
||||||
|
removed.append(virtual_id)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("on_cards_deleted", exc)
|
||||||
|
return
|
||||||
|
if not removed:
|
||||||
|
return
|
||||||
|
# The restore cache still holds the deleted rows; without this the
|
||||||
|
# card would be re-injected on the next frame.
|
||||||
|
self.restore_hook.invalidate()
|
||||||
|
fragment = get_last_fragment()
|
||||||
|
remove_cards(fragment, removed)
|
||||||
|
BulletinHelper.show_info(
|
||||||
|
"Summary deleted" if len(removed) == 1 else f"{len(removed)} summaries deleted",
|
||||||
|
fragment,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _storage_summary(self) -> str:
|
||||||
|
db = self._get_summary_db()
|
||||||
|
if db is None:
|
||||||
|
return "Storage unavailable"
|
||||||
|
try:
|
||||||
|
count, size = db.stats()
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("_storage_summary", exc)
|
||||||
|
return "Storage unavailable"
|
||||||
|
noun = "summary" if count == 1 else "summaries"
|
||||||
|
return f"{count} {noun} saved · about {_format_size(size)}"
|
||||||
|
|
||||||
|
def _on_clear_all_click(self, view: Any = None) -> None:
|
||||||
|
try:
|
||||||
|
from client_utils import get_last_fragment
|
||||||
|
from ui.alert import AlertDialogBuilder
|
||||||
|
|
||||||
|
fragment = get_last_fragment()
|
||||||
|
activity = fragment.getParentActivity() if fragment is not None else None
|
||||||
|
if activity is None:
|
||||||
|
return
|
||||||
|
builder = AlertDialogBuilder(activity)
|
||||||
|
builder.set_title("Clear all summaries?")
|
||||||
|
builder.set_message(
|
||||||
|
"Every saved summary in every chat will be deleted. This cannot be undone."
|
||||||
|
)
|
||||||
|
builder.set_positive_button("Clear", lambda b, w: self._clear_all_summaries())
|
||||||
|
builder.set_negative_button("Cancel", lambda b, w: b.dismiss())
|
||||||
|
builder.show()
|
||||||
|
builder.make_button_red(AlertDialogBuilder.BUTTON_POSITIVE)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("_on_clear_all_click", exc)
|
||||||
|
|
||||||
|
def _clear_all_summaries(self) -> None:
|
||||||
|
db = self._get_summary_db()
|
||||||
|
if db is None:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
removed = db.clear_all()
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("_clear_all_summaries", exc)
|
||||||
|
BulletinHelper.show_error("Chat Summaries: could not clear summaries")
|
||||||
|
return
|
||||||
|
self.restore_hook.invalidate()
|
||||||
|
# Re-render the settings page so the size row shows the new total.
|
||||||
|
self.set_setting(SETTING_STORAGE_REVISION, str(_now()), reload_settings=True)
|
||||||
|
BulletinHelper.show_success(
|
||||||
|
"Deleted 1 summary" if removed == 1 else f"Deleted {removed} summaries"
|
||||||
|
)
|
||||||
|
|
||||||
def _fetch_reply_context(self, messages: list) -> dict:
|
def _fetch_reply_context(self, messages: list) -> dict:
|
||||||
"""Reply parents that fall outside the analyzed range.
|
"""Reply parents that fall outside the analyzed range.
|
||||||
|
|
||||||
@@ -519,3 +625,17 @@ class ChatSummariesPlugin(BasePlugin):
|
|||||||
def _save_prompt_for(self, dialog_id: int, topic_id: int, prompt: str) -> None:
|
def _save_prompt_for(self, dialog_id: int, topic_id: int, prompt: str) -> None:
|
||||||
if prompt:
|
if prompt:
|
||||||
self.set_setting(self._prompt_key(dialog_id, topic_id), prompt)
|
self.set_setting(self._prompt_key(dialog_id, topic_id), prompt)
|
||||||
|
|
||||||
|
|
||||||
|
def _now() -> float:
|
||||||
|
import time
|
||||||
|
|
||||||
|
return time.monotonic()
|
||||||
|
|
||||||
|
|
||||||
|
def _format_size(size: int) -> str:
|
||||||
|
if size < 1024:
|
||||||
|
return f"{size} B"
|
||||||
|
if size < 1024 * 1024:
|
||||||
|
return f"{size / 1024:.0f} KB"
|
||||||
|
return f"{size / (1024 * 1024):.1f} MB"
|
||||||
+316
-156
@@ -16,7 +16,14 @@ Verified against exteraGram 12.5.x (dex inspection):
|
|||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
import diagnostics
|
import diagnostics
|
||||||
from services.rich_markdown import build_rich_message
|
from config import (
|
||||||
|
JUMP_SETTLE_DELAYS_MS,
|
||||||
|
PLACEMENT_RELOAD_EVERY,
|
||||||
|
PLACEMENT_RETRIES,
|
||||||
|
PLACEMENT_RETRY_DELAY_MS,
|
||||||
|
SUMMARY_HASHTAG,
|
||||||
|
)
|
||||||
|
from services.rich_markdown import build_rich_message, hashtag_block, markdown_to_plain
|
||||||
|
|
||||||
# Above Telegram's issued id range, so this user exists only locally.
|
# Above Telegram's issued id range, so this user exists only locally.
|
||||||
MOCK_SENDER_ID = 8000000000000001
|
MOCK_SENDER_ID = 8000000000000001
|
||||||
@@ -108,8 +115,15 @@ def build_summary_message(
|
|||||||
thinking: str = "",
|
thinking: str = "",
|
||||||
streaming: bool = False,
|
streaming: bool = False,
|
||||||
status: str = "",
|
status: str = "",
|
||||||
|
tagged: bool = False,
|
||||||
) -> Optional[Any]:
|
) -> Optional[Any]:
|
||||||
"""Builds the local MessageObject for a summary, or None on failure."""
|
"""Builds the local MessageObject for a summary, or None on failure.
|
||||||
|
|
||||||
|
`tagged` marks a finished summary: SUMMARY_HASHTAG is appended to the
|
||||||
|
card as a native hashtag node (TL_iv.textHashtag), and to the message's
|
||||||
|
plain-text body with a TL_messageEntityHashtag. The tag is added here rather than stored, so every
|
||||||
|
summary restored from the DB carries it too.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -124,7 +138,6 @@ def build_summary_message(
|
|||||||
msg.unread = False
|
msg.unread = False
|
||||||
msg.post = False
|
msg.post = False
|
||||||
msg.flags = 0
|
msg.flags = 0
|
||||||
msg.message = ""
|
|
||||||
msg.peer_id = peer_for_dialog(dialog_id)
|
msg.peer_id = peer_for_dialog(dialog_id)
|
||||||
|
|
||||||
sender = TLRPC.TL_peerUser()
|
sender = TLRPC.TL_peerUser()
|
||||||
@@ -143,7 +156,12 @@ def build_summary_message(
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("injector.reply_header", exc)
|
diagnostics.record_error("injector.reply_header", exc)
|
||||||
|
|
||||||
|
tagged = tagged and bool((answer_markdown or "").strip())
|
||||||
|
_set_plain_body(msg, answer_markdown, tagged, TLRPC)
|
||||||
|
|
||||||
msg.rich_message = build_rich_message(answer_markdown, thinking, streaming, status)
|
msg.rich_message = build_rich_message(answer_markdown, thinking, streaming, status)
|
||||||
|
if tagged:
|
||||||
|
msg.rich_message.blocks.add(hashtag_block(SUMMARY_HASHTAG))
|
||||||
msg.flags2 = int(getattr(msg, "flags2", 0) or 0) | FLAGS2_RICH_MESSAGE
|
msg.flags2 = int(getattr(msg, "flags2", 0) or 0) | FLAGS2_RICH_MESSAGE
|
||||||
|
|
||||||
message_object = MessageObject(account, msg, True, True)
|
message_object = MessageObject(account, msg, True, True)
|
||||||
@@ -156,6 +174,33 @@ def build_summary_message(
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _utf16_len(text: str) -> int:
|
||||||
|
"""Length in UTF-16 code units, which is what Telegram entity offsets count."""
|
||||||
|
return len(text.encode("utf-16-le")) // 2
|
||||||
|
|
||||||
|
|
||||||
|
def _set_plain_body(msg: Any, answer_markdown: str, tagged: bool, TLRPC: Any) -> None:
|
||||||
|
"""Fills `message` with the summary as plain text.
|
||||||
|
|
||||||
|
The card renders from rich_message, but copy, translate and any text
|
||||||
|
matching in the app read `message`. Left empty, a summary is invisible to
|
||||||
|
all of them.
|
||||||
|
"""
|
||||||
|
text = markdown_to_plain(answer_markdown)
|
||||||
|
if tagged:
|
||||||
|
text = (text + "\n\n" + SUMMARY_HASHTAG) if text else SUMMARY_HASHTAG
|
||||||
|
msg.message = text
|
||||||
|
if not tagged:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
entity = TLRPC.TL_messageEntityHashtag()
|
||||||
|
entity.offset = _utf16_len(text) - _utf16_len(SUMMARY_HASHTAG)
|
||||||
|
entity.length = _utf16_len(SUMMARY_HASHTAG)
|
||||||
|
msg.entities.add(entity)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("injector.hashtag_entity", exc)
|
||||||
|
|
||||||
|
|
||||||
def get_messages_list(chat_activity: Any) -> Optional[Any]:
|
def get_messages_list(chat_activity: Any) -> Optional[Any]:
|
||||||
"""Returns the ChatActivity's live ArrayList<MessageObject>, or None."""
|
"""Returns the ChatActivity's live ArrayList<MessageObject>, or None."""
|
||||||
try:
|
try:
|
||||||
@@ -260,7 +305,12 @@ def notify_adapter(chat_activity: Any) -> None:
|
|||||||
|
|
||||||
|
|
||||||
class SummaryInjection:
|
class SummaryInjection:
|
||||||
"""A single injected summary message, updatable in place while streaming."""
|
"""A single injected summary message, updatable in place while streaming.
|
||||||
|
|
||||||
|
Created only once the summary has started arriving, with its final anchor
|
||||||
|
and virtual id already known: nothing about it is provisional, so it is
|
||||||
|
placed exactly once, directly after the newest analyzed message.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, chat_activity: Any, account: int, dialog_id: int, topic_id: int, anchor_msg_id: int, virtual_id: int):
|
def __init__(self, chat_activity: Any, account: int, dialog_id: int, topic_id: int, anchor_msg_id: int, virtual_id: int):
|
||||||
self.chat_activity = chat_activity
|
self.chat_activity = chat_activity
|
||||||
@@ -270,25 +320,13 @@ class SummaryInjection:
|
|||||||
self.anchor_msg_id = int(anchor_msg_id)
|
self.anchor_msg_id = int(anchor_msg_id)
|
||||||
self.virtual_id = int(virtual_id)
|
self.virtual_id = int(virtual_id)
|
||||||
self.message_object: Optional[Any] = None
|
self.message_object: Optional[Any] = None
|
||||||
# Every id this injection has ever placed in the list. reanchor()
|
# True once the card is in the list. Until then renders only replace
|
||||||
# changes the id mid-flight, so an update must be able to find and
|
# message_object, which placement reads when it finally lands.
|
||||||
# remove the card under its previous id instead of adding a second one.
|
self.placed = False
|
||||||
self._used_ids = {int(virtual_id)}
|
self._placing = False
|
||||||
|
|
||||||
def insert(
|
def _build(self, answer_markdown: str, thinking: str, streaming: bool, status: str, tagged: bool) -> Optional[Any]:
|
||||||
self,
|
return build_summary_message(
|
||||||
answer_markdown: str = "",
|
|
||||||
thinking: str = "",
|
|
||||||
streaming: bool = True,
|
|
||||||
status: str = "",
|
|
||||||
) -> bool:
|
|
||||||
messages_list = get_messages_list(self.chat_activity)
|
|
||||||
if messages_list is None:
|
|
||||||
diagnostics.note("injector: ChatActivity messages list not found")
|
|
||||||
return False
|
|
||||||
|
|
||||||
register_mock_sender(self.account)
|
|
||||||
message_object = build_summary_message(
|
|
||||||
self.account,
|
self.account,
|
||||||
self.dialog_id,
|
self.dialog_id,
|
||||||
self.topic_id,
|
self.topic_id,
|
||||||
@@ -298,81 +336,223 @@ class SummaryInjection:
|
|||||||
thinking,
|
thinking,
|
||||||
streaming,
|
streaming,
|
||||||
status,
|
status,
|
||||||
|
tagged,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def insert(
|
||||||
|
self,
|
||||||
|
answer_markdown: str = "",
|
||||||
|
thinking: str = "",
|
||||||
|
streaming: bool = True,
|
||||||
|
status: str = "",
|
||||||
|
tagged: bool = False,
|
||||||
|
) -> bool:
|
||||||
|
"""Places the card after its anchor and jumps the chat to it.
|
||||||
|
|
||||||
|
Returns False only when the chat cannot take an injected card at all.
|
||||||
|
An anchor that is not loaded yet is not a failure: the chat is asked to
|
||||||
|
load it and placement retries until it arrives.
|
||||||
|
"""
|
||||||
|
if get_messages_list(self.chat_activity) is None:
|
||||||
|
diagnostics.note("injector: ChatActivity messages list not found")
|
||||||
|
return False
|
||||||
|
|
||||||
|
register_mock_sender(self.account)
|
||||||
|
message_object = self._build(answer_markdown, thinking, streaming, status, tagged)
|
||||||
if message_object is None:
|
if message_object is None:
|
||||||
return False
|
return False
|
||||||
self.message_object = message_object
|
self.message_object = message_object
|
||||||
|
self._placing = True
|
||||||
|
_on_ui(lambda: self._place(PLACEMENT_RETRIES))
|
||||||
|
return True
|
||||||
|
|
||||||
# The index must be computed on the UI thread, immediately before the
|
def _place(self, retries_left: int) -> None:
|
||||||
# mutation that uses it. Computing it here would race: loading a large
|
"""Runs on the UI thread. The index is computed immediately before the
|
||||||
# range takes long enough for paging or an incoming message to shift the
|
mutation that uses it: computed earlier, paging or an incoming message
|
||||||
# list, and the stale index then lands the card after the wrong message.
|
could shift the list and land the card after the wrong message.
|
||||||
def run():
|
"""
|
||||||
try:
|
try:
|
||||||
messages_list_now = get_messages_list(self.chat_activity)
|
messages_list = get_messages_list(self.chat_activity)
|
||||||
if messages_list_now is None:
|
if messages_list is None:
|
||||||
|
self._placing = False
|
||||||
return
|
return
|
||||||
existing = find_existing(messages_list_now, self.virtual_id)
|
message_object = self.message_object
|
||||||
if existing >= 0:
|
index = find_existing(messages_list, self.virtual_id)
|
||||||
messages_list_now.set(existing, message_object)
|
if index >= 0:
|
||||||
|
# A card for this summary is already on screen -- the restore
|
||||||
|
# hook re-injected the saved row being re-summarized. Claim it.
|
||||||
|
messages_list.set(index, message_object)
|
||||||
else:
|
else:
|
||||||
# A live card is a direct response to the user pressing
|
index = find_index_for_anchor(messages_list, self.anchor_msg_id)
|
||||||
# Summarize, so it must become visible even when the
|
|
||||||
# provisional anchor is not loaded (a large offset reaches
|
|
||||||
# past the loaded window). Fall back to id ordering, which
|
|
||||||
# still keeps every newer message below the card, and let
|
|
||||||
# reanchor() move it to the exact spot once the analyzed
|
|
||||||
# range is known. Restoring saved summaries stays strict --
|
|
||||||
# there is no follow-up correction there.
|
|
||||||
index = find_index_for_anchor(messages_list_now, self.anchor_msg_id)
|
|
||||||
if index < 0:
|
if index < 0:
|
||||||
index = find_index_for_anchor(
|
attempt = PLACEMENT_RETRIES - retries_left
|
||||||
messages_list_now, self.anchor_msg_id, require_anchor=False
|
if retries_left > 0 and attempt % PLACEMENT_RELOAD_EVERY == 0:
|
||||||
)
|
# The analyzed range ends outside the loaded window
|
||||||
if index < 0:
|
# (an offset, or history scrolled away). Have the chat
|
||||||
# No anchor at all (nothing loaded yet, or the offset
|
# load and show it, then place once it is in the list.
|
||||||
# reached past everything). Show the card at the bottom,
|
# Asked again periodically: scrollToMessageId drops the
|
||||||
# where the user is looking after pressing Summarize;
|
# request outright while waitingForGetDifference, and
|
||||||
# reanchor() relocates it once the range is known.
|
# a load can be superseded by another one.
|
||||||
diagnostics.note(
|
diagnostics.note(
|
||||||
f"injector: anchor {self.anchor_msg_id} unresolved, "
|
f"injector: anchor {self.anchor_msg_id} not loaded "
|
||||||
"showing status card at the bottom"
|
f"({_id_span(messages_list)}), requesting it (attempt {attempt})"
|
||||||
|
)
|
||||||
|
self._load_anchor()
|
||||||
|
if retries_left > 0:
|
||||||
|
_on_ui(lambda: self._place(retries_left - 1), PLACEMENT_RETRY_DELAY_MS)
|
||||||
|
else:
|
||||||
|
self._placing = False
|
||||||
|
diagnostics.note(
|
||||||
|
f"injector: anchor {self.anchor_msg_id} never loaded, card not placed"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
messages_list.add(index, message_object)
|
||||||
|
# A render may have landed while this was queued; show the newest.
|
||||||
|
if self.message_object is not message_object:
|
||||||
|
messages_list.set(index, self.message_object)
|
||||||
|
self.placed = True
|
||||||
|
self._placing = False
|
||||||
|
diagnostics.note(
|
||||||
|
f"injector: card placed after anchor {self.anchor_msg_id} at index {index} "
|
||||||
|
f"(retries left {retries_left})"
|
||||||
)
|
)
|
||||||
index = 0
|
|
||||||
messages_list_now.add(index, message_object)
|
|
||||||
adapter = get_chat_adapter(self.chat_activity)
|
adapter = get_chat_adapter(self.chat_activity)
|
||||||
if adapter is not None:
|
if adapter is not None:
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
# The analyzed range can end far up the history, so the card
|
# On placement only -- scrolling on every streaming update would
|
||||||
# may be injected off-screen. Bring it into view once, on the
|
# fight the user for control of the list. The settle checks right
|
||||||
# first insert only -- scrolling on every streaming update
|
# after only undo scrolls the app itself makes in that window.
|
||||||
# would fight the user for control of the list.
|
if self.scroll_into_view():
|
||||||
if existing < 0:
|
self._schedule_settle(0)
|
||||||
self.scroll_into_view()
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("injector.insert", exc)
|
self._placing = False
|
||||||
|
diagnostics.record_error("injector.place", exc)
|
||||||
|
|
||||||
_on_ui(run)
|
def _schedule_settle(self, step: int) -> None:
|
||||||
return True
|
if step >= len(JUMP_SETTLE_DELAYS_MS):
|
||||||
|
return
|
||||||
|
previous = JUMP_SETTLE_DELAYS_MS[step - 1] if step else 0
|
||||||
|
_on_ui(lambda: self._settle(step), JUMP_SETTLE_DELAYS_MS[step] - previous)
|
||||||
|
|
||||||
|
def _settle(self, step: int) -> None:
|
||||||
|
"""Re-jumps if something scrolled the fresh card off screen.
|
||||||
|
|
||||||
|
Stops for good as soon as the user touches the list: from then on
|
||||||
|
where the chat is scrolled is theirs to decide.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
list_view = _chat_list_view(self.chat_activity)
|
||||||
|
if list_view is not None and int(list_view.getScrollState()) == 1: # SCROLL_STATE_DRAGGING
|
||||||
|
diagnostics.note("injector: user is scrolling, jump settled")
|
||||||
|
return
|
||||||
|
span = self._visible_span()
|
||||||
|
if span is not None:
|
||||||
|
position, first, last = span
|
||||||
|
if not first <= position <= last:
|
||||||
|
diagnostics.note(
|
||||||
|
f"injector: card row {position} scrolled away (visible {first}..{last}), "
|
||||||
|
f"jumping back (check {step})"
|
||||||
|
)
|
||||||
|
self._jump_to_card()
|
||||||
|
elif self.placed and find_existing(get_messages_list(self.chat_activity), self.virtual_id) < 0:
|
||||||
|
# The list was reloaded under the card. Put it back.
|
||||||
|
messages_list = get_messages_list(self.chat_activity)
|
||||||
|
index = find_index_for_anchor(messages_list, self.anchor_msg_id)
|
||||||
|
if index >= 0:
|
||||||
|
diagnostics.note(f"injector: list reloaded, re-placing card (check {step})")
|
||||||
|
messages_list.add(index, self.message_object)
|
||||||
|
adapter = get_chat_adapter(self.chat_activity)
|
||||||
|
if adapter is not None:
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
self._jump_to_card()
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("injector.settle", exc)
|
||||||
|
self._schedule_settle(step + 1)
|
||||||
|
|
||||||
|
def _visible_span(self) -> Optional[tuple]:
|
||||||
|
"""(card adapter position, first visible, last visible), or None."""
|
||||||
|
try:
|
||||||
|
from hook_utils import get_private_field
|
||||||
|
|
||||||
|
messages_list = get_messages_list(self.chat_activity)
|
||||||
|
adapter = get_chat_adapter(self.chat_activity)
|
||||||
|
layout_manager = getattr(self.chat_activity, "chatLayoutManager", None) or get_private_field(
|
||||||
|
self.chat_activity, "chatLayoutManager"
|
||||||
|
)
|
||||||
|
if messages_list is None or adapter is None or layout_manager is None:
|
||||||
|
return None
|
||||||
|
index = find_existing(messages_list, self.virtual_id)
|
||||||
|
if index < 0:
|
||||||
|
return None
|
||||||
|
first = int(layout_manager.findFirstVisibleItemPosition())
|
||||||
|
last = int(layout_manager.findLastVisibleItemPosition())
|
||||||
|
if first < 0 or last < 0:
|
||||||
|
return None
|
||||||
|
return int(adapter.messagesStartRow) + index, min(first, last), max(first, last)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("injector.visible_span", exc)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _load_anchor(self) -> None:
|
||||||
|
"""Asks the chat to load the anchor and scroll to it.
|
||||||
|
|
||||||
|
ChatActivity.scrollToMessageId(id, fromMessageId, select, loadIndex,
|
||||||
|
forceScroll, unreadCount) -- arity verified in the APK. For an
|
||||||
|
unloaded id it requests history around it and jumps there on arrival.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.chat_activity.scrollToMessageId(int(self.anchor_msg_id), 0, False, 0, True, 0)
|
||||||
|
except Exception as exc:
|
||||||
|
diagnostics.record_error("injector.load_anchor", exc)
|
||||||
|
|
||||||
def scroll_into_view(self) -> bool:
|
def scroll_into_view(self) -> bool:
|
||||||
"""Scrolls the chat so the summary card is visible.
|
"""Jumps the chat so the card sits a third of the way down the screen,
|
||||||
|
with its anchor directly above it.
|
||||||
|
|
||||||
Targets the anchor, not the card: the anchor is a real message the app
|
A hard jump, not scrollToMessageId: for a message already in the list
|
||||||
can resolve and load, and the card sits directly above it, so both end
|
that only does smoothScrollBy() from the current cell positions, which
|
||||||
up on screen. ChatActivity.scrollToMessageId(id, fromMessageId,
|
are stale right after an insert (the layout pass has not run yet), so
|
||||||
select, loadIndex, forceScroll, unreadCount) -- arity verified in the
|
it undershoots or does nothing. Setting the layout manager's pending
|
||||||
APK.
|
position is applied on the next layout, after the insert, and is what
|
||||||
|
ChatActivity itself does when it must land on a specific row.
|
||||||
"""
|
"""
|
||||||
if not self.anchor_msg_id:
|
if self._jump_to_card():
|
||||||
|
return True
|
||||||
|
self._load_anchor()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _jump_to_card(self) -> bool:
|
||||||
try:
|
try:
|
||||||
self.chat_activity.scrollToMessageId(
|
from hook_utils import get_private_field
|
||||||
int(self.anchor_msg_id), 0, False, 0, True, 0
|
|
||||||
|
messages_list = get_messages_list(self.chat_activity)
|
||||||
|
adapter = get_chat_adapter(self.chat_activity)
|
||||||
|
layout_manager = getattr(self.chat_activity, "chatLayoutManager", None) or get_private_field(
|
||||||
|
self.chat_activity, "chatLayoutManager"
|
||||||
)
|
)
|
||||||
|
list_view = _chat_list_view(self.chat_activity)
|
||||||
|
if messages_list is None or adapter is None or layout_manager is None:
|
||||||
|
return False
|
||||||
|
index = find_existing(messages_list, self.virtual_id)
|
||||||
|
if index < 0:
|
||||||
|
return False
|
||||||
|
# Public on ChatActivityAdapter; already current, because
|
||||||
|
# notifyDataSetChanged() runs updateRowsInternal() before returning.
|
||||||
|
position = int(adapter.messagesStartRow) + index
|
||||||
|
offset = 0
|
||||||
|
if list_view is not None:
|
||||||
|
try:
|
||||||
|
list_view.stopScroll()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
offset = int(list_view.getMeasuredHeight()) // 3
|
||||||
|
# bottom=False: offset counts from the top edge, as in
|
||||||
|
# ChatActivity.scrollToMessageId's own scrollToPosition call.
|
||||||
|
layout_manager.scrollToPositionWithOffset(position, offset, False)
|
||||||
|
diagnostics.note(f"injector: jump to row {position} offset {offset}")
|
||||||
return True
|
return True
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("injector.scroll_into_view", exc)
|
diagnostics.record_error("injector.jump_to_card", exc)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def update(
|
def update(
|
||||||
@@ -381,6 +561,7 @@ class SummaryInjection:
|
|||||||
thinking: str = "",
|
thinking: str = "",
|
||||||
streaming: bool = True,
|
streaming: bool = True,
|
||||||
status: str = "",
|
status: str = "",
|
||||||
|
tagged: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Re-renders the card in place, without rebuilding the whole list.
|
"""Re-renders the card in place, without rebuilding the whole list.
|
||||||
|
|
||||||
@@ -397,32 +578,28 @@ class SummaryInjection:
|
|||||||
a fallback when the live cell cannot be found.
|
a fallback when the live cell cannot be found.
|
||||||
"""
|
"""
|
||||||
if self.message_object is None:
|
if self.message_object is None:
|
||||||
self.insert(answer_markdown, thinking, streaming, status)
|
self.insert(answer_markdown, thinking, streaming, status, tagged)
|
||||||
return
|
return
|
||||||
|
|
||||||
replacement = build_summary_message(
|
replacement = self._build(answer_markdown, thinking, streaming, status, tagged)
|
||||||
self.account,
|
|
||||||
self.dialog_id,
|
|
||||||
self.topic_id,
|
|
||||||
self.anchor_msg_id,
|
|
||||||
self.virtual_id,
|
|
||||||
answer_markdown,
|
|
||||||
thinking,
|
|
||||||
streaming,
|
|
||||||
status,
|
|
||||||
)
|
|
||||||
if replacement is None:
|
if replacement is None:
|
||||||
return
|
return
|
||||||
self.message_object = replacement
|
self.message_object = replacement
|
||||||
|
|
||||||
|
if not self.placed:
|
||||||
|
# Placement is still waiting for the anchor and will show this
|
||||||
|
# render. If it gave up, give it one more look now: the user may
|
||||||
|
# have scrolled the anchor in since.
|
||||||
|
if not self._placing:
|
||||||
|
self._placing = True
|
||||||
|
_on_ui(lambda: self._place(0))
|
||||||
|
return
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
try:
|
try:
|
||||||
messages_list = get_messages_list(self.chat_activity)
|
messages_list = get_messages_list(self.chat_activity)
|
||||||
inserted = False
|
inserted = False
|
||||||
if messages_list is not None:
|
if messages_list is not None:
|
||||||
# Replace at the current id; drop any card left behind
|
|
||||||
# under an id this injection used earlier, so a reanchor
|
|
||||||
# mid-stream cannot leave two cards on screen.
|
|
||||||
index = find_existing(messages_list, self.virtual_id)
|
index = find_existing(messages_list, self.virtual_id)
|
||||||
if index >= 0:
|
if index >= 0:
|
||||||
messages_list.set(index, replacement)
|
messages_list.set(index, replacement)
|
||||||
@@ -434,16 +611,9 @@ class SummaryInjection:
|
|||||||
return
|
return
|
||||||
messages_list.add(index, replacement)
|
messages_list.add(index, replacement)
|
||||||
inserted = True
|
inserted = True
|
||||||
for stale_id in self._used_ids:
|
|
||||||
if stale_id == self.virtual_id:
|
|
||||||
continue
|
|
||||||
stale = find_existing(messages_list, stale_id)
|
|
||||||
if stale >= 0:
|
|
||||||
messages_list.remove(stale)
|
|
||||||
inserted = True
|
|
||||||
|
|
||||||
# A row was added or removed, so the adapter's item count
|
# A row was added, so the adapter's item count changed and only
|
||||||
# changed and only a full rebind is correct.
|
# a full rebind is correct.
|
||||||
if inserted or not self._force_update_cell(replacement):
|
if inserted or not self._force_update_cell(replacement):
|
||||||
adapter = get_chat_adapter(self.chat_activity)
|
adapter = get_chat_adapter(self.chat_activity)
|
||||||
if adapter is not None:
|
if adapter is not None:
|
||||||
@@ -467,9 +637,7 @@ class SummaryInjection:
|
|||||||
delegate = getattr(self.chat_activity, "chatMessageCellDelegate", None) or get_private_field(
|
delegate = getattr(self.chat_activity, "chatMessageCellDelegate", None) or get_private_field(
|
||||||
self.chat_activity, "chatMessageCellDelegate"
|
self.chat_activity, "chatMessageCellDelegate"
|
||||||
)
|
)
|
||||||
list_view = getattr(self.chat_activity, "chatListView", None) or get_private_field(
|
list_view = _chat_list_view(self.chat_activity)
|
||||||
self.chat_activity, "chatListView"
|
|
||||||
)
|
|
||||||
if delegate is None or list_view is None:
|
if delegate is None or list_view is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -491,79 +659,70 @@ class SummaryInjection:
|
|||||||
diagnostics.record_error("injector._force_update_cell", exc)
|
diagnostics.record_error("injector._force_update_cell", exc)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def reanchor(self, anchor_msg_id: int, virtual_id: int) -> None:
|
|
||||||
"""Re-targets an already-visible card once the analyzed range is known.
|
|
||||||
|
|
||||||
The card is injected on click, before any messages are fetched, so both
|
def _chat_list_view(chat_activity: Any) -> Optional[Any]:
|
||||||
the anchor and the DB row id only become known later. Moving it keeps
|
try:
|
||||||
the promise that a summary sits directly after its newest analyzed
|
from hook_utils import get_private_field
|
||||||
message.
|
|
||||||
|
return getattr(chat_activity, "chatListView", None) or get_private_field(chat_activity, "chatListView")
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _id_span(messages_list: Any) -> str:
|
||||||
|
"""'loaded ids A..B, N rows' for diagnostics."""
|
||||||
|
try:
|
||||||
|
ids = [
|
||||||
|
int(getattr(getattr(messages_list.get(i), "messageOwner", None), "id", 0) or 0)
|
||||||
|
for i in range(messages_list.size())
|
||||||
|
]
|
||||||
|
real = [i for i in ids if i > 0]
|
||||||
|
if not real:
|
||||||
|
return f"no messages, {len(ids)} rows"
|
||||||
|
return f"loaded ids {min(real)}..{max(real)}, {len(ids)} rows"
|
||||||
|
except Exception:
|
||||||
|
return "loaded ids unknown"
|
||||||
|
|
||||||
|
|
||||||
|
def remove_cards(chat_activity: Any, virtual_ids) -> None:
|
||||||
|
"""Drops injected cards from an open chat's list.
|
||||||
|
|
||||||
|
ChatActivity removes deleted messages by looking them up in messagesDict,
|
||||||
|
which injected cards are never added to, so a deleted card would otherwise
|
||||||
|
stay on screen.
|
||||||
"""
|
"""
|
||||||
anchor_msg_id = int(anchor_msg_id)
|
wanted = {int(v) for v in virtual_ids}
|
||||||
virtual_id = int(virtual_id)
|
if chat_activity is None or not wanted:
|
||||||
if anchor_msg_id == self.anchor_msg_id and virtual_id == self.virtual_id:
|
|
||||||
return
|
|
||||||
|
|
||||||
message_object = self.message_object
|
|
||||||
old_virtual_id = self.virtual_id
|
|
||||||
self.anchor_msg_id = anchor_msg_id
|
|
||||||
self.virtual_id = virtual_id
|
|
||||||
self._used_ids.add(virtual_id)
|
|
||||||
if message_object is None:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
try:
|
try:
|
||||||
owner = message_object.messageOwner
|
messages_list = get_messages_list(chat_activity)
|
||||||
messages_list = get_messages_list(self.chat_activity)
|
if messages_list is None:
|
||||||
# Locate the card by its OLD id before rewriting it, otherwise
|
return
|
||||||
# the lookup misses and the card is never moved.
|
removed = 0
|
||||||
current = find_existing(messages_list, old_virtual_id) if messages_list is not None else -1
|
for i in range(messages_list.size() - 1, -1, -1):
|
||||||
|
owner = getattr(messages_list.get(i), "messageOwner", None)
|
||||||
owner.id = virtual_id
|
if owner is not None and int(getattr(owner, "id", 0) or 0) in wanted:
|
||||||
try:
|
messages_list.remove(i)
|
||||||
header = _tlrpc().TL_messageReplyHeader()
|
removed += 1
|
||||||
header.reply_to_msg_id = anchor_msg_id
|
if removed:
|
||||||
if self.topic_id:
|
adapter = get_chat_adapter(chat_activity)
|
||||||
header.reply_to_top_id = int(self.topic_id)
|
|
||||||
owner.reply_to = header
|
|
||||||
except Exception as exc:
|
|
||||||
diagnostics.record_error("injector.reanchor.reply_header", exc)
|
|
||||||
|
|
||||||
if messages_list is not None and current >= 0:
|
|
||||||
messages_list.remove(current)
|
|
||||||
# A card may already sit under the new id -- e.g. the
|
|
||||||
# restore hook re-injected this summary's saved row. Claim
|
|
||||||
# that id rather than ending up with two cards.
|
|
||||||
duplicate = find_existing(messages_list, virtual_id)
|
|
||||||
if duplicate >= 0:
|
|
||||||
messages_list.remove(duplicate)
|
|
||||||
# The card was just removed, so it must go back somewhere:
|
|
||||||
# the new anchor came from the loaded list and is normally
|
|
||||||
# present, but if it paged out in between, fall back to id
|
|
||||||
# ordering rather than dropping the card entirely.
|
|
||||||
target = find_index_for_anchor(messages_list, anchor_msg_id)
|
|
||||||
if target < 0:
|
|
||||||
target = find_index_for_anchor(
|
|
||||||
messages_list, anchor_msg_id, require_anchor=False
|
|
||||||
)
|
|
||||||
messages_list.add(max(target, 0), message_object)
|
|
||||||
adapter = get_chat_adapter(self.chat_activity)
|
|
||||||
if adapter is not None:
|
if adapter is not None:
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
# The card just moved to the real end of the analyzed range,
|
|
||||||
# which may be somewhere else entirely, so follow it there.
|
|
||||||
self.scroll_into_view()
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
diagnostics.record_error("injector.reanchor", exc)
|
diagnostics.record_error("injector.remove_cards", exc)
|
||||||
|
|
||||||
_on_ui(run)
|
_on_ui(run)
|
||||||
|
|
||||||
|
|
||||||
def _on_ui(fn) -> None:
|
def _on_ui(fn, delay_ms: int = 0) -> None:
|
||||||
try:
|
try:
|
||||||
from android_utils import run_on_ui_thread
|
from android_utils import run_on_ui_thread
|
||||||
|
|
||||||
|
if delay_ms:
|
||||||
|
run_on_ui_thread(fn, delay_ms)
|
||||||
|
else:
|
||||||
run_on_ui_thread(fn)
|
run_on_ui_thread(fn)
|
||||||
except Exception:
|
except Exception:
|
||||||
fn()
|
fn()
|
||||||
@@ -621,6 +780,7 @@ def restore_saved_summaries(chat_activity: Any, account: int, dialog_id: int, to
|
|||||||
summary.text,
|
summary.text,
|
||||||
summary.thinking,
|
summary.thinking,
|
||||||
False,
|
False,
|
||||||
|
tagged=True,
|
||||||
)
|
)
|
||||||
if message_object is not None:
|
if message_object is not None:
|
||||||
pending.append((summary.anchor_msg_id, virtual_id, message_object))
|
pending.append((summary.anchor_msg_id, virtual_id, message_object))
|
||||||
|
|||||||
@@ -296,3 +296,38 @@ def _thinking_details(TL_iv: Any, thinking: str) -> Any:
|
|||||||
if not details.blocks:
|
if not details.blocks:
|
||||||
details.blocks.add(_block(TL_iv, "pageBlockParagraph", build_rich_text(thinking, TL_iv)))
|
details.blocks.add(_block(TL_iv, "pageBlockParagraph", build_rich_text(thinking, TL_iv)))
|
||||||
return details
|
return details
|
||||||
|
|
||||||
|
|
||||||
|
_PLAIN_BLOCK_RE = re.compile(r"^\s{0,3}(?:#{1,6}\s+|>\s?)", re.MULTILINE)
|
||||||
|
_FENCE_LINE_RE = re.compile(r"^\s*```+.*$\n?", re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
|
def markdown_to_plain(markdown: str) -> str:
|
||||||
|
"""Markdown reduced to the text a reader sees, for the message's plain body.
|
||||||
|
|
||||||
|
Links keep their label, emphasis markers and heading/quote prefixes are
|
||||||
|
dropped. List bullets stay: they still read naturally as plain text.
|
||||||
|
"""
|
||||||
|
text = _FENCE_LINE_RE.sub("", markdown or "")
|
||||||
|
text = _PLAIN_BLOCK_RE.sub("", text)
|
||||||
|
|
||||||
|
def inline(m: "re.Match") -> str:
|
||||||
|
for body in ("code_body", "link_text", "bold_body", "strike_body", "em_body"):
|
||||||
|
if m.group(body) is not None:
|
||||||
|
return markdown_to_plain(m.group(body)) if body != "code_body" else m.group(body)
|
||||||
|
return m.group(0)
|
||||||
|
|
||||||
|
return _INLINE_RE.sub(inline, text).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def hashtag_block(tag: str, TL_iv: Optional[Any] = None) -> Any:
|
||||||
|
"""A paragraph holding one native hashtag node.
|
||||||
|
|
||||||
|
TL_iv.textHashtag is the rich-text counterpart of a hashtag entity
|
||||||
|
(verified in the APK: it carries only `text`), so the layout styles it
|
||||||
|
and handles taps on it the way it does any other hashtag.
|
||||||
|
"""
|
||||||
|
TL_iv = TL_iv or _tl_iv()
|
||||||
|
node = TL_iv.textHashtag()
|
||||||
|
node.text = _plain(TL_iv, tag)
|
||||||
|
return _block(TL_iv, "pageBlockParagraph", node)
|
||||||
@@ -8,7 +8,7 @@ message when the chat is reopened.
|
|||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
from typing import List, Optional
|
from typing import List, Optional, Tuple
|
||||||
|
|
||||||
DB_DIRNAME = "chat_summaries"
|
DB_DIRNAME = "chat_summaries"
|
||||||
DB_FILENAME = "summaries.db"
|
DB_FILENAME = "summaries.db"
|
||||||
@@ -107,6 +107,32 @@ class SummaryDb:
|
|||||||
).fetchone()
|
).fetchone()
|
||||||
return int(row["id"])
|
return int(row["id"])
|
||||||
|
|
||||||
|
def reserve(self, dialog_id: int, topic_id: int, anchor_msg_id: int, msg_count: int = 0) -> Tuple[int, bool]:
|
||||||
|
"""Claims a row id for a summary that is about to stream.
|
||||||
|
|
||||||
|
Returns (row id, created). An existing summary for the same anchor is
|
||||||
|
left untouched, so re-summarizing a range does not wipe the saved text
|
||||||
|
before the new one has actually arrived; a stream that fails then keeps
|
||||||
|
the previous summary instead of leaving an empty row behind.
|
||||||
|
"""
|
||||||
|
now = int(time.time())
|
||||||
|
with self._connect() as conn:
|
||||||
|
cur = conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO summaries
|
||||||
|
(dialog_id, topic_id, anchor_msg_id, text, thinking, msg_count, created_at)
|
||||||
|
VALUES (?, ?, ?, '', '', ?, ?)
|
||||||
|
ON CONFLICT (dialog_id, topic_id, anchor_msg_id) DO NOTHING
|
||||||
|
""",
|
||||||
|
(int(dialog_id), int(topic_id), int(anchor_msg_id), int(msg_count), now),
|
||||||
|
)
|
||||||
|
created = cur.rowcount > 0
|
||||||
|
row = conn.execute(
|
||||||
|
"SELECT id FROM summaries WHERE dialog_id = ? AND topic_id = ? AND anchor_msg_id = ?",
|
||||||
|
(int(dialog_id), int(topic_id), int(anchor_msg_id)),
|
||||||
|
).fetchone()
|
||||||
|
return int(row["id"]), created
|
||||||
|
|
||||||
def get_for_scope(self, dialog_id: int, topic_id: int = 0) -> List[StoredSummary]:
|
def get_for_scope(self, dialog_id: int, topic_id: int = 0) -> List[StoredSummary]:
|
||||||
"""All summaries for a chat/topic, oldest anchor first."""
|
"""All summaries for a chat/topic, oldest anchor first."""
|
||||||
with self._connect() as conn:
|
with self._connect() as conn:
|
||||||
@@ -120,6 +146,15 @@ class SummaryDb:
|
|||||||
).fetchall()
|
).fetchall()
|
||||||
return [_row_to_summary(r) for r in rows]
|
return [_row_to_summary(r) for r in rows]
|
||||||
|
|
||||||
|
def get_for_dialog(self, dialog_id: int) -> List[StoredSummary]:
|
||||||
|
"""All summaries for a chat across every topic."""
|
||||||
|
with self._connect() as conn:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT * FROM summaries WHERE dialog_id = ? ORDER BY anchor_msg_id ASC",
|
||||||
|
(int(dialog_id),),
|
||||||
|
).fetchall()
|
||||||
|
return [_row_to_summary(r) for r in rows]
|
||||||
|
|
||||||
def get(self, summary_id: int) -> Optional[StoredSummary]:
|
def get(self, summary_id: int) -> Optional[StoredSummary]:
|
||||||
with self._connect() as conn:
|
with self._connect() as conn:
|
||||||
row = conn.execute("SELECT * FROM summaries WHERE id = ?", (int(summary_id),)).fetchone()
|
row = conn.execute("SELECT * FROM summaries WHERE id = ?", (int(summary_id),)).fetchone()
|
||||||
@@ -138,6 +173,33 @@ class SummaryDb:
|
|||||||
)
|
)
|
||||||
return cur.rowcount
|
return cur.rowcount
|
||||||
|
|
||||||
|
def clear_all(self) -> int:
|
||||||
|
"""Deletes every stored summary and compacts the file. Returns rows removed."""
|
||||||
|
with self._connect() as conn:
|
||||||
|
cur = conn.execute("DELETE FROM summaries")
|
||||||
|
removed = cur.rowcount
|
||||||
|
# VACUUM cannot run inside a transaction, and the `with` block above
|
||||||
|
# commits on exit, so it gets a connection of its own.
|
||||||
|
conn = self._connect()
|
||||||
|
try:
|
||||||
|
conn.isolation_level = None
|
||||||
|
conn.execute("VACUUM")
|
||||||
|
finally:
|
||||||
|
conn.close()
|
||||||
|
return removed
|
||||||
|
|
||||||
|
def stats(self) -> Tuple[int, int]:
|
||||||
|
"""(row count, approximate on-disk size in bytes)."""
|
||||||
|
with self._connect() as conn:
|
||||||
|
count = int(conn.execute("SELECT COUNT(*) FROM summaries").fetchone()[0])
|
||||||
|
size = 0
|
||||||
|
for suffix in ("", "-wal", "-journal"):
|
||||||
|
try:
|
||||||
|
size += os.path.getsize(self.db_path + suffix)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
return count, size
|
||||||
|
|
||||||
|
|
||||||
def _row_to_summary(row: sqlite3.Row) -> StoredSummary:
|
def _row_to_summary(row: sqlite3.Row) -> StoredSummary:
|
||||||
return StoredSummary(
|
return StoredSummary(
|
||||||
|
|||||||
Executable → Regular
+46
@@ -2,6 +2,9 @@
|
|||||||
"""Bundle a multi-file exteraGram plugin (plugin.json + src/) into a single .plugin file.
|
"""Bundle a multi-file exteraGram plugin (plugin.json + src/) into a single .plugin file.
|
||||||
|
|
||||||
Usage: python3 tools/bundle.py [--dev|--release] [--out DIR] [--check]
|
Usage: python3 tools/bundle.py [--dev|--release] [--out DIR] [--check]
|
||||||
|
|
||||||
|
Also enables the repo's git hooks (scripts/pre-commit, scripts/post-commit) on
|
||||||
|
first run; see ensure_git_hooks().
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -9,13 +12,18 @@ import argparse
|
|||||||
import ast
|
import ast
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
import stat
|
||||||
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import zlib
|
import zlib
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
REPO_ROOT = Path(__file__).resolve().parent.parent
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||||
SRC_DIR = REPO_ROOT / "src"
|
SRC_DIR = REPO_ROOT / "src"
|
||||||
|
HOOKS_DIR = "scripts"
|
||||||
|
HOOK_NAMES = ("pre-commit", "post-commit")
|
||||||
PLUGIN_JSON = REPO_ROOT / "plugin.json"
|
PLUGIN_JSON = REPO_ROOT / "plugin.json"
|
||||||
|
|
||||||
RECOGNIZED_KEYS = {
|
RECOGNIZED_KEYS = {
|
||||||
@@ -300,6 +308,43 @@ def assemble_bundle(data: dict, modules: dict, release: bool) -> str:
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_git_hooks() -> None:
|
||||||
|
"""Point git at scripts/ for hooks, once per clone.
|
||||||
|
|
||||||
|
Git never enables hooks from repository content, so a fresh clone (or a
|
||||||
|
repo created from the template) starts without them. Every workflow runs
|
||||||
|
this bundler first, directly or via scripts/deploy.sh and scripts/watch.sh,
|
||||||
|
which makes it the one place that can switch them on without an extra
|
||||||
|
setup step. Skipped in CI, outside a git work tree, and when
|
||||||
|
core.hooksPath is already set (including to something else on purpose).
|
||||||
|
Never fails the build.
|
||||||
|
"""
|
||||||
|
if os.environ.get("CI"):
|
||||||
|
return
|
||||||
|
hooks = [REPO_ROOT / HOOKS_DIR / name for name in HOOK_NAMES]
|
||||||
|
if not any(h.is_file() for h in hooks):
|
||||||
|
return
|
||||||
|
|
||||||
|
def git(*args):
|
||||||
|
return subprocess.run(["git", *args], cwd=REPO_ROOT, capture_output=True, text=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
top = git("rev-parse", "--show-toplevel")
|
||||||
|
if top.returncode != 0 or Path(top.stdout.strip()).resolve() != REPO_ROOT:
|
||||||
|
return
|
||||||
|
if git("config", "--get", "core.hooksPath").stdout.strip():
|
||||||
|
return
|
||||||
|
# Git silently skips hooks that are not executable.
|
||||||
|
for hook in hooks:
|
||||||
|
if hook.is_file():
|
||||||
|
mode = hook.stat().st_mode
|
||||||
|
hook.chmod(mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
|
||||||
|
if git("config", "core.hooksPath", HOOKS_DIR).returncode == 0:
|
||||||
|
print(f"enabled git hooks from {HOOKS_DIR}/ (git config core.hooksPath {HOOKS_DIR})", file=sys.stderr)
|
||||||
|
except (OSError, subprocess.SubprocessError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def parse_args(argv):
|
def parse_args(argv):
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Bundle a multi-file exteraGram plugin into a single .plugin file."
|
description="Bundle a multi-file exteraGram plugin into a single .plugin file."
|
||||||
@@ -318,6 +363,7 @@ def parse_args(argv):
|
|||||||
|
|
||||||
def main(argv=None) -> int:
|
def main(argv=None) -> int:
|
||||||
args = parse_args(sys.argv[1:] if argv is None else argv)
|
args = parse_args(sys.argv[1:] if argv is None else argv)
|
||||||
|
ensure_git_hooks()
|
||||||
release = bool(args.release)
|
release = bool(args.release)
|
||||||
try:
|
try:
|
||||||
data = load_metadata()
|
data = load_metadata()
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Renders images/icon.svg to a square PNG (512x512 by default).
|
||||||
|
|
||||||
|
Stdlib only: the actual rasterising is delegated to the first renderer found,
|
||||||
|
in this order: the cairosvg Python package, rsvg-convert (librsvg),
|
||||||
|
ImageMagick (magick/convert), Inkscape. Install any one of them.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
tools/icon_png.py # images/icon.svg -> images/icon.png, 512x512
|
||||||
|
tools/icon_png.py --resolution 1024 # 1024x1024
|
||||||
|
tools/icon_png.py other.svg -o out.png # explicit input/output
|
||||||
|
|
||||||
|
The output is always PNG, always <resolution>x<resolution>.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
||||||
|
DEFAULT_SVG = REPO_ROOT / "images" / "icon.svg"
|
||||||
|
DEFAULT_RESOLUTION = 512
|
||||||
|
|
||||||
|
|
||||||
|
def fail(message: str) -> "None":
|
||||||
|
print(f"error: {message}", file=sys.stderr)
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def cli_command(svg: Path, png: Path, size: int) -> Optional[List[str]]:
|
||||||
|
"""Returns the command line for the first installed CLI renderer, if any."""
|
||||||
|
if shutil.which("rsvg-convert"):
|
||||||
|
return ["rsvg-convert", "-w", str(size), "-h", str(size), "-f", "png",
|
||||||
|
"-o", str(png), str(svg)]
|
||||||
|
magick = shutil.which("magick") or shutil.which("convert")
|
||||||
|
if magick:
|
||||||
|
# A high density rasterises the vector sharply before scaling down;
|
||||||
|
# "!" forces the exact size.
|
||||||
|
return [magick, "-background", "none", "-density", "1200", str(svg),
|
||||||
|
"-resize", f"{size}x{size}!", f"png:{png}"]
|
||||||
|
if shutil.which("inkscape"):
|
||||||
|
return ["inkscape", str(svg), "--export-type=png", f"--export-filename={png}",
|
||||||
|
"-w", str(size), "-h", str(size)]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def render(svg: Path, png: Path, size: int) -> str:
|
||||||
|
"""Renders svg into png at size x size, returning the renderer's name."""
|
||||||
|
try:
|
||||||
|
import cairosvg # type: ignore[import-not-found]
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
cairosvg.svg2png(url=str(svg), write_to=str(png),
|
||||||
|
output_width=size, output_height=size)
|
||||||
|
return "cairosvg"
|
||||||
|
|
||||||
|
command = cli_command(svg, png, size)
|
||||||
|
if command is None:
|
||||||
|
fail("no SVG renderer found; install one of: cairosvg (pip), "
|
||||||
|
"rsvg-convert (librsvg), ImageMagick, Inkscape")
|
||||||
|
result = subprocess.run(command, capture_output=True, text=True)
|
||||||
|
if result.returncode != 0:
|
||||||
|
fail(f"{Path(command[0]).name} failed:\n{result.stderr.strip()}")
|
||||||
|
return Path(command[0]).name
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
parser = argparse.ArgumentParser(description="Render an SVG icon to a square PNG.")
|
||||||
|
parser.add_argument("svg", nargs="?", type=Path, default=DEFAULT_SVG,
|
||||||
|
help="input SVG (default: images/icon.svg)")
|
||||||
|
parser.add_argument("-r", "--resolution", type=int, default=DEFAULT_RESOLUTION,
|
||||||
|
help=f"width and height in pixels (default: {DEFAULT_RESOLUTION})")
|
||||||
|
parser.add_argument("-o", "--output", type=Path,
|
||||||
|
help="output PNG (default: the input path with a .png suffix)")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.resolution <= 0:
|
||||||
|
fail("--resolution must be a positive integer")
|
||||||
|
if not args.svg.is_file():
|
||||||
|
fail(f"{args.svg} not found")
|
||||||
|
png = args.output or args.svg.with_suffix(".png")
|
||||||
|
if png.suffix.lower() != ".png":
|
||||||
|
fail(f"output must be a .png file, got {png}")
|
||||||
|
png.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
renderer = render(args.svg, png, args.resolution)
|
||||||
|
print(f"{png} ({args.resolution}x{args.resolution}, via {renderer})")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
+514
-107
@@ -162,6 +162,7 @@ class _TLObj:
|
|||||||
self.blocks = _JavaList()
|
self.blocks = _JavaList()
|
||||||
self.items = _JavaList()
|
self.items = _JavaList()
|
||||||
self.texts = _JavaList()
|
self.texts = _JavaList()
|
||||||
|
self.entities = _JavaList()
|
||||||
self.flags = 0
|
self.flags = 0
|
||||||
self.flags2 = 0
|
self.flags2 = 0
|
||||||
|
|
||||||
@@ -254,6 +255,7 @@ def _fake_send_request(request, callback, *, account=None):
|
|||||||
|
|
||||||
_KNOWN_METHODS = {
|
_KNOWN_METHODS = {
|
||||||
"org.telegram.ui.LaunchActivity": ["dispatchTouchEvent"],
|
"org.telegram.ui.LaunchActivity": ["dispatchTouchEvent"],
|
||||||
|
"org.telegram.messenger.MessagesController": ["deleteMessages", "deleteMessages"],
|
||||||
"org.telegram.ui.ChatActivity": [
|
"org.telegram.ui.ChatActivity": [
|
||||||
"onPageDownClicked",
|
"onPageDownClicked",
|
||||||
"updateMessagesVisiblePart",
|
"updateMessagesVisiblePart",
|
||||||
@@ -343,9 +345,16 @@ class FakeAlertDialogBuilder:
|
|||||||
BUTTON_POSITIVE = 1
|
BUTTON_POSITIVE = 1
|
||||||
BUTTON_NEGATIVE = 2
|
BUTTON_NEGATIVE = 2
|
||||||
|
|
||||||
|
built: List[Any] = []
|
||||||
|
|
||||||
def __init__(self, activity, progress_style=0, resources_provider=None):
|
def __init__(self, activity, progress_style=0, resources_provider=None):
|
||||||
self.activity = activity
|
self.activity = activity
|
||||||
self._view = None
|
self._view = None
|
||||||
|
self._positive = None
|
||||||
|
self._negative = None
|
||||||
|
self.red_buttons = []
|
||||||
|
self.shown = False
|
||||||
|
FakeAlertDialogBuilder.built.append(self)
|
||||||
|
|
||||||
def set_title(self, *a, **k):
|
def set_title(self, *a, **k):
|
||||||
return self
|
return self
|
||||||
@@ -372,8 +381,12 @@ class FakeAlertDialogBuilder:
|
|||||||
return self
|
return self
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
|
self.shown = True
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def make_button_red(self, button_type):
|
||||||
|
self.red_buttons.append(button_type)
|
||||||
|
|
||||||
def dismiss(self):
|
def dismiss(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -572,6 +585,7 @@ def install_fakes() -> None:
|
|||||||
"TL_peerChannel": staticmethod(lambda: _TLObj("TL_peerChannel")),
|
"TL_peerChannel": staticmethod(lambda: _TLObj("TL_peerChannel")),
|
||||||
"TL_messageReplyHeader": staticmethod(lambda: _TLObj("TL_messageReplyHeader")),
|
"TL_messageReplyHeader": staticmethod(lambda: _TLObj("TL_messageReplyHeader")),
|
||||||
"TL_emojiStatus": staticmethod(lambda: _TLObj("TL_emojiStatus")),
|
"TL_emojiStatus": staticmethod(lambda: _TLObj("TL_emojiStatus")),
|
||||||
|
"TL_messageEntityHashtag": staticmethod(lambda: _TLObj("TL_messageEntityHashtag")),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
sys.modules["org.telegram.tgnet"] = tgnet_mod
|
sys.modules["org.telegram.tgnet"] = tgnet_mod
|
||||||
@@ -588,7 +602,7 @@ def install_fakes() -> None:
|
|||||||
"textFixed textUnderline textUrl pageBlockParagraph pageBlockHeading1 "
|
"textFixed textUnderline textUrl pageBlockParagraph pageBlockHeading1 "
|
||||||
"pageBlockHeading2 pageBlockHeading3 pageBlockPreformatted pageBlockBlockquote "
|
"pageBlockHeading2 pageBlockHeading3 pageBlockPreformatted pageBlockBlockquote "
|
||||||
"pageBlockDivider pageBlockThinking pageBlockList pageBlockOrderedList "
|
"pageBlockDivider pageBlockThinking pageBlockList pageBlockOrderedList "
|
||||||
"pageBlockDetails TL_pageListItemText TL_pageListOrderedItemText"
|
"pageBlockDetails TL_pageListItemText TL_pageListOrderedItemText textHashtag"
|
||||||
).split()
|
).split()
|
||||||
tgnet_tl_mod.TL_iv = type(
|
tgnet_tl_mod.TL_iv = type(
|
||||||
"TL_iv",
|
"TL_iv",
|
||||||
@@ -711,6 +725,242 @@ def expected_dunders(meta: dict) -> Dict[str, Any]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def run_pipeline_checks(plugin, injector, summary_db_mod, chat_activity_cls, message_cls) -> None:
|
||||||
|
"""Drives the whole Summarize pipeline: fetch -> stream -> card.
|
||||||
|
|
||||||
|
The rule under test: the chat is not touched until the endpoint has
|
||||||
|
actually started answering. Every earlier failure is a bulletin only.
|
||||||
|
"""
|
||||||
|
if plugin is None or message_cls is None:
|
||||||
|
check("pipeline checks need the plugin class", False, "plugin not loaded")
|
||||||
|
return
|
||||||
|
|
||||||
|
dialog_id = -4242
|
||||||
|
requests_mod = sys.modules["requests"]
|
||||||
|
real_post = requests_mod.post
|
||||||
|
cu_mod = sys.modules["client_utils"]
|
||||||
|
real_last_fragment = cu_mod.get_last_fragment
|
||||||
|
plugin.set_setting("custom_base_url", "https://example.test/v1")
|
||||||
|
|
||||||
|
def fresh_db():
|
||||||
|
plugin.summary_db = summary_db_mod.SummaryDb(str(Path(tempfile.mkdtemp()) / "summaries.db"))
|
||||||
|
return plugin.summary_db
|
||||||
|
|
||||||
|
def feed(messages=None, error=None):
|
||||||
|
plugin.fetcher.fetch_history = lambda d, c, o, t, cb: cb(messages, error)
|
||||||
|
|
||||||
|
def run(activity):
|
||||||
|
BULLETINS.clear()
|
||||||
|
plugin._run_summary(None, activity, dialog_id, 0, 3, 0, "Summarize")
|
||||||
|
|
||||||
|
analyzed = [
|
||||||
|
message_cls("Alice", "a", id=500, sender_id=1),
|
||||||
|
message_cls("Bob", "b", id=555, sender_id=2),
|
||||||
|
message_cls("Alice", "c", id=520, sender_id=1),
|
||||||
|
]
|
||||||
|
|
||||||
|
class _ErrorResponse:
|
||||||
|
status_code = 500
|
||||||
|
text = "upstream exploded"
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class _BreakingResponse:
|
||||||
|
status_code = 200
|
||||||
|
text = ""
|
||||||
|
|
||||||
|
def iter_lines(self):
|
||||||
|
yield b'data: {"choices":[{"delta":{"content":"Partial"}}]}'
|
||||||
|
raise IOError("connection reset")
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 1. History fetch fails: bulletin only, chat untouched.
|
||||||
|
db = fresh_db()
|
||||||
|
activity = chat_activity_cls([600, 555, 500])
|
||||||
|
feed(error="flood wait")
|
||||||
|
run(activity)
|
||||||
|
check(
|
||||||
|
"a failed history fetch shows a bulletin and leaves the chat untouched",
|
||||||
|
activity.ids() == [600, 555, 500]
|
||||||
|
and activity.chatLayoutManager.jumps == []
|
||||||
|
and activity.scrolled_to == []
|
||||||
|
and ("error", "Chat Summaries: flood wait") in BULLETINS,
|
||||||
|
f"ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
|
||||||
|
feed(messages=[])
|
||||||
|
run(activity)
|
||||||
|
check(
|
||||||
|
"an empty history shows a bulletin and leaves the chat untouched",
|
||||||
|
activity.ids() == [600, 555, 500] and any("no messages" in b[1] for b in BULLETINS),
|
||||||
|
f"ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 2. Endpoint fails before the first chunk (HTTP error, unreachable
|
||||||
|
# host): no card, no scroll, no DB row.
|
||||||
|
feed(messages=analyzed)
|
||||||
|
requests_mod.post = lambda *a, **k: _ErrorResponse()
|
||||||
|
run(activity)
|
||||||
|
check(
|
||||||
|
"an HTTP error before the first chunk injects nothing",
|
||||||
|
activity.ids() == [600, 555, 500]
|
||||||
|
and activity.chatLayoutManager.jumps == []
|
||||||
|
and any("HTTP 500" in b[1] for b in BULLETINS if b[0] == "error"),
|
||||||
|
f"ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
|
||||||
|
def unreachable(*a, **k):
|
||||||
|
raise ConnectionError("no route to host")
|
||||||
|
|
||||||
|
requests_mod.post = unreachable
|
||||||
|
run(activity)
|
||||||
|
check(
|
||||||
|
"an unreachable endpoint injects nothing and reports the failure",
|
||||||
|
activity.ids() == [600, 555, 500]
|
||||||
|
and any("request failed" in b[1] for b in BULLETINS if b[0] == "error"),
|
||||||
|
f"ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"a failure before the first chunk leaves no row in the store",
|
||||||
|
db.stats()[0] == 0,
|
||||||
|
f"rows={db.stats()[0]}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 3. Success: the card appears right after the newest analyzed message
|
||||||
|
# (555, not the chat's newest 600), the chat jumps to it once, and the
|
||||||
|
# finished summary is tagged and persisted.
|
||||||
|
requests_mod.post = real_post
|
||||||
|
plugin.restore_hook._scope_state["done"].add(("stale",))
|
||||||
|
run(activity)
|
||||||
|
ids = activity.ids()
|
||||||
|
cards = [i for i in ids if i < 0]
|
||||||
|
card_index = ids.index(cards[0]) if cards else -1
|
||||||
|
check(
|
||||||
|
"a successful stream places exactly one card directly after the newest analyzed message",
|
||||||
|
len(cards) == 1 and card_index == ids.index(555) - 1,
|
||||||
|
f"ids={ids}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"the chat hard-jumps to the card exactly once",
|
||||||
|
activity.chatLayoutManager.jumps == [(2 + card_index, 300, False)],
|
||||||
|
f"jumps={activity.chatLayoutManager.jumps}",
|
||||||
|
)
|
||||||
|
final_owner = activity.messages.get(card_index).messageOwner if cards else None
|
||||||
|
check(
|
||||||
|
"the finished card is tagged #aisummaries and carries the answer",
|
||||||
|
final_owner is not None
|
||||||
|
and final_owner.message == "Fake summary.\n\n#aisummaries",
|
||||||
|
f"message={getattr(final_owner, 'message', None)!r}",
|
||||||
|
)
|
||||||
|
rows = db.get_for_scope(dialog_id, 0)
|
||||||
|
check(
|
||||||
|
"the finished summary is saved under the analyzed anchor without the tag",
|
||||||
|
len(rows) == 1 and rows[0].anchor_msg_id == 555 and rows[0].text == "Fake summary."
|
||||||
|
and cards and cards[0] == injector.virtual_id_for(rows[0].id, 555),
|
||||||
|
f"rows={rows}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"saving a summary invalidates the restore cache",
|
||||||
|
("stale",) not in plugin.restore_hook._scope_state["done"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# 4. Re-running a failing stream over a saved range keeps the saved text.
|
||||||
|
requests_mod.post = lambda *a, **k: _BreakingResponse()
|
||||||
|
run(activity)
|
||||||
|
rows = db.get_for_scope(dialog_id, 0)
|
||||||
|
check(
|
||||||
|
"a stream that breaks mid-way does not wipe the previously saved summary",
|
||||||
|
len(rows) == 1 and rows[0].text == "Fake summary.",
|
||||||
|
f"rows={rows}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"a mid-stream failure reuses the existing card and reports the error on it",
|
||||||
|
len([i for i in activity.ids() if i < 0]) == 1
|
||||||
|
and "Error:" in (activity.messages.get(card_index).messageOwner.message or "")
|
||||||
|
and any("stream failed" in b[1] for b in BULLETINS if b[0] == "error"),
|
||||||
|
f"ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# A fresh range that breaks mid-way leaves no empty row behind.
|
||||||
|
broken_activity = chat_activity_cls([700, 690])
|
||||||
|
feed(messages=[message_cls("Alice", "x", id=690, sender_id=1)])
|
||||||
|
run(broken_activity)
|
||||||
|
check(
|
||||||
|
"a fresh stream that breaks mid-way removes its reserved row",
|
||||||
|
[r.anchor_msg_id for r in db.get_for_dialog(dialog_id)] == [555],
|
||||||
|
f"rows={db.get_for_dialog(dialog_id)}",
|
||||||
|
)
|
||||||
|
requests_mod.post = real_post
|
||||||
|
|
||||||
|
# 5. Deleting the card through the app's own delete path forgets it.
|
||||||
|
card_id = cards[0]
|
||||||
|
cu_mod.get_last_fragment = lambda: activity
|
||||||
|
delete_hooks = [
|
||||||
|
hook for method, hook in HOOKS_INSTALLED
|
||||||
|
if getattr(method, "class_name", "") == "org.telegram.messenger.MessagesController"
|
||||||
|
]
|
||||||
|
BULLETINS.clear()
|
||||||
|
plugin.restore_hook._scope_state["done"].add(("stale",))
|
||||||
|
param = types.SimpleNamespace(
|
||||||
|
args=[_JavaArrayList([card_id, 600]), None, None, dialog_id, False, 0]
|
||||||
|
)
|
||||||
|
for hook in delete_hooks:
|
||||||
|
hook.before_hooked_method(param)
|
||||||
|
check(
|
||||||
|
"deleting a summary card removes its row, its card, and says so once",
|
||||||
|
db.get_for_dialog(dialog_id) == []
|
||||||
|
and card_id not in activity.ids()
|
||||||
|
and BULLETINS == [("info", "Summary deleted")]
|
||||||
|
and ("stale",) not in plugin.restore_hook._scope_state["done"],
|
||||||
|
f"rows={db.get_for_dialog(dialog_id)} ids={activity.ids()} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
BULLETINS.clear()
|
||||||
|
for hook in delete_hooks:
|
||||||
|
hook.before_hooked_method(types.SimpleNamespace(args=[_JavaArrayList([600]), None, None, dialog_id]))
|
||||||
|
check(
|
||||||
|
"deleting ordinary messages never touches the summary store",
|
||||||
|
BULLETINS == [],
|
||||||
|
f"bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# 6. Clear all: confirmed through a red-buttoned alert.
|
||||||
|
db.save(dialog_id, 0, 555, "one", "", 1)
|
||||||
|
db.save(-1, 0, 7, "two", "", 1)
|
||||||
|
count, size = db.stats()
|
||||||
|
check("stats report row count and a non-zero file size", count == 2 and size > 0, f"{count} {size}")
|
||||||
|
|
||||||
|
class _Fragment:
|
||||||
|
def getParentActivity(self):
|
||||||
|
return object()
|
||||||
|
|
||||||
|
cu_mod.get_last_fragment = lambda: _Fragment()
|
||||||
|
FakeAlertDialogBuilder.built.clear()
|
||||||
|
BULLETINS.clear()
|
||||||
|
plugin._on_clear_all_click(None)
|
||||||
|
dialog = FakeAlertDialogBuilder.built[-1] if FakeAlertDialogBuilder.built else None
|
||||||
|
check(
|
||||||
|
"Clear all asks for confirmation with a red button before deleting",
|
||||||
|
dialog is not None and dialog.shown
|
||||||
|
and dialog.red_buttons == [FakeAlertDialogBuilder.BUTTON_POSITIVE]
|
||||||
|
and db.stats()[0] == 2,
|
||||||
|
f"dialog={getattr(dialog, '__dict__', None)}",
|
||||||
|
)
|
||||||
|
if dialog is not None and dialog._positive is not None:
|
||||||
|
dialog._positive(dialog, FakeAlertDialogBuilder.BUTTON_POSITIVE)
|
||||||
|
check(
|
||||||
|
"confirming Clear all wipes every summary and reports it",
|
||||||
|
db.stats()[0] == 0 and ("success", "Deleted 2 summaries") in BULLETINS,
|
||||||
|
f"rows={db.stats()[0]} bulletins={BULLETINS}",
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
requests_mod.post = real_post
|
||||||
|
cu_mod.get_last_fragment = real_last_fragment
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None) -> int:
|
def main(argv=None) -> int:
|
||||||
argv = sys.argv[1:] if argv is None else argv
|
argv = sys.argv[1:] if argv is None else argv
|
||||||
if len(argv) != 1:
|
if len(argv) != 1:
|
||||||
@@ -1007,44 +1257,26 @@ def main(argv=None) -> int:
|
|||||||
f"page_results={page_results}",
|
f"page_results={page_results}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Provisional anchor, chosen before the fetch returns: with an offset
|
|
||||||
# the analyzed range ends partway up the history, so the guess has to
|
|
||||||
# skip that many messages instead of grabbing the newest.
|
|
||||||
class _AnchorList(list):
|
|
||||||
def size(self):
|
|
||||||
return len(self)
|
|
||||||
|
|
||||||
def get(self, index):
|
|
||||||
return self[index]
|
|
||||||
|
|
||||||
class _AnchorActivity:
|
|
||||||
def __init__(self, ids):
|
|
||||||
self.messages = _AnchorList()
|
|
||||||
for mid in ids:
|
|
||||||
owner_obj = _TLObj("TL_message")
|
|
||||||
owner_obj.id = mid
|
|
||||||
holder = types.SimpleNamespace(messageOwner=owner_obj)
|
|
||||||
self.messages.append(holder)
|
|
||||||
|
|
||||||
anchor_activity = _AnchorActivity([900, 800, 700, 600])
|
|
||||||
check(
|
check(
|
||||||
"with no offset the provisional anchor is the newest message",
|
"delete_hook hooks every MessagesController.deleteMessages overload",
|
||||||
plugin._newest_visible_message_id(anchor_activity) == 900,
|
len(plugin.delete_hook._unhooks) == 2,
|
||||||
f"got {plugin._newest_visible_message_id(anchor_activity)}",
|
f"installed hooks: {len(plugin.delete_hook._unhooks)} (expected 2)",
|
||||||
)
|
|
||||||
check(
|
|
||||||
"an offset moves the provisional anchor up the history",
|
|
||||||
plugin._newest_visible_message_id(anchor_activity, skip=2) == 700,
|
|
||||||
f"got {plugin._newest_visible_message_id(anchor_activity, skip=2)}",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Injected cards carry negative ids and must never be chosen as the
|
# Storage settings: an approximate size readout and a destructive,
|
||||||
# anchor, or a second summary would anchor to the first one's card.
|
# confirmed "clear all" action.
|
||||||
mixed_anchor = _AnchorActivity([-700007, 900, 800])
|
storage_rows = {getattr(r, "text", None): r for r in settings_rows}
|
||||||
|
clear_row = storage_rows.get("Clear all summaries")
|
||||||
|
size_row = storage_rows.get("Saved summaries")
|
||||||
check(
|
check(
|
||||||
"an existing injected card is never picked as the anchor",
|
"settings show the saved summary count and approximate size",
|
||||||
plugin._newest_visible_message_id(mixed_anchor) == 900,
|
size_row is not None and "saved" in (size_row.subtext or "") and "B" in size_row.subtext,
|
||||||
f"got {plugin._newest_visible_message_id(mixed_anchor)}",
|
f"subtext={getattr(size_row, 'subtext', None)!r}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"settings offer a red Clear all summaries action",
|
||||||
|
clear_row is not None and clear_row.red is True and callable(clear_row.on_click),
|
||||||
|
f"row={getattr(clear_row, '__dict__', None)}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@@ -1176,6 +1408,14 @@ def main(argv=None) -> int:
|
|||||||
[b.tl_name for b in answered.blocks] == ["pageBlockHeading1"],
|
[b.tl_name for b in answered.blocks] == ["pageBlockHeading1"],
|
||||||
f"blocks={[b.tl_name for b in answered.blocks]}",
|
f"blocks={[b.tl_name for b in answered.blocks]}",
|
||||||
)
|
)
|
||||||
|
plain = rich_markdown.markdown_to_plain(
|
||||||
|
"# Title\n\n**Bold** and *it* with `code` by [Alice](tg://user?id=1)\n> quote\n```\nx = 1\n```"
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"markdown_to_plain keeps the readable text and drops the markup",
|
||||||
|
plain == "Title\n\nBold and it with code by Alice\nquote\nx = 1",
|
||||||
|
f"plain={plain!r}",
|
||||||
|
)
|
||||||
empty_rm = rich_markdown.build_rich_message("", streaming=True)
|
empty_rm = rich_markdown.build_rich_message("", streaming=True)
|
||||||
check(
|
check(
|
||||||
"an empty RichMessage still carries one block so the cell renders",
|
"an empty RichMessage still carries one block so the cell renders",
|
||||||
@@ -1481,6 +1721,25 @@ def main(argv=None) -> int:
|
|||||||
f"topic0={len(db.get_for_scope(-100, 0))} topic7={len(db.get_for_scope(-100, 7))}",
|
f"topic0={len(db.get_for_scope(-100, 0))} topic7={len(db.get_for_scope(-100, 7))}",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
reserved, created = db.reserve(-100, 0, 555, 9)
|
||||||
|
check(
|
||||||
|
"reserving an anchor that already has a summary keeps its text",
|
||||||
|
reserved == first and created is False and db.get(first).text == "text two",
|
||||||
|
f"reserved={reserved} created={created} text={db.get(first).text!r}",
|
||||||
|
)
|
||||||
|
fresh, fresh_created = db.reserve(-100, 0, 777, 9)
|
||||||
|
check(
|
||||||
|
"reserving a new anchor creates an empty row",
|
||||||
|
fresh_created is True and db.get(fresh).text == "",
|
||||||
|
f"created={fresh_created}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"get_for_dialog spans every topic of the chat",
|
||||||
|
sorted((r.topic_id, r.anchor_msg_id) for r in db.get_for_dialog(-100))
|
||||||
|
== [(0, 555), (0, 777), (7, 555)],
|
||||||
|
f"rows={db.get_for_dialog(-100)}",
|
||||||
|
)
|
||||||
|
|
||||||
check("summary_db.delete removes a row", db.delete(first) and db.get(first) is None)
|
check("summary_db.delete removes a row", db.delete(first) and db.get(first) is None)
|
||||||
|
|
||||||
injector = bundled("services.rich_injector")
|
injector = bundled("services.rich_injector")
|
||||||
@@ -1547,6 +1806,10 @@ def main(argv=None) -> int:
|
|||||||
# Insertion order: ChatActivity.messages is newest-first, so the
|
# Insertion order: ChatActivity.messages is newest-first, so the
|
||||||
# summary must land at the anchor's index to sit after it on screen.
|
# summary must land at the anchor's index to sit after it on screen.
|
||||||
class _FakeAdapter:
|
class _FakeAdapter:
|
||||||
|
# ChatActivityAdapter rows before the first message (header,
|
||||||
|
# loading row): the jump must offset the list index by this.
|
||||||
|
messagesStartRow = 2
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.notified = 0
|
self.notified = 0
|
||||||
|
|
||||||
@@ -1581,6 +1844,17 @@ def main(argv=None) -> int:
|
|||||||
class _FakeListView:
|
class _FakeListView:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.cells = []
|
self.cells = []
|
||||||
|
self.stopped = 0
|
||||||
|
self.scroll_state = 0
|
||||||
|
|
||||||
|
def getScrollState(self):
|
||||||
|
return self.scroll_state
|
||||||
|
|
||||||
|
def getMeasuredHeight(self):
|
||||||
|
return 900
|
||||||
|
|
||||||
|
def stopScroll(self):
|
||||||
|
self.stopped += 1
|
||||||
|
|
||||||
def getChildCount(self):
|
def getChildCount(self):
|
||||||
return len(self.cells)
|
return len(self.cells)
|
||||||
@@ -1588,6 +1862,32 @@ def main(argv=None) -> int:
|
|||||||
def getChildAt(self, index):
|
def getChildAt(self, index):
|
||||||
return self.cells[index]
|
return self.cells[index]
|
||||||
|
|
||||||
|
class _FakeLayoutManager:
|
||||||
|
"""Records jumps and reports the visible row range they produce.
|
||||||
|
|
||||||
|
`hijack` simulates ChatActivity's own postponed scroll landing
|
||||||
|
right after a jump and carrying the view elsewhere.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.jumps = []
|
||||||
|
self.visible = (-1, -1)
|
||||||
|
self.hijack = 0
|
||||||
|
|
||||||
|
def scrollToPositionWithOffset(self, position, offset, bottom):
|
||||||
|
self.jumps.append((position, offset, bottom))
|
||||||
|
if self.hijack:
|
||||||
|
self.hijack -= 1
|
||||||
|
self.visible = (40, 46)
|
||||||
|
else:
|
||||||
|
self.visible = (max(0, position - 3), position + 3)
|
||||||
|
|
||||||
|
def findFirstVisibleItemPosition(self):
|
||||||
|
return self.visible[0]
|
||||||
|
|
||||||
|
def findLastVisibleItemPosition(self):
|
||||||
|
return self.visible[1]
|
||||||
|
|
||||||
class _FakeChatActivity:
|
class _FakeChatActivity:
|
||||||
def __init__(self, ids):
|
def __init__(self, ids):
|
||||||
self.messages = _JavaList()
|
self.messages = _JavaList()
|
||||||
@@ -1598,13 +1898,24 @@ def main(argv=None) -> int:
|
|||||||
self.chatAdapter = _FakeAdapter()
|
self.chatAdapter = _FakeAdapter()
|
||||||
self.chatMessageCellDelegate = _FakeDelegate()
|
self.chatMessageCellDelegate = _FakeDelegate()
|
||||||
self.chatListView = _FakeListView()
|
self.chatListView = _FakeListView()
|
||||||
|
self.chatLayoutManager = _FakeLayoutManager()
|
||||||
self.scrolled_to = []
|
self.scrolled_to = []
|
||||||
|
# Ids scrollToMessageId will "load" into the list, mimicking
|
||||||
|
# the history request it issues for an unloaded id.
|
||||||
|
self.loadable = {}
|
||||||
|
|
||||||
def getDialogId(self):
|
def getDialogId(self):
|
||||||
return -4242
|
return -4242
|
||||||
|
|
||||||
def scrollToMessageId(self, msg_id, from_id, select, load_index, force, unread):
|
def scrollToMessageId(self, msg_id, from_id, select, load_index, force, unread):
|
||||||
self.scrolled_to.append(msg_id)
|
self.scrolled_to.append(msg_id)
|
||||||
|
for mid in self.loadable.pop(msg_id, ()):
|
||||||
|
owner_obj = _TLObj("TL_message")
|
||||||
|
owner_obj.id = mid
|
||||||
|
self.messages.add(_FakeMessageObject(0, owner_obj))
|
||||||
|
|
||||||
|
def ids(self):
|
||||||
|
return [m.messageOwner.id for m in self.messages]
|
||||||
|
|
||||||
activity = _FakeChatActivity([600, 555, 500])
|
activity = _FakeChatActivity([600, 555, 500])
|
||||||
check(
|
check(
|
||||||
@@ -1684,7 +1995,7 @@ def main(argv=None) -> int:
|
|||||||
|
|
||||||
virtual_id = injector.virtual_id_for(9, 555)
|
virtual_id = injector.virtual_id_for(9, 555)
|
||||||
inj = injector.SummaryInjection(activity, 0, -4242, 0, 555, virtual_id)
|
inj = injector.SummaryInjection(activity, 0, -4242, 0, 555, virtual_id)
|
||||||
inserted = inj.insert(status="Fetching messages…", streaming=True)
|
inserted = inj.insert(thinking="first chunk", streaming=True)
|
||||||
check(
|
check(
|
||||||
"injection inserts one message at the anchor index and refreshes the adapter",
|
"injection inserts one message at the anchor index and refreshes the adapter",
|
||||||
inserted
|
inserted
|
||||||
@@ -1695,12 +2006,18 @@ def main(argv=None) -> int:
|
|||||||
)
|
)
|
||||||
|
|
||||||
# The analyzed range can end far up the history, so the card may be
|
# The analyzed range can end far up the history, so the card may be
|
||||||
# injected off-screen; the chat has to move to it. The anchor is the
|
# injected off-screen; the chat has to move to it. scrollToMessageId
|
||||||
# scroll target because it is a real message the app can resolve, and
|
# only smooth-scrolls from stale cell positions right after an insert,
|
||||||
# the card sits directly above it.
|
# so placement hard-jumps the layout manager to the card's own row.
|
||||||
check(
|
check(
|
||||||
"inserting the card scrolls the chat to the analyzed range",
|
"placing the card hard-jumps the layout manager to the card's row",
|
||||||
activity.scrolled_to == [555],
|
activity.chatLayoutManager.jumps == [(2 + 1, 300, False)]
|
||||||
|
and activity.chatListView.stopped == 1,
|
||||||
|
f"jumps={activity.chatLayoutManager.jumps} stopped={activity.chatListView.stopped}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"a loaded anchor needs no history load before placement",
|
||||||
|
activity.scrolled_to == [],
|
||||||
f"scrolled_to={activity.scrolled_to}",
|
f"scrolled_to={activity.scrolled_to}",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -1720,8 +2037,8 @@ def main(argv=None) -> int:
|
|||||||
)
|
)
|
||||||
check(
|
check(
|
||||||
"a streaming update does not scroll again and fight the user",
|
"a streaming update does not scroll again and fight the user",
|
||||||
activity.scrolled_to == [555],
|
activity.scrolled_to == [] and len(activity.chatLayoutManager.jumps) == 1,
|
||||||
f"scrolled_to={activity.scrolled_to}",
|
f"scrolled_to={activity.scrolled_to} jumps={activity.chatLayoutManager.jumps}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Streaming repaints should hit the one visible cell, not rebind every
|
# Streaming repaints should hit the one visible cell, not rebind every
|
||||||
@@ -1750,46 +2067,6 @@ def main(argv=None) -> int:
|
|||||||
f"notified={activity.chatAdapter.notified} (was {notified_before})",
|
f"notified={activity.chatAdapter.notified} (was {notified_before})",
|
||||||
)
|
)
|
||||||
|
|
||||||
# The card is injected before the fetch, so its anchor starts
|
|
||||||
# provisional and must be corrected once the real range is known.
|
|
||||||
reanchor_activity = _FakeChatActivity([600, 555, 500])
|
|
||||||
provisional_id = injector.virtual_id_for(0, 600)
|
|
||||||
reinj = injector.SummaryInjection(reanchor_activity, 0, -4242, 0, 600, provisional_id)
|
|
||||||
reinj.insert(status="Fetching messages…", streaming=True)
|
|
||||||
check(
|
|
||||||
"provisional card lands at the newest message before the fetch",
|
|
||||||
reanchor_activity.messages.get(0).messageOwner.id == provisional_id,
|
|
||||||
f"ids={[m.messageOwner.id for m in reanchor_activity.messages]}",
|
|
||||||
)
|
|
||||||
|
|
||||||
final_id = injector.virtual_id_for(11, 555)
|
|
||||||
reinj.reanchor(555, final_id)
|
|
||||||
ids = [m.messageOwner.id for m in reanchor_activity.messages]
|
|
||||||
check(
|
|
||||||
"reanchor moves the card to the real anchor and adopts its virtual id",
|
|
||||||
reanchor_activity.messages.size() == 4
|
|
||||||
and ids.index(final_id) == 1
|
|
||||||
and reanchor_activity.messages.get(1).messageOwner.reply_to.reply_to_msg_id == 555,
|
|
||||||
f"ids={ids}",
|
|
||||||
)
|
|
||||||
check(
|
|
||||||
"reanchor follows the card to the real end of the analyzed range",
|
|
||||||
reanchor_activity.scrolled_to == [600, 555],
|
|
||||||
f"scrolled_to={reanchor_activity.scrolled_to}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Regression: a streaming update after a reanchor used to add a second
|
|
||||||
# card, because the lookup missed the old id and fell through to add().
|
|
||||||
reinj.update(status="Summarizing 98 messages…", streaming=True)
|
|
||||||
ids = [m.messageOwner.id for m in reanchor_activity.messages]
|
|
||||||
check(
|
|
||||||
"updating after a reanchor leaves exactly one summary card",
|
|
||||||
reanchor_activity.messages.size() == 4
|
|
||||||
and ids.count(final_id) == 1
|
|
||||||
and provisional_id not in ids,
|
|
||||||
f"ids={ids}",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Re-summarizing a range whose saved card the restore hook already
|
# Re-summarizing a range whose saved card the restore hook already
|
||||||
# re-injected must claim that card, not sit beside it.
|
# re-injected must claim that card, not sit beside it.
|
||||||
collide_activity = _FakeChatActivity([600, 555, 500])
|
collide_activity = _FakeChatActivity([600, 555, 500])
|
||||||
@@ -1801,18 +2078,164 @@ def main(argv=None) -> int:
|
|||||||
0,
|
0,
|
||||||
[summary_db_mod.StoredSummary(12, -4242, 0, 555, "previous run", "", 5, 0)],
|
[summary_db_mod.StoredSummary(12, -4242, 0, 555, "previous run", "", 5, 0)],
|
||||||
)
|
)
|
||||||
collide_provisional = injector.virtual_id_for(0, 600)
|
collide = injector.SummaryInjection(collide_activity, 0, -4242, 0, 555, saved_id)
|
||||||
collide = injector.SummaryInjection(collide_activity, 0, -4242, 0, 600, collide_provisional)
|
collide.insert(thinking="new run", streaming=True)
|
||||||
collide.insert(status="Fetching messages…", streaming=True)
|
collide.update(answer_markdown="new answer", streaming=True)
|
||||||
collide.reanchor(555, saved_id)
|
ids = collide_activity.ids()
|
||||||
collide.update(status="Summarizing…", streaming=True)
|
|
||||||
ids = [m.messageOwner.id for m in collide_activity.messages]
|
|
||||||
check(
|
check(
|
||||||
"re-summarizing a restored range reuses its card instead of duplicating",
|
"re-summarizing a restored range reuses its card instead of duplicating",
|
||||||
ids.count(saved_id) == 1 and collide_provisional not in ids,
|
ids.count(saved_id) == 1 and len(ids) == 4,
|
||||||
f"ids={ids}",
|
f"ids={ids}",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# The anchor is the newest ANALYZED message; with an offset it can be
|
||||||
|
# outside the loaded window. The card must never fall back to the chat
|
||||||
|
# bottom: the chat is asked to load the anchor, and the card is placed
|
||||||
|
# directly after it once it arrives.
|
||||||
|
far = _FakeChatActivity([900, 800])
|
||||||
|
far.loadable[300] = [310, 300, 290]
|
||||||
|
far_card = injector.virtual_id_for(5, 300)
|
||||||
|
far_inj = injector.SummaryInjection(far, 0, -4242, 0, 300, far_card)
|
||||||
|
far_inj.insert(thinking="chunk", streaming=True)
|
||||||
|
far_ids = far.ids()
|
||||||
|
check(
|
||||||
|
"an unloaded anchor is loaded first, then the card lands right after it",
|
||||||
|
far.scrolled_to == [300]
|
||||||
|
and far_inj.placed
|
||||||
|
and far_ids.index(far_card) == far_ids.index(300) - 1,
|
||||||
|
f"scrolled_to={far.scrolled_to} ids={far_ids}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"the card is never parked at the bottom while its anchor loads",
|
||||||
|
far_ids[0] == 900,
|
||||||
|
f"ids={far_ids}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Loading a far anchor makes ChatActivity run its own postponed scroll,
|
||||||
|
# which can land after the jump and carry the view away from the card.
|
||||||
|
# The settle checks must bring it back.
|
||||||
|
hijacked = _FakeChatActivity([900, 800])
|
||||||
|
hijacked.loadable[300] = [310, 300, 290]
|
||||||
|
hijacked.chatLayoutManager.hijack = 1
|
||||||
|
hijacked_card = injector.virtual_id_for(7, 300)
|
||||||
|
injector.SummaryInjection(hijacked, 0, -4242, 0, 300, hijacked_card).insert(
|
||||||
|
thinking="chunk", streaming=True
|
||||||
|
)
|
||||||
|
card_row = 2 + hijacked.ids().index(hijacked_card)
|
||||||
|
first, last = hijacked.chatLayoutManager.visible
|
||||||
|
check(
|
||||||
|
"a jump overridden by the app's own scroll is redone until the card is on screen",
|
||||||
|
len(hijacked.chatLayoutManager.jumps) == 2 and first <= card_row <= last,
|
||||||
|
f"jumps={hijacked.chatLayoutManager.jumps} visible={(first, last)} row={card_row}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Once the user takes hold of the list, the plugin stops steering it.
|
||||||
|
dragged = _FakeChatActivity([600, 555, 500])
|
||||||
|
dragged.chatLayoutManager.hijack = 1
|
||||||
|
dragged.chatListView.scroll_state = 1
|
||||||
|
injector.SummaryInjection(dragged, 0, -4242, 0, 555, injector.virtual_id_for(8, 555)).insert(
|
||||||
|
thinking="chunk", streaming=True
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"no re-jump while the user is dragging the list",
|
||||||
|
len(dragged.chatLayoutManager.jumps) == 1,
|
||||||
|
f"jumps={dragged.chatLayoutManager.jumps}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# A cache-then-server reload can replace the list under a fresh card.
|
||||||
|
reloaded = _FakeChatActivity([600, 555, 500])
|
||||||
|
reloaded_card = injector.virtual_id_for(9, 555)
|
||||||
|
reloaded_inj = injector.SummaryInjection(reloaded, 0, -4242, 0, 555, reloaded_card)
|
||||||
|
real_on_ui_for_reload = injector._on_ui
|
||||||
|
dropped = []
|
||||||
|
|
||||||
|
def _reloading_on_ui(fn, delay_ms=0):
|
||||||
|
if delay_ms and not dropped:
|
||||||
|
# First settle check: the chat reloads its window first.
|
||||||
|
reloaded.messages[:] = [m for m in reloaded.messages if m.messageOwner.id > 0]
|
||||||
|
dropped.append(True)
|
||||||
|
fn()
|
||||||
|
|
||||||
|
injector._on_ui = _reloading_on_ui
|
||||||
|
try:
|
||||||
|
reloaded_inj.insert(thinking="chunk", streaming=True)
|
||||||
|
finally:
|
||||||
|
injector._on_ui = real_on_ui_for_reload
|
||||||
|
reloaded_ids = reloaded.ids()
|
||||||
|
check(
|
||||||
|
"a card dropped by a list reload right after placement is put back",
|
||||||
|
dropped and reloaded_ids.count(reloaded_card) == 1
|
||||||
|
and reloaded_ids.index(reloaded_card) == reloaded_ids.index(555) - 1,
|
||||||
|
f"ids={reloaded_ids}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# An anchor that never loads: no card anywhere, rather than a card
|
||||||
|
# attached to messages it did not analyze.
|
||||||
|
never = _FakeChatActivity([900, 800])
|
||||||
|
never_card = injector.virtual_id_for(6, 300)
|
||||||
|
never_inj = injector.SummaryInjection(never, 0, -4242, 0, 300, never_card)
|
||||||
|
never_inj.insert(thinking="chunk", streaming=True)
|
||||||
|
never_inj.update(answer_markdown="more", streaming=True)
|
||||||
|
check(
|
||||||
|
"an anchor that never loads places no card at a wrong position",
|
||||||
|
never_card not in never.ids() and not never_inj.placed,
|
||||||
|
f"ids={never.ids()}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"a far anchor that does not arrive is requested again, not just once",
|
||||||
|
len(never.scrolled_to) >= 2 and set(never.scrolled_to) == {300},
|
||||||
|
f"scrolled_to={never.scrolled_to}",
|
||||||
|
)
|
||||||
|
# ...and the card shows up once the user scrolls the anchor in.
|
||||||
|
for mid in (310, 300):
|
||||||
|
owner_obj = _TLObj("TL_message")
|
||||||
|
owner_obj.id = mid
|
||||||
|
never.messages.add(_FakeMessageObject(0, owner_obj))
|
||||||
|
never_inj.update(answer_markdown="final", streaming=False, tagged=True)
|
||||||
|
never_ids = never.ids()
|
||||||
|
check(
|
||||||
|
"a card whose anchor loads late is placed on the next render",
|
||||||
|
never_inj.placed and never_ids.index(never_card) == never_ids.index(300) - 1,
|
||||||
|
f"ids={never_ids}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Finished summaries carry the #aisummaries tag, in the card and in
|
||||||
|
# the plain-text body (with a hashtag entity), so copied text and any
|
||||||
|
# text matching see it. Streaming renders stay untagged.
|
||||||
|
tagged_mo = injector.build_summary_message(
|
||||||
|
0, -4242, 0, 555, injector.virtual_id_for(1, 555),
|
||||||
|
"**Done** see [Alice](tg://user?id=1)", "", False, tagged=True,
|
||||||
|
)
|
||||||
|
tagged_owner = tagged_mo.messageOwner
|
||||||
|
last_block = tagged_owner.rich_message.blocks[-1]
|
||||||
|
entity = tagged_owner.entities[0] if len(tagged_owner.entities) else None
|
||||||
|
tag_node = getattr(last_block, "text", None)
|
||||||
|
check(
|
||||||
|
"a finished summary ends with a native #aisummaries hashtag node",
|
||||||
|
last_block.tl_name == "pageBlockParagraph"
|
||||||
|
and getattr(tag_node, "tl_name", None) == "textHashtag"
|
||||||
|
and getattr(tag_node.text, "text", None) == "#aisummaries",
|
||||||
|
f"last={last_block.tl_name} node={getattr(tag_node, '__dict__', None)}",
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"the plain-text body is searchable text plus a hashtag entity",
|
||||||
|
tagged_owner.message == "Done see Alice\n\n#aisummaries"
|
||||||
|
and entity is not None
|
||||||
|
and entity.tl_name == "TL_messageEntityHashtag"
|
||||||
|
and entity.offset == len("Done see Alice\n\n")
|
||||||
|
and entity.length == len("#aisummaries"),
|
||||||
|
f"message={tagged_owner.message!r} entity={getattr(entity, '__dict__', None)}",
|
||||||
|
)
|
||||||
|
streaming_mo = injector.build_summary_message(
|
||||||
|
0, -4242, 0, 555, injector.virtual_id_for(1, 555), "partial", "", True
|
||||||
|
)
|
||||||
|
check(
|
||||||
|
"a streaming render is not tagged",
|
||||||
|
"#aisummaries" not in streaming_mo.messageOwner.message
|
||||||
|
and len(streaming_mo.messageOwner.entities) == 0,
|
||||||
|
f"message={streaming_mo.messageOwner.message!r}",
|
||||||
|
)
|
||||||
|
|
||||||
# Re-inserting the same virtual id must replace, never duplicate.
|
# Re-inserting the same virtual id must replace, never duplicate.
|
||||||
inj2 = injector.SummaryInjection(activity, 0, -4242, 0, 555, virtual_id)
|
inj2 = injector.SummaryInjection(activity, 0, -4242, 0, 555, virtual_id)
|
||||||
inj2.insert(answer_markdown="again", thinking="", streaming=False)
|
inj2.insert(answer_markdown="again", thinking="", streaming=False)
|
||||||
@@ -2015,23 +2438,7 @@ def main(argv=None) -> int:
|
|||||||
f"dispatched={sep_n} ids={sep_ids}",
|
f"dispatched={sep_n} ids={sep_ids}",
|
||||||
)
|
)
|
||||||
|
|
||||||
# A live status card is the only feedback that pressing Summarize did
|
run_pipeline_checks(plugin if plugin_cls is not None else None, injector, summary_db_mod, _FakeChatActivity, _FakeMessage if plugin_cls is not None else None)
|
||||||
# anything, so it must appear even when the provisional anchor cannot be
|
|
||||||
# placed: a large offset reaches past the loaded window, and an
|
|
||||||
# unresolved anchor arrives as 0. Requiring a loaded anchor here left the
|
|
||||||
# user staring at an unchanged chat.
|
|
||||||
for label, anchor in (("unresolved anchor 0", 0), ("anchor past the window", 4242)):
|
|
||||||
live = _FakeChatActivity([700, 690, 680])
|
|
||||||
live_injection = injector.SummaryInjection(
|
|
||||||
live, 0, -4242, 0, anchor, injector.virtual_id_for(0, anchor or 1)
|
|
||||||
)
|
|
||||||
shown = live_injection.insert(status="Fetching messages…", streaming=True)
|
|
||||||
live_ids = [m.messageOwner.id for m in live.messages]
|
|
||||||
check(
|
|
||||||
f"the status card still appears with an {label}",
|
|
||||||
shown and len([i for i in live_ids if i < 0]) == 1,
|
|
||||||
f"inserted={shown} ids={live_ids}",
|
|
||||||
)
|
|
||||||
|
|
||||||
check(
|
check(
|
||||||
"sys.modules gained no entry for bundled plugin module names",
|
"sys.modules gained no entry for bundled plugin module names",
|
||||||
|
|||||||
Reference in new issue
Block a user