@linxin666/dsh-liangshen 0.3.19 → 0.3.21

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/lib/client.js ADDED
@@ -0,0 +1,640 @@
1
+ window.__ModuleLoader__.load({
2
+ id: "@linxin666/dsh-liangshen",
3
+ factory: (require) => {
4
+ var module = { exports: {} };
5
+ var exports = module.exports;
6
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
7
+ let react = require("react");
8
+ let react_jsx_runtime = require("react/jsx-runtime");
9
+ //#region \0dsh-css:packages/dsh-liangshen/src/client/LiangShenLever.module.css.mjs
10
+ const css = ".hYkIZq_lever{align-items:center;font-size:12px;line-height:1;display:inline-flex;position:relative}.hYkIZq_control{color:var(--dsw-alias-label-secondary,inherit);font:inherit;cursor:pointer;touch-action:none;background:0 0;border:none;border-radius:999px;align-items:center;gap:6px;margin:0;padding:2px 8px 2px 4px;transition:background-color .16s,color .16s;display:inline-flex}.hYkIZq_control:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover,#0000000f)}.hYkIZq_control:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,currentColor);outline-offset:2px}.hYkIZq_control:disabled{cursor:default;opacity:.55}.hYkIZq_track{background:linear-gradient(#2b2f36,#14161a);border-radius:8px;justify-content:center;align-items:flex-end;width:24px;height:28px;display:inline-flex;position:relative;overflow:hidden;box-shadow:inset 0 0 0 1px #ffffff24,inset 0 6px 10px #0000008c}.hYkIZq_track:after{content:\"\";background:#ffffff38;border-radius:2px;height:3px;position:absolute;bottom:2px;left:3px;right:3px}.hYkIZq_arm{transform-origin:50% 24px;width:16px;height:26px;margin-left:-8px;transition:transform .34s cubic-bezier(.2,1.7,.32,1);position:absolute;bottom:2px;left:50%;transform:rotate(-24deg)}.hYkIZq_lever[data-state=on] .hYkIZq_arm{transform:rotate(24deg)}.hYkIZq_rod{fill:#c9ced6;transition:fill .2s}.hYkIZq_knob{fill:#e04a33;stroke:#00000059;stroke-width:.6px;transition:fill .2s}.hYkIZq_lever[data-state=on] .hYkIZq_rod{fill:#ffd76a}.hYkIZq_lever[data-state=on] .hYkIZq_knob{fill:#f7c637}.hYkIZq_lever[data-state=on] .hYkIZq_track{box-shadow:inset 0 0 0 1px var(--dsw-alias-border-l3,#0000001f), 0 0 10px #f7c6378c}.hYkIZq_lever[data-busy=true] .hYkIZq_arm{animation:.42s ease-in-out infinite hYkIZq_lever-jitter}.hYkIZq_readout{white-space:nowrap;letter-spacing:.02em;min-width:4.4em}.hYkIZq_lever[data-state=on] .hYkIZq_readout{color:var(--dsw-alias-label-primary,inherit);font-weight:600}.hYkIZq_error{background:var(--dsw-alias-bg-layer-3,#0000000f);max-width:22ch;color:var(--dsw-alias-state-error-primary,#c0392b);text-overflow:ellipsis;white-space:nowrap;border-radius:6px;margin-left:4px;padding:2px 6px;overflow:hidden}.hYkIZq_burst{z-index:60;pointer-events:none;place-items:center;display:grid;position:fixed;inset:0;overflow:hidden}.hYkIZq_flash{background:radial-gradient(circle at 50% 62%,#fff6c8eb,#ffd04a73 38%,#ffa00000 72%);animation:.72s ease-out both hYkIZq_burst-flash;position:absolute;inset:0}.hYkIZq_ring{border:6px solid #ffd65ad9;border-radius:50%;width:220px;height:220px;animation:1.1s cubic-bezier(.16,.9,.3,1) both hYkIZq_burst-ring;position:absolute}.hYkIZq_ring:nth-of-type(2){animation-delay:.12s}.hYkIZq_ring:nth-of-type(3){animation-delay:.24s}.hYkIZq_banner{color:#ffe9a8;text-align:center;background:linear-gradient(160deg,#181004f0,#402604eb);border-radius:22px;flex-direction:column;align-items:center;gap:6px;padding:22px 40px;animation:.6s cubic-bezier(.16,1.5,.3,1) both hYkIZq_banner-slam,.5s ease-in-out .6s 2 hYkIZq_banner-shake;display:flex;position:relative;box-shadow:0 0 0 2px #ffd65abf,0 24px 70px #00000073}.hYkIZq_bannerName{letter-spacing:.16em;background:linear-gradient(#fff6d0,#ffc93c 55%,#b8730a);color:#0000;text-shadow:0 0 24px #ffc93ca6;-webkit-background-clip:text;background-clip:text;font-size:44px;font-weight:800}.hYkIZq_line{letter-spacing:.08em;font-size:15px}.hYkIZq_code{font-family:var(--dsw-font-mono,ui-monospace, SFMono-Regular, Menlo, monospace);letter-spacing:.06em;opacity:.82;font-size:11px;animation:.5s steps(2,end) 6 hYkIZq_code-blink}.hYkIZq_sparks{place-items:center;display:grid;position:absolute;inset:0}.hYkIZq_spark{transform-origin:50%;background:linear-gradient(#ffeca0f2,#ffaa1400);border-radius:3px;grid-area:1/1;width:3px;height:30px;animation:.95s ease-out both hYkIZq_spark-fly}@keyframes hYkIZq_lever-jitter{0%,to{transform:rotate(-22deg)}50%{transform:rotate(-8deg)}}@keyframes hYkIZq_burst-flash{0%{opacity:0;transform:scale(.6)}18%{opacity:1}to{opacity:0;transform:scale(1.5)}}@keyframes hYkIZq_burst-ring{0%{opacity:.9;transform:scale(.2)}to{opacity:0;transform:scale(3.4)}}@keyframes hYkIZq_banner-slam{0%{opacity:0;transform:scale(3.6)rotate(-9deg)}55%{opacity:1;transform:scale(.94)rotate(1.5deg)}75%{transform:scale(1.06)rotate(-1deg)}to{opacity:1;transform:scale(1)rotate(0)}}@keyframes hYkIZq_banner-shake{0%,to{transform:translate(0)}25%{transform:translate(-9px)rotate(-.6deg)}75%{transform:translate(9px)rotate(.6deg)}}@keyframes hYkIZq_spark-fly{0%{opacity:0;transform:translateY(-40px)scaleY(.3)}18%{opacity:1}to{opacity:0;transform:translateY(-300px)scaleY(1.15)}}@keyframes hYkIZq_code-blink{0%,to{opacity:.35}50%{opacity:1}}@media (prefers-reduced-motion:reduce){.hYkIZq_arm,.hYkIZq_control,.hYkIZq_rod,.hYkIZq_knob{transition:none}.hYkIZq_lever[data-busy=true] .hYkIZq_arm,.hYkIZq_flash,.hYkIZq_ring,.hYkIZq_banner,.hYkIZq_code,.hYkIZq_spark{animation:none}.hYkIZq_burst{animation:.3s ease-out both hYkIZq_burst-flash}.hYkIZq_spark{display:none}}";
11
+ const tagId = "@linxin666/dsh-liangshen/packages/dsh-liangshen/src/client/LiangShenLever.module.css";
12
+ if (typeof document !== "undefined" && document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") === null) {
13
+ const tag = document.createElement("style");
14
+ tag.dataset.plugin = "@linxin666/dsh-liangshen";
15
+ tag.dataset.pluginCss = tagId;
16
+ tag.textContent = css;
17
+ document.head.appendChild(tag);
18
+ }
19
+ var LiangShenLever_module_css_default = {
20
+ "arm": "hYkIZq_arm",
21
+ "banner": "hYkIZq_banner",
22
+ "banner-shake": "hYkIZq_banner-shake",
23
+ "banner-slam": "hYkIZq_banner-slam",
24
+ "bannerName": "hYkIZq_bannerName",
25
+ "burst": "hYkIZq_burst",
26
+ "burst-flash": "hYkIZq_burst-flash",
27
+ "burst-ring": "hYkIZq_burst-ring",
28
+ "code": "hYkIZq_code",
29
+ "code-blink": "hYkIZq_code-blink",
30
+ "control": "hYkIZq_control",
31
+ "error": "hYkIZq_error",
32
+ "flash": "hYkIZq_flash",
33
+ "knob": "hYkIZq_knob",
34
+ "lever": "hYkIZq_lever",
35
+ "lever-jitter": "hYkIZq_lever-jitter",
36
+ "line": "hYkIZq_line",
37
+ "readout": "hYkIZq_readout",
38
+ "ring": "hYkIZq_ring",
39
+ "rod": "hYkIZq_rod",
40
+ "spark": "hYkIZq_spark",
41
+ "spark-fly": "hYkIZq_spark-fly",
42
+ "sparks": "hYkIZq_sparks",
43
+ "track": "hYkIZq_track"
44
+ };
45
+ //#endregion
46
+ //#region src/client/LiangShenLever.tsx
47
+ /**
48
+ * The homepage lever: a slot-machine arm beside the model selector.
49
+ *
50
+ * Pull it down and the session about to start becomes LiangShen mode; push it
51
+ * up and the preset the user was on comes back. The arm tracks the real preset
52
+ * (never a local guess), and the theatrical part — flash, shockwave, sparks,
53
+ * and the three-language jackpot banner — plays only once the switch actually
54
+ * landed, so a refused switch never celebrates.
55
+ *
56
+ * The component is pure: every fact and verb arrives through the injected face.
57
+ * A pointer drag past {@link DRAG_THRESHOLD_PX} counts as a pull or a push; a
58
+ * plain click or a keyboard activation toggles instead.
59
+ */
60
+ /** Vertical travel that separates a pull or push from a plain click. */
61
+ const DRAG_THRESHOLD_PX = 14;
62
+ /** How long the burst overlay stays mounted, matching its CSS duration. */
63
+ const BURST_MS = 2200;
64
+ /** Jackpot lines that read the same in every language. */
65
+ const BINARY = "01001100 01001001 01000001 01001110 01000111";
66
+ const MORSE = "-.. . . .--. ... . . -.- / .... .- .-. -. . ... ...";
67
+ /** One spark per spoke of the burst. */
68
+ const SPARKS = Array.from({ length: 14 }, (_, index) => index);
69
+ /** Slot entry for `conversation.input.right` (left of the model selector). */
70
+ function LiangShenLever(face) {
71
+ const { state, restoreLabel, busy, error, burst } = (0, react.useSyncExternalStore)(face.store.subscribe, face.store.getSnapshot);
72
+ const [burstKey, setBurstKey] = (0, react.useState)(0);
73
+ const seen = (0, react.useRef)(burst);
74
+ const drag = (0, react.useRef)(void 0);
75
+ const actionable = !busy && (state === "on" || state === "off");
76
+ const on = state === "on";
77
+ const errorText = error === void 0 ? void 0 : error.kind === "locked" ? face.t("lever.failed.locked") : error.kind === "missing" ? face.t("lever.failed.missing") : error.kind === "timeout" ? face.t("lever.failed.timeout") : face.t("lever.failed.failed", { reason: error.reason });
78
+ (0, react.useEffect)(() => {
79
+ if (burst > seen.current) setBurstKey(burst);
80
+ seen.current = burst;
81
+ }, [burst]);
82
+ (0, react.useEffect)(() => {
83
+ if (burstKey === 0) return;
84
+ const timer = setTimeout(() => {
85
+ setBurstKey(0);
86
+ }, BURST_MS);
87
+ return () => {
88
+ clearTimeout(timer);
89
+ };
90
+ }, [burstKey]);
91
+ if (state === "locked" || state === "missing") return null;
92
+ const toggle = () => {
93
+ if (!actionable) return;
94
+ if (on) face.push();
95
+ else face.pull();
96
+ };
97
+ const onPointerDown = (event) => {
98
+ if (!actionable) return;
99
+ drag.current = {
100
+ y: event.clientY,
101
+ fired: false
102
+ };
103
+ event.currentTarget.setPointerCapture?.(event.pointerId);
104
+ };
105
+ const onPointerMove = (event) => {
106
+ const pending = drag.current;
107
+ if (pending === void 0 || pending.fired) return;
108
+ const travel = event.clientY - pending.y;
109
+ if (travel >= DRAG_THRESHOLD_PX) {
110
+ pending.fired = true;
111
+ face.pull();
112
+ } else if (travel <= -14) {
113
+ pending.fired = true;
114
+ face.push();
115
+ }
116
+ };
117
+ const onPointerUp = () => {
118
+ const pending = drag.current;
119
+ drag.current = void 0;
120
+ if (pending === void 0 || pending.fired) return;
121
+ toggle();
122
+ };
123
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
124
+ className: LiangShenLever_module_css_default.lever,
125
+ "data-dsh-plugin": "liangshen",
126
+ "data-dsh-part": "lever",
127
+ "data-state": state,
128
+ "data-busy": busy ? "true" : void 0,
129
+ children: [
130
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
131
+ type: "button",
132
+ role: "switch",
133
+ "aria-checked": on,
134
+ "aria-label": face.t("lever.a11y"),
135
+ "aria-busy": busy,
136
+ title: hint(face, state, restoreLabel),
137
+ className: LiangShenLever_module_css_default.control,
138
+ disabled: !actionable,
139
+ onPointerDown,
140
+ onPointerMove,
141
+ onPointerUp,
142
+ onPointerCancel: onPointerUp,
143
+ onClick: (event) => {
144
+ if (event.detail === 0) toggle();
145
+ },
146
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
147
+ className: LiangShenLever_module_css_default.track,
148
+ "data-dsh-part": "lever-track",
149
+ "aria-hidden": "true",
150
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("svg", {
151
+ className: LiangShenLever_module_css_default.arm,
152
+ "data-dsh-part": "lever-arm",
153
+ viewBox: "0 0 16 26",
154
+ "aria-hidden": "true",
155
+ children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("rect", {
156
+ className: LiangShenLever_module_css_default.rod,
157
+ x: "6.5",
158
+ y: "3.5",
159
+ width: "3",
160
+ height: "21",
161
+ rx: "1.5"
162
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("circle", {
163
+ className: LiangShenLever_module_css_default.knob,
164
+ cx: "8",
165
+ cy: "4",
166
+ r: "3.4"
167
+ })]
168
+ })
169
+ }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
170
+ className: LiangShenLever_module_css_default.readout,
171
+ children: busy ? face.t("lever.busy") : on ? face.t("lever.state.on") : face.t("lever.state.off")
172
+ })]
173
+ }),
174
+ errorText !== void 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
175
+ className: LiangShenLever_module_css_default.error,
176
+ role: "status",
177
+ title: errorText,
178
+ children: errorText
179
+ }),
180
+ burstKey !== 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Burst, { face }, burstKey)
181
+ ]
182
+ });
183
+ }
184
+ /** The hover hint: what this gesture would do right now. */
185
+ function hint(face, state, restoreLabel) {
186
+ if (state === "locked") return face.t("lever.hint.locked");
187
+ if (state === "missing") return face.t("lever.hint.missing");
188
+ if (state === "on") return face.t("lever.hint.push", { preset: restoreLabel === "" ? face.t("lever.state.off") : restoreLabel });
189
+ return face.t("lever.hint.pull");
190
+ }
191
+ /** The jackpot overlay: flash, shockwave rings, sparks, and the banner. */
192
+ function Burst({ face }) {
193
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
194
+ className: LiangShenLever_module_css_default.burst,
195
+ "data-dsh-part": "lever-burst",
196
+ "aria-hidden": "true",
197
+ children: [
198
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: LiangShenLever_module_css_default.flash }),
199
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: LiangShenLever_module_css_default.ring }),
200
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: LiangShenLever_module_css_default.ring }),
201
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: LiangShenLever_module_css_default.ring }),
202
+ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
203
+ className: LiangShenLever_module_css_default.banner,
204
+ "data-dsh-part": "lever-banner",
205
+ children: [
206
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
207
+ className: LiangShenLever_module_css_default.bannerName,
208
+ children: face.t("lever.name")
209
+ }),
210
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
211
+ className: LiangShenLever_module_css_default.line,
212
+ children: face.t("burst.line1")
213
+ }),
214
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
215
+ className: LiangShenLever_module_css_default.line,
216
+ children: face.t("burst.line2")
217
+ }),
218
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
219
+ className: LiangShenLever_module_css_default.code,
220
+ children: BINARY
221
+ }),
222
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
223
+ className: LiangShenLever_module_css_default.code,
224
+ children: MORSE
225
+ })
226
+ ]
227
+ }),
228
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
229
+ className: LiangShenLever_module_css_default.sparks,
230
+ children: SPARKS.map((spoke) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("i", {
231
+ className: LiangShenLever_module_css_default.spark,
232
+ style: { rotate: `${spoke * (360 / SPARKS.length)}deg` }
233
+ }, spoke))
234
+ })
235
+ ]
236
+ });
237
+ }
238
+ //#endregion
239
+ //#region \0dsh-store-engine
240
+ const platform = ["@deepseek-ai/dsh-client", "-store"].join("");
241
+ const legacy = ["@deepseek-ai/dsh-client-runtime", "/client"].join("");
242
+ let engine;
243
+ try {
244
+ engine = require(platform);
245
+ } catch {
246
+ engine = require(legacy);
247
+ }
248
+ const createSnapshotStore = engine.createSnapshotStore;
249
+ engine.defineStore;
250
+ engine.shallowEqual;
251
+ //#endregion
252
+ //#region src/core/lever.ts
253
+ /**
254
+ * LiangShen lever logic — framework-free, no DOM, compiled by both programs.
255
+ *
256
+ * The lever is a homepage control beside the model selector: pulling it down
257
+ * turns the current session's preset into LiangShen mode, pushing it up
258
+ * restores the preset the user was on before. The facts it needs are the
259
+ * current session's preset, whether that session may still change preset at
260
+ * all, and the preset roster; the decisions are pure so they can be tested
261
+ * without the browser runtime.
262
+ *
263
+ * A preset is fixed once a session starts (the host refuses the switch with
264
+ * `agent-preset/locked`), so the lever is only meaningful while the session is
265
+ * blank — which is exactly the new-session screen it renders on.
266
+ */
267
+ /** The preset id this lever turns on; also the preset directory name. */
268
+ const LIANGSHEN_PRESET_ID = "liangshen";
269
+ /** Resolve what the lever shows for one set of facts. */
270
+ function leverState(facts) {
271
+ if (!facts.available.includes("liangshen")) return "missing";
272
+ if (!facts.blank) return "locked";
273
+ return facts.agentPreset === "liangshen" ? "on" : "off";
274
+ }
275
+ /**
276
+ * The preset a push-up restores: the preset the user was on before pulling the
277
+ * lever, else the deployment default. A candidate is skipped when it is the
278
+ * LiangShen preset itself (restoring it would be a no-op) or when the roster
279
+ * no longer supplies it.
280
+ */
281
+ function restoreTarget(facts) {
282
+ for (const candidate of [facts.previous, facts.fallback]) {
283
+ if (candidate === void 0 || candidate === "liangshen") continue;
284
+ if (facts.available.includes(candidate)) return candidate;
285
+ }
286
+ }
287
+ /** Whether the lever can act at all in its current state. */
288
+ function isActionable(state) {
289
+ return state === "on" || state === "off";
290
+ }
291
+ /** Read the preset a session summary reports, when it reports one. */
292
+ function presetOf(session) {
293
+ const value = session?.projectionValues?.["agentPreset"];
294
+ return typeof value === "string" ? value : void 0;
295
+ }
296
+ /** The lever controller: roster read, session facts, and the preset switch. */
297
+ var LeverController = class {
298
+ ctx;
299
+ store;
300
+ /**
301
+ * The browser services this controller reads, resolved defensively: the
302
+ * context proxy throws on any service the fiber did not inject, so a
303
+ * deployment that cannot answer one of them must leave the lever inert
304
+ * rather than take the plugin (and the composer row) down with it.
305
+ */
306
+ sessions;
307
+ remote;
308
+ /** Roster rows as last read; empty until the first read lands. */
309
+ rows = [];
310
+ /** The preset the user was on before the last pull-down. */
311
+ previous;
312
+ loading = false;
313
+ /** Ceiling on one in-flight switch, so a lost Remote answer cannot hang the row. */
314
+ selectTimeoutMs;
315
+ disposers = [];
316
+ constructor(ctx, options = {}) {
317
+ this.ctx = ctx;
318
+ this.selectTimeoutMs = options.selectTimeoutMs ?? 1e4;
319
+ this.sessions = readService(() => ctx.sessions);
320
+ this.remote = readService(() => ctx.remote.agentPresets);
321
+ this.store = createSnapshotStore({
322
+ state: "off",
323
+ restoreLabel: "",
324
+ busy: false,
325
+ burst: 0
326
+ });
327
+ }
328
+ /** The snapshot store the view subscribes to. */
329
+ snapshot() {
330
+ return this.store;
331
+ }
332
+ /** Follow the roster and the current session, then read the roster once. */
333
+ start() {
334
+ const list = this.sessions?.list;
335
+ if (list !== void 0) this.disposers.push(list.subscribe(() => {
336
+ this.refresh();
337
+ }));
338
+ const remote = readService(() => this.ctx.remote);
339
+ if (typeof remote?.$on === "function") this.disposers.push(remote.$on("settings/document-updated", (ns) => {
340
+ if (ns === "agent-presets") this.load();
341
+ }));
342
+ this.refresh();
343
+ if (this.remote !== void 0) this.load();
344
+ }
345
+ /** Release every subscription. Idempotent. */
346
+ dispose() {
347
+ for (const dispose of this.disposers.splice(0)) dispose();
348
+ }
349
+ /** The inject face handed to the slot entry. */
350
+ face() {
351
+ return {
352
+ store: this.store,
353
+ pull: () => {
354
+ this.toggle("down");
355
+ },
356
+ push: () => {
357
+ this.toggle("up");
358
+ },
359
+ t: (key, vars) => {
360
+ const translate = readService(() => this.ctx.locale.bind("liangshen"));
361
+ return translate === void 0 ? key : translate(key, vars);
362
+ }
363
+ };
364
+ }
365
+ /** Read the roster; a refusal leaves the lever as it was. */
366
+ async load() {
367
+ if (this.loading) return;
368
+ const remote = this.remote;
369
+ if (remote === void 0) return;
370
+ this.loading = true;
371
+ try {
372
+ const result = await remote.list();
373
+ if (result.ok) this.rows = result.value.presets;
374
+ } catch {} finally {
375
+ this.loading = false;
376
+ this.refresh();
377
+ }
378
+ }
379
+ /**
380
+ * Recompute the snapshot from the current session and the last roster read.
381
+ * Only the derived fields are written: an in-flight switch, the last refusal,
382
+ * and the burst counter belong to the gesture, not to a session refresh.
383
+ */
384
+ refresh() {
385
+ const facts = this.facts();
386
+ const snapshot = this.store.getSnapshot();
387
+ const state = leverState(facts);
388
+ const target = restoreTarget(facts);
389
+ const restoreLabel = target === void 0 ? "" : this.labelOf(target);
390
+ if (snapshot.state === state && snapshot.restoreLabel === restoreLabel) return;
391
+ this.store.set({
392
+ ...snapshot,
393
+ state,
394
+ restoreLabel
395
+ });
396
+ }
397
+ /** The verb behind one gesture direction. */
398
+ async toggle(direction) {
399
+ const snapshot = this.store.getSnapshot();
400
+ if (snapshot.busy) return;
401
+ const remote = this.remote;
402
+ if (remote === void 0) return;
403
+ const facts = this.facts();
404
+ if (!isActionable(leverState(facts))) return;
405
+ const target = direction === "down" ? LIANGSHEN_PRESET_ID : restoreTarget(facts);
406
+ const sessionId = this.currentSessionId();
407
+ if (target === void 0 || sessionId === void 0) return;
408
+ if (facts.agentPreset === target) {
409
+ this.refresh();
410
+ return;
411
+ }
412
+ this.store.set({
413
+ ...snapshot,
414
+ busy: true,
415
+ error: void 0
416
+ });
417
+ let result;
418
+ try {
419
+ result = await withTimeout(remote.select(sessionId, target), this.selectTimeoutMs);
420
+ } catch (error) {
421
+ const mapped = error instanceof SwitchTimeout ? { kind: "timeout" } : {
422
+ kind: "failed",
423
+ reason: message(error)
424
+ };
425
+ this.store.set({
426
+ ...this.store.getSnapshot(),
427
+ busy: false,
428
+ error: mapped
429
+ });
430
+ this.refresh();
431
+ return;
432
+ }
433
+ if (!result.ok) {
434
+ this.store.set({
435
+ ...this.store.getSnapshot(),
436
+ busy: false,
437
+ error: refusal(result.error)
438
+ });
439
+ this.refresh();
440
+ return;
441
+ }
442
+ this.previous = direction === "down" ? facts.agentPreset : void 0;
443
+ const next = this.store.getSnapshot();
444
+ this.store.set({
445
+ ...next,
446
+ busy: false,
447
+ error: void 0,
448
+ burst: direction === "down" ? next.burst + 1 : next.burst
449
+ });
450
+ this.refresh();
451
+ }
452
+ /** The facts one decision reads, from the live session and the roster. */
453
+ facts() {
454
+ const summary = this.currentSession();
455
+ const available = this.rows.filter((row) => row.broken === void 0).map((row) => row.id);
456
+ const fallback = this.rows.find((row) => row.isDefault)?.id;
457
+ return {
458
+ blank: summary?.blank === true,
459
+ agentPreset: presetOf(summary),
460
+ available,
461
+ fallback,
462
+ previous: this.previous
463
+ };
464
+ }
465
+ currentSessionId() {
466
+ const current = this.sessions?.list.getSnapshot().current;
467
+ return current === void 0 ? void 0 : String(current);
468
+ }
469
+ currentSession() {
470
+ const state = this.sessions?.list.getSnapshot();
471
+ const current = state?.current;
472
+ if (state === void 0 || current === void 0) return void 0;
473
+ return state.byId[current];
474
+ }
475
+ /** Display name of one preset id, falling back to the id itself. */
476
+ labelOf(id) {
477
+ return this.rows.find((candidate) => candidate.id === id)?.name ?? id;
478
+ }
479
+ };
480
+ /** Raised when one switch outlives {@link SELECT_TIMEOUT_MS}. */
481
+ var SwitchTimeout = class extends Error {
482
+ constructor() {
483
+ super("the preset switch did not answer in time");
484
+ this.name = "SwitchTimeout";
485
+ }
486
+ };
487
+ /** Resolve with `work`, or reject with a {@link SwitchTimeout} after `ms`. */
488
+ function withTimeout(work, ms) {
489
+ return new Promise((resolve, reject) => {
490
+ const timer = setTimeout(() => {
491
+ reject(new SwitchTimeout());
492
+ }, ms);
493
+ work.then((value) => {
494
+ clearTimeout(timer);
495
+ resolve(value);
496
+ }, (error) => {
497
+ clearTimeout(timer);
498
+ reject(error instanceof Error ? error : new Error(String(error)));
499
+ });
500
+ });
501
+ }
502
+ /**
503
+ * Read one context service, treating the proxy's "without inject" refusal (and
504
+ * any other resolution fault) as the service being absent.
505
+ */
506
+ function readService(read) {
507
+ try {
508
+ return read();
509
+ } catch {
510
+ return;
511
+ }
512
+ }
513
+ /** The host's own reason for a refusal, mapped to lever copy. */
514
+ function refusal(error) {
515
+ if (error.code === "agent-preset/locked") return { kind: "locked" };
516
+ if (error.code === "agent-preset/not-found") return { kind: "missing" };
517
+ const details = error.details;
518
+ if (typeof details === "object" && details !== null && typeof details.reason === "string") return {
519
+ kind: "failed",
520
+ reason: details.reason
521
+ };
522
+ return {
523
+ kind: "failed",
524
+ reason: error.message ?? error.code ?? "unknown"
525
+ };
526
+ }
527
+ function message(error) {
528
+ return error instanceof Error ? error.message : String(error);
529
+ }
530
+ //#endregion
531
+ //#region src/client/locales.ts
532
+ /**
533
+ * Lever copy: zh-first dictionary with an English counterpart. The zh side is
534
+ * the key-set source of truth; `packages/dsh-i18n` mirrors the keys into the
535
+ * centralized ru dictionary and `pnpm i18n:check` enforces the parity.
536
+ */
537
+ /** Lever copy, key source of truth. */
538
+ const zh = {
539
+ "lever.a11y": "梁神模式拨杆",
540
+ "lever.name": "梁神模式",
541
+ "lever.hint.pull": "拨下拨杆:开启「梁神模式」",
542
+ "lever.hint.push": "上拨拨杆:返回「{preset}」",
543
+ "lever.hint.locked": "会话已开始,模式不可再更改",
544
+ "lever.hint.missing": "未找到「梁神模式」预设,请先启用该插件行",
545
+ "lever.state.on": "梁神模式",
546
+ "lever.state.off": "普通模式",
547
+ "lever.busy": "正在切换…",
548
+ "lever.failed.locked": "会话已经开始,模式已锁定",
549
+ "lever.failed.missing": "没有找到「梁神模式」预设",
550
+ "lever.failed.timeout": "切换超时,请重试",
551
+ "lever.failed.failed": "切换失败:{reason}",
552
+ "burst.line1": "三秒,三辈子的代码",
553
+ "burst.line2": "文言文 · 二进制 · 摩斯电码"
554
+ };
555
+ /** English counterpart; the key set mirrors {@link zh} exactly. */
556
+ const en = {
557
+ "lever.a11y": "LiangShen mode lever",
558
+ "lever.name": "LiangShen mode",
559
+ "lever.hint.pull": "Pull the lever down to turn on LiangShen mode",
560
+ "lever.hint.push": "Push the lever up to return to \"{preset}\"",
561
+ "lever.hint.locked": "The session has started, so its mode can no longer change",
562
+ "lever.hint.missing": "The LiangShen preset is not installed; enable that plugin row first",
563
+ "lever.state.on": "LiangShen",
564
+ "lever.state.off": "Standard",
565
+ "lever.busy": "Switching…",
566
+ "lever.failed.locked": "The session already started, so the mode is locked",
567
+ "lever.failed.missing": "The LiangShen preset was not found",
568
+ "lever.failed.timeout": "The switch timed out; try again",
569
+ "lever.failed.failed": "Switch failed: {reason}",
570
+ "burst.line1": "Three seconds, three lifetimes of code",
571
+ "burst.line2": "Classical Chinese · Binary · Morse code"
572
+ };
573
+ //#endregion
574
+ //#region src/client/index.ts
575
+ /** Locale namespace this half owns. */
576
+ const NS = "liangshen";
577
+ /**
578
+ * Required client services: the slot registry, locale, sessions, and the roster
579
+ * Remote. Both `remote` and `remote.agentPresets` are declared: the context
580
+ * proxy refuses an uninjected service, and a nested service name does not imply
581
+ * its parent, so reading `ctx.remote.agentPresets` needs `remote` as well.
582
+ */
583
+ const inject = [
584
+ "slots",
585
+ "locale",
586
+ "sessions",
587
+ "remote",
588
+ "remote.agentPresets"
589
+ ];
590
+ /**
591
+ * Mount the lever: register the copy, follow the roster and the current
592
+ * session, and claim the composer tool row.
593
+ * @param ctx - the browser plugin context.
594
+ */
595
+ function apply(ctx) {
596
+ ctx.effect(() => {
597
+ try {
598
+ return ctx.locale.register(NS, {
599
+ zh,
600
+ en
601
+ });
602
+ } catch {
603
+ return () => {};
604
+ }
605
+ }, "liangshen: lever dictionaries");
606
+ const controller = new LeverController(ctx);
607
+ ctx.effect(() => () => controller.dispose(), "liangshen: lever controller");
608
+ try {
609
+ controller.start();
610
+ } catch {}
611
+ ctx.slots.inject("conversation.input.right", () => {
612
+ try {
613
+ const unregister = ctx.slots.register({
614
+ name: "conversation.input.right",
615
+ id: "liangshen-lever",
616
+ order: 20,
617
+ inject: () => controller.face()
618
+ }, LiangShenLever);
619
+ return () => {
620
+ unregister();
621
+ };
622
+ } catch {
623
+ return () => {};
624
+ }
625
+ });
626
+ }
627
+ //#endregion
628
+ exports.LIANGSHEN_PRESET_ID = LIANGSHEN_PRESET_ID;
629
+ exports.LeverController = LeverController;
630
+ exports.LiangShenLever = LiangShenLever;
631
+ exports.NS = NS;
632
+ exports.apply = apply;
633
+ exports.inject = inject;
634
+ exports.leverState = leverState;
635
+ exports.restoreTarget = restoreTarget;
636
+ return module.exports;
637
+ }
638
+ });
639
+
640
+ //# sourceMappingURL=client.js.map