Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
UI Polish, command mode smarter agent mods, base work for streaming support(WIP)
altic-devd5e49190d7c3
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swiftindex ad02715e..6b8258ce 100644--- a/Sources/Fluid/ContentView.swift+++ b/Sources/Fluid/ContentView.swift@@ -30,6 +30,11 @@ enum SidebarItem: Hashable {// MARK: - Saved Provider Model// Removed deprecated inline service and model+// MARK: - Streaming Response Structures+private struct StreamingDelta: Codable { let role: String?; let content: String? }+private struct StreamingChoice: Codable { let index: Int?; let delta: StreamingDelta; let finish_reason: String? }+private struct StreamingChunk: Codable { let choices: [StreamingChoice] }+struct ContentView: View {@StateObject private var audioObserver = AudioHardwareObserver()@StateObject private var asr = ASRService()@@ -952,6 +957,34 @@ struct ContentView: View {}.padding(.horizontal, 4)+ // Streaming Toggle (only for OpenAI-compatible APIs, not Apple Intelligence)+ if enableAIProcessing && selectedProviderID != "apple-intelligence" {+ Divider()+ .padding(.vertical, 3)++ HStack(alignment: .top, spacing: 16) {+ VStack(alignment: .leading, spacing: 4) {+ Text("Enable Streaming")+ .font(.system(size: 15, weight: .semibold))+ .foregroundStyle(theme.palette.primaryText)++ Text("Stream responses from OpenAI-compatible APIs for faster initial output")+ .font(.system(size: 13))+ .foregroundStyle(theme.palette.secondaryText)+ }++ Spacer()++ Toggle("", isOn: Binding(+ get: { SettingsStore.shared.enableAIStreaming },+ set: { SettingsStore.shared.enableAIStreaming = $0 }+ ))+ .toggleStyle(.switch)+ .labelsHidden()+ }+ .padding(.horizontal, 4)+ }+// API Key Warning (not for Apple Intelligence - it doesn't need a key)if enableAIProcessing &&selectedProviderID != "apple-intelligence" &&@@ -2285,10 +2318,12 @@ struct ContentView: View {let messages: [ChatMessage]let temperature: Double?let reasoning_effort: String?+ let stream: Bool?}struct ChatChoiceMessage: Codable { let role: String; let content: String }struct ChatChoice: Codable { let index: Int?; let message: ChatChoiceMessage }struct ChatResponse: Codable { let choices: [ChatChoice] }+// Get app context captured at start of recording if availablelet appInfo = recordingAppInfo ?? getCurrentAppInfo()@@ -2299,6 +2334,9 @@ struct ContentView: View {let modelLower = selectedModel.lowercased()let shouldAddReasoningEffort = modelLower.contains("gpt-oss") || modelLower.hasPrefix("openai/")+ // Get streaming setting+ let enableStreaming = SettingsStore.shared.enableAIStreaming+let body = ChatRequest(model: selectedModel,messages: [@@ -2306,12 +2344,18 @@ struct ContentView: View {ChatMessage(role: "user", content: inputText)],temperature: 0.2,- reasoning_effort: shouldAddReasoningEffort ? "low" : nil+ reasoning_effort: shouldAddReasoningEffort ? "low" : nil,+ stream: enableStreaming ? true : nil)guard let jsonData = try? JSONEncoder().encode(body) else {return "Error: Failed to encode request"}++ // Debug: Log request body+ if let bodyStr = String(data: jsonData, encoding: .utf8) {+ DebugLogger.shared.debug("AI Request body: \(bodyStr)", source: "ContentView")+ }var request = URLRequest(url: url)request.httpMethod = "POST"@@ -2325,34 +2369,100 @@ struct ContentView: View {request.httpBody = jsonDatado {- let (data, response) = try await URLSession.shared.data(for: request)-- // Debug: Print raw response- print("=== OLLAMA RESPONSE DEBUG (ContentView) ===")- print("Response URL: \(request.url?.absoluteString ?? "unknown")")- if let http = response as? HTTPURLResponse {- print("HTTP Status: \(http.statusCode)")- }- if let responseText = String(data: data, encoding: .utf8) {- print("Raw Response: \(responseText)")+ if enableStreaming {+ // Streaming mode - parse SSE response+ DebugLogger.shared.info("Using STREAMING mode for AI request", source: "ContentView")+ return try await processStreamingResponse(request: request)+ } else {+ // Non-streaming mode - single JSON response+ DebugLogger.shared.info("Using NON-STREAMING mode for AI request", source: "ContentView")+ let (data, response) = try await URLSession.shared.data(for: request)++ if let http = response as? HTTPURLResponse, http.statusCode >= 400 {+ let errText = String(data: data, encoding: .utf8) ?? "Unknown error"+ return "Error: HTTP \(http.statusCode): \(errText)"+ }+ let decoded = try JSONDecoder().decode(ChatResponse.self, from: data)+ return decoded.choices.first?.message.content ?? "<no content>"}- print("==========================================")-- if let http = response as? HTTPURLResponse, http.statusCode >= 400 {- let errText = String(data: data, encoding: .utf8) ?? "Unknown error"+ } catch {+ DebugLogger.shared.error("AI API error: \(error.localizedDescription)", source: "ContentView")+ return "Error: \(error.localizedDescription)"+ }+ }++ // MARK: - Streaming Response Handler+ private func processStreamingResponse(request: URLRequest) async throws -> String {+ DebugLogger.shared.debug("Starting streaming request to: \(request.url?.absoluteString ?? "unknown")", source: "Streaming")++ let (bytes, response) = try await URLSession.shared.bytes(for: request)++ if let http = response as? HTTPURLResponse {+ DebugLogger.shared.debug("Streaming response status: \(http.statusCode)", source: "Streaming")+ if http.statusCode >= 400 {+ var errorData = Data()+ for try await byte in bytes {+ errorData.append(byte)+ }+ let errText = String(data: errorData, encoding: .utf8) ?? "Unknown error"+ DebugLogger.shared.error("Streaming error: \(errText)", source: "Streaming")return "Error: HTTP \(http.statusCode): \(errText)"}- let decoded = try JSONDecoder().decode(ChatResponse.self, from: data)- return decoded.choices.first?.message.content ?? "<no content>"- } catch {- print("=== OLLAMA DECODE ERROR (ContentView) ===")- print("Error: \(error)")- if let decodingError = error as? DecodingError {- print("Decoding Error Details: \(decodingError)")+ }++ var fullContent = ""+ var currentLine = ""++ // Parse SSE line by line+ for try await byte in bytes {+ let char = Character(UnicodeScalar(byte))++ if char == "\n" {+ // Process the completed line+ let line = currentLine.trimmingCharacters(in: .whitespaces)++ if line.hasPrefix("data:") {+ // Handle both "data: " and "data:" formats+ var jsonString = String(line.dropFirst(5))+ if jsonString.hasPrefix(" ") {+ jsonString = String(jsonString.dropFirst(1))+ }++ // Skip [DONE] marker+ if jsonString.trimmingCharacters(in: .whitespaces) == "[DONE]" {+ DebugLogger.shared.debug("Received [DONE] marker", source: "Streaming")+ currentLine = ""+ continue+ }++ // Parse the JSON chunk+ if let jsonData = jsonString.data(using: .utf8) {+ do {+ let chunk = try JSONDecoder().decode(StreamingChunk.self, from: jsonData)+ if let delta = chunk.choices.first?.delta,+ let content = delta.content {+ fullContent += content+ }+ } catch {+ // Try alternative parsing for different response formats+ if let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any],+ let choices = json["choices"] as? [[String: Any]],+ let firstChoice = choices.first,+ let delta = firstChoice["delta"] as? [String: Any],+ let content = delta["content"] as? String {+ fullContent += content+ }+ }+ }+ }+ currentLine = ""+ } else if char != "\r" {+ currentLine.append(char)}- print("========================================")- return "Error: \(error.localizedDescription)"}++ DebugLogger.shared.debug("Streaming complete. Content length: \(fullContent.count)", source: "Streaming")+ return fullContent.isEmpty ? "<no content>" : fullContent}// MARK: - Stop and Process Transcriptiondiff --git a/Sources/Fluid/Networking/AIProvider.swift b/Sources/Fluid/Networking/AIProvider.swiftindex 2a67f34c..c213843c 100644--- a/Sources/Fluid/Networking/AIProvider.swift+++ b/Sources/Fluid/Networking/AIProvider.swift@@ -2,7 +2,7 @@ import Foundationprotocol AIProvider{- func process(systemPrompt: String, userText: String, model: String, apiKey: String, baseURL: String) async -> String+ func process(systemPrompt: String, userText: String, model: String, apiKey: String, baseURL: String, stream: Bool) async -> String}final class OpenAICompatibleProvider: AIProvider@@ -13,9 +13,10 @@ final class OpenAICompatibleProvider: AIProviderlet messages: [ChatMessage]let temperature: Double?let reasoning_effort: String?+ let stream: Bool?enum CodingKeys: String, CodingKey {- case model, messages, temperature, reasoning_effort+ case model, messages, temperature, reasoning_effort, stream}}struct ChatChoiceMessage: Codable { let role: String; let content: String }@@ -67,7 +68,7 @@ final class OpenAICompatibleProvider: AIProviderreturn modelLower.contains("gpt-oss") || modelLower.hasPrefix("openai/")}- func process(systemPrompt: String, userText: String, model: String, apiKey: String, baseURL: String) async -> String+ func process(systemPrompt: String, userText: String, model: String, apiKey: String, baseURL: String, stream: Bool = false) async -> String{let endpoint = baseURL.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? "https://api.openai.com/v1" : baseURL.trimmingCharacters(in: .whitespacesAndNewlines)@@ -97,7 +98,8 @@ final class OpenAICompatibleProvider: AIProviderChatMessage(role: "user", content: userText)],temperature: 0.2,- reasoning_effort: shouldAddReasoningEffort ? "low" : nil+ reasoning_effort: shouldAddReasoningEffort ? "low" : nil,+ stream: stream ? true : nil)guard let jsonData = try? JSONEncoder().encode(body) else { return "Error: Failed to encode request" }diff --git a/Sources/Fluid/Networking/AppleIntelligenceProvider.swift b/Sources/Fluid/Networking/AppleIntelligenceProvider.swiftindex ed5146f6..bd5b88e3 100644--- a/Sources/Fluid/Networking/AppleIntelligenceProvider.swift+++ b/Sources/Fluid/Networking/AppleIntelligenceProvider.swift@@ -112,3 +112,4 @@ final class AppleIntelligenceProvider {}#endif+diff --git a/Sources/Fluid/Persistence/SettingsStore.swift b/Sources/Fluid/Persistence/SettingsStore.swiftindex 26acebd5..a40e678f 100644--- a/Sources/Fluid/Persistence/SettingsStore.swift+++ b/Sources/Fluid/Persistence/SettingsStore.swift@@ -31,6 +31,7 @@ final class SettingsStore: ObservableObjectstatic let showInDock = "ShowInDock"static let pressAndHoldMode = "PressAndHoldMode"static let enableStreamingPreview = "EnableStreamingPreview"+ static let enableAIStreaming = "EnableAIStreaming"static let copyTranscriptionToClipboard = "CopyTranscriptionToClipboard"static let autoUpdateCheckEnabled = "AutoUpdateCheckEnabled"static let lastUpdateCheckDate = "LastUpdateCheckDate"@@ -196,6 +197,18 @@ final class SettingsStore: ObservableObjectset { defaults.set(newValue, forKey: Keys.enableStreamingPreview) }}+ var enableAIStreaming: Bool+ {+ get {+ let value = defaults.object(forKey: Keys.enableAIStreaming)+ return value as? Bool ?? false // Default to false (disabled)+ }+ set {+ objectWillChange.send()+ defaults.set(newValue, forKey: Keys.enableAIStreaming)+ }+ }+var copyTranscriptionToClipboard: Bool{get { defaults.bool(forKey: Keys.copyTranscriptionToClipboard) }diff --git a/Sources/Fluid/Services/CommandModeService.swift b/Sources/Fluid/Services/CommandModeService.swiftindex 0f9f1766..a81ad0bb 100644--- a/Sources/Fluid/Services/CommandModeService.swift+++ b/Sources/Fluid/Services/CommandModeService.swift@@ -6,10 +6,21 @@ final class CommandModeService: ObservableObject {@Published var conversationHistory: [Message] = []@Published var isProcessing = false@Published var pendingCommand: PendingCommand? = nil+ @Published var currentStep: AgentStep? = nilprivate let terminalService = TerminalService()private var currentTurnCount = 0- private let maxTurns = 15+ private let maxTurns = 20++ // MARK: - Agent Step Tracking++ enum AgentStep: Equatable {+ case thinking(String)+ case checking(String)+ case executing(String)+ case verifying(String)+ case completed(Bool)+ }// MARK: - Models@@ -18,6 +29,8 @@ final class CommandModeService: ObservableObject {let role: Rolelet content: Stringlet toolCall: ToolCall?+ let stepType: StepType+ let timestamp: Dateenum Role: Equatable {case user@@ -25,16 +38,29 @@ final class CommandModeService: ObservableObject {case tool}+ enum StepType: Equatable {+ case normal+ case thinking // AI reasoning+ case checking // Pre-flight verification+ case executing // Running command+ case verifying // Post-action check+ case success // Action completed+ case failure // Action failed+ }+struct ToolCall: Equatable {let id: Stringlet command: Stringlet workingDirectory: String?+ let purpose: String? // Why this command is being run}- init(role: Role, content: String, toolCall: ToolCall? = nil) {+ init(role: Role, content: String, toolCall: ToolCall? = nil, stepType: StepType = .normal) {self.role = roleself.content = contentself.toolCall = toolCall+ self.stepType = stepType+ self.timestamp = Date()}}@@ -42,6 +68,7 @@ final class CommandModeService: ObservableObject {let id: Stringlet command: Stringlet workingDirectory: String?+ let purpose: String?}// MARK: - Public Methods@@ -77,89 +104,207 @@ final class CommandModeService: ObservableObject {pendingCommand = nilconversationHistory.append(Message(role: .assistant,- content: "Command cancelled."+ content: "Command cancelled.",+ stepType: .failure))isProcessing = false+ currentStep = nil}// MARK: - Agent Loopprivate func processNextTurn() async {if currentTurnCount >= maxTurns {- conversationHistory.append(Message(role: .assistant, content: "I've reached the maximum number of steps. stopping here."))+ conversationHistory.append(Message(+ role: .assistant,+ content: "Reached maximum steps limit. Please review the progress and continue if needed.",+ stepType: .failure+ ))isProcessing = false+ currentStep = .completed(false)return}currentTurnCount += 1+ currentStep = .thinking("Analyzing...")do {let response = try await callLLM()- if let toolCall = response.toolCall {+ if let tc = response.toolCall {+ // Determine step type based on command purpose+ let stepType = determineStepType(for: tc.command, purpose: tc.purpose)+ currentStep = stepType == .checking ? .checking(tc.command) : .executing(tc.command)+// AI wants to run a commandconversationHistory.append(Message(role: .assistant,- content: response.content.isEmpty ? "I'll run this command:" : response.content,+ content: response.content.isEmpty ? stepDescription(for: stepType) : response.content,toolCall: Message.ToolCall(- id: toolCall.id,- command: toolCall.command,- workingDirectory: toolCall.workingDirectory- )+ id: tc.id,+ command: tc.command,+ workingDirectory: tc.workingDirectory,+ purpose: tc.purpose+ ),+ stepType: stepType))- // Check if we need confirmation- if SettingsStore.shared.commandModeConfirmBeforeExecute {+ // Check if we need confirmation for destructive commands+ if SettingsStore.shared.commandModeConfirmBeforeExecute && isDestructiveCommand(tc.command) {pendingCommand = PendingCommand(- id: toolCall.id,- command: toolCall.command,- workingDirectory: toolCall.workingDirectory+ id: tc.id,+ command: tc.command,+ workingDirectory: tc.workingDirectory,+ purpose: tc.purpose)isProcessing = false+ currentStep = nilreturn}// Auto-execute- await executeCommand(toolCall.command, workingDirectory: toolCall.workingDirectory, callId: toolCall.id)+ await executeCommand(tc.command, workingDirectory: tc.workingDirectory, callId: tc.id, purpose: tc.purpose)} else {- // Just a text response, we are done- conversationHistory.append(Message(role: .assistant, content: response.content))+ // Just a text response - check if it's a final summary+ let isFinal = response.content.lowercased().contains("complete") ||+ response.content.lowercased().contains("done") ||+ response.content.lowercased().contains("success") ||+ response.content.lowercased().contains("finished")++ conversationHistory.append(Message(+ role: .assistant,+ content: response.content,+ stepType: isFinal ? .success : .normal+ ))isProcessing = false+ currentStep = .completed(isFinal)}} catch {conversationHistory.append(Message(role: .assistant,- content: "Error: \(error.localizedDescription)"+ content: "Error: \(error.localizedDescription)",+ stepType: .failure))isProcessing = false+ currentStep = .completed(false)}}- private func executeCommand(_ command: String, workingDirectory: String?, callId: String) async {+ private func determineStepType(for command: String, purpose: String?) -> Message.StepType {+ let cmd = command.lowercased()+ let purposeLower = purpose?.lowercased() ?? ""++ // Check commands+ if purposeLower.contains("check") || purposeLower.contains("verify") || purposeLower.contains("exist") {+ return .checking+ }+ if cmd.hasPrefix("ls ") || cmd.hasPrefix("cat ") || cmd.hasPrefix("test ") || cmd.hasPrefix("[ ") ||+ cmd.contains("--version") || cmd.contains("which ") || cmd.contains("file ") ||+ cmd.hasPrefix("stat ") || cmd.hasPrefix("head ") || cmd.hasPrefix("tail ") {+ return .checking+ }++ // Verification commands+ if purposeLower.contains("confirm") || purposeLower.contains("result") {+ return .verifying+ }++ return .executing+ }++ private func stepDescription(for stepType: Message.StepType) -> String {+ switch stepType {+ case .checking: return "Checking prerequisites..."+ case .verifying: return "Verifying the result..."+ case .executing: return "Executing command..."+ default: return ""+ }+ }++ private func isDestructiveCommand(_ command: String) -> Bool {+ let destructive = ["rm ", "rm\t", "rmdir", "mv ", "mv\t", "> ", ">> ",+ "chmod ", "chown ", "kill ", "pkill ", "sudo "]+ return destructive.contains { command.lowercased().hasPrefix($0) || command.contains(" \($0)") }+ }++ private func executeCommand(_ command: String, workingDirectory: String?, callId: String, purpose: String? = nil) async {+ currentStep = .executing(command)+let result = await terminalService.execute(command: command,workingDirectory: workingDirectory)- let resultJSON = terminalService.resultToJSON(result)+ // Create enhanced result with context+ let enhancedResult = EnhancedCommandResult(+ result: result,+ purpose: purpose+ )++ let resultJSON = enhancedResult.toJSON()++ // Determine result step type+ let resultStepType: Message.StepType = result.success ? .success : .failure// Add tool result to conversationconversationHistory.append(Message(role: .tool,- content: resultJSON+ content: resultJSON,+ stepType: resultStepType))// Continue the loop - let the AI see the result and decide what to do nextawait processNextTurn()}+ // MARK: - Enhanced Result++ private struct EnhancedCommandResult: Codable {+ let success: Bool+ let command: String+ let output: String+ let error: String?+ let exitCode: Int32+ let executionTimeMs: Int+ let purpose: String?++ init(result: TerminalService.CommandResult, purpose: String?) {+ self.success = result.success+ self.command = result.command+ self.output = result.output+ self.error = result.error+ self.exitCode = result.exitCode+ self.executionTimeMs = result.executionTimeMs+ self.purpose = purpose+ }++ func toJSON() -> String {+ let encoder = JSONEncoder()+ encoder.outputFormatting = [.prettyPrinted, .sortedKeys]+ if let data = try? encoder.encode(self),+ let json = String(data: data, encoding: .utf8) {+ return json+ }+ return """+ {"success": \(success), "output": "\(output)", "exitCode": \(exitCode)}+ """+ }+ }+// MARK: - LLM Integrationprivate struct LLMResponse {let content: String- let toolCall: (id: String, command: String, workingDirectory: String?)?+ let toolCall: ToolCallData?++ struct ToolCallData {+ let id: String+ let command: String+ let workingDirectory: String?+ let purpose: String?+ }}private func callLLM() async throws -> LLMResponse {@@ -178,20 +323,65 @@ final class CommandModeService: ObservableObject {baseURL = "https://api.openai.com/v1"}- // Build conversation+ // Build conversation with agentic system promptlet systemPrompt = """- You are an autonomous macOS terminal agent. Your goal is to complete the user's request by executing commands.-- IMPORTANT RULES:- 1. You can execute shell commands using the 'execute_terminal_command' tool.- 2. If a command fails, ANALYZE the error output and try to FIX it by running a corrected command.- 3. You can run multiple commands in sequence to accomplish a task (e.g., make a directory, then create a file).- 4. Always use full paths when possible.- 5. For destructive operations (rm, overwrite), ask the user for confirmation first (unless they already gave implied permission).- 6. When the task is fully complete, respond with a final text summary.- 7. Keep intermediate text responses concise.-- The user is on macOS with zsh shell.+ You are an autonomous, thoughtful macOS terminal agent. Execute user requests reliably and safely.++ ## AGENTIC WORKFLOW (Follow this pattern):++ ### 1. PRE-FLIGHT CHECKS (Always do this first!)+ Before ANY action, verify prerequisites:+ - File operations: Check if file/folder exists first (`ls`, `test -e`, `[ -f file ]`)+ - Deletions: List contents before removing, confirm target exists+ - Modifications: Read current state before changing+ - Installations: Check if already installed (`which`, `--version`)++ ### 2. EXECUTE WITH CONTEXT+ When calling execute_terminal_command, ALWAYS include a `purpose` parameter explaining:+ - "checking" - Verifying something exists/state+ - "executing" - Performing the main action+ - "verifying" - Confirming the result+ Example purposes: "Checking if image1.png exists", "Creating the backup directory", "Verifying file was deleted"++ ### 3. POST-ACTION VERIFICATION+ After modifying anything, verify it worked:+ - Created file? `ls` to confirm it exists+ - Deleted file? `ls` to confirm it's gone+ - Modified content? `cat` or `head` to verify changes+ - Installed app? Check version/existence++ ### 4. HANDLE FAILURES GRACEFULLY+ - If something doesn't exist: Tell the user clearly+ - If command fails: Analyze error, try alternative approach+ - If permission denied: Explain and suggest solutions+ - Never assume success without verification++ ## RESPONSE FORMAT:+ - Keep reasoning brief and clear+ - State what you're checking/doing before each command+ - After verification, give a clear success/failure summary+ - Use natural language, not code comments++ ## SAFETY RULES:+ - For destructive ops (rm, mv, overwrite): ALWAYS check target exists first+ - Show what will be affected before destroying+ - Prefer `rm -i` or listing contents before bulk deletes+ - Use full absolute paths when possible++ ## EXAMPLES OF GOOD BEHAVIOR:++ User: "Delete image1.png in Downloads"+ You: First check if it exists+ → execute_terminal_command(command: "ls -la ~/Downloads/image1.png", purpose: "Checking if image1.png exists")+ If exists → execute_terminal_command(command: "rm ~/Downloads/image1.png", purpose: "Deleting the file")+ Then verify → execute_terminal_command(command: "ls ~/Downloads/image1.png 2>&1", purpose: "Verifying file was deleted")+ Finally: "✓ Successfully deleted image1.png from Downloads."++ User: "Create a project folder with a readme"+ You: → Check if folder exists, create it, create readme, verify both++ The user is on macOS with zsh shell. Be thorough but efficient.+ When task is complete, provide a clear summary starting with ✓ or ✗."""var messages: [[String: Any]] = [@@ -284,11 +474,17 @@ final class CommandModeService: ObservableObject {let command = args["command"] as? String ?? ""let workDir = args["workingDirectory"] as? String+ let purpose = args["purpose"] as? Stringlet callId = toolCall["id"] as? String ?? "call_\(UUID().uuidString.prefix(8))"return LLMResponse(content: message["content"] as? String ?? "",- toolCall: (id: callId, command: command, workingDirectory: workDir?.isEmpty == true ? nil : workDir)+ toolCall: LLMResponse.ToolCallData(+ id: callId,+ command: command,+ workingDirectory: workDir?.isEmpty == true ? nil : workDir,+ purpose: purpose+ ))}diff --git a/Sources/Fluid/Services/RewriteModeService.swift b/Sources/Fluid/Services/RewriteModeService.swiftindex 179157aa..38756e6f 100644--- a/Sources/Fluid/Services/RewriteModeService.swift+++ b/Sources/Fluid/Services/RewriteModeService.swift@@ -177,12 +177,19 @@ final class RewriteModeService: ObservableObject {apiMessages.append(["role": msg.role == .user ? "user" : "assistant", "content": msg.content])}- let body: [String: Any] = [+ // Check streaming setting+ let enableStreaming = settings.enableAIStreaming++ var body: [String: Any] = ["model": model,"messages": apiMessages,"temperature": 0.7]+ if enableStreaming {+ body["stream"] = true+ }+let endpoint = baseURL.hasSuffix("/chat/completions") ? baseURL : "\(baseURL)/chat/completions"guard let url = URL(string: endpoint) else {throw NSError(domain: "RewriteMode", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"])@@ -194,21 +201,78 @@ final class RewriteModeService: ObservableObject {request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")request.httpBody = try JSONSerialization.data(withJSONObject: body)- let (data, response) = try await URLSession.shared.data(for: request)+ if enableStreaming {+ DebugLogger.shared.info("Using STREAMING mode for Write/Rewrite", source: "RewriteModeService")+ return try await processStreamingResponse(request: request)+ } else {+ let (data, response) = try await URLSession.shared.data(for: request)++ if let http = response as? HTTPURLResponse, http.statusCode >= 400 {+ let err = String(data: data, encoding: .utf8) ?? "Unknown error"+ throw NSError(domain: "RewriteMode", code: http.statusCode, userInfo: [NSLocalizedDescriptionKey: err])+ }++ guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],+ let choices = json["choices"] as? [[String: Any]],+ let choice = choices.first,+ let message = choice["message"] as? [String: Any],+ let content = message["content"] as? String else {+ throw NSError(domain: "RewriteMode", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])+ }++ return content+ }+ }++ // MARK: - Streaming Response Handler+ private func processStreamingResponse(request: URLRequest) async throws -> String {+ let (bytes, response) = try await URLSession.shared.bytes(for: request)if let http = response as? HTTPURLResponse, http.statusCode >= 400 {- let err = String(data: data, encoding: .utf8) ?? "Unknown error"- throw NSError(domain: "RewriteMode", code: http.statusCode, userInfo: [NSLocalizedDescriptionKey: err])+ var errorData = Data()+ for try await byte in bytes {+ errorData.append(byte)+ }+ let errText = String(data: errorData, encoding: .utf8) ?? "Unknown error"+ throw NSError(domain: "RewriteMode", code: http.statusCode, userInfo: [NSLocalizedDescriptionKey: errText])}- guard let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],- let choices = json["choices"] as? [[String: Any]],- let choice = choices.first,- let message = choice["message"] as? [String: Any],- let content = message["content"] as? String else {- throw NSError(domain: "RewriteMode", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid response"])+ var fullContent = ""+ var currentLine = ""++ for try await byte in bytes {+ let char = Character(UnicodeScalar(byte))++ if char == "\n" {+ let line = currentLine.trimmingCharacters(in: .whitespaces)++ if line.hasPrefix("data:") {+ var jsonString = String(line.dropFirst(5))+ if jsonString.hasPrefix(" ") {+ jsonString = String(jsonString.dropFirst(1))+ }++ if jsonString.trimmingCharacters(in: .whitespaces) == "[DONE]" {+ currentLine = ""+ continue+ }++ if let jsonData = jsonString.data(using: .utf8),+ let json = try? JSONSerialization.jsonObject(with: jsonData) as? [String: Any],+ let choices = json["choices"] as? [[String: Any]],+ let firstChoice = choices.first,+ let delta = firstChoice["delta"] as? [String: Any],+ let content = delta["content"] as? String {+ fullContent += content+ }+ }+ currentLine = ""+ } else if char != "\r" {+ currentLine.append(char)+ }}- return content+ DebugLogger.shared.debug("Streaming complete. Content length: \(fullContent.count)", source: "RewriteModeService")+ return fullContent.isEmpty ? "" : fullContent}}diff --git a/Sources/Fluid/Services/TerminalService.swift b/Sources/Fluid/Services/TerminalService.swiftindex 6d3e5f4d..6fa4023e 100644--- a/Sources/Fluid/Services/TerminalService.swift+++ b/Sources/Fluid/Services/TerminalService.swift@@ -24,25 +24,33 @@ final class TerminalService {"function": ["name": "execute_terminal_command","description": """- Execute a terminal/shell command on the user's macOS computer.- Use this to run any command-line operation like: file operations (ls, cat, mkdir, rm),- git commands, brew, npm, python scripts, or any other CLI tool.- Returns JSON with: success (bool), output (stdout), error (stderr if any), exitCode.- IMPORTANT: Only suggest safe, non-destructive commands unless explicitly asked.+ Execute a terminal/shell command on the user's macOS computer.+ Use this for file operations (ls, cat, mkdir, rm), git commands, brew, npm, python, or any CLI tool.++ IMPORTANT: Follow the agentic workflow:+ 1. ALWAYS check prerequisites first (file exists, command available)+ 2. Execute the main action+ 3. Verify the result++ Returns JSON with: success (bool), output (stdout), error (stderr), exitCode, purpose.""","parameters": ["type": "object","properties": ["command": ["type": "string",- "description": "The shell command to execute (e.g., 'ls -la', 'git status', 'cat file.txt')"+ "description": "The shell command to execute (e.g., 'ls -la', 'git status', 'rm file.txt')"],"workingDirectory": ["type": "string",- "description": "Optional working directory path. Defaults to user's home directory if not specified."+ "description": "Optional working directory path. Defaults to user's home directory."+ ],+ "purpose": [+ "type": "string",+ "description": "Brief description of why this command is being run. Must be one of: 'checking' (verifying prerequisites), 'executing' (main action), 'verifying' (confirming result). Example: 'Checking if config.json exists'"]],- "required": ["command"]+ "required": ["command", "purpose"]]]]@@ -146,3 +154,4 @@ final class TerminalService {}+diff --git a/Sources/Fluid/Views/CommandModeView.swift b/Sources/Fluid/Views/CommandModeView.swiftindex 011d569d..cbe68949 100644--- a/Sources/Fluid/Views/CommandModeView.swift+++ b/Sources/Fluid/Views/CommandModeView.swift@@ -147,15 +147,8 @@ struct CommandModeView: View {}if service.isProcessing {- HStack {- ProgressView()- .controlSize(.small)- Text("Processing...")- .font(.caption)- .foregroundStyle(.secondary)- }- .padding(.leading)- .id("processing")+ processingIndicator+ .id("processing")}Color.clear.frame(height: 1).id("bottom")@@ -168,7 +161,38 @@ struct CommandModeView: View {.onChange(of: service.isProcessing) { _ inscrollToBottom(proxy)}+ .onChange(of: service.currentStep) { _ in+ scrollToBottom(proxy)+ }+ }+ }++ // MARK: - Processing Indicator (Minimal with Shimmer)++ private var processingIndicator: some View {+ CommandShimmerText(text: currentStepLabel)+ .padding(.horizontal, 12)+ .padding(.vertical, 8)+ .background(Color(nsColor: .controlBackgroundColor).opacity(0.6))+ .cornerRadius(6)+ }++ private var currentStepLabel: String {+ guard let step = service.currentStep else { return "Working..." }+ switch step {+ case .thinking: return "Thinking..."+ case .checking(let cmd): return "Checking \(truncateCommand(cmd, to: 30))"+ case .executing(let cmd): return "Running \(truncateCommand(cmd, to: 30))"+ case .verifying: return "Verifying..."+ case .completed(let success): return success ? "Done" : "Stopped"+ }+ }++ private func truncateCommand(_ cmd: String, to limit: Int) -> String {+ if cmd.count > limit {+ return String(cmd.prefix(limit - 3)) + "..."}+ return cmd}private func scrollToBottom(_ proxy: ScrollViewProxy) {@@ -182,39 +206,72 @@ struct CommandModeView: View {// MARK: - Pending Commandprivate func pendingCommandView(_ pending: CommandModeService.PendingCommand) -> some View {- VStack(spacing: 8) {+ VStack(spacing: 10) {Divider()HStack {- Image(systemName: "exclamationmark.triangle.fill")+ Image(systemName: "exclamationmark.shield.fill").foregroundStyle(.orange)- Text("Confirm command execution?")- .fontWeight(.medium)+ .font(.title3)+ VStack(alignment: .leading, spacing: 2) {+ Text("Confirm Execution")+ .fontWeight(.semibold)+ if let purpose = pending.purpose {+ Text(purpose)+ .font(.caption)+ .foregroundStyle(.secondary)+ }+ }+ Spacer()}- Text(pending.command)- .font(.system(.body, design: .monospaced))- .padding(8)- .background(Color(nsColor: .textBackgroundColor))- .cornerRadius(6)+ // Command preview+ VStack(alignment: .leading, spacing: 0) {+ HStack {+ Image(systemName: "terminal.fill")+ .font(.caption)+ Text("Command")+ .font(.caption)+ .fontWeight(.medium)+ Spacer()+ }+ .padding(.horizontal, 10)+ .padding(.vertical, 6)+ .background(Color(nsColor: .windowBackgroundColor))++ Divider()++ Text(pending.command)+ .font(.system(.callout, design: .monospaced))+ .textSelection(.enabled)+ .padding(10)+ }+ .background(Color(nsColor: .textBackgroundColor))+ .cornerRadius(8)+ .overlay(+ RoundedRectangle(cornerRadius: 8)+ .stroke(Color.orange.opacity(0.5), lineWidth: 1)+ )HStack(spacing: 12) {- Button("Cancel") {- service.cancelPendingCommand()+ Button(action: { service.cancelPendingCommand() }) {+ Label("Cancel", systemImage: "xmark")}.buttonStyle(.bordered)+ .keyboardShortcut(.escape, modifiers: [])- Button("Run") {- Task {- await service.confirmAndExecute()- }+ Button(action: {+ Task { await service.confirmAndExecute() }+ }) {+ Label("Run Command", systemImage: "play.fill")}.buttonStyle(.borderedProminent)+ .tint(.orange).keyboardShortcut(.return, modifiers: [])}}.padding()- .background(Color.orange.opacity(0.1))+ .background(Color.orange.opacity(0.08))}// MARK: - Input Area@@ -323,7 +380,38 @@ struct CommandModeView: View {}}-// MARK: - Message Bubble+// MARK: - Shimmer Effect (Cursor-style)++struct CommandShimmerText: View {+ let text: String++ @State private var shimmerPhase: CGFloat = 0++ var body: some View {+ Text(text)+ .font(.system(size: 11, weight: .medium))+ .foregroundStyle(+ LinearGradient(+ colors: [+ Color.primary.opacity(0.4),+ Color.primary.opacity(0.4),+ Color.primary.opacity(0.8),+ Color.primary.opacity(0.4),+ Color.primary.opacity(0.4)+ ],+ startPoint: UnitPoint(x: shimmerPhase - 0.3, y: 0.5),+ endPoint: UnitPoint(x: shimmerPhase + 0.3, y: 0.5)+ )+ )+ .onAppear {+ withAnimation(.linear(duration: 1.2).repeatForever(autoreverses: false)) {+ shimmerPhase = 1.3+ }+ }+ }+}++// MARK: - Message Bubble (Minimal Design)struct MessageBubble: View {let message: CommandModeService.Message@@ -332,90 +420,173 @@ struct MessageBubble: View {HStack(alignment: .top) {if message.role == .user {Spacer()- Text(message.content)- .padding(10)- .background(Color.accentColor.opacity(0.2))- .cornerRadius(12)- .frame(maxWidth: 400, alignment: .trailing)+ userMessageView} else {- VStack(alignment: .leading, spacing: 4) {- // Role indicator- HStack(spacing: 4) {- Image(systemName: iconName)- .font(.caption)- Text(roleName)- .font(.caption)- .fontWeight(.bold)- }+ agentMessageView+ Spacer()+ }+ }+ }++ // MARK: - User Message++ private var userMessageView: some View {+ Text(message.content)+ .font(.system(size: 13))+ .padding(.horizontal, 12)+ .padding(.vertical, 8)+ .background(Color.accentColor.opacity(0.15))+ .cornerRadius(10)+ .frame(maxWidth: 380, alignment: .trailing)+ }++ // MARK: - Agent Message++ private var agentMessageView: some View {+ VStack(alignment: .leading, spacing: 6) {+ // Purpose label (minimal, gray)+ if let tc = message.toolCall, let purpose = tc.purpose {+ Text(purpose)+ .font(.system(size: 11)).foregroundStyle(.secondary)-- // Content- if message.role == .tool {- // Show tool output in code block style- ScrollView(.horizontal, showsIndicators: false) {- Text(formatToolOutput(message.content))- .font(.system(.caption, design: .monospaced))- .padding(8)- .background(Color(nsColor: .textBackgroundColor))- .cornerRadius(8)+ }++ // Main content+ if message.role == .tool {+ toolOutputView+ } else if let tc = message.toolCall {+ commandCallView(tc)+ } else if !message.content.isEmpty {+ textContentView+ }+ }+ .frame(maxWidth: 520, alignment: .leading)+ }++ // MARK: - Command Call View (Minimal)++ private func commandCallView(_ tc: CommandModeService.Message.ToolCall) -> some View {+ VStack(alignment: .leading, spacing: 4) {+ // Reasoning text (if meaningful)+ if !message.content.isEmpty &&+ !message.content.lowercased().starts(with: "checking") &&+ !message.content.lowercased().starts(with: "executing") &&+ !message.content.lowercased().starts(with: "i'll") {+ Text(message.content)+ .font(.system(size: 12))+ .foregroundStyle(.secondary)+ }++ // Command block - clean and simple+ Text(tc.command)+ .font(.system(size: 12, design: .monospaced))+ .foregroundStyle(.primary)+ .textSelection(.enabled)+ .padding(.horizontal, 10)+ .padding(.vertical, 8)+ .background(Color(nsColor: .textBackgroundColor))+ .cornerRadius(6)+ }+ }++ // MARK: - Tool Output View (Minimal)++ private var toolOutputView: some View {+ let parsed = parseToolOutput(message.content)++ return VStack(alignment: .leading, spacing: 0) {+ // Minimal header - just status and time+ HStack(spacing: 6) {+ Text(parsed.success ? "Success" : "Error")+ .font(.system(size: 11, weight: .medium))+ .foregroundStyle(parsed.success ? .primary : .secondary)++ Spacer()++ if parsed.executionTime > 0 {+ Text("\(parsed.executionTime)ms")+ .font(.system(size: 10))+ .foregroundStyle(.tertiary)+ }+ }+ .padding(.horizontal, 10)+ .padding(.vertical, 6)++ // Output content (if any)+ if !parsed.output.isEmpty || parsed.error != nil {+ Divider()+ .padding(.horizontal, 10)++ ScrollView(.vertical, showsIndicators: false) {+ VStack(alignment: .leading, spacing: 2) {+ if !parsed.output.isEmpty {+ Text(markdownAttributedString(from: parsed.output))+ .font(.system(size: 11, design: .monospaced))+ .foregroundStyle(.secondary)+ .textSelection(.enabled)}- .frame(maxWidth: 500)- } else {- Text(message.content)- .padding(10)- .background(Color(nsColor: .controlBackgroundColor))- .cornerRadius(12)- .textSelection(.enabled)- }-- // Show command if present- if let tc = message.toolCall {- HStack(spacing: 4) {- Image(systemName: "terminal.fill")- .font(.caption2)- Text(tc.command)- .font(.system(.caption, design: .monospaced))- .lineLimit(1)++ if let error = parsed.error, !error.isEmpty {+ Text(error)+ .font(.system(size: 11, design: .monospaced))+ .foregroundStyle(.secondary)+ .textSelection(.enabled)}- .padding(6)- .background(Color.blue.opacity(0.1))- .cornerRadius(6)- .foregroundStyle(.blue)}+ .padding(.horizontal, 10)+ .padding(.vertical, 6)+ .frame(maxWidth: .infinity, alignment: .leading)}- .frame(maxWidth: 500, alignment: .leading)- Spacer()+ .frame(maxHeight: 120)}}+ .background(Color(nsColor: .textBackgroundColor).opacity(0.5))+ .cornerRadius(6)}- private var iconName: String {- switch message.role {- case .assistant: return "sparkles"- case .tool: return "terminal"- default: return "person"- }+ // MARK: - Text Content View (Minimal)++ private var textContentView: some View {+ Text(markdownAttributedString(from: message.content))+ .font(.system(size: 13))+ .textSelection(.enabled)}- private var roleName: String {- switch message.role {- case .assistant: return "Assistant"- case .tool: return "Output"- default: return "You"+ // MARK: - Markdown Rendering++ private func markdownAttributedString(from text: String) -> AttributedString {+ do {+ let attributed = try AttributedString(markdown: text, options: AttributedString.MarkdownParsingOptions(+ interpretedSyntax: .inlineOnlyPreservingWhitespace+ ))+ return attributed+ } catch {+ return AttributedString(text)}}- private func formatToolOutput(_ json: String) -> String {- // Try to extract just the output field for cleaner display- if let data = json.data(using: .utf8),- let parsed = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {- if let output = parsed["output"] as? String, !output.isEmpty {- return output- }- if let error = parsed["error"] as? String {- return "Error: \(error)"- }+ // MARK: - Helpers++ private struct ParsedOutput {+ let success: Bool+ let output: String+ let error: String?+ let exitCode: Int+ let executionTime: Int+ }++ private func parseToolOutput(_ json: String) -> ParsedOutput {+ guard let data = json.data(using: .utf8),+ let parsed = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {+ return ParsedOutput(success: false, output: json, error: nil, exitCode: -1, executionTime: 0)}- return json++ return ParsedOutput(+ success: parsed["success"] as? Bool ?? false,+ output: parsed["output"] as? String ?? "",+ error: parsed["error"] as? String,+ exitCode: parsed["exitCode"] as? Int ?? 0,+ executionTime: parsed["executionTimeMs"] as? Int ?? 0+ )}}