import { b as getEnvString, i as logger_default, y as getEnvInt } from "./logger-CATn0kWr.js";
import { n as telemetry_default } from "./telemetry-DJmQomyY.js";

//#region src/providers/bedrock/base.ts
var AwsBedrockGenericProvider = class {
	modelName;
	env;
	bedrock;
	config;
	constructor(modelName, options = {}) {
		const { config, id, env } = options;
		this.env = env;
		this.modelName = modelName;
		this.config = config || {};
		this.id = id ? () => id : this.id;
		if (this.config.guardrailIdentifier) telemetry_default.record("feature_used", {
			feature: "guardrail",
			provider: "bedrock"
		});
	}
	id() {
		return `bedrock:${this.modelName}`;
	}
	toString() {
		return `[Amazon Bedrock Provider ${this.modelName}]`;
	}
	getApiKey() {
		return this.config.apiKey || getEnvString("AWS_BEARER_TOKEN_BEDROCK");
	}
	async getCredentials() {
		if (this.config.accessKeyId && this.config.secretAccessKey) {
			logger_default.debug(`Using credentials from config file`);
			return {
				accessKeyId: this.config.accessKeyId,
				secretAccessKey: this.config.secretAccessKey,
				sessionToken: this.config.sessionToken
			};
		}
		if (this.getApiKey()) {
			logger_default.debug(`Using Bedrock API key authentication`);
			return;
		}
		if (this.config.profile) {
			logger_default.debug(`Using SSO profile: ${this.config.profile}`);
			try {
				const { fromSSO } = await import("@aws-sdk/credential-provider-sso");
				return fromSSO({ profile: this.config.profile });
			} catch (err) {
				logger_default.error(`Error loading @aws-sdk/credential-provider-sso: ${err}`);
				throw new Error("The @aws-sdk/credential-provider-sso package is required for SSO profiles. Please install it: npm install @aws-sdk/credential-provider-sso");
			}
		}
		logger_default.debug(`No explicit credentials in config, falling back to AWS default chain`);
	}
	async getBedrockInstance() {
		if (!this.bedrock) {
			let handler;
			const apiKey = this.getApiKey();
			if (getEnvString("HTTP_PROXY") || getEnvString("HTTPS_PROXY") || apiKey) try {
				const { NodeHttpHandler } = await import("@smithy/node-http-handler");
				const { ProxyAgent } = await import("proxy-agent");
				const proxyAgent = getEnvString("HTTP_PROXY") || getEnvString("HTTPS_PROXY") ? new ProxyAgent() : void 0;
				handler = new NodeHttpHandler({
					...proxyAgent ? { httpsAgent: proxyAgent } : {},
					requestTimeout: 3e5
				});
				if (apiKey) {
					const originalHandle = handler.handle.bind(handler);
					handler.handle = async (request, options) => {
						request.headers = {
							...request.headers,
							Authorization: `Bearer ${apiKey}`
						};
						return originalHandle(request, options);
					};
				}
			} catch {
				const reason = apiKey ? "API key authentication requires the @smithy/node-http-handler package" : "Proxy configuration requires the @smithy/node-http-handler package";
				throw new Error(`${reason}. Please install it in your project or globally.`);
			}
			try {
				const { BedrockRuntime } = await import("@aws-sdk/client-bedrock-runtime");
				const credentials = await this.getCredentials();
				this.bedrock = new BedrockRuntime({
					region: this.getRegion(),
					maxAttempts: getEnvInt("AWS_BEDROCK_MAX_RETRIES", 10),
					retryMode: "adaptive",
					...credentials ? { credentials } : {},
					...handler ? { requestHandler: handler } : {},
					...this.config.endpoint ? { endpoint: this.config.endpoint } : {}
				});
			} catch (err) {
				logger_default.error(`Error creating BedrockRuntime: ${err}`);
				throw new Error("The @aws-sdk/client-bedrock-runtime package is required as a peer dependency. Please install it in your project or globally.");
			}
		}
		return this.bedrock;
	}
	getRegion() {
		return this.config?.region || this.env?.AWS_BEDROCK_REGION || getEnvString("AWS_BEDROCK_REGION") || "us-east-1";
	}
};

//#endregion
export { AwsBedrockGenericProvider as t };
//# sourceMappingURL=base-DhoVWN3i.js.map