const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
const require_event_source_parse = require('./utils/event_source_parse.cjs');
const __langchain_core_outputs = require_rolldown_runtime.__toESM(require("@langchain/core/outputs"));
const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
const __langchain_core_language_models_llms = require_rolldown_runtime.__toESM(require("@langchain/core/language_models/llms"));

//#region src/llms.ts
/**
* Class representing the CloudflareWorkersAI language model. It extends the LLM (Large
* Language Model) class, providing a standard interface for interacting
* with the CloudflareWorkersAI language model.
*/
var CloudflareWorkersAI = class extends __langchain_core_language_models_llms.LLM {
	model = "@cf/meta/llama-2-7b-chat-int8";
	cloudflareAccountId;
	cloudflareApiToken;
	baseUrl;
	streaming = false;
	static lc_name() {
		return "CloudflareWorkersAI";
	}
	lc_serializable = true;
	constructor(fields) {
		super(fields ?? {});
		this.model = fields?.model ?? this.model;
		this.streaming = fields?.streaming ?? this.streaming;
		this.cloudflareAccountId = fields?.cloudflareAccountId ?? (0, __langchain_core_utils_env.getEnvironmentVariable)("CLOUDFLARE_ACCOUNT_ID");
		this.cloudflareApiToken = fields?.cloudflareApiToken ?? (0, __langchain_core_utils_env.getEnvironmentVariable)("CLOUDFLARE_API_TOKEN");
		this.baseUrl = fields?.baseUrl ?? `https://api.cloudflare.com/client/v4/accounts/${this.cloudflareAccountId}/ai/run`;
		if (this.baseUrl.endsWith("/")) this.baseUrl = this.baseUrl.slice(0, -1);
	}
	/**
	* Method to validate the environment.
	*/
	validateEnvironment() {
		if (this.baseUrl === void 0) {
			if (!this.cloudflareAccountId) throw new Error(`No Cloudflare account ID found. Please provide it when instantiating the CloudflareWorkersAI class, or set it as "CLOUDFLARE_ACCOUNT_ID" in your environment variables.`);
			if (!this.cloudflareApiToken) throw new Error(`No Cloudflare API key found. Please provide it when instantiating the CloudflareWorkersAI class, or set it as "CLOUDFLARE_API_KEY" in your environment variables.`);
		}
	}
	/** Get the identifying parameters for this LLM. */
	get identifyingParams() {
		return { model: this.model };
	}
	/**
	* Get the parameters used to invoke the model
	*/
	invocationParams() {
		return { model: this.model };
	}
	/** Get the type of LLM. */
	_llmType() {
		return "cloudflare";
	}
	async _request(prompt, options, stream) {
		this.validateEnvironment();
		const url = `${this.baseUrl}/${this.model}`;
		const headers = {
			Authorization: `Bearer ${this.cloudflareApiToken}`,
			"Content-Type": "application/json"
		};
		const data = {
			prompt,
			stream
		};
		return this.caller.call(async () => {
			const response = await fetch(url, {
				method: "POST",
				headers,
				body: JSON.stringify(data),
				signal: options.signal
			});
			if (!response.ok) {
				const error = /* @__PURE__ */ new Error(`Cloudflare LLM call failed with status code ${response.status}`);
				error.response = response;
				throw error;
			}
			return response;
		});
	}
	async *_streamResponseChunks(prompt, options, runManager) {
		const response = await this._request(prompt, options, true);
		if (!response.body) throw new Error("Empty response from Cloudflare. Please try again.");
		const stream = require_event_source_parse.convertEventStreamToIterableReadableDataStream(response.body);
		for await (const chunk of stream) if (chunk !== "[DONE]") {
			const parsedChunk = JSON.parse(chunk);
			const generationChunk = new __langchain_core_outputs.GenerationChunk({ text: parsedChunk.response });
			yield generationChunk;
			runManager?.handleLLMNewToken(generationChunk.text ?? "");
		}
	}
	/** Call out to CloudflareWorkersAI's complete endpoint.
	Args:
	prompt: The prompt to pass into the model.
	Returns:
	The string generated by the model.
	Example:
	let response = CloudflareWorkersAI.call("Tell me a joke.");
	*/
	async _call(prompt, options, runManager) {
		if (!this.streaming) {
			const response = await this._request(prompt, options);
			const responseData = await response.json();
			return responseData.result.response;
		} else {
			const stream = this._streamResponseChunks(prompt, options, runManager);
			let finalResult;
			for await (const chunk of stream) if (finalResult === void 0) finalResult = chunk;
			else finalResult = finalResult.concat(chunk);
			return finalResult?.text ?? "";
		}
	}
};

//#endregion
exports.CloudflareWorkersAI = CloudflareWorkersAI;
//# sourceMappingURL=llms.cjs.map