"use strict";
/**
 * This file was auto-generated by Fern from our API Definition.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CohereClient = void 0;
const environments = __importStar(require("./environments"));
const core = __importStar(require("./core"));
const Cohere = __importStar(require("./api"));
const serializers = __importStar(require("./serialization"));
const url_join_1 = __importDefault(require("url-join"));
const errors = __importStar(require("./errors"));
const Client_1 = require("./api/resources/embedJobs/client/Client");
const Client_2 = require("./api/resources/datasets/client/Client");
const Client_3 = require("./api/resources/connectors/client/Client");
const Client_4 = require("./api/resources/models/client/Client");
const Client_5 = require("./api/resources/finetuning/client/Client");
class CohereClient {
    constructor(_options = {}) {
        this._options = _options;
    }
    /**
     * Generates a text response to a user message.
     * To learn how to use Chat with Streaming and RAG follow [this guide](https://docs.cohere.com/docs/cochat-beta#various-ways-of-using-the-chat-endpoint).
     */
    chatStream(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "chat"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                    "accept": "*/*, text/event-stream, application/stream+json",
                },
                contentType: "application/json",
                body: Object.assign(Object.assign({}, (yield serializers.ChatStreamRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }))), { stream: true }),
                responseType: "streaming",
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return new core.Stream({
                    response: _response,
                    terminator: "\n",
                    parse: (data) => __awaiter(this, void 0, void 0, function* () {
                        return yield serializers.StreamedChatResponse.parseOrThrow(data, {
                            unrecognizedObjectKeys: "passthrough",
                            allowUnrecognizedUnionMembers: true,
                            allowUnrecognizedEnumValues: true,
                            skipValidation: true,
                            breadcrumbsPrefix: ["response"],
                        });
                    }),
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * Generates a text response to a user message.
     * To learn how to use Chat with Streaming and RAG follow [this guide](https://docs.cohere.com/docs/cochat-beta#various-ways-of-using-the-chat-endpoint).
     * @throws {@link Cohere.TooManyRequestsError}
     *
     * @example
     *     await cohere.chat({
     *         message: "Can you give me a global market overview of solar panels?",
     *         stream: false,
     *         chatHistory: [{
     *                 role: Cohere.ChatMessageRole.Chatbot,
     *                 message: "Hi!"
     *             }, {
     *                 role: Cohere.ChatMessageRole.Chatbot,
     *                 message: "How can I help you today?"
     *             }],
     *         promptTruncation: Cohere.ChatRequestPromptTruncation.Off,
     *         temperature: 0.3
     *     })
     */
    chat(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "chat"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: Object.assign(Object.assign({}, (yield serializers.ChatRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }))), { stream: false }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.NonStreamedChatResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * > 🚧 Warning
     * >
     * > This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
     *
     * Generates realistic text conditioned on a given input.
     */
    generateStream(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "generate"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                    "accept": "*/*, text/event-stream, application/stream+json",
                },
                contentType: "application/json",
                body: Object.assign(Object.assign({}, (yield serializers.GenerateStreamRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }))), { stream: true }),
                responseType: "streaming",
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return new core.Stream({
                    response: _response,
                    terminator: "\n",
                    parse: (data) => __awaiter(this, void 0, void 0, function* () {
                        return yield serializers.GenerateStreamedResponse.parseOrThrow(data, {
                            unrecognizedObjectKeys: "passthrough",
                            allowUnrecognizedUnionMembers: true,
                            allowUnrecognizedEnumValues: true,
                            skipValidation: true,
                            breadcrumbsPrefix: ["response"],
                        });
                    }),
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 400:
                        throw new Cohere.BadRequestError(_response.error.body);
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    case 500:
                        throw new Cohere.InternalServerError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * > 🚧 Warning
     * >
     * > This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
     *
     * Generates realistic text conditioned on a given input.
     * @throws {@link Cohere.BadRequestError}
     * @throws {@link Cohere.TooManyRequestsError}
     * @throws {@link Cohere.InternalServerError}
     *
     * @example
     *     await cohere.generate({
     *         prompt: "Please explain to me how LLMs work",
     *         stream: false,
     *         preset: "my-preset-a58sbd"
     *     })
     */
    generate(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "generate"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: Object.assign(Object.assign({}, (yield serializers.GenerateRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }))), { stream: false }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.Generation.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 400:
                        throw new Cohere.BadRequestError(_response.error.body);
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    case 500:
                        throw new Cohere.InternalServerError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * This endpoint returns text embeddings. An embedding is a list of floating point numbers that captures semantic information about the text that it represents.
     *
     * Embeddings can be used to create text classifiers as well as empower semantic search. To learn more about embeddings, see the embedding page.
     *
     * If you want to learn more how to use the embedding model, have a look at the [Semantic Search Guide](/docs/semantic-search).
     * @throws {@link Cohere.BadRequestError}
     * @throws {@link Cohere.TooManyRequestsError}
     * @throws {@link Cohere.InternalServerError}
     *
     * @example
     *     await cohere.embed({
     *         texts: ["string"],
     *         model: "string",
     *         inputType: Cohere.EmbedInputType.SearchDocument,
     *         embeddingTypes: [Cohere.EmbeddingType.Float],
     *         truncate: Cohere.EmbedRequestTruncate.None
     *     })
     */
    embed(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "embed"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.EmbedRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.EmbedResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 400:
                        throw new Cohere.BadRequestError(_response.error.body);
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    case 500:
                        throw new Cohere.InternalServerError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * This endpoint takes in a query and a list of texts and produces an ordered array with each text assigned a relevance score.
     * @throws {@link Cohere.TooManyRequestsError}
     *
     * @example
     *     await cohere.rerank({
     *         model: "rerank-english-v2.0",
     *         query: "What is the capital of the United States?",
     *         documents: ["Carson City is the capital city of the American state of Nevada.", "The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.", "Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.", "Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."]
     *     })
     */
    rerank(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "rerank"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.RerankRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.RerankResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * This endpoint makes a prediction about which label fits the specified text inputs best. To make a prediction, Classify uses the provided `examples` of text + label pairs as a reference.
     * Note: [Fine-tuned models](https://docs.cohere.com/docs/classify-fine-tuning) trained on classification examples don't require the `examples` parameter to be passed in explicitly.
     * @throws {@link Cohere.BadRequestError}
     * @throws {@link Cohere.TooManyRequestsError}
     * @throws {@link Cohere.InternalServerError}
     *
     * @example
     *     await cohere.classify({
     *         inputs: ["Confirm your email address", "hey i need u to send some $"],
     *         examples: [{
     *                 text: "Dermatologists don't like her!",
     *                 label: "Spam"
     *             }, {
     *                 text: "Hello, open to this?",
     *                 label: "Spam"
     *             }, {
     *                 text: "I need help please wire me $1000 right now",
     *                 label: "Spam"
     *             }, {
     *                 text: "Nice to know you ;)",
     *                 label: "Spam"
     *             }, {
     *                 text: "Please help me?",
     *                 label: "Spam"
     *             }, {
     *                 text: "Your parcel will be delivered today",
     *                 label: "Not spam"
     *             }, {
     *                 text: "Review changes to our Terms and Conditions",
     *                 label: "Not spam"
     *             }, {
     *                 text: "Weekly sync notes",
     *                 label: "Not spam"
     *             }, {
     *                 text: "Re: Follow up from today\u2019s meeting",
     *                 label: "Not spam"
     *             }, {
     *                 text: "Pre-read for tomorrow",
     *                 label: "Not spam"
     *             }],
     *         preset: "my-preset-a58sbd"
     *     })
     */
    classify(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "classify"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.ClassifyRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.ClassifyResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 400:
                        throw new Cohere.BadRequestError(_response.error.body);
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    case 500:
                        throw new Cohere.InternalServerError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * > 🚧 Warning
     * >
     * > This API is marked as "Legacy" and is no longer maintained. Follow the [migration guide](/docs/migrating-from-cogenerate-to-cochat) to start using the Chat API.
     *
     * Generates a summary in English for a given text.
     * @throws {@link Cohere.TooManyRequestsError}
     *
     * @example
     *     await cohere.summarize({
     *         text: "Ice cream is a sweetened frozen food typically eaten as a snack or dessert. It may be made from milk or cream and is flavoured with a sweetener, either sugar or an alternative, and a spice, such as cocoa or vanilla, or with fruit such as strawberries or peaches. It can also be made by whisking a flavored cream base and liquid nitrogen together. Food coloring is sometimes added, in addition to stabilizers. The mixture is cooled below the freezing point of water and stirred to incorporate air spaces and to prevent detectable ice crystals from forming. The result is a smooth, semi-solid foam that is solid at very low temperatures (below 2 \u00B0C or 35 \u00B0F). It becomes more malleable as its temperature increases.\n\nThe meaning of the name \"ice cream\" varies from one country to another. In some countries, such as the United States, \"ice cream\" applies only to a specific variety, and most governments regulate the commercial use of the various terms according to the relative quantities of the main ingredients, notably the amount of cream. Products that do not meet the criteria to be called ice cream are sometimes labelled \"frozen dairy dessert\" instead. In other countries, such as Italy and Argentina, one word is used fo\r all variants. Analogues made from dairy alternatives, such as goat's or sheep's milk, or milk substitutes (e.g., soy, cashew, coconut, almond milk or tofu), are available for those who are lactose intolerant, allergic to dairy protein or vegan."
     *     })
     */
    summarize(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "summarize"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.SummarizeRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.SummarizeResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * This endpoint splits input text into smaller units called tokens using byte-pair encoding (BPE). To learn more about tokenization and byte pair encoding, see the tokens page.
     * @throws {@link Cohere.BadRequestError}
     * @throws {@link Cohere.TooManyRequestsError}
     * @throws {@link Cohere.InternalServerError}
     *
     * @example
     *     await cohere.tokenize({
     *         text: "tokenize me! :D",
     *         model: "command"
     *     })
     */
    tokenize(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "tokenize"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.TokenizeRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.TokenizeResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 400:
                        throw new Cohere.BadRequestError(_response.error.body);
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    case 500:
                        throw new Cohere.InternalServerError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    /**
     * This endpoint takes tokens using byte-pair encoding and returns their text representation. To learn more about tokenization and byte pair encoding, see the tokens page.
     * @throws {@link Cohere.TooManyRequestsError}
     *
     * @example
     *     await cohere.detokenize({
     *         tokens: [10104, 12221, 1315, 34, 1420, 69],
     *         model: "command"
     *     })
     */
    detokenize(request, requestOptions) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const _response = yield core.fetcher({
                url: (0, url_join_1.default)((_a = (yield core.Supplier.get(this._options.environment))) !== null && _a !== void 0 ? _a : environments.CohereEnvironment.Production, "detokenize"),
                method: "POST",
                headers: {
                    Authorization: yield this._getAuthorizationHeader(),
                    "X-Client-Name": (yield core.Supplier.get(this._options.clientName)) != null
                        ? yield core.Supplier.get(this._options.clientName)
                        : undefined,
                    "X-Fern-Language": "JavaScript",
                    "X-Fern-SDK-Name": "cohere-ai",
                    "X-Fern-SDK-Version": "7.9.5",
                    "X-Fern-Runtime": core.RUNTIME.type,
                    "X-Fern-Runtime-Version": core.RUNTIME.version,
                },
                contentType: "application/json",
                body: yield serializers.DetokenizeRequest.jsonOrThrow(request, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                }),
                timeoutMs: (requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.timeoutInSeconds) != null ? requestOptions.timeoutInSeconds * 1000 : 300000,
                maxRetries: requestOptions === null || requestOptions === void 0 ? void 0 : requestOptions.maxRetries,
            });
            if (_response.ok) {
                return yield serializers.DetokenizeResponse.parseOrThrow(_response.body, {
                    unrecognizedObjectKeys: "passthrough",
                    allowUnrecognizedUnionMembers: true,
                    allowUnrecognizedEnumValues: true,
                    skipValidation: true,
                    breadcrumbsPrefix: ["response"],
                });
            }
            if (_response.error.reason === "status-code") {
                switch (_response.error.statusCode) {
                    case 429:
                        throw new Cohere.TooManyRequestsError(_response.error.body);
                    default:
                        throw new errors.CohereError({
                            statusCode: _response.error.statusCode,
                            body: _response.error.body,
                        });
                }
            }
            switch (_response.error.reason) {
                case "non-json":
                    throw new errors.CohereError({
                        statusCode: _response.error.statusCode,
                        body: _response.error.rawBody,
                    });
                case "timeout":
                    throw new errors.CohereTimeoutError();
                case "unknown":
                    throw new errors.CohereError({
                        message: _response.error.errorMessage,
                    });
            }
        });
    }
    get embedJobs() {
        var _a;
        return ((_a = this._embedJobs) !== null && _a !== void 0 ? _a : (this._embedJobs = new Client_1.EmbedJobs(this._options)));
    }
    get datasets() {
        var _a;
        return ((_a = this._datasets) !== null && _a !== void 0 ? _a : (this._datasets = new Client_2.Datasets(this._options)));
    }
    get connectors() {
        var _a;
        return ((_a = this._connectors) !== null && _a !== void 0 ? _a : (this._connectors = new Client_3.Connectors(this._options)));
    }
    get models() {
        var _a;
        return ((_a = this._models) !== null && _a !== void 0 ? _a : (this._models = new Client_4.Models(this._options)));
    }
    get finetuning() {
        var _a;
        return ((_a = this._finetuning) !== null && _a !== void 0 ? _a : (this._finetuning = new Client_5.Finetuning(this._options)));
    }
    _getAuthorizationHeader() {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const bearer = (_a = (yield core.Supplier.get(this._options.token))) !== null && _a !== void 0 ? _a : process === null || process === void 0 ? void 0 : process.env["CO_API_KEY"];
            if (bearer == null) {
                throw new errors.CohereError({
                    message: "Please specify CO_API_KEY when instantiating the client.",
                });
            }
            return `Bearer ${bearer}`;
        });
    }
}
exports.CohereClient = CohereClient;
