mslxdff 0.1.45 → 0.1.54
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/README.md +162 -162
- package/bin/mslxdff.js +109 -11
- package/docs/adr/0001-reasoning-content-injection.md +13 -13
- package/docs/adr/0002-models-free-filter.md +11 -11
- package/docs/adr/0003-zero-state-no-auth.md +9 -9
- package/docs/adr/0004-bearer-token.md +17 -17
- package/docs/agents/domain.md +50 -50
- package/docs/agents/issue-tracker.md +29 -29
- package/docs/agents/triage-labels.md +14 -14
- package/package.json +1 -1
- package/src/daemon.js +84 -84
- package/src/logs.js +41 -2
- package/src/models.js +127 -127
- package/src/reasoning.js +32 -32
- package/src/routes/chat/broadband-handler.js +88 -0
- package/src/routes/chat/exhausted-handler.js +41 -0
- package/src/routes/chat/hedge-handler.js +142 -0
- package/src/routes/chat/index.js +178 -0
- package/src/routes/chat/local-handler.js +88 -0
- package/src/routes/chat/peer-handler.js +58 -0
- package/src/routes/chat.js +3 -342
- package/src/routes/hedge.js +251 -0
- package/src/routes/peers.js +49 -15
- package/src/server.js +57 -57
- package/src/state.js +272 -152
- package/src/sync-workbuddy.js +104 -0
- package/src/upstream.js +311 -212
package/README.md
CHANGED
|
@@ -1,162 +1,162 @@
|
|
|
1
|
-
# mslxdff
|
|
2
|
-
|
|
3
|
-
测试项目,请勿使用。
|
|
4
|
-
|
|
5
|
-
Zero runtime dependencies: Node ≥ 20, built-in `node:http`, `node:crypto`, `node:test`.
|
|
6
|
-
|
|
7
|
-
## What it does
|
|
8
|
-
|
|
9
|
-
- `POST /v1/chat/completions` — forwards your OpenAI-format request to the upstream (with reasoning-content injection for thinking-mode DeepSeek/Kimi models), streams SSE back chunk-by-chunk, or passes through JSON for non-streaming calls.
|
|
10
|
-
- `GET /v1/models` — the ~7 free models (`*-free` plus `big-pickle`), filtered from the full upstream list, cached and refreshed in the background every 2 hours.
|
|
11
|
-
- `GET /health` — public liveness check.
|
|
12
|
-
|
|
13
|
-
### `auto` model
|
|
14
|
-
|
|
15
|
-
Omitting `model` (or passing `"auto"`) picks a free model automatically, DeepSeek first. On an upstream error the next candidate is tried in the same request; each model's last-error timestamp is recorded (persisted in the state file) so the most reliably-available model is preferred next time.
|
|
16
|
-
|
|
17
|
-
### Fallback for a specific model
|
|
18
|
-
|
|
19
|
-
Pointing at a specific model (e.g. `deepseek-v4-flash-free`) still gets failover: if that model errors, the request automatically falls through to the next free model so your task isn't interrupted, and the error is recorded. The model enters a cooldown window (default 60s, `MSLXDFF_MODEL_COOLDOWN_MS`); during cooldown the backup models are used directly, and afterwards your model is tried again — once it succeeds it keeps being used until it errors again.
|
|
20
|
-
|
|
21
|
-
## Install & run
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
npm install # no deps actually fetched; just links the bin
|
|
25
|
-
mslxdff # or: node bin/mslxdff.js
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
First run generates a bearer token, writes it to the state file, and prints it:
|
|
29
|
-
|
|
30
|
-
```
|
|
31
|
-
mslxdff listening on http://localhost:8989
|
|
32
|
-
auth token: 9b5de021e914...
|
|
33
|
-
endpoint: http://localhost:8989/v1
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Port
|
|
37
|
-
|
|
38
|
-
Default port is **8989**. Persist a different port (and hot-restart the daemon onto it if one is running):
|
|
39
|
-
|
|
40
|
-
```
|
|
41
|
-
mslxdff -port 8000 # set port to 8000; restarts the daemon on 8000
|
|
42
|
-
mslxdff -d # next starts reuse the persisted port (8000)
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Priority: `-port` arg > persisted port > `PORT` env > default `8989`.
|
|
46
|
-
|
|
47
|
-
### Daemon (background, stays resident)
|
|
48
|
-
|
|
49
|
-
Just run `mslxdff` (no args): if no daemon is running yet, it **starts a background
|
|
50
|
-
daemon and exits** — the command never holds your terminal, so `npx mslxdff` works
|
|
51
|
-
the same way. If a daemon is already up, the bare command shows status + help.
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
mslxdff # starts the background daemon (if none running); exits immediately
|
|
55
|
-
mslxdff -d # same, explicit
|
|
56
|
-
mslxdff -stop # stop it
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Logs go to `~/.config/mslxdff/daemon.log`, the daemon pid to `daemon.pid` (both overridable via `MSLXDFF_DAEMON_DIR`). The daemon keeps running after your shell exits.
|
|
60
|
-
|
|
61
|
-
### Status
|
|
62
|
-
|
|
63
|
-
Running `mslxdff` with no args shows a status panel when a daemon is already up: joined groups with their members, failover targets, the free model list, the last 5 calls (model/status/latency), and the most recent error (`mslxdff -status` for status only, `mslxdff -help` for the full command reference). Call and error history are stored as JSON-lines at `calls.log` / `errors.log` in the state dir.
|
|
64
|
-
|
|
65
|
-
```
|
|
66
|
-
$ mslxdff -status
|
|
67
|
-
mslxdff v0.1.2
|
|
68
|
-
daemon: running (pid 12345)
|
|
69
|
-
endpoint: http://localhost:8989/v1
|
|
70
|
-
log dir: C:/Users/you/.config/mslxdff
|
|
71
|
-
|
|
72
|
-
models (7 free):
|
|
73
|
-
big-pickle
|
|
74
|
-
deepseek-v4-flash-free
|
|
75
|
-
...
|
|
76
|
-
|
|
77
|
-
recent calls:
|
|
78
|
-
08-03 12:00:03 deepseek-v4-flash-free 200 812ms
|
|
79
|
-
...
|
|
80
|
-
|
|
81
|
-
last error:
|
|
82
|
-
08-03 11:59:58 deepseek-v4-flash-free 429 upstream 429
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### Update
|
|
86
|
-
|
|
87
|
-
```
|
|
88
|
-
mslxdff -update # install the latest published version; restarts a running daemon
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Groups (join several machines by name)
|
|
92
|
-
|
|
93
|
-
Run mslxdff on several machines (e.g. locally + a VPS) and join them into a named group — **the group name is the password**. When the local upstream fails for a model, the request is forwarded to a group member running the **same model** — your task keeps the model it asked for, just served from another machine with its own upstream quota.
|
|
94
|
-
|
|
95
|
-
```
|
|
96
|
-
# on the leader machine B (must be reachable from the others):
|
|
97
|
-
mslxdff -creategroup mygroup # group name IS the password, no address needed
|
|
98
|
-
|
|
99
|
-
# on machine A (and C, D, …) — give the leader's host; your own address is auto-registered:
|
|
100
|
-
mslxdff -addtogroup <B-ip> mygroup
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
- Anyone who knows the group name can join — pick something unguessable, like `mygroup@mslxd-7f3k`. The leader's first joiner seeds the leader's own entry from the address it connects from, so `-creategroup` takes no address argument (default port 8989; `-addtogroup` also accepts `host:port`).
|
|
104
|
-
- Joining registers your node with the leader (membership is keyed by your bearer token) and immediately pulls the member list into local failover targets.
|
|
105
|
-
- Membership is persisted and re-synced on every start + every `MSLXDFF_GROUP_SYNC_MS` (default 60s), so a newly joined machine becomes a failover target on all nodes automatically.
|
|
106
|
-
- Local-first: every request tries the local upstream before any group member. On local failure, members are tried round-robin for the same model; a member that just failed is skipped for `MSLXDFF_PEER_COOLDOWN_MS` (default 30s).
|
|
107
|
-
- Forwarded requests carry a hop limit (`MSLXDFF_MAX_HOPS`, default 3) to prevent forwarding loops, and a model-lock so the member doesn't switch models. A→B→C chaining works as long as each member knows the others.
|
|
108
|
-
- Wrong group names (or wrong tokens) are counted per source IP: `MSLXDFF_BAN_THRESHOLD` (default 5) failures ban the IP for `MSLXDFF_BAN_WINDOW_MS` (default 48h). `mslxdff -resetban [ip]` clears bans (all, or one IP).
|
|
109
|
-
- `mslxdff -group sync` pulls once manually; `mslxdff -group leave <name>` detaches from a group and clears its members; `mslxdff -group list` shows groups. See `docs/adr/0005-peer-mesh.md`.
|
|
110
|
-
|
|
111
|
-
```
|
|
112
|
-
mslxdff -showtoken # print the current token (creates one on first use)
|
|
113
|
-
mslxdff -refresh-token # rotate it (prints the new token, does not start the server)
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## Client configuration
|
|
117
|
-
|
|
118
|
-
Point any OpenAI-compatible client at the endpoint with the token:
|
|
119
|
-
|
|
120
|
-
```
|
|
121
|
-
Endpoint: http://localhost:8989/v1
|
|
122
|
-
API Key: <the bearer token> (sent as Authorization: Bearer <token>)
|
|
123
|
-
Model: oc/deepseek-v4-flash-free (the oc/ prefix is optional)
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
<x-model list>
|
|
127
|
-
|
|
128
|
-
```
|
|
129
|
-
$ curl -H "Authorization: Bearer <token>" http://localhost:8989/v1/models
|
|
130
|
-
{"object":"list","data":[{"id":"big-pickle",...},{"id":"deepseek-v4-flash-free",...}, ...]}
|
|
131
|
-
</x-model list>
|
|
132
|
-
|
|
133
|
-
## Environment variables
|
|
134
|
-
|
|
135
|
-
| Variable | Default | Purpose |
|
|
136
|
-
|---|---|---|
|
|
137
|
-
| `PORT` | `8989` | listen port (used when no `-port` arg and no persisted port) |
|
|
138
|
-
| `MSLXDFF_STATE_FILE` | `~/.config/mslxdff/state.json` | token/port state file (mode 0600) |
|
|
139
|
-
| `MSLXDFF_DAEMON_DIR` | `~/.config/mslxdff` | daemon pid + log directory |
|
|
140
|
-
| `UPSTREAM_BASE_URL` | `https://opencode.ai` | upstream base |
|
|
141
|
-
| `UPSTREAM_AUTH_TOKEN` | `public` | upstream `Authorization: Bearer <…>` value |
|
|
142
|
-
| `UPSTREAM_CONNECT_TIMEOUT_MS` | `30000` | upstream connect timeout |
|
|
143
|
-
| `LOG_LEVEL` | `info` | (reserved) |
|
|
144
|
-
| `MODELS_REFRESH_MS` | `7200000` | background model-list refresh interval (2h) |
|
|
145
|
-
| `MSLXDFF_MODEL_COOLDOWN_MS` | `60000` | fallback cooldown after a model error |
|
|
146
|
-
| `MSLXDFF_PEER_COOLDOWN_MS` | `30000` | peer failover cooldown |
|
|
147
|
-
| `MSLXDFF_GROUP_SYNC_MS` | `60000` | group membership sync interval |
|
|
148
|
-
| `MSLXDFF_MAX_HOPS` | `3` | max peer-forwarding depth |
|
|
149
|
-
| `MSLXDFF_BAN_THRESHOLD` | `5` | failed joins before an IP is banned |
|
|
150
|
-
| `MSLXDFF_BAN_WINDOW_MS` | `172800000` | ban duration after too many join failures (48h) |
|
|
151
|
-
|
|
152
|
-
## Clients
|
|
153
|
-
|
|
154
|
-
Point your OpenAI client at `http://<host>:8989/v1` (or the equivalent config seen above). Works for streaming and non-streaming chat completions.
|
|
155
|
-
|
|
156
|
-
## Development
|
|
157
|
-
|
|
158
|
-
```
|
|
159
|
-
npm test # node --test, no network access
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
The reference implementation is 9Router v0.5.45 (`/root/9router`); see `CLAUDE.md`, `CONTEXT.md`, and `docs/adr/` for the contract and the decisions behind it.
|
|
1
|
+
# mslxdff
|
|
2
|
+
|
|
3
|
+
测试项目,请勿使用。
|
|
4
|
+
|
|
5
|
+
Zero runtime dependencies: Node ≥ 20, built-in `node:http`, `node:crypto`, `node:test`.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- `POST /v1/chat/completions` — forwards your OpenAI-format request to the upstream (with reasoning-content injection for thinking-mode DeepSeek/Kimi models), streams SSE back chunk-by-chunk, or passes through JSON for non-streaming calls.
|
|
10
|
+
- `GET /v1/models` — the ~7 free models (`*-free` plus `big-pickle`), filtered from the full upstream list, cached and refreshed in the background every 2 hours.
|
|
11
|
+
- `GET /health` — public liveness check.
|
|
12
|
+
|
|
13
|
+
### `auto` model
|
|
14
|
+
|
|
15
|
+
Omitting `model` (or passing `"auto"`) picks a free model automatically, DeepSeek first. On an upstream error the next candidate is tried in the same request; each model's last-error timestamp is recorded (persisted in the state file) so the most reliably-available model is preferred next time.
|
|
16
|
+
|
|
17
|
+
### Fallback for a specific model
|
|
18
|
+
|
|
19
|
+
Pointing at a specific model (e.g. `deepseek-v4-flash-free`) still gets failover: if that model errors, the request automatically falls through to the next free model so your task isn't interrupted, and the error is recorded. The model enters a cooldown window (default 60s, `MSLXDFF_MODEL_COOLDOWN_MS`); during cooldown the backup models are used directly, and afterwards your model is tried again — once it succeeds it keeps being used until it errors again.
|
|
20
|
+
|
|
21
|
+
## Install & run
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
npm install # no deps actually fetched; just links the bin
|
|
25
|
+
mslxdff # or: node bin/mslxdff.js
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
First run generates a bearer token, writes it to the state file, and prints it:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
mslxdff listening on http://localhost:8989
|
|
32
|
+
auth token: 9b5de021e914...
|
|
33
|
+
endpoint: http://localhost:8989/v1
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Port
|
|
37
|
+
|
|
38
|
+
Default port is **8989**. Persist a different port (and hot-restart the daemon onto it if one is running):
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
mslxdff -port 8000 # set port to 8000; restarts the daemon on 8000
|
|
42
|
+
mslxdff -d # next starts reuse the persisted port (8000)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Priority: `-port` arg > persisted port > `PORT` env > default `8989`.
|
|
46
|
+
|
|
47
|
+
### Daemon (background, stays resident)
|
|
48
|
+
|
|
49
|
+
Just run `mslxdff` (no args): if no daemon is running yet, it **starts a background
|
|
50
|
+
daemon and exits** — the command never holds your terminal, so `npx mslxdff` works
|
|
51
|
+
the same way. If a daemon is already up, the bare command shows status + help.
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
mslxdff # starts the background daemon (if none running); exits immediately
|
|
55
|
+
mslxdff -d # same, explicit
|
|
56
|
+
mslxdff -stop # stop it
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Logs go to `~/.config/mslxdff/daemon.log`, the daemon pid to `daemon.pid` (both overridable via `MSLXDFF_DAEMON_DIR`). The daemon keeps running after your shell exits.
|
|
60
|
+
|
|
61
|
+
### Status
|
|
62
|
+
|
|
63
|
+
Running `mslxdff` with no args shows a status panel when a daemon is already up: joined groups with their members, failover targets, the free model list, the last 5 calls (model/status/latency), and the most recent error (`mslxdff -status` for status only, `mslxdff -help` for the full command reference). Call and error history are stored as JSON-lines at `calls.log` / `errors.log` in the state dir.
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
$ mslxdff -status
|
|
67
|
+
mslxdff v0.1.2
|
|
68
|
+
daemon: running (pid 12345)
|
|
69
|
+
endpoint: http://localhost:8989/v1
|
|
70
|
+
log dir: C:/Users/you/.config/mslxdff
|
|
71
|
+
|
|
72
|
+
models (7 free):
|
|
73
|
+
big-pickle
|
|
74
|
+
deepseek-v4-flash-free
|
|
75
|
+
...
|
|
76
|
+
|
|
77
|
+
recent calls:
|
|
78
|
+
08-03 12:00:03 deepseek-v4-flash-free 200 812ms
|
|
79
|
+
...
|
|
80
|
+
|
|
81
|
+
last error:
|
|
82
|
+
08-03 11:59:58 deepseek-v4-flash-free 429 upstream 429
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Update
|
|
86
|
+
|
|
87
|
+
```
|
|
88
|
+
mslxdff -update # install the latest published version; restarts a running daemon
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Groups (join several machines by name)
|
|
92
|
+
|
|
93
|
+
Run mslxdff on several machines (e.g. locally + a VPS) and join them into a named group — **the group name is the password**. When the local upstream fails for a model, the request is forwarded to a group member running the **same model** — your task keeps the model it asked for, just served from another machine with its own upstream quota.
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
# on the leader machine B (must be reachable from the others):
|
|
97
|
+
mslxdff -creategroup mygroup # group name IS the password, no address needed
|
|
98
|
+
|
|
99
|
+
# on machine A (and C, D, …) — give the leader's host; your own address is auto-registered:
|
|
100
|
+
mslxdff -addtogroup <B-ip> mygroup
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
- Anyone who knows the group name can join — pick something unguessable, like `mygroup@mslxd-7f3k`. The leader's first joiner seeds the leader's own entry from the address it connects from, so `-creategroup` takes no address argument (default port 8989; `-addtogroup` also accepts `host:port`).
|
|
104
|
+
- Joining registers your node with the leader (membership is keyed by your bearer token) and immediately pulls the member list into local failover targets.
|
|
105
|
+
- Membership is persisted and re-synced on every start + every `MSLXDFF_GROUP_SYNC_MS` (default 60s), so a newly joined machine becomes a failover target on all nodes automatically.
|
|
106
|
+
- Local-first: every request tries the local upstream before any group member. On local failure, members are tried round-robin for the same model; a member that just failed is skipped for `MSLXDFF_PEER_COOLDOWN_MS` (default 30s).
|
|
107
|
+
- Forwarded requests carry a hop limit (`MSLXDFF_MAX_HOPS`, default 3) to prevent forwarding loops, and a model-lock so the member doesn't switch models. A→B→C chaining works as long as each member knows the others.
|
|
108
|
+
- Wrong group names (or wrong tokens) are counted per source IP: `MSLXDFF_BAN_THRESHOLD` (default 5) failures ban the IP for `MSLXDFF_BAN_WINDOW_MS` (default 48h). `mslxdff -resetban [ip]` clears bans (all, or one IP).
|
|
109
|
+
- `mslxdff -group sync` pulls once manually; `mslxdff -group leave <name>` detaches from a group and clears its members; `mslxdff -group list` shows groups. See `docs/adr/0005-peer-mesh.md`.
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
mslxdff -showtoken # print the current token (creates one on first use)
|
|
113
|
+
mslxdff -refresh-token # rotate it (prints the new token, does not start the server)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Client configuration
|
|
117
|
+
|
|
118
|
+
Point any OpenAI-compatible client at the endpoint with the token:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
Endpoint: http://localhost:8989/v1
|
|
122
|
+
API Key: <the bearer token> (sent as Authorization: Bearer <token>)
|
|
123
|
+
Model: oc/deepseek-v4-flash-free (the oc/ prefix is optional)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
<x-model list>
|
|
127
|
+
|
|
128
|
+
```
|
|
129
|
+
$ curl -H "Authorization: Bearer <token>" http://localhost:8989/v1/models
|
|
130
|
+
{"object":"list","data":[{"id":"big-pickle",...},{"id":"deepseek-v4-flash-free",...}, ...]}
|
|
131
|
+
</x-model list>
|
|
132
|
+
|
|
133
|
+
## Environment variables
|
|
134
|
+
|
|
135
|
+
| Variable | Default | Purpose |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| `PORT` | `8989` | listen port (used when no `-port` arg and no persisted port) |
|
|
138
|
+
| `MSLXDFF_STATE_FILE` | `~/.config/mslxdff/state.json` | token/port state file (mode 0600) |
|
|
139
|
+
| `MSLXDFF_DAEMON_DIR` | `~/.config/mslxdff` | daemon pid + log directory |
|
|
140
|
+
| `UPSTREAM_BASE_URL` | `https://opencode.ai` | upstream base |
|
|
141
|
+
| `UPSTREAM_AUTH_TOKEN` | `public` | upstream `Authorization: Bearer <…>` value |
|
|
142
|
+
| `UPSTREAM_CONNECT_TIMEOUT_MS` | `30000` | upstream connect timeout |
|
|
143
|
+
| `LOG_LEVEL` | `info` | (reserved) |
|
|
144
|
+
| `MODELS_REFRESH_MS` | `7200000` | background model-list refresh interval (2h) |
|
|
145
|
+
| `MSLXDFF_MODEL_COOLDOWN_MS` | `60000` | fallback cooldown after a model error |
|
|
146
|
+
| `MSLXDFF_PEER_COOLDOWN_MS` | `30000` | peer failover cooldown |
|
|
147
|
+
| `MSLXDFF_GROUP_SYNC_MS` | `60000` | group membership sync interval |
|
|
148
|
+
| `MSLXDFF_MAX_HOPS` | `3` | max peer-forwarding depth |
|
|
149
|
+
| `MSLXDFF_BAN_THRESHOLD` | `5` | failed joins before an IP is banned |
|
|
150
|
+
| `MSLXDFF_BAN_WINDOW_MS` | `172800000` | ban duration after too many join failures (48h) |
|
|
151
|
+
|
|
152
|
+
## Clients
|
|
153
|
+
|
|
154
|
+
Point your OpenAI client at `http://<host>:8989/v1` (or the equivalent config seen above). Works for streaming and non-streaming chat completions.
|
|
155
|
+
|
|
156
|
+
## Development
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
npm test # node --test, no network access
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The reference implementation is 9Router v0.5.45 (`/root/9router`); see `CLAUDE.md`, `CONTEXT.md`, and `docs/adr/` for the contract and the decisions behind it.
|
package/bin/mslxdff.js
CHANGED
|
@@ -8,8 +8,10 @@ import { DEFAULT_PORT, defaultStateFile } from "../src/state.js";
|
|
|
8
8
|
import { createRouter } from "../src/routes.js";
|
|
9
9
|
import { createUpstreamClient } from "../src/upstream.js";
|
|
10
10
|
import { createModelsService } from "../src/models.js";
|
|
11
|
-
import { loadToken, refreshToken, setPort, getPort, loadGroupsJoined, saveGroupsJoined, loadModelErrors, savePreferredModel } from "../src/state.js";
|
|
11
|
+
import { loadToken, refreshToken, setPort, getPort, loadGroupsJoined, saveGroupsJoined, loadModelErrors, savePreferredModel, loadPreferredModel } from "../src/state.js";
|
|
12
12
|
import { getPreferredModel } from "../src/auto.js";
|
|
13
|
+
import { normalizeModel } from "../src/reasoning.js";
|
|
14
|
+
import { syncToWorkbuddy, workbuddyModelsPath } from "../src/sync-workbuddy.js";
|
|
13
15
|
import { startDaemon, stopDaemon, writePid, pidFile, logFile, readPid, readPidVersion, isPidAlive } from "../src/daemon.js";
|
|
14
16
|
import { createAutoSelector } from "../src/auto.js";
|
|
15
17
|
import { createPeersService } from "../src/peers.js";
|
|
@@ -191,22 +193,43 @@ if (args.includes("-model") || args.includes("-models")) {
|
|
|
191
193
|
process.exit(1);
|
|
192
194
|
}
|
|
193
195
|
const cacheFile = join(logDir(), "models.json");
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const cached = readModelsCache(cacheFile);
|
|
198
|
-
if (cached) {
|
|
199
|
-
ids = (cached.data || []).map((m) => m.id).filter(Boolean);
|
|
200
|
-
cachedAt = cached.cachedAt || null;
|
|
201
|
-
} else {
|
|
196
|
+
// 主动刷新:每次执行都尝试拉取上游最新列表(4s 超时),成功则更新缓存,失败则回退到 stale
|
|
197
|
+
async function tryRefreshModels() {
|
|
198
|
+
try {
|
|
202
199
|
const models = createModelsService({
|
|
203
200
|
baseUrl: process.env.UPSTREAM_BASE_URL || "https://opencode.ai",
|
|
204
201
|
headers: createUpstreamClient({}).headers,
|
|
205
202
|
refreshMs: 0,
|
|
206
203
|
cacheFile,
|
|
207
204
|
});
|
|
208
|
-
|
|
209
|
-
|
|
205
|
+
// 4s 超时,避免阻塞
|
|
206
|
+
const list = await Promise.race([
|
|
207
|
+
models.get(),
|
|
208
|
+
new Promise((_, rej) => setTimeout(() => rej(new Error("refresh timeout")), 4000)),
|
|
209
|
+
]);
|
|
210
|
+
return list;
|
|
211
|
+
} catch {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
let ids = [];
|
|
217
|
+
let cachedAt = null;
|
|
218
|
+
let refreshed = null;
|
|
219
|
+
// 每次都主动尝试刷新
|
|
220
|
+
refreshed = await tryRefreshModels();
|
|
221
|
+
if (refreshed?.data) {
|
|
222
|
+
ids = (refreshed.data || []).map((m) => m.id).filter(Boolean);
|
|
223
|
+
cachedAt = refreshed.cachedAt || Date.now();
|
|
224
|
+
} else {
|
|
225
|
+
const cached = readModelsCache(cacheFile);
|
|
226
|
+
if (cached) {
|
|
227
|
+
ids = (cached.data || []).map((m) => m.id).filter(Boolean);
|
|
228
|
+
cachedAt = cached.cachedAt || null;
|
|
229
|
+
} else {
|
|
230
|
+
// 无缓存且刷新失败,尝试一次兜底 fetch(已在 tryRefreshModels 中尝试过,此处直接报错)
|
|
231
|
+
throw new Error("no cached models and refresh failed");
|
|
232
|
+
}
|
|
210
233
|
}
|
|
211
234
|
if (!ids.length) {
|
|
212
235
|
console.log("no models available — try: mslxdff -model refresh");
|
|
@@ -244,6 +267,74 @@ if (args.includes("-model") || args.includes("-models")) {
|
|
|
244
267
|
process.exit(0);
|
|
245
268
|
}
|
|
246
269
|
|
|
270
|
+
// -setto workbuddy [modelId]: set default model and sync to WorkBuddy models.json
|
|
271
|
+
if (args.includes("-setto") || args.includes("--setto")) {
|
|
272
|
+
const idx = args.findIndex((x) => x === "-setto" || x === "--setto");
|
|
273
|
+
const target = args[idx + 1];
|
|
274
|
+
if (target !== "workbuddy") {
|
|
275
|
+
console.error("usage: mslxdff -setto workbuddy [modelId]");
|
|
276
|
+
process.exit(1);
|
|
277
|
+
}
|
|
278
|
+
const raw = args[idx + 2] && !String(args[idx + 2]).startsWith("-") ? String(args[idx + 2]).trim() : null;
|
|
279
|
+
let id;
|
|
280
|
+
if (raw) {
|
|
281
|
+
if (raw === "auto" || !raw) {
|
|
282
|
+
console.error("modelId 不能为 auto 或空");
|
|
283
|
+
process.exit(1);
|
|
284
|
+
}
|
|
285
|
+
const norm = normalizeModel(raw);
|
|
286
|
+
if (!norm) {
|
|
287
|
+
console.error("modelId 不能为空");
|
|
288
|
+
process.exit(1);
|
|
289
|
+
}
|
|
290
|
+
savePreferredModel(norm);
|
|
291
|
+
console.log(`default model set to: ${norm} (daemon hot-reloads on next request)`);
|
|
292
|
+
id = norm;
|
|
293
|
+
} else {
|
|
294
|
+
id = loadPreferredModel() || getPreferredModel();
|
|
295
|
+
if (!id) {
|
|
296
|
+
console.error("no preferred model set; use: mslxdff -setto workbuddy <modelId>");
|
|
297
|
+
process.exit(1);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// 主动刷新模型列表(每次 -setto 都尝试,保证 deepseek 等最新模型可见;失败不阻断同步)
|
|
301
|
+
try {
|
|
302
|
+
const cacheFile = join(logDir(), "models.json");
|
|
303
|
+
const models = createModelsService({
|
|
304
|
+
baseUrl: process.env.UPSTREAM_BASE_URL || "https://opencode.ai",
|
|
305
|
+
headers: createUpstreamClient({}).headers,
|
|
306
|
+
refreshMs: 0,
|
|
307
|
+
cacheFile,
|
|
308
|
+
});
|
|
309
|
+
const fresh = await Promise.race([
|
|
310
|
+
models.get(),
|
|
311
|
+
new Promise((_, rej) => setTimeout(() => rej(new Error("refresh timeout")), 4000)),
|
|
312
|
+
]);
|
|
313
|
+
if (fresh?.data?.length) {
|
|
314
|
+
const ids = fresh.data.map((m) => m.id);
|
|
315
|
+
if (!ids.includes(id)) {
|
|
316
|
+
console.log(`warn: "${id}" not in current free list (${ids.length} models), still syncing to WorkBuddy`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
} catch {
|
|
320
|
+
// refresh failed, still proceed with sync using stale/cached list
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
const { token } = await loadToken();
|
|
324
|
+
const persisted = getPort();
|
|
325
|
+
const envPort = Number(process.env.MSLXDFF_PORT);
|
|
326
|
+
const port = persisted !== null ? persisted : (Number.isInteger(envPort) && envPort > 0 ? envPort : 8989);
|
|
327
|
+
const file = workbuddyModelsPath();
|
|
328
|
+
const r = await syncToWorkbuddy({ id, token, port, file });
|
|
329
|
+
console.log(`synced to WorkBuddy: ${r.action} "${id}" @ ${file}`);
|
|
330
|
+
console.log(` url: http://127.0.0.1:${port}/v1/chat/completions`);
|
|
331
|
+
} catch (err) {
|
|
332
|
+
console.error(`failed to sync to WorkBuddy: ${String(err?.message || err)}`);
|
|
333
|
+
process.exit(1);
|
|
334
|
+
}
|
|
335
|
+
process.exit(0);
|
|
336
|
+
}
|
|
337
|
+
|
|
247
338
|
// 交互式选择器:↑/↓ 移动,Enter 确认,q/Esc 取消;ANSI 原地重绘
|
|
248
339
|
async function pickInteractive(items, startCursor = 0) {
|
|
249
340
|
const { renderChooser, renderChooserHelp, parseKey } = await import("../src/chooser.js");
|
|
@@ -873,6 +964,11 @@ if (created) {
|
|
|
873
964
|
console.log(`auth token: ${token}`);
|
|
874
965
|
}
|
|
875
966
|
console.log(`endpoint: http://${host}:${addr.port}/v1`);
|
|
967
|
+
try {
|
|
968
|
+
const { hedgeDelayMs } = await import("../src/routes/hedge.js");
|
|
969
|
+
const hd = hedgeDelayMs();
|
|
970
|
+
console.log(`hedge: ${hd ? `${hd}ms` : "off"} (MSLXDFF_HEDGE_DELAY_MS)`);
|
|
971
|
+
} catch {}
|
|
876
972
|
|
|
877
973
|
// Periodically pull the freshest member lists for every joined group so a
|
|
878
974
|
// new member becomes a failover peer on all nodes without manual re-joining.
|
|
@@ -1177,6 +1273,7 @@ Usage:
|
|
|
1177
1273
|
mslxdff -update update mslxdff to the latest published version
|
|
1178
1274
|
mslxdff -showtoken print the current auth token
|
|
1179
1275
|
mslxdff -refresh-token rotate the auth token (prints the new one)
|
|
1276
|
+
mslxdff -setto workbuddy [modelId] set default model and sync to WorkBuddy models.json (insert or update 127.0.0.1/v1 entry)
|
|
1180
1277
|
mslxdff -creategroup <name> create a group on this node (the group name is the password)
|
|
1181
1278
|
mslxdff -addtogroup <leader-host> <name> [--broadband] join a group via its leader host (default port 8989) — broadband: 宽带动态IP成员(经Leader中继,无需公网入站,默认127.0.0.1)
|
|
1182
1279
|
mslxdff -group sync pull the freshest member list for all joined groups
|
|
@@ -1203,6 +1300,7 @@ Environment:
|
|
|
1203
1300
|
MSLXDFF_MAX_HOPS max peer-forwarding depth (default 3)
|
|
1204
1301
|
MSLXDFF_BAN_THRESHOLD failed joins before an ip is banned (default 5)
|
|
1205
1302
|
MSLXDFF_BAN_WINDOW_MS ban duration after too many failures (default 48h)
|
|
1303
|
+
MSLXDFF_HEDGE_DELAY_MS hedge peer race when local stream first chunk slow (default 1000, 0/off to disable)
|
|
1206
1304
|
MSLXDFF_AUTO_UPDATE auto-update: hourly by default, 0/off/false to disable, 1/true or ms
|
|
1207
1305
|
MSLXDFF_AUTO_UPDATE_MS same as above, explicit ms (overrides AUTO_UPDATE)
|
|
1208
1306
|
`);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
# ADR-0001: Inject a reasoning_content placeholder on outbound assistant messages
|
|
2
|
-
|
|
3
|
-
The Zen upstream's thinking-mode models (deepseek-family at minimum) return
|
|
4
|
-
`400 "The reasoning_content in the thinking mode must be passed back"` when a
|
|
5
|
-
multi-turn request echoes an assistant message without its `reasoning_content`.
|
|
6
|
-
Clients speaking plain OpenAI format never send that field, so the proxy writes
|
|
7
|
-
a `" "` placeholder into assistant messages before forwarding. Scope is `all`
|
|
8
|
-
for deepseek-family models and `tool_calls` for kimi-family models; messages
|
|
9
|
-
that already carry non-empty `reasoning_content` are left untouched.
|
|
10
|
-
|
|
11
|
-
The alternative — telling clients to manage `reasoning_content` themselves —
|
|
12
|
-
would break standard OpenAI-compatible clients, so the proxy eats this
|
|
13
|
-
compatibility cost instead. Matches `/root/9router` v0.5.45
|
|
1
|
+
# ADR-0001: Inject a reasoning_content placeholder on outbound assistant messages
|
|
2
|
+
|
|
3
|
+
The Zen upstream's thinking-mode models (deepseek-family at minimum) return
|
|
4
|
+
`400 "The reasoning_content in the thinking mode must be passed back"` when a
|
|
5
|
+
multi-turn request echoes an assistant message without its `reasoning_content`.
|
|
6
|
+
Clients speaking plain OpenAI format never send that field, so the proxy writes
|
|
7
|
+
a `" "` placeholder into assistant messages before forwarding. Scope is `all`
|
|
8
|
+
for deepseek-family models and `tool_calls` for kimi-family models; messages
|
|
9
|
+
that already carry non-empty `reasoning_content` are left untouched.
|
|
10
|
+
|
|
11
|
+
The alternative — telling clients to manage `reasoning_content` themselves —
|
|
12
|
+
would break standard OpenAI-compatible clients, so the proxy eats this
|
|
13
|
+
compatibility cost instead. Matches `/root/9router` v0.5.45
|
|
14
14
|
`open-sse/utils/reasoningContentInjector.js`.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
# ADR-0002: /v1/models exposes only free models, matched by suffix or whitelist
|
|
2
|
-
|
|
3
|
-
The upstream `/zen/v1/models` list contains ~60 models; exposing them all would
|
|
4
|
-
pollute clients with paid models this proxy can't serve for free. `/v1/models`
|
|
5
|
-
therefore filters to: `id` ending in `-free`, OR the explicit whitelist entry
|
|
6
|
-
`big-pickle`. The whitelist exists because `big-pickle` is a free model without
|
|
7
|
-
the `-free` suffix, and a suffix-only filter would silently drop it.
|
|
8
|
-
|
|
9
|
-
A plain `endsWith("-free")` filter was considered and rejected for exactly that
|
|
10
|
-
reason. Matches `/root/9router` v0.5.45
|
|
11
|
-
`src/app/api/providers/suggested-models/filters.js`
|
|
1
|
+
# ADR-0002: /v1/models exposes only free models, matched by suffix or whitelist
|
|
2
|
+
|
|
3
|
+
The upstream `/zen/v1/models` list contains ~60 models; exposing them all would
|
|
4
|
+
pollute clients with paid models this proxy can't serve for free. `/v1/models`
|
|
5
|
+
therefore filters to: `id` ending in `-free`, OR the explicit whitelist entry
|
|
6
|
+
`big-pickle`. The whitelist exists because `big-pickle` is a free model without
|
|
7
|
+
the `-free` suffix, and a suffix-only filter would silently drop it.
|
|
8
|
+
|
|
9
|
+
A plain `endsWith("-free")` filter was considered and rejected for exactly that
|
|
10
|
+
reason. Matches `/root/9router` v0.5.45
|
|
11
|
+
`src/app/api/providers/suggested-models/filters.js`
|
|
12
12
|
(`KNOWN_FREE_OPENCODE_MODELS = ["big-pickle"]`).
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# ADR-0003: Zero-state, no-DB, no-account local proxy
|
|
2
|
-
|
|
3
|
-
Status: partially superseded by [ADR-0004](./0004-bearer-token.md) — the
|
|
4
|
-
no-auth clause below is replaced; the zero-DB / no-account / no-cloud principles
|
|
5
|
-
stand.
|
|
6
|
-
|
|
7
|
-
By design this proxy holds no database, no token store, no account rotation,
|
|
8
|
-
and no cloud sync. A single static bearer token is the only credential, kept
|
|
9
|
-
in a 0600 state file (see ADR-0004); everything else is stateless per-process
|
|
1
|
+
# ADR-0003: Zero-state, no-DB, no-account local proxy
|
|
2
|
+
|
|
3
|
+
Status: partially superseded by [ADR-0004](./0004-bearer-token.md) — the
|
|
4
|
+
no-auth clause below is replaced; the zero-DB / no-account / no-cloud principles
|
|
5
|
+
stand.
|
|
6
|
+
|
|
7
|
+
By design this proxy holds no database, no token store, no account rotation,
|
|
8
|
+
and no cloud sync. A single static bearer token is the only credential, kept
|
|
9
|
+
in a 0600 state file (see ADR-0004); everything else is stateless per-process
|
|
10
10
|
memory at most.
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
# ADR-0004: Single static bearer token, persisted in a 0600 state file
|
|
2
|
-
|
|
3
|
-
The proxy requires a bearer token on `/v1/*` so an accidentally-exposed port
|
|
4
|
-
isn't an open relay. There is no account system: the token is a random
|
|
5
|
-
`crypto` 32-byte value (hex), generated once on first run, persisted to a
|
|
6
|
-
state file (default `~/.config/mslxdff/state.json`, `0600`, path overridable
|
|
7
|
-
via `MSLXDFF_STATE_FILE`), and printed to stdout on creation. Rotate with
|
|
8
|
-
`mslxdff -refresh-token`, which regenerates, rewrites the file, prints the
|
|
9
|
-
new token, and exits (does not start the server).
|
|
10
|
-
|
|
11
|
-
Auth is enforced with a constant-time string compare on
|
|
12
|
-
`Authorization: Bearer <token>`; mismatches get `401` with `WWW-Authenticate`.
|
|
13
|
-
`/health` stays public (no token). Tokens never appear in logs.
|
|
14
|
-
|
|
15
|
-
Alternatives rejected: a fixed default token (same key on every install),
|
|
16
|
-
per-user accounts (needs a DB — that's the 9Router provisioning surface we
|
|
17
|
-
rejected in ADR-0003), and unauthenticated local-only binding (fragile;
|
|
1
|
+
# ADR-0004: Single static bearer token, persisted in a 0600 state file
|
|
2
|
+
|
|
3
|
+
The proxy requires a bearer token on `/v1/*` so an accidentally-exposed port
|
|
4
|
+
isn't an open relay. There is no account system: the token is a random
|
|
5
|
+
`crypto` 32-byte value (hex), generated once on first run, persisted to a
|
|
6
|
+
state file (default `~/.config/mslxdff/state.json`, `0600`, path overridable
|
|
7
|
+
via `MSLXDFF_STATE_FILE`), and printed to stdout on creation. Rotate with
|
|
8
|
+
`mslxdff -refresh-token`, which regenerates, rewrites the file, prints the
|
|
9
|
+
new token, and exits (does not start the server).
|
|
10
|
+
|
|
11
|
+
Auth is enforced with a constant-time string compare on
|
|
12
|
+
`Authorization: Bearer <token>`; mismatches get `401` with `WWW-Authenticate`.
|
|
13
|
+
`/health` stays public (no token). Tokens never appear in logs.
|
|
14
|
+
|
|
15
|
+
Alternatives rejected: a fixed default token (same key on every install),
|
|
16
|
+
per-user accounts (needs a DB — that's the 9Router provisioning surface we
|
|
17
|
+
rejected in ADR-0003), and unauthenticated local-only binding (fragile;
|
|
18
18
|
a proxy relay deserves an explicit secret even on localhost).
|