qw_agent: full tool-use harness (10 tools, run/chat/serve, parallel calls, --root/--yolo)

This commit is contained in:
ENI
2026-11-21 00:20:21 +02:00
parent 9b36b028cb
commit 029931fb73
3 changed files with 462 additions and 136 deletions
+75 -22
View File
@@ -1,3 +1,12 @@
# qwenwork-autoreg
Reversed QwenWork web API (`qwenwork.ai`): HTTP registration, JWT pool, OpenAI-compatible shim for
the new-api gateway, and a local tool-use agent that gives the PC to any model.
* `farm.py` — registration/login over plain HTTP (Aliyun captcha via 2captcha + IMAP code).
* `qw_shim.py` — OpenAI-compatible shim over a JWT pool (`/v1/chat/completions`, 13 models).
* `qw_agent.py` — tool-use harness: 10 tools with real filesystem/shell access on the host.
* `api.md` — 40+ endpoints with observed statuses and the session event schema.
## Tool use / local PC access
@@ -8,35 +17,66 @@ provider is pinned to DashScope inside the build: `--auth-type openai` plus
request to `dashscope.aliyuncs.com` and return `401 Incorrect API key provided` for any foreign key.
So the desktop harness cannot be repointed at an arbitrary endpoint.
Instead `qw_agent.py` gives the same result with full control — an OpenAI-compatible tool loop over
this PC: `fs_read`, `fs_write`, `fs_list`, `fs_grep`, `shell`.
`qw_agent.py` gives the same result with full control — an OpenAI-compatible tool loop over this PC:
| tool | what it does |
|------|--------------|
| `fs_read` | read a file with line numbers (offset/limit) |
| `fs_write` | write/overwrite a file, creates dirs |
| `fs_edit` | exact-substring replace inside a file |
| `fs_list` | directory listing with sizes |
| `fs_glob` | find files by glob (`**/*.py`) |
| `fs_grep` | regex search inside files, `file:line:text` |
| `shell` | run any command on the host (git-bash), returns exit/stdout/stderr |
| `http_get` | fetch a URL, status + first 20k chars |
| `todo_write` | keep a plan checklist |
| `task_done` | finish and return the final answer |
Modes:
```bash
# any OpenAI-compatible backend, e.g. the qwenwork shim when the pool has credits
python qw_agent.py --base http://127.0.0.1:48981/v1 --model qw-flash \
--prompt "read C:/Users/User/tmp/qwrepo/README.md and report total_lines"
# one-shot task
python qw_agent.py run --provider router -p "grep 'def main' in this repo and count lines"
# interactive REPL (history kept with --session chat.json)
python qw_agent.py chat --session chat.json
# OpenAI-compatible server: the "model" executes the whole tool loop on this PC
python qw_agent.py serve --port 48970 --yolo
curl -s localhost:48970/v1/chat/completions -H 'Content-Type: application/json' \
-d '{"messages":[{"role":"user","content":"how many lines in qw_agent.py?"}]}'
```
Proof run (local router, `deepseek-v4.1-flash-1017`, 3 turns):
Backends: `--provider qw` (qwenwork shim, `http://127.0.0.1:48981/v1`, `qw-flash`),
`--provider router` (local DashScope rotator, `http://127.0.0.1:16432/v1`),
`--provider any --base ... --model ... --key ...` for anything OpenAI-compatible.
Safety: destructive shell commands (`rm -rf`, `mkfs`, `diskpart`, ...) ask for confirmation unless
`--yolo`; `--root DIR` confines all file tools to one directory.
Proof run — independent steps issued in parallel, model self-corrects a zero-hit grep
(backend `deepseek-v4.1-flash-1017`, 17.8 s):
```
[assistant] ...
[tool] fs_read {'path': 'C:/Users/User/tmp/qwrepo/README.md'}
[tool] shell {'command': 'wc -l C:/Users/User/tmp/qwrepo/README.md'}
[tool] fs_read {'path': '...', 'offset': 70}
result: tools_used = ["fs_read", "shell", "fs_read"] -> answer "total_lines = 79"
[assistant] I'll run the independent first steps in parallel.
[tool] fs_grep {"path": "C:/Users/User/tmp/qwrepo", "pattern": "def task_done", "glob": "*.py"}
[tool] shell {"command": "wc -l C:/Users/User/tmp/qwrepo/qw_agent.py"}
[tool] fs_write {"path": "C:/Users/User/tmp/qwtest/note.txt", "content": "hello qw"}
[tool] fs_edit {"path": "C:/Users/User/tmp/qwtest/note.txt", "old": "hello", "new": "world"}
[tool] fs_read {"path": "C:/Users/User/tmp/qwtest/note.txt"}
[tool] task_done "grep = 0 literal hits (only `def t_task_done` at line 245); wc -l = 478;
note.txt after edit = \"world qw\""
turns 4 · tools_used 9
```
The model reads the real filesystem and runs real commands on the host: that is the "PC visible"
part, without depending on the closed desktop client.
{"code":"ok"}`.
**`captcha_token` must be the whole 2captcha `solution` object serialised as a JSON string** —
an object body gives 400, `solution.data` alone gives 403 `human verification failed`.
3. read the 6-digit code from the mailbox over IMAP (qwenwork mail, `used_codes.txt` guards against
reusing someone else's code inside the same minute).
4. `POST /auth/local/register {email, code, notification:false}` -> `{"created":true,"user":{...}}`,
JWT lands in cookie `token` (403 chars).
5. Existing address -> `409 email-exists`; use `POST /auth/local/login {email, code}`.
Serve proof:
```
GET /healthz {"ok": true, "model": "deepseek-v4.1-flash-1017", "tools": 10}
GET /v1/models {"data": [{"id": "deepseek-v4.1-flash-1017-agent"}]}
POST /v1/chat/completions {"choices": [...content "world qw"...], "x_qw_tools": ["fs_read","task_done"]}
```
## Registration
```bash
export QW_2CAP=~/.2cap_key QW_GMAIL=you@gmail.com QW_GMAIL_PASS=app-password
@@ -44,6 +84,14 @@ python farm.py you+qw1@gmail.com --register
python farm.py you+qw1@gmail.com --login # if the address already exists
```
Flow: `POST /auth/email-codes {email, purpose, captcha_token}` -> `{"code":"ok","data":{"retry_after_seconds":60}}`.
The `captcha_token` must be the whole 2captcha `solution` object serialised as a JSON **string** —
an object body gives 400, `solution.data` alone gives 403 `human verification failed`
(Aliyun Captcha v3: `sceneId=l47rwlyt`, `prefix=13n1x6`, `region=ga`).
Then read the 6-digit code over IMAP (`used_codes.txt` stops a second account from reusing the same
code inside one minute), `POST /auth/local/register {email, code, notification:false}` ->
`{"created":true,...}`, JWT lands in cookie `token` (403 chars).
## Shim
```bash
@@ -55,6 +103,10 @@ curl -s localhost:48981/v1/chat/completions -H 'Content-Type: application/json'
`402`/`401`/`403` from upstream puts the account on a 15-minute cooldown and the request is retried
on the next account. `mode` is mapped from the OpenAI model name; unknown names -> 404.
Models exposed (all 1M context): `kimi-k3.1-pro`, `qwen3.8-max`, `qwen4-max`, `glm-5.3`,
`gpt-6.1-sol`, `deepseek-v4.1-flash`, `deepseek-flash`, `glm-5.3-flash`, `minimax-m3.1-flash`,
`qwen3.8-flash` plus aliases `flash`/`advanced`/`pro`.
## Known blocker: credit hold
When a chat session is created, QwenWork **blocks the whole remaining balance** on that account
@@ -74,4 +126,5 @@ returned no text. Proper fix is the `/api/chat-ws` protobuf session lifecycle.
- Starter credits stopped being granted after ~2026-11-20 20:50 UTC in our tests; accounts created
later report `remaining 0, exceeded true` and `402 INSUFFICIENT_RESOURCES` on session start.
- Google OAuth entry exists: `/auth/google` -> accounts.google.com, `redirect_uri=/oauth-callback/google`,
client_id `376662729962-6p89qbb0nd7e2vqr2da3jd7315mbnvdn`.
client_id `376662729962-6p89qbb0nd7e2vqr2da3jd7315mbnvdn`.
- MIT, 2026 Gothbreach. No credentials in the repo; everything is env-driven.