-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathconfig.ts
More file actions
229 lines (203 loc) · 7.13 KB
/
Copy pathconfig.ts
File metadata and controls
229 lines (203 loc) · 7.13 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Configuration management for the Hindsight OpenCode plugin.
*
* Loading order (later entries win):
* 1. Built-in defaults
* 2. User config file (~/.hindsight/opencode.json)
* 3. Plugin options (from opencode.json plugin tuple)
* 4. Environment variable overrides
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
/** Default API URL used when no override is supplied via env, file, or plugin options. */
export const DEFAULT_HINDSIGHT_API_URL = "https://api.hindsight.vectorize.io";
export interface HindsightConfig {
// Recall
autoRecall: boolean;
recallBudget: string;
recallMaxTokens: number;
recallTypes: string[];
recallContextTurns: number;
recallMaxQueryChars: number;
recallPromptPreamble: string;
recallTags: string[];
recallTagsMatch: "any" | "all" | "any_strict" | "all_strict";
// Retain
autoRetain: boolean;
retainMode: string;
retainEveryNTurns: number;
retainOverlapTurns: number;
retainContext: string;
retainTags: string[];
retainMetadata: Record<string, string>;
// Connection
hindsightApiUrl: string | null;
hindsightApiToken: string | null;
// Bank
bankId: string | null;
bankIdPrefix: string;
dynamicBankId: boolean;
dynamicBankGranularity: string[];
bankMission: string;
retainMission: string | null;
agentName: string;
// Misc
debug: boolean;
}
const DEFAULTS: HindsightConfig = {
// Recall
autoRecall: true,
recallBudget: "mid",
recallMaxTokens: 1024,
recallTypes: ["world", "experience"],
recallContextTurns: 1,
recallMaxQueryChars: 800,
recallTags: [],
recallTagsMatch: "any",
recallPromptPreamble:
"Relevant memories from past conversations (prioritize recent when " +
"conflicting). Only use memories that are directly useful to continue " +
"this conversation; ignore the rest:",
// Retain
autoRetain: true,
retainMode: "full-session",
retainEveryNTurns: 3,
retainOverlapTurns: 2,
retainContext: "opencode",
retainTags: [],
retainMetadata: {},
// Connection
hindsightApiUrl: DEFAULT_HINDSIGHT_API_URL,
hindsightApiToken: null,
// Bank
bankId: null,
bankIdPrefix: "",
dynamicBankId: false,
dynamicBankGranularity: ["agent", "project"],
bankMission: "",
retainMission: null,
agentName: "opencode",
// Misc
debug: false,
};
/** Env var → config key + type mapping */
const ENV_OVERRIDES: Record<string, [keyof HindsightConfig, "string" | "bool" | "int"]> = {
HINDSIGHT_API_URL: ["hindsightApiUrl", "string"],
HINDSIGHT_API_TOKEN: ["hindsightApiToken", "string"],
HINDSIGHT_BANK_ID: ["bankId", "string"],
HINDSIGHT_AGENT_NAME: ["agentName", "string"],
HINDSIGHT_AUTO_RECALL: ["autoRecall", "bool"],
HINDSIGHT_AUTO_RETAIN: ["autoRetain", "bool"],
HINDSIGHT_RETAIN_MODE: ["retainMode", "string"],
HINDSIGHT_RECALL_BUDGET: ["recallBudget", "string"],
HINDSIGHT_RECALL_MAX_TOKENS: ["recallMaxTokens", "int"],
HINDSIGHT_RECALL_MAX_QUERY_CHARS: ["recallMaxQueryChars", "int"],
HINDSIGHT_RECALL_CONTEXT_TURNS: ["recallContextTurns", "int"],
HINDSIGHT_DYNAMIC_BANK_ID: ["dynamicBankId", "bool"],
HINDSIGHT_BANK_MISSION: ["bankMission", "string"],
HINDSIGHT_BANK_ID_PREFIX: ["bankIdPrefix", "string"],
HINDSIGHT_RETAIN_EVERY_N_TURNS: ["retainEveryNTurns", "int"],
HINDSIGHT_RETAIN_OVERLAP_TURNS: ["retainOverlapTurns", "int"],
HINDSIGHT_RECALL_TAGS: ["recallTags", "string"],
HINDSIGHT_RETAIN_TAGS: ["retainTags", "string"],
HINDSIGHT_RECALL_TAGS_MATCH: ["recallTagsMatch", "string"],
HINDSIGHT_RECALL_PROMPT_PREAMBLE: ["recallPromptPreamble", "string"],
HINDSIGHT_RETAIN_CONTEXT: ["retainContext", "string"],
// NOTE: `debug` is intentionally NOT an env override. It is a proper config
// option set via opencode.json plugin options or ~/.hindsight/opencode.json,
// because env vars are unreliable to set for OpenCode's plugin runtime
// (notably on Windows).
};
function castEnv(value: string, typ: "string" | "bool" | "int"): string | boolean | number | null {
if (typ === "bool") return ["true", "1", "yes"].includes(value.toLowerCase());
if (typ === "int") {
const n = parseInt(value, 10);
return isNaN(n) ? null : n;
}
return value;
}
function loadSettingsFile(path: string): Record<string, unknown> {
try {
const raw = readFileSync(path, "utf-8");
return JSON.parse(raw);
} catch {
return {};
}
}
export function loadConfig(pluginOptions?: Record<string, unknown>): HindsightConfig {
// 1. Start with defaults
const config: Record<string, unknown> = { ...DEFAULTS };
// 2. User config file (~/.hindsight/opencode.json)
const userConfigPath = join(homedir(), ".hindsight", "opencode.json");
const fileConfig = loadSettingsFile(userConfigPath);
for (const [key, value] of Object.entries(fileConfig)) {
if (value !== null && value !== undefined) {
config[key] = value;
}
}
// 3. Plugin options (from opencode.json: ["@vectorize-io/opencode-hindsight", { ... }])
if (pluginOptions) {
for (const [key, value] of Object.entries(pluginOptions)) {
if (value !== null && value !== undefined) {
config[key] = value;
}
}
}
// 4. Environment variable overrides (highest priority)
for (const [envName, [key, typ]] of Object.entries(ENV_OVERRIDES)) {
const val = process.env[envName];
if (val !== undefined) {
const castVal = castEnv(val, typ);
if (castVal !== null) {
config[key] = castVal;
}
}
}
// Array env vars (comma-separated)
const recallTagsEnv = process.env["HINDSIGHT_RECALL_TAGS"];
if (recallTagsEnv !== undefined) {
config["recallTags"] = recallTagsEnv
.split(",")
.map((t) => t.trim())
.filter(Boolean);
}
const recallTagsMatchEnv = process.env["HINDSIGHT_RECALL_TAGS_MATCH"];
if (recallTagsMatchEnv !== undefined) {
config["recallTagsMatch"] = recallTagsMatchEnv;
}
const retainTagsEnv = process.env["HINDSIGHT_RETAIN_TAGS"];
if (retainTagsEnv !== undefined) {
config["retainTags"] = retainTagsEnv
.split(",")
.map((t) => t.trim())
.filter(Boolean);
}
const result = config as unknown as HindsightConfig;
// Validate enum-like fields to catch typos early
const VALID_RETAIN_MODES = ["full-session", "last-turn"];
if (!VALID_RETAIN_MODES.includes(result.retainMode)) {
console.error(
`[Hindsight] Unknown retainMode "${result.retainMode}" — ` +
`valid: ${VALID_RETAIN_MODES.join(", ")}. Falling back to "full-session".`
);
result.retainMode = "full-session";
}
const VALID_TAGS_MATCH = ["any", "all", "any_strict", "all_strict"];
if (!VALID_TAGS_MATCH.includes(result.recallTagsMatch)) {
console.error(
`[Hindsight] Unknown recallTagsMatch "${result.recallTagsMatch}" — ` +
`valid: ${VALID_TAGS_MATCH.join(", ")}. Falling back to "any".`
);
result.recallTagsMatch = "any";
}
const VALID_BUDGETS = ["low", "mid", "high"];
if (!VALID_BUDGETS.includes(result.recallBudget)) {
console.error(
`[Hindsight] Unknown recallBudget "${result.recallBudget}" — ` +
`valid: ${VALID_BUDGETS.join(", ")}. Falling back to "mid".`
);
result.recallBudget = "mid";
}
return result;
}