Add custom MCP profiles and reusable workspace libraries
This commit is contained in:
+101
-3
@@ -6,6 +6,22 @@ struct Conversation: Identifiable {
|
||||
let title: String
|
||||
}
|
||||
|
||||
struct WorkspaceSnippet: Identifiable {
|
||||
enum Kind: String, CaseIterable, Identifiable {
|
||||
case skill = "Skill"
|
||||
case context = "Context"
|
||||
case prompt = "Prompt"
|
||||
case webClip = "Web Clip"
|
||||
|
||||
var id: Self { self }
|
||||
}
|
||||
|
||||
let id: UUID
|
||||
let kind: Kind
|
||||
let title: String
|
||||
let content: String
|
||||
}
|
||||
|
||||
final class Store {
|
||||
private let db: OpaquePointer
|
||||
private let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self)
|
||||
@@ -24,6 +40,12 @@ final class Store {
|
||||
CREATE TABLE IF NOT EXISTS chats(
|
||||
id TEXT PRIMARY KEY, title TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS knowledge_stacks(
|
||||
id TEXT PRIMARY KEY REFERENCES chats(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS workspace_snippets(
|
||||
id TEXT PRIMARY KEY, kind TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS messages(
|
||||
id TEXT PRIMARY KEY, chat TEXT NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
role TEXT NOT NULL, content TEXT NOT NULL, thinking TEXT NOT NULL, backend TEXT NOT NULL
|
||||
@@ -50,7 +72,7 @@ final class Store {
|
||||
deinit { sqlite3_close(db) }
|
||||
|
||||
func conversations() throws -> [Conversation] {
|
||||
try query("SELECT id,title FROM chats ORDER BY rowid DESC") {
|
||||
try query("SELECT id,title FROM chats WHERE id NOT IN (SELECT id FROM knowledge_stacks) ORDER BY rowid DESC") {
|
||||
Conversation(id: try uuid($0, 0), title: string($0, 1))
|
||||
}
|
||||
}
|
||||
@@ -60,8 +82,10 @@ final class Store {
|
||||
guard !term.isEmpty else { return try conversations() }
|
||||
return try query("""
|
||||
SELECT c.id, c.title FROM chats c
|
||||
WHERE instr(lower(c.title), lower(?)) > 0 OR EXISTS (
|
||||
SELECT 1 FROM messages m WHERE m.chat=c.id AND instr(lower(m.content), lower(?)) > 0
|
||||
WHERE c.id NOT IN (SELECT id FROM knowledge_stacks) AND (
|
||||
instr(lower(c.title), lower(?)) > 0 OR EXISTS (
|
||||
SELECT 1 FROM messages m WHERE m.chat=c.id AND instr(lower(m.content), lower(?)) > 0
|
||||
)
|
||||
)
|
||||
ORDER BY c.rowid DESC
|
||||
""", [term, term]) {
|
||||
@@ -69,6 +93,45 @@ final class Store {
|
||||
}
|
||||
}
|
||||
|
||||
func knowledgeStacks() throws -> [Conversation] {
|
||||
try query("SELECT c.id,c.title FROM chats c JOIN knowledge_stacks s ON s.id=c.id ORDER BY c.rowid DESC") {
|
||||
Conversation(id: try uuid($0, 0), title: string($0, 1))
|
||||
}
|
||||
}
|
||||
|
||||
func createKnowledgeStack(title: String) throws -> Conversation {
|
||||
try execute("BEGIN IMMEDIATE")
|
||||
do {
|
||||
let entry = try create(title: title)
|
||||
try mutate("INSERT INTO knowledge_stacks(id) VALUES(?)", [entry.id.uuidString])
|
||||
try execute("COMMIT")
|
||||
return entry
|
||||
} catch {
|
||||
try? execute("ROLLBACK")
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
func snippets() throws -> [WorkspaceSnippet] {
|
||||
try query("SELECT id,kind,title,content FROM workspace_snippets ORDER BY rowid DESC") {
|
||||
guard let kind = WorkspaceSnippet.Kind(rawValue: string($0, 1)) else {
|
||||
throw StudioError("Invalid workspace resource.")
|
||||
}
|
||||
return WorkspaceSnippet(id: try uuid($0, 0), kind: kind, title: string($0, 2), content: string($0, 3))
|
||||
}
|
||||
}
|
||||
|
||||
func saveSnippet(id: UUID, kind: WorkspaceSnippet.Kind, title: String, content: String) throws {
|
||||
try mutate("""
|
||||
INSERT INTO workspace_snippets(id,kind,title,content) VALUES(?,?,?,?)
|
||||
ON CONFLICT(id) DO UPDATE SET kind=excluded.kind, title=excluded.title, content=excluded.content
|
||||
""", [id.uuidString, kind.rawValue, title, content])
|
||||
}
|
||||
|
||||
func deleteSnippet(_ id: UUID) throws {
|
||||
try mutate("DELETE FROM workspace_snippets WHERE id=?", [id.uuidString])
|
||||
}
|
||||
|
||||
func rename(_ id: UUID, title: String) throws {
|
||||
try mutate("UPDATE chats SET title=? WHERE id=?", [title, id.uuidString])
|
||||
}
|
||||
@@ -180,6 +243,41 @@ final class Store {
|
||||
return excerpts
|
||||
}
|
||||
|
||||
func exportWorkspace(to url: URL) throws {
|
||||
guard !FileManager.default.fileExists(atPath: url.path) else {
|
||||
throw StudioError("Choose a new filename for the workspace backup.")
|
||||
}
|
||||
try mutate("VACUUM INTO ?", [url.path])
|
||||
}
|
||||
|
||||
func restoreWorkspace(from url: URL) throws {
|
||||
var input: OpaquePointer?
|
||||
let opened = sqlite3_open_v2(url.path, &input, SQLITE_OPEN_READONLY, nil)
|
||||
guard opened == SQLITE_OK, let input else {
|
||||
if let input { sqlite3_close(input) }
|
||||
throw StudioError("The workspace backup could not be opened.")
|
||||
}
|
||||
defer { sqlite3_close(input) }
|
||||
let check = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name IN ('chats','messages','documents','workspace_snippets','knowledge_stacks')"
|
||||
var prepared: OpaquePointer?
|
||||
guard sqlite3_prepare_v2(input, check, -1, &prepared, nil) == SQLITE_OK, let prepared else {
|
||||
throw StudioError("The selected file is not an AI Studio workspace backup.")
|
||||
}
|
||||
let valid = sqlite3_step(prepared) == SQLITE_ROW && sqlite3_column_int(prepared, 0) == 5
|
||||
sqlite3_finalize(prepared)
|
||||
guard valid else { throw StudioError("The selected file is not an AI Studio workspace backup.") }
|
||||
var checkHandle: OpaquePointer?
|
||||
guard sqlite3_prepare_v2(input, "PRAGMA quick_check", -1, &checkHandle, nil) == SQLITE_OK,
|
||||
let integrity = checkHandle else { throw StudioError("Could not verify backup integrity.") }
|
||||
let healthy = sqlite3_step(integrity) == SQLITE_ROW && string(integrity, 0) == "ok"
|
||||
sqlite3_finalize(integrity)
|
||||
guard healthy else { throw StudioError("The selected backup did not pass SQLite integrity checks.") }
|
||||
guard let backup = sqlite3_backup_init(db, "main", input, "main") else { throw failure }
|
||||
let step = sqlite3_backup_step(backup, -1)
|
||||
let finish = sqlite3_backup_finish(backup)
|
||||
guard step == SQLITE_DONE, finish == SQLITE_OK else { throw failure }
|
||||
}
|
||||
|
||||
private func execute(_ sql: String) throws {
|
||||
guard sqlite3_exec(db, sql, nil, nil, nil) == SQLITE_OK else { throw failure }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user