/**
 * 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 deepseek_exports = {};
__export(deepseek_exports, {
  DeepSeekProvider: () => DeepSeekProvider,
  deepseekProviderOptions: () => deepseekProviderOptions
});
module.exports = __toCommonJS(deepseek_exports);
var import_deepseek = require("@langchain/deepseek");
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");
class ReasoningDeepSeek extends import_deepseek.ChatDeepSeek {
  async _generate(messages, options, runManager) {
    const reasoningMap = (0, import_reasoning.collectReasoningMap)(messages);
    const nextOptions = {
      ...options || {},
      [import_reasoning.REASONING_MAP_KEY]: reasoningMap
    };
    return super._generate(messages, nextOptions, runManager);
  }
  async *_streamResponseChunks(messages, options, runManager) {
    const reasoningMap = (options == null ? void 0 : options[import_reasoning.REASONING_MAP_KEY]) instanceof Map ? options[import_reasoning.REASONING_MAP_KEY] : (0, import_reasoning.collectReasoningMap)(messages);
    const nextOptions = {
      ...options || {},
      [import_reasoning.REASONING_MAP_KEY]: reasoningMap
    };
    yield* super._streamResponseChunks(messages, nextOptions, runManager);
  }
  async completionWithRetry(request, requestOptions) {
    const reasoningMap = requestOptions == null ? void 0 : requestOptions[import_reasoning.REASONING_MAP_KEY];
    (0, import_reasoning.patchRequestMessagesReasoning)(request, reasoningMap);
    if (request.stream) {
      return super.completionWithRetry(request, requestOptions);
    }
    return super.completionWithRetry(request, requestOptions);
  }
}
class DeepSeekProvider extends import_provider.LLMProvider {
  get baseURL() {
    return "https://api.deepseek.com";
  }
  createModel() {
    const { baseURL, apiKey } = this.serviceOptions || {};
    const { responseFormat } = this.modelOptions || {};
    const modelKwargs = {};
    if (responseFormat) {
      modelKwargs["response_format"] = {
        type: responseFormat
      };
    }
    return new ReasoningDeepSeek({
      apiKey,
      ...this.modelOptions,
      modelKwargs,
      configuration: {
        baseURL: baseURL || this.baseURL
      },
      verbose: false
    });
  }
  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) {
    return false;
  }
}
const deepseekProviderOptions = {
  title: "DeepSeek",
  supportedModel: [import_ai_manager.SupportedModel.LLM],
  models: {
    [import_ai_manager.SupportedModel.LLM]: ["deepseek-chat", "deepseek-reasoner"]
  },
  provider: DeepSeekProvider
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  DeepSeekProvider,
  deepseekProviderOptions
});
