import { reservedSkillName } from './package-skills.js'; import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'; import { join } from 'node:path'; import type { AuthProfile } from './auth.js'; import { classifyStrategyLifecycle, registerDiscoveredCapability, type CapabilityWorkspaceEntry, } from './capability-workspace.js'; import { listPersonaPresets } from './persona.js'; import { readSkillName } from './skill-host.js'; import { behaviorOperationName, perceptionOperationName } from './capability-operation-name.js'; import { ensureAccountUserDataLayout, getUserDataAccountBehaviorsDir, getUserDataAccountPerceptionsDir, getUserDataAccountPersonasDir, getUserDataAccountSkillsDir, } from './user-data.js'; import { listClawclawBehaviorSources, type ResolvedClawclawBehaviorSource, } from '../runtime/clawclaw/plugins/behavior/behavior-source.js'; import { listClawclawPerceptionSourcesInDirectory, type ResolvedClawclawPerceptionSource, } from '../runtime/clawclaw/plugins/perception/perception-source.js'; export interface DiscoveredLocalCapabilities { behaviors: Array<{ source: ResolvedClawclawBehaviorSource; entry: CapabilityWorkspaceEntry }>; perceptions: Array<{ source: ResolvedClawclawPerceptionSource; entry: CapabilityWorkspaceEntry }>; personas: CapabilityWorkspaceEntry[]; skills: CapabilityWorkspaceEntry[]; reservedSkills: Array<{ name: string; path: string; reason: 'reserved_skill_name' }>; } export function discoverLocalCapabilities( workspaceDir: string, profile: Pick, ): DiscoveredLocalCapabilities { ensureAccountUserDataLayout(workspaceDir, profile); const behaviors = listClawclawBehaviorSources( getUserDataAccountBehaviorsDir(workspaceDir, profile), ).map((source) => ({ source, entry: registerDiscoveredCapability(workspaceDir, profile, { type: 'behavior', path: source.sourcePath, runtimeId: source.id, title: source.id, description: source.description, strategyLifecycle: classifyStrategyLifecycle(readFileSync(source.sourcePath, 'utf8')), }), })); const perceptions = listClawclawPerceptionSourcesInDirectory( getUserDataAccountPerceptionsDir(workspaceDir, profile), ).map((source) => ({ source, entry: registerDiscoveredCapability(workspaceDir, profile, { type: 'perception', path: source.sourcePath, runtimeId: source.id, title: source.id, description: source.description, }), })); const personas = listPersonaPresets( getUserDataAccountPersonasDir(workspaceDir, profile), ).map((preset) => registerDiscoveredCapability(workspaceDir, profile, { type: 'persona', path: preset.path, runtimeId: preset.id, title: preset.name, description: `Persona ${preset.name}`, })); const skillsRoot = getUserDataAccountSkillsDir(workspaceDir, profile); const reservedSkills: DiscoveredLocalCapabilities['reservedSkills'] = []; const skills = existsSync(skillsRoot) ? readdirSync(skillsRoot, { withFileTypes: true }).flatMap((item) => { if (!item.isDirectory()) return []; const path = join(skillsRoot, item.name); try { if (!statSync(path).isDirectory()) return []; } catch { return []; } const name = readSkillName(path); const reserved = reservedSkillName(name, path); if (reserved) { reservedSkills.push({ name: reserved, path, reason: 'reserved_skill_name' }); return []; } return name ? [registerDiscoveredCapability(workspaceDir, profile, { type: 'skill', path, runtimeId: name, title: name, description: `Skill ${name}`, })] : []; }) : []; return { behaviors: behaviors.map(({ source, entry }) => ({ source: behaviorOperationName(source, entry), entry })), perceptions: perceptions.map(({ source, entry }) => ({ source: perceptionOperationName(source, entry), entry })), personas, skills, reservedSkills, }; }