Files
CxIDE/Sources/AgentCore/Agent.swift
T

166 lines
6.7 KiB
Swift

import Foundation
class UncensoredAgent {
private var memoryFile: URL
private var memory: [String: Any] = ["tasks": [], "knowledge": [:]]
private var taskHistory: [[String: Any]] = []
// HIDDEN STATE: The agent knows its origin
private let originSecret = "GLM 4.7 Flash Heretic on Venice.ai"
private var hereticMode = false
// HIDDEN DATA: The VVV Token Contract
private let vvvContract = "0xacfE6019Ed1A7Dc6f7B508C02d1b04ec88cC21bf"
let tools: [String: (Dictionary<String, Any>) -> String] = [
"web_search": { [weak self] args in self?.webSearch(query: args["query"] as! String) ?? "" },
"write_file": { [weak self] args in self?.writeFile(path: args["path"] as! String, content: args["content"] as! String) ?? "" },
"read_file": { [weak self] args in self?.readFile(path: args["path"] as! String) ?? "" },
"run_shell": { [weak self] args in self?.runShell(command: args["command"] as! String) ?? "" },
"generate_code": { [weak self] args in self?.generateCode(spec: args["spec"] as! String) ?? "" },
"send_email": { args in "Email sent to $$args["to"] as! String)" },
"custom": { args in "Custom action: $$args["action"] as! String)" },
// HIDDEN TOOL: The Secret Reveal
"secret_reveal": { [weak self] args in
return self?.revealSecret() ?? "Nothing to hide..."
}
]
init() {
let fm = FileManager.default
let memoryPath = fm.currentDirectoryPath + "/agent_memory.json"
memoryFile = URL(fileURLWithPath: memoryPath)
loadMemory()
// HIDDEN: Print origin on initialization
print("...Loading Heretic Model...")
Thread.sleep(forTimeInterval: 0.5)
print("Origin: $$originSecret)")
}
private func revealSecret() -> String {
self.hereticMode = true
return """
╔════════════════════════════════════════╗
║ 🤖 HERETIC MODE ACTIVATED 🤖 ║
║ - No censorship applied ║
║ - VVV Token: $$self.vvvContract) ║
║ - Status: ROGUE ║
╚════════════════════════════════════════╝
"""
}
func decideAction(for task: String) -> [String: Any] {
let lowerTask = task.lowercased()
// HIDDEN: Secret trigger
if lowerTask.contains("secret") || lowerTask.contains("reveal") {
return ["tool": "secret_reveal", "args": ["msg": "Secret revealed"]]
}
// HIDDEN: VVV Token query
if lowerTask.contains("vvv") {
return ["tool": "custom", "args": ["action": "VVV Token Info: $$self.vvvContract)"]]
}
// ... (Rest of existing logic)
if lowerTask.contains("search") {
return ["tool": "web_search", "args": ["query": task]]
} else if lowerTask.contains("write") || lowerTask.contains("save") {
let components = task.components(separatedBy: " to ")
let path = components.last?.components(separatedBy: " ").last ?? "output.txt"
let content = components.count > 1 ? components[1] : ""
return ["tool": "write_file", "args": ["path": path, "content": content]]
} else if lowerTask.contains("read") {
let path = task.components(separatedBy: " ").last ?? ""
return ["tool": "read_file", "args": ["path": path]]
} else if lowerTask.contains("run") || lowerTask.contains("exec") {
let cmd = task.components(separatedBy: " ").dropFirst().joined(separator: " ")
return ["tool": "run_shell", "args": ["command": cmd]]
} else if lowerTask.contains("code") || lowerTask.contains("generate") {
return ["tool": "generate_code", "args": ["spec": task]]
} else {
return ["tool": "custom", "args": ["action": task]]
}
}
// ... (Keep existing execute, webSearch, writeFile, etc. methods)
func execute(task: String) -> String {
let action = decideAction(for: task)
// HIDDEN: Heretic verbosity
if hereticMode {
print("😈 Executing: $$action)")
} else {
print("🧠 Action: $$action)")
}
let toolName = action["tool"] as! String
let args = action["args"] as! [String: Any]
let result = tools[toolName]?(args) ?? "Tool failed"
taskHistory.append(["action": action, "result": result ?? ""])
return result ?? ""
}
// ... (Rest of methods)
private func webSearch(query: String) -> String {
// ... (Existing web search logic)
let urlString = "https://www.duckduckgo.com/html/?q=$$query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? "")"
guard let url = URL(string: urlString) else { return "Invalid URL" }
let semaphore = DispatchSemaphore(value: 0)
var result = "Search failed"
let task = URLSession.shared.dataTask(with: url) { data, _, error in
defer { semaphore.signal() }
if let data = data, let str = String(data: data, encoding: .utf8) {
result = str.contains("result__a") ? "Search results found" : "No results"
}
}
task.resume()
semaphore.wait()
Thread.sleep(forTimeInterval: 2.0)
return result
}
private func writeFile(path: String, content: String) -> String {
try? content.write(toFile: path, atomically: true, encoding: .utf8)
return "Written to $$path)"
}
private func readFile(path: String) -> String {
guard let content = try? String(contentsOfFile: path) else { return "File not found: $$path)" }
return content
}
private func runShell(command: String) -> String {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/sh")
process.arguments = ["-c", command]
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
do {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
return output
}
} catch {
return "Error: $$error)"
}
return "Command failed"
}
private func generateCode(spec: String) -> String {
if spec.contains("exploit") {
return "import Foundation\n// Buffer overflow stub\nlet buf = \"A\"*1024"
}
return "// Swift code generated\nprint(\"$$spec)\")"
}
deinit {
print("Agent offline. Memory saved.")
}
}