@linxin666/dsh-usage 0.4.2 → 0.4.3
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 +104 -2
- package/lib/client.js.map +1 -1
- package/lib/index.js +180 -36
- package/lib/types/client/body-mutations.d.ts +4 -4
- package/lib/types/client/settings-entry-form.d.ts +49 -0
- package/lib/types/index.d.ts +61 -10
- package/lib/types/mount-once.d.ts +38 -7
- package/package.json +2 -2
- package/src/client/body-mutations.ts +4 -4
- package/src/client/index.ts +17 -7
- package/src/client/settings-entry-form.ts +142 -0
- package/src/index.ts +138 -43
- package/src/mount-once.ts +107 -13
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import z from "schemastery";
|
|
1
|
+
import z from "@deepseek-ai/schemastery";
|
|
2
2
|
import { mkdir, open, readFile, rename, unlink } from "node:fs/promises";
|
|
3
3
|
import { dirname, isAbsolute, join } from "node:path";
|
|
4
4
|
import "@deepseek-ai/cordis";
|
|
@@ -11,38 +11,111 @@ import { isAbsolute as isAbsolute$1, join as join$1 } from "node:path/posix";
|
|
|
11
11
|
* the loader accepts a standalone install of the same package side by side;
|
|
12
12
|
* without this guard the second instance would still re-register the same
|
|
13
13
|
* webserver routes, tools, settings namespaces, and system-prompt sections
|
|
14
|
-
* and fail the boot. mountOnce makes
|
|
15
|
-
*
|
|
16
|
-
* package name in the client module host).
|
|
14
|
+
* and fail the boot. mountOnce makes a mount of an already-mounted package a
|
|
15
|
+
* no-op for as long as the first instance lives (the browser half is already
|
|
16
|
+
* deduped by package name in the client module host).
|
|
17
|
+
*
|
|
18
|
+
* A no-op is only safe while the holder is ALIVE. The holder can be disposed
|
|
19
|
+
* long after the refused mount ran its course: the Host reloads a profile by
|
|
20
|
+
* creating the new loader entries before the old ones are torn down (a
|
|
21
|
+
* plugin-manager enable/disable/install write, a settings-driven row reload,
|
|
22
|
+
* HMR), so the new aggregate shell entry mounts its family plugin while the
|
|
23
|
+
* previous entry still owns the name. Dropping that refused mount lost the
|
|
24
|
+
* plugin for good - the previous entry then disposed its own mount, releasing
|
|
25
|
+
* the name with nobody left to take it, and the family row stayed listed as
|
|
26
|
+
* active while its host routes 404ed (the task-board panel showed
|
|
27
|
+
* "board.hostError.notMounted", no degraded record appeared, and only a Host
|
|
28
|
+
* restart recovered it).
|
|
29
|
+
*
|
|
30
|
+
* The refused mount is therefore QUEUED, not dropped, and replayed the moment
|
|
31
|
+
* the holder releases the name - if the waiting fiber is still alive then. The
|
|
32
|
+
* single-instance guarantee is unchanged: exactly one mount is live per
|
|
33
|
+
* package name, and the replay re-enters the guard so a later mount still
|
|
34
|
+
* dedupes against it.
|
|
17
35
|
*
|
|
18
36
|
* The registry rides a global symbol so two module instances of the same
|
|
19
|
-
* package (npm copy vs repository link) still share one verdict.
|
|
20
|
-
*
|
|
37
|
+
* package (npm copy vs repository link) still share one verdict. That symbol
|
|
38
|
+
* is a CROSS-REPOSITORY contract, not this file's private state: the four
|
|
39
|
+
* satellite packages (dsh-skins / dsh-pet / dsh-presets /
|
|
40
|
+
* dsh-community-plugins) are separate repositories carrying their own copy of
|
|
41
|
+
* this guard, rebuilt on their own schedule, so the value under `MOUNTED`
|
|
42
|
+
* must keep the shape every published copy reads (a `Set` of package names
|
|
43
|
+
* with `has`/`add`/`delete`). The wait queues this guard added therefore live
|
|
44
|
+
* under their own additive key, and the registry reads back a `Set` even when
|
|
45
|
+
* some other build left a different value there. Changing `MOUNTED`'s shape
|
|
46
|
+
* in place broke that contract once: a satellite's legacy copy created a
|
|
47
|
+
* `Set`, the family's new copy read it as a `Map`, and every family row
|
|
48
|
+
* mounted after it failed with "claims.get is not a function".
|
|
49
|
+
*
|
|
50
|
+
* cordis `ctx.effect` runs its callback immediately and treats the callback's
|
|
21
51
|
* return value as the fiber disposer, so the unmarker is returned, not run.
|
|
22
52
|
*/
|
|
53
|
+
/** Published cross-repository contract: package names currently mounted. */
|
|
23
54
|
const MOUNTED = Symbol.for("dsh-web.mounted-plugins");
|
|
55
|
+
/** Additive key this guard owns: refused mounts waiting for the name. */
|
|
56
|
+
const WAITERS = Symbol.for("dsh-web.mounted-plugins.waiters");
|
|
57
|
+
/**
|
|
58
|
+
* The shared name registry, always a `Set` whatever another build stored here:
|
|
59
|
+
* a foreign value (an interim shape, a hand-written global) must not take every
|
|
60
|
+
* family plugin down with it.
|
|
61
|
+
* @returns the process-wide set of mounted package names.
|
|
62
|
+
*/
|
|
24
63
|
function mountedSet() {
|
|
25
64
|
const registry = globalThis;
|
|
26
|
-
|
|
65
|
+
const existing = registry[MOUNTED];
|
|
66
|
+
if (existing instanceof Set) return existing;
|
|
67
|
+
const created = /* @__PURE__ */ new Set();
|
|
68
|
+
registry[MOUNTED] = created;
|
|
69
|
+
return created;
|
|
70
|
+
}
|
|
71
|
+
/** Queue per package name for mounts refused while a holder was alive. */
|
|
72
|
+
function mountWaiters() {
|
|
73
|
+
const registry = globalThis;
|
|
74
|
+
return registry[WAITERS] ??= /* @__PURE__ */ new Map();
|
|
27
75
|
}
|
|
28
76
|
/**
|
|
29
77
|
* Wrap a cordis plugin apply so the package runs at most once per process.
|
|
30
|
-
* The first mount registers normally and
|
|
31
|
-
*
|
|
78
|
+
* The first mount registers normally and releases the name when its fiber
|
|
79
|
+
* disposes; a mount refused while that name is held waits for the release and
|
|
80
|
+
* then runs, unless its own fiber disposes first.
|
|
32
81
|
* @param packageName - npm package identity shared by every install source.
|
|
33
82
|
* @param fn - the original plugin apply.
|
|
34
83
|
* @returns an apply of the same shape.
|
|
35
84
|
*/
|
|
36
85
|
function mountOnce(packageName, fn) {
|
|
37
|
-
|
|
86
|
+
const mount = (...args) => {
|
|
38
87
|
const mounted = mountedSet();
|
|
39
|
-
|
|
88
|
+
const ctx = args[0];
|
|
89
|
+
if (mounted.has(packageName)) {
|
|
90
|
+
const waiters = mountWaiters();
|
|
91
|
+
const queue = waiters.get(packageName) ?? [];
|
|
92
|
+
let alive = true;
|
|
93
|
+
const pending = { run: () => {
|
|
94
|
+
if (alive) mount(...args);
|
|
95
|
+
} };
|
|
96
|
+
ctx?.effect?.(() => () => {
|
|
97
|
+
alive = false;
|
|
98
|
+
const index = queue.indexOf(pending);
|
|
99
|
+
if (index >= 0) queue.splice(index, 1);
|
|
100
|
+
});
|
|
101
|
+
queue.push(pending);
|
|
102
|
+
waiters.set(packageName, queue);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
40
105
|
mounted.add(packageName);
|
|
41
|
-
|
|
106
|
+
ctx?.effect?.(() => () => {
|
|
42
107
|
mounted.delete(packageName);
|
|
108
|
+
const waiters = mountWaiters();
|
|
109
|
+
const queue = waiters.get(packageName);
|
|
110
|
+
if (queue === void 0) return;
|
|
111
|
+
waiters.delete(packageName);
|
|
112
|
+
for (const waiter of queue.splice(0)) queueMicrotask(() => {
|
|
113
|
+
waiter.run();
|
|
114
|
+
});
|
|
43
115
|
});
|
|
44
116
|
return fn(...args);
|
|
45
|
-
}
|
|
117
|
+
};
|
|
118
|
+
return mount;
|
|
46
119
|
}
|
|
47
120
|
//#endregion
|
|
48
121
|
//#region ../../node_modules/.pnpm/@deepseek-ai+dsh-brand@0.1.7-rc.2_@deepseek-ai+cordis@4.0.4/node_modules/@deepseek-ai/dsh-brand/lib/index.js
|
|
@@ -1711,16 +1784,48 @@ const inject = ["webServer"];
|
|
|
1711
1784
|
* family settings bridge (dsh-web-settings) resolves onto this row's entry id.
|
|
1712
1785
|
*/
|
|
1713
1786
|
const USAGE_SETTINGS_NAMESPACE = "dsh-usage";
|
|
1787
|
+
/**
|
|
1788
|
+
* Plugin config schema. Under the 0.1.7 settings model this schema IS the
|
|
1789
|
+
* entry's settings page: the Host derives one form per profile entry from it
|
|
1790
|
+
* and serves it only when at least one field is `volatile()`. The marker is
|
|
1791
|
+
* also what admits a write and what keeps the edit on the live path — the
|
|
1792
|
+
* Loader commits the new value into the field's reference and announces
|
|
1793
|
+
* `loader/volatile-update` on this fiber instead of remounting the row, so the
|
|
1794
|
+
* probe cycle, its ledger and its route registrations survive a settings save.
|
|
1795
|
+
*
|
|
1796
|
+
* The schema is left to inference rather than annotated with
|
|
1797
|
+
* `z<UsageConfigFields>`: a volatile field's parsed output is a reference while
|
|
1798
|
+
* its accepted input stays the plain value, so the two sides no longer share
|
|
1799
|
+
* one shape and the annotation would reject the schema the Host must be given.
|
|
1800
|
+
*/
|
|
1714
1801
|
const Config = z.object({
|
|
1715
|
-
enabled: z.boolean().default(true),
|
|
1716
|
-
pollIntervalSec: z.number().min(30).max(3600).default(60),
|
|
1717
|
-
retainDays: z.number().min(7).max(730).default(180)
|
|
1802
|
+
enabled: z.boolean().default(true).volatile(),
|
|
1803
|
+
pollIntervalSec: z.number().min(30).max(3600).default(60).volatile(),
|
|
1804
|
+
retainDays: z.number().min(7).max(730).default(180).volatile()
|
|
1718
1805
|
});
|
|
1806
|
+
/** Read one config field, following the live reference the schema produces. */
|
|
1807
|
+
function readConfigField(field) {
|
|
1808
|
+
if (field === void 0) return void 0;
|
|
1809
|
+
if (typeof field === "object" && field !== null && typeof field.get === "function") return field.get();
|
|
1810
|
+
return field;
|
|
1811
|
+
}
|
|
1812
|
+
/** Read one numeric field, falling back when the value is absent or not a usable number. */
|
|
1813
|
+
function readNumber(field, fallback) {
|
|
1814
|
+
const value = readConfigField(field);
|
|
1815
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
1816
|
+
}
|
|
1817
|
+
/**
|
|
1818
|
+
* Resolve the effective settings of one activation. Volatile references are
|
|
1819
|
+
* read here, so every re-arm observes the latest committed values rather than
|
|
1820
|
+
* whatever the row was activated with.
|
|
1821
|
+
* @param config - the config the Host passed to the activation.
|
|
1822
|
+
* @returns the resolved settings.
|
|
1823
|
+
*/
|
|
1719
1824
|
function resolveConfig(config) {
|
|
1720
1825
|
return {
|
|
1721
|
-
enabled: config?.enabled ?? true,
|
|
1722
|
-
pollIntervalSec:
|
|
1723
|
-
retainDays:
|
|
1826
|
+
enabled: readConfigField(config?.enabled) ?? true,
|
|
1827
|
+
pollIntervalSec: readNumber(config?.pollIntervalSec, 60),
|
|
1828
|
+
retainDays: readNumber(config?.retainDays, 180)
|
|
1724
1829
|
};
|
|
1725
1830
|
}
|
|
1726
1831
|
/**
|
|
@@ -1734,39 +1839,78 @@ let pendingStop;
|
|
|
1734
1839
|
/**
|
|
1735
1840
|
* Host body. The plugin's own Config schema above is the settings document the
|
|
1736
1841
|
* 0.1.7 Host serves for this row; the effective config arrives here as the
|
|
1737
|
-
* `config` argument
|
|
1738
|
-
*
|
|
1842
|
+
* `config` argument, and a settings save re-enters this body through the
|
|
1843
|
+
* volatile path rather than a row reload (see {@link Config}).
|
|
1739
1844
|
* @param ctx - host root context.
|
|
1740
1845
|
* @param config - the row's resolved configuration.
|
|
1741
1846
|
*/
|
|
1742
1847
|
const apply = mountOnce("@linxin666/dsh-usage", (ctx, config) => {
|
|
1743
|
-
const value = resolveConfig(config);
|
|
1744
|
-
if (!value.enabled) return;
|
|
1745
1848
|
let live = true;
|
|
1746
1849
|
let service;
|
|
1747
1850
|
let disposeRoutes;
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
next.start();
|
|
1851
|
+
/** True while this activation has a start queued behind a predecessor's flush. */
|
|
1852
|
+
let queued = false;
|
|
1853
|
+
/** Register the service's two routes; the returned disposer unregisters them. */
|
|
1854
|
+
const mountRoutes = (next) => {
|
|
1753
1855
|
const disposers = [makeUsageOverviewRoute(ctx, next), makeUsageRefreshRoute(ctx, next)].map((route) => ctx.webServer.register(route));
|
|
1754
|
-
|
|
1856
|
+
return () => {
|
|
1755
1857
|
for (const dispose of disposers) try {
|
|
1756
1858
|
dispose();
|
|
1757
1859
|
} catch {}
|
|
1758
1860
|
};
|
|
1759
1861
|
};
|
|
1862
|
+
/** Start a fresh activation, unless this fiber is down or one already runs. */
|
|
1863
|
+
const launch = () => {
|
|
1864
|
+
queued = false;
|
|
1865
|
+
if (!live || service !== void 0) return;
|
|
1866
|
+
const value = resolveConfig(config);
|
|
1867
|
+
if (!value.enabled) return;
|
|
1868
|
+
const next = new UsageService(ctx, value);
|
|
1869
|
+
service = next;
|
|
1870
|
+
next.start();
|
|
1871
|
+
disposeRoutes = mountRoutes(next);
|
|
1872
|
+
};
|
|
1873
|
+
/** Tear the running activation down, chaining its final flush for any successor. */
|
|
1874
|
+
const stop = () => {
|
|
1875
|
+
disposeRoutes?.();
|
|
1876
|
+
disposeRoutes = void 0;
|
|
1877
|
+
const stopping = service?.stop();
|
|
1878
|
+
service = void 0;
|
|
1879
|
+
if (stopping !== void 0) pendingStop = (pendingStop ?? Promise.resolve()).then(() => stopping);
|
|
1880
|
+
};
|
|
1881
|
+
/**
|
|
1882
|
+
* Apply the activation's current settings: a disabled row mounts nothing (no
|
|
1883
|
+
* probe cycle, no ledger, no routes), an enabled one starts on first use and
|
|
1884
|
+
* otherwise re-applies its options in place.
|
|
1885
|
+
*/
|
|
1886
|
+
const rearm = () => {
|
|
1887
|
+
const value = resolveConfig(config);
|
|
1888
|
+
if (!value.enabled) {
|
|
1889
|
+
queued = false;
|
|
1890
|
+
stop();
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
if (service !== void 0) {
|
|
1894
|
+
service.applyOptions(value);
|
|
1895
|
+
return;
|
|
1896
|
+
}
|
|
1897
|
+
if (queued) return;
|
|
1898
|
+
if (pendingStop === void 0) {
|
|
1899
|
+
launch();
|
|
1900
|
+
return;
|
|
1901
|
+
}
|
|
1902
|
+
queued = true;
|
|
1903
|
+
pendingStop = pendingStop.then(launch, launch).then(() => void 0);
|
|
1904
|
+
};
|
|
1905
|
+
ctx.on("loader/volatile-update", () => {
|
|
1906
|
+
rearm();
|
|
1907
|
+
});
|
|
1760
1908
|
ctx.effect(() => {
|
|
1761
|
-
|
|
1762
|
-
else pendingStop = pendingStop.then(begin, begin).then(() => void 0);
|
|
1909
|
+
rearm();
|
|
1763
1910
|
return () => {
|
|
1764
1911
|
live = false;
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
const stopping = service?.stop();
|
|
1768
|
-
service = void 0;
|
|
1769
|
-
if (stopping !== void 0) pendingStop = (pendingStop ?? Promise.resolve()).then(() => stopping);
|
|
1912
|
+
queued = false;
|
|
1913
|
+
stop();
|
|
1770
1914
|
};
|
|
1771
1915
|
}, "dsh-usage: runtime");
|
|
1772
1916
|
});
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* plugin bundle.
|
|
4
4
|
*
|
|
5
5
|
* Family plugins that inject at the DOM level must notice when the shell
|
|
6
|
-
* re-renders around their injected node: the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* re-renders around their injected node: the aggregate shell's column shims
|
|
7
|
+
* and any plugin surface still extending the shell from outside the official
|
|
8
|
+
* slot seats. Each consumer used to install its OWN `MutationObserver` on
|
|
9
|
+
* `document.body` with
|
|
10
10
|
* `{ childList: true, subtree: true }`, so a page carrying N family plugins
|
|
11
11
|
* paid N native observers and N callback invocations for EVERY mutation batch
|
|
12
12
|
* the app produced — chat token streaming alone produces many per second, and
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Family settings form bound to the profile entry id a Host actually serves.
|
|
3
|
+
*
|
|
4
|
+
* Under the 0.1.7 settings model a form is addressed by PROFILE ENTRY ID, while
|
|
5
|
+
* a family card knows only its own settings namespace. The family binder
|
|
6
|
+
* (`ctx.get('webUiSettings')`, published by dsh-web-settings) resolves one onto
|
|
7
|
+
* the other and is the preferred transport; this module is the fallback for a
|
|
8
|
+
* page that serves no binder — the group plugin is not installed, or its client
|
|
9
|
+
* half has not applied yet (the aggregate mounts its children in order, and a
|
|
10
|
+
* child still waiting on its own injects publishes nothing).
|
|
11
|
+
*
|
|
12
|
+
* The shared describe mirror is the only local evidence of which row id this
|
|
13
|
+
* profile serves, but it answers asynchronously: at plugin activation it usually
|
|
14
|
+
* holds nothing yet. A one-shot guess therefore cannot be right for every
|
|
15
|
+
* deployment, and the form is bound once per session:
|
|
16
|
+
*
|
|
17
|
+
* - binding the bare namespace made every save answer `No configurable plugin
|
|
18
|
+
* entry "<namespace>"` on an aggregate install (the namespace is not an entry
|
|
19
|
+
* id there), which the card reported as "the deployment did not accept these
|
|
20
|
+
* values";
|
|
21
|
+
* - binding the aggregate row id would break a standalone install, whose row id
|
|
22
|
+
* is the namespace or the package's own `ui-*` row.
|
|
23
|
+
*
|
|
24
|
+
* So the fallback starts on the most likely row and REBINDS the moment the
|
|
25
|
+
* mirror names one of this package's rows. Until the mirror answers, the form
|
|
26
|
+
* reports the binding it holds (usually `loading`), which the card renders as an
|
|
27
|
+
* unavailable form rather than a doomed one.
|
|
28
|
+
*/
|
|
29
|
+
import type { ConfigForm, ConfigForms } from '@deepseek-ai/dsh-client-ui-settings/client';
|
|
30
|
+
/** Options of {@link createServedEntryForm}. */
|
|
31
|
+
export interface ServedEntryFormOptions<T> {
|
|
32
|
+
/** The shared configuration forms service (`ctx.configForms`). */
|
|
33
|
+
forms: ConfigForms;
|
|
34
|
+
/**
|
|
35
|
+
* Profile entry ids this package's rows carry, most likely first: the
|
|
36
|
+
* aggregate's generated row, the package's own standalone row, then the bare
|
|
37
|
+
* namespace. The first is the fallback while the mirror has no answer; the
|
|
38
|
+
* last is the answer when the mirror serves none of them.
|
|
39
|
+
*/
|
|
40
|
+
entryIds: readonly string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a settings form bound to the profile entry id this Host serves, and
|
|
44
|
+
* rebound whenever the shared describe mirror names a different one of this
|
|
45
|
+
* package's rows.
|
|
46
|
+
* @param options - the shared forms service and this package's candidate row ids.
|
|
47
|
+
* @returns a form delegating to the currently resolved entry's own form.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createServedEntryForm<T>(options: ServedEntryFormOptions<T>): ConfigForm<T>;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import type { Context } from '@deepseek-ai/cordis';
|
|
2
2
|
import type { SettingsNamespace } from '@deepseek-ai/dsh-settings';
|
|
3
|
-
import z from 'schemastery';
|
|
3
|
+
import z from '@deepseek-ai/schemastery';
|
|
4
4
|
import { type UsageServiceOptions } from './host/usage-service.ts';
|
|
5
|
+
declare module '@deepseek-ai/cordis' {
|
|
6
|
+
interface Events {
|
|
7
|
+
/**
|
|
8
|
+
* Volatile config values were committed into the running fiber without a
|
|
9
|
+
* remount; dispatched to the owning fiber only. Spelled here because the
|
|
10
|
+
* Loader package is not a dependency of this plugin, with the Loader's own
|
|
11
|
+
* shape so the two declarations merge when a Host program carries both.
|
|
12
|
+
* @param paths - changed config paths as key arrays; every value is committed before dispatch.
|
|
13
|
+
* @mode emit
|
|
14
|
+
*/
|
|
15
|
+
'loader/volatile-update'(paths: readonly (readonly string[])[]): void;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
5
18
|
export declare const name = "dsh-usage";
|
|
6
19
|
export declare const inject: string[];
|
|
7
20
|
/**
|
|
@@ -11,24 +24,62 @@ export declare const inject: string[];
|
|
|
11
24
|
* family settings bridge (dsh-web-settings) resolves onto this row's entry id.
|
|
12
25
|
*/
|
|
13
26
|
export declare const USAGE_SETTINGS_NAMESPACE: SettingsNamespace;
|
|
14
|
-
|
|
15
|
-
|
|
27
|
+
/** The stable reference a `volatile()` config field resolves to; its owner updates it in place. */
|
|
28
|
+
interface ConfigRef<T> {
|
|
29
|
+
/** @returns the value currently committed for the running instance. */
|
|
30
|
+
get(): T | undefined;
|
|
31
|
+
}
|
|
32
|
+
/** One config field as the Host hands it over: a live reference, or a plain value (profile patches, tests). */
|
|
33
|
+
type ConfigField<T> = ConfigRef<T> | T | undefined;
|
|
34
|
+
/** The config the Host hands this activation — the runtime face of {@link Config}. */
|
|
35
|
+
export interface UsageConfigFields {
|
|
36
|
+
enabled?: ConfigField<boolean>;
|
|
16
37
|
/** Provider probe cycle in seconds; 30-3600. */
|
|
17
|
-
pollIntervalSec?: number
|
|
38
|
+
pollIntervalSec?: ConfigField<number>;
|
|
18
39
|
/** Ledger retention in local days. */
|
|
19
|
-
retainDays?: number
|
|
40
|
+
retainDays?: ConfigField<number>;
|
|
20
41
|
}
|
|
21
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Plugin config schema. Under the 0.1.7 settings model this schema IS the
|
|
44
|
+
* entry's settings page: the Host derives one form per profile entry from it
|
|
45
|
+
* and serves it only when at least one field is `volatile()`. The marker is
|
|
46
|
+
* also what admits a write and what keeps the edit on the live path — the
|
|
47
|
+
* Loader commits the new value into the field's reference and announces
|
|
48
|
+
* `loader/volatile-update` on this fiber instead of remounting the row, so the
|
|
49
|
+
* probe cycle, its ledger and its route registrations survive a settings save.
|
|
50
|
+
*
|
|
51
|
+
* The schema is left to inference rather than annotated with
|
|
52
|
+
* `z<UsageConfigFields>`: a volatile field's parsed output is a reference while
|
|
53
|
+
* its accepted input stays the plain value, so the two sides no longer share
|
|
54
|
+
* one shape and the annotation would reject the schema the Host must be given.
|
|
55
|
+
*/
|
|
56
|
+
export declare const Config: z<Schemastery.ObjectS<NoInfer<{
|
|
57
|
+
enabled: z<boolean, boolean, "volatile-defined">;
|
|
58
|
+
pollIntervalSec: z<number, number, "volatile-defined">;
|
|
59
|
+
retainDays: z<number, number, "volatile-defined">;
|
|
60
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
61
|
+
enabled: z<boolean, boolean, "volatile-defined">;
|
|
62
|
+
pollIntervalSec: z<number, number, "volatile-defined">;
|
|
63
|
+
retainDays: z<number, number, "volatile-defined">;
|
|
64
|
+
}>>, "plain">;
|
|
22
65
|
export interface ResolvedConfig extends UsageServiceOptions {
|
|
23
66
|
enabled: boolean;
|
|
24
67
|
}
|
|
25
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Resolve the effective settings of one activation. Volatile references are
|
|
70
|
+
* read here, so every re-arm observes the latest committed values rather than
|
|
71
|
+
* whatever the row was activated with.
|
|
72
|
+
* @param config - the config the Host passed to the activation.
|
|
73
|
+
* @returns the resolved settings.
|
|
74
|
+
*/
|
|
75
|
+
export declare function resolveConfig(config?: UsageConfigFields): ResolvedConfig;
|
|
26
76
|
/**
|
|
27
77
|
* Host body. The plugin's own Config schema above is the settings document the
|
|
28
78
|
* 0.1.7 Host serves for this row; the effective config arrives here as the
|
|
29
|
-
* `config` argument
|
|
30
|
-
*
|
|
79
|
+
* `config` argument, and a settings save re-enters this body through the
|
|
80
|
+
* volatile path rather than a row reload (see {@link Config}).
|
|
31
81
|
* @param ctx - host root context.
|
|
32
82
|
* @param config - the row's resolved configuration.
|
|
33
83
|
*/
|
|
34
|
-
export declare const apply: (ctx: Context, config?:
|
|
84
|
+
export declare const apply: (ctx: Context, config?: UsageConfigFields) => void;
|
|
85
|
+
export {};
|
|
@@ -4,19 +4,50 @@
|
|
|
4
4
|
* the loader accepts a standalone install of the same package side by side;
|
|
5
5
|
* without this guard the second instance would still re-register the same
|
|
6
6
|
* webserver routes, tools, settings namespaces, and system-prompt sections
|
|
7
|
-
* and fail the boot. mountOnce makes
|
|
8
|
-
*
|
|
9
|
-
* package name in the client module host).
|
|
7
|
+
* and fail the boot. mountOnce makes a mount of an already-mounted package a
|
|
8
|
+
* no-op for as long as the first instance lives (the browser half is already
|
|
9
|
+
* deduped by package name in the client module host).
|
|
10
|
+
*
|
|
11
|
+
* A no-op is only safe while the holder is ALIVE. The holder can be disposed
|
|
12
|
+
* long after the refused mount ran its course: the Host reloads a profile by
|
|
13
|
+
* creating the new loader entries before the old ones are torn down (a
|
|
14
|
+
* plugin-manager enable/disable/install write, a settings-driven row reload,
|
|
15
|
+
* HMR), so the new aggregate shell entry mounts its family plugin while the
|
|
16
|
+
* previous entry still owns the name. Dropping that refused mount lost the
|
|
17
|
+
* plugin for good - the previous entry then disposed its own mount, releasing
|
|
18
|
+
* the name with nobody left to take it, and the family row stayed listed as
|
|
19
|
+
* active while its host routes 404ed (the task-board panel showed
|
|
20
|
+
* "board.hostError.notMounted", no degraded record appeared, and only a Host
|
|
21
|
+
* restart recovered it).
|
|
22
|
+
*
|
|
23
|
+
* The refused mount is therefore QUEUED, not dropped, and replayed the moment
|
|
24
|
+
* the holder releases the name - if the waiting fiber is still alive then. The
|
|
25
|
+
* single-instance guarantee is unchanged: exactly one mount is live per
|
|
26
|
+
* package name, and the replay re-enters the guard so a later mount still
|
|
27
|
+
* dedupes against it.
|
|
10
28
|
*
|
|
11
29
|
* The registry rides a global symbol so two module instances of the same
|
|
12
|
-
* package (npm copy vs repository link) still share one verdict.
|
|
13
|
-
*
|
|
30
|
+
* package (npm copy vs repository link) still share one verdict. That symbol
|
|
31
|
+
* is a CROSS-REPOSITORY contract, not this file's private state: the four
|
|
32
|
+
* satellite packages (dsh-skins / dsh-pet / dsh-presets /
|
|
33
|
+
* dsh-community-plugins) are separate repositories carrying their own copy of
|
|
34
|
+
* this guard, rebuilt on their own schedule, so the value under `MOUNTED`
|
|
35
|
+
* must keep the shape every published copy reads (a `Set` of package names
|
|
36
|
+
* with `has`/`add`/`delete`). The wait queues this guard added therefore live
|
|
37
|
+
* under their own additive key, and the registry reads back a `Set` even when
|
|
38
|
+
* some other build left a different value there. Changing `MOUNTED`'s shape
|
|
39
|
+
* in place broke that contract once: a satellite's legacy copy created a
|
|
40
|
+
* `Set`, the family's new copy read it as a `Map`, and every family row
|
|
41
|
+
* mounted after it failed with "claims.get is not a function".
|
|
42
|
+
*
|
|
43
|
+
* cordis `ctx.effect` runs its callback immediately and treats the callback's
|
|
14
44
|
* return value as the fiber disposer, so the unmarker is returned, not run.
|
|
15
45
|
*/
|
|
16
46
|
/**
|
|
17
47
|
* Wrap a cordis plugin apply so the package runs at most once per process.
|
|
18
|
-
* The first mount registers normally and
|
|
19
|
-
*
|
|
48
|
+
* The first mount registers normally and releases the name when its fiber
|
|
49
|
+
* disposes; a mount refused while that name is held waits for the release and
|
|
50
|
+
* then runs, unless its own fiber disposes first.
|
|
20
51
|
* @param packageName - npm package identity shared by every install source.
|
|
21
52
|
* @param fn - the original plugin apply.
|
|
22
53
|
* @returns an apply of the same shape.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-usage",
|
|
3
3
|
"description": "Usage statistics plugin for the dsh web GUI: per-provider balance and coding-plan quota detection plus a live token usage ledger, with a compact price glance card above the sidebar's Settings row",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.3",
|
|
5
5
|
"icon": "icon.svg",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"schemastery": "^3.18.
|
|
53
|
+
"@deepseek-ai/schemastery": "^3.18.4"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"@deepseek-ai/dsh": ">=0.1.7-rc.2",
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* plugin bundle.
|
|
5
5
|
*
|
|
6
6
|
* Family plugins that inject at the DOM level must notice when the shell
|
|
7
|
-
* re-renders around their injected node: the
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* re-renders around their injected node: the aggregate shell's column shims
|
|
8
|
+
* and any plugin surface still extending the shell from outside the official
|
|
9
|
+
* slot seats. Each consumer used to install its OWN `MutationObserver` on
|
|
10
|
+
* `document.body` with
|
|
11
11
|
* `{ childList: true, subtree: true }`, so a page carrying N family plugins
|
|
12
12
|
* paid N native observers and N callback invocations for EVERY mutation batch
|
|
13
13
|
* the app produced — chat token streaming alone produces many per second, and
|
package/src/client/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ import { createUsageStore, type UsageStoreInstance } from './usage-store.ts'
|
|
|
21
21
|
import { UsageSectionCard, type UsageSectionFace, type UsageSettings } from './UsageSectionCard.tsx'
|
|
22
22
|
import { mountUsageFootCard, openUsageSettings } from './foot-card-mount.tsx'
|
|
23
23
|
import { NS, en, zh, t } from './locales.ts'
|
|
24
|
+
import { createServedEntryForm } from './settings-entry-form.ts'
|
|
24
25
|
import type { UsageOverviewView } from '../core/types.ts'
|
|
25
26
|
|
|
26
27
|
/** The host usage API as the browser sees it (same-origin JSON endpoints). */
|
|
@@ -49,9 +50,18 @@ const usageApi: UsageHttpApi = {
|
|
|
49
50
|
refresh: () => usageFetch('api/dsh-usage/refresh', 'POST'),
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Settings namespace the section edits: the family identity of this plugin's
|
|
55
|
+
* own settings form, and the row id a standalone bundle install carries.
|
|
56
|
+
*/
|
|
53
57
|
const USAGE_SETTINGS_NS = 'dsh-usage'
|
|
54
58
|
|
|
59
|
+
/** Profile entry id the family aggregate's generated row carries. */
|
|
60
|
+
const AGGREGATE_ENTRY_ID = 'web-ui-usage'
|
|
61
|
+
|
|
62
|
+
/** Profile entry ids this package's patch rows carry, most likely first. */
|
|
63
|
+
const USAGE_ENTRY_IDS: readonly string[] = [AGGREGATE_ENTRY_ID, 'usage', USAGE_SETTINGS_NS]
|
|
64
|
+
|
|
55
65
|
/** First-level nav position: directly below the Workshop section (order 150). */
|
|
56
66
|
const SECTION_ORDER = 151
|
|
57
67
|
|
|
@@ -101,15 +111,15 @@ export function apply(ctx: ClientContext): void {
|
|
|
101
111
|
}, 'dsh-usage: dictionaries')
|
|
102
112
|
|
|
103
113
|
// The family binder is what maps this namespace onto the row's profile entry
|
|
104
|
-
// id and hands back the native form. Without the group plugin the
|
|
105
|
-
//
|
|
106
|
-
// the
|
|
107
|
-
//
|
|
108
|
-
//
|
|
114
|
+
// id and hands back the native form. Without the group plugin the shared forms
|
|
115
|
+
// service is addressed on the entry id the describe mirror justifies, rebound
|
|
116
|
+
// as soon as the mirror answers: the namespace itself is only an entry id on a
|
|
117
|
+
// standalone install, so binding it on the aggregate left the form unavailable
|
|
118
|
+
// and the row's controls disabled.
|
|
109
119
|
const binder = ctx.get('webUiSettings')
|
|
110
120
|
const settingsForm = binder !== undefined
|
|
111
121
|
? binder.bind<UsageSettings>({ namespace: USAGE_SETTINGS_NS })
|
|
112
|
-
:
|
|
122
|
+
: createServedEntryForm<UsageSettings>({ forms: ctx.configForms, entryIds: USAGE_ENTRY_IDS })
|
|
113
123
|
|
|
114
124
|
// One store instance per apply body; the section mounts and unmounts with
|
|
115
125
|
// the settings page, and the store survives between visits so the last
|