// tools/compact/core.ts
import OpenAI from "openai";
var DEFAULT_API_URL = "https://api.morphllm.com";
var DEFAULT_TIMEOUT = 12e4;
var CompactClient = class {
  config;
  constructor(config = {}) {
    this.config = {
      morphApiUrl: DEFAULT_API_URL,
      timeout: DEFAULT_TIMEOUT,
      ...config
    };
  }
  /**
   * Compact conversation context using Morph compactor
   *
   * @param input - Compact parameters including input content and optional prompt
   * @returns Compact result with compacted output and usage stats
   *
   * @example
   * ```typescript
   * const client = new CompactClient({ morphApiKey: 'sk-...' });
   * const result = await client.compact({
   *   input: 'Long conversation context to compact...',
   *   prompt: 'Focus on the code review feedback',
   * });
   * 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 client = new OpenAI({
      apiKey,
      baseURL: `${this.config.morphApiUrl}/v1`,
      timeout: this.config.timeout,
      maxRetries: this.config.retryConfig?.maxRetries ?? 3
    });
    const instructions = input.prompt ? `The user's next message will be: "${input.prompt}"
Prioritize keeping context relevant to this query.` : void 0;
    if (debug) {
      console.log(`[Compact] Calling ${this.config.morphApiUrl}/v1/responses`);
      console.log(`[Compact] Model: ${input.model || "morph-compactor"}`);
    }
    const startTime = Date.now();
    const response = await client.responses.create({
      model: input.model || "morph-compactor",
      input: input.input,
      ...instructions && { instructions },
      ...input.temperature != null && { temperature: input.temperature },
      ...input.max_output_tokens != null && { max_output_tokens: input.max_output_tokens },
      stream: false
    });
    const elapsed = Date.now() - startTime;
    if (debug) {
      console.log(`[Compact] Success in ${elapsed}ms`);
    }
    return {
      id: response.id,
      output: response.output,
      usage: response.usage,
      status: response.status ?? "completed",
      model: response.model
    };
  }
};

export {
  CompactClient
};
//# sourceMappingURL=chunk-J267T54M.js.map