skills-config.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Skills external-source ingestion config (Unified Skill Ingestion).
  3. *
  4. * — user-authored skill directories that Mavis honours as external sources
  5. * independent of which runtime executes the agent session. See plan
  6. * `~/.claude/plans/ok-image-1-unified-tower.md`.
  7. *
  8. * Schema reasoning:
  9. * - `enabled` master switch: legal/compliance kill-switch in case any
  10. * enterprise deployment needs all external ingestion off.
  11. * - `walkUp` toggles whether daemon walks parent dirs up to the git root
  12. * when discovering project-level `.claude` / `.agents`. Matches the
  13. * compatible external skill discovery behavior.
  14. * - `duplicateWarn` controls collision diagnostics for same-named skills
  15. * across sources. The visible skill roster still keeps only the priority
  16. * winner.
  17. * - `sources`: per-source-kind `{ enabled, priority }`. Higher priority
  18. * wins on name collision. Only the externally-discoverable sources are
  19. * surfaced — Mavis-internal sources (`builtin-*`, `agent-*`, `user-mavis`)
  20. * are not user-configurable here (handled by the SkillStore directly).
  21. */
  22. const EXTERNAL_SOURCE_KINDS = [
  23. 'user-cc',
  24. 'user-codex',
  25. 'user-agents',
  26. 'workspace-minimax',
  27. 'workspace-cc',
  28. 'workspace-agents',
  29. ] as const;
  30. export type ExternalSourceKindLabel = (typeof EXTERNAL_SOURCE_KINDS)[number];
  31. // Persisted config identifiers from before the source-kind rename. Keep them
  32. // as read-only aliases; normalized output always uses the current keys above.
  33. const LEGACY_SOURCE_KEY_BY_KIND: Partial<Record<ExternalSourceKindLabel, string>> = {
  34. 'user-cc': 'user-claude',
  35. 'workspace-cc': 'workspace-claude',
  36. };
  37. export interface SkillsExternalSourceConfig {
  38. enabled: boolean;
  39. /** Higher wins on name collision. Defaults from `DEFAULT_SOURCE_PRIORITY`. */
  40. priority: number;
  41. }
  42. export interface SkillsExternalConfig {
  43. /** Master switch. When false, none of the external sources are scanned. */
  44. enabled: boolean;
  45. /** Walk parent dirs up to the nearest `.git/` collecting `.claude/.agents`. */
  46. walkUp: boolean;
  47. /** Log diagnostics for duplicate skill names; visible roster remains first-wins. */
  48. duplicateWarn: boolean;
  49. /** Per-source-kind enable + priority. */
  50. sources: Record<ExternalSourceKindLabel, SkillsExternalSourceConfig>;
  51. }
  52. export interface SkillsConfig {
  53. external: SkillsExternalConfig;
  54. }
  55. export const DEFAULT_SKILLS_CONFIG: SkillsConfig = {
  56. external: {
  57. enabled: true,
  58. walkUp: true,
  59. duplicateWarn: true,
  60. sources: {
  61. 'workspace-minimax': { enabled: true, priority: 65 },
  62. 'workspace-cc': { enabled: true, priority: 60 },
  63. 'workspace-agents': { enabled: true, priority: 55 },
  64. 'user-cc': { enabled: true, priority: 40 },
  65. 'user-codex': { enabled: true, priority: 35 },
  66. 'user-agents': { enabled: true, priority: 30 },
  67. },
  68. },
  69. };
  70. function isObject(v: unknown): v is Record<string, unknown> {
  71. return v != null && typeof v === 'object' && !Array.isArray(v);
  72. }
  73. function parseSource(
  74. raw: unknown,
  75. fallback: SkillsExternalSourceConfig,
  76. ): SkillsExternalSourceConfig {
  77. if (!isObject(raw)) return fallback;
  78. return {
  79. enabled: typeof raw.enabled === 'boolean' ? raw.enabled : fallback.enabled,
  80. priority:
  81. typeof raw.priority === 'number' && Number.isFinite(raw.priority)
  82. ? raw.priority
  83. : fallback.priority,
  84. };
  85. }
  86. export function parseSkillsConfig(raw: Record<string, unknown>): SkillsConfig {
  87. const defaults = DEFAULT_SKILLS_CONFIG;
  88. const skillsObj = isObject(raw.skills) ? (raw.skills as Record<string, unknown>) : {};
  89. const externalObj = isObject(skillsObj.external)
  90. ? (skillsObj.external as Record<string, unknown>)
  91. : {};
  92. const sourcesObj = isObject(externalObj.sources)
  93. ? (externalObj.sources as Record<string, unknown>)
  94. : {};
  95. const sources: Record<ExternalSourceKindLabel, SkillsExternalSourceConfig> = {
  96. ...defaults.external.sources,
  97. };
  98. for (const kind of EXTERNAL_SOURCE_KINDS) {
  99. const legacyKind = LEGACY_SOURCE_KEY_BY_KIND[kind];
  100. const sourceRaw = Object.hasOwn(sourcesObj, kind)
  101. ? sourcesObj[kind]
  102. : legacyKind
  103. ? sourcesObj[legacyKind]
  104. : undefined;
  105. sources[kind] = parseSource(sourceRaw, defaults.external.sources[kind]);
  106. }
  107. return {
  108. external: {
  109. enabled:
  110. typeof externalObj.enabled === 'boolean' ? externalObj.enabled : defaults.external.enabled,
  111. walkUp:
  112. typeof externalObj.walkUp === 'boolean' ? externalObj.walkUp : defaults.external.walkUp,
  113. duplicateWarn:
  114. typeof externalObj.duplicateWarn === 'boolean'
  115. ? externalObj.duplicateWarn
  116. : defaults.external.duplicateWarn,
  117. sources,
  118. },
  119. };
  120. }
  121. export const EXTERNAL_SOURCE_KIND_LIST = EXTERNAL_SOURCE_KINDS;