Skip to content

Code source EN

martialzinsou edited this page Sep 21, 2026 · 3 revisions

Source code

🇫🇷 French version: Code source

Repository overview

KarenOs/
├── Sources/
│   └── KarenOS/
│       ├── KarenOSApp.swift          ← entry point, tabs, environment
│       ├── ContentView.swift         ← tabs container
│       ├── ChatView.swift            ← chat UI + active skill
│       ├── ChatService.swift         ← history, streaming, local OpenAI call
│       ├── ModelStore.swift          ← registry of installed models
│       ├── LocalModel.swift          ← data model of an installed model
│       ├── StoreView.swift           ← Store (search, cards)
│       ├── StoreCard.swift           ← a model card in the Store
│       ├── StoreDetailSheet.swift    ← detail sheet (files, license, dialog)
│       ├── HuggingFaceClient.swift   ← Hugging Face API (search, details, CDN)
│       ├── DownloadManager.swift     ← download with progress
│       ├── StoreModel.swift          ← data model of a Store entry
│       ├── Skill.swift               ← Skill model + SkillStore
│       ├── SkillsView.swift          ← Skills tab (list, detail, editor)
│       ├── ModelListView.swift       ← « My Models » library
│       ├── EngineManager.swift       ← engine: install, free port, launch, health
│       ├── AppPaths.swift            ← paths to ~/Library/Application Support
│       └── ...
├── Scripts/
│   ├── make-app.sh                   ← app compilation + assembly
│   └── …                             ← other helper scripts
├── Packaging/
│   └── Info.plist                    ← app properties
├── build/                            ← build output (ignored by git)
├── screenshots/                      ← screenshots for the README
├── docs/DOCUMENTATION.md             ← full documentation (Mermaid diagrams)
└── README.md

Responsibility map

File Key responsibility
KarenOSApp.swift @main; builds the SkillStore and AgentStore as environmentObject; sets up the 4 tabs.
Agent.swift Agent, AgentTrigger (manual/launch/interval), AgentEndCondition (iterations, duration, keyword), persisted AgentStore.
AgentRunner.swift Mission loop: loads the agent's model, streaming iterations, pause/resume/stop, end conditions, restore previous model, log.
AgentsView.swift Agents tab: list, card (mission, execution, log), editor (model, trigger, loop, end conditions).
Multimodal.swift ChatAttachment (image/video/audio/file), selection panel, SpeechInputController (local dictation SFSpeechRecognizer).
ContentView.swift The TabView: My Models / Store / Agents / Skills.
ChatView.swift Input, send, streaming display; « Skill » menu; status badge; attachments and mic (multimodal).
ChatService.swift Compiles history, inserts the system prompt, calls the engine, parses the SSE events, yields text; vision support (multi-part content).
ModelStore.swift Loads models.json, dedupes, resolves list/files, remembers installations.
StoreView.swift Search (450 ms debounce), 12-result cap, cards, Store state handling.
HuggingFaceClient.swift Per-model categories: search, details, resolve (CDN URL of a file). Detects gated models.
DownloadManager.swift Download session with progress, clean resume/cancel, writes into Models/.
EngineManager.swift Idempotent engine install, free port, launch, /health polling (max 120 s), clean stop.
Skill.swift Persistent encode/decode + systemPrompt (Role / Context / Outcomes).
SkillsView.swift List, detail sheet (prompt preview), editor with example templates, activation.

Conventions

  • Strict Swift: explicitly typed data models (LocalModel, StoreModel, Skill), no external dependencies (no CocoaPods / usable SPM — bare swiftc build).
  • No superfluous comments: self-explanatory code.
  • Uniform headers: // KarenOS + // By Martial Zinsou on every source.
  • SwiftUI UI: TabView, List, Sheet, Menu, AsyncImage, Task {} / @State / @EnvironmentObject.

Example: the streaming core (ChatService)

// (pseudo-structure)
func streamReply(messages: [String], baseURL: URL) -> AsyncThrowingStream<String, Error> {
    AsyncThrowingStream { continuation in
        // 1. build the /v1/chat/completions URL
        // 2. POST request { messages, stream: true, max_tokens: 512, temperature: 0.7 }
        // 3. for try await line in lines {
        //      parse SSE "data: {delta.content}" → continuation.yield(token)
        //    }
        // 4. "[DONE]" → continuation.finish()
    }
}

Compile and run

Shortcut: Scripts/make-app.sh then open build/KarenOS.app. See the Build & launch page for the full details.

See also: Architecture, Persistence.

Clone this wiki locally