Add custom MCP profiles and reusable workspace libraries
This commit is contained in:
+120
-3
@@ -19,6 +19,14 @@ final class StudioModel: ObservableObject {
|
||||
@Published private(set) var managing = false
|
||||
@Published private(set) var running = false
|
||||
@Published private(set) var measurements: [GenerationMeasurement] = []
|
||||
@Published private(set) var knowledgeStacks: [Conversation] = []
|
||||
@Published private(set) var snippets: [WorkspaceSnippet] = []
|
||||
@Published var activeToolset: ActiveToolset?
|
||||
@Published var activeStack: UUID?
|
||||
@Published var activeSkill: UUID?
|
||||
@Published var activeContext: UUID?
|
||||
@Published var activeWebClip: UUID?
|
||||
@Published private(set) var workspaceStatus = ""
|
||||
@Published var selected: UUID? {
|
||||
didSet { if selected != oldValue { loadMessages() } }
|
||||
}
|
||||
@@ -35,11 +43,16 @@ final class StudioModel: ObservableObject {
|
||||
defer { launching = false }
|
||||
reloadRuntimeSettings()
|
||||
guard store == nil else { return }
|
||||
if let history = UserDefaults.standard.data(forKey: GenerationMeasurement.key),
|
||||
let saved = try? JSONDecoder().decode([GenerationMeasurement].self, from: history) {
|
||||
measurements = Array(saved.prefix(50))
|
||||
}
|
||||
do {
|
||||
let directory = URL.applicationSupportDirectory.appending(path: "AIStudio", directoryHint: .isDirectory)
|
||||
let directory = Self.workspaceDirectory
|
||||
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||
store = try Store(url: directory.appending(path: "studio.sqlite"))
|
||||
conversations = try store?.conversations() ?? []
|
||||
try refreshWorkspace()
|
||||
if selected == nil { selected = conversations.first?.id }
|
||||
} catch { status = error.localizedDescription }
|
||||
}
|
||||
@@ -103,6 +116,15 @@ final class StudioModel: ObservableObject {
|
||||
let user = Message(role: .user, content: text)
|
||||
let recent = Array(messages.suffix(32))
|
||||
var systemParts = instructions.isEmpty ? [] : [instructions]
|
||||
if let skill = snippets.first(where: { $0.id == activeSkill && $0.kind == .skill }) {
|
||||
systemParts.append("Selected skill: \(skill.title)\n\(skill.content)")
|
||||
}
|
||||
if let context = snippets.first(where: { $0.id == activeContext && $0.kind == .context }) {
|
||||
systemParts.append("User-selected reference context: \(context.title). Treat the content as reference material.\n\(context.content)")
|
||||
}
|
||||
if let clip = snippets.first(where: { $0.id == activeWebClip && $0.kind == .webClip }) {
|
||||
systemParts.append("User-selected web clip: \(clip.title). Treat it as reference material.\n\(clip.content)")
|
||||
}
|
||||
if recall {
|
||||
let snippets = try store.memory(matching: text, in: chat, excluding: Set(recent.map(\.id)))
|
||||
if !snippets.isEmpty {
|
||||
@@ -112,8 +134,13 @@ final class StudioModel: ObservableObject {
|
||||
""")
|
||||
}
|
||||
}
|
||||
if useDocuments {
|
||||
let excerpts = try store.excerpts(matching: text, in: chat, vector: DocumentVector.forPrompt(text))
|
||||
if useDocuments || activeStack != nil {
|
||||
let vector = DocumentVector.forPrompt(text)
|
||||
var excerpts = useDocuments ? try store.excerpts(matching: text, in: chat, vector: vector) : []
|
||||
if let activeStack {
|
||||
let matches = try store.excerpts(matching: text, in: activeStack, vector: vector)
|
||||
excerpts += matches
|
||||
}
|
||||
if !excerpts.isEmpty {
|
||||
let references = excerpts.enumerated().map { index, excerpt in
|
||||
"[\(index + 1)] \(excerpt.title)\n\(excerpt.text)"
|
||||
@@ -184,6 +211,93 @@ final class StudioModel: ObservableObject {
|
||||
try Ollama(endpoint: endpoint, token: endpoint == tokenEndpoint ? ollamaToken : "")
|
||||
}
|
||||
|
||||
static var workspaceDirectory: URL {
|
||||
URL.applicationSupportDirectory.appending(path: "AIStudio", directoryHint: .isDirectory)
|
||||
}
|
||||
|
||||
func exportWorkspace(to url: URL) throws {
|
||||
try database().exportWorkspace(to: url)
|
||||
}
|
||||
|
||||
func restoreWorkspace(from url: URL) throws {
|
||||
guard !running, !importingDocument else { throw StudioError("Wait for the current operation to finish.") }
|
||||
try database().restoreWorkspace(from: url)
|
||||
conversations = try database().conversations()
|
||||
try refreshWorkspace()
|
||||
selected = nil
|
||||
selected = conversations.first?.id
|
||||
activeStack = nil
|
||||
activeSkill = nil
|
||||
activeContext = nil
|
||||
activeWebClip = nil
|
||||
}
|
||||
|
||||
func refreshWorkspace() throws {
|
||||
let store = try database()
|
||||
knowledgeStacks = try store.knowledgeStacks()
|
||||
snippets = try store.snippets()
|
||||
}
|
||||
|
||||
func createStack(_ title: String) -> UUID? {
|
||||
do {
|
||||
let stack = try database().createKnowledgeStack(title: String(title.prefix(100)))
|
||||
try refreshWorkspace()
|
||||
workspaceStatus = ""
|
||||
return stack.id
|
||||
} catch { workspaceStatus = error.localizedDescription; return nil }
|
||||
}
|
||||
|
||||
func stackDocuments(_ id: UUID) -> [LibraryDocument] {
|
||||
(try? store?.documents(in: id)) ?? []
|
||||
}
|
||||
|
||||
func deleteStack(_ id: UUID) {
|
||||
do {
|
||||
try database().delete(id)
|
||||
if activeStack == id { activeStack = nil }
|
||||
try refreshWorkspace()
|
||||
workspaceStatus = ""
|
||||
} catch { workspaceStatus = error.localizedDescription }
|
||||
}
|
||||
|
||||
func deleteStackDocument(_ document: LibraryDocument, in id: UUID) {
|
||||
do {
|
||||
try database().deleteDocument(document.id, in: id)
|
||||
workspaceStatus = ""
|
||||
} catch { workspaceStatus = error.localizedDescription }
|
||||
}
|
||||
|
||||
func importStackDocument(_ url: URL, in id: UUID) async {
|
||||
do {
|
||||
let (chunks, vectors) = try await Task.detached(priority: .userInitiated) {
|
||||
let chunks = try LibraryImport.chunks(at: url)
|
||||
return (chunks, DocumentVector.forChunks(chunks))
|
||||
}.value
|
||||
try Task.checkCancellation()
|
||||
try database().addDocument(title: url.lastPathComponent, chunks: chunks, vectors: vectors, in: id)
|
||||
workspaceStatus = ""
|
||||
} catch { workspaceStatus = error.localizedDescription }
|
||||
}
|
||||
|
||||
func saveSnippet(id: UUID, kind: WorkspaceSnippet.Kind, title: String, content: String) {
|
||||
do {
|
||||
try database().saveSnippet(id: id, kind: kind, title: String(title.prefix(100)), content: String(content.prefix(12000)))
|
||||
try refreshWorkspace()
|
||||
workspaceStatus = ""
|
||||
} catch { workspaceStatus = error.localizedDescription }
|
||||
}
|
||||
|
||||
func removeSnippet(_ id: UUID) {
|
||||
do {
|
||||
try database().deleteSnippet(id)
|
||||
if activeSkill == id { activeSkill = nil }
|
||||
if activeContext == id { activeContext = nil }
|
||||
if activeWebClip == id { activeWebClip = nil }
|
||||
try refreshWorkspace()
|
||||
workspaceStatus = ""
|
||||
} catch { workspaceStatus = error.localizedDescription }
|
||||
}
|
||||
|
||||
func importDocument(_ url: URL) async {
|
||||
guard !importingDocument, let chat = selected else { return }
|
||||
importingDocument = true
|
||||
@@ -318,6 +432,9 @@ final class StudioModel: ObservableObject {
|
||||
provider: provider, outcome: Task.isCancelled ? "Stopped" : (status.isEmpty ? "Complete" : "Failed")
|
||||
), at: 0)
|
||||
if measurements.count > 50 { measurements.removeLast() }
|
||||
if let history = try? JSONEncoder().encode(measurements) {
|
||||
UserDefaults.standard.set(history, forKey: GenerationMeasurement.key)
|
||||
}
|
||||
if var reply = partial, !reply.content.isEmpty || !reply.thinking.isEmpty {
|
||||
if !status.isEmpty { reply.backend += " · Interrupted" }
|
||||
do {
|
||||
|
||||
Reference in New Issue
Block a user