@aigne/afs-cli 1.11.0-beta.7 → 1.11.0-beta.9
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/dist/_virtual/rolldown_runtime.mjs +7 -0
- package/dist/config/afs-loader.cjs +466 -22
- package/dist/config/afs-loader.d.cts +6 -1
- package/dist/config/afs-loader.d.cts.map +1 -1
- package/dist/config/afs-loader.d.mts +6 -1
- package/dist/config/afs-loader.d.mts.map +1 -1
- package/dist/config/afs-loader.mjs +465 -22
- package/dist/config/afs-loader.mjs.map +1 -1
- package/dist/config/loader.cjs +28 -11
- package/dist/config/loader.mjs +28 -11
- package/dist/config/loader.mjs.map +1 -1
- package/dist/config/mount-commands.cjs +96 -46
- package/dist/config/mount-commands.d.cts +0 -6
- package/dist/config/mount-commands.d.cts.map +1 -1
- package/dist/config/mount-commands.d.mts +0 -6
- package/dist/config/mount-commands.d.mts.map +1 -1
- package/dist/config/mount-commands.mjs +93 -45
- package/dist/config/mount-commands.mjs.map +1 -1
- package/dist/config/schema.cjs +2 -2
- package/dist/config/schema.mjs +2 -2
- package/dist/config/schema.mjs.map +1 -1
- package/dist/core/commands/exec.cjs +6 -3
- package/dist/core/commands/exec.d.cts.map +1 -1
- package/dist/core/commands/exec.d.mts.map +1 -1
- package/dist/core/commands/exec.mjs +6 -3
- package/dist/core/commands/exec.mjs.map +1 -1
- package/dist/core/commands/explain.cjs +1 -1
- package/dist/core/commands/explain.mjs +1 -1
- package/dist/core/commands/mount.cjs +106 -23
- package/dist/core/commands/mount.d.cts +2 -0
- package/dist/core/commands/mount.d.cts.map +1 -1
- package/dist/core/commands/mount.d.mts +2 -0
- package/dist/core/commands/mount.d.mts.map +1 -1
- package/dist/core/commands/mount.mjs +106 -23
- package/dist/core/commands/mount.mjs.map +1 -1
- package/dist/core/commands/serve.cjs +38 -13
- package/dist/core/commands/serve.mjs +38 -13
- package/dist/core/commands/serve.mjs.map +1 -1
- package/dist/core/commands/types.cjs +6 -1
- package/dist/core/commands/types.d.cts.map +1 -1
- package/dist/core/commands/types.d.mts.map +1 -1
- package/dist/core/commands/types.mjs +6 -1
- package/dist/core/commands/types.mjs.map +1 -1
- package/dist/credential/auth-server.cjs +247 -0
- package/dist/credential/auth-server.mjs +247 -0
- package/dist/credential/auth-server.mjs.map +1 -0
- package/dist/credential/cli-auth-context.cjs +86 -0
- package/dist/credential/cli-auth-context.d.mts +1 -0
- package/dist/credential/cli-auth-context.mjs +86 -0
- package/dist/credential/cli-auth-context.mjs.map +1 -0
- package/dist/credential/index.cjs +5 -0
- package/dist/credential/index.d.mts +4 -0
- package/dist/credential/index.mjs +7 -0
- package/dist/credential/mcp-auth-context.cjs +186 -0
- package/dist/credential/mcp-auth-context.d.mts +1 -0
- package/dist/credential/mcp-auth-context.mjs +186 -0
- package/dist/credential/mcp-auth-context.mjs.map +1 -0
- package/dist/credential/resolver.cjs +125 -0
- package/dist/credential/resolver.d.mts +1 -0
- package/dist/credential/resolver.mjs +125 -0
- package/dist/credential/resolver.mjs.map +1 -0
- package/dist/credential/store.cjs +106 -0
- package/dist/credential/store.d.cts +30 -0
- package/dist/credential/store.d.cts.map +1 -0
- package/dist/credential/store.d.mts +30 -0
- package/dist/credential/store.d.mts.map +1 -0
- package/dist/credential/store.mjs +106 -0
- package/dist/credential/store.mjs.map +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +3 -1
- package/dist/mcp/http-transport.cjs +22 -3
- package/dist/mcp/http-transport.mjs +22 -3
- package/dist/mcp/http-transport.mjs.map +1 -1
- package/dist/mcp/prompts.cjs +2 -2
- package/dist/mcp/prompts.mjs +2 -2
- package/dist/mcp/prompts.mjs.map +1 -1
- package/dist/mcp/server.cjs +16 -6
- package/dist/mcp/server.mjs +15 -6
- package/dist/mcp/server.mjs.map +1 -1
- package/dist/mcp/tools.cjs +2 -46
- package/dist/mcp/tools.mjs +2 -46
- package/dist/mcp/tools.mjs.map +1 -1
- package/dist/repl.cjs +9 -3
- package/dist/repl.d.cts.map +1 -1
- package/dist/repl.d.mts.map +1 -1
- package/dist/repl.mjs +9 -3
- package/dist/repl.mjs.map +1 -1
- package/package.json +22 -22
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
2
|
const require_loader = require('./loader.cjs');
|
|
3
|
+
const require_mount_commands = require('./mount-commands.cjs');
|
|
3
4
|
let _aigne_afs = require("@aigne/afs");
|
|
4
|
-
let
|
|
5
|
+
let _aigne_afs_utils_uri = require("@aigne/afs/utils/uri");
|
|
5
6
|
|
|
6
7
|
//#region src/config/afs-loader.ts
|
|
7
8
|
/**
|
|
8
|
-
*
|
|
9
|
+
* Register the workspace:// scheme on a ProviderRegistry.
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
* createAFS()
|
|
12
|
-
*
|
|
11
|
+
* Extracted so that both createAFS() and verifyMount() can support
|
|
12
|
+
* workspace URIs. createAFS() overrides this with a richer variant
|
|
13
|
+
* that includes credential resolution.
|
|
13
14
|
*/
|
|
15
|
+
function registerWorkspaceFactory(registry) {
|
|
16
|
+
registry.register("workspace", async (mount, parsed) => {
|
|
17
|
+
const mod = await import("@aigne/afs-workspace");
|
|
18
|
+
const AFSWorkspace = mod.AFSWorkspace ?? mod.default;
|
|
19
|
+
if (!AFSWorkspace) throw new Error("workspace:// scheme requires @aigne/afs-workspace package. Install it with: pnpm add @aigne/afs-workspace");
|
|
20
|
+
return new AFSWorkspace({
|
|
21
|
+
workspacePath: parsed.body,
|
|
22
|
+
registry,
|
|
23
|
+
name: mount.path.slice(1).replace(/\//g, "-") || "workspace",
|
|
24
|
+
description: mount.description,
|
|
25
|
+
accessMode: mount.access_mode,
|
|
26
|
+
...mount.options
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
14
30
|
let cached;
|
|
15
31
|
/**
|
|
16
32
|
* Load AFS with caching - first call creates, subsequent calls return cached instance
|
|
@@ -33,40 +49,132 @@ async function loadAFS(cwd, options) {
|
|
|
33
49
|
* - If no mounts configured, returns empty AFS (no error)
|
|
34
50
|
*/
|
|
35
51
|
async function createAFS(cwd, options) {
|
|
36
|
-
const config = await new require_loader.ConfigLoader().
|
|
52
|
+
const { config, mountSources } = await new require_loader.ConfigLoader().loadWithSources(cwd);
|
|
37
53
|
const afs = new _aigne_afs.AFS();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
};
|
|
42
|
-
const registry = (0, _aigne_afs_provider_registry.createProviderRegistry)();
|
|
54
|
+
const authContext = options?.authContext;
|
|
55
|
+
const credentialStore = options?.credentialStore;
|
|
56
|
+
const registry = new _aigne_afs.ProviderRegistry();
|
|
43
57
|
registry.register("workspace", async (mount, parsed) => {
|
|
44
58
|
const mod = await import("@aigne/afs-workspace");
|
|
45
59
|
const AFSWorkspace = mod.AFSWorkspace ?? mod.default;
|
|
46
60
|
if (!AFSWorkspace) throw new Error("workspace:// scheme requires @aigne/afs-workspace package. Install it with: pnpm add @aigne/afs-workspace");
|
|
61
|
+
const { resolve } = await import("node:path");
|
|
62
|
+
resolve(parsed.body);
|
|
47
63
|
return new AFSWorkspace({
|
|
48
|
-
workspacePath: parsed.
|
|
64
|
+
workspacePath: parsed.body,
|
|
49
65
|
registry,
|
|
66
|
+
createProvider: async (subMount) => {
|
|
67
|
+
const credResult = await resolveAndMergeCredentials(subMount, authContext, credentialStore, registry);
|
|
68
|
+
const provider = await registry.createProvider(subMount);
|
|
69
|
+
if (credResult) {
|
|
70
|
+
await persistCredentialResult(subMount, credResult);
|
|
71
|
+
const opts = subMount.options ?? {};
|
|
72
|
+
for (const key of Object.keys(credResult.sensitive)) delete opts[key];
|
|
73
|
+
subMount.options = Object.keys(opts).length > 0 ? opts : void 0;
|
|
74
|
+
if (Object.keys(credResult.nonSensitive).length > 0) subMount.options = {
|
|
75
|
+
...subMount.options ?? {},
|
|
76
|
+
...credResult.nonSensitive
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return provider;
|
|
80
|
+
},
|
|
50
81
|
name: mount.path.slice(1).replace(/\//g, "-") || "workspace",
|
|
51
82
|
description: mount.description,
|
|
52
83
|
accessMode: mount.access_mode,
|
|
53
84
|
...mount.options
|
|
54
85
|
});
|
|
55
86
|
});
|
|
56
|
-
afs.loadProvider = async (uri, mountPath) => {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
87
|
+
afs.loadProvider = async (uri, mountPath, options$1) => {
|
|
88
|
+
(0, _aigne_afs_utils_uri.parseURI)(uri);
|
|
89
|
+
const { cleanUri: loadConfigUri, envRecord: loadEnvRecord } = extractEnvFromURI(uri);
|
|
90
|
+
const hasLoadEnv = Object.keys(loadEnvRecord).length > 0;
|
|
91
|
+
const { accessMode, auth, description, scope, ...providerOptions } = options$1 ?? {};
|
|
92
|
+
if (hasLoadEnv) providerOptions.env = {
|
|
93
|
+
...providerOptions.env ?? {},
|
|
94
|
+
...loadEnvRecord
|
|
95
|
+
};
|
|
96
|
+
const mount = {
|
|
60
97
|
uri,
|
|
61
|
-
path: mountPath
|
|
62
|
-
|
|
63
|
-
|
|
98
|
+
path: mountPath,
|
|
99
|
+
access_mode: accessMode ?? void 0,
|
|
100
|
+
auth: auth ?? void 0,
|
|
101
|
+
description: description ?? void 0,
|
|
102
|
+
options: Object.keys(providerOptions).length > 0 ? providerOptions : void 0
|
|
103
|
+
};
|
|
104
|
+
const persistScope = scope || "cwd";
|
|
105
|
+
let credResult = await resolveAndMergeCredentials(mount, authContext, credentialStore, registry);
|
|
106
|
+
try {
|
|
107
|
+
const provider = await registry.createProvider(mount);
|
|
108
|
+
await afs.mount(provider, mountPath);
|
|
109
|
+
} catch (mountError) {
|
|
110
|
+
if (credResult === null && authContext) {
|
|
111
|
+
const retryProviderOptions = { ...providerOptions };
|
|
112
|
+
const retryMount = {
|
|
113
|
+
uri,
|
|
114
|
+
path: mountPath,
|
|
115
|
+
access_mode: accessMode ?? void 0,
|
|
116
|
+
auth: auth ?? void 0,
|
|
117
|
+
description: description ?? void 0,
|
|
118
|
+
options: Object.keys(retryProviderOptions).length > 0 ? retryProviderOptions : void 0
|
|
119
|
+
};
|
|
120
|
+
credResult = await resolveAndMergeCredentials(retryMount, authContext, credentialStore, registry, { forceCollect: true });
|
|
121
|
+
const retryProvider = await registry.createProvider(retryMount);
|
|
122
|
+
await afs.mount(retryProvider, mountPath);
|
|
123
|
+
Object.assign(mount, retryMount);
|
|
124
|
+
} else throw mountError;
|
|
125
|
+
}
|
|
126
|
+
if (credResult) await persistCredentialResult(mount, credResult);
|
|
127
|
+
if (hasLoadEnv && credentialStore) try {
|
|
128
|
+
const envCreds = {};
|
|
129
|
+
for (const [k, v] of Object.entries(loadEnvRecord)) envCreds[`env:${k}`] = v;
|
|
130
|
+
const existing = credResult ? { ...credResult.sensitive } : {};
|
|
131
|
+
await credentialStore.set(loadConfigUri, {
|
|
132
|
+
...existing,
|
|
133
|
+
...envCreds
|
|
134
|
+
});
|
|
135
|
+
} catch (err) {
|
|
136
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
137
|
+
console.warn(`[mount] env credential persistence failed: ${msg}`);
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const entry = {
|
|
141
|
+
path: mountPath,
|
|
142
|
+
uri: hasLoadEnv ? loadConfigUri : mount.uri
|
|
143
|
+
};
|
|
144
|
+
if (description) entry.description = description;
|
|
145
|
+
if (accessMode) entry.access_mode = accessMode;
|
|
146
|
+
if (auth) entry.auth = auth;
|
|
147
|
+
const mergedOptions = {
|
|
148
|
+
...providerOptions,
|
|
149
|
+
...credResult?.nonSensitive
|
|
150
|
+
};
|
|
151
|
+
if (hasLoadEnv) delete mergedOptions.env;
|
|
152
|
+
if (Object.keys(mergedOptions).length > 0) entry.options = mergedOptions;
|
|
153
|
+
await require_mount_commands.persistMount(cwd, entry, persistScope);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
156
|
+
console.warn(`[mount] config persistence failed: ${msg}`);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
afs.unloadProvider = async (mountPath, options$1) => {
|
|
160
|
+
try {
|
|
161
|
+
await require_mount_commands.unpersistMount(cwd, mountPath, options$1?.scope || void 0);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
164
|
+
console.warn(`[unmount] config removal failed: ${msg}`);
|
|
165
|
+
}
|
|
64
166
|
};
|
|
65
167
|
if (config.registry?.enabled !== false) try {
|
|
66
168
|
const { AFSRegistry } = await import("@aigne/afs-registry");
|
|
67
|
-
const
|
|
68
|
-
await afs.mount(
|
|
169
|
+
const officialRegistry = new AFSRegistry(config.registry?.providers?.length ? { providers: config.registry.providers } : { url: "https://raw.githubusercontent.com/ArcBlock/afs-registry/refs/heads/main/providers.json" });
|
|
170
|
+
await afs.mount(officialRegistry, "/registry/official");
|
|
171
|
+
const internalRegistry = new AFSRegistry();
|
|
172
|
+
await afs.mount(internalRegistry, "/registry/internal");
|
|
69
173
|
} catch {}
|
|
174
|
+
if (config.mounts.length === 0) return {
|
|
175
|
+
afs,
|
|
176
|
+
failures: []
|
|
177
|
+
};
|
|
70
178
|
const total = config.mounts.length;
|
|
71
179
|
let completed = 0;
|
|
72
180
|
let failedCount = 0;
|
|
@@ -75,6 +183,7 @@ async function createAFS(cwd, options) {
|
|
|
75
183
|
completed: 0,
|
|
76
184
|
failed: 0
|
|
77
185
|
});
|
|
186
|
+
if (credentialStore && mountSources.size > 0) await mergeStoredCredentials(config.mounts, mountSources, credentialStore);
|
|
78
187
|
const results = await Promise.allSettled(config.mounts.map(async (mount) => {
|
|
79
188
|
const provider = await registry.createProvider(mount);
|
|
80
189
|
await afs.mount(provider, mount.path, { namespace: mount.namespace ?? null });
|
|
@@ -117,7 +226,342 @@ async function createAFS(cwd, options) {
|
|
|
117
226
|
failures
|
|
118
227
|
};
|
|
119
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Extract env query params from MCP URIs for secure credential storage.
|
|
231
|
+
*
|
|
232
|
+
* MCP servers receive secrets via env vars (e.g., `mcp+stdio://npx?env=API_KEY=sk-xxx`).
|
|
233
|
+
* This function extracts env params from the URI so they can be stored in credentials.toml
|
|
234
|
+
* instead of being persisted in plaintext in config.toml.
|
|
235
|
+
*
|
|
236
|
+
* Only applies to MCP schemes (mcp://, mcp+stdio://, mcp+sse://).
|
|
237
|
+
* Non-MCP URIs are returned unchanged.
|
|
238
|
+
*/
|
|
239
|
+
function extractEnvFromURI(uri) {
|
|
240
|
+
const parsed = (0, _aigne_afs_utils_uri.parseURI)(uri);
|
|
241
|
+
const envRecord = {};
|
|
242
|
+
if (!parsed.scheme.startsWith("mcp")) return {
|
|
243
|
+
cleanUri: uri,
|
|
244
|
+
envRecord
|
|
245
|
+
};
|
|
246
|
+
const envValues = parsed.query.env;
|
|
247
|
+
if (!envValues) return {
|
|
248
|
+
cleanUri: uri,
|
|
249
|
+
envRecord
|
|
250
|
+
};
|
|
251
|
+
const envList = Array.isArray(envValues) ? envValues : [envValues];
|
|
252
|
+
for (const entry of envList) {
|
|
253
|
+
const eqIdx = entry.indexOf("=");
|
|
254
|
+
if (eqIdx > 0) envRecord[entry.slice(0, eqIdx)] = entry.slice(eqIdx + 1);
|
|
255
|
+
}
|
|
256
|
+
const queryIndex = uri.indexOf("?");
|
|
257
|
+
if (queryIndex < 0) return {
|
|
258
|
+
cleanUri: uri,
|
|
259
|
+
envRecord
|
|
260
|
+
};
|
|
261
|
+
const rawQuery = uri.slice(queryIndex + 1);
|
|
262
|
+
const params = new URLSearchParams(rawQuery);
|
|
263
|
+
params.delete("env");
|
|
264
|
+
const newQuery = params.toString();
|
|
265
|
+
const base = uri.slice(0, queryIndex);
|
|
266
|
+
return {
|
|
267
|
+
cleanUri: newQuery ? `${base}?${newQuery}` : base,
|
|
268
|
+
envRecord
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Extract template variables from mount.uri using the manifest's uriTemplate
|
|
273
|
+
* and merge them into mount.options so the credential resolver sees them as "known".
|
|
274
|
+
*
|
|
275
|
+
* Example: cloudflare://d9e5fca3... + template "cloudflare://{accountId}"
|
|
276
|
+
* → mount.options.accountId = "d9e5fca3..."
|
|
277
|
+
*/
|
|
278
|
+
function mergeTemplateVarsIntoMount(mount, manifest) {
|
|
279
|
+
if (!manifest?.uriTemplate) return;
|
|
280
|
+
const { parseTemplate } = require("@aigne/afs/utils/uri-template");
|
|
281
|
+
const parsed = (0, _aigne_afs_utils_uri.parseURI)(mount.uri);
|
|
282
|
+
let templateVars;
|
|
283
|
+
try {
|
|
284
|
+
templateVars = parseTemplate(manifest.uriTemplate, parsed.body);
|
|
285
|
+
} catch {
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
for (const [key, value] of Object.entries(templateVars)) if (value !== void 0) {
|
|
289
|
+
if (!mount.options) mount.options = {};
|
|
290
|
+
if (mount.options[key] === void 0) mount.options[key] = value;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* After credential resolution, rebuild mount.uri from the template if the
|
|
295
|
+
* current URI body is empty/incomplete and resolved options can fill template vars.
|
|
296
|
+
*
|
|
297
|
+
* Example: mount.uri = "cloudflare://" (empty body), mount.options.accountId = "abc"
|
|
298
|
+
* → mount.uri = "cloudflare://abc"
|
|
299
|
+
*/
|
|
300
|
+
function rebuildURIFromTemplate(mount, manifest) {
|
|
301
|
+
if (!manifest?.uriTemplate) return;
|
|
302
|
+
const { buildURI, getTemplateVariableNames } = require("@aigne/afs/utils/uri-template");
|
|
303
|
+
const varNames = getTemplateVariableNames(manifest.uriTemplate);
|
|
304
|
+
if (varNames.length === 0) return;
|
|
305
|
+
const parsed = (0, _aigne_afs_utils_uri.parseURI)(mount.uri);
|
|
306
|
+
const { parseTemplate } = require("@aigne/afs/utils/uri-template");
|
|
307
|
+
let existingVars;
|
|
308
|
+
try {
|
|
309
|
+
existingVars = parseTemplate(manifest.uriTemplate, parsed.body);
|
|
310
|
+
} catch {
|
|
311
|
+
existingVars = {};
|
|
312
|
+
}
|
|
313
|
+
const allVars = { ...existingVars };
|
|
314
|
+
for (const name of varNames) if (!allVars[name] && mount.options?.[name] != null) allVars[name] = String(mount.options[name]);
|
|
315
|
+
try {
|
|
316
|
+
const newURI = buildURI(manifest.uriTemplate, allVars);
|
|
317
|
+
if (newURI !== mount.uri) mount.uri = newURI;
|
|
318
|
+
} catch {}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Attempt credential resolution for a mount, merging resolved values into mount.options.
|
|
322
|
+
*
|
|
323
|
+
* Returns the credential result if any fields were collected interactively,
|
|
324
|
+
* or null if no interactive collection was needed (or no schema/authContext).
|
|
325
|
+
*
|
|
326
|
+
* This function mutates mount.options and mount.auth/mount.token when credentials
|
|
327
|
+
* are resolved, so the subsequent registry.createProvider(mount) receives complete values.
|
|
328
|
+
*/
|
|
329
|
+
async function resolveAndMergeCredentials(mount, authContext, credentialStore, registry, opts) {
|
|
330
|
+
const info = await registry.getProviderInfo(mount.uri);
|
|
331
|
+
const schema = info?.schema ?? null;
|
|
332
|
+
const providerAuth = info?.auth;
|
|
333
|
+
if (!schema) return null;
|
|
334
|
+
mergeTemplateVarsIntoMount(mount, info?.manifest);
|
|
335
|
+
const { getSensitiveFields } = await import("@aigne/afs/utils/schema");
|
|
336
|
+
const sensitiveFieldsInSchema = getSensitiveFields(schema);
|
|
337
|
+
const schemaProps = schema.properties ?? {};
|
|
338
|
+
const hasEnvFields = Object.values(schemaProps).some((p) => Array.isArray(p?.env));
|
|
339
|
+
if (sensitiveFieldsInSchema.length === 0 && !hasEnvFields && !providerAuth) return null;
|
|
340
|
+
const { resolveCredentials } = await Promise.resolve().then(() => require("../credential/resolver.cjs"));
|
|
341
|
+
const result = await resolveCredentials({
|
|
342
|
+
mount,
|
|
343
|
+
schema,
|
|
344
|
+
authContext,
|
|
345
|
+
credentialStore,
|
|
346
|
+
providerAuth,
|
|
347
|
+
forceCollect: opts?.forceCollect
|
|
348
|
+
});
|
|
349
|
+
if (!result) {
|
|
350
|
+
const fieldNames = Object.keys(schema.properties ?? {});
|
|
351
|
+
const known = /* @__PURE__ */ new Set();
|
|
352
|
+
if (mount.auth !== void 0) known.add("auth");
|
|
353
|
+
if (mount.token !== void 0) known.add("token");
|
|
354
|
+
if (mount.options) for (const k of Object.keys(mount.options)) known.add(k);
|
|
355
|
+
const missing = fieldNames.filter((f) => !known.has(f));
|
|
356
|
+
const fieldList = missing.length > 0 ? missing.join(", ") : fieldNames.join(", ");
|
|
357
|
+
throw new Error(`Missing credentials: ${fieldList}. Retry with them as args, e.g. { "uri": "${mount.uri}", "path": "${mount.path}", ${missing.map((f) => `"${f}": "..."`).join(", ")} }`);
|
|
358
|
+
}
|
|
359
|
+
if (Object.keys(result.values).length > 0) {
|
|
360
|
+
if (result.values.token !== void 0 && mount.token === void 0) mount.token = String(result.values.token);
|
|
361
|
+
if (result.values.auth !== void 0 && mount.auth === void 0) mount.auth = String(result.values.auth);
|
|
362
|
+
const opts$1 = mount.options ?? {};
|
|
363
|
+
for (const [key, value] of Object.entries(result.values)) if (key !== "token" && key !== "auth" && opts$1[key] === void 0) opts$1[key] = value;
|
|
364
|
+
if (Object.keys(opts$1).length > 0) mount.options = opts$1;
|
|
365
|
+
}
|
|
366
|
+
rebuildURIFromTemplate(mount, info?.manifest);
|
|
367
|
+
return result.collected ? result : null;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Persist credential resolution result (sensitive → credentials.toml, non-sensitive → config).
|
|
371
|
+
*/
|
|
372
|
+
async function persistCredentialResult(mount, result) {
|
|
373
|
+
if (Object.keys(result.sensitive).length > 0) try {
|
|
374
|
+
const { createCredentialStore } = await Promise.resolve().then(() => require("../credential/store.cjs"));
|
|
375
|
+
await createCredentialStore().set(mount.uri, result.sensitive);
|
|
376
|
+
} catch (err) {
|
|
377
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
378
|
+
console.warn(`[mount] credential persistence failed: ${msg}`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* Load stored credentials and merge into mount options during startup.
|
|
383
|
+
*
|
|
384
|
+
* Credentials are keyed by URI (the resource identity), not mount path.
|
|
385
|
+
*/
|
|
386
|
+
async function mergeStoredCredentials(mounts, _mountSources, store) {
|
|
387
|
+
for (const mount of mounts) try {
|
|
388
|
+
const stored = await store.get(mount.uri);
|
|
389
|
+
if (stored) {
|
|
390
|
+
const opts = mount.options ?? {};
|
|
391
|
+
for (const [key, value] of Object.entries(stored)) if (key.startsWith("env:")) {
|
|
392
|
+
if (!opts.env) opts.env = {};
|
|
393
|
+
opts.env[key.slice(4)] = value;
|
|
394
|
+
} else if (opts[key] === void 0) opts[key] = value;
|
|
395
|
+
if (Object.keys(opts).length > 0) mount.options = opts;
|
|
396
|
+
}
|
|
397
|
+
} catch {}
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Resolve and persist credentials for a mount configuration.
|
|
401
|
+
*
|
|
402
|
+
* Used by `mount add` CLI command to trigger credential collection
|
|
403
|
+
* at add-time rather than deferring to AFS creation.
|
|
404
|
+
*
|
|
405
|
+
* - Gets provider schema via registry
|
|
406
|
+
* - Runs 4-step credential resolution
|
|
407
|
+
* - Persists sensitive values to credentials.toml
|
|
408
|
+
* - Returns non-sensitive values for caller to update config
|
|
409
|
+
*
|
|
410
|
+
* Returns null if no schema found or no credentials needed.
|
|
411
|
+
* Throws if user cancels collection when credentials are required.
|
|
412
|
+
*/
|
|
413
|
+
async function resolveCredentialsForMount(options) {
|
|
414
|
+
const { uri, mountPath, authContext, credentialStore, extraOptions, sensitiveArgs } = options;
|
|
415
|
+
const { cleanUri: configUri, envRecord } = extractEnvFromURI(uri);
|
|
416
|
+
const hasExtractedEnv = Object.keys(envRecord).length > 0;
|
|
417
|
+
const info = await (options.registry ?? new _aigne_afs.ProviderRegistry()).getProviderInfo(uri);
|
|
418
|
+
let schema = info?.schema ?? null;
|
|
419
|
+
const providerAuth = info?.auth;
|
|
420
|
+
if (!schema && extraOptions && Object.keys(extraOptions).length > 0) {
|
|
421
|
+
const { buildAdHocSchema } = await import("@aigne/afs/utils/schema");
|
|
422
|
+
schema = buildAdHocSchema(extraOptions, sensitiveArgs ?? []);
|
|
423
|
+
}
|
|
424
|
+
const envOnlyResult = () => {
|
|
425
|
+
if (!hasExtractedEnv) return null;
|
|
426
|
+
return {
|
|
427
|
+
collected: false,
|
|
428
|
+
nonSensitive: {},
|
|
429
|
+
allValues: {},
|
|
430
|
+
persistCredentials: async () => {
|
|
431
|
+
const toStore = {};
|
|
432
|
+
for (const [key, val] of Object.entries(envRecord)) toStore[`env:${key}`] = val;
|
|
433
|
+
if (credentialStore) try {
|
|
434
|
+
await credentialStore.set(configUri, toStore);
|
|
435
|
+
} catch (err) {
|
|
436
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
437
|
+
console.warn(`[mount add] credential persistence failed: ${msg}`);
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
sensitiveFields: [],
|
|
441
|
+
configUri
|
|
442
|
+
};
|
|
443
|
+
};
|
|
444
|
+
if (!schema) return envOnlyResult();
|
|
445
|
+
const properties = schema.properties;
|
|
446
|
+
if (!properties || Object.keys(properties).length === 0) return envOnlyResult();
|
|
447
|
+
if (sensitiveArgs && sensitiveArgs.length > 0) {
|
|
448
|
+
const mergedProps = { ...properties };
|
|
449
|
+
let changed = false;
|
|
450
|
+
for (const field of sensitiveArgs) if (mergedProps[field]) {
|
|
451
|
+
mergedProps[field] = {
|
|
452
|
+
...mergedProps[field],
|
|
453
|
+
sensitive: true
|
|
454
|
+
};
|
|
455
|
+
changed = true;
|
|
456
|
+
} else if (extraOptions?.[field] !== void 0) {
|
|
457
|
+
const value = extraOptions[field];
|
|
458
|
+
mergedProps[field] = {
|
|
459
|
+
type: typeof value === "number" ? "number" : typeof value === "boolean" ? "boolean" : "string",
|
|
460
|
+
sensitive: true
|
|
461
|
+
};
|
|
462
|
+
changed = true;
|
|
463
|
+
}
|
|
464
|
+
if (changed) schema = {
|
|
465
|
+
...schema,
|
|
466
|
+
properties: mergedProps
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
const { getSensitiveFields } = await import("@aigne/afs/utils/schema");
|
|
470
|
+
const sensitiveFieldsInSchema = getSensitiveFields(schema);
|
|
471
|
+
const schemaProps = schema.properties;
|
|
472
|
+
const hasEnvFields = Object.values(schemaProps).some((p) => Array.isArray(p?.env));
|
|
473
|
+
if (sensitiveFieldsInSchema.length === 0 && !hasEnvFields && !extraOptions) return envOnlyResult();
|
|
474
|
+
const mount = {
|
|
475
|
+
uri,
|
|
476
|
+
path: mountPath
|
|
477
|
+
};
|
|
478
|
+
if (extraOptions && Object.keys(extraOptions).length > 0) mount.options = {
|
|
479
|
+
...mount.options ?? {},
|
|
480
|
+
...extraOptions
|
|
481
|
+
};
|
|
482
|
+
mergeTemplateVarsIntoMount(mount, info?.manifest);
|
|
483
|
+
const { resolveCredentials } = await Promise.resolve().then(() => require("../credential/resolver.cjs"));
|
|
484
|
+
const result = await resolveCredentials({
|
|
485
|
+
mount,
|
|
486
|
+
schema,
|
|
487
|
+
authContext,
|
|
488
|
+
credentialStore,
|
|
489
|
+
providerAuth,
|
|
490
|
+
forceCollect: options.forceCollect
|
|
491
|
+
});
|
|
492
|
+
if (!result) {
|
|
493
|
+
const fieldNames = Object.keys(properties);
|
|
494
|
+
throw new Error(`Missing credentials: ${fieldNames.join(", ")}. Retry with them as args, e.g. { "uri": "${uri}", "path": "${mountPath}", ${fieldNames.map((f) => `"${f}": "..."`).join(", ")} }`);
|
|
495
|
+
}
|
|
496
|
+
const sensitiveFieldSet = new Set(sensitiveFieldsInSchema);
|
|
497
|
+
const flatSensitive = {};
|
|
498
|
+
for (const field of sensitiveFieldsInSchema) {
|
|
499
|
+
const val = result.values[field];
|
|
500
|
+
if (val === void 0) continue;
|
|
501
|
+
if (field === "env" && typeof val === "object" && val !== null) for (const [envKey, envVal] of Object.entries(val)) flatSensitive[`env:${envKey}`] = String(envVal);
|
|
502
|
+
else flatSensitive[field] = String(val);
|
|
503
|
+
}
|
|
504
|
+
const nonSensitive = result.collected ? result.nonSensitive : extraOptions ? Object.fromEntries(Object.entries(extraOptions).filter(([k]) => !sensitiveFieldSet.has(k))) : {};
|
|
505
|
+
const persistCredentials = async () => {
|
|
506
|
+
const toStore = { ...flatSensitive };
|
|
507
|
+
for (const [key, val] of Object.entries(envRecord)) toStore[`env:${key}`] = val;
|
|
508
|
+
if (Object.keys(toStore).length > 0 && credentialStore) try {
|
|
509
|
+
await credentialStore.set(configUri, toStore);
|
|
510
|
+
} catch (err) {
|
|
511
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
512
|
+
console.warn(`[mount add] credential persistence failed: ${msg}`);
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
return {
|
|
516
|
+
collected: result.collected,
|
|
517
|
+
nonSensitive,
|
|
518
|
+
allValues: result.values,
|
|
519
|
+
persistCredentials,
|
|
520
|
+
sensitiveFields: sensitiveFieldsInSchema,
|
|
521
|
+
configUri: hasExtractedEnv ? configUri : void 0
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Verify that a mount configuration produces a working provider.
|
|
526
|
+
*
|
|
527
|
+
* Creates the provider and mounts it on a temporary AFS instance,
|
|
528
|
+
* which triggers the built-in checkProviderOnMount (stat + data validation + list).
|
|
529
|
+
* Throws if the mount check fails.
|
|
530
|
+
*
|
|
531
|
+
* @param uri - Provider URI
|
|
532
|
+
* @param mountPath - Mount path
|
|
533
|
+
* @param options - Merged options (non-sensitive + sensitive) for provider creation
|
|
534
|
+
*/
|
|
535
|
+
async function verifyMount(uri, mountPath, options) {
|
|
536
|
+
const mount = {
|
|
537
|
+
uri,
|
|
538
|
+
path: mountPath,
|
|
539
|
+
options: options && Object.keys(options).length > 0 ? options : void 0
|
|
540
|
+
};
|
|
541
|
+
const registry = new _aigne_afs.ProviderRegistry();
|
|
542
|
+
registerWorkspaceFactory(registry);
|
|
543
|
+
let provider;
|
|
544
|
+
try {
|
|
545
|
+
provider = await registry.createProvider(mount);
|
|
546
|
+
} catch (err) {
|
|
547
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
548
|
+
throw new Error(`Mount verification failed (provider creation): ${msg}`);
|
|
549
|
+
}
|
|
550
|
+
try {
|
|
551
|
+
const { AFS } = await import("@aigne/afs");
|
|
552
|
+
await new AFS().mount(provider, mountPath);
|
|
553
|
+
} catch (err) {
|
|
554
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
555
|
+
throw new Error(`Mount verification failed: could not reach provider at ${uri}. Error: ${msg}. Check your URI and credentials.`);
|
|
556
|
+
} finally {
|
|
557
|
+
if (typeof provider.close === "function") try {
|
|
558
|
+
await provider.close();
|
|
559
|
+
} catch {}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
120
562
|
|
|
121
563
|
//#endregion
|
|
122
564
|
exports.createAFS = createAFS;
|
|
123
|
-
exports.loadAFS = loadAFS;
|
|
565
|
+
exports.loadAFS = loadAFS;
|
|
566
|
+
exports.resolveCredentialsForMount = resolveCredentialsForMount;
|
|
567
|
+
exports.verifyMount = verifyMount;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CredentialStore } from "../credential/store.cjs";
|
|
2
|
+
import { AFS, AuthContext } from "@aigne/afs";
|
|
2
3
|
|
|
3
4
|
//#region src/config/afs-loader.d.ts
|
|
4
5
|
interface MountProgressEvent {
|
|
@@ -8,6 +9,10 @@ interface MountProgressEvent {
|
|
|
8
9
|
}
|
|
9
10
|
interface CreateAFSOptions {
|
|
10
11
|
onProgress?: (event: MountProgressEvent) => void;
|
|
12
|
+
/** Auth context for interactive credential collection (CLI or MCP) */
|
|
13
|
+
authContext?: AuthContext;
|
|
14
|
+
/** Credential store for reading/writing credentials */
|
|
15
|
+
credentialStore?: CredentialStore;
|
|
11
16
|
}
|
|
12
17
|
//#endregion
|
|
13
18
|
export { CreateAFSOptions };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"afs-loader.d.cts","names":[],"sources":["../../src/config/afs-loader.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"afs-loader.d.cts","names":[],"sources":["../../src/config/afs-loader.ts"],"mappings":";;;;UAyDiB,kBAAA;EACf,KAAA;EACA,SAAA;EACA,MAAA;AAAA;AAAA,UAae,gBAAA;EACf,UAAA,IAAc,KAAA,EAAO,kBAAA;;EAErB,WAAA,GAAc,WAAA;;EAEd,eAAA,GAAkB,eAAA;AAAA"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CredentialStore } from "../credential/store.mjs";
|
|
2
|
+
import { AFS, AuthContext, ProviderRegistry } from "@aigne/afs";
|
|
2
3
|
|
|
3
4
|
//#region src/config/afs-loader.d.ts
|
|
4
5
|
interface MountProgressEvent {
|
|
@@ -8,6 +9,10 @@ interface MountProgressEvent {
|
|
|
8
9
|
}
|
|
9
10
|
interface CreateAFSOptions {
|
|
10
11
|
onProgress?: (event: MountProgressEvent) => void;
|
|
12
|
+
/** Auth context for interactive credential collection (CLI or MCP) */
|
|
13
|
+
authContext?: AuthContext;
|
|
14
|
+
/** Credential store for reading/writing credentials */
|
|
15
|
+
credentialStore?: CredentialStore;
|
|
11
16
|
}
|
|
12
17
|
//#endregion
|
|
13
18
|
export { CreateAFSOptions };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"afs-loader.d.mts","names":[],"sources":["../../src/config/afs-loader.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"afs-loader.d.mts","names":[],"sources":["../../src/config/afs-loader.ts"],"mappings":";;;;UAyDiB,kBAAA;EACf,KAAA;EACA,SAAA;EACA,MAAA;AAAA;AAAA,UAae,gBAAA;EACf,UAAA,IAAc,KAAA,EAAO,kBAAA;;EAErB,WAAA,GAAc,WAAA;;EAEd,eAAA,GAAkB,eAAA;AAAA"}
|