import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
import {
  createCliHostCapabilities
} from "../../../chunks/chunk-YLOLJRLJ.js";
import {
  createSharedFileSystemTools,
  createSubagentCapability
} from "../../../chunks/chunk-SW6G4XW2.js";
import "../../../chunks/chunk-D7NAXU7F.js";
import "../../../chunks/chunk-TG2EQLX2.js";

// builtin/extensions/subagent/index.ts
import * as os from "os";
import * as path from "path";
import * as fs from "fs";
function debugLog(module, message, data) {
  try {
    const homeDir = process.env.HOME || process.env.USERPROFILE || ".";
    const logDir = path.join(homeDir, ".pie", "sessions");
    const files = fs.readdirSync(logDir).filter((f) => f.endsWith(".log")).sort().reverse();
    if (files.length > 0) {
      const logPath = path.join(logDir, files[0]);
      const entry = `[${(/* @__PURE__ */ new Date()).toISOString()}] [subagent:${module}] ${message}${data ? " " + JSON.stringify(data) : ""}
`;
      fs.appendFileSync(logPath, entry);
    }
  } catch {
  }
}
function subagentExtension(ctx) {
  ctx.log("Subagent extension loaded (transparent parallel mode)");
  const apiKey = ctx.apiKey || process.env.KIMI_API_KEY || process.env.OPENAI_API_KEY;
  let model = ctx.model;
  if (!model) {
    model = {
      id: process.env.PIE_MODEL_ID || "kimi-api/kimi-k2.5",
      name: "Default Model",
      reasoning: false,
      input: ["text"],
      cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
      contextWindow: 2e5,
      maxTokens: 8192
    };
  }
  const skills = ctx.skills || [];
  if (!apiKey) {
    ctx.log("Warning: No API key available for subagent.");
  }
  const capability = createSubagentCapability({
    apiKey,
    model,
    skills,
    yoloMode: ctx.yoloMode,
    getParentTools: () => ctx.getAllTools(),
    log: (message) => ctx.log(message),
    notify: ctx.hasUI ? (message, type) => ctx.ui.notify(message, type) : void 0,
    debugLog,
    resolveModelClass: (modelClass) => ctx.resolveModelClass?.(modelClass),
    createRuntimeTools: () => {
      const parentRuntimeTools = ctx.getRuntimeTools?.();
      if (Array.isArray(parentRuntimeTools) && parentRuntimeTools.length > 0) {
        return parentRuntimeTools.filter((tool) => tool?.name !== "spawn_subagents_parallel");
      }
      const configDir = path.join(os.homedir(), ".pie");
      const fsOptions = ctx.yoloMode ? { allowlistedDirs: ["/"] } : {
        accessRoots: [
          ctx.cwd,
          configDir,
          ...skills.map((skill) => skill.baseDir).filter((entry) => entry && entry.length > 0)
        ]
      };
      const fsTools = createSharedFileSystemTools(ctx.cwd, fsOptions);
      const cliHostCapabilities = createCliHostCapabilities(ctx.cwd);
      return [...fsTools, ...cliHostCapabilities.tools];
    }
  });
  for (const tool of capability.tools) {
    ctx.registerTool(tool);
  }
}
export {
  subagentExtension as default
};
