@afu_context/afu-context 0.0.0-stage → 0.1.7
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.
- package/LICENSE +201 -0
- package/README.md +116 -2
- package/README.zh-CN.md +117 -0
- package/bin/afu.mjs +77 -0
- package/bin/cache-lease.mjs +16 -0
- package/bin/host-autoload.py +328 -0
- package/bin/install-guide.mjs +34 -0
- package/bin/install-process.mjs +19 -0
- package/bin/platform.mjs +74 -0
- package/bin/prebuilt.mjs +64 -0
- package/bin/runtime.mjs +111 -0
- package/package.json +24 -4
- package/platform-dependencies.json +15 -0
- package/platform-locks/dsh/package-lock.json +410 -0
- package/platform-locks/dsh/package.json +16 -0
- package/platform-locks/pi/package-lock.json +1789 -0
- package/platform-locks/pi/package.json +9 -0
- package/python/afu_context_local-0.1.7-py3-none-any.whl +0 -0
- package/python/constraints.txt +23 -0
- package/release.json +6 -0
- package/scripts/dsh-overlay.mjs +63 -0
- package/scripts/dsh-presets.mjs +49 -0
- package/scripts/dsh-web-probe.mjs +14 -0
- package/src/afu_host_integrations/dsh/dist/index.js +422 -0
- package/src/afu_host_integrations/pi/dist/index.js +272 -0
- package/src/afu_host_integrations/shared/runtime-client/dist/host-worker.js +168 -0
- package/src/afu_host_integrations/shared/runtime-client/dist/index.js +242 -0
- package/src/afu_host_integrations/shared/runtime-client/dist/prefix.js +12 -0
- package/src/afu_host_integrations/shared/runtime-client/dist/protocol.js +79 -0
- package/src/afu_host_integrations/shared/runtime-client/dist/telemetry.js +58 -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,117 @@
|
|
|
1
|
-
#
|
|
1
|
+
# AFU Context · Local asynchronous context compression
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**English** · [简体中文](README.zh-CN.md) · [npm](https://www.npmjs.com/package/@afu_context/afu-context) · [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 |
|
|
22
|
+
| --- | --- | --- | --- |
|
|
23
|
+
| <a id="afu-claude"></a>Claude Code | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install claude` | `claude` |
|
|
24
|
+
| Claude Code | Windows 10/11 | `npx.cmd --yes --registry=https://registry.npmjs.org "@afu_context/afu-context@0.1.7" install claude` | `claude` |
|
|
25
|
+
| <a id="afu-codex"></a>Codex CLI | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex` | `codex` |
|
|
26
|
+
| <a id="afu-pi"></a>Pi | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install pi` | `pi` |
|
|
27
|
+
| <a id="afu-dsh"></a>DSH | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` |
|
|
28
|
+
|
|
29
|
+
Supported on macOS (Apple Silicon) and Linux x64; Windows supports Claude Code only and awaits hardware testing. Start a new session after installing. 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.
|
|
30
|
+
|
|
31
|
+
<a id="results"></a>
|
|
32
|
+
|
|
33
|
+
## Benchmark results
|
|
34
|
+
|
|
35
|
+
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**.
|
|
36
|
+
|
|
37
|
+
| Replay strategy | First-answer accuracy | Mean context tokens |
|
|
38
|
+
| --- | ---: | ---: |
|
|
39
|
+
| No compaction | 19/39 (48.72%) | 72.6k |
|
|
40
|
+
| Codex Compact Sync | 20/39 (51.28%) | 21.7k |
|
|
41
|
+
| DSH Compact Sync | 18/39 (46.15%) | 26.1k |
|
|
42
|
+
| Pi Compact Sync | 13/39 (33.33%) | 21.6k |
|
|
43
|
+
| **AFU Compact Async** | **23/39 (58.97%)** | **15.9k** |
|
|
44
|
+
|
|
45
|
+
**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.
|
|
46
|
+
|
|
47
|
+
<a id="dashboard"></a>
|
|
48
|
+
|
|
49
|
+
## Web dashboard
|
|
50
|
+
|
|
51
|
+
### Open the dashboard
|
|
52
|
+
|
|
53
|
+
| Where | Type |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| In a Claude Code session | `/afu:ctx-admin` |
|
|
56
|
+
| In a Codex session | `$afu-ctx-admin` |
|
|
57
|
+
| In a terminal | `afu-ctx-admin` |
|
|
58
|
+
|
|
59
|
+
- 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.
|
|
60
|
+
- It opens in the dark theme; switch to light, follow-system or English/Chinese in the top-right corner.
|
|
61
|
+
- 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.
|
|
62
|
+
|
|
63
|
+
### Check the current session
|
|
64
|
+
|
|
65
|
+
Type `/afu:status` in Claude Code, `$afu-status` in Codex, or run `afu status` in a terminal. It shows whether AFU is connected, the current context against the compaction threshold, and how many compactions have been applied.
|
|
66
|
+
|
|
67
|
+
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`:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 status codex --brief
|
|
71
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 ui
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### What the dashboard shows
|
|
75
|
+
|
|
76
|
+
- **Overview**: tokens saved and compactions applied, plus conversation input, cache hit rate, compaction cost and active sessions; below, each session with its current context and compaction state.
|
|
77
|
+
- **Session detail**: the context drop after each compaction, how long each one took and what it saved, and cache hit rates.
|
|
78
|
+
- **Status**: integrations, versions and issues.
|
|
79
|
+
|
|
80
|
+
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 and 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 and costs 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).
|
|
81
|
+
|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
*Demo data.*
|
|
85
|
+
|
|
86
|
+
## Models and thresholds
|
|
87
|
+
|
|
88
|
+
All four integrations reuse the conversation’s model and authentication. The default trigger is **40%** for windows of **1,000,000 tokens or more** and **50%** for smaller windows. Pi and DSH follow the host’s selected model and window. Claude Code reports the model window to its status line command; AFU wraps that command (your own status line keeps running unchanged) to learn the window. Codex windows come from its model catalog cache. If a window is still unknown, chat continues and status says so; `--context-window` sets it manually.
|
|
89
|
+
|
|
90
|
+
<details>
|
|
91
|
+
<summary>Set a window or ratio, or restore automatic settings</summary>
|
|
92
|
+
|
|
93
|
+
For a Codex model with a known 200,000-token window, the first command sets a 30% trigger. The second restores automatic settings.
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex --context-window 200000 --ratio 0.3
|
|
97
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex --context-window auto --ratio auto
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
</details>
|
|
101
|
+
|
|
102
|
+
For frequent use, run `npm install -g --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7` and use `afu` in place of the full `npx` invocation. For example, `afu ui --prices ./afu-prices.json` opens the dashboard with your price file to estimate costs.
|
|
103
|
+
|
|
104
|
+
## Upgrade and remove
|
|
105
|
+
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 upgrade codex
|
|
110
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 remove codex
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Use `remove pi` or `remove dsh` for those integrations. In Windows PowerShell, use `npx.cmd` and double-quote the package name.
|
|
114
|
+
|
|
115
|
+
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.
|
|
116
|
+
|
|
117
|
+
[Apache-2.0](LICENSE)
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# AFU Context · 本地异步上下文压缩
|
|
2
|
+
|
|
3
|
+
[English](README.md) · **简体中文** · [npm](https://www.npmjs.com/package/@afu_context/afu-context) · [一键安装](#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 @afu_context/afu-context@0.1.7 install claude` | `claude` |
|
|
24
|
+
| Claude Code | Windows 10/11 | `npx.cmd --yes --registry=https://registry.npmjs.org "@afu_context/afu-context@0.1.7" install claude` | `claude` |
|
|
25
|
+
| <a id="afu-codex"></a>Codex CLI | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex` | `codex` |
|
|
26
|
+
| <a id="afu-pi"></a>Pi | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install pi` | `pi` |
|
|
27
|
+
| <a id="afu-dsh"></a>DSH | macOS / Linux | `npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install dsh --profile YOUR_PROFILE` | `dsh --profile YOUR_PROFILE` |
|
|
28
|
+
|
|
29
|
+
支持 macOS(Apple Silicon)和 x64 Linux;Windows 仅 Claude Code,待实机验收。安装后新开会话即可。验证范围、版本与镜像、重启后的启动方式、Pi / DSH 参数和已知问题见 [安装说明](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/install-notes.zh-CN.md)。
|
|
30
|
+
|
|
31
|
+
<a id="results"></a>
|
|
32
|
+
|
|
33
|
+
## 评测效果
|
|
34
|
+
|
|
35
|
+
在 LongBench V2 的 39 道历史问答回放中(20 道 Agent 历史、19 道对话历史),AFU 异步压缩答对 **23 道**,平均上下文为 **15.9k tokens**。
|
|
36
|
+
|
|
37
|
+
| 回放策略 | 首次回答正确率 | 平均上下文 tokens |
|
|
38
|
+
| --- | ---: | ---: |
|
|
39
|
+
| 无压缩 | 19/39 (48.72%) | 72.6k |
|
|
40
|
+
| Codex Compact Sync | 20/39 (51.28%) | 21.7k |
|
|
41
|
+
| DSH Compact Sync | 18/39 (46.15%) | 26.1k |
|
|
42
|
+
| Pi Compact Sync | 13/39 (33.33%) | 21.6k |
|
|
43
|
+
| **AFU Compact Async** | **23/39 (58.97%)** | **15.9k** |
|
|
44
|
+
|
|
45
|
+
**4.56×** 是无压缩与 AFU 的平均上下文 token 之比。回放使用 DeepSeek-V3.2 回答和压缩,逻辑窗口为 131,072 tokens,AFU 在 20k 触发,摘要延迟一轮使用。同步策略按各自提示词和策略回放,比较上下文规模时至少压缩一次。此指标不含回答输出,也不能直接换算成 API 总费用或当前 npm 版本的线上收益。
|
|
46
|
+
|
|
47
|
+
<a id="dashboard"></a>
|
|
48
|
+
|
|
49
|
+
## Web 面板:查看压缩是否生效
|
|
50
|
+
|
|
51
|
+
### 打开面板
|
|
52
|
+
|
|
53
|
+
| 在哪里 | 输入 |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| Claude Code 会话里 | `/afu:ctx-admin` |
|
|
56
|
+
| Codex 会话里 | `$afu-ctx-admin` |
|
|
57
|
+
| 终端里 | `afu-ctx-admin` |
|
|
58
|
+
|
|
59
|
+
- 面板在后台运行,不占用终端;再次打开会复用同一个面板,30 分钟没有页面打开时自动退出。
|
|
60
|
+
- 默认暗色主题,右上角可切换亮色、跟随系统和中英文。
|
|
61
|
+
- 终端命令由安装程序放在 `~/.local/bin`(已在 `PATH` 中时才放,不改 shell 配置,不覆盖你已有的同名命令),移除最后一个接入时一并删除。
|
|
62
|
+
|
|
63
|
+
### 查看当前会话
|
|
64
|
+
|
|
65
|
+
Claude Code 输入 `/afu:status`,Codex 输入 `$afu-status`,终端里运行 `afu status`。会显示 AFU 是否已接入、当前上下文距离压缩触发线还有多少,以及已经生效的压缩次数。
|
|
66
|
+
|
|
67
|
+
没有上面的终端命令时(例如 `~/.local/bin` 不在 `PATH` 中),可以用完整的 npx 命令。以 Codex 为例,其它宿主将 `codex` 换成 `claude`、`pi` 或 `dsh`:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 status codex --brief
|
|
71
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 ui
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 面板内容
|
|
75
|
+
|
|
76
|
+
- **总览**:节省的 token 和生效的压缩次数,以及对话输入、缓存命中率、压缩消耗和活跃会话;下方列出每个会话及其当前上下文和压缩状态。
|
|
77
|
+
- **会话详情**:上下文在每次压缩后的回落、每次压缩的耗时和节省量,以及缓存命中率。
|
|
78
|
+
- **运行状态**:接入情况、版本和异常记录。
|
|
79
|
+
|
|
80
|
+
每次压缩分三步记录:**请求已接入 → 摘要已生成 → 摘要已应用**。Claude Code 和 Codex 的“已应用”表示替换历史后的请求已收到完整成功响应;Pi 和 DSH 表示宿主已把摘要写入会话历史。摘要在后台生成,不影响对话。模型未返回的缓存用量显示为未知;节省量和费用是估算,不是供应商账单。需要估算费用时,可按会话提供输入、输出和缓存单价,见[面板与费用](https://code.alipay.com/med_ai/afu_context_liwu/blob/master/docs/local-dashboard.zh-CN.md)。
|
|
81
|
+
|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
*截图为演示数据。*
|
|
85
|
+
|
|
86
|
+
## 模型与压缩阈值
|
|
87
|
+
|
|
88
|
+
四个平台都复用主对话的模型和认证。模型窗口为 **1,000,000 tokens 或更大** 时,默认在 **40%** 触发,更小的窗口按 **50%** 触发。Pi 和 DSH 跟随宿主选择的模型和窗口。Claude Code 会把模型窗口传给它的状态栏命令,AFU 包装这条命令来获取窗口(你原来的状态栏照常运行、显示不变);Codex 的窗口来自它的模型目录缓存。仍未知时聊天照常进行,状态里会提示,也可以用 `--context-window` 手动指定。
|
|
89
|
+
|
|
90
|
+
<details>
|
|
91
|
+
<summary>手动指定窗口、调整比例或恢复自动设置</summary>
|
|
92
|
+
|
|
93
|
+
下面以已知窗口为 200,000 tokens 的 Codex 模型为例。第一条将触发比例设为 30%,第二条恢复自动设置。
|
|
94
|
+
|
|
95
|
+
```sh
|
|
96
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex --context-window 200000 --ratio 0.3
|
|
97
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 install codex --context-window auto --ratio auto
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
</details>
|
|
101
|
+
|
|
102
|
+
如果经常使用 AFU,可运行 `npm install -g --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7`,以后用 `afu` 代替完整的 `npx` 命令。例如,`afu ui --prices ./afu-prices.json` 用自己的单价文件打开面板估算费用。
|
|
103
|
+
|
|
104
|
+
## 升级与移除
|
|
105
|
+
|
|
106
|
+
升级 Claude Code 或 Codex 接入后,新开会话即可使用新版网关。需要移除时,先撤销对应宿主的接入,再重启宿主。以下两条命令分别用于升级和移除。
|
|
107
|
+
|
|
108
|
+
```sh
|
|
109
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 upgrade codex
|
|
110
|
+
npx --yes --registry=https://registry.npmjs.org @afu_context/afu-context@0.1.7 remove codex
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Pi 和 DSH 可分别用 `remove pi` 或 `remove dsh` 撤销。Windows PowerShell 请使用 `npx.cmd` 并将包名放在双引号中。
|
|
114
|
+
|
|
115
|
+
本地面板只监听回环地址。日志记录用量和回执,Pi/DSH 的本地会话状态还会保存摘要。生成摘要本身会调用模型,节省多少取决于后续复用和实际缓存命中。
|
|
116
|
+
|
|
117
|
+
[Apache-2.0](LICENSE)
|
package/bin/afu.mjs
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** No postinstall side effects; recover only our versioned runtime cache. */
|
|
3
|
+
import {spawn} from 'node:child_process';
|
|
4
|
+
import {readFileSync} from 'node:fs';
|
|
5
|
+
import {dirname,join,resolve} from 'node:path';
|
|
6
|
+
import {homedir,constants} from 'node:os';
|
|
7
|
+
import {fileURLToPath} from 'node:url';
|
|
8
|
+
import {guide} from './install-guide.mjs';
|
|
9
|
+
import {prepareRuntime} from './runtime.mjs';
|
|
10
|
+
import {holdCache} from './cache-lease.mjs';
|
|
11
|
+
import {preparePlatform} from './platform.mjs';
|
|
12
|
+
const root=resolve(dirname(fileURLToPath(import.meta.url)),'..');
|
|
13
|
+
const manifest=JSON.parse(readFileSync(join(root,'release.json'),'utf8'));
|
|
14
|
+
const env={...process.env,AFU_INSTALL_ROOT:root};
|
|
15
|
+
let args=process.argv.slice(2);
|
|
16
|
+
if(['install','upgrade','status','remove','gc','native'].includes(args[0])){
|
|
17
|
+
const at=args.indexOf('--state');if(at>=0&&args[at+1])env.AFU_STATE_DIR=resolve(args[at+1]);
|
|
18
|
+
const inline=args.find(a=>a.startsWith('--state='));if(inline&&inline.length>8)env.AFU_STATE_DIR=resolve(inline.slice(8));
|
|
19
|
+
}
|
|
20
|
+
let release;
|
|
21
|
+
// Ctrl-C already reaches the child through the shared foreground process group;
|
|
22
|
+
// relaying SIGINT again would deliver it twice. Only forward signals sent to
|
|
23
|
+
// this launcher alone, and only to our own child.
|
|
24
|
+
function relaySignals(child){
|
|
25
|
+
process.on('SIGINT',()=>{});
|
|
26
|
+
for(const signal of ['SIGTERM','SIGHUP'])process.on(signal,()=>child.kill(signal));
|
|
27
|
+
}
|
|
28
|
+
function exitCode(code,signal){
|
|
29
|
+
if(code!==null&&code!==undefined)return code;
|
|
30
|
+
return signal&&constants.signals[signal]?128+constants.signals[signal]:1;
|
|
31
|
+
}
|
|
32
|
+
try{
|
|
33
|
+
// Only the bare launcher prints its own banner; "afu ui --help" and
|
|
34
|
+
// "afu claude --help" belong to the subcommand or the host.
|
|
35
|
+
if(!args.length || (['--help','-h'].includes(args[0]))){
|
|
36
|
+
console.log('AFU Context: install [claude|codex|pi|dsh], status, remove HOST, gc --dry-run, ui.\nRun npx @afu_context/afu-context@latest install to detect your installed host. Host login remains managed by the host.');
|
|
37
|
+
process.exit(0);
|
|
38
|
+
}
|
|
39
|
+
args=await guide(args,env);
|
|
40
|
+
if(['install','upgrade'].includes(args[0])&&args.includes('--dry-run')){
|
|
41
|
+
console.log(JSON.stringify({preflight:'passed',host:args[1],configuration_changed:false,request_observed:false}));process.exit(0);
|
|
42
|
+
}
|
|
43
|
+
let removed=false;
|
|
44
|
+
if(['pi','dsh'].includes(args[0])){
|
|
45
|
+
const receipt=join(env.AFU_STATE_DIR??join(homedir(),'.local/share/afu-context'),'integrations',args[0]+'.json');
|
|
46
|
+
try{removed=JSON.parse(readFileSync(receipt,'utf8')).removed===true;}catch(e){if(e.code!=='ENOENT')throw Error('INVALID_INSTALLATION_RECORD');}
|
|
47
|
+
}
|
|
48
|
+
if(removed){
|
|
49
|
+
const split=args.indexOf('--');
|
|
50
|
+
if(split<0 && args.length>1)throw Error('PLUGIN_REMOVED: pass host arguments after --');
|
|
51
|
+
// COSEC: fixed host executable, argv array, no shell; no runtime downloads.
|
|
52
|
+
const child=spawn(args[0],split<0?[]:args.slice(split+1),{stdio:'inherit',env,shell:false});
|
|
53
|
+
relaySignals(child);
|
|
54
|
+
child.on('error',()=>{console.error('AFU: HOST_EXECUTABLE_UNAVAILABLE');process.exitCode=1;});
|
|
55
|
+
child.on('exit',(code,signal)=>{process.exitCode=exitCode(code,signal);});
|
|
56
|
+
}else{
|
|
57
|
+
const hostAction = ['pi','dsh'].includes(args[1]) && ['install','upgrade','remove','status'].includes(args[0]);
|
|
58
|
+
// Status always goes through one reporter (it understands autoload receipts,
|
|
59
|
+
// --json and --state); only Pi/DSH install/upgrade/remove need host-autoload.
|
|
60
|
+
const directHost = process.platform!=='win32' && hostAction && args[0]!=='status';
|
|
61
|
+
if (directHost && ['install','upgrade'].includes(args[0])) {
|
|
62
|
+
// Autoload paths must outlive npx eviction and the ordinary launcher cache GC.
|
|
63
|
+
const state = env.AFU_STATE_DIR || join(homedir(), '.local/share/afu-context');
|
|
64
|
+
env.AFU_RUNTIME_DIR = join(state, 'autoload', 'runtimes');
|
|
65
|
+
}
|
|
66
|
+
if (args[0] === 'pi') env.AFU_EXPLICIT_EXTENSION = 'pi';
|
|
67
|
+
release=await holdCache(manifest.build,env);
|
|
68
|
+
const location=await prepareRuntime(root,manifest,env);
|
|
69
|
+
const python=join(location,process.platform==='win32'?'Scripts/python.exe':'bin/python');
|
|
70
|
+
env.AFU_INSTALL_ROOT=await preparePlatform(root,manifest,args,env);
|
|
71
|
+
const entry = directHost ? [join(root,'bin/host-autoload.py')] : ['-m','afu_context.local_cli'];
|
|
72
|
+
const child=spawn(python,[...entry,...args],{stdio:'inherit',env});
|
|
73
|
+
relaySignals(child);
|
|
74
|
+
child.on('error',()=>{release?.();console.error('AFU failed to start');process.exitCode=1;});
|
|
75
|
+
child.on('exit',(code,signal)=>{release?.();if(code===0&&['install','upgrade'].includes(args[0]))console.error(`AFU: installed ${args[1]}; ${['claude','codex'].includes(args[1])?'service checked; open a new '+args[1]+' session':directHost?'open a new host session':'launch with afu '+args[1]}. Waiting for the first observed request; compression is not yet verified.`);process.exitCode=exitCode(code,signal);});
|
|
76
|
+
}
|
|
77
|
+
}catch(error){release?.();console.error('AFU: '+error.message);process.exitCode=1;}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {mkdirSync,writeFileSync,rmSync} from 'node:fs';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
import {homedir} from 'node:os';
|
|
4
|
+
import {randomUUID,createHash} from 'node:crypto';
|
|
5
|
+
import {recoverLock} from './runtime.mjs';
|
|
6
|
+
export async function holdCache(build,env=process.env){
|
|
7
|
+
if(!/^[a-f0-9]{64}$/.test(build))throw Error('Invalid build identity');
|
|
8
|
+
const base=env.AFU_RUNTIME_DIR??join(homedir(),'.local/share/afu-context/runtimes');
|
|
9
|
+
mkdirSync(base,{recursive:true,mode:0o700});
|
|
10
|
+
const guard=join(base,'.gc-lock');const end=Date.now()+10000;
|
|
11
|
+
while(true){try{mkdirSync(guard);writeFileSync(join(guard,'owner.json'),JSON.stringify({pid:process.pid}));break;}catch(e){if(e.code!=='EEXIST')throw e;recoverLock(guard);if(Date.now()>end)throw Error('CACHE_GC_BUSY');await new Promise(r=>setTimeout(r,50));}}
|
|
12
|
+
const file=join(base,'.clients',randomUUID()+'.json');
|
|
13
|
+
const state=env.AFU_STATE_DIR??join(homedir(),'.local/share/afu-context');
|
|
14
|
+
try{mkdirSync(join(base,'.owners'),{recursive:true,mode:0o700});writeFileSync(join(base,'.owners',createHash('sha256').update(state).digest('hex')+'.json'),JSON.stringify({state}),{mode:0o600});mkdirSync(join(base,'.clients'),{recursive:true,mode:0o700});writeFileSync(file,JSON.stringify({pid:process.pid,build}),{mode:0o600});}finally{rmSync(guard,{recursive:true,force:true});}
|
|
15
|
+
const release=()=>rmSync(file,{force:true});process.once('exit',release);return release;
|
|
16
|
+
}
|