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/src/main.js CHANGED
@@ -1,136 +1,142 @@
1
- /**
2
- * Module face: install the page halves into one cordis plugin.
3
- *
4
- * Everything the page does hangs off this `apply`: the animation stylesheet, the
5
- * transcript recycler, the settings section, and the debug handle that reports
6
- * what they are doing.
7
- */
8
-
9
- const { installAnimations } = require('./animate.js')
10
- const { LAYOUT_PROPERTIES, PAINT_PROPERTIES, classify, coarsen, planSheets, planSweep, readAnimations, readKeyframes } = require('./animations.js')
11
- const { VERSION, cloneConfig, desktop, getConfig, hostElectron, loadConfig, onConfig } = require('./config.js')
12
- const { createKeepAwake } = require('./keepawake.js')
13
- const { CONTAINMENT_CSS, createRecycler } = require('./recycle.js')
14
- const { mainSessions, sliceHistory } = require('./release.js')
15
- const { SETTINGS_CSS, SettingsSection } = require('./settings.js')
16
-
17
- const CSS_ID = 'dsh-ui-smooth/css'
18
-
19
- function installStyleSheet() {
20
- if (document.getElementById(CSS_ID) !== null) return
21
- const element = document.createElement('style')
22
- element.id = CSS_ID
23
- element.textContent = `${CONTAINMENT_CSS}\n${SETTINGS_CSS}`
24
- ;(document.head ?? document.documentElement).appendChild(element)
25
- }
26
-
27
- function apply(ctx) {
28
- installStyleSheet()
29
- const animations = installAnimations()
30
- const recycler = createRecycler(ctx)
31
- const keepAwake = createKeepAwake()
32
- const slotIds = () => {
33
- try {
34
- return ctx?.slots?.entries?.('settings.section')?.map?.((entry) => entry.id) ?? []
35
- } catch (reason) {
36
- return []
37
- }
38
- }
39
-
40
- const handle = {
41
- version: VERSION,
42
- status: () => ({
43
- animations: animations.status(),
44
- recycling: recycler.stats(),
45
- config: cloneConfig(getConfig()),
46
- electron: hostElectron(),
47
- settingsSection: slotIds()
48
- }),
49
- rescan: () => animations.rescan(),
50
- recycle: () => recycler.pass(),
51
- refreshConfig: () => loadConfig().then(() => {
52
- recycler.pass()
53
- return cloneConfig(getConfig())
54
- }),
55
- restore: () => animations.restore(),
56
- config: () => cloneConfig(getConfig()),
57
- throttle: () => ({ host: hostElectron(), desktop: desktop(), ...keepAwake.status() }),
58
- /** What the aggressive tier sees: the loaded window it would trim. */
59
- history: () => {
60
- const session = mainSessions(ctx?.sessions)[0]
61
- if (session === undefined || session === null) return null
62
- let entries = []
63
- try {
64
- entries = session.eventSource?.getSnapshot?.()?.entries ?? []
65
- } catch (reason) {
66
- entries = []
67
- }
68
- return {
69
- sessionId: session.sessionId,
70
- entries: entries.length,
71
- turns: entries.filter((entry) => entry?.event?.type === 'turn/start').length,
72
- baseSeq: session.baseSeq,
73
- hasMore: session.hasMore === true
74
- }
75
- }
76
- }
77
- window.__dshUiSmooth = handle
78
-
79
- /**
80
- * The switch reads "throttle me in the background". Off means the page keeps
81
- * running there: the host half asks the shell when it is inside one, and the
82
- * page holds an inaudible audio stream open for the case where it is not.
83
- */
84
- const syncThrottle = () => {
85
- const off = getConfig().backgroundThrottle === false
86
- if (off && (hostElectron() || desktop())) keepAwake.start()
87
- else keepAwake.stop()
88
- }
89
-
90
- onConfig(() => {
91
- recycler.restoreAll()
92
- syncThrottle()
93
- })
94
-
95
- loadConfig().then(() => {
96
- recycler.pass()
97
- syncThrottle()
98
- })
99
-
100
- if (ctx !== undefined && ctx !== null && typeof ctx.slots?.inject === 'function') {
101
- ctx.slots.inject('settings.section', () =>
102
- ctx.slots.register({
103
- name: 'settings.section',
104
- id: 'dsh-ui-smooth',
105
- order: 60,
106
- label: () => '流畅性',
107
- registrant: 'dsh-ui-smooth'
108
- }, SettingsSection)
109
- )
110
- }
111
-
112
- if (ctx !== undefined && ctx !== null && typeof ctx.effect === 'function') {
113
- ctx.effect(() => () => {
114
- animations.dispose()
115
- keepAwake.stop()
116
- recycler.dispose()
117
- const css = document.getElementById(CSS_ID)
118
- if (css !== null) css.remove()
119
- if (window.__dshUiSmooth === handle) delete window.__dshUiSmooth
120
- }, 'dsh-ui-smooth: page passes')
121
- }
122
- }
123
-
124
- exports.apply = apply
125
- exports.inject = ['slots', 'sessions']
126
- exports.internals = {
127
- LAYOUT_PROPERTIES,
128
- PAINT_PROPERTIES,
129
- classify,
130
- coarsen,
131
- planSheets,
132
- planSweep,
133
- readAnimations,
134
- readKeyframes,
135
- sliceHistory
136
- }
1
+ /**
2
+ * Module face: install the page halves into one cordis plugin.
3
+ *
4
+ * Everything the page does hangs off this `apply`: the animation stylesheet, the
5
+ * transcript recycler, the settings section, and the debug handle that reports
6
+ * what they are doing.
7
+ */
8
+
9
+ const { installAnimations } = require('./animate.js')
10
+ const { LAYOUT_PROPERTIES, PAINT_PROPERTIES, classify, coarsen, planSheets, planSweep, readAnimations, readKeyframes } = require('./animations.js')
11
+ const { VERSION, cloneConfig, getConfig, loadConfig, onConfig } = require('./config.js')
12
+ const { createFollow } = require('./follow.js')
13
+ const { createKeepAwake } = require('./keepawake.js')
14
+ const { CONTAINMENT_CSS, createRecycler } = require('./recycle.js')
15
+ const { mainSessions, sliceHistory } = require('./release.js')
16
+ const { SETTINGS_CSS, SettingsSection } = require('./settings.js')
17
+
18
+ const CSS_ID = 'dsh-ui-smooth/css'
19
+
20
+ function installStyleSheet() {
21
+ if (document.getElementById(CSS_ID) !== null) return
22
+ const element = document.createElement('style')
23
+ element.id = CSS_ID
24
+ element.textContent = `${CONTAINMENT_CSS}\n${SETTINGS_CSS}`
25
+ ;(document.head ?? document.documentElement).appendChild(element)
26
+ }
27
+
28
+ function apply(ctx) {
29
+ installStyleSheet()
30
+ const animations = installAnimations()
31
+ const recycler = createRecycler(ctx)
32
+ const follow = createFollow()
33
+ const keepAwake = createKeepAwake()
34
+ const slotIds = () => {
35
+ try {
36
+ return ctx?.slots?.entries?.('settings.section')?.map?.((entry) => entry.id) ?? []
37
+ } catch (reason) {
38
+ return []
39
+ }
40
+ }
41
+
42
+ const handle = {
43
+ version: VERSION,
44
+ status: () => ({
45
+ animations: animations.status(),
46
+ recycling: recycler.stats(),
47
+ config: cloneConfig(getConfig()),
48
+ settingsSection: slotIds()
49
+ }),
50
+ rescan: () => animations.rescan(),
51
+ recycle: () => recycler.pass(),
52
+ refreshConfig: () => loadConfig().then(() => {
53
+ recycler.pass()
54
+ return cloneConfig(getConfig())
55
+ }),
56
+ restore: () => animations.restore(),
57
+ config: () => cloneConfig(getConfig()),
58
+ throttle: () => keepAwake.status(),
59
+ /** What the aggressive tier sees: the loaded window it would trim. */
60
+ history: () => {
61
+ const session = mainSessions(ctx?.sessions)[0]
62
+ if (session === undefined || session === null) return null
63
+ let entries = []
64
+ try {
65
+ entries = session.eventSource?.getSnapshot?.()?.entries ?? []
66
+ } catch (reason) {
67
+ entries = []
68
+ }
69
+ return {
70
+ sessionId: session.sessionId,
71
+ entries: entries.length,
72
+ turns: entries.filter((entry) => entry?.event?.type === 'turn/start').length,
73
+ baseSeq: session.baseSeq,
74
+ hasMore: session.hasMore === true
75
+ }
76
+ }
77
+ }
78
+ window.__dshUiSmooth = handle
79
+
80
+ /**
81
+ * The switch reads "throttle me in the background". Off means the page keeps
82
+ * running there: the host half asks the shell when it is inside one, and the
83
+ * page holds an inaudible audio stream open for the case where it is not.
84
+ */
85
+ const syncThrottle = () => {
86
+ // "Do not throttle in the background" is a request the page can honour
87
+ // whatever hosts it: the shell's own escape hatch is out of reach from a
88
+ // Node-mode host, so the audio stream is the only lever, and a browser
89
+ // tab wants the same thing. Asking for it twice would only cost an
90
+ // idle oscillator.
91
+ if (getConfig().backgroundThrottle === false) keepAwake.start()
92
+ else keepAwake.stop()
93
+ }
94
+
95
+ onConfig(() => {
96
+ recycler.restoreAll()
97
+ syncThrottle()
98
+ })
99
+
100
+ loadConfig().then(() => {
101
+ recycler.pass()
102
+ syncThrottle()
103
+ })
104
+
105
+ if (ctx !== undefined && ctx !== null && typeof ctx.slots?.inject === 'function') {
106
+ ctx.slots.inject('settings.section', () =>
107
+ ctx.slots.register({
108
+ name: 'settings.section',
109
+ id: 'dsh-ui-smooth',
110
+ order: 60,
111
+ label: () => '流畅性',
112
+ registrant: 'dsh-ui-smooth'
113
+ }, SettingsSection)
114
+ )
115
+ }
116
+
117
+ if (ctx !== undefined && ctx !== null && typeof ctx.effect === 'function') {
118
+ ctx.effect(() => () => {
119
+ animations.dispose()
120
+ follow.dispose()
121
+ keepAwake.stop()
122
+ recycler.dispose()
123
+ const css = document.getElementById(CSS_ID)
124
+ if (css !== null) css.remove()
125
+ if (window.__dshUiSmooth === handle) delete window.__dshUiSmooth
126
+ }, 'dsh-ui-smooth: page passes')
127
+ }
128
+ }
129
+
130
+ exports.apply = apply
131
+ exports.inject = ['slots', 'sessions']
132
+ exports.internals = {
133
+ LAYOUT_PROPERTIES,
134
+ PAINT_PROPERTIES,
135
+ classify,
136
+ coarsen,
137
+ planSheets,
138
+ planSweep,
139
+ readAnimations,
140
+ readKeyframes,
141
+ sliceHistory
142
+ }