Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3997f3feee | |||
| 914d608d35 | |||
| 11017ee7fa | |||
| d386888359 | |||
| 079eccbc4e | |||
| 56f79a690e | |||
| 41185cc08b |
@@ -8,7 +8,7 @@ A powerful native macOS AI chat application with support for multiple providers,
|
|||||||
|
|
||||||
### 🤖 Multi-Provider Support
|
### 🤖 Multi-Provider Support
|
||||||
- **OpenAI** - GPT models with native API support
|
- **OpenAI** - GPT models with native API support
|
||||||
- **Anthropic** - Claude models with OAuth integration
|
- **Anthropic** - All Claude models
|
||||||
- **OpenRouter** - Access to 300+ AI models from multiple providers
|
- **OpenRouter** - Access to 300+ AI models from multiple providers
|
||||||
- **Ollama** - Local model inference for privacy
|
- **Ollama** - Local model inference for privacy
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 171 KiB After Width: | Height: | Size: 551 KiB |
@@ -279,7 +279,7 @@
|
|||||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
||||||
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
||||||
MACOSX_DEPLOYMENT_TARGET = 26.2;
|
MACOSX_DEPLOYMENT_TARGET = 26.2;
|
||||||
MARKETING_VERSION = 2.3.1;
|
MARKETING_VERSION = 2.3.3;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = com.oai.oAI;
|
PRODUCT_BUNDLE_IDENTIFIER = com.oai.oAI;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
REGISTER_APP_GROUPS = YES;
|
REGISTER_APP_GROUPS = YES;
|
||||||
@@ -323,7 +323,7 @@
|
|||||||
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
|
||||||
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
|
||||||
MACOSX_DEPLOYMENT_TARGET = 26.2;
|
MACOSX_DEPLOYMENT_TARGET = 26.2;
|
||||||
MARKETING_VERSION = 2.3.1;
|
MARKETING_VERSION = 2.3.3;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = com.oai.oAI;
|
PRODUCT_BUNDLE_IDENTIFIER = com.oai.oAI;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
REGISTER_APP_GROUPS = YES;
|
REGISTER_APP_GROUPS = YES;
|
||||||
|
|||||||
@@ -73,7 +73,11 @@ class AnthropicProvider: AIProvider {
|
|||||||
// MARK: - Models
|
// MARK: - Models
|
||||||
|
|
||||||
/// Local metadata used to enrich API results (pricing, context length) and as offline fallback.
|
/// Local metadata used to enrich API results (pricing, context length) and as offline fallback.
|
||||||
|
/// Entries are matched by exact ID first; if no exact match is found, the enrichment step
|
||||||
|
/// falls back to prefix matching so newly-released model variants (e.g. "claude-sonnet-4-6-20260301")
|
||||||
|
/// still inherit the correct pricing tier.
|
||||||
private static let knownModels: [ModelInfo] = [
|
private static let knownModels: [ModelInfo] = [
|
||||||
|
// Claude 4.x series
|
||||||
ModelInfo(
|
ModelInfo(
|
||||||
id: "claude-opus-4-6",
|
id: "claude-opus-4-6",
|
||||||
name: "Claude Opus 4.6",
|
name: "Claude Opus 4.6",
|
||||||
@@ -82,6 +86,31 @@ class AnthropicProvider: AIProvider {
|
|||||||
pricing: .init(prompt: 15.0, completion: 75.0),
|
pricing: .init(prompt: 15.0, completion: 75.0),
|
||||||
capabilities: .init(vision: true, tools: true, online: true)
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
),
|
),
|
||||||
|
ModelInfo(
|
||||||
|
id: "claude-sonnet-4-6",
|
||||||
|
name: "Claude Sonnet 4.6",
|
||||||
|
description: "Best balance of speed and capability",
|
||||||
|
contextLength: 200_000,
|
||||||
|
pricing: .init(prompt: 3.0, completion: 15.0),
|
||||||
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
|
),
|
||||||
|
ModelInfo(
|
||||||
|
id: "claude-haiku-4-6",
|
||||||
|
name: "Claude Haiku 4.6",
|
||||||
|
description: "Fastest and most affordable",
|
||||||
|
contextLength: 200_000,
|
||||||
|
pricing: .init(prompt: 0.80, completion: 4.0),
|
||||||
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
|
),
|
||||||
|
// Claude 4.5 series
|
||||||
|
ModelInfo(
|
||||||
|
id: "claude-opus-4-5",
|
||||||
|
name: "Claude Opus 4.5",
|
||||||
|
description: "Previous generation Opus",
|
||||||
|
contextLength: 200_000,
|
||||||
|
pricing: .init(prompt: 15.0, completion: 75.0),
|
||||||
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
|
),
|
||||||
ModelInfo(
|
ModelInfo(
|
||||||
id: "claude-opus-4-5-20251101",
|
id: "claude-opus-4-5-20251101",
|
||||||
name: "Claude Opus 4.5",
|
name: "Claude Opus 4.5",
|
||||||
@@ -90,6 +119,14 @@ class AnthropicProvider: AIProvider {
|
|||||||
pricing: .init(prompt: 15.0, completion: 75.0),
|
pricing: .init(prompt: 15.0, completion: 75.0),
|
||||||
capabilities: .init(vision: true, tools: true, online: true)
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
),
|
),
|
||||||
|
ModelInfo(
|
||||||
|
id: "claude-sonnet-4-5",
|
||||||
|
name: "Claude Sonnet 4.5",
|
||||||
|
description: "Best balance of speed and capability",
|
||||||
|
contextLength: 200_000,
|
||||||
|
pricing: .init(prompt: 3.0, completion: 15.0),
|
||||||
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
|
),
|
||||||
ModelInfo(
|
ModelInfo(
|
||||||
id: "claude-sonnet-4-5-20250929",
|
id: "claude-sonnet-4-5-20250929",
|
||||||
name: "Claude Sonnet 4.5",
|
name: "Claude Sonnet 4.5",
|
||||||
@@ -98,6 +135,14 @@ class AnthropicProvider: AIProvider {
|
|||||||
pricing: .init(prompt: 3.0, completion: 15.0),
|
pricing: .init(prompt: 3.0, completion: 15.0),
|
||||||
capabilities: .init(vision: true, tools: true, online: true)
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
),
|
),
|
||||||
|
ModelInfo(
|
||||||
|
id: "claude-haiku-4-5",
|
||||||
|
name: "Claude Haiku 4.5",
|
||||||
|
description: "Fastest and most affordable",
|
||||||
|
contextLength: 200_000,
|
||||||
|
pricing: .init(prompt: 0.80, completion: 4.0),
|
||||||
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
|
),
|
||||||
ModelInfo(
|
ModelInfo(
|
||||||
id: "claude-haiku-4-5-20251001",
|
id: "claude-haiku-4-5-20251001",
|
||||||
name: "Claude Haiku 4.5",
|
name: "Claude Haiku 4.5",
|
||||||
@@ -106,6 +151,7 @@ class AnthropicProvider: AIProvider {
|
|||||||
pricing: .init(prompt: 0.80, completion: 4.0),
|
pricing: .init(prompt: 0.80, completion: 4.0),
|
||||||
capabilities: .init(vision: true, tools: true, online: true)
|
capabilities: .init(vision: true, tools: true, online: true)
|
||||||
),
|
),
|
||||||
|
// Claude 3.x series
|
||||||
ModelInfo(
|
ModelInfo(
|
||||||
id: "claude-3-7-sonnet-20250219",
|
id: "claude-3-7-sonnet-20250219",
|
||||||
name: "Claude 3.7 Sonnet",
|
name: "Claude 3.7 Sonnet",
|
||||||
@@ -124,6 +170,14 @@ class AnthropicProvider: AIProvider {
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/// Pricing tiers used for fuzzy fallback matching on unknown model IDs.
|
||||||
|
/// Keyed by model name prefix (longest match wins).
|
||||||
|
private static let pricingFallback: [(prefix: String, prompt: Double, completion: Double)] = [
|
||||||
|
("claude-opus", 15.0, 75.0),
|
||||||
|
("claude-sonnet", 3.0, 15.0),
|
||||||
|
("claude-haiku", 0.80, 4.0),
|
||||||
|
]
|
||||||
|
|
||||||
/// Fetch live model list from GET /v1/models, enriched with local pricing/context metadata.
|
/// Fetch live model list from GET /v1/models, enriched with local pricing/context metadata.
|
||||||
/// Falls back to knownModels if the request fails (no key, offline, etc.).
|
/// Falls back to knownModels if the request fails (no key, offline, etc.).
|
||||||
func listModels() async throws -> [ModelInfo] {
|
func listModels() async throws -> [ModelInfo] {
|
||||||
@@ -158,14 +212,20 @@ class AnthropicProvider: AIProvider {
|
|||||||
guard let id = item["id"] as? String,
|
guard let id = item["id"] as? String,
|
||||||
id.hasPrefix("claude-") else { return nil }
|
id.hasPrefix("claude-") else { return nil }
|
||||||
let displayName = item["display_name"] as? String ?? id
|
let displayName = item["display_name"] as? String ?? id
|
||||||
|
// Exact match first
|
||||||
if let known = enrichment[id] { return known }
|
if let known = enrichment[id] { return known }
|
||||||
// Unknown new model — use display name and sensible defaults
|
// Fuzzy fallback: find the longest prefix that matches
|
||||||
|
let fallback = Self.pricingFallback
|
||||||
|
.filter { id.hasPrefix($0.prefix) }
|
||||||
|
.max(by: { $0.prefix.count < $1.prefix.count })
|
||||||
|
let pricing = fallback.map { ModelInfo.Pricing(prompt: $0.prompt, completion: $0.completion) }
|
||||||
|
?? ModelInfo.Pricing(prompt: 0, completion: 0)
|
||||||
return ModelInfo(
|
return ModelInfo(
|
||||||
id: id,
|
id: id,
|
||||||
name: displayName,
|
name: displayName,
|
||||||
description: item["description"] as? String ?? "",
|
description: item["description"] as? String ?? "",
|
||||||
contextLength: 200_000,
|
contextLength: 200_000,
|
||||||
pricing: .init(prompt: 0, completion: 0),
|
pricing: pricing,
|
||||||
capabilities: .init(vision: true, tools: true, online: false)
|
capabilities: .init(vision: true, tools: true, online: false)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -273,7 +273,7 @@ final class EmailHandlerService {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Please provide a complete, well-formatted response to this email. Your response will be sent as an HTML email.
|
Please provide a complete, well-formatted response to this email. Write the reply directly — do not wrap it in code fences or markdown blocks.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
@@ -299,7 +299,8 @@ final class EmailHandlerService {
|
|||||||
- Be professional and courteous
|
- Be professional and courteous
|
||||||
- Keep responses concise and relevant
|
- Keep responses concise and relevant
|
||||||
- Use proper email etiquette
|
- Use proper email etiquette
|
||||||
- Format your response using Markdown (it will be converted to HTML)
|
- Format your response using Markdown (bold, lists, headings are welcome)
|
||||||
|
- Do NOT wrap your response in code fences or ```html blocks — write the email body directly
|
||||||
- If you need information from files, you have read-only access via MCP tools
|
- If you need information from files, you have read-only access via MCP tools
|
||||||
- Never claim to write, modify, or delete files (read-only access)
|
- Never claim to write, modify, or delete files (read-only access)
|
||||||
- Sign emails appropriately
|
- Sign emails appropriately
|
||||||
@@ -412,27 +413,46 @@ final class EmailHandlerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func markdownToHTML(_ markdown: String) -> String {
|
private func markdownToHTML(_ markdown: String) -> String {
|
||||||
// Basic markdown to HTML conversion
|
var text = markdown.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
// TODO: Use proper markdown parser for production
|
|
||||||
var html = markdown
|
|
||||||
|
|
||||||
// Paragraphs
|
// Strip outer code fence wrapping the entire response
|
||||||
html = html.replacingOccurrences(of: "\n\n", with: "</p><p>")
|
// Models sometimes wrap their reply in ```html ... ``` or ``` ... ```
|
||||||
html = "<p>\(html)</p>"
|
let lines = text.components(separatedBy: "\n")
|
||||||
|
if lines.count >= 2 {
|
||||||
|
let first = lines[0].trimmingCharacters(in: .whitespaces)
|
||||||
|
let last = lines[lines.count - 1].trimmingCharacters(in: .whitespaces)
|
||||||
|
if (first == "```" || first.hasPrefix("```")) && last == "```" {
|
||||||
|
text = lines.dropFirst().dropLast().joined(separator: "\n")
|
||||||
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip any remaining fenced code blocks (preserve content, remove fences)
|
||||||
|
text = text.replacingOccurrences(
|
||||||
|
of: #"```[a-z]*\n([\s\S]*?)```"#,
|
||||||
|
with: "$1",
|
||||||
|
options: .regularExpression
|
||||||
|
)
|
||||||
|
|
||||||
// Bold
|
// Bold
|
||||||
html = html.replacingOccurrences(of: #"\*\*(.+?)\*\*"#, with: "<strong>$1</strong>", options: .regularExpression)
|
text = text.replacingOccurrences(of: #"\*\*(.+?)\*\*"#, with: "<strong>$1</strong>", options: .regularExpression)
|
||||||
|
|
||||||
// Italic
|
// Italic (avoid matching bold's leftover *)
|
||||||
html = html.replacingOccurrences(of: #"\*(.+?)\*"#, with: "<em>$1</em>", options: .regularExpression)
|
text = text.replacingOccurrences(of: #"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)"#, with: "<em>$1</em>", options: .regularExpression)
|
||||||
|
|
||||||
// Inline code
|
// Inline code
|
||||||
html = html.replacingOccurrences(of: #"`(.+?)`"#, with: "<code>$1</code>", options: .regularExpression)
|
text = text.replacingOccurrences(of: #"`([^`\n]+)`"#, with: "<code>$1</code>", options: .regularExpression)
|
||||||
|
|
||||||
// Line breaks
|
// Split into paragraphs on double newlines, wrap each in <p>
|
||||||
html = html.replacingOccurrences(of: "\n", with: "<br>")
|
let paragraphs = text.components(separatedBy: "\n\n")
|
||||||
|
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||||
|
.filter { !$0.isEmpty }
|
||||||
|
.map { para in
|
||||||
|
let withBreaks = para.replacingOccurrences(of: "\n", with: "<br>")
|
||||||
|
return "<p>\(withBreaks)</p>"
|
||||||
|
}
|
||||||
|
|
||||||
return html
|
return paragraphs.joined(separator: "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Error Handling
|
// MARK: - Error Handling
|
||||||
|
|||||||
@@ -110,10 +110,23 @@ class MCPService {
|
|||||||
var respectGitignore: Bool { settings.mcpRespectGitignore }
|
var respectGitignore: Bool { settings.mcpRespectGitignore }
|
||||||
|
|
||||||
private let anytypeService = AnytypeMCPService.shared
|
private let anytypeService = AnytypeMCPService.shared
|
||||||
|
private let paperlessService = PaperlessService.shared
|
||||||
|
|
||||||
|
// MARK: - Bash Approval State
|
||||||
|
|
||||||
|
struct PendingBashCommand: Identifiable {
|
||||||
|
let id = UUID()
|
||||||
|
let command: String
|
||||||
|
let workingDirectory: String
|
||||||
|
}
|
||||||
|
|
||||||
|
private(set) var pendingBashCommand: PendingBashCommand? = nil
|
||||||
|
private var pendingBashContinuation: CheckedContinuation<[String: Any], Never>? = nil
|
||||||
|
private(set) var bashSessionApproved: Bool = false
|
||||||
|
|
||||||
// MARK: - Tool Schema Generation
|
// MARK: - Tool Schema Generation
|
||||||
|
|
||||||
func getToolSchemas() -> [Tool] {
|
func getToolSchemas(onlineMode: Bool = false) -> [Tool] {
|
||||||
var tools: [Tool] = [
|
var tools: [Tool] = [
|
||||||
makeTool(
|
makeTool(
|
||||||
name: "read_file",
|
name: "read_file",
|
||||||
@@ -214,6 +227,39 @@ class MCPService {
|
|||||||
tools.append(contentsOf: anytypeService.getToolSchemas())
|
tools.append(contentsOf: anytypeService.getToolSchemas())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add Paperless-NGX tools if enabled and configured
|
||||||
|
if settings.paperlessEnabled && settings.paperlessConfigured {
|
||||||
|
tools.append(contentsOf: paperlessService.getToolSchemas())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add bash_execute tool when bash is enabled
|
||||||
|
if settings.bashEnabled {
|
||||||
|
let workDir = settings.bashWorkingDirectory
|
||||||
|
let timeout = settings.bashTimeout
|
||||||
|
let approvalNote = settings.bashRequireApproval ? " User approval required before execution." : ""
|
||||||
|
tools.append(makeTool(
|
||||||
|
name: "bash_execute",
|
||||||
|
description: "Execute a shell command via /bin/zsh and return stdout/stderr. Working directory: \(workDir). Timeout: \(timeout)s.\(approvalNote)",
|
||||||
|
properties: [
|
||||||
|
"command": prop("string", "The shell command to execute")
|
||||||
|
],
|
||||||
|
required: ["command"]
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add web_search tool when online mode is active
|
||||||
|
// (OpenRouter handles search natively via :online model suffix, so excluded here)
|
||||||
|
if onlineMode {
|
||||||
|
tools.append(makeTool(
|
||||||
|
name: "web_search",
|
||||||
|
description: "Search the web for current information using DuckDuckGo. Use this when you need up-to-date information, news, or facts not in your training data. Formulate a concise, focused search query.",
|
||||||
|
properties: [
|
||||||
|
"query": prop("string", "The search query to look up")
|
||||||
|
],
|
||||||
|
required: ["query"]
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
return tools
|
return tools
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,11 +373,41 @@ class MCPService {
|
|||||||
}
|
}
|
||||||
return copyFile(source: source, destination: destination)
|
return copyFile(source: source, destination: destination)
|
||||||
|
|
||||||
|
case "bash_execute":
|
||||||
|
guard settings.bashEnabled else {
|
||||||
|
return ["error": "Bash execution is disabled. Enable it in Settings > MCP."]
|
||||||
|
}
|
||||||
|
guard let command = args["command"] as? String, !command.isEmpty else {
|
||||||
|
return ["error": "Missing required parameter: command"]
|
||||||
|
}
|
||||||
|
let workDir = settings.bashWorkingDirectory
|
||||||
|
if settings.bashRequireApproval {
|
||||||
|
return await executeBashWithApproval(command: command, workingDirectory: workDir)
|
||||||
|
} else {
|
||||||
|
return await runBashCommand(command, workingDirectory: workDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
case "web_search":
|
||||||
|
let query = args["query"] as? String ?? ""
|
||||||
|
guard !query.isEmpty else {
|
||||||
|
return ["error": "Missing required parameter: query"]
|
||||||
|
}
|
||||||
|
let results = await WebSearchService.shared.search(query: query)
|
||||||
|
if results.isEmpty {
|
||||||
|
return ["results": [], "message": "No results found for: \(query)"]
|
||||||
|
}
|
||||||
|
let mapped = results.map { ["title": $0.title, "url": $0.url, "snippet": $0.snippet] }
|
||||||
|
return ["results": mapped]
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Route anytype_* tools to AnytypeMCPService
|
// Route anytype_* tools to AnytypeMCPService
|
||||||
if name.hasPrefix("anytype_") {
|
if name.hasPrefix("anytype_") {
|
||||||
return await anytypeService.executeTool(name: name, arguments: arguments)
|
return await anytypeService.executeTool(name: name, arguments: arguments)
|
||||||
}
|
}
|
||||||
|
// Route paperless_* tools to PaperlessService
|
||||||
|
if name.hasPrefix("paperless_") {
|
||||||
|
return await paperlessService.executeTool(name: name, arguments: arguments)
|
||||||
|
}
|
||||||
return ["error": "Unknown tool: \(name)"]
|
return ["error": "Unknown tool: \(name)"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -671,6 +747,113 @@ class MCPService {
|
|||||||
return ["success": true, "source": resolvedSrc, "destination": resolvedDst]
|
return ["success": true, "source": resolvedSrc, "destination": resolvedDst]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Bash Execution
|
||||||
|
|
||||||
|
private func executeBashWithApproval(command: String, workingDirectory: String) async -> [String: Any] {
|
||||||
|
// If the user already approved all commands for this session, skip the UI
|
||||||
|
if bashSessionApproved {
|
||||||
|
return await runBashCommand(command, workingDirectory: workingDirectory)
|
||||||
|
}
|
||||||
|
return await withCheckedContinuation { continuation in
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.pendingBashCommand = PendingBashCommand(command: command, workingDirectory: workingDirectory)
|
||||||
|
self.pendingBashContinuation = continuation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func approvePendingBashCommand(forSession: Bool = false) {
|
||||||
|
guard let pending = pendingBashCommand, let cont = pendingBashContinuation else { return }
|
||||||
|
pendingBashCommand = nil
|
||||||
|
pendingBashContinuation = nil
|
||||||
|
if forSession {
|
||||||
|
bashSessionApproved = true
|
||||||
|
}
|
||||||
|
Task.detached(priority: .userInitiated) {
|
||||||
|
let result = await self.runBashCommand(pending.command, workingDirectory: pending.workingDirectory)
|
||||||
|
cont.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func denyPendingBashCommand() {
|
||||||
|
guard pendingBashCommand != nil else { return }
|
||||||
|
pendingBashCommand = nil
|
||||||
|
pendingBashContinuation?.resume(returning: ["error": "User denied command execution"])
|
||||||
|
pendingBashContinuation = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resetBashSessionApproval() {
|
||||||
|
bashSessionApproved = false
|
||||||
|
}
|
||||||
|
|
||||||
|
private func runBashCommand(_ command: String, workingDirectory: String) async -> [String: Any] {
|
||||||
|
let timeoutSeconds = settings.bashTimeout
|
||||||
|
let workDir = ((workingDirectory as NSString).expandingTildeInPath as NSString).standardizingPath
|
||||||
|
Log.mcp.info("bash_execute: \(command)")
|
||||||
|
|
||||||
|
return await withCheckedContinuation { continuation in
|
||||||
|
DispatchQueue.global(qos: .userInitiated).async {
|
||||||
|
let process = Process()
|
||||||
|
process.executableURL = URL(fileURLWithPath: "/bin/zsh")
|
||||||
|
process.arguments = ["-c", command]
|
||||||
|
|
||||||
|
var isDir: ObjCBool = false
|
||||||
|
if FileManager.default.fileExists(atPath: workDir, isDirectory: &isDir), isDir.boolValue {
|
||||||
|
process.currentDirectoryURL = URL(fileURLWithPath: workDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
let stdoutPipe = Pipe()
|
||||||
|
let stderrPipe = Pipe()
|
||||||
|
process.standardOutput = stdoutPipe
|
||||||
|
process.standardError = stderrPipe
|
||||||
|
|
||||||
|
var timedOut = false
|
||||||
|
let timeoutItem = DispatchWorkItem {
|
||||||
|
if process.isRunning {
|
||||||
|
timedOut = true
|
||||||
|
process.terminate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(timeoutSeconds), execute: timeoutItem)
|
||||||
|
|
||||||
|
do {
|
||||||
|
try process.run()
|
||||||
|
process.waitUntilExit()
|
||||||
|
} catch {
|
||||||
|
timeoutItem.cancel()
|
||||||
|
Log.mcp.error("bash_execute failed to start: \(error.localizedDescription)")
|
||||||
|
continuation.resume(returning: ["error": "Failed to run command: \(error.localizedDescription)"])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
timeoutItem.cancel()
|
||||||
|
|
||||||
|
let stdout = String(data: stdoutPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
|
||||||
|
let stderr = String(data: stderrPipe.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) ?? ""
|
||||||
|
let exitCode = Int(process.terminationStatus)
|
||||||
|
|
||||||
|
Log.mcp.info("bash_execute exit=\(exitCode) stdout=\(stdout.count)b stderr=\(stderr.count)b timedOut=\(timedOut)")
|
||||||
|
|
||||||
|
var result: [String: Any] = ["exit_code": exitCode]
|
||||||
|
if !stdout.isEmpty {
|
||||||
|
let maxOut = 20_000
|
||||||
|
result["stdout"] = stdout.count > maxOut
|
||||||
|
? String(stdout.prefix(maxOut)) + "\n... (output truncated)"
|
||||||
|
: stdout
|
||||||
|
}
|
||||||
|
if !stderr.isEmpty {
|
||||||
|
result["stderr"] = String(stderr.prefix(5_000))
|
||||||
|
}
|
||||||
|
if timedOut {
|
||||||
|
result["timed_out"] = true
|
||||||
|
result["note"] = "Command terminated after \(timeoutSeconds)s timeout"
|
||||||
|
}
|
||||||
|
|
||||||
|
continuation.resume(returning: result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Gitignore Support
|
// MARK: - Gitignore Support
|
||||||
|
|
||||||
/// Reload gitignore rules for all allowed folders
|
/// Reload gitignore rules for all allowed folders
|
||||||
|
|||||||
496
oAI/Services/PaperlessService.swift
Normal file
496
oAI/Services/PaperlessService.swift
Normal file
@@ -0,0 +1,496 @@
|
|||||||
|
//
|
||||||
|
// PaperlessService.swift
|
||||||
|
// oAI
|
||||||
|
//
|
||||||
|
// Paperless-NGX integration: search, read, and upload documents via REST API
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
// Copyright (C) 2026 Rune Olsen
|
||||||
|
//
|
||||||
|
// This file is part of oAI.
|
||||||
|
//
|
||||||
|
// oAI is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as
|
||||||
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// oAI is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
|
||||||
|
// Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public
|
||||||
|
// License along with oAI. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import os
|
||||||
|
|
||||||
|
@Observable
|
||||||
|
class PaperlessService {
|
||||||
|
static let shared = PaperlessService()
|
||||||
|
|
||||||
|
private let settings = SettingsService.shared
|
||||||
|
private let log = Logger(subsystem: "com.oai.oAI", category: "mcp")
|
||||||
|
private let readTimeout: TimeInterval = 15
|
||||||
|
private let uploadTimeout: TimeInterval = 60
|
||||||
|
|
||||||
|
private(set) var isConnected = false
|
||||||
|
|
||||||
|
// In-memory caches for ID → name resolution
|
||||||
|
private var tagCache: [Int: String] = [:]
|
||||||
|
private var correspondentCache: [Int: String] = [:]
|
||||||
|
private var documentTypeCache: [Int: String] = [:]
|
||||||
|
|
||||||
|
private init() {}
|
||||||
|
|
||||||
|
// MARK: - Connection Test
|
||||||
|
|
||||||
|
func testConnection() async -> Result<String, Error> {
|
||||||
|
do {
|
||||||
|
let result = try await request(endpoint: "/api/documents/", queryParams: ["page_size": "1"])
|
||||||
|
if let count = result["count"] as? Int {
|
||||||
|
isConnected = true
|
||||||
|
return .success("Connected (\(count) document\(count == 1 ? "" : "s"))")
|
||||||
|
} else {
|
||||||
|
isConnected = true
|
||||||
|
return .success("Connected to Paperless-NGX")
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
isConnected = false
|
||||||
|
return .failure(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Tool Schemas
|
||||||
|
|
||||||
|
func getToolSchemas() -> [Tool] {
|
||||||
|
return [
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_search",
|
||||||
|
description: "Search for documents in Paperless-NGX by title, content, tags, or any text. Returns document metadata and a preview of OCR-extracted content. Use this to find invoices, contracts, letters, or any stored document.",
|
||||||
|
properties: [
|
||||||
|
"query": prop("string", "Search query — can be text from document content, title, correspondent name, or tag"),
|
||||||
|
"page": prop("number", "Page number for pagination (default: 1, each page has 25 results)")
|
||||||
|
],
|
||||||
|
required: ["query"]
|
||||||
|
),
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_get_document",
|
||||||
|
description: "Get the full details and complete OCR-extracted text content of a specific Paperless-NGX document by ID. Use after paperless_search to read the full text of a document.",
|
||||||
|
properties: [
|
||||||
|
"document_id": prop("number", "The numeric ID of the document to retrieve")
|
||||||
|
],
|
||||||
|
required: ["document_id"]
|
||||||
|
),
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_list_tags",
|
||||||
|
description: "List all tags defined in Paperless-NGX with their document counts.",
|
||||||
|
properties: [:],
|
||||||
|
required: []
|
||||||
|
),
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_list_correspondents",
|
||||||
|
description: "List all correspondents (senders/recipients) defined in Paperless-NGX with their document counts.",
|
||||||
|
properties: [:],
|
||||||
|
required: []
|
||||||
|
),
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_list_document_types",
|
||||||
|
description: "List all document types defined in Paperless-NGX with their document counts.",
|
||||||
|
properties: [:],
|
||||||
|
required: []
|
||||||
|
),
|
||||||
|
makeTool(
|
||||||
|
name: "paperless_upload_document",
|
||||||
|
description: "Upload a local file to Paperless-NGX for OCR processing and storage. Supports PDF, PNG, JPEG, TIFF, and other image formats.",
|
||||||
|
properties: [
|
||||||
|
"file_path": prop("string", "Absolute path to the local file to upload"),
|
||||||
|
"title": prop("string", "Optional title for the document"),
|
||||||
|
"tag_ids": prop("string", "Optional comma-separated tag IDs to assign (e.g. '1,3,7')")
|
||||||
|
],
|
||||||
|
required: ["file_path"]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Tool Execution
|
||||||
|
|
||||||
|
func executeTool(name: String, arguments: String) async -> [String: Any] {
|
||||||
|
log.info("Executing Paperless tool: \(name)")
|
||||||
|
|
||||||
|
guard let argData = arguments.data(using: .utf8),
|
||||||
|
let args = try? JSONSerialization.jsonObject(with: argData) as? [String: Any] else {
|
||||||
|
return ["error": "Invalid arguments JSON"]
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
switch name {
|
||||||
|
case "paperless_search":
|
||||||
|
guard let query = args["query"] as? String else {
|
||||||
|
return ["error": "Missing required parameter: query"]
|
||||||
|
}
|
||||||
|
let page: Int
|
||||||
|
if let p = args["page"] as? Int { page = p }
|
||||||
|
else if let p = args["page"] as? Double { page = Int(p) }
|
||||||
|
else { page = 1 }
|
||||||
|
return try await searchDocuments(query: query, page: page)
|
||||||
|
|
||||||
|
case "paperless_get_document":
|
||||||
|
let docId: Int
|
||||||
|
if let id = args["document_id"] as? Int { docId = id }
|
||||||
|
else if let id = args["document_id"] as? Double { docId = Int(id) }
|
||||||
|
else { return ["error": "Missing or invalid parameter: document_id (expected integer)"] }
|
||||||
|
return try await getDocument(id: docId)
|
||||||
|
|
||||||
|
case "paperless_list_tags":
|
||||||
|
return try await listTags()
|
||||||
|
|
||||||
|
case "paperless_list_correspondents":
|
||||||
|
return try await listCorrespondents()
|
||||||
|
|
||||||
|
case "paperless_list_document_types":
|
||||||
|
return try await listDocumentTypes()
|
||||||
|
|
||||||
|
case "paperless_upload_document":
|
||||||
|
guard let filePath = args["file_path"] as? String else {
|
||||||
|
return ["error": "Missing required parameter: file_path"]
|
||||||
|
}
|
||||||
|
let title = args["title"] as? String
|
||||||
|
let tagIds = args["tag_ids"] as? String
|
||||||
|
return try await uploadDocument(filePath: filePath, title: title, tagIds: tagIds)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return ["error": "Unknown Paperless tool: \(name)"]
|
||||||
|
}
|
||||||
|
} catch PaperlessError.notConfigured {
|
||||||
|
return ["error": "Paperless-NGX is not configured. Set your URL and API token in Settings > Paperless."]
|
||||||
|
} catch PaperlessError.unauthorized {
|
||||||
|
return ["error": "Invalid API token. Check your Paperless-NGX token in Settings > Paperless."]
|
||||||
|
} catch PaperlessError.httpError(let code, let msg) {
|
||||||
|
return ["error": "Paperless-NGX API error \(code): \(msg)"]
|
||||||
|
} catch {
|
||||||
|
return ["error": "Paperless error: \(error.localizedDescription)"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - API Operations
|
||||||
|
|
||||||
|
private func searchDocuments(query: String, page: Int) async throws -> [String: Any] {
|
||||||
|
await prefetchCaches()
|
||||||
|
|
||||||
|
let result = try await request(endpoint: "/api/documents/", queryParams: [
|
||||||
|
"query": query,
|
||||||
|
"page": String(page)
|
||||||
|
])
|
||||||
|
|
||||||
|
let total = result["count"] as? Int ?? 0
|
||||||
|
guard let rawResults = result["results"] as? [[String: Any]] else {
|
||||||
|
return ["total": total, "page": page, "results": []]
|
||||||
|
}
|
||||||
|
|
||||||
|
let formatted = rawResults.map { doc -> [String: Any] in
|
||||||
|
var item: [String: Any] = [:]
|
||||||
|
item["id"] = doc["id"] ?? 0
|
||||||
|
item["title"] = doc["title"] ?? "Untitled"
|
||||||
|
item["created"] = (doc["created"] as? String).map { String($0.prefix(10)) } ?? ""
|
||||||
|
|
||||||
|
if let corrId = doc["correspondent"] as? Int {
|
||||||
|
item["correspondent"] = correspondentCache[corrId] ?? "ID:\(corrId)"
|
||||||
|
}
|
||||||
|
|
||||||
|
if let dtId = doc["document_type"] as? Int {
|
||||||
|
item["document_type"] = documentTypeCache[dtId] ?? "ID:\(dtId)"
|
||||||
|
}
|
||||||
|
|
||||||
|
if let tagIds = doc["tags"] as? [Int] {
|
||||||
|
item["tags"] = tagIds.map { tagCache[$0] ?? "ID:\($0)" }
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content preview capped at 500 chars
|
||||||
|
if let content = doc["content"] as? String, !content.isEmpty {
|
||||||
|
let preview = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
item["content_preview"] = String(preview.prefix(500))
|
||||||
|
}
|
||||||
|
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
|
return ["total": total, "page": page, "results": formatted]
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getDocument(id: Int) async throws -> [String: Any] {
|
||||||
|
await prefetchCaches()
|
||||||
|
|
||||||
|
let doc = try await request(endpoint: "/api/documents/\(id)/")
|
||||||
|
|
||||||
|
var result: [String: Any] = [:]
|
||||||
|
result["id"] = doc["id"] ?? id
|
||||||
|
result["title"] = doc["title"] ?? "Untitled"
|
||||||
|
result["created"] = (doc["created"] as? String).map { String($0.prefix(10)) } ?? ""
|
||||||
|
result["added"] = (doc["added"] as? String).map { String($0.prefix(10)) } ?? ""
|
||||||
|
result["modified"] = (doc["modified"] as? String).map { String($0.prefix(10)) } ?? ""
|
||||||
|
|
||||||
|
if let corrId = doc["correspondent"] as? Int {
|
||||||
|
result["correspondent"] = correspondentCache[corrId] ?? "ID:\(corrId)"
|
||||||
|
}
|
||||||
|
if let dtId = doc["document_type"] as? Int {
|
||||||
|
result["document_type"] = documentTypeCache[dtId] ?? "ID:\(dtId)"
|
||||||
|
}
|
||||||
|
if let tagIds = doc["tags"] as? [Int] {
|
||||||
|
result["tags"] = tagIds.map { tagCache[$0] ?? "ID:\($0)" }
|
||||||
|
}
|
||||||
|
if let asn = doc["archive_serial_number"] as? String {
|
||||||
|
result["archive_serial_number"] = asn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full OCR content capped at 30,000 chars
|
||||||
|
if let content = doc["content"] as? String {
|
||||||
|
let trimmed = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||||
|
result["content"] = String(trimmed.prefix(30_000))
|
||||||
|
result["content_length"] = trimmed.count
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private func listTags() async throws -> [String: Any] {
|
||||||
|
let result = try await request(endpoint: "/api/tags/", queryParams: ["page_size": "250"])
|
||||||
|
guard let items = result["results"] as? [[String: Any]] else {
|
||||||
|
return ["count": 0, "tags": []]
|
||||||
|
}
|
||||||
|
let formatted = items.map { tag -> [String: Any] in
|
||||||
|
["id": tag["id"] ?? 0, "name": tag["name"] ?? "Unknown", "count": tag["document_count"] ?? 0]
|
||||||
|
}
|
||||||
|
return ["count": formatted.count, "tags": formatted]
|
||||||
|
}
|
||||||
|
|
||||||
|
private func listCorrespondents() async throws -> [String: Any] {
|
||||||
|
let result = try await request(endpoint: "/api/correspondents/", queryParams: ["page_size": "250"])
|
||||||
|
guard let items = result["results"] as? [[String: Any]] else {
|
||||||
|
return ["count": 0, "correspondents": []]
|
||||||
|
}
|
||||||
|
let formatted = items.map { c -> [String: Any] in
|
||||||
|
["id": c["id"] ?? 0, "name": c["name"] ?? "Unknown", "count": c["document_count"] ?? 0]
|
||||||
|
}
|
||||||
|
return ["count": formatted.count, "correspondents": formatted]
|
||||||
|
}
|
||||||
|
|
||||||
|
private func listDocumentTypes() async throws -> [String: Any] {
|
||||||
|
let result = try await request(endpoint: "/api/document_types/", queryParams: ["page_size": "250"])
|
||||||
|
guard let items = result["results"] as? [[String: Any]] else {
|
||||||
|
return ["count": 0, "document_types": []]
|
||||||
|
}
|
||||||
|
let formatted = items.map { dt -> [String: Any] in
|
||||||
|
["id": dt["id"] ?? 0, "name": dt["name"] ?? "Unknown", "count": dt["document_count"] ?? 0]
|
||||||
|
}
|
||||||
|
return ["count": formatted.count, "document_types": formatted]
|
||||||
|
}
|
||||||
|
|
||||||
|
private func uploadDocument(filePath: String, title: String?, tagIds: String?) async throws -> [String: Any] {
|
||||||
|
let expanded = (filePath as NSString).expandingTildeInPath
|
||||||
|
let resolved = (expanded as NSString).standardizingPath
|
||||||
|
|
||||||
|
guard FileManager.default.fileExists(atPath: resolved) else {
|
||||||
|
return ["error": "File not found: \(filePath)"]
|
||||||
|
}
|
||||||
|
guard let fileData = FileManager.default.contents(atPath: resolved) else {
|
||||||
|
return ["error": "Cannot read file: \(filePath)"]
|
||||||
|
}
|
||||||
|
|
||||||
|
let fileName = (resolved as NSString).lastPathComponent
|
||||||
|
|
||||||
|
guard let token = settings.paperlessAPIToken, !token.isEmpty else {
|
||||||
|
throw PaperlessError.notConfigured
|
||||||
|
}
|
||||||
|
let baseURL = settings.paperlessURL
|
||||||
|
guard !baseURL.isEmpty, let url = URL(string: baseURL + "/api/documents/post_document/") else {
|
||||||
|
throw PaperlessError.notConfigured
|
||||||
|
}
|
||||||
|
|
||||||
|
let boundary = "PaperlessBoundary\(UUID().uuidString.replacingOccurrences(of: "-", with: ""))"
|
||||||
|
var body = Data()
|
||||||
|
|
||||||
|
func appendField(_ name: String, _ value: String) {
|
||||||
|
body.append("--\(boundary)\r\n".data(using: .utf8)!)
|
||||||
|
body.append("Content-Disposition: form-data; name=\"\(name)\"\r\n\r\n".data(using: .utf8)!)
|
||||||
|
body.append("\(value)\r\n".data(using: .utf8)!)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let title = title, !title.isEmpty {
|
||||||
|
appendField("title", title)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let tagIds = tagIds {
|
||||||
|
let ids = tagIds.split(separator: ",").compactMap { Int($0.trimmingCharacters(in: .whitespaces)) }
|
||||||
|
for id in ids {
|
||||||
|
appendField("tags", String(id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mimeType = mimeTypeFor(fileName: fileName)
|
||||||
|
body.append("--\(boundary)\r\n".data(using: .utf8)!)
|
||||||
|
body.append("Content-Disposition: form-data; name=\"document\"; filename=\"\(fileName)\"\r\n".data(using: .utf8)!)
|
||||||
|
body.append("Content-Type: \(mimeType)\r\n\r\n".data(using: .utf8)!)
|
||||||
|
body.append(fileData)
|
||||||
|
body.append("\r\n--\(boundary)--\r\n".data(using: .utf8)!)
|
||||||
|
|
||||||
|
var urlRequest = URLRequest(url: url, timeoutInterval: uploadTimeout)
|
||||||
|
urlRequest.httpMethod = "POST"
|
||||||
|
urlRequest.setValue("Token \(token)", forHTTPHeaderField: "Authorization")
|
||||||
|
urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
|
||||||
|
urlRequest.httpBody = body
|
||||||
|
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: urlRequest)
|
||||||
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
|
throw PaperlessError.httpError(0, "Invalid response")
|
||||||
|
}
|
||||||
|
|
||||||
|
if httpResponse.statusCode == 401 { throw PaperlessError.unauthorized }
|
||||||
|
|
||||||
|
if (200...299).contains(httpResponse.statusCode) {
|
||||||
|
return ["success": true, "message": "Document uploaded successfully. Paperless-NGX will process it shortly."]
|
||||||
|
}
|
||||||
|
|
||||||
|
let msg = String(data: data, encoding: .utf8) ?? "Unknown error"
|
||||||
|
throw PaperlessError.httpError(httpResponse.statusCode, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Cache Prefetch
|
||||||
|
|
||||||
|
private func prefetchCaches() async {
|
||||||
|
if tagCache.isEmpty {
|
||||||
|
if let result = try? await request(endpoint: "/api/tags/", queryParams: ["page_size": "250"]),
|
||||||
|
let items = result["results"] as? [[String: Any]] {
|
||||||
|
for item in items {
|
||||||
|
if let id = item["id"] as? Int, let name = item["name"] as? String {
|
||||||
|
tagCache[id] = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if correspondentCache.isEmpty {
|
||||||
|
if let result = try? await request(endpoint: "/api/correspondents/", queryParams: ["page_size": "250"]),
|
||||||
|
let items = result["results"] as? [[String: Any]] {
|
||||||
|
for item in items {
|
||||||
|
if let id = item["id"] as? Int, let name = item["name"] as? String {
|
||||||
|
correspondentCache[id] = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if documentTypeCache.isEmpty {
|
||||||
|
if let result = try? await request(endpoint: "/api/document_types/", queryParams: ["page_size": "250"]),
|
||||||
|
let items = result["results"] as? [[String: Any]] {
|
||||||
|
for item in items {
|
||||||
|
if let id = item["id"] as? Int, let name = item["name"] as? String {
|
||||||
|
documentTypeCache[id] = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - HTTP Client
|
||||||
|
|
||||||
|
private func request(endpoint: String, queryParams: [String: String] = [:]) async throws -> [String: Any] {
|
||||||
|
guard let token = settings.paperlessAPIToken, !token.isEmpty else {
|
||||||
|
throw PaperlessError.notConfigured
|
||||||
|
}
|
||||||
|
let baseURL = settings.paperlessURL
|
||||||
|
guard !baseURL.isEmpty else { throw PaperlessError.notConfigured }
|
||||||
|
|
||||||
|
var urlString = baseURL + endpoint
|
||||||
|
if !queryParams.isEmpty {
|
||||||
|
var comps = URLComponents(string: urlString) ?? URLComponents()
|
||||||
|
comps.queryItems = queryParams.map { URLQueryItem(name: $0.key, value: $0.value) }
|
||||||
|
urlString = comps.url?.absoluteString ?? urlString
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let url = URL(string: urlString) else {
|
||||||
|
throw PaperlessError.httpError(0, "Invalid URL: \(urlString)")
|
||||||
|
}
|
||||||
|
|
||||||
|
var urlRequest = URLRequest(url: url, timeoutInterval: readTimeout)
|
||||||
|
urlRequest.httpMethod = "GET"
|
||||||
|
urlRequest.setValue("Token \(token)", forHTTPHeaderField: "Authorization")
|
||||||
|
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
|
||||||
|
|
||||||
|
do {
|
||||||
|
let (data, response) = try await URLSession.shared.data(for: urlRequest)
|
||||||
|
|
||||||
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
|
throw PaperlessError.httpError(0, "Invalid response")
|
||||||
|
}
|
||||||
|
|
||||||
|
if httpResponse.statusCode == 401 { throw PaperlessError.unauthorized }
|
||||||
|
|
||||||
|
guard (200...299).contains(httpResponse.statusCode) else {
|
||||||
|
let msg = String(data: data, encoding: .utf8) ?? "Unknown error"
|
||||||
|
throw PaperlessError.httpError(httpResponse.statusCode, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
||||||
|
return [:]
|
||||||
|
}
|
||||||
|
return json
|
||||||
|
} catch let error as PaperlessError {
|
||||||
|
throw error
|
||||||
|
} catch {
|
||||||
|
throw PaperlessError.httpError(0, error.localizedDescription)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Helpers
|
||||||
|
|
||||||
|
private func mimeTypeFor(fileName: String) -> String {
|
||||||
|
let ext = (fileName as NSString).pathExtension.lowercased()
|
||||||
|
switch ext {
|
||||||
|
case "pdf": return "application/pdf"
|
||||||
|
case "png": return "image/png"
|
||||||
|
case "jpg", "jpeg": return "image/jpeg"
|
||||||
|
case "tiff", "tif": return "image/tiff"
|
||||||
|
case "gif": return "image/gif"
|
||||||
|
case "webp": return "image/webp"
|
||||||
|
default: return "application/octet-stream"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func makeTool(name: String, description: String, properties: [String: Tool.Function.Parameters.Property], required: [String]) -> Tool {
|
||||||
|
Tool(
|
||||||
|
type: "function",
|
||||||
|
function: Tool.Function(
|
||||||
|
name: name,
|
||||||
|
description: description,
|
||||||
|
parameters: Tool.Function.Parameters(
|
||||||
|
type: "object",
|
||||||
|
properties: properties,
|
||||||
|
required: required
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func prop(_ type: String, _ description: String) -> Tool.Function.Parameters.Property {
|
||||||
|
Tool.Function.Parameters.Property(type: type, description: description, enum: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Error Types
|
||||||
|
|
||||||
|
enum PaperlessError: LocalizedError {
|
||||||
|
case notConfigured
|
||||||
|
case unauthorized
|
||||||
|
case httpError(Int, String)
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
switch self {
|
||||||
|
case .notConfigured:
|
||||||
|
return "Paperless-NGX is not configured. Set your URL and API token in Settings > Paperless."
|
||||||
|
case .unauthorized:
|
||||||
|
return "Invalid API token. Check your Paperless-NGX token in Settings > Paperless."
|
||||||
|
case .httpError(let code, let msg):
|
||||||
|
return "Paperless-NGX API error \(code): \(msg)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@ class SettingsService {
|
|||||||
static let googleAPIKey = "googleAPIKey"
|
static let googleAPIKey = "googleAPIKey"
|
||||||
static let googleSearchEngineID = "googleSearchEngineID"
|
static let googleSearchEngineID = "googleSearchEngineID"
|
||||||
static let anytypeMcpAPIKey = "anytypeMcpAPIKey"
|
static let anytypeMcpAPIKey = "anytypeMcpAPIKey"
|
||||||
|
static let paperlessAPIToken = "paperlessAPIToken"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Old keychain keys (for migration only)
|
// Old keychain keys (for migration only)
|
||||||
@@ -446,6 +447,83 @@ class SettingsService {
|
|||||||
return !key.isEmpty
|
return !key.isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Bash Execution Settings
|
||||||
|
|
||||||
|
var bashEnabled: Bool {
|
||||||
|
get { cache["bashEnabled"] == "true" }
|
||||||
|
set {
|
||||||
|
cache["bashEnabled"] = String(newValue)
|
||||||
|
DatabaseService.shared.setSetting(key: "bashEnabled", value: String(newValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bashRequireApproval: Bool {
|
||||||
|
get { cache["bashRequireApproval"].map { $0 == "true" } ?? true }
|
||||||
|
set {
|
||||||
|
cache["bashRequireApproval"] = String(newValue)
|
||||||
|
DatabaseService.shared.setSetting(key: "bashRequireApproval", value: String(newValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bashWorkingDirectory: String {
|
||||||
|
get { cache["bashWorkingDirectory"] ?? "~" }
|
||||||
|
set {
|
||||||
|
cache["bashWorkingDirectory"] = newValue
|
||||||
|
DatabaseService.shared.setSetting(key: "bashWorkingDirectory", value: newValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var bashTimeout: Int {
|
||||||
|
get { cache["bashTimeout"].flatMap(Int.init) ?? 30 }
|
||||||
|
set {
|
||||||
|
cache["bashTimeout"] = String(newValue)
|
||||||
|
DatabaseService.shared.setSetting(key: "bashTimeout", value: String(newValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Paperless-NGX Settings
|
||||||
|
|
||||||
|
var paperlessEnabled: Bool {
|
||||||
|
get { cache["paperlessEnabled"] == "true" }
|
||||||
|
set {
|
||||||
|
cache["paperlessEnabled"] = String(newValue)
|
||||||
|
DatabaseService.shared.setSetting(key: "paperlessEnabled", value: String(newValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var paperlessURL: String {
|
||||||
|
get { cache["paperlessURL"] ?? "" }
|
||||||
|
set {
|
||||||
|
var trimmed = newValue.trimmingCharacters(in: .whitespaces)
|
||||||
|
// Remove trailing slash for consistency
|
||||||
|
while trimmed.hasSuffix("/") { trimmed = String(trimmed.dropLast()) }
|
||||||
|
if trimmed.isEmpty {
|
||||||
|
cache.removeValue(forKey: "paperlessURL")
|
||||||
|
DatabaseService.shared.deleteSetting(key: "paperlessURL")
|
||||||
|
} else {
|
||||||
|
cache["paperlessURL"] = trimmed
|
||||||
|
DatabaseService.shared.setSetting(key: "paperlessURL", value: trimmed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var paperlessAPIToken: String? {
|
||||||
|
get { try? DatabaseService.shared.getEncryptedSetting(key: EncryptedKeys.paperlessAPIToken) }
|
||||||
|
set {
|
||||||
|
if let value = newValue, !value.isEmpty {
|
||||||
|
try? DatabaseService.shared.setEncryptedSetting(key: EncryptedKeys.paperlessAPIToken, value: value)
|
||||||
|
} else {
|
||||||
|
DatabaseService.shared.deleteEncryptedSetting(key: EncryptedKeys.paperlessAPIToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var paperlessConfigured: Bool {
|
||||||
|
guard !paperlessURL.isEmpty else { return false }
|
||||||
|
guard let token = paperlessAPIToken else { return false }
|
||||||
|
return !token.isEmpty
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Search Settings
|
// MARK: - Search Settings
|
||||||
|
|
||||||
var searchProvider: Settings.SearchProvider {
|
var searchProvider: Settings.SearchProvider {
|
||||||
|
|||||||
@@ -36,37 +36,44 @@ final class UpdateCheckService {
|
|||||||
var updateAvailable: Bool = false
|
var updateAvailable: Bool = false
|
||||||
var latestVersion: String? = nil
|
var latestVersion: String? = nil
|
||||||
|
|
||||||
private let apiURL = "https://gitlab.pm/api/v4/projects/rune%2Foai-swift/releases"
|
private let apiURL = "https://gitlab.pm/api/v1/repos/rune/oai-swift/releases/latest"
|
||||||
private let releasesURL = URL(string: "https://gitlab.pm/rune/oai-swift/releases")!
|
private let releasesURL = URL(string: "https://gitlab.pm/rune/oai-swift/releases")!
|
||||||
|
|
||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
/// Kick off a background update check. Silently does nothing on failure.
|
/// Kick off a background update check. Silently does nothing on failure.
|
||||||
func checkForUpdates() {
|
func checkForUpdates() {
|
||||||
Task {
|
Task.detached(priority: .background) {
|
||||||
await performCheck()
|
await self.performCheck()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor
|
|
||||||
private func performCheck() async {
|
private func performCheck() async {
|
||||||
guard let url = URL(string: apiURL) else { return }
|
guard let url = URL(string: apiURL) else { return }
|
||||||
|
|
||||||
var request = URLRequest(url: url)
|
var request = URLRequest(url: url)
|
||||||
request.timeoutInterval = 10
|
request.timeoutInterval = 10
|
||||||
|
|
||||||
guard let (data, _) = try? await URLSession.shared.data(for: request) else { return }
|
guard let (data, _) = try? await URLSession.shared.data(for: request) else {
|
||||||
guard let releases = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]],
|
Log.ui.warning("UpdateCheck: network request failed")
|
||||||
let latest = releases.first,
|
return
|
||||||
let tagName = latest["tag_name"] as? String else { return }
|
}
|
||||||
|
|
||||||
|
guard let release = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||||
|
let tagName = release["tag_name"] as? String else {
|
||||||
|
Log.ui.warning("UpdateCheck: unexpected API response — \(String(data: data, encoding: .utf8) ?? "<binary>")")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Strip leading "v" from tag (e.g. "v2.3.1" → "2.3.1")
|
// Strip leading "v" from tag (e.g. "v2.3.1" → "2.3.1")
|
||||||
let latestVer = tagName.hasPrefix("v") ? String(tagName.dropFirst()) : tagName
|
let latestVer = tagName.hasPrefix("v") ? String(tagName.dropFirst()) : tagName
|
||||||
let currentVer = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"
|
let currentVer = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "0.0.0"
|
||||||
|
|
||||||
if isNewer(latestVer, than: currentVer) {
|
if isNewer(latestVer, than: currentVer) {
|
||||||
self.latestVersion = latestVer
|
await MainActor.run {
|
||||||
self.updateAvailable = true
|
self.latestVersion = latestVer
|
||||||
|
self.updateAvailable = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class ChatViewModel {
|
|||||||
var modelInfoTarget: ModelInfo? = nil
|
var modelInfoTarget: ModelInfo? = nil
|
||||||
var commandHistory: [String] = []
|
var commandHistory: [String] = []
|
||||||
var historyIndex: Int = 0
|
var historyIndex: Int = 0
|
||||||
var isAutoContinuing: Bool = false
|
private var silentContinuePrompt: String? = nil
|
||||||
|
|
||||||
// Save tracking
|
// Save tracking
|
||||||
var currentConversationId: UUID? = nil
|
var currentConversationId: UUID? = nil
|
||||||
@@ -67,7 +67,6 @@ class ChatViewModel {
|
|||||||
let chatCount = messages.filter { $0.role != .system }.count
|
let chatCount = messages.filter { $0.role != .system }.count
|
||||||
return chatCount > 0 && chatCount != savedMessageCount
|
return chatCount > 0 && chatCount != savedMessageCount
|
||||||
}
|
}
|
||||||
var autoContinueCountdown: Int = 0
|
|
||||||
|
|
||||||
// MARK: - Auto-Save Tracking
|
// MARK: - Auto-Save Tracking
|
||||||
|
|
||||||
@@ -78,7 +77,6 @@ class ChatViewModel {
|
|||||||
// MARK: - Private State
|
// MARK: - Private State
|
||||||
|
|
||||||
private var streamingTask: Task<Void, Never>?
|
private var streamingTask: Task<Void, Never>?
|
||||||
private var autoContinueTask: Task<Void, Never>?
|
|
||||||
private let settings = SettingsService.shared
|
private let settings = SettingsService.shared
|
||||||
private let providerRegistry = ProviderRegistry.shared
|
private let providerRegistry = ProviderRegistry.shared
|
||||||
|
|
||||||
@@ -164,6 +162,13 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
// Otherwise, build the prompt: default + conditional sections + custom (if append mode)
|
// Otherwise, build the prompt: default + conditional sections + custom (if append mode)
|
||||||
var prompt = defaultSystemPrompt
|
var prompt = defaultSystemPrompt
|
||||||
|
|
||||||
|
// Prepend model identity to prevent models trained on Claude data from misidentifying themselves.
|
||||||
|
// Skip for direct Anthropic/OpenAI providers — those models know who they are.
|
||||||
|
if let model = selectedModel,
|
||||||
|
currentProvider != .anthropic && currentProvider != .openai {
|
||||||
|
prompt = "You are \(model.name).\n\n" + prompt
|
||||||
|
}
|
||||||
|
|
||||||
// Add tool-specific guidelines if MCP is enabled (tools are available)
|
// Add tool-specific guidelines if MCP is enabled (tools are available)
|
||||||
if mcpEnabled {
|
if mcpEnabled {
|
||||||
prompt += toolUsageGuidelines
|
prompt += toolUsageGuidelines
|
||||||
@@ -347,59 +352,21 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
streamingTask?.cancel()
|
streamingTask?.cancel()
|
||||||
streamingTask = nil
|
streamingTask = nil
|
||||||
isGenerating = false
|
isGenerating = false
|
||||||
cancelAutoContinue() // Also cancel any pending auto-continue
|
silentContinuePrompt = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startAutoContinue() {
|
func startAutoContinue() {
|
||||||
isAutoContinuing = true
|
showSystemMessage("↩ Continuing…")
|
||||||
autoContinueCountdown = 5
|
silentContinuePrompt = "Please continue from where you left off."
|
||||||
|
Task { @MainActor in
|
||||||
autoContinueTask = Task { @MainActor in
|
generateAIResponse(to: "", attachments: nil)
|
||||||
// Countdown from 5 to 1
|
|
||||||
for i in (1...5).reversed() {
|
|
||||||
if Task.isCancelled {
|
|
||||||
isAutoContinuing = false
|
|
||||||
return
|
|
||||||
}
|
|
||||||
autoContinueCountdown = i
|
|
||||||
try? await Task.sleep(for: .seconds(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Continue the task
|
|
||||||
isAutoContinuing = false
|
|
||||||
autoContinueCountdown = 0
|
|
||||||
|
|
||||||
let continuePrompt = "Please continue from where you left off."
|
|
||||||
|
|
||||||
// Add user message
|
|
||||||
let userMessage = Message(
|
|
||||||
role: .user,
|
|
||||||
content: continuePrompt,
|
|
||||||
tokens: nil,
|
|
||||||
cost: nil,
|
|
||||||
timestamp: Date(),
|
|
||||||
attachments: nil,
|
|
||||||
responseTime: nil,
|
|
||||||
wasInterrupted: false,
|
|
||||||
modelId: selectedModel?.id
|
|
||||||
)
|
|
||||||
messages.append(userMessage)
|
|
||||||
|
|
||||||
// Continue generation
|
|
||||||
generateAIResponse(to: continuePrompt, attachments: nil)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cancelAutoContinue() {
|
|
||||||
autoContinueTask?.cancel()
|
|
||||||
autoContinueTask = nil
|
|
||||||
isAutoContinuing = false
|
|
||||||
autoContinueCountdown = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func clearChat() {
|
func clearChat() {
|
||||||
messages.removeAll()
|
messages.removeAll()
|
||||||
sessionStats.reset()
|
sessionStats.reset()
|
||||||
|
MCPService.shared.resetBashSessionApproval()
|
||||||
showSystemMessage("Chat cleared")
|
showSystemMessage("Chat cleared")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,6 +379,7 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
|
|
||||||
messages.removeAll()
|
messages.removeAll()
|
||||||
sessionStats.reset()
|
sessionStats.reset()
|
||||||
|
MCPService.shared.resetBashSessionApproval()
|
||||||
messages = loadedMessages
|
messages = loadedMessages
|
||||||
|
|
||||||
// Rebuild session stats from loaded messages
|
// Rebuild session stats from loaded messages
|
||||||
@@ -435,6 +403,17 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Infer which provider owns a given model ID based on naming conventions.
|
/// Infer which provider owns a given model ID based on naming conventions.
|
||||||
|
/// Update the selected model and keep currentProvider + settings in sync.
|
||||||
|
/// Call this whenever the user picks a model in the model selector.
|
||||||
|
func selectModel(_ model: ModelInfo) {
|
||||||
|
let newProvider = inferProvider(from: model.id) ?? currentProvider
|
||||||
|
selectedModel = model
|
||||||
|
currentProvider = newProvider
|
||||||
|
settings.defaultModel = model.id
|
||||||
|
settings.defaultProvider = newProvider
|
||||||
|
MCPService.shared.resetBashSessionApproval()
|
||||||
|
}
|
||||||
|
|
||||||
private func inferProvider(from modelId: String) -> Settings.Provider? {
|
private func inferProvider(from modelId: String) -> Settings.Provider? {
|
||||||
// OpenRouter models always contain a "/" (e.g. "anthropic/claude-3-5-sonnet")
|
// OpenRouter models always contain a "/" (e.g. "anthropic/claude-3-5-sonnet")
|
||||||
if modelId.contains("/") { return .openrouter }
|
if modelId.contains("/") { return .openrouter }
|
||||||
@@ -767,8 +746,9 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
let mcp = MCPService.shared
|
let mcp = MCPService.shared
|
||||||
let mcpActive = mcpEnabled || settings.mcpEnabled
|
let mcpActive = mcpEnabled || settings.mcpEnabled
|
||||||
let anytypeActive = settings.anytypeMcpEnabled && settings.anytypeMcpConfigured
|
let anytypeActive = settings.anytypeMcpEnabled && settings.anytypeMcpConfigured
|
||||||
|
let bashActive = settings.bashEnabled
|
||||||
let modelSupportTools = selectedModel?.capabilities.tools ?? false
|
let modelSupportTools = selectedModel?.capabilities.tools ?? false
|
||||||
if modelSupportTools && (anytypeActive || (mcpActive && !mcp.allowedFolders.isEmpty)) {
|
if modelSupportTools && (anytypeActive || bashActive || (mcpActive && !mcp.allowedFolders.isEmpty)) {
|
||||||
generateAIResponseWithTools(provider: provider, modelId: modelId)
|
generateAIResponseWithTools(provider: provider, modelId: modelId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -881,7 +861,7 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
temperature: chatRequest.temperature,
|
temperature: chatRequest.temperature,
|
||||||
imageGeneration: true
|
imageGeneration: true
|
||||||
)
|
)
|
||||||
let response = try await provider.chat(request: nonStreamRequest)
|
let response = try await withOverloadedRetry { try await provider.chat(request: nonStreamRequest) }
|
||||||
let responseTime = Date().timeIntervalSince(startTime)
|
let responseTime = Date().timeIntervalSince(startTime)
|
||||||
|
|
||||||
if let index = messages.firstIndex(where: { $0.id == messageId }) {
|
if let index = messages.firstIndex(where: { $0.id == messageId }) {
|
||||||
@@ -1220,7 +1200,8 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
let startTime = Date()
|
let startTime = Date()
|
||||||
var wasCancelled = false
|
var wasCancelled = false
|
||||||
do {
|
do {
|
||||||
let tools = mcp.getToolSchemas()
|
// Include web_search tool when online mode is on (not needed for OpenRouter — it handles search via :online suffix)
|
||||||
|
let tools = mcp.getToolSchemas(onlineMode: onlineMode && currentProvider != .openrouter)
|
||||||
|
|
||||||
// Apply :online suffix for OpenRouter when online mode is active
|
// Apply :online suffix for OpenRouter when online mode is active
|
||||||
var effectiveModelId = modelId
|
var effectiveModelId = modelId
|
||||||
@@ -1259,20 +1240,6 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
? messages.filter { $0.role != .system }
|
? messages.filter { $0.role != .system }
|
||||||
: [messages.last(where: { $0.role == .user })].compactMap { $0 }
|
: [messages.last(where: { $0.role == .user })].compactMap { $0 }
|
||||||
|
|
||||||
// Web search via our WebSearchService
|
|
||||||
// Append results to last user message content (matching Python oAI approach)
|
|
||||||
if onlineMode && currentProvider != .openrouter {
|
|
||||||
if let lastUserIdx = messagesToSend.lastIndex(where: { $0.role == .user }) {
|
|
||||||
Log.search.info("Running web search for tool-aware path (\(currentProvider.displayName))")
|
|
||||||
let results = await WebSearchService.shared.search(query: messagesToSend[lastUserIdx].content)
|
|
||||||
if !results.isEmpty {
|
|
||||||
let searchContext = "\n\n\(WebSearchService.shared.formatResults(results))\n\nPlease use the above web search results to help answer the user's question."
|
|
||||||
messagesToSend[lastUserIdx].content += searchContext
|
|
||||||
Log.search.info("Injected \(results.count) search results into user message")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let systemPrompt: [String: Any] = [
|
let systemPrompt: [String: Any] = [
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": systemContent
|
"content": systemContent
|
||||||
@@ -1300,6 +1267,12 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
return ["role": msg.role.rawValue, "content": msg.content]
|
return ["role": msg.role.rawValue, "content": msg.content]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If this is a silent auto-continue, inject the prompt into the API call only
|
||||||
|
if let continuePrompt = silentContinuePrompt {
|
||||||
|
apiMessages.append(["role": "user", "content": continuePrompt])
|
||||||
|
silentContinuePrompt = nil
|
||||||
|
}
|
||||||
|
|
||||||
let maxIterations = 10 // Increased from 5 to reduce hitting client-side limit
|
let maxIterations = 10 // Increased from 5 to reduce hitting client-side limit
|
||||||
var finalContent = ""
|
var finalContent = ""
|
||||||
var totalUsage: ChatResponse.Usage?
|
var totalUsage: ChatResponse.Usage?
|
||||||
@@ -1311,13 +1284,15 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = try await provider.chatWithToolMessages(
|
let response = try await withOverloadedRetry {
|
||||||
model: effectiveModelId,
|
try await provider.chatWithToolMessages(
|
||||||
messages: apiMessages,
|
model: effectiveModelId,
|
||||||
tools: tools,
|
messages: apiMessages,
|
||||||
maxTokens: settings.maxTokens > 0 ? settings.maxTokens : nil,
|
tools: tools,
|
||||||
temperature: settings.temperature > 0 ? settings.temperature : nil
|
maxTokens: settings.maxTokens > 0 ? settings.maxTokens : nil,
|
||||||
)
|
temperature: settings.temperature > 0 ? settings.temperature : nil
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
if let usage = response.usage { totalUsage = usage }
|
if let usage = response.usage { totalUsage = usage }
|
||||||
|
|
||||||
@@ -1532,6 +1507,11 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
return "Server error. The provider may be experiencing issues. Try again shortly."
|
return "Server error. The provider may be experiencing issues. Try again shortly."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Overloaded / 529
|
||||||
|
if desc.contains("529") || desc.lowercased().contains("overloaded") {
|
||||||
|
return "API is overloaded. Please try again shortly."
|
||||||
|
}
|
||||||
|
|
||||||
// Timeout patterns
|
// Timeout patterns
|
||||||
if desc.lowercased().contains("timed out") || desc.lowercased().contains("timeout") {
|
if desc.lowercased().contains("timed out") || desc.lowercased().contains("timeout") {
|
||||||
return "Request timed out. Try a shorter message or different model."
|
return "Request timed out. Try a shorter message or different model."
|
||||||
@@ -1541,6 +1521,30 @@ Don't narrate future actions ("Let me...") - just use the tools.
|
|||||||
return desc
|
return desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retry an async operation on overloaded (529) errors with exponential backoff.
|
||||||
|
private func withOverloadedRetry<T>(maxAttempts: Int = 4, operation: () async throws -> T) async throws -> T {
|
||||||
|
var attempt = 0
|
||||||
|
while true {
|
||||||
|
do {
|
||||||
|
return try await operation()
|
||||||
|
} catch {
|
||||||
|
let desc = error.localizedDescription
|
||||||
|
let isOverloaded = desc.contains("529") || desc.lowercased().contains("overloaded")
|
||||||
|
attempt += 1
|
||||||
|
if isOverloaded && attempt < maxAttempts && !Task.isCancelled {
|
||||||
|
let delay = Double(1 << attempt) // 2s, 4s, 8s
|
||||||
|
Log.api.warning("API overloaded, retrying in \(Int(delay))s (attempt \(attempt)/\(maxAttempts - 1))...")
|
||||||
|
await MainActor.run {
|
||||||
|
showSystemMessage("⏳ API overloaded, retrying in \(Int(delay))s… (attempt \(attempt)/\(maxAttempts - 1))")
|
||||||
|
}
|
||||||
|
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
|
||||||
private func showModelInfo(_ model: ModelInfo) {
|
private func showModelInfo(_ model: ModelInfo) {
|
||||||
|
|||||||
@@ -81,40 +81,6 @@ struct ChatView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Auto-continue countdown banner
|
|
||||||
if viewModel.isAutoContinuing {
|
|
||||||
HStack(spacing: 12) {
|
|
||||||
ProgressView()
|
|
||||||
.progressViewStyle(.circular)
|
|
||||||
.scaleEffect(0.7)
|
|
||||||
|
|
||||||
VStack(alignment: .leading, spacing: 2) {
|
|
||||||
Text(ThinkingVerbs.random())
|
|
||||||
.font(.system(size: 13, weight: .medium))
|
|
||||||
.foregroundColor(.oaiPrimary)
|
|
||||||
Text("Continuing in \(viewModel.autoContinueCountdown)s")
|
|
||||||
.font(.system(size: 11))
|
|
||||||
.foregroundColor(.oaiSecondary)
|
|
||||||
}
|
|
||||||
|
|
||||||
Spacer()
|
|
||||||
|
|
||||||
Button("Cancel") {
|
|
||||||
viewModel.cancelAutoContinue()
|
|
||||||
}
|
|
||||||
.buttonStyle(.bordered)
|
|
||||||
.controlSize(.small)
|
|
||||||
.tint(.red)
|
|
||||||
}
|
|
||||||
.padding(.horizontal, 16)
|
|
||||||
.padding(.vertical, 10)
|
|
||||||
.background(Color.blue.opacity(0.1))
|
|
||||||
.overlay(
|
|
||||||
Rectangle()
|
|
||||||
.stroke(Color.blue.opacity(0.3), lineWidth: 1)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Input bar
|
// Input bar
|
||||||
InputBar(
|
InputBar(
|
||||||
text: $viewModel.inputText,
|
text: $viewModel.inputText,
|
||||||
@@ -140,6 +106,16 @@ struct ChatView: View {
|
|||||||
.sheet(isPresented: $viewModel.showSkills) {
|
.sheet(isPresented: $viewModel.showSkills) {
|
||||||
AgentSkillsView()
|
AgentSkillsView()
|
||||||
}
|
}
|
||||||
|
.sheet(item: Binding(
|
||||||
|
get: { MCPService.shared.pendingBashCommand },
|
||||||
|
set: { _ in }
|
||||||
|
)) { pending in
|
||||||
|
BashApprovalSheet(
|
||||||
|
pending: pending,
|
||||||
|
onApprove: { forSession in MCPService.shared.approvePendingBashCommand(forSession: forSession) },
|
||||||
|
onDeny: { MCPService.shared.denyPendingBashCommand() }
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,8 +63,7 @@ struct ContentView: View {
|
|||||||
selectedModel: chatViewModel.selectedModel,
|
selectedModel: chatViewModel.selectedModel,
|
||||||
onSelect: { model in
|
onSelect: { model in
|
||||||
let oldModel = chatViewModel.selectedModel
|
let oldModel = chatViewModel.selectedModel
|
||||||
chatViewModel.selectedModel = model
|
chatViewModel.selectModel(model)
|
||||||
SettingsService.shared.defaultModel = model.id
|
|
||||||
chatViewModel.showModelSelector = false
|
chatViewModel.showModelSelector = false
|
||||||
// Trigger auto-save on model switch
|
// Trigger auto-save on model switch
|
||||||
Task {
|
Task {
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ struct SyncStatusFooter: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func updateSyncStatus() {
|
private func updateSyncStatus() {
|
||||||
if let error = gitSync.lastSyncError {
|
if gitSync.lastSyncError != nil {
|
||||||
syncText = "Sync Error"
|
syncText = "Sync Error"
|
||||||
syncColor = .red
|
syncColor = .red
|
||||||
} else if gitSync.isSyncing {
|
} else if gitSync.isSyncing {
|
||||||
|
|||||||
128
oAI/Views/Screens/BashApprovalSheet.swift
Normal file
128
oAI/Views/Screens/BashApprovalSheet.swift
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
//
|
||||||
|
// BashApprovalSheet.swift
|
||||||
|
// oAI
|
||||||
|
//
|
||||||
|
// Approval UI for AI-requested bash commands
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
// Copyright (C) 2026 Rune Olsen
|
||||||
|
//
|
||||||
|
// This file is part of oAI.
|
||||||
|
//
|
||||||
|
// oAI is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as
|
||||||
|
// published by the Free Software Foundation, either version 3 of the
|
||||||
|
// License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// oAI is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General
|
||||||
|
// Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public
|
||||||
|
// License along with oAI. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
|
struct BashApprovalSheet: View {
|
||||||
|
let pending: MCPService.PendingBashCommand
|
||||||
|
let onApprove: (_ forSession: Bool) -> Void
|
||||||
|
let onDeny: () -> Void
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
VStack(alignment: .leading, spacing: 20) {
|
||||||
|
// Header
|
||||||
|
HStack(spacing: 12) {
|
||||||
|
Image(systemName: "terminal.fill")
|
||||||
|
.font(.title2)
|
||||||
|
.foregroundStyle(.orange)
|
||||||
|
VStack(alignment: .leading, spacing: 2) {
|
||||||
|
Text("Allow Shell Command?")
|
||||||
|
.font(.system(size: 17, weight: .semibold))
|
||||||
|
Text("The AI wants to run the following command")
|
||||||
|
.font(.system(size: 13))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command display
|
||||||
|
VStack(alignment: .leading, spacing: 6) {
|
||||||
|
Text("COMMAND")
|
||||||
|
.font(.system(size: 11, weight: .medium))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
ScrollView {
|
||||||
|
Text(pending.command)
|
||||||
|
.font(.system(size: 13, design: .monospaced))
|
||||||
|
.foregroundStyle(.primary)
|
||||||
|
.frame(maxWidth: .infinity, alignment: .leading)
|
||||||
|
.textSelection(.enabled)
|
||||||
|
.padding(12)
|
||||||
|
}
|
||||||
|
.frame(maxHeight: 180)
|
||||||
|
.background(Color.secondary.opacity(0.08))
|
||||||
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
||||||
|
.overlay(
|
||||||
|
RoundedRectangle(cornerRadius: 8)
|
||||||
|
.stroke(Color.secondary.opacity(0.2), lineWidth: 1)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Working directory
|
||||||
|
HStack(spacing: 6) {
|
||||||
|
Image(systemName: "folder")
|
||||||
|
.font(.system(size: 12))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
Text("Working directory:")
|
||||||
|
.font(.system(size: 12))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
Text(pending.workingDirectory)
|
||||||
|
.font(.system(size: 12, design: .monospaced))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Warning banner
|
||||||
|
HStack(alignment: .top, spacing: 8) {
|
||||||
|
Image(systemName: "exclamationmark.triangle.fill")
|
||||||
|
.foregroundStyle(.orange)
|
||||||
|
.font(.system(size: 13))
|
||||||
|
.padding(.top, 1)
|
||||||
|
Text("Shell commands have full access to your system. Only approve commands you understand and trust.")
|
||||||
|
.font(.system(size: 12))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
|
}
|
||||||
|
.padding(10)
|
||||||
|
.background(Color.orange.opacity(0.08))
|
||||||
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
||||||
|
|
||||||
|
// Buttons
|
||||||
|
HStack(spacing: 8) {
|
||||||
|
Button("Deny") {
|
||||||
|
onDeny()
|
||||||
|
}
|
||||||
|
.buttonStyle(.bordered)
|
||||||
|
.tint(.red)
|
||||||
|
.keyboardShortcut(.escape, modifiers: [])
|
||||||
|
|
||||||
|
Spacer()
|
||||||
|
|
||||||
|
Button("Allow Once") {
|
||||||
|
onApprove(false)
|
||||||
|
}
|
||||||
|
.buttonStyle(.bordered)
|
||||||
|
.tint(.orange)
|
||||||
|
|
||||||
|
Button("Allow for Session") {
|
||||||
|
onApprove(true)
|
||||||
|
}
|
||||||
|
.buttonStyle(.borderedProminent)
|
||||||
|
.tint(.orange)
|
||||||
|
.keyboardShortcut(.return, modifiers: [])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.padding(24)
|
||||||
|
.frame(width: 480)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,7 @@ struct CommandDetail: Identifiable {
|
|||||||
let brief: String
|
let brief: String
|
||||||
let detail: String
|
let detail: String
|
||||||
let examples: [String]
|
let examples: [String]
|
||||||
|
var shortcut: String? = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CommandCategory: Identifiable {
|
struct CommandCategory: Identifiable {
|
||||||
@@ -50,13 +51,15 @@ private let helpCategories: [CommandCategory] = [
|
|||||||
command: "/history",
|
command: "/history",
|
||||||
brief: "View command history",
|
brief: "View command history",
|
||||||
detail: "Opens a searchable modal showing all your previous messages with timestamps in European format (dd.MM.yyyy HH:mm:ss). Search by text content or date to find specific messages. Click any entry to reuse it.",
|
detail: "Opens a searchable modal showing all your previous messages with timestamps in European format (dd.MM.yyyy HH:mm:ss). Search by text content or date to find specific messages. Click any entry to reuse it.",
|
||||||
examples: ["/history"]
|
examples: ["/history"],
|
||||||
|
shortcut: "⌘H"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/clear",
|
command: "/clear",
|
||||||
brief: "Clear chat history",
|
brief: "Clear chat history",
|
||||||
detail: "Removes all messages from the current session and resets the conversation. This does not delete saved conversations.",
|
detail: "Removes all messages from the current session and resets the conversation. This does not delete saved conversations.",
|
||||||
examples: ["/clear"]
|
examples: ["/clear"],
|
||||||
|
shortcut: "⌘K"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/retry",
|
command: "/retry",
|
||||||
@@ -82,7 +85,8 @@ private let helpCategories: [CommandCategory] = [
|
|||||||
command: "/model",
|
command: "/model",
|
||||||
brief: "Select AI model",
|
brief: "Select AI model",
|
||||||
detail: "Opens the model selector to browse and choose from available models. The list depends on your active provider and API key.",
|
detail: "Opens the model selector to browse and choose from available models. The list depends on your active provider and API key.",
|
||||||
examples: ["/model"]
|
examples: ["/model"],
|
||||||
|
shortcut: "⌘M"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/provider [name]",
|
command: "/provider [name]",
|
||||||
@@ -114,13 +118,15 @@ private let helpCategories: [CommandCategory] = [
|
|||||||
command: "/load",
|
command: "/load",
|
||||||
brief: "Load saved conversation",
|
brief: "Load saved conversation",
|
||||||
detail: "Opens the conversation list to browse and load a previously saved conversation. Replaces the current chat.",
|
detail: "Opens the conversation list to browse and load a previously saved conversation. Replaces the current chat.",
|
||||||
examples: ["/load"]
|
examples: ["/load"],
|
||||||
|
shortcut: "⌘L"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/list",
|
command: "/list",
|
||||||
brief: "List saved conversations",
|
brief: "List saved conversations",
|
||||||
detail: "Opens the conversation list showing all saved conversations with their dates and message counts.",
|
detail: "Opens the conversation list showing all saved conversations with their dates and message counts.",
|
||||||
examples: ["/list"]
|
examples: ["/list"],
|
||||||
|
shortcut: "⌘L"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/delete <name>",
|
command: "/delete <name>",
|
||||||
@@ -178,7 +184,8 @@ private let helpCategories: [CommandCategory] = [
|
|||||||
command: "/config",
|
command: "/config",
|
||||||
brief: "Open settings",
|
brief: "Open settings",
|
||||||
detail: "Opens the settings panel where you can configure providers, API keys, MCP permissions, appearance, and more. Also available via /settings.",
|
detail: "Opens the settings panel where you can configure providers, API keys, MCP permissions, appearance, and more. Also available via /settings.",
|
||||||
examples: ["/config", "/settings"]
|
examples: ["/config", "/settings"],
|
||||||
|
shortcut: "⌘,"
|
||||||
),
|
),
|
||||||
CommandDetail(
|
CommandDetail(
|
||||||
command: "/stats",
|
command: "/stats",
|
||||||
@@ -461,6 +468,15 @@ private struct CommandRow: View {
|
|||||||
|
|
||||||
Spacer()
|
Spacer()
|
||||||
|
|
||||||
|
if let shortcut = command.shortcut {
|
||||||
|
Text(shortcut)
|
||||||
|
.font(.system(size: 11, weight: .medium))
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.padding(.horizontal, 6)
|
||||||
|
.padding(.vertical, 2)
|
||||||
|
.background(.quaternary, in: RoundedRectangle(cornerRadius: 5))
|
||||||
|
}
|
||||||
|
|
||||||
Image(systemName: "chevron.right")
|
Image(systemName: "chevron.right")
|
||||||
.font(.caption)
|
.font(.caption)
|
||||||
.foregroundStyle(.tertiary)
|
.foregroundStyle(.tertiary)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user