Files
Agent-Workbench/src/core/models/transport.mjs
T
2026-11-02 20:11:21 +08:00

18 lines
1.2 KiB
JavaScript

import { Agent,EnvHttpProxyAgent,fetch } from 'undici';
export class ModelTransport {
constructor({environment=process.env,lookup,connectTimeoutMs=10000}={}){
const setting=(lower,upper)=>environment[lower]??environment[upper]??'';
this.proxy={httpProxy:setting('http_proxy','HTTP_PROXY'),httpsProxy:setting('https_proxy','HTTPS_PROXY'),noProxy:setting('no_proxy','NO_PROXY')};
this.connect={autoSelectFamily:true,autoSelectFamilyAttemptTimeout:250,timeout:connectTimeoutMs,...(lookup?{lookup}:{})};
this.profile={schemaVersion:1,implementation:'undici-7.30.1',autoSelectFamily:true,familyAttemptTimeoutMs:250,connectTimeoutMs,
httpProxyConfigured:!!this.proxy.httpProxy,httpsProxyConfigured:!!this.proxy.httpsProxy,noProxyConfigured:!!this.proxy.noProxy};
}
fetch(url,options){
this.dispatcher??=this.profile.httpProxyConfigured||this.profile.httpsProxyConfigured
?new EnvHttpProxyAgent({...this.proxy,connect:this.connect,proxyTls:this.connect,requestTls:this.connect}):new Agent({connect:this.connect});
return fetch(url,{...options,dispatcher:this.dispatcher});
}
async close(){const dispatcher=this.dispatcher;this.dispatcher=null;await dispatcher?.close();}
}