-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathToolAutoApprovalManager.swift
More file actions
194 lines (159 loc) · 7.43 KB
/
Copy pathToolAutoApprovalManager.swift
File metadata and controls
194 lines (159 loc) · 7.43 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
import Foundation
public actor ToolAutoApprovalManager {
public static let shared = ToolAutoApprovalManager()
public enum AutoApproval: Equatable, Sendable {
case mcpTool(scope: AutoApprovalScope, serverName: String, toolName: String)
case mcpServer(scope: AutoApprovalScope, serverName: String)
case fetchWebPage(conversationId: ConversationID, urls: [String])
case sensitiveFile(
scope: AutoApprovalScope,
toolName: String,
description: String,
pattern: String?
)
case terminal(scope: AutoApprovalScope, commands: [String])
}
private var mcpStorage = MCPApprovalStorage()
private var sensitiveFileStorage = SensitiveFileApprovalStorage()
private var terminalStorage = TerminalApprovalStorage()
private var fetchWebPageStorage = FetchWebPageApprovalStorage()
public init() {}
public func approve(_ approval: AutoApproval) {
switch approval {
case let .mcpTool(scope, serverName, toolName):
switch scope {
case .session(let conversationId):
allowMCPTool(conversationId: conversationId, serverName: serverName, toolName: toolName)
case .global:
allowMCPToolGlobally(serverName: serverName, toolName: toolName)
}
case let .mcpServer(scope, serverName):
switch scope {
case .session(let conversationId):
allowMCPServer(conversationId: conversationId, serverName: serverName)
case .global:
allowMCPServerGlobally(serverName: serverName)
}
case let .fetchWebPage(conversationId, urls):
allowFetchWebPage(conversationId: conversationId, urls: urls)
case let .sensitiveFile(scope, toolName, description, pattern):
switch scope {
case .session(let conversationId):
let key = resolveFileKey(description: description, pattern: pattern)
allowSensitiveFile(conversationId: conversationId, toolName: toolName, fileKey: key)
case .global:
guard let pattern, !pattern.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
// Global approvals require an explicit pattern.
return
}
allowSensitiveRuleGlobally(description: description, pattern: pattern)
}
case let .terminal(scope, commands):
switch scope {
case .global:
allowTerminalCommandGlobally(commands: commands)
case .session(let conversationId):
if commands.isEmpty {
allowTerminalAllCommandsInSession(conversationId: conversationId)
} else {
allowTerminalCommandsInSession(conversationId: conversationId, commands: commands)
}
}
}
}
// MARK: - Fetch webpage approvals
public func allowFetchWebPage(conversationId: ConversationID, urls: [String]) {
fetchWebPageStorage.allowURLs(conversationId: conversationId, urls: urls)
}
public func isFetchWebPageAllowed(conversationId: ConversationID, urls: [String]) -> Bool {
fetchWebPageStorage.areAllowed(conversationId: conversationId, urls: urls)
}
// MARK: - MCP approvals
public func allowMCPTool(conversationId: String, serverName: String, toolName: String) {
mcpStorage.allowTool(scope: .session(conversationId), serverName: serverName, toolName: toolName)
}
public func allowMCPServer(conversationId: String, serverName: String) {
mcpStorage.allowServer(scope: .session(conversationId), serverName: serverName)
}
public func isMCPAllowed(
conversationId: String,
serverName: String,
toolName: String
) -> Bool {
mcpStorage.isAllowed(scope: .session(conversationId), serverName: serverName, toolName: toolName)
}
// MARK: - Global MCP approvals
public func allowMCPToolGlobally(serverName: String, toolName: String) {
mcpStorage.allowTool(scope: .global, serverName: serverName, toolName: toolName)
}
public func allowMCPServerGlobally(serverName: String) {
mcpStorage.allowServer(scope: .global, serverName: serverName)
}
public func isMCPAllowedGlobally(serverName: String, toolName: String) -> Bool {
mcpStorage.isAllowed(scope: .global, serverName: serverName, toolName: toolName)
}
// MARK: - Sensitive file approvals
public func allowSensitiveFile(conversationId: String, toolName: String, fileKey: String) {
sensitiveFileStorage.allowFile(scope: .session(conversationId), toolName: toolName, fileKey: fileKey)
}
public func isSensitiveFileAllowed(
conversationId: String,
toolName: String,
fileKey: String
) -> Bool {
sensitiveFileStorage.isAllowed(scope: .session(conversationId), toolName: toolName, fileKey: fileKey)
}
// MARK: - Global Sensitive file approvals
public func allowSensitiveRuleGlobally(description: String, pattern: String) {
// toolName is intentionally ignored for global sensitive-file approvals.
sensitiveFileStorage.allowFile(
scope: .global,
description: description,
pattern: pattern
)
}
// MARK: - Global terminal approvals
/// Stores global auto-approvals for one or more terminal command lines.
public func allowTerminalCommandGlobally(commands: [String]) {
terminalStorage.allowCommands(scope: .global, commands: commands)
}
/// Stores session-scoped auto-approvals.
///
/// Heuristic:
/// - entries containing whitespace are treated as exact command lines
/// - otherwise treated as command names (matching `cmd ...`)
public func allowTerminalCommandsInSession(conversationId: String, commands: [String]) {
terminalStorage.allowCommands(scope: .session(conversationId), commands: commands)
}
public func allowTerminalAllCommandsInSession(conversationId: String) {
terminalStorage.allowAllCommands(scope: .session(conversationId))
}
public func isTerminalAllowed(conversationId: String, commandLine: String?) -> Bool {
guard let commandLine, !commandLine.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
return terminalStorage.isAllCommandsAllowedInSession(conversationId: conversationId)
}
return terminalStorage.isAllowed(scope: .session(conversationId), commandLine: commandLine)
}
private func resolveFileKey(description: String, pattern: String?) -> String {
if let pattern, !pattern.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return pattern
}
return SensitiveFileConfirmationInfo(
description: description,
pattern: pattern
).sessionKey
}
// MARK: - Cleanup
public func clearConversationData(conversationId: String?) {
guard let conversationId else { return }
mcpStorage.clear(scope: .session(conversationId))
sensitiveFileStorage.clear(scope: .session(conversationId))
terminalStorage.clear(scope: .session(conversationId))
fetchWebPageStorage.clear(conversationId: conversationId)
}
public func clearGlobalData() {
mcpStorage.clear(scope: .global)
sensitiveFileStorage.clear(scope: .global)
terminalStorage.clear(scope: .global)
}
}