|
| 1 | +import Foundation |
| 2 | + |
| 3 | +public actor ToolAutoApprovalManager { |
| 4 | + public static let shared = ToolAutoApprovalManager() |
| 5 | + |
| 6 | + public enum AutoApproval: Equatable, Sendable { |
| 7 | + case mcpTool(conversationId: String, serverName: String, toolName: String) |
| 8 | + case mcpServer(conversationId: String, serverName: String) |
| 9 | + case sensitiveFile(conversationId: String, toolName: String, fileKey: String) |
| 10 | + } |
| 11 | + |
| 12 | + private var mcpStorage = MCPApprovalStorage() |
| 13 | + private var sensitiveFileStorage = SensitiveFileApprovalStorage() |
| 14 | + |
| 15 | + public init() {} |
| 16 | + |
| 17 | + public func approve(_ approval: AutoApproval) { |
| 18 | + switch approval { |
| 19 | + case let .mcpTool(conversationId, serverName, toolName): |
| 20 | + allowMCPTool(conversationId: conversationId, serverName: serverName, toolName: toolName) |
| 21 | + case let .mcpServer(conversationId, serverName): |
| 22 | + allowMCPServer(conversationId: conversationId, serverName: serverName) |
| 23 | + case let .sensitiveFile(conversationId, toolName, fileKey): |
| 24 | + allowSensitiveFile(conversationId: conversationId, toolName: toolName, fileKey: fileKey) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // MARK: - MCP approvals |
| 29 | + |
| 30 | + public func allowMCPTool(conversationId: String, serverName: String, toolName: String) { |
| 31 | + mcpStorage.allowTool(scope: .session(conversationId), serverName: serverName, toolName: toolName) |
| 32 | + } |
| 33 | + |
| 34 | + public func allowMCPServer(conversationId: String, serverName: String) { |
| 35 | + mcpStorage.allowServer(scope: .session(conversationId), serverName: serverName) |
| 36 | + } |
| 37 | + |
| 38 | + public func isMCPAllowed( |
| 39 | + conversationId: String, |
| 40 | + serverName: String, |
| 41 | + toolName: String |
| 42 | + ) -> Bool { |
| 43 | + mcpStorage.isAllowed(scope: .session(conversationId), serverName: serverName, toolName: toolName) |
| 44 | + } |
| 45 | + |
| 46 | + // MARK: - Sensitive file approvals |
| 47 | + |
| 48 | + public func allowSensitiveFile(conversationId: String, toolName: String, fileKey: String) { |
| 49 | + sensitiveFileStorage.allowFile(scope: .session(conversationId), toolName: toolName, fileKey: fileKey) |
| 50 | + } |
| 51 | + |
| 52 | + public func isSensitiveFileAllowed( |
| 53 | + conversationId: String, |
| 54 | + toolName: String, |
| 55 | + fileKey: String |
| 56 | + ) -> Bool { |
| 57 | + sensitiveFileStorage.isAllowed(scope: .session(conversationId), toolName: toolName, fileKey: fileKey) |
| 58 | + } |
| 59 | + |
| 60 | + // MARK: - Cleanup |
| 61 | + |
| 62 | + public func clearConversationData(conversationId: String?) { |
| 63 | + guard let conversationId else { return } |
| 64 | + mcpStorage.clear(scope: .session(conversationId)) |
| 65 | + sensitiveFileStorage.clear(scope: .session(conversationId)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
0 commit comments