resolveSkill function

Future<SkillResult> resolveSkill({
  1. String? topic,
  2. bool allowDiskLookup = true,
})

Resolves and reads the bundled skill file from the package root, falling back to the embedded copy in bundledSkillFiles for standalone AOT builds.

Without topic, reads the core lib/skill/SKILL.md (install instructions, fdb_helper setup, and the full command index with topic routing).

With topic (e.g. "launch", "interact"), reads the corresponding lib/skill/<TOPIC>.md sub-document. Topic matching is case-insensitive.

Available topics: launch, interact, data, diagnostics, memory, simulator.

The authoritative skill content lives under lib/skill/ (internal, bundled with the CLI) and is mirrored in bundledSkillFiles so standalone executables compiled via dart install or dart compile exe work without source files on disk.

Implementation

Future<SkillResult> resolveSkill({
  String? topic,
  bool allowDiskLookup = true,
}) async {
  final filename = topic != null ? '${topic.toUpperCase()}.md' : 'SKILL.md';
  const relativeDir = 'lib/skill/';

  if (allowDiskLookup) {
    // Primary: resolve via package URI — works for `dart run` and
    // `dart pub global activate` installs.
    try {
      final packageUri = Uri.parse('package:fdb/core/commands/skill/skill.dart');
      final resolved = await Isolate.resolvePackageUri(packageUri);
      if (resolved != null && resolved.isScheme('file')) {
        // Five .parent calls: skill.dart -> skill/ -> commands/ -> core/ -> lib/ -> package root
        final packageRoot = File.fromUri(resolved).parent.parent.parent.parent.parent;
        final candidate = File('${packageRoot.path}/$relativeDir$filename');
        if (candidate.existsSync()) {
          return SkillContent(candidate.readAsStringSync());
        }
      }
    } catch (_) {}

    // Secondary: resolve relative to Platform.script (`bin/fdb.dart`) when file-backed.
    try {
      final scriptUri = Platform.script;
      if (scriptUri.isScheme('file')) {
        final scriptDir = Directory.fromUri(scriptUri).parent;
        final packageRoot = scriptDir.parent;
        final fallback = File('${packageRoot.path}/$relativeDir$filename');
        if (fallback.existsSync()) {
          return SkillContent(fallback.readAsStringSync());
        }
      }
    } catch (_) {}
  }

  final bundled = bundledSkillFiles[filename];
  if (bundled != null) {
    return SkillContent(bundled);
  }

  if (topic != null) {
    return SkillTopicNotFound(topic);
  }
  return const SkillNotFound();
}