How arthur-ficial/apfel works
apfel is a macOS command-line interface and local OpenAI-compatible server for Apple’s on-device FoundationModels. The main workflow accepts a prompt, piped input, or attachments; prepares conversation context; runs model generation and optional MCP tool rounds; then returns text or structured output. The server exposes compatible HTTP APIs, including chat and Responses-style handling. The README documents additional schema-constrained output, token counting, vision inputs, demos, and MCP integrations. Some implementation files were not sampled, so uncertain wiring is omitted or marked unverified.
Main entry points: main.swift CLI dispatch · Server.swift HTTP server · ContextManager.swift Context assembly · Session.swift Model session
Components 25
CLI and interaction
- CLI dispatch
Sources/main.swift - Argument parsing
Sources/CLI/CLIArguments.swift - Interactive chat
Sources/CLI/ChatHistory.swift - Output formatting
Sources/Output.swift - File input
Sources/LesbarFileReader.swift - Bundled demos
Sources/Core/GeneratedDemos.swift
HTTP API
- HTTP server
Sources/Server.swift - Chat completions
Sources/Handlers.swift - Responses API
Sources/ResponsesHandlers.swift - Server security
Sources/Core/ServerSecurity.swift
Model and context
- Context assembly
Sources/ContextManager.swift - Model session
Sources/Session.swift - Token counting
Sources/TokenCounter.swift - Context strategy
Sources/Core/ContextStrategy.swift - Schema conversion
Sources/SchemaConverter.swift - Schema parsing
Sources/Core/SchemaParser.swift - Image input
Sources/Core/ImageInput.swift
Tools and integrations
- MCP client
Sources/MCPClient.swift - Tool execution
Sources/Core/ToolCallHandler.swift - Tool exchange grouping
Sources/Core/ToolExchangeGrouping.swift - Tool directives
Sources/Core/ToolCallingDirective.swift
Other components
- CLI user
- API client
- FoundationModels
- MCP servers
Connections 29
- CLI user → CLI dispatch: invokes
README.md - CLI dispatch → Argument parsing: parses arguments
Sources/main.swift - CLI dispatch → File input: reads attachments
Sources/main.swift - CLI dispatch → HTTP server: starts server
Sources/main.swift - CLI dispatch → Output formatting: prints results
- CLI user → Interactive chat: starts chat
README.md - Interactive chat → Model session: runs conversation
- API client → HTTP server: sends requests
README.md - HTTP server → Chat completions: routes chat requests
- HTTP server → Responses API: routes Responses requests
- HTTP server → Server security: uses security checks
Sources/Server.swift - Chat completions → Context assembly: builds session context
Sources/Handlers.swift - Responses API → Context assembly: builds session context
Sources/ResponsesHandlers.swift - Context assembly → Schema conversion: converts tool schemas
Sources/ContextManager.swift - Context assembly → Model session: prepares session
Sources/Session.swift - Model session → Token counting: counts context
Sources/Session.swift - Model session → Tool execution: executes tool calls
Sources/Session.swift - Tool execution → MCP client: dispatches MCP calls
- MCP client → MCP servers: communicates with
README.md - Context assembly → Tool exchange grouping: uses grouped history
Sources/ContextManager.swift - Chat completions → Tool directives: applies tool directives
Sources/Handlers.swift - Responses API → Tool directives: applies tool directives
Sources/ResponsesHandlers.swift - Argument parsing → Context strategy: selects strategy
Sources/CLI/CLIArguments.swift - Argument parsing → Schema parsing: parses schema
Sources/CLI/CLIArguments.swift - Schema parsing → Schema conversion: provides schema definitions
Sources/SchemaConverter.swift - Model session → FoundationModels: generates responses
README.md - Token counting → FoundationModels: counts tokens
Sources/TokenCounter.swift - CLI dispatch → Bundled demos: writes bundled demos
README.md - File input → Image input: supports image attachments