'use strict';

/**
 * LM Studio local model provider — Item D of provider-routing deep-dive.
 *
 * LM Studio exposes an OpenAI-compatible HTTP server (default
 * `http://localhost:1234/v1`). This wrapper delegates to the OpenAI
 * provider with a baseUrl override so we get tools, streaming, etc.
 * for free without re-implementing the chat shape.
 *
 * Inspired by OpenClaw's `extensions/lmstudio/` peer plugin.
 */

const { createOpenAIProvider } = require('./openai');

const DEFAULT_BASE_URL = 'http://localhost:1234/v1';
const DEFAULT_MODEL = 'lmstudio:default';

/**
 * @param {{baseUrl?: string, apiKey?: string}} config
 *   - baseUrl: defaults to http://localhost:1234/v1 (LM Studio default)
 *   - apiKey: defaults to 'lm-studio' (LM Studio doesn't enforce auth
 *             but the OpenAI client expects something non-empty)
 */
function createLmStudioProvider(config = {}) {
  const baseUrl = config.baseUrl || process.env.LMSTUDIO_BASE_URL || DEFAULT_BASE_URL;
  const apiKey = config.apiKey || process.env.LMSTUDIO_API_KEY || 'lm-studio';
  const provider = createOpenAIProvider({ baseUrl, apiKey });
  // Override .type so callers can distinguish from OpenAI
  return Object.assign(Object.create(Object.getPrototypeOf(provider)), provider, {
    type: 'lmstudio',
    baseUrl,
  });
}

module.exports = {
  createLmStudioProvider,
  DEFAULT_BASE_URL,
  DEFAULT_MODEL,
};
