A SwiftUI chat application built with Apple's Foundation Models framework, showcasing on-device AI capabilities with persistent conversation storage.
Requirements
- iOS 26.0+ / iPadOS 26.0+ / macOS 26.0+ / visionOS 26.0+
- Xcode 26.0+ (with iOS 26 SDK)
- Device with Apple Intelligence support
- Apple Intelligence must be enabled in Settings
Features
- ✅ Multiple Conversations: Create and manage multiple chat conversations
- ✅ Persistent Storage: SwiftData integration for conversation history
- ✅ Real-time Streaming: Live streaming responses with structured output
- ✅ Smart Summaries: Automatic conversation summarization
- ✅ Tool Integration: Web page analysis with metadata extraction
- ✅ Rich Message Display: Support for attachments with thumbnails and descriptions
- ✅ Availability Checking: Proper Foundation Models availability handling
- ✅ Modern SwiftUI: Clean interface with navigation stack and swipe actions
- ✅ On-device AI: No internet required, complete privacy
- ✅ Error Handling: Graceful error display in the UI
Architecture
Core Components
ChatEngine: Manages Foundation Models sessions and streaming responsesConversation&Message: SwiftData models for persistent storageMessageGenerable: @Generable struct for structured AI responses with web metadataConversationsListView: Main interface for managing conversationsConversationDetailView: Chat interface with streaming message supportWebAnalyserTool: Tool for extracting structured data from web pages
Data Flow
- Conversations List: Shows all conversations sorted by last message timestamp
- Chat Interface: Real-time streaming with SwiftData persistence
- Message Streaming: Uses
@Generablefor structured AI responses - Auto-Summarization: Updates conversation summaries after each exchange
Getting Started
- Open
FoundationChat.xcodeprojin Xcode - Ensure your development device has Apple Intelligence enabled
- Build and run on a supported device (simulator not recommended for performance)
- Create a new conversation with the "+" button
- Start chatting with the on-device AI
Key Implementation Details
Foundation Models Integration
@Observable class ChatEngine { private let model = SystemLanguageModel.default private let session: LanguageModelSession var isAvailable: Bool { switch model.availability { case .available: return true default: return false } } }
Structured Streaming Response
func respondTo() async -> LanguageModelSession.ResponseStream<MessageGenerable>? { session.streamResponse(generating: MessageGenerable.self) { // Conversation context with full history } }
Tool Integration
struct WebAnalyserTool: Tool { let name = "WebAnalyser" let description = "Analyse a website and return structured content" func call(arguments: Arguments) async throws -> ToolOutput { // Extracts title, thumbnail, and description } }
SwiftData Persistence
@Model class Conversation { @Relationship(deleteRule: .cascade) var messages: [Message] var summary: String? }
Documentation
- CLAUDE.md - Development guidelines and framework overview
- Examples - Comprehensive implementation examples:
- Basic Usage
- Structured Output
- Streaming Responses
- Tool Calling
- Performance & Safety
- Complete Chat App
Project Structure
FoundationChat/
├── Models/
│ ├── SwiftData/ # Data persistence models
│ └── Generable/ # Foundation Models structures
├── Views/
│ ├── ConversationsList/ # Main conversation list
│ └── ConversationDetail/ # Chat interface with message views
├── Tools/
│ └── WebAnalyserTool.swift # Web content extraction tool
├── Env/
│ └── ChatEngine.swift # Foundation Models integration
└── FoundationChatApp.swift # App entry point
Contributing
This is a demonstration project showcasing Apple's Foundation Models framework. Feel free to:
- Fork and experiment with different AI prompts
- Extend with additional Foundation Models features
- Add new UI components and interactions
- Contribute improvements and bug fixes
License
MIT License - See LICENSE file for details
