-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathconfig.ts
More file actions
121 lines (109 loc) · 3.65 KB
/
config.ts
File metadata and controls
121 lines (109 loc) · 3.65 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
import { existsSync, readFileSync } 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;
}
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,
};
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 loadConfig(): SupermemoryConfig {
for (const path of CONFIG_FILES) {
if (existsSync(path)) {
try {
const content = readFileSync(path, "utf-8");
const json = stripJsoncComments(content);
return JSON.parse(json) as SupermemoryConfig;
} catch {
// Invalid config, use defaults
}
}
}
return {};
}
const fileConfig = loadConfig();
function getApiKey(): string | undefined {
// Priority: env var > config file > OAuth credentials
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 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),
};
export function isConfigured(): boolean {
return !!SUPERMEMORY_API_KEY;
}