@duckmind/dm-windows-x64 0.63.4 → 0.63.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.
Files changed (121) hide show
  1. package/dm.exe +0 -0
  2. package/extensions/.dm-extensions.json +236 -13
  3. package/extensions/dm-context/package.json +1 -1
  4. package/extensions/dm-context/skills/context-management/SKILL.md +173 -223
  5. package/extensions/dm-context/skills/context-management/references/development-and-troubleshooting.md +75 -0
  6. package/extensions/dm-context/skills/context-management/references/interleaved-async-work.md +143 -0
  7. package/extensions/dm-context/skills/context-management/references/planning-and-execution.md +81 -0
  8. package/extensions/dm-context/skills/context-management/references/repeated-items-and-batch-work.md +69 -0
  9. package/extensions/dm-context/skills/context-management/references/retry-branch-and-pivot.md +80 -0
  10. package/extensions/dm-context/skills/context-management/references/search-research-and-reading.md +103 -0
  11. package/extensions/dm-context/skills/context-management/references/task-switching-and-cleanup.md +73 -0
  12. package/extensions/dm-context/src/context.js +3 -2
  13. package/extensions/dm-context/src/index.js +168 -84
  14. package/extensions/dm-mcp-adapter/OAUTH.md +3 -0
  15. package/extensions/dm-mcp-adapter/THIRD_PARTY_NOTICES.md +4 -0
  16. package/extensions/dm-mcp-adapter/abort.js +35 -0
  17. package/extensions/dm-mcp-adapter/agent-dir.js +45 -0
  18. package/extensions/dm-mcp-adapter/agent-plugin-loader.js +349 -0
  19. package/extensions/dm-mcp-adapter/app-bridge.bundle.js +5843 -0
  20. package/extensions/dm-mcp-adapter/cli.js +354 -0
  21. package/extensions/dm-mcp-adapter/commands.js +619 -0
  22. package/extensions/dm-mcp-adapter/config.js +1060 -0
  23. package/extensions/dm-mcp-adapter/consent-manager.js +60 -0
  24. package/extensions/dm-mcp-adapter/direct-tools.js +544 -0
  25. package/extensions/dm-mcp-adapter/dist/agent-dir.js +45 -0
  26. package/extensions/dm-mcp-adapter/dist/agent-plugin-loader.js +349 -0
  27. package/extensions/dm-mcp-adapter/dist/config.js +1060 -0
  28. package/extensions/dm-mcp-adapter/dist/mcp-bearer-store.js +311 -0
  29. package/extensions/dm-mcp-adapter/dist/metadata-cache.js +285 -0
  30. package/extensions/dm-mcp-adapter/dist/package-mcp-loader.js +144 -0
  31. package/extensions/dm-mcp-adapter/dist/resource-tools.js +7 -0
  32. package/extensions/dm-mcp-adapter/dist/types.js +247 -0
  33. package/extensions/dm-mcp-adapter/dist/ui-app-bridge-helpers.js +36 -0
  34. package/extensions/dm-mcp-adapter/dist/ui-stream-types.js +58 -0
  35. package/extensions/dm-mcp-adapter/dist/ui-tool-visibility.js +26 -0
  36. package/extensions/dm-mcp-adapter/dist/utils.js +307 -0
  37. package/extensions/dm-mcp-adapter/elicitation-handler.js +306 -0
  38. package/extensions/dm-mcp-adapter/error-signal.js +16 -0
  39. package/extensions/dm-mcp-adapter/errors.js +225 -0
  40. package/extensions/dm-mcp-adapter/failure-backoff.js +19 -0
  41. package/extensions/dm-mcp-adapter/glimpse-ui.js +61 -0
  42. package/extensions/dm-mcp-adapter/host-html-template.js +464 -0
  43. package/extensions/dm-mcp-adapter/index.js +1079 -0
  44. package/extensions/dm-mcp-adapter/init.js +624 -0
  45. package/extensions/dm-mcp-adapter/json-schema-validator.js +53 -0
  46. package/extensions/dm-mcp-adapter/lifecycle.js +385 -0
  47. package/extensions/dm-mcp-adapter/logger.js +115 -0
  48. package/extensions/dm-mcp-adapter/mcp-auth-flow.js +824 -0
  49. package/extensions/dm-mcp-adapter/mcp-auth.js +732 -0
  50. package/extensions/dm-mcp-adapter/mcp-bearer-store.js +311 -0
  51. package/extensions/dm-mcp-adapter/mcp-callback-server.js +388 -0
  52. package/extensions/dm-mcp-adapter/mcp-code.js +291 -0
  53. package/extensions/dm-mcp-adapter/mcp-keyring-helper.cjs +88 -0
  54. package/extensions/dm-mcp-adapter/mcp-oauth-provider.js +490 -0
  55. package/extensions/dm-mcp-adapter/mcp-output-guard.js +386 -0
  56. package/extensions/dm-mcp-adapter/mcp-panel.js +951 -0
  57. package/extensions/dm-mcp-adapter/mcp-probe.js +151 -0
  58. package/extensions/dm-mcp-adapter/mcp-references.js +263 -0
  59. package/extensions/dm-mcp-adapter/mcp-script-worker.mjs +126 -0
  60. package/extensions/dm-mcp-adapter/mcp-setup-panel.js +578 -0
  61. package/extensions/dm-mcp-adapter/mcp-status.js +80 -0
  62. package/extensions/dm-mcp-adapter/mcp-trace.js +215 -0
  63. package/extensions/dm-mcp-adapter/metadata-cache.js +292 -0
  64. package/extensions/dm-mcp-adapter/namespace-tools.js +155 -0
  65. package/extensions/dm-mcp-adapter/npx-resolver.js +465 -0
  66. package/extensions/dm-mcp-adapter/oauth-handler.js +17 -0
  67. package/extensions/dm-mcp-adapter/oauth.js +17 -0
  68. package/extensions/dm-mcp-adapter/onboarding-state.js +62 -0
  69. package/extensions/dm-mcp-adapter/package-mcp-loader.js +144 -0
  70. package/extensions/dm-mcp-adapter/package.json +68 -0
  71. package/extensions/dm-mcp-adapter/panel-keys.js +26 -0
  72. package/extensions/dm-mcp-adapter/prompts.js +258 -0
  73. package/extensions/dm-mcp-adapter/proxy-modes.js +1336 -0
  74. package/extensions/dm-mcp-adapter/request-headers-command.js +293 -0
  75. package/extensions/dm-mcp-adapter/resource-tools.js +7 -0
  76. package/extensions/dm-mcp-adapter/runtime-owner.js +79 -0
  77. package/extensions/dm-mcp-adapter/sampling-handler.js +222 -0
  78. package/extensions/dm-mcp-adapter/sandbox-proxy-template.js +190 -0
  79. package/extensions/dm-mcp-adapter/search-ranking.js +198 -0
  80. package/extensions/dm-mcp-adapter/server-manager.js +1258 -0
  81. package/extensions/dm-mcp-adapter/session-recovery.js +82 -0
  82. package/extensions/dm-mcp-adapter/skills/mcp-scripting/SKILL.md +39 -0
  83. package/extensions/dm-mcp-adapter/state.js +0 -0
  84. package/extensions/dm-mcp-adapter/test-runner.mjs +18 -0
  85. package/extensions/dm-mcp-adapter/tool-approval.js +151 -0
  86. package/extensions/dm-mcp-adapter/tool-metadata.js +247 -0
  87. package/extensions/dm-mcp-adapter/tool-registrar.js +212 -0
  88. package/extensions/dm-mcp-adapter/tool-result-renderer.js +355 -0
  89. package/extensions/dm-mcp-adapter/ts-shape.js +157 -0
  90. package/extensions/dm-mcp-adapter/types.js +264 -0
  91. package/extensions/dm-mcp-adapter/ui-app-bridge-helpers.js +36 -0
  92. package/extensions/dm-mcp-adapter/ui-resource-handler.js +209 -0
  93. package/extensions/dm-mcp-adapter/ui-server.js +815 -0
  94. package/extensions/dm-mcp-adapter/ui-session.js +455 -0
  95. package/extensions/dm-mcp-adapter/ui-stream-types.js +58 -0
  96. package/extensions/dm-mcp-adapter/ui-tool-visibility.js +26 -0
  97. package/extensions/dm-mcp-adapter/unix-socket-transport.js +83 -0
  98. package/extensions/dm-mcp-adapter/utils.js +307 -0
  99. package/extensions/dm-skills-manager/THIRD_PARTY_NOTICES.md +27 -0
  100. package/extensions/dm-skills-manager/extensions/skills-manager/components.js +265 -0
  101. package/extensions/dm-skills-manager/extensions/skills-manager/constants.js +31 -0
  102. package/extensions/dm-skills-manager/extensions/skills-manager/creation-fallback.js +20 -0
  103. package/extensions/dm-skills-manager/extensions/skills-manager/creation.js +145 -0
  104. package/extensions/dm-skills-manager/extensions/skills-manager/dialog.js +738 -0
  105. package/extensions/dm-skills-manager/extensions/skills-manager/dm-ai-compat.js +15 -0
  106. package/extensions/dm-skills-manager/extensions/skills-manager/format.js +125 -0
  107. package/extensions/dm-skills-manager/extensions/skills-manager/glyphs.js +142 -0
  108. package/extensions/dm-skills-manager/extensions/skills-manager/layout.js +93 -0
  109. package/extensions/dm-skills-manager/extensions/skills-manager/paths.js +76 -0
  110. package/extensions/dm-skills-manager/extensions/skills-manager/registry.js +95 -0
  111. package/extensions/dm-skills-manager/extensions/skills-manager/settings.js +93 -0
  112. package/extensions/dm-skills-manager/extensions/skills-manager/startup.js +32 -0
  113. package/extensions/dm-skills-manager/extensions/skills-manager/toggle.js +54 -0
  114. package/extensions/dm-skills-manager/extensions/skills-manager/types.js +14 -0
  115. package/extensions/dm-skills-manager/extensions/skills-manager/ui.js +121 -0
  116. package/extensions/dm-skills-manager/extensions/skills-manager.js +111 -0
  117. package/extensions/dm-skills-manager/package.json +121 -0
  118. package/package.json +13 -1
  119. package/extensions/dm-localllm-provider/detect.test.js +0 -1051
  120. package/extensions/dm-localllm-provider/index.test.js +0 -149
  121. package/extensions/dm-localllm-provider/keychain.test.js +0 -61
@@ -0,0 +1,465 @@
1
+ import { existsSync, readFileSync, realpathSync, readdirSync, statSync, writeFileSync, renameSync, mkdirSync, openSync, readSync, closeSync, unlinkSync } from "node:fs";
2
+ import { join, dirname, extname, resolve, sep } from "node:path";
3
+ import { getAgentPath } from "./agent-dir.js";
4
+ import { throwIfAborted } from "./abort.js";
5
+ import crossSpawn from "cross-spawn";
6
+ const CACHE_VERSION = 2;
7
+ const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
8
+ const EXACT_PACKAGE_VERSION_RE = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z][0-9A-Za-z.-]*)?(?:\+[0-9A-Za-z][0-9A-Za-z.-]*)?$/;
9
+ export async function resolveNpxBinary(command, args, signal) {
10
+ throwIfAborted(signal);
11
+ const parsed = command === "npx" ? parseNpxArgs(args) : command === "npm" ? parseNpmExecArgs(args) : null;
12
+ if (!parsed)
13
+ return null;
14
+ const packageSpec = parsePackageSpec(parsed.packageSpec);
15
+ const cacheKey = JSON.stringify([command, parsed.packageSpec, parsed.binName ?? ""]);
16
+ const cache = loadCache();
17
+ const cached = cache?.entries?.[cacheKey];
18
+ if (cached && Date.now() - cached.resolvedAt < CACHE_TTL_MS && existsSync(cached.resolvedBin) && (!packageSpec?.exactVersion || cached.packageVersion === packageSpec.exactVersion)) {
19
+ return { binPath: cached.resolvedBin, extraArgs: parsed.extraArgs, isJs: cached.isJs };
20
+ }
21
+ const resolved = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
22
+ if (resolved) {
23
+ saveCacheEntry(cacheKey, resolved);
24
+ return { binPath: resolved.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolved.isJs };
25
+ }
26
+ await forceNpxCache(parsed.packageSpec, signal);
27
+ const resolvedAfterInstall = resolveFromNpmCache(parsed.packageSpec, parsed.binName);
28
+ if (resolvedAfterInstall) {
29
+ saveCacheEntry(cacheKey, resolvedAfterInstall);
30
+ return { binPath: resolvedAfterInstall.resolvedBin, extraArgs: parsed.extraArgs, isJs: resolvedAfterInstall.isJs };
31
+ }
32
+ return null;
33
+ }
34
+ function parseNpxArgs(args) {
35
+ const separatorIndex = args.indexOf("--");
36
+ const before = separatorIndex >= 0 ? args.slice(0, separatorIndex) : args;
37
+ const after = separatorIndex >= 0 ? args.slice(separatorIndex + 1) : [];
38
+ const positionals = [];
39
+ let packageSpec;
40
+ let sawPackageFlag = false;
41
+ let foundFirstPositional = false;
42
+ for (let i = 0;i < before.length; i++) {
43
+ const arg = before[i];
44
+ if (arg === undefined)
45
+ return null;
46
+ if (foundFirstPositional) {
47
+ positionals.push(arg);
48
+ continue;
49
+ }
50
+ if (arg === "-y" || arg === "--yes")
51
+ continue;
52
+ if (arg === "-p" || arg === "--package") {
53
+ const value = before[i + 1];
54
+ if (!value || value.startsWith("-"))
55
+ return null;
56
+ if (!packageSpec)
57
+ packageSpec = value;
58
+ sawPackageFlag = true;
59
+ i++;
60
+ continue;
61
+ }
62
+ if (arg.startsWith("--package=")) {
63
+ const value = arg.slice("--package=".length);
64
+ if (!value)
65
+ return null;
66
+ if (!packageSpec)
67
+ packageSpec = value;
68
+ sawPackageFlag = true;
69
+ continue;
70
+ }
71
+ if (arg.startsWith("-")) {
72
+ return null;
73
+ }
74
+ positionals.push(arg);
75
+ foundFirstPositional = true;
76
+ }
77
+ const separatedAfter = separatorIndex >= 0 && after.length > 0 ? ["--", ...after] : after;
78
+ if (sawPackageFlag) {
79
+ const binName = positionals[0];
80
+ if (!packageSpec || !binName)
81
+ return null;
82
+ const extraArgs = positionals.slice(1).concat(separatedAfter);
83
+ return { packageSpec, binName, extraArgs };
84
+ }
85
+ const packagePositional = positionals[0];
86
+ if (!packagePositional)
87
+ return null;
88
+ const extraArgs = positionals.slice(1).concat(separatedAfter);
89
+ return { packageSpec: packagePositional, extraArgs };
90
+ }
91
+ function parseNpmExecArgs(args) {
92
+ if (args[0] !== "exec")
93
+ return null;
94
+ const execArgs = args.slice(1);
95
+ const separatorIndex = execArgs.indexOf("--");
96
+ if (separatorIndex < 0)
97
+ return null;
98
+ const before = execArgs.slice(0, separatorIndex);
99
+ const after = execArgs.slice(separatorIndex + 1);
100
+ let packageSpec;
101
+ for (let i = 0;i < before.length; i++) {
102
+ const arg = before[i];
103
+ if (arg === undefined)
104
+ return null;
105
+ if (arg === "-y" || arg === "--yes")
106
+ continue;
107
+ if (arg === "--package") {
108
+ const value = before[i + 1];
109
+ if (!value || value.startsWith("-"))
110
+ return null;
111
+ if (!packageSpec)
112
+ packageSpec = value;
113
+ i++;
114
+ continue;
115
+ }
116
+ if (arg.startsWith("--package=")) {
117
+ const value = arg.slice("--package=".length);
118
+ if (!value)
119
+ return null;
120
+ if (!packageSpec)
121
+ packageSpec = value;
122
+ continue;
123
+ }
124
+ if (arg.startsWith("-")) {
125
+ return null;
126
+ }
127
+ }
128
+ const binName = after[0];
129
+ if (!packageSpec || !binName)
130
+ return null;
131
+ const extraArgs = after.slice(1);
132
+ return { packageSpec, binName, extraArgs };
133
+ }
134
+ function resolveFromNpmCache(packageSpec, binName) {
135
+ const cacheDir = getNpmCacheDir();
136
+ if (!cacheDir)
137
+ return null;
138
+ const parsedSpec = parsePackageSpec(packageSpec);
139
+ if (!parsedSpec)
140
+ return null;
141
+ const { packageName, exactVersion } = parsedSpec;
142
+ const packageDir = findCachedPackageDir(cacheDir, packageName, exactVersion);
143
+ if (!packageDir)
144
+ return null;
145
+ const packageJsonPath = join(packageDir, "package.json");
146
+ if (!existsSync(packageJsonPath))
147
+ return null;
148
+ let pkg = null;
149
+ try {
150
+ pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
151
+ } catch {
152
+ return null;
153
+ }
154
+ const binField = pkg?.bin;
155
+ if (!binField)
156
+ return null;
157
+ const candidates = buildBinCandidates(packageName, binName);
158
+ let chosenBinName;
159
+ let binRel;
160
+ if (typeof binField === "string") {
161
+ chosenBinName = defaultBinName(packageName);
162
+ binRel = binField;
163
+ } else {
164
+ for (const candidate of candidates) {
165
+ if (binField[candidate]) {
166
+ chosenBinName = candidate;
167
+ binRel = binField[candidate];
168
+ break;
169
+ }
170
+ }
171
+ if (!binRel) {
172
+ const firstEntry = Object.entries(binField)[0];
173
+ if (firstEntry) {
174
+ chosenBinName = firstEntry[0];
175
+ binRel = firstEntry[1];
176
+ }
177
+ }
178
+ }
179
+ if (!binRel)
180
+ return null;
181
+ const nodeModulesDir = findNodeModulesDir(packageDir);
182
+ const binLink = chosenBinName ? join(nodeModulesDir, ".bin", chosenBinName) : null;
183
+ let resolvedBin = binLink && existsSync(binLink) ? safeRealpath(binLink) : "";
184
+ if (!resolvedBin) {
185
+ resolvedBin = resolve(packageDir, binRel);
186
+ if (!existsSync(resolvedBin))
187
+ return null;
188
+ }
189
+ const isJs = detectJsBinary(resolvedBin);
190
+ return {
191
+ resolvedBin,
192
+ resolvedAt: Date.now(),
193
+ ...pkg?.version !== undefined ? { packageVersion: pkg.version } : {},
194
+ isJs
195
+ };
196
+ }
197
+ const FORCE_CACHE_TIMEOUT_MS = 30000;
198
+ async function forceNpxCache(packageSpec, signal) {
199
+ throwIfAborted(signal);
200
+ try {
201
+ await new Promise((resolve, reject) => {
202
+ const proc = crossSpawn("npm", ["exec", "--yes", "--package", packageSpec, "--", "node", "-e", "1"], { stdio: "ignore" });
203
+ const timer = setTimeout(() => {
204
+ proc.kill();
205
+ reject(new Error("timeout"));
206
+ }, FORCE_CACHE_TIMEOUT_MS);
207
+ const abort = () => {
208
+ proc.kill();
209
+ reject(signal?.reason instanceof Error ? signal.reason : new Error("MCP request aborted"));
210
+ };
211
+ signal?.addEventListener("abort", abort, { once: true });
212
+ timer.unref();
213
+ proc.on("close", () => {
214
+ clearTimeout(timer);
215
+ signal?.removeEventListener("abort", abort);
216
+ resolve();
217
+ });
218
+ proc.on("error", (err) => {
219
+ clearTimeout(timer);
220
+ signal?.removeEventListener("abort", abort);
221
+ reject(err);
222
+ });
223
+ });
224
+ } catch (error) {
225
+ if (signal?.aborted)
226
+ throwIfAborted(signal);
227
+ }
228
+ throwIfAborted(signal);
229
+ }
230
+ function buildBinCandidates(packageName, explicitBin) {
231
+ const candidates = [];
232
+ if (explicitBin)
233
+ candidates.push(explicitBin);
234
+ if (packageName.startsWith("@")) {
235
+ const namePart = packageName.split("/")[1] ?? "";
236
+ const scopePart = packageName.split("/")[0]?.replace("@", "") ?? "";
237
+ if (namePart)
238
+ candidates.push(namePart);
239
+ if (scopePart && namePart)
240
+ candidates.push(`${scopePart}-${namePart}`);
241
+ } else {
242
+ candidates.push(packageName);
243
+ }
244
+ return [...new Set(candidates.filter(Boolean))];
245
+ }
246
+ function parsePackageSpec(spec) {
247
+ const trimmed = spec.trim();
248
+ if (!trimmed)
249
+ return null;
250
+ let packageName;
251
+ let requestedVersion;
252
+ if (trimmed.startsWith("@")) {
253
+ const slashIndex = trimmed.indexOf("/");
254
+ if (slashIndex < 0)
255
+ return null;
256
+ const atIndex = trimmed.lastIndexOf("@");
257
+ if (atIndex > slashIndex) {
258
+ packageName = trimmed.slice(0, atIndex);
259
+ requestedVersion = trimmed.slice(atIndex + 1);
260
+ } else {
261
+ packageName = trimmed;
262
+ }
263
+ } else {
264
+ const atIndex = trimmed.indexOf("@");
265
+ if (atIndex >= 0) {
266
+ packageName = trimmed.slice(0, atIndex);
267
+ requestedVersion = trimmed.slice(atIndex + 1);
268
+ } else {
269
+ packageName = trimmed;
270
+ }
271
+ }
272
+ if (!packageName)
273
+ return null;
274
+ const normalizedVersion = requestedVersion?.replace(/^=/, "").replace(/^v/i, "");
275
+ return {
276
+ packageName,
277
+ ...normalizedVersion && EXACT_PACKAGE_VERSION_RE.test(normalizedVersion) ? { exactVersion: normalizedVersion } : {}
278
+ };
279
+ }
280
+ function defaultBinName(packageName) {
281
+ if (packageName.startsWith("@")) {
282
+ const parts = packageName.split("/");
283
+ return parts[1] ?? packageName.replace("@", "").replace("/", "-");
284
+ }
285
+ return packageName;
286
+ }
287
+ function findCachedPackageDir(cacheDir, packageName, exactVersion) {
288
+ const npxDir = join(cacheDir, "_npx");
289
+ if (!existsSync(npxDir))
290
+ return null;
291
+ const packagePathParts = packageName.startsWith("@") ? packageName.split("/") : [packageName];
292
+ const candidates = readdirSync(npxDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => {
293
+ const full = join(npxDir, entry.name);
294
+ const mtime = safeStatMtime(full);
295
+ return { name: entry.name, mtime };
296
+ }).sort((a, b) => b.mtime - a.mtime);
297
+ for (const entry of candidates) {
298
+ const pkgDir = join(npxDir, entry.name, "node_modules", ...packagePathParts);
299
+ const packageJsonPath = join(pkgDir, "package.json");
300
+ if (!existsSync(packageJsonPath))
301
+ continue;
302
+ if (exactVersion) {
303
+ try {
304
+ const pkg = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
305
+ if (pkg.version !== exactVersion)
306
+ continue;
307
+ } catch {
308
+ continue;
309
+ }
310
+ }
311
+ return pkgDir;
312
+ }
313
+ return null;
314
+ }
315
+ function findNodeModulesDir(packageDir) {
316
+ const parts = packageDir.split(sep);
317
+ const idx = parts.lastIndexOf("node_modules");
318
+ if (idx >= 0) {
319
+ return parts.slice(0, idx + 1).join(sep);
320
+ }
321
+ return join(packageDir, "..");
322
+ }
323
+ function detectJsBinary(binPath) {
324
+ const ext = extname(binPath).toLowerCase();
325
+ if (ext === ".js" || ext === ".mjs" || ext === ".cjs")
326
+ return true;
327
+ try {
328
+ const fd = openSync(binPath, "r");
329
+ try {
330
+ const buf = Buffer.alloc(256);
331
+ readSync(fd, buf, 0, 256, 0);
332
+ const firstLine = buf.toString("utf-8").split(`
333
+ `)[0] ?? "";
334
+ return firstLine.startsWith("#!") && firstLine.includes("node");
335
+ } finally {
336
+ closeSync(fd);
337
+ }
338
+ } catch {
339
+ return false;
340
+ }
341
+ }
342
+ let npmCacheDirCached;
343
+ function getNpmCacheDir() {
344
+ if (npmCacheDirCached !== undefined)
345
+ return npmCacheDirCached;
346
+ if (process.env.NPM_CONFIG_CACHE) {
347
+ npmCacheDirCached = process.env.NPM_CONFIG_CACHE;
348
+ return npmCacheDirCached;
349
+ }
350
+ try {
351
+ const result = crossSpawn.sync("npm", ["config", "get", "cache"], { encoding: "utf-8" });
352
+ if (result.status === 0) {
353
+ const path = String(result.stdout).trim();
354
+ npmCacheDirCached = path || null;
355
+ return npmCacheDirCached;
356
+ }
357
+ } catch {
358
+ npmCacheDirCached = null;
359
+ return null;
360
+ }
361
+ npmCacheDirCached = null;
362
+ return null;
363
+ }
364
+ function getNpxCachePath() {
365
+ return getAgentPath("mcp-npx-cache.json");
366
+ }
367
+ function readNpxCachePayload(cachePath) {
368
+ if (!existsSync(cachePath))
369
+ return null;
370
+ try {
371
+ return JSON.parse(readFileSync(cachePath, "utf-8"));
372
+ } catch {
373
+ return null;
374
+ }
375
+ }
376
+ function asRecord(value) {
377
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
378
+ }
379
+ function createCacheEntries() {
380
+ return Object.create(null);
381
+ }
382
+ function toNpxCacheEntry(value) {
383
+ const raw = asRecord(value);
384
+ if (!raw)
385
+ return null;
386
+ if (typeof raw.resolvedBin !== "string")
387
+ return null;
388
+ if (typeof raw.resolvedAt !== "number" || !Number.isFinite(raw.resolvedAt))
389
+ return null;
390
+ if (typeof raw.isJs !== "boolean")
391
+ return null;
392
+ if (raw.packageVersion !== undefined && typeof raw.packageVersion !== "string")
393
+ return null;
394
+ return {
395
+ resolvedBin: raw.resolvedBin,
396
+ resolvedAt: raw.resolvedAt,
397
+ ...raw.packageVersion !== undefined ? { packageVersion: raw.packageVersion } : {},
398
+ isJs: raw.isJs
399
+ };
400
+ }
401
+ function toNpxCache(value) {
402
+ const raw = asRecord(value);
403
+ if (!raw || raw.version !== CACHE_VERSION)
404
+ return null;
405
+ const rawEntries = asRecord(raw.entries);
406
+ if (!rawEntries)
407
+ return null;
408
+ const entries = createCacheEntries();
409
+ for (const [key, rawEntry] of Object.entries(rawEntries)) {
410
+ const entry = toNpxCacheEntry(rawEntry);
411
+ if (entry)
412
+ entries[key] = entry;
413
+ }
414
+ return { version: CACHE_VERSION, entries };
415
+ }
416
+ function clearLegacyCache() {
417
+ const cachePath = getNpxCachePath();
418
+ const raw = asRecord(readNpxCachePayload(cachePath));
419
+ if (raw?.version !== 1)
420
+ return false;
421
+ try {
422
+ unlinkSync(cachePath);
423
+ } catch {
424
+ try {
425
+ writeFileSync(cachePath, "", "utf-8");
426
+ } catch {}
427
+ }
428
+ return true;
429
+ }
430
+ clearLegacyCache();
431
+ function loadCache() {
432
+ if (clearLegacyCache())
433
+ return null;
434
+ return toNpxCache(readNpxCachePayload(getNpxCachePath()));
435
+ }
436
+ function saveCacheEntry(key, entry) {
437
+ try {
438
+ const cachePath = getNpxCachePath();
439
+ const dir = dirname(cachePath);
440
+ mkdirSync(dir, { recursive: true });
441
+ const existing = toNpxCache(readNpxCachePayload(cachePath));
442
+ const entries = createCacheEntries();
443
+ if (existing)
444
+ Object.assign(entries, existing.entries);
445
+ const merged = { version: CACHE_VERSION, entries };
446
+ merged.entries[key] = entry;
447
+ const tmpPath = `${cachePath}.${process.pid}.tmp`;
448
+ writeFileSync(tmpPath, JSON.stringify(merged, null, 2), "utf-8");
449
+ renameSync(tmpPath, cachePath);
450
+ } catch {}
451
+ }
452
+ function safeRealpath(path) {
453
+ try {
454
+ return realpathSync(path);
455
+ } catch {
456
+ return "";
457
+ }
458
+ }
459
+ function safeStatMtime(path) {
460
+ try {
461
+ return statSync(path).mtimeMs;
462
+ } catch {
463
+ return 0;
464
+ }
465
+ }
@@ -0,0 +1,17 @@
1
+ import { getAuthEntry } from "./mcp-auth.js";
2
+ export function getStoredTokens(serverName) {
3
+ const tokens = getAuthEntry(serverName)?.tokens;
4
+ if (!tokens)
5
+ return;
6
+ if (tokens.expiresAt !== undefined && tokens.expiresAt < Date.now() / 1000) {
7
+ return;
8
+ }
9
+ return {
10
+ access_token: tokens.accessToken,
11
+ token_type: "Bearer",
12
+ refresh_token: tokens.refreshToken,
13
+ expires_in: tokens.expiresAt !== undefined ? Math.max(0, Math.floor(tokens.expiresAt - Date.now() / 1000)) : undefined,
14
+ scope: tokens.scope,
15
+ ...tokens.issuer !== undefined ? { issuer: tokens.issuer } : {}
16
+ };
17
+ }
@@ -0,0 +1,17 @@
1
+ import { getValidToken } from "./mcp-auth-flow.js";
2
+ import {
3
+ inspectAuthForUrl,
4
+ updateTokens
5
+ } from "./mcp-auth.js";
6
+ export async function getMcpOAuthTokensForUrl(serverName, serverUrl, options = {}) {
7
+ return await getValidToken(serverName, serverUrl, options) ?? undefined;
8
+ }
9
+ export function inspectMcpOAuthTokensForUrl(serverName, serverUrl, options) {
10
+ const status = inspectAuthForUrl(serverName, serverUrl, options);
11
+ if (status.status !== "present")
12
+ return status;
13
+ return status.entry.tokens ? { status: "present", tokens: status.entry.tokens } : { status: "absent" };
14
+ }
15
+ export function updateMcpOAuthTokensForUrl(serverName, serverUrl, tokens, options) {
16
+ updateTokens(serverName, tokens, serverUrl, options);
17
+ }
@@ -0,0 +1,62 @@
1
+ import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from "node:fs";
2
+ import { dirname } from "node:path";
3
+ import { getAgentPath } from "./agent-dir.js";
4
+ const DEFAULT_STATE = {
5
+ version: 1,
6
+ sharedConfigHintShown: false,
7
+ setupCompleted: false
8
+ };
9
+ export function getOnboardingStatePath() {
10
+ return getAgentPath("mcp-onboarding.json");
11
+ }
12
+ export function loadOnboardingState() {
13
+ const path = getOnboardingStatePath();
14
+ if (!existsSync(path))
15
+ return { ...DEFAULT_STATE };
16
+ try {
17
+ const raw = JSON.parse(readFileSync(path, "utf-8"));
18
+ if (!raw || typeof raw !== "object")
19
+ return { ...DEFAULT_STATE };
20
+ return {
21
+ version: 1,
22
+ sharedConfigHintShown: raw.sharedConfigHintShown === true,
23
+ setupCompleted: raw.setupCompleted === true,
24
+ ...typeof raw.lastDiscoveryFingerprint === "string" ? { lastDiscoveryFingerprint: raw.lastDiscoveryFingerprint } : {}
25
+ };
26
+ } catch {
27
+ return { ...DEFAULT_STATE };
28
+ }
29
+ }
30
+ export function saveOnboardingState(state) {
31
+ const path = getOnboardingStatePath();
32
+ mkdirSync(dirname(path), { recursive: true });
33
+ const tmpPath = `${path}.${process.pid}.tmp`;
34
+ writeFileSync(tmpPath, `${JSON.stringify(state, null, 2)}
35
+ `, "utf-8");
36
+ renameSync(tmpPath, path);
37
+ }
38
+ export function updateOnboardingState(updater) {
39
+ const next = updater(loadOnboardingState());
40
+ saveOnboardingState(next);
41
+ return next;
42
+ }
43
+ export function markSharedConfigHintShown(fingerprint) {
44
+ return updateOnboardingState((state) => {
45
+ const lastDiscoveryFingerprint = fingerprint ?? state.lastDiscoveryFingerprint;
46
+ return {
47
+ ...state,
48
+ sharedConfigHintShown: true,
49
+ ...lastDiscoveryFingerprint !== undefined ? { lastDiscoveryFingerprint } : {}
50
+ };
51
+ });
52
+ }
53
+ export function markSetupCompleted(fingerprint) {
54
+ return updateOnboardingState((state) => {
55
+ const lastDiscoveryFingerprint = fingerprint ?? state.lastDiscoveryFingerprint;
56
+ return {
57
+ ...state,
58
+ setupCompleted: true,
59
+ ...lastDiscoveryFingerprint !== undefined ? { lastDiscoveryFingerprint } : {}
60
+ };
61
+ });
62
+ }