refactor(api.ts): reformat imports to be grouped and sorted alphabetically
refactor(api.ts): replace axios with execa to get the latest version of opencommit from npm registry
This commit is contained in:
+14
-11
@@ -7,9 +7,14 @@ import {
|
|||||||
OpenAIApi
|
OpenAIApi
|
||||||
} from 'openai';
|
} from 'openai';
|
||||||
|
|
||||||
import {CONFIG_MODES, DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
|
import {
|
||||||
import {tokenCount} from './utils/tokenCount';
|
CONFIG_MODES,
|
||||||
import {GenerateCommitMessageErrorEnum} from './generateCommitMessageFromGitDiff';
|
DEFAULT_MODEL_TOKEN_LIMIT,
|
||||||
|
getConfig
|
||||||
|
} from './commands/config';
|
||||||
|
import { tokenCount } from './utils/tokenCount';
|
||||||
|
import { GenerateCommitMessageErrorEnum } from './generateCommitMessageFromGitDiff';
|
||||||
|
import { execa } from 'execa';
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
||||||
@@ -58,11 +63,11 @@ class OpenAi {
|
|||||||
max_tokens: maxTokens || 500
|
max_tokens: maxTokens || 500
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const REQUEST_TOKENS = messages.map(
|
const REQUEST_TOKENS = messages
|
||||||
(msg) => tokenCount(msg.content) + 4
|
.map((msg) => tokenCount(msg.content) + 4)
|
||||||
).reduce((a, b) => a + b, 0);
|
.reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
if (REQUEST_TOKENS > (DEFAULT_MODEL_TOKEN_LIMIT - maxTokens)) {
|
if (REQUEST_TOKENS > DEFAULT_MODEL_TOKEN_LIMIT - maxTokens) {
|
||||||
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
|
throw new Error(GenerateCommitMessageErrorEnum.tooMuchTokens);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,10 +103,8 @@ export const getOpenCommitLatestVersion = async (): Promise<
|
|||||||
string | undefined
|
string | undefined
|
||||||
> => {
|
> => {
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.get(
|
const { stdout } = await execa('npm', ['view', 'opencommit', 'version']);
|
||||||
'https://unpkg.com/opencommit/package.json'
|
return stdout;
|
||||||
);
|
|
||||||
return data.version;
|
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
outro('Error while getting the latest version of opencommit');
|
outro('Error while getting the latest version of opencommit');
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user