// @bun
import {
  init_skillLearning
} from "./chunk-pvrbk0yt.js";
import {
  promoteGapToDraft,
  readSkillGaps
} from "./chunk-b0rn9j35.js";
import {
  exportInstincts,
  findPromotionCandidates,
  generateSkillCandidates,
  loadInstincts,
  prunePendingInstincts,
  saveInstinct,
  upsertInstinct
} from "./chunk-1fc8nbhj.js";
import {
  applySkillLifecycleDecision,
  compareExistingSkills,
  decideSkillLifecycle
} from "./chunk-5yva30am.js";
import {
  listKnownProjects,
  resolveProjectContext
} from "./chunk-fqcfzg3j.js";
import {
  analyzeObservations
} from "./chunk-bazy7696.js";
import"./chunk-w9ddp3yf.js";
import"./chunk-03z007bm.js";
import {
  ingestTranscript,
  readObservations
} from "./chunk-v3ey5j7f.js";
import"./chunk-421yegj3.js";
import"./chunk-aaxz8a5g.js";
import"./chunk-2kcgxnvp.js";
import"./chunk-14p6wvsq.js";
import"./chunk-6s0q1s5r.js";
import"./chunk-0cpg8qh8.js";
import"./chunk-9phn5khx.js";
import"./chunk-vk9xcryd.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-ptxteaeh.js";
import"./chunk-7fcmqvbz.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-q3b4n194.js";
import"./chunk-zaqfb3tz.js";
import"./chunk-6hzx0kqr.js";
import"./chunk-71sdcaq6.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-dxa3y9vh.js";
import"./chunk-gjvhjywj.js";
import"./chunk-1w20m7gw.js";
import"./chunk-3gam18xn.js";
import"./chunk-rpmntgyh.js";
import"./chunk-24kv69g3.js";
import"./chunk-tzcrwnrf.js";
import"./chunk-k7xszak2.js";
import"./chunk-ztmb7geg.js";
import"./chunk-tbdkekz3.js";
import"./chunk-65zbgg1n.js";
import"./chunk-4spgkgr3.js";
import"./chunk-60fkafk2.js";
import"./chunk-h5zz0vxy.js";
import"./chunk-6hygvrhn.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-8gv2pjby.js";
import"./chunk-pn23dh3d.js";
import"./chunk-rpdh02rm.js";
import"./chunk-j1wktfe2.js";
import"./chunk-fjtzfz1t.js";
import"./chunk-6hr7742d.js";
import"./chunk-xyg1sk2w.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-hem4f4gz.js";
import"./chunk-hqxp6b72.js";
import"./chunk-ccw8pse0.js";
import"./chunk-wsnxtggw.js";
import"./chunk-k8nwt8yq.js";
import"./chunk-vxbjzggp.js";
import"./chunk-d6h68rz1.js";
import"./chunk-8yvsnrkr.js";
import"./chunk-5mmhfbxb.js";
import"./chunk-s2t5anwv.js";
import"./chunk-ga1jkyqy.js";
import"./chunk-9hbhydvp.js";
import"./chunk-ck9r8hpf.js";
import"./chunk-k7hexw3v.js";
import"./chunk-trqrj23e.js";
import"./chunk-za3k6h2h.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-hjxqmtg5.js";
import"./chunk-bzyzrq0k.js";
import"./chunk-vwenx8ke.js";
import"./chunk-q2h79ncs.js";
import"./chunk-v4ypszbb.js";
import"./chunk-qfsn720k.js";
import"./chunk-7qx3xhv3.js";
import"./chunk-j9475b46.js";
import"./chunk-fdwabr8p.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-dbts5q5p.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import"./chunk-q1vrhh0q.js";
import"./chunk-hn4w9pkj.js";
import"./chunk-2g0ytd9d.js";
import"./chunk-de0qnnf7.js";
import"./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-92bjkrf2.js";
import"./chunk-10k5jfc5.js";
import"./chunk-n9g24mwe.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-hzmzwte2.js";
import"./chunk-pdvg91cg.js";
import"./chunk-c696122m.js";
import"./chunk-xbj5keyy.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import"./chunk-aeysytks.js";
import {
  getClaudeConfigHomeDir,
  init_envUtils
} from "./chunk-s3hafnk3.js";
import"./chunk-kb3758f7.js";
import"./chunk-9h9k5vz3.js";
import"./chunk-xmpgmeb8.js";
import"./chunk-2kz09j2s.js";
import"./chunk-c0k7b0jw.js";
import"./chunk-pecy49yr.js";
import"./chunk-azbab59e.js";
import"./chunk-3nk9q8dr.js";
import {
  __esm,
  __require
} from "./chunk-hhsxm2yr.js";

// src/commands/skill-learning/skill-learning.ts
import { join } from "path";
function parseFlagString(parts, flag) {
  const eqForm = parts.find((p) => p.startsWith(`${flag}=`));
  if (eqForm)
    return eqForm.slice(flag.length + 1) || undefined;
  const idx = parts.indexOf(flag);
  if (idx >= 0 && parts[idx + 1] && !parts[idx + 1].startsWith("--")) {
    return parts[idx + 1];
  }
  return;
}
function parseFlagNumber(parts, flag, fallback) {
  const raw = parseFlagString(parts, flag);
  if (raw === undefined)
    return fallback;
  const value = Number(raw);
  return Number.isFinite(value) ? value : fallback;
}
var call = async (args) => {
  const parts = args.trim().split(/\s+/).filter(Boolean);
  const sub = parts[0] ?? "status";
  const project = resolveProjectContext(process.cwd());
  const rootDir = process.env.CLAUDE_SKILL_LEARNING_HOME;
  const options = { project, rootDir };
  switch (sub) {
    case "status": {
      const [observations, instincts] = await Promise.all([
        readObservations(options),
        loadInstincts(options)
      ]);
      return {
        type: "text",
        value: [
          `Skill Learning status for ${project.projectName} (${project.projectId})`,
          `Observations: ${observations.length}`,
          `Instincts: ${instincts.length}`
        ].join(`
`)
      };
    }
    case "ingest": {
      const transcript = parts[1];
      if (!transcript) {
        return {
          type: "text",
          value: "Usage: /skill-learning ingest <transcript.jsonl> [--min-session-length=<n>]"
        };
      }
      const minSessionLength = parseFlagNumber(parts, "--min-session-length", 10);
      const observations = await ingestTranscript(transcript, options);
      if (observations.length < minSessionLength) {
        return {
          type: "text",
          value: `Session too short for learning (${observations.length} < min=${minSessionLength}). Skipping instinct extraction.`
        };
      }
      const instincts = analyzeObservations(observations);
      const saved = [];
      for (const instinct of instincts) {
        saved.push(await upsertInstinct(instinct, options));
      }
      return {
        type: "text",
        value: `Ingested ${observations.length} observations and saved ${saved.length} instincts.`
      };
    }
    case "evolve": {
      const generate = parts.includes("--generate");
      const instincts = await loadInstincts(options);
      const drafts = generateSkillCandidates(instincts, { cwd: process.cwd() });
      const written = [];
      if (generate) {
        for (const draft of drafts) {
          const roots = [
            join(process.cwd(), ".claude", "skills"),
            join(getClaudeConfigHomeDir(), "skills")
          ];
          const existing = await compareExistingSkills(draft, roots);
          const decision = decideSkillLifecycle(draft, existing);
          const result = await applySkillLifecycleDecision(decision);
          written.push(`${decision.type}: ${result.activePath ?? result.archivedPath ?? result.deletedPath ?? "no active write"}`);
        }
      }
      return {
        type: "text",
        value: generate ? `Generated ${written.length} learned skill(s):
${written.join(`
`)}` : `Found ${drafts.length} skill candidate(s). Use --generate to write them.`
      };
    }
    case "export": {
      const output = parts[1] ?? "skill-learning-instincts.json";
      const scope = parseFlagString(parts, "--scope");
      const minConf = parseFlagNumber(parts, "--min-conf", undefined);
      const domain = parseFlagString(parts, "--domain");
      const filter = (instincts) => instincts.filter((i) => {
        if (scope && i.scope !== scope)
          return false;
        if (minConf !== undefined && i.confidence < minConf)
          return false;
        if (domain && i.domain !== domain)
          return false;
        return true;
      });
      const all = await loadInstincts(options);
      const filtered = filter(all);
      if (filtered.length !== all.length) {
        await exportInstincts(output, options);
        const { writeFile } = await import("fs/promises");
        await writeFile(output, `${JSON.stringify(filtered, null, 2)}
`);
      } else {
        await exportInstincts(output, options);
      }
      const parts2 = [
        `Exported ${filtered.length} instincts to ${output}`
      ];
      if (scope || minConf !== undefined || domain) {
        const filters = [];
        if (scope)
          filters.push(`scope=${scope}`);
        if (minConf !== undefined)
          filters.push(`min-conf=${minConf}`);
        if (domain)
          filters.push(`domain=${domain}`);
        parts2.push(`(filters: ${filters.join(", ")})`);
      }
      return { type: "text", value: parts2.join(" ") };
    }
    case "import": {
      const input = parts[1];
      if (!input) {
        return {
          type: "text",
          value: "Usage: /skill-learning import <instincts.json> [--scope=<scope>] [--min-conf=<n>] [--domain=<d>] [--dry-run]"
        };
      }
      const scope = parseFlagString(parts, "--scope");
      const minConf = parseFlagNumber(parts, "--min-conf", undefined);
      const domain = parseFlagString(parts, "--domain");
      const dryRun = parts.includes("--dry-run");
      const { readFile: readFileFs } = await import("fs/promises");
      const parsed = JSON.parse(await readFileFs(input, "utf8"));
      const filtered = parsed.filter((i) => {
        if (scope && i.scope !== scope)
          return false;
        if (minConf !== undefined && i.confidence < minConf)
          return false;
        if (domain && i.domain !== domain)
          return false;
        return true;
      });
      if (dryRun) {
        return {
          type: "text",
          value: `Dry run: would import ${filtered.length}/${parsed.length} instincts.`
        };
      }
      for (const instinct of filtered) {
        await upsertInstinct(instinct, options);
      }
      return {
        type: "text",
        value: `Imported ${filtered.length}/${parsed.length} instincts.`
      };
    }
    case "prune": {
      const maxAgeIndex = parts.indexOf("--max-age");
      const maxAge = maxAgeIndex >= 0 && parts[maxAgeIndex + 1] ? Number(parts[maxAgeIndex + 1]) : 30;
      const pruned = await prunePendingInstincts(maxAge, options);
      return {
        type: "text",
        value: `Pruned ${pruned.length} pending instincts.`
      };
    }
    case "promote": {
      const target = parts[1];
      if (!target) {
        const gaps = await readSkillGaps(project, rootDir);
        const instincts = await loadInstincts(options);
        const candidates = findPromotionCandidates(instincts);
        const lines = [
          `Promotion candidates for ${project.projectName} (${project.projectId}):`,
          `Pending gaps: ${gaps.filter((g) => g.status === "pending").length}`,
          `Global-eligible instincts (>=2 projects, avg confidence >=0.8): ${candidates.length}`,
          "",
          "Usage:",
          "  /skill-learning promote gap <gap-key>           # pending gap -> draft",
          "  /skill-learning promote instinct <instinct-id>  # project instinct -> global"
        ];
        return { type: "text", value: lines.join(`
`) };
      }
      if (target === "gap") {
        const gapKey = parts[2];
        if (!gapKey) {
          return {
            type: "text",
            value: "Usage: /skill-learning promote gap <gap-key>"
          };
        }
        const updated = await promoteGapToDraft(gapKey, project, rootDir);
        if (!updated) {
          return { type: "text", value: `No gap found for key "${gapKey}".` };
        }
        return {
          type: "text",
          value: `Promoted gap ${gapKey} to status=${updated.status} (draft=${updated.draft?.skillPath ?? "none"}).`
        };
      }
      if (target === "instinct") {
        const instinctId = parts[2];
        if (!instinctId) {
          return {
            type: "text",
            value: "Usage: /skill-learning promote instinct <instinct-id>"
          };
        }
        const projectInstincts = await loadInstincts(options);
        const match = projectInstincts.find((i) => i.id === instinctId);
        if (!match) {
          return {
            type: "text",
            value: `No project-scoped instinct found for id "${instinctId}".`
          };
        }
        if (match.scope === "global") {
          return {
            type: "text",
            value: `Instinct ${instinctId} is already global.`
          };
        }
        const globalCopy = { ...match, scope: "global" };
        await saveInstinct(globalCopy, { scope: "global", rootDir });
        return {
          type: "text",
          value: `Promoted instinct ${instinctId} to global scope.`
        };
      }
      return {
        type: "text",
        value: "Usage: /skill-learning promote [gap <gap-key>|instinct <instinct-id>]"
      };
    }
    case "projects": {
      const projects = listKnownProjects();
      if (projects.length === 0) {
        return { type: "text", value: "No known project scopes yet." };
      }
      const lines = ["Known project scopes:"];
      for (const record of projects) {
        const projectOptions = { project: record, rootDir };
        const [instincts, observations] = await Promise.all([
          loadInstincts(projectOptions),
          readObservations(projectOptions)
        ]);
        lines.push(`- ${record.projectName} (${record.projectId}) \u2014 instincts: ${instincts.length}, observations: ${observations.length}, lastSeen: ${record.lastSeenAt}`);
      }
      return { type: "text", value: lines.join(`
`) };
    }
    default:
      return {
        type: "text",
        value: "Usage: /skill-learning [status|ingest|evolve|export|import|prune|promote|projects]"
      };
  }
};
var init_skill_learning = __esm(() => {
  init_envUtils();
  init_skillLearning();
});
init_skill_learning();

export {
  call
};
