@laraveladmin/dsh-stay-awake 0.0.0-stage → 0.1.0
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/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.en.md +224 -0
- package/README.md +226 -2
- package/client.js +650 -0
- package/cordis.patch.yml +3 -0
- package/docs/REQUIREMENTS.md +516 -0
- package/docs/VERIFICATION.md +122 -0
- package/docs/images/indicator.svg +19 -0
- package/docs/images/panel.svg +65 -0
- package/icon.svg +7 -0
- package/index.js +270 -0
- package/lib/activity.js +74 -0
- package/lib/config.js +81 -0
- package/lib/controller.js +305 -0
- package/lib/log.js +21 -0
- package/lib/platform.js +105 -0
- package/lib/power.js +121 -0
- package/lib/server.js +91 -0
- package/lib/state.js +70 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +60 -4
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 420 96" width="420" height="96" role="img" aria-label="状态栏指示器示意">
|
|
2
|
+
<rect width="420" height="96" rx="10" fill="#f7f8fa"/>
|
|
3
|
+
<text x="18" y="26" font-family="PingFang SC, Noto Sans SC, Helvetica, Arial, sans-serif" font-size="11" fill="#8a8f98">输入框下方状态栏(activity / usage 之后)</text>
|
|
4
|
+
|
|
5
|
+
<!-- fake composer dock line -->
|
|
6
|
+
<rect x="18" y="40" width="384" height="34" rx="8" fill="#ffffff" stroke="#e4e6eb"/>
|
|
7
|
+
<text x="34" y="61" font-family="PingFang SC, Noto Sans SC, Helvetica, Arial, sans-serif" font-size="11" fill="#b4b8c0">12.3k tokens · 2 个任务</text>
|
|
8
|
+
|
|
9
|
+
<!-- indicator, right aligned -->
|
|
10
|
+
<g transform="translate(232,40)">
|
|
11
|
+
<path d="M11 12a7.4 7.4 0 1 0 4.7 10.8A6.1 6.1 0 0 1 11 12z" fill="#22c55e"/>
|
|
12
|
+
<text x="22" y="21" font-family="PingFang SC, Noto Sans SC, Helvetica, Arial, sans-serif" font-size="12" fill="#6b7280">守夜 · 保持中</text>
|
|
13
|
+
<circle cx="116" cy="16" r="2.6" fill="#22c55e"/>
|
|
14
|
+
</g>
|
|
15
|
+
|
|
16
|
+
<!-- callouts -->
|
|
17
|
+
<line x1="300" y1="40" x2="300" y2="30" stroke="#c9ced6"/>
|
|
18
|
+
<text x="238" y="26" font-family="PingFang SC, Noto Sans SC, Helvetica, Arial, sans-serif" font-size="10" fill="#8a8f98">点击展开设置弹层</text>
|
|
19
|
+
</svg>
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 380 356" width="380" height="356" role="img" aria-label="设置弹层示意">
|
|
2
|
+
<style>
|
|
3
|
+
.bg{fill:#eef0f3}
|
|
4
|
+
.panel{fill:#ffffff}
|
|
5
|
+
.t{font-family:"PingFang SC","Noto Sans SC",Helvetica,Arial,sans-serif}
|
|
6
|
+
.title{font-size:12px;fill:#6b7280}
|
|
7
|
+
.key{font-size:11px;fill:#9aa0a8}
|
|
8
|
+
.val{font-size:11px;fill:#3c4149}
|
|
9
|
+
.label{font-size:12px;fill:#3c4149}
|
|
10
|
+
.sub{font-size:10px;fill:#9aa0a8}
|
|
11
|
+
.btn{fill:#f6f7f9;stroke:#e0e2e7}
|
|
12
|
+
.btntext{font-size:11px;fill:#3c4149}
|
|
13
|
+
</style>
|
|
14
|
+
|
|
15
|
+
<rect class="bg" width="380" height="356"/>
|
|
16
|
+
<rect class="panel" x="10" y="10" width="360" height="336" rx="12"/>
|
|
17
|
+
|
|
18
|
+
<!-- header: same status line as the collapsed indicator -->
|
|
19
|
+
<g transform="translate(24,26)">
|
|
20
|
+
<path d="M12 1.5a7.4 7.4 0 1 0 4.6 11A6.1 6.1 0 0 1 12 1.5z" fill="#22c55e" transform="scale(1.15)"/>
|
|
21
|
+
<text class="t title" x="20" y="12">守夜 · 保持中</text>
|
|
22
|
+
<circle cx="104" cy="8" r="2.4" fill="#22c55e"/>
|
|
23
|
+
</g>
|
|
24
|
+
<text class="t" x="352" y="40" font-size="15" fill="#9aa0a8" text-anchor="middle">×</text>
|
|
25
|
+
|
|
26
|
+
<!-- status grid -->
|
|
27
|
+
<g class="t">
|
|
28
|
+
<text class="key" x="24" y="72">状态</text><text class="val" x="86" y="72">保持中</text>
|
|
29
|
+
<text class="key" x="24" y="92">活跃源</text><text class="val" x="86" y="92">会话 1 · 维护 0 · 后台任务 0</text>
|
|
30
|
+
<text class="key" x="24" y="112">范围</text><text class="val" x="86" y="112">仅系统</text>
|
|
31
|
+
<text class="key" x="24" y="132">后端</text><text class="val" x="86" y="132">caffeinate</text>
|
|
32
|
+
<text class="key" x="24" y="152">已持续</text><text class="val" x="86" y="152">00:12:31</text>
|
|
33
|
+
</g>
|
|
34
|
+
|
|
35
|
+
<!-- toggles -->
|
|
36
|
+
<g class="t">
|
|
37
|
+
<text class="label" x="24" y="186">总开关</text>
|
|
38
|
+
<rect x="320" y="174" width="34" height="18" rx="9" fill="#22c55e"/>
|
|
39
|
+
<circle cx="345" cy="183" r="6" fill="#ffffff"/>
|
|
40
|
+
|
|
41
|
+
<text class="label" x="24" y="212">允许屏幕休眠</text>
|
|
42
|
+
<text class="sub" x="24" y="226">只防系统休眠,屏幕按系统设置熄灭</text>
|
|
43
|
+
<rect x="320" y="200" width="34" height="18" rx="9" fill="#22c55e"/>
|
|
44
|
+
<circle cx="345" cy="209" r="6" fill="#ffffff"/>
|
|
45
|
+
</g>
|
|
46
|
+
|
|
47
|
+
<!-- hold row -->
|
|
48
|
+
<g class="t">
|
|
49
|
+
<text class="sub" x="24" y="256">临时保持</text>
|
|
50
|
+
<rect class="btn" x="24" y="264" width="64" height="22" rx="8"/>
|
|
51
|
+
<text class="btntext" x="56" y="279" text-anchor="middle">30 分钟</text>
|
|
52
|
+
<rect class="btn" x="94" y="264" width="58" height="22" rx="8"/>
|
|
53
|
+
<text class="btntext" x="123" y="279" text-anchor="middle">2 小时</text>
|
|
54
|
+
<rect class="btn" x="158" y="264" width="50" height="22" rx="8"/>
|
|
55
|
+
<text class="btntext" x="183" y="279" text-anchor="middle">取消</text>
|
|
56
|
+
</g>
|
|
57
|
+
|
|
58
|
+
<!-- footer: advanced hint + repo icon + npm version -->
|
|
59
|
+
<g class="t">
|
|
60
|
+
<text class="sub" x="24" y="316">防抖、兜底时长等高级参数在 cordis.patch.yml 中配置</text>
|
|
61
|
+
<path d="M306 306 l-3.4 3.6 3.4 3.6 M313 306 l3.4 3.6 -3.4 3.6"
|
|
62
|
+
fill="none" stroke="#6b7280" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
|
|
63
|
+
<text class="sub" x="322" y="316" fill="#6b7280">v0.1.0</text>
|
|
64
|
+
</g>
|
|
65
|
+
</svg>
|
package/icon.svg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Stay awake">
|
|
2
|
+
<rect x="2" y="2" width="60" height="60" rx="14" fill="#16233a"/>
|
|
3
|
+
<path d="M39 12a21 21 0 1 0 0 40 17 17 0 1 1 0-40z" fill="#f2c86b"/>
|
|
4
|
+
<circle cx="47" cy="16" r="2.6" fill="#7dd3fc"/>
|
|
5
|
+
<circle cx="54" cy="24" r="1.8" fill="#7dd3fc"/>
|
|
6
|
+
<circle cx="51" cy="31" r="1.3" fill="#7dd3fc"/>
|
|
7
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stay awake — a DeepSeek Harness plugin that holds a system power assertion
|
|
3
|
+
* only while unattended work is still running:
|
|
4
|
+
*
|
|
5
|
+
* - agent turns that are actually running (`agent/status`);
|
|
6
|
+
* - the maintenance bracket of a manual `/compact` (session events
|
|
7
|
+
* `compaction/start` .. `compaction/end`, while `Agent.status` reports idle);
|
|
8
|
+
* - live background jobs (`ctx.jobs`), which keep running after the agent idles.
|
|
9
|
+
*
|
|
10
|
+
* The assertion is released after a debounce window once no source remains, and
|
|
11
|
+
* every process it spawns is bound to this host process so a crash cannot pin
|
|
12
|
+
* the machine awake forever.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { readFileSync } from 'node:fs'
|
|
16
|
+
|
|
17
|
+
import { createActivity } from './lib/activity.js'
|
|
18
|
+
import { applyState, normalizeConfig } from './lib/config.js'
|
|
19
|
+
import { createController } from './lib/controller.js'
|
|
20
|
+
import { createLog } from './lib/log.js'
|
|
21
|
+
import { backendName, verifyCommand } from './lib/platform.js'
|
|
22
|
+
import { createPower } from './lib/power.js'
|
|
23
|
+
import { createStatusHandler } from './lib/server.js'
|
|
24
|
+
import { defaultStatePath, loadState, saveState } from './lib/state.js'
|
|
25
|
+
|
|
26
|
+
const ROUTE = '/api/stay-awake'
|
|
27
|
+
|
|
28
|
+
let VERSION = '0.0.0'
|
|
29
|
+
try {
|
|
30
|
+
VERSION = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')).version
|
|
31
|
+
} catch {
|
|
32
|
+
// A packaged copy without package.json still works; the version is cosmetic.
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const inject = ['agents']
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
39
|
+
* @param {Record<string, unknown> | undefined} rawConfig
|
|
40
|
+
*/
|
|
41
|
+
export function apply(ctx, rawConfig) {
|
|
42
|
+
return start(ctx, rawConfig, {})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Activation body. `deps` is a test seam: production leaves it empty, tests
|
|
47
|
+
* inject a fake power backend and a deterministic clock.
|
|
48
|
+
*
|
|
49
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
50
|
+
* @param {Record<string, unknown> | undefined} rawConfig
|
|
51
|
+
* @param {{ power?: object, platform?: string, statePath?: string | false, now?: () => number, setTimer?: Function, clearTimer?: Function }} deps
|
|
52
|
+
*/
|
|
53
|
+
export function start(ctx, rawConfig, deps = {}) {
|
|
54
|
+
const normalized = normalizeConfig(rawConfig)
|
|
55
|
+
const log = createLog()
|
|
56
|
+
for (const warning of normalized.warnings) log.warn(warning)
|
|
57
|
+
|
|
58
|
+
// Launcher-owned preferences (popover switches) are persisted next to the
|
|
59
|
+
// profile; the patch layer remains the default for everything else.
|
|
60
|
+
const statePath = deps.statePath ?? defaultStatePath()
|
|
61
|
+
const loaded = deps.statePath === false ? { state: {}, warning: null } : loadState(statePath)
|
|
62
|
+
if (loaded.warning !== null) log.warn(`stay-awake: ${loaded.warning}`)
|
|
63
|
+
const config = applyState(normalized.config, loaded.state)
|
|
64
|
+
|
|
65
|
+
const persist = () => {
|
|
66
|
+
if (deps.statePath === false) return
|
|
67
|
+
const result = saveState(statePath, { enabled: config.enabled, scope: config.scope })
|
|
68
|
+
if (!result.ok) log.warn(`stay-awake: could not persist settings: ${result.error}`)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const platform = deps.platform ?? process.platform
|
|
72
|
+
const backend = backendName(platform)
|
|
73
|
+
const supported = backend !== null
|
|
74
|
+
|
|
75
|
+
const activity = createActivity()
|
|
76
|
+
const power = deps.power ?? createPower({ platform, config, log })
|
|
77
|
+
const controller = createController({
|
|
78
|
+
config,
|
|
79
|
+
power,
|
|
80
|
+
log,
|
|
81
|
+
supported,
|
|
82
|
+
now: deps.now,
|
|
83
|
+
setTimer: deps.setTimer,
|
|
84
|
+
clearTimer: deps.clearTimer,
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const sync = () => controller.update(activity.counts(config))
|
|
88
|
+
|
|
89
|
+
if (!supported) {
|
|
90
|
+
log.warn(`stay-awake: no power backend on "${platform}"; the plugin is a no-op here`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
ctx.effect(() => () => controller.dispose(), 'stay-awake: assertion')
|
|
94
|
+
|
|
95
|
+
ctx.effect(() => {
|
|
96
|
+
const disposers = [
|
|
97
|
+
ctx.on('agent/status', (payload) => {
|
|
98
|
+
const agent = payload?.agent
|
|
99
|
+
if (agent === undefined) return
|
|
100
|
+
activity.setAgent(agent.id, payload.status === 'running')
|
|
101
|
+
sync()
|
|
102
|
+
}),
|
|
103
|
+
ctx.on('agent/created', (payload) => {
|
|
104
|
+
const agent = payload?.agent
|
|
105
|
+
if (agent === undefined) return
|
|
106
|
+
const status = statusOf(ctx, agent)
|
|
107
|
+
if (status !== undefined) activity.setAgent(agent.id, status === 'running')
|
|
108
|
+
sync()
|
|
109
|
+
}),
|
|
110
|
+
ctx.on('agent/disposed', (payload) => {
|
|
111
|
+
const agent = payload?.agent
|
|
112
|
+
if (agent === undefined) return
|
|
113
|
+
activity.removeAgent(agent.id)
|
|
114
|
+
sync()
|
|
115
|
+
}),
|
|
116
|
+
ctx.on('session/event', (session, event) => {
|
|
117
|
+
if (config.includeMaintenance !== true) return
|
|
118
|
+
if (event?.type === 'compaction/start') {
|
|
119
|
+
activity.openMaintenance(session.id, event.data?.compactionId)
|
|
120
|
+
sync()
|
|
121
|
+
} else if (event?.type === 'compaction/end') {
|
|
122
|
+
activity.closeMaintenance(session.id, event.data?.compactionId)
|
|
123
|
+
sync()
|
|
124
|
+
}
|
|
125
|
+
}),
|
|
126
|
+
]
|
|
127
|
+
return () => {
|
|
128
|
+
for (const dispose of disposers) {
|
|
129
|
+
try {
|
|
130
|
+
dispose()
|
|
131
|
+
} catch {
|
|
132
|
+
// Unloading is best effort.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}, 'stay-awake: activity sources')
|
|
137
|
+
|
|
138
|
+
const jobs = ctx.get('jobs')
|
|
139
|
+
if (jobs !== undefined && jobs !== null && config.includeBackgroundJobs) {
|
|
140
|
+
ctx.effect(() => {
|
|
141
|
+
try {
|
|
142
|
+
return jobs.events.subscribe({ owners: 'all' }, (event) => {
|
|
143
|
+
const job = event?.job
|
|
144
|
+
if (job === undefined || job === null) return
|
|
145
|
+
if (event.type === 'settled' || event.type === 'removed') activity.removeJob(job.id)
|
|
146
|
+
else activity.setJob(job.id, job.status)
|
|
147
|
+
sync()
|
|
148
|
+
})
|
|
149
|
+
} catch (error) {
|
|
150
|
+
log.warn(`stay-awake: could not subscribe to background jobs: ${describe(error)}`)
|
|
151
|
+
return () => {}
|
|
152
|
+
}
|
|
153
|
+
}, 'stay-awake: background jobs')
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function reconcile() {
|
|
157
|
+
try {
|
|
158
|
+
const live = new Set()
|
|
159
|
+
for (const agent of ctx.agents.list()) {
|
|
160
|
+
if (agent === undefined || agent === null) continue
|
|
161
|
+
live.add(agent.id)
|
|
162
|
+
const status = statusOf(ctx, agent)
|
|
163
|
+
if (status !== undefined) activity.setAgent(agent.id, status === 'running')
|
|
164
|
+
}
|
|
165
|
+
for (const id of activity.agentIds()) {
|
|
166
|
+
if (!live.has(id)) activity.removeAgent(id)
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
log.warn(`stay-awake: agent reconcile failed: ${describe(error)}`)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (jobs !== undefined && jobs !== null && config.includeBackgroundJobs) {
|
|
173
|
+
const seen = new Set()
|
|
174
|
+
try {
|
|
175
|
+
for (const view of jobs.list()) {
|
|
176
|
+
seen.add(view.id)
|
|
177
|
+
activity.setJob(view.id, view.status)
|
|
178
|
+
}
|
|
179
|
+
for (const id of activity.agentIds()) {
|
|
180
|
+
for (const view of jobs.list(id)) {
|
|
181
|
+
seen.add(view.id)
|
|
182
|
+
activity.setJob(view.id, view.status)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
log.warn(`stay-awake: job reconcile failed: ${describe(error)}`)
|
|
187
|
+
}
|
|
188
|
+
for (const id of activity.jobIds()) {
|
|
189
|
+
if (!seen.has(id)) activity.removeJob(id)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
activity.pruneMaintenance(config.maxMaintenanceMinutes)
|
|
194
|
+
sync()
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (config.reconcileSeconds > 0) {
|
|
198
|
+
ctx.effect(() => {
|
|
199
|
+
const timer = setInterval(reconcile, config.reconcileSeconds * 1000)
|
|
200
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
201
|
+
return () => clearInterval(timer)
|
|
202
|
+
}, 'stay-awake: reconcile')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
ctx.inject(['connection'], (scope) => {
|
|
206
|
+
const handler = createStatusHandler({
|
|
207
|
+
controller,
|
|
208
|
+
activity,
|
|
209
|
+
config,
|
|
210
|
+
platform,
|
|
211
|
+
version: VERSION,
|
|
212
|
+
backend,
|
|
213
|
+
verify: verifyCommand(platform),
|
|
214
|
+
onChange: persist,
|
|
215
|
+
})
|
|
216
|
+
scope.effect(() => {
|
|
217
|
+
let dispose
|
|
218
|
+
try {
|
|
219
|
+
dispose = scope.connection.fetch.register({
|
|
220
|
+
path: ROUTE,
|
|
221
|
+
methods: ['GET', 'POST'],
|
|
222
|
+
requestBody: 'buffered',
|
|
223
|
+
fetch: handler,
|
|
224
|
+
})
|
|
225
|
+
} catch (error) {
|
|
226
|
+
log.warn(`stay-awake: could not register ${ROUTE}: ${describe(error)}`)
|
|
227
|
+
return () => {}
|
|
228
|
+
}
|
|
229
|
+
return () => {
|
|
230
|
+
try {
|
|
231
|
+
Promise.resolve(dispose()).catch(() => {})
|
|
232
|
+
} catch {
|
|
233
|
+
// Unloading is best effort.
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}, 'stay-awake: status route')
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
// A plugin loaded while a turn is already running has no event to replay.
|
|
240
|
+
reconcile()
|
|
241
|
+
log.info(`stay-awake ${VERSION} active on ${platform} (${backend ?? 'no backend'})`)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Read an agent's running state without trusting one internal shape.
|
|
246
|
+
* `Agent.status` reports `idle` during maintenance; an open turn is the
|
|
247
|
+
* fallback for a future version that drops the getter.
|
|
248
|
+
*/
|
|
249
|
+
function statusOf(ctx, agent) {
|
|
250
|
+
try {
|
|
251
|
+
const status = agent.status
|
|
252
|
+
if (status === 'running' || status === 'idle') return status
|
|
253
|
+
} catch {
|
|
254
|
+
// Fall through to the turn-boundary probe.
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
const session = agent.session
|
|
258
|
+
if (session === undefined || session === null) return undefined
|
|
259
|
+
const boundary = ctx.agents.turnBoundaryOf(session)
|
|
260
|
+
if (boundary?.openTurnStartSeq !== null && boundary?.openTurnStartSeq !== undefined) return 'running'
|
|
261
|
+
return 'idle'
|
|
262
|
+
} catch {
|
|
263
|
+
return undefined
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function describe(error) {
|
|
268
|
+
if (error instanceof Error) return error.message
|
|
269
|
+
return String(error)
|
|
270
|
+
}
|
package/lib/activity.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live activity sources.
|
|
3
|
+
*
|
|
4
|
+
* The tracker is a plain data structure: the plugin's event wiring mutates it,
|
|
5
|
+
* and the controller only ever asks for counts. Keeping it pure makes the
|
|
6
|
+
* "who counts as work" rules unit-testable without a DSH runtime.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function createActivity({ now = Date.now } = {}) {
|
|
10
|
+
/** @type {Set<string>} running agent/session ids */
|
|
11
|
+
const agents = new Set()
|
|
12
|
+
/** @type {Map<string, { compactionId: string | undefined, startedAt: number }>} */
|
|
13
|
+
const maintenance = new Map()
|
|
14
|
+
/** @type {Map<string, string>} jobId -> status */
|
|
15
|
+
const jobs = new Map()
|
|
16
|
+
|
|
17
|
+
function setAgent(id, running) {
|
|
18
|
+
if (running) agents.add(id)
|
|
19
|
+
else agents.delete(id)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function openMaintenance(sessionId, compactionId) {
|
|
23
|
+
maintenance.set(sessionId, { compactionId, startedAt: now() })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function closeMaintenance(sessionId, compactionId) {
|
|
27
|
+
const current = maintenance.get(sessionId)
|
|
28
|
+
if (current === undefined) return
|
|
29
|
+
if (compactionId !== undefined && current.compactionId !== undefined && current.compactionId !== compactionId) return
|
|
30
|
+
maintenance.delete(sessionId)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function pruneMaintenance(maxMinutes) {
|
|
34
|
+
const cutoff = now() - maxMinutes * 60_000
|
|
35
|
+
for (const [sessionId, record] of maintenance) {
|
|
36
|
+
if (record.startedAt < cutoff) maintenance.delete(sessionId)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function counts(config) {
|
|
41
|
+
const liveJobs = config.includeBackgroundJobs
|
|
42
|
+
? [...jobs.values()].filter((status) => status === 'running' || status === 'stopping').length
|
|
43
|
+
: 0
|
|
44
|
+
return {
|
|
45
|
+
agents: agents.size,
|
|
46
|
+
maintenance: config.includeMaintenance ? maintenance.size : 0,
|
|
47
|
+
jobs: liveJobs,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function hasWork(config) {
|
|
52
|
+
const value = counts(config)
|
|
53
|
+
return value.agents + value.maintenance + value.jobs > 0
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
setAgent,
|
|
58
|
+
removeAgent: (id) => agents.delete(id),
|
|
59
|
+
agentIds: () => [...agents],
|
|
60
|
+
openMaintenance,
|
|
61
|
+
closeMaintenance,
|
|
62
|
+
pruneMaintenance,
|
|
63
|
+
setJob: (id, status) => jobs.set(id, status),
|
|
64
|
+
removeJob: (id) => jobs.delete(id),
|
|
65
|
+
jobIds: () => [...jobs.keys()],
|
|
66
|
+
counts,
|
|
67
|
+
hasWork,
|
|
68
|
+
reset() {
|
|
69
|
+
agents.clear()
|
|
70
|
+
maintenance.clear()
|
|
71
|
+
jobs.clear()
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
}
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Config normalization for the Stay Awake plugin.
|
|
3
|
+
*
|
|
4
|
+
* This plugin deliberately declares no `Config` schema: it must keep working
|
|
5
|
+
* with no third-party runtime dependency, and the Loader still passes the row's
|
|
6
|
+
* `config` object through unchanged. Every value is therefore validated here
|
|
7
|
+
* and an invalid value falls back to its default with a warning instead of
|
|
8
|
+
* refusing to load.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export const DEFAULTS = Object.freeze({
|
|
12
|
+
enabled: true,
|
|
13
|
+
scope: 'system',
|
|
14
|
+
releaseDelayMs: 30_000,
|
|
15
|
+
maxHoldHours: 8,
|
|
16
|
+
includeBackgroundJobs: true,
|
|
17
|
+
includeMaintenance: true,
|
|
18
|
+
reconcileSeconds: 60,
|
|
19
|
+
maxMaintenanceMinutes: 120,
|
|
20
|
+
caffeinatePath: '/usr/bin/caffeinate',
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const SCOPES = new Set(['system', 'display'])
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Overlay persisted UI state on top of the patch-layer config.
|
|
27
|
+
* The patch layer is the *default*; what the popover saved wins.
|
|
28
|
+
* @param {typeof DEFAULTS} config
|
|
29
|
+
* @param {{ enabled?: boolean, scope?: 'system'|'display' }} state
|
|
30
|
+
*/
|
|
31
|
+
export function applyState(config, state) {
|
|
32
|
+
const next = { ...config }
|
|
33
|
+
if (typeof state?.enabled === 'boolean') next.enabled = state.enabled
|
|
34
|
+
if (SCOPES.has(state?.scope)) next.scope = state.scope
|
|
35
|
+
return next
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function asBoolean(value, fallback) {
|
|
39
|
+
return typeof value === 'boolean' ? value : fallback
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function asNumber(value, fallback, { min = 0, max = Number.MAX_SAFE_INTEGER } = {}) {
|
|
43
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return fallback
|
|
44
|
+
if (value < min) return min
|
|
45
|
+
if (value > max) return max
|
|
46
|
+
return value
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function asPath(value, fallback) {
|
|
50
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : fallback
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {Record<string, unknown> | undefined} raw
|
|
55
|
+
* @returns {{ config: typeof DEFAULTS, warnings: string[] }}
|
|
56
|
+
*/
|
|
57
|
+
export function normalizeConfig(raw) {
|
|
58
|
+
const input = raw !== null && typeof raw === 'object' ? raw : {}
|
|
59
|
+
const warnings = []
|
|
60
|
+
|
|
61
|
+
const config = {
|
|
62
|
+
enabled: asBoolean(input.enabled, DEFAULTS.enabled),
|
|
63
|
+
scope: SCOPES.has(input.scope) ? input.scope : DEFAULTS.scope,
|
|
64
|
+
releaseDelayMs: asNumber(input.releaseDelayMs, DEFAULTS.releaseDelayMs, { min: 0, max: 600_000 }),
|
|
65
|
+
maxHoldHours: asNumber(input.maxHoldHours, DEFAULTS.maxHoldHours, { min: 0, max: 24 * 30 }),
|
|
66
|
+
includeBackgroundJobs: asBoolean(input.includeBackgroundJobs, DEFAULTS.includeBackgroundJobs),
|
|
67
|
+
includeMaintenance: asBoolean(input.includeMaintenance, DEFAULTS.includeMaintenance),
|
|
68
|
+
reconcileSeconds: asNumber(input.reconcileSeconds, DEFAULTS.reconcileSeconds, { min: 0, max: 3_600 }),
|
|
69
|
+
maxMaintenanceMinutes: asNumber(input.maxMaintenanceMinutes, DEFAULTS.maxMaintenanceMinutes, { min: 1, max: 24 * 60 }),
|
|
70
|
+
caffeinatePath: asPath(input.caffeinatePath, DEFAULTS.caffeinatePath),
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const key of Object.keys(config)) {
|
|
74
|
+
if (input[key] === undefined) continue
|
|
75
|
+
if (config[key] !== input[key]) {
|
|
76
|
+
warnings.push(`config: "${key}" has an invalid value (${JSON.stringify(input[key])}); using ${JSON.stringify(config[key])}`)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return { config, warnings }
|
|
81
|
+
}
|