@morgan9668/dsh-llm-gateway 1.2.0 → 1.3.0

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/cordis.patch.yml CHANGED
@@ -5,3 +5,47 @@
5
5
  host: '127.0.0.1'
6
6
  port: 8392
7
7
  apiKey: ''
8
+ # 任务完成通知。设置页(设置 → DSH 大模型网关)写入的是
9
+ # ~/.dsh/dsh-llm-gateway.notify.json,它优先于这里;想全部用 YAML
10
+ # 管理,就别在设置页里保存,或者删掉那个文件。
11
+ #
12
+ # 为什么不把设置存回这一行:改插件 config 会让它 reconcile,
13
+ # 也就是 server.close() 重启,正在跑的外部客户端请求会被掐断。
14
+ notify:
15
+ enabled: true
16
+ reasons:
17
+ - completed
18
+ - error
19
+ - blocked
20
+ notifySubagents: false
21
+ maxExcerptChars: 400
22
+ timeoutMs: 8000
23
+ sources:
24
+ gateway: true
25
+ session: true
26
+ gateway:
27
+ # 安静这么多秒算「任务结束」,把整段请求压成一条通知。
28
+ # 0 = 每个请求都发。
29
+ settleSeconds: 12
30
+ showClient: true
31
+ inApp:
32
+ enabled: false
33
+ flashes: 18
34
+ durationSeconds: 6
35
+ color: '#22c55e'
36
+ channels:
37
+ feishu:
38
+ enabled: false
39
+ webhook: ''
40
+ secret: ''
41
+ dingtalk:
42
+ enabled: false
43
+ webhook: ''
44
+ secret: ''
45
+ wecom:
46
+ enabled: false
47
+ webhook: ''
48
+ wechat:
49
+ enabled: false
50
+ serverchanSendKey: ''
51
+ wxpusherAppToken: ''
package/docs/DETAIL.md CHANGED
@@ -17,6 +17,8 @@ token accounting and provider adapters stay inside DSH.
17
17
  `reasoning_supported_efforts` and `reasoning_default_effort`.
18
18
  - `POST /v1/chat/completions` — Chat Completions (streaming and non-streaming).
19
19
  - `POST /v1/responses` — Responses API (streaming and non-streaming), used by Codex.
20
+ - `GET`/`POST /api/llm-gateway/*` — settings API served by the host's own web server,
21
+ for the settings page (see Task notifications).
20
22
 
21
23
  By default the server listens on `127.0.0.1:8392`. Configuration lives in the
22
24
  bundle patch (`host`, `port`, `apiKey`, `maxBodyBytes`); the gateway does not define
@@ -26,11 +28,22 @@ the whole history every turn, and image-heavy conversations get large quickly.
26
28
 
27
29
  ## Authentication and origin checks
28
30
 
29
- Authentication is on by default. Leaving `apiKey` empty does **not** mean "no
30
- auth": on first start the gateway generates a random key and writes it to
31
- `apiKeyFile` (default `~/.dsh/dsh-llm-gateway.key`, mode `600`), logging the path.
32
- Clients send `Authorization: Bearer <that key>`; the comparison uses
33
- `timingSafeEqual`, not an early-returning string compare.
31
+ Authentication is **off by default**, deliberately: existing clients (Codex,
32
+ SDKs, scripts) connect with no credentials, and upgrading a gateway should not
33
+ break them. The dangerous cases are covered by two checks that are on by default
34
+ instead — Host and Origin, below.
35
+
36
+ Turning it on takes one setting:
37
+
38
+ ```yaml
39
+ config:
40
+ requireAuth: true
41
+ ```
42
+
43
+ An empty `apiKey` then generates a random key into `apiKeyFile` (default
44
+ `~/.dsh/dsh-llm-gateway.key`, mode `600`) and logs the path; clients send
45
+ `Authorization: Bearer <that key>`. Setting `apiKey` directly pins your own value.
46
+ The comparison uses `timingSafeEqual`, not an early-returning string compare.
34
47
 
35
48
  Two more checks run before routing:
36
49
 
@@ -204,6 +217,92 @@ Keep those three top-level keys above every `[table]`, and regenerate the catalo
204
217
  after the upstream model set or effort levels change. The script picks the
205
218
  highest level it knows as the default and ignores `reasoning_default_effort`.
206
219
 
220
+ ## Task notifications
221
+
222
+ When a task finishes, the gateway can push a summary, elapsed time and token usage
223
+ to a Feishu, DingTalk or WeCom group bot, or flash the DSH window instead. The
224
+ settings page is **Settings → Gateway & notifications** (next to "Task completion
225
+ notifier"; both are notification settings).
226
+
227
+ There are two independent trigger sources, sharing one channel configuration:
228
+
229
+ | Source | Fires when | Notes |
230
+ |---|---|---|
231
+ | Gateway request | An external client (Codex, the ChatGPT desktop app, an SDK, a script) finishes a task through this gateway | Aggregated by a settle window: only after 12 quiet seconds is the whole task reported as one message |
232
+ | DSH session turn | A local agent turn reaches `turn/end` | Overlaps `@morgan9668/dsh-task-notify`; see below |
233
+
234
+ > **Relation to `dsh-task-notify`.** That plugin also listens to `turn/end`. With
235
+ > both enabled, every DSH session turn sends **one message from each plugin and
236
+ > flashes twice**. This is deliberate, not a defect: turn off one of the two
237
+ > sources to keep a single notifier (the gateway's "DSH session turn" checkbox,
238
+ > or task-notify's enable switch). Only this plugin can report gateway requests.
239
+ >
240
+ > While the session source is on, the settings page shows an explicit warning
241
+ > about this. It looks enough like a bug that hiding it in 12px grey text would
242
+ > be dishonest; the warning disappears when the checkbox is cleared.
243
+
244
+ Which outcomes are reported can be chosen (completed / error / blocked /
245
+ max-tokens / aborted). The first three are the default, inherited from
246
+ task-notify: interrupted or truncated tasks send nothing unless enabled. At
247
+ least one must stay selected, which the settings page enforces.
248
+
249
+ ### Settle window
250
+
251
+ Codex sends one request per tool call, so a 40-step task is 40 requests. To avoid
252
+ forty group messages the gateway folds every request inside the window into one
253
+ notification: elapsed time spans first to last, token usage is summed, the body
254
+ and outcome come from the last request (the last step is the conclusion), and the
255
+ request count is reported. Setting it to `0` reports every request instead.
256
+
257
+ ### Channels
258
+
259
+ | Channel | Credential | Where to get it |
260
+ |---|---|---|
261
+ | Feishu | Group-bot webhook, optional signature secret | Group → Settings → Bots → Add → Custom bot |
262
+ | DingTalk | Group-bot webhook, secret when signing is on | Group assistant → Add bot → Custom |
263
+ | WeCom | Group-bot webhook | Group settings → Group bots → Add |
264
+ | WeChat | ServerChan SendKey or WxPusher appToken | WeChat has no direct personal-bot push; a relay service is required |
265
+
266
+ Sending is fire-and-forget: a failure is only logged and never changes or blocks
267
+ the model response. "Send test" verifies the webhooks; with no enabled channel it
268
+ reports "no enabled channel" rather than pretending to succeed.
269
+
270
+ ### In-app flashes
271
+
272
+ With this on, the whole window flashes when a gateway request or session turn
273
+ ends — no webhook needed. Four settings: enable, count (1–60), duration (1–80 s),
274
+ and colour. Count and duration are independent; the total duration is split
275
+ evenly, so 18 flashes in 6 seconds is one 333 ms light/dark cycle.
276
+
277
+ The Host broadcasts a `flash` event over `GET /api/llm-gateway/events` (SSE); the
278
+ Client half subscribes when the plugin mounts (so it flashes even with the
279
+ settings page closed) and renders a `pointer-events: none` overlay in
280
+ `shell.overlay`. It never touches theme variables or `document.body`, and with
281
+ `prefers-reduced-motion: reduce` the animation is replaced by a static veil.
282
+
283
+ ### Where the settings live
284
+
285
+ The settings page writes to `~/.dsh/dsh-llm-gateway.notify.json` (mode 600), not
286
+ to the plugin's `config`. Changing `config` reconciles the plugin, which for this
287
+ plugin means `server.close()` and a fresh listen — that would cut off external
288
+ clients mid-task, so it is not acceptable for a checkbox. When the file exists it
289
+ takes precedence over the `notify:` block in the patch; delete it to manage
290
+ everything from YAML.
291
+
292
+ ### Settings API
293
+
294
+ The settings page uses the host process's `webServer` (same origin as the page),
295
+ not the gateway port, so configuration cannot be changed by anything that can
296
+ reach `127.0.0.1:8392`.
297
+
298
+ | Method | Path | Purpose |
299
+ |---|---|---|
300
+ | GET | `/api/llm-gateway/settings` | Current config plus read-only gateway state |
301
+ | POST | `/api/llm-gateway/settings` | `{ path: ['channels','feishu','webhook'], value: '...' }` |
302
+ | POST | `/api/llm-gateway/test` | Send a test notification |
303
+ | POST | `/api/llm-gateway/flash` | Preview a flash, optionally with unsaved overrides |
304
+ | GET | `/api/llm-gateway/events` | Flash event stream (SSE) |
305
+
207
306
  ## Logs
208
307
 
209
308
  Startup, catalog failures and request failures are appended to
@@ -227,10 +326,31 @@ and `retry-after: 0`; a missing `content-length` (chunked upload) is still count
227
326
  occurrences are omitted first while the newest are kept; a byte overflow also triggers
228
327
  omission; nothing is touched below the budget; and the result really fits.
229
328
 
230
- `node tests/auth.mjs` runs ten cases for authentication and origin checks: missing
231
- or wrong token returns 401; a non-allowlisted Host returns 421; an origin outside
232
- `allowedOrigins` returns 403; clients without an Origin header still work; leaving
233
- `apiKey` empty generates a key file with mode 600; and an explicit key wins.
234
-
235
- Conversion, pairing repair, slots, routing, attachments and the status map are not
236
- covered.
329
+ `node tests/auth.mjs` runs eleven cases for authentication and origin checks: the
330
+ default requires no token; with `requireAuth` a missing or wrong token returns 401;
331
+ a non-allowlisted Host returns 421; an origin outside `allowedOrigins` returns 403;
332
+ clients without an Origin header still work; `requireAuth` generates a key file with
333
+ mode 600; and an explicit key wins.
334
+
335
+ `node tests/notify.mjs` runs thirty-one cases over the notification pure functions
336
+ and scheduler: colour and number clamping, outcome badges, per-turn usage sums,
337
+ "not provided" instead of a fabricated zero, the stream tap forwarding every chunk
338
+ unchanged, tool calls counted once per id, config-store file precedence with a
339
+ corrupt-file fallback, and the gateway settle window.
340
+
341
+ `node tests/client.mjs` renders both `client.js` slot components once through a
342
+ minimal React stub: registration succeeds, the module id and `inject` are right,
343
+ both the loading and loaded states render without throwing, and the loaded state
344
+ really contains the warning bar, the three-state channel badge, the outcome
345
+ checkboxes and the global-switch dimming. A throwing component blanks its whole
346
+ slot entry, and there is no browser here to notice.
347
+
348
+ `node tests/notify-api.mjs` starts the plugin and runs nine cases over the settings
349
+ API and gateway triggering: GET/POST settings, the gateway staying on the same port
350
+ after a save (so no reconcile restart), POST returning the saved value, a flash
351
+ preview with unsaved overrides, an honest empty result from `POST /test` with no
352
+ channel, SSE output unchanged by the tap, a dead webhook not turning a model
353
+ request into a 500, and the gateway working without any `webServer`.
354
+
355
+ Conversion, pairing repair, slots, routing, attachments, the status map and UI
356
+ interaction details beyond the settings page are not covered.