Command and rewrite modes
Command Mode turns a request into a language-model task that can inspect and change the Mac through terminal commands. Rewrite Mode turns an instruction into new text or a revision of text selected in another app. Both modes connect voice input, prompt settings, model choice, and output delivery to the wider AI assistance system.
Mode boundaries
Command Mode is an agent flow for tasks that may need work on the Mac. The model can return an answer or request a terminal action. The service keeps the request, actions, results, and final response in one conversation so the user can follow the task.
Rewrite Mode is a text transformation flow. It can create text from a request when no source text exists, or apply an instruction to text selected in another app. The view calls this feature Edit Mode because both uses share the same edit prompt and output path.
Command mode
Command Mode presents a chat with voice and text input. It also provides recent chats, provider and model controls, progress states, and an optional confirmation gate for risky actions. The mode is marked Alpha in the user interface.
The command service owns the agent turn loop. It sends the conversation to the model, handles a terminal function call when one is returned, adds the command result to the conversation, and asks the model what to do next. A plain text response ends the current task. The service can use the shared model client or the Apple Intelligence system model when that provider is available. It also protects a voice request from stale overlay work, so an invalidated request does not add later model output to the active chat.
Command conversations can be created, switched, cleared, archived, restored, and deleted when no command is running. The active conversation remains stable while a command or confirmation is pending.
References: Sources/…/CommandModeService.swift• L5-21 Sources/…/CommandModeService.swift• L116-242 Sources/…/CommandModeService.swift• L363-412 Sources/…/CommandModeService.swift• L511-652 Sources/…/AppleIntelligenceCommandProvider.swift• L108-119
Rewrite mode
Rewrite Mode presents source text and transformed text as separate states. The user can inspect the result, try another instruction, or choose Replace Original. The service does not replace the source until the result is accepted.
The mode uses one shared edit concept for writing and rewriting. Older write and rewrite prompt values resolve to the current edit prompt, which keeps prompt selection consistent across these uses. The service can also read the target window for private or local edit routes when that setting is enabled. It records prompt-processing diagnostics through the shared debug logger only when rewrite diagnostics or prompt tracing is enabled.
References: Sources/…/RewriteModeService.swift• L7-49 Sources/…/RewriteModeService.swift• L62-94 Sources/…/RewriteModeService.swift• L207-238 Sources/…/RewriteModeService.swift• L477-487 Sources/…/RewriteModeView.swift• L21-57 Sources/…/RewriteModeView.swift• L61-145 Sources/…/SettingsStore.swift• L113-145
Invocation and input handling
The application shell selects the mode before it starts audio capture. It keeps that choice with the recording until the final speech text is routed to the matching service. This prevents a command from entering the rewrite flow or a rewrite instruction from entering normal dictation.
Mode invocation
A Command Mode shortcut starts command recording. A Rewrite Mode shortcut first captures the current selection while the other app is active, then starts edit recording. A live switch to edit mode uses the same selection check when no edit context is already present.
The shell also records the target application at the start of the session. If the mode opens an overlay and changes focus, later output can still return to the application that received the original request.
References: Sources/…/ContentView.swift• L3101-3180 Sources/…/ContentView.swift• L3483-3556 Sources/…/ContentView.swift• L2089-2133
Voice and text input
Both views accept typed input and voice input. The speech recognizer supplies final text to the same request path used by the text field, so spoken and typed requests follow the same service behavior.
Command Mode sends non-empty text to the agent loop. Rewrite Mode clears its input before it starts the asynchronous transformation and disables its main send control while processing. Command Mode also places completed speech into the input field before submission, which keeps the captured request visible during handoff.
References: Sources/…/CommandModeView.swift• L76-85 Sources/…/CommandModeView.swift• L557-667 Sources/…/RewriteModeView.swift• L239-272 Sources/…/ContentView.swift• L3056-3075 Sources/…/ContentView.swift• L3183-3199
Text selection context
Selected text is the source context for a rewrite. TextSelectionService asks macOS for the focused element and its selected text. If the element does not expose selected text directly, the service can rebuild it from the selected range and the element's full value.
If accessibility access or a usable selection is not available, Rewrite Mode starts in write mode. The user's request then becomes the basis for generated text instead of an instruction applied to external text. The original context remains available until the user accepts the result.
When enabled for the selected edit route, the service also starts a short-lived read of the target window while that window is still in front. It discards the captured context if the user changes the setting, target, or edit session before the read completes. The service passes this window context only to routes that support it and logs counts rather than the window contents.
References: Sources/…/TextSelectionService.swift• L5-47 Sources/…/TextSelectionService.swift• L77-135 Sources/…/RewriteModeService.swift• L50-82 Sources/…/RewriteModeService.swift• L207-238 Sources/…/RewriteModeService.swift• L347-378 Sources/…/ContentView.swift• L3514-3539
Prompt and model selection
The two services use shared provider and prompt settings, but they send different kinds of requests. Rewrite Mode needs an edit prompt and optional selected context. Command Mode needs an agent prompt that permits terminal function calls and sets a safety-oriented work pattern.
Prompt resolution
Rewrite requests use the effective Edit prompt for the target application. Prompt resolution can select an app-specific profile, a chosen profile, a default override, or the built-in default. When selected text exists, the service places that text into the shared context template so the model receives both the instruction and the material to change.
The first rewrite request asks for only the transformed text. A later request refers to the prior result and asks for an updated version. This keeps the result ready for insertion instead of mixing it with an explanation.
References: Sources/…/RewriteModeService.swift• L96-166 Sources/…/RewriteModeService.swift• L247-305 Sources/…/SettingsStore.swift• L1094-1175 Sources/…/SettingsStore.swift• L1225-1231
Provider and model choice
Each mode can use its own provider and model. Command Mode can also follow the global AI Enhancement selection through its Sync setting. The views update their available model choices when the linked provider or shared AI selection changes. Provider contracts and model rules are covered in AI provider and model management.
Before a request starts, the services require a usable provider and model. Rewrite Mode also checks that a selected provider is verified and that a selected private model is installed and available through the private route. Command Mode reports Apple Intelligence availability separately when its system model cannot run because of the macOS version, device, system setting, or model readiness. The Apple route also keeps its native transcript within the system model's context limit. Each service sends the selected model, provider endpoint, prompt, and optional window context to the matching model service. A failed readiness check stays visible as a settings issue instead of producing an empty result.
References: Sources/…/CommandModeService.swift• L827-845 Sources/…/RewriteModeService.swift• L247-380 Sources/…/RewriteModeService.swift• L490-530 Sources/…/CommandModeView.swift• L86-97 Sources/…/CommandModeView.swift• L478-581 Sources/…/CommandModeView.swift• L669-681 Sources/…/RewriteModeView.swift• L173-197 Sources/…/AppleIntelligenceCommandProvider.swift• L7-43 Sources/…/AppleIntelligenceCommandProvider.swift• L167-186
Command execution loop
Command Mode gives the model a terminal function. The function call is the boundary between model intent and a local action. The service keeps that boundary visible to the user and returns structured command results to the model for the next decision.
Function-call requests
The terminal function is named execute_terminal_command. A call contains a shell command, an optional working directory, and a purpose. The purpose labels the call as a check, an action, or a result check. A plain model message means that no terminal action is needed.
The service turns a function call into a visible command step. It classifies the step as checking, executing, or verifying. Model thinking is display-only. It is not added to the tool conversation as an instruction. The Apple Intelligence provider exposes the same function contract through the system model and captures its call for the service to approve or run.
References: Sources/…/TerminalService.swift• L17-62 Sources/…/CommandModeService.swift• L547-570 Sources/…/CommandModeService.swift• L665-695 Sources/…/LLMClient.swift• L85-109 Sources/…/AppleIntelligenceCommandProvider.swift• L121-149
Terminal interaction
The terminal boundary runs the command with the user's macOS shell and returns a structured result. The result reports success, standard output, error output, exit status, elapsed time, and the purpose of the call. A time limit stops a command that does not finish.
After execution, the service adds the result to the conversation and calls the model again. The model can perform another check, take the next action, verify a change, or give a final summary. The Apple Intelligence route caps long output and error fields only in the transcript sent to the model. It leaves stored history unchanged. If the native model cannot fit the conversation, the provider drops the oldest complete request; if no request can be dropped, it asks the user to start a new chat. A turn limit also stops an unclear task from running without end. A command that already ran remains recorded even if the user cancels the later agent steps.
The flow below shows the path from a user request through model decisions and terminal results.
flowchart TD
A[User voice or text request] --> B[Command Mode service]
B --> C[Language model]
C -->|Text answer| D[Final conversation response]
C -->|Terminal function call| E{Risky command?}
E -->|Yes| F[Show command for approval]
E -->|No| G[Run terminal command]
F -->|Cancel| H[Record cancellation]
F -->|Approve| G
G --> I[Structured command result]
I --> C
D --> J[Show result to user]
H --> J
References: Sources/…/TerminalService.swift• L64-142 Sources/…/CommandModeService.swift• L739-775 Sources/…/CommandModeService.swift• L511-533 Sources/…/AppleIntelligenceCommandProvider.swift• L62-103 Sources/…/AppleIntelligenceCommandProvider.swift• L167-205
Command safety gate
The service pauses before commands that match its destructive-command rules. The pending command view shows the command and its purpose. The user can cancel it or approve it with an explicit action. The agent prompt also requires checks before changes and verification after changes.
References: Sources/…/CommandModeService.swift• L573-593 Sources/…/CommandModeService.swift• L697-737 Sources/…/CommandModeView.swift• L405-474 Sources/…/CommandModeService.swift• L847-927
Output delivery
The two modes return output through different user actions. Command Mode builds a conversation result and can mirror it in the notch overlay. Rewrite Mode presents a candidate transformation and then sends accepted text back to the app that was active at invocation.
Command conversation output
Command Mode displays user messages, assistant responses, command calls, and terminal results in its chat view. Streaming text and thinking text can appear while the model works. Tool results remain structured and selectable so the user can inspect success, errors, output, and timing.
The view scrolls when the conversation changes or a new command step starts. It does not scroll for every streamed token. Thinking text is optional and can be expanded for persisted messages. This keeps live command progress readable without letting frequent model updates disturb the user's position.
Command conversations are saved as chat sessions. The user can create, switch, clear, archive, restore, or delete chats. The notch view receives user and assistant messages when notch synchronization is enabled, while verbose tool output stays in the full command view.
References: Sources/…/CommandModeView.swift• L305-377 Sources/…/CommandModeView.swift• L738-928 Sources/…/CommandModeService.swift• L116-242 Sources/…/CommandModeService.swift• L340-361 Sources/…/ChatHistoryStore.swift• L54-87
Rewrite result delivery
In the full Edit Mode view, transformed text remains a candidate until Replace Original is selected. That action hides the overlay, restores focus to the app that received the request, and types the accepted text into its active field.
The rewrite service uses the recorded target context when it is available. It prepares that target before typing and otherwise waits briefly for focus to settle. A failed request leaves no replacement text to insert.
References: Sources/…/RewriteModeView.swift• L112-145 Sources/…/RewriteModeService.swift• L96-192 Sources/…/ContentView.swift• L3062-3098 Sources/…/TypingService.swift• L1203-1234
Focus and insertion boundary
Output delivery crosses from Fluid into another macOS app. The target process is captured before the overlay takes focus. The typing service then uses the active accessibility text field and several insertion paths to place text or replace the selected range.
This boundary is shared with other text output in the app. Accessibility permission, a stable target, and a field that accepts text are required for a reliable return path.
References: Sources/…/ContentView.swift• L3031-3047 Sources/…/RewriteModeService.swift• L170-192 Sources/…/TypingService.swift• L1246-1290 Sources/…/TypingService.swift• L1454-1541
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the command function name and its argument names aligned across the tool definition, model response parsing, conversation replay, and terminal execution.
- Keep command result fields aligned between
TerminalService.CommandResult, the enhanced result sent to the model, persisted chat messages, and the command result view. - Keep recording ownership, presentation activity, and output-validity checks aligned with shortcut routing. A change in start or stop order can stop another recording or add stale output after an overlay closes.
- Keep the active recording mode and target-process capture aligned with shortcut routing. A change in invocation order can lose the selected text or send output to the overlay instead of the source app.
- Keep Edit prompt resolution, selected-text context, and optional window context aligned with the route that receives them. A context mismatch can make a private edit use stale or incomplete source material.
- Keep the private edit route's provider, model, local path, and context limits aligned with the selected settings. A route can pass readiness checks but still fail if its runtime model does not match its installed model.
- Keep the destructive-command rules aligned with the confirmation view and the agent prompt. A new risky command form must not bypass the approval path.
- Keep rewrite provider verification and private-model verification aligned with the selected provider and runtime model.
- Keep rewrite diagnostic output on the shared debug logger. Do not restore a separate file write that bypasses the app's logging controls.
- Keep the Apple Intelligence turn mapping aligned with stored user, assistant, and tool messages. The tool-call ID must remain paired with the matching terminal result.
- Keep Apple Intelligence availability and context-limit errors visible as request failures. Do not let an unavailable system model produce an empty command response.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| macOS accessibility access is off or the focused app exposes no usable selection | Rewrite Mode falls back to writing new text instead of changing the selected text | TextSelectionService checks trust and supports a selected-range fallback; the service records write mode when no selection exists |
| A voice request becomes invalid while a terminal command is pending | Later agent output can be shown after the source overlay has closed | Output-validity checks discard undispatched messages while retaining the outcome of a command that already ran |
| The command view loses its active presentation state while it owns a recording | Voice capture can continue after the command view closes | The view tracks recording ownership and stops only its own active recording on deactivation |
| No verified provider or model is available | The request does not reach the language model | Mode readiness checks block submission and link the user to AI Settings |
| Apple Intelligence is unavailable because the Mac, system setting, macOS version, or model state is not ready | Command Mode cannot use the Apple Intelligence route | Availability checks return the specific reason before the native model is called |
| An Apple Intelligence conversation exceeds the native context size after older requests are removed | The request fails and the user must start a new chat | The provider removes the oldest complete request and raises a context-too-long error when only the current request remains |
| A private edit model is not installed, available, or matched to the selected route | Rewrite Mode reports an error instead of using an unverified or wrong local model | Private-provider route validation resolves the installed model and its local path before the request |
| A captured window becomes stale while the target or screen-reading setting changes | The model can receive context from a different target or an outdated edit session | Revision checks cancel outdated captures and reject results that no longer match the active target |
| A terminal command fails or exceeds its time limit | The model receives a failed structured result and may stop or choose another action | Exit status, error output, and timeout state return to the agent loop |
| A command matches destructive-command rules | Execution pauses until the user approves it | The pending command view provides Cancel and Run Command actions |
| The original app loses focus before output delivery | Text may not return to the intended field | The target process is captured before the overlay and focus is restored before typing |
| Rewrite diagnostics are sent through a separate file path | Log controls and shared log handling can be bypassed | appendDiagnosticLog sends enabled diagnostics through DebugLogger |
Key files
| File | Why to read it |
|---|---|
| Sources/…/CommandModeService.swift | Defines the command conversation, agent loop, safety gate, terminal result handling, chat state, and model request |
| Sources/…/AppleIntelligenceCommandProvider.swift | Defines Apple Intelligence availability and maps the native system model to the command and terminal-call contract |
| Sources/…/RewriteModeService.swift | Defines selection state, write versus rewrite behavior, prompt and window context, model routes, accepted output, provider checks, and diagnostic logging |
| Sources/…/CommandModeView.swift | Shows command input, recording ownership, progress, confirmation, conversation output, and terminal results |
| Sources/…/RewriteModeView.swift | Shows edit input, source and transformed text, provider and model choice, retry and replacement actions, and voice input |
| Sources/…/TextSelectionService.swift | Defines the macOS selection boundary used before a rewrite begins |
| Sources/…/TerminalService.swift | Defines the terminal function contract and structured command result |
| Sources/…/ContentView.swift | Routes shortcuts and completed voice input, captures the target app, starts window reading, and coordinates rewrite delivery |
| Sources/…/TypingService.swift | Defines the cross-app text insertion boundary used for accepted transformations |
| Sources/…/ChatHistoryStore.swift | Defines local persistence and navigation for Command Mode conversations |
| Sources/…/SettingsStore.swift | Defines prompt resolution, selected context formatting, and shared mode configuration |