// @bun
import {
  editFileInEditor,
  init_promptEditor
} from "./chunk-ht6fwttn.js";
import {
  Select,
  clearMemoryFileCaches,
  getAgentMemoryDir,
  getMemoryFiles,
  init_AppState,
  init_CustomSelect,
  init_agentMemory,
  init_claudemd,
  init_config1 as init_config,
  init_consolidationLock,
  isAutoDreamEnabled,
  readLastConsolidatedAt,
  useAppState
} from "./chunk-4vz571bb.js";
import"./chunk-14p6wvsq.js";
import"./chunk-8hseq8b1.js";
import"./chunk-ddtxq6k5.js";
import"./chunk-28jd8qjx.js";
import"./chunk-djt39ze3.js";
import"./chunk-ptxteaeh.js";
import"./chunk-wd53a7m4.js";
import"./chunk-hms8ge0k.js";
import"./chunk-whpphypt.js";
import"./chunk-pht800m5.js";
import"./chunk-pjkqh1kq.js";
import"./chunk-9a9g5hbj.js";
import"./chunk-9hn8e6h1.js";
import"./chunk-rgyzsbs3.js";
import"./chunk-qn6me9n1.js";
import"./chunk-qd3r2s2n.js";
import"./chunk-djzd7tef.js";
import"./chunk-gyhnpbkx.js";
import"./chunk-71sdcaq6.js";
import"./chunk-60gf7p5s.js";
import"./chunk-sj3d0nqx.js";
import"./chunk-9pk9rk2w.js";
import"./chunk-javpza6a.js";
import"./chunk-6v7bre98.js";
import"./chunk-24kv69g3.js";
import {
  init_useExitOnCtrlCDWithKeybindings,
  useExitOnCtrlCDWithKeybindings
} from "./chunk-fcqebw9m.js";
import"./chunk-dnfd3c9s.js";
import"./chunk-15ezj4ww.js";
import"./chunk-1h284c6e.js";
import"./chunk-582g9yce.js";
import {
  init_useKeybinding
} from "./chunk-y3ztgztb.js";
import"./chunk-4spgkgr3.js";
import"./chunk-n8kbhswc.js";
import"./chunk-60fkafk2.js";
import"./chunk-fgbkrbk3.js";
import"./chunk-ssw61jfd.js";
import"./chunk-093ej2sf.js";
import"./chunk-7tfdhkpy.js";
import"./chunk-nde5ym6a.js";
import"./chunk-k8wq4qsj.js";
import"./chunk-g7d3xx1p.js";
import"./chunk-3wrx2943.js";
import"./chunk-7p9kkh0f.js";
import"./chunk-qp8cgz66.js";
import {
  init_browser,
  openPath
} from "./chunk-1mg610h2.js";
import"./chunk-93hc149s.js";
import"./chunk-935nrvdb.js";
import"./chunk-8zz4z1q3.js";
import"./chunk-nnat9jw5.js";
import"./chunk-hqxp6b72.js";
import"./chunk-4jzkqgf1.js";
import"./chunk-33rd41ng.js";
import"./chunk-f257kqw9.js";
import"./chunk-w5hnghah.js";
import"./chunk-36twraxy.js";
import"./chunk-1bkvnj0y.js";
import"./chunk-akdpcc5p.js";
import {
  getAutoMemPath,
  getDisplayPath,
  init_file,
  init_paths,
  init_settings1 as init_settings,
  isAutoMemoryEnabled,
  updateSettingsForSource
} from "./chunk-7rrkrej7.js";
import"./chunk-6r0bxrh6.js";
import"./chunk-d6500tvx.js";
import"./chunk-jg4bw4hp.js";
import"./chunk-epvbnq43.js";
import"./chunk-tn2sx7j6.js";
import"./chunk-6tzyv21c.js";
import"./chunk-8kf8h7xf.js";
import"./chunk-bgan4cpf.js";
import"./chunk-jmv7k0jn.js";
import"./chunk-ch4pgvwq.js";
import"./chunk-99jc4tcw.js";
import"./chunk-fghczfqt.js";
import"./chunk-vwenx8ke.js";
import"./chunk-f86mwfyd.js";
import"./chunk-v4ypszbb.js";
import"./chunk-bk6ck5c2.js";
import"./chunk-ym6j0wv1.js";
import"./chunk-hjmatcgt.js";
import"./chunk-6mdh70q0.js";
import"./chunk-ch92ycde.js";
import"./chunk-e4dsy4g1.js";
import"./chunk-326zehp8.js";
import"./chunk-kc67kt75.js";
import"./chunk-40t1d75v.js";
import"./chunk-nt5dmk8q.js";
import"./chunk-e3abfxpy.js";
import"./chunk-q44zc68f.js";
import"./chunk-fejeqe61.js";
import {
  formatRelativeTimeAgo,
  init_format
} from "./chunk-0gp4q8aq.js";
import"./chunk-hn4w9pkj.js";
import {
  Dialog,
  Link,
  ListItem,
  ThemedBox_default,
  ThemedText,
  init_source,
  init_src,
  require_react,
  source_default,
  useKeybinding
} from "./chunk-0r6npc3t.js";
import {
  require_jsx_runtime
} from "./chunk-gxyx8b8w.js";
import {
  init_analytics,
  logEvent
} from "./chunk-j1mep9ck.js";
import"./chunk-6x35ffpx.js";
import"./chunk-1zbwhcbt.js";
import"./chunk-t0450ma3.js";
import {
  findGitRoot,
  init_git
} from "./chunk-0cqw3y8h.js";
import"./chunk-k1sw7km0.js";
import"./chunk-e3j7m7k2.js";
import"./chunk-sc9ycxra.js";
import {
  getCwd,
  init_cwd
} from "./chunk-hhta0nge.js";
import {
  init_log,
  logError
} from "./chunk-8wsj9fff.js";
import"./chunk-m28vg9w4.js";
import"./chunk-f8prtxs3.js";
import"./chunk-41a6f2kv.js";
import"./chunk-c1yc761e.js";
import"./chunk-c5g9shkw.js";
import {
  getErrnoCode,
  init_errors
} from "./chunk-byd20vdq.js";
import"./chunk-kb3758f7.js";
import {
  getOriginalCwd,
  init_state
} from "./chunk-3j8w0rq8.js";
import"./chunk-tj0dzck2.js";
import"./chunk-aeysytks.js";
import"./chunk-ns1htxgd.js";
import"./chunk-ztqzmfx1.js";
import {
  getClaudeConfigHomeDir,
  init_envUtils
} from "./chunk-6k1rsk85.js";
import"./chunk-nxzx0ey9.js";
import"./chunk-yes1my80.js";
import"./chunk-pecy49yr.js";
import"./chunk-azbab59e.js";
import"./chunk-3nk9q8dr.js";
import {
  __esm,
  __toESM
} from "./chunk-hhsxm2yr.js";

// src/utils/memory/versions.ts
function projectIsInGitRepo(cwd) {
  return findGitRoot(cwd) !== null;
}
var init_versions = __esm(() => {
  init_git();
});

// src/components/memory/MemoryFileSelector.tsx
import { mkdir } from "fs/promises";
import { join } from "path";
function MemoryFileSelector({ onSelect, onCancel }) {
  const existingMemoryFiles = import_react.use(getMemoryFiles());
  const userMemoryPath = join(getClaudeConfigHomeDir(), "CLAUDE.md");
  const projectMemoryPath = join(getOriginalCwd(), "CLAUDE.md");
  const hasUserMemory = existingMemoryFiles.some((f) => f.path === userMemoryPath);
  const hasProjectMemory = existingMemoryFiles.some((f) => f.path === projectMemoryPath);
  const allMemoryFiles = [
    ...existingMemoryFiles.filter((f) => f.type !== "AutoMem" && f.type !== "TeamMem").map((f) => ({ ...f, exists: true })),
    ...hasUserMemory ? [] : [
      {
        path: userMemoryPath,
        type: "User",
        content: "",
        exists: false
      }
    ],
    ...hasProjectMemory ? [] : [
      {
        path: projectMemoryPath,
        type: "Project",
        content: "",
        exists: false
      }
    ]
  ];
  const depths = new Map;
  const memoryOptions = allMemoryFiles.map((file) => {
    const displayPath = getDisplayPath(file.path);
    const existsLabel = file.exists ? "" : " (new)";
    const depth = file.parent ? (depths.get(file.parent) ?? 0) + 1 : 0;
    depths.set(file.path, depth);
    const indent = depth > 0 ? "  ".repeat(depth - 1) : "";
    let label;
    if (file.type === "User" && !file.isNested && file.path === userMemoryPath) {
      label = `User memory`;
    } else if (file.type === "Project" && !file.isNested && file.path === projectMemoryPath) {
      label = `Project memory`;
    } else if (depth > 0) {
      label = `${indent}L ${displayPath}${existsLabel}`;
    } else {
      label = `${displayPath}`;
    }
    let description;
    const isGit = projectIsInGitRepo(getOriginalCwd());
    if (file.type === "User" && !file.isNested) {
      description = "Saved in ~/.claude/CLAUDE.md";
    } else if (file.type === "Project" && !file.isNested && file.path === projectMemoryPath) {
      description = `${isGit ? "Checked in at" : "Saved in"} ./CLAUDE.md`;
    } else if (file.parent) {
      description = "@-imported";
    } else if (file.isNested) {
      description = "dynamically loaded";
    } else {
      description = "";
    }
    return {
      label,
      value: file.path,
      description
    };
  });
  const folderOptions = [];
  const agentDefinitions = useAppState((s) => s.agentDefinitions);
  if (isAutoMemoryEnabled()) {
    folderOptions.push({
      label: "Open auto-memory folder",
      value: `${OPEN_FOLDER_PREFIX}${getAutoMemPath()}`,
      description: ""
    });
    if (false) {}
    for (const agent of agentDefinitions.activeAgents) {
      if (agent.memory) {
        const agentDir = getAgentMemoryDir(agent.agentType, agent.memory);
        folderOptions.push({
          label: `Open ${source_default.bold(agent.agentType)} agent memory`,
          value: `${OPEN_FOLDER_PREFIX}${agentDir}`,
          description: `${agent.memory} scope`
        });
      }
    }
  }
  memoryOptions.push(...folderOptions);
  const initialPath = lastSelectedPath && memoryOptions.some((opt) => opt.value === lastSelectedPath) ? lastSelectedPath : memoryOptions[0]?.value || "";
  const [autoMemoryOn, setAutoMemoryOn] = import_react.useState(isAutoMemoryEnabled);
  const [autoDreamOn, setAutoDreamOn] = import_react.useState(isAutoDreamEnabled);
  const [showDreamRow] = import_react.useState(isAutoMemoryEnabled);
  const isDreamRunning = useAppState((s) => Object.values(s.tasks).some((t) => t.type === "dream" && t.status === "running"));
  const [lastDreamAt, setLastDreamAt] = import_react.useState(null);
  import_react.useEffect(() => {
    if (!showDreamRow)
      return;
    readLastConsolidatedAt().then(setLastDreamAt);
  }, [showDreamRow, isDreamRunning]);
  const dreamStatus = isDreamRunning ? "running" : lastDreamAt === null ? "" : lastDreamAt === 0 ? "never" : `last ran ${formatRelativeTimeAgo(new Date(lastDreamAt))}`;
  const [focusedToggle, setFocusedToggle] = import_react.useState(null);
  const toggleFocused = focusedToggle !== null;
  const lastToggleIndex = showDreamRow ? 1 : 0;
  function handleToggleAutoMemory() {
    const newValue = !autoMemoryOn;
    updateSettingsForSource("userSettings", { autoMemoryEnabled: newValue });
    setAutoMemoryOn(newValue);
    logEvent("tengu_auto_memory_toggled", { enabled: newValue });
  }
  function handleToggleAutoDream() {
    const newValue = !autoDreamOn;
    updateSettingsForSource("userSettings", { autoDreamEnabled: newValue });
    setAutoDreamOn(newValue);
    logEvent("tengu_auto_dream_toggled", { enabled: newValue });
  }
  useExitOnCtrlCDWithKeybindings();
  useKeybinding("confirm:no", onCancel, { context: "Confirmation" });
  useKeybinding("confirm:yes", () => {
    if (focusedToggle === 0)
      handleToggleAutoMemory();
    else if (focusedToggle === 1)
      handleToggleAutoDream();
  }, { context: "Confirmation", isActive: toggleFocused });
  useKeybinding("select:next", () => {
    setFocusedToggle((prev) => prev !== null && prev < lastToggleIndex ? prev + 1 : null);
  }, { context: "Select", isActive: toggleFocused });
  useKeybinding("select:previous", () => {
    setFocusedToggle((prev) => prev !== null && prev > 0 ? prev - 1 : prev);
  }, { context: "Select", isActive: toggleFocused });
  return /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
    flexDirection: "column",
    width: "100%",
    children: [
      /* @__PURE__ */ jsx_runtime.jsxs(ThemedBox_default, {
        flexDirection: "column",
        marginBottom: 1,
        children: [
          /* @__PURE__ */ jsx_runtime.jsx(ListItem, {
            isFocused: focusedToggle === 0,
            children: /* @__PURE__ */ jsx_runtime.jsxs(ThemedText, {
              children: [
                "Auto-memory: ",
                autoMemoryOn ? "on" : "off"
              ]
            })
          }),
          showDreamRow && /* @__PURE__ */ jsx_runtime.jsx(ListItem, {
            isFocused: focusedToggle === 1,
            styled: false,
            children: /* @__PURE__ */ jsx_runtime.jsxs(ThemedText, {
              color: focusedToggle === 1 ? "suggestion" : undefined,
              children: [
                "Auto-dream: ",
                autoDreamOn ? "on" : "off",
                dreamStatus && /* @__PURE__ */ jsx_runtime.jsxs(ThemedText, {
                  dimColor: true,
                  children: [
                    " \xB7 ",
                    dreamStatus
                  ]
                }),
                !isDreamRunning && autoDreamOn && /* @__PURE__ */ jsx_runtime.jsx(ThemedText, {
                  dimColor: true,
                  children: " \xB7 /dream to run"
                })
              ]
            })
          })
        ]
      }),
      /* @__PURE__ */ jsx_runtime.jsx(Select, {
        defaultFocusValue: initialPath,
        options: memoryOptions,
        isDisabled: toggleFocused,
        onChange: (value) => {
          if (value.startsWith(OPEN_FOLDER_PREFIX)) {
            const folderPath = value.slice(OPEN_FOLDER_PREFIX.length);
            mkdir(folderPath, { recursive: true }).catch(() => {}).then(() => openPath(folderPath));
            return;
          }
          lastSelectedPath = value;
          onSelect(value);
        },
        onCancel,
        onUpFromFirstItem: () => setFocusedToggle(lastToggleIndex)
      })
    ]
  });
}
var import_react, jsx_runtime, lastSelectedPath, OPEN_FOLDER_PREFIX = "__open_folder__";
var init_MemoryFileSelector = __esm(() => {
  init_source();
  init_state();
  init_useExitOnCtrlCDWithKeybindings();
  init_src();
  init_useKeybinding();
  init_paths();
  init_analytics();
  init_config();
  init_consolidationLock();
  init_AppState();
  init_agentMemory();
  init_browser();
  init_claudemd();
  init_envUtils();
  init_file();
  init_format();
  init_versions();
  init_settings();
  init_CustomSelect();
  import_react = __toESM(require_react(), 1);
  jsx_runtime = __toESM(require_jsx_runtime(), 1);
});

// src/components/memory/MemoryUpdateNotification.tsx
import { homedir } from "os";
import { relative } from "path";
function getRelativeMemoryPath(path) {
  const homeDir = homedir();
  const cwd = getCwd();
  const relativeToHome = path.startsWith(homeDir) ? "~" + path.slice(homeDir.length) : null;
  const relativeToCwd = path.startsWith(cwd) ? "./" + relative(cwd, path) : null;
  if (relativeToHome && relativeToCwd) {
    return relativeToHome.length <= relativeToCwd.length ? relativeToHome : relativeToCwd;
  }
  return relativeToHome || relativeToCwd || path;
}
var jsx_runtime2;
var init_MemoryUpdateNotification = __esm(() => {
  init_src();
  init_cwd();
  jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
});

// src/commands/memory/memory.tsx
import { mkdir as mkdir2, writeFile } from "fs/promises";
function MemoryCommand({
  onDone
}) {
  const handleSelectMemoryFile = async (memoryPath) => {
    try {
      if (memoryPath.includes(getClaudeConfigHomeDir())) {
        await mkdir2(getClaudeConfigHomeDir(), { recursive: true });
      }
      try {
        await writeFile(memoryPath, "", { encoding: "utf8", flag: "wx" });
      } catch (e) {
        if (getErrnoCode(e) !== "EEXIST") {
          throw e;
        }
      }
      await editFileInEditor(memoryPath);
      let editorSource = "default";
      let editorValue = "";
      if (process.env.VISUAL) {
        editorSource = "$VISUAL";
        editorValue = process.env.VISUAL;
      } else if (process.env.EDITOR) {
        editorSource = "$EDITOR";
        editorValue = process.env.EDITOR;
      }
      const editorInfo = editorSource !== "default" ? `Using ${editorSource}="${editorValue}".` : "";
      const editorHint = editorInfo ? `> ${editorInfo} To change editor, set $EDITOR or $VISUAL environment variable.` : `> To use a different editor, set the $EDITOR or $VISUAL environment variable.`;
      onDone(`Opened memory file at ${getRelativeMemoryPath(memoryPath)}

${editorHint}`, { display: "system" });
    } catch (error) {
      logError(error);
      onDone(`Error opening memory file: ${error}`);
    }
  };
  const handleCancel = () => {
    onDone("Cancelled memory editing", { display: "system" });
  };
  return /* @__PURE__ */ jsx_runtime3.jsx(Dialog, {
    title: "Memory",
    onCancel: handleCancel,
    color: "remember",
    children: /* @__PURE__ */ jsx_runtime3.jsxs(ThemedBox_default, {
      flexDirection: "column",
      children: [
        /* @__PURE__ */ jsx_runtime3.jsx(React.Suspense, {
          fallback: null,
          children: /* @__PURE__ */ jsx_runtime3.jsx(MemoryFileSelector, {
            onSelect: handleSelectMemoryFile,
            onCancel: handleCancel
          })
        }),
        /* @__PURE__ */ jsx_runtime3.jsx(ThemedBox_default, {
          marginTop: 1,
          children: /* @__PURE__ */ jsx_runtime3.jsxs(ThemedText, {
            dimColor: true,
            children: [
              "Learn more: ",
              /* @__PURE__ */ jsx_runtime3.jsx(Link, {
                url: "https://code.claude.com/docs/en/memory"
              })
            ]
          })
        })
      ]
    })
  });
}
var React, jsx_runtime3, call = async (onDone) => {
  clearMemoryFileCaches();
  await getMemoryFiles();
  return /* @__PURE__ */ jsx_runtime3.jsx(MemoryCommand, {
    onDone
  });
};
var init_memory = __esm(() => {
  init_src();
  init_MemoryFileSelector();
  init_MemoryUpdateNotification();
  init_src();
  init_claudemd();
  init_envUtils();
  init_errors();
  init_log();
  init_promptEditor();
  React = __toESM(require_react(), 1);
  jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
});
init_memory();

export {
  call
};

//# debugId=88E6D9ACCDCA795564756E2164756E21
//# sourceMappingURL=chunk-7t1cy00z.js.map
