pi-chat-gateway-py 1.3.0__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.
- pi_chat_gateway_py-1.3.0/.gitignore +37 -0
- pi_chat_gateway_py-1.3.0/PKG-INFO +421 -0
- pi_chat_gateway_py-1.3.0/README.md +389 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/__init__.py +18 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/__main__.py +5 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/acp_client.py +121 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/agent_env.py +74 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/approvals.py +178 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/audit.py +33 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/backend.py +335 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/cli.py +490 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/commands.py +37 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/config.py +453 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/credential_store.py +143 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/gateway.py +537 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/identity.py +128 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/logs.py +94 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/__init__.py +1 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/base.py +122 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/cardkit.py +300 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/fake.py +165 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/feishu.py +577 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/kinds.py +129 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/slack.py +451 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/platforms/wecom.py +465 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/render/__init__.py +1 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/render/cards.py +279 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/render/text.py +379 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/render/throttle.py +62 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/render/view.py +131 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/runner.py +886 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/store.py +245 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/texts.py +255 -0
- pi_chat_gateway_py-1.3.0/pi_chat_gateway/trust.py +79 -0
- pi_chat_gateway_py-1.3.0/pyproject.toml +60 -0
- pi_chat_gateway_py-1.3.0/tests/conftest.py +113 -0
- pi_chat_gateway_py-1.3.0/tests/gateway_support.py +75 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_agent.py +468 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_cli.py +223 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_feishu.py +489 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_slack.py +269 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_units.py +740 -0
- pi_chat_gateway_py-1.3.0/tests/test_chat_wecom.py +325 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*.egg-info/
|
|
4
|
+
.eggs/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
!tui/crates/build/
|
|
8
|
+
.pytest_cache/
|
|
9
|
+
.pytest-audit/
|
|
10
|
+
.mypy_cache/
|
|
11
|
+
.ruff_cache/
|
|
12
|
+
.venv/
|
|
13
|
+
.venv-*/
|
|
14
|
+
venv/
|
|
15
|
+
.env
|
|
16
|
+
# Rust workspaces (tui/, crates/) and NSH CLI build outputs
|
|
17
|
+
tui/target/
|
|
18
|
+
crates/target/
|
|
19
|
+
.nsh/
|
|
20
|
+
**/*.rs.bk
|
|
21
|
+
|
|
22
|
+
# Benchmark evaluation cache and trial artifacts
|
|
23
|
+
.cache/
|
|
24
|
+
.pi-eval/
|
|
25
|
+
|
|
26
|
+
# Temporary files, coverage and logs
|
|
27
|
+
*.log
|
|
28
|
+
*.tmp
|
|
29
|
+
*.bak
|
|
30
|
+
*.swp
|
|
31
|
+
*.orig
|
|
32
|
+
.coverage
|
|
33
|
+
.coverage.*
|
|
34
|
+
coverage.xml
|
|
35
|
+
htmlcov/
|
|
36
|
+
|
|
37
|
+
.DS_Store
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: pi-chat-gateway-py
|
|
3
|
+
Version: 1.3.0
|
|
4
|
+
Summary: Use the zycode agent from a chat app (Feishu / Lark first): a standard ACP client that starts zycode-agent
|
|
5
|
+
Project-URL: Homepage, https://github.com/zy1233/zycode
|
|
6
|
+
Project-URL: Website, https://zycode.dev
|
|
7
|
+
Project-URL: Repository, https://github.com/zy1233/zycode
|
|
8
|
+
Project-URL: Issues, https://github.com/zy1233/zycode/issues
|
|
9
|
+
Author-email: zy1233 <zy1233@users.noreply.github.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
Keywords: acp,agent,bot,chat,coding-agent,feishu,lark
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Requires-Dist: agent-client-protocol<1.0.0,>=0.12.0
|
|
21
|
+
Requires-Dist: keyring<26,>=24
|
|
22
|
+
Requires-Dist: markdown-it-py<5,>=3
|
|
23
|
+
Requires-Dist: pi-agent-cli-lc<2,>=1.3.0
|
|
24
|
+
Provides-Extra: feishu
|
|
25
|
+
Requires-Dist: lark-oapi<2,>=1.9; extra == 'feishu'
|
|
26
|
+
Provides-Extra: slack
|
|
27
|
+
Requires-Dist: aiohttp>=3.9; extra == 'slack'
|
|
28
|
+
Requires-Dist: slack-sdk<4,>=3.40; extra == 'slack'
|
|
29
|
+
Provides-Extra: wecom
|
|
30
|
+
Requires-Dist: wecom-aibot-python-sdk<2,>=1.0.2; extra == 'wecom'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# pi-chat-gateway-py
|
|
34
|
+
|
|
35
|
+
Use the [zycode](https://github.com/zy1233/zycode) coding agent from a chat app: **Feishu / Lark**,
|
|
36
|
+
**Slack** and **WeCom (企业微信)**. Version 0.1 is for one person: the owner of the bot.
|
|
37
|
+
|
|
38
|
+
The gateway (`zycode-chat`) is one more standard [ACP](https://agentclientprotocol.com) client,
|
|
39
|
+
like the VS Code extension and `zypi`. It keeps a long connection to each platform (Feishu's
|
|
40
|
+
long connection, Slack's Socket Mode, WeCom's smart-bot long connection: it connects out, so no
|
|
41
|
+
public port is needed), turns your messages into prompts, and starts one `zycode-agent` per
|
|
42
|
+
project on your machine. The agent does the work there; the chat shows it and asks you to approve.
|
|
43
|
+
|
|
44
|
+
Design: [docs/specs/2026-11-28-chat-gateway-design.md](https://github.com/zy1233/zycode/blob/main/docs/specs/2026-11-28-chat-gateway-design.md).
|
|
45
|
+
|
|
46
|
+
Status: alpha, on PyPI from 1.3.0 (`pi-chat-gateway-py`, released with the agent). It is tested against the real agent with a fake chat
|
|
47
|
+
platform, and each adapter is tested with hand-built events and a fake SDK client. Feishu was run
|
|
48
|
+
against a live tenant on macOS on 2026-11-29 (see [Live check](#live-check-2026-11-29)). Slack and
|
|
49
|
+
WeCom have not met a live workspace or tenant yet; what that leaves open is under
|
|
50
|
+
[Not verified](#not-verified).
|
|
51
|
+
|
|
52
|
+
## What it does
|
|
53
|
+
|
|
54
|
+
- **Tasks from the chat.** Send the bot a direct message. The answer is a card that the gateway
|
|
55
|
+
updates while the agent works (status, one line per tool call, the answer). On Feishu the card
|
|
56
|
+
streams: it is a CardKit card entity, and the client types the answer as it grows (an update
|
|
57
|
+
every 0.5 s). Without CardKit (the scope `cardkit:card:write` is missing, or `cardkit = false`)
|
|
58
|
+
it is a plain card updated at most once a second.
|
|
59
|
+
- **One writer per session.** A session that VS Code or `zypi` is writing (a turn there) is not
|
|
60
|
+
written from the chat: the gateway says which program holds it. What they wrote meanwhile is
|
|
61
|
+
in the context of the next turn, and the next turn card opens with one line that says so
|
|
62
|
+
(register item X11).
|
|
63
|
+
This needs an agent that takes the lock (1.3.0 or later); an older agent in VS Code does not.
|
|
64
|
+
- **Approvals on your phone.** When the agent asks before a tool call, a card shows the whole
|
|
65
|
+
command, path, code or script, with **Allow once** and **Reject**. A text too long for one card
|
|
66
|
+
follows in full in more cards; nothing is cut silently. Only the owner (and, in a bound group,
|
|
67
|
+
the listed `approvers`) can approve. An approval nobody answers in 30 minutes is rejected.
|
|
68
|
+
- **Steer.** A message sent while a turn runs is added to it (`pi/session/steer`): ⏳ when it is
|
|
69
|
+
queued, ✅ when the model read it, ❌ with a note when the turn ended first.
|
|
70
|
+
- **`/btw`.** A side question answered in its own card from a copy of the session, never saved
|
|
71
|
+
in it (`pi/session/btw`).
|
|
72
|
+
- **Commands:** `/new`, `/project <name>`, `/resume`, `/stop`, `/btw <question>` (`/btw end`),
|
|
73
|
+
`/mode ask|auto`, `/model`, `/thinking`, `/status`, `/help`. Any other `/command` goes to the
|
|
74
|
+
agent (its `/skill:<name>` commands, say).
|
|
75
|
+
- **Groups (optional).** A group listed under `[[channels]]` is bound to one project. Mention the
|
|
76
|
+
bot to start; each mention starts a thread with its own session. Approvals of group sessions
|
|
77
|
+
go to the owner's direct chat.
|
|
78
|
+
|
|
79
|
+
## Safety rules
|
|
80
|
+
|
|
81
|
+
These are fixed, not options:
|
|
82
|
+
|
|
83
|
+
1. **Never `always-approve`.** `start_mode` is `ask` (default) or `auto` (only with
|
|
84
|
+
`allow_auto = true`). If `agent.toml` puts a session in `always-approve`, the gateway puts it
|
|
85
|
+
back to `ask` and says so; a session it cannot put back is not used. The gateway never sends
|
|
86
|
+
`pi/yolo_mode_changed`, which would move every session of an agent process.
|
|
87
|
+
2. **No project trust from the chat.** When the agent asks whether to trust the project (its
|
|
88
|
+
extensions, saved workflows, prompt files or skills), the gateway answers no and tells you to
|
|
89
|
+
trust the project on the machine (in `zypi` or VS Code). Trusting runs the project's code.
|
|
90
|
+
3. **Only the owner is heard**, and in a bound group the `users` listed there. Messages from
|
|
91
|
+
anyone else, or from another tenant, are dropped without an answer, except `/pair`, which
|
|
92
|
+
always gets one (whether the code worked). The owner pairs with a one-time code printed in the
|
|
93
|
+
machine's terminal.
|
|
94
|
+
4. **The credentials are in the system's credential manager, not in a file.** macOS Keychain,
|
|
95
|
+
Windows Credential Manager, or the Secret Service on Linux (GNOME Keyring, KWallet), through
|
|
96
|
+
`keyring`: service `zycode-chat`, account `<platform id>@<home>`. Not in `local.env`, which
|
|
97
|
+
the agent loads into its own environment, and the agents start without any variable that
|
|
98
|
+
holds a platform credential. The agent's file tools (`read`, `grep`, `find`) cannot reach an
|
|
99
|
+
entry of the credential manager; a `bash` call can (`security`, `secret-tool`, the `keyring`
|
|
100
|
+
command, a line of Python), and `ask` asks about every `bash` call, so read what you approve.
|
|
101
|
+
In `auto`, the rules ask for the programs they know; for one they do not decide (the
|
|
102
|
+
`keyring` command, say), the model step decides when it is on (`classifier = "rules"` asks). That is the
|
|
103
|
+
boundary this gives, and no more: for a process of the same user, the boundary is a sandbox
|
|
104
|
+
(`zypi --sandbox`, a container or a VM).
|
|
105
|
+
Only the managers above count: a `keyring` backend that writes a file (the plaintext ones of
|
|
106
|
+
`keyrings.alt`) counts as none.
|
|
107
|
+
**A machine with no credential manager** (a server with no desktop session, a container)
|
|
108
|
+
keeps them in `<home>/chat/credentials.toml` (mode 600; a looser file is refused), and
|
|
109
|
+
`setup`, `status` and `run` say so. The agent's `read` declares itself harmless, so `ask` and
|
|
110
|
+
`auto` run it with no question, and the file is in the agent's own home: a prompt that makes
|
|
111
|
+
the model read it (from you, from a listed `users` member, or from text the agent read) puts
|
|
112
|
+
the platform secrets into the model's context, the session file and the request to the model
|
|
113
|
+
provider (the posted text is redacted, so the plain value does not reach the chat). Run the
|
|
114
|
+
agents in a sandbox there, and keep `users` to people you would give those secrets to.
|
|
115
|
+
`credential_store` in `[gateway]` chooses: `auto` (default), `keyring` (refuse to start
|
|
116
|
+
without one) or `file`. `zycode-chat credentials --to-keyring` moves a file of an earlier
|
|
117
|
+
setup into the credential manager, drops the copies the manager already holds, and deletes
|
|
118
|
+
the file when nothing is left; `status` and `run` name every table that the file still
|
|
119
|
+
holds and nothing reads.
|
|
120
|
+
5. **Nothing the platform could fetch.** Links in answers are shown as code and Markdown images
|
|
121
|
+
as text, so the platform's servers do not fetch a URL the model wrote (a link preview would
|
|
122
|
+
send whatever the URL carries). After that, a CommonMark parser reads the text: if it still
|
|
123
|
+
finds a link, an image or HTML, every link opener and tag of the text gets a zero-width
|
|
124
|
+
space. Card tags such as `<at id=all>` and the mention forms of Slack and WeCom
|
|
125
|
+
(`<!channel>`, `<@U...>`) are shown as text, in the card and in its notification line. Known
|
|
126
|
+
secret values and common token formats are redacted (best effort); while a card still
|
|
127
|
+
changes, an end that could be the start of a secret waits for the next piece.
|
|
128
|
+
6. **Tool output is not posted.** A tool call shows as one line; its output stays on the machine.
|
|
129
|
+
|
|
130
|
+
What is posted to the chat (answers, commands, paths, code) is stored on Feishu's servers. The
|
|
131
|
+
gateway is not a sandbox: the agent runs on your machine with your permissions, and Auto mode
|
|
132
|
+
does not guard what a call reads.
|
|
133
|
+
|
|
134
|
+
## Set up
|
|
135
|
+
|
|
136
|
+
1. Install it next to the agent (Python 3.11+):
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
pip install "pi-chat-gateway-py[feishu]" # or [slack], [wecom], or several
|
|
140
|
+
# In a checkout of the repository:
|
|
141
|
+
pip install -e "./packages/pi-chat-gateway[feishu]"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Each platform's SDK is an extra: `lark-oapi` (Feishu), `slack-sdk` and `aiohttp` (Slack),
|
|
145
|
+
and the official `wecom-aibot-python-sdk` (WeCom).
|
|
146
|
+
|
|
147
|
+
The agent must be 1.2.0 or later (steer and `/btw`). The gateway uses `zycode-agent` if it
|
|
148
|
+
finds one next to itself or on `PATH`, else `python -m pi_agent_cli` of its own environment;
|
|
149
|
+
`agent_command` in `chat.toml` overrides both. The agent needs a model in
|
|
150
|
+
`agent.toml` of its home as usual (`zycode-agent --setup`). The home is `~/.zycode`, or
|
|
151
|
+
`~/.pi-python` for an install that has only that folder; `ZYCODE_HOME` or `PI_HOME` overrides it.
|
|
152
|
+
Below, `~/.zycode` names the home.
|
|
153
|
+
|
|
154
|
+
2. Make the bot on the platform's side ([Feishu / Lark](#feishu--lark), [Slack](#slack),
|
|
155
|
+
[WeCom](#wecom-企业微信) below).
|
|
156
|
+
|
|
157
|
+
3. Run `zycode-chat setup`. It asks for the project and the platform, writes
|
|
158
|
+
`~/.zycode/chat.toml` (the first time), stores the credentials in the system's credential
|
|
159
|
+
manager (or, on a machine without one, in `~/.zycode/chat/credentials.toml`), then tests
|
|
160
|
+
the credentials (Feishu and Slack: it reads the bot's identity; WeCom: not until `run`, because
|
|
161
|
+
connecting would take the bot's one long connection from a gateway that runs). It prints the
|
|
162
|
+
platform's steps again.
|
|
163
|
+
|
|
164
|
+
4. Run `zycode-chat run`, then `zycode-chat pair` in another terminal and send the code it prints
|
|
165
|
+
to the bot in a direct chat: `/pair ABCD-EFGH`. The code is good for 10 minutes and one use.
|
|
166
|
+
|
|
167
|
+
To run it as a service, start `zycode-chat run` from a systemd user unit, a launchd agent or a
|
|
168
|
+
Windows scheduled task. With `env = "login-shell"` (the default on Linux and macOS) the agents get
|
|
169
|
+
the environment of your login shell, so `bash` finds the same programs as in a terminal.
|
|
170
|
+
|
|
171
|
+
### Feishu / Lark
|
|
172
|
+
|
|
173
|
+
In the Feishu (or Lark) developer console, create an app in your tenant:
|
|
174
|
+
- add the **Bot** capability;
|
|
175
|
+
- grant the permissions to receive the messages users send to the bot (direct chats and
|
|
176
|
+
@mentions in groups), to send messages as the app, to read message resources (images), to
|
|
177
|
+
add and remove reactions, and to create and update cards (CardKit, for the streaming turn
|
|
178
|
+
card). On Feishu these are the app scopes below; the console's **batch import** takes them
|
|
179
|
+
as JSON:
|
|
180
|
+
|
|
181
|
+
```json
|
|
182
|
+
{"scopes": {"tenant": ["im:message.p2p_msg:readonly", "im:message.group_at_msg:readonly",
|
|
183
|
+
"im:message:send_as_bot", "im:message.reactions:write_only", "im:resource",
|
|
184
|
+
"cardkit:card:write"], "user": []}}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
The first live check of 2026-11-29 used the first five; the second added
|
|
188
|
+
`cardkit:card:write` (no review: the console published the change by itself). Without it
|
|
189
|
+
the gateway logs a warning and posts plain cards.
|
|
190
|
+
|
|
191
|
+
- under events and callbacks, choose **receive through a long connection**, subscribe to
|
|
192
|
+
`im.message.receive_v1`, and add the callback `card.action.trigger` (the new one, not
|
|
193
|
+
`card.action.trigger_v1`). The console saves the long connection only while it sees one,
|
|
194
|
+
so run `zycode-chat setup` and `zycode-chat run` (steps 3 and 4) before this step;
|
|
195
|
+
- publish a version and have it approved in your tenant (with only you, or up to 5 more
|
|
196
|
+
members, in its availability, Feishu publishes it with no review).
|
|
197
|
+
|
|
198
|
+
### Slack
|
|
199
|
+
|
|
200
|
+
On https://api.slack.com/apps, **Create New App > From a manifest**, pick the workspace, paste
|
|
201
|
+
this manifest and install the app:
|
|
202
|
+
|
|
203
|
+
```json
|
|
204
|
+
{
|
|
205
|
+
"display_information": {"name": "zycode"},
|
|
206
|
+
"features": {
|
|
207
|
+
"bot_user": {"display_name": "zycode", "always_online": true},
|
|
208
|
+
"app_home": {"messages_tab_enabled": true, "messages_tab_read_only_enabled": false}
|
|
209
|
+
},
|
|
210
|
+
"oauth_config": {"scopes": {"bot": ["app_mentions:read", "chat:write", "im:history",
|
|
211
|
+
"im:write", "reactions:write", "files:read"]}},
|
|
212
|
+
"settings": {
|
|
213
|
+
"event_subscriptions": {"bot_events": ["app_mention", "message.im"]},
|
|
214
|
+
"interactivity": {"is_enabled": true},
|
|
215
|
+
"socket_mode_enabled": true,
|
|
216
|
+
"token_rotation_enabled": false
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
Then **Basic Information > App-Level Tokens**: generate one with the scope `connections:write`
|
|
222
|
+
(`xapp-...`); **OAuth & Permissions**: the Bot User OAuth Token (`xoxb-...`). `setup` asks for
|
|
223
|
+
both. Invite the app to a channel that you list under `[[channels]]` (its id starts with `C`).
|
|
224
|
+
|
|
225
|
+
How the gateway uses Slack: a direct message to the app, or an @mention in a bound channel,
|
|
226
|
+
which starts a thread (the thread is the session). Cards are Block Kit messages (a header,
|
|
227
|
+
sections of at most 3,000 characters, a context line, buttons), posted with link and media
|
|
228
|
+
unfurls off; a turn card is updated with `chat.update` every 1.5 s at most. The streaming API
|
|
229
|
+
(`chat.startStream`) is not used yet. A warning for the person who tapped a button (a stranger,
|
|
230
|
+
a closed request) is an ephemeral message. Images are downloaded with the bot token from Slack's
|
|
231
|
+
own hosts only.
|
|
232
|
+
|
|
233
|
+
### WeCom (企业微信)
|
|
234
|
+
|
|
235
|
+
In the WeCom admin console, create a smart bot (智能机器人) in API mode with the **long
|
|
236
|
+
connection** (长连接), copy its Bot ID and Secret, set who may use it (可见范围), and add it to a
|
|
237
|
+
group that you list under `[[channels]]` (the group's `chatid`). Send the bot a message first: it
|
|
238
|
+
can write only to a chat that wrote to it. One bot keeps one long connection, so run one gateway
|
|
239
|
+
per bot (a second one takes the connection over, and the first logs it).
|
|
240
|
+
|
|
241
|
+
How the gateway uses WeCom, by the platform's rules (its long-connection docs, read 2026-11-29):
|
|
242
|
+
|
|
243
|
+
- A reply goes to the last message of the chat (for 24 hours); without one the gateway sends
|
|
244
|
+
proactively. A turn card is a stream reply (the whole text in each frame, 20,000 bytes at
|
|
245
|
+
most), updated every 2.5 s: replies and proactive messages share 30 a minute per chat. A
|
|
246
|
+
stream ends after 10 minutes, so a longer turn goes on in a new message.
|
|
247
|
+
- An approval is the whole request as Markdown, then a button card (its subtitle holds 112
|
|
248
|
+
characters). A tap changes the card in place (the platform allows that within 5 seconds of the
|
|
249
|
+
tap only); an approval that times out or is cancelled gets a line of its own instead.
|
|
250
|
+
- No reactions: a steer that is queued gets a line. No threads: a group has one current session,
|
|
251
|
+
as a direct chat has (`/new` starts the next one).
|
|
252
|
+
- Images in direct chats are downloaded and decrypted by the SDK; WeCom sends none from groups.
|
|
253
|
+
|
|
254
|
+
## `chat.toml`
|
|
255
|
+
|
|
256
|
+
```toml
|
|
257
|
+
[gateway]
|
|
258
|
+
start_mode = "ask" # ask | auto (needs allow_auto = true)
|
|
259
|
+
allow_auto = false
|
|
260
|
+
approval_timeout_minutes = 30
|
|
261
|
+
approvals_in_dm = true
|
|
262
|
+
idle_agent_minutes = 20 # an idle agent process is stopped, and started again when needed
|
|
263
|
+
max_active_turns = 3
|
|
264
|
+
show_thinking = false
|
|
265
|
+
env = "login-shell" # login-shell | inherit
|
|
266
|
+
language = "zh" # zh | en
|
|
267
|
+
credential_store = "auto" # auto | keyring | file (rule 4)
|
|
268
|
+
|
|
269
|
+
[[projects]]
|
|
270
|
+
name = "zycode"
|
|
271
|
+
path = "/home/me/work/zycode" # absolute; the chat picks projects by name only
|
|
272
|
+
|
|
273
|
+
[[platforms]]
|
|
274
|
+
id = "feishu"
|
|
275
|
+
type = "feishu"
|
|
276
|
+
domain = "feishu" # feishu (open.feishu.cn) | lark (open.larksuite.com)
|
|
277
|
+
# tenant = "..." # empty: the tenant of the owner, learned at pairing
|
|
278
|
+
cardkit = true # streaming turn cards (scope cardkit:card:write); false: plain
|
|
279
|
+
|
|
280
|
+
# [[platforms]] # Slack: credentials bot_token (xoxb-) and app_token (xapp-)
|
|
281
|
+
# id = "slack"
|
|
282
|
+
# type = "slack"
|
|
283
|
+
# tenant = "T..." # the workspace; empty: the owner's, learned at pairing
|
|
284
|
+
|
|
285
|
+
# [[platforms]] # WeCom: credentials bot_id and secret
|
|
286
|
+
# id = "wecom"
|
|
287
|
+
# type = "wecom"
|
|
288
|
+
|
|
289
|
+
[[channels]] # optional
|
|
290
|
+
platform = "feishu"
|
|
291
|
+
chat_id = "oc_..." # Slack: C...; WeCom: the group's chatid
|
|
292
|
+
project = "zycode"
|
|
293
|
+
users = [] # besides the owner, who may talk to the agent there
|
|
294
|
+
approvers = [] # besides the owner, who may approve there
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
The gateway keeps its own state in `~/.zycode/chat/`: `state.sqlite` (the owner, the pairing
|
|
298
|
+
codes as hashes, which chat maps to which session, the ids of handled events, and the ids of the
|
|
299
|
+
cards that still change; no message text), `audit.log` (pairings, approvals, refusals) and
|
|
300
|
+
`logs/` (the gateway's log and each agent's stderr, all mode 600). The conversations stay in the
|
|
301
|
+
agent's session files. If the gateway stops while a turn runs or an approval waits (a crash, a
|
|
302
|
+
killed process, a reboot), the turn and the request end with it; when it starts again, it marks
|
|
303
|
+
those cards **interrupted**, so that no card asks for an answer that nothing waits for.
|
|
304
|
+
|
|
305
|
+
`zycode-chat run --verbose` logs the gateway's own debug messages. The libraries that log whole
|
|
306
|
+
requests stay at INFO, and every line, tracebacks included, passes a filter that removes the
|
|
307
|
+
credentials and the `access_key`, `ticket` and token parameters of a URL. The WeCom SDK logs
|
|
308
|
+
through it too, without its DEBUG lines (which print every frame it receives).
|
|
309
|
+
|
|
310
|
+
## Live check (2026-11-29)
|
|
311
|
+
|
|
312
|
+
One run on macOS against a Feishu tenant, with an app made as in [Set up](#set-up) (the scopes
|
|
313
|
+
above, long connection, version 0.1.0 available to the owner only) and a real model behind the
|
|
314
|
+
agent. The chat was driven in the Feishu desktop client.
|
|
315
|
+
|
|
316
|
+
Seen working: the bot's identity and the long connection (the console's check of the long
|
|
317
|
+
connection said "connected"); pairing with `/pair`; a turn card that updates while the agent
|
|
318
|
+
works and ends with the answer; an approval card with the whole command and its arguments, where
|
|
319
|
+
**Allow once** ran the command and closed the card ("allowed · owner"), and the audit log has
|
|
320
|
+
`allowed`; the `card.action.trigger` callback over the long connection, also for a card of a
|
|
321
|
+
previous process ("this request is closed"); a steer while a turn ran (⏳ `OnIt` when it was
|
|
322
|
+
queued, ✅ `DONE` when the model read it, and the model followed it); `/btw` (its own card, a
|
|
323
|
+
right answer from the session); `/status`; `/stop` while an approval waited (the approval card
|
|
324
|
+
says cancelled, the command never ran, the audit log has `cancelled`); and no credential in the
|
|
325
|
+
environment of the agent process.
|
|
326
|
+
|
|
327
|
+
Found in that run and fixed the same day (each one was checked again in the tenant):
|
|
328
|
+
|
|
329
|
+
1. `--verbose` wrote the request line of the long connection to `logs/gateway.log`, with its
|
|
330
|
+
`access_key` and `ticket`, and the file was mode 644. The log is now private and filtered (see
|
|
331
|
+
above).
|
|
332
|
+
2. The turn card showed the raw ACP stop reason (`end_turn`, `cancelled`) under the answer. It
|
|
333
|
+
now shows a stop reason only when it says something (the length limit, the limit of model
|
|
334
|
+
calls, a refusal).
|
|
335
|
+
3. A closed approval card kept the title "approval needed". The title now names the outcome.
|
|
336
|
+
4. A steer message kept ⏳ beside ✅. The hourglass is now taken off.
|
|
337
|
+
5. A gateway that was killed while an approval waited left a turn card that said "waiting for
|
|
338
|
+
approval" for good. Such cards are now marked interrupted at the next start.
|
|
339
|
+
|
|
340
|
+
### Second live check: CardKit and one writer per session (2026-11-29, evening)
|
|
341
|
+
|
|
342
|
+
The same tenant and app on macOS, with agent 1.2.0 of this tree and the gateway of
|
|
343
|
+
`197d170`.
|
|
344
|
+
|
|
345
|
+
- **Streaming card.** Without the scope, creating an entity failed with `99991672` and the
|
|
346
|
+
gateway logged one warning and posted plain cards. With `cardkit:card:write` added (a scope
|
|
347
|
+
that needs no review; the console published the change itself), a turn of about 2000
|
|
348
|
+
characters went: create the entity, one full update (status and first text), six stream-text
|
|
349
|
+
calls 0.5 s apart, streaming off, the final card; all answered 200. The client typed the
|
|
350
|
+
answer, the chat list showed "[生成中...]" while it streamed and the start of the answer after.
|
|
351
|
+
A short answer that the model sends in one burst (this one thinks for about 9 s, then sends
|
|
352
|
+
400 characters within 1 s) goes out as the final update alone, which is right. A `/btw` card
|
|
353
|
+
is an entity too and closed the same way.
|
|
354
|
+
- **A streaming card of a killed process.** `kill -9` after nine stream-text calls, then a new
|
|
355
|
+
start: the gateway turned streaming off and put the interrupted card in place, on the first
|
|
356
|
+
try (the sequence from the clock was larger than the dead process's).
|
|
357
|
+
- **One writer per session.** A process that held the chat's lock (`client = x11-live-check`):
|
|
358
|
+
a message from Feishu got "this session is being written by another zycode process
|
|
359
|
+
(x11-live-check, pid …)" and no turn ran. After it let go, a second agent process loaded the
|
|
360
|
+
chat over ACP and took turns on it; the next Feishu turn knew what was said there.
|
|
361
|
+
|
|
362
|
+
- **One writer per session, with VS Code.** The VS Code extension 1.14.0, pointed at the agent of
|
|
363
|
+
this tree (`zycode.agentPath`; its bundled agent does not announce `singleWriter` and takes no
|
|
364
|
+
lock), on the same chat as the gateway. A Feishu turn that waited for an approval held the
|
|
365
|
+
lock: a message in VS Code failed with "This chat is in use in another zycode process
|
|
366
|
+
(zycode-chat 0.1.0, pid …)". A VS Code turn that waited for an approval held it: a message
|
|
367
|
+
from Feishu got the same in the chat's words, naming `zycode-vscode 1.14.0`. Neither refused
|
|
368
|
+
message reached the file. After each side's turn the other side's next turn had it (the Feishu
|
|
369
|
+
card opened with "1 turn taken in"; VS Code showed the Feishu turn), and the chat file stayed
|
|
370
|
+
one line of entries with no fork (60 entries). A turn that ends within seconds is hard to hit
|
|
371
|
+
by hand; a turn waiting for an approval holds the lock as long as it waits.
|
|
372
|
+
|
|
373
|
+
Found and fixed: the line that says what another process wrote came as a notice of its own, 0.8 s
|
|
374
|
+
after the turn card was posted, so it stood below the answer; and it counted turns but said
|
|
375
|
+
"messages". The line now opens the turn card and says "turns".
|
|
376
|
+
|
|
377
|
+
## Not verified
|
|
378
|
+
|
|
379
|
+
What the two reviews of PR 158 left open (none of it blocks personal mode) is kept in
|
|
380
|
+
[`docs/AUDIT/REVIEW-PR158-FOLLOWUPS-2026-11-30.md`](https://github.com/zy1233/zycode/blob/main/docs/AUDIT/REVIEW-PR158-FOLLOWUPS-2026-11-30.md).
|
|
381
|
+
|
|
382
|
+
- **Slack and WeCom have not run against a live workspace or tenant.** Their adapters follow the
|
|
383
|
+
platforms' docs as read on 2026-11-29, and are tested with hand-built events and fake clients.
|
|
384
|
+
Open: the manifest above, the Socket Mode envelopes, Block Kit and `mrkdwn` as Slack shows them,
|
|
385
|
+
`chat.update` limits in a busy workspace; WeCom's stream and template-card bodies, the button
|
|
386
|
+
styles (taken as 1 primary, 2 plain, 3 danger), Markdown as the clients show it, whether stream
|
|
387
|
+
updates count against the 30 a minute, a group's `chatid` and @mention text.
|
|
388
|
+
- The credential manager on a real machine: the tests use one in memory (none of them may touch
|
|
389
|
+
the runner's own). Not tried: the Keychain prompt on macOS, Credential Manager on Windows, a
|
|
390
|
+
locked Secret Service collection on Linux.
|
|
391
|
+
- Lark (the international domain), Feishu on Linux or Windows, and the Feishu mobile apps.
|
|
392
|
+
- Groups: a bound channel, threads, @mentions and approvals sent to the owner's direct chat.
|
|
393
|
+
- Images in messages, the ❌ reaction (`CrossMark`), the approval timeout, `/new`, `/project`,
|
|
394
|
+
`/resume`, `/mode`, `/model`, `/thinking`, and an answer or a request too long for one card.
|
|
395
|
+
- Whether Feishu previews links inside a card; the gateway neutralizes links either way.
|
|
396
|
+
- CardKit: the 10-minute end of streaming mode and turning it on again (no turn ran that
|
|
397
|
+
long), a refused sequence (`300317`), the scope name `cardkit:card:write` on Lark, and how the
|
|
398
|
+
mobile apps show the typing.
|
|
399
|
+
- Windows: the credentials file is not checked for permissions there (ACLs), and the login-shell
|
|
400
|
+
environment is not read (the gateway's own environment is used). The tests of the gateway and
|
|
401
|
+
of the session lock run in CI on Windows and macOS since the review of PR 158.
|
|
402
|
+
- WeCom: a stream is closed and a new one begins after 5 minutes (the doc says 10; two other
|
|
403
|
+
sources say about 6); `from.userid` may be an encrypted id unless the bot's creator is a super
|
|
404
|
+
admin, so `users` and `approvers` would need that form; the tenant check never refuses there
|
|
405
|
+
(the tenant is the bot id).
|
|
406
|
+
- One writer per session (X11) is tested with real agent processes on Linux and macOS, and live
|
|
407
|
+
on macOS with VS Code (extension 1.14.0 with the agent of this tree) against the gateway, both
|
|
408
|
+
ways. Not with `zypi`, and not on Windows (the lock is `msvcrt.locking` there). An
|
|
409
|
+
agent of 1.2.0 or older (the one in VS Code extension 1.14.0) does not take the lock: with it
|
|
410
|
+
on the other side, do not continue one session in two places at once.
|
|
411
|
+
|
|
412
|
+
## Tests
|
|
413
|
+
|
|
414
|
+
```bash
|
|
415
|
+
pytest packages/pi-chat-gateway/tests
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
The tests start real agents with the mock LLM and with the scripted tool agent of
|
|
419
|
+
`packages/pi-agent-cli/tests`; nothing goes to the network. The Feishu, Slack and WeCom tests
|
|
420
|
+
need their SDKs (`lark-oapi`, `slack-sdk`, `wecom-aibot-python-sdk`) and are skipped without
|
|
421
|
+
them.
|