- Architecture
- AI systems
- Local-first
Keep the interface away from the model
A backend boundary gives a local-first assistant one place to validate requests, persist state, stream work and recover from failure.
An AI interface becomes easier to reason about when it is not responsible for knowing where the model lives. The UI should describe user intent and display state; a backend should own the boundary to speech-to-text, language-model and text-to-speech services. This is the shape documented by the public RAFIQ repository: rafiq-ai provides STT, LLM and TTS capabilities, HTTP connects it to rafiq-backend, and the backend connects to rafiq-gui through Server-Sent Events. The repository states the rule directly: the GUI never talks directly to AI.
The interface should send an application request, not carry the knowledge of every AI provider and execution detail.
The boundary is a product decision
The separation is more than a diagram. It defines who is allowed to make decisions. Electron and React can collect input, show connection state and render streamed output. The backend can validate the input, choose a workflow, record the conversation and translate service-specific failures into an application response. When a model changes, the interface does not need to learn a new SDK. When a desktop screen is replaced, persistence and orchestration remain in one place.
In a local-first product, this boundary also keeps the deployment story explicit. The assistant can use local services or configured remote services behind the same backend contract. HTTP is the request boundary to the AI layer; Fastify, SQLite, SSE and MQTT are part of the backend system described in RAFIQ. The GUI receives a stream from the backend rather than opening a second, hidden path to the model.
One place for validation and persistence
A request that reaches an AI service is already a meaningful operation. It may create a conversation turn, consume a local resource, trigger device-aware behavior or produce data that the next request needs. Putting validation and persistence at the backend boundary means those concerns can be handled before the model call begins. The backend can reject an incomplete request, attach an identifier, save the accepted turn and then start work.
- Validate the application request rather than trusting values assembled by the UI.
- Persist the accepted input and the state needed to resume or inspect the operation.
- Run the selected AI workflow behind a stable HTTP contract.
- Stream progress and output to the GUI over SSE, with errors represented as application events.
This arrangement does not guarantee that every operation succeeds. It does make the failure location legible. A validation failure is different from a persistence failure, a model failure and a disconnected stream. That distinction gives the interface enough information to explain what happened without embedding backend policy in the client.
Retries and streams need ownership
Retries are safest when the server owns them. A client retry can accidentally create duplicate conversation turns if it cannot tell whether the first request was persisted before the connection dropped. The backend can use an operation identifier, check existing state and decide whether a retry is safe. The exact policy depends on the operation, but the location of the policy should not be scattered across screens.
Streaming has the same property. SSE is a transport for updates, not a replacement for application state. The backend can emit a started event, incremental output and a completed or failed event while keeping the authoritative record in SQLite. If the GUI reconnects, the backend can decide whether to replay stored state, resume a stream or report that the operation ended. The UI remains focused on rendering those states.
Keep contracts small and observable
A useful contract does not expose every internal service. It names the operation, its input, its current state and the events the interface can render. That keeps the architecture open to a different model provider, a different desktop shell or an additional client without making those changes a rewrite of the whole system.
The practical lesson is simple: let the interface own interaction and let the backend own the workflow. For a local-first assistant, that single boundary gives validation, persistence, retries and streaming a coherent home. It also makes the system easier to explain: the GUI talks to the backend, and the backend talks to AI and devices.