universal-plugin-hub 1.3.1 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/skills.js CHANGED
@@ -3,7 +3,7 @@ import { existsSync, readdirSync, readFileSync, realpathSync, statSync, watch }
3
3
  import { homedir } from 'node:os'
4
4
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
5
5
  import { load as parseYaml } from 'js-yaml'
6
- import { readState, writeState, dshSkillsDir, installedDir } from './store.js'
6
+ import { readState, writeState, dshSkillsDir, installedDir, resolveOptionalService } from './store.js'
7
7
  import { isUploadedSkillTarget, uploadedSkillRecord } from './skill-uploads.js'
8
8
  import { formatDisplayName } from './parser.js'
9
9
 
@@ -540,7 +540,7 @@ export function createSkillsManager(root) {
540
540
  // A preset's filesystem provider shadows the global layer regardless of
541
541
  // rank. Explicit off choices must also live in the exact agent scope.
542
542
  // Enabled skills continue to resolve through their native providers.
543
- const serviceFor = () => ctx.get?.('agentPresets')?.serviceFor(agent, 'skills') ?? registry
543
+ const serviceFor = () => resolveOptionalService(ctx, 'agentPresets')?.serviceFor(agent, 'skills') ?? registry
544
544
  const service = serviceFor()
545
545
  if (service !== registry && !presetProviders.has(service)) {
546
546
  let invalidatePreset
@@ -555,13 +555,13 @@ export function createSkillsManager(root) {
555
555
  }
556
556
  if (service !== registry && presetProviders.has(service)) presetProviders.get(service).users++
557
557
  const dispose = (createScope ? (() => { const policy = scopePolicies.get(agent); scopePolicies.delete(agent); return policy?.dispose() })
558
- : guard('agent provider', () => agent.ctx.skills.registerProvider(() => disabledProvider()))) || (() => {})
558
+ : guard('agent provider', () => resolveOptionalService(agent?.ctx, 'skills')?.registerProvider?.(() => disabledProvider()))) || (() => {})
559
559
  agentViews.set(agent, { dispose: () => {
560
560
  const pending = dispose()
561
561
  const preset = presetProviders.get(service)
562
562
  if (preset && --preset.users === 0) { preset.dispose(); presetProviders.delete(service) }
563
563
  return pending
564
- }, registry: serviceFor, lookup: () => ({ scope: agent, cwd: ctx.get?.('workingDirectory')?.get(agent.session) ?? agent.session?.header?.cwd }) })
564
+ }, registry: serviceFor, lookup: () => ({ scope: agent, cwd: resolveOptionalService(ctx, 'workingDirectory')?.get(agent.session) ?? agent.session?.header?.cwd }) })
565
565
  }
566
566
  const unbind = (agent) => { const view = agentViews.get(agent); agentViews.delete(agent); return view?.dispose() }
567
567
  const offCreated = ctx.on('agent/created', ({ agent }) => bind(agent))
package/src/store.js CHANGED
@@ -9,6 +9,7 @@
9
9
  import { homedir } from 'node:os'
10
10
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'
11
11
  import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync, statSync, lstatSync, rmSync, rmdirSync, unlinkSync, readdirSync } from 'node:fs'
12
+ import { rm } from 'node:fs/promises'
12
13
  import { execFileSync } from 'node:child_process'
13
14
 
14
15
  let TRASH_SEQ = 0
@@ -40,6 +41,14 @@ function scheduleTrashSweep(trashed) {
40
41
  TRASH_SWEEP_TIMER.unref?.()
41
42
  }
42
43
 
44
+ function moveToTrash(dir, quarantineRoot) {
45
+ mkdirSync(quarantineRoot, { recursive: true })
46
+ const seq = (TRASH_SEQ = (TRASH_SEQ + 1) & 0xffffff).toString(36)
47
+ const trashed = join(quarantineRoot, `${basename(dir) || 'dir'}-${process.pid}-${Date.now().toString(36)}-${seq}`)
48
+ renameSync(dir, trashed)
49
+ return trashed
50
+ }
51
+
43
52
  function unlinkReparsePointsSync(dir) {
44
53
  let entries
45
54
  try {
@@ -88,11 +97,7 @@ export function safeRmDir(dir, { allowTrash = true, quarantineRoot = trashRoot()
88
97
  } catch (err) {
89
98
  if (allowTrash && quarantineRoot && !isSameOrInsidePath(dir, quarantineRoot) && !isSameOrInsidePath(quarantineRoot, dir)) {
90
99
  try {
91
- mkdirSync(quarantineRoot, { recursive: true })
92
- const seq = (TRASH_SEQ = (TRASH_SEQ + 1) & 0xffffff).toString(36)
93
- const trashed = join(quarantineRoot, `${basename(dir) || 'dir'}-${process.pid}-${Date.now().toString(36)}-${seq}`)
94
- renameSync(dir, trashed)
95
- scheduleTrashSweep(trashed)
100
+ scheduleTrashSweep(moveToTrash(dir, quarantineRoot))
96
101
  return
97
102
  } catch {}
98
103
  }
@@ -123,7 +128,27 @@ export function safeRmDir(dir, { allowTrash = true, quarantineRoot = trashRoot()
123
128
  }
124
129
 
125
130
  /** Remove managed files before forgetting the record needed to retry cleanup. */
126
- export function removeManagedDir(dir, { allowTrash = false } = {}) {
131
+ export function removeManagedDir(dir, { allowTrash = false, deferCleanup = false } = {}) {
132
+ if (deferCleanup) {
133
+ try {
134
+ if (!lstatSync(dir, { throwIfNoEntry: false })) return
135
+ // Detach the checkout as a unit. An open Windows handle can block the
136
+ // move, in which case the caller retries while the checkout stays intact.
137
+ const quarantineRoot = trashRoot()
138
+ const trashed = moveToTrash(dir, quarantineRoot)
139
+ void rm(trashed, { recursive: true, force: true }).then(
140
+ () => { try { rmdirSync(quarantineRoot) } catch {} },
141
+ () => scheduleTrashSweep(trashed),
142
+ )
143
+ return
144
+ } catch (cause) {
145
+ throw Object.assign(new Error(`无法清理目录:${dir}`, { cause }), {
146
+ code: cause.code,
147
+ syscall: cause.syscall,
148
+ path: dir,
149
+ })
150
+ }
151
+ }
127
152
  safeRmDir(dir, { allowTrash })
128
153
  let remaining = null
129
154
  let lstatErr = null
@@ -142,7 +167,14 @@ export function removeManagedDir(dir, { allowTrash = false } = {}) {
142
167
  }
143
168
 
144
169
  let STATE_CACHE = null
170
+ let STATE_CACHE_PATH = ''
145
171
  let STATE_CACHE_MTIME = 0
172
+ let STATE_CACHE_SIZE = -1
173
+ let STATE_REVISION = 0
174
+
175
+ export function stateRevision() {
176
+ return STATE_REVISION
177
+ }
146
178
 
147
179
  export const ROOT_DIR_NAME = 'universal-plugin-hub'
148
180
 
@@ -247,17 +279,28 @@ export function defaultState() {
247
279
  }
248
280
  }
249
281
 
250
- /** Read state.json with 0ms in-memory cache and mtime invalidation. */
282
+ /** Read state.json with 0ms in-memory cache and path + mtime + size invalidation. */
251
283
  export function readState() {
252
284
  const path = statePath()
253
285
  if (!existsSync(path)) {
286
+ if (STATE_CACHE_PATH === path) {
287
+ STATE_CACHE = null
288
+ STATE_CACHE_PATH = ''
289
+ STATE_CACHE_MTIME = 0
290
+ STATE_CACHE_SIZE = -1
291
+ }
254
292
  const fresh = defaultState()
255
293
  writeState(fresh)
256
294
  return structuredClone(fresh)
257
295
  }
258
296
  try {
259
297
  const stat = statSync(path)
260
- if (STATE_CACHE && STATE_CACHE_MTIME === stat.mtimeMs) {
298
+ if (
299
+ STATE_CACHE &&
300
+ STATE_CACHE_PATH === path &&
301
+ STATE_CACHE_MTIME === stat.mtimeMs &&
302
+ STATE_CACHE_SIZE === stat.size
303
+ ) {
261
304
  return structuredClone(STATE_CACHE)
262
305
  }
263
306
  const parsed = JSON.parse(readFileSync(path, 'utf8'))
@@ -269,9 +312,17 @@ export function readState() {
269
312
  plugins: Array.isArray(parsed.plugins) ? parsed.plugins : [],
270
313
  }
271
314
  STATE_CACHE = merged
315
+ STATE_CACHE_PATH = path
272
316
  STATE_CACHE_MTIME = stat.mtimeMs
317
+ STATE_CACHE_SIZE = stat.size
273
318
  return structuredClone(merged)
274
319
  } catch {
320
+ if (STATE_CACHE_PATH === path) {
321
+ STATE_CACHE = null
322
+ STATE_CACHE_PATH = ''
323
+ STATE_CACHE_MTIME = 0
324
+ STATE_CACHE_SIZE = -1
325
+ }
275
326
  return defaultState()
276
327
  }
277
328
  }
@@ -284,11 +335,16 @@ export function writeState(state) {
284
335
  const snapshot = structuredClone(state)
285
336
  writeFileSync(tmp, JSON.stringify(snapshot, null, 2), 'utf8')
286
337
  renameSync(tmp, file)
338
+ STATE_REVISION += 1
287
339
  STATE_CACHE = snapshot
340
+ STATE_CACHE_PATH = file
288
341
  try {
289
- STATE_CACHE_MTIME = statSync(file).mtimeMs
342
+ const stat = statSync(file)
343
+ STATE_CACHE_MTIME = stat.mtimeMs
344
+ STATE_CACHE_SIZE = stat.size
290
345
  } catch {
291
346
  STATE_CACHE_MTIME = Date.now()
347
+ STATE_CACHE_SIZE = -1
292
348
  }
293
349
  }
294
350
 
@@ -353,12 +409,18 @@ export function parseGitUrl(url) {
353
409
  return null
354
410
  }
355
411
 
356
- /** Suggested display name for a source: repo name simplified & capitalized; official repo → "Anthropic". */
357
- export function suggestSourceName(url) {
412
+ function formatMarketLabel(name) {
413
+ // Keep spaced labels intact and preserve casing within each slug word.
414
+ if (/\s/.test(name)) return name
415
+ return name.replace(/[-_]+/g, ' ').replace(/(^| )([a-z])/g, (_, space, initial) => space + initial.toUpperCase())
416
+ }
417
+
418
+ /** Prefer the market title and fall back to a readable Git repository name. */
419
+ export function suggestSourceName(url, marketName) {
358
420
  const parsed = parseGitUrl(url)
359
421
  if (!parsed) {
360
422
  const raw = String(url).split('/').pop()?.replace(/\.git$/, '') || 'Custom'
361
- return raw.charAt(0).toUpperCase() + raw.slice(1)
423
+ return formatMarketLabel(marketName || raw)
362
424
  }
363
425
  const { owner, repo } = parsed
364
426
  const ownerLower = (owner || '').toLowerCase()
@@ -369,10 +431,12 @@ export function suggestSourceName(url) {
369
431
  if (repoLower === 'claude-plugins-community') return 'Community'
370
432
 
371
433
  const genericRepoNames = new Set([
372
- 'skills', 'plugins', 'agents', 'tools', 'marketplace', 'hub',
373
- 'claude-skills', 'claude-plugins', 'claude-agents', 'dsh-plugins', 'extensions', 'packages',
434
+ 'skill', 'skills', 'plugin', 'plugins', 'agent', 'agents', 'tool', 'tools', 'marketplace', 'hub',
435
+ 'claude-skills', 'claude-plugins', 'claude-agents', 'dsh-plugins', 'extensions', 'packages', 'repo', 'repository', 'custom',
374
436
  ])
375
437
 
438
+ if (marketName && !genericRepoNames.has(marketName.toLowerCase())) return formatMarketLabel(marketName)
439
+
376
440
  // If repo is generic (e.g. "skills", "plugins", "tools"), use capitalized owner (e.g. Mattpocock)
377
441
  if (genericRepoNames.has(repoLower) && owner) {
378
442
  return owner.charAt(0).toUpperCase() + owner.slice(1)
@@ -382,7 +446,10 @@ export function suggestSourceName(url) {
382
446
  .replace(/^(claude|dsh|anthropic)[-_]?(plugins?)?[-_]?/i, '')
383
447
  .replace(/[-_]?(plugins?|skills?|agents?|official|marketplace)$/i, '')
384
448
  const name = simplified || repo
385
- return name.charAt(0).toUpperCase() + name.slice(1)
449
+ if (genericRepoNames.has(name.toLowerCase()) && owner) {
450
+ return owner.charAt(0).toUpperCase() + owner.slice(1)
451
+ }
452
+ return formatMarketLabel(name)
386
453
  }
387
454
 
388
455
  /** Find an existing source that duplicates the given id, normalized URL, or git repo. */
@@ -416,3 +483,14 @@ export function findDuplicateSource(state, id, url) {
416
483
  return null
417
484
  }
418
485
 
486
+ /**
487
+ * Read an optional Cordis service without tripping its inject proxy check
488
+ * (`ctx.get` returns `undefined` when missing or inactive), falling back to
489
+ * direct property lookup only on plain mock objects without `.get`.
490
+ */
491
+ export function resolveOptionalService(ctx, name) {
492
+ if (!ctx || typeof name !== 'string') return undefined
493
+ if (typeof ctx.get === 'function') return ctx.get(name)
494
+ return ctx[name]
495
+ }
496
+