afucontext 0.0.0-stage → 0.1.18

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 (32) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +182 -2
  3. package/README.zh-CN.md +183 -0
  4. package/bin/afu.mjs +127 -0
  5. package/bin/cache-lease.mjs +16 -0
  6. package/bin/host-autoload.py +546 -0
  7. package/bin/install-guide.mjs +56 -0
  8. package/bin/install-process.mjs +19 -0
  9. package/bin/platform.mjs +74 -0
  10. package/bin/prebuilt.mjs +64 -0
  11. package/bin/runtime.mjs +115 -0
  12. package/package.json +29 -4
  13. package/platform-dependencies.json +15 -0
  14. package/platform-locks/dsh/package-lock.json +425 -0
  15. package/platform-locks/dsh/package.json +16 -0
  16. package/platform-locks/pi/package-lock.json +1789 -0
  17. package/platform-locks/pi/package.json +9 -0
  18. package/python/afu_context_local-0.1.18-py3-none-any.whl +0 -0
  19. package/python/constraints.txt +25 -0
  20. package/release.json +7 -0
  21. package/scripts/dsh-overlay.mjs +105 -0
  22. package/scripts/dsh-presets.mjs +49 -0
  23. package/scripts/dsh-web-probe.mjs +14 -0
  24. package/src/afu_host_integrations/dsh/dist/commands.js +28 -0
  25. package/src/afu_host_integrations/dsh/dist/index.js +548 -0
  26. package/src/afu_host_integrations/pi/dist/index.js +565 -0
  27. package/src/afu_host_integrations/shared/runtime-client/dist/commands.js +40 -0
  28. package/src/afu_host_integrations/shared/runtime-client/dist/host-worker.js +170 -0
  29. package/src/afu_host_integrations/shared/runtime-client/dist/index.js +243 -0
  30. package/src/afu_host_integrations/shared/runtime-client/dist/prefix.js +12 -0
  31. package/src/afu_host_integrations/shared/runtime-client/dist/protocol.js +79 -0
  32. package/src/afu_host_integrations/shared/runtime-client/dist/telemetry.js +78 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,183 @@
1
- # Temporary Holding Version
1
+ # AFU Context · Local asynchronous context compression
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ **English** · [简体中文](README.zh-CN.md) · [npm](https://www.npmjs.com/package/afucontext) · [Install](#install) · [Results](#results) · [Dashboard](#dashboard)
4
+
5
+ In a long session, the same tool output can travel with every model request. AFU uses the host’s selected model to summarize completed history in the background. Once ready, the summary can replace that history on a later request while preserving newer tool calls and messages.
6
+
7
+ ## How AFU compacts a session
8
+
9
+ | When | What AFU does |
10
+ | --- | --- |
11
+ | A session reaches its threshold | Prepares a summary in the background while the session continues. |
12
+ | The host next needs the history | Checks that the covered prefix still matches before replacing it. |
13
+ | After replacement | Records a successful upstream response or a host commit so the dashboard can distinguish generation from application. |
14
+
15
+ <a id="install"></a>
16
+
17
+ ## Quick start
18
+
19
+ Install and sign in to your host first. You need npm, Node.js **22.19+ (22.x) or 24+** and Python **3.11+** on `PATH`. Pick one row, run the install command in your terminal, then use the launch command.
20
+
21
+ | Host | System | Install | Launch after installation | Tested version |
22
+ | --- | --- | --- | --- | --- |
23
+ | <a id="afu-claude"></a>Claude Code | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install claude` | `claude` | 2.1.293 |
24
+ | <a id="afu-codex"></a>Codex CLI | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex` | `codex` | 0.153.4 |
25
+ | <a id="afu-pi"></a>Pi | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install pi` | `pi` | 0.84.4, 1.5.0 |
26
+ | <a id="afu-dsh"></a>DSH | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` | 0.2.2-rc.1 |
27
+ | Codex CLI | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install codex` | `codex` | preview, not yet tested; official in the next release |
28
+ | Pi | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install pi` | `pi` | preview, not yet tested; official in the next release |
29
+ | DSH | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` | preview, not yet tested; official in the next release |
30
+
31
+ Supported on macOS (Apple Silicon) and Linux x64. Start a new session after installing.
32
+
33
+ ¹ Windows: Codex CLI, Pi and DSH are implemented but not yet tested on Windows hardware; this release is a preview, and Windows support becomes official in the next release. Run the commands in PowerShell; `npx.cmd` and the quotes around the package name are required there. Claude Code on Windows is not supported yet.
34
+
35
+ See [Installation notes](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/install-notes.md) for verification scope, versions and mirrors, startup after reboot, Pi / DSH options and known issues.
36
+
37
+ <a id="results"></a>
38
+
39
+ ## Commands
40
+
41
+ The terminal commands (`afuctx …`) are placed in `~/.local/bin` by the first install; if that folder is not on `PATH`, use `npx --yes --registry=https://registry.npmjs.org afucontext@latest` in place of `afuctx`. Install, upgrade and remove replace the runtime itself, so they always run through npx.
42
+
43
+ | Command | What it does |
44
+ | --- | --- |
45
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest install claude\|codex\|pi\|dsh` | Install for a host; for DSH add `--profile NAME` |
46
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest install dsh --runtime-only` | Prepare only the runtime, for use with `dsh plugin --profile NAME add afucontext-dsh-plugin` |
47
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest update` | Upgrade every host installed on this machine to this version |
48
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest remove HOST` | Remove; host settings return to their state before installation. For DSH, `--profile NAME` removes one profile |
49
+ | `afuctx status` | Whether each host is connected, how far the context is from the trigger, compactions applied; `--json` for the full record |
50
+ | `afuctx dashboard` | Open the local dashboard (same as `afuctx-admin`); `--prices FILE` estimates costs with your prices |
51
+ | `afuctx repair codex --network` | Find and verify the proxy route to the model again (`repair claude` for Claude Code) |
52
+ | `afuctx gc --dry-run` | List old runtimes and logs that can be removed; without `--dry-run` it removes them |
53
+ | `afuctx --version` / `afuctx --help` | Version / help |
54
+
55
+ Inside a host session:
56
+
57
+ | Host | Status | Dashboard |
58
+ | --- | --- | --- |
59
+ | Claude Code | `/afuctx:status` | `/afuctx:dashboard` |
60
+ | Codex | `$afuctx-status` | `$afuctx-dashboard` |
61
+ | Pi / DSH | `/afuctx-status` | `/afuctx-dashboard` |
62
+
63
+ ## Benchmark results
64
+
65
+ In a LongBench V2 replay of 39 history questions (20 agent histories and 19 dialogue histories), AFU’s asynchronous strategy answered **23 correctly** with an average context of **15.9k tokens**.
66
+
67
+ | Replay strategy | First-answer accuracy | Mean context tokens |
68
+ | --- | ---: | ---: |
69
+ | No compaction | 19/39 (48.72%) | 72.6k |
70
+ | Codex Compact Sync | 20/39 (51.28%) | 21.7k |
71
+ | DSH Compact Sync | 18/39 (46.15%) | 26.1k |
72
+ | Pi Compact Sync | 13/39 (33.33%) | 21.6k |
73
+ | **AFU Compact Async** | **23/39 (58.97%)** | **15.9k** |
74
+
75
+ **4.56×** is the ratio of mean context tokens without compaction to those with AFU. The replay used DeepSeek-V3.2 for both answers and summaries, a 131,072-token logical window, and a 20k AFU trigger with a one-turn delay. Synchronous strategies replayed their respective prompts and policies, with at least one compaction for the context-size comparison. This metric excludes answer output and does not measure total API cost or production savings for the npm release.
76
+
77
+ <a id="dashboard"></a>
78
+
79
+ ## Web dashboard
80
+
81
+ ### Open the dashboard
82
+
83
+ | Where | Type |
84
+ | --- | --- |
85
+ | In a Claude Code session | `/afuctx:dashboard` |
86
+ | In a Codex session | `$afuctx-dashboard` |
87
+ | In a Pi or DSH session | `/afuctx-dashboard` |
88
+ | In a terminal | `afuctx dashboard` |
89
+
90
+ - The dashboard runs in the background and does not occupy a terminal; opening it again reuses the same one, and it exits after 30 minutes without an open page.
91
+ - It opens in the dark theme; switch to light, follow-system or English/Chinese in the top-right corner.
92
+ - The terminal commands are placed in `~/.local/bin` by the installer (only when that folder is already on `PATH`; shell startup files are never edited and existing commands of the same name are kept) and are removed with the last integration.
93
+
94
+ ### Check the current session
95
+
96
+ Type `/afuctx:status` in Claude Code, `$afuctx-status` in Codex, `/afuctx-status` in Pi or DSH, or run `afuctx status` in a terminal. It shows whether AFU is connected, the current context against the compaction threshold, and how many compactions have been applied.
97
+
98
+ Without the terminal commands (for example when `~/.local/bin` is not on `PATH`), use the full npx commands. For Codex; for another host replace `codex` with `claude`, `pi` or `dsh`:
99
+
100
+ ```sh
101
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 status codex --brief
102
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 ui
103
+ ```
104
+
105
+ ### What the dashboard shows
106
+
107
+ - **Overview**: tokens saved and compactions applied, plus conversation input, cache hit rate, compaction cost and active sessions; below, one row per conversation with its title, compaction state and a bar showing how far the context is from the compaction trigger.
108
+ - **Session detail**: the context drop after each compaction, how long each one took and what it saved, and cache hit rates. A conversation the host split into segments (sub-agents, title requests, its own compaction) lists them here.
109
+ - **Status**: integrations, versions and issues.
110
+
111
+ Each compaction is recorded in three steps: **request observed → summary ready → summary applied**. For Claude Code and Codex, "applied" means a request carrying the summary received a complete successful response; for Pi it means a model request was sent with the summary in place of the history it covers; for DSH it means the host committed the summary to session history. Summaries run in the background and never delay the conversation. Unreported cache usage is shown as unknown; savings are calibrated against the token counts the model service reports for requests sent without a summary, and like costs they are estimates, not provider bills. To estimate cost, supply per-session input, output and cache prices; see [Dashboard and costs](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/local-dashboard.md).
112
+
113
+ ![AFU local dashboard overview (dark)](https://code.alipay.com/med_ai/afu_context_liwu/raw/master/docs/assets/dashboard-overview.en.jpg)
114
+
115
+ *Demo data.*
116
+
117
+ ## Models and thresholds
118
+
119
+ All four integrations reuse the conversation’s model and authentication. When the context passes the trigger, AFU summarizes earlier messages in the background with the current model, and later requests use the compacted history. Trigger = model window × trigger ratio. Defaults:
120
+
121
+ | Model window | Default ratio | Example trigger |
122
+ | --- | --- | --- |
123
+ | 1,000,000 tokens or more | 50% | 1,000,000 window → 500,000 |
124
+ | Under 1,000,000 tokens | 60% | 272,000 window → 163,200; 200,000 window → 120,000 |
125
+
126
+ The model window is detected automatically:
127
+
128
+ | Host | Where the window comes from |
129
+ | --- | --- |
130
+ | Claude Code | The window Claude Code passes to its status line command. AFU wraps that command to read it; your own status line keeps running unchanged |
131
+ | Codex | Codex's local model catalog cache |
132
+ | Pi / DSH | The model and window the host has selected |
133
+
134
+ If a window is still unknown, chat continues and `/afuctx:status` or `$afuctx-status` says so.
135
+
136
+ ### Set the window or ratio manually
137
+
138
+ Run the same install command with options, then start a new session. For Codex; for another host replace `codex` with `claude`, `pi` or `dsh`.
139
+
140
+ | To | Option |
141
+ | --- | --- |
142
+ | Compact earlier or later | `--ratio 0.3` (between 0 and 1) |
143
+ | Set the model window | `--context-window 200000` |
144
+ | Restore automatic settings | `--context-window auto --ratio auto` |
145
+
146
+ ```sh
147
+ # Trigger at 30% of a manually set 200,000-token window (60,000 tokens)
148
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex --context-window 200000 --ratio 0.3
149
+ # Restore automatic settings
150
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex --context-window auto --ratio auto
151
+ ```
152
+
153
+ For frequent use, run `npm install -g --registry=https://registry.npmjs.org afucontext@0.1.18` and use `afuctx` in place of the full `npx` invocation. For example, `afuctx dashboard --prices ./afu-prices.json` opens the dashboard with your price file to estimate costs.
154
+
155
+ ## Upgrade and remove
156
+
157
+ After upgrading a Claude Code or Codex integration, start a new session to use the new gateway. To uninstall, remove the host integration first, then restart the host. Use the appropriate command below.
158
+
159
+ To move every installed integration (Claude Code, Codex, Pi and each DSH profile) to the latest release in one step, run `update` with `@latest`. It upgrades them one by one with the settings they were installed with and prints the version change for each:
160
+
161
+ ```sh
162
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest update
163
+ ```
164
+
165
+ To check which version you have, run `--version` (the launcher), or `status --brief`, which shows the installed version of each host:
166
+
167
+ ```sh
168
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest --version
169
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest status --brief
170
+ ```
171
+
172
+ To upgrade or remove one host:
173
+
174
+ ```sh
175
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 upgrade codex
176
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 remove codex
177
+ ```
178
+
179
+ Use `remove pi` or `remove dsh` for those integrations. In Windows PowerShell, use `npx.cmd` and double-quote the package name.
180
+
181
+ The dashboard listens on loopback. Logs record usage and receipts, and Pi/DSH also persist summaries in local session state. Generating summaries uses model tokens. Savings depend on subsequent reuse and actual cache hits.
182
+
183
+ [Apache-2.0](LICENSE)
@@ -0,0 +1,183 @@
1
+ # AFU Context · 本地异步上下文压缩
2
+
3
+ [English](README.md) · **简体中文** · [npm](https://www.npmjs.com/package/afucontext) · [一键安装](#install) · [评测效果](#results) · [Web 面板](#dashboard)
4
+
5
+ 长对话里,同一段工具输出可能随每次请求反复发送。AFU 用宿主当前的模型在后台整理已经结束的对话。摘要就绪后,下一次合适的请求可以用摘要替换旧历史,同时保留之后发生的工具调用和新消息。
6
+
7
+ ## AFU 如何压缩对话
8
+
9
+ | 时机 | AFU 的行为 |
10
+ | --- | --- |
11
+ | 达到压缩阈值 | 后台生成摘要,当前会话可以继续。 |
12
+ | 下一次使用历史 | 检查摘要覆盖的对话前缀是否仍然匹配,匹配才替换。 |
13
+ | 使用之后 | 记录模型请求成功回执或宿主提交结果,面板区分已生成与已应用。 |
14
+
15
+ <a id="install"></a>
16
+
17
+ ## 快速开始
18
+
19
+ 先安装并登录要使用的宿主,准备 npm、Node.js **22.19+(22.x)或 24+** 和 Python **3.11+**(需在 `PATH` 中)。选一行,在终端执行安装命令,然后用最后一列的命令启动。
20
+
21
+ | 宿主 | 系统 | 一键安装 | 安装后启动 | 实测版本 |
22
+ | --- | --- | --- | --- | --- |
23
+ | <a id="afu-claude"></a>Claude Code | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install claude` | `claude` | 2.1.293 |
24
+ | <a id="afu-codex"></a>Codex CLI | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex` | `codex` | 0.153.4 |
25
+ | <a id="afu-pi"></a>Pi | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install pi` | `pi` | 0.84.4、1.5.0 |
26
+ | <a id="afu-dsh"></a>DSH | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` | 0.2.2-rc.1 |
27
+ | Codex CLI | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install codex` | `codex` | 预览,未实测,下一版本正式提供 |
28
+ | Pi | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install pi` | `pi` | 预览,未实测,下一版本正式提供 |
29
+ | DSH | Windows 10/11¹ | `npx.cmd --yes --registry=https://registry.npmjs.org "afucontext@0.1.18" install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` | 预览,未实测,下一版本正式提供 |
30
+
31
+ 支持 macOS(Apple Silicon)和 x64 Linux;安装后新开会话即可。
32
+
33
+ ¹ Windows:Codex CLI、Pi、DSH 已实现,但尚未在 Windows 实机测试,本版本仅供预览,下一版本正式提供。请在 PowerShell 中执行,命令里的 `npx.cmd` 和包名两侧的双引号都不能省。Claude Code 暂不支持 Windows。
34
+
35
+ 验证范围、版本与镜像、重启后的启动方式、Pi / DSH 参数和已知问题见 [安装说明](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/install-notes.zh-CN.md)。
36
+
37
+ <a id="results"></a>
38
+
39
+ ## 命令一览
40
+
41
+ 终端命令(`afuctx …`)在首次安装后放到 `~/.local/bin`;该目录不在 `PATH` 时,把 `afuctx` 换成 `npx --yes --registry=https://registry.npmjs.org afucontext@latest`。安装、升级、移除这类会替换运行时本身的命令,始终用 npx 执行。
42
+
43
+ | 命令 | 作用 |
44
+ | --- | --- |
45
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest install claude\|codex\|pi\|dsh` | 安装到指定宿主;DSH 加 `--profile 名称` |
46
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest install dsh --runtime-only` | 只准备运行时,配合 `dsh plugin --profile 名称 add afucontext-dsh-plugin` 使用 |
47
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest update` | 把本机已装的所有宿主一次升级到这个版本 |
48
+ | `npx --yes --registry=https://registry.npmjs.org afucontext@latest remove 宿主` | 移除,宿主配置恢复成安装前的样子;DSH 可加 `--profile 名称` 只移除一个 profile |
49
+ | `afuctx status` | 各宿主是否接入、当前上下文离触发线多远、已压缩几次;加 `--json` 输出完整信息 |
50
+ | `afuctx dashboard` | 打开本地面板(同 `afuctx-admin`);`--prices 文件` 用自己的单价估算费用 |
51
+ | `afuctx repair codex --network` | 重新查找并验证连到模型服务的代理(Claude Code 用 `repair claude`) |
52
+ | `afuctx gc --dry-run` | 查看可清理的旧版本运行时和日志;去掉 `--dry-run` 才会真正清理 |
53
+ | `afuctx --version` / `afuctx --help` | 版本号 / 帮助 |
54
+
55
+ 宿主会话里也可以直接用:
56
+
57
+ | 宿主 | 查看状态 | 打开面板 |
58
+ | --- | --- | --- |
59
+ | Claude Code | `/afuctx:status` | `/afuctx:dashboard` |
60
+ | Codex | `$afuctx-status` | `$afuctx-dashboard` |
61
+ | Pi / DSH | `/afuctx-status` | `/afuctx-dashboard` |
62
+
63
+ ## 评测效果
64
+
65
+ 在 LongBench V2 的 39 道历史问答回放中(20 道 Agent 历史、19 道对话历史),AFU 异步压缩答对 **23 道**,平均上下文为 **15.9k tokens**。
66
+
67
+ | 回放策略 | 首次回答正确率 | 平均上下文 tokens |
68
+ | --- | ---: | ---: |
69
+ | 无压缩 | 19/39 (48.72%) | 72.6k |
70
+ | Codex Compact Sync | 20/39 (51.28%) | 21.7k |
71
+ | DSH Compact Sync | 18/39 (46.15%) | 26.1k |
72
+ | Pi Compact Sync | 13/39 (33.33%) | 21.6k |
73
+ | **AFU Compact Async** | **23/39 (58.97%)** | **15.9k** |
74
+
75
+ **4.56×** 是无压缩与 AFU 的平均上下文 token 之比。回放使用 DeepSeek-V3.2 回答和压缩,逻辑窗口为 131,072 tokens,AFU 在 20k 触发,摘要延迟一轮使用。同步策略按各自提示词和策略回放,比较上下文规模时至少压缩一次。此指标不含回答输出,也不能直接换算成 API 总费用或当前 npm 版本的线上收益。
76
+
77
+ <a id="dashboard"></a>
78
+
79
+ ## Web 面板:查看压缩是否生效
80
+
81
+ ### 打开面板
82
+
83
+ | 在哪里 | 输入 |
84
+ | --- | --- |
85
+ | Claude Code 会话里 | `/afuctx:dashboard` |
86
+ | Codex 会话里 | `$afuctx-dashboard` |
87
+ | Pi / DSH 会话里 | `/afuctx-dashboard` |
88
+ | 终端里 | `afuctx dashboard` |
89
+
90
+ - 面板在后台运行,不占用终端;再次打开会复用同一个面板,30 分钟没有页面打开时自动退出。
91
+ - 默认暗色主题,右上角可切换亮色、跟随系统和中英文。
92
+ - 终端命令由安装程序放在 `~/.local/bin`(已在 `PATH` 中时才放,不改 shell 配置,不覆盖你已有的同名命令),移除最后一个接入时一并删除。
93
+
94
+ ### 查看当前会话
95
+
96
+ Claude Code 输入 `/afuctx:status`,Codex 输入 `$afuctx-status`,Pi 和 DSH 输入 `/afuctx-status`,终端里运行 `afuctx status`。会显示 AFU 是否已接入、当前上下文距离压缩触发线还有多少,以及已经生效的压缩次数。
97
+
98
+ 没有上面的终端命令时(例如 `~/.local/bin` 不在 `PATH` 中),可以用完整的 npx 命令。以 Codex 为例,其它宿主将 `codex` 换成 `claude`、`pi` 或 `dsh`:
99
+
100
+ ```sh
101
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 status codex --brief
102
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 ui
103
+ ```
104
+
105
+ ### 面板内容
106
+
107
+ - **总览**:节省的 token 和生效的压缩次数,以及对话输入、缓存命中率、压缩消耗和活跃会话;下方每个对话一行,显示标题、压缩状态,以及距离触发压缩还差多少的进度条。
108
+ - **会话详情**:上下文在每次压缩后的回落、每次压缩的耗时和节省量,以及缓存命中率。宿主把一个对话分成多段时(子代理、起标题请求、宿主自己压缩后),这里会列出各段。
109
+ - **运行状态**:接入情况、版本和异常记录。
110
+
111
+ 每次压缩分三步记录:**请求已接入 → 摘要已生成 → 摘要已应用**。Claude Code 和 Codex 的“已应用”表示替换历史后的请求已收到完整成功响应;Pi 表示已有模型请求用摘要替换了它覆盖的历史;DSH 表示宿主已把摘要写入会话历史。摘要在后台生成,不影响对话。模型未返回的缓存用量显示为未知;节省量按模型服务对未压缩请求报告的实际 token 数校准,与费用一样都是估算,不是供应商账单。需要估算费用时,可按会话提供输入、输出和缓存单价,见[面板与费用](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/local-dashboard.zh-CN.md)。
112
+
113
+ ![AFU 本地面板的总览页(暗色)](https://code.alipay.com/med_ai/afu_context_liwu/raw/master/docs/assets/dashboard-overview.jpg)
114
+
115
+ *截图为演示数据。*
116
+
117
+ ## 模型与压缩阈值
118
+
119
+ 四个平台都复用主对话的模型和认证。上下文超过触发线时,AFU 在后台用当前模型生成摘要,之后的请求改用压缩后的历史。触发线 = 模型窗口 × 触发比例,默认如下:
120
+
121
+ | 模型窗口 | 默认触发比例 | 触发线示例 |
122
+ | --- | --- | --- |
123
+ | 1,000,000 tokens 及以上 | 50% | 1,000,000 窗口 → 500,000 |
124
+ | 小于 1,000,000 tokens | 60% | 272,000 窗口 → 163,200;200,000 窗口 → 120,000 |
125
+
126
+ 模型窗口自动获取,不需要手动配置:
127
+
128
+ | 宿主 | 窗口从哪来 |
129
+ | --- | --- |
130
+ | Claude Code | Claude Code 传给状态栏命令的模型窗口。AFU 包装这条命令来读取,你原来的状态栏照常运行、显示不变 |
131
+ | Codex | Codex 本地的模型目录缓存 |
132
+ | Pi / DSH | 宿主当前选择的模型和窗口 |
133
+
134
+ 窗口仍未知时聊天照常进行,`/afuctx:status` 或 `$afuctx-status` 会提示。
135
+
136
+ ### 手动指定窗口和调整比例
137
+
138
+ 用同一条安装命令加参数即可,改完后新开会话生效。下面以 Codex 为例,其它宿主将 `codex` 换成 `claude`、`pi` 或 `dsh`。
139
+
140
+ | 想做的事 | 参数 |
141
+ | --- | --- |
142
+ | 提前或推迟压缩 | `--ratio 0.3`(取值 0 到 1 之间) |
143
+ | 手动指定模型窗口 | `--context-window 200000` |
144
+ | 恢复自动 | `--context-window auto --ratio auto` |
145
+
146
+ ```sh
147
+ # 在 30% 触发,并手动指定 200,000 的窗口(即 60,000 触发)
148
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex --context-window 200000 --ratio 0.3
149
+ # 恢复自动设置
150
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 install codex --context-window auto --ratio auto
151
+ ```
152
+
153
+ 如果经常使用 AFU,可运行 `npm install -g --registry=https://registry.npmjs.org afucontext@0.1.18`,以后用 `afuctx` 代替完整的 `npx` 命令。例如,`afuctx dashboard --prices ./afu-prices.json` 用自己的单价文件打开面板估算费用。
154
+
155
+ ## 升级与移除
156
+
157
+ 升级 Claude Code 或 Codex 接入后,新开会话即可使用新版网关。需要移除时,先撤销对应宿主的接入,再重启宿主。以下两条命令分别用于升级和移除。
158
+
159
+ 想把已装的所有接入(Claude Code、Codex、Pi 和每个 DSH profile)一次升级到最新版,用 `@latest` 执行 `update`。它会按安装时的设置逐个升级,并打印每个接入从哪个版本升到哪个版本:
160
+
161
+ ```sh
162
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest update
163
+ ```
164
+
165
+ 查看版本:`--version` 显示启动器版本,`status --brief` 显示每个宿主当前装的版本:
166
+
167
+ ```sh
168
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest --version
169
+ npx --yes --registry=https://registry.npmjs.org afucontext@latest status --brief
170
+ ```
171
+
172
+ 只升级或移除某一个宿主:
173
+
174
+ ```sh
175
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 upgrade codex
176
+ npx --yes --registry=https://registry.npmjs.org afucontext@0.1.18 remove codex
177
+ ```
178
+
179
+ Pi 和 DSH 可分别用 `remove pi` 或 `remove dsh` 撤销。Windows PowerShell 请使用 `npx.cmd` 并将包名放在双引号中。
180
+
181
+ 本地面板只监听回环地址。日志记录用量和回执,Pi/DSH 的本地会话状态还会保存摘要。生成摘要本身会调用模型,节省多少取决于后续复用和实际缓存命中。
182
+
183
+ [Apache-2.0](LICENSE)