feat(ai): Support HTTP proxy through environment variables

This commit is contained in:
haoqixu 2026-01-25 15:47:24 +08:00
parent 3635e45ffd
commit 1e718e63ea
4 changed files with 314 additions and 13 deletions

View file

@ -32,6 +32,8 @@
"chalk": "^5.6.2",
"openai": "6.10.0",
"partial-json": "^0.1.7",
"proxy-agent": "^6.5.0",
"undici": "^7.19.1",
"zod-to-json-schema": "^3.24.6"
},
"keywords": [

View file

@ -1,5 +1,6 @@
import {
BedrockRuntimeClient,
type BedrockRuntimeClientConfig,
StopReason as BedrockStopReason,
type Tool as BedrockTool,
CachePointType,
@ -84,11 +85,42 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOpt
const blocks = output.content as Block[];
const config: BedrockRuntimeClientConfig = {
region: options.region,
profile: options.profile,
};
// in Node.js/Bun environment only
if (typeof process !== "undefined" && (process.versions?.node || process.versions?.bun)) {
config.region = config.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION;
if (
process.env.HTTP_PROXY ||
process.env.HTTPS_PROXY ||
process.env.NO_PROXY ||
process.env.http_proxy ||
process.env.https_proxy ||
process.env.no_proxy
) {
const nodeHttpHandler = await import("@smithy/node-http-handler");
const proxyAgent = await import("proxy-agent");
const agent = new proxyAgent.ProxyAgent();
// Bedrock runtime uses NodeHttp2Handler by default since v3.798.0, which is based
// on `http2` module and has no support for http agent.
// Use NodeHttpHandler to support http agent.
config.requestHandler = new nodeHttpHandler.NodeHttpHandler({
httpAgent: agent,
httpsAgent: agent,
});
}
}
config.region = config.region || "us-east-1";
try {
const client = new BedrockRuntimeClient({
region: options.region || process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || "us-east-1",
profile: options.profile,
});
const client = new BedrockRuntimeClient(config);
const commandInput = {
modelId: model.id,

View file

@ -14,6 +14,15 @@ import type {
export { getEnvApiKey } from "./env-api-keys.js";
// Set up http proxy according to env variables for `fetch` based SDKs in Node.js.
// Bun has builtin support for this.
if (typeof process !== "undefined" && process.versions?.node) {
import("undici").then((m) => {
const { EnvHttpProxyAgent, setGlobalDispatcher } = m;
setGlobalDispatcher(new EnvHttpProxyAgent());
});
}
function resolveApiProvider(api: Api) {
const provider = getApiProvider(api);
if (!provider) {