Skip to content

Commit

Permalink
fix: support http protocol for custom OpenAI api url
Browse files Browse the repository at this point in the history
  • Loading branch information
rookiezn committed Mar 6, 2024
1 parent 1092770 commit 431cc41
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
36 changes: 25 additions & 11 deletions src/axiosConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import { LocalStorage, showToast, Toast } from "@raycast/api";
import axios from "axios";
import EventEmitter from "events";
import { HttpsProxyAgent } from "hpagent";
import { HttpProxyAgent, HttpsProxyAgent } from "hpagent";
import { getMacSystemProxy } from "mac-system-proxy";
import { networkTimeout } from "./consts";

Expand All @@ -28,6 +28,7 @@ configDefaultAxios();
export const requestCostTime = "requestCostTime";

export let httpsAgent: HttpsProxyAgent | undefined;
export let httpAgent: HttpProxyAgent | undefined;

/**
* Becacuse get system proxy will block 0.4s, we need to get it after finish query.
Expand Down Expand Up @@ -184,12 +185,15 @@ export function getSystemProxyURL(): Promise<string | undefined> {
*
* * Note: this function will block ~0.4s, so should call it at the right time.
*/
export function getProxyAgent(): Promise<HttpsProxyAgent | undefined> {
export function getProxyAgent(isHttps: boolean = true): Promise<HttpsProxyAgent | HttpProxyAgent | undefined> {
console.log(`---> start getProxyAgent`);

if (httpsAgent) {
if (isHttps && httpsAgent) {
console.log(`---> return cached httpsAgent`);
return Promise.resolve(httpsAgent);
} else if (!isHttps && httpAgent) {
console.log(`---> return cached httpAgent`);
return Promise.resolve(httpAgent);
}

return new Promise((resolve) => {
Expand All @@ -203,17 +207,27 @@ export function getProxyAgent(): Promise<HttpsProxyAgent | undefined> {
}

console.log(`---> get system proxy url: ${systemProxyURL}`);
const agent = new HttpsProxyAgent({
keepAlive: true,
proxy: systemProxyURL,
});

httpsAgent = agent;
resolve(agent);
if (isHttps) {
httpsAgent = new HttpsProxyAgent({
keepAlive: true,
proxy: systemProxyURL,
});
resolve(httpsAgent);
} else {
httpAgent = new HttpProxyAgent({
keepAlive: true,
proxy: systemProxyURL,
});
resolve(httpAgent);
}
})
.catch((error) => {
console.error(`---> get system proxy url error: ${error}`);
httpsAgent = undefined;
if (isHttps) {
httpsAgent = undefined;
} else {
httpAgent = undefined;
}
resolve(undefined);
});
});
Expand Down
10 changes: 9 additions & 1 deletion src/translation/openAI/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ export async function requestOpenAIStreamTranslate(queryWordInfo: QueryWordInfo)
let openAIResult: QueryTypeResult;

const httpsAgent = await getProxyAgent();
const httpAgent = await getProxyAgent(false);
const agent = function (url: URL) {
if (url.protocol === "http:") {
return httpAgent;
} else {
return httpsAgent;
}
};
console.warn(`---> openai agent: ${JSON.stringify(httpsAgent)}`);

return new Promise((resolve, reject) => {
fetchSSE(`${url}`, {
method: "POST",
headers,
body: JSON.stringify(params),
agent: httpsAgent,
agent: agent,
signal: controller.signal,
onMessage: (msg) => {
// console.warn(`---> openai msg: ${JSON.stringify(msg)}`);
Expand Down

0 comments on commit 431cc41

Please sign in to comment.