import { SageMakerRuntimeClient, InvokeEndpointCommand, } from "@aws-sdk/client-sagemaker-runtime";
import { Embeddings } from "@langchain/core/embeddings";
export class SageMakerEndpointEmbeddings extends Embeddings {
    constructor(fields) {
        super(fields ?? {});
        Object.defineProperty(this, "endpointName", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "client", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        const regionName = fields.clientOptions.region;
        if (!regionName) {
            throw new Error(`Please pass a "clientOptions" object with a "region" field to the constructor`);
        }
        const endpointName = fields?.endpointName;
        if (!endpointName) {
            throw new Error(`Please pass an "endpointName" field to the constructor`);
        }
        this.endpointName = fields.endpointName;
        this.client = new SageMakerRuntimeClient(fields.clientOptions);
    }
    async _embedText(text) {
        const inputBuffer = Buffer.from(JSON.stringify({
            inputs: [text],
        }));
        const response = await this.caller.call(() => this.client.send(new InvokeEndpointCommand({
            Body: inputBuffer,
            EndpointName: this.endpointName,
            ContentType: "application/json",
        })));
        return new TextDecoder().decode(response.Body);
    }
    embedQuery(document) {
        return this.caller.callWithOptions({}, this._embedText.bind(this), document);
    }
    embedDocuments(documents) {
        return Promise.all(documents.map((document) => this._embedText(document)));
    }
}
