dsh-ui-smooth 0.1.0 → 0.1.1

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/index.js CHANGED
@@ -1,248 +1,169 @@
1
- /**
2
- * Host loader entry for dsh-ui-smooth.
3
- *
4
- * The page does the heavy lifting; this half owns the two things a page cannot
5
- * do for itself:
6
- *
7
- * 1. Background throttling. Chromium stops producing frames for an occluded or
8
- * minimized window, so a running agent's output only piles up and the switch
9
- * back pays for all of it at once. Electron can turn that off per
10
- * `webContents`; browsers cannot, so on a plain web host this is a no-op.
11
- *
12
- * 2. The config file. Both thresholds live in `$DSH_HOME/dsh-ui-smooth.json`
13
- * and are served to the page over `/dsh-ui-smooth/config`, so the settings
14
- * section can read and write one value that survives a rebuild, and the
15
- * throttle switch can apply before any page exists.
16
- */
17
-
18
- import { mkdir, readFile, writeFile } from 'node:fs/promises'
19
- import { homedir } from 'node:os'
20
- import { dirname, join } from 'node:path'
21
- import { createRequire } from 'node:module'
22
-
23
- export const name = 'dsh-ui-smooth'
24
- /** The route needs the HTTP server; a host without one simply does not mount this plugin. */
25
- export const inject = ['webServer']
26
-
27
- const ROUTE = '/dsh-ui-smooth/config'
28
- const FILE_NAME = 'dsh-ui-smooth.json'
29
- const MAX_BODY = 8 * 1024
30
-
31
- const DEFAULTS = {
32
- animations: true,
33
- backgroundThrottle: true,
34
- duringStream: true,
35
- skip: { enabled: true, turns: 3, minutes: 5 },
36
- unmount: { enabled: true, turns: 5, minutes: 16 },
37
- release: { enabled: false, turns: 8, minutes: 20 }
38
- }
39
-
40
- function configPath() {
41
- const home = process.env.DSH_HOME ?? join(homedir(), '.dsh')
42
- return join(home, FILE_NAME)
43
- }
44
-
45
- function clampNumber(value, fallback, max) {
46
- const number = Number(value)
47
- if (!Number.isFinite(number) || number < 0) return fallback
48
- return Math.min(max, Math.floor(number))
49
- }
50
-
51
- /** Accept only the shape the page is allowed to store. */
52
- function normalizeTier(source, key) {
53
- const tier = source?.[key] ?? {}
54
- return {
55
- enabled: tier.enabled !== false,
56
- turns: clampNumber(tier.turns, DEFAULTS[key].turns, 10000),
57
- minutes: clampNumber(tier.minutes, DEFAULTS[key].minutes, 10000)
58
- }
59
- }
60
-
61
- /** The aggressive tier keeps a tail of turns and waits out a quiet period. */
62
- function normalizeRelease(source) {
63
- const tier = source?.release ?? {}
64
- return {
65
- enabled: tier.enabled === true,
66
- turns: Math.max(2, clampNumber(tier.turns, DEFAULTS.release.turns, 10000)),
67
- minutes: clampNumber(tier.minutes, DEFAULTS.release.minutes, 10000)
68
- }
69
- }
70
-
71
- function normalizeConfig(raw) {
72
- const source = raw !== null && typeof raw === 'object' ? raw : {}
73
- return {
74
- animations: source.animations !== false,
75
- backgroundThrottle: source.backgroundThrottle !== false,
76
- duringStream: source.duringStream !== false,
77
- skip: normalizeTier(source, 'skip'),
78
- unmount: normalizeTier(source, 'unmount'),
79
- release: normalizeRelease(source)
80
- }
81
- }
82
-
83
- async function readConfig() {
84
- try {
85
- const text = await readFile(configPath(), 'utf8')
86
- return normalizeConfig(JSON.parse(text))
87
- } catch (reason) {
88
- return normalizeConfig(DEFAULTS)
89
- }
90
- }
91
-
92
- async function writeConfig(config) {
93
- const path = configPath()
94
- await mkdir(dirname(path), { recursive: true })
95
- await writeFile(path, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
96
- }
97
-
98
- /**
99
- * `electron` resolves only inside the desktop shell's main process. A web or
100
- * CLI host has no window to un-throttle, which is not an error.
101
- */
102
- function loadElectron() {
103
- try {
104
- const electron = createRequire(import.meta.url)('electron')
105
- if (electron === null || typeof electron !== 'object') return null
106
- if (typeof electron.webContents?.getAllWebContents !== 'function') return null
107
- if (typeof electron.app?.on !== 'function') return null
108
- return electron
109
- } catch (reason) {
110
- return null
111
- }
112
- }
113
-
114
- /**
115
- * Keep the shell's windows throttled in the background (the Electron default)
116
- * or let them keep rendering there, for every window the shell owns and for the
117
- * ones it opens later.
118
- */
119
- function createThrottleControl(electron) {
120
- let watch = null
121
- const applyOne = (contents, throttled) => {
122
- if (contents === null || contents === undefined) return false
123
- if (typeof contents.setBackgroundThrottling !== 'function') return false
124
- if (contents.getType?.() === 'window' && typeof contents.isDestroyed === 'function' && contents.isDestroyed()) return false
125
- try {
126
- contents.setBackgroundThrottling(throttled)
127
- return true
128
- } catch (reason) {
129
- return false
130
- }
131
- }
132
-
133
- const applyAll = (throttled) => {
134
- let count = 0
135
- for (const contents of electron.webContents.getAllWebContents()) {
136
- if (applyOne(contents, throttled)) count += 1
137
- }
138
- return count
139
- }
140
-
141
- return {
142
- apply: (throttled) => {
143
- if (electron === null) return 0
144
- const base = applyAll(throttled)
145
- if (watch === null) {
146
- watch = (_event, contents) => {
147
- applyOne(contents, state.throttled)
148
- }
149
- electron.app.on('web-contents-created', watch)
150
- }
151
- state.throttled = throttled
152
- return base
153
- },
154
- dispose: () => {
155
- if (watch !== null && typeof electron?.app?.removeListener === 'function') {
156
- electron.app.removeListener('web-contents-created', watch)
157
- watch = null
158
- }
159
- }
160
- }
161
- }
162
-
163
- const state = { throttled: true }
164
-
165
- function sendJson(response, status, payload) {
166
- const body = JSON.stringify(payload)
167
- response.statusCode = status
168
- response.setHeader('content-type', 'application/json; charset=utf-8')
169
- response.setHeader('cache-control', 'no-store')
170
- response.end(body)
171
- }
172
-
173
- function readBody(request) {
174
- return new Promise((resolve) => {
175
- const chunks = []
176
- let size = 0
177
- request.on('data', (chunk) => {
178
- size += chunk.length
179
- if (size > MAX_BODY) {
180
- resolve(undefined)
181
- request.destroy()
182
- return
183
- }
184
- chunks.push(chunk)
185
- })
186
- request.on('end', () => {
187
- try {
188
- resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
189
- } catch (reason) {
190
- resolve(undefined)
191
- }
192
- })
193
- request.on('error', () => resolve(undefined))
194
- })
195
- }
196
-
197
- export function apply(ctx) {
198
- const electron = loadElectron()
199
- const throttle = createThrottleControl(electron)
200
- let config = normalizeConfig(DEFAULTS)
201
-
202
- const sync = (next) => {
203
- config = normalizeConfig(next)
204
- // The switch says "throttle me in the background", which is Electron's own
205
- // default; turning it off is what buys a page that keeps rendering.
206
- throttle.apply(config.backgroundThrottle)
207
- }
208
-
209
- readConfig()
210
- .then((stored) => sync(stored))
211
- .catch(() => sync(DEFAULTS))
212
-
213
- let unregister
214
- try {
215
- const server = ctx?.webServer ?? (typeof ctx?.get === 'function' ? ctx.get('webServer') : undefined)
216
- if (server !== undefined && server !== null && typeof server.register === 'function') {
217
- unregister = server.register({
218
- kind: 'exact',
219
- path: ROUTE,
220
- handler: async (request, response) => {
221
- const method = String(request?.method ?? 'GET').toUpperCase()
222
- if (method === 'GET' || method === 'HEAD') {
223
- return sendJson(response, 200, { config, electron: electron !== null })
224
- }
225
- if (method !== 'POST') return sendJson(response, 405, { status: 'method' })
226
- const body = await readBody(request)
227
- if (body === undefined) return sendJson(response, 400, { status: 'invalid' })
228
- sync(body)
229
- try {
230
- await writeConfig(config)
231
- } catch (reason) {
232
- return sendJson(response, 500, { status: 'unstored', config, electron: electron !== null })
233
- }
234
- return sendJson(response, 200, { config, electron: electron !== null })
235
- }
236
- })
237
- }
238
- } catch (reason) {
239
- unregister = undefined
240
- }
241
-
242
- if (typeof ctx?.effect === 'function') {
243
- ctx.effect(() => () => {
244
- if (typeof unregister === 'function') unregister()
245
- throttle.dispose()
246
- }, 'dsh-ui-smooth: host wiring')
247
- }
248
- }
1
+ /**
2
+ * Host loader entry for dsh-ui-smooth.
3
+ *
4
+ * The page does the work; this half owns the one thing a page cannot do for
5
+ * itself: the config file. Both thresholds live in
6
+ * `$DSH_HOME/dsh-ui-smooth.json` and are served to the page over
7
+ * `/dsh-ui-smooth/config`, so the settings section reads and writes one value
8
+ * that survives a rebuild.
9
+ */
10
+
11
+ import { mkdir, readFile, writeFile } from 'node:fs/promises'
12
+ import { homedir } from 'node:os'
13
+ import { dirname, join } from 'node:path'
14
+
15
+ export const name = 'dsh-ui-smooth'
16
+ /** The route needs the HTTP server; a host without one simply does not mount this plugin. */
17
+ export const inject = ['webServer']
18
+
19
+ const ROUTE = '/dsh-ui-smooth/config'
20
+ const FILE_NAME = 'dsh-ui-smooth.json'
21
+ const MAX_BODY = 8 * 1024
22
+
23
+ const DEFAULTS = {
24
+ animations: true,
25
+ backgroundThrottle: true,
26
+ duringStream: true,
27
+ follow: true,
28
+ skip: { enabled: true, turns: 3, minutes: 5 },
29
+ unmount: { enabled: true, turns: 5, minutes: 16 },
30
+ release: { enabled: false, turns: 8, minutes: 20 }
31
+ }
32
+
33
+ function configPath() {
34
+ const home = process.env.DSH_HOME ?? join(homedir(), '.dsh')
35
+ return join(home, FILE_NAME)
36
+ }
37
+
38
+ function clampNumber(value, fallback, max) {
39
+ const number = Number(value)
40
+ if (!Number.isFinite(number) || number < 0) return fallback
41
+ return Math.min(max, Math.floor(number))
42
+ }
43
+
44
+ /** Accept only the shape the page is allowed to store. */
45
+ function normalizeTier(source, key) {
46
+ const tier = source?.[key] ?? {}
47
+ return {
48
+ enabled: tier.enabled !== false,
49
+ turns: clampNumber(tier.turns, DEFAULTS[key].turns, 10000),
50
+ minutes: clampNumber(tier.minutes, DEFAULTS[key].minutes, 10000)
51
+ }
52
+ }
53
+
54
+ /** The aggressive tier keeps a tail of turns and waits out a quiet period. */
55
+ function normalizeRelease(source) {
56
+ const tier = source?.release ?? {}
57
+ return {
58
+ enabled: tier.enabled === true,
59
+ turns: Math.max(2, clampNumber(tier.turns, DEFAULTS.release.turns, 10000)),
60
+ minutes: clampNumber(tier.minutes, DEFAULTS.release.minutes, 10000)
61
+ }
62
+ }
63
+
64
+ function normalizeConfig(raw) {
65
+ const source = raw !== null && typeof raw === 'object' ? raw : {}
66
+ return {
67
+ animations: source.animations !== false,
68
+ backgroundThrottle: source.backgroundThrottle !== false,
69
+ duringStream: source.duringStream !== false,
70
+ follow: source.follow !== false,
71
+ skip: normalizeTier(source, 'skip'),
72
+ unmount: normalizeTier(source, 'unmount'),
73
+ release: normalizeRelease(source)
74
+ }
75
+ }
76
+
77
+ async function readConfig() {
78
+ try {
79
+ const text = await readFile(configPath(), 'utf8')
80
+ return normalizeConfig(JSON.parse(text))
81
+ } catch (reason) {
82
+ return normalizeConfig(DEFAULTS)
83
+ }
84
+ }
85
+
86
+ async function writeConfig(config) {
87
+ const path = configPath()
88
+ await mkdir(dirname(path), { recursive: true })
89
+ await writeFile(path, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
90
+ }
91
+
92
+ function sendJson(response, status, payload) {
93
+ const body = JSON.stringify(payload)
94
+ response.statusCode = status
95
+ response.setHeader('content-type', 'application/json; charset=utf-8')
96
+ response.setHeader('cache-control', 'no-store')
97
+ response.end(body)
98
+ }
99
+
100
+ function readBody(request) {
101
+ return new Promise((resolve) => {
102
+ const chunks = []
103
+ let size = 0
104
+ request.on('data', (chunk) => {
105
+ size += chunk.length
106
+ if (size > MAX_BODY) {
107
+ resolve(undefined)
108
+ request.destroy()
109
+ return
110
+ }
111
+ chunks.push(chunk)
112
+ })
113
+ request.on('end', () => {
114
+ try {
115
+ resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
116
+ } catch (reason) {
117
+ resolve(undefined)
118
+ }
119
+ })
120
+ request.on('error', () => resolve(undefined))
121
+ })
122
+ }
123
+
124
+ export function apply(ctx) {
125
+ let config = normalizeConfig(DEFAULTS)
126
+
127
+ const sync = (next) => {
128
+ config = normalizeConfig(next)
129
+ }
130
+
131
+ readConfig()
132
+ .then((stored) => sync(stored))
133
+ .catch(() => sync(DEFAULTS))
134
+
135
+ let unregister
136
+ try {
137
+ const server = ctx?.webServer ?? (typeof ctx?.get === 'function' ? ctx.get('webServer') : undefined)
138
+ if (server !== undefined && server !== null && typeof server.register === 'function') {
139
+ unregister = server.register({
140
+ kind: 'exact',
141
+ path: ROUTE,
142
+ handler: async (request, response) => {
143
+ const method = String(request?.method ?? 'GET').toUpperCase()
144
+ if (method === 'GET' || method === 'HEAD') {
145
+ return sendJson(response, 200, { config })
146
+ }
147
+ if (method !== 'POST') return sendJson(response, 405, { status: 'method' })
148
+ const body = await readBody(request)
149
+ if (body === undefined) return sendJson(response, 400, { status: 'invalid' })
150
+ sync(body)
151
+ try {
152
+ await writeConfig(config)
153
+ } catch (reason) {
154
+ return sendJson(response, 500, { status: 'unstored', config })
155
+ }
156
+ return sendJson(response, 200, { config })
157
+ }
158
+ })
159
+ }
160
+ } catch (reason) {
161
+ unregister = undefined
162
+ }
163
+
164
+ if (typeof ctx?.effect === 'function') {
165
+ ctx.effect(() => () => {
166
+ if (typeof unregister === 'function') unregister()
167
+ }, 'dsh-ui-smooth: host wiring')
168
+ }
169
+ }
package/package.json CHANGED
@@ -1,68 +1,68 @@
1
- {
2
- "name": "dsh-ui-smooth",
3
- "version": "0.1.0",
4
- "description": "Web UI smoothness budget for DeepSeek Harness: keeps idle animations off the main thread, recycles old transcript turns, unloads old history from memory, and stops the desktop shell from freezing the page in the background.",
5
- "type": "module",
6
- "main": "lib/index.js",
7
- "license": "GPL-3.0-or-later",
8
- "author": "knight <knight-z@foxmail.com>",
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/XKPU/dsh-ui-smooth.git"
12
- },
13
- "bugs": {
14
- "url": "https://github.com/XKPU/dsh-ui-smooth/issues"
15
- },
16
- "homepage": "https://github.com/XKPU/dsh-ui-smooth#readme",
17
- "icon": "./icon.svg",
18
- "keywords": [
19
- "dsh",
20
- "dsh-plugin",
21
- "deepseek-harness",
22
- "performance",
23
- "animation",
24
- "layout",
25
- "memory",
26
- "cpu",
27
- "web-ui"
28
- ],
29
- "engines": {
30
- "dsh": ">=0.2.0 <=0.2.3"
31
- },
32
- "exports": {
33
- ".": {
34
- "default": "./lib/index.js"
35
- },
36
- "./client": {
37
- "default": "./lib/client.js"
38
- },
39
- "./package.json": "./package.json",
40
- "./locale/*.json": "./locale/*.json"
41
- },
42
- "files": [
43
- "lib",
44
- "src",
45
- "scripts",
46
- "cordis.patch.yml",
47
- "icon.svg",
48
- "README.md",
49
- "CHANGELOG.md",
50
- "LICENSE",
51
- "locale"
52
- ],
53
- "scripts": {
54
- "build": "node scripts/build.mjs",
55
- "pretest": "node scripts/build.mjs",
56
- "test": "node test/engine.test.mjs"
57
- },
58
- "dsh": {
59
- "manifestVersion": 1,
60
- "bundle": {
61
- "patch": "./cordis.patch.yml"
62
- },
63
- "client": {
64
- "platform": "web",
65
- "inject": []
66
- }
67
- }
68
- }
1
+ {
2
+ "name": "dsh-ui-smooth",
3
+ "version": "0.1.1",
4
+ "description": "Web UI smoothness budget for DeepSeek Harness: keeps idle animations off the main thread, recycles old transcript turns, unloads old history from memory, and stops the desktop shell from freezing the page in the background.",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "license": "GPL-3.0-or-later",
8
+ "author": "knight <knight-z@foxmail.com>",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/XKPU/dsh-ui-smooth.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/XKPU/dsh-ui-smooth/issues"
15
+ },
16
+ "homepage": "https://github.com/XKPU/dsh-ui-smooth#readme",
17
+ "icon": "./icon.svg",
18
+ "keywords": [
19
+ "dsh",
20
+ "dsh-plugin",
21
+ "deepseek-harness",
22
+ "performance",
23
+ "animation",
24
+ "layout",
25
+ "memory",
26
+ "cpu",
27
+ "web-ui"
28
+ ],
29
+ "engines": {
30
+ "dsh": ">=0.2.0 <=0.2.3"
31
+ },
32
+ "exports": {
33
+ ".": {
34
+ "default": "./lib/index.js"
35
+ },
36
+ "./client": {
37
+ "default": "./lib/client.js"
38
+ },
39
+ "./package.json": "./package.json",
40
+ "./locale/*.json": "./locale/*.json"
41
+ },
42
+ "files": [
43
+ "lib",
44
+ "src",
45
+ "scripts",
46
+ "cordis.patch.yml",
47
+ "icon.svg",
48
+ "README.md",
49
+ "CHANGELOG.md",
50
+ "LICENSE",
51
+ "locale"
52
+ ],
53
+ "scripts": {
54
+ "build": "node scripts/build.mjs",
55
+ "pretest": "node scripts/build.mjs",
56
+ "test": "node test/engine.test.mjs"
57
+ },
58
+ "dsh": {
59
+ "manifestVersion": 1,
60
+ "bundle": {
61
+ "patch": "./cordis.patch.yml"
62
+ },
63
+ "client": {
64
+ "platform": "web",
65
+ "inject": []
66
+ }
67
+ }
68
+ }