@unihc/dsh-process-manager 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +65 -2
- package/catalog.js +571 -0
- package/client.js +1537 -0
- package/collector.js +145 -0
- package/cordis.patch.yml +4 -0
- package/docs/DETAIL.md +1 -0
- package/docs/DETAIL.zh.md +1 -0
- package/docs/panel.png +0 -0
- package/docs/preview.svg +42 -0
- package/icon.svg +10 -0
- package/index.js +255 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/model.js +481 -0
- package/native.js +863 -0
- package/operations.js +540 -0
- package/package.json +61 -4
package/model.js
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn normalised processes and startup entries into the model the panel and
|
|
3
|
+
* the agent tools share: app grouping, identity text, risk, and the
|
|
4
|
+
* process <-> startup cross links. Pure functions.
|
|
5
|
+
*/
|
|
6
|
+
import { describeProcess, describeService, lookupVendor, normalizeName } from './catalog.js'
|
|
7
|
+
import { exeNameOf } from './collector.js'
|
|
8
|
+
|
|
9
|
+
const BOUNDARY = new Set([
|
|
10
|
+
'explorer.exe', 'services.exe', 'wininit.exe', 'svchost.exe',
|
|
11
|
+
'winlogon.exe', 'smss.exe', 'csrss.exe', 'system', 'registry',
|
|
12
|
+
'secure system', 'system idle process',
|
|
13
|
+
])
|
|
14
|
+
|
|
15
|
+
function normalizePath(value) {
|
|
16
|
+
return String(value ?? '').replace(/\//g, '\\').toLowerCase()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function riskRank(risk) {
|
|
20
|
+
return risk === 'protected' ? 4 : risk === 'critical' ? 3 : risk === 'caution' ? 2 : 1
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function worstRisk(list) {
|
|
24
|
+
let result = 'normal'
|
|
25
|
+
for (const risk of list) {
|
|
26
|
+
if (riskRank(risk) > riskRank(result)) result = risk
|
|
27
|
+
}
|
|
28
|
+
return result
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function formatBytes(bytes) {
|
|
32
|
+
const value = Number(bytes)
|
|
33
|
+
if (!Number.isFinite(value) || value <= 0) return '0 B'
|
|
34
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
35
|
+
let index = 0
|
|
36
|
+
let size = value
|
|
37
|
+
while (size >= 1024 && index < units.length - 1) {
|
|
38
|
+
size /= 1024
|
|
39
|
+
index += 1
|
|
40
|
+
}
|
|
41
|
+
const digits = index === 0 ? 0 : size >= 100 ? 0 : 1
|
|
42
|
+
return `${size.toFixed(digits)} ${units[index]}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function formatPercent(value) {
|
|
46
|
+
const number = Number(value)
|
|
47
|
+
if (!Number.isFinite(number) || number <= 0) return '0%'
|
|
48
|
+
return `${number >= 10 ? number.toFixed(0) : number.toFixed(1)}%`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Resolve the executable a startup command refers to when WMI gave none. */
|
|
52
|
+
function exeFromCommand(command) {
|
|
53
|
+
const text = String(command ?? '').trim()
|
|
54
|
+
if (text === '') return ''
|
|
55
|
+
if (text.startsWith('"')) {
|
|
56
|
+
const end = text.indexOf('"', 1)
|
|
57
|
+
if (end > 1) return text.slice(1, end)
|
|
58
|
+
}
|
|
59
|
+
if (text.toLowerCase().startsWith('explorer.exe ')) return text
|
|
60
|
+
return text.split(/\s+/)[0] ?? ''
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function describeExe(exePath, publisher, command) {
|
|
64
|
+
let resolved = String(exePath ?? '').trim().replace(/^"+|"+$/g, '')
|
|
65
|
+
if (resolved === '' || /\s/.test(resolved) && !/^[a-z]:[\\/]/i.test(resolved)) {
|
|
66
|
+
const fromCommand = exeFromCommand(command)
|
|
67
|
+
if (fromCommand !== '') resolved = fromCommand
|
|
68
|
+
}
|
|
69
|
+
const name = exeNameOf(resolved)
|
|
70
|
+
const info = describeProcess({ name, company: publisher, path: resolved }, 'zh')
|
|
71
|
+
return {
|
|
72
|
+
exe_path: resolved,
|
|
73
|
+
exe_name: name,
|
|
74
|
+
label: name ? info.label : (String(command ?? '').split(/\s+/)[0] || '未知启动项'),
|
|
75
|
+
label_en: name ? info.labelEn : (String(command ?? '').split(/\s+/)[0] || 'Unknown'),
|
|
76
|
+
purpose: info.purpose,
|
|
77
|
+
purpose_en: info.purposeEn,
|
|
78
|
+
risk: info.risk,
|
|
79
|
+
vendor: info.vendor,
|
|
80
|
+
vendor_en: info.vendorEn,
|
|
81
|
+
protected_reason: info.protectedReason,
|
|
82
|
+
category: info.category,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function buildRunEntry(run) {
|
|
87
|
+
const info = describeExe(run.exe_path, run.publisher, run.command)
|
|
88
|
+
return {
|
|
89
|
+
id: `run:${run.hive}:${run.key}:${run.name}`,
|
|
90
|
+
source: 'run',
|
|
91
|
+
scope: run.hive === 'HKCU' ? 'user' : 'machine',
|
|
92
|
+
name: run.name,
|
|
93
|
+
label: info.label,
|
|
94
|
+
label_en: info.label_en,
|
|
95
|
+
command: run.command,
|
|
96
|
+
exe_path: info.exe_path,
|
|
97
|
+
exe_name: info.exe_name,
|
|
98
|
+
publisher: run.publisher || info.vendor,
|
|
99
|
+
purpose: info.purpose,
|
|
100
|
+
purpose_en: info.purpose_en,
|
|
101
|
+
enabled: run.approved !== 'disabled',
|
|
102
|
+
state_source: 'startup-approved',
|
|
103
|
+
admin_required: run.hive !== 'HKCU',
|
|
104
|
+
risk: info.risk,
|
|
105
|
+
protected_reason: info.protected_reason,
|
|
106
|
+
triggers: run.key.toLowerCase().includes('runonce') ? ['once'] : ['logon'],
|
|
107
|
+
description: `${run.hive}\\…\\${run.key}`,
|
|
108
|
+
detail: { hive: run.hive, key: run.key, name: run.name },
|
|
109
|
+
running_pids: [],
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function buildFolderEntry(folder) {
|
|
114
|
+
const info = describeExe(folder.exe_path, folder.publisher, folder.target)
|
|
115
|
+
const base = folder.file.replace(/\.[^.]+$/, '')
|
|
116
|
+
return {
|
|
117
|
+
id: `folder:${folder.scope}:${folder.file}`,
|
|
118
|
+
source: 'folder',
|
|
119
|
+
scope: folder.scope,
|
|
120
|
+
name: folder.file,
|
|
121
|
+
label: info.label || base,
|
|
122
|
+
label_en: info.label_en || base,
|
|
123
|
+
command: folder.target || folder.path,
|
|
124
|
+
exe_path: info.exe_path,
|
|
125
|
+
exe_name: info.exe_name,
|
|
126
|
+
publisher: folder.publisher || info.vendor,
|
|
127
|
+
purpose: info.purpose,
|
|
128
|
+
purpose_en: info.purpose_en,
|
|
129
|
+
enabled: folder.approved !== 'disabled',
|
|
130
|
+
state_source: 'startup-approved',
|
|
131
|
+
admin_required: folder.scope === 'machine',
|
|
132
|
+
risk: info.risk,
|
|
133
|
+
protected_reason: info.protected_reason,
|
|
134
|
+
triggers: ['logon'],
|
|
135
|
+
description: folder.scope === 'machine' ? '公共启动文件夹' : '当前用户启动文件夹',
|
|
136
|
+
detail: { scope: folder.scope, file: folder.file },
|
|
137
|
+
running_pids: [],
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function buildTaskEntry(task) {
|
|
142
|
+
const info = describeExe(task.exec, task.author, task.args)
|
|
143
|
+
const isMicrosoft = /^\\microsoft(\\|$)/i.test(task.path)
|
|
144
|
+
return {
|
|
145
|
+
id: `task:${task.path}${task.name}`,
|
|
146
|
+
source: 'task',
|
|
147
|
+
scope: 'machine',
|
|
148
|
+
name: task.name,
|
|
149
|
+
label: task.name,
|
|
150
|
+
label_en: task.name,
|
|
151
|
+
app_label: info.label,
|
|
152
|
+
app_label_en: info.label_en,
|
|
153
|
+
command: [task.exec, task.args].filter(Boolean).join(' '),
|
|
154
|
+
exe_path: info.exe_path,
|
|
155
|
+
exe_name: info.exe_name,
|
|
156
|
+
publisher: task.author || info.vendor,
|
|
157
|
+
purpose: info.purpose || task.description,
|
|
158
|
+
purpose_en: info.purpose_en,
|
|
159
|
+
enabled: task.state !== 'Disabled',
|
|
160
|
+
state_source: 'scheduled-task',
|
|
161
|
+
admin_required: true,
|
|
162
|
+
risk: isMicrosoft ? 'protected' : info.risk,
|
|
163
|
+
protected_reason: isMicrosoft ? 'Windows 自带任务,面板不修改' : info.protected_reason,
|
|
164
|
+
triggers: task.triggers,
|
|
165
|
+
description: `${task.path} · ${task.triggers.map(item => (item.toLowerCase().includes('boot') ? '开机时' : '登录时')).join('/') || '登录时'}运行`,
|
|
166
|
+
detail: { path: task.path, name: task.name },
|
|
167
|
+
hidden: task.hidden,
|
|
168
|
+
running_pids: [],
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function buildServiceEntry(service) {
|
|
173
|
+
const described = describeService(service, 'zh')
|
|
174
|
+
const pathInfo = describeExe(service.exe || service.path, service.publisher, service.path)
|
|
175
|
+
const info = {
|
|
176
|
+
...pathInfo,
|
|
177
|
+
label: described.label || pathInfo.label,
|
|
178
|
+
label_en: described.labelEn || pathInfo.label_en,
|
|
179
|
+
purpose: described.purpose || pathInfo.purpose,
|
|
180
|
+
risk: described.risk,
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
id: `service:${service.name}`,
|
|
184
|
+
source: 'service',
|
|
185
|
+
scope: 'machine',
|
|
186
|
+
name: service.name,
|
|
187
|
+
label: info.label,
|
|
188
|
+
label_en: info.label_en,
|
|
189
|
+
app_label: pathInfo.label,
|
|
190
|
+
app_label_en: pathInfo.label_en,
|
|
191
|
+
command: service.path,
|
|
192
|
+
exe_path: pathInfo.exe_path,
|
|
193
|
+
exe_name: pathInfo.exe_name,
|
|
194
|
+
publisher: service.publisher || pathInfo.vendor,
|
|
195
|
+
purpose: info.purpose,
|
|
196
|
+
purpose_en: info.purpose_en,
|
|
197
|
+
enabled: service.start !== 'Disabled',
|
|
198
|
+
state_source: 'service',
|
|
199
|
+
admin_required: true,
|
|
200
|
+
risk: info.risk,
|
|
201
|
+
protected_reason: described.system ? 'Windows 系统服务,面板不修改' : null,
|
|
202
|
+
system: described.system,
|
|
203
|
+
triggers: service.start === 'Disabled' ? [] : ['logon'],
|
|
204
|
+
description: `服务 · 启动类型 ${service.start}${service.delayed ? '(延迟)' : ''}${service.state ? ` · ${service.state}` : ''}`,
|
|
205
|
+
detail: { name: service.name, delayed: service.delayed },
|
|
206
|
+
start_mode: service.start,
|
|
207
|
+
state: service.state,
|
|
208
|
+
service_pid: service.pid,
|
|
209
|
+
delayed: service.delayed,
|
|
210
|
+
running_pids: [],
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function buildStartupEntries(startup, { lang = 'zh' } = {}) {
|
|
215
|
+
const entries = [
|
|
216
|
+
...(startup?.run ?? []).map(buildRunEntry),
|
|
217
|
+
...(startup?.folder ?? []).map(buildFolderEntry),
|
|
218
|
+
...(startup?.tasks ?? []).map(buildTaskEntry),
|
|
219
|
+
...(startup?.services ?? []).map(buildServiceEntry),
|
|
220
|
+
]
|
|
221
|
+
const sourceOrder = { run: 0, folder: 1, task: 2, service: 3 }
|
|
222
|
+
entries.sort((a, b) => {
|
|
223
|
+
// Enabled entries first, disabled ones after; the panel and the tools both
|
|
224
|
+
// read this order, so the rule lives here rather than in one consumer.
|
|
225
|
+
if (a.enabled !== b.enabled) return a.enabled ? -1 : 1
|
|
226
|
+
const bySource = (sourceOrder[a.source] ?? 9) - (sourceOrder[b.source] ?? 9)
|
|
227
|
+
if (bySource !== 0) return bySource
|
|
228
|
+
return String(a.labelEn ?? a.label).localeCompare(String(b.labelEn ?? b.label), lang === 'en' ? 'en' : 'zh-Hans')
|
|
229
|
+
})
|
|
230
|
+
return entries
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function computeRoots(processes) {
|
|
234
|
+
const byPid = new Map()
|
|
235
|
+
for (const process of processes) byPid.set(process.pid, process)
|
|
236
|
+
const roots = new Map()
|
|
237
|
+
for (const process of processes) {
|
|
238
|
+
if (process.has_window) {
|
|
239
|
+
roots.set(process.pid, process.pid)
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
let chosen = process
|
|
243
|
+
let current = process
|
|
244
|
+
const seen = new Set([process.pid])
|
|
245
|
+
for (let depth = 0; depth < 16; depth += 1) {
|
|
246
|
+
const parent = byPid.get(current.ppid)
|
|
247
|
+
if (parent === undefined || seen.has(parent.pid)) break
|
|
248
|
+
seen.add(parent.pid)
|
|
249
|
+
if (BOUNDARY.has(normalizeName(parent.name))) break
|
|
250
|
+
chosen = parent
|
|
251
|
+
current = parent
|
|
252
|
+
}
|
|
253
|
+
roots.set(process.pid, chosen.pid)
|
|
254
|
+
}
|
|
255
|
+
return roots
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function matchStartup(processes, entries) {
|
|
259
|
+
const byExe = new Map()
|
|
260
|
+
for (const entry of entries) {
|
|
261
|
+
if (entry.source === 'service') continue
|
|
262
|
+
const key = normalizePath(entry.exe_path)
|
|
263
|
+
if (key === '') continue
|
|
264
|
+
const list = byExe.get(key) ?? []
|
|
265
|
+
list.push(entry)
|
|
266
|
+
byExe.set(key, list)
|
|
267
|
+
}
|
|
268
|
+
for (const process of processes) {
|
|
269
|
+
const key = normalizePath(process.path)
|
|
270
|
+
const matches = key === '' ? [] : (byExe.get(key) ?? [])
|
|
271
|
+
process.autostart = matches.filter(entry => entry.enabled).map(entry => entry.id)
|
|
272
|
+
process.autostart_any = matches.map(entry => entry.id)
|
|
273
|
+
for (const entry of matches) {
|
|
274
|
+
if (!entry.running_pids.includes(process.pid)) entry.running_pids.push(process.pid)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
for (const entry of entries) {
|
|
278
|
+
if (entry.source !== 'service') continue
|
|
279
|
+
entry.running_pids = entry.enabled && entry.state === 'Running' && entry.service_pid > 0 ? [entry.service_pid] : []
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function decorateProcess(process) {
|
|
284
|
+
const info = describeProcess(process, 'zh')
|
|
285
|
+
return {
|
|
286
|
+
pid: process.pid,
|
|
287
|
+
ppid: process.ppid,
|
|
288
|
+
name: process.name,
|
|
289
|
+
path: process.path,
|
|
290
|
+
cmdline: process.cmdline,
|
|
291
|
+
company: process.company,
|
|
292
|
+
product: process.product,
|
|
293
|
+
description: process.description,
|
|
294
|
+
version: process.version,
|
|
295
|
+
started_at: process.started_at,
|
|
296
|
+
session_id: process.session_id,
|
|
297
|
+
has_window: process.has_window,
|
|
298
|
+
window_title: process.window_title,
|
|
299
|
+
mem_bytes: process.mem_bytes,
|
|
300
|
+
cpu_percent: process.cpu_percent,
|
|
301
|
+
services: process.services,
|
|
302
|
+
label: info.label,
|
|
303
|
+
label_en: info.labelEn,
|
|
304
|
+
purpose: info.purpose,
|
|
305
|
+
purpose_en: info.purposeEn,
|
|
306
|
+
category: info.category,
|
|
307
|
+
vendor: info.vendor,
|
|
308
|
+
risk: info.risk,
|
|
309
|
+
protected_reason: info.protectedReason,
|
|
310
|
+
hash_like: info.hashLike,
|
|
311
|
+
kind: process.has_window ? 'app' : (process.path && /^[a-z]:\\windows\\/i.test(process.path.replace(/\//g, '\\')) ? 'system' : (info.risk === 'protected' || info.category === 'system' ? 'system' : 'background')),
|
|
312
|
+
autostart: process.autostart ?? [],
|
|
313
|
+
autostart_any: process.autostart_any ?? [],
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export function buildSnapshot(normalized, { lang = 'zh' } = {}) {
|
|
318
|
+
const processes = Array.isArray(normalized?.processes) ? normalized.processes : []
|
|
319
|
+
const entries = buildStartupEntries(normalized?.startup, { lang })
|
|
320
|
+
matchStartup(processes, entries)
|
|
321
|
+
const decorated = processes.map(item => {
|
|
322
|
+
const result = decorateProcess(item)
|
|
323
|
+
return result
|
|
324
|
+
})
|
|
325
|
+
const roots = computeRoots(processes)
|
|
326
|
+
const byPid = new Map(decorated.map(item => [item.pid, item]))
|
|
327
|
+
|
|
328
|
+
const groups = new Map()
|
|
329
|
+
for (const process of decorated) {
|
|
330
|
+
const rootPid = roots.get(process.pid) ?? process.pid
|
|
331
|
+
const root = byPid.get(rootPid) ?? process
|
|
332
|
+
const key = normalizePath(root.path) || `name:${normalizeName(root.name)}`
|
|
333
|
+
let group = groups.get(key)
|
|
334
|
+
if (group === undefined) {
|
|
335
|
+
group = {
|
|
336
|
+
key,
|
|
337
|
+
root_pid: root.pid,
|
|
338
|
+
label: root.label,
|
|
339
|
+
label_en: root.label_en,
|
|
340
|
+
publisher: root.vendor || root.company || '',
|
|
341
|
+
purpose: root.purpose,
|
|
342
|
+
purpose_en: root.purpose_en,
|
|
343
|
+
category: root.category,
|
|
344
|
+
kind: root.has_window ? 'app' : (root.kind === 'system' ? 'system' : root.kind),
|
|
345
|
+
risks: [],
|
|
346
|
+
pids: [],
|
|
347
|
+
mem_bytes: 0,
|
|
348
|
+
cpu_percent: 0,
|
|
349
|
+
autostart: false,
|
|
350
|
+
}
|
|
351
|
+
groups.set(key, group)
|
|
352
|
+
}
|
|
353
|
+
group.pids.push(process.pid)
|
|
354
|
+
group.mem_bytes += process.mem_bytes
|
|
355
|
+
group.cpu_percent += process.cpu_percent
|
|
356
|
+
group.risks.push(process.risk)
|
|
357
|
+
if (process.autostart.length > 0) group.autostart = true
|
|
358
|
+
if (process.has_window) group.kind = 'app'
|
|
359
|
+
else if (process.kind === 'system' && group.kind !== 'app') group.kind = 'system'
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const groupList = [...groups.values()].map(group => ({
|
|
363
|
+
key: group.key,
|
|
364
|
+
root_pid: group.root_pid,
|
|
365
|
+
label: group.label,
|
|
366
|
+
label_en: group.label_en,
|
|
367
|
+
publisher: group.publisher,
|
|
368
|
+
purpose: group.purpose,
|
|
369
|
+
purpose_en: group.purpose_en,
|
|
370
|
+
category: group.category,
|
|
371
|
+
kind: group.kind,
|
|
372
|
+
risk: worstRisk(group.risks),
|
|
373
|
+
pids: group.pids.sort((a, b) => a - b),
|
|
374
|
+
process_count: group.pids.length,
|
|
375
|
+
mem_bytes: group.mem_bytes,
|
|
376
|
+
cpu_percent: Math.min(100, group.cpu_percent),
|
|
377
|
+
autostart: group.autostart,
|
|
378
|
+
})).sort((a, b) => b.mem_bytes - a.mem_bytes)
|
|
379
|
+
|
|
380
|
+
const groupByKey = new Map(groupList.map(group => [group.key, group]))
|
|
381
|
+
for (const process of decorated) {
|
|
382
|
+
const rootPid = roots.get(process.pid) ?? process.pid
|
|
383
|
+
const root = byPid.get(rootPid) ?? process
|
|
384
|
+
const key = normalizePath(root.path) || `name:${normalizeName(root.name)}`
|
|
385
|
+
process.app_key = key
|
|
386
|
+
process.app_label = groupByKey.get(key)?.label ?? process.label
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const startupSummary = {
|
|
390
|
+
total: entries.length,
|
|
391
|
+
enabled: entries.filter(entry => entry.enabled).length,
|
|
392
|
+
disabled: entries.filter(entry => !entry.enabled).length,
|
|
393
|
+
run: entries.filter(entry => entry.source === 'run').length,
|
|
394
|
+
folder: entries.filter(entry => entry.source === 'folder').length,
|
|
395
|
+
task: entries.filter(entry => entry.source === 'task').length,
|
|
396
|
+
service: entries.filter(entry => entry.source === 'service').length,
|
|
397
|
+
running: entries.filter(entry => entry.enabled && entry.running_pids.length > 0).length,
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const totalMem = decorated.reduce((sum, item) => sum + item.mem_bytes, 0)
|
|
401
|
+
const totalCpu = Math.min(100, decorated.reduce((sum, item) => sum + item.cpu_percent, 0))
|
|
402
|
+
|
|
403
|
+
return {
|
|
404
|
+
collected_at: normalized?.collected_at ?? new Date().toISOString(),
|
|
405
|
+
cores: normalized?.cores ?? 1,
|
|
406
|
+
summary: {
|
|
407
|
+
process_count: decorated.length,
|
|
408
|
+
group_count: groupList.length,
|
|
409
|
+
mem_bytes: totalMem,
|
|
410
|
+
cpu_percent: totalCpu,
|
|
411
|
+
app_count: groupList.filter(group => group.kind === 'app').length,
|
|
412
|
+
system_count: groupList.filter(group => group.kind === 'system').length,
|
|
413
|
+
background_count: groupList.filter(group => group.kind === 'background').length,
|
|
414
|
+
autostart_running: decorated.filter(item => item.autostart.length > 0).length,
|
|
415
|
+
},
|
|
416
|
+
groups: groupList,
|
|
417
|
+
processes: decorated.sort((a, b) => a.app_label.localeCompare(b.app_label, 'zh-Hans') || b.mem_bytes - a.mem_bytes),
|
|
418
|
+
startup: {
|
|
419
|
+
entries,
|
|
420
|
+
summary: startupSummary,
|
|
421
|
+
},
|
|
422
|
+
warnings: [],
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Compact snapshot for an agent tool result. */
|
|
427
|
+
export function compactSnapshot(snapshot, { limit = 40 } = {}) {
|
|
428
|
+
const groups = snapshot.groups.slice(0, limit)
|
|
429
|
+
return {
|
|
430
|
+
collected_at: snapshot.collected_at,
|
|
431
|
+
summary: snapshot.summary,
|
|
432
|
+
groups: groups.map(group => ({
|
|
433
|
+
label: group.label,
|
|
434
|
+
label_en: group.label_en,
|
|
435
|
+
publisher: group.publisher,
|
|
436
|
+
kind: group.kind,
|
|
437
|
+
risk: group.risk,
|
|
438
|
+
process_count: group.process_count,
|
|
439
|
+
mem_bytes: group.mem_bytes,
|
|
440
|
+
cpu_percent: group.cpu_percent,
|
|
441
|
+
autostart: group.autostart,
|
|
442
|
+
purpose: group.purpose,
|
|
443
|
+
pids: group.pids.slice(0, 20),
|
|
444
|
+
})),
|
|
445
|
+
startup_summary: snapshot.startup.summary,
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export function compactStartup(snapshot, { includeSystem = false } = {}) {
|
|
450
|
+
const entries = snapshot.startup.entries
|
|
451
|
+
.filter(entry => includeSystem || (entry.risk !== 'protected' && entry.system !== true))
|
|
452
|
+
.slice(0, 120)
|
|
453
|
+
.map(entry => ({
|
|
454
|
+
source: entry.source,
|
|
455
|
+
scope: entry.scope,
|
|
456
|
+
label: entry.label,
|
|
457
|
+
label_en: entry.label_en,
|
|
458
|
+
name: entry.name,
|
|
459
|
+
command: entry.command,
|
|
460
|
+
exe_path: entry.exe_path,
|
|
461
|
+
publisher: entry.publisher,
|
|
462
|
+
purpose: entry.purpose,
|
|
463
|
+
enabled: entry.enabled,
|
|
464
|
+
admin_required: entry.admin_required,
|
|
465
|
+
risk: entry.risk,
|
|
466
|
+
running_pids: entry.running_pids,
|
|
467
|
+
description: entry.description,
|
|
468
|
+
detail: entry.detail,
|
|
469
|
+
}))
|
|
470
|
+
return {
|
|
471
|
+
collected_at: snapshot.collected_at,
|
|
472
|
+
summary: snapshot.startup.summary,
|
|
473
|
+
entries,
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export function vendorLabel(company, lang = 'zh') {
|
|
478
|
+
const vendor = lookupVendor(company)
|
|
479
|
+
if (vendor === null) return ''
|
|
480
|
+
return lang === 'en' ? vendor.en : vendor.zh
|
|
481
|
+
}
|