/**
 * 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 responses_exports = {};
__export(responses_exports, {
  OpenAIResponsesProvider: () => OpenAIResponsesProvider
});
module.exports = __toCommonJS(responses_exports);
var import_openai = require("@langchain/openai");
var import_provider = require("../provider");
var import_utils = require("../../utils");
class OpenAIResponsesProvider extends import_provider.LLMProvider {
  get baseURL() {
    return "https://api.openai.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["name"] = "default";
      responseFormatOptions["schema"] = schema;
    }
    return new import_openai.ChatOpenAI({
      apiKey,
      ...this.modelOptions,
      modelKwargs: {
        text: {
          format: responseFormatOptions
        }
      },
      configuration: {
        baseURL: baseURL || this.baseURL
      },
      verbose: false,
      useResponsesApi: true
    });
  }
  parseResponseChunk(chunk) {
    if (chunk && Array.isArray(chunk)) {
      if (chunk[0] && chunk[0].type === "text") {
        chunk = chunk[0].text;
      }
    }
    return (0, import_utils.stripToolCallTags)(chunk);
  }
  parseResponseMessage(message) {
    var _a, _b, _c, _d;
    const { content: rawContent, messageId, metadata, role, toolCalls, attachments, workContext } = message;
    const content = {
      ...rawContent,
      messageId,
      metadata,
      attachments,
      workContext
    };
    if (toolCalls) {
      content.tool_calls = toolCalls;
    }
    if (Array.isArray(content.content)) {
      const textMessage = content.content.find((msg) => msg.type === "text");
      content.content = textMessage == null ? void 0 : textMessage.text;
      if ((_a = textMessage == null ? void 0 : textMessage.annotations) == null ? void 0 : _a.length) {
        content.reference = content.reference ?? [];
        for (const annotation of (textMessage == null ? void 0 : textMessage.annotations) ?? []) {
          content.reference.push({
            title: annotation.title,
            url: annotation.url
          });
        }
      }
    }
    if ((_c = (_b = metadata == null ? void 0 : metadata.response_metadata) == null ? void 0 : _b.output) == null ? void 0 : _c.length) {
      content.reference = content.reference ?? [];
      const output = metadata.response_metadata.output.find((it) => it.type === "message");
      if ((_d = output == null ? void 0 : output.content) == null ? void 0 : _d.length) {
        const outputContent = output.content.find((it) => it.type === "output_text");
        for (const annotation of (outputContent == null ? void 0 : outputContent.annotations) ?? []) {
          content.reference.push({
            title: annotation.title,
            url: annotation.url
          });
        }
      }
    }
    return {
      key: messageId,
      content,
      role
    };
  }
  builtInTools() {
    var _a, _b;
    if (((_b = (_a = this.modelOptions) == null ? void 0 : _a.builtIn) == null ? void 0 : _b.webSearch) === true) {
      const webSearchTool = {
        type: "web_search_preview"
      };
      return [webSearchTool];
    }
    return [];
  }
  isToolConflict() {
    return false;
  }
  parseWebSearchAction(chunk) {
    var _a;
    const tool_outputs = (_a = chunk == null ? void 0 : chunk.additional_kwargs) == null ? void 0 : _a.tool_outputs;
    if (!tool_outputs) {
      return [];
    }
    return tool_outputs.filter((tool) => tool.type === "web_search_call").filter((tool) => tool.action).map((tool) => tool.action);
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  OpenAIResponsesProvider
});
