forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryExportConnectionDetector.swift
More file actions
244 lines (220 loc) · 7.63 KB
/
Copy pathMemoryExportConnectionDetector.swift
File metadata and controls
244 lines (220 loc) · 7.63 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import Foundation
enum MemoryExportConnectionDetector {
nonisolated(unsafe) static var homeOverrideForTesting: URL?
private static var home: URL {
homeOverrideForTesting ?? FileManager.default.homeDirectoryForCurrentUser
}
private enum ConfigFile: CaseIterable, Hashable {
case codex
case claudeDesktop
case claudeCodeGlobal
case claudeCodeSettings
case openclaw
case hermes
var url: URL {
let home = MemoryExportConnectionDetector.home
switch self {
case .codex:
return home.appendingPathComponent(".codex/config.toml")
case .claudeDesktop:
return home.appendingPathComponent("Library/Application Support/Claude/claude_desktop_config.json")
case .claudeCodeGlobal:
return home.appendingPathComponent(".claude.json")
case .claudeCodeSettings:
return home.appendingPathComponent(".claude/settings.json")
case .openclaw:
return home.appendingPathComponent(".openclaw/openclaw.json")
case .hermes:
return home.appendingPathComponent(".hermes/config.yaml")
}
}
var destinations: Set<MemoryExportDestination> {
switch self {
case .codex: return [.codex]
case .claudeDesktop: return [.claude]
case .claudeCodeGlobal, .claudeCodeSettings: return [.claudeCode]
case .openclaw: return [.openclaw]
case .hermes: return [.hermes]
}
}
}
static func hasExistingConnection(
for destination: MemoryExportDestination,
matchingKey key: String?
) -> Bool {
scanLocalMCPConnections(for: destination, matchingKey: key).contains(destination)
}
static func scanLocalMCPConnections(matchingKey key: String?) -> Set<MemoryExportDestination> {
guard let key = normalizedKey(key) else { return [] }
return scan(ConfigFile.allCases, matchingKey: key)
}
static func scanLocalMCPConnections(
for destination: MemoryExportDestination,
matchingKey key: String?
) -> Set<MemoryExportDestination> {
guard let key = normalizedKey(key) else { return [] }
let files = ConfigFile.allCases.filter { $0.destinations.contains(destination) }
return scan(files, matchingKey: key)
}
private static func scan(
_ files: [ConfigFile],
matchingKey key: String
) -> Set<MemoryExportDestination> {
files.reduce(into: Set<MemoryExportDestination>()) { result, file in
result.formUnion(parse(file, matchingKey: key))
}
}
private static func parse(
_ file: ConfigFile,
matchingKey key: String
) -> Set<MemoryExportDestination> {
switch file {
case .codex:
return codexConfigHasOmiMCP(file.url, matchingKey: key) ? [.codex] : []
case .claudeDesktop:
return jsonConfigHasOmiMCP(
file.url,
serverPath: ["mcpServers", "omi-memory"],
matchingKey: key
) ? [.claude] : []
case .claudeCodeGlobal, .claudeCodeSettings:
return jsonConfigHasOmiMCP(
file.url,
serverPath: ["mcpServers", "omi-memory"],
matchingKey: key
) ? [.claudeCode] : []
case .openclaw:
return jsonConfigHasOmiMCP(
file.url,
serverPath: ["mcp", "servers", "omi-memory"],
matchingKey: key
) ? [.openclaw] : []
case .hermes:
return hermesConfigHasOmiMCP(file.url, matchingKey: key) ? [.hermes] : []
}
}
private static func codexConfigHasOmiMCP(_ url: URL, matchingKey key: String) -> Bool {
guard let content = try? String(contentsOf: url, encoding: .utf8) else { return false }
var inOmiServer = false
var body = ""
for rawLine in content.components(separatedBy: .newlines) {
let line = stripInlineComment(rawLine, comment: "#").trimmingCharacters(in: .whitespaces)
guard !line.isEmpty else { continue }
if line.hasPrefix("[") && line.hasSuffix("]") {
inOmiServer = line == "[mcp_servers.omi-memory]"
continue
}
if inOmiServer {
body += "\n" + line
}
}
return bodyContainsCurrentOmiMCP(body, matchingKey: key)
}
private static func hermesConfigHasOmiMCP(_ url: URL, matchingKey key: String) -> Bool {
guard let content = try? String(contentsOf: url, encoding: .utf8) else { return false }
var inMCPServers = false
var inOmiServer = false
var body = ""
for rawLine in content.components(separatedBy: .newlines) {
let line = stripInlineComment(rawLine, comment: "#")
let trimmed = line.trimmingCharacters(in: .whitespaces)
guard !trimmed.isEmpty else { continue }
if !line.hasPrefix(" "), !line.hasPrefix("\t") {
inMCPServers = trimmed == "mcp_servers:"
inOmiServer = false
continue
}
guard inMCPServers else { continue }
if line.hasPrefix(" "), !line.hasPrefix(" "), trimmed.hasSuffix(":") {
inOmiServer = trimmed == "omi-memory:"
continue
}
if inOmiServer {
body += "\n" + trimmed
}
}
return bodyContainsCurrentOmiMCP(body, matchingKey: key)
}
private static func jsonConfigHasOmiMCP(
_ url: URL,
serverPath: [String],
matchingKey key: String
) -> Bool {
guard
let data = try? Data(contentsOf: url),
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let server = value(in: json, at: serverPath) as? [String: Any],
server["disabled"] as? Bool != true,
server["enabled"] as? Bool != false
else {
return false
}
return bodyContainsCurrentOmiMCP(stringValues(in: server).joined(separator: "\n"), matchingKey: key)
}
private static func value(in dictionary: [String: Any], at path: [String]) -> Any? {
var current: Any? = dictionary
for key in path {
current = (current as? [String: Any])?[key]
}
return current
}
private static func stringValues(in value: Any) -> [String] {
if let string = value as? String {
return [string]
}
if let array = value as? [Any] {
return array.flatMap(stringValues)
}
if let dictionary = value as? [String: Any] {
return dictionary.values.flatMap(stringValues)
}
return []
}
private static func bodyContainsCurrentOmiMCP(_ body: String, matchingKey key: String) -> Bool {
body.range(of: MemoryExportDestination.mcpServerURL, options: [.caseInsensitive]) != nil
&& bearerTokens(in: body).contains(key)
}
private static func bearerTokens(in body: String) -> Set<String> {
guard
let regex = try? NSRegularExpression(
pattern: #"(?i)\bBearer\s+([^\s"',}\]]+)"#,
options: []
)
else {
return []
}
let nsRange = NSRange(body.startIndex..<body.endIndex, in: body)
return Set(
regex.matches(in: body, options: [], range: nsRange).compactMap { match in
guard
match.numberOfRanges > 1,
let range = Range(match.range(at: 1), in: body)
else {
return nil
}
return String(body[range])
})
}
private static func normalizedKey(_ key: String?) -> String? {
let trimmed = (key ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}
private static func stripInlineComment(_ line: String, comment: Character) -> String {
var result = ""
var isInSingleQuote = false
var isInDoubleQuote = false
var previous: Character?
for character in line {
if character == "'", !isInDoubleQuote {
isInSingleQuote.toggle()
} else if character == "\"", !isInSingleQuote, previous != "\\" {
isInDoubleQuote.toggle()
} else if character == comment, !isInSingleQuote, !isInDoubleQuote {
break
}
result.append(character)
previous = character
}
return result
}
}