Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions Applications/MLXChatExample/Models/LMModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import MLXLMCommon
/// Represents a language model configuration with its associated properties and type.
/// Can represent either a large language model (LLM) or a vision-language model (VLM).
struct LMModel {
/// Name of the model
/// Identifier for the model, used for cache keys and equality.
let name: String

/// User-facing label shown in the model picker.
let displayName: String

/// Configuration settings for model initialization
let configuration: ModelConfiguration

Expand All @@ -31,15 +34,6 @@ struct LMModel {
// MARK: - Helpers

extension LMModel {
/// Display name with additional "(Vision)" suffix for vision models
var displayName: String {
if isVisionModel {
"\(name) (Vision)"
} else {
name
}
}

/// Whether the model is a large language model
var isLanguageModel: Bool {
type == .llm
Expand Down
22 changes: 19 additions & 3 deletions Applications/MLXChatExample/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ class Message: Identifiable {
/// The role of the message sender (user, assistant, or system)
let role: Role

/// The text content of the message
var content: String
/// On-screen regions in arrival order. User/system messages have a
/// single ``MessageSegment/content(_:)`` holding the whole text.
/// Assistant messages grow as the parser surfaces reasoning runs,
/// tool-call items, and content runs, interleaved.
var segments: [MessageSegment]

/// Array of image URLs attached to the message
var images: [URL]
Expand All @@ -29,6 +32,17 @@ class Message: Identifiable {
/// Timestamp when the message was created
let timestamp: Date

/// Concatenated text from every ``MessageSegment/content(_:)`` segment.
/// Used for user/system rendering and for re-seeding assistant turns
/// into a `Chat.Message` history – the model only needs to see what
/// it actually said, not its reasoning or tool-call wire syntax.
var content: String {
segments.compactMap {
if case let .content(segment) = $0 { return segment.text }
return nil
}.joined()
}

/// Creates a new message with the specified role, content, and optional media attachments
/// - Parameters:
/// - role: The role of the message sender
Expand All @@ -38,7 +52,9 @@ class Message: Identifiable {
init(role: Role, content: String, images: [URL] = [], videos: [URL] = []) {
self.id = UUID()
self.role = role
self.content = content
self.segments = content.isEmpty
? []
: [.content(TextSegment(itemId: "_initial", text: content))]
self.images = images
self.videos = videos
self.timestamp = .now
Expand Down
42 changes: 42 additions & 0 deletions Applications/MLXChatExample/Models/MessageSegment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// MessageSegment.swift
// MLXChatExample
//

import Foundation

/// One on-screen region inside an assistant ``Message``: a run of content
/// text, a run of reasoning text, or a tool call. Stored in arrival order
/// so the UI renders reasoning, tool-call cards, and answer text
/// interleaved as the parser surfaced them.
enum MessageSegment: Identifiable {
case content(TextSegment)
case reasoning(TextSegment)
case toolCall(ToolCall)

var id: String {
switch self {
case .content(let segment), .reasoning(let segment):
return segment.id.uuidString
case .toolCall(let toolCall):
return toolCall.id
}
}
}

/// A growable text run inside a ``MessageSegment``. `itemId` is the
/// parser's `msg_…` / `rs_…` id, used to merge consecutive deltas of
/// the same item into one segment. `id` is a stable UUID for SwiftUI
/// `Identifiable` – distinct from `itemId` to stay safe if the parser
/// ever reuses an id across non-contiguous spans.
@Observable
class TextSegment: Identifiable {
let id = UUID()
let itemId: String
var text: String

init(itemId: String, text: String = "") {
self.itemId = itemId
self.text = text
}
}
40 changes: 40 additions & 0 deletions Applications/MLXChatExample/Models/ToolCall.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ToolCall.swift
// MLXChatExample
//

import Foundation

/// One assistant-issued tool call surfaced in the UI. `argumentsRaw`
/// accumulates as `functionCallArgumentsDelta` events arrive; `result`
/// fills in when the matching `functionCallOutput` lands.
@Observable
class ToolCall: Identifiable {
/// Parser-minted `fc_…` id. Used for `Identifiable` and for matching
/// `functionCallArgumentsDelta.itemId`.
let id: String

/// Parser-minted `call_…` id used to match the eventual
/// `functionCallOutput` back to this call.
let callId: String

/// Function name the model wants to invoke.
let name: String

/// Raw JSON arguments string, growing one delta at a time.
var argumentsRaw: String

/// Tool result once dispatch returns; `nil` while in flight.
var result: String?

init(
id: String, callId: String, name: String,
argumentsRaw: String = "", result: String? = nil
) {
self.id = id
self.callId = callId
self.name = name
self.argumentsRaw = argumentsRaw
self.result = result
}
}
Loading