File size: 25,488 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import { basename, join, sep } from 'node:path'
import { getBuiltinPluginDefinition } from '../../plugins/builtinPlugins.js'
import type { McpServerConfig } from '../../services/mcp/types.js'
import {
  disablePluginOp,
  enablePluginOp,
  type InstallableScope,
  uninstallPluginOp,
  updatePluginOp,
} from '../../services/plugins/pluginOperations.js'
import { getAgentDefinitionsWithOverrides } from '../../tools/AgentTool/loadAgentsDir.js'
import type { LoadedPlugin, PluginError } from '../../types/plugin.js'
import { getPluginErrorMessage } from '../../types/plugin.js'
import { clearAllCaches } from '../../utils/plugins/cacheUtils.js'
import {
  getMarketplaceSourceDisplay,
} from '../../utils/plugins/marketplaceHelpers.js'
import { loadInstalledPluginsV2 } from '../../utils/plugins/installedPluginsManager.js'
import {
  loadKnownMarketplacesConfig,
} from '../../utils/plugins/marketplaceManager.js'
import { loadPluginLspServers } from '../../utils/plugins/lspPluginIntegration.js'
import { loadPluginMcpServers } from '../../utils/plugins/mcpPluginIntegration.js'
import { parsePluginIdentifier } from '../../utils/plugins/pluginIdentifier.js'
import { loadAllPlugins } from '../../utils/plugins/pluginLoader.js'
import { loadPluginHooks } from '../../utils/plugins/loadPluginHooks.js'
import { getPluginCommands } from '../../utils/plugins/loadPluginCommands.js'
import { clearPluginCacheExclusions } from '../../utils/plugins/orphanedPluginFilter.js'
import { parseFrontmatter } from '../../utils/frontmatterParser.js'
import { extractDescriptionFromMarkdown } from '../../utils/markdownConfigLoader.js'
import { resetSettingsCache } from '../../utils/settings/settingsCache.js'
import type {
  PluginInstallationEntry,
  PluginScope,
} from '../../utils/plugins/schemas.js'
import { ApiError } from '../middleware/errorHandler.js'
import { walkPluginMarkdown } from '../../utils/plugins/walkPluginMarkdown.js'
import type { HookCommand, HooksSettings } from '../../utils/settings/types.js'

export type ApiPluginCapabilitySet = {
  commands: string[]
  agents: string[]
  skills: string[]
  hooks: string[]
  mcpServers: string[]
  lspServers: string[]
}

export type ApiPluginSummary = {
  id: string
  name: string
  marketplace: string
  scope: PluginScope | 'builtin'
  enabled: boolean
  hasErrors: boolean
  isBuiltin: boolean
  version?: string
  description?: string
  authorName?: string
  installPath?: string
  projectPath?: string
  componentCounts: Record<keyof ApiPluginCapabilitySet, number>
  errors: string[]
}

export type ApiPluginSkillEntry = {
  name: string
  displayName?: string
  description: string
  version?: string
  pluginName?: string
}

export type ApiPluginCommandEntry = {
  name: string
  description: string
}

export type ApiPluginAgentEntry = {
  name: string
  displayName?: string
  description: string
}

export type ApiPluginHookEntry = {
  event: string
  matcher?: string
  actions: string[]
}

export type ApiPluginMcpServerEntry = {
  name: string
  displayName?: string
  transport: string
  summary: string
}

export type ApiPluginDetail = ApiPluginSummary & {
  capabilities: ApiPluginCapabilitySet
  commandEntries: ApiPluginCommandEntry[]
  agentEntries: ApiPluginAgentEntry[]
  hookEntries: ApiPluginHookEntry[]
  skillEntries: ApiPluginSkillEntry[]
  mcpServerEntries: ApiPluginMcpServerEntry[]
}

export type ApiPluginMarketplaceSummary = {
  name: string
  source: string
  lastUpdated?: string
  autoUpdate: boolean
  installedCount: number
}

export type ApiPluginListResponse = {
  plugins: ApiPluginSummary[]
  marketplaces: ApiPluginMarketplaceSummary[]
  summary: {
    total: number
    enabled: number
    errorCount: number
    marketplaceCount: number
  }
}

export type ApiPluginActionResponse = {
  ok: true
  message: string
}

export type ApiPluginReloadResponse = {
  ok: true
  summary: {
    enabled: number
    disabled: number
    skills: number
    agents: number
    hooks: number
    mcpServers: number
    lspServers: number
    errors: number
  }
}

type HydratedPluginState = {
  enabled: LoadedPlugin[]
  disabled: LoadedPlugin[]
  errors: PluginError[]
}

export class PluginService {
  async listPlugins(cwd?: string): Promise<ApiPluginListResponse> {
    const { plugins, marketplaces } = await this.collectPluginState(cwd)
    return {
      plugins,
      marketplaces,
      summary: {
        total: plugins.length,
        enabled: plugins.filter((plugin) => plugin.enabled).length,
        errorCount: plugins.reduce((sum, plugin) => sum + plugin.errors.length, 0),
        marketplaceCount: marketplaces.length,
      },
    }
  }

  async getPluginDetail(
    pluginId: string,
    cwd?: string,
  ): Promise<ApiPluginDetail> {
    const { plugins, detailById } = await this.collectPluginState(cwd)
    const detail = detailById.get(pluginId)

    if (!detail) {
      throw ApiError.notFound(`Plugin not found: ${pluginId}`)
    }

    return detail
  }

  async enablePlugin(
    pluginId: string,
    scope?: InstallableScope,
  ): Promise<ApiPluginActionResponse> {
    const result = await enablePluginOp(pluginId, scope)
    if (!result.success) {
      throw ApiError.badRequest(result.message)
    }
    return { ok: true, message: result.message }
  }

  async disablePlugin(
    pluginId: string,
    scope?: InstallableScope,
  ): Promise<ApiPluginActionResponse> {
    const result = await disablePluginOp(pluginId, scope)
    if (!result.success) {
      throw ApiError.badRequest(result.message)
    }
    return { ok: true, message: result.message }
  }

  async uninstallPlugin(
    pluginId: string,
    scope?: InstallableScope,
    keepData = false,
  ): Promise<ApiPluginActionResponse> {
    if (!scope) {
      throw ApiError.badRequest('Plugin uninstall requires a scope')
    }

    const result = await uninstallPluginOp(pluginId, scope, keepData)
    if (!result.success) {
      throw ApiError.badRequest(result.message)
    }
    return { ok: true, message: result.message }
  }

  async updatePlugin(
    pluginId: string,
    scope?: PluginScope,
  ): Promise<ApiPluginActionResponse> {
    if (!scope) {
      throw ApiError.badRequest('Plugin update requires a scope')
    }

    const result = await updatePluginOp(pluginId, scope)
    if (!result.success) {
      throw ApiError.badRequest(result.message)
    }
    return { ok: true, message: result.message }
  }

  async reloadPlugins(cwd?: string): Promise<ApiPluginReloadResponse> {
    resetSettingsCache()
    clearAllCaches()
    clearPluginCacheExclusions()

    const pluginState = await this.loadPluginState()
    const { enabled, disabled, errors } = pluginState

    const [commands, agentDefinitions] = await Promise.all([
      getPluginCommands(),
      getAgentDefinitionsWithOverrides(cwd),
    ])

    const hookCount = await this.getHookCount(enabled)
    const mcpCounts = await Promise.all(
      enabled.map(async (plugin) => {
        const servers = plugin.mcpServers || await loadPluginMcpServers(plugin, errors)
        return servers ? Object.keys(servers).length : 0
      }),
    )
    const lspCounts = await Promise.all(
      enabled.map(async (plugin) => {
        const servers = plugin.lspServers || await loadPluginLspServers(plugin, errors)
        return servers ? Object.keys(servers).length : 0
      }),
    )

    return {
      ok: true,
      summary: {
        enabled: enabled.length,
        disabled: disabled.length,
        skills: commands.length,
        agents: agentDefinitions.allAgents.length,
        hooks: hookCount,
        mcpServers: mcpCounts.reduce((sum, count) => sum + count, 0),
        lspServers: lspCounts.reduce((sum, count) => sum + count, 0),
        errors: errors.length,
      },
    }
  }

  private async collectPluginState(cwd?: string): Promise<{
    plugins: ApiPluginSummary[]
    detailById: Map<string, ApiPluginDetail>
    marketplaces: ApiPluginMarketplaceSummary[]
  }> {
    const [pluginState, installedData, marketplaceConfig] = await Promise.all([
      this.loadPluginState(),
      Promise.resolve(loadInstalledPluginsV2()),
      loadKnownMarketplacesConfig(),
    ])

    const allLoaded = [...pluginState.enabled, ...pluginState.disabled]
    const loadedById = new Map(
      allLoaded
        .filter((plugin) => !plugin.source.endsWith('@inline'))
        .map((plugin) => [plugin.source, plugin]),
    )

    const pluginIds = new Set<string>([
      ...Object.keys(installedData.plugins),
      ...allLoaded
        .filter((plugin) => !plugin.source.endsWith('@inline'))
        .map((plugin) => plugin.source),
    ])

    const detailById = new Map<string, ApiPluginDetail>()

    for (const pluginId of [...pluginIds].sort()) {
      const installation = this.pickInstallation(
        installedData.plugins[pluginId] ?? [],
        cwd,
      )
      const loaded = loadedById.get(pluginId)
      const detail = await this.serializePluginDetail(
        pluginId,
        installation,
        loaded,
        pluginState.errors,
      )
      detailById.set(pluginId, detail)
    }

    const plugins = [...detailById.values()].map((detail) =>
      this.toSummary(detail),
    )

    const marketplaces = Object.entries(marketplaceConfig.marketplaces ?? {})
      .map(([name, entry]) => ({
        name,
        source: getMarketplaceSourceDisplay(entry.source),
        lastUpdated: entry.lastUpdated,
        autoUpdate: entry.autoUpdate !== false,
        installedCount: plugins.filter((plugin) => plugin.marketplace === name).length,
      }))
      .sort((a, b) => a.name.localeCompare(b.name))

    return { plugins, detailById, marketplaces }
  }

  private async loadPluginState(): Promise<HydratedPluginState> {
    const result = await loadAllPlugins()
    await Promise.all(
      result.enabled.map(async (plugin) => {
        plugin.mcpServers = plugin.mcpServers || await loadPluginMcpServers(plugin, result.errors)
        plugin.lspServers = plugin.lspServers || await loadPluginLspServers(plugin, result.errors)
      }),
    )
    return result
  }

  private async serializePluginDetail(
    pluginId: string,
    installation: PluginInstallationEntry | null,
    loaded: LoadedPlugin | undefined,
    errors: PluginError[],
  ): Promise<ApiPluginDetail> {
    const { name, marketplace } = parsePluginIdentifier(pluginId)
    const pluginErrors = this.getErrorsForPlugin(pluginId, name, errors)

    if (!loaded) {
      return {
        id: pluginId,
        name,
        marketplace: marketplace || 'unknown',
        scope: installation?.scope ?? 'user',
        enabled: false,
        hasErrors: pluginErrors.length > 0,
        isBuiltin: false,
        installPath: installation?.installPath,
        projectPath: installation?.projectPath,
        errors: pluginErrors,
        componentCounts: this.countCapabilities(this.emptyCapabilities()),
        capabilities: this.emptyCapabilities(),
        commandEntries: [],
        agentEntries: [],
        hookEntries: [],
        skillEntries: [],
        mcpServerEntries: [],
      }
    }

    const {
      capabilities,
      commandEntries,
      agentEntries,
      hookEntries,
      skillEntries,
      mcpServerEntries,
    } = await this.collectCapabilities(loaded)
    return {
      id: pluginId,
      name: loaded.name,
      marketplace: marketplace || 'unknown',
      scope: installation?.scope ?? 'user',
      enabled: loaded.enabled !== false,
      hasErrors: pluginErrors.length > 0,
      isBuiltin: Boolean(loaded.isBuiltin),
      version: loaded.manifest.version,
      description: loaded.manifest.description,
      authorName: loaded.manifest.author?.name,
      installPath: installation?.installPath,
      projectPath: installation?.projectPath,
      errors: pluginErrors,
      componentCounts: this.countCapabilities(capabilities),
      capabilities,
      commandEntries,
      agentEntries,
      hookEntries,
      skillEntries,
      mcpServerEntries,
    }
  }

  private async collectCapabilities(
    plugin: LoadedPlugin,
  ): Promise<{
    capabilities: ApiPluginCapabilitySet
    commandEntries: ApiPluginCommandEntry[]
    agentEntries: ApiPluginAgentEntry[]
    hookEntries: ApiPluginHookEntry[]
    skillEntries: ApiPluginSkillEntry[]
    mcpServerEntries: ApiPluginMcpServerEntry[]
  }> {
    if (plugin.isBuiltin) {
      const definition = getBuiltinPluginDefinition(plugin.name)
      const skillEntries = (definition?.skills ?? []).map((skill) => ({
        name: skill.name,
        description: skill.description,
      }))
      const mcpServerEntries = Object.entries(definition?.mcpServers ?? {}).map(([serverName, config]) => ({
        name: `plugin:${plugin.name}:${serverName}`,
        displayName: serverName,
        transport: this.getPluginMcpTransport(config),
        summary: this.getPluginMcpSummary(config),
      }))

      return {
        capabilities: {
          commands: [],
          agents: [],
          skills: skillEntries.map((skill) => skill.name),
          hooks: definition?.hooks ? Object.keys(definition.hooks) : [],
          mcpServers: mcpServerEntries.map((server) => server.name),
          lspServers: [],
        },
        commandEntries: [],
        agentEntries: [],
        hookEntries: this.collectHookEntries(definition?.hooks),
        skillEntries,
        mcpServerEntries,
      }
    }

    const commandEntries = await this.collectCommandEntries(plugin)
    const agentEntries = await this.collectAgentEntries(plugin)
    const hookEntries = this.collectHookEntries(plugin.hooksConfig)
    const skillEntries = await this.collectSkillEntries([
      plugin.skillsPath,
      ...(plugin.skillsPaths ?? []),
    ], plugin.name)
    const mcpServerEntries = this.collectMcpServerEntries(plugin.name, plugin.mcpServers)

    return {
      capabilities: {
        commands: commandEntries.map((command) => command.name),
        agents: agentEntries.map((agent) => agent.name),
        skills: skillEntries.map((skill) => skill.name),
        hooks: [...new Set(hookEntries.map((hook) => hook.event))],
        mcpServers: mcpServerEntries.map((server) => server.name),
        lspServers: plugin.lspServers ? Object.keys(plugin.lspServers) : [],
      },
      commandEntries,
      agentEntries,
      hookEntries,
      skillEntries,
      mcpServerEntries,
    }
  }

  private async collectCommandEntries(
    plugin: LoadedPlugin,
  ): Promise<ApiPluginCommandEntry[]> {
    return this.collectMarkdownEntriesWithDescriptions(
      plugin.name,
      [plugin.commandsPath, ...(plugin.commandsPaths ?? [])],
      { stopAtSkillDir: true, useNamespace: true },
    )
  }

  private async collectAgentEntries(
    plugin: LoadedPlugin,
  ): Promise<ApiPluginAgentEntry[]> {
    return this.collectMarkdownEntriesWithDescriptions(
      plugin.name,
      [plugin.agentsPath, ...(plugin.agentsPaths ?? [])],
      { stopAtSkillDir: false, useNamespace: true, preferFrontmatterName: true },
    )
  }

  private async collectMarkdownEntries(paths: Array<string | undefined>): Promise<string[]> {
    const fs = await import('node:fs/promises')
    const names = new Set<string>()

    for (const dirPath of paths) {
      if (!dirPath) continue

      try {
        const dirEntries = await fs.readdir(dirPath, { withFileTypes: true })
        for (const entry of dirEntries) {
          if (!entry.isFile() || !entry.name.endsWith('.md')) continue
          names.add(entry.name.replace(/\.md$/i, ''))
        }
      } catch {
        // Ignore unreadable plugin component directories and keep rendering.
      }
    }

    return [...names].sort()
  }

  private async collectMarkdownEntriesWithDescriptions(
    pluginName: string,
    paths: Array<string | undefined>,
    options: {
      stopAtSkillDir: boolean
      useNamespace: boolean
      preferFrontmatterName?: boolean
    },
  ): Promise<Array<{ name: string; displayName?: string; description: string }>> {
    const fs = await import('node:fs/promises')
    const entries = new Map<string, { name: string; displayName?: string; description: string }>()

    for (const rootPath of paths) {
      if (!rootPath) continue

      await walkPluginMarkdown(
        rootPath,
        async (fullPath, namespace) => {
          const raw = await fs.readFile(fullPath, 'utf-8')
          const parsed = parseFrontmatter(raw, fullPath)
          const baseName = basename(fullPath).replace(/\.md$/i, '')
          const resolvedLeafName =
            options.preferFrontmatterName &&
            typeof parsed.frontmatter.name === 'string' &&
            parsed.frontmatter.name.trim().length > 0
              ? parsed.frontmatter.name.trim()
              : /^skill$/i.test(baseName)
                ? basename(join(fullPath, '..'))
                : baseName
          const leafName = resolvedLeafName
          const name = options.useNamespace && namespace.length > 0
            ? `${pluginName}:${namespace.join(':')}:${leafName}`
            : options.useNamespace
              ? `${pluginName}:${leafName}`
              : leafName
          const description =
            (typeof parsed.frontmatter.description === 'string' && parsed.frontmatter.description.trim()) ||
            (typeof parsed.frontmatter['when-to-use'] === 'string' && parsed.frontmatter['when-to-use'].trim()) ||
            extractDescriptionFromMarkdown(parsed.content, 'No description')

          if (!entries.has(name)) {
            entries.set(name, {
              name,
              displayName: leafName !== name ? leafName : undefined,
              description,
            })
          }
        },
        {
          stopAtSkillDir: options.stopAtSkillDir,
          logLabel: options.useNamespace ? 'plugin-details' : 'markdown',
        },
      )
    }

    return [...entries.values()].sort((a, b) => a.name.localeCompare(b.name))
  }

  private async collectSkillEntries(
    paths: Array<string | undefined>,
    pluginName: string,
  ): Promise<ApiPluginSkillEntry[]> {
    const fs = await import('node:fs/promises')
    const skillEntriesByName = new Map<string, ApiPluginSkillEntry>()

    for (const dirPath of paths) {
      if (!dirPath) continue

      try {
        const directSkill = await this.readPluginSkillEntry(dirPath, pluginName)
        if (directSkill && !skillEntriesByName.has(directSkill.name)) {
          skillEntriesByName.set(directSkill.name, directSkill)
          continue
        }
      } catch {
        // Fall back to scanning as a skill root.
      }

      try {
        const dirEntries = await fs.readdir(dirPath, { withFileTypes: true })
        for (const entry of dirEntries) {
          if (!entry.isDirectory() && !entry.isSymbolicLink()) continue

          try {
            const skillEntry = await this.readPluginSkillEntry(join(dirPath, entry.name), pluginName)
            if (skillEntry && !skillEntriesByName.has(skillEntry.name)) {
              skillEntriesByName.set(skillEntry.name, skillEntry)
            }
          } catch {
            // Ignore non-skill directories.
          }
        }
      } catch {
        // Ignore unreadable plugin component directories and keep rendering.
      }
    }

    return [...skillEntriesByName.values()].sort((a, b) => a.name.localeCompare(b.name))
  }

  private async readPluginSkillEntry(
    skillDir: string,
    pluginName: string,
  ): Promise<ApiPluginSkillEntry | null> {
    const fs = await import('node:fs/promises')
    const skillFile = join(skillDir, 'SKILL.md')
    try {
      const stat = await fs.stat(skillFile)
      if (!stat.isFile()) return null

      const raw = await fs.readFile(skillFile, 'utf-8')
      const parsed = parseFrontmatter(raw, skillFile)
      const body = parsed.content
      const name = typeof parsed.frontmatter.name === 'string' && parsed.frontmatter.name.trim().length > 0
        ? parsed.frontmatter.name.trim()
        : basename(skillDir)

      const description =
        (typeof parsed.frontmatter.description === 'string' && parsed.frontmatter.description.trim()) ||
        body
          .split('\n')
          .find((line) => line.trim().length > 0)
          ?.trim() ||
        'No description'

      return {
        name: `${pluginName}:${basename(skillDir)}`,
        displayName: name !== basename(skillDir) ? name : undefined,
        description,
        version: parsed.frontmatter.version != null ? String(parsed.frontmatter.version) : undefined,
        pluginName,
      }
    } catch {
      return null
    }
  }

  private collectMcpServerEntries(
    pluginName: string,
    servers?: Record<string, McpServerConfig>,
  ): ApiPluginMcpServerEntry[] {
    return Object.entries(servers ?? {})
      .map(([name, config]) => ({
        name: `plugin:${pluginName}:${name}`,
        displayName: name,
        transport: this.getPluginMcpTransport(config),
        summary: this.getPluginMcpSummary(config),
      }))
      .sort((a, b) => a.name.localeCompare(b.name))
  }

  private collectHookEntries(hooks?: HooksSettings): ApiPluginHookEntry[] {
    const entries: ApiPluginHookEntry[] = []
    for (const [event, matchers] of Object.entries(hooks ?? {})) {
      for (const matcher of matchers ?? []) {
        entries.push({
          event,
          matcher: matcher.matcher,
          actions: matcher.hooks.map((hook) => this.describeHookAction(hook)),
        })
      }
    }

    return entries.sort((a, b) => {
      if (a.event !== b.event) return a.event.localeCompare(b.event)
      return (a.matcher ?? '').localeCompare(b.matcher ?? '')
    })
  }

  private describeHookAction(hook: HookCommand): string {
    switch (hook.type) {
      case 'command':
        return hook.command
      case 'prompt':
        return hook.prompt
      case 'agent':
        return hook.prompt
      case 'http':
        return hook.url
      default:
        return hook.type
    }
  }

  private getPluginMcpTransport(config: McpServerConfig): string {
    return config.type ?? 'stdio'
  }

  private getPluginMcpSummary(config: McpServerConfig): string {
    if (!config.type || config.type === 'stdio') {
      const stdioConfig = config as McpServerConfig & {
        command?: string
        args?: string[]
      }
      return [stdioConfig.command, ...(stdioConfig.args ?? [])].filter(Boolean).join(' ').trim()
    }

    if ('url' in config && typeof config.url === 'string') {
      return config.url
    }

    return config.type
  }

  private getErrorsForPlugin(
    pluginId: string,
    pluginName: string,
    errors: PluginError[],
  ): string[] {
    return errors
      .filter((error) => {
        if (error.source === pluginId) return true
        if ('plugin' in error && error.plugin === pluginName) return true
        return error.source.startsWith(`${pluginName}@`)
      })
      .map(getPluginErrorMessage)
  }

  private pickInstallation(
    installations: PluginInstallationEntry[],
    cwd?: string,
  ): PluginInstallationEntry | null {
    if (!installations.length) return null

    const relevantForCwd = cwd
      ? installations.filter((entry) =>
          entry.projectPath ? this.isPathWithinProject(cwd, entry.projectPath) : false,
        )
      : []

    const localMatch = relevantForCwd.find((entry) => entry.scope === 'local')
    if (localMatch) return localMatch

    const projectMatch = relevantForCwd.find((entry) => entry.scope === 'project')
    if (projectMatch) return projectMatch

    const userMatch = installations.find((entry) => entry.scope === 'user')
    if (userMatch) return userMatch

    return installations[0] ?? null
  }

  private isPathWithinProject(cwd: string, projectPath: string): boolean {
    return cwd === projectPath || cwd.startsWith(`${projectPath}${sep}`)
  }

  private emptyCapabilities(): ApiPluginCapabilitySet {
    return {
      commands: [],
      agents: [],
      skills: [],
      hooks: [],
      mcpServers: [],
      lspServers: [],
    }
  }

  private countCapabilities(
    capabilities: ApiPluginCapabilitySet,
  ): Record<keyof ApiPluginCapabilitySet, number> {
    return {
      commands: capabilities.commands.length,
      agents: capabilities.agents.length,
      skills: capabilities.skills.length,
      hooks: capabilities.hooks.length,
      mcpServers: capabilities.mcpServers.length,
      lspServers: capabilities.lspServers.length,
    }
  }

  private toSummary(detail: ApiPluginDetail): ApiPluginSummary {
    return {
      id: detail.id,
      name: detail.name,
      marketplace: detail.marketplace,
      scope: detail.scope,
      enabled: detail.enabled,
      hasErrors: detail.hasErrors,
      isBuiltin: detail.isBuiltin,
      version: detail.version,
      description: detail.description,
      authorName: detail.authorName,
      installPath: detail.installPath,
      projectPath: detail.projectPath,
      componentCounts: detail.componentCounts,
      errors: detail.errors,
    }
  }

  private async getHookCount(plugins: LoadedPlugin[]): Promise<number> {
    try {
      await loadPluginHooks()
    } catch {
      // Hook loading failures are already represented in the shared plugin errors.
    }

    return plugins.reduce((sum, plugin) => {
      if (!plugin.hooksConfig) return sum
      return sum + Object.values(plugin.hooksConfig).reduce((hookSum, matchers) => (
        hookSum + (matchers?.reduce((matcherSum, matcher) => matcherSum + matcher.hooks.length, 0) ?? 0)
      ), 0)
    }, 0)
  }
}