dsh-dot-life 0.23.1 → 0.23.2

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.
@@ -0,0 +1,420 @@
1
+ // One gate in front of every model request in this dsh, so her life in the background does not use
2
+ // up the requests-per-minute budget the owner's conversation needs (users hit
3
+ // `429 rate_limit_rpm_exceeded` in a normal chat). Built on dsh-llm's `llm/stream` waterfall, which
4
+ // every model call goes through — the agent loop's steps and direct `ctx.llm.stream(...)` alike.
5
+ // After LangChain's InMemoryRateLimiter (a bucket in front of model calls), LiteLLM's request
6
+ // priorities and cooldowns, promptfoo's scheduler (AIMD per provider, honour retry-after) and the
7
+ // OpenAI SDKs' backoff with jitter — at the scale of one person's desktop: no dependency, process memory only.
8
+ //
9
+ // Which requests are ours in the background (only these are ever paced; anything unknown is foreground):
10
+ //
11
+ // request where class
12
+ // ───────────────────────────────────────────── ──────────────────────────── ──────────
13
+ // interlude round, its continuity refresh, interlude/core.js (marked) background
14
+ // the weekly growth review
15
+ // memory consolidation, diary memory/memory.js complete() background
16
+ // QQ group scoring (groupPolicy 'score') qq/qq.js (marked) background
17
+ // howto reflection after a phone run dot/core.js reflectOnce() background
18
+ // her errand session (current and retired): dot/core.js isErrand background
19
+ // non-conversation app notifications, phone
20
+ // checks, interlude look / play / try / free
21
+ // time (dshDot.task)
22
+ // interlude without dsh-dot: interlude.vmSession interlude/core.js background
23
+ // (a session set aside for looks / plays /
24
+ // tries), and a look / try / free-time session
25
+ // that is not her conversation
26
+ // her conversation (lastSession): the owner's — foreground
27
+ // dsh / IM chat, QQ messages (private and
28
+ // group) and conversation-app notifications
29
+ // (QQ, WeChat) — these wake her there
30
+ // a phone check or look when there is no errand — foreground
31
+ // (it falls back to her conversation)
32
+ // anything else (other plugins, sessions we do — foreground
33
+ // not know, compaction, titles)
34
+ //
35
+ // What the gate does, per provider (a limit on one provider never slows another):
36
+ // - counts every request started in the last 60 s (both classes);
37
+ // - a background request starts only when the window holds fewer than `backgroundRpm` requests,
38
+ // at least 60 s / cap after the previous background one (bucket refill: no bursts), one at a
39
+ // time, and not while a foreground request is in flight (one open for 10 min is taken as
40
+ // abandoned) or within `foregroundQuietSeconds` after one ended (the owner's turn has several steps);
41
+ // - a RATE_LIMIT finish (any class) starts a cooldown for background requests — at least the 60 s
42
+ // 60 s after the latest refusal, doubling with jitter when a fresh window is refused again
43
+ // (≤ 10 min), or the provider's retry-after — and halves the background cap (≥ 1) once per
44
+ // incident (refusals within 60 s of the counted one, e.g. dsh's retries, are the same incident);
45
+ // after 3 min without a 429 the cap grows back by 1 a minute (AIMD);
46
+ // - a background request never waits longer than `maxBackgroundWaitSeconds` (≤ 270 s, below dot's
47
+ // turnStallMs, so an errand step is never taken for a stalled turn), and stops waiting at once
48
+ // when its signal aborts (the adapter then ends it with dsh's own aborted finish);
49
+ // - `holdRetries`: see holdFor() — the one case a foreground request is touched;
50
+ // - nothing in here may break a model call: any error in the bookkeeping lets the request through.
51
+ import { sectionOf } from './settings.js';
52
+
53
+ export const defaults = {
54
+ backgroundRpm: 20, // see DEFAULT_RPM_WHY; 0 = no pacing at all (the gate only counts)
55
+ foregroundQuietSeconds: 15, // background waits this long after her conversation's last request ended
56
+ maxBackgroundWaitSeconds: 180, // a background request never waits longer; capped at MAX_WAIT_CAP_SECONDS
57
+ holdRetries: true, // hold dsh's fast retry of a rate-limited request until the window has room
58
+ retryHoldMaxSeconds: 30, // …but never longer than this
59
+ };
60
+ // Why 20: one background request every 3 s at most. The measured burst (one "like a post" act) was
61
+ // dozens of requests in under a minute; 20 leaves most of a typical entry-tier RPM limit (tens per
62
+ // minute) to the owner, while a single errand loop, whose steps already take seconds each (tools,
63
+ // the phone), barely slows down. Below that, AIMD halves the cap on the first 429 anyway.
64
+
65
+ const WINDOW_MS = 60000;
66
+ const RETRY_WINDOW_MS = 12000; // dsh's retry waits ≤ 10 s (+10% jitter) after a failure: a start this soon is a retry
67
+ const COOLDOWN_MAX_MS = 600000;
68
+ const RECOVER_AFTER_MS = 180000; // no 429 for this long: the cap grows again, repeated-hit count resets
69
+ const RAISE_EVERY_MS = 60000;
70
+ const FG_STALE_MS = 600000; // a foreground request "in flight" this long is taken as abandoned (never iterated / dropped)
71
+ // dot's turnStallMs is 300 s: a background agent step held longer would make her errand look stalled.
72
+ export const MAX_WAIT_CAP_SECONDS = 270;
73
+ const JITTER = 0.25;
74
+
75
+ // Hand-built requests the plugin makes for background work. A WeakSet of the options objects:
76
+ // the waterfall hands every listener the very object passed to ctx.llm.stream.
77
+ const BACKGROUND = new WeakSet();
78
+ /** Mark one hand-built request as background work; returns the same options object. */
79
+ export function background(options) {
80
+ try { if (options && typeof options === 'object') BACKGROUND.add(options); } catch { /* never in the way */ }
81
+ return options;
82
+ }
83
+
84
+ const num = (v, d, min = 0) => (Number.isFinite(Number(v)) && Number(v) >= min ? Number(v) : d);
85
+ function normalize(c = {}) {
86
+ return {
87
+ backgroundRpm: Math.floor(num(c.backgroundRpm, defaults.backgroundRpm)),
88
+ quietMs: num(c.foregroundQuietSeconds, defaults.foregroundQuietSeconds) * 1000,
89
+ maxWaitMs: Math.min(MAX_WAIT_CAP_SECONDS, num(c.maxBackgroundWaitSeconds, defaults.maxBackgroundWaitSeconds)) * 1000,
90
+ holdRetries: c.holdRetries === undefined ? defaults.holdRetries : c.holdRetries !== false,
91
+ retryHoldMaxMs: num(c.retryHoldMaxSeconds, defaults.retryHoldMaxSeconds) * 1000,
92
+ };
93
+ }
94
+
95
+ /**
96
+ * The gate's logic, clock-injectable. `config`: the `llm` settings section; `now`/`setTimer`/
97
+ * `clearTimer`/`random` for tests; `logger` ({ info, debug }); `audit(event, data)`.
98
+ */
99
+ export function createGate({ config = {}, now = Date.now, setTimer = setTimeout, clearTimer = clearTimeout, random = Math.random, logger = {}, audit = () => {} } = {}) {
100
+ const cfg = normalize(config);
101
+ const pacing = cfg.backgroundRpm > 0;
102
+ const providers = new Map();
103
+ const sessionTests = new Map(); // name → (sessionId) => boolean
104
+ const limits = new Map(); // provider \0 session → { at, retryAfterMs, inWindow }: its last RATE_LIMIT
105
+ const debug = (m) => { try { logger.debug?.(m); } catch { /* ignore */ } };
106
+ const info = (m) => { try { logger.info?.(m); } catch { /* ignore */ } };
107
+ const note = (event, data) => { try { audit(event, data); } catch { /* ignore */ } };
108
+ const timer = (fn, ms) => { const h = setTimer(fn, Math.max(0, ms)); h?.unref?.(); return h; };
109
+ const stamp = (t) => new Date(t).toTimeString().slice(0, 8);
110
+
111
+ const P = (name) => {
112
+ let p = providers.get(name);
113
+ if (!p) {
114
+ p = { name, starts: [], bgStarts: [], fg: new Map(), fgLastEnd: -Infinity, lastBgStart: -Infinity, cap: cfg.backgroundRpm, raiseAt: Infinity, hits: 0, lastHitAt: -Infinity, lastAnyHitAt: -Infinity, coolingUntil: 0, coolTimer: null, queue: [], timer: null, timerAt: Infinity, delayed: 0, rateLimited: 0, heldRetries: 0 };
115
+ providers.set(name, p);
116
+ }
117
+ return p;
118
+ };
119
+
120
+ function refresh(p, t) {
121
+ while (p.starts.length && p.starts[0] <= t - WINDOW_MS) p.starts.shift();
122
+ while (p.bgStarts.length && p.bgStarts[0] <= t - WINDOW_MS) p.bgStarts.shift();
123
+ while (p.cap < cfg.backgroundRpm && t >= p.raiseAt) { p.cap++; p.raiseAt += RAISE_EVERY_MS; } // additive increase
124
+ if (p.cap >= cfg.backgroundRpm) p.raiseAt = Infinity;
125
+ if (p.hits && t - p.lastAnyHitAt >= RECOVER_AFTER_MS) p.hits = 0;
126
+ for (const [rec, at] of p.fg) if (at <= t - FG_STALE_MS) p.fg.delete(rec); // abandoned: never held the background for ever
127
+ for (const [k, l] of limits) if (t - l.at > RETRY_WINDOW_MS) limits.delete(k); // only ever read within RETRY_WINDOW_MS
128
+ if (p.coolingUntil && t >= p.coolingUntil) {
129
+ p.coolingUntil = 0;
130
+ info(`${p.name}: cooldown over, background requests resume (cap ${p.cap}/min)`);
131
+ note('cooldown-end', { provider: p.name, cap: p.cap });
132
+ }
133
+ }
134
+
135
+ /** The earliest time the head of the background queue may start (Infinity: wait for an event). */
136
+ function readyAt(p, t) {
137
+ // While his request is in flight: not before it ends (an event pumps then) or is taken as abandoned.
138
+ const fgOpen = p.fg.size ? Math.min(...p.fg.values()) + FG_STALE_MS : -Infinity;
139
+ let at = Math.max(t, fgOpen, p.fgLastEnd + cfg.quietMs, p.coolingUntil, p.lastBgStart + WINDOW_MS / p.cap);
140
+ if (p.starts.length >= p.cap) at = Math.max(at, p.starts[p.starts.length - p.cap] + WINDOW_MS);
141
+ return at;
142
+ }
143
+
144
+ /** Count one request as started now. */
145
+ function start(rec, t) {
146
+ const p = rec.p;
147
+ rec.started = true;
148
+ p.starts.push(t);
149
+ if (rec.cls === 'foreground') p.fg.set(rec, t);
150
+ else { p.bgStarts.push(t); p.lastBgStart = t; }
151
+ }
152
+
153
+ function admit(p, w, t) {
154
+ p.queue.splice(p.queue.indexOf(w), 1);
155
+ w.off();
156
+ start(w.rec, t);
157
+ if (t > w.since) p.delayed++;
158
+ w.resolve();
159
+ }
160
+
161
+ function pump(p) {
162
+ try {
163
+ const t = now();
164
+ refresh(p, t);
165
+ for (const w of [...p.queue]) if (t >= w.deadline) admit(p, w, t); // the bound outranks every rule
166
+ if (p.queue.length && readyAt(p, t) <= t) admit(p, p.queue[0], t); // one at a time
167
+ schedule(p, t);
168
+ } catch (err) {
169
+ debug(`llm gate: pump failed (${err.message}); releasing ${p.queue.length} waiting`);
170
+ for (const w of p.queue.splice(0)) { try { w.off(); } catch { /* ignore */ } w.resolve(); }
171
+ }
172
+ }
173
+
174
+ function schedule(p, t) {
175
+ if (!p.queue.length) { if (p.timer) clearTimer(p.timer); p.timer = null; p.timerAt = Infinity; return; }
176
+ const next = Math.min(readyAt(p, t), ...p.queue.map((w) => w.deadline));
177
+ if (next === p.timerAt && p.timer) return;
178
+ if (p.timer) clearTimer(p.timer);
179
+ p.timerAt = next;
180
+ p.timer = Number.isFinite(next) ? timer(() => { p.timer = null; p.timerAt = Infinity; pump(p); }, next - t) : null;
181
+ }
182
+
183
+ /** Wait until `until` (or the signal aborts); resolves either way. */
184
+ const sleep = (ms, signal) => new Promise((resolve) => {
185
+ if (signal?.aborted || ms <= 0) { resolve(); return; }
186
+ let h = null;
187
+ const done = () => { if (h) clearTimer(h); signal?.removeEventListener?.('abort', done); resolve(); };
188
+ h = timer(done, ms);
189
+ signal?.addEventListener?.('abort', done, { once: true });
190
+ });
191
+
192
+ /**
193
+ * dsh's own retry after a RATE_LIMIT (500 ms, 1 s, 2 s … ≤ 10 s) lands inside the same 60 s window
194
+ * and is refused again. A foreground request of the same session and provider starting within
195
+ * RETRY_WINDOW_MS of that finish is taken for the retry and held until the provider's retry-after,
196
+ * else until enough of the last minute's requests have aged out: the hit came at the L-th request
197
+ * in the window, so the limit is taken as L - 1 and the retry waits for the window to hold fewer.
198
+ * A rough estimate, and always capped at retryHoldMaxSeconds (and the request's own abort).
199
+ */
200
+ function holdFor(p, rec, t) {
201
+ if (!cfg.holdRetries || !rec.session) return 0;
202
+ const l = limits.get(`${p.name}\0${rec.session}`);
203
+ if (!l || t - l.at > RETRY_WINDOW_MS) return 0;
204
+ let until;
205
+ if (l.retryAfterMs > 0) until = l.at + l.retryAfterMs;
206
+ else {
207
+ const allowed = Math.max(1, l.inWindow - 1);
208
+ const i = p.starts.length - allowed;
209
+ until = i >= 0 ? p.starts[i] + WINDOW_MS : 0;
210
+ }
211
+ return Math.max(0, Math.min(until, t + cfg.retryHoldMaxMs) - t);
212
+ }
213
+
214
+ function classify(options) {
215
+ if (BACKGROUND.has(options)) return 'background';
216
+ const id = options?.sessionId;
217
+ if (id) {
218
+ for (const test of sessionTests.values()) {
219
+ try { if (test(String(id))) return 'background'; } catch (err) { debug(`llm gate: session test failed (${err.message})`); }
220
+ }
221
+ }
222
+ return 'foreground';
223
+ }
224
+
225
+ /** Book one request in: { rec, wait } (wait: a promise, or null when it may start now). */
226
+ function begin(options) {
227
+ const t = now();
228
+ const p = P(String(options?.provider ?? '?'));
229
+ refresh(p, t);
230
+ const rec = { p, cls: classify(options), session: options?.sessionId ? String(options.sessionId) : '', started: false, signal: options?.signal };
231
+ // An already aborted request is not counted: the adapter ends it at once without a provider call.
232
+ if (rec.signal?.aborted) return { rec, wait: null };
233
+ if (rec.cls === 'foreground') {
234
+ const hold = holdFor(p, rec, t);
235
+ if (!hold) { start(rec, t); return { rec, wait: null }; }
236
+ p.heldRetries++;
237
+ debug(`llm gate: holding a retry in ${rec.session} on ${p.name} for ${Math.round(hold / 1000)} s`);
238
+ return { rec, wait: sleep(hold, rec.signal).then(() => { if (!rec.signal?.aborted) { const s = now(); refresh(p, s); start(rec, s); } }) };
239
+ }
240
+ if (!pacing) { start(rec, t); return { rec, wait: null }; }
241
+ const wait = new Promise((resolve) => {
242
+ const w = { rec, since: t, deadline: t + cfg.maxWaitMs, resolve, off: () => {} };
243
+ if (rec.signal?.addEventListener) {
244
+ const onAbort = () => { const i = p.queue.indexOf(w); if (i < 0) return; p.queue.splice(i, 1); resolve(); pump(p); };
245
+ rec.signal.addEventListener('abort', onAbort, { once: true });
246
+ w.off = () => rec.signal.removeEventListener?.('abort', onAbort);
247
+ }
248
+ p.queue.push(w);
249
+ });
250
+ pump(p);
251
+ return { rec, wait };
252
+ }
253
+
254
+ function hit(p, rec, failure, t) {
255
+ p.rateLimited++;
256
+ const retryAfterMs = Number(failure?.providerRetryAfterMs) > 0 ? Number(failure.providerRetryAfterMs) : 0;
257
+ if (rec.session) limits.set(`${p.name}\0${rec.session}`, { at: t, retryAfterMs, inWindow: p.starts.length });
258
+ if (!pacing) return;
259
+ // One incident, one hit: refusals within WINDOW_MS of the last counted hit (a burst refused
260
+ // together, dsh's five fast retries) are the same incident. Only a fresh window refused again is a
261
+ // repeat (halves the cap again, doubles the cooldown); every hit still extends the cooldown below.
262
+ const repeat = t - p.lastHitAt >= WINDOW_MS;
263
+ if (repeat) {
264
+ p.hits++; p.lastHitAt = t;
265
+ p.cap = Math.max(1, Math.floor(p.cap / 2)); // multiplicative decrease
266
+ }
267
+ p.lastAnyHitAt = t;
268
+ p.raiseAt = t + RECOVER_AFTER_MS; // recovery counts from the latest refusal
269
+ const backoff = repeat ? Math.min(COOLDOWN_MAX_MS, WINDOW_MS * 2 ** (p.hits - 1) * (1 + JITTER * random())) : WINDOW_MS;
270
+ const until = t + Math.min(COOLDOWN_MAX_MS, Math.max(backoff, retryAfterMs));
271
+ if (until <= p.coolingUntil) return;
272
+ const was = p.coolingUntil;
273
+ p.coolingUntil = until;
274
+ if (p.coolTimer) clearTimer(p.coolTimer);
275
+ p.coolTimer = timer(() => { p.coolTimer = null; pump(p); }, until - t);
276
+ if (!was) {
277
+ info(`${p.name}: rate limited (${rec.cls}${rec.session ? ` in ${rec.session}` : ''}); background requests wait until ${stamp(until)}, cap ${p.cap}/min`);
278
+ note('cooldown-start', { provider: p.name, until: new Date(until).toISOString(), cap: p.cap, hits: p.hits, retryAfterMs: retryAfterMs || undefined, class: rec.cls });
279
+ }
280
+ }
281
+
282
+ /** The request ended: `reason` is its finish chunk's reason, or null (stopped early, thrown). */
283
+ function finish(rec, reason) {
284
+ const p = rec.p, t = now();
285
+ refresh(p, t);
286
+ if (rec.cls === 'foreground' && rec.started) { p.fg.delete(rec); p.fgLastEnd = t; }
287
+ rec.started = false;
288
+ if (reason?.kind === 'error' && reason.failure?.code === 'RATE_LIMIT') hit(p, rec, reason.failure, t);
289
+ else if (rec.session && reason) limits.delete(`${p.name}\0${rec.session}`);
290
+ pump(p);
291
+ }
292
+
293
+ /** Pass the chunks through unchanged, noting the terminal finish (or that the stream ended without one). */
294
+ async function* observe(source, rec) {
295
+ let done = false;
296
+ const end = (reason) => { if (done) return; done = true; try { finish(rec, reason); } catch (err) { debug(`llm gate: finish failed (${err.message})`); } };
297
+ try {
298
+ for await (const chunk of source) {
299
+ if (chunk?.type === 'finish') end(chunk.reason ?? null);
300
+ yield chunk;
301
+ }
302
+ } finally { end(null); }
303
+ }
304
+
305
+ return {
306
+ config: cfg,
307
+ /** The `llm/stream` listener body: delays (when it must) and observes one request. */
308
+ wrap(options, next) {
309
+ let booked;
310
+ try { booked = begin(options); } catch (err) { debug(`llm gate: ${err.message}; request let through`); return next(); }
311
+ const { rec, wait } = booked;
312
+ if (!wait) {
313
+ let source;
314
+ try { source = next(); } catch (err) { try { finish(rec, null); } catch { /* ignore */ } throw err; }
315
+ return observe(source, rec);
316
+ }
317
+ return (async function* gated() {
318
+ try { await wait; } catch (err) { debug(`llm gate: wait failed (${err.message})`); }
319
+ let source;
320
+ try { source = next(); } catch (err) { try { finish(rec, null); } catch { /* ignore */ } throw err; }
321
+ yield* observe(source, rec);
322
+ })();
323
+ },
324
+ classify,
325
+ /** Register which sessions are background (a predicate on the session id); returns the remover. */
326
+ backgroundSessions(name, test) {
327
+ sessionTests.set(name, test);
328
+ return () => { if (sessionTests.get(name) === test) sessionTests.delete(name); };
329
+ },
330
+ /** Per provider: requests in the last minute, background cap, cooldown, waiting, totals. */
331
+ status() {
332
+ const t = now();
333
+ return {
334
+ pacing, backgroundRpm: cfg.backgroundRpm, quietSeconds: cfg.quietMs / 1000, maxWaitSeconds: cfg.maxWaitMs / 1000, holdRetries: cfg.holdRetries,
335
+ providers: [...providers.values()].map((p) => {
336
+ try { refresh(p, t); } catch { /* status only */ }
337
+ return { provider: p.name, lastMinute: p.starts.length, backgroundLastMinute: p.bgStarts.length, cap: p.cap, coolingUntil: p.coolingUntil || null, waiting: p.queue.length, foregroundInFlight: p.fg.size, delayed: p.delayed, rateLimited: p.rateLimited, heldRetries: p.heldRetries };
338
+ }),
339
+ };
340
+ },
341
+ /** Let everything waiting go and drop the timers (dispose). */
342
+ stop() {
343
+ for (const p of providers.values()) {
344
+ for (const w of p.queue.splice(0)) { try { w.off(); } catch { /* ignore */ } w.resolve(); }
345
+ if (p.timer) clearTimer(p.timer);
346
+ if (p.coolTimer) clearTimer(p.coolTimer);
347
+ p.timer = p.coolTimer = null; p.timerAt = Infinity;
348
+ }
349
+ },
350
+ };
351
+ }
352
+
353
+ /** One line for /dot. */
354
+ export function statusLine(st) {
355
+ if (!st) return 'model requests: gate not installed';
356
+ const head = st.pacing ? `background ≤${st.backgroundRpm}/min, waits ${st.quietSeconds} s after yours, ≤${st.maxWaitSeconds} s` : 'pacing off (llm.backgroundRpm 0)';
357
+ const per = st.providers.map((p) => `${p.provider} ${p.lastMinute}/min (bg cap ${p.cap}${p.coolingUntil ? `, cooling until ${new Date(p.coolingUntil).toTimeString().slice(0, 8)}` : ''}${p.waiting ? `, ${p.waiting} waiting` : ''}, ${p.delayed} delayed, ${p.rateLimited}×429${p.heldRetries ? `, ${p.heldRetries} retries held` : ''})`);
358
+ return `model requests: ${per.join(' · ') || 'none yet'} — ${head}`;
359
+ }
360
+
361
+ // One gate per dsh (per Cordis root), whichever components are on. Each component that makes model
362
+ // calls installs it; the listener lives on one of their contexts and moves to another if that one is
363
+ // disposed, so turning one component off never takes the gate away from the others.
364
+ const installs = new WeakMap();
365
+ const rootOf = (ctx) => { try { return ctx?.root ?? ctx; } catch { return ctx; } };
366
+
367
+ /** The gate installed for this dsh, if any (the dot page's status). */
368
+ export function gateFor(ctx) { return installs.get(rootOf(ctx))?.gate; }
369
+
370
+ /**
371
+ * Install the gate once per dsh (idempotent) and, with `backgroundSessions`, register which of this
372
+ * component's sessions are background for as long as the component lives. Returns the gate, or null
373
+ * when it could not be installed (the plugin then works exactly as before).
374
+ */
375
+ export function installLlmGate(ctx, { name = 'dsh-dot', backgroundSessions = null, deps = {} } = {}) {
376
+ try {
377
+ const root = rootOf(ctx);
378
+ let inst = installs.get(root);
379
+ if (!inst) {
380
+ const logger = (() => { try { return ctx.logger?.('dsh-dot/llm-gate') ?? {}; } catch { return {}; } })();
381
+ inst = { holders: new Set(), owner: null, off: null, gate: null };
382
+ const audit = (event, data) => {
383
+ const c = inst.owner ?? ctx;
384
+ const a = (() => { try { return c.get?.('dshComputer')?.audit ?? c.get?.('dshPhone')?.audit; } catch { return undefined; } })();
385
+ a?.({ kind: 'llm-gate', event, ...data });
386
+ };
387
+ inst.gate = createGate({ config: deps.config ?? sectionOf('llm', (m) => logger.warn?.(m)), logger, audit, ...deps });
388
+ installs.set(root, inst);
389
+ }
390
+ const { gate } = inst;
391
+ const attach = () => {
392
+ if (inst.owner) return;
393
+ for (const h of inst.holders) {
394
+ try {
395
+ // `global`: dsh-llm dispatches with the LlmRuntime as `this`; never filtered out by scope.
396
+ inst.off = h.on('llm/stream', (options, next) => gate.wrap(options, next), { global: true });
397
+ inst.owner = h;
398
+ return;
399
+ } catch { /* try the next holder */ }
400
+ }
401
+ };
402
+ const setup = () => {
403
+ inst.holders.add(ctx);
404
+ const removeTest = backgroundSessions ? gate.backgroundSessions(name, backgroundSessions) : () => {};
405
+ attach();
406
+ return () => {
407
+ removeTest();
408
+ inst.holders.delete(ctx);
409
+ if (inst.owner === ctx) { try { inst.off?.(); } catch { /* disposed with its fiber anyway */ } inst.owner = null; inst.off = null; attach(); }
410
+ if (!inst.holders.size) { gate.stop(); installs.delete(root); }
411
+ };
412
+ };
413
+ if (typeof ctx.effect === 'function') ctx.effect(setup, `${name}: llm request gate`);
414
+ else setup();
415
+ return gate;
416
+ } catch (err) {
417
+ try { ctx?.logger?.('dsh-dot/llm-gate')?.debug?.(`llm gate not installed: ${err.message}`); } catch { /* ignore */ }
418
+ return null;
419
+ }
420
+ }
@@ -28,6 +28,7 @@ import { createRecall } from './recall.js';
28
28
  import { annotate } from './when.js';
29
29
  import { dayMark, unstamp } from '../clock.js';
30
30
  import { sectionOf, setValues } from '../settings.js';
31
+ import { background, installLlmGate } from '../llm-gate.js';
31
32
  import { configure as configurePrompts, prompt, table } from '../prompts.js';
32
33
 
33
34
  export const name = 'dot-memory';
@@ -265,6 +266,7 @@ export function apply(ctx, config = {}) {
265
266
  const files = { memory: join(dir, 'memory.json'), md: join(dir, 'memory.md'), transcript: join(dir, 'transcript.jsonl'), handoffs: join(dir, 'handoffs.jsonl'), calls: join(dir, 'calls.jsonl'), diary: join(dir, 'diary.md'), archive: join(dir, 'memory.v019.json'), migration: join(dir, 'memory-migration.md') };
266
267
  mkdirSync(dir, { recursive: true });
267
268
  const dot = () => ctx.get('dshDot');
269
+ installLlmGate(ctx, { name: 'dsh-dot/memory' }); // her consolidation and diary wait behind his requests (complete() below)
268
270
  // Checked when used, not once at boot: the other plugin may start after this one.
269
271
  let coexistLogged = false;
270
272
  const peopleOnly = () => {
@@ -467,7 +469,7 @@ export function apply(ctx, config = {}) {
467
469
  let usage = null;
468
470
  try {
469
471
  let finish = null;
470
- for await (const chunk of ctx.llm.stream({ provider: sel.provider, model: sel.model, ...(sel.reasoningEffort ? { reasoningEffort: sel.reasoningEffort } : {}), maxTokens: cfg.maxTokens, messages: [createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'dsh-dot-memory' } })] })) {
472
+ for await (const chunk of ctx.llm.stream(background({ provider: sel.provider, model: sel.model, ...(sel.reasoningEffort ? { reasoningEffort: sel.reasoningEffort } : {}), maxTokens: cfg.maxTokens, messages: [createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'dsh-dot-memory' } })] }))) {
471
473
  if (chunk.type === 'text-delta') raw += chunk.text;
472
474
  if (chunk.type === 'usage') usage = chunk.usage ?? null;
473
475
  if (chunk.type === 'finish') finish = chunk.reason;
package/src/page/api.js CHANGED
@@ -9,6 +9,7 @@ import { homedir } from 'node:os';
9
9
  import { rejectUnlessOwner } from '../web-auth.js';
10
10
  import { dshHome, fileError, readAll, writeSections } from '../settings.js';
11
11
  import { describe, validatePatch } from '../settings-schema.js';
12
+ import { gateFor } from '../llm-gate.js';
12
13
  import { doctorFor, FIXES } from '../doctor.js';
13
14
  import { bundle, summary, versions } from './diag.js';
14
15
  import { PAGE_PATH, pageHtml } from './page.js';
@@ -39,7 +40,9 @@ function services(ctx) {
39
40
  export function statusOf(ctx) {
40
41
  const s = services(ctx);
41
42
  const st = (svc) => { try { return svc?.status?.() ?? (svc ? {} : null); } catch (err) { return { state: 'error', error: err.message }; } };
42
- return { phone: st(s.phone), computer: st(s.computer), qq: st(s.qq), dot: s.dot ? { state: 'on' } : null, interlude: s.interlude ? { state: 'on' } : null, compat: s.compat ? { state: 'on', qqBridge: (() => { try { const b = s.compat.bridge(); return b ? { root: b.root, mode: b.mode, bots: b.botUins } : null; } catch { return null; } })() } : null };
43
+ // The model-request gate (src/llm-gate.js) is listed only when a component installed it.
44
+ const llm = (() => { try { const g = gateFor(ctx)?.status(); return g ? { state: g.providers.some((p) => p.coolingUntil) ? 'cooling' : 'on', ...g } : undefined; } catch { return undefined; } })();
45
+ return { phone: st(s.phone), computer: st(s.computer), qq: st(s.qq), dot: s.dot ? { state: 'on' } : null, interlude: s.interlude ? { state: 'on' } : null, compat: s.compat ? { state: 'on', qqBridge: (() => { try { const b = s.compat.bridge(); return b ? { root: b.root, mode: b.mode, bots: b.botUins } : null; } catch { return null; } })() } : null, ...(llm ? { llm } : {}) };
43
46
  }
44
47
 
45
48
  const need = (svc, what) => { if (!svc) throw new Error(`${what}组件没开(在 dsh 的插件页打开)`); return svc; };
@@ -63,6 +66,8 @@ const ACTIONS = {
63
66
  return `已导入:${r.name}(${r.title},作者 @${r.author},${r.license})`;
64
67
  },
65
68
  'phone-accept': async ({ phone }, body) => { need(phone, '手机'); if (body?.gb) phone.setStorage(String(body.gb)); phone.accept(); return '已接受许可,开始安装'; },
69
+ 'phone-power': async ({ phone }, body) => { await need(phone, '手机').power(body?.on === true); return body?.on === true ? '手机开机中' : '手机已关机(重启 dsh 也保持关着)'; },
70
+ 'computer-power': async ({ computer }, body) => { await need(computer, '电脑').power(body?.on === true); return body?.on === true ? '电脑开机中' : '电脑已关机(重启 dsh 也保持关着)'; },
66
71
  'phone-restart': async ({ phone }) => { await need(phone, '手机').restart(); return '手机重启中'; },
67
72
  'phone-retry': async ({ phone }) => { await need(phone, '手机').restart(); return '重试中'; },
68
73
  };
@@ -77,6 +82,7 @@ export function setupSteps(ctx, stored = readSetup()) {
77
82
  const dev = (svc, x, okStates) => !svc ? { state: 'skip', detail: '组件没开' }
78
83
  : okStates.includes(x?.state) ? { state: 'done', detail: '就绪' }
79
84
  : x?.state === 'error' ? { state: 'blocked', detail: x.error ?? '出错' }
85
+ : x?.state === 'off' ? { state: 'skip', detail: '已关机(页面顶部可以开机)' }
80
86
  : x?.state === 'needs-license' ? { state: 'todo', detail: '还没装' }
81
87
  : { state: 'working', detail: x?.progress?.text ?? x?.stage ?? x?.state ?? '' };
82
88
  const canon = (() => { try { return s.interlude?.canon?.(); } catch { return null; } })();
package/src/page/page.js CHANGED
@@ -40,6 +40,7 @@ textarea{width:100%;box-sizing:border-box;min-height:160px;font:12px/1.5 ui-mono
40
40
  </style></head><body><main>
41
41
  <div class="row" style="justify-content:space-between"><h1>dot 设置</h1><span class="mute small" id="ver"></span></div>
42
42
  <div class="row" id="badges"></div>
43
+ <div class="row" id="power" style="margin-top:8px"></div><div class="msg" id="powerMsg"></div>
43
44
  <div class="card banner" id="fileErr" hidden></div>
44
45
  <div class="tabs" role="tablist"><button data-tab="setup">开始</button><button data-tab="status">状态</button><button data-tab="settings" class="on">设置</button><button data-tab="doctor">体检</button><button data-tab="diag">诊断</button></div>
45
46
  <section id="tab-setup" hidden><div id="steps" class="mute">加载中…</div></section>
@@ -69,24 +70,46 @@ async function api(method, path, body) {
69
70
  if (!r.ok) throw new Error(j.error || ('HTTP ' + r.status));
70
71
  return j;
71
72
  }
72
- const NAMES = { phone: '手机', computer: '电脑', gate: '审批', qq: 'QQ', dot: 'dot', interlude: '幕间', compat: '兼容' };
73
+ const NAMES = { phone: '手机', computer: '电脑', gate: '审批', qq: 'QQ', dot: 'dot', interlude: '幕间', compat: '兼容', llm: '模型请求' };
73
74
  const APPLY = { live: '即时生效', 'restart-phone': '重启手机生效', 'restart-dsh': '重启 dsh 生效', create: '只对新建生效' };
74
- function level(s) { if (!s) return ''; if (/ready|on/.test(s.state)) return 'ok'; if (s.state === 'error') return 'bad'; return 'warn'; }
75
+ function level(s) { if (!s || s.state === 'off') return ''; if (/ready|on/.test(s.state)) return 'ok'; if (s.state === 'error') return 'bad'; return 'warn'; }
75
76
  function stateText(name, s) {
76
77
  if (!s) return '关';
77
78
  if (s.progress && s.progress.text) return s.progress.text + (s.progress.pct != null ? ' · ' + s.progress.pct + '%' : '');
78
79
  if (s.slowBoot) return '开机较慢,还在开';
79
80
  if (name === 'qq' && s.connected) return '已登录';
80
- return ({ ready: '就绪', on: '开着', 'needs-license': '等待安装', downloading: '下载中', booting: '开机中', starting: '准备中', error: '出错', 'shell-only': '部分就绪', stopped: '没在运行', idle: '没在运行', installing: '安装中', 'not-installed': '没装', connecting: '连接中', 'needs-account': '没填她的 QQ 号', connected: '已连接', installed: '已安装', reconnecting: '重连中', blocked: '被拦下' })[s.state] || s.state || '';
81
+ return ({ off: '已关机', ready: '就绪', on: '开着', 'needs-license': '等待安装', downloading: '下载中', booting: '开机中', starting: '准备中', error: '出错', 'shell-only': '部分就绪', stopped: '没在运行', idle: '没在运行', installing: '安装中', 'not-installed': '没装', connecting: '连接中', 'needs-account': '没填她的 QQ 号', connected: '已连接', installed: '已安装', reconnecting: '重连中', blocked: '被拦下', cooling: '被限流,后台在等' })[s.state] || s.state || '';
81
82
  }
82
83
  async function loadStatus() {
83
84
  try {
84
85
  const j = await api('GET', '/status');
85
86
  $('ver').textContent = 'dsh-dot ' + j.version;
86
87
  $('badges').innerHTML = Object.entries(j.status).map(([k, s]) => '<span class="badge ' + level(s) + '" title="' + esc(s && s.error || '') + '"><span class="dotc"></span>' + NAMES[k] + ':' + esc(stateText(k, s)) + '</span>').join('');
88
+ renderPower(j.status);
87
89
  showFileErr(j.fileError);
88
90
  } catch (e) { $('badges').innerHTML = '<span class="badge bad"><span class="dotc"></span>' + esc(e.message) + '</span>'; }
89
91
  }
92
+ // The power switches: her phone and her computer, on or off at once; the choice survives dsh restarts.
93
+ // Both are watched here in dsh (the live views), not in windows of their own.
94
+ let powerBusy = '';
95
+ function renderPower(st) {
96
+ const one = (k, label) => {
97
+ const x = st[k];
98
+ if (!x) return '';
99
+ const on = x.state !== 'off';
100
+ return '<span class="badge">' + label + ' <button class="btn" data-power="' + k + '" data-on="' + (on ? '0' : '1') + '"' + (powerBusy === k ? ' disabled' : '') + '>' + (powerBusy === k ? '…' : on ? '关机' : '开机') + '</button>'
101
+ + (on && x.liveViewUrl ? ' <a href="' + esc(x.liveViewUrl) + '" target="_blank" rel="noopener">看画面</a>' : '') + '</span>';
102
+ };
103
+ $('power').innerHTML = one('phone', '手机') + one('computer', '电脑');
104
+ for (const b of document.querySelectorAll('[data-power]')) b.onclick = async () => {
105
+ const k = b.dataset.power, on = b.dataset.on === '1';
106
+ if (!on && k === 'computer' && st.qq && !confirm('关掉电脑,她的 QQ 也会下线(QQ 跑在这台电脑里)。继续?')) return;
107
+ powerBusy = k; renderPower(st);
108
+ try { const j = await api('POST', '/action/' + k + '-power', { on }); $('powerMsg').className = 'msg ok'; $('powerMsg').textContent = j.message; }
109
+ catch (e) { $('powerMsg').className = 'msg err'; $('powerMsg').textContent = e.message; }
110
+ powerBusy = ''; loadStatus();
111
+ };
112
+ }
90
113
  function showFileErr(err) {
91
114
  const b = $('fileErr');
92
115
  if (!err) { b.hidden = true; return; }
@@ -241,6 +264,7 @@ function renderStatus(st) {
241
264
  phone: (x) => [x.serial && '设备 ' + x.serial, x.api && 'Android API ' + x.api + (x.abi ? ' (' + x.abi + ')' : ''), x.storage && x.storage.gb && '存储 ' + x.storage.gb + ' GB'],
242
265
  computer: (x) => [x.backend && '后端 ' + x.backend, x.stage && '阶段 ' + x.stage],
243
266
  qq: (x) => [x.connected ? '已连接' : '未连接', x.qq && '她的号 ' + x.qq, x.owner && '你的号 ' + x.owner],
267
+ llm: (x) => [x.pacing ? '后台每分钟最多 ' + x.backgroundRpm + ' 次' : '后台不限速'].concat((x.providers || []).map((p) => p.provider + ':近一分钟 ' + p.lastMinute + ' 次,后台上限 ' + p.cap + (p.coolingUntil ? ',等到 ' + new Date(p.coolingUntil).toLocaleTimeString() : '') + (p.waiting ? ',' + p.waiting + ' 个在等' : '') + ',压后过 ' + p.delayed + ' 次,429 共 ' + p.rateLimited + ' 次')),
244
268
  compat: (x) => [x.qqBridge ? 'qq-bridge:' + x.qqBridge.root + '(模式 ' + x.qqBridge.mode + (x.qqBridge.bots && x.qqBridge.bots.length ? ',机器人 ' + x.qqBridge.bots.join('/') : '') + ')' : '没发现 qq-bridge'],
245
269
  };
246
270
  $('statusCards').className = '';
@@ -0,0 +1,31 @@
1
+ // The entry dsh-dot starts mobile-mcp through (see mountMcp in phone.js).
2
+ // On Windows the server runs without a console (inside the desktop app it is Electron running as
3
+ // Node), and it calls adb with execFileSync for every tap and screenshot: a console program started
4
+ // from a console-less parent with no `windowsHide` opens its own console window each time. So every
5
+ // child this process starts is hidden by default; a caller that sets windowsHide itself keeps its choice.
6
+ 'use strict';
7
+ const NAMES = ['spawn', 'spawnSync', 'exec', 'execSync', 'execFile', 'execFileSync', 'fork'];
8
+ const plain = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
9
+
10
+ /** Wrap `cp`'s process starters so `windowsHide` defaults to true. Returns `cp`. */
11
+ function hideChildWindows(cp) {
12
+ for (const name of NAMES) {
13
+ const original = cp[name];
14
+ if (typeof original !== 'function' || original.dshHidden) continue;
15
+ const wrapped = function (...args) {
16
+ const at = args.findIndex((a, i) => i > 0 && plain(a));
17
+ if (at >= 0) args[at] = { windowsHide: true, ...args[at] };
18
+ else { const cb = typeof args[args.length - 1] === 'function' ? args.length - 1 : args.length; args.splice(cb, 0, { windowsHide: true }); }
19
+ return original.apply(this, args);
20
+ };
21
+ wrapped.dshHidden = true;
22
+ cp[name] = wrapped;
23
+ }
24
+ return cp;
25
+ }
26
+ module.exports = { hideChildWindows };
27
+
28
+ if (require.main === module) {
29
+ if (process.platform === 'win32') hideChildWindows(require('node:child_process'));
30
+ require(process.env.DSH_DOT_MOBILE_MCP_ENTRY);
31
+ }