import { Embeddings } from "@langchain/core/embeddings";
import { chunkArray } from "@langchain/core/utils/chunk_array";

//#region src/embeddings.ts
var CloudflareWorkersAIEmbeddings = class extends Embeddings {
	modelName = "@cf/baai/bge-base-en-v1.5";
	model = "@cf/baai/bge-base-en-v1.5";
	batchSize = 50;
	stripNewLines = true;
	ai;
	constructor(fields) {
		super(fields);
		if (!fields.binding) throw new Error("Must supply a Workers AI binding, eg { binding: env.AI }");
		this.ai = fields.binding;
		this.modelName = fields?.model ?? fields.modelName ?? this.model;
		this.model = this.modelName;
		this.stripNewLines = fields.stripNewLines ?? this.stripNewLines;
	}
	async embedDocuments(texts) {
		const batches = chunkArray(this.stripNewLines ? texts.map((t) => t.replace(/\n/g, " ")) : texts, this.batchSize);
		const batchRequests = batches.map((batch) => this.runEmbedding(batch));
		const batchResponses = await Promise.all(batchRequests);
		const embeddings = [];
		for (let i = 0; i < batchResponses.length; i += 1) {
			const batchResponse = batchResponses[i];
			for (let j = 0; j < batchResponse.length; j += 1) embeddings.push(batchResponse[j]);
		}
		return embeddings;
	}
	async embedQuery(text) {
		const data = await this.runEmbedding([this.stripNewLines ? text.replace(/\n/g, " ") : text]);
		return data[0];
	}
	async runEmbedding(texts) {
		return this.caller.call(async () => {
			const response = await this.ai.run(this.model, { text: texts });
			return response.data;
		});
	}
};

//#endregion
export { CloudflareWorkersAIEmbeddings };
//# sourceMappingURL=embeddings.js.map