/**
 * This file is part of the NocoBase (R) project.
 * Copyright (c) 2020-2024 NocoBase Co., Ltd.
 * Authors: NocoBase Team.
 *
 * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
 * For more information, please refer to: https://www.nocobase.com/agreement.
 */

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var mimo_exports = {};
__export(mimo_exports, {
  MiMoProvider: () => MiMoProvider,
  mimoProviderOptions: () => mimoProviderOptions
});
module.exports = __toCommonJS(mimo_exports);
var import_provider = require("./provider");
var import_ai_manager = require("../manager/ai-manager");
var import_lodash = __toESM(require("lodash"));
var import_openai = require("@langchain/openai");
var import_messages = require("@langchain/core/messages");
var import_reasoning = require("./common/reasoning");
class MiMoProvider extends import_provider.LLMProvider {
  get baseURL() {
    return "https://api.xiaomimimo.com/v1";
  }
  createModel() {
    const { baseURL, apiKey } = this.serviceOptions || {};
    const { responseFormat, structuredOutput } = this.modelOptions || {};
    const { schema } = structuredOutput || {};
    const responseFormatOptions = {
      type: responseFormat ?? "text"
    };
    if (responseFormat === "json_schema" && schema) {
      responseFormatOptions["json_schema"] = schema;
    }
    return new ChatMiMoCompletions({
      apiKey,
      ...this.modelOptions,
      modelKwargs: {
        response_format: responseFormatOptions
      },
      configuration: {
        baseURL: baseURL || this.baseURL
      },
      verbose: true
    });
  }
  isApiSupportedAttachment(attachment) {
    const media = ["image/"];
    const supportedMedia = media.some((it) => {
      var _a;
      return (_a = attachment == null ? void 0 : attachment.mimetype) == null ? void 0 : _a.startsWith(it);
    });
    return supportedMedia;
  }
  parseResponseMessage(message) {
    var _a;
    const result = super.parseResponseMessage(message);
    if (["user", "tool"].includes(result == null ? void 0 : result.role)) {
      return result;
    }
    const { metadata } = (message == null ? void 0 : message.toJSON()) ?? {};
    if (!import_lodash.default.isEmpty((_a = metadata == null ? void 0 : metadata.additional_kwargs) == null ? void 0 : _a.reasoning_content)) {
      result.content = {
        ...result.content ?? {},
        reasoning: {
          status: "stop",
          content: metadata == null ? void 0 : metadata.additional_kwargs.reasoning_content
        }
      };
    }
    return result;
  }
  parseReasoningContent(chunk) {
    var _a;
    if (!import_lodash.default.isEmpty((_a = chunk == null ? void 0 : chunk.additional_kwargs) == null ? void 0 : _a.reasoning_content)) {
      return {
        status: "streaming",
        content: chunk.additional_kwargs.reasoning_content
      };
    }
    return null;
  }
  builtInTools() {
    var _a, _b;
    if (((_b = (_a = this.modelOptions) == null ? void 0 : _a.builtIn) == null ? void 0 : _b.webSearch) === true) {
      return [
        {
          type: "web_search",
          force_search: true
        }
      ];
    }
    return [];
  }
  isToolConflict() {
    return true;
  }
}
class ChatMiMoCompletions extends import_reasoning.ReasoningChatOpenAI {
  _convertCompletionsDeltaToBaseMessageChunk(delta, rawResponse, defaultRole) {
    const chunk = (0, import_openai.convertCompletionsDeltaToBaseMessageChunk)({
      delta,
      rawResponse,
      includeRawResponse: this.__includeRawResponse,
      defaultRole
    });
    if (chunk instanceof import_messages.AIMessageChunk) {
      if (delta.reasoning_content) {
        chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
      }
    }
    return chunk;
  }
}
const mimoProviderOptions = {
  title: "MiMo",
  supportWebSearch: true,
  supportedModel: [import_ai_manager.SupportedModel.LLM],
  models: {
    [import_ai_manager.SupportedModel.LLM]: ["mimo-v2.5-pro", "mimo-v2.5"]
  },
  provider: MiMoProvider
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  MiMoProvider,
  mimoProviderOptions
});
