@banzhe/dsh-devcontainer 0.0.0-stage → 0.2.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/README.md +79 -2
- package/README.zh-CN.md +80 -0
- package/cordis.patch.yml +71 -0
- package/lib/client.js +789 -0
- package/lib/client.js.map +1 -0
- package/lib/exec-CKAAz2zz.js +241 -0
- package/lib/exec-CKAAz2zz.js.map +1 -0
- package/lib/exec-_8g5fdSx.d.ts +26 -0
- package/lib/exec-_8g5fdSx.d.ts.map +1 -0
- package/lib/fs.d.ts +530 -0
- package/lib/fs.d.ts.map +1 -0
- package/lib/fs.js +1737 -0
- package/lib/fs.js.map +1 -0
- package/lib/helper/main.mjs +1008 -0
- package/lib/index.d.ts +88 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +927 -0
- package/lib/index.js.map +1 -0
- package/lib/sbx-1GsFjQBk.js +1489 -0
- package/lib/sbx-1GsFjQBk.js.map +1 -0
- package/lib/shell.d.ts +175 -0
- package/lib/shell.d.ts.map +1 -0
- package/lib/shell.js +454 -0
- package/lib/shell.js.map +1 -0
- package/lib/types-DsF9Hz5P.d.ts +33 -0
- package/lib/types-DsF9Hz5P.d.ts.map +1 -0
- package/package.json +116 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,927 @@
|
|
|
1
|
+
import { A as variantNameSuffixOf, C as presetKindOf, D as VARIANT_KINDS, H as containerOptions, M as hostPathStyle, O as variantIdOf, R as toContainerPath, U as presetSourceFields, V as containerOptionFields, _ as containerPresetOf, a as sandboxNameFor, c as sbxWorkspaceRootFor, d as DEVCONTAINER_ENV_KEY, f as DEVCONTAINER_PRESET_ENV_KEY, g as callingVariantKind, k as variantKindOf, l as repoRegistry, m as SBX_PRESET_ENV_KEY, p as SBX_ENV_KEY, s as sbxSpecFor, t as asSbxSpec, z as toHostPath } from "./sbx-1GsFjQBk.js";
|
|
2
|
+
import z from "@deepseek-ai/schemastery";
|
|
3
|
+
import { posix, win32 } from "node:path";
|
|
4
|
+
//#region src/prompt.ts
|
|
5
|
+
/** Name of the context `WorkingDirectoryService` contributes. */
|
|
6
|
+
const WORKING_DIRECTORY_CONTEXT = "working-directory:current";
|
|
7
|
+
/**
|
|
8
|
+
* The label the prompt names the container by: the explicit container, its
|
|
9
|
+
* compose service, or the repository directory name.
|
|
10
|
+
* @param spec - the repository container specification.
|
|
11
|
+
* @returns a short human label.
|
|
12
|
+
*/
|
|
13
|
+
function containerLabel(spec) {
|
|
14
|
+
if (spec.container !== void 0) return spec.container;
|
|
15
|
+
if (spec.composeService !== void 0) return `service ${spec.composeService}`;
|
|
16
|
+
const base = hostPathStyle(spec.hostWorkspaceRoot) === "win32" ? win32.basename(spec.hostWorkspaceRoot) : posix.basename(spec.hostWorkspaceRoot);
|
|
17
|
+
return base === "" ? spec.hostWorkspaceRoot : base;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The replacement text for `working-directory:current`.
|
|
21
|
+
* @param containerWorkdir - POSIX directory the command runs in.
|
|
22
|
+
* @param spec - the repository container specification.
|
|
23
|
+
* @param hostWorkdir - the host directory that mount corresponds to.
|
|
24
|
+
* @returns the assembled context text.
|
|
25
|
+
*/
|
|
26
|
+
function containerContextText(containerWorkdir, spec, hostWorkdir) {
|
|
27
|
+
return `Current working directory: "${containerWorkdir}".\nRuntime environment: Linux Docker container (${containerLabel(spec)}) mounted from host directory "${hostWorkdir}". The active shell is Linux bash (/bin/bash). Always write POSIX/Linux commands and never PowerShell commands.`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The replacement text for `working-directory:current` under a `(sbx)` preset.
|
|
31
|
+
*
|
|
32
|
+
* A sandbox needs no declaration, so this text never explains a refusal: it names
|
|
33
|
+
* the sandbox and states the one fact the model cannot infer — that the paths are
|
|
34
|
+
* the host's own, because `sbx` mounts a workspace at the same absolute path.
|
|
35
|
+
* @param containerWorkdir - the directory the command runs in.
|
|
36
|
+
* @param sandboxName - the Docker Sandbox it runs in.
|
|
37
|
+
* @param hostWorkdir - the host directory that sandbox mounts.
|
|
38
|
+
* @returns the assembled context text.
|
|
39
|
+
*/
|
|
40
|
+
function sbxContextText(containerWorkdir, sandboxName, hostWorkdir) {
|
|
41
|
+
return `Current working directory: "${containerWorkdir}".\nRuntime environment: Linux Docker Sandbox "${sandboxName}" (sbx) mounting host directory "${hostWorkdir}" at the same absolute path. The active shell is Linux bash (/bin/bash). Always write POSIX/Linux commands and never PowerShell commands.`;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The replacement text for `working-directory:current` when a `(devContainer)`
|
|
45
|
+
* preset meets a repository that declares no container.
|
|
46
|
+
*
|
|
47
|
+
* The instruction is explicit about the two things the model must not do: retry
|
|
48
|
+
* the command or the file access, and describe the failure as if it were temporary. It also says
|
|
49
|
+
* which language to report in, because the person on the other side chose a
|
|
50
|
+
* Chinese-labelled preset.
|
|
51
|
+
* @param hostWorkdir - the host directory the session is working in.
|
|
52
|
+
* @param preset - the derived preset id the session runs.
|
|
53
|
+
* @returns the assembled context text.
|
|
54
|
+
*/
|
|
55
|
+
function missingContainerContextText(hostWorkdir, preset) {
|
|
56
|
+
return `Current working directory: "${hostWorkdir}".\nRuntime environment: this session runs the (devContainer) preset "${preset}", but the working directory "${hostWorkdir}" declares no Dev Container configuration.\nLooked for .devcontainer/devcontainer.json, .devcontainer.json, .dsh/devcontainer.json, and .dsh/docker.json in that directory and every ancestor directory.
|
|
57
|
+
Every shell command and every file operation is BLOCKED: the shell tool refuses to run commands on the host, the file tools refuse to read or write host files, so nothing has executed and nothing will execute until this repository gets a container configuration.
|
|
58
|
+
Tell the user this immediately, in Chinese: the (devContainer) preset requires a container configuration for this repository, and there are two ways out — add one of those files (for example .devcontainer/devcontainer.json, or .dsh/docker.json pointing at a container that is already running), or switch back to an ordinary preset (标准模式 / 委派模式). Do not retry shell commands or file operations while this stands, and do not describe it as a temporary failure.`;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Replace (or add) the working-directory context of one assembly.
|
|
62
|
+
* @param assembly - the mutable assembly the waterfall produced.
|
|
63
|
+
* @param text - the container context text.
|
|
64
|
+
* @returns true when the assembly was changed.
|
|
65
|
+
*/
|
|
66
|
+
function applyContainerContext(assembly, text) {
|
|
67
|
+
const contribution = assembly.contexts.find((entry) => entry.name === WORKING_DIRECTORY_CONTEXT);
|
|
68
|
+
if (contribution === void 0) {
|
|
69
|
+
assembly.contexts.unshift({
|
|
70
|
+
name: WORKING_DIRECTORY_CONTEXT,
|
|
71
|
+
text,
|
|
72
|
+
interpolate: false
|
|
73
|
+
});
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
contribution.text = text;
|
|
77
|
+
contribution.interpolate = false;
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Read the host directory back out of an assembly that carries no agent.
|
|
82
|
+
* @param assembly - the assembly to read.
|
|
83
|
+
* @returns the host directory the `working-directory:current` context names, or undefined.
|
|
84
|
+
*/
|
|
85
|
+
function hostDirectoryFromContext(assembly) {
|
|
86
|
+
const contribution = assembly.contexts.find((entry) => entry.name === WORKING_DIRECTORY_CONTEXT);
|
|
87
|
+
if (contribution === void 0) return void 0;
|
|
88
|
+
const match = /Current working directory: (".*?")\./.exec(contribution.text);
|
|
89
|
+
if (match === null) return void 0;
|
|
90
|
+
try {
|
|
91
|
+
const parsed = JSON.parse(match[1]);
|
|
92
|
+
return typeof parsed === "string" ? parsed : void 0;
|
|
93
|
+
} catch {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
//#region src/presets.ts
|
|
99
|
+
/**
|
|
100
|
+
* Preset-variant transforms.
|
|
101
|
+
*
|
|
102
|
+
* A preset is a declarative plugin list, and the shipped presets pick their
|
|
103
|
+
* shell tool with `disabled: !!js process.platform === 'win32'`: on Windows every
|
|
104
|
+
* agent gets `pwsh` and no `bash`. Inside a Linux container that is backwards,
|
|
105
|
+
* but a preset is config DATA, not a Loader row — a patch entry cannot reach
|
|
106
|
+
* into `config.plugins`, and the registry has no mutation API. So this Bundle
|
|
107
|
+
* reads a preset's declaration out of the Loader tree and registers a derived
|
|
108
|
+
* one: same roster, shell tools flipped, subagent routes pointing at the derived
|
|
109
|
+
* subagent presets.
|
|
110
|
+
*
|
|
111
|
+
* Every function here is pure, so the whole derivation is unit-tested without a
|
|
112
|
+
* Cordis composition.
|
|
113
|
+
* @module @banzhe/dsh-devcontainer/presets
|
|
114
|
+
*/
|
|
115
|
+
/**
|
|
116
|
+
* Every row that gives an agent a POSIX shell. The shipped presets pick between
|
|
117
|
+
* these and their PowerShell twins with `disabled: !!js process.platform === 'win32'`,
|
|
118
|
+
* and they do not always use the plain tool: `minimal` mounts
|
|
119
|
+
* `tool-bash-persistent` / `tool-pwsh-persistent` plus `terminal-bash`, so a
|
|
120
|
+
* variant that only flipped the plain pair would still come up with PowerShell.
|
|
121
|
+
*/
|
|
122
|
+
const BASH_TOOL_MODULES = [
|
|
123
|
+
"@deepseek-ai/dsh-tool-bash",
|
|
124
|
+
"@deepseek-ai/dsh-tool-bash-persistent",
|
|
125
|
+
"@deepseek-ai/dsh-terminal-bash"
|
|
126
|
+
];
|
|
127
|
+
/** Every row that gives an agent a PowerShell shell. */
|
|
128
|
+
const PWSH_TOOL_MODULES = ["@deepseek-ai/dsh-tool-pwsh", "@deepseek-ai/dsh-tool-pwsh-persistent"];
|
|
129
|
+
/**
|
|
130
|
+
* Rows whose tool spawns and OWNS its shell process (a PTY, or a persistent
|
|
131
|
+
* session) instead of asking `ctx.shell` for one.
|
|
132
|
+
*
|
|
133
|
+
* They cannot be part of a container variant: the process would start on the
|
|
134
|
+
* host, which is exactly what this Bundle exists to avoid, and on Windows the
|
|
135
|
+
* terminal backend collides with the one the platform already registered
|
|
136
|
+
* ("a PTY backend named \"shell\" is already registered" — the derived preset
|
|
137
|
+
* then mounts as `broken` and disappears from the picker).
|
|
138
|
+
*/
|
|
139
|
+
const PERSISTENT_SHELL_MODULES = ["@deepseek-ai/dsh-tool-bash-persistent", "@deepseek-ai/dsh-terminal-bash"];
|
|
140
|
+
/** The plain bash tool row, kept as the historical single-name export. */
|
|
141
|
+
const BASH_TOOL_MODULE = BASH_TOOL_MODULES[0];
|
|
142
|
+
PWSH_TOOL_MODULES[0];
|
|
143
|
+
/**
|
|
144
|
+
* The derived id for one source preset on one track.
|
|
145
|
+
* @param sourceId - the declared preset id.
|
|
146
|
+
* @param kind - the track; `(devContainer)` when omitted.
|
|
147
|
+
* @returns the variant id, or the id itself when it already names a variant.
|
|
148
|
+
*/
|
|
149
|
+
function variantId(sourceId, kind = "devcontainer") {
|
|
150
|
+
return variantKindOf(sourceId) === void 0 ? variantIdOf(sourceId, kind) : sourceId;
|
|
151
|
+
}
|
|
152
|
+
function isRow(value) {
|
|
153
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.name === "string";
|
|
154
|
+
}
|
|
155
|
+
function childRows(row) {
|
|
156
|
+
return row.group === true && Array.isArray(row.config) ? row.config.filter(isRow) : void 0;
|
|
157
|
+
}
|
|
158
|
+
/** Deep-clone preset data (plain objects, arrays, and `!!js` marker objects). */
|
|
159
|
+
function cloneRows(rows) {
|
|
160
|
+
return rows.map((row) => {
|
|
161
|
+
const cloned = {};
|
|
162
|
+
for (const [key, value] of Object.entries(row)) cloned[key] = cloneValue(value);
|
|
163
|
+
return cloned;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
function cloneValue(value) {
|
|
167
|
+
if (Array.isArray(value)) return value.map(cloneValue);
|
|
168
|
+
if (typeof value === "object" && value !== null) {
|
|
169
|
+
const cloned = {};
|
|
170
|
+
for (const [key, entry] of Object.entries(value)) cloned[key] = cloneValue(entry);
|
|
171
|
+
return cloned;
|
|
172
|
+
}
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Whether one row asks for a PowerShell shell.
|
|
177
|
+
*
|
|
178
|
+
* The module alone does not decide: `terminal-bash` carries BOTH dialects, and a
|
|
179
|
+
* `shellDialect: pwsh` row left enabled is what collides with the bash backend.
|
|
180
|
+
* @param row - the declared row.
|
|
181
|
+
* @returns true for a PowerShell row in either form.
|
|
182
|
+
*/
|
|
183
|
+
function isPowerShellRow(row) {
|
|
184
|
+
if (PWSH_TOOL_MODULES.includes(row.name)) return true;
|
|
185
|
+
return typeof row.config === "object" && row.config !== null && row.config.shellDialect === "pwsh";
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Flip every shell tool row at any nesting depth, so the variant speaks bash
|
|
189
|
+
* inside the container and nothing else.
|
|
190
|
+
*
|
|
191
|
+
* - a PowerShell row (by module or by `shellDialect`) is disabled;
|
|
192
|
+
* - a persistent/terminal bash row cannot keep its module: it owns a host-side
|
|
193
|
+
* shell process, so it is rewritten into the ordinary `tool-bash` row, which
|
|
194
|
+
* goes through `ctx.shell` and therefore into the container;
|
|
195
|
+
* - only ONE such row is rewritten, because a second would mount a duplicate
|
|
196
|
+
* `bash` tool; the rest are disabled;
|
|
197
|
+
* - a row that already is `tool-bash`, or has no `disabled` field at all, gains
|
|
198
|
+
* an explicit one, so a preset can no longer inherit the platform guess.
|
|
199
|
+
* @param rows - the declared plugin list.
|
|
200
|
+
* @returns a new list; the input is never mutated.
|
|
201
|
+
*/
|
|
202
|
+
function rewriteShellTools(rows) {
|
|
203
|
+
const state = {
|
|
204
|
+
plain: false,
|
|
205
|
+
replaced: false
|
|
206
|
+
};
|
|
207
|
+
const scan = (list) => {
|
|
208
|
+
for (const row of list) {
|
|
209
|
+
if (row.name === BASH_TOOL_MODULE) state.plain = true;
|
|
210
|
+
const children = childRows(row);
|
|
211
|
+
if (children !== void 0) scan(children);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
scan(rows);
|
|
215
|
+
const rewrite = (list) => list.map((row) => {
|
|
216
|
+
const next = { ...row };
|
|
217
|
+
if (isPowerShellRow(row)) next.disabled = true;
|
|
218
|
+
else if (row.name === BASH_TOOL_MODULE) next.disabled = false;
|
|
219
|
+
else if (PERSISTENT_SHELL_MODULES.includes(row.name)) {
|
|
220
|
+
if (state.plain || state.replaced) next.disabled = true;
|
|
221
|
+
else {
|
|
222
|
+
state.replaced = true;
|
|
223
|
+
next.name = BASH_TOOL_MODULE;
|
|
224
|
+
next.disabled = false;
|
|
225
|
+
delete next.config;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const children = childRows(row);
|
|
229
|
+
if (children !== void 0) next.config = rewrite(children);
|
|
230
|
+
return next;
|
|
231
|
+
});
|
|
232
|
+
return rewrite(rows);
|
|
233
|
+
}
|
|
234
|
+
/** The subagent presets one row's config names, in declaration order. */
|
|
235
|
+
function presetRefsOf(config) {
|
|
236
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) return [];
|
|
237
|
+
const { agentPreset, allowedAgentPresets } = config;
|
|
238
|
+
const found = [];
|
|
239
|
+
if (typeof agentPreset === "string" && agentPreset !== "") found.push(agentPreset);
|
|
240
|
+
if (Array.isArray(allowedAgentPresets)) {
|
|
241
|
+
for (const entry of allowedAgentPresets) if (typeof entry === "string" && entry !== "") found.push(entry);
|
|
242
|
+
}
|
|
243
|
+
return found;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Every preset id a plugin list routes a subagent to.
|
|
247
|
+
*
|
|
248
|
+
* A subagent row names its preset twice over: `agentPreset` fixes it, and
|
|
249
|
+
* `allowedAgentPresets` (the array form `@deepseek-ai/dsh-tool-subagent` accepts)
|
|
250
|
+
* lists the choices a caller may make. Both have to be derived, or a route can
|
|
251
|
+
* point at a preset that has no container variant.
|
|
252
|
+
* @param rows - the declared plugin list.
|
|
253
|
+
* @returns the referenced preset ids, in first-seen order, without duplicates.
|
|
254
|
+
*/
|
|
255
|
+
function referencedPresets(rows) {
|
|
256
|
+
const found = [];
|
|
257
|
+
const collect = (list) => {
|
|
258
|
+
for (const row of list) {
|
|
259
|
+
for (const id of presetRefsOf(row.config)) if (!found.includes(id)) found.push(id);
|
|
260
|
+
const children = childRows(row);
|
|
261
|
+
if (children !== void 0) collect(children);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
collect(rows);
|
|
265
|
+
return found;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Point every subagent route at its derived preset, and cut the routes that
|
|
269
|
+
* have none.
|
|
270
|
+
*
|
|
271
|
+
* A route whose preset has no container variant must not survive as it is: the
|
|
272
|
+
* subagent would start on the host with an ordinary preset while the calling
|
|
273
|
+
* agent keeps believing every command runs in the container. Such a route is
|
|
274
|
+
* removed instead — an `allowedAgentPresets` choice is dropped, and a single
|
|
275
|
+
* fixed `agentPreset` disables its whole row, so the tool is absent rather than
|
|
276
|
+
* silently host-bound. The blocked id stays in the disabled row's config, which
|
|
277
|
+
* is where a reader looking for the cause will look.
|
|
278
|
+
* @param rows - the declared plugin list.
|
|
279
|
+
* @param map - source preset id → derived id, or undefined to leave the route alone.
|
|
280
|
+
* @param blocked - whether one preset id has no variant and its routes must be cut.
|
|
281
|
+
* @returns a new list; the input is never mutated.
|
|
282
|
+
*/
|
|
283
|
+
function rewriteAgentPresetRefs(rows, map, blocked = () => false) {
|
|
284
|
+
const remap = (id) => {
|
|
285
|
+
const mapped = map(id);
|
|
286
|
+
return mapped === void 0 ? id : mapped;
|
|
287
|
+
};
|
|
288
|
+
return rows.map((row) => {
|
|
289
|
+
const next = { ...row };
|
|
290
|
+
const config = row.config;
|
|
291
|
+
let cut = false;
|
|
292
|
+
if (typeof config === "object" && config !== null && !Array.isArray(config)) {
|
|
293
|
+
const record = config;
|
|
294
|
+
const rewritten = { ...record };
|
|
295
|
+
let changed = false;
|
|
296
|
+
if (typeof record.agentPreset === "string") {
|
|
297
|
+
if (blocked(record.agentPreset)) cut = true;
|
|
298
|
+
else {
|
|
299
|
+
const mapped = remap(record.agentPreset);
|
|
300
|
+
if (mapped !== record.agentPreset) {
|
|
301
|
+
rewritten.agentPreset = mapped;
|
|
302
|
+
changed = true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (Array.isArray(record.allowedAgentPresets)) {
|
|
307
|
+
const ids = record.allowedAgentPresets.filter((entry) => typeof entry === "string");
|
|
308
|
+
const kept = ids.filter((id) => !blocked(id));
|
|
309
|
+
const mapped = kept.map(remap);
|
|
310
|
+
if (kept.length !== ids.length || mapped.some((id, index) => id !== kept[index])) {
|
|
311
|
+
rewritten.allowedAgentPresets = mapped;
|
|
312
|
+
changed = true;
|
|
313
|
+
}
|
|
314
|
+
if (kept.length === 0) cut = true;
|
|
315
|
+
}
|
|
316
|
+
if (changed) next.config = rewritten;
|
|
317
|
+
}
|
|
318
|
+
if (cut) next.disabled = true;
|
|
319
|
+
const children = childRows(row);
|
|
320
|
+
if (children !== void 0) next.config = rewriteAgentPresetRefs(children, map, blocked);
|
|
321
|
+
return next;
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Read one preset declaration out of a Loader row's raw config.
|
|
326
|
+
* @param config - the row's `config` value.
|
|
327
|
+
* @returns the declaration, or undefined when the value is not one.
|
|
328
|
+
*/
|
|
329
|
+
function asDeclaration(config) {
|
|
330
|
+
if (typeof config !== "object" || config === null || Array.isArray(config)) return void 0;
|
|
331
|
+
const record = config;
|
|
332
|
+
if (typeof record.id !== "string" || record.id === "" || !Array.isArray(record.plugins)) return void 0;
|
|
333
|
+
const plugins = record.plugins.filter(isRow);
|
|
334
|
+
if (plugins.length === 0) return void 0;
|
|
335
|
+
return {
|
|
336
|
+
id: record.id,
|
|
337
|
+
...typeof record.name === "string" ? { name: record.name } : {},
|
|
338
|
+
...typeof record.description === "string" ? { description: record.description } : {},
|
|
339
|
+
...typeof record.order === "number" ? { order: record.order } : {},
|
|
340
|
+
plugins
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
const DEFAULT_VARIANT_DEPTH = 4;
|
|
344
|
+
/**
|
|
345
|
+
* Build the description of one derived preset.
|
|
346
|
+
* @param source - the source declaration.
|
|
347
|
+
* @param kind - the track the variant belongs to.
|
|
348
|
+
* @returns the source description with one added sentence.
|
|
349
|
+
*/
|
|
350
|
+
function variantDescription(source, kind = "devcontainer") {
|
|
351
|
+
const note = kind === "sbx" ? "Every command runs inside the Docker Sandbox mounted on this workspace; the shell tool is bash, and file paths are the same absolute Linux paths they have on the host." : "Every command runs inside the repository's Linux Dev Container when it declares one; the shell tool is bash and file paths accept the container form /workspaces/<repo>/... .";
|
|
352
|
+
return source.description === void 0 ? note : `${source.description} ${note}`;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* The sentence a variant's description gains when the depth cap cut one of its routes.
|
|
356
|
+
* @param maxDepth - the route-following depth this plan was built with.
|
|
357
|
+
* @returns the explanation appended to the source description.
|
|
358
|
+
*/
|
|
359
|
+
function depthBlockNote(maxDepth) {
|
|
360
|
+
return `Subagent routes deeper than ${maxDepth} nested levels are disabled: their presets would run on the host instead of inside the container.`;
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Plan the derived preset set for one composition.
|
|
364
|
+
*
|
|
365
|
+
* The whole point is what it does NOT change: a source preset that mounts no
|
|
366
|
+
* shell tool (the delegate main agent) derives to a preset that still mounts
|
|
367
|
+
* none — only the subagent presets it routes to get bash. A source that does
|
|
368
|
+
* mount the pair gets it flipped. Nothing is inserted, so no variant can gain a
|
|
369
|
+
* capability its source never had.
|
|
370
|
+
*
|
|
371
|
+
* Routes are followed breadth-first and depth-bounded. A route to a preset this
|
|
372
|
+
* composition never declares stays untouched rather than dangling. A route the
|
|
373
|
+
* DEPTH CAP stops is cut instead of kept: the preset behind it would otherwise
|
|
374
|
+
* start an agent on the host under an ordinary preset, which is exactly the
|
|
375
|
+
* silent downgrade the derived presets exist to prevent. Route depth also bounds
|
|
376
|
+
* recursion, so a cyclic route graph terminates here.
|
|
377
|
+
*
|
|
378
|
+
* Two tracks are planned side by side: `sources` derives the
|
|
379
|
+
* `… (devContainer)` set, `sbxSources` the `… (sbx)` set. A track only ever
|
|
380
|
+
* routes into itself — a sandbox subagent under a Dev Container root would run
|
|
381
|
+
* commands in a different runtime than the agent that delegated to it — so the
|
|
382
|
+
* same source can appear on both lists and derives two independent trees.
|
|
383
|
+
*
|
|
384
|
+
* Referenced variants are emitted BEFORE the preset that routes to them.
|
|
385
|
+
*
|
|
386
|
+
* @param input - declarations, both source lists, and the labelling offsets.
|
|
387
|
+
* @returns the variants to register, in dependency order, Dev Container track first.
|
|
388
|
+
*/
|
|
389
|
+
function planVariants(input) {
|
|
390
|
+
const maxDepth = input.maxDepth ?? DEFAULT_VARIANT_DEPTH;
|
|
391
|
+
const planned = /* @__PURE__ */ new Map();
|
|
392
|
+
const keyOf = (kind, sourceId) => `${kind}\u0000${sourceId}`;
|
|
393
|
+
const visit = (kind, sourceId, depth) => {
|
|
394
|
+
const key = keyOf(kind, sourceId);
|
|
395
|
+
if (planned.has(key)) return;
|
|
396
|
+
const source = input.declarations.get(sourceId);
|
|
397
|
+
if (source === void 0) return;
|
|
398
|
+
const id = variantId(sourceId, kind);
|
|
399
|
+
const flipped = rewriteShellTools(cloneRows(source.plugins));
|
|
400
|
+
const routes = /* @__PURE__ */ new Map();
|
|
401
|
+
const blocked = /* @__PURE__ */ new Set();
|
|
402
|
+
for (const referenced of referencedPresets(flipped)) {
|
|
403
|
+
if (referenced === sourceId) routes.set(referenced, id);
|
|
404
|
+
else if (depth < maxDepth) {
|
|
405
|
+
visit(kind, referenced, depth + 1);
|
|
406
|
+
const derived = planned.get(keyOf(kind, referenced));
|
|
407
|
+
if (derived !== void 0) routes.set(referenced, derived.id);
|
|
408
|
+
} else if (planned.has(keyOf(kind, referenced))) routes.set(referenced, variantId(referenced, kind));
|
|
409
|
+
if (!routes.has(referenced) && input.declarations.has(referenced)) blocked.add(referenced);
|
|
410
|
+
}
|
|
411
|
+
const description = variantDescription(source, kind);
|
|
412
|
+
planned.set(key, {
|
|
413
|
+
id,
|
|
414
|
+
source: sourceId,
|
|
415
|
+
kind,
|
|
416
|
+
name: `${source.name ?? source.id}${variantNameSuffixOf(kind)}`,
|
|
417
|
+
description: blocked.size === 0 ? description : `${description} ${depthBlockNote(maxDepth)}`,
|
|
418
|
+
order: (source.order ?? 0) + input.orderOffset,
|
|
419
|
+
plugins: rewriteAgentPresetRefs(flipped, (referenced) => routes.get(referenced), (referenced) => blocked.has(referenced))
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
for (const kind of VARIANT_KINDS) {
|
|
423
|
+
const sources = kind === "sbx" ? input.sbxSources ?? [] : input.sources;
|
|
424
|
+
for (const sourceId of sources) visit(kind, sourceId, 0);
|
|
425
|
+
}
|
|
426
|
+
return [...planned.values()];
|
|
427
|
+
}
|
|
428
|
+
//#endregion
|
|
429
|
+
//#region src/ptc.ts
|
|
430
|
+
/**
|
|
431
|
+
* The directory the host process may actually start in.
|
|
432
|
+
* @param cwd - the directory the caller, or the provider's own default, named.
|
|
433
|
+
* @param repository - the container repository that owns it, when one does.
|
|
434
|
+
* @returns the host path of the same directory, or `cwd` unchanged.
|
|
435
|
+
*/
|
|
436
|
+
function mapPtcCwd(cwd, repository) {
|
|
437
|
+
return repository === void 0 ? cwd : toHostPath(cwd, repository);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Make every program run in a directory that exists on this host.
|
|
441
|
+
*
|
|
442
|
+
* The seam is decorated in place rather than replaced: Cordis holds one
|
|
443
|
+
* implementation per service name, so a second `ptcRuntime` row would be refused
|
|
444
|
+
* at activation, while every run that needs no rewrite — an ordinary session's
|
|
445
|
+
* program, a program in a host repository — must keep the provider's own process
|
|
446
|
+
* mechanics. Both inherited methods are called with their own receiver, and a
|
|
447
|
+
* request or spec object is passed through untouched when nothing is mapped.
|
|
448
|
+
*
|
|
449
|
+
* `resolve` is decorated as well as `run`, not instead of it: a caller that keeps
|
|
450
|
+
* the resolved spec (a consumer that reports the directory, or a persistent
|
|
451
|
+
* runtime that compares policies) must not be handed a container path that the
|
|
452
|
+
* provider then chdirs into. A request that names no directory is resolved by the
|
|
453
|
+
* provider first and mapped afterwards, because the provider fills it from the
|
|
454
|
+
* sandbox policy's workspace root, which is the container spelling this module
|
|
455
|
+
* exists to replace.
|
|
456
|
+
* @param seam - the mounted PTC runtime.
|
|
457
|
+
* @param repositoryOf - the repository that owns one directory, when one does.
|
|
458
|
+
* @returns the disposer that puts the inherited methods back.
|
|
459
|
+
*/
|
|
460
|
+
function installPtcCwdMapping(seam, repositoryOf) {
|
|
461
|
+
const inheritedResolve = seam.resolve.bind(seam);
|
|
462
|
+
const inheritedRun = seam.run.bind(seam);
|
|
463
|
+
const mappedResolve = (request) => {
|
|
464
|
+
const requested = request.cwd;
|
|
465
|
+
if (requested !== void 0) {
|
|
466
|
+
const cwd = mapPtcCwd(requested, repositoryOf(requested));
|
|
467
|
+
return inheritedResolve(cwd === requested ? request : {
|
|
468
|
+
...request,
|
|
469
|
+
cwd
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const resolved = inheritedResolve(request);
|
|
473
|
+
const cwd = mapPtcCwd(resolved.cwd, repositoryOf(resolved.cwd));
|
|
474
|
+
return cwd === resolved.cwd ? resolved : {
|
|
475
|
+
...resolved,
|
|
476
|
+
cwd
|
|
477
|
+
};
|
|
478
|
+
};
|
|
479
|
+
const mappedRun = (spec) => {
|
|
480
|
+
const cwd = mapPtcCwd(spec.cwd, repositoryOf(spec.cwd));
|
|
481
|
+
return inheritedRun(cwd === spec.cwd ? spec : {
|
|
482
|
+
...spec,
|
|
483
|
+
cwd
|
|
484
|
+
});
|
|
485
|
+
};
|
|
486
|
+
Object.defineProperty(seam, "resolve", {
|
|
487
|
+
configurable: true,
|
|
488
|
+
writable: true,
|
|
489
|
+
value: mappedResolve
|
|
490
|
+
});
|
|
491
|
+
Object.defineProperty(seam, "run", {
|
|
492
|
+
configurable: true,
|
|
493
|
+
writable: true,
|
|
494
|
+
value: mappedRun
|
|
495
|
+
});
|
|
496
|
+
return () => {
|
|
497
|
+
for (const [method, installed] of [["resolve", mappedResolve], ["run", mappedRun]]) {
|
|
498
|
+
if (Object.getOwnPropertyDescriptor(seam, method)?.value !== installed) continue;
|
|
499
|
+
Reflect.deleteProperty(seam, method);
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
//#endregion
|
|
504
|
+
//#region src/subprocess.ts
|
|
505
|
+
/** Read one service without a typed `Context` augmentation, as the other rows do. */
|
|
506
|
+
function serviceOf$1(ctx, service) {
|
|
507
|
+
return ctx.get(service);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* The working directory a host process may actually start in.
|
|
511
|
+
* @param cwd - the directory the caller asked for.
|
|
512
|
+
* @param repository - the container repository that owns it, when one does.
|
|
513
|
+
* @returns the host path of the same directory, or `cwd` unchanged.
|
|
514
|
+
*/
|
|
515
|
+
function mapSpawnCwd(cwd, repository) {
|
|
516
|
+
return repository === void 0 ? cwd : toHostPath(cwd, repository);
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* The host directories that can name the repository behind a container path.
|
|
520
|
+
*
|
|
521
|
+
* Discovery walks host ancestors, and a container path has none, so the walk needs
|
|
522
|
+
* a real directory to start from. The initiating Agent's own two are the ones this
|
|
523
|
+
* session started with: its working-directory projection is where the model
|
|
524
|
+
* believes it is, and its Session header keeps the project directory the Session
|
|
525
|
+
* was created in — a host path, because the Session is created on the host. An
|
|
526
|
+
* anchor that names nothing real simply discovers nothing; ownership still decides
|
|
527
|
+
* whether a repository may claim the path.
|
|
528
|
+
* @param ctx - the calling row's context.
|
|
529
|
+
* @returns candidate host directories, best first, without duplicates.
|
|
530
|
+
*/
|
|
531
|
+
function hostAnchors(ctx) {
|
|
532
|
+
let initiator;
|
|
533
|
+
try {
|
|
534
|
+
initiator = serviceOf$1(ctx, "agents")?.currentInitiator();
|
|
535
|
+
} catch {
|
|
536
|
+
initiator = void 0;
|
|
537
|
+
}
|
|
538
|
+
if (initiator === void 0) return [];
|
|
539
|
+
const anchors = [];
|
|
540
|
+
const directories = serviceOf$1(ctx, "workingDirectory");
|
|
541
|
+
if (directories !== void 0) try {
|
|
542
|
+
anchors.push(directories.get(initiator.session));
|
|
543
|
+
} catch {}
|
|
544
|
+
const project = initiator.session.header?.cwd;
|
|
545
|
+
if (project !== void 0) anchors.push(project);
|
|
546
|
+
return [...new Set(anchors)];
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* The repository whose runtime owns one directory.
|
|
550
|
+
*
|
|
551
|
+
* A `(sbx)` session mounts the host directory at its own absolute path, so the
|
|
552
|
+
* specification handed back is the sandbox view of the same repository: the
|
|
553
|
+
* declared `workspaceFolder` (`/workspaces/<repo>`) is replaced by the host
|
|
554
|
+
* root, which keeps the translation this module performs a normalization instead
|
|
555
|
+
* of a wrong mount. A sandbox directory with no declaration at all is described
|
|
556
|
+
* from the directory itself, because a sandbox needs no declaration.
|
|
557
|
+
* @param ctx - the calling row's context.
|
|
558
|
+
* @param path - a container path, a host path, or a relative path.
|
|
559
|
+
* @param options - the row's container defaults, applied to a fresh discovery.
|
|
560
|
+
* @returns the owning repository specification, or undefined when nothing declares a container for it.
|
|
561
|
+
*/
|
|
562
|
+
function repositoryOfPath(ctx, path, options) {
|
|
563
|
+
const kind = callingVariantKind(ctx);
|
|
564
|
+
const adopt = (spec) => kind === "sbx" ? asSbxSpec(spec) : spec;
|
|
565
|
+
const registry = repoRegistry(ctx);
|
|
566
|
+
for (const anchor of hostAnchors(ctx)) {
|
|
567
|
+
const found = registry.locate(path, {
|
|
568
|
+
...options,
|
|
569
|
+
cwd: anchor
|
|
570
|
+
});
|
|
571
|
+
if (found !== void 0) return adopt(found);
|
|
572
|
+
}
|
|
573
|
+
const found = registry.locate(path, options);
|
|
574
|
+
if (found !== void 0) return adopt(found);
|
|
575
|
+
if (kind !== "sbx") return void 0;
|
|
576
|
+
const root = sbxWorkspaceRootFor(path, void 0, void 0);
|
|
577
|
+
return root === void 0 ? void 0 : sbxSpecFor(root, options);
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Start every host process in a directory that exists on this host.
|
|
581
|
+
*
|
|
582
|
+
* The seam is decorated in place rather than replaced: Cordis holds one
|
|
583
|
+
* implementation per service name, so a second `subprocess` row would be refused
|
|
584
|
+
* at activation, while every spawn that needs no rewrite — an ordinary session's
|
|
585
|
+
* shell, a language server in a host repository — must keep the provider's own
|
|
586
|
+
* process mechanics. The inherited method is called with its own receiver, and
|
|
587
|
+
* the original spec object is passed through untouched when nothing is mapped.
|
|
588
|
+
* @param seam - the mounted subprocess service.
|
|
589
|
+
* @param repositoryOf - the repository that owns one directory, when one does.
|
|
590
|
+
* @returns the disposer that puts the inherited method back.
|
|
591
|
+
*/
|
|
592
|
+
function installSpawnCwdMapping(seam, repositoryOf) {
|
|
593
|
+
const inherited = seam.spawn.bind(seam);
|
|
594
|
+
const mapped = (spec) => {
|
|
595
|
+
const cwd = mapSpawnCwd(spec.cwd, repositoryOf(spec.cwd));
|
|
596
|
+
return inherited(cwd === spec.cwd ? spec : {
|
|
597
|
+
...spec,
|
|
598
|
+
cwd
|
|
599
|
+
});
|
|
600
|
+
};
|
|
601
|
+
Object.defineProperty(seam, "spawn", {
|
|
602
|
+
configurable: true,
|
|
603
|
+
writable: true,
|
|
604
|
+
value: mapped
|
|
605
|
+
});
|
|
606
|
+
return () => {
|
|
607
|
+
if (Object.getOwnPropertyDescriptor(seam, "spawn")?.value !== mapped) return;
|
|
608
|
+
Reflect.deleteProperty(seam, "spawn");
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
//#endregion
|
|
612
|
+
//#region src/variants.ts
|
|
613
|
+
/**
|
|
614
|
+
* Keeps the registered variant set equal to the two stored source lists.
|
|
615
|
+
*
|
|
616
|
+
* Every call is idempotent: `sync` registers what is missing, disposes what is
|
|
617
|
+
* no longer asked for, and touches nothing that is already correct. A variant
|
|
618
|
+
* that fails to register is dropped from the bookkeeping and logged, so the rest
|
|
619
|
+
* of the set still applies.
|
|
620
|
+
*/
|
|
621
|
+
var VariantSync = class {
|
|
622
|
+
options;
|
|
623
|
+
/** Registered variant ids, in registration order, with their disposers. */
|
|
624
|
+
registered = /* @__PURE__ */ new Map();
|
|
625
|
+
/** Serializes overlapping syncs so a later list cannot observe a half-applied one. */
|
|
626
|
+
queue = Promise.resolve();
|
|
627
|
+
/** Set once the row unloads: a queued sync then registers nothing. */
|
|
628
|
+
stopped = false;
|
|
629
|
+
constructor(options) {
|
|
630
|
+
this.options = options;
|
|
631
|
+
}
|
|
632
|
+
/** The ids registered right now, in dependency order. */
|
|
633
|
+
ids() {
|
|
634
|
+
return [...this.registered.keys()];
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Apply both source lists.
|
|
638
|
+
* @param sources - the preset ids to derive `… (devContainer)` variants from.
|
|
639
|
+
* @param sbxSources - the preset ids to derive `… (sbx)` variants from.
|
|
640
|
+
* @returns the settled application, in queue order.
|
|
641
|
+
*/
|
|
642
|
+
sync(sources, sbxSources = []) {
|
|
643
|
+
return this.enqueue(async () => {
|
|
644
|
+
if (this.stopped) return;
|
|
645
|
+
const planned = planVariants({
|
|
646
|
+
declarations: this.options.declarations(),
|
|
647
|
+
sources,
|
|
648
|
+
sbxSources,
|
|
649
|
+
orderOffset: this.options.orderOffset,
|
|
650
|
+
...this.options.maxDepth === void 0 ? {} : { maxDepth: this.options.maxDepth }
|
|
651
|
+
});
|
|
652
|
+
await this.withdraw(planned);
|
|
653
|
+
await this.add(planned);
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Dispose every registered variant.
|
|
658
|
+
* @returns the settled teardown, in queue order.
|
|
659
|
+
*/
|
|
660
|
+
clear() {
|
|
661
|
+
return this.enqueue(async () => {
|
|
662
|
+
await this.withdraw([]);
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Stop for good: queued work is skipped and nothing new registers.
|
|
667
|
+
* @returns the settled teardown, in queue order.
|
|
668
|
+
*/
|
|
669
|
+
stop() {
|
|
670
|
+
this.stopped = true;
|
|
671
|
+
return this.clear();
|
|
672
|
+
}
|
|
673
|
+
enqueue(work) {
|
|
674
|
+
const next = this.queue.then(work, work);
|
|
675
|
+
this.queue = next.catch(() => {});
|
|
676
|
+
return next;
|
|
677
|
+
}
|
|
678
|
+
/** Dispose the registered variants the plan no longer contains, dependants first. */
|
|
679
|
+
async withdraw(planned) {
|
|
680
|
+
const keep = new Set(planned.map((variant) => variant.id));
|
|
681
|
+
const order = [...this.registered.keys()].reverse();
|
|
682
|
+
for (const id of order) {
|
|
683
|
+
if (keep.has(id)) continue;
|
|
684
|
+
const dispose = this.registered.get(id);
|
|
685
|
+
this.registered.delete(id);
|
|
686
|
+
if (dispose === void 0) continue;
|
|
687
|
+
try {
|
|
688
|
+
await dispose();
|
|
689
|
+
} catch (error) {
|
|
690
|
+
this.options.logger.warn(`devcontainer: preset variant ${id} did not unregister cleanly: ${String(error)}`);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
/** Register the planned variants that are not registered yet, dependencies first. */
|
|
695
|
+
async add(planned) {
|
|
696
|
+
for (const variant of planned) {
|
|
697
|
+
if (this.registered.has(variant.id)) continue;
|
|
698
|
+
if (this.stopped) return;
|
|
699
|
+
try {
|
|
700
|
+
const dispose = await this.options.registry.register({
|
|
701
|
+
id: variant.id,
|
|
702
|
+
name: variant.name,
|
|
703
|
+
description: variant.description,
|
|
704
|
+
order: variant.order,
|
|
705
|
+
plugins: variant.plugins
|
|
706
|
+
});
|
|
707
|
+
this.registered.set(variant.id, dispose);
|
|
708
|
+
} catch (error) {
|
|
709
|
+
this.options.logger.warn(`devcontainer: preset variant ${variant.id} unavailable: ${String(error)}`);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
//#endregion
|
|
715
|
+
//#region src/index.ts
|
|
716
|
+
/** Plugin name used by the Loader row, and the settings namespace the page edits. */
|
|
717
|
+
const name = "devcontainer";
|
|
718
|
+
/** How deep a derived preset may follow its subagent routes. */
|
|
719
|
+
const MAX_VARIANT_DEPTH = 4;
|
|
720
|
+
/** The host-level event a settings write emits once the entry's config changed. */
|
|
721
|
+
const SETTINGS_UPDATED = "settings/document-updated";
|
|
722
|
+
/** The Loader event a volatile-only config change commits through. */
|
|
723
|
+
const VOLATILE_UPDATED = "loader/volatile-update";
|
|
724
|
+
/**
|
|
725
|
+
* The settings namespace the shipped preset surfaces watch for roster changes.
|
|
726
|
+
*
|
|
727
|
+
* They re-read `agentPresets.list` when `settings/document-updated` names the
|
|
728
|
+
* preset REGISTRY entry (`packages/client/ui-agent-preset/src/client/index.ts:164-167`),
|
|
729
|
+
* which is the only roster-refresh signal a third-party registration can reach.
|
|
730
|
+
*/
|
|
731
|
+
const PRESET_REGISTRY_NS = "agent-preset-registry";
|
|
732
|
+
/** Labelling and gating the schema also supplies, kept here for the runtime read. */
|
|
733
|
+
const VARIANT_DEFAULTS = {
|
|
734
|
+
enabled: true,
|
|
735
|
+
win32Only: true,
|
|
736
|
+
orderOffset: 1
|
|
737
|
+
};
|
|
738
|
+
/**
|
|
739
|
+
* The live schema the settings page edits and the Loader validates.
|
|
740
|
+
*
|
|
741
|
+
* Deliberately not annotated `z<Config>`: the interface describes the runtime
|
|
742
|
+
* config (`sources` is a `Volatile` reference), while the schema's own output is
|
|
743
|
+
* the plain field type.
|
|
744
|
+
*/
|
|
745
|
+
const Config = z.object({
|
|
746
|
+
...containerOptionFields(),
|
|
747
|
+
...presetSourceFields(),
|
|
748
|
+
presets: z.object({
|
|
749
|
+
enabled: z.boolean().default(true),
|
|
750
|
+
win32Only: z.boolean().default(true),
|
|
751
|
+
orderOffset: z.number().default(1)
|
|
752
|
+
})
|
|
753
|
+
});
|
|
754
|
+
/** Read one service without a typed `Context` augmentation. */
|
|
755
|
+
function serviceOf(ctx, service) {
|
|
756
|
+
return ctx.get(service);
|
|
757
|
+
}
|
|
758
|
+
/** The host directory one assembly is about. */
|
|
759
|
+
function hostDirectoryOf(ctx, assembly, agent) {
|
|
760
|
+
if (agent === void 0) return hostDirectoryFromContext(assembly);
|
|
761
|
+
const workingDirectory = serviceOf(ctx, "workingDirectory");
|
|
762
|
+
return workingDirectory === void 0 ? hostDirectoryFromContext(assembly) : workingDirectory.get(agent.session);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Load the row: rewrite the working-directory context, and keep the derived
|
|
766
|
+
* preset set equal to the two stored source lists.
|
|
767
|
+
*
|
|
768
|
+
* Deliberately the module's only plugin export besides its schema and name: the
|
|
769
|
+
* Loader's `unwrapExports` prefers a `default` export, and a `default`-exported
|
|
770
|
+
* `apply` would be the whole plugin — losing this module's `Config`, so the row
|
|
771
|
+
* would activate with the raw (absent) config instead of the resolved one.
|
|
772
|
+
* @param ctx - the Host plugin context.
|
|
773
|
+
* @param configured - this row's resolved config; absent when the row declares none.
|
|
774
|
+
*/
|
|
775
|
+
function apply(ctx, configured) {
|
|
776
|
+
const config = configured ?? {};
|
|
777
|
+
ctx.inject(["systemPrompt"], (child) => {
|
|
778
|
+
child.on("system-prompt/assemble", async (_assembly, context, next) => {
|
|
779
|
+
const result = await next();
|
|
780
|
+
const agent = context.agent;
|
|
781
|
+
const preset = containerPresetOf(serviceOf(child, "agentPresets"), agent?.session);
|
|
782
|
+
if (preset === void 0 && !config.requireContainer) return result;
|
|
783
|
+
const hostWorkdir = hostDirectoryOf(child, result, agent);
|
|
784
|
+
if (hostWorkdir === void 0) return result;
|
|
785
|
+
const options = containerOptions(config);
|
|
786
|
+
const repository = repoRegistry(child).locate(hostWorkdir, {
|
|
787
|
+
...options,
|
|
788
|
+
cwd: hostWorkdir
|
|
789
|
+
});
|
|
790
|
+
if (presetKindOf(preset) === "sbx") {
|
|
791
|
+
const root = sbxWorkspaceRootFor(hostWorkdir, hostWorkdir, repository);
|
|
792
|
+
if (root === void 0) return result;
|
|
793
|
+
const spec = sbxSpecFor(root, options, repository);
|
|
794
|
+
applyContainerContext(result, sbxContextText(toContainerPath(hostWorkdir, spec), spec.sandbox ?? sandboxNameFor(root), hostWorkdir));
|
|
795
|
+
return result;
|
|
796
|
+
}
|
|
797
|
+
if (repository === void 0) {
|
|
798
|
+
if (preset === void 0) return result;
|
|
799
|
+
applyContainerContext(result, missingContainerContextText(hostWorkdir, preset));
|
|
800
|
+
return result;
|
|
801
|
+
}
|
|
802
|
+
applyContainerContext(result, containerContextText(toContainerPath(hostWorkdir, repository), repository, hostWorkdir));
|
|
803
|
+
return result;
|
|
804
|
+
}, { prepend: true });
|
|
805
|
+
});
|
|
806
|
+
ctx.inject(["subprocess"], (child) => {
|
|
807
|
+
child.effect(() => installSpawnCwdMapping(child.subprocess, (cwd) => repositoryOfPath(child, cwd, containerOptions(config))), "devcontainer.spawn-cwd");
|
|
808
|
+
});
|
|
809
|
+
ctx.inject(["ptcRuntime"], (child) => {
|
|
810
|
+
child.effect(() => installPtcCwdMapping(child.ptcRuntime, (cwd) => repositoryOfPath(child, cwd, containerOptions(config))), "devcontainer.ptc-cwd");
|
|
811
|
+
});
|
|
812
|
+
ctx.effect(() => {
|
|
813
|
+
const cache = repoRegistry(ctx);
|
|
814
|
+
markContainerPresets(ctx);
|
|
815
|
+
return () => {
|
|
816
|
+
cache.reset();
|
|
817
|
+
};
|
|
818
|
+
});
|
|
819
|
+
registerVariants(ctx, config);
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Mark every shell call issued from a derived preset.
|
|
823
|
+
*
|
|
824
|
+
* The marker rides the managed `DSH_*` environment, which the shell tools build
|
|
825
|
+
* per call from the calling agent: `exec.agent` is the one fact the executor
|
|
826
|
+
* cannot otherwise see, so this is what lets `DevcontainerShellExecutor` refuse a
|
|
827
|
+
* command instead of guessing whose it is. Each track has its own pair of
|
|
828
|
+
* variables — `DSH_DEVCONTAINER` / `DSH_DEVCONTAINER_PRESET` and `DSH_SBX` /
|
|
829
|
+
* `DSH_SBX_PRESET` — so the executor, the filesystem, the subprocess, and the PTC
|
|
830
|
+
* seams route a call without re-deriving which preset asked for it. Values are
|
|
831
|
+
* recomputed per call, so switching presets mid-session needs no reload.
|
|
832
|
+
*/
|
|
833
|
+
function markContainerPresets(ctx) {
|
|
834
|
+
ctx.inject(["shellEnv"], (child) => {
|
|
835
|
+
const shellEnv = serviceOf(child, "shellEnv");
|
|
836
|
+
if (shellEnv === void 0) return;
|
|
837
|
+
child.effect(() => shellEnv.register({
|
|
838
|
+
name: "devcontainer-preset",
|
|
839
|
+
variables: {
|
|
840
|
+
[DEVCONTAINER_ENV_KEY]: { description: "Set for shell calls issued from a (devContainer) preset; the command must reach a container." },
|
|
841
|
+
[DEVCONTAINER_PRESET_ENV_KEY]: { description: "The (devContainer) preset id that issued this shell call." },
|
|
842
|
+
[SBX_ENV_KEY]: { description: "Set for shell calls issued from a (sbx) preset; the command must reach its Docker Sandbox." },
|
|
843
|
+
[SBX_PRESET_ENV_KEY]: { description: "The (sbx) preset id that issued this shell call." }
|
|
844
|
+
},
|
|
845
|
+
resolve: (execution) => {
|
|
846
|
+
const presets = serviceOf(child, "agentPresets");
|
|
847
|
+
const preset = containerPresetOf(presets, execution.agent?.session);
|
|
848
|
+
if (preset === void 0) return {};
|
|
849
|
+
if (presetKindOf(preset) === "sbx") return {
|
|
850
|
+
[SBX_ENV_KEY]: "1",
|
|
851
|
+
[SBX_PRESET_ENV_KEY]: preset
|
|
852
|
+
};
|
|
853
|
+
return {
|
|
854
|
+
[DEVCONTAINER_ENV_KEY]: "1",
|
|
855
|
+
[DEVCONTAINER_PRESET_ENV_KEY]: preset
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
}));
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Wire the variant set: derive once the tree is up, then re-derive on every write
|
|
863
|
+
* that lands in this row's config.
|
|
864
|
+
*
|
|
865
|
+
* The sync is built inside `ctx.inject`, not at mount time. Both services can
|
|
866
|
+
* activate AFTER this row (this row is inserted by the Bundle patch; the preset
|
|
867
|
+
* registry is configured by the Profile patch), and a lookup made at mount time —
|
|
868
|
+
* memoized like any other lazy init — would keep reporting "no registry" for the
|
|
869
|
+
* life of the process, which looks exactly like a page whose checkbox never
|
|
870
|
+
* registers anything. Cordis holds this callback until both exist and re-runs it
|
|
871
|
+
* if they ever go away, so the derived set follows the composition.
|
|
872
|
+
*/
|
|
873
|
+
function registerVariants(ctx, config) {
|
|
874
|
+
const presets = {
|
|
875
|
+
...VARIANT_DEFAULTS,
|
|
876
|
+
...config.presets
|
|
877
|
+
};
|
|
878
|
+
if (!presets.enabled) return;
|
|
879
|
+
if (presets.win32Only && process.platform !== "win32") return;
|
|
880
|
+
const storedList = (field) => {
|
|
881
|
+
const value = ctx.fiber?.effectiveConfig?.[field] ?? config[field];
|
|
882
|
+
if (value === void 0 || value === null) return [];
|
|
883
|
+
const list = typeof value.get === "function" ? value.get() : value;
|
|
884
|
+
return Array.isArray(list) ? list.filter((id) => typeof id === "string" && id.length > 0) : [];
|
|
885
|
+
};
|
|
886
|
+
const root = ctx.root;
|
|
887
|
+
ctx.inject(["loader", "agentPresets"], (child) => {
|
|
888
|
+
const loader = serviceOf(child, "loader");
|
|
889
|
+
const registry = serviceOf(child, "agentPresets");
|
|
890
|
+
const declarations = () => {
|
|
891
|
+
const found = /* @__PURE__ */ new Map();
|
|
892
|
+
for (const entry of loader.entries()) {
|
|
893
|
+
if (entry.options.name !== "@deepseek-ai/dsh-agent-preset") continue;
|
|
894
|
+
const declaration = asDeclaration(entry.options.config);
|
|
895
|
+
if (declaration !== void 0) found.set(declaration.id, declaration);
|
|
896
|
+
}
|
|
897
|
+
return found;
|
|
898
|
+
};
|
|
899
|
+
const sync = new VariantSync({
|
|
900
|
+
registry,
|
|
901
|
+
logger: ctx.logger,
|
|
902
|
+
declarations,
|
|
903
|
+
orderOffset: presets.orderOffset,
|
|
904
|
+
maxDepth: MAX_VARIANT_DEPTH
|
|
905
|
+
});
|
|
906
|
+
const run = () => {
|
|
907
|
+
(async () => {
|
|
908
|
+
await loader.await();
|
|
909
|
+
const before = sync.ids().join("\n");
|
|
910
|
+
await sync.sync(storedList("sources"), storedList("sbxSources"));
|
|
911
|
+
if (sync.ids().join("\n") !== before) root.emit(SETTINGS_UPDATED, PRESET_REGISTRY_NS, Date.now());
|
|
912
|
+
})().catch((error) => {
|
|
913
|
+
ctx.logger.warn(`devcontainer: preset variants unavailable: ${String(error)}`);
|
|
914
|
+
});
|
|
915
|
+
};
|
|
916
|
+
child.effect(() => () => {
|
|
917
|
+
sync.stop();
|
|
918
|
+
}, "devcontainer.preset-variants");
|
|
919
|
+
child.on(SETTINGS_UPDATED, run);
|
|
920
|
+
child.on(VOLATILE_UPDATED, run);
|
|
921
|
+
run();
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
//#endregion
|
|
925
|
+
export { Config, apply, name };
|
|
926
|
+
|
|
927
|
+
//# sourceMappingURL=index.js.map
|