acpw 0.0.1__tar.gz
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.
- acpw-0.0.1/.github/workflows/ci.yml +21 -0
- acpw-0.0.1/.gitignore +12 -0
- acpw-0.0.1/LICENSE +21 -0
- acpw-0.0.1/PKG-INFO +142 -0
- acpw-0.0.1/README.en.md +130 -0
- acpw-0.0.1/README.md +129 -0
- acpw-0.0.1/pyproject.toml +41 -0
- acpw-0.0.1/src/acpw/__init__.py +3 -0
- acpw-0.0.1/src/acpw/__main__.py +3 -0
- acpw-0.0.1/src/acpw/check.py +67 -0
- acpw-0.0.1/src/acpw/cli.py +230 -0
- acpw-0.0.1/src/acpw/config.py +39 -0
- acpw-0.0.1/src/acpw/errors.py +5 -0
- acpw-0.0.1/src/acpw/log.py +9 -0
- acpw-0.0.1/src/acpw/models.py +84 -0
- acpw-0.0.1/src/acpw/proxy/__init__.py +5 -0
- acpw-0.0.1/src/acpw/proxy/agent.py +568 -0
- acpw-0.0.1/src/acpw/proxy/backend.py +145 -0
- acpw-0.0.1/src/acpw/store/__init__.py +5 -0
- acpw-0.0.1/src/acpw/store/db.py +228 -0
- acpw-0.0.1/tests/fixtures/compacting_agent.py +88 -0
- acpw-0.0.1/tests/fixtures/echo_agent.py +89 -0
- acpw-0.0.1/tests/fixtures/flaky_agent.py +54 -0
- acpw-0.0.1/tests/fixtures/hang_agent.py +56 -0
- acpw-0.0.1/tests/test_cli.py +80 -0
- acpw-0.0.1/tests/test_http.py +99 -0
- acpw-0.0.1/tests/test_proxy.py +381 -0
- acpw-0.0.1/tests/test_smoke.py +5 -0
- acpw-0.0.1/uv.lock +907 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v7
|
|
13
|
+
|
|
14
|
+
- uses: astral-sh/setup-uv@v10.4.0
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.12"
|
|
17
|
+
|
|
18
|
+
- run: uv sync --dev
|
|
19
|
+
- run: uv run ruff check src tests
|
|
20
|
+
- run: uv run mypy src
|
|
21
|
+
- run: uv run pytest -q
|
acpw-0.0.1/.gitignore
ADDED
acpw-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ACPW contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
acpw-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: acpw
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: ACP 会话保持代理:客户端只连一个会话,后端可热切换并重建上下文
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Requires-Dist: agent-client-protocol>=1.1.0
|
|
9
|
+
Provides-Extra: http
|
|
10
|
+
Requires-Dist: agent-client-protocol[http]>=1.1.0; extra == 'http'
|
|
11
|
+
Requires-Dist: uvicorn>=0.30; extra == 'http'
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# ACPW
|
|
15
|
+
|
|
16
|
+
[](https://github.com/mmdsnb/acpw/actions/workflows/ci.yml)
|
|
17
|
+
[](https://www.python.org/downloads/)
|
|
18
|
+
[](LICENSE)
|
|
19
|
+
|
|
20
|
+
**ACP 会话保持代理**([Agent Client Protocol](https://agentclientprotocol.com)):对客户端只呈现**一个** agent、**一个**会话,背后挂着可热切换的 ACP 后端。后端额度耗尽或进程死亡时,会话不变,ACPW 用自己落库的历史重建上下文、接续执行。
|
|
21
|
+
|
|
22
|
+
[English](README.en.md)
|
|
23
|
+
|
|
24
|
+
## 快速使用
|
|
25
|
+
|
|
26
|
+
需要 Python >= 3.11。
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
uv tool install acpw # 或 pipx install acpw / pip install acpw
|
|
30
|
+
uv tool install 'acpw[http]' # 需要 Streamable HTTP 传输时
|
|
31
|
+
# 或从源码安装
|
|
32
|
+
uv tool install git+https://github.com/mmdsnb/acpw
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
后端就是任何会说 ACP 的 stdio 命令,没有厂商绑定:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
acpw backend add opencode --cmd "opencode acp"
|
|
39
|
+
acpw backend add qoder --cmd "qoder acp"
|
|
40
|
+
acpw backend list
|
|
41
|
+
|
|
42
|
+
# 自检:验证一条命令能不能当后端用(握手 + 建会话 + 探测模型,不发 prompt、不耗额度)
|
|
43
|
+
acpw check "opencode acp"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
✓ handshake: OpenCode 2.4.2, protocol v1
|
|
48
|
+
✓ session/new: ses_dce33d55cffeTXgIQwwxu5ChG5
|
|
49
|
+
✓ model picker: 22 models
|
|
50
|
+
✓ session/delete: supported (probe session removed)
|
|
51
|
+
verdict: usable as an ACP backend
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
启动代理:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
acpw acp # stdio,默认使用上次使用的后端
|
|
58
|
+
acpw acp --backend qoder # 本次指定后端
|
|
59
|
+
acpw http --port 8000 # Streamable HTTP / SSE,ACP 端点为 /acp
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
在任何支持 ACP 的客户端里注册为 agent 即可:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{ "command": "acpw", "args": ["acp"] }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**在客户端里换后端**:模型下拉由 ACPW 合成,值编码为 `后端/模型id`(按第一个 `/` 分隔),显示名是 `后端 / 模型名`。选中另一个后端的模型,就在 turn 边界完成切换。
|
|
69
|
+
|
|
70
|
+
## 原理
|
|
71
|
+
|
|
72
|
+
```mermaid
|
|
73
|
+
flowchart LR
|
|
74
|
+
C["ACP 客户端<br/>只看到一个 agent、一个会话"]
|
|
75
|
+
subgraph P["acpw 代理进程"]
|
|
76
|
+
direction TB
|
|
77
|
+
M["会话映射<br/>acpw-N ⇄ 后端 sessionId"]
|
|
78
|
+
H["交接构建<br/>压缩摘要 + 锚点后对话"]
|
|
79
|
+
DB[("SQLite<br/>消息 / 压缩 / 模型选择")]
|
|
80
|
+
M <--> DB
|
|
81
|
+
H <--> DB
|
|
82
|
+
end
|
|
83
|
+
A["后端 A(当前)<br/>任意 ACP stdio agent"]
|
|
84
|
+
B["后端 B<br/>任意 ACP stdio agent"]
|
|
85
|
+
C <-->|"ACP JSON-RPC<br/>stdio 或 Streamable HTTP"| P
|
|
86
|
+
P -->|当前后端| A
|
|
87
|
+
P -.->|"turn 边界切换 / 死后重建"| B
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```mermaid
|
|
91
|
+
sequenceDiagram
|
|
92
|
+
autonumber
|
|
93
|
+
participant C as ACP 客户端
|
|
94
|
+
participant P as acpw
|
|
95
|
+
participant A as 后端 A(当前)
|
|
96
|
+
participant B as 后端 B(切换后)
|
|
97
|
+
C->>P: session/prompt(会话 acpw-1)
|
|
98
|
+
P->>A: 转发 prompt(换成 A 的 sessionId)
|
|
99
|
+
Note over A: 额度耗尽 / 进程死亡
|
|
100
|
+
P-->>C: stopReason: cancelled + 说明
|
|
101
|
+
C->>P: 下一轮 session/prompt
|
|
102
|
+
P->>B: session/new + 注入交接(摘要 + 尾部)
|
|
103
|
+
B-->>P: 正常执行
|
|
104
|
+
P-->>C: 结果(会话仍是 acpw-1)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
- **虚拟会话**:客户端只看到 `acpw-N`,到后端时经映射表换成真实 sessionId,`session/update` 反向换回。
|
|
108
|
+
- **只在 turn 边界切换**:turn 中途后端死亡 → 本轮回 `cancelled` 并补说明,不回放半轮、不做副作用归因;文件改动保留在工作区。
|
|
109
|
+
- **重建 + 交接**:下一轮拉起新后端、新建会话,把历史作为**一条静默 prompt** 注入(发生过压缩时 =「最新摘要 + 锚点之后的对话」,否则全量历史)。纯文本注入,不依赖 `session/load`、`resume` 等可选能力。
|
|
110
|
+
- **压缩镜像**:对后端强制声明 `session.compaction`,按协议落库压缩事件;客户端没声明该能力时只落库不转发。
|
|
111
|
+
- **模型聚合**:把各后端 `category=model` 的配置项合成一个下拉,值 `后端/模型id`;同后端直连转发,跨后端在 turn 边界重建。
|
|
112
|
+
- **store 是唯一真相**:消息与压缩实体都在 SQLite,客户端重连时按时间线回放。
|
|
113
|
+
|
|
114
|
+
## 支持范围
|
|
115
|
+
|
|
116
|
+
- 任何实现 ACP 的 stdio agent 都可以接:`acpw backend add` 注册,`acpw check` 自检。已实测 opencode、qoder。
|
|
117
|
+
- **必需能力**:`initialize` / `session/new` / `session/prompt` / `session/update`——所有 ACP agent 都有。
|
|
118
|
+
- **加分项**(没有也能用):
|
|
119
|
+
- `category=model` 的配置项 → 客户端里能聚合出模型下拉;没有则固定单后端
|
|
120
|
+
- `compaction_update` → 交接可用摘要;没有则退化为全量历史交接
|
|
121
|
+
- `session/delete` → 探测会话可清理;没有则可能残留
|
|
122
|
+
- **不需要**:`session/load`、`session/resume`、`session/fork`。
|
|
123
|
+
- **限制**:
|
|
124
|
+
- 一个代理进程同一时刻只运行一个后端进程(所有虚拟会话共享)
|
|
125
|
+
- turn 中途不做无缝迁移:该轮回 `cancelled`,下一轮接续
|
|
126
|
+
- 不做自动 failover:换后端由客户端在模型选择器里发起
|
|
127
|
+
|
|
128
|
+
## 环境变量
|
|
129
|
+
|
|
130
|
+
| 变量 | 作用 |
|
|
131
|
+
|---|---|
|
|
132
|
+
| `ACPW_HOME` | 数据目录,默认 `$XDG_DATA_HOME/acpw`;SQLite 为 `acpw.db` |
|
|
133
|
+
| `ACPW_CONFIG` | 后端配置文件路径,默认 `$XDG_CONFIG_HOME/.acpw/config.toml` |
|
|
134
|
+
| `ACPW_DEBUG` | 设置后打开 SDK 调试日志(stderr) |
|
|
135
|
+
| `ACPW_REQUEST_TIMEOUT` | 非 prompt 类后端请求的超时秒数,默认 `120`;超时即杀掉后端进程 |
|
|
136
|
+
| `ACPW_STALL_TIMEOUT` | turn 进行中后端静默的容忍秒数,默认 `600`;超时即杀进程,本轮回 `cancelled` |
|
|
137
|
+
|
|
138
|
+
后端 API key 等凭据走后端自身的环境变量,ACPW 完整透传环境。
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
[MIT](LICENSE)
|
acpw-0.0.1/README.en.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# ACPW
|
|
2
|
+
|
|
3
|
+
[](https://github.com/mmdsnb/acpw/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**An ACP ([Agent Client Protocol](https://agentclientprotocol.com)) session-keeping proxy.** It presents a single agent and a single session to the client, while routing to swappable ACP backends underneath. When a backend dies or runs out of quota, the session stays the same — ACPW cancels the hanging turn and rebuilds the context on the next backend from its own SQLite transcript.
|
|
8
|
+
|
|
9
|
+
[中文](README.md)
|
|
10
|
+
|
|
11
|
+
## Quick start
|
|
12
|
+
|
|
13
|
+
Requires Python >= 3.11.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
uv tool install acpw # or: pipx install acpw / pip install acpw
|
|
17
|
+
uv tool install 'acpw[http]' # if you need the Streamable HTTP transport
|
|
18
|
+
# or install from source
|
|
19
|
+
uv tool install git+https://github.com/mmdsnb/acpw
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
A backend is just any stdio command that speaks ACP — no vendor lock-in:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
acpw backend add opencode --cmd "opencode acp"
|
|
26
|
+
acpw backend add qoder --cmd "qoder acp"
|
|
27
|
+
acpw backend list
|
|
28
|
+
|
|
29
|
+
# Sanity check: can this command act as a backend?
|
|
30
|
+
# (handshake + session/new + model probe; no prompt, no quota spent)
|
|
31
|
+
acpw check "opencode acp"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
✓ handshake: OpenCode 2.4.2, protocol v1
|
|
36
|
+
✓ session/new: ses_dce33d55cffeTXgIQwwxu5ChG5
|
|
37
|
+
✓ model picker: 22 models
|
|
38
|
+
✓ session/delete: supported (probe session removed)
|
|
39
|
+
verdict: usable as an ACP backend
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Run the proxy:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
acpw acp # stdio; uses the last-used backend by default
|
|
46
|
+
acpw acp --backend qoder # pin the backend for this run
|
|
47
|
+
acpw http --port 8000 # Streamable HTTP / SSE, ACP endpoint at /acp
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Register it as an agent in any ACP-capable client:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
{ "command": "acpw", "args": ["acp"] }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**Switching backends from the client**: the model dropdown is synthesized by ACPW. Values are encoded as `backend/model-id` (split on the first `/`), labels read `backend / model-name`. Picking a model from another backend triggers the switch at the next turn boundary.
|
|
57
|
+
|
|
58
|
+
## How it works
|
|
59
|
+
|
|
60
|
+
```mermaid
|
|
61
|
+
flowchart LR
|
|
62
|
+
C["ACP client<br/>sees one agent, one session"]
|
|
63
|
+
subgraph P["acpw proxy process"]
|
|
64
|
+
direction TB
|
|
65
|
+
M["Session mapping<br/>acpw-N ⇄ backend sessionId"]
|
|
66
|
+
H["Handoff builder<br/>summary + tail after anchor"]
|
|
67
|
+
DB[("SQLite<br/>messages / compaction / model choice")]
|
|
68
|
+
M <--> DB
|
|
69
|
+
H <--> DB
|
|
70
|
+
end
|
|
71
|
+
A["Backend A (active)<br/>any ACP stdio agent"]
|
|
72
|
+
B["Backend B<br/>any ACP stdio agent"]
|
|
73
|
+
C <-->|"ACP JSON-RPC<br/>stdio or Streamable HTTP"| P
|
|
74
|
+
P -->|active backend| A
|
|
75
|
+
P -.->|"switch at turn boundary / rebuild after death"| B
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```mermaid
|
|
79
|
+
sequenceDiagram
|
|
80
|
+
autonumber
|
|
81
|
+
participant C as ACP client
|
|
82
|
+
participant P as acpw
|
|
83
|
+
participant A as Backend A (active)
|
|
84
|
+
participant B as Backend B (after switch)
|
|
85
|
+
C->>P: session/prompt (session acpw-1)
|
|
86
|
+
P->>A: forward prompt (rewritten to A's sessionId)
|
|
87
|
+
Note over A: quota exhausted / process died
|
|
88
|
+
P-->>C: stopReason: cancelled + note
|
|
89
|
+
C->>P: next session/prompt
|
|
90
|
+
P->>B: session/new + inject handoff (summary + tail)
|
|
91
|
+
B-->>P: executes
|
|
92
|
+
P-->>C: result (still session acpw-1)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
- **Virtual session**: the client only sees `acpw-N`; the mapping to real backend sessionIds stays internal, and `session/update` is rewritten back.
|
|
96
|
+
- **Switching only at turn boundaries**: if the backend dies mid-turn, the turn ends with `cancelled` and an explanatory message — no half-turn replay, file changes stay in the workspace.
|
|
97
|
+
- **Rebuild + handoff**: the next turn spawns the new backend, creates a fresh session, and injects the history as **one silent prompt** (latest compaction summary + messages after the anchor, or full history when no compaction happened). Plain text injection — no reliance on optional `session/load` or `resume`.
|
|
98
|
+
- **Compaction mirror**: ACPW force-declares `session.compaction` to the backend and records compaction events per protocol; if the client didn't declare the capability, events are stored but not forwarded.
|
|
99
|
+
- **Model aggregation**: every backend's `category=model` config option is merged into one dropdown with values `backend/model-id`; same-backend picks are forwarded directly, cross-backend picks trigger a rebuild at the next turn boundary.
|
|
100
|
+
- **The store is the only truth**: messages and compaction entities live in SQLite; reconnecting clients get a timeline replay.
|
|
101
|
+
|
|
102
|
+
## Support scope
|
|
103
|
+
|
|
104
|
+
- Any ACP-speaking stdio agent works: register it with `acpw backend add`, verify it with `acpw check`. Tested with opencode and qoder.
|
|
105
|
+
- **Required**: `initialize` / `session/new` / `session/prompt` / `session/update` — every ACP agent has these.
|
|
106
|
+
- **Nice to have** (optional):
|
|
107
|
+
- a `category=model` config option → enables the aggregated model dropdown; without it you're pinned to one backend
|
|
108
|
+
- `compaction_update` → handoff can use a summary; otherwise the full history is replayed as the handoff
|
|
109
|
+
- `session/delete` → probe sessions get cleaned up; otherwise they may linger
|
|
110
|
+
- **Not required**: `session/load`, `session/resume`, `session/fork`.
|
|
111
|
+
- **Limitations**:
|
|
112
|
+
- one proxy process drives exactly one backend process at a time (shared by all virtual sessions)
|
|
113
|
+
- no seamless mid-turn migration: that turn returns `cancelled` and the next turn continues elsewhere
|
|
114
|
+
- no automatic failover: switching is initiated by the client through the model dropdown
|
|
115
|
+
|
|
116
|
+
## Environment variables
|
|
117
|
+
|
|
118
|
+
| Variable | Purpose |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `ACPW_HOME` | Data directory, defaults to `$XDG_DATA_HOME/acpw`; SQLite file is `acpw.db` |
|
|
121
|
+
| `ACPW_CONFIG` | Backend config path, defaults to `$XDG_CONFIG_HOME/.acpw/config.toml` |
|
|
122
|
+
| `ACPW_DEBUG` | Enable SDK debug logging on stderr |
|
|
123
|
+
| `ACPW_REQUEST_TIMEOUT` | Timeout in seconds for non-prompt backend requests, default `120`; on timeout the backend process is killed |
|
|
124
|
+
| `ACPW_STALL_TIMEOUT` | Tolerated silence (no updates / reverse requests) during a turn, default `600`; on timeout the turn ends `cancelled` and the process is killed |
|
|
125
|
+
|
|
126
|
+
Backend credentials (API keys etc.) go through the backend's own environment variables — ACPW passes the environment through untouched.
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
[MIT](LICENSE)
|
acpw-0.0.1/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# ACPW
|
|
2
|
+
|
|
3
|
+
[](https://github.com/mmdsnb/acpw/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**ACP 会话保持代理**([Agent Client Protocol](https://agentclientprotocol.com)):对客户端只呈现**一个** agent、**一个**会话,背后挂着可热切换的 ACP 后端。后端额度耗尽或进程死亡时,会话不变,ACPW 用自己落库的历史重建上下文、接续执行。
|
|
8
|
+
|
|
9
|
+
[English](README.en.md)
|
|
10
|
+
|
|
11
|
+
## 快速使用
|
|
12
|
+
|
|
13
|
+
需要 Python >= 3.11。
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
uv tool install acpw # 或 pipx install acpw / pip install acpw
|
|
17
|
+
uv tool install 'acpw[http]' # 需要 Streamable HTTP 传输时
|
|
18
|
+
# 或从源码安装
|
|
19
|
+
uv tool install git+https://github.com/mmdsnb/acpw
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
后端就是任何会说 ACP 的 stdio 命令,没有厂商绑定:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
acpw backend add opencode --cmd "opencode acp"
|
|
26
|
+
acpw backend add qoder --cmd "qoder acp"
|
|
27
|
+
acpw backend list
|
|
28
|
+
|
|
29
|
+
# 自检:验证一条命令能不能当后端用(握手 + 建会话 + 探测模型,不发 prompt、不耗额度)
|
|
30
|
+
acpw check "opencode acp"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
✓ handshake: OpenCode 2.4.2, protocol v1
|
|
35
|
+
✓ session/new: ses_dce33d55cffeTXgIQwwxu5ChG5
|
|
36
|
+
✓ model picker: 22 models
|
|
37
|
+
✓ session/delete: supported (probe session removed)
|
|
38
|
+
verdict: usable as an ACP backend
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
启动代理:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
acpw acp # stdio,默认使用上次使用的后端
|
|
45
|
+
acpw acp --backend qoder # 本次指定后端
|
|
46
|
+
acpw http --port 8000 # Streamable HTTP / SSE,ACP 端点为 /acp
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
在任何支持 ACP 的客户端里注册为 agent 即可:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{ "command": "acpw", "args": ["acp"] }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**在客户端里换后端**:模型下拉由 ACPW 合成,值编码为 `后端/模型id`(按第一个 `/` 分隔),显示名是 `后端 / 模型名`。选中另一个后端的模型,就在 turn 边界完成切换。
|
|
56
|
+
|
|
57
|
+
## 原理
|
|
58
|
+
|
|
59
|
+
```mermaid
|
|
60
|
+
flowchart LR
|
|
61
|
+
C["ACP 客户端<br/>只看到一个 agent、一个会话"]
|
|
62
|
+
subgraph P["acpw 代理进程"]
|
|
63
|
+
direction TB
|
|
64
|
+
M["会话映射<br/>acpw-N ⇄ 后端 sessionId"]
|
|
65
|
+
H["交接构建<br/>压缩摘要 + 锚点后对话"]
|
|
66
|
+
DB[("SQLite<br/>消息 / 压缩 / 模型选择")]
|
|
67
|
+
M <--> DB
|
|
68
|
+
H <--> DB
|
|
69
|
+
end
|
|
70
|
+
A["后端 A(当前)<br/>任意 ACP stdio agent"]
|
|
71
|
+
B["后端 B<br/>任意 ACP stdio agent"]
|
|
72
|
+
C <-->|"ACP JSON-RPC<br/>stdio 或 Streamable HTTP"| P
|
|
73
|
+
P -->|当前后端| A
|
|
74
|
+
P -.->|"turn 边界切换 / 死后重建"| B
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
```mermaid
|
|
78
|
+
sequenceDiagram
|
|
79
|
+
autonumber
|
|
80
|
+
participant C as ACP 客户端
|
|
81
|
+
participant P as acpw
|
|
82
|
+
participant A as 后端 A(当前)
|
|
83
|
+
participant B as 后端 B(切换后)
|
|
84
|
+
C->>P: session/prompt(会话 acpw-1)
|
|
85
|
+
P->>A: 转发 prompt(换成 A 的 sessionId)
|
|
86
|
+
Note over A: 额度耗尽 / 进程死亡
|
|
87
|
+
P-->>C: stopReason: cancelled + 说明
|
|
88
|
+
C->>P: 下一轮 session/prompt
|
|
89
|
+
P->>B: session/new + 注入交接(摘要 + 尾部)
|
|
90
|
+
B-->>P: 正常执行
|
|
91
|
+
P-->>C: 结果(会话仍是 acpw-1)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- **虚拟会话**:客户端只看到 `acpw-N`,到后端时经映射表换成真实 sessionId,`session/update` 反向换回。
|
|
95
|
+
- **只在 turn 边界切换**:turn 中途后端死亡 → 本轮回 `cancelled` 并补说明,不回放半轮、不做副作用归因;文件改动保留在工作区。
|
|
96
|
+
- **重建 + 交接**:下一轮拉起新后端、新建会话,把历史作为**一条静默 prompt** 注入(发生过压缩时 =「最新摘要 + 锚点之后的对话」,否则全量历史)。纯文本注入,不依赖 `session/load`、`resume` 等可选能力。
|
|
97
|
+
- **压缩镜像**:对后端强制声明 `session.compaction`,按协议落库压缩事件;客户端没声明该能力时只落库不转发。
|
|
98
|
+
- **模型聚合**:把各后端 `category=model` 的配置项合成一个下拉,值 `后端/模型id`;同后端直连转发,跨后端在 turn 边界重建。
|
|
99
|
+
- **store 是唯一真相**:消息与压缩实体都在 SQLite,客户端重连时按时间线回放。
|
|
100
|
+
|
|
101
|
+
## 支持范围
|
|
102
|
+
|
|
103
|
+
- 任何实现 ACP 的 stdio agent 都可以接:`acpw backend add` 注册,`acpw check` 自检。已实测 opencode、qoder。
|
|
104
|
+
- **必需能力**:`initialize` / `session/new` / `session/prompt` / `session/update`——所有 ACP agent 都有。
|
|
105
|
+
- **加分项**(没有也能用):
|
|
106
|
+
- `category=model` 的配置项 → 客户端里能聚合出模型下拉;没有则固定单后端
|
|
107
|
+
- `compaction_update` → 交接可用摘要;没有则退化为全量历史交接
|
|
108
|
+
- `session/delete` → 探测会话可清理;没有则可能残留
|
|
109
|
+
- **不需要**:`session/load`、`session/resume`、`session/fork`。
|
|
110
|
+
- **限制**:
|
|
111
|
+
- 一个代理进程同一时刻只运行一个后端进程(所有虚拟会话共享)
|
|
112
|
+
- turn 中途不做无缝迁移:该轮回 `cancelled`,下一轮接续
|
|
113
|
+
- 不做自动 failover:换后端由客户端在模型选择器里发起
|
|
114
|
+
|
|
115
|
+
## 环境变量
|
|
116
|
+
|
|
117
|
+
| 变量 | 作用 |
|
|
118
|
+
|---|---|
|
|
119
|
+
| `ACPW_HOME` | 数据目录,默认 `$XDG_DATA_HOME/acpw`;SQLite 为 `acpw.db` |
|
|
120
|
+
| `ACPW_CONFIG` | 后端配置文件路径,默认 `$XDG_CONFIG_HOME/.acpw/config.toml` |
|
|
121
|
+
| `ACPW_DEBUG` | 设置后打开 SDK 调试日志(stderr) |
|
|
122
|
+
| `ACPW_REQUEST_TIMEOUT` | 非 prompt 类后端请求的超时秒数,默认 `120`;超时即杀掉后端进程 |
|
|
123
|
+
| `ACPW_STALL_TIMEOUT` | turn 进行中后端静默的容忍秒数,默认 `600`;超时即杀进程,本轮回 `cancelled` |
|
|
124
|
+
|
|
125
|
+
后端 API key 等凭据走后端自身的环境变量,ACPW 完整透传环境。
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "acpw"
|
|
3
|
+
version = "0.0.1"
|
|
4
|
+
description = "ACP 会话保持代理:客户端只连一个会话,后端可热切换并重建上下文"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.11"
|
|
9
|
+
dependencies = ["agent-client-protocol>=1.1.0"]
|
|
10
|
+
|
|
11
|
+
[project.optional-dependencies]
|
|
12
|
+
http = ["agent-client-protocol[http]>=1.1.0", "uvicorn>=0.30"]
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
acpw = "acpw.cli:main"
|
|
16
|
+
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["hatchling"]
|
|
19
|
+
build-backend = "hatchling.build"
|
|
20
|
+
|
|
21
|
+
[tool.hatch.build.targets.wheel]
|
|
22
|
+
packages = ["src/acpw"]
|
|
23
|
+
|
|
24
|
+
[dependency-groups]
|
|
25
|
+
dev = ["pytest>=8", "ruff>=0.6", "mypy>=1.11", "agent-client-protocol[http]>=1.1.0", "uvicorn>=0.30"]
|
|
26
|
+
|
|
27
|
+
[tool.ruff]
|
|
28
|
+
line-length = 120
|
|
29
|
+
target-version = "py311"
|
|
30
|
+
|
|
31
|
+
[tool.ruff.lint]
|
|
32
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
33
|
+
|
|
34
|
+
[tool.mypy]
|
|
35
|
+
python_version = "3.11"
|
|
36
|
+
strict = false
|
|
37
|
+
warn_unused_ignores = true
|
|
38
|
+
|
|
39
|
+
[[tool.mypy.overrides]]
|
|
40
|
+
module = ["uvicorn", "starlette.*", "httpx2.*"]
|
|
41
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import os
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import cast
|
|
7
|
+
|
|
8
|
+
from acp import PROTOCOL_VERSION, spawn_agent_process
|
|
9
|
+
from acp.interfaces import Client
|
|
10
|
+
from acp.schema import ClientCapabilities, Implementation
|
|
11
|
+
|
|
12
|
+
from acpw.models import SilentClient, dump_model, model_option
|
|
13
|
+
|
|
14
|
+
CHECK_TIMEOUT = 30.0
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class CheckReport:
|
|
19
|
+
cmd: list[str]
|
|
20
|
+
ok: bool = False
|
|
21
|
+
agent: str | None = None
|
|
22
|
+
protocol_version: int | None = None
|
|
23
|
+
session_id: str | None = None
|
|
24
|
+
has_model_option: bool = False
|
|
25
|
+
model_count: int = 0
|
|
26
|
+
can_delete: bool = False
|
|
27
|
+
error: str | None = None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
async def check_command(cmd: list[str], cwd: str = ".") -> CheckReport:
|
|
31
|
+
"""握手 + 新建会话 + 模型目录探测,不发 prompt,不耗后端额度。"""
|
|
32
|
+
report = CheckReport(cmd=cmd)
|
|
33
|
+
client = SilentClient()
|
|
34
|
+
cwd = os.path.abspath(cwd)
|
|
35
|
+
|
|
36
|
+
async def run() -> None:
|
|
37
|
+
async with spawn_agent_process(
|
|
38
|
+
lambda _c: cast(Client, client),
|
|
39
|
+
*cmd,
|
|
40
|
+
env=dict(os.environ),
|
|
41
|
+
transport_kwargs={"stderr": None},
|
|
42
|
+
) as (conn, _proc):
|
|
43
|
+
init = await conn.initialize(
|
|
44
|
+
PROTOCOL_VERSION,
|
|
45
|
+
ClientCapabilities(),
|
|
46
|
+
Implementation(name="acpw-check", version="0.0.1"),
|
|
47
|
+
)
|
|
48
|
+
report.protocol_version = init.protocol_version
|
|
49
|
+
info = init.agent_info
|
|
50
|
+
report.agent = f"{info.name} {info.version}".strip() if info else None
|
|
51
|
+
session = await conn.new_session(cwd=cwd)
|
|
52
|
+
report.session_id = session.session_id
|
|
53
|
+
data = dump_model(model_option(session.config_options))
|
|
54
|
+
report.has_model_option = data is not None
|
|
55
|
+
report.model_count = len(data["options"]) if data else 0
|
|
56
|
+
try:
|
|
57
|
+
await conn.delete_session(session.session_id)
|
|
58
|
+
report.can_delete = True
|
|
59
|
+
except Exception:
|
|
60
|
+
report.can_delete = False
|
|
61
|
+
|
|
62
|
+
try:
|
|
63
|
+
await asyncio.wait_for(run(), timeout=CHECK_TIMEOUT)
|
|
64
|
+
report.ok = report.session_id is not None
|
|
65
|
+
except Exception as exc:
|
|
66
|
+
report.error = str(exc) or exc.__class__.__name__
|
|
67
|
+
return report
|