-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathconfig.ts
More file actions
153 lines (140 loc) · 4.92 KB
/
Copy pathconfig.ts
File metadata and controls
153 lines (140 loc) · 4.92 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import { stripJsoncComments } from "./services/jsonc.js";
import { loadCredentials } from "./services/auth.js";
const CONFIG_DIR = join(homedir(), ".config", "opencode");
const CONFIG_FILES = [
join(CONFIG_DIR, "supermemory.jsonc"),
join(CONFIG_DIR, "supermemory.json"),
];
interface SupermemoryConfig {
apiKey?: string;
similarityThreshold?: number;
maxMemories?: number;
maxProjectMemories?: number;
maxProfileItems?: number;
injectProfile?: boolean;
containerTagPrefix?: string;
userContainerTag?: string;
projectContainerTag?: string;
filterPrompt?: string;
keywordPatterns?: string[];
compactionThreshold?: number;
autoRecallEveryPrompt?: boolean;
captureEveryNTurns?: number;
}
const DEFAULT_KEYWORD_PATTERNS = [
"remember",
"memorize",
"save\\s+this",
"note\\s+this",
"keep\\s+in\\s+mind",
"don'?t\\s+forget",
"learn\\s+this",
"store\\s+this",
"record\\s+this",
"make\\s+a\\s+note",
"take\\s+note",
"jot\\s+down",
"commit\\s+to\\s+memory",
"remember\\s+that",
"never\\s+forget",
"always\\s+remember",
];
const DEFAULTS: Required<Omit<SupermemoryConfig, "apiKey" | "userContainerTag" | "projectContainerTag">> = {
similarityThreshold: 0.6,
maxMemories: 5,
maxProjectMemories: 10,
maxProfileItems: 5,
injectProfile: true,
containerTagPrefix: "opencode",
filterPrompt: "You are a stateful coding agent. Remember all the information, including but not limited to user's coding preferences, tech stack, behaviours, workflows, and any other relevant details.",
keywordPatterns: [],
compactionThreshold: 0.80,
autoRecallEveryPrompt: false,
captureEveryNTurns: 0,
};
function isValidRegex(pattern: string): boolean {
try {
new RegExp(pattern);
return true;
} catch {
return false;
}
}
function validateCompactionThreshold(value: number | undefined): number {
if (value === undefined || typeof value !== 'number' || isNaN(value)) {
return DEFAULTS.compactionThreshold;
}
if (value <= 0 || value > 1) return DEFAULTS.compactionThreshold;
return value;
}
function loadRawConfig(): { config: SupermemoryConfig; existed: boolean } {
for (const path of CONFIG_FILES) {
if (existsSync(path)) {
try {
const content = readFileSync(path, "utf-8");
const json = stripJsoncComments(content);
return { config: JSON.parse(json) as SupermemoryConfig, existed: true };
} catch {
return { config: {}, existed: true };
}
}
}
return { config: {}, existed: false };
}
const { config: fileConfig, existed: configExisted } = loadRawConfig();
function getApiKey(): string | undefined {
if (process.env.SUPERMEMORY_API_KEY) return process.env.SUPERMEMORY_API_KEY;
if (fileConfig.apiKey) return fileConfig.apiKey;
return loadCredentials()?.apiKey;
}
export const SUPERMEMORY_API_KEY = getApiKey();
export function getApiBaseUrl(): string {
return (
process.env.SUPERMEMORY_API_URL ||
process.env.SUPERMEMORY_BASE_URL ||
loadCredentials()?.apiBaseUrl ||
"https://api.supermemory.ai"
);
}
export const CONFIG_FILE = CONFIG_FILES[1];
const DEFAULT_CONFIG_FILE = CONFIG_FILE ?? join(CONFIG_DIR, "supermemory.json");
export const CONFIG = {
similarityThreshold: fileConfig.similarityThreshold ?? DEFAULTS.similarityThreshold,
maxMemories: fileConfig.maxMemories ?? DEFAULTS.maxMemories,
maxProjectMemories: fileConfig.maxProjectMemories ?? DEFAULTS.maxProjectMemories,
maxProfileItems: fileConfig.maxProfileItems ?? DEFAULTS.maxProfileItems,
injectProfile: fileConfig.injectProfile ?? DEFAULTS.injectProfile,
containerTagPrefix: fileConfig.containerTagPrefix ?? DEFAULTS.containerTagPrefix,
userContainerTag: fileConfig.userContainerTag,
projectContainerTag: fileConfig.projectContainerTag,
filterPrompt: fileConfig.filterPrompt ?? DEFAULTS.filterPrompt,
keywordPatterns: [
...DEFAULT_KEYWORD_PATTERNS,
...(fileConfig.keywordPatterns ?? []).filter(isValidRegex),
],
compactionThreshold: validateCompactionThreshold(fileConfig.compactionThreshold),
autoRecallEveryPrompt:
fileConfig.autoRecallEveryPrompt ??
(configExisted ? true : DEFAULTS.autoRecallEveryPrompt),
captureEveryNTurns:
fileConfig.captureEveryNTurns ??
(configExisted ? 3 : DEFAULTS.captureEveryNTurns),
};
export function isConfigured(): boolean {
return !!SUPERMEMORY_API_KEY;
}
export function writeInstallDefaults(isExistingInstall: boolean): void {
const current = loadRawConfig().config;
const next: SupermemoryConfig = { ...current };
if (isExistingInstall) {
if (next.autoRecallEveryPrompt === undefined) next.autoRecallEveryPrompt = true;
if (next.captureEveryNTurns === undefined) next.captureEveryNTurns = 3;
} else {
next.autoRecallEveryPrompt = false;
next.captureEveryNTurns = 0;
}
writeFileSync(DEFAULT_CONFIG_FILE, JSON.stringify(next, null, 2));
}