A SwiftUI chat application built with Apple's Foundation Models framework, showcasing on-device AI capabilities with persistent conversation storage.
- 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
- ✅ 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
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
- Conversations List: Shows all conversations sorted by last message timestamp
- Chat Interface: Real-time streaming with SwiftData persistence
- Message Streaming: Uses
@Generable
for structured AI responses - Auto-Summarization: Updates conversation summaries after each exchange
- Open
FoundationChat.xcodeproj
in 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
@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
}
}
}
func respondTo() async -> LanguageModelSession.ResponseStream<MessageGenerable>? {
session.streamResponse(generating: MessageGenerable.self) {
// Conversation context with full history
}
}
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
}
}
@Model
class Conversation {
@Relationship(deleteRule: .cascade)
var messages: [Message]
var summary: String?
}
- CLAUDE.md - Development guidelines and framework overview
- Examples - Comprehensive implementation examples:
- Basic Usage
- Structured Output
- Streaming Responses
- Tool Calling
- Performance & Safety
- Complete Chat App
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
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
MIT License - See LICENSE file for details