dsh-plugin-container-windows 0.0.0-stage → 0.5.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +109 -2
  3. package/README.zh-CN.md +110 -0
  4. package/cordis.patch.yml +3 -0
  5. package/docs/ai-chat.png +0 -0
  6. package/docs/ai-new-session.png +0 -0
  7. package/docs/apple-container.png +0 -0
  8. package/docs/attach.png +0 -0
  9. package/docs/docker-console.png +0 -0
  10. package/docs/docker-overview.png +0 -0
  11. package/docs/docker-volumes.png +0 -0
  12. package/docs/image-architecture.png +0 -0
  13. package/docs/package-install.png +0 -0
  14. package/docs/provider-switch.png +0 -0
  15. package/docs/run-container.png +0 -0
  16. package/index.js +141 -0
  17. package/lib/client.js +4092 -0
  18. package/package.json +72 -4
  19. package/src/client/api.js +98 -0
  20. package/src/client/attach.jsx +134 -0
  21. package/src/client/build-image.jsx +224 -0
  22. package/src/client/components.jsx +325 -0
  23. package/src/client/hooks.js +161 -0
  24. package/src/client/i18n.js +602 -0
  25. package/src/client/icons.jsx +239 -0
  26. package/src/client/index.jsx +90 -0
  27. package/src/client/page.jsx +854 -0
  28. package/src/client/platforms.js +2 -0
  29. package/src/client/push-image.jsx +113 -0
  30. package/src/client/run-container.jsx +187 -0
  31. package/src/client/runs.js +182 -0
  32. package/src/client/store.js +91 -0
  33. package/src/client/styles.js +383 -0
  34. package/src/client/tabs/compose.jsx +94 -0
  35. package/src/client/tabs/console.jsx +211 -0
  36. package/src/client/tabs/containers.jsx +148 -0
  37. package/src/client/tabs/images.jsx +154 -0
  38. package/src/client/tabs/networks.jsx +91 -0
  39. package/src/client/tabs/overview.jsx +266 -0
  40. package/src/client/tabs/volumes.jsx +76 -0
  41. package/src/host/apple-cli.js +319 -0
  42. package/src/host/apple.js +495 -0
  43. package/src/host/attach.js +246 -0
  44. package/src/host/commands.js +241 -0
  45. package/src/host/docker-cli.js +468 -0
  46. package/src/host/images.js +143 -0
  47. package/src/host/jobs.js +245 -0
  48. package/src/host/parsers.js +484 -0
  49. package/src/host/runtime.js +379 -0
  50. package/src/host/service.js +892 -0
  51. package/src/host/tools.js +198 -0
  52. package/src/host/util.js +97 -0
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Long-running Docker/CLI jobs with offset polling, cancellation, timeouts,
3
+ * bounded retention, and a concurrency cap.
4
+ *
5
+ * Jobs are independent of a single RPC call: `run` returns immediately and
6
+ * `poll` reads non-consuming collectors. That makes the terminal resilient to
7
+ * tab switches and navigation while the host command keeps running.
8
+ */
9
+ import { randomUUID } from 'node:crypto'
10
+
11
+ import { DockerCommandError } from './docker-cli.js'
12
+ import { codeOf, messageOf } from './util.js'
13
+
14
+ const MAX_RUNS = 4
15
+ const RETENTION_MS = 10 * 60_000
16
+ const MAX_RECORDS = 30
17
+ const MIN_TIMEOUT_MS = 5_000
18
+ const MAX_TIMEOUT_MS = 30 * 60_000
19
+ const DEFAULT_TIMEOUT_MS = 120_000
20
+
21
+ /** @param {unknown} value @param {number} fallback */
22
+ function clampOffset(value, fallback = 0) {
23
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? Math.trunc(value) : fallback
24
+ }
25
+
26
+ /** @param {unknown} value @param {number} min */
27
+ function clampTimeout(value, min = MIN_TIMEOUT_MS) {
28
+ const requested = typeof value === 'number' && Number.isFinite(value) ? Math.trunc(value) : DEFAULT_TIMEOUT_MS
29
+ return Math.min(Math.max(requested, min), MAX_TIMEOUT_MS)
30
+ }
31
+
32
+ export class JobManager {
33
+ /**
34
+ * @param {{
35
+ * subprocess: any,
36
+ * cli: import('./docker-cli.js').DockerCli,
37
+ * maxOutputBytes?: number,
38
+ * maxRuns?: number,
39
+ * retentionMs?: number,
40
+ * maxRecords?: number,
41
+ * }} options
42
+ */
43
+ constructor(options) {
44
+ this.subprocess = options.subprocess
45
+ this.cli = options.cli
46
+ this.maxOutputBytes = options.maxOutputBytes ?? 1024 * 1024
47
+ this.maxRuns = options.maxRuns ?? MAX_RUNS
48
+ this.retentionMs = options.retentionMs ?? RETENTION_MS
49
+ this.maxRecords = options.maxRecords ?? MAX_RECORDS
50
+ this.minTimeoutMs = options.minTimeoutMs ?? MIN_TIMEOUT_MS
51
+ this.idPrefix = options.idPrefix ?? 'run'
52
+ /** @type {Map<string, any>} */
53
+ this.records = new Map()
54
+ this.sweeper = setInterval(() => this.sweep(), 60_000)
55
+ this.sweeper.unref?.()
56
+ }
57
+
58
+ /** @param {string} runId */
59
+ find(runId) {
60
+ const record = this.records.get(runId)
61
+ if (record === undefined) {
62
+ throw Object.assign(new Error(`Docker 运行记录不存在或已过期: ${runId}`), { code: 'docker/run-not-found' })
63
+ }
64
+ return record
65
+ }
66
+
67
+ /** @param {any} record */
68
+ publicView(record) {
69
+ return {
70
+ id: record.id,
71
+ argv: record.argv,
72
+ displayCommand: record.displayCommand,
73
+ context: record.context,
74
+ cwd: record.cwd,
75
+ title: record.title,
76
+ status: record.status,
77
+ exitCode: record.exitCode,
78
+ signal: record.signal,
79
+ startedAt: record.startedAt,
80
+ endedAt: record.endedAt,
81
+ timedOut: record.timedOut,
82
+ error: record.error,
83
+ }
84
+ }
85
+
86
+ /**
87
+ * @param {{
88
+ * argv: string[],
89
+ * displayCommand: string,
90
+ * context?: string | null,
91
+ * cwd: string,
92
+ * title?: string,
93
+ * timeoutMs?: number,
94
+ * maxOutputBytes?: number,
95
+ * }} input
96
+ */
97
+ start(input) {
98
+ const active = [...this.records.values()].filter((record) => record.status === 'running' || record.status === 'cancelling')
99
+ if (active.length >= this.maxRuns) {
100
+ throw Object.assign(
101
+ new Error(`同时运行的 Docker 命令已达上限(${this.maxRuns} 个)`),
102
+ { code: 'docker/run-limit', details: { active: active.map((record) => record.id) } },
103
+ )
104
+ }
105
+ if (!Array.isArray(input.argv) || input.argv.length === 0) throw new DockerCommandError('命令为空', { code: 'docker/invalid-command' })
106
+ const timeoutMs = clampTimeout(input.timeoutMs, this.minTimeoutMs)
107
+ const requestedMax = Number(input.maxOutputBytes ?? this.maxOutputBytes)
108
+ const maxBytes = Number.isFinite(requestedMax) ? Math.min(Math.max(requestedMax, 64 * 1024), 8 * 1024 * 1024) : 1024 * 1024
109
+ const id = `${this.idPrefix}_${randomUUID()}`
110
+ const record = {
111
+ id,
112
+ argv: [...input.argv],
113
+ displayCommand: input.displayCommand,
114
+ context: input.context ?? null,
115
+ cwd: input.cwd,
116
+ title: input.title ?? '',
117
+ status: 'running',
118
+ exitCode: null,
119
+ signal: null,
120
+ startedAt: Date.now(),
121
+ endedAt: null,
122
+ timedOut: false,
123
+ cancelled: false,
124
+ error: null,
125
+ handle: undefined,
126
+ stdout: undefined,
127
+ stderr: undefined,
128
+ timer: undefined,
129
+ abort: undefined,
130
+ }
131
+
132
+ try {
133
+ record.handle = this.subprocess.spawn({
134
+ argv: record.argv,
135
+ cwd: record.cwd,
136
+ stdio: { stdin: 'ignore', stdout: { maxBytes }, stderr: { maxBytes } },
137
+ graceMs: 2000,
138
+ env: this.cli.childEnv(),
139
+ })
140
+ } catch (error) {
141
+ throw new DockerCommandError(`无法启动命令: ${messageOf(error)}`, {
142
+ code: codeOf(error) === 'ENOENT' ? 'docker/cli-missing' : 'docker/exec-failed',
143
+ })
144
+ }
145
+
146
+ record.stdout = record.handle.collected?.stdout
147
+ record.stderr = record.handle.collected?.stderr
148
+ record.timer = setTimeout(() => {
149
+ if (record.status !== 'running') return
150
+ record.timedOut = true
151
+ try {
152
+ record.handle?.terminate?.()
153
+ } catch {
154
+ // done still settles the record.
155
+ }
156
+ }, timeoutMs)
157
+ record.timer.unref?.()
158
+
159
+ const finish = (outcome) => {
160
+ if (record.status === 'exited' || record.status === 'cancelled' || record.status === 'timed-out' || record.status === 'failed') return
161
+ clearTimeout(record.timer)
162
+ record.endedAt = Date.now()
163
+ record.exitCode = outcome?.exitCode ?? null
164
+ record.signal = outcome?.signal ?? null
165
+ if (record.timedOut) record.status = 'timed-out'
166
+ else if (record.cancelled === true) record.status = 'cancelled'
167
+ else if (record.exitCode === 0) record.status = 'exited'
168
+ else record.status = 'failed'
169
+ this.sweep()
170
+ }
171
+
172
+ record.handle.done.then(finish, (error) => {
173
+ if (record.status !== 'running') return
174
+ clearTimeout(record.timer)
175
+ record.endedAt = Date.now()
176
+ record.error = messageOf(error)
177
+ record.status = 'failed'
178
+ this.sweep()
179
+ })
180
+
181
+ this.records.set(id, record)
182
+ return this.publicView(record)
183
+ }
184
+
185
+ /**
186
+ * @param {{runId: string, stdoutOffset?: number, stderrOffset?: number}} input
187
+ */
188
+ poll(input) {
189
+ const record = this.find(input.runId)
190
+ const read = (reader, offset) => {
191
+ if (reader === undefined) return { text: '', nextOffset: offset, lossy: false }
192
+ const value = reader.readFrom(clampOffset(offset))
193
+ return { text: value.text, nextOffset: value.nextOffset, lossy: value.lossy === true }
194
+ }
195
+ return {
196
+ run: this.publicView(record),
197
+ stdout: read(record.stdout, clampOffset(input.stdoutOffset)),
198
+ stderr: read(record.stderr, clampOffset(input.stderrOffset)),
199
+ }
200
+ }
201
+
202
+ /** @param {string} runId */
203
+ cancel(runId) {
204
+ const record = this.find(runId)
205
+ if (record.status !== 'running' && record.status !== 'cancelling') return this.publicView(record)
206
+ record.cancelled = true
207
+ record.status = 'cancelling'
208
+ try {
209
+ record.handle?.terminate?.()
210
+ } catch (error) {
211
+ record.error = messageOf(error)
212
+ }
213
+ return this.publicView(record)
214
+ }
215
+
216
+ /** Remove terminal records beyond retention or count. */
217
+ sweep() {
218
+ const now = Date.now()
219
+ const terminal = [...this.records.values()]
220
+ .filter((record) => record.status !== 'running' && record.status !== 'cancelling')
221
+ .sort((left, right) => (left.endedAt ?? left.startedAt) - (right.endedAt ?? right.startedAt))
222
+ for (const record of terminal) {
223
+ if (now - (record.endedAt ?? record.startedAt) > this.retentionMs) this.records.delete(record.id)
224
+ }
225
+ const remainingTerminal = [...this.records.values()]
226
+ .filter((record) => record.status !== 'running' && record.status !== 'cancelling')
227
+ .sort((left, right) => (right.endedAt ?? right.startedAt) - (left.endedAt ?? left.startedAt))
228
+ for (const record of remainingTerminal.slice(this.maxRecords)) this.records.delete(record.id)
229
+ }
230
+
231
+ dispose() {
232
+ clearInterval(this.sweeper)
233
+ for (const record of this.records.values()) {
234
+ clearTimeout(record.timer)
235
+ if (record.status === 'running' || record.status === 'cancelling') {
236
+ try {
237
+ record.handle?.terminate?.()
238
+ } catch {
239
+ // Best effort on plugin unload.
240
+ }
241
+ }
242
+ }
243
+ this.records.clear()
244
+ }
245
+ }