MCPcopy Create free account
hub / github.com/EvoMap/evolver / _proxyOpenAIResponses

Method _proxyOpenAIResponses

src/proxy/index.js:887–957  ·  view source on GitHub ↗
(reqPath, body, opts = {})

Source from the content-addressed store, hash-verified

885 // proxy token is consumed by ProxyHttpServer and must never be forwarded as
886 // upstream auth; the daemon supplies the real upstream key from env.
887 async _proxyOpenAIResponses(reqPath, body, opts = {}) {
888 const baseUrl = resolveOpenAIBaseUrl(opts.baseUrl || this._openaiBaseUrl || DEFAULT_OPENAI_BASE_URL, {
889 trustedOverride: !!opts.baseUrl || this._openaiBaseUrlTrusted,
890 });
891 const inbound = opts.inboundHeaders || {};
892 const timeoutMs = opts.timeoutMs || 60_000;
893
894 const fwd = { 'content-type': 'application/json' };
895 for (const [k, v] of Object.entries(inbound)) {
896 if (v === undefined || v === null) continue;
897 const lk = k.toLowerCase();
898 if (
899 lk === 'openai-organization'
900 || lk === 'openai-project'
901 || lk === 'openai-beta'
902 || lk.startsWith('x-stainless-')
903 ) {
904 fwd[lk] = Array.isArray(v) ? v.join(', ') : String(v);
905 }
906 }
907
908 const upstreamKey = process.env.EVOMAP_OPENAI_API_KEY || process.env.OPENAI_API_KEY || '';
909 if (!upstreamKey) {
910 const err = new Error('openai api key required');
911 err.statusCode = 401;
912 throw err;
913 }
914 if (upstreamKey) {
915 fwd.authorization = `Bearer ${upstreamKey}`;
916 }
917
918 const endpoint = `${baseUrl}${reqPath}`;
919 const abortController = new AbortController();
920 const timeoutErr = new Error('openai upstream timed out');
921 timeoutErr.name = 'TimeoutError';
922 const abortTimer = setTimeout(() => abortController.abort(timeoutErr), timeoutMs);
923 abortTimer.unref?.();
924 const method = (opts.method || 'POST').toUpperCase();
925 const init = { method, headers: fwd, signal: abortController.signal };
926 if (method !== 'GET' && method !== 'HEAD') init.body = JSON.stringify(body || {}); // GET (e.g. /models) sends no body
927 let res;
928 try {
929 res = await fetch(endpoint, init);
930 } catch (err) {
931 clearTimeout(abortTimer);
932 throw makeOpenAIGatewayError(err);
933 }
934
935 const headers = Object.fromEntries(res.headers.entries());
936 const contentType = (headers['content-type'] || '').toLowerCase();
937 const isStream = contentType.includes('text/event-stream');
938 if (isStream) clearTimeout(abortTimer);
939
940 const readText = async () => {
941 try {
942 return await res.text();
943 } catch (err) {
944 throw makeOpenAIGatewayError(err);

Callers 3

startMethod · 0.95

Calls 3

resolveOpenAIBaseUrlFunction · 0.85
makeOpenAIGatewayErrorFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected