-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathconfig.ts
More file actions
191 lines (172 loc) · 6.2 KB
/
Copy pathconfig.ts
File metadata and controls
191 lines (172 loc) · 6.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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");
export const PLUGIN_VERSION = "2.0.8";
const CONFIG_FILES = [
join(CONFIG_DIR, "supermemory.jsonc"),
join(CONFIG_DIR, "supermemory.json"),
];
export const DEFAULT_BASE_URL = "https://api.supermemory.ai";
interface SupermemoryConfig {
apiKey?: string;
baseUrl?: 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;
recallDirective?: string | null;
}
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" | "baseUrl" | "userContainerTag" | "projectContainerTag" | "recallDirective">> = {
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();
function normalizeBaseUrl(baseUrl: unknown): string | null {
if (typeof baseUrl !== "string" || !baseUrl.trim()) return null;
try {
const url = new URL(baseUrl.trim());
if (url.protocol !== "http:" && url.protocol !== "https:") return null;
url.pathname = url.pathname.replace(/\/+$/, "");
url.search = "";
url.hash = "";
return url.toString().replace(/\/$/, "");
} catch {
return null;
}
}
export function getApiBaseUrl(): string {
const configured =
process.env.SUPERMEMORY_API_URL ||
process.env.SUPERMEMORY_BASE_URL ||
fileConfig.baseUrl ||
loadCredentials()?.apiBaseUrl ||
DEFAULT_BASE_URL;
const normalized = normalizeBaseUrl(configured);
if (!normalized) {
throw new Error("Invalid baseUrl: expected an absolute http(s) URL");
}
return normalized;
}
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),
recallDirective: fileConfig.recallDirective ?? null,
};
export function isConfigured(): boolean {
return !!SUPERMEMORY_API_KEY;
}
/**
* Resolve the reasoned-recall directive override. Reasoned recall is always on
* (a built-in optimization, not a toggle); the only knob is `recallDirective`,
* which overrides the directive text the model is shown each turn. Returns
* `{ directive: null }` to fall back to the built-in default in recall.ts.
*/
export function getRecallConfig(): { directive: string | null } {
return { directive: CONFIG.recallDirective ?? null };
}
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));
}