@deepseek-ai/dsh-api-settings-controller 0.1.2-alpha.2
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.i18n.yaml +6 -0
- package/README.md +70 -0
- package/README.zh.md +70 -0
- package/lib/index.js +582 -0
- package/lib/invariant.js +16 -0
- package/lib/typert.host.d.ts +3 -0
- package/lib/typert.host.js +540 -0
- package/lib/typert.remote-client.d.ts +46 -0
- package/lib/typert.remote-client.js +407 -0
- package/lib/types/credentials.d.ts +63 -0
- package/lib/types/credentials.js +175 -0
- package/lib/types/index.d.ts +114 -0
- package/lib/types/index.js +317 -0
- package/lib/types/invariant.d.ts +9 -0
- package/lib/types/invariant.js +15 -0
- package/lib/types/types.d.ts +48 -0
- package/lib/types/types.js +10 -0
- package/package.json +75 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { dirname } from "node:path";
|
|
2
|
+
import Schema from "@deepseek-ai/schemastery";
|
|
3
|
+
import { canOpenNativePath, openNativePath, openNativeTextFile } from "@deepseek-ai/dsh-native-command";
|
|
4
|
+
import { Remote, RemoteError, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
7
|
+
//#region lib/types/credentials.js
|
|
8
|
+
/**
|
|
9
|
+
* Host owner of the `credentials` Remote namespace: the reference half of
|
|
10
|
+
* `ctx.credentials` as a browser configuration page reads and writes it.
|
|
11
|
+
*
|
|
12
|
+
* @module @deepseek-ai/dsh-api-settings-controller/src/credentials.ts
|
|
13
|
+
*/
|
|
14
|
+
var __runInitializers$1 = function(thisArg, initializers, value) {
|
|
15
|
+
var useValue = arguments.length > 2;
|
|
16
|
+
for (var i = 0; i < initializers.length; i++) value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
17
|
+
return useValue ? value : void 0;
|
|
18
|
+
};
|
|
19
|
+
var __esDecorate$1 = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
20
|
+
function accept(f) {
|
|
21
|
+
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
|
|
22
|
+
return f;
|
|
23
|
+
}
|
|
24
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
25
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
26
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
27
|
+
var _, done = false;
|
|
28
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
29
|
+
var context = {};
|
|
30
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
31
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
32
|
+
context.addInitializer = function(f) {
|
|
33
|
+
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
|
|
34
|
+
extraInitializers.push(accept(f || null));
|
|
35
|
+
};
|
|
36
|
+
var result = (0, decorators[i])(kind === "accessor" ? {
|
|
37
|
+
get: descriptor.get,
|
|
38
|
+
set: descriptor.set
|
|
39
|
+
} : descriptor[key], context);
|
|
40
|
+
if (kind === "accessor") {
|
|
41
|
+
if (result === void 0) continue;
|
|
42
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
43
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
44
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
45
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
46
|
+
} else if (_ = accept(result)) if (kind === "field") initializers.unshift(_);
|
|
47
|
+
else descriptor[key] = _;
|
|
48
|
+
}
|
|
49
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
50
|
+
done = true;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Fan-out bound on one remote `describe` batch. A settings page asks about the
|
|
54
|
+
* references its own rows name, so this is far above any real page and still
|
|
55
|
+
* keeps one authenticated request from starting unbounded provider work.
|
|
56
|
+
*/
|
|
57
|
+
const MAX_DESCRIBE_REFS = 64;
|
|
58
|
+
const credentialRefSchema = z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/);
|
|
59
|
+
const describeRequestSchema = z.object({ refs: z.array(credentialRefSchema).max(MAX_DESCRIBE_REFS) });
|
|
60
|
+
const setRequestSchema = z.object({
|
|
61
|
+
ref: credentialRefSchema,
|
|
62
|
+
value: z.string().min(1)
|
|
63
|
+
});
|
|
64
|
+
const unsetRequestSchema = z.object({ ref: credentialRefSchema });
|
|
65
|
+
/** Parse the domain constraints that are more specific than generated TypeScript codecs. */
|
|
66
|
+
function parseRequest(method, schema, value) {
|
|
67
|
+
const parsed = schema.safeParse(value);
|
|
68
|
+
if (!parsed.success) throw new RemoteError("gateway/bad-request", `invalid payload for ${method}`, { issues: parsed.error.issues });
|
|
69
|
+
return parsed.data;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Copy exactly the fields {@link CredentialInfo} declares. The Gateway returns
|
|
73
|
+
* a business result without decoding it, so a provider whose `describe` carried
|
|
74
|
+
* extra enumerable properties would otherwise serialize them to the caller.
|
|
75
|
+
* @param info - the provider's answer for one reference.
|
|
76
|
+
* @returns the same facts with nothing else attached.
|
|
77
|
+
*/
|
|
78
|
+
function projectCredentialInfo(info) {
|
|
79
|
+
return {
|
|
80
|
+
configured: info.configured,
|
|
81
|
+
...info.source === void 0 ? {} : { source: info.source },
|
|
82
|
+
writable: info.writable
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Host service backing the generated `ctx.remote.credentials` namespace. It
|
|
87
|
+
* carries every wire obligation the credential seam itself does not: the batch
|
|
88
|
+
* fan-out bound, the field-by-field view projection, the reference-grammar
|
|
89
|
+
* guard, and the refusal mapping. Secret values cross in one direction only —
|
|
90
|
+
* no method here returns one.
|
|
91
|
+
*/
|
|
92
|
+
let CredentialsController = (() => {
|
|
93
|
+
let _classSuper = TypertRemoteService;
|
|
94
|
+
let _instanceExtraInitializers = [];
|
|
95
|
+
let _describe_decorators;
|
|
96
|
+
let _set_decorators;
|
|
97
|
+
let _unset_decorators;
|
|
98
|
+
return class CredentialsController extends _classSuper {
|
|
99
|
+
static {
|
|
100
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
101
|
+
_describe_decorators = [Remote];
|
|
102
|
+
_set_decorators = [Remote];
|
|
103
|
+
_unset_decorators = [Remote];
|
|
104
|
+
__esDecorate$1(this, null, _describe_decorators, {
|
|
105
|
+
kind: "method",
|
|
106
|
+
name: "describe",
|
|
107
|
+
static: false,
|
|
108
|
+
private: false,
|
|
109
|
+
access: {
|
|
110
|
+
has: (obj) => "describe" in obj,
|
|
111
|
+
get: (obj) => obj.describe
|
|
112
|
+
},
|
|
113
|
+
metadata: _metadata
|
|
114
|
+
}, null, _instanceExtraInitializers);
|
|
115
|
+
__esDecorate$1(this, null, _set_decorators, {
|
|
116
|
+
kind: "method",
|
|
117
|
+
name: "set",
|
|
118
|
+
static: false,
|
|
119
|
+
private: false,
|
|
120
|
+
access: {
|
|
121
|
+
has: (obj) => "set" in obj,
|
|
122
|
+
get: (obj) => obj.set
|
|
123
|
+
},
|
|
124
|
+
metadata: _metadata
|
|
125
|
+
}, null, _instanceExtraInitializers);
|
|
126
|
+
__esDecorate$1(this, null, _unset_decorators, {
|
|
127
|
+
kind: "method",
|
|
128
|
+
name: "unset",
|
|
129
|
+
static: false,
|
|
130
|
+
private: false,
|
|
131
|
+
access: {
|
|
132
|
+
has: (obj) => "unset" in obj,
|
|
133
|
+
get: (obj) => obj.unset
|
|
134
|
+
},
|
|
135
|
+
metadata: _metadata
|
|
136
|
+
}, null, _instanceExtraInitializers);
|
|
137
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: true,
|
|
140
|
+
writable: true,
|
|
141
|
+
value: _metadata
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** @param ctx - Host context where a credential provider may be mounted. */
|
|
145
|
+
constructor(ctx) {
|
|
146
|
+
super(ctx, "credentialsController", { namespace: "credentials" });
|
|
147
|
+
__runInitializers$1(this, _instanceExtraInitializers);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Describe several references for one configuration surface. Batched because
|
|
151
|
+
* a settings page describes every reference its rows name at once, and one
|
|
152
|
+
* round trip keeps those rows from settling separately.
|
|
153
|
+
* @param refs - reference names, at most {@link MAX_DESCRIBE_REFS}; a name outside the grammar
|
|
154
|
+
* rejects the whole call as `gateway/bad-request`.
|
|
155
|
+
* @returns one view per requested name, keyed by that name.
|
|
156
|
+
* @throws RemoteError when the request is invalid or no credential provider is mounted.
|
|
157
|
+
*/
|
|
158
|
+
async describe(refs) {
|
|
159
|
+
const branded = parseRequest("credentials.describe", describeRequestSchema, { refs }).refs.map((ref) => [ref, credentialRef(ref)]);
|
|
160
|
+
const credentials = this.provider();
|
|
161
|
+
const entries = await Promise.all(branded.map(async ([ref, key]) => [ref, projectCredentialInfo(await credentials.describe(key))]));
|
|
162
|
+
return Object.fromEntries(entries);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Store one value from a configuration surface. The value crosses the wire in
|
|
166
|
+
* this direction only: no read path returns it.
|
|
167
|
+
* @param ref - reference name to store under.
|
|
168
|
+
* @param value - the non-empty secret value.
|
|
169
|
+
* @throws RemoteError when the request is invalid, no provider is mounted, or the provider refuses the write.
|
|
170
|
+
*/
|
|
171
|
+
async set(ref, value) {
|
|
172
|
+
const request = parseRequest("credentials.set", setRequestSchema, {
|
|
173
|
+
ref,
|
|
174
|
+
value
|
|
175
|
+
});
|
|
176
|
+
const branded = credentialRef(request.ref);
|
|
177
|
+
const credentials = this.provider();
|
|
178
|
+
await this.write(request.ref, () => credentials.set(branded, request.value));
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Remove one reference from a configuration surface.
|
|
182
|
+
* @param ref - reference name to remove.
|
|
183
|
+
* @throws RemoteError when the request is invalid, no provider is mounted, or the provider refuses the write.
|
|
184
|
+
*/
|
|
185
|
+
async unset(ref) {
|
|
186
|
+
const request = parseRequest("credentials.unset", unsetRequestSchema, { ref });
|
|
187
|
+
const branded = credentialRef(request.ref);
|
|
188
|
+
const credentials = this.provider();
|
|
189
|
+
await this.write(request.ref, () => credentials.unset(branded));
|
|
190
|
+
}
|
|
191
|
+
/** Resolve the optional provider or report how to supply it. */
|
|
192
|
+
provider() {
|
|
193
|
+
const credentials = this.ctx.get("credentials");
|
|
194
|
+
if (credentials === void 0) throw new RemoteError("gateway/internal", "credentials service is absent: this deployment does not mount a credential provider (e.g. @deepseek-ai/dsh-credentials-local) in its composition", {});
|
|
195
|
+
return credentials;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Run one remote write and report every refusal as `credential/rejected`
|
|
199
|
+
* carrying the seam's own message: a read-only source shadowing the reference
|
|
200
|
+
* is what a configuration surface must show verbatim. Callers brand the
|
|
201
|
+
* reference before entering, so a name outside the grammar never reaches this
|
|
202
|
+
* path and fails the same way it does on the read side. The details name only
|
|
203
|
+
* the reference, so no failure path can carry the value back out.
|
|
204
|
+
*/
|
|
205
|
+
async write(ref, write) {
|
|
206
|
+
try {
|
|
207
|
+
await write();
|
|
208
|
+
} catch (error) {
|
|
209
|
+
throw new RemoteError("credential/rejected", error instanceof Error ? error.message : String(error), { ref }, { cause: error });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
})();
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region lib/types/index.js
|
|
216
|
+
/**
|
|
217
|
+
* Host Remote owner for the configuration surfaces over the settings-domain
|
|
218
|
+
* seams. Two namespaces: `settings`, the redacted reads and writes of
|
|
219
|
+
* `ctx.settings`, owned by the class below; and `credentials`, mounted from
|
|
220
|
+
* here as its own plugin.
|
|
221
|
+
*
|
|
222
|
+
* @module @deepseek-ai/dsh-api-settings-controller
|
|
223
|
+
*/
|
|
224
|
+
var __runInitializers = function(thisArg, initializers, value) {
|
|
225
|
+
var useValue = arguments.length > 2;
|
|
226
|
+
for (var i = 0; i < initializers.length; i++) value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
227
|
+
return useValue ? value : void 0;
|
|
228
|
+
};
|
|
229
|
+
var __esDecorate = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
230
|
+
function accept(f) {
|
|
231
|
+
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
|
|
232
|
+
return f;
|
|
233
|
+
}
|
|
234
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
235
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
236
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
237
|
+
var _, done = false;
|
|
238
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
239
|
+
var context = {};
|
|
240
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
241
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
242
|
+
context.addInitializer = function(f) {
|
|
243
|
+
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
|
|
244
|
+
extraInitializers.push(accept(f || null));
|
|
245
|
+
};
|
|
246
|
+
var result = (0, decorators[i])(kind === "accessor" ? {
|
|
247
|
+
get: descriptor.get,
|
|
248
|
+
set: descriptor.set
|
|
249
|
+
} : descriptor[key], context);
|
|
250
|
+
if (kind === "accessor") {
|
|
251
|
+
if (result === void 0) continue;
|
|
252
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
253
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
254
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
255
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
256
|
+
} else if (_ = accept(result)) if (kind === "field") initializers.unshift(_);
|
|
257
|
+
else descriptor[key] = _;
|
|
258
|
+
}
|
|
259
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
260
|
+
done = true;
|
|
261
|
+
};
|
|
262
|
+
const settingsNamespaceRequestSchema = z.object({ ns: z.string().min(1) });
|
|
263
|
+
/** Read abort state afresh after an awaited provider or opener call. */
|
|
264
|
+
function isAborted(signal) {
|
|
265
|
+
return signal.aborted;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Project one redacted descriptor onto its wire view, field by field. The
|
|
269
|
+
* Gateway returns a business result without decoding it, so a provider whose
|
|
270
|
+
* descriptor carried extra enumerable properties would otherwise serialize them
|
|
271
|
+
* to the caller.
|
|
272
|
+
* @param descriptor - one descriptor read under `redactSecrets`.
|
|
273
|
+
* @returns the same facts with nothing else attached.
|
|
274
|
+
*/
|
|
275
|
+
function namespaceView(descriptor) {
|
|
276
|
+
return {
|
|
277
|
+
ns: String(descriptor.ns),
|
|
278
|
+
schema: descriptor.schema,
|
|
279
|
+
value: descriptor.value,
|
|
280
|
+
...descriptor.base === void 0 ? {} : { base: descriptor.base },
|
|
281
|
+
...descriptor.user === void 0 ? {} : { user: descriptor.user },
|
|
282
|
+
applies: descriptor.applies,
|
|
283
|
+
secrets: (descriptor.secrets ?? []).map((secret) => ({
|
|
284
|
+
path: [...secret.path],
|
|
285
|
+
set: secret.set
|
|
286
|
+
})),
|
|
287
|
+
revision: descriptor.revision
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Host service backing the generated `ctx.remote.settings` namespace. Every
|
|
292
|
+
* remote read uses `redactSecrets: true`, so a `role('secret')` field cannot
|
|
293
|
+
* ride a response. Writes expose the settings service's merge, replacement,
|
|
294
|
+
* and path-addressed operations, and classify every provider refusal as
|
|
295
|
+
* `settings/conflict` or `settings/rejected` with the service's message.
|
|
296
|
+
*/
|
|
297
|
+
let SettingsController = (() => {
|
|
298
|
+
let _classSuper = TypertRemoteService;
|
|
299
|
+
let _instanceExtraInitializers = [];
|
|
300
|
+
let _describe_decorators;
|
|
301
|
+
let _canOpenAgentPresetDirectory_decorators;
|
|
302
|
+
let _update_decorators;
|
|
303
|
+
let _replace_decorators;
|
|
304
|
+
let _mutate_decorators;
|
|
305
|
+
let _openSettingsDocument_decorators;
|
|
306
|
+
let _openAgentPresetDirectory_decorators;
|
|
307
|
+
return class SettingsController extends _classSuper {
|
|
308
|
+
static {
|
|
309
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
310
|
+
_describe_decorators = [Remote];
|
|
311
|
+
_canOpenAgentPresetDirectory_decorators = [Remote];
|
|
312
|
+
_update_decorators = [Remote];
|
|
313
|
+
_replace_decorators = [Remote];
|
|
314
|
+
_mutate_decorators = [Remote];
|
|
315
|
+
_openSettingsDocument_decorators = [Remote];
|
|
316
|
+
_openAgentPresetDirectory_decorators = [Remote];
|
|
317
|
+
__esDecorate(this, null, _describe_decorators, {
|
|
318
|
+
kind: "method",
|
|
319
|
+
name: "describe",
|
|
320
|
+
static: false,
|
|
321
|
+
private: false,
|
|
322
|
+
access: {
|
|
323
|
+
has: (obj) => "describe" in obj,
|
|
324
|
+
get: (obj) => obj.describe
|
|
325
|
+
},
|
|
326
|
+
metadata: _metadata
|
|
327
|
+
}, null, _instanceExtraInitializers);
|
|
328
|
+
__esDecorate(this, null, _canOpenAgentPresetDirectory_decorators, {
|
|
329
|
+
kind: "method",
|
|
330
|
+
name: "canOpenAgentPresetDirectory",
|
|
331
|
+
static: false,
|
|
332
|
+
private: false,
|
|
333
|
+
access: {
|
|
334
|
+
has: (obj) => "canOpenAgentPresetDirectory" in obj,
|
|
335
|
+
get: (obj) => obj.canOpenAgentPresetDirectory
|
|
336
|
+
},
|
|
337
|
+
metadata: _metadata
|
|
338
|
+
}, null, _instanceExtraInitializers);
|
|
339
|
+
__esDecorate(this, null, _update_decorators, {
|
|
340
|
+
kind: "method",
|
|
341
|
+
name: "update",
|
|
342
|
+
static: false,
|
|
343
|
+
private: false,
|
|
344
|
+
access: {
|
|
345
|
+
has: (obj) => "update" in obj,
|
|
346
|
+
get: (obj) => obj.update
|
|
347
|
+
},
|
|
348
|
+
metadata: _metadata
|
|
349
|
+
}, null, _instanceExtraInitializers);
|
|
350
|
+
__esDecorate(this, null, _replace_decorators, {
|
|
351
|
+
kind: "method",
|
|
352
|
+
name: "replace",
|
|
353
|
+
static: false,
|
|
354
|
+
private: false,
|
|
355
|
+
access: {
|
|
356
|
+
has: (obj) => "replace" in obj,
|
|
357
|
+
get: (obj) => obj.replace
|
|
358
|
+
},
|
|
359
|
+
metadata: _metadata
|
|
360
|
+
}, null, _instanceExtraInitializers);
|
|
361
|
+
__esDecorate(this, null, _mutate_decorators, {
|
|
362
|
+
kind: "method",
|
|
363
|
+
name: "mutate",
|
|
364
|
+
static: false,
|
|
365
|
+
private: false,
|
|
366
|
+
access: {
|
|
367
|
+
has: (obj) => "mutate" in obj,
|
|
368
|
+
get: (obj) => obj.mutate
|
|
369
|
+
},
|
|
370
|
+
metadata: _metadata
|
|
371
|
+
}, null, _instanceExtraInitializers);
|
|
372
|
+
__esDecorate(this, null, _openSettingsDocument_decorators, {
|
|
373
|
+
kind: "method",
|
|
374
|
+
name: "openSettingsDocument",
|
|
375
|
+
static: false,
|
|
376
|
+
private: false,
|
|
377
|
+
access: {
|
|
378
|
+
has: (obj) => "openSettingsDocument" in obj,
|
|
379
|
+
get: (obj) => obj.openSettingsDocument
|
|
380
|
+
},
|
|
381
|
+
metadata: _metadata
|
|
382
|
+
}, null, _instanceExtraInitializers);
|
|
383
|
+
__esDecorate(this, null, _openAgentPresetDirectory_decorators, {
|
|
384
|
+
kind: "method",
|
|
385
|
+
name: "openAgentPresetDirectory",
|
|
386
|
+
static: false,
|
|
387
|
+
private: false,
|
|
388
|
+
access: {
|
|
389
|
+
has: (obj) => "openAgentPresetDirectory" in obj,
|
|
390
|
+
get: (obj) => obj.openAgentPresetDirectory
|
|
391
|
+
},
|
|
392
|
+
metadata: _metadata
|
|
393
|
+
}, null, _instanceExtraInitializers);
|
|
394
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, {
|
|
395
|
+
enumerable: true,
|
|
396
|
+
configurable: true,
|
|
397
|
+
writable: true,
|
|
398
|
+
value: _metadata
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
static Config = Schema.object({ nativeOpen: Schema.boolean() });
|
|
402
|
+
openPath = __runInitializers(this, _instanceExtraInitializers);
|
|
403
|
+
openTextFile;
|
|
404
|
+
canOpenPath;
|
|
405
|
+
/**
|
|
406
|
+
* Register the settings namespace and mount the credentials namespace beside
|
|
407
|
+
* it. Both namespaces stay registered when a provider is absent so calls can
|
|
408
|
+
* return the configuration API's actionable missing-provider diagnostic.
|
|
409
|
+
* @param ctx - Host context where settings and credential providers may be mounted.
|
|
410
|
+
*/
|
|
411
|
+
constructor(ctx, config = {}, internals = {}) {
|
|
412
|
+
super(ctx, "settingsController", { namespace: "settings" });
|
|
413
|
+
this.openPath = internals.openPath ?? openNativePath;
|
|
414
|
+
this.openTextFile = internals.openTextFile ?? openNativeTextFile;
|
|
415
|
+
this.canOpenPath = internals.canOpenPath ?? (() => config.nativeOpen ?? (internals.openPath !== void 0 || canOpenNativePath()));
|
|
416
|
+
ctx.plugin(CredentialsController);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Describe every registered namespace for a configuration page: redacted
|
|
420
|
+
* layered values plus the serialized schema the page renders its form from.
|
|
421
|
+
* @returns provider writability, local-document presence, and one view per namespace.
|
|
422
|
+
* @throws RemoteError when no settings provider is mounted.
|
|
423
|
+
*/
|
|
424
|
+
describe() {
|
|
425
|
+
const settings = this.provider();
|
|
426
|
+
return {
|
|
427
|
+
writable: settings.writable,
|
|
428
|
+
hasDocument: settings.documentPath !== void 0,
|
|
429
|
+
namespaces: settings.describe({ redactSecrets: true }).map(namespaceView)
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Report whether this deployment can open an authored Agent preset directory natively.
|
|
434
|
+
* @returns true when the matching open operation is available.
|
|
435
|
+
*/
|
|
436
|
+
canOpenAgentPresetDirectory() {
|
|
437
|
+
return this.canOpenPath();
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Merge a patch into one namespace's stored user section.
|
|
441
|
+
* @param ns - namespace key to write.
|
|
442
|
+
* @param patch - fields to merge into the user section.
|
|
443
|
+
* @param expectedRevision - revision the caller read; `undefined` writes unconditionally.
|
|
444
|
+
* @returns the namespace's redacted view after the write.
|
|
445
|
+
* @throws RemoteError when the request is invalid, no provider is mounted, or the provider refuses the write.
|
|
446
|
+
*/
|
|
447
|
+
update(ns, patch, expectedRevision) {
|
|
448
|
+
return this.write(ns, "update", patch, expectedRevision);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Replace one namespace's stored user section wholesale.
|
|
452
|
+
* @param ns - namespace key to write.
|
|
453
|
+
* @param section - complete replacement user section.
|
|
454
|
+
* @param expectedRevision - revision the caller read; `undefined` writes unconditionally.
|
|
455
|
+
* @returns the namespace's redacted view after the write.
|
|
456
|
+
* @throws RemoteError when the request is invalid, no provider is mounted, or the provider refuses the write.
|
|
457
|
+
*/
|
|
458
|
+
replace(ns, section, expectedRevision) {
|
|
459
|
+
return this.write(ns, "replace", section, expectedRevision);
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Apply path-addressed edits to one namespace's user section, resolved against
|
|
463
|
+
* the section as stored rather than against whatever the caller last read,
|
|
464
|
+
* then answer with that namespace's new redacted view.
|
|
465
|
+
* @param ns - namespace key to write.
|
|
466
|
+
* @param ops - the edits to apply, in order.
|
|
467
|
+
* @param expectedRevision - revision the caller read; `undefined` writes unconditionally.
|
|
468
|
+
* @returns the namespace's redacted view after the write.
|
|
469
|
+
* @throws RemoteError when the request is invalid, no provider is mounted, or the provider refuses the write.
|
|
470
|
+
*/
|
|
471
|
+
async mutate(ns, ops, expectedRevision) {
|
|
472
|
+
return this.write(ns, "mutate", ops, expectedRevision);
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Materialize the provider-owned settings document and open it in a native text editor.
|
|
476
|
+
* @param signal - caller lifetime; abort terminates preparation or the native command.
|
|
477
|
+
* @returns confirmation after the native opener accepts the document.
|
|
478
|
+
* @throws RemoteError when no document exists, preparation fails, or opening fails.
|
|
479
|
+
*/
|
|
480
|
+
async openSettingsDocument(signal) {
|
|
481
|
+
const settings = this.provider();
|
|
482
|
+
if (isAborted(signal)) throw new RemoteError("gateway/cancelled", "settings document open was aborted", {});
|
|
483
|
+
let path;
|
|
484
|
+
try {
|
|
485
|
+
path = await settings.prepareDocument();
|
|
486
|
+
} catch (error) {
|
|
487
|
+
if (isAborted(signal)) throw new RemoteError("gateway/cancelled", "settings document preparation was aborted", {});
|
|
488
|
+
throw new RemoteError("gateway/internal", `settings document preparation failed: ${messageOf(error)}`, {}, { cause: error });
|
|
489
|
+
}
|
|
490
|
+
if (path === void 0) throw new RemoteError("gateway/internal", "settings provider has no local document to open", {});
|
|
491
|
+
if (isAborted(signal)) throw new RemoteError("gateway/cancelled", "settings document open was aborted", {});
|
|
492
|
+
try {
|
|
493
|
+
await this.openTextFile(path, signal);
|
|
494
|
+
return { opened: true };
|
|
495
|
+
} catch (error) {
|
|
496
|
+
if (isAborted(signal)) throw new RemoteError("gateway/cancelled", "settings document open was aborted", {});
|
|
497
|
+
throw new RemoteError("gateway/internal", `path open failed: ${messageOf(error)}`, {}, { cause: error });
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Open one user-authored Agent preset directory or return its path when no native opener exists.
|
|
502
|
+
* @param agentPreset - preset id resolved against Host-owned roots.
|
|
503
|
+
* @param signal - caller lifetime; abort terminates the native command.
|
|
504
|
+
* @returns an opened confirmation or the resolved directory for text display.
|
|
505
|
+
* @throws RemoteError when the preset is missing, read-only, invalid, or cannot be opened.
|
|
506
|
+
*/
|
|
507
|
+
async openAgentPresetDirectory(agentPreset, signal) {
|
|
508
|
+
if (agentPreset.length === 0) throw new RemoteError("gateway/bad-request", "agent preset id must not be empty", {});
|
|
509
|
+
const presets = this.ctx.get("agentPresets");
|
|
510
|
+
if (presets === void 0) throw new RemoteError("agent-preset/not-found", "this deployment composes no agent presets", {
|
|
511
|
+
agentPreset,
|
|
512
|
+
available: []
|
|
513
|
+
});
|
|
514
|
+
const preset = await presets.resolve(agentPreset);
|
|
515
|
+
if (preset.trust !== "user") throw new RemoteError("agent-preset/read-only", `agent-presets: preset "${preset.id}" cannot be written: it ships with the deployment`, {
|
|
516
|
+
agentPreset: preset.id,
|
|
517
|
+
reason: "it ships with the deployment"
|
|
518
|
+
});
|
|
519
|
+
const directory = dirname(preset.path);
|
|
520
|
+
if (!this.canOpenPath()) return {
|
|
521
|
+
opened: false,
|
|
522
|
+
path: directory
|
|
523
|
+
};
|
|
524
|
+
try {
|
|
525
|
+
await this.openPath(directory, signal);
|
|
526
|
+
return { opened: true };
|
|
527
|
+
} catch (error) {
|
|
528
|
+
if (signal.aborted) throw new RemoteError("gateway/cancelled", "path open was aborted", {});
|
|
529
|
+
throw new RemoteError("gateway/internal", `path open failed: ${messageOf(error)}`, {}, { cause: error });
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
async write(ns, mode, input, expectedRevision) {
|
|
533
|
+
const parsed = settingsNamespaceRequestSchema.safeParse({ ns });
|
|
534
|
+
if (!parsed.success) throw new RemoteError("gateway/bad-request", `invalid payload for settings.${mode}`, { issues: parsed.error.issues });
|
|
535
|
+
const settings = this.provider();
|
|
536
|
+
const namespace = parsed.data.ns;
|
|
537
|
+
try {
|
|
538
|
+
if (mode === "update") await settings.update(namespace, input, expectedRevision);
|
|
539
|
+
else if (mode === "replace") await settings.replace(namespace, input, expectedRevision);
|
|
540
|
+
else await settings.mutate(namespace, input, expectedRevision);
|
|
541
|
+
} catch (error) {
|
|
542
|
+
throw rejected(ns, error);
|
|
543
|
+
}
|
|
544
|
+
const descriptor = settings.describe({ redactSecrets: true }).find((candidate) => candidate.ns === namespace);
|
|
545
|
+
if (descriptor === void 0) throw new RemoteError("gateway/internal", `settings namespace "${ns}" was disposed after the ${mode}`, {});
|
|
546
|
+
return namespaceView(descriptor);
|
|
547
|
+
}
|
|
548
|
+
/** Resolve the optional provider or report how to supply it. */
|
|
549
|
+
provider() {
|
|
550
|
+
const settings = this.ctx.get("settings");
|
|
551
|
+
if (settings === void 0) throw new RemoteError("gateway/internal", "settings service is absent: this deployment does not mount a settings provider (e.g. @deepseek-ai/dsh-settings-file) in its composition", {});
|
|
552
|
+
return settings;
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
})();
|
|
556
|
+
function messageOf(error) {
|
|
557
|
+
return error instanceof Error ? error.message : String(error);
|
|
558
|
+
}
|
|
559
|
+
function settingsConflictOf(error) {
|
|
560
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
561
|
+
if (Reflect.get(error, "code") !== "SETTINGS_CONFLICT" || typeof Reflect.get(error, "message") !== "string" || typeof Reflect.get(error, "expected") !== "number" || typeof Reflect.get(error, "actual") !== "number") return void 0;
|
|
562
|
+
return error;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Classify one seam refusal. A stale writer is its own outcome, not a malformed
|
|
566
|
+
* request: the client must re-read and re-apply rather than treat the write as
|
|
567
|
+
* invalid.
|
|
568
|
+
* @param ns - the namespace the write addressed.
|
|
569
|
+
* @param error - whatever the seam threw.
|
|
570
|
+
* @returns the failure to raise for that refusal.
|
|
571
|
+
*/
|
|
572
|
+
function rejected(ns, error) {
|
|
573
|
+
const conflict = settingsConflictOf(error);
|
|
574
|
+
if (conflict !== void 0) return new RemoteError("settings/conflict", conflict.message, {
|
|
575
|
+
ns,
|
|
576
|
+
expected: conflict.expected,
|
|
577
|
+
actual: conflict.actual
|
|
578
|
+
}, { cause: error });
|
|
579
|
+
return new RemoteError("settings/rejected", messageOf(error), { ns }, { cause: error });
|
|
580
|
+
}
|
|
581
|
+
//#endregion
|
|
582
|
+
export { CredentialsController, SettingsController, SettingsController as default };
|
package/lib/invariant.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region lib/types/invariant.js
|
|
2
|
+
/** Package-owned invariant companion. @module @deepseek-ai/dsh-api-settings-controller/invariant */
|
|
3
|
+
const PACKAGE_NAME = "@deepseek-ai/dsh-api-settings-controller";
|
|
4
|
+
/** Cordis companion plugin name. */
|
|
5
|
+
const name = "api-settings-controller-invariant";
|
|
6
|
+
/** Service required before the companion can reserve package ownership. */
|
|
7
|
+
const inject = ["invariants"];
|
|
8
|
+
/**
|
|
9
|
+
* No runtime invariant: the settings and credential seams own storage and
|
|
10
|
+
* update events, while this package only projects their methods onto the wire.
|
|
11
|
+
*/
|
|
12
|
+
const install = () => {};
|
|
13
|
+
/** Register this package's invariant companion. */
|
|
14
|
+
const apply = (ctx) => Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install));
|
|
15
|
+
//#endregion
|
|
16
|
+
export { apply, inject, name };
|