/**
 * 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 __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
  convertAIMessage: () => convertAIMessage,
  convertHumanMessage: () => convertHumanMessage,
  convertToolMessage: () => convertToolMessage
});
module.exports = __toCommonJS(utils_exports);
const convertAIMessage = ({
  aiEmployee,
  providerName: provider,
  model,
  aiMessage
}) => {
  var _a;
  const message = aiMessage.content;
  const toolCalls = aiMessage.tool_calls;
  const skills = (_a = aiEmployee.skillSettings) == null ? void 0 : _a.skills;
  if (!message && !(toolCalls == null ? void 0 : toolCalls.length)) {
    return null;
  }
  let textContent = message;
  let reference;
  if (Array.isArray(message)) {
    const textBlocks = message.filter((block) => block.type === "text");
    textContent = textBlocks.map((block) => block.text).join("") || "";
    for (const block of message) {
      if (block.type === "web_search_tool_result" && Array.isArray(block.content)) {
        reference = reference || [];
        for (const item of block.content) {
          if (item.type === "web_search_result" && item.url) {
            reference.push({ title: item.title || "", url: item.url });
          }
        }
      }
    }
  }
  const values = {
    role: aiEmployee.employee.username,
    content: {
      type: "text",
      content: textContent,
      ...(reference == null ? void 0 : reference.length) ? { reference } : {}
    },
    metadata: {
      id: aiMessage.id,
      model,
      provider,
      usage_metadata: {}
    },
    toolCalls: null
  };
  if (toolCalls == null ? void 0 : toolCalls.length) {
    values.toolCalls = toolCalls;
    values.metadata.autoCallTools = toolCalls.filter((tool) => {
      return skills == null ? void 0 : skills.some((s) => s.name === tool.name && s.autoCall);
    }).map((tool) => tool.name);
  }
  if (aiMessage.usage_metadata) {
    values.metadata.usage_metadata = aiMessage.usage_metadata;
  }
  if (aiMessage.response_metadata) {
    values.metadata.response_metadata = aiMessage.response_metadata;
  }
  if (aiMessage.additional_kwargs) {
    values.metadata.additional_kwargs = aiMessage.additional_kwargs;
  }
  return values;
};
const convertHumanMessage = ({
  providerName: provider,
  model,
  humanMessage
}) => {
  var _a;
  if (!humanMessage.additional_kwargs.userContent) {
    return null;
  }
  const values = {
    role: "user",
    content: (_a = humanMessage.additional_kwargs) == null ? void 0 : _a.userContent,
    metadata: {
      id: humanMessage.id,
      model,
      provider
    }
  };
  values.attachments = humanMessage.additional_kwargs.attachments;
  values.workContext = humanMessage.additional_kwargs.workContext;
  return values;
};
const convertToolMessage = ({
  providerName: provider,
  model,
  toolMessage
}) => {
  const values = {
    role: "tool",
    content: {
      type: "text",
      content: toolMessage.content
    },
    metadata: {
      id: toolMessage.id,
      model,
      provider,
      toolCallId: toolMessage.tool_call_id,
      toolName: toolMessage.name
    }
  };
  return values;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  convertAIMessage,
  convertHumanMessage,
  convertToolMessage
});
