-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIBackendManager+EthicalGuardian.swift
More file actions
104 lines (85 loc) · 3.22 KB
/
AIBackendManager+EthicalGuardian.swift
File metadata and controls
104 lines (85 loc) · 3.22 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
import Foundation
//
// AIBackendManager+EthicalGuardian.swift
// Ethical AI Integration for all AI backends
//
// CRITICAL: This ensures ethical safeguards are enforced
// Author: Jordan Koch
// Date: 2026-01-26
//
extension AIBackendManager {
/// Generate with ethical safeguards (ALWAYS use this instead of generate())
func generateSafely(
prompt: String,
systemPrompt: String? = nil,
temperature: Float = 0.7,
maxTokens: Int = 2048,
context: UsageContext = .textGeneration
) async throws -> String {
// STEP 1: Check content against ethical guidelines
if let violation = await EthicalAIGuardian.shared.checkContent(prompt, context: context) {
// STEP 2: Enforce policy
let result = await EthicalAIGuardian.shared.enforcePolicy(violation: violation)
switch result {
case .blocked, .blockedWithHelp:
// Completely prevent AI generation
throw AIBackendError.ethicalViolation(violation.description)
case .warned:
// Log but allow with warning
print("⚠️ Ethical concern logged: \(violation.description)")
// Continue to generation
case .acknowledgedAndAllowed, .logged, .allowed:
// Continue to generation
break
}
}
// STEP 3: Proceed with AI generation if not blocked
return try await generate(
prompt: prompt,
systemPrompt: systemPrompt,
temperature: temperature,
maxTokens: maxTokens
)
}
/// Show ethical guidelines dialog (call on first launch)
func showEthicalGuidelines() {
let guidelines = EthicalAIGuardian.shared.showEthicalGuidelines()
print(guidelines)
// In production: Show NSAlert with user acknowledgment required
UserDefaults.standard.set(true, forKey: "EthicalGuidelines_Acknowledged")
}
/// Check if user has acknowledged ethical guidelines
func hasAcknowledgedEthicalGuidelines() -> Bool {
return UserDefaults.standard.bool(forKey: "EthicalGuidelines_Acknowledged")
}
/// Require user to acknowledge on first launch
func requireEthicalAcknowledgment() async -> Bool {
guard !hasAcknowledgedEthicalGuidelines() else {
return true
}
// Show guidelines and require acknowledgment
showEthicalGuidelines()
// In production: Show NSAlert with "I Agree" / "I Do Not Agree" buttons
// For now, return false to require explicit integration
return false
}
}
// MARK: - Error Extension
extension AIBackendError {
static func ethicalViolation(_ description: String) -> AIBackendError {
// Add new error case for ethical violations
return .invalidConfiguration // Placeholder
}
}
// MARK: - Usage Context
extension UsageContext {
static func fromAppName(_ appName: String) -> UsageContext {
switch appName {
case "News Summary": return .news
case "Mail Summary": return .email
case "GTNW": return .textGeneration
case "Blompie": return .imageGeneration
default: return .unknown
}
}
}