feat: request timeout config (#191)

Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
This commit is contained in:
Rocktim
2023-04-08 17:54:47 +05:30
committed by GitHub
parent 75eee29a4e
commit 42a2a39f6f
7 changed files with 79 additions and 7 deletions

View File

@@ -58,6 +58,7 @@ export default async (
config.locale,
staged.diff,
config.generate,
config.timeout,
config.proxy,
);
} finally {

View File

@@ -45,6 +45,7 @@ export default () => (async () => {
config.locale,
staged!.diff,
config.generate,
config.timeout,
config.proxy,
);
} finally {

View File

@@ -68,6 +68,18 @@ const configParsers = {
return model as TiktokenModel;
},
timeout(timeout?: string) {
if (!timeout) {
return 10_000;
}
parseAssert('timeout', /^\d+$/.test(timeout), 'Must be an integer');
const parsed = Number(timeout);
parseAssert('timeout', parsed >= 500, 'Must be greater than 500ms');
return parsed;
},
} as const;
type ConfigKeys = keyof typeof configParsers;

View File

@@ -10,6 +10,7 @@ const httpsPost = async (
path: string,
headers: Record<string, string>,
json: unknown,
timeout: number,
proxy?: string,
) => new Promise<{
request: ClientRequest;
@@ -28,7 +29,7 @@ const httpsPost = async (
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postContent),
},
timeout: 10_000, // 10s
timeout,
agent: (
proxy
? createHttpsProxyAgent(proxy)
@@ -50,7 +51,7 @@ const httpsPost = async (
request.on('error', reject);
request.on('timeout', () => {
request.destroy();
reject(new KnownError('Request timed out'));
reject(new KnownError(`Time out error: request took over ${timeout}ms. Try increasing the \`timeout\` config, or checking the OpenAI API status https://status.openai.com`));
});
request.write(postContent);
@@ -60,6 +61,7 @@ const httpsPost = async (
const createChatCompletion = async (
apiKey: string,
json: CreateChatCompletionRequest,
timeout: number,
proxy?: string,
) => {
const { response, data } = await httpsPost(
@@ -69,6 +71,7 @@ const createChatCompletion = async (
Authorization: `Bearer ${apiKey}`,
},
json,
timeout,
proxy,
);
@@ -105,6 +108,7 @@ export const generateCommitMessage = async (
locale: string,
diff: string,
completions: number,
timeout: number,
proxy?: string,
) => {
const prompt = getPrompt(locale, diff);
@@ -126,6 +130,7 @@ export const generateCommitMessage = async (
stream: false,
n: completions,
},
timeout,
proxy,
);