Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support http protocol for custom OpenAI api url #51

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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