"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// tools/compact/core.ts
var core_exports = {};
__export(core_exports, {
  CompactClient: () => CompactClient
});
module.exports = __toCommonJS(core_exports);
var DEFAULT_API_URL = "https://api.morphllm.com";
var DEFAULT_TIMEOUT = 12e4;
var CompactClient = class {
  config;
  constructor(config = {}) {
    this.config = {
      ...config,
      morphApiUrl: config.morphApiUrl ?? DEFAULT_API_URL,
      timeout: config.timeout ?? DEFAULT_TIMEOUT
    };
  }
  /**
   * Compact messages or text via /v1/compact.
   * Returns per-message `compacted_line_ranges` showing which lines were removed.
   *
   * @example
   * ```typescript
   * const client = new CompactClient({ morphApiKey: 'sk-...' });
   * const result = await client.compact({
   *   input: codeFile,
   *   query: "authentication",
   *   compressionRatio: 0.5,
   * });
   * console.log(result.output);
   * ```
   */
  async compact(input) {
    const apiKey = this.config.morphApiKey || (typeof process !== "undefined" ? process.env?.MORPH_API_KEY : void 0);
    const debug = this.config.debug || false;
    if (!apiKey) {
      throw new Error(
        "Morph API key not found. Set MORPH_API_KEY environment variable or pass morphApiKey in config."
      );
    }
    const url = `${this.config.morphApiUrl}/v1/compact`;
    if (debug) {
      console.log(`[Compact] Calling ${url}`);
    }
    const body = {
      compression_ratio: input.compressionRatio ?? 0.5,
      preserve_recent: input.preserveRecent ?? 2,
      model: input.model ?? "morph-compactor",
      include_line_ranges: input.includeLineRanges ?? true,
      include_markers: input.includeMarkers ?? true
    };
    if (input.query !== void 0) {
      body.query = input.query;
    }
    if (input.messages) {
      body.messages = input.messages;
    } else if (typeof input.input === "string") {
      body.input = input.input;
    } else if (Array.isArray(input.input)) {
      body.messages = input.input;
    } else {
      throw new Error("Either 'input' or 'messages' must be provided");
    }
    const startTime = Date.now();
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Authorization": `Bearer ${apiKey}`,
        "Content-Type": "application/json"
      },
      body: JSON.stringify(body),
      signal: AbortSignal.timeout(this.config.timeout)
    });
    if (!response.ok) {
      const text = await response.text();
      throw new Error(`Compact API error ${response.status}: ${text}`);
    }
    const data = await response.json();
    if (debug) {
      console.log(`[Compact] compact() done in ${Date.now() - startTime}ms`);
    }
    return data;
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  CompactClient
});
//# sourceMappingURL=core.cjs.map