/**
 * 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 dashscope_exports = {};
__export(dashscope_exports, {
  DashscopeEmbeddingProvider: () => DashscopeEmbeddingProvider,
  DashscopeProvider: () => DashscopeProvider,
  dashscopeProviderOptions: () => dashscopeProviderOptions
});
module.exports = __toCommonJS(dashscope_exports);
var import_openai = require("@langchain/openai");
var import_provider = require("./provider");
var import_ai_manager = require("../manager/ai-manager");
var import_lodash = __toESM(require("lodash"));
var import_reasoning = require("./common/reasoning");
const DASHSCOPE_URL = "https://dashscope.aliyuncs.com/compatible-mode/v1";
class DashscopeProvider extends import_provider.LLMProvider {
  get baseURL() {
    return DASHSCOPE_URL;
  }
  createModel() {
    var _a, _b;
    const { baseURL, apiKey } = this.serviceOptions || {};
    const { responseFormat, structuredOutput } = this.modelOptions || {};
    const { schema } = structuredOutput || {};
    const modelKwargs = {};
    if (responseFormat) {
      const responseFormatOptions = {
        type: responseFormat
      };
      if (responseFormat === "json_schema" && schema) {
        responseFormatOptions["json_schema"] = schema;
      }
      modelKwargs["response_format"] = responseFormatOptions;
    } else {
      modelKwargs["response_format"] = { type: "text" };
    }
    if (((_b = (_a = this.modelOptions) == null ? void 0 : _a.builtIn) == null ? void 0 : _b.webSearch) === true) {
      modelKwargs["enable_search"] = true;
    }
    return new import_reasoning.ReasoningChatOpenAI({
      apiKey,
      topP: 0.8,
      temperature: 0.7,
      ...this.modelOptions,
      modelKwargs,
      configuration: {
        baseURL: baseURL || this.baseURL
      },
      verbose: false
    });
  }
  isToolConflict() {
    return true;
  }
  resolveTools(toolDefinitions) {
    var _a, _b;
    if (this.isToolConflict() && ((_b = (_a = this.modelOptions) == null ? void 0 : _a.builtIn) == null ? void 0 : _b.webSearch) === true) {
      return [];
    } else {
      return toolDefinitions;
    }
  }
  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;
  }
  isApiSupportedAttachment(attachment) {
    var _a;
    return ((_a = attachment.mimetype) == null ? void 0 : _a.startsWith("image/")) ?? false;
  }
}
class DashscopeEmbeddingProvider extends import_provider.EmbeddingProvider {
  getDefaultUrl() {
    return DASHSCOPE_URL;
  }
  createEmbedding() {
    return new import_openai.OpenAIEmbeddings({
      configuration: {
        baseURL: this.baseURL ?? "",
        apiKey: this.apiKey
      },
      model: this.model
    });
  }
}
const dashscopeProviderOptions = {
  title: '{{t("Dashscope", {ns: "ai"})}}',
  supportedModel: [import_ai_manager.SupportedModel.LLM, import_ai_manager.SupportedModel.EMBEDDING],
  supportWebSearch: true,
  models: {
    [import_ai_manager.SupportedModel.LLM]: [
      "qwen-long",
      "qwq-plus",
      "qwen-max",
      "qwen-plus",
      "qwen-turbo",
      "qwen-math-plus",
      "qwen-math-turbo",
      "qwen-coder-plus",
      "qwen-coder-turbo"
    ],
    [import_ai_manager.SupportedModel.EMBEDDING]: [
      "text-embedding-v4",
      "text-embedding-v3",
      "text-embedding-v2",
      "text-embedding-v1",
      "text-embedding-async-v2",
      "text-embedding-async-v1"
    ]
  },
  provider: DashscopeProvider,
  embedding: DashscopeEmbeddingProvider
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DashscopeEmbeddingProvider,
  DashscopeProvider,
  dashscopeProviderOptions
});
