"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExaRetriever = exports._getMetadata = void 0;
const retrievers_1 = require("@langchain/core/retrievers");
const documents_1 = require("@langchain/core/documents");
function _getMetadata(result) {
    const newMetadata = { ...result };
    delete newMetadata.text;
    return newMetadata;
}
exports._getMetadata = _getMetadata;
/**
 * @example
 * ```typescript
 * const retriever = new ExaRetriever({
 *   client: new Exa(
 *     process.env.EXA_API_KEY,
 *     process.env.EXA_BASE_URL,
 *   ),
 * });
 * const docs = await retriever.getRelevantDocuments("hello");
 * ```
 */
class ExaRetriever extends retrievers_1.BaseRetriever {
    static lc_name() {
        return "ExaRetriever";
    }
    constructor(fields) {
        super(fields);
        Object.defineProperty(this, "lc_namespace", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: ["langchain", "retrievers", "exa"]
        });
        Object.defineProperty(this, "client", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "searchArgs", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.client = fields.client;
        this.searchArgs = fields.searchArgs;
    }
    async _getRelevantDocuments(query) {
        const res = await this.client.searchAndContents(query, this.searchArgs);
        const documents = [];
        for (const result of res.results) {
            let pageContent;
            if ("text" in result) {
                pageContent = result.text;
            }
            else if ("highlights" in result) {
                pageContent = result.highlights.join("\n\n");
            }
            else {
                pageContent = "No results found.";
            }
            documents.push(new documents_1.Document({
                pageContent,
                metadata: _getMetadata(result),
            }));
        }
        return documents;
    }
}
exports.ExaRetriever = ExaRetriever;
