const require_logger = require('./logger-B3t6CmTC.cjs');
const require_telemetry = require('./telemetry-ltJIXIXu.cjs');

//#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) require_telemetry.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 || require_logger.getEnvString("AWS_BEARER_TOKEN_BEDROCK");
	}
	async getCredentials() {
		if (this.config.accessKeyId && this.config.secretAccessKey) {
			require_logger.logger_default.debug(`Using credentials from config file`);
			return {
				accessKeyId: this.config.accessKeyId,
				secretAccessKey: this.config.secretAccessKey,
				sessionToken: this.config.sessionToken
			};
		}
		if (this.getApiKey()) {
			require_logger.logger_default.debug(`Using Bedrock API key authentication`);
			return;
		}
		if (this.config.profile) {
			require_logger.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) {
				require_logger.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");
			}
		}
		require_logger.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 (require_logger.getEnvString("HTTP_PROXY") || require_logger.getEnvString("HTTPS_PROXY") || apiKey) try {
				const { NodeHttpHandler } = await import("@smithy/node-http-handler");
				const { ProxyAgent } = await import("proxy-agent");
				const proxyAgent = require_logger.getEnvString("HTTP_PROXY") || require_logger.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: require_logger.getEnvInt("AWS_BEDROCK_MAX_RETRIES", 10),
					retryMode: "adaptive",
					...credentials ? { credentials } : {},
					...handler ? { requestHandler: handler } : {},
					...this.config.endpoint ? { endpoint: this.config.endpoint } : {}
				});
			} catch (err) {
				require_logger.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 || require_logger.getEnvString("AWS_BEDROCK_REGION") || "us-east-1";
	}
};

//#endregion
Object.defineProperty(exports, 'AwsBedrockGenericProvider', {
  enumerable: true,
  get: function () {
    return AwsBedrockGenericProvider;
  }
});
//# sourceMappingURL=base-CYo31Ko6.cjs.map