magpie-insights 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +49 -104
  2. package/README.zh-CN.md +50 -105
  3. package/package.json +5 -4
package/README.md CHANGED
@@ -2,62 +2,53 @@
2
2
 
3
3
  English | [简体中文](README.zh-CN.md)
4
4
 
5
- Insights (`magpie-insights`, displayed as "洞察" in the Chinese interface) is a [page plugin](https://usemagpie.ai/docs/plugins) for magpie, a local gateway that pools model subscriptions and accounts and serves them to coding agents such as Claude Code and Codex. It adds an Insights page under Plugins in magpie's sidebar in both the desktop window and `magpie web`, and adds an "Open Insights" item to the right-click menu of provider rows and agent rows.
5
+ <p align="center"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/hero-en.webp" alt="Insights for magpie: the Overview, an AI change waiting for confirmation, and the phone layout" width="100%"></p>
6
+
7
+ Insights is a page plugin for [magpie](https://usemagpie.ai), the local gateway that pools model subscriptions and accounts for coding agents such as Claude Code and Codex. It shows where your requests, tokens and money go, which accounts are close to their limits, and what is failing and why. Its AI assistant answers questions about all of it. The page sits under Plugins in magpie's sidebar, in the desktop window and in `magpie web`.
6
8
 
7
9
  ## Install
8
10
 
9
11
  Requires magpie 0.1.2836 or later.
10
12
 
11
- Install from npm (recommended); the package contains the built files:
12
-
13
13
  ```sh
14
14
  magpie plugin add magpie-insights
15
15
  ```
16
16
 
17
- Installing directly from the GitHub repository (`magpie plugin add github:vika2603/magpie-insights`) does not work because magpie loads a repository as committed, the built `dist/` directory is not committed, and magpie does not run install scripts.
18
-
19
- To install from a local checkout (Node.js 26 or later and pnpm are needed only for building and testing):
20
-
21
- ```sh
22
- git clone https://github.com/vika2603/magpie-insights
23
- cd magpie-insights
24
- pnpm install
25
- pnpm run build
26
- cd ..
27
- magpie plugin add ./magpie-insights
28
- ```
29
-
30
- After installing, turn the plugin on in magpie's Plugins page if it is not already on. The Insights page then appears under Plugins in the sidebar.
17
+ If the plugin is off, turn it on in magpie's Plugins page.
31
18
 
32
- ## What it shows
19
+ ## What you get
33
20
 
34
- - Usage: requests, failures, interruptions, retries, cost, and tokens for the chosen time range, with trends and breakdowns, and the current month's spend projected to its end.
35
- - Analysis: a pivot table where you pick the row dimension, the column dimension, and the metric, and can swap rows and columns with one click. Dimensions include gateway key, model, provider, account, agent, project folder, and time (day, hour, hour of day, weekday).
36
- - Requests & sessions: the most expensive sessions with the project folder they ran in, each opening in place to its conversation (what was asked, the answers, thinking, and the tools it ran, page by page) or, where no text can be read, its requests one by one; search and expandable details for individual requests, live traffic (requests in progress, the routing decision for each, and skipped accounts), and the prompt token breakdown (such as system, tools, skills, memory, files, and results).
37
- - Accounts & limits: each account's state, active cooldown countdowns, every quota window, errors in the last hour, and a consumption forecast.
38
- - Failures & retries: first what needs you now, whatever the range and filters: an account suspended, signed out, out of credit or failing on every try, or a gateway key that keeps blocking a model, each with what to do. Then the range's failures by cause, each saying whether it needs action, and the full log, including failed tries that another account finished.
39
- - AI assistant: a chat that calls read-only tools to query the usage ledger, the live trace, magpie's runtime log, local agent session folders and content, and magpie's configuration. With an admin key set in Settings, or with magpie and its CLI on the same computer, the assistant can also propose configuration changes from an allow-list (such as key limits, allowed accounts and models, concurrency, account groups, routing groups, and short-lived keys). Each proposal appears as a card; nothing changes until you click Confirm, and you can confirm several cards at once. With an admin key, Insights applies changes through magpie's management API (`/v1/magpie/admin/`), on this computer or a remote magpie alike; without one, it runs the same changes as commands of the local `magpie` CLI. Changes that API has no call for (an agent's model, magpie's settings, subscription sign-ins, a provider's fallback, and a key's memory permission) always run the local `magpie` CLI, so they are offered only when magpie runs on the same computer and its CLI is found. A message can carry up to 4 images (PNG, JPEG, WebP or GIF), attached with the image button, by pasting or by dropping them; the page scales an image whose longer side exceeds 2000 pixels down to that size, keeping its shorter side at 768 pixels or more but no side over 8000 pixels, and sends each at up to 3 MB. Images go only to models whose entry in magpie's model list names image input; with another model selected, the composer says so and offers one that reads images, if one is configured. Conversations are saved and survive restarts. A reply can lay several charts out as a dashboard, in sections and in panels of the widths and heights the assistant chooses, which narrower windows and phones show in fewer columns; any panel opens full width with the button in its corner. The panels the assistant binds to a query of the local usage copy show its figures as they are when the reply is shown, and the dashboard's range control and filter chips read them again for another range or without a filter, until the page is reloaded (only where the page's requests reach the service). On any data view, the AI ball on the page's right edge (or Cmd+K) opens a floating AI window that gives the assistant a text snapshot of what the page currently shows; drag the ball up or down to move it.
21
+ - **Overview**: what needs you now, followed by today's usage, accounts, failures and sessions on one page.
22
+ - **Usage**: requests, tokens, cost and failures for any range up to 366 days, compared with the previous period, and this month's spend projected to its end.
23
+ - **Analysis**: a pivot table of any two dimensions, such as gateway key, model, provider, account, agent, project folder, day or hour.
24
+ - **Requests & sessions**: the costliest sessions and their conversations, single requests, and live traffic with each routing decision.
25
+ - **Accounts & limits**: every account's quota windows, resets and cooldowns, with a forecast of when it runs out.
26
+ - **Failures & retries**: failures grouped by cause, each one saying whether you need to act.
27
+ - **AI assistant**: ask in plain words. It reads usage, logs, sessions and configuration, and can search the web. A change to magpie's configuration shows up as a card and runs only after you confirm it. On any data view, Cmd+K (Ctrl+K) opens it in a floating window that sees what the view shows.
40
28
 
41
- Right-clicking a provider row or an agent row in magpie shows an "Open Insights" item that opens the Insights page filtered to that provider or agent.
29
+ Right-clicking a provider or an agent in magpie opens Insights filtered to it. The interface follows magpie's language, English or Simplified Chinese.
42
30
 
43
- Each view keeps its own time range and filters. A time range is a preset (tonight from 18:00, today, the last 24 hours, yesterday, 7 days, 30 days, or all), the last N hours, or a range of days whose ends may include a time. A filter takes one or more values of a dimension and either keeps only those or leaves them out.
31
+ ## Screenshots
44
32
 
45
- You can copy tables as CSV. Because the page's sandbox does not allow file downloads, Insights copies table data to the clipboard; if the browser blocks clipboard access, the page shows a text box for manual copying. The usage trend, the failure trend and the AI assistant's trend and share charts can be copied or saved as PNG pictures with the buttons in their top right corner; magpie puts them on the clipboard or in Downloads.
33
+ <table>
34
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/overview-en.webp" alt="Overview"><br><b>Overview</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/usage-en.webp" alt="Usage"><br><b>Usage</b></td></tr>
35
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/analysis-en.webp" alt="Analysis"><br><b>Analysis</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/requests-en.webp" alt="Requests &amp; sessions"><br><b>Requests &amp; sessions</b></td></tr>
36
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/accounts-en.webp" alt="Accounts &amp; limits"><br><b>Accounts &amp; limits</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/ai-en.webp" alt="AI assistant"><br><b>AI assistant</b></td></tr>
37
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/overview-dark-en.webp" alt="Dark theme"><br><b>Dark theme</b></td><td width="50%" valign="top" align="center"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/phone-en.webp" width="200" alt="On a phone"><br><b>On a phone</b></td></tr>
38
+ </table>
46
39
 
47
- The interface is available in English and Simplified Chinese and follows magpie's language setting. Traditional Chinese falls back to Simplified Chinese, and other magpie languages fall back to English.
40
+ ## Settings
48
41
 
49
- ## Configuration
42
+ Use the Settings button at the top right of the page, or the plugin's options in magpie (Plugins › Installed › Insights › Options).
50
43
 
51
- You can change settings in two places: the Settings button at the top right of the Insights page (recommended), or the plugin's options JSON in magpie (Plugins › Installed › Insights › Options). Saving from the Settings dialog writes values to `~/.config/magpie/magpie-insights/config.json` with file mode `0600`; of a saved value and a plugin option, the one changed later applies. The Settings dialog can test the connection, and changes apply without a restart. Where the page reaches the service only by loading pages, as behind Cloudflare Access in magpie web, the Settings dialog instead saves into the plugin options through magpie's window, without the connection test; magpie then restarts its plugin host, which briefly restarts the other plugins too.
52
-
53
- | Option | Default | Meaning |
54
- | ------------------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
55
- | `key` | empty | magpie admin gateway key. Needed for gateway-key limits and account groups (admin endpoints); without it, the AI assistant's changes use the local magpie CLI. |
56
- | `url` | empty | Address of a magpie on another computer. Empty means the local magpie. |
57
- | `aiModel` | empty | Default model for the AI assistant. When empty, Insights picks the first available model from a built-in preference list, or else the first model magpie offers. |
58
- | `showConversation` | `true` | Whether a session opened in Requests & sessions shows its conversation; `false` shows only its requests. |
59
- | `autoCompact` | `true` | Whether an AI chat whose history nears the room the model's window leaves for it is compacted into the model's summary of its earlier turns; `false` cuts them. |
60
- | `port` | `4311` | Port of the plugin's local service (plugin options only). Change it if `4311` is taken. |
44
+ | Option | Default | Meaning |
45
+ | ------------------ | ------- | ---------------------------------------------------------------------------------------------------- |
46
+ | `key` | empty | A magpie admin gateway key. It is needed for gateway-key limits and account groups. |
47
+ | `url` | empty | The address of a magpie on another computer. Empty means the local magpie. |
48
+ | `aiModel` | empty | The AI assistant's default model. Empty means Insights picks one. |
49
+ | `showConversation` | `true` | Whether a session shows its conversation; `false` shows only its requests. |
50
+ | `autoCompact` | `true` | Whether a long AI chat is summarised as it nears the model's window; `false` drops its oldest turns. |
51
+ | `port` | `4311` | The port of the plugin's local service (plugin options only). |
61
52
 
62
53
  To create an admin key for the local magpie (magpie prints the key once):
63
54
 
@@ -67,82 +58,36 @@ magpie gateway-key admin <id> local
67
58
  magpie gateway-key models <id> none
68
59
  ```
69
60
 
70
- `models none` prevents the key from calling models. For the local magpie, Insights calls usage endpoints without a key and sends `key` only to admin endpoints.
71
-
72
- When reading a magpie on another computer (`url` is set), Insights sends `key` to every endpoint. That key must have admin scope `any` and must not be limited by models, accounts, or budget; a restricted key sees only its own requests.
73
-
74
- ## How it works
75
-
76
- magpie runs plugin pages in a sandboxed iframe that cannot reach the network. Insights navigates its frame to the plugin's own local service on `127.0.0.1:<port>`, which the plugin runs as a child process of magpie's plugin host, under the Bun runtime that magpie downloads for the host. When the service stops with an error, stops answering, or has made no progress reading magpie for 2 minutes while the page is in use (the plugin checks it every 30 seconds and stops it after three checks in a row that get no answer within 5 seconds or a report that reading is stuck), the plugin starts it again after 5 seconds; each time it fails again within a minute of starting, the wait doubles, up to 5 minutes. When you open `magpie web` from another computer, magpie forwards the service under its own address. Behind a proxy that signs readers in with a cookie, such as Cloudflare Access, the page's own requests carry no cookie: each time the page loads, it checks once whether its requests reach the service, and if they do not, every action (switching views, changing filters, asking the AI assistant) loads the page again instead. magpie keeps the page loaded while you are on another of its pages, so coming back shows the view as you left it, scrolled where it was; a view last read more than 2 minutes earlier is read again then.
77
-
78
- Because magpie's API splits usage by one dimension per query, the service keeps a local copy of magpie's per-request records: every day magpie has, up to the last 366 days. On first start, the service reads those days day by day while the page shows progress; usage older than 366 days is not included. Afterward, while the page is in use, the service reads magpie's records every 20 seconds from the minute its last successful read began, so the read after a pause, a sleep or an outage closes the whole gap. magpie records a call when it ends and reads agents' session files as they are written, so records can arrive late: each read also compares magpie's totals for the hour before that minute with the copy's, and where they differ, the service narrows the difference down to spans of at most 40 records or one minute and rereads only those. The service also reads before building a view when its last read is more than 3 seconds old, waiting at most 1.5 seconds for it, except while the startup read is still under way. A page that shows a data view other than live traffic asks the service every 15 seconds whether the records or magpie's answers that view shows have changed, and loads the view again in place when they have; while the startup read is under way, it loads the view again at each ask, so its progress stays current. A page that loads every action as a page does not ask. After 5 minutes without a request from the page it stops these reads until the next request. Every 10 minutes (hourly while the page is not in use) it compares today's totals hour by hour the same way, and yesterday's until 3 hours after magpie's day ended, when it rereads yesterday whole once. A magpie version from before November 2026 cannot give ranges that end at a minute; with it, each read starts an hour before the last successful read, and the 10-minute check rereads today, and yesterday until that point, whole. After each such check, and after a restart, it compares the totals of the older days with magpie's daily totals and rereads the days that changed, such as days that gained calls reported later or read late from an agent's session file; after a restart it rereads all of them, otherwise at most 8 at a time, newest first. Days start where magpie's own days start, in magpie's time zone and from its day start setting, as on magpie's Usage page. It stores the copy as one binary file per UTC day, column by column and brotli-compressed, and writes only what changed: a saved day that only gained or changed records gets a small file of those records, and the day's file is written again a day after the day ends, after 24 such files, or when records went away. The copy also keeps the project folder of each session that magpie's live trace named, as the trace holds only recent requests. The copy is in `~/Library/Caches/magpie-insights/` on macOS and `$XDG_CACHE_HOME/magpie-insights/` (default `~/.cache/magpie-insights/`) elsewhere, outside `~/.config/magpie`, which magpie may sync. Before writing to disk, the service masks key-like fragments in error messages. When the service stops, it writes the records read since its last save, taking at most 2 seconds. After a restart, the service loads the saved copy and rereads only from the day before it was saved, and also reads the days magpie has before the copy begins; deleting this copy makes the next start read every day again. The same directory holds the AI assistant's conversations, the images attached to them (in a directory beside the conversations file, removed with their conversation; past 256 MB in all, the images of the conversations used longest ago are removed first), and the page token.
79
-
80
- All counts come from the local copy and follow magpie's rules. A request that magpie retried on another account or provider counts once: each failed try it handed on counts as a try, and its tokens and cost count, but it is neither a request nor a failure. Requests that magpie rejects before choosing a provider are counted separately and are not usage; magpie's own calls (provider tests, MCP tool calls, and search API calls) are not counted. A request counts as interrupted when magpie marks it canceled, and as failed when it ends with an error status or message and was not interrupted. Retried counts requests that needed more than one try, however they ended; of those, the ones that eventually succeeded equal magpie's retried count. Cost and tokens match magpie.
61
+ Without an admin key, the assistant makes its changes through the local `magpie` command. For a magpie on another computer, the key needs admin scope `any` and no model, account or budget limits.
81
62
 
82
63
  ## Privacy
83
64
 
84
- Insights reads `/v1/magpie/usage/requests`, `/v1/magpie/quotas`, `/v1/magpie/quotas/history`, `/v1/magpie/accounts`, `/v1/magpie/trace`, and `/v1/models` from magpie's gateway API, plus `/v1/magpie/admin/gateway-keys`, `/v1/magpie/admin/account-groups`, `/v1/magpie/admin/routing-groups` and `/v1/magpie/admin/providers` when an admin key is configured.
85
-
86
- To show a session's conversation (option `showConversation`, on by default), Insights reads it with the local CLI's `magpie sessions show`, which reads the transcripts of the agents magpie may read (`magpie sessions read`). Where that has none and magpie runs on the same computer with its gateway recordings on (`gatewayConversations` in `~/.config/magpie/settings.json`), it reads magpie's recordings under `~/.config/magpie/gateway-conversations/`. Neither source is read for a remote magpie. Each answer holds a page of at most about 200 KB, each part is cut to 8,000 characters until you ask for the rest (up to 60,000), and key-like fragments are masked.
87
-
88
- Insights changes magpie's configuration only through allow-listed calls of magpie's management API and, without an admin key or for what that API has no call for, allow-listed subcommands of the local `magpie` CLI; the plugin does not edit files under `~/.config/magpie`. A provider's answer from the management API holds its keys; the assistant is given only the provider's id, name, state, routing, fallback and model ids.
89
-
90
- The AI assistant runs only when you send it a message. It sends its conversation to the selected model through magpie's `/v1/chat/completions` endpoint, routing to whichever provider serves that model. The conversation includes the tool results that the model requested: usage data, configuration, log lines, and, when the model reads them, local agent session content. Images attached to a message are sent with it, and again with the conversation's later messages while the selected model reads images; otherwise the model is told only that an image was left out. One request carries at most the newest 20 images and 20 MB of them, so older images drop out of what the model sees as a conversation grows, each replaced by a note that one was there.
91
-
92
- Besides magpie (the local instance or the one at the configured `url`), the plugin contacts no other server.
93
-
94
- ## Service security
95
-
96
- - The local service listens on `127.0.0.1` only and answers only requests whose `Host` is `127.0.0.1:<port>` or `localhost:<port>`.
97
- - The service accepts cross-origin requests only from magpie's sandboxed page (origin `null`) or its own address, and refuses requests from other websites.
98
- - Data and action endpoints require a random token handed only to the page the service renders. The service keeps it in its cache directory with file mode `0600`, so an open page keeps working when magpie restarts the service, and replaces it at start once it is 7 days old or when other users can read the file.
99
- - AI-proposed configuration changes run only after the user confirms them.
100
- - Requests that change configuration (confirming an AI-proposed change, testing or saving settings) are POST requests that carry the token in their body rather than in the address. They are plain text requests with no header of their own, so the browser sends no preflight, which an authenticating proxy such as Cloudflare Access would refuse. Where the page cannot make such requests and loads pages instead, a confirmation answers with a redirect to the page without it, so reloading or going back does not repeat it, and the settings go through magpie's window, sent only to the origin that magpie names in the page's address.
65
+ - Insights keeps a copy of magpie's request records, up to 366 days, on this computer: in `~/Library/Caches/magpie-insights/` on macOS and `~/.cache/magpie-insights/` elsewhere.
66
+ - It reads a session's conversation only when you or the assistant open that session, and never from a magpie on another computer.
67
+ - The AI assistant runs only when you send it a message. It sends the conversation, with the data its tools read, to the model you choose through magpie. For release notes, commits and web pages, it reaches `usemagpie.ai`, GitHub and public sites, and it refuses to send keys, account emails or session text.
68
+ - Nothing in magpie changes until you confirm a change, which then goes through magpie's own API or `magpie` command. Insights writes no magpie file itself; its own settings are in `~/.config/magpie/magpie-insights/config.json`.
101
69
 
102
70
  ## Troubleshooting
103
71
 
104
- | Symptom | Cause | Fix |
105
- | ---------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
106
- | The page says the Insights service is not responding | The plugin host did not start the service, the service stopped and waits to be restarted (5 seconds to 5 minutes), or the port is used by another program | The page keeps trying, at intervals that double up to a minute, and opens once the service answers. Run `lsof -nP -iTCP:4311 -sTCP:LISTEN` to see what holds the port; turn the plugin off and on again in magpie's Plugins page, or set another `port` in the plugin options |
107
- | Gateway key limits ask for a key or fail to load | No admin key, or the key lost admin rights | Create an admin key as shown in Configuration and enter it in Settings |
108
- | The gateway key list shows only one key's data | The key used for a remote magpie is limited by models, accounts, or budget | Use an unrestricted key with admin scope `any` |
109
- | The page shows "Failed to read usage from magpie" | magpie's gateway is unreachable, or `url` is wrong | Check that magpie runs and `url` is reachable; the service retries every 20 seconds while the page is in use |
110
- | A view or the AI lacks some data without saying why | A read of magpie the service can do without failed, and the view shows what it has | Start magpie with `MAGPIE_INSIGHTS_DEBUG=1` in its environment: the service then writes a line to magpie's log for each such failure, saying what it read and why it failed |
111
-
112
- ## Development
113
-
114
- Repository layout:
115
-
116
- - `src/ui/`: the page (React and TypeScript).
117
- - `src/server/`: the local service (entry `main.ts`).
118
- - `src/plugin/`: the entry magpie's plugin host loads.
119
- - `src/shared/`: constants, types, and translations shared by both.
120
- - `page/`: the launch page magpie shows first.
121
- - `tests/`: unit tests and a browser end-to-end suite.
72
+ - **The page says the service is not responding**: another program may hold port 4311 (`lsof -nP -iTCP:4311 -sTCP:LISTEN`). Set another `port` in the plugin options, or turn the plugin off and on.
73
+ - **Gateway key limits ask for a key**: create an admin key as shown above and enter it in Settings.
74
+ - **"Failed to read usage from magpie"**: check that magpie is running and that `url`, if set, is reachable.
122
75
 
123
- Commands (Node.js 26 or later, pnpm):
76
+ How the service reads and counts magpie's records, its security, every option, and more fixes are in the [reference](docs/reference.md).
124
77
 
125
- - `pnpm install`
126
- - `pnpm run dev`: rebuilds on change.
127
- - `pnpm run build`: builds `dist/` with esbuild.
128
- - `pnpm run check`: runs Biome (lint and format), the TypeScript typecheck, the build, and the unit tests. Two of them run the built service under Bun: `$BUN`, else the Bun magpie downloaded, else `bun` on `PATH`; without one they are skipped. One of the two, `tests/budget.test.mjs`, loads 1.1 million synthetic records over 365 days and checks the load time, each view's build time and size, the size of the filter option lists, the resident memory at ready and at its peak, and the JS heap against the budgets at the top of the file.
129
- - `pnpm run test:e2e`: runs the end-to-end suite in a local headless Chrome (set `CHROME_PATH` if Chrome is not in a standard location).
130
-
131
- magpie loads `dist/index.mjs`, which starts `dist/server.mjs`; run `pnpm run build` after changing `src/`. When you add a plugin folder by path, magpie restarts its plugin host whenever files in that folder change.
132
-
133
- For interface text, the English text is the key passed to `t()` from `src/shared/i18n.ts`, and Chinese translations live in `src/shared/i18n-zh.ts`. `tests/i18n.test.mjs` fails when a string lacks a Chinese translation, when the dictionary has unused entries, or when Chinese appears in `src/` outside the dictionary.
134
-
135
- On every push to `main` and every pull request, CI runs `pnpm run check` with Bun 1.4.5 and `pnpm run test:e2e` on Linux. A separate workflow runs `pnpm run test:e2e` on macOS, where magpie's desktop window draws the page, once a day and when started by hand; nothing waits on it, because GitHub's macOS runners can be unavailable for long stretches.
78
+ ## Development
136
79
 
137
- To create a release, push a tag `vX.Y.Z` equal to the version in `package.json`. CI runs the checks, attaches the packed tarball to a GitHub Release, and publishes to npm when the `NPM_TOKEN` secret is set.
80
+ Needs Node.js 26 or later and pnpm.
138
81
 
139
- ## Limits
82
+ ```sh
83
+ pnpm install
84
+ pnpm run build # dist/, which magpie loads
85
+ pnpm run check # lint, typecheck, build and unit tests
86
+ pnpm run test:e2e # browser tests in a local headless Chrome
87
+ pnpm run screenshots # the pictures in docs/screenshots, from demo data
88
+ ```
140
89
 
141
- - History older than 366 days is not shown.
142
- - Export is copy-only (no file download in the plugin sandbox).
143
- - Interface languages are English and Simplified Chinese.
144
- - AI assistant configuration changes need an admin key or a local magpie CLI; without either, the assistant is read-only and suggests `magpie ...` commands instead. With a remote `url`, changes need the admin key, and those the management API has no call for are not offered.
145
- - Images can be attached only where the page reaches its service directly. Where it loads pages instead (behind a sign-in proxy such as Cloudflare Access), the image button is off, and images already in a conversation show as placeholders.
90
+ To install a checkout, run `magpie plugin add ./magpie-insights` after building it. The layout, translations, CI and releases are described in the [reference](docs/reference.md#development).
146
91
 
147
92
  ## License
148
93
 
package/README.zh-CN.md CHANGED
@@ -2,62 +2,53 @@
2
2
 
3
3
  [English](README.md) | 简体中文
4
4
 
5
- 洞察(英文界面为 Insights,npm 包名 `magpie-insights`)是 magpie 的[页面插件](https://usemagpie.ai/docs/plugins)。magpie 是本地网关,汇总模型订阅与账号,提供给 Claude Code、Codex 等编程 Agent 使用。插件会在 magpie 桌面窗口和 `magpie web` 侧边栏的「插件」下添加「洞察」页面,并在供应商行和 Agent 行的右键菜单中添加「在洞察中查看」菜单项。
5
+ <p align="center"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/hero-zh.webp" alt="magpie 洞察:总览、等待确认的 AI 修改和手机布局" width="100%"></p>
6
+
7
+ 洞察(英文界面为 Insights)是 [magpie](https://usemagpie.ai) 的页面插件。magpie 是本地网关,汇总模型订阅与账号,提供给 Claude Code、Codex 等编程 Agent 使用。洞察告诉你请求、token 和费用花在了哪里,哪些账号快到额度上限,哪些请求失败了、原因是什么;AI 助手可以回答关于这些数据的问题。页面位于 magpie 侧边栏的「插件」下,桌面窗口和 `magpie web` 中都有。
6
8
 
7
9
  ## 安装
8
10
 
9
11
  需要 magpie 0.1.2836 或更高版本。
10
12
 
11
- 从 npm 安装(推荐),发布包内已包含构建好的文件:
12
-
13
13
  ```sh
14
14
  magpie plugin add magpie-insights
15
15
  ```
16
16
 
17
- 不能直接从 GitHub 仓库安装(`magpie plugin add github:vika2603/magpie-insights`),因为 magpie 按仓库提交内容直接加载,构建后的 `dist/` 目录未提交到仓库,且 magpie 不会运行安装脚本。
18
-
19
- 从本地检出目录安装(仅构建和测试需要 Node.js 26 或更高版本及 pnpm):
20
-
21
- ```sh
22
- git clone https://github.com/vika2603/magpie-insights
23
- cd magpie-insights
24
- pnpm install
25
- pnpm run build
26
- cd ..
27
- magpie plugin add ./magpie-insights
28
- ```
29
-
30
- 安装后,若插件尚未开启,请在 magpie 的「插件」页面将其开启。开启后,侧边栏的「插件」下会出现「洞察」页面。
17
+ 如果插件未开启,在 magpie 的「插件」页面中开启。
31
18
 
32
- ## 显示内容
19
+ ## 功能
33
20
 
34
- - 用量:所选时间范围内的请求、失败、中断、重试、费用和 token,含趋势与细分,以及本月花费到月底的预测。
35
- - 多维分析:自选行维度、列维度和指标的透视表,可一键互换行与列。维度包括网关 Key、模型、供应商、账号、Agent、项目目录和时间(天、小时、一天中的小时、星期)。
36
- - 明细与会话:费用最高的会话及其运行所在的项目目录,每个会话可就地展开为其对话(提问、回答、思考和运行的工具,逐页加载),读不到文本时展开为其逐条请求;单条请求的搜索与可展开详情、实时流量(进行中的请求、每条请求的路由决策及跳过的账号),以及 Prompt token 构成(如系统、工具、技能、记忆、文件和结果)。
37
- - 账号与限额:各账号的状态、当前冷却倒计时、所有额度窗口、最近一小时的错误以及消耗预测。
38
- - 失败与重试:先列出现在需要你处理的事,不受时间范围和筛选影响:被封、登录失效、余额不足或每次尝试都失败的账号,以及反复拦下某个模型的网关 Key,并说明怎么处理。再按原因列出这段时间的失败,标明是否需要处理,最后是全部失败记录,含换账号后完成的尝试。
39
- - AI 助手:对话式助手,调用只读工具查询本地用量记录、实时 trace、magpie 运行日志、本地 Agent 会话目录与内容,以及 magpie 配置。在设置中配置管理 key 后,或 magpie 及其 CLI 位于本机时,助手还可提议白名单内的配置修改(如网关 Key 限额、允许的账号与模型、并发、账号组、路由组和临时 Key)。每项提议显示为一张卡片;点击确认前不会发生任何修改,可一次确认多张卡片。配置了管理 key 时,洞察通过 magpie 的管理 API(`/v1/magpie/admin/`)应用修改,本机和远程的 magpie 都适用;未配置时,改为运行本地 `magpie` CLI 的对应命令完成同样的修改。管理 API 没有对应接口的修改(Agent 的模型、magpie 设置、订阅账号登录、供应商 Fallback 和 Key 的记忆权限)始终运行本地 `magpie` CLI,因此只在 magpie 运行于本机且能找到其 CLI 时提供。每条消息可通过图片按钮、粘贴或拖放附带最多 4 张图片(PNG、JPEG、WebP 或 GIF);长边超过 2000 像素的图片会先在页面中缩小到该尺寸,但短边不小于 768 像素,且任何一边都不超过 8000 像素,每张发送时最大 3 MB。图片只发送给在 magpie 模型列表中声明支持图片输入的模型;选中其他模型时,输入框会说明原因,并在已配置能读图片的模型时提供切换。对话会保存,重启后依然保留。回复可以把多个图表组成一个仪表盘,由助手决定分组以及各面板的宽度和高度,较窄的窗口和手机上会以更少的列显示;面板右上角的按钮可全宽查看该面板。助手绑定到本地用量副本查询的面板,显示回复展示时的数据,仪表盘的时间范围切换和筛选标签可按其他时间范围或去掉某个筛选重新读取,直到页面重新加载(仅在页面请求能直接到达服务时可用)。在任意数据视图中点页面右侧的 AI 悬浮球(或按 Cmd+K)打开 AI 浮窗即可共视,将当前页面显示内容的文本快照提供给助手;上下拖动悬浮球可移动其位置。
21
+ - **总览**:先列出现在需要你处理的事,再在一页内汇总今天的用量、账号、失败和会话。
22
+ - **用量**:最长 366 天内任意时间范围的请求、token、费用和失败,与上一周期对比,并预测本月花费到月底的金额。
23
+ - **多维分析**:任选两个维度的透视表,维度包括网关 Key、模型、供应商、账号、Agent、项目目录、天和小时。
24
+ - **明细与会话**:费用最高的会话及其对话、单条请求,以及实时流量和每条请求的路由决策。
25
+ - **账号与限额**:每个账号的额度窗口、重置时间和冷却,并预测额度何时用完。
26
+ - **失败与重试**:按原因归类的失败,每类都标明是否需要你处理。
27
+ - **AI 助手**:用自然语言提问。它读取用量、日志、会话和配置,也能搜索网页。修改 magpie 配置时会先显示一张卡片,经你确认后才执行。在任意数据视图中按 Cmd+K(Ctrl+K)可打开浮动窗口,助手能看到当前视图显示的内容。
40
28
 
41
- 在 magpie 中右键点击供应商行或 Agent 行会显示「在洞察中查看」菜单项,点击后打开按该供应商或 Agent 筛选的洞察页面。
29
+ 在 magpie 中右键点击供应商或 Agent,可打开按其筛选的洞察页面。界面跟随 magpie 的语言设置,支持英文和简体中文。
42
30
 
43
- 每个视图各自保留独立的时间范围和筛选条件。时间范围可以是预设值(今晚 18:00 起、今天、最近 24 小时、昨天、7 天、30 天或全部)、最近 N 小时,或起止两端可包含时间的日期范围。筛选条件可选择某个维度的一个或多个值,只保留这些值或排除这些值。
31
+ ## 截图
44
32
 
45
- 可将表格复制为 CSV。由于页面沙箱不允许下载文件,洞察会将表格数据复制到剪贴板;如果浏览器阻止访问剪贴板,页面会显示文本框供手动复制。用量趋势图、失败趋势图以及 AI 助手的趋势图和占比图,可以用图表右上角的按钮复制或保存为 PNG 图片,由 magpie 放入剪贴板或保存到“下载”文件夹。
33
+ <table>
34
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/overview-zh.webp" alt="总览"><br><b>总览</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/usage-zh.webp" alt="用量"><br><b>用量</b></td></tr>
35
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/analysis-zh.webp" alt="多维分析"><br><b>多维分析</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/requests-zh.webp" alt="明细与会话"><br><b>明细与会话</b></td></tr>
36
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/accounts-zh.webp" alt="账号与限额"><br><b>账号与限额</b></td><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/ai-zh.webp" alt="AI 助手"><br><b>AI 助手</b></td></tr>
37
+ <tr><td width="50%" valign="top"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/overview-dark-zh.webp" alt="深色主题"><br><b>深色主题</b></td><td width="50%" valign="top" align="center"><img src="https://raw.githubusercontent.com/vika2603/magpie-insights/main/docs/screenshots/phone-zh.webp" width="200" alt="手机上"><br><b>手机上</b></td></tr>
38
+ </table>
46
39
 
47
- 界面支持英文和简体中文,跟随 magpie 的语言设置。繁体中文回退为简体中文,magpie 的其他语言回退为英文。
40
+ ## 设置
48
41
 
49
- ## 配置
42
+ 点击页面右上角的「设置」按钮,或在 magpie 中编辑插件选项(插件 › 已安装 › Insights › 选项)。
50
43
 
51
- 可在两处修改设置:洞察页面右上角的「设置」按钮(推荐),或 magpie 中的插件选项 JSON(插件 › 已安装 › Insights › 选项)。在「设置」对话框中保存时,配置会以 `0600` 文件权限写入 `~/.config/magpie/magpie-insights/config.json`;已保存的值与插件选项中,后修改的一方生效。「设置」对话框可测试连接,修改无需重启即可生效。页面只能通过加载页面访问服务时(例如 magpie web 位于 Cloudflare Access 之后),「设置」对话框改为经 magpie 窗口保存到插件选项,不提供连接测试;随后 magpie 会重启插件宿主,其他插件也会短暂重启。
52
-
53
- | 选项 | 默认值 | 说明 |
54
- | ------------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
55
- | `key` | 空 | 具有管理权限的 magpie 网关 Key(管理 key)。读取网关 Key 限额和账号组(管理接口)时需要;未配置时,AI 助手改用本地 magpie CLI 修改配置。 |
56
- | `url` | 空 | 另一台电脑上 magpie 的地址。留空表示本机 magpie。 |
57
- | `aiModel` | 空 | AI 助手的默认模型。留空时,洞察从内置优先列表中选择第一个可用模型,否则选择 magpie 提供的第一个模型。 |
58
- | `showConversation` | `true` | 在明细与会话中展开会话时是否显示其对话;为 `false` 时只显示其请求。 |
59
- | `autoCompact` | `true` | AI 对话的历史接近模型窗口留给它的空间时,是否由模型把较早的轮次总结成摘要;为 `false` 时直接截掉它们。 |
60
- | `port` | `4311` | 插件本地服务的端口(只能在插件选项中设置)。若 `4311` 被占用请修改此项。 |
44
+ | 选项 | 默认值 | 说明 |
45
+ | ------------------ | ------ | ----------------------------------------------------------------------------- |
46
+ | `key` | 空 | 具有管理权限的 magpie 网关 Key(管理 key),读取网关 Key 限额和账号组时需要。 |
47
+ | `url` | 空 | 另一台电脑上 magpie 的地址。留空表示本机 magpie。 |
48
+ | `aiModel` | 空 | AI 助手的默认模型。留空时由洞察选择。 |
49
+ | `showConversation` | `true` | 会话是否显示其对话;为 `false` 时只显示其请求。 |
50
+ | `autoCompact` | `true` | AI 对话接近模型窗口上限时是否压缩为摘要;为 `false` 时丢弃最早的轮次。 |
51
+ | `port` | `4311` | 插件本地服务的端口(只能在插件选项中设置)。 |
61
52
 
62
53
  为本机 magpie 创建管理 key(magpie 只会打印一次该 key):
63
54
 
@@ -67,83 +58,37 @@ magpie gateway-key admin <id> local
67
58
  magpie gateway-key models <id> none
68
59
  ```
69
60
 
70
- `models none` 禁止该 key 调用模型。对于本机 magpie,洞察调用用量接口时不带 key,仅向管理接口发送 `key`。
71
-
72
- 读取另一台电脑上的 magpie(已设置 `url`)时,洞察会向所有接口发送 `key`。该 key 的管理权限范围必须为 `any`,且不能受模型、账号或预算限制;受限的 key 只能看到自身的请求。
73
-
74
- ## 工作原理
75
-
76
- magpie 在无法访问网络的沙箱 iframe 中运行插件页面。洞察把所在的 iframe 导航到插件的本地服务 `127.0.0.1:<port>`,该服务由插件作为 magpie 插件宿主的子进程运行,使用 magpie 为宿主下载的 Bun 运行时。服务因错误退出、不再响应,或在页面使用期间读取 magpie 连续 2 分钟没有进展时(插件每 30 秒检查一次,连续三次未在 5 秒内得到响应或得到读取停滞的报告即终止服务),插件会在 5 秒后重新启动它;若启动后一分钟内再次失败,等待时间加倍,最长 5 分钟。从另一台电脑打开 `magpie web` 时,magpie 会在自身地址下转发该服务。如果前面还有通过 cookie 验证访问者的代理(如 Cloudflare Access),页面自身发出的请求不带 cookie:页面每次加载时检查一次请求能否到达服务,若不能,之后的每个操作(切换视图、修改筛选条件、向 AI 助手提问)都改为重新加载页面。切换到 magpie 的其他页面时,magpie 会保留洞察页面,回来时视图和滚动位置保持原样;视图距上次读取超过 2 分钟时,回来后会重新读取。
77
-
78
- 由于 magpie 的 API 每次查询只能按一个维度拆分用量,本地服务会保存 magpie 逐条请求记录的本地副本,包含 magpie 保存的每一天,最多保留最近 366 天。首次启动时,服务按天读取这些数据,页面同时显示进度;早于 366 天的用量不包含在内。此后,页面使用期间,服务每 20 秒从上次成功读取开始的那一分钟起读取 magpie 的记录,因此暂停、休眠或中断之后的那次读取会补齐整段空缺。magpie 在调用结束时才记录调用,并在 Agent 写入会话文件时读取其中的调用,因此记录可能晚到:每次读取还会将 magpie 在该分钟之前一小时的合计与副本比较,如有差异,服务将差异缩小到最多 40 条记录或一分钟的时段,只重新读取这些时段。打开视图时,如果上次读取已超过 3 秒,也会先读取一次,最多等待 1.5 秒;启动时的读取尚未完成时除外。页面显示实时流量以外的数据视图时,每 15 秒询问服务该视图所用的记录或 magpie 的回应是否有变化,有变化就在原处重新加载视图;启动时的读取尚未完成时,每次询问都重新加载,使进度保持最新。每次操作都重新加载页面的模式下,页面不询问。页面 5 分钟没有请求后,服务停止这类读取,直到下一次请求。服务每 10 分钟(页面未使用时每小时)按同样方式逐小时比较今天的合计,并比较昨天的合计,直到 magpie 的这一天结束 3 小时后,再完整重新读取一次昨天。2026 年 11 月之前的 magpie 版本不支持结束于某一分钟的时段;使用这类版本时,每次读取从上次成功读取的前一小时读起,每 10 分钟的检查会完整重新读取今天,以及截至上述时间之前的昨天。每次这样的检查之后以及重启之后,服务会将更早日期的合计与 magpie 的每日合计比较,重新读取有变化的日期,例如事后上报的调用或较晚从 Agent 会话文件读到的调用所在的日期;重启后全部重新读取,其他时候每次最多 8 天,由近及远。每天的起止与 magpie 一致,按 magpie 的时区和每日开始时间划分,与 magpie 用量页相同。服务将副本按 UTC 日期分为二进制文件,按列存储并用 brotli 压缩,只写入有变化的部分:已保存的日期如果只新增或更新了记录,就为这些记录另写一个小文件;该日期结束一天后、累计 24 个这样的小文件后或有记录被删除时,再重写该日期的文件。由于 magpie 的实时 trace 只保留最近的请求,副本还会保存 trace 中出现过的每个会话的项目目录。副本在 macOS 上存放在 `~/Library/Caches/magpie-insights/`,在其他系统上存放在 `$XDG_CACHE_HOME/magpie-insights/`(默认 `~/.cache/magpie-insights/`),位于 magpie 可能会同步的 `~/.config/magpie` 之外。写入磁盘前,服务会遮盖错误消息中形似 key 的片段。服务停止时会写入上次保存后读取的记录,最多用时 2 秒。重启后,服务加载已保存的副本,只重新读取保存前一天至今的数据,并读取 magpie 中早于副本起始日期的数据;删除该副本会让下次启动重新读取全部数据。同一目录还保存 AI 助手的对话、对话中附带的图片(存放在对话文件旁的目录中,随对话一起删除;总量超过 256 MB 时,先删除最久未使用的对话的图片)和页面 token。
79
-
80
- 所有计数都来自本地副本,并遵循 magpie 的规则。magpie 换用其他账号或供应商重试过的请求只计一次:转交出去的每次失败尝试计为一次尝试,其 token 和费用计入统计,但不计为请求,也不计为失败。magpie 在选定供应商之前就拒绝的请求单独计数,不计入用量;magpie 自身的调用(供应商测试、MCP 工具调用和搜索 API 调用)不计入。magpie 标记为取消的请求计为中断;以错误状态码或错误消息结束且未中断的请求计为失败。重试数统计需要多次尝试的请求,无论最终结果如何;其中最终成功的请求数与 magpie 的重试数一致。费用和 token 与 magpie 一致。
61
+ 未配置管理 key 时,助手通过本机的 `magpie` 命令执行修改。连接另一台电脑上的 magpie 时,key 的管理权限范围必须为 `any`,且不能受模型、账号或预算限制。
81
62
 
82
63
  ## 隐私
83
64
 
84
- 洞察从 magpie 网关 API 读取 `/v1/magpie/usage/requests`、`/v1/magpie/quotas`、`/v1/magpie/quotas/history`、`/v1/magpie/accounts`、`/v1/magpie/trace` 和 `/v1/models`;配置了管理 key 时,还会读取 `/v1/magpie/admin/gateway-keys`、`/v1/magpie/admin/account-groups`、`/v1/magpie/admin/routing-groups` 和 `/v1/magpie/admin/providers`。
85
-
86
- 为显示会话的对话(选项 `showConversation`,默认开启),洞察用本地 CLI 的 `magpie sessions show` 读取,它读取 magpie 获准读取的 Agent 的会话记录(`magpie sessions read`)。读不到且 magpie 与插件运行在同一台电脑、并开启了网关对话录制(`~/.config/magpie/settings.json` 中的 `gatewayConversations`)时,读取 `~/.config/magpie/gateway-conversations/` 下的录制。magpie 在远程时不读取这两个来源。每次返回最多约 200 KB 的一页,每段先截断到 8,000 个字符,按需再加载其余部分(最多 60,000 个字符),形似 key 的片段会被遮盖。
87
-
88
- 洞察只通过 magpie 管理 API 中白名单内的调用修改 magpie 配置,未配置管理 key 或管理 API 没有对应接口时才调用本地 `magpie` CLI 白名单内的子命令;插件不会编辑 `~/.config/magpie` 下的文件。管理 API 返回的供应商信息包含其 key,提供给助手的只有供应商的 ID、名称、状态、路由方式、Fallback 和模型 ID。
89
-
90
- AI 助手仅在你发送消息时运行。它调用 magpie 的 `/v1/chat/completions` 接口将对话发送给所选模型,由 magpie 路由至提供该模型的供应商。对话中包含模型所请求工具的返回结果:用量数据、配置、日志行,以及模型读取本地 Agent 会话时返回的内容。消息附带的图片随该消息发送;所选模型能读图片时,同一对话之后的消息也会再次带上,否则模型只会得知此处有一张图片被略去。每次请求最多带上最新的 20 张、共 20 MB 的图片,因此对话变长后,较早的图片不再发给模型,原处改为一条说明此处曾有图片的提示。
91
-
92
- 除 magpie(本机实例或 `url` 配置的实例)外,插件不连接任何其他服务器。
93
-
94
- ## 服务安全
95
-
96
- - 本地服务仅监听 `127.0.0.1`,且仅响应 `Host` 为 `127.0.0.1:<port>` 或 `localhost:<port>` 的请求。
97
- - 服务仅接受来自 magpie 沙箱页面(origin 为 `null`)或服务自身地址的跨域请求,拒绝来自其他网站的请求。
98
- - 数据和操作接口要求携带随机 token,该 token 仅交给服务自身渲染的页面。服务将其以 `0600` 文件权限保存在缓存目录中,magpie 重启服务后已打开的页面仍可继续使用;启动时若该 token 已满 7 天,或文件可被其他用户读取,服务会换用新的 token。
99
- - AI 提议的配置修改须经用户确认后才会执行。
100
- - 修改配置的请求(确认 AI 提议的修改、测试或保存设置)使用 POST,并将 token 放在请求体而非地址中。这类请求以纯文本发送,不带自定义请求头,浏览器因此不发送预检请求;Cloudflare Access 等认证代理会拒绝预检请求。页面无法发送这类请求而改为加载页面时,确认操作会重定向到不含该操作的页面,因此刷新或后退不会重复执行;设置则经 magpie 窗口保存,只发送给 magpie 在页面地址中指明的来源。
65
+ - 洞察在本机保存 magpie 请求记录的副本,最多 366 天:macOS 上位于 `~/Library/Caches/magpie-insights/`,其他系统位于 `~/.cache/magpie-insights/`。
66
+ - 只有你或助手打开某个会话时,洞察才读取该会话的对话;从不读取另一台电脑上 magpie 的对话。
67
+ - AI 助手只在你发送消息后运行。它把对话及其工具读到的数据经 magpie 发给你选择的模型。查看版本说明、提交记录和网页时,它会访问 `usemagpie.ai`、GitHub 和公开网站,并拒绝发送 key、账号邮箱和会话内容。
68
+ - 你确认之前,magpie 不会有任何修改;确认后的修改通过 magpie 自己的 API 或 `magpie` 命令执行。洞察不直接写入 magpie 的文件,自身设置保存在 `~/.config/magpie/magpie-insights/config.json`。
101
69
 
102
70
  ## 故障排查
103
71
 
104
- | 现象 | 原因 | 解决方法 |
105
- | ------------------------------------ | ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
106
- | 页面提示「洞察服务没有响应」 | 插件宿主未启动该服务,服务已退出、正等待重新启动(5 秒至 5 分钟),或已有其他程序占用该端口 | 页面会继续尝试,间隔逐次翻倍、最长一分钟,服务一响应即打开。运行 `lsof -nP -iTCP:4311 -sTCP:LISTEN` 查看占用端口的程序;在 magpie 的「插件」页面将插件关闭再重新开启,或在插件选项中设置其他 `port` |
107
- | 网关 Key 限额提示需要 key 或加载失败 | 未配置管理 key,或该 key 已失去管理权限 | 按「配置」一节的方法创建管理 key,并填入「设置」 |
108
- | 网关 Key 列表只显示一个 key 的数据 | 连接远程 magpie 所用的 key 带有模型、账号或预算限制 | 改用管理权限范围为 `any` 且无限制的 key |
109
- | 页面显示「读取 magpie 用量失败」 | 无法连接 magpie 网关,或 `url` 填写有误 | 检查 magpie 是否正在运行且 `url` 是否可达;页面使用期间,服务每 20 秒重试一次 |
110
- | 视图或 AI 缺少部分数据,没有说明原因 | 服务读取 magpie 的某项可缺省的数据失败,视图只显示已有内容 | 在 magpie 的环境中设置 `MAGPIE_INSIGHTS_DEBUG=1` 后启动 magpie:服务会为每次此类失败在 magpie 的日志中写一行,说明读取的内容和失败原因 |
111
-
112
- ## 开发
113
-
114
- 仓库目录结构:
115
-
116
- - `src/ui/`:页面(React 与 TypeScript)。
117
- - `src/server/`:本地服务(入口为 `main.ts`)。
118
- - `src/plugin/`:magpie 插件宿主加载的入口。
119
- - `src/shared/`:两端共享的常量、类型与翻译。
120
- - `page/`:magpie 首先显示的启动页。
121
- - `tests/`:单元测试与浏览器端到端测试套件。
72
+ - **页面提示服务没有响应**:端口 4311 可能被其他程序占用(`lsof -nP -iTCP:4311 -sTCP:LISTEN`)。在插件选项中换一个 `port`,或将插件关闭再开启。
73
+ - **网关 Key 限额要求填写 key**:按上文创建管理 key,并填入「设置」。
74
+ - **「读取 magpie 用量失败」**:检查 magpie 是否在运行;如果设置了 `url`,检查该地址是否可达。
122
75
 
123
- 命令(Node.js 26 或更高版本,pnpm):
76
+ 服务如何读取和统计 magpie 的记录、服务安全、全部设置项和更多故障排查,见[参考文档](docs/reference.zh-CN.md)。
124
77
 
125
- - `pnpm install`
126
- - `pnpm run dev`:文件改动时重新构建。
127
- - `pnpm run build`:使用 esbuild 构建 `dist/`。
128
- - `pnpm run check`:运行 Biome(代码检查与格式化)、TypeScript 类型检查、构建和单元测试。其中两项测试在 Bun 下运行构建后的服务:依次使用 `$BUN`、magpie 下载的 Bun、`PATH` 中的 `bun`;都没有时跳过这两项测试。其中 `tests/budget.test.mjs` 载入 110 万条覆盖 365 天的合成记录,按文件开头的预算检查载入时间、各视图的构建时间与数据大小、筛选选项列表的大小、就绪时与峰值的常驻内存,以及 JS 堆内存。
129
- - `pnpm run test:e2e`:在本地无头 Chrome 中运行端到端测试套件(若 Chrome 不在标准位置,请设置 `CHROME_PATH`)。
130
-
131
- magpie 加载 `dist/index.mjs`,后者启动 `dist/server.mjs`;修改 `src/` 后需运行 `pnpm run build`。按路径添加插件目录时,只要该目录内的文件发生变动,magpie 就会重启插件宿主。
132
-
133
- 界面文案以英文原文作为键传入 `src/shared/i18n.ts` 的 `t()`,中文翻译存放在 `src/shared/i18n-zh.ts`。当字符串缺少中文翻译、词典存在未使用的条目,或 `src/` 中词典以外出现中文时,`tests/i18n.test.mjs` 会失败。
134
-
135
- 每次推送到 `main` 和每个 pull request 时,CI 在 Linux 上使用 Bun 1.4.5 运行 `pnpm run check`,并运行 `pnpm run test:e2e`。另有一个工作流每天一次、也可手动触发,在 macOS(magpie 桌面窗口绘制页面的系统)上运行 `pnpm run test:e2e`;由于 GitHub 的 macOS 运行器可能长时间无法分配,其他任务都不等待它。
78
+ ## 开发
136
79
 
137
- 创建发布版本时,推送与 `package.json` 中版本号一致的 `vX.Y.Z` 标签。CI 会运行检查,将打包好的 tarball 附加到 GitHub Release,并在配置了 `NPM_TOKEN` 密钥时发布到 npm。
80
+ 需要 Node.js 26 或更高版本和 pnpm。
138
81
 
139
- ## 限制
82
+ ```sh
83
+ pnpm install
84
+ pnpm run build # 构建 magpie 加载的 dist/
85
+ pnpm run check # lint、类型检查、构建和单元测试
86
+ pnpm run test:e2e # 在本地无头 Chrome 中运行浏览器测试
87
+ pnpm run screenshots # 用演示数据生成 docs/screenshots 中的图片
88
+ ```
140
89
 
141
- - 不显示早于 366 天的历史数据。
142
- - 导出仅支持复制(插件沙箱内无法下载文件)。
143
- - 界面语言为英文和简体中文。
144
- - AI 助手修改配置需要管理 key 或本地 magpie CLI;两者都没有时,助手为只读模式,改为给出 `magpie ...` 命令建议。配置了远程 `url` 时,修改需要管理 key,且不提供管理 API 没有对应接口的修改。
145
- - 只有页面能直接访问服务时才能添加图片。页面改为加载页面时(位于 Cloudflare Access 等登录代理之后),图片按钮不可用,对话中已有的图片显示为占位符。
90
+ 构建后运行 `magpie plugin add ./magpie-insights` 可从本地目录安装。目录结构、翻译、CI 和发布流程见[参考文档](docs/reference.zh-CN.md#开发)。
146
91
 
147
92
  ## 许可证
148
93
 
149
- MIT。详见 [LICENSE](LICENSE)。
94
+ MIT,见 [LICENSE](LICENSE)。
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "magpie-insights",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
- "description": "Shows usage analytics by gateway key, model, provider, account, agent and time, plus quotas, cooldowns and failures; its AI assistant changes configuration only after you confirm. Reads magpie's usage API into a local copy of up to 366 days; runs a 127.0.0.1 service only its page can use.",
5
+ "description": "Shows usage analytics by gateway key, model, provider, account, agent and time, plus quotas, cooldowns and failures; its AI assistant changes configuration only after you confirm. Reads magpie's usage API into a local copy of up to 366 days; runs a local service on 127.0.0.1 for its page.",
6
6
  "keywords": [
7
7
  "magpie",
8
8
  "magpie-plugin",
@@ -53,6 +53,7 @@
53
53
  "test:e2e": "node --test --test-timeout=120000 tests/e2e/*.e2e.ts",
54
54
  "check": "pnpm run biome && pnpm run typecheck && pnpm run build && pnpm run test",
55
55
  "check:all": "pnpm run check && pnpm run test:e2e",
56
+ "screenshots": "node scripts/screenshots.ts",
56
57
  "prepublishOnly": "pnpm run check"
57
58
  },
58
59
  "devDependencies": {
@@ -108,8 +109,8 @@
108
109
  }
109
110
  ],
110
111
  "summary": {
111
- "en": "Shows usage analytics by gateway key, model, provider, account, agent and time, plus quotas, cooldowns and failures; its AI assistant changes configuration only after you confirm. Reads magpie's usage API into a local copy of up to 366 days; runs a 127.0.0.1 service only its page can use.",
112
- "zh": "按网关 Key、模型、供应商、账号、Agent 和时间分析用量,展示额度、冷却与失败;含 AI 助手,配置修改仅在你确认后执行。读取 magpie 用量 API 存入本地最多 366 天的副本;在 127.0.0.1 运行仅自身页面可用的本地服务。"
112
+ "en": "Shows usage analytics by gateway key, model, provider, account, agent and time, plus quotas, cooldowns and failures; its AI assistant changes configuration only after you confirm. Reads magpie's usage API into a local copy of up to 366 days; runs a local service on 127.0.0.1 for its page.",
113
+ "zh": "按网关 Key、模型、供应商、账号、Agent 和时间分析用量,展示额度、冷却与失败;含 AI 助手,配置修改仅在你确认后执行。读取 magpie 用量 API 存入本地最多 366 天的副本;在 127.0.0.1 运行供页面使用的本地服务。"
113
114
  }
114
115
  }
115
116
  }