Skip to content

Commit 541ce59

Browse files
author
김덕현
committedDec 30, 2023
refactor: Update TS config to ES2021 and add new file
Update the target property in tsconfig.json from es2020 to es2021. Create a new file 'src/utils/removeEscapeCharacters.ts' to implement a utility function for removing escape characters. made by ollama-commit
1 parent 0048601 commit 541ce59

9 files changed

+60
-16
lines changed
 

‎src/config.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import * as dotenv from "dotenv";
22

3-
import { DEFAULT_LANGUAGE, DEFAULT_MODEL, DEFAULT_PROVIDER } from "./constants";
3+
import {
4+
DEFAULT_API_HOST,
5+
DEFAULT_LANGUAGE,
6+
DEFAULT_MODEL,
7+
DEFAULT_PROVIDER,
8+
} from "./constants";
49
import { getArgs } from "./utils/getArgs";
510

611
dotenv.config();
712

813
export const args = getArgs();
914

15+
/**
16+
* String configurations
17+
*/
1018
export const PROVIDER = DEFAULT_PROVIDER;
1119
export const MODEL = args.model ?? process.env.MODEL ?? DEFAULT_MODEL;
1220
export const RESPONSE_LANGUAGE =
1321
args.language ?? process.env.LANGUAGE ?? DEFAULT_LANGUAGE;
22+
export const API_HOST =
23+
args.api_host ?? process.env.API_HOST ?? DEFAULT_API_HOST;
24+
25+
/**
26+
* Boolean configurations
27+
*/
1428
export const SIGNATURE = args.signature ?? process.env.SIGNATURE;

‎src/constants/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export const DEFAULT_PROVIDER = "ollama";
44
export const DEFAULT_MODEL = "mistral";
55
export const DEFAULT_LANGUAGE = "English";
66
export const DEFAULT_MAX_DIFF_LENGTH = 8000;
7+
export const DEFAULT_API_HOST = "http://localhost:11434";

‎src/constants/prompt.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import { RESPONSE_LANGUAGE } from "../config";
22

33
export const defaultSystemMessage = `
4-
You are the creator of the commit message.
5-
The only key values in JSON are type, scope, title, and body.
6-
The output should provide only one JSON response and should not provide unnecessary output.
4+
Generate a concise JSON response written in present tense for code differences using the specifications given below:
5+
JSON must have the following keys: type, scope, title, and body.
6+
Select the type that best describes git diff from the type-description JSON below:
7+
{
8+
"docs": "Documentation only changes",
9+
"style": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)",
10+
"refactor": "A code change that neither fixes a bug nor adds a feature",
11+
"perf": "A code change that improves performance",
12+
"test": "Adding missing tests or correcting existing tests",
13+
"build": "Changes that affect the build system or external dependencies",
14+
"ci": "Changes to our CI configuration files and scripts",
15+
"chore": "Other changes that don't modify src or test files",
16+
"revert": "Reverts a previous commit",
17+
"feat": "A new feature",
18+
"fix": "A bug fix"
19+
}
20+
scope represents the scope of changes in a commit. scope is optional and is used to specify the scope affected by the commit, such as changed files, modules, components, etc.
21+
title creates a concise summary title for the commit. The first letter is capitalized and no periods are used.
22+
The body is the part that describes the details of the commit. You can describe in more detail the commit's changes, reasons, scope of impact, etc.
23+
724
You should respond in ${RESPONSE_LANGUAGE}.
825
926
An example output is:
1027
{
1128
"type": "fix",
1229
"scope": "api",
13-
"subject": "prevent racing of requests",
30+
"title": "prevent racing of requests",
1431
"body": [
1532
"Introduce a request id and a reference to latest request. Dismiss incoming responses other than from latest request.",
1633
"Remove timeouts which were used to mitigate the racing issue but are obsolete now."

‎src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@ const main = async () => {
3030
createCommit(message);
3131
};
3232

33-
main().catch((error) => console.error(error));
33+
main().catch((error) => {
34+
console.error(error);
35+
process.exit(1);
36+
});

‎src/utils/convertMessage.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ export const convertMessageToCommitFormat = (message: string): string => {
22
const obj = JSON.parse(message);
33

44
let commitMessage = `${obj.type}${obj.scope ? `(${obj.scope})` : ""}: ${
5-
obj.subject
5+
obj.title
66
}`;
77

8-
if (obj.description && obj.description.length > 0) {
9-
commitMessage += "\n\n" + obj.description.join("\n");
8+
if (obj.body && obj.body.length > 0) {
9+
commitMessage += "\n\n" + obj.body.join("\n");
1010
}
1111

1212
return commitMessage;

‎src/utils/generateCommitMessage.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { MODEL } from "../config";
1+
import { API_HOST, MODEL } from "../config";
22
import { defaultSystemMessage } from "../constants/prompt";
3+
import { removeEscapeCharacters } from "./removeEscapeCharacters";
34

45
export const generateCommitMessage = async (diff: string) => {
56
const { Ollama } = await import("ollama");
6-
const ollama = new Ollama();
7+
const ollama = new Ollama({
8+
address: API_HOST,
9+
});
710

811
let content = "";
912
for await (const token of ollama.generate(MODEL, diff, {
@@ -14,8 +17,9 @@ export const generateCommitMessage = async (diff: string) => {
1417
top_p: 0.9,
1518
},
1619
})) {
17-
// process.stdout.write(token);
20+
process.stdout.write(token);
1821
content += token;
1922
}
20-
return content;
23+
24+
return removeEscapeCharacters(content);
2125
};

‎src/utils/pullModel.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { SingleBar } from "cli-progress";
2-
import { MODEL, PROVIDER } from "../config";
2+
import { API_HOST, MODEL, PROVIDER } from "../config";
33

44
export const pullModel = async () => {
55
const { Ollama } = await import("ollama");
6-
const ollama = new Ollama();
6+
const ollama = new Ollama({
7+
address: API_HOST,
8+
});
79

810
const it = await ollama.pull(MODEL);
911
let pullingResult = await it.next();

‎src/utils/removeEscapeCharacters.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const removeEscapeCharacters = (str: string) => {
2+
return str.replaceAll(/\\(.)/g, "$1");
3+
};

‎tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "es2020",
3+
"target": "es2021",
44
"module": "esnext",
55
"strict": true,
66
"esModuleInterop": true,

0 commit comments

Comments
 (0)
Please sign in to comment.