-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathconfig_storage.ts
88 lines (71 loc) · 2.13 KB
/
config_storage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { homedir } from "os";
import { readFileSync, writeFileSync, existsSync } from "fs";
import { defaultPromptTemplate } from "./template.js";
const GLOBAL_CONFIG_PATH = `${homedir()}/.commitgpt.json`;
const LOCAL_CONFIG_PATH = `${process.cwd()}/.commitgpt.json`;
const GLOBAL_PROMPT_TEMPLATE_PATH = `${homedir()}/.commitgpt-template`;
const LOCAL_PROMPT_TEMPLATE_PATH = `${process.cwd()}/.commitgpt-template`;
interface Config {
apiKey?: string;
promptTemplate?: string;
model: string;
temperature: number;
maxTokens: number;
}
const defaultConfig = {
model: "text-davinci-003",
temperature: 0.5,
maxTokens: 2048,
} satisfies Config;
const writeJsonFile = (path: string, data: unknown) => {
writeFileSync(path, JSON.stringify(data, null, 2));
};
function ensureGlobal() {
if (!existsSync(GLOBAL_CONFIG_PATH)) {
writeJsonFile(GLOBAL_CONFIG_PATH, {});
}
}
function loadGlobal() {
return JSON.parse(readFileSync(GLOBAL_CONFIG_PATH, "utf-8"));
}
function loadLocal(): Partial<Config> {
if (!existsSync(LOCAL_CONFIG_PATH)) return {};
return JSON.parse(readFileSync(LOCAL_CONFIG_PATH, "utf-8"));
}
let cache = null;
function load() {
if (cache) return cache;
ensureGlobal();
const global = loadGlobal();
const local = loadLocal();
cache = { ...defaultConfig, ...global, ...local };
return cache;
}
function assertTempValid(t: string) {
// should include {{diff}}
if (!t.includes("{{diff}}")) {
throw new Error("Template must include {{diff}}");
}
}
export function loadPromptTemplate(): string {
if (existsSync(LOCAL_CONFIG_PATH)) {
const temp = readFileSync(LOCAL_PROMPT_TEMPLATE_PATH, "utf-8");
assertTempValid(temp);
return temp;
}
if (existsSync(GLOBAL_PROMPT_TEMPLATE_PATH)) {
const temp = readFileSync(GLOBAL_PROMPT_TEMPLATE_PATH, "utf-8");
assertTempValid(temp);
return temp;
}
return defaultPromptTemplate;
}
export function getConfig<T>(key: string): T {
const config = load();
return config[key];
}
export function setGlobalConfig(key: string, value: unknown) {
const config = loadGlobal();
config[key] = value;
writeJsonFile(GLOBAL_CONFIG_PATH, config);
}