14 Commits

Author SHA1 Message Date
rune 52ab774785 Merge pull request '2.4.1' (#7) from 2.4.1 into main
Reviewed-on: #7
2026-07-14 11:01:35 +02:00
rune 5031dceff8 Merge remote-tracking branch 'origin/main' into 2.4.1
# Conflicts:
#	oAI.xcodeproj/project.pbxproj
2026-07-14 11:00:16 +02:00
rune 0cefef16e4 Change Command History shortcut from ⌘H to ⇧⌘H
⌘H is reserved by macOS for "Hide Application" and pre-empts app-level
menu bindings before they ever fire, so the Command History shortcut
never actually worked. Moved to ⇧⌘H and updated all references (in-app
help, macOS Help Book, README).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 10:46:53 +02:00
rune 8cba92d768 Document External MCP Servers, Personal Data Tools, and Research Agents in Help
Adds three new Help sections covering the 2.4.1 features: connecting
external stdio MCP servers, Calendar/Reminders/Location access with
its approval flow, and parallel read-only research sub-agents. Also
notes French as a supported language and that OpenRouter's dedicated
image models are merged into the model picker automatically. Cleans
up a stray misplaced HTML comment above the Anytype section.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 10:33:50 +02:00
rune bfcdd0164c Fix assistant message text truncating with "…" instead of wrapping
Two contributing layout issues in the chat message bubble:

1. MessageRow's content VStack (icon + text HStack) had no
   .frame(maxWidth: .infinity), so it sized to its content's ideal
   width instead of the space actually available.

2. swift-markdown-ui renders paragraphs with mixed inline styling
   (bold/italic runs next to plain text) as concatenated Text(+)
   segments, which on macOS report their ideal unwrapped single-line
   size for height purposes instead of wrapping — truncating with "…"
   regardless of window width. Plain single-style paragraphs (a single
   Text) weren't affected, which is why some lines wrapped fine and
   others didn't.

Fixed by adding .frame(maxWidth: .infinity, alignment: .leading) to
the MessageRow content stack, and .fixedSize(horizontal: false,
vertical: true) to the markdown paragraph label so height is
recomputed for the width actually given instead of the ideal width.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 08:38:39 +02:00
rune 7119cd1d06 Silence placeholder/continuing messages in the tool-call auto-retry path
The tool loop's max-iterations and empty-response fallbacks were showing
placeholder assistant bubbles ("[Tool loop reached maximum iterations]",
"[No response from the model — retrying]") followed by a "↩ Continuing…"
system message before silently re-running. None of that added anything
for the user, so the auto-continue now happens without any visible
message when there's no real content to show; genuine partial content
is still displayed as before, and usage/cost tracking is unaffected.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 08:05:07 +02:00
rune e6f965ff19 Add external MCP server support (stdio JSON-RPC)
Lets the AI connect to any external stdio MCP server (e.g. safaridriver
--mcp) configured in Settings, with tools auto-discovered and prefixed
by server slug. Includes crash detection with backoff restart (5s/15s/30s)
and a Settings UI to add/enable/disable/remove servers.

Fixes the temp-dir allowlist in MCPService.isPathAllowed to also match
/tmp and /private/tmp (not just NSTemporaryDirectory(), which resolves
to a different per-user Darwin temp dir) so the MCP file tools can
actually read files external servers and image generation write there.
Also switches the Add Server sheet's argument parsing to a quote-aware
tokenizer so args containing spaces survive intact.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-14 08:01:00 +02:00
rune f2949cea3b Add OpenRouter dedicated images API support
Fetches /api/v1/images/models in parallel with /models and merges results
into the model picker. Image-only models (e.g. Sourceful, Seedream, Flux
via this endpoint) were previously invisible since they don't appear in the
standard /models endpoint.

Models from the images API get usesImagesAPI=true and route through a new
generateImageAPIResponse() path in ChatViewModel that POSTs to /api/v1/images
with {model, prompt} instead of the chat completions endpoint. The response's
b64_json data is decoded and displayed via the existing GeneratedImagesView.

Cost is taken directly from the usage.cost field in the images API response
(USD per image) via a new rawCostUSD field on ChatResponse.Usage, bypassing
the token-based calculateCost() path used for chat models.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-29 14:34:56 +02:00
rune 40c5f25517 Add personal data tools, 2nd Brain trust toggle, and research agents
Personal Data Tools: native Calendar, Reminders, Contacts (hidden pending
Apple TCC fix in beta), and Location & Maps access via EventKit, Contacts
framework, and MapKit. Write actions (create event/reminder, complete
reminder) gate through an approval sheet. Four hardened-runtime entitlements
added to oAI.entitlements; Info.plist usage strings added for all services.
Personal Data section shows a β badge while Contacts is hidden.

2nd Brain always-trust: inline toggle on the Agent Skills row for the skill
named "2nd Brain" skips the bash approval dialog when the command contains
.brain_helper.py, gated by three runtime checks in MCPService.

Research agents: spawn_research_agents tool runs up to 5 concurrent read-only
sub-agents (read_file, list_directory, search_files, web_search — no write,
no bash, no nesting). Bounded by maxConcurrentAgents setting (default 3) and
a hard ceiling of 8 tasks. Added items field to Tool.Function.Parameters.Property
for JSON Schema array support; wired into AnthropicProvider.convertParametersToDict.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-29 14:04:47 +02:00
rune 454cef4193 Update AppLogo imageset
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-29 14:04:27 +02:00
rune 66c9054bd5 Update README with i18n disclosure and newly added features
Document Jarvis integration, Combine Conversations, model category
filter, sidebar navigation, prompt caching, and the 2nd Brain trust
toggle. Clarify that localization is AI/machine-translated rather than
reviewed by native speakers. Normalize all em-dashes to plain hyphens.
2026-06-22 11:14:34 +02:00
rune 56099c079c Fix accidental macOS 27 deployment target bump
MACOSX_DEPLOYMENT_TARGET was silently bumped 26.2 -> 27.0 in commit
8451db1, most likely by Xcode beta auto-updating it when the project
was opened/built with the macOS 27 beta SDK. This shipped in the public
v2.4 release, meaning the app refused to launch on anything older than
macOS 27 beta. No code in the project actually requires macOS 27 APIs.
2026-06-22 11:06:29 +02:00
rune 20121981a0 Add French localization and catch up nb/sv/da/de translations
French (fr) added as a 5th supported language; full catalog translated.
Also caught up nb/sv/da/de for ~300 strings added since the last
localization pass (Jarvis, Anytype, model categories, reasoning effort,
Combine Conversations) plus Button/Toggle/Menu/CommandMenu titles and
custom sectionHeader/row helpers in Settings that were never extracted
by prior tooling, leaving Settings and the View menu English-only
regardless of locale.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 11:17:36 +02:00
rune e8db4ad7a3 Merge pull request '2.4' (#6) from 2.4 into main
Reviewed-on: #6
2026-06-19 08:05:36 +02:00
31 changed files with 15030 additions and 131 deletions
+37 -23
View File
@@ -8,19 +8,20 @@ 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** - All Claude models - **Anthropic** - All Claude models; prompt caching support (direct API and via OpenRouter) reduces cost on repeated system prompts/context
- **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
### 💬 Core Chat Capabilities ### 💬 Core Chat Capabilities
- **Streaming Responses** - Real-time token streaming for faster interactions - **Streaming Responses** - Real-time token streaming for faster interactions
- **Conversation Management** - Save, load, export, and search conversations - **Conversation Management** - Save, load, export, and search conversations
- **Combine Conversations** - Merge 2+ saved conversations, either by chronological concatenation or AI-assisted synthesis
- **File Attachments** - Support for text files, images, and PDFs - **File Attachments** - Support for text files, images, and PDFs
- **Image Generation** - Create images with supported models (DALL-E, Flux, etc.) renders inline in chat - **Image Generation** - Create images with supported models (DALL-E, Flux, etc.) - renders inline in chat
- **Reasoning / Thinking Tokens** - Stream live reasoning from thinking-capable models (DeepSeek R1, Claude 3.7+, o1/o3, Qwen); configurable effort level (High/Medium/Low/Minimal); collapsible block auto-expands while thinking and collapses when the answer arrives - **Reasoning / Thinking Tokens** - Stream live reasoning from thinking-capable models (DeepSeek R1, Claude 3.7+, o1/o3, Qwen); configurable effort level (High/Medium/Low/Minimal); collapsible block auto-expands while thinking and collapses when the answer arrives
- **Online Mode** - DuckDuckGo and Google web search integration - **Online Mode** - DuckDuckGo and Google web search integration
- **Session Statistics** - Track token usage, costs, and response times - **Session Statistics** - Track token usage, costs, and response times
- **Command History** - Navigate previous commands with searchable modal (⌘H) - **Command History** - Navigate previous commands with searchable modal (⌘H)
### 🧠 Enhanced Memory & Context System ### 🧠 Enhanced Memory & Context System
- **Smart Context Selection** - Automatically select relevant messages to reduce token usage by 50-80% - **Smart Context Selection** - Automatically select relevant messages to reduce token usage by 50-80%
@@ -51,17 +52,25 @@ Seamless conversation backup and sync across devices:
### ⚡ Shortcuts & Agent Skills ### ⚡ Shortcuts & Agent Skills
- **Shortcuts** - Personal slash commands that expand to prompt templates; optional `{{input}}` placeholder for inline input - **Shortcuts** - Personal slash commands that expand to prompt templates; optional `{{input}}` placeholder for inline input
- **Agent Skills (SKILL.md)** - Markdown instruction files injected into the system prompt; compatible with skill0.io, skillsmp.com, and other SKILL.md marketplaces; import as `.md` or `.zip` bundle with attached data files - **Agent Skills (SKILL.md)** - Markdown instruction files injected into the system prompt; compatible with skill0.io, skillsmp.com, and other SKILL.md marketplaces; import as `.md` or `.zip` bundle with attached data files; a skill named exactly "2nd Brain" can be marked always-trusted, skipping the bash approval prompt for its helper-script calls
### 📚 Anytype Integration ### 📚 Anytype Integration
Connect oAI to your local [Anytype](https://anytype.io) knowledge base: Connect oAI to your local [Anytype](https://anytype.io) knowledge base:
- **Search** find objects by keyword across all spaces or within a specific one - **Search** - find objects by keyword across all spaces or within a specific one
- **Read** open any object and read its full markdown content - **Read** - open any object and read its full markdown content
- **Append** add content to the end of an existing object without touching existing text or internal links (preferred over full update) - **Append** - add content to the end of an existing object without touching existing text or internal links (preferred over full update)
- **Create** make new notes, tasks, or pages - **Create** - make new notes, tasks, or pages
- **Checkbox tools** surgically toggle to-do checkboxes or set task done/undone via native relation - **Checkbox tools** - surgically toggle to-do checkboxes or set task done/undone via native relation
- All data stays on your machine (local API, no cloud) - All data stays on your machine (local API, no cloud)
### 🛰️ Jarvis Integration
Connect oAI to a self-hosted [Jarvis](https://jarvis.pm) agent-automation server:
- **Agent Management** - List, create, edit, enable/disable, run, and stop agents
- **Run History** - Expandable per-run output with status and timing
- **Usage & Credits** - Per-agent usage stats and credits balance
- **Queue Control** - Pause/resume all agents
- `/jarvis` slash command opens the Jarvis panel directly
### 🖥️ Power-User Features ### 🖥️ Power-User Features
- **Bash Execution** - AI can run shell commands via `/bin/zsh` (opt-in, with per-command approval prompt) - **Bash Execution** - AI can run shell commands via `/bin/zsh` (opt-in, with per-command approval prompt)
- **iCloud Backup** - One-click settings backup to iCloud Drive; restore on any Mac; API keys excluded for security - **iCloud Backup** - One-click settings backup to iCloud Drive; restore on any Mac; API keys excluded for security
@@ -85,9 +94,10 @@ Automated email responses powered by AI:
- Footer stats display (messages, tokens, cost, sync status) - Footer stats display (messages, tokens, cost, sync status)
- Header status indicators (MCP, Online mode, Git sync) - Header status indicators (MCP, Online mode, Git sync)
- Responsive message layout with copy buttons - Responsive message layout with copy buttons
- **Model Selector (⌘M)** - Filter by capability (Vision / Tools / Online / Image Gen / Thinking 🧠), sort by price or context window, search by name or description, per-row ⓘ info button; ★ favourite any model favourites float to the top and can be filtered in one click - **Model Selector (⌘M)** - Filter by capability (Vision / Tools / Online / Image Gen / Thinking 🧠) or by category (Programming, Math, Medical, Translation, Roleplay, Creative, Science, Finance, Legal), sort by price or context window, search by name or description, per-row ⓘ info button; ★ favourite any model - favourites float to the top and can be filtered in one click
- **Default Model** - Set a fixed startup model in Settings → General; switching models during a session does not overwrite it - **Default Model** - Set a fixed startup model in Settings → General; switching models during a session does not overwrite it
- **Localization** - UI ~~fully translated~~ being translated into Norwegian Bokmål, Swedish, Danish, and German; follows macOS language preference automatically - **Sidebar Navigation** - Collapsible sidebar for switching between conversations
- **Localization** - Fully localized into Norwegian Bokmål, Swedish, Danish, German, and French; follows macOS language preference automatically. Translations are AI-generated (machine translation), not reviewed by native speakers - if you spot an awkward or incorrect phrase, please [open an issue](https://gitlab.pm/rune/oai-swift/issues/new)
![Advanced Features](Screenshots/4.png) ![Advanced Features](Screenshots/4.png)
@@ -97,8 +107,8 @@ Automated email responses powered by AI:
Download the latest release from the [Releases page](https://gitlab.pm/rune/oai-swift/releases). Two builds are available: Download the latest release from the [Releases page](https://gitlab.pm/rune/oai-swift/releases). Two builds are available:
- **oAI-x.x.x-AppleSilicon.dmg** for Macs with an Apple Silicon chip (M1 and later) - **oAI-x.x.x-AppleSilicon.dmg** - for Macs with an Apple Silicon chip (M1 and later)
- **oAI-x.x.x-Universal.dmg** runs natively on both Apple Silicon and Intel Macs - **oAI-x.x.x-Universal.dmg** - runs natively on both Apple Silicon and Intel Macs
### Installing from DMG ### Installing from DMG
@@ -107,19 +117,19 @@ Download the latest release from the [Releases page](https://gitlab.pm/rune/oai-
3. Eject the DMG 3. Eject the DMG
4. Launch oAI from Applications or Spotlight 4. Launch oAI from Applications or Spotlight
### First Launch Gatekeeper Warning ### First Launch - Gatekeeper Warning
oAI is **signed by the developer** but has **not yet been notarized by Apple**. Notarization is Apple's automated malware scan the app itself is safe, but macOS Gatekeeper may block it on first launch with a message saying the app "cannot be opened because the developer cannot be verified." oAI is **signed by the developer** but has **not yet been notarized by Apple**. Notarization is Apple's automated malware scan - the app itself is safe, but macOS Gatekeeper may block it on first launch with a message saying the app "cannot be opened because the developer cannot be verified."
To open the app, you have two options: To open the app, you have two options:
**Option A Right-click to open (quickest):** **Option A - Right-click to open (quickest):**
1. Right-click (or Control-click) `oAI.app` in Applications 1. Right-click (or Control-click) `oAI.app` in Applications
2. Select **Open** from the context menu 2. Select **Open** from the context menu
3. Click **Open** in the dialog that appears 3. Click **Open** in the dialog that appears
4. After doing this once, the app opens normally from then on 4. After doing this once, the app opens normally from then on
**Option B Remove the quarantine flag via Terminal:** **Option B - Remove the quarantine flag via Terminal:**
```bash ```bash
xattr -dr com.apple.quarantine /Applications/oAI.app xattr -dr com.apple.quarantine /Applications/oAI.app
@@ -139,7 +149,7 @@ Add your API keys in Settings (⌘,) → General tab:
- **Anthropic** - Get from [Anthropic Console](https://console.anthropic.com/) or use OAuth - **Anthropic** - Get from [Anthropic Console](https://console.anthropic.com/) or use OAuth
- **OpenRouter** - Get from [OpenRouter Keys](https://openrouter.ai/keys) - **OpenRouter** - Get from [OpenRouter Keys](https://openrouter.ai/keys)
- **Ollama** - Base URL (default: http://localhost:11434) - **Ollama** - Base URL (default: http://localhost:11434)
- **Google** - API key used for Google Custom Search (web search) and Google embeddings (semantic search) not a chat provider - **Google** - API key used for Google Custom Search (web search) and Google embeddings (semantic search) - not a chat provider
### Essential Settings ### Essential Settings
@@ -183,7 +193,7 @@ Add your API keys in Settings (⌘,) → General tab:
- `/load` or `/list` - List and load saved conversations (⌘L) - `/load` or `/list` - List and load saved conversations (⌘L)
- `/delete <name>` - Delete a saved conversation - `/delete <name>` - Delete a saved conversation
- `/export <md|json> [filename]` - Export conversation - `/export <md|json> [filename]` - Export conversation
- `/history` - Open command history modal (⌘H) - `/history` - Open command history modal (⌘H)
### Provider & Settings ### Provider & Settings
- `/provider [name]` - Switch or display current provider - `/provider [name]` - Switch or display current provider
@@ -228,7 +238,7 @@ Can you review this code? @~/project/main.swift
- `⌘,` - Open settings - `⌘,` - Open settings
- `⌘N` - New conversation - `⌘N` - New conversation
- `⌘L` - List saved conversations - `⌘L` - List saved conversations
- `⌘H` - Command history - `⌘H` - Command history
- `Esc` - Cancel generation / Close dropdown - `Esc` - Cancel generation / Close dropdown
- `↑/↓` - Navigate command dropdown (when typing `/`) - `↑/↓` - Navigate command dropdown (when typing `/`)
- `Return` - Send message - `Return` - Send message
@@ -313,12 +323,16 @@ AI-powered email auto-responder:
- [x] Vector index for faster semantic search (sqlite-vss) - [x] Vector index for faster semantic search (sqlite-vss)
- [x] Reasoning / thinking tokens (streamed live, collapsible) - [x] Reasoning / thinking tokens (streamed live, collapsible)
- [x] Localization (Norwegian Bokmål, Swedish, Danish, German) - [x] Localization (Norwegian Bokmål, Swedish, Danish, German, French)
- [x] iCloud Backup (settings export/restore) - [x] iCloud Backup (settings export/restore)
- [x] Bash execution with per-command approval - [x] Bash execution with per-command approval
- [x] Anytype integration (read, append, create, checkbox tools) - [x] Anytype integration (read, append, create, checkbox tools)
- [x] Model favourites (starred models, filter, float to top) - [x] Model favourites (starred models, filter, float to top)
- [ ] SOUL.md / USER.md — living identity documents injected into system prompt - [x] Jarvis integration (agent management, run history, usage/credits)
- [x] Model category filter (Programming, Math, Medical, etc.)
- [x] Combine saved conversations (concatenation or AI-assisted synthesis)
- [x] Sidebar navigation redesign
- [ ] SOUL.md / USER.md - living identity documents injected into system prompt
- [ ] Parallel research agents (read-only, concurrent) - [ ] Parallel research agents (read-only, concurrent)
- [ ] Local embeddings (sentence-transformers, $0 cost) - [ ] Local embeddings (sentence-transformers, $0 cost)
- [ ] Multi-modal conversation export (PDF, HTML) - [ ] Multi-modal conversation export (PDF, HTML)
@@ -346,7 +360,7 @@ See [LICENSE](LICENSE) for the full license text, or visit [gnu.org/licenses/agp
## Disclaimer ## Disclaimer
oAI takes real actions on your behalf it can send emails, write files, make calendar changes, and post Telegram messages. Review your whitelist and permission settings carefully before use. Content you send is processed by your configured AI provider (Anthropic, OpenRouter, or OpenAI). oAI-Web is provided "as is" without warranty of any kind the author accepts no responsibility for actions taken by the agent or any consequences thereof. See LICENSE for full terms. oAI takes real actions on your behalf - it can send emails, write files, make calendar changes, and post Telegram messages. Review your whitelist and permission settings carefully before use. Content you send is processed by your configured AI provider (Anthropic, OpenRouter, or OpenAI). oAI-Web is provided "as is" without warranty of any kind - the author accepts no responsibility for actions taken by the agent or any consequences thereof. See LICENSE for full terms.
--- ---
+15 -4
View File
@@ -104,6 +104,7 @@
da, da,
de, de,
sv, sv,
fr,
); );
mainGroup = A550A6592F3B72EA00136F2B; mainGroup = A550A6592F3B72EA00136F2B;
minimizedProjectReferenceProxies = 1; minimizedProjectReferenceProxies = 1;
@@ -269,6 +270,11 @@
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
ENABLE_USER_SELECTED_FILES = readonly; ENABLE_USER_SELECTED_FILES = readonly;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSCalendarsFullAccessUsageDescription = "oAI can read and create calendar events when you ask it to, if you enable Calendar access in Settings.";
INFOPLIST_KEY_NSContactsUsageDescription = "oAI can search your contacts when you ask it to, if you enable Contacts access in Settings.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "oAI can use your current location to answer questions, if you enable Location & Maps access in Settings.";
INFOPLIST_KEY_NSRemindersFullAccessUsageDescription = "oAI can read and create reminders when you ask it to, if you enable Reminders access in Settings.";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
@@ -282,8 +288,8 @@
IPHONEOS_DEPLOYMENT_TARGET = 27.0; IPHONEOS_DEPLOYMENT_TARGET = 27.0;
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 = 27.0; MACOSX_DEPLOYMENT_TARGET = 26.2;
MARKETING_VERSION = 2.4.0; MARKETING_VERSION = 2.4.1;
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;
@@ -313,6 +319,11 @@
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
ENABLE_USER_SELECTED_FILES = readonly; ENABLE_USER_SELECTED_FILES = readonly;
GENERATE_INFOPLIST_FILE = YES; GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.utilities";
INFOPLIST_KEY_NSCalendarsFullAccessUsageDescription = "oAI can read and create calendar events when you ask it to, if you enable Calendar access in Settings.";
INFOPLIST_KEY_NSContactsUsageDescription = "oAI can search your contacts when you ask it to, if you enable Contacts access in Settings.";
INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "oAI can use your current location to answer questions, if you enable Location & Maps access in Settings.";
INFOPLIST_KEY_NSRemindersFullAccessUsageDescription = "oAI can read and create reminders when you ask it to, if you enable Reminders access in Settings.";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES; "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphonesimulator*]" = YES;
"INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES; "INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents[sdk=iphoneos*]" = YES;
@@ -326,8 +337,8 @@
IPHONEOS_DEPLOYMENT_TARGET = 27.0; IPHONEOS_DEPLOYMENT_TARGET = 27.0;
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 = 27.0; MACOSX_DEPLOYMENT_TARGET = 26.2;
MARKETING_VERSION = 2.4.0; MARKETING_VERSION = 2.4.1;
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;
+8
View File
@@ -4,6 +4,14 @@
"filename" : "AppLogo.png", "filename" : "AppLogo.png",
"idiom" : "universal", "idiom" : "universal",
"scale" : "1x" "scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
} }
], ],
"info" : { "info" : {
+11657 -40
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -57,4 +57,10 @@ struct AgentSkill: Codable, Identifiable {
} }
return name return name
} }
/// Matches the user's "2nd Brain" skill by name there's no canonical skill ID,
/// so this is the only way to recognize it (used to gate the "always trust" bash setting).
var isSecondBrainSkill: Bool {
name.trimmingCharacters(in: .whitespacesAndNewlines).caseInsensitiveCompare("2nd Brain") == .orderedSame
}
} }
+1
View File
@@ -47,6 +47,7 @@ struct ModelInfo: Identifiable, Codable, Hashable {
let online: Bool // Web search let online: Bool // Web search
var imageGeneration: Bool = false // Image output var imageGeneration: Bool = false // Image output
var thinking: Bool = false // Reasoning/thinking tokens var thinking: Bool = false // Reasoning/thinking tokens
var usesImagesAPI: Bool = false // OpenRouter dedicated /images endpoint
} }
struct Architecture: Codable, Hashable { struct Architecture: Codable, Hashable {
+28 -1
View File
@@ -132,15 +132,19 @@ struct ChatResponse: Codable {
let totalTokens: Int let totalTokens: Int
let cacheCreationInputTokens: Int? let cacheCreationInputTokens: Int?
let cacheReadInputTokens: Int? let cacheReadInputTokens: Int?
/// Direct USD cost returned by the images API (bypasses token-based calculation).
let rawCostUSD: Double?
init(promptTokens: Int, completionTokens: Int, totalTokens: Int, cacheCreationInputTokens: Int? = nil, cacheReadInputTokens: Int? = nil) { init(promptTokens: Int, completionTokens: Int, totalTokens: Int, cacheCreationInputTokens: Int? = nil, cacheReadInputTokens: Int? = nil, rawCostUSD: Double? = nil) {
self.promptTokens = promptTokens self.promptTokens = promptTokens
self.completionTokens = completionTokens self.completionTokens = completionTokens
self.totalTokens = totalTokens self.totalTokens = totalTokens
self.cacheCreationInputTokens = cacheCreationInputTokens self.cacheCreationInputTokens = cacheCreationInputTokens
self.cacheReadInputTokens = cacheReadInputTokens self.cacheReadInputTokens = cacheReadInputTokens
self.rawCostUSD = rawCostUSD
} }
// rawCostUSD is set programmatically, never decoded from API responses
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens" case promptTokens = "prompt_tokens"
case completionTokens = "completion_tokens" case completionTokens = "completion_tokens"
@@ -148,6 +152,16 @@ struct ChatResponse: Codable {
case cacheCreationInputTokens = "cache_creation_input_tokens" case cacheCreationInputTokens = "cache_creation_input_tokens"
case cacheReadInputTokens = "cache_read_input_tokens" case cacheReadInputTokens = "cache_read_input_tokens"
} }
init(from decoder: Decoder) throws {
let c = try decoder.container(keyedBy: CodingKeys.self)
promptTokens = try c.decode(Int.self, forKey: .promptTokens)
completionTokens = try c.decode(Int.self, forKey: .completionTokens)
totalTokens = try c.decode(Int.self, forKey: .totalTokens)
cacheCreationInputTokens = try c.decodeIfPresent(Int.self, forKey: .cacheCreationInputTokens)
cacheReadInputTokens = try c.decodeIfPresent(Int.self, forKey: .cacheReadInputTokens)
rawCostUSD = nil
}
} }
// Custom Codable since ToolCallInfo/generatedImages are not from API directly // Custom Codable since ToolCallInfo/generatedImages are not from API directly
@@ -229,6 +243,19 @@ struct Tool: Codable {
let type: String let type: String
let description: String let description: String
let `enum`: [String]? let `enum`: [String]?
let items: Items?
/// Item schema for `type: "array"` properties (e.g. an array of strings).
struct Items: Codable {
let type: String
}
init(type: String, description: String, enum: [String]? = nil, items: Items? = nil) {
self.type = type
self.description = description
self.enum = `enum`
self.items = items
}
} }
} }
} }
+3
View File
@@ -753,6 +753,9 @@ class AnthropicProvider: AIProvider {
if let enumVals = prop.enum { if let enumVals = prop.enum {
propDict["enum"] = enumVals propDict["enum"] = enumVals
} }
if let items = prop.items {
propDict["items"] = ["type": items.type]
}
props[key] = propDict props[key] = propDict
} }
var dict: [String: Any] = [ var dict: [String: Any] = [
+61
View File
@@ -420,6 +420,67 @@ struct ToolResultMessage: Encodable {
} }
} }
// MARK: - Images API Model Discovery
struct OpenRouterImageModelsResponse: Codable {
let data: [ImageModelData]
struct ImageModelData: Codable {
let id: String
let name: String
let description: String?
let architecture: Architecture?
let supportsStreaming: Bool?
struct Architecture: Codable {
let inputModalities: [String]?
let outputModalities: [String]?
enum CodingKeys: String, CodingKey {
case inputModalities = "input_modalities"
case outputModalities = "output_modalities"
}
}
enum CodingKeys: String, CodingKey {
case id, name, description, architecture
case supportsStreaming = "supports_streaming"
}
}
}
// MARK: - Images API Generation Response
struct OpenRouterImageGenerationResponse: Codable {
let created: Int?
let data: [ImageData]
let usage: Usage?
struct ImageData: Codable {
let b64Json: String
let mediaType: String?
enum CodingKeys: String, CodingKey {
case b64Json = "b64_json"
case mediaType = "media_type"
}
}
struct Usage: Codable {
let promptTokens: Int
let completionTokens: Int
let totalTokens: Int
let cost: Double?
enum CodingKeys: String, CodingKey {
case promptTokens = "prompt_tokens"
case completionTokens = "completion_tokens"
case totalTokens = "total_tokens"
case cost
}
}
}
// MARK: - Error Response // MARK: - Error Response
struct OpenRouterErrorResponse: Codable { struct OpenRouterErrorResponse: Codable {
+110 -22
View File
@@ -53,30 +53,16 @@ class OpenRouterProvider: AIProvider {
func listModels() async throws -> [ModelInfo] { func listModels() async throws -> [ModelInfo] {
Log.api.info("Fetching model list from OpenRouter") Log.api.info("Fetching model list from OpenRouter")
let url = URL(string: "\(baseURL)/models")!
var request = URLRequest(url: url)
request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let (data, response) = try await session.data(for: request) // Fetch chat models and image models in parallel
async let chatData = fetchRaw(path: "/models")
async let imageData = fetchRaw(path: "/images/models")
let (chatRaw, imageRaw) = try await (chatData, imageData)
guard let httpResponse = response as? HTTPURLResponse else { let modelsResponse = try JSONDecoder().decode(OpenRouterModelsResponse.self, from: chatRaw)
Log.api.error("OpenRouter models: invalid response (not HTTP)") Log.api.info("OpenRouter loaded \(modelsResponse.data.count) chat models")
throw ProviderError.invalidResponse
}
guard httpResponse.statusCode == 200 else { var models = modelsResponse.data.map { modelData in
if let errorResponse = try? JSONDecoder().decode(OpenRouterErrorResponse.self, from: data) {
Log.api.error("OpenRouter models HTTP \(httpResponse.statusCode): \(errorResponse.error.message)")
throw ProviderError.unknown(errorResponse.error.message)
}
Log.api.error("OpenRouter models HTTP \(httpResponse.statusCode)")
throw ProviderError.unknown("HTTP \(httpResponse.statusCode)")
}
let modelsResponse = try JSONDecoder().decode(OpenRouterModelsResponse.self, from: data)
Log.api.info("OpenRouter loaded \(modelsResponse.data.count) models")
return modelsResponse.data.map { modelData in
let promptPrice = Double(modelData.pricing.prompt) ?? 0.0 let promptPrice = Double(modelData.pricing.prompt) ?? 0.0
let completionPrice = Double(modelData.pricing.completion) ?? 0.0 let completionPrice = Double(modelData.pricing.completion) ?? 0.0
@@ -129,8 +115,110 @@ class OpenRouterProvider: AIProvider {
) )
return info return info
} }
// Merge dedicated image models (these don't appear in /models)
if let imageModelsResponse = try? JSONDecoder().decode(OpenRouterImageModelsResponse.self, from: imageRaw) {
Log.api.info("OpenRouter loaded \(imageModelsResponse.data.count) image models")
let existingIds = Set(models.map { $0.id })
let imageModels = imageModelsResponse.data.compactMap { m -> ModelInfo? in
guard !existingIds.contains(m.id) else { return nil }
let acceptsImageInput = m.architecture?.inputModalities?.contains("image") ?? false
var info = ModelInfo(
id: m.id,
name: m.name,
description: m.description,
contextLength: 0,
pricing: ModelInfo.Pricing(prompt: 0, completion: 0),
capabilities: ModelInfo.ModelCapabilities(
vision: acceptsImageInput,
tools: false,
online: false,
imageGeneration: true,
thinking: false,
usesImagesAPI: true
),
topProvider: m.id.components(separatedBy: "/").first
)
info.categories = ModelCategory.infer(name: m.name, id: m.id, description: m.description)
return info
}
models.append(contentsOf: imageModels)
}
return models
} }
private func fetchRaw(path: String) async throws -> Data {
let url = URL(string: "\(baseURL)\(path)")!
var request = URLRequest(url: url)
request.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let (data, response) = try await session.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else { throw ProviderError.invalidResponse }
guard httpResponse.statusCode == 200 else {
if let err = try? JSONDecoder().decode(OpenRouterErrorResponse.self, from: data) {
throw ProviderError.unknown(err.error.message)
}
throw ProviderError.unknown("HTTP \(httpResponse.statusCode)")
}
return data
}
// MARK: - Images API
func generateImage(model: String, prompt: String) async throws -> ChatResponse {
Log.api.info("OpenRouter images API: model=\(model)")
let url = URL(string: "\(baseURL)/images")!
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.addValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("https://github.com/yourusername/oAI", forHTTPHeaderField: "HTTP-Referer")
urlRequest.addValue("oAI-Swift", forHTTPHeaderField: "X-Title")
urlRequest.httpBody = try JSONSerialization.data(withJSONObject: ["model": model, "prompt": prompt])
let (data, response) = try await session.data(for: urlRequest)
guard let httpResponse = response as? HTTPURLResponse else { throw ProviderError.invalidResponse }
if httpResponse.statusCode != 200 {
if let err = try? JSONDecoder().decode(OpenRouterErrorResponse.self, from: data) {
Log.api.error("OpenRouter images HTTP \(httpResponse.statusCode): \(err.error.message)")
throw ProviderError.unknown(err.error.message)
}
Log.api.error("OpenRouter images HTTP \(httpResponse.statusCode)")
throw ProviderError.unknown("HTTP \(httpResponse.statusCode)")
}
if let rawStr = String(data: data, encoding: .utf8) {
Log.api.debug("Images API raw response (first 200 chars): \(rawStr.prefix(200))")
}
let imageResponse = try JSONDecoder().decode(OpenRouterImageGenerationResponse.self, from: data)
let images: [Data] = imageResponse.data.compactMap { item in
Data(base64Encoded: item.b64Json)
}
let usage: ChatResponse.Usage? = imageResponse.usage.map { u in
ChatResponse.Usage(
promptTokens: u.promptTokens,
completionTokens: u.completionTokens,
totalTokens: u.totalTokens,
rawCostUSD: u.cost
)
}
return ChatResponse(
id: UUID().uuidString,
model: model,
content: "",
role: "assistant",
finishReason: "stop",
usage: usage,
created: Date(),
generatedImages: images.isEmpty ? nil : images
)
}
func getModel(_ id: String) async throws -> ModelInfo? { func getModel(_ id: String) async throws -> ModelInfo? {
let models = try await listModels() let models = try await listModels()
return models.first { $0.id == id } return models.first { $0.id == id }
@@ -36,6 +36,9 @@
<li><a href="#shortcuts">Shortcuts (Prompt Templates)</a></li> <li><a href="#shortcuts">Shortcuts (Prompt Templates)</a></li>
<li><a href="#agent-skills">Agent Skills (SKILL.md)</a></li> <li><a href="#agent-skills">Agent Skills (SKILL.md)</a></li>
<li><a href="#anytype">Anytype Integration</a></li> <li><a href="#anytype">Anytype Integration</a></li>
<li><a href="#external-mcp">External MCP Servers</a></li>
<li><a href="#personal-data">Personal Data Tools</a></li>
<li><a href="#research-agents">Research Agents</a></li>
<li><a href="#bash-execution">Bash Execution</a></li> <li><a href="#bash-execution">Bash Execution</a></li>
<li><a href="#icloud-backup">iCloud Backup</a></li> <li><a href="#icloud-backup">iCloud Backup</a></li>
<li><a href="#reasoning">Reasoning / Thinking Tokens</a></li> <li><a href="#reasoning">Reasoning / Thinking Tokens</a></li>
@@ -49,7 +52,7 @@
<!-- Getting Started --> <!-- Getting Started -->
<section id="getting-started"> <section id="getting-started">
<h2>Getting Started</h2> <h2>Getting Started</h2>
<p>oAI is a powerful AI chat assistant that connects to multiple AI providers including OpenAI, Anthropic, OpenRouter, and local models via Ollama. The app is available in English, Norwegian Bokmål, Swedish, Danish, and German — it follows your macOS language preference automatically.</p> <p>oAI is a powerful AI chat assistant that connects to multiple AI providers including OpenAI, Anthropic, OpenRouter, and local models via Ollama. The app is available in English, Norwegian Bokmål, Swedish, Danish, German, and French — it follows your macOS language preference automatically.</p>
<div class="steps"> <div class="steps">
<h3>Quick Start</h3> <h3>Quick Start</h3>
@@ -115,6 +118,10 @@
<li><strong>🧠 Thinking</strong> — models that support reasoning / thinking tokens</li> <li><strong>🧠 Thinking</strong> — models that support reasoning / thinking tokens</li>
</ul> </ul>
<div class="note">
<strong>Note:</strong> On OpenRouter, dedicated image-generation models (e.g. Sourceful, Seedream, Flux) are fetched from OpenRouter's separate images catalog and merged into the picker automatically — you don't need to configure anything extra to see them.
</div>
<h3>Sorting</h3> <h3>Sorting</h3>
<p>Click the <strong>↑↓ Sort</strong> button to sort the list by:</p> <p>Click the <strong>↑↓ Sort</strong> button to sort the list by:</p>
<ul> <ul>
@@ -1370,7 +1377,6 @@ Whenever the user asks you to translate something, translate it to Norwegian Bok
</div> </div>
</section> </section>
<!-- Keyboard Shortcuts -->
<!-- Anytype Integration --> <!-- Anytype Integration -->
<section id="anytype"> <section id="anytype">
<h2>Anytype Integration</h2> <h2>Anytype Integration</h2>
@@ -1413,6 +1419,104 @@ Whenever the user asks you to translate something, translate it to Norwegian Bok
</ul> </ul>
</section> </section>
<!-- External MCP Servers -->
<section id="external-mcp">
<h2>External MCP Servers</h2>
<p>Connect any external MCP server that speaks the stdio JSON-RPC protocol (for example <code>safaridriver --mcp</code>) and give the AI access to its tools — no custom integration code needed.</p>
<h3>Adding a Server</h3>
<ol>
<li>Press <kbd>⌘,</kbd> to open Settings</li>
<li>Go to the <strong>MCP</strong> tab → <strong>External MCP Servers</strong> section</li>
<li>Click <strong>Add Server…</strong></li>
<li>Enter a <strong>Name</strong> (used to prefix its tools, e.g. "Safari" → <code>safari_navigate_to_url</code>), the <strong>Command</strong> to launch it, and any <strong>Arguments</strong></li>
<li>Click <strong>Add</strong> — the server starts automatically and its tools are discovered</li>
</ol>
<div class="tip">
<strong>💡 Tip:</strong> Arguments containing spaces can be quoted, e.g. <code>--root "/Users/you/My Documents"</code>.
</div>
<h3>Server Status</h3>
<p>Each configured server shows a status dot and label:</p>
<ul>
<li><strong>🟢 Connected</strong> — running and its tools are available to the AI</li>
<li><strong>🟠 Connecting…</strong> — starting up or performing the initial handshake</li>
<li><strong>🔴 Error / Crashed</strong> — failed to start or exited unexpectedly</li>
<li><strong>⚪ Not started</strong> — disabled via the toggle</li>
</ul>
<p>Toggle a server off/on or delete it entirely with the trash icon. Crashed servers automatically restart up to 3 times with increasing delay (5s, 15s, 30s) before giving up.</p>
<div class="note">
<strong>Note:</strong> Tool names from every external server are prefixed with that server's slug (derived from its Name) so they never collide with oAI's built-in tools or each other.
</div>
</section>
<!-- Personal Data Tools -->
<section id="personal-data">
<h2>Personal Data Tools <span style="font-size: 0.75em; background: #f90; color: #fff; border-radius: 4px; padding: 1px 5px; vertical-align: middle;">Beta</span></h2>
<p>Let the AI access your Calendar, Reminders, and Location &amp; Maps to answer questions about your schedule and surroundings. Each service is opt-in and uses Apple's own frameworks (EventKit, CoreLocation, MapKit) with standard macOS permission prompts — nothing goes through a third-party service.</p>
<h3>Enabling a Service</h3>
<ol>
<li>Press <kbd>⌘,</kbd> to open Settings</li>
<li>Go to the <strong>MCP</strong> tab → <strong>Personal Data</strong> section</li>
<li>Toggle on the services you want: <strong>Calendar</strong>, <strong>Reminders</strong>, or <strong>Location &amp; Maps</strong></li>
<li>Click <strong>Request Access</strong> next to a service — macOS shows its standard permission prompt</li>
</ol>
<h3>What the AI Can Do</h3>
<ul>
<li><strong>Calendar</strong> — list your calendars and upcoming events, create new events</li>
<li><strong>Reminders</strong> — list reminder lists and items, create new reminders, mark reminders complete</li>
<li><strong>Location &amp; Maps</strong> — get your current location, search for places, geocode addresses, and get directions (all read-only)</li>
</ul>
<div class="warning">
<strong>⚠️ Write actions require approval:</strong> Creating a calendar event or reminder, or completing a reminder, shows an approval sheet first with a plain-language summary of what will happen. Choose <strong>Deny</strong>, <strong>Allow Once</strong>, or <strong>Allow for Session</strong>. Toggle this requirement off in Settings → MCP → Personal Data → Require Approval for Changes.
</div>
<div class="note">
<strong>Note:</strong> Contacts support exists internally but is currently hidden while a macOS permission bug affecting hardened-runtime apps is worked out on Apple's side.
</div>
<h3>Example Prompts</h3>
<ul>
<li>"What's on my calendar tomorrow?"</li>
<li>"Remind me to call the dentist on Friday at 2pm"</li>
<li>"How far is the nearest coffee shop from here?"</li>
</ul>
</section>
<!-- Research Agents -->
<section id="research-agents">
<h2>Research Agents</h2>
<p>For tasks that involve searching or reading many files, the AI can spawn several read-only research sub-agents that work in parallel instead of doing everything itself, one step at a time.</p>
<div class="warning">
<strong>⚠️ Cost warning:</strong> Each sub-agent runs its own full chain of model calls. A single request that spawns several agents can cost several times a normal reply. Leave this off unless you specifically want that tradeoff.
</div>
<h3>Enabling Research Agents</h3>
<ol>
<li>Press <kbd>⌘,</kbd> to open Settings</li>
<li>Go to the <strong>MCP</strong> tab → <strong>Research Agents</strong> section</li>
<li>Toggle <strong>Enable Research Agents</strong> on</li>
<li>Adjust <strong>Max Concurrent Agents</strong> (15, default 3) to control how many sub-agents can run at once</li>
</ol>
<h3>What Sub-Agents Can Do</h3>
<p>Sub-agents are intentionally limited to read-only investigation — they cannot write files, run shell commands, or spawn further sub-agents:</p>
<ul>
<li>Read file contents</li>
<li>List directory contents</li>
<li>Search for files</li>
<li>Search the web</li>
</ul>
<p class="note">Intended for genuinely independent research tasks (e.g. "compare these five files" or "look into three unrelated topics"), not everyday questions — the AI is instructed to reserve this for cases that actually benefit from parallelism.</p>
</section>
<section id="keyboard-shortcuts"> <section id="keyboard-shortcuts">
<h2>Keyboard Shortcuts</h2> <h2>Keyboard Shortcuts</h2>
<p>Work faster with these keyboard shortcuts.</p> <p>Work faster with these keyboard shortcuts.</p>
@@ -1431,7 +1535,7 @@ Whenever the user asks you to translate something, translate it to Norwegian Bok
<dt><kbd>⌘L</kbd></dt> <dt><kbd>⌘L</kbd></dt>
<dd>Browse Conversations</dd> <dd>Browse Conversations</dd>
<dt><kbd>⌘H</kbd></dt> <dt><kbd>⌘H</kbd></dt>
<dd>Command History</dd> <dd>Command History</dd>
<dt><kbd>⌘M</kbd></dt> <dt><kbd>⌘M</kbd></dt>
@@ -1490,6 +1594,9 @@ Whenever the user asks you to translate something, translate it to Norwegian Bok
<li>Enable/disable write, delete, move, and bash execution permissions</li> <li>Enable/disable write, delete, move, and bash execution permissions</li>
<li>Configure gitignore respect</li> <li>Configure gitignore respect</li>
<li><strong>Bash Execution</strong> — enable AI shell access, set working directory, timeout, and approval behaviour (see <a href="#bash-execution">Bash Execution</a>)</li> <li><strong>Bash Execution</strong> — enable AI shell access, set working directory, timeout, and approval behaviour (see <a href="#bash-execution">Bash Execution</a>)</li>
<li><strong>Research Agents</strong> — let the AI spawn parallel read-only research sub-agents (see <a href="#research-agents">Research Agents</a>)</li>
<li><strong>External MCP Servers</strong> — connect any stdio MCP server for additional tools (see <a href="#external-mcp">External MCP Servers</a>)</li>
<li><strong>Personal Data</strong> — Calendar, Reminders, and Location &amp; Maps access (see <a href="#personal-data">Personal Data Tools</a>)</li>
</ul> </ul>
<h3>Sync Tab</h3> <h3>Sync Tab</h3>
+250
View File
@@ -0,0 +1,250 @@
//
// ContactsService.swift
// oAI
//
// Read-only Contacts integration: search and "my card" lookup
//
// 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 Contacts
import Foundation
import os
@Observable
class ContactsService {
static let shared = ContactsService()
private let store = CNContactStore()
private let maxResults = 20
private let maxScan = 5000
private init() {}
// MARK: - Authorization
var authStatus: CNAuthorizationStatus {
CNContactStore.authorizationStatus(for: .contacts)
}
var authorized: Bool {
authStatus == .authorized
}
var accessState: PersonalDataAccessState {
let status = authStatus
Log.mcp.debug("ContactsService.accessState -> status=\(Self.describe(status)) (raw=\(status.rawValue))")
switch status {
case .authorized: return .granted
case .notDetermined: return .notDetermined
default: return .denied
}
}
@discardableResult
func requestAccess() async -> Bool {
let before = CNContactStore.authorizationStatus(for: .contacts)
Log.mcp.info("ContactsService.requestAccess: status before request = \(Self.describe(before)) (raw=\(before.rawValue))")
return await withCheckedContinuation { continuation in
store.requestAccess(for: .contacts) { granted, error in
let after = CNContactStore.authorizationStatus(for: .contacts)
if let error {
Log.mcp.error("ContactsService.requestAccess: error=\(error.localizedDescription); granted=\(granted); status after = \(Self.describe(after)) (raw=\(after.rawValue))")
} else {
Log.mcp.info("ContactsService.requestAccess: granted=\(granted); status after = \(Self.describe(after)) (raw=\(after.rawValue))")
}
continuation.resume(returning: granted)
}
}
}
nonisolated static func describe(_ status: CNAuthorizationStatus) -> String {
switch status {
case .notDetermined: return "notDetermined"
case .restricted: return "restricted"
case .denied: return "denied"
case .authorized: return "authorized"
case .limited: return "limited"
@unknown default: return "unknown"
}
}
// MARK: - Tool Schemas
func getToolSchemas() -> [Tool] {
[
makeTool(
name: "contacts_search",
description: "Search Contacts by name, phone number, or email address. Returns matching contacts with their phone numbers and emails. This does NOT match relationship labels like 'mother' or 'spouse' — for those, call contacts_get_me first to find the related person's name, then search for that name.",
properties: [
"query": prop("string", "Name, phone number, or email fragment to search for")
],
required: ["query"]
),
makeTool(
name: "contacts_get_me",
description: "Get the user's own contact card (\"My Card\" in Contacts.app), if configured. Includes any defined relationships (e.g. mother, spouse, child) with the related person's name — use contacts_search with that name to find their phone/email.",
properties: [:],
required: []
)
]
}
// MARK: - Tool Execution
func executeTool(name: String, arguments: String) async -> [String: Any] {
Log.mcp.info("Executing Contacts tool: \(name)")
guard authorized else {
return ["error": "Contacts permission not granted. Grant access in Settings > MCP."]
}
switch name {
case "contacts_search":
guard let data = arguments.data(using: .utf8),
let args = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let query = args["query"] as? String, !query.isEmpty else {
return ["error": "Missing required parameter: query"]
}
return search(query: query)
case "contacts_get_me":
return getMe()
default:
return ["error": "Unknown Contacts tool: \(name)"]
}
}
// MARK: - Implementation
private let keysToFetch: [CNKeyDescriptor] = [
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactOrganizationNameKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor,
CNContactEmailAddressesKey as CNKeyDescriptor,
CNContactRelationsKey as CNKeyDescriptor
]
private func search(query: String) -> [String: Any] {
var matches: [CNContact] = []
// Fast path: name predicate
let namePredicate = CNContact.predicateForContacts(matchingName: query)
if let nameMatches = try? store.unifiedContacts(matching: namePredicate, keysToFetch: keysToFetch) {
matches.append(contentsOf: nameMatches)
}
// Fallback: scan for phone/email substring matches
if matches.isEmpty {
let lowerQuery = query.lowercased()
let digitsQuery = query.filter(\.isNumber)
let request = CNContactFetchRequest(keysToFetch: keysToFetch)
var scanned = 0
try? store.enumerateContacts(with: request) { contact, stop in
scanned += 1
if scanned > self.maxScan || matches.count >= self.maxResults {
stop.pointee = true
return
}
let emailMatch = contact.emailAddresses.contains {
($0.value as String).lowercased().contains(lowerQuery)
}
let phoneMatch = !digitsQuery.isEmpty && contact.phoneNumbers.contains {
$0.value.stringValue.filter(\.isNumber).contains(digitsQuery)
}
if emailMatch || phoneMatch {
matches.append(contact)
}
}
}
let deduped = dedupContacts(matches)
let formatted = deduped.prefix(maxResults).map(formatContact)
return ["count": formatted.count, "contacts": Array(formatted)]
}
/// Collapses contacts that share a phone number or email Contacts.app's "linked contacts"
/// merge doesn't catch every real-world duplicate card, so do a best-effort merge here too.
private func dedupContacts(_ contacts: [CNContact]) -> [CNContact] {
var result: [CNContact] = []
outer: for contact in contacts {
let phones = Set(contact.phoneNumbers.map { $0.value.stringValue.filter(\.isNumber) })
let emails = Set(contact.emailAddresses.map { ($0.value as String).lowercased() })
for existing in result {
let existingPhones = Set(existing.phoneNumbers.map { $0.value.stringValue.filter(\.isNumber) })
let existingEmails = Set(existing.emailAddresses.map { ($0.value as String).lowercased() })
if !phones.isDisjoint(with: existingPhones) || !emails.isDisjoint(with: existingEmails) {
continue outer
}
}
result.append(contact)
}
return result
}
private func getMe() -> [String: Any] {
guard let me = try? store.unifiedMeContactWithKeys(toFetch: keysToFetch) else {
return ["error": "No 'My Card' is configured in Contacts.app"]
}
return formatContact(me)
}
private func formatContact(_ contact: CNContact) -> [String: Any] {
var item: [String: Any] = [
"given_name": contact.givenName,
"family_name": contact.familyName
]
if !contact.organizationName.isEmpty {
item["organization"] = contact.organizationName
}
if !contact.phoneNumbers.isEmpty {
item["phones"] = contact.phoneNumbers.map { $0.value.stringValue }
}
if !contact.emailAddresses.isEmpty {
item["emails"] = contact.emailAddresses.map { $0.value as String }
}
if !contact.contactRelations.isEmpty {
item["relations"] = contact.contactRelations.map { labeled -> [String: String] in
let label = labeled.label.map { CNLabeledValue<CNContactRelation>.localizedString(forLabel: $0) } ?? "relation"
return ["label": label, "name": labeled.value.name]
}
}
return item
}
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)
}
}
+593
View File
@@ -0,0 +1,593 @@
//
// EventKitService.swift
// oAI
//
// Calendar and Reminders integration via EventKit
//
// 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 EventKit
import Foundation
import os
/// Shared tri-state authorization status for the Settings UI across all Personal Data services.
/// `.denied` also covers `.restricted` and (for Calendar) `.writeOnly` states where the OS
/// will not show a request dialog again; the user must go to System Settings manually.
enum PersonalDataAccessState {
case notDetermined
case denied
case granted
}
@Observable
class EventKitService {
static let shared = EventKitService()
private let store = EKEventStore()
private init() {}
// MARK: - Authorization
var calendarAuthStatus: EKAuthorizationStatus {
let status = EKEventStore.authorizationStatus(for: .event)
Log.mcp.debug("EventKitService.calendarAuthStatus -> \(Self.describe(status)) (raw=\(status.rawValue))")
return status
}
var reminderAuthStatus: EKAuthorizationStatus {
let status = EKEventStore.authorizationStatus(for: .reminder)
Log.mcp.debug("EventKitService.reminderAuthStatus -> \(Self.describe(status)) (raw=\(status.rawValue))")
return status
}
var calendarAuthorized: Bool {
calendarAuthStatus == .fullAccess
}
var reminderAuthorized: Bool {
reminderAuthStatus == .fullAccess
}
var calendarAccessState: PersonalDataAccessState {
switch calendarAuthStatus {
case .fullAccess: return .granted
case .notDetermined: return .notDetermined
default: return .denied // .denied, .restricted, .writeOnly (no read access for our tools)
}
}
var reminderAccessState: PersonalDataAccessState {
switch reminderAuthStatus {
case .fullAccess: return .granted
case .notDetermined: return .notDetermined
default: return .denied
}
}
@discardableResult
func requestCalendarAccess() async -> Bool {
let before = EKEventStore.authorizationStatus(for: .event)
Log.mcp.info("requestCalendarAccess: status before request = \(Self.describe(before)) (raw=\(before.rawValue))")
do {
let granted = try await store.requestFullAccessToEvents()
let after = EKEventStore.authorizationStatus(for: .event)
Log.mcp.info("requestCalendarAccess: API returned granted=\(granted); status after request = \(Self.describe(after)) (raw=\(after.rawValue))")
return granted
} catch {
let after = EKEventStore.authorizationStatus(for: .event)
Log.mcp.error("requestCalendarAccess: threw error: \(error.localizedDescription); status after = \(Self.describe(after)) (raw=\(after.rawValue))")
return false
}
}
@discardableResult
func requestReminderAccess() async -> Bool {
let before = EKEventStore.authorizationStatus(for: .reminder)
Log.mcp.info("requestReminderAccess: status before request = \(Self.describe(before)) (raw=\(before.rawValue))")
do {
let granted = try await store.requestFullAccessToReminders()
let after = EKEventStore.authorizationStatus(for: .reminder)
Log.mcp.info("requestReminderAccess: API returned granted=\(granted); status after request = \(Self.describe(after)) (raw=\(after.rawValue))")
return granted
} catch {
let after = EKEventStore.authorizationStatus(for: .reminder)
Log.mcp.error("requestReminderAccess: threw error: \(error.localizedDescription); status after = \(Self.describe(after)) (raw=\(after.rawValue))")
return false
}
}
nonisolated static func describe(_ status: EKAuthorizationStatus) -> String {
switch status {
case .notDetermined: return "notDetermined"
case .restricted: return "restricted"
case .denied: return "denied"
case .fullAccess: return "fullAccess"
case .writeOnly: return "writeOnly"
@unknown default: return "unknown"
}
}
// MARK: - Tool Schemas
func getToolSchemas(calendarEnabled: Bool, remindersEnabled: Bool) -> [Tool] {
var tools: [Tool] = []
if calendarEnabled {
tools.append(makeTool(
name: "calendar_list_calendars",
description: "List all calendars available on this Mac (e.g. iCloud, Work, Home).",
properties: [:],
required: []
))
tools.append(makeTool(
name: "calendar_list_events",
description: "List calendar events within a date range. Dates are ISO8601 (e.g. '2026-06-20T00:00:00' or '2026-06-20'). Range is limited to 1 year. For open-ended queries like 'next appointment' or 'upcoming events', do NOT limit the range to just today — use a generous forward-looking window (e.g. today through +90 days) so you don't miss events further out.",
properties: [
"start_date": prop("string", "Start of the date range (ISO8601)"),
"end_date": prop("string", "End of the date range (ISO8601)"),
"calendar_name": prop("string", "Optional: only list events from this calendar")
],
required: ["start_date", "end_date"]
))
tools.append(makeTool(
name: "calendar_create_event",
description: "Create a new calendar event. Requires user approval before it is actually created.",
properties: [
"title": prop("string", "Event title"),
"start_date": prop("string", "Start date/time (ISO8601, e.g. '2026-06-20T14:00:00')"),
"end_date": prop("string", "End date/time (ISO8601)"),
"calendar_name": prop("string", "Optional: calendar to add the event to (defaults to the system default calendar)"),
"location": prop("string", "Optional: event location text"),
"notes": prop("string", "Optional: event notes"),
"all_day": prop("boolean", "Optional: whether this is an all-day event (default: false)"),
"alarm_minutes_before": prop("number", "Optional: minutes before the start time to show an alert")
],
required: ["title", "start_date", "end_date"]
))
}
if remindersEnabled {
tools.append(makeTool(
name: "reminders_list_lists",
description: "List all reminder lists available on this Mac.",
properties: [:],
required: []
))
tools.append(makeTool(
name: "reminders_list",
description: "List reminders. Omit list_name to search across ALL reminder lists in a single call — prefer this over calling once per list. Incomplete reminders only unless include_completed is true.",
properties: [
"list_name": prop("string", "Optional: only list reminders from this one list (omit to search all lists at once)"),
"include_completed": prop("boolean", "Optional: include completed reminders (default: false)")
],
required: []
))
tools.append(makeTool(
name: "reminders_create",
description: "Create a new reminder. Requires user approval before it is actually created.",
properties: [
"title": prop("string", "Reminder title"),
"list_name": prop("string", "Optional: reminder list to add to (defaults to the system default list)"),
"due_date": prop("string", "Optional: due date/time (ISO8601)"),
"priority": prop("string", "Optional: priority level", enumValues: ["none", "low", "medium", "high"]),
"notes": prop("string", "Optional: reminder notes")
],
required: ["title"]
))
tools.append(makeTool(
name: "reminders_complete",
description: "Mark a reminder as completed. Requires user approval. Use reminders_list to find the reminder_id first.",
properties: [
"reminder_id": prop("string", "The reminder's identifier, from reminders_list")
],
required: ["reminder_id"]
))
}
return tools
}
// MARK: - Read Tool Execution
func executeTool(name: String, arguments: String) async -> [String: Any] {
Log.mcp.info("Executing EventKit tool: \(name)")
let args = Self.parseArgs(arguments)
switch name {
case "calendar_list_calendars":
guard calendarAuthorized else { return Self.permissionError(domain: "Calendar") }
return listCalendars()
case "calendar_list_events":
guard calendarAuthorized else { return Self.permissionError(domain: "Calendar") }
guard let startStr = args["start_date"] as? String, let start = Self.parseDate(startStr) else {
return ["error": "Missing or invalid parameter: start_date"]
}
guard let endStr = args["end_date"] as? String, let end = Self.parseDate(endStr) else {
return ["error": "Missing or invalid parameter: end_date"]
}
let calendarName = args["calendar_name"] as? String
return listEvents(start: start, end: end, calendarName: calendarName)
case "reminders_list_lists":
guard reminderAuthorized else { return Self.permissionError(domain: "Reminders") }
return listReminderLists()
case "reminders_list":
guard reminderAuthorized else { return Self.permissionError(domain: "Reminders") }
let listName = args["list_name"] as? String
let includeCompleted = args["include_completed"] as? Bool ?? false
return await listReminders(listName: listName, includeCompleted: includeCompleted)
default:
return ["error": "Unknown EventKit tool: \(name)"]
}
}
// MARK: - Write Tool Execution (called only after approval)
func executeWriteTool(name: String, arguments: String) async -> [String: Any] {
Log.mcp.info("Executing EventKit write tool: \(name)")
let args = Self.parseArgs(arguments)
switch name {
case "calendar_create_event":
guard calendarAuthorized else { return Self.permissionError(domain: "Calendar") }
return createEvent(args: args)
case "reminders_create":
guard reminderAuthorized else { return Self.permissionError(domain: "Reminders") }
return createReminder(args: args)
case "reminders_complete":
guard reminderAuthorized else { return Self.permissionError(domain: "Reminders") }
guard let reminderId = args["reminder_id"] as? String else {
return ["error": "Missing required parameter: reminder_id"]
}
return completeReminder(reminderId: reminderId)
default:
return ["error": "Unknown EventKit write tool: \(name)"]
}
}
// MARK: - Approval Summary
/// Human-readable description shown in the approval sheet before a write tool runs.
func approvalSummary(forTool name: String, arguments: String) -> String {
let args = Self.parseArgs(arguments)
switch name {
case "calendar_create_event":
let title = args["title"] as? String ?? "Untitled event"
let start = (args["start_date"] as? String).flatMap(Self.parseDate) ?? Date()
let end = (args["end_date"] as? String).flatMap(Self.parseDate) ?? start
return "Create calendar event \"\(title)\" from \(Self.displayFormatter.string(from: start)) to \(Self.displayFormatter.string(from: end))"
case "reminders_create":
let title = args["title"] as? String ?? "Untitled reminder"
if let dueStr = args["due_date"] as? String, let due = Self.parseDate(dueStr) {
return "Create reminder \"\(title)\" due \(Self.displayFormatter.string(from: due))"
}
return "Create reminder \"\(title)\""
case "reminders_complete":
return "Mark reminder as completed"
default:
return "Perform action: \(name)"
}
}
// MARK: - Calendar Read Implementations
private func listCalendars() -> [String: Any] {
let calendars = store.calendars(for: .event).map { cal -> [String: Any] in
[
"name": cal.title,
"type": calendarTypeDescription(cal),
"allows_modifications": cal.allowsContentModifications
]
}
return ["calendars": calendars]
}
private func listEvents(start: Date, end: Date, calendarName: String?) -> [String: Any] {
guard end > start else { return ["error": "end_date must be after start_date"] }
guard end.timeIntervalSince(start) <= 366 * 24 * 60 * 60 else {
return ["error": "Date range too large — limit to 1 year or less"]
}
var calendars = store.calendars(for: .event)
if let calendarName {
calendars = calendars.filter { $0.title.caseInsensitiveCompare(calendarName) == .orderedSame }
if calendars.isEmpty {
return ["error": "No calendar found named '\(calendarName)'"]
}
}
let predicate = store.predicateForEvents(withStart: start, end: end, calendars: calendars)
let events = store.events(matching: predicate)
.sorted { $0.startDate < $1.startDate }
.prefix(200)
.map { event -> [String: Any] in
var item: [String: Any] = [
"id": event.eventIdentifier ?? "",
"title": event.title ?? "Untitled",
"start": Self.isoFormatter.string(from: event.startDate),
"end": Self.isoFormatter.string(from: event.endDate),
"all_day": event.isAllDay,
"calendar": event.calendar?.title ?? ""
]
if let location = event.location, !location.isEmpty {
item["location"] = location
}
if let notes = event.notes, !notes.isEmpty {
item["notes"] = String(notes.prefix(500))
}
return item
}
return ["count": events.count, "events": Array(events)]
}
private func createEvent(args: [String: Any]) -> [String: Any] {
guard let title = args["title"] as? String, !title.isEmpty else {
return ["error": "Missing required parameter: title"]
}
guard let startStr = args["start_date"] as? String, let start = Self.parseDate(startStr) else {
return ["error": "Missing or invalid parameter: start_date"]
}
guard let endStr = args["end_date"] as? String, let end = Self.parseDate(endStr) else {
return ["error": "Missing or invalid parameter: end_date"]
}
guard end >= start else {
return ["error": "end_date must not be before start_date"]
}
let event = EKEvent(eventStore: store)
event.title = title
event.startDate = start
event.endDate = end
event.isAllDay = args["all_day"] as? Bool ?? false
if let calendarName = args["calendar_name"] as? String,
let calendar = store.calendars(for: .event).first(where: { $0.title.caseInsensitiveCompare(calendarName) == .orderedSame }) {
event.calendar = calendar
} else if let defaultCalendar = store.defaultCalendarForNewEvents {
event.calendar = defaultCalendar
} else {
guard let fallback = store.calendars(for: .event).first(where: { $0.allowsContentModifications }) else {
return ["error": "No writable calendar available"]
}
event.calendar = fallback
}
if let location = args["location"] as? String { event.location = location }
if let notes = args["notes"] as? String { event.notes = notes }
if let minutesBefore = (args["alarm_minutes_before"] as? Double) ?? (args["alarm_minutes_before"] as? Int).map(Double.init) {
event.addAlarm(EKAlarm(relativeOffset: -(minutesBefore * 60)))
}
do {
try store.save(event, span: .thisEvent, commit: true)
return ["success": true, "event_id": event.eventIdentifier ?? "", "calendar": event.calendar?.title ?? ""]
} catch {
Log.mcp.error("calendar_create_event failed: \(error.localizedDescription)")
return ["error": "Failed to create event: \(error.localizedDescription)"]
}
}
// MARK: - Reminders Read Implementations
private func listReminderLists() -> [String: Any] {
let lists = store.calendars(for: .reminder).map { cal -> [String: Any] in
["name": cal.title, "allows_modifications": cal.allowsContentModifications]
}
return ["lists": lists]
}
private func listReminders(listName: String?, includeCompleted: Bool) async -> [String: Any] {
var lists = store.calendars(for: .reminder)
if let listName {
lists = lists.filter { $0.title.caseInsensitiveCompare(listName) == .orderedSame }
if lists.isEmpty {
return ["error": "No reminder list found named '\(listName)'"]
}
}
let predicate = store.predicateForReminders(in: lists)
let reminders: [EKReminder] = await withCheckedContinuation { continuation in
store.fetchReminders(matching: predicate) { results in
continuation.resume(returning: results ?? [])
}
}
let filtered = reminders
.filter { includeCompleted || !$0.isCompleted }
.sorted { lhs, rhs in
let l = lhs.dueDateComponents?.date ?? .distantFuture
let r = rhs.dueDateComponents?.date ?? .distantFuture
return l < r
}
.prefix(200)
.map { reminder -> [String: Any] in
var item: [String: Any] = [
"id": reminder.calendarItemIdentifier,
"title": reminder.title ?? "Untitled",
"completed": reminder.isCompleted,
"list": reminder.calendar?.title ?? ""
]
if let due = reminder.dueDateComponents?.date {
item["due"] = Self.isoFormatter.string(from: due)
}
if reminder.priority > 0 {
item["priority"] = priorityDescription(reminder.priority)
}
if let notes = reminder.notes, !notes.isEmpty {
item["notes"] = String(notes.prefix(500))
}
return item
}
return ["count": filtered.count, "reminders": Array(filtered)]
}
private func createReminder(args: [String: Any]) -> [String: Any] {
guard let title = args["title"] as? String, !title.isEmpty else {
return ["error": "Missing required parameter: title"]
}
let reminder = EKReminder(eventStore: store)
reminder.title = title
if let listName = args["list_name"] as? String,
let list = store.calendars(for: .reminder).first(where: { $0.title.caseInsensitiveCompare(listName) == .orderedSame }) {
reminder.calendar = list
} else if let defaultList = store.defaultCalendarForNewReminders() {
reminder.calendar = defaultList
} else {
guard let fallback = store.calendars(for: .reminder).first(where: { $0.allowsContentModifications }) else {
return ["error": "No writable reminder list available"]
}
reminder.calendar = fallback
}
if let dueStr = args["due_date"] as? String, let due = Self.parseDate(dueStr) {
reminder.dueDateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: due)
}
if let notes = args["notes"] as? String { reminder.notes = notes }
if let priority = args["priority"] as? String { reminder.priority = priorityValue(priority) }
do {
try store.save(reminder, commit: true)
return ["success": true, "reminder_id": reminder.calendarItemIdentifier, "list": reminder.calendar?.title ?? ""]
} catch {
Log.mcp.error("reminders_create failed: \(error.localizedDescription)")
return ["error": "Failed to create reminder: \(error.localizedDescription)"]
}
}
private func completeReminder(reminderId: String) -> [String: Any] {
guard let item = store.calendarItem(withIdentifier: reminderId) as? EKReminder else {
return ["error": "No reminder found with id '\(reminderId)'"]
}
item.isCompleted = true
item.completionDate = Date()
do {
try store.save(item, commit: true)
return ["success": true, "reminder_id": reminderId, "title": item.title ?? ""]
} catch {
Log.mcp.error("reminders_complete failed: \(error.localizedDescription)")
return ["error": "Failed to complete reminder: \(error.localizedDescription)"]
}
}
// MARK: - Helpers
private func calendarTypeDescription(_ cal: EKCalendar) -> String {
switch cal.type {
case .local: return "local"
case .calDAV: return "caldav"
case .exchange: return "exchange"
case .subscription: return "subscription"
case .birthday: return "birthday"
@unknown default: return "unknown"
}
}
private func priorityDescription(_ value: Int) -> String {
switch value {
case 1...4: return "high"
case 5: return "medium"
case 6...9: return "low"
default: return "none"
}
}
private func priorityValue(_ description: String) -> Int {
switch description.lowercased() {
case "high": return 1
case "medium": return 5
case "low": return 9
default: return 0
}
}
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, enumValues: [String]? = nil) -> Tool.Function.Parameters.Property {
Tool.Function.Parameters.Property(type: type, description: description, enum: enumValues)
}
nonisolated static func parseArgs(_ arguments: String) -> [String: Any] {
guard let data = arguments.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return [:]
}
return dict
}
nonisolated static func permissionError(domain: String) -> [String: Any] {
["error": "\(domain) permission not granted. Grant access in Settings > MCP."]
}
nonisolated(unsafe) static let isoFormatter: ISO8601DateFormatter = {
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
return f
}()
nonisolated static let displayFormatter: DateFormatter = {
let f = DateFormatter()
f.dateStyle = .medium
f.timeStyle = .short
return f
}()
nonisolated static func parseDate(_ string: String) -> Date? {
if let date = isoFormatter.date(from: string) { return date }
let isoNoTimezone = ISO8601DateFormatter()
isoNoTimezone.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
if let date = isoNoTimezone.date(from: string) { return date }
let localDateTime = DateFormatter()
localDateTime.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let date = localDateTime.date(from: string) { return date }
let dateOnly = DateFormatter()
dateOnly.dateFormat = "yyyy-MM-dd"
if let date = dateOnly.date(from: string) { return date }
return nil
}
}
+280
View File
@@ -0,0 +1,280 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Rune Olsen
import Foundation
// MARK: - ExternalMCPClient
/// Manages one MCP stdio server process. All state is MainActor-isolated
/// (consistent with SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor project setting).
/// Background I/O runs in Task.detached; state mutations hop back to MainActor.
@MainActor
final class ExternalMCPClient {
let server: ExternalMCPServer
weak var stateDelegate: (any ExternalMCPStateDelegate)?
private var process: Process?
private var stdinHandle: FileHandle?
private var readTask: Task<Void, Never>?
private var stderrTask: Task<Void, Never>?
private var nextRequestId: Int = 1
private var pendingCalls: [Int: CheckedContinuation<Data, Error>] = [:]
private var lineBuffer = Data()
private(set) var state: MCPClientState = .idle
private(set) var discoveredTools: [MCPToolDefinition] = []
init(server: ExternalMCPServer, stateDelegate: (any ExternalMCPStateDelegate)?) {
self.server = server
self.stateDelegate = stateDelegate
}
// MARK: - Lifecycle
func start() async throws {
guard state == .idle || state == .stopped || state == .crashed else { return }
state = .connecting
stateDelegate?.clientDidChangeState(id: server.id, state: .connecting)
let proc = Process()
if server.command.hasPrefix("/") {
proc.executableURL = URL(fileURLWithPath: server.command)
proc.arguments = server.args
} else {
proc.executableURL = URL(fileURLWithPath: "/usr/bin/env")
proc.arguments = [server.command] + server.args
}
proc.environment = ProcessInfo.processInfo.environment
let stdinPipe = Pipe()
let stdoutPipe = Pipe()
let stderrPipe = Pipe()
proc.standardInput = stdinPipe
proc.standardOutput = stdoutPipe
proc.standardError = stderrPipe
proc.terminationHandler = { [weak self] _ in
Task { @MainActor [weak self] in self?.handleProcessTerminated() }
}
do {
try proc.run()
} catch {
state = .error(error.localizedDescription)
stateDelegate?.clientDidChangeState(id: server.id, state: .error(error.localizedDescription))
throw MCPClientError.processLaunchFailed(error.localizedDescription)
}
process = proc
stdinHandle = stdinPipe.fileHandleForWriting
startReadLoop(pipe: stdoutPipe)
startStderrLoop(pipe: stderrPipe)
do {
let _: MCPInitializeResult = try await timedRequest(seconds: 15, method: "initialize", params: [
"protocolVersion": "2024-11-05",
"capabilities": [:] as [String: Any],
"clientInfo": ["name": "oAI", "version": "1.0"] as [String: Any]
])
try sendNotification(method: "notifications/initialized")
let toolsResult: MCPToolsListResult = try await timedRequest(seconds: 15, method: "tools/list", params: nil)
discoveredTools = toolsResult.tools
} catch {
state = .error(error.localizedDescription)
stateDelegate?.clientDidChangeState(id: server.id, state: .error(error.localizedDescription))
proc.terminate()
throw error
}
state = .ready
stateDelegate?.clientDidBecomeReady(id: server.id, tools: discoveredTools, server: server)
}
func stop() {
state = .stopped
readTask?.cancel()
stderrTask?.cancel()
process?.terminate()
process = nil
stdinHandle = nil
lineBuffer = Data()
for (_, cont) in pendingCalls { cont.resume(throwing: MCPClientError.notConnected) }
pendingCalls.removeAll()
}
// MARK: - Tool Execution
func callTool(originalName: String, argumentsJSON: String) async -> [String: Any] {
guard state == .ready else {
return ["error": "MCP server '\(server.name)' is not connected"]
}
guard let argData = argumentsJSON.data(using: .utf8),
let argsDict = try? JSONSerialization.jsonObject(with: argData) as? [String: Any] else {
return ["error": "Invalid arguments JSON for tool \(originalName)"]
}
do {
let result: MCPToolCallResult = try await timedRequest(
seconds: server.timeout,
method: "tools/call",
params: ["name": originalName, "arguments": argsDict]
)
return convertMCPResult(result)
} catch MCPClientError.timeout {
return ["error": "MCP server '\(server.name)' timed out after \(Int(server.timeout))s"]
} catch {
return ["error": "MCP call '\(originalName)' failed: \(error.localizedDescription)"]
}
}
// MARK: - I/O Loops (detached from MainActor)
private func startReadLoop(pipe: Pipe) {
readTask = Task.detached { [weak self] in
let handle = pipe.fileHandleForReading
while true {
let data = handle.availableData
if data.isEmpty { break }
await self?.receiveData(data)
}
}
}
private func startStderrLoop(pipe: Pipe) {
let name = server.name
stderrTask = Task.detached {
let handle = pipe.fileHandleForReading
var buf = Data()
while true {
let data = handle.availableData
if data.isEmpty { break }
buf.append(data)
while let idx = buf.firstIndex(of: UInt8(ascii: "\n")) {
let line = String(data: buf[buf.startIndex..<idx], encoding: .utf8) ?? ""
buf = Data(buf[buf.index(after: idx)...])
if !line.trimmingCharacters(in: .whitespaces).isEmpty {
Log.extMcp.warning("[\(name)] \(line)")
}
}
}
}
}
// MARK: - Data Processing (MainActor)
private func receiveData(_ data: Data) {
lineBuffer.append(data)
while let idx = lineBuffer.firstIndex(of: UInt8(ascii: "\n")) {
let lineData = Data(lineBuffer[lineBuffer.startIndex..<idx])
lineBuffer = Data(lineBuffer[lineBuffer.index(after: idx)...])
processLine(lineData)
}
}
private func processLine(_ data: Data) {
guard !data.isEmpty,
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let id = json["id"] as? Int,
let cont = pendingCalls.removeValue(forKey: id) else { return }
if let err = json["error"] as? [String: Any] {
cont.resume(throwing: MCPClientError.invalidResponse(err["message"] as? String ?? "Unknown error"))
} else if let result = json["result"],
let resultData = try? JSONSerialization.data(withJSONObject: result) {
cont.resume(returning: resultData)
} else {
cont.resume(throwing: MCPClientError.invalidResponse("Missing result field"))
}
}
// MARK: - JSON-RPC
/// Send a JSON-RPC request with a per-call timeout. The timeout fires a cancellation
/// directly into the pending-calls table rather than using a task group (which would
/// pass the generic T through a @Sendable closure and trigger an isolated-conformance warning).
private func timedRequest<T: Decodable>(seconds: Double, method: String, params: [String: Any]?) async throws -> T {
let id = nextRequestId
nextRequestId += 1
var message: [String: Any] = ["jsonrpc": "2.0", "method": method, "id": id]
if let params { message["params"] = params }
try writeJSON(message)
// Schedule timeout: cancels the specific pending call by ID
let timeoutId = id
Task { [weak self, timeoutId] in
try? await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
self?.cancelPendingCall(id: timeoutId, with: MCPClientError.timeout)
}
// Await response data, then decode on MainActor
let resultData: Data = try await withCheckedThrowingContinuation { cont in
pendingCalls[id] = cont
}
return try JSONDecoder().decode(T.self, from: resultData)
}
private func cancelPendingCall(id: Int, with error: Error) {
pendingCalls.removeValue(forKey: id)?.resume(throwing: error)
}
private func sendNotification(method: String) throws {
try writeJSON(["jsonrpc": "2.0", "method": method])
}
private func writeJSON(_ message: [String: Any]) throws {
guard let handle = stdinHandle, process?.isRunning == true else {
throw MCPClientError.writeFailed
}
guard let data = try? JSONSerialization.data(withJSONObject: message),
let line = String(data: data, encoding: .utf8) else {
throw MCPClientError.writeFailed
}
do {
try handle.write(contentsOf: Data((line + "\n").utf8))
} catch {
throw MCPClientError.writeFailed
}
}
// MARK: - Process termination
private func handleProcessTerminated() {
guard state != .stopped else { return }
state = .crashed
for (_, cont) in pendingCalls { cont.resume(throwing: MCPClientError.notConnected) }
pendingCalls.removeAll()
stateDelegate?.clientDidChangeState(id: server.id, state: .crashed)
}
// MARK: - Result conversion
private func convertMCPResult(_ result: MCPToolCallResult) -> [String: Any] {
let isError = result.isError ?? false
var parts: [String] = []
for content in result.content {
switch content.type {
case "text":
if let text = content.text { parts.append(text) }
case "image":
if let base64 = content.data, let imageData = Data(base64Encoded: base64) {
parts.append("[Image saved to: \(writeTempImage(imageData, mimeType: content.mimeType))]")
}
case "resource":
if let text = content.text { parts.append(text) }
else if let uri = content.uri { parts.append("[Resource: \(uri)]") }
default:
if let text = content.text { parts.append(text) }
}
}
let combined = parts.joined(separator: "\n")
return isError ? ["error": combined.isEmpty ? "Tool returned an error" : combined] : ["output": combined]
}
private func writeTempImage(_ data: Data, mimeType: String?) -> String {
let ext = mimeType?.contains("png") == true ? "png" : "jpg"
let path = "/tmp/oai_mcp_\(Int(Date().timeIntervalSince1970 * 1000)).\(ext)"
try? data.write(to: URL(fileURLWithPath: path))
return path
}
}
+202
View File
@@ -0,0 +1,202 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Rune Olsen
import Foundation
// MARK: - ExternalMCPManager
@Observable
@MainActor
final class ExternalMCPManager {
nonisolated static let shared = ExternalMCPManager()
private(set) var clientStates: [UUID: MCPClientState] = [:]
private(set) var cachedToolSchemas: [Tool] = []
// Keep server config alongside client so we can access slug without await
private var clients: [UUID: ExternalMCPClient] = [:]
private var serverConfigs: [UUID: ExternalMCPServer] = [:]
private var restartTasks: [UUID: Task<Void, Never>] = [:]
private var restartAttempts: [UUID: Int] = [:]
private nonisolated init() {}
// MARK: - Lifecycle
func startAll() {
for server in SettingsService.shared.externalMCPServers where server.isEnabled {
startClient(for: server)
}
}
func stopAll() {
for client in clients.values { client.stop() }
clients.removeAll()
serverConfigs.removeAll()
clientStates.removeAll()
cachedToolSchemas.removeAll()
for task in restartTasks.values { task.cancel() }
restartTasks.removeAll()
restartAttempts.removeAll()
}
func reconfigure(servers: [ExternalMCPServer]) {
let activeIds = Set(servers.filter { $0.isEnabled }.map { $0.id })
for id in clients.keys where !activeIds.contains(id) {
clients[id]?.stop()
clients.removeValue(forKey: id)
serverConfigs.removeValue(forKey: id)
clientStates.removeValue(forKey: id)
restartTasks[id]?.cancel()
restartTasks.removeValue(forKey: id)
restartAttempts.removeValue(forKey: id)
removeCachedSchemas(for: id)
}
for server in servers where server.isEnabled && clients[server.id] == nil {
startClient(for: server)
}
}
private func startClient(for server: ExternalMCPServer) {
// Stop any existing client for this ID before creating a new one
clients[server.id]?.stop()
let client = ExternalMCPClient(server: server, stateDelegate: self)
clients[server.id] = client
serverConfigs[server.id] = server
clientStates[server.id] = .connecting
Task {
do {
try await client.start()
} catch MCPClientError.processLaunchFailed(let msg) {
// Process never started termination handler won't fire, so manually trigger crashed
Log.extMcp.error("Failed to launch '\(server.name)': \(msg)")
clientDidChangeState(id: server.id, state: .crashed)
} catch {
// Handshake/other failure proc.terminate() was called in start(), termination
// handler will fire and set .crashed, which drives the restart from one place only.
Log.extMcp.warning("'\(server.name)' start failed: \(error.localizedDescription)")
}
}
}
private func scheduleRestart(for server: ExternalMCPServer, attempt: Int) {
let delays: [Double] = [5, 15, 30]
let delay = delays[min(attempt - 1, delays.count - 1)]
Log.extMcp.warning("MCP server '\(server.name)' crashed — restarting in \(Int(delay))s (attempt \(attempt)/3)")
restartTasks[server.id]?.cancel()
let id = server.id
restartTasks[id] = Task { [weak self, id] in
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
guard !Task.isCancelled, let self,
self.clients[id] != nil,
SettingsService.shared.externalMCPServers.contains(where: { $0.id == id && $0.isEnabled })
else { return }
// startClient is the single place that creates and launches clients.
// It handles processLaunchFailed by calling clientDidChangeState(.crashed),
// and all other failures let the termination handler drive the .crashed callback.
self.startClient(for: server)
}
}
// MARK: - Tool Schema Integration (synchronous)
func getToolSchemas() -> [Tool] { cachedToolSchemas }
func isExternalTool(_ name: String) -> Bool {
cachedToolSchemas.contains { $0.function.name == name }
}
// MARK: - Tool Execution
func executeTool(name: String, argumentsJSON: String) async -> [String: Any] {
for (id, client) in clients {
guard clientStates[id] == .ready,
let server = serverConfigs[id] else { continue }
let prefix = "\(server.slug)_"
if name.hasPrefix(prefix) {
let originalName = String(name.dropFirst(prefix.count))
return await client.callTool(originalName: originalName, argumentsJSON: argumentsJSON)
}
}
return ["error": "No external MCP server found for tool: \(name)"]
}
// MARK: - Schema Cache
private func rebuildCache(for server: ExternalMCPServer, tools: [MCPToolDefinition]) {
removeCachedSchemas(for: server.id, slug: server.slug)
let prefixed = tools.compactMap { convertToolDefinition($0, server: server) }
cachedToolSchemas.append(contentsOf: prefixed)
Log.extMcp.info("[\(server.name)] cached \(prefixed.count) tools: \(prefixed.map { $0.function.name }.joined(separator: ", "))")
}
private func removeCachedSchemas(for id: UUID) {
guard let server = serverConfigs[id] else { return }
removeCachedSchemas(for: id, slug: server.slug)
}
private func removeCachedSchemas(for id: UUID, slug: String) {
cachedToolSchemas.removeAll { $0.function.name.hasPrefix("\(slug)_") }
}
private func convertToolDefinition(_ def: MCPToolDefinition, server: ExternalMCPServer) -> Tool? {
Tool(
type: "function",
function: Tool.Function(
name: "\(server.slug)_\(def.name)",
description: "[\(server.name)] \(def.description ?? "")",
parameters: convertInputSchema(def.inputSchema)
)
)
}
private func convertInputSchema(_ schema: MCPInputSchema) -> Tool.Function.Parameters {
var properties: [String: Tool.Function.Parameters.Property] = [:]
for (key, prop) in schema.properties ?? [:] {
let normalized: String
switch prop.type ?? "string" {
case "integer": normalized = "number"
case "string", "number", "boolean", "array", "object": normalized = prop.type!
default: normalized = "string"
}
var items: Tool.Function.Parameters.Property.Items? = nil
if normalized == "array", let t = prop.items?.type { items = .init(type: t) }
properties[key] = Tool.Function.Parameters.Property(
type: normalized,
description: prop.description ?? "",
enum: prop.enum,
items: items
)
}
return Tool.Function.Parameters(type: "object", properties: properties, required: schema.required)
}
}
// MARK: - ExternalMCPStateDelegate
extension ExternalMCPManager: ExternalMCPStateDelegate {
func clientDidBecomeReady(id: UUID, tools: [MCPToolDefinition], server: ExternalMCPServer) {
clientStates[id] = .ready
restartAttempts.removeValue(forKey: id)
rebuildCache(for: server, tools: tools)
}
func clientDidChangeState(id: UUID, state: MCPClientState) {
clientStates[id] = state
if case .crashed = state,
let server = serverConfigs[id],
SettingsService.shared.externalMCPServers.contains(where: { $0.id == id && $0.isEnabled }) {
removeCachedSchemas(for: id, slug: server.slug)
let attempt = (restartAttempts[id] ?? 0) + 1
guard attempt <= 3 else {
Log.extMcp.error("MCP server '\(server.name)' gave up after 3 restart attempts")
clientStates[id] = .error("Maximum restart attempts reached")
restartAttempts.removeValue(forKey: id)
return
}
restartAttempts[id] = attempt
scheduleRestart(for: server, attempt: attempt)
}
}
}
+170
View File
@@ -0,0 +1,170 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Rune Olsen
import Foundation
// MARK: - Server Configuration
struct ExternalMCPServer: Codable, Identifiable, Sendable {
var id: UUID
var name: String
var command: String
var args: [String]
var isEnabled: Bool
var timeout: TimeInterval
var createdAt: Date
init(id: UUID = UUID(), name: String, command: String, args: [String] = [],
isEnabled: Bool = true, timeout: TimeInterval = 30, createdAt: Date = Date()) {
self.id = id
self.name = name
self.command = command
self.args = args
self.isEnabled = isEnabled
self.timeout = timeout
self.createdAt = createdAt
}
var slug: String { Self.makeSlug(from: name) }
static func makeSlug(from name: String) -> String {
let s = name
.lowercased()
.components(separatedBy: CharacterSet.alphanumerics.inverted)
.filter { !$0.isEmpty }
.joined(separator: "_")
return s.isEmpty ? "ext" : s
}
/// Splits a raw arguments string into tokens, respecting single/double-quoted
/// segments so arguments containing spaces (e.g. `--root "/Users/x/My Documents"`)
/// survive intact instead of being split on every space.
static func parseArguments(_ input: String) -> [String] {
var args: [String] = []
var current = ""
var inSingleQuotes = false
var inDoubleQuotes = false
for char in input {
if char == "'" && !inDoubleQuotes {
inSingleQuotes.toggle()
} else if char == "\"" && !inSingleQuotes {
inDoubleQuotes.toggle()
} else if char.isWhitespace && !inSingleQuotes && !inDoubleQuotes {
if !current.isEmpty {
args.append(current)
current = ""
}
} else {
current.append(char)
}
}
if !current.isEmpty { args.append(current) }
return args
}
static let reservedSlugs: Set<String> = [
"anytype", "paperless", "calendar", "reminders",
"contacts", "location", "maps", "bash", "web", "read", "write",
"list", "search", "edit", "delete", "create", "move", "copy", "spawn"
]
var isSlugReserved: Bool { Self.reservedSlugs.contains(slug) }
}
// MARK: - Client State
enum MCPClientState: Equatable {
case idle
case connecting
case ready
case error(String)
case crashed
case stopped
}
// MARK: - State Delegate (all callbacks on MainActor)
@MainActor
protocol ExternalMCPStateDelegate: AnyObject {
func clientDidBecomeReady(id: UUID, tools: [MCPToolDefinition], server: ExternalMCPServer)
func clientDidChangeState(id: UUID, state: MCPClientState)
}
// MARK: - Client Errors
enum MCPClientError: LocalizedError {
case notConnected
case invalidResponse(String)
case timeout
case processLaunchFailed(String)
case handshakeFailed(String)
case writeFailed
var errorDescription: String? {
switch self {
case .notConnected: return "MCP server is not connected"
case .invalidResponse(let s): return "Invalid MCP response: \(s)"
case .timeout: return "MCP request timed out"
case .processLaunchFailed(let s): return "Failed to launch MCP server: \(s)"
case .handshakeFailed(let s): return "MCP handshake failed: \(s)"
case .writeFailed: return "Failed to write to MCP server stdin"
}
}
}
// MARK: - MCP Protocol Types
struct MCPInitializeResult: Decodable {
let protocolVersion: String
let capabilities: MCPCapabilities
let serverInfo: MCPServerInfo?
}
struct MCPCapabilities: Decodable {
let tools: MCPToolsCapability?
struct MCPToolsCapability: Decodable { let listChanged: Bool? }
}
struct MCPServerInfo: Decodable {
let name: String
let version: String?
}
struct MCPToolsListResult: Decodable {
let tools: [MCPToolDefinition]
let nextCursor: String?
}
struct MCPToolDefinition: Decodable {
let name: String
let description: String?
let inputSchema: MCPInputSchema
}
struct MCPInputSchema: Decodable {
let type: String
let properties: [String: MCPPropertySchema]?
let required: [String]?
}
struct MCPPropertySchema: Decodable {
let type: String?
let description: String?
let `enum`: [String]?
let items: MCPItemsSchema?
struct MCPItemsSchema: Decodable { let type: String? }
}
struct MCPToolCallResult: Decodable {
let content: [MCPContent]
let isError: Bool?
}
struct MCPContent: Decodable {
let type: String
let text: String?
let data: String?
let mimeType: String?
let uri: String?
}
+343
View File
@@ -0,0 +1,343 @@
//
// LocationMapsService.swift
// oAI
//
// Read-only Location and Maps integration via CoreLocation and MapKit
//
// 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 CoreLocation
import Foundation
import MapKit
import os
@Observable
class LocationMapsService: NSObject, CLLocationManagerDelegate {
static let shared = LocationMapsService()
private let locationManager = CLLocationManager()
private var authContinuation: CheckedContinuation<Bool, Never>?
private var locationContinuation: CheckedContinuation<CLLocation?, Never>?
private override init() {
super.init()
locationManager.delegate = self
}
// MARK: - Authorization
var authStatus: CLAuthorizationStatus {
locationManager.authorizationStatus
}
var authorized: Bool {
authStatus == .authorizedAlways || authStatus == .authorized
}
var accessState: PersonalDataAccessState {
let status = authStatus
Log.mcp.debug("LocationMapsService.accessState -> status=\(Self.describe(status)) (raw=\(status.rawValue))")
switch status {
case .authorizedAlways, .authorized: return .granted
case .notDetermined: return .notDetermined
default: return .denied
}
}
@discardableResult
func requestAccess() async -> Bool {
let before = locationManager.authorizationStatus
Log.mcp.info("LocationMapsService.requestAccess: status before = \(Self.describe(before)) (raw=\(before.rawValue))")
if before != .notDetermined {
Log.mcp.info("LocationMapsService.requestAccess: skipping OS prompt (not notDetermined)")
return authorized
}
return await withCheckedContinuation { continuation in
self.authContinuation = continuation
locationManager.requestWhenInUseAuthorization()
}
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus
Log.mcp.info("LocationMapsService: authorization changed -> \(Self.describe(status)) (raw=\(status.rawValue))")
authContinuation?.resume(returning: authorized)
authContinuation = nil
}
nonisolated static func describe(_ status: CLAuthorizationStatus) -> String {
switch status {
case .notDetermined: return "notDetermined"
case .restricted: return "restricted"
case .denied: return "denied"
case .authorizedAlways: return "authorizedAlways"
case .authorized: return "authorized"
@unknown default: return "unknown"
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationContinuation?.resume(returning: locations.last)
locationContinuation = nil
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
Log.mcp.error("Location request failed: \(error.localizedDescription)")
locationContinuation?.resume(returning: nil)
locationContinuation = nil
}
private func currentLocation() async -> CLLocation? {
await withCheckedContinuation { continuation in
self.locationContinuation = continuation
locationManager.requestLocation()
}
}
// MARK: - Tool Schemas
func getToolSchemas() -> [Tool] {
[
makeTool(
name: "location_get_current",
description: "Get the device's current location, including a human-readable address.",
properties: [:],
required: []
),
makeTool(
name: "maps_search_places",
description: "Search for places (businesses, landmarks, addresses) by name or category.",
properties: [
"query": prop("string", "What to search for, e.g. 'coffee shops' or 'Eiffel Tower'"),
"near": prop("string", "Optional: an address or 'latitude,longitude' to search near")
],
required: ["query"]
),
makeTool(
name: "maps_geocode",
description: "Convert an address into geographic coordinates and a formatted address.",
properties: [
"address": prop("string", "The address to geocode")
],
required: ["address"]
),
makeTool(
name: "maps_get_directions",
description: "Get distance and estimated travel time between two locations.",
properties: [
"origin": prop("string", "Starting address or 'latitude,longitude'"),
"destination": prop("string", "Destination address or 'latitude,longitude'"),
"transport_type": prop("string", "Mode of transport", enumValues: ["driving", "walking", "transit"])
],
required: ["origin", "destination"]
)
]
}
// MARK: - Tool Execution
func executeTool(name: String, arguments: String) async -> [String: Any] {
Log.mcp.info("Executing LocationMaps tool: \(name)")
let args = parseArgs(arguments)
switch name {
case "location_get_current":
guard authorized else { return ["error": "Location permission not granted. Grant access in Settings > MCP."] }
return await getCurrentLocation()
case "maps_search_places":
guard let query = args["query"] as? String, !query.isEmpty else {
return ["error": "Missing required parameter: query"]
}
let near = args["near"] as? String
return await searchPlaces(query: query, near: near)
case "maps_geocode":
guard let address = args["address"] as? String, !address.isEmpty else {
return ["error": "Missing required parameter: address"]
}
return await geocode(address: address)
case "maps_get_directions":
guard let origin = args["origin"] as? String, let destination = args["destination"] as? String else {
return ["error": "Missing required parameter: origin and/or destination"]
}
let transportType = args["transport_type"] as? String ?? "driving"
return await getDirections(origin: origin, destination: destination, transportType: transportType)
default:
return ["error": "Unknown LocationMaps tool: \(name)"]
}
}
// MARK: - Implementations
private func getCurrentLocation() async -> [String: Any] {
guard let location = await currentLocation() else {
return ["error": "Could not determine current location"]
}
var result: [String: Any] = [
"latitude": location.coordinate.latitude,
"longitude": location.coordinate.longitude
]
if let mapItem = await reverseGeocode(location), let address = addressString(for: mapItem) {
result["address"] = address
}
return result
}
private func searchPlaces(query: String, near: String?) async -> [String: Any] {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = query
if let near, let coordinate = await coordinate(for: near) {
request.region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 20_000, longitudinalMeters: 20_000)
}
do {
let response = try await MKLocalSearch(request: request).start()
let places = response.mapItems.prefix(15).map { item -> [String: Any] in
var place: [String: Any] = ["name": item.name ?? "Unknown"]
place["latitude"] = item.location.coordinate.latitude
place["longitude"] = item.location.coordinate.longitude
if let address = addressString(for: item) {
place["address"] = address
}
if let phone = item.phoneNumber { place["phone"] = phone }
return place
}
return ["count": places.count, "places": Array(places)]
} catch {
return ["error": "Search failed: \(error.localizedDescription)"]
}
}
private func geocode(address: String) async -> [String: Any] {
guard let request = MKGeocodingRequest(addressString: address) else {
return ["error": "Invalid address: \(address)"]
}
do {
guard let mapItem = try await request.mapItems.first else {
return ["error": "No results found for address: \(address)"]
}
var result: [String: Any] = [
"latitude": mapItem.location.coordinate.latitude,
"longitude": mapItem.location.coordinate.longitude
]
if let formatted = addressString(for: mapItem) {
result["formatted_address"] = formatted
}
return result
} catch {
return ["error": "Geocoding failed: \(error.localizedDescription)"]
}
}
private func getDirections(origin: String, destination: String, transportType: String) async -> [String: Any] {
guard let originCoordinate = await coordinate(for: origin) else {
return ["error": "Could not resolve origin: \(origin)"]
}
guard let destinationCoordinate = await coordinate(for: destination) else {
return ["error": "Could not resolve destination: \(destination)"]
}
let request = MKDirections.Request()
request.source = MKMapItem(location: CLLocation(latitude: originCoordinate.latitude, longitude: originCoordinate.longitude), address: nil)
request.destination = MKMapItem(location: CLLocation(latitude: destinationCoordinate.latitude, longitude: destinationCoordinate.longitude), address: nil)
switch transportType {
case "walking": request.transportType = .walking
case "transit": request.transportType = .transit
default: request.transportType = .automobile
}
do {
let response = try await MKDirections(request: request).calculate()
guard let route = response.routes.first else {
return ["error": "No route found"]
}
let distanceFormatter = MKDistanceFormatter()
let durationFormatter = DateComponentsFormatter()
durationFormatter.allowedUnits = [.hour, .minute]
durationFormatter.unitsStyle = .short
return [
"distance_meters": route.distance,
"distance_text": distanceFormatter.string(fromDistance: route.distance),
"duration_seconds": route.expectedTravelTime,
"duration_text": durationFormatter.string(from: route.expectedTravelTime) ?? "",
"transport_type": transportType
]
} catch {
return ["error": "Directions failed: \(error.localizedDescription)"]
}
}
// MARK: - Helpers
private func coordinate(for text: String) async -> CLLocationCoordinate2D? {
let parts = text.split(separator: ",").map { $0.trimmingCharacters(in: .whitespaces) }
if parts.count == 2, let lat = Double(parts[0]), let lon = Double(parts[1]) {
return CLLocationCoordinate2D(latitude: lat, longitude: lon)
}
guard let request = MKGeocodingRequest(addressString: text) else { return nil }
if let mapItems = try? await request.mapItems, let mapItem = mapItems.first {
return mapItem.location.coordinate
}
return nil
}
private func reverseGeocode(_ location: CLLocation) async -> MKMapItem? {
guard let request = MKReverseGeocodingRequest(location: location) else { return nil }
let mapItems = try? await request.mapItems
return mapItems?.first
}
private func addressString(for mapItem: MKMapItem) -> String? {
mapItem.address?.fullAddress
?? mapItem.addressRepresentations?.fullAddress(includingRegion: true, singleLine: true)
}
private func parseArgs(_ arguments: String) -> [String: Any] {
guard let data = arguments.data(using: .utf8),
let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
return [:]
}
return dict
}
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, enumValues: [String]? = nil) -> Tool.Function.Parameters.Property {
Tool.Function.Parameters.Property(type: type, description: description, enum: enumValues)
}
}
+292 -1
View File
@@ -98,6 +98,17 @@ class MCPService {
func isPathAllowed(_ path: String) -> Bool { func isPathAllowed(_ path: String) -> Bool {
let resolved = ((path as NSString).expandingTildeInPath as NSString).standardizingPath let resolved = ((path as NSString).expandingTildeInPath as NSString).standardizingPath
// Always allow the system temp directory external MCP servers and tools write
// intermediate data there (e.g. Safari MCP page-content files, generated images).
// Check both NSTemporaryDirectory() (per-user Darwin temp dir) and /tmp (what this
// codebase's own temp files actually use, e.g. ChatViewModel's /tmp/oai_generated_*
// and ExternalMCPClient's /tmp/oai_mcp_* they resolve to different directories.
let tmpCandidates = [
(NSTemporaryDirectory() as NSString).standardizingPath,
"/tmp",
"/private/tmp"
]
if tmpCandidates.contains(where: { resolved.hasPrefix($0) }) { return true }
return allowedFolders.contains { resolved.hasPrefix($0) } return allowedFolders.contains { resolved.hasPrefix($0) }
} }
@@ -111,6 +122,9 @@ class MCPService {
private let anytypeService = AnytypeMCPService.shared private let anytypeService = AnytypeMCPService.shared
private let paperlessService = PaperlessService.shared private let paperlessService = PaperlessService.shared
private let eventKitService = EventKitService.shared
private let contactsService = ContactsService.shared
private let locationMapsService = LocationMapsService.shared
// MARK: - Bash Approval State // MARK: - Bash Approval State
@@ -124,6 +138,19 @@ class MCPService {
private var pendingBashContinuation: CheckedContinuation<[String: Any], Never>? = nil private var pendingBashContinuation: CheckedContinuation<[String: Any], Never>? = nil
private(set) var bashSessionApproved: Bool = false private(set) var bashSessionApproved: Bool = false
// MARK: - Personal Data (Calendar/Reminders) Approval State
struct PendingPersonalDataAction: Identifiable {
let id = UUID()
let toolName: String
let argumentsJSON: String
let summary: String
}
private(set) var pendingPersonalDataAction: PendingPersonalDataAction? = nil
private var pendingPersonalDataContinuation: CheckedContinuation<[String: Any], Never>? = nil
private(set) var personalDataSessionApproved: Bool = false
// MARK: - Tool Schema Generation // MARK: - Tool Schema Generation
func getToolSchemas(onlineMode: Bool = false) -> [Tool] { func getToolSchemas(onlineMode: Bool = false) -> [Tool] {
@@ -232,6 +259,24 @@ class MCPService {
tools.append(contentsOf: paperlessService.getToolSchemas()) tools.append(contentsOf: paperlessService.getToolSchemas())
} }
// Add Calendar/Reminders tools if enabled
if settings.calendarEnabled || settings.remindersEnabled {
tools.append(contentsOf: eventKitService.getToolSchemas(
calendarEnabled: settings.calendarEnabled,
remindersEnabled: settings.remindersEnabled
))
}
// Add Contacts tools if enabled
if settings.contactsEnabled {
tools.append(contentsOf: contactsService.getToolSchemas())
}
// Add Location/Maps tools if enabled
if settings.locationMapsEnabled {
tools.append(contentsOf: locationMapsService.getToolSchemas())
}
// Add bash_execute tool when bash is enabled // Add bash_execute tool when bash is enabled
if settings.bashEnabled { if settings.bashEnabled {
let workDir = settings.bashWorkingDirectory let workDir = settings.bashWorkingDirectory
@@ -260,6 +305,25 @@ class MCPService {
)) ))
} }
// Add spawn_research_agents when Research Agents are enabled
if settings.agentsEnabled {
tools.append(makeTool(
name: "spawn_research_agents",
description: "Spawn multiple READ-ONLY research sub-agents that investigate independent questions IN PARALLEL. Each sub-agent gets its own read_file/list_directory/search_files/web_search loop — no write, no bash, no nesting (sub-agents cannot call this tool). ONLY use this when you have 2 or more genuinely independent research questions that benefit from running at the same time (e.g. comparing several unrelated files, topics, or sources). Do NOT use this for a single lookup, a sequential task, or anything answerable with one direct tool call — call read_file/search_files/web_search yourself instead. Each sub-agent is its own full chain of model calls and meaningfully increases cost and latency; using this for trivial tasks is wasteful. Prefer the smallest number of tasks that actually need to run in parallel.",
properties: [
"tasks": Tool.Function.Parameters.Property(
type: "array",
description: "List of independent, self-contained research questions — one per sub-agent. Keep this list as short as the task genuinely requires.",
items: .init(type: "string")
)
],
required: ["tasks"]
))
}
// Add tools from external MCP servers (stdio JSON-RPC protocol)
tools.append(contentsOf: ExternalMCPManager.shared.getToolSchemas())
return tools return tools
} }
@@ -284,7 +348,10 @@ class MCPService {
// MARK: - Tool Execution // MARK: - Tool Execution
func executeTool(name: String, arguments: String) async -> [String: Any] { /// `agentProvider`/`agentModelId` are only needed for `spawn_research_agents`, which drives
/// its own model calls every other tool ignores them. Passed in by the caller's active
/// chat session rather than stored on MCPService, since this service has no provider state.
func executeTool(name: String, arguments: String, agentProvider: AIProvider? = nil, agentModelId: String? = nil) async -> [String: Any] {
Log.mcp.info("Executing tool: \(name)") Log.mcp.info("Executing tool: \(name)")
guard let argData = arguments.data(using: .utf8), guard let argData = arguments.data(using: .utf8),
let args = try? JSONSerialization.jsonObject(with: argData) as? [String: Any] else { let args = try? JSONSerialization.jsonObject(with: argData) as? [String: Any] else {
@@ -399,7 +466,30 @@ class MCPService {
let mapped = results.map { ["title": $0.title, "url": $0.url, "snippet": $0.snippet] } let mapped = results.map { ["title": $0.title, "url": $0.url, "snippet": $0.snippet] }
return ["results": mapped] return ["results": mapped]
case "spawn_research_agents":
guard settings.agentsEnabled else {
return ["error": "Research agents are disabled. Enable 'Research Agents' in Settings > MCP."]
}
guard let agentProvider, let agentModelId else {
return ["error": "Internal error: missing model context for spawn_research_agents"]
}
guard let tasks = args["tasks"] as? [String], !tasks.isEmpty else {
return ["error": "Missing required parameter: tasks (non-empty array of strings)"]
}
return await runResearchAgents(tasks: tasks, provider: agentProvider, modelId: agentModelId)
case "calendar_create_event", "reminders_create", "reminders_complete":
guard settings.calendarEnabled || settings.remindersEnabled else {
return ["error": "Calendar/Reminders access is disabled. Enable it in Settings > MCP."]
}
let summary = eventKitService.approvalSummary(forTool: name, arguments: arguments)
return await executePersonalDataAction(toolName: name, argumentsJSON: arguments, summary: summary)
default: default:
// Route to external MCP servers (stdio JSON-RPC)
if ExternalMCPManager.shared.isExternalTool(name) {
return await ExternalMCPManager.shared.executeTool(name: name, argumentsJSON: arguments)
}
// 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)
@@ -408,6 +498,18 @@ class MCPService {
if name.hasPrefix("paperless_") { if name.hasPrefix("paperless_") {
return await paperlessService.executeTool(name: name, arguments: arguments) return await paperlessService.executeTool(name: name, arguments: arguments)
} }
// Route calendar_*/reminders_* read tools to EventKitService
if name.hasPrefix("calendar_") || name.hasPrefix("reminders_") {
return await eventKitService.executeTool(name: name, arguments: arguments)
}
// Route contacts_* tools to ContactsService
if name.hasPrefix("contacts_") {
return await contactsService.executeTool(name: name, arguments: arguments)
}
// Route location_*/maps_* tools to LocationMapsService
if name.hasPrefix("location_") || name.hasPrefix("maps_") {
return await locationMapsService.executeTool(name: name, arguments: arguments)
}
return ["error": "Unknown tool: \(name)"] return ["error": "Unknown tool: \(name)"]
} }
} }
@@ -754,6 +856,11 @@ class MCPService {
if bashSessionApproved { if bashSessionApproved {
return await runBashCommand(command, workingDirectory: workingDirectory) return await runBashCommand(command, workingDirectory: workingDirectory)
} }
// 2nd Brain calls its helper script via bash_execute let the user mark that
// specific traffic as always-trusted instead of approving it every time.
if isTrustedSecondBrainCommand(command) {
return await runBashCommand(command, workingDirectory: workingDirectory)
}
return await withCheckedContinuation { continuation in return await withCheckedContinuation { continuation in
DispatchQueue.main.async { DispatchQueue.main.async {
self.pendingBashCommand = PendingBashCommand(command: command, workingDirectory: workingDirectory) self.pendingBashCommand = PendingBashCommand(command: command, workingDirectory: workingDirectory)
@@ -786,6 +893,190 @@ class MCPService {
bashSessionApproved = false bashSessionApproved = false
} }
private func isTrustedSecondBrainCommand(_ command: String) -> Bool {
guard settings.trustSecondBrainSkill, command.contains(".brain_helper.py") else { return false }
return settings.agentSkills.contains { $0.isActive && $0.isSecondBrainSkill }
}
// MARK: - Research Agents (read-only, parallel)
/// Runs `tasks.count` sub-agents (capped) with bounded concurrency, each in its own
/// read-only tool loop, and returns their findings concatenated for the orchestrator.
private func runResearchAgents(tasks: [String], provider: AIProvider, modelId: String) async -> [String: Any] {
let maxConcurrent = max(1, min(5, settings.maxConcurrentAgents))
// Hard cap on total sub-agents regardless of concurrency setting, so a model
// requesting an unreasonably long task list can't run away with cost.
let cappedTasks = Array(tasks.prefix(8))
var results: [(Int, String)] = []
await withTaskGroup(of: (Int, String).self) { group in
var nextIndex = 0
func launchNext() {
guard nextIndex < cappedTasks.count else { return }
let idx = nextIndex
let task = cappedTasks[idx]
nextIndex += 1
group.addTask {
let answer = await self.runSingleResearchAgent(task: task, provider: provider, modelId: modelId)
return (idx, answer)
}
}
for _ in 0..<min(maxConcurrent, cappedTasks.count) { launchNext() }
for await result in group {
results.append(result)
launchNext()
}
}
let sorted = results.sorted { $0.0 < $1.0 }
let formatted = sorted.map { idx, answer in
"### Agent \(idx + 1): \(cappedTasks[idx])\n\(answer)"
}.joined(separator: "\n\n")
var response: [String: Any] = ["agent_count": sorted.count, "results": formatted]
if tasks.count > cappedTasks.count {
response["note"] = "Only the first \(cappedTasks.count) of \(tasks.count) requested tasks were run (per-call cap)."
}
return response
}
/// A single sub-agent's self-contained tool loop. Read-only tools only; cannot write,
/// run bash, or spawn further sub-agents (spawn_research_agents is not in its tool list).
private func runSingleResearchAgent(task: String, provider: AIProvider, modelId: String) async -> String {
let readOnlyTools: [Tool] = [
makeTool(
name: "read_file",
description: "Read the contents of a file. Maximum file size is 10MB.",
properties: ["file_path": prop("string", "The absolute path to the file to read")],
required: ["file_path"]
),
makeTool(
name: "list_directory",
description: "List the contents of a directory. Skips hidden/build directories like .git, node_modules, etc.",
properties: [
"dir_path": prop("string", "The absolute path to the directory to list"),
"recursive": prop("boolean", "Whether to list recursively (default: false)")
],
required: ["dir_path"]
),
makeTool(
name: "search_files",
description: "Search for files by name pattern or content.",
properties: [
"pattern": prop("string", "Glob pattern to match filenames (e.g. '*.py', 'README*')"),
"search_path": prop("string", "Directory to search in (defaults to first allowed folder)"),
"content_search": prop("string", "Optional text to search for inside files")
],
required: ["pattern"]
),
makeTool(
name: "web_search",
description: "Search the web for current information using DuckDuckGo.",
properties: ["query": prop("string", "The search query to look up")],
required: ["query"]
)
]
let allowedNames = Set(readOnlyTools.map { $0.function.name })
var apiMessages: [[String: Any]] = [
["role": "system", "content": "You are a read-only research sub-agent. Investigate the assigned task using the available tools and report concise findings as plain text. You cannot write, delete, or execute anything, and cannot spawn further sub-agents. Once you have enough information, respond with your final answer and stop calling tools."],
["role": "user", "content": task]
]
let maxIterations = 6
for iteration in 0..<maxIterations {
if Task.isCancelled { return "(cancelled)" }
guard let response = try? await provider.chatWithToolMessages(
model: modelId, messages: apiMessages, tools: readOnlyTools, maxTokens: nil, temperature: nil
) else {
return "(error: sub-agent request failed)"
}
let toolCalls = response.toolCalls ?? []
guard !toolCalls.isEmpty else {
return response.content.isEmpty ? "(no findings)" : response.content
}
var assistantMsg: [String: Any] = ["role": "assistant"]
if !response.content.isEmpty { assistantMsg["content"] = response.content }
assistantMsg["tool_calls"] = toolCalls.map { tc in
["id": tc.id, "type": tc.type, "function": ["name": tc.functionName, "arguments": tc.arguments]]
}
apiMessages.append(assistantMsg)
for tc in toolCalls {
let resultJSON: String
if allowedNames.contains(tc.functionName) {
let result = await executeTool(name: tc.functionName, arguments: tc.arguments)
resultJSON = serializeToolResult(result)
} else {
resultJSON = "{\"error\": \"Tool not available to research sub-agents\"}"
}
apiMessages.append([
"role": "tool",
"tool_call_id": tc.id,
"name": tc.functionName,
"content": resultJSON
])
}
if iteration == maxIterations - 1 {
return "(research incomplete: sub-agent reached its iteration limit)"
}
}
return "(no findings)"
}
private func serializeToolResult(_ result: [String: Any], maxBytes: Int = 20_000) -> String {
guard let data = try? JSONSerialization.data(withJSONObject: result),
let str = String(data: data, encoding: .utf8) else {
return "{\"error\": \"Failed to serialize result\"}"
}
guard str.utf8.count > maxBytes, let truncated = String(str.utf8.prefix(maxBytes)) else {
return str
}
return truncated + "\n... (result truncated)"
}
// MARK: - Personal Data (Calendar/Reminders) Approval
private func executePersonalDataAction(toolName: String, argumentsJSON: String, summary: String) async -> [String: Any] {
guard settings.personalDataRequireApproval, !personalDataSessionApproved else {
return await eventKitService.executeWriteTool(name: toolName, arguments: argumentsJSON)
}
return await withCheckedContinuation { continuation in
DispatchQueue.main.async {
self.pendingPersonalDataAction = PendingPersonalDataAction(toolName: toolName, argumentsJSON: argumentsJSON, summary: summary)
self.pendingPersonalDataContinuation = continuation
}
}
}
func approvePendingPersonalDataAction(forSession: Bool = false) {
guard let pending = pendingPersonalDataAction, let cont = pendingPersonalDataContinuation else { return }
pendingPersonalDataAction = nil
pendingPersonalDataContinuation = nil
if forSession {
personalDataSessionApproved = true
}
Task.detached(priority: .userInitiated) {
let result = await self.eventKitService.executeWriteTool(name: pending.toolName, arguments: pending.argumentsJSON)
cont.resume(returning: result)
}
}
func denyPendingPersonalDataAction() {
guard pendingPersonalDataAction != nil else { return }
pendingPersonalDataAction = nil
pendingPersonalDataContinuation?.resume(returning: ["error": "User denied this action"])
pendingPersonalDataContinuation = nil
}
func resetPersonalDataSessionApproval() {
personalDataSessionApproved = false
}
private func runBashCommand(_ command: String, workingDirectory: String) async -> [String: Any] { private func runBashCommand(_ command: String, workingDirectory: String) async -> [String: Any] {
let timeoutSeconds = settings.bashTimeout let timeoutSeconds = settings.bashTimeout
let workDir = ((workingDirectory as NSString).expandingTildeInPath as NSString).standardizingPath let workDir = ((workingDirectory as NSString).expandingTildeInPath as NSString).standardizingPath
+127
View File
@@ -27,6 +27,18 @@ import Foundation
import os import os
import Security import Security
/// Kill switch for the Personal Data tools (Calendar/Reminders/Contacts/Location & Maps).
/// Flip `isHiddenPendingAppleFix` back to `false` once Apple fixes the macOS 27 beta TCC bug
/// (filed with Apple). Each flag hides the relevant UI and forces the `*Enabled` getter to
/// return `false` regardless of the persisted DB value no code deleted, just inert.
enum PersonalDataTools {
/// Hides the entire Personal Data section. Flip to `false` once all four services work.
static let isHiddenPendingAppleFix = false
/// Hides only the Contacts row. Contacts TCC still broken under hardened runtime on
/// macOS 27 beta 2 while Calendar/Reminders/Location are fixed. Flip to `false` once fixed.
static let isContactsHiddenPendingAppleFix = true
}
@Observable @Observable
class SettingsService { class SettingsService {
static let shared = SettingsService() static let shared = SettingsService()
@@ -449,6 +461,48 @@ class SettingsService {
} }
} }
// MARK: - External MCP Servers
var externalMCPServers: [ExternalMCPServer] {
get {
guard let json = cache["externalMCPServers"],
let data = json.data(using: .utf8) else { return [] }
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return (try? decoder.decode([ExternalMCPServer].self, from: data)) ?? []
}
set {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
if let data = try? encoder.encode(newValue),
let json = String(data: data, encoding: .utf8) {
cache["externalMCPServers"] = json
DatabaseService.shared.setSetting(key: "externalMCPServers", value: json)
}
Task { @MainActor in ExternalMCPManager.shared.reconfigure(servers: newValue) }
}
}
func addExternalMCPServer(_ server: ExternalMCPServer) {
externalMCPServers = externalMCPServers + [server]
}
func updateExternalMCPServer(_ server: ExternalMCPServer) {
externalMCPServers = externalMCPServers.map { $0.id == server.id ? server : $0 }
}
func deleteExternalMCPServer(id: UUID) {
externalMCPServers = externalMCPServers.filter { $0.id != id }
}
func toggleExternalMCPServer(id: UUID) {
externalMCPServers = externalMCPServers.map { s in
s.id == id ? ExternalMCPServer(id: s.id, name: s.name, command: s.command,
args: s.args, isEnabled: !s.isEnabled,
timeout: s.timeout, createdAt: s.createdAt) : s
}
}
// MARK: - Favorite Models // MARK: - Favorite Models
var favoriteModelIds: Set<String> { var favoriteModelIds: Set<String> {
@@ -577,6 +631,37 @@ class SettingsService {
} }
} }
// MARK: - Research Agents Settings
/// When true, the AI can call `spawn_research_agents` to run multiple read-only
/// sub-agents in parallel. Each sub-agent is its own full chain of model calls, so
/// this can noticeably increase cost opt-in, off by default.
var agentsEnabled: Bool {
get { cache["agentsEnabled"] == "true" }
set {
cache["agentsEnabled"] = String(newValue)
DatabaseService.shared.setSetting(key: "agentsEnabled", value: String(newValue))
}
}
var maxConcurrentAgents: Int {
get { cache["maxConcurrentAgents"].flatMap(Int.init) ?? 3 }
set {
cache["maxConcurrentAgents"] = String(newValue)
DatabaseService.shared.setSetting(key: "maxConcurrentAgents", value: String(newValue))
}
}
/// When true (and an active "2nd Brain" Agent Skill is installed), bash commands that
/// invoke the 2nd Brain helper script skip the approval dialog entirely.
var trustSecondBrainSkill: Bool {
get { cache["trustSecondBrainSkill"] == "true" }
set {
cache["trustSecondBrainSkill"] = String(newValue)
DatabaseService.shared.setSetting(key: "trustSecondBrainSkill", value: String(newValue))
}
}
var bashWorkingDirectory: String { var bashWorkingDirectory: String {
get { cache["bashWorkingDirectory"] ?? "~" } get { cache["bashWorkingDirectory"] ?? "~" }
set { set {
@@ -593,6 +678,48 @@ class SettingsService {
} }
} }
// MARK: - Personal Data Settings (Calendar/Reminders/Contacts/Location/Maps)
var calendarEnabled: Bool {
get { !PersonalDataTools.isHiddenPendingAppleFix && cache["calendarEnabled"] == "true" }
set {
cache["calendarEnabled"] = String(newValue)
DatabaseService.shared.setSetting(key: "calendarEnabled", value: String(newValue))
}
}
var remindersEnabled: Bool {
get { !PersonalDataTools.isHiddenPendingAppleFix && cache["remindersEnabled"] == "true" }
set {
cache["remindersEnabled"] = String(newValue)
DatabaseService.shared.setSetting(key: "remindersEnabled", value: String(newValue))
}
}
var contactsEnabled: Bool {
get { !PersonalDataTools.isHiddenPendingAppleFix && !PersonalDataTools.isContactsHiddenPendingAppleFix && cache["contactsEnabled"] == "true" }
set {
cache["contactsEnabled"] = String(newValue)
DatabaseService.shared.setSetting(key: "contactsEnabled", value: String(newValue))
}
}
var locationMapsEnabled: Bool {
get { !PersonalDataTools.isHiddenPendingAppleFix && cache["locationMapsEnabled"] == "true" }
set {
cache["locationMapsEnabled"] = String(newValue)
DatabaseService.shared.setSetting(key: "locationMapsEnabled", value: String(newValue))
}
}
var personalDataRequireApproval: Bool {
get { cache["personalDataRequireApproval"].map { $0 == "true" } ?? true }
set {
cache["personalDataRequireApproval"] = String(newValue)
DatabaseService.shared.setSetting(key: "personalDataRequireApproval", value: String(newValue))
}
}
// MARK: - Paperless-NGX Settings // MARK: - Paperless-NGX Settings
var paperlessEnabled: Bool { var paperlessEnabled: Bool {
+1
View File
@@ -153,4 +153,5 @@ enum Log {
nonisolated static let search = AppLogger(subsystem: subsystem, category: "search") nonisolated static let search = AppLogger(subsystem: subsystem, category: "search")
nonisolated static let ui = AppLogger(subsystem: subsystem, category: "ui") nonisolated static let ui = AppLogger(subsystem: subsystem, category: "ui")
nonisolated static let general = AppLogger(subsystem: subsystem, category: "general") nonisolated static let general = AppLogger(subsystem: subsystem, category: "general")
nonisolated static let extMcp = AppLogger(subsystem: subsystem, category: "ext-mcp")
} }
+117 -32
View File
@@ -360,7 +360,6 @@ Don't narrate future actions ("Let me...") - just use the tools.
} }
func startAutoContinue() { func startAutoContinue() {
showSystemMessage("↩ Continuing…")
silentContinuePrompt = "Please continue from where you left off." silentContinuePrompt = "Please continue from where you left off."
Task { @MainActor in Task { @MainActor in
generateAIResponse(to: "", attachments: nil) generateAIResponse(to: "", attachments: nil)
@@ -799,8 +798,18 @@ Don't narrate future actions ("Let me...") - just use the tools.
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 bashActive = settings.bashEnabled
let personalDataActive = settings.calendarEnabled || settings.remindersEnabled || settings.contactsEnabled || settings.locationMapsEnabled
let researchAgentsActive = settings.agentsEnabled
let externalMCPActive = !settings.externalMCPServers.filter { $0.isEnabled }.isEmpty
// Dedicated images API path (OpenRouter /images endpoint separate from chat completions)
if selectedModel?.capabilities.usesImagesAPI == true,
let orProvider = provider as? OpenRouterProvider {
generateImageAPIResponse(orProvider: orProvider, modelId: modelId, prompt: prompt)
return
}
let modelSupportTools = selectedModel?.capabilities.tools ?? false let modelSupportTools = selectedModel?.capabilities.tools ?? false
if modelSupportTools && (anytypeActive || bashActive || (mcpActive && !mcp.allowedFolders.isEmpty)) { if modelSupportTools && (anytypeActive || bashActive || personalDataActive || researchAgentsActive || externalMCPActive || (mcpActive && !mcp.allowedFolders.isEmpty)) {
generateAIResponseWithTools(provider: provider, modelId: modelId) generateAIResponseWithTools(provider: provider, modelId: modelId)
return return
} }
@@ -1261,6 +1270,55 @@ Don't narrate future actions ("Let me...") - just use the tools.
// MARK: - AI Response with Tool Calls // MARK: - AI Response with Tool Calls
// MARK: - Images API Generation
private func generateImageAPIResponse(orProvider: OpenRouterProvider, modelId: String, prompt: String) {
isGenerating = true
streamingTask?.cancel()
streamingTask = Task {
let startTime = Date()
let assistantMessage = Message(
role: .assistant,
content: ThinkingVerbs.random(),
tokens: nil,
cost: nil,
timestamp: Date(),
attachments: nil,
modelId: modelId,
isStreaming: true
)
let messageId = assistantMessage.id
messages.append(assistantMessage)
do {
let response = try await orProvider.generateImage(model: modelId, prompt: prompt)
let responseTime = Date().timeIntervalSince(startTime)
if let index = messages.firstIndex(where: { $0.id == messageId }) {
messages[index].content = response.content
messages[index].isStreaming = false
messages[index].generatedImages = response.generatedImages
messages[index].responseTime = responseTime
if let usage = response.usage {
messages[index].tokens = usage.completionTokens
let cost = usage.rawCostUSD
messages[index].cost = cost
sessionStats.addMessage(inputTokens: usage.promptTokens, outputTokens: usage.completionTokens, cost: cost)
}
}
_ = detectGoodbyePhrase(in: "")
} catch {
if let index = messages.firstIndex(where: { $0.id == messageId }) {
messages[index].content = "❌ Image generation failed: \(error.localizedDescription)"
messages[index].isStreaming = false
}
Log.api.error("Images API error: \(error)")
}
isGenerating = false
}
}
private func generateAIResponseWithTools(provider: AIProvider, modelId: String) { private func generateAIResponseWithTools(provider: AIProvider, modelId: String) {
let mcp = MCPService.shared let mcp = MCPService.shared
Log.ui.info("generateAIResponseWithTools: model=\(modelId)") Log.ui.info("generateAIResponseWithTools: model=\(modelId)")
@@ -1302,6 +1360,12 @@ Don't narrate future actions ("Let me...") - just use the tools.
systemParts.append("You have access to the user's Anytype knowledge base through tool calls (anytype_* tools). You can search across all spaces, list spaces, get objects, and create or update notes, tasks, and pages. Use these tools proactively when the user asks about their notes, tasks, or knowledge base.") systemParts.append("You have access to the user's Anytype knowledge base through tool calls (anytype_* tools). You can search across all spaces, list spaces, get objects, and create or update notes, tasks, and pages. Use these tools proactively when the user asks about their notes, tasks, or knowledge base.")
} }
let activeExternalServers = settings.externalMCPServers.filter { $0.isEnabled }
if !activeExternalServers.isEmpty {
let names = activeExternalServers.map { $0.name }.joined(separator: ", ")
systemParts.append("You have access to external tools from MCP servers: \(names). Their tools are prefixed with the server slug (e.g. safari_navigate_to_url). Use them proactively when the user's request relates to what those servers provide.")
}
var systemContent = systemParts.joined(separator: "\n\n") var systemContent = systemParts.joined(separator: "\n\n")
// Append the complete system prompt (default + custom) // Append the complete system prompt (default + custom)
@@ -1354,6 +1418,7 @@ Don't narrate future actions ("Let me...") - just use the tools.
var didContinueAfterImages = false // Only inject temp-file continuation once var didContinueAfterImages = false // Only inject temp-file continuation once
var totalUsage: ChatResponse.Usage? var totalUsage: ChatResponse.Usage?
var hitIterationLimit = false // Track if we exited due to hitting the limit var hitIterationLimit = false // Track if we exited due to hitting the limit
var finishedWithEmptyContent = false // Model stopped calling tools but said nothing
for iteration in 0..<maxIterations { for iteration in 0..<maxIterations {
if Task.isCancelled { if Task.isCancelled {
@@ -1406,6 +1471,12 @@ Don't narrate future actions ("Let me...") - just use the tools.
continue continue
} }
} }
if finalContent.isEmpty {
// Some models (observed with Qwen via OpenRouter) stop calling tools
// after a long tool-call chain but return no summarizing text at all.
// Silently nudge a follow-up turn instead of showing a placeholder bubble.
finishedWithEmptyContent = true
}
break break
} }
@@ -1452,7 +1523,7 @@ Don't narrate future actions ("Let me...") - just use the tools.
break break
} }
let result = await mcp.executeTool(name: tc.functionName, arguments: tc.arguments) let result = await mcp.executeTool(name: tc.functionName, arguments: tc.arguments, agentProvider: provider, agentModelId: effectiveModelId)
let resultJSON: String let resultJSON: String
if let data = try? JSONSerialization.data(withJSONObject: result), if let data = try? JSONSerialization.data(withJSONObject: result),
let str = String(data: data, encoding: .utf8) { let str = String(data: data, encoding: .utf8) {
@@ -1493,9 +1564,7 @@ Don't narrate future actions ("Let me...") - just use the tools.
// If this was the last iteration, note it // If this was the last iteration, note it
if iteration == maxIterations - 1 { if iteration == maxIterations - 1 {
hitIterationLimit = true // We're exiting with pending tool calls hitIterationLimit = true // We're exiting with pending tool calls
finalContent = response.content.isEmpty finalContent = response.content
? "[Tool loop reached maximum iterations]"
: response.content
} }
} }
@@ -1504,41 +1573,57 @@ Don't narrate future actions ("Let me...") - just use the tools.
wasCancelled = true wasCancelled = true
} }
// If we hit the iteration limit or the model returned no text at all, silently
// nudge a follow-up turn instead of showing a placeholder/blank bubble.
let willAutoContinue = (hitIterationLimit || finishedWithEmptyContent) && !wasCancelled
// Display the final response as an assistant message // Display the final response as an assistant message
let responseTime = Date().timeIntervalSince(startTime) let responseTime = Date().timeIntervalSince(startTime)
let assistantMessage = Message( if willAutoContinue && finalContent.isEmpty {
role: .assistant, // Nothing worth showing yet still record usage/cost for this turn.
content: finalContent, if let usage = totalUsage, let model = selectedModel {
tokens: totalUsage?.completionTokens, let hasPricing = model.pricing.prompt > 0 || model.pricing.completion > 0
cost: nil, let cost: Double? = hasPricing ? calculateCost(usage: usage, pricing: model.pricing) : nil
timestamp: Date(), sessionStats.addMessage(
attachments: nil, inputTokens: usage.promptTokens,
responseTime: responseTime, outputTokens: usage.completionTokens,
wasInterrupted: wasCancelled, cost: cost
modelId: modelId, )
generatedImages: finalImages.isEmpty ? nil : finalImages
)
messages.append(assistantMessage)
// Calculate cost
if let usage = totalUsage, let model = selectedModel {
let hasPricing = model.pricing.prompt > 0 || model.pricing.completion > 0
let cost: Double? = hasPricing ? calculateCost(usage: usage, pricing: model.pricing) : nil
if let index = messages.lastIndex(where: { $0.id == assistantMessage.id }) {
messages[index].cost = cost
} }
sessionStats.addMessage( } else {
inputTokens: usage.promptTokens, let assistantMessage = Message(
outputTokens: usage.completionTokens, role: .assistant,
cost: cost content: finalContent,
tokens: totalUsage?.completionTokens,
cost: nil,
timestamp: Date(),
attachments: nil,
responseTime: responseTime,
wasInterrupted: wasCancelled,
modelId: modelId,
generatedImages: finalImages.isEmpty ? nil : finalImages
) )
messages.append(assistantMessage)
// Calculate cost
if let usage = totalUsage, let model = selectedModel {
let hasPricing = model.pricing.prompt > 0 || model.pricing.completion > 0
let cost: Double? = hasPricing ? calculateCost(usage: usage, pricing: model.pricing) : nil
if let index = messages.lastIndex(where: { $0.id == assistantMessage.id }) {
messages[index].cost = cost
}
sessionStats.addMessage(
inputTokens: usage.promptTokens,
outputTokens: usage.completionTokens,
cost: cost
)
}
} }
isGenerating = false isGenerating = false
streamingTask = nil streamingTask = nil
// If we hit the iteration limit and weren't cancelled, start auto-continue if willAutoContinue {
if hitIterationLimit && !wasCancelled {
startAutoContinue() startAutoContinue()
} }
+10
View File
@@ -123,6 +123,16 @@ struct ChatView: View {
onDeny: { MCPService.shared.denyPendingBashCommand() } onDeny: { MCPService.shared.denyPendingBashCommand() }
) )
} }
.sheet(item: Binding(
get: { MCPService.shared.pendingPersonalDataAction },
set: { _ in }
)) { pending in
PersonalDataApprovalSheet(
pending: pending,
onApprove: { forSession in MCPService.shared.approvePendingPersonalDataAction(forSession: forSession) },
onDeny: { MCPService.shared.denyPendingPersonalDataAction() }
)
}
} }
} }
+7
View File
@@ -61,8 +61,15 @@ struct MarkdownContentView: View {
.markdownBlockStyle(\.paragraph) { configuration in .markdownBlockStyle(\.paragraph) { configuration in
configuration.label configuration.label
.markdownMargin(top: 0, bottom: 8) .markdownMargin(top: 0, bottom: 8)
// MarkdownUI builds mixed-style paragraphs (bold/italic runs alongside
// plain text) as concatenated Text(+) segments, which on macOS report
// their ideal (unwrapped, single-line) size instead of wrapping to the
// width actually available truncating with "" mid-word. Forcing the
// height to be recomputed for the given (flexible) width fixes it.
.fixedSize(horizontal: false, vertical: true)
} }
.textSelection(.enabled) .textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
} }
// MARK: - Parsing // MARK: - Parsing
+1
View File
@@ -185,6 +185,7 @@ struct MessageRow: View {
.foregroundColor(.oaiSecondary) .foregroundColor(.oaiSecondary)
} }
} }
.frame(maxWidth: .infinity, alignment: .leading)
} }
.padding(16) .padding(16)
.background(Color.messageBackground(for: message.role)) .background(Color.messageBackground(for: message.role))
+16 -2
View File
@@ -113,7 +113,9 @@ struct AgentSkillsView: View {
onToggle: { settings.toggleAgentSkill(id: skill.id) }, onToggle: { settings.toggleAgentSkill(id: skill.id) },
onEdit: { editContext = SkillEditContext(skill: skill) }, onEdit: { editContext = SkillEditContext(skill: skill) },
onExport: { exportOne(skill) }, onExport: { exportOne(skill) },
onDelete: { settings.deleteAgentSkill(id: skill.id) } onDelete: { settings.deleteAgentSkill(id: skill.id) },
isTrusted: settings.trustSecondBrainSkill,
onToggleTrust: { settings.trustSecondBrainSkill.toggle() }
) )
} }
} }
@@ -349,6 +351,8 @@ private struct AgentSkillRow: View {
let onEdit: () -> Void let onEdit: () -> Void
let onExport: () -> Void let onExport: () -> Void
let onDelete: () -> Void let onDelete: () -> Void
var isTrusted: Bool = false
var onToggleTrust: (() -> Void)? = nil
private var fileCount: Int { private var fileCount: Int {
AgentSkillFilesService.shared.listFiles(for: skill.id).count AgentSkillFilesService.shared.listFiles(for: skill.id).count
@@ -378,6 +382,14 @@ private struct AgentSkillRow: View {
Spacer() Spacer()
// 2nd Brain: let the user mark bash calls to its helper script as always-trusted,
// skipping the bash approval dialog. Only shown for this specific skill while active.
if skill.isActive, skill.isSecondBrainSkill, let onToggleTrust {
Toggle("Trust", isOn: Binding(get: { isTrusted }, set: { _ in onToggleTrust() }))
.toggleStyle(.switch).controlSize(.small)
.help("When on, bash commands that call the 2nd Brain helper script run without asking for approval each time.")
}
// File count badge // File count badge
if fileCount > 0 { if fileCount > 0 {
Label("^[\(fileCount) file](inflect: true)", systemImage: "doc") Label("^[\(fileCount) file](inflect: true)", systemImage: "doc")
@@ -479,7 +491,9 @@ struct AgentSkillsTabContent: View {
onToggle: { settings.toggleAgentSkill(id: skill.id) }, onToggle: { settings.toggleAgentSkill(id: skill.id) },
onEdit: { editContext = SkillEditContext(skill: skill) }, onEdit: { editContext = SkillEditContext(skill: skill) },
onExport: { exportOne(skill) }, onExport: { exportOne(skill) },
onDelete: { settings.deleteAgentSkill(id: skill.id) } onDelete: { settings.deleteAgentSkill(id: skill.id) },
isTrusted: settings.trustSecondBrainSkill,
onToggleTrust: { settings.trustSecondBrainSkill.toggle() }
) )
if idx < settings.agentSkills.count - 1 { Divider() } if idx < settings.agentSkills.count - 1 { Divider() }
} }
+1 -1
View File
@@ -52,7 +52,7 @@ private let helpCategories: [CommandCategory] = [
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" shortcut: "⌘H"
), ),
CommandDetail( CommandDetail(
command: "/clear", command: "/clear",
@@ -0,0 +1,97 @@
//
// PersonalDataApprovalSheet.swift
// oAI
//
// Approval UI for AI-requested Calendar/Reminders write actions
//
// 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 PersonalDataApprovalSheet: View {
let pending: MCPService.PendingPersonalDataAction
let onApprove: (_ forSession: Bool) -> Void
let onDeny: () -> Void
var body: some View {
VStack(alignment: .leading, spacing: 20) {
// Header
HStack(spacing: 12) {
Image(systemName: "calendar.badge.exclamationmark")
.font(.title2)
.foregroundStyle(.orange)
VStack(alignment: .leading, spacing: 2) {
Text("Allow This Action?")
.font(.system(size: 17, weight: .semibold))
Text("The AI wants to make a change to your calendar or reminders")
.font(.system(size: 13))
.foregroundStyle(.secondary)
}
Spacer()
}
// Action description
VStack(alignment: .leading, spacing: 6) {
Text("ACTION")
.font(.system(size: 11, weight: .medium))
.foregroundStyle(.secondary)
Text(pending.summary)
.font(.system(size: 13))
.foregroundStyle(.primary)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
.padding(12)
.background(Color.secondary.opacity(0.08))
.clipShape(RoundedRectangle(cornerRadius: 8))
.overlay(
RoundedRectangle(cornerRadius: 8)
.stroke(Color.secondary.opacity(0.2), lineWidth: 1)
)
}
// 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)
}
}
+426
View File
@@ -75,6 +75,20 @@ struct SettingsView: View {
// Default model picker state // Default model picker state
@State private var showDefaultModelPicker = false @State private var showDefaultModelPicker = false
// External MCP Servers state
@State private var showAddExternalMCPServer = false
@State private var newMCPServerName = ""
@State private var newMCPServerCommand = ""
@State private var newMCPServerArgs = ""
@State private var newMCPServerTimeout: Double = 30
private var externalMCPManager = ExternalMCPManager.shared
// Personal Data state (Calendar/Reminders/Contacts/Location/Maps)
@State private var calendarAccessState = EventKitService.shared.calendarAccessState
@State private var remindersAccessState = EventKitService.shared.reminderAccessState
@State private var contactsAccessState = ContactsService.shared.accessState
@State private var locationAccessState = LocationMapsService.shared.accessState
// Paperless-NGX state // Paperless-NGX state
@State private var paperlessURL = "" @State private var paperlessURL = ""
@State private var paperlessToken = "" @State private var paperlessToken = ""
@@ -752,6 +766,367 @@ It's better to admit "I need more information" or "I cannot do that" than to fak
.padding(.horizontal, 4) .padding(.horizontal, 4)
} }
} }
// MARK: Research Agents
Divider()
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "person.3.fill")
.font(.title2)
.foregroundStyle(.indigo)
Text("Research Agents")
.font(.system(size: 18, weight: .semibold))
}
Text("Let the AI spawn read-only research sub-agents to investigate multiple things in parallel (read files, list/search directories, search the web — no writing, no bash). Intended for genuinely independent research tasks, not everyday questions.")
.font(.system(size: 14))
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.bottom, 4)
VStack(alignment: .leading, spacing: 6) {
sectionHeader("Status")
formSection {
row("Enable Research Agents") {
Toggle("", isOn: $settingsService.agentsEnabled)
.toggleStyle(.switch)
}
}
}
HStack(alignment: .top, spacing: 6) {
Image(systemName: "exclamationmark.triangle.fill")
.font(.system(size: 12))
.foregroundStyle(.orange)
.padding(.top, 1)
Text("Cost warning: each sub-agent runs its own full chain of model calls. A single request that spawns several agents can cost several times a normal reply. The AI is instructed to only use this for genuinely parallel research, but model behavior can vary — leave this off unless you want that tradeoff.")
.font(.system(size: 13))
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.horizontal, 4)
if settingsService.agentsEnabled {
VStack(alignment: .leading, spacing: 6) {
sectionHeader("Settings")
formSection {
row("Max Concurrent Agents") {
HStack(spacing: 8) {
Stepper("", value: $settingsService.maxConcurrentAgents, in: 1...5)
.labelsHidden()
Text("\(settingsService.maxConcurrentAgents)")
.font(.system(size: 13))
.foregroundStyle(.secondary)
.frame(width: 24, alignment: .trailing)
}
}
}
}
}
// MARK: External MCP Servers
Divider()
externalMCPSection
// MARK: Personal Data
// isHiddenPendingAppleFix hides the entire section (macOS 27 beta TCC bug).
// isContactsHiddenPendingAppleFix hides just the Contacts row (still broken in beta 2
// under hardened runtime while Calendar/Reminders/Location are fixed). Flip each flag
// to false once Apple ships a fix.
if !PersonalDataTools.isHiddenPendingAppleFix {
Divider()
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "person.crop.circle.badge.checkmark")
.font(.title2)
.foregroundStyle(.teal)
Text("Personal Data")
.font(.system(size: 18, weight: .semibold))
if PersonalDataTools.isContactsHiddenPendingAppleFix {
Text("β")
.font(.system(size: 11, weight: .bold))
.foregroundStyle(.orange)
.padding(.horizontal, 5)
.padding(.vertical, 2)
.background(Color.orange.opacity(0.15))
.clipShape(RoundedRectangle(cornerRadius: 4))
.help("Beta Feature. May change.")
}
}
Text("Let the AI access your Calendar, Reminders, and Location & Maps to answer questions about your schedule and surroundings. Each service is opt-in and uses standard macOS permission prompts. This functionality is in beta and may change.")
.font(.system(size: 14))
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.bottom, 4)
.onAppear {
// Permission status can change outside the app (System Settings, or a prior
// request elsewhere) re-read it fresh every time this tab appears rather than
// trusting the one-time @State initializer.
calendarAccessState = EventKitService.shared.calendarAccessState
remindersAccessState = EventKitService.shared.reminderAccessState
contactsAccessState = ContactsService.shared.accessState
locationAccessState = LocationMapsService.shared.accessState
}
VStack(alignment: .leading, spacing: 6) {
sectionHeader("Services")
formSection {
personalDataRow(
title: "Calendar",
isEnabled: $settingsService.calendarEnabled,
state: calendarAccessState,
systemSettingsAnchor: "Privacy_Calendars",
requestAccess: { calendarAccessState = await EventKitService.shared.requestCalendarAccess() ? .granted : EventKitService.shared.calendarAccessState }
)
rowDivider()
personalDataRow(
title: "Reminders",
isEnabled: $settingsService.remindersEnabled,
state: remindersAccessState,
systemSettingsAnchor: "Privacy_Reminders",
requestAccess: { remindersAccessState = await EventKitService.shared.requestReminderAccess() ? .granted : EventKitService.shared.reminderAccessState }
)
rowDivider()
if !PersonalDataTools.isContactsHiddenPendingAppleFix {
personalDataRow(
title: "Contacts",
isEnabled: $settingsService.contactsEnabled,
state: contactsAccessState,
systemSettingsAnchor: "Privacy_Contacts",
requestAccess: { contactsAccessState = await ContactsService.shared.requestAccess() ? .granted : ContactsService.shared.accessState }
)
rowDivider()
}
personalDataRow(
title: "Location & Maps",
isEnabled: $settingsService.locationMapsEnabled,
state: locationAccessState,
systemSettingsAnchor: "Privacy_LocationServices",
requestAccess: { locationAccessState = await LocationMapsService.shared.requestAccess() ? .granted : LocationMapsService.shared.accessState }
)
}
}
if settingsService.calendarEnabled || settingsService.remindersEnabled {
VStack(alignment: .leading, spacing: 6) {
sectionHeader("Write Actions")
formSection {
row("Require Approval for Changes") {
Toggle("", isOn: $settingsService.personalDataRequireApproval)
.toggleStyle(.switch)
}
}
}
Text("Creating calendar events or reminders, and completing reminders, will ask for your approval first.")
.font(.system(size: 13))
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
.padding(.horizontal, 4)
}
}
}
// MARK: - External MCP Servers Section
@ViewBuilder
private var externalMCPSection: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 8) {
Image(systemName: "server.rack")
.font(.title2)
.foregroundStyle(.purple)
Text("External MCP Servers")
.font(.system(size: 18, weight: .semibold))
}
Text("Connect any stdio MCP server (e.g. safaridriver --mcp) to give the AI access to its tools. Tool names are prefixed with the server slug.")
.font(.system(size: 14))
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.bottom, 4)
VStack(alignment: .leading, spacing: 6) {
sectionHeader("Configured Servers")
formSection {
if settingsService.externalMCPServers.isEmpty {
VStack(spacing: 8) {
Image(systemName: "server.rack")
.font(.system(size: 32))
.foregroundStyle(.tertiary)
Text("No external servers configured")
.font(.system(size: 14, weight: .medium))
.foregroundStyle(.secondary)
Text("Add a server below, e.g.: safaridriver --mcp")
.font(.system(size: 12))
.foregroundStyle(.tertiary)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 20)
} else {
ForEach(settingsService.externalMCPServers) { server in
HStack(spacing: 10) {
Circle()
.fill(mcpStatusColor(externalMCPManager.clientStates[server.id]))
.frame(width: 8, height: 8)
VStack(alignment: .leading, spacing: 2) {
Text(server.name)
.font(.system(size: 14))
Text(([server.command] + server.args).joined(separator: " "))
.font(.system(size: 11, design: .monospaced))
.foregroundStyle(.secondary)
.lineLimit(1)
}
Spacer()
Text(mcpStatusLabel(externalMCPManager.clientStates[server.id]))
.font(.system(size: 11))
.foregroundStyle(.secondary)
Toggle("", isOn: Binding(
get: { server.isEnabled },
set: { _ in settingsService.toggleExternalMCPServer(id: server.id) }
))
.toggleStyle(.switch)
.labelsHidden()
Button {
settingsService.deleteExternalMCPServer(id: server.id)
} label: {
Image(systemName: "trash.fill")
.foregroundStyle(.red)
.font(.system(size: 13))
}
.buttonStyle(.plain)
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
if server.id != settingsService.externalMCPServers.last?.id {
rowDivider()
}
}
}
}
Button {
newMCPServerName = ""
newMCPServerCommand = ""
newMCPServerArgs = ""
newMCPServerTimeout = 30
showAddExternalMCPServer = true
} label: {
Label("Add Server…", systemImage: "plus")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(.white)
.padding(.horizontal, 14)
.padding(.vertical, 7)
.background(Color.purple)
.clipShape(RoundedRectangle(cornerRadius: 8))
}
.buttonStyle(.plain)
}
.sheet(isPresented: $showAddExternalMCPServer) {
addExternalMCPServerSheet
}
}
@ViewBuilder
private var addExternalMCPServerSheet: some View {
VStack(alignment: .leading, spacing: 20) {
Text("Add External MCP Server")
.font(.system(size: 16, weight: .semibold))
.frame(maxWidth: .infinity, alignment: .center)
formSection {
row("Name") {
TextField("Safari", text: $newMCPServerName)
.textFieldStyle(.roundedBorder)
.frame(width: 240)
}
rowDivider()
row("Command") {
TextField("safaridriver", text: $newMCPServerCommand)
.textFieldStyle(.roundedBorder)
.font(.system(size: 13, design: .monospaced))
.frame(width: 240)
}
rowDivider()
row("Arguments") {
TextField("--mcp", text: $newMCPServerArgs)
.textFieldStyle(.roundedBorder)
.font(.system(size: 13, design: .monospaced))
.frame(width: 240)
.help("Space-separated arguments")
}
rowDivider()
row("Timeout") {
HStack(spacing: 8) {
Stepper("", value: $newMCPServerTimeout, in: 5...120, step: 5)
.labelsHidden()
Text("\(Int(newMCPServerTimeout))s")
.font(.system(size: 13))
.foregroundStyle(.secondary)
.frame(width: 32, alignment: .leading)
}
}
}
if !newMCPServerName.isEmpty {
let slug = ExternalMCPServer.makeSlug(from: newMCPServerName)
HStack(spacing: 6) {
Text("Tool prefix:")
.font(.system(size: 12))
.foregroundStyle(.secondary)
Text("\(slug)_")
.font(.system(size: 12, design: .monospaced))
.foregroundStyle(.secondary)
}
if ExternalMCPServer.reservedSlugs.contains(slug) {
Text("'\(slug)' is a reserved prefix. Choose a different name.")
.font(.system(size: 12))
.foregroundStyle(.red)
}
}
HStack {
Button("Cancel") { showAddExternalMCPServer = false }
Spacer()
Button("Add") {
let args = ExternalMCPServer.parseArguments(newMCPServerArgs)
let server = ExternalMCPServer(
name: newMCPServerName,
command: newMCPServerCommand,
args: args,
timeout: newMCPServerTimeout
)
settingsService.addExternalMCPServer(server)
showAddExternalMCPServer = false
}
.buttonStyle(.borderedProminent)
.disabled(newMCPServerName.isEmpty || newMCPServerCommand.isEmpty ||
ExternalMCPServer.reservedSlugs.contains(ExternalMCPServer.makeSlug(from: newMCPServerName)))
}
}
.padding(24)
.frame(minWidth: 460, minHeight: 320)
}
private func mcpStatusColor(_ state: MCPClientState?) -> Color {
switch state {
case .ready: return .green
case .connecting: return .orange
case .error, .crashed: return .red
default: return Color(nsColor: .tertiaryLabelColor)
}
}
private func mcpStatusLabel(_ state: MCPClientState?) -> LocalizedStringKey {
switch state {
case .ready: return "Connected"
case .connecting: return "Connecting…"
case .error: return "Error"
case .crashed: return "Crashed"
default: return "Not started"
}
} }
// MARK: - Appearance Tab // MARK: - Appearance Tab
@@ -2446,6 +2821,57 @@ It's better to admit "I need more information" or "I cannot do that" than to fak
Divider().padding(.leading, 16) Divider().padding(.leading, 16)
} }
@ViewBuilder
private func personalDataRow(title: LocalizedStringKey, isEnabled: Binding<Bool>, state: PersonalDataAccessState, systemSettingsAnchor: String, requestAccess: @escaping () async -> Void) -> some View {
VStack(alignment: .leading, spacing: 6) {
HStack(alignment: .center, spacing: 12) {
Text(title).font(.system(size: 14))
Spacer()
Toggle("", isOn: isEnabled)
.toggleStyle(.switch)
}
if isEnabled.wrappedValue {
HStack(spacing: 6) {
Image(systemName: state == .granted ? "checkmark.circle.fill" : (state == .denied ? "exclamationmark.circle.fill" : "circle"))
.foregroundStyle(state == .granted ? .green : (state == .denied ? .orange : .secondary))
.font(.system(size: 12))
Text(statusText(for: state))
.font(.system(size: 12))
.foregroundStyle(.secondary)
Spacer()
if state == .notDetermined {
Button("Request Access") {
Task { await requestAccess() }
}
.buttonStyle(.bordered)
.controlSize(.small)
} else if state == .denied {
Button("Open System Settings") {
openPrivacySettings(anchor: systemSettingsAnchor)
}
.buttonStyle(.bordered)
.controlSize(.small)
}
}
}
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
}
private func statusText(for state: PersonalDataAccessState) -> LocalizedStringKey {
switch state {
case .granted: return "Access granted"
case .denied: return "Access denied — enable in System Settings"
case .notDetermined: return "Access not granted"
}
}
private func openPrivacySettings(anchor: String) {
guard let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?\(anchor)") else { return }
NSWorkspace.shared.open(url)
}
private func abbreviatePath(_ path: String) -> String { private func abbreviatePath(_ path: String) -> String {
let home = NSHomeDirectory() let home = NSHomeDirectory()
if path.hasPrefix(home) { if path.hasPrefix(home) {
+48
View File
@@ -36,6 +36,54 @@
} }
} }
} }
},
"NSCalendarsFullAccessUsageDescription" : {
"comment" : "Privacy - Calendars Full Access Usage Description",
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "oAI can read and create calendar events when you ask it to, if you enable Calendar access in Settings."
}
}
}
},
"NSContactsUsageDescription" : {
"comment" : "Privacy - Contacts Usage Description",
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "oAI can search your contacts when you ask it to, if you enable Contacts access in Settings."
}
}
}
},
"NSLocationWhenInUseUsageDescription" : {
"comment" : "Privacy - Location When In Use Usage Description",
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "oAI can use your current location to answer questions, if you enable Location & Maps access in Settings."
}
}
}
},
"NSRemindersFullAccessUsageDescription" : {
"comment" : "Privacy - Reminders Full Access Usage Description",
"extractionState" : "extracted_with_value",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "oAI can read and create reminders when you ask it to, if you enable Reminders access in Settings."
}
}
}
} }
}, },
"version" : "1.1" "version" : "1.1"
+8
View File
@@ -8,5 +8,13 @@
<true/> <true/>
<key>com.apple.security.network.server</key> <key>com.apple.security.network.server</key>
<false/> <false/>
<key>com.apple.security.personal-information.calendars</key>
<true/>
<key>com.apple.security.personal-information.reminders</key>
<true/>
<key>com.apple.security.personal-information.contacts</key>
<true/>
<key>com.apple.security.personal-information.location</key>
<true/>
</dict> </dict>
</plist> </plist>
+5 -2
View File
@@ -37,6 +37,9 @@ struct oAIApp: App {
// Start email handler on app launch // Start email handler on app launch
EmailHandlerService.shared.start() EmailHandlerService.shared.start()
// Start external MCP servers
Task { @MainActor in ExternalMCPManager.shared.startAll() }
// Sync Git changes on app launch (pull + import) // Sync Git changes on app launch (pull + import)
Task { Task {
await GitSyncService.shared.syncOnStartup() await GitSyncService.shared.syncOnStartup()
@@ -56,7 +59,7 @@ struct oAIApp: App {
} }
#if os(macOS) #if os(macOS)
.onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)) { _ in .onReceive(NotificationCenter.default.publisher(for: NSApplication.willTerminateNotification)) { _ in
// Trigger auto-save on app quit Task { @MainActor in ExternalMCPManager.shared.stopAll() }
Task { Task {
await chatViewModel.onAppWillTerminate() await chatViewModel.onAppWillTerminate()
} }
@@ -129,7 +132,7 @@ struct oAIApp: App {
Divider() Divider()
Button("Command History") { chatViewModel.showHistory = true } Button("Command History") { chatViewModel.showHistory = true }
.keyboardShortcut("h", modifiers: .command) .keyboardShortcut("h", modifiers: [.command, .shift])
Button("In-App Help") { chatViewModel.showHelp = true } Button("In-App Help") { chatViewModel.showHelp = true }
.keyboardShortcut("/", modifiers: .command) .keyboardShortcut("/", modifiers: .command)