Skip to content

feat: add token-usage-widget Mini App - #177

Open
nahyuw wants to merge 4 commits into
MiniMax-AI:mainfrom
nahyuw:add-token-usage-widget
Open

nahyuw wants to merge 4 commits into
MiniMax-AI:mainfrom
nahyuw:add-token-usage-widget

Conversation

@nahyuw

@nahyuw nahyuw commented Nov 28, 2026 •

Copy link
Copy Markdown

A floating dial that shows how much of your MiniMax account's Token quota is used, how fast it is being spent, and how much was used over the last 7 days — with an always-on-top popup window and a bilingual settings page that follows the system language. Every reading is account-side, and the trend curve always follows the account's live Token counter over a window you pick: 10 minutes, 30 minutes, 60 minutes or 2 hours.

The Account data switch in the settings page

The Account data switch in the settings page, after you turn it on.

Tested environment

  • MiniMax Code 3.1.19 on macOS 26.6.2. Minimum client version: MiniMax Code 3.1.2 — the manifest declares surface.window, so older clients reject the package.
  • The branch is rebased onto the current main (655fdf6). main was not merged into the branch: the two README tables were resolved by hand and the table rows that main added in the meantime were kept.
  • Installed locally, published through the Host, then checked in the in-app Browser: the dial renders, the water level and the outer ring each follow their own metric, hovering opens the detail panel with all three enabled metrics visible and no scrolling, the 7-day sparkline draws, and the console stays free of errors and warnings.
  • The Account data switch was exercised in both positions. Off: the dial turns grey, the readout shows --, one line says the account data is off, the service reports account_disabled, the sampling file in dataDir keeps its old timestamp across two polls, and no Host file is read at all. On: the account feed returns, the readings come back and the trend curve draws again.
  • All four trend windows were exercised (10 / 30 / 60 / 120 minutes). The service returns one bucket per minute of the chosen window, the curve is redrawn with that many points, the accessible label names the window, and switching the setting takes effect immediately instead of waiting for the next poll.
  • An idle stretch was exercised as well: the minutes without a sample stay at zero, the delta that arrives when the counter catches up is attributed to the minutes just before it was seen, and a window with no consumption draws no curve at all. The accessible label reads 「无消耗」 / "No consumption", the same wording the README uses.
  • Both languages were exercised. With the language set to follow the system the page renders in the system language; switching the setting to 简体中文 or English re-renders the dial, the detail panel, the settings page, the metric names and the accessible labels in place.
  • npm run check reports plugins/nahyuw/token-usage-widget: OK — 0 errors, 0 warnings, nothing left to explain. The repository test suite passes 111 of 113 with 2 skipped and 0 failures. The package's own node --test tests/ passes 12 of 12.

Not verified

  • Windows was not tested; only macOS was.
  • The separate always-on-top desktop window was verified through the in-app Browser rather than as a floating window on the macOS desktop.
  • The account API was exercised against a signed-in account only; the signed-out path was not tested end to end. The expired-credential path was exercised through the package's own tests against a temporary home directory, not by letting a real credential expire.
  • The switch was checked in its default-off position; the local installation's settings file was then switched on by hand, which is the state a user reaches after opting in.
  • A first-minute install was not exercised. The live account counter refreshes every 30–60 seconds, so the rate and the trend start from a calibrated estimate and only become exact once the counter has covered the whole window.

Access declared

No permission grants. The package declares no agentAccess, toolAccess, deviceAccess, hostAccess or hostConnectorAccess, runs no Agent turn, reads no conversation, calls no tool, spawns no child process, and uses no microphone, camera or notification. The only feature beyond the plain manifest is surface.window.

It does read two files that belong to MiniMax Code itself — the signed-in access token and the login region — because the Host offers no API for them. That now happens only after the user turns on the Account data switch: with the switch off (the default) the service resolves no runtime root, reads no Host file, calls no account API and writes no sampling file. The README names both files, says how they are located, and states plainly that this relies on an unspecified internal layout.

Written by MiniMax M3.1 Flash

What it does

The dial carries three readings at once, and each one is the user's choice:

  • Centre readout — one metric as a number, for example today's Token total.
  • Water level — how full the current 5-hour or 7-day cycle is, drawn as water inside a glass dial.
  • Outer ring — a third metric as a progress ring: a cycle's usage, a reset countdown, or the share of the current 5-hour cycle that the present speed would burn.

Hovering opens a detail panel with every enabled metric, the last 7 days as a sparkline, and one line each naming the metric behind the centre, the water and the ring, so a rate-driven ring never leaves you guessing which reading it stands for.

Ten metrics are available, all of them account-side: usage in the current 5-hour cycle, usage in the current 7-day cycle, the two reset countdowns, today's Token total, the 7-day Token total, the account's 10-minute and 30-minute Token rate, today's cache hit rate and today's input / output split. The settings page covers the account-data switch, the enabled metrics and their order, the refresh rate, the window size and position, the trend window, the centre / water / ring assignment, the theme, and the language.

Until the account-data switch is turned on the dial is grey, the readout shows --, and one line says where to turn it on — the readings are not faked and the metric rows are not drawn.

Review feedback addressed

The two rounds of review on this pull request are answered here:

  1. The account feed was read by default. It now sits behind the Account data switch in the settings page, stored as readAccountData in settings.json and off by default. With the switch off the service returns account_disabled without resolving the runtime root, reading a file, calling the API or writing the sampling file; the dial turns grey and explains itself.
  2. The branch conflicted with main. The branch is rebased onto the current main (655fdf6) — main is not merged into it — and the two README tables are merged by hand with the rows main added in the meantime.
  3. The runtime database was read in full. The file is now only stat-ed: its type is checked and its contents are never read. A test names a FIFO instead of a database file, so opening it would block the test — it does not.
  4. The runtime root could resolve to the wrong login. The candidates are tried in the same order as before, but the one that actually holds the credentials now wins. That check only looks at directory entries and file names; the runtime database is consulted afterwards as a fallback hint, and only for its type.
  5. An expired token was still sent. Only a record that carries an explicit expiry which has not passed (with a 60-second margin) is used. A missing, zero or already-past expiry is never sent; the widget reports the session as expired instead of trying it.
  6. The server's status_msg was echoed to the page. The wording is ours now — a small table of our own messages per status code — and only the status code is kept.
  7. Outbound requests did not refuse redirects. Every account request now sends redirect: 'error', so the credential cannot be handed to another host.
  8. There were no tests. The package now ships tests/ with node --test tests/ covering the switch (off means no Host file is read, on means it is), the expiry rules, the runtime-root resolution, the error wording, the error mapping and the trend buckets: 12 tests, all passing.
  9. The English interface said "Idle" while the README said "no consumption". The English string is now No consumption, and the Chinese one is 「无消耗」, so the three texts agree.

Notes on the implementation

  • The account API gives a percentage, not a Token count. A cycle arrives as a used percentage and a reset time. The package keeps a small sampling history in its own data directory and turns that percentage into a Token rate by calibrating it against the account's live Token counter, so the rate is measured rather than guessed. While the counter has not yet covered the whole window, the service marks the trend as an estimate and the metric hint says so.
  • The trend window is a server-side parameter, not a client-side slice. The service returns one bucket per minute of the chosen window, and the page takes the window from that same response, so the curve and the sentence naming its window can never disagree. The smoothing radius scales with the number of points, so 120 buckets do not collapse into a sawtooth.
  • An idle stretch stays empty. The account's live Token counter only reports a new sample when its value changes, so a long gap between samples means the counter was not reporting during that time. The delta that arrives when the counter catches up is attributed to the minutes just before it was seen instead of being spread across the whole gap — otherwise a quiet stretch becomes a uniformly loaded curve, and a chart that scales to its own maximum pins that curve to the top and paints the area under it, which reads as "full". The vertical scale keeps its minimum at zero and falls back to a fixed value, and a window with no consumption at all is not drawn at all: the dial shows the readout and the water only, and the accessible label says "no consumption" instead of "steady".
  • Colour stays in one family. The whole usage ramp is a single cool-blue hue moving from light to deep. Interpolating blue through teal and yellow to red passes through green, which made a half-full dial look muddy; crossing blue to red by interpolation flashes magenta, so the 90% alarm threshold is a hard cut instead.
  • The dry half of the dial is a mask, not an opacity. Above the waterline the glass is drawn at a fraction of its opacity through an SVG mask that reuses the visible wave path, so the dry edge and the water surface stay aligned on every frame of the wave animation.
  • Bilingual from the first commit. miniapp/client/i18n.js holds the whole string table for English and 简体中文. The locale follows the system by default and can be pinned in the settings page. The Node service sends every metric name and hint as a { zh, en } pair so one catalog serves both languages, and the service's error messages carry an English variant next to the Chinese one.
  • The script bundler is recursive and deduplicated. Client modules are ES modules, but the Host also serves them as plain scripts for a cached-HTML fallback. Inlining one level deep would leave a nested import inside an IIFE, which is invalid in a plain script, and inlining the same module twice would give a stateful module two copies — two locales at once. The bundler now collects the transitive dependencies, builds each one once in dependency order, and points every reference at the same instance.
  • The host-facing rules live in two testable modules. miniapp/node/host-access.mjs holds the file-type check, the runtime-root candidates, the credential collection and the record selection; miniapp/node/trend-buckets.mjs holds the bucket attribution. server.mjs stays the HTTP layer. Both modules are pure enough to test with node --test without starting a server, which is what the new tests do.

Package identity

The plugin ID in this pull request is token-usage-widget. The author's local installation keeps the ID that the Host generated for it, so the two differ; the directory name and plugin.json.name here match each other, which is what the contract requires.

Data & access

The README documents every file read, every file written, every network host, the spawned processes and the state.

Two files belonging to MiniMax Code itself are read, and only while the Account data switch is on: the login token in auth.json (a record is used only when it carries an explicit expiry that has not passed) and the region in auth-state.json. Locating them relies on an unspecified internal layout, because the Host offers no API for it; the README says so plainly.

The access token stays inside the Node process: it never reaches the page, a log line, or a response body. The package spawns no child process and reads no other Host file. The runtime database is stat-ed, never read, and the account requests refuse redirects, so the credential cannot be handed to another host.

The package's own tests live in tests/ (outside miniapp/, as the repository asks) and run with node --test tests/.

License

MIT.

@MyPrototypeWhat MyPrototypeWhat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the Token Usage Widget PR. We re-checked on a95ba34d. This isn't ready to merge yet.

The merge blocker is that the plugin reads the MiniMax Code signed-in access token from the host's undocumented auth files by default, as soon as it is enabled. Opening the widget or the settings page calls GET /api/state, and that path always runs fetchAccount() / loadAuthProfile(). There is no separate setting that starts off and has to be turned on before this read happens. A hostAccess declaration is not present here, and in this contract it would only cover navigation.read anyway. It does not authorize reading login files.

Please add a default-off switch, keep the token read behind it, and document the switch in the README.

A few related notes, not merge blockers on their own:

  • The README and the PR body say this package reads one host file (auth.json) and no other host file. In the source, choosing a runtime root also readFiles the whole v2/sqlite/runtime-state.sqlite, and readAuthRegion reads region from auth-state.json. Those contents are not placed into the /api/state payload in the source we checked, but the sqlite file is read again whenever the 15-second snapshot cache expires.
  • If MINIMAX_DATA_DIR is set and contains auth.json, but there is no sqlite file and auth/prod is a directory (pathExists only treats a file as a hit), root resolution falls through to ~/.minimax, which may be a different login.
  • A missing or 0 expiresAtMs still sends the token. For account API errors other than status_code 0 or 1004, the raw status_msg is returned on account.message (this is not the token from auth.json). Outbound fetch does not set redirect: 'error'.
  • There are no test files among the 18 changed files.

We confirmed from the source that parseAccountPayload does not include accessToken. A reviewer simulation also did not see the token in /api/state, /api/settings, page or asset responses, logs, or the package's own dataDir. We did not re-run that probe. Host, Electron, and the in-app Browser were not tested.

Happy to re-review once the default-off switch is in.

@MyPrototypeWhat MyPrototypeWhat left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the two trend commits. We re-checked this head 8e4a08d8 on its own, without carrying over the conclusion from a95ba34d. It is still not ready to merge.

The original merge blocker is still in place. These two commits did not change the auth path. Opening the widget or the settings page still calls GET /api/state (widget.js refresh() / settings.js load()). When the snapshot cache has expired, that path unconditionally runs fetchAccount() / loadAuthProfile() and reads the signed-in access token from the host's undocumented auth/**/auth.json files. DEFAULT_SETTINGS and normalizeSettings still only cover metrics, refresh, theme, language, scale, the trend window, position, and the three dial assignments. There is still no default-off switch that has to be turned on before this read happens. The README Data & access section documents the token read as something that happens once the app is open, and it does not describe a switch. plugin.json and miniapp.json still do not declare hostAccess. Even if they did, this contract only covers navigation.read, which would not authorize reading login files.

Please add a default-off switch, keep the token read behind it, and document the switch in the README.

Separately, this branch cannot merge into current main as it stands. GitHub reports mergeable=false / dirty because the community catalog tables in the root README.md and README.zh-CN.md conflict. This PR inserts the Token Usage Widget row immediately under the table header, while main has since inserted Postman Lite, Weekly Planner, and 天气 · 新闻看板 at that same spot. That is a mechanical catalog conflict, not a logic issue introduced by the two trend commits, but it does block the merge. Please rebase onto current main and fold the new catalog rows together. Being behind main alone would not have been a reason to rebase. The conflict is.

A few related notes, still not merge blockers on their own:

  • The README and the PR body say this package reads one host file (auth.json) and no other host file. In the source, choosing a runtime root also readFiles the whole v2/sqlite/runtime-state.sqlite, and readAuthRegion reads region from auth-state.json. Those contents are not placed into the /api/state payload in the source we checked, but the sqlite file is read again whenever the 15-second snapshot cache expires.
  • If MINIMAX_DATA_DIR is set and contains auth.json, but there is no sqlite file and auth/prod is a directory (pathExists only treats a file as a hit), root resolution falls through to ~/.minimax, which may be a different login.
  • A missing or 0 expiresAtMs still sends the token. For account API errors other than status_code 0 or 1004, the raw status_msg is returned on account.message (this is not the token from auth.json). Outbound fetch does not set redirect: 'error'.
  • There are still no package tests. The English accessible label for an empty window is Idle (i18n.js), while the English README says "no consumption". The Chinese copy is 「无消耗」 on both sides. That is a wording mismatch from these commits, not a new merge blocker.

We confirmed from the source that parseAccountPayload does not include accessToken. We did not re-run a token-leak probe, and we did not test on Windows, a real host / Electron / in-app Browser, real login files, or the live account API. This head has no repository CI run. Only Proof was skipped.

Please update the branch, and we will look at the new head.

Copy link
Copy Markdown
Collaborator

This branch now also has a real merge conflict with current main. Please rebase onto current main, and don't merge main into the working branch. The earlier review still needs to be addressed. A rebase alone isn't enough.

@nahyuw
nahyuw force-pushed the add-token-usage-widget branch from 8e4a08d to 8531325 Compare December 4, 2026 06:04
@nahyuw

nahyuw commented Dec 4, 2026

Copy link
Copy Markdown
Author

Thanks for the review — the branch is updated.

Rebased, not merged. The branch is now rebased onto the current main (8cf3ac9); main is not merged into it. The two catalog tables in the root READMEs are folded together by hand, so the rows main added in the meantime are kept.

The merge blocker: the account feed is behind a default-off switch. There is a new Account data switch in the settings page (readAccountData in settings.json, off by default). While it is off:

  • the service returns account_disabled without resolving the runtime root, so no Host file is read at all;
  • it does not call the account API and does not write the sampling file;
  • the dial turns grey, the readout shows --, and one line says where to turn the switch on.

Turning it on starts the account feed on the next poll. The switch is documented in the README, in English and in 中文.

The notes:

  1. v2/sqlite/runtime-state.sqlite is now only stat-ed — the file's type is checked, its contents are never opened. A test points the same code at a FIFO, which would block if it were opened.
  2. Root resolution now prefers the candidate that actually holds the credentials, so MINIMAX_DATA_DIR pointing at another install no longer falls through to ~/.minimax.
  3. Only a record with an explicit expiry that has not passed (60 s margin) is sent; a missing, zero or past expiry is never sent.
  4. The account API's status_msg is no longer echoed — the wording is ours, only the status code is kept.
  5. Outbound requests send redirect: 'error', so the credential cannot be handed to another host.
  6. The package now ships tests/ (outside miniapp/): node --test tests/ → 12 tests, all passing.
  7. The English empty-window label is now "No consumption", matching the README.

npm run check reports plugins/nahyuw/token-usage-widget: OK (0 errors, 0 warnings); the repository suite passes 111 of 113 with 2 skipped and 0 failures.

The PR body now covers the tested environment, the switch in both positions, and what was not verified.

Copy link
Copy Markdown
Collaborator

Thanks for rebasing Token Usage Widget earlier. After several merges landed on main this afternoon, including #208 Work Group, this branch now has a real merge conflict with current main, so it can't be merged as-is.

Please rebase this branch onto current main, and don't merge main into the working branch. The conflict is in the community catalog tables:

  • README.md
  • README.zh-CN.md

Your Token Usage Widget row and the newly merged Work Group row are both inserted just above Prompt Enhancer. Keep both rows after the rebase.

Please don't use GitHub's Update branch button. That would merge main into the working branch.

nahyuw and others added 4 commits December 4, 2026 23:40
The review asked for an explicit opt-in before the signed-in token is
read from the host's auth files. The Account data switch in the settings
page is now off by default. While it is off the service resolves no
runtime root, reads no Host file, calls no account API and writes no
sampling file; the dial turns grey and says where to turn the switch on.

The review notes are addressed too: the runtime database is only stat-ed
and never opened, the runtime root prefers the candidate that actually
holds the credentials, only a record with an explicit unexpired expiry
is sent, the account API's status_msg is no longer echoed, outbound
requests refuse redirects, the English empty-window label now matches the
README, and the package ships its own tests.
@nahyuw
nahyuw force-pushed the add-token-usage-widget branch from 8531325 to 3aa7ca7 Compare December 4, 2026 15:43
@nahyuw

nahyuw commented Dec 4, 2026

Copy link
Copy Markdown
Author

Rebased again onto the current main (655fdf6) — main is not merged into the branch.

The catalog conflict is resolved by hand in README.md and README.zh-CN.md: the Token Usage Widget row and the Work Group row are both kept, just above Prompt Enhancer. Nothing from main was dropped — the diff against main is one added row per README.

The code itself is unchanged since the last push; only the branch base moved. npm run check reports plugins/nahyuw/token-usage-widget: OK, and the repository suite passes 113 of 115 with 2 skipped and 0 failures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants