dsh-plugin-update-center 0.0.0-stage → 1.0.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.
@@ -0,0 +1,3 @@
1
+ - insert:
2
+ - id: dsh-plugin-update-center
3
+ name: dsh-plugin-update-center
package/icon.svg ADDED
@@ -0,0 +1,6 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="none">
2
+ <rect x="13" y="15" width="28" height="28" rx="8" fill="#F2B45A" transform="rotate(-8 27 29)"/>
3
+ <rect x="8" y="7" width="32" height="32" rx="9" fill="#4176E6"/>
4
+ <path d="M24 31.5V18.5" stroke="#FFFFFF" stroke-width="3.2" stroke-linecap="round"/>
5
+ <path d="M18.2 24.3 24 18.5l5.8 5.8" stroke="#FFFFFF" stroke-width="3.2" stroke-linecap="round" stroke-linejoin="round"/>
6
+ </svg>
package/index.js ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Host half of the plugin update center.
3
+ *
4
+ * It exposes one same-origin POST route so any Web window can read the update
5
+ * snapshot, start a batch, and poll progress. The actual package operations are
6
+ * delegated to the official `pluginManager`; nothing here writes the profile.
7
+ */
8
+
9
+ import { dirname } from 'node:path'
10
+ import { createBackup } from './lib/backup.js'
11
+ import { createEngine } from './lib/engine.js'
12
+ import { createHandler, ROUTE } from './lib/http.js'
13
+ import { createRegistry } from './lib/registry.js'
14
+ import { createResidueService } from './lib/residue.js'
15
+
16
+ export const name = 'dsh-plugin-update-center'
17
+ export const inject = ['connection', 'pluginManager']
18
+
19
+ function readActivity(ctx) {
20
+ try {
21
+ const agents = ctx.get?.('agents')
22
+ const jobs = ctx.get?.('jobs')
23
+ const agentList = typeof agents?.list === 'function' ? agents.list() : []
24
+ const jobList = typeof jobs?.list === 'function' ? jobs.list() : []
25
+ const statesKnown = Array.isArray(agentList)
26
+ && Array.isArray(jobList)
27
+ && agentList.every((agent) => agent?.status === 'idle' || agent?.status === 'running')
28
+ && jobList.every((job) => typeof job?.status === 'string')
29
+ if (!statesKnown) return { known: false, busy: false }
30
+ const runningAgents = agentList.filter((agent) => agent.status === 'running').length
31
+ const runningJobs = jobList.filter((job) => job.status === 'running' || job.status === 'stopping').length
32
+ return { known: true, busy: runningAgents + runningJobs > 0 }
33
+ } catch {
34
+ return { known: false, busy: false }
35
+ }
36
+ }
37
+
38
+ export function apply(ctx) {
39
+ const manager = ctx.pluginManager
40
+ const connection = ctx.connection
41
+ if (
42
+ typeof manager?.listBundles !== 'function'
43
+ || typeof manager?.installBundle !== 'function'
44
+ || typeof manager?.removeBundle !== 'function'
45
+ ) return
46
+ if (typeof connection?.fetch?.register !== 'function') return
47
+
48
+ const profile = ctx.get?.('profileContext')
49
+ const profileDir = typeof profile?.dir === 'string' && profile.dir !== '' ? profile.dir : undefined
50
+ const homeDir = process.env.DSH_HOME ?? (profileDir === undefined ? undefined : dirname(dirname(profileDir)))
51
+ const backup = profileDir !== undefined && homeDir !== undefined
52
+ ? createBackup({ profileDir, homeDir })
53
+ : null
54
+ const residue = profileDir !== undefined
55
+ ? createResidueService({ profileDir, homeDir, manager, backup })
56
+ : null
57
+
58
+ const engine = createEngine({
59
+ manager,
60
+ registry: createRegistry(),
61
+ profileDir,
62
+ backup,
63
+ runtimeVersion: (bundles) => {
64
+ if (!Array.isArray(bundles)) return undefined
65
+ const base = bundles.find((bundle) => bundle?.name === '@deepseek-ai/dsh-base' && typeof bundle.version === 'string')
66
+ return base?.version
67
+ },
68
+ activity: async () => readActivity(ctx),
69
+ logger: ctx.logger,
70
+ })
71
+
72
+ ctx.effect(
73
+ () => connection.fetch.register({
74
+ path: ROUTE,
75
+ methods: ['POST'],
76
+ requestBody: 'streaming',
77
+ fetch: createHandler(engine, residue),
78
+ }),
79
+ 'dsh-plugin-update-center: http route',
80
+ )
81
+ ctx.effect(() => () => { void engine.dispose() }, 'dsh-plugin-update-center: engine')
82
+ }
package/lib/backup.js ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Profile snapshots and an update history kept outside the profile.
3
+ *
4
+ * A snapshot is the only rollback point that survives a hard crash or a DSH
5
+ * that no longer boots, so the engine refuses to update when it cannot create
6
+ * one. `repair.mjs` restores the same directory from a terminal.
7
+ */
8
+
9
+ import { existsSync } from 'node:fs'
10
+ import { cp, mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises'
11
+ import { join } from 'node:path'
12
+
13
+ export const SNAPSHOT_FILES = Object.freeze([
14
+ 'package.json',
15
+ 'pnpm-lock.yaml',
16
+ 'pnpm-workspace.yaml',
17
+ 'cordis.patch.yml',
18
+ 'cordis.yml',
19
+ ])
20
+
21
+ function stampOf(ms) {
22
+ return new Date(ms).toISOString().replace(/[:.]/g, '-')
23
+ }
24
+
25
+ /**
26
+ * @param {object} options
27
+ * @param {string} options.profileDir absolute profile directory
28
+ * @param {string} options.homeDir absolute DSH home directory
29
+ * @param {() => number} [options.clock]
30
+ * @param {number} [options.keep] retained snapshots
31
+ */
32
+ export function createBackup({ profileDir, homeDir, clock = () => Date.now(), keep = 10 } = {}) {
33
+ if (typeof profileDir !== 'string' || profileDir === '') throw new TypeError('profileDir is required')
34
+ if (typeof homeDir !== 'string' || homeDir === '') throw new TypeError('homeDir is required')
35
+ const root = join(homeDir, 'plugin-updates')
36
+ const snapshots = join(root, 'snapshots')
37
+
38
+ /**
39
+ * Copy the profile's authoritative files into a new timestamped snapshot.
40
+ * @param {Record<string, unknown>} meta extra facts stored beside the files
41
+ * @returns {Promise<{ dir: string, files: string[] }>}
42
+ */
43
+ async function create(meta = {}) {
44
+ const dir = join(snapshots, stampOf(clock()))
45
+ await mkdir(dir, { recursive: true })
46
+ const copied = []
47
+ for (const name of SNAPSHOT_FILES) {
48
+ const source = join(profileDir, name)
49
+ if (!existsSync(source)) continue
50
+ await cp(source, join(dir, name), { force: true })
51
+ copied.push(name)
52
+ }
53
+ const manifest = JSON.parse(await readFile(join(profileDir, 'package.json'), 'utf8'))
54
+ await writeFile(join(dir, 'meta.json'), JSON.stringify({
55
+ schema: 1,
56
+ createdAt: clock(),
57
+ profileDir,
58
+ files: copied,
59
+ dependencies: manifest?.dependencies ?? {},
60
+ bundles: manifest?.dsh?.profile?.bundles ?? [],
61
+ ...meta,
62
+ }, null, 2) + '\n')
63
+ await prune()
64
+ return { dir, files: copied }
65
+ }
66
+
67
+ async function prune() {
68
+ const entries = (await readdir(snapshots, { withFileTypes: true }).catch(() => []))
69
+ .filter((entry) => entry.isDirectory())
70
+ .map((entry) => entry.name)
71
+ .sort()
72
+ for (const old of entries.slice(0, Math.max(0, entries.length - keep))) {
73
+ await rm(join(snapshots, old), { recursive: true, force: true }).catch(() => {})
74
+ }
75
+ }
76
+
77
+ /** Append one human-readable line per plugin attempt. */
78
+ async function history(entry) {
79
+ try {
80
+ await mkdir(root, { recursive: true })
81
+ await writeFile(join(root, 'history.jsonl'), JSON.stringify({ at: clock(), ...entry }) + '\n', { flag: 'a' })
82
+ } catch {
83
+ // History is diagnostic only; never fail an update over it.
84
+ }
85
+ }
86
+
87
+ return { create, history, root, snapshots }
88
+ }