-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.ts
More file actions
152 lines (138 loc) · 4.12 KB
/
config.ts
File metadata and controls
152 lines (138 loc) · 4.12 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
import { homedir, hostname } from "node:os"
import { isAbsolute, resolve } from "node:path"
export type CloudConfig = {
url: string
api_key: string
}
export type BasicMemoryConfig = {
project: string
bmPath: string
memoryDir: string
memoryFile: string
projectPath: string
autoCapture: boolean
captureMinChars: number
autoRecall: boolean
recallPrompt: string
debug: boolean
cloud?: CloudConfig
}
const ALLOWED_KEYS = [
"project",
"bmPath",
"memoryDir",
"memory_dir",
"memoryFile",
"memory_file",
"projectPath",
"autoCapture",
"captureMinChars",
"capture_min_chars",
"autoRecall",
"auto_recall",
"recallPrompt",
"recall_prompt",
"debug",
"cloud",
]
function assertAllowedKeys(
value: Record<string, unknown>,
allowed: string[],
label: string,
): void {
const unknown = Object.keys(value).filter((k) => !allowed.includes(k))
if (unknown.length > 0) {
throw new Error(`${label} has unknown keys: ${unknown.join(", ")}`)
}
}
function defaultProject(): string {
return `openclaw-${hostname()
.replace(/[^a-zA-Z0-9-]/g, "-")
.toLowerCase()}`
}
function expandUserPath(path: string): string {
if (path === "~") return homedir()
if (path.startsWith("~/")) return `${homedir()}/${path.slice(2)}`
return path
}
export function resolveProjectPath(
projectPath: string,
workspaceDir: string,
): string {
const expanded = expandUserPath(projectPath)
if (isAbsolute(expanded)) return expanded
return resolve(workspaceDir, expanded)
}
export function parseConfig(raw: unknown): BasicMemoryConfig {
const cfg =
raw && typeof raw === "object" && !Array.isArray(raw)
? (raw as Record<string, unknown>)
: {}
if (Object.keys(cfg).length > 0) {
assertAllowedKeys(cfg, ALLOWED_KEYS, "basic-memory config")
}
// Support both camelCase and snake_case for memory_dir / memory_file
const memoryDir =
typeof cfg.memoryDir === "string" && cfg.memoryDir.length > 0
? cfg.memoryDir
: typeof cfg.memory_dir === "string" &&
(cfg.memory_dir as string).length > 0
? (cfg.memory_dir as string)
: "memory/"
const memoryFile =
typeof cfg.memoryFile === "string" && cfg.memoryFile.length > 0
? cfg.memoryFile
: typeof cfg.memory_file === "string" &&
(cfg.memory_file as string).length > 0
? (cfg.memory_file as string)
: "MEMORY.md"
let cloud: CloudConfig | undefined
if (cfg.cloud && typeof cfg.cloud === "object" && !Array.isArray(cfg.cloud)) {
const c = cfg.cloud as Record<string, unknown>
if (typeof c.url === "string" && typeof c.api_key === "string") {
cloud = { url: c.url, api_key: c.api_key }
}
}
return {
project:
typeof cfg.project === "string" && cfg.project.length > 0
? cfg.project
: defaultProject(),
projectPath:
typeof cfg.projectPath === "string" && cfg.projectPath.length > 0
? cfg.projectPath
: ".",
bmPath:
typeof cfg.bmPath === "string" && cfg.bmPath.length > 0
? cfg.bmPath
: "bm",
memoryDir,
memoryFile,
autoCapture: typeof cfg.autoCapture === "boolean" ? cfg.autoCapture : true,
captureMinChars:
typeof cfg.captureMinChars === "number" && cfg.captureMinChars >= 0
? cfg.captureMinChars
: typeof cfg.capture_min_chars === "number" &&
(cfg.capture_min_chars as number) >= 0
? (cfg.capture_min_chars as number)
: 10,
autoRecall:
typeof cfg.autoRecall === "boolean"
? cfg.autoRecall
: typeof cfg.auto_recall === "boolean"
? (cfg.auto_recall as boolean)
: true,
recallPrompt:
typeof cfg.recallPrompt === "string" && cfg.recallPrompt.length > 0
? cfg.recallPrompt
: typeof cfg.recall_prompt === "string" &&
(cfg.recall_prompt as string).length > 0
? (cfg.recall_prompt as string)
: "Check for active tasks and recent activity. Summarize anything relevant to the current session.",
debug: typeof cfg.debug === "boolean" ? cfg.debug : false,
cloud,
}
}
export const basicMemoryConfigSchema = {
parse: parseConfig,
}