78 lines
2.0 KiB
Swift
78 lines
2.0 KiB
Swift
// CxLLM Studio — macOS Application
|
|
// Services/AppState.swift — Global application state
|
|
|
|
import SwiftUI
|
|
import CxCode
|
|
import CxAWS
|
|
|
|
@Observable
|
|
final class AppState {
|
|
enum Tab: String, CaseIterable {
|
|
case chat, models, aws, git, agent, mcp, settings
|
|
}
|
|
|
|
var selectedTab: Tab = .chat
|
|
var conversations: [Conversation] = []
|
|
var activeConversationId: String?
|
|
var selectedModel: CxModelSlot = .spark
|
|
|
|
var activeConversation: Conversation? {
|
|
get { conversations.first { $0.id == activeConversationId } }
|
|
set {
|
|
if let newValue, let idx = conversations.firstIndex(where: { $0.id == newValue.id }) {
|
|
conversations[idx] = newValue
|
|
}
|
|
}
|
|
}
|
|
|
|
func createConversation() {
|
|
let conv = Conversation(model: selectedModel)
|
|
conversations.insert(conv, at: 0)
|
|
activeConversationId = conv.id
|
|
}
|
|
|
|
func deleteConversation(_ id: String) {
|
|
conversations.removeAll { $0.id == id }
|
|
if activeConversationId == id {
|
|
activeConversationId = conversations.first?.id
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Data Models
|
|
|
|
struct Conversation: Identifiable, Codable {
|
|
var id: String = UUID().uuidString
|
|
var title: String = "New Chat"
|
|
var model: CxModelSlot
|
|
var messages: [Message] = []
|
|
var createdAt: Date = Date()
|
|
var isAgentMode: Bool = false
|
|
var sessionId: String?
|
|
|
|
var lastMessage: String? {
|
|
messages.last?.content
|
|
}
|
|
}
|
|
|
|
struct Message: Identifiable, Codable {
|
|
var id: String = UUID().uuidString
|
|
var role: Role
|
|
var content: String
|
|
var timestamp: Date = Date()
|
|
var model: String?
|
|
var isStreaming: Bool = false
|
|
|
|
enum Role: String, Codable {
|
|
case system, user, assistant, tool
|
|
}
|
|
|
|
static func user(_ content: String) -> Message {
|
|
Message(role: .user, content: content)
|
|
}
|
|
|
|
static func assistant(_ content: String, model: String? = nil) -> Message {
|
|
Message(role: .assistant, content: content, model: model)
|
|
}
|
|
}
|