new features
This commit is contained in:
+62
-5
@@ -79,6 +79,21 @@ final class Store {
|
||||
END;
|
||||
""")
|
||||
try preparePromptLibrary()
|
||||
try prepareWebSources()
|
||||
}
|
||||
|
||||
private func prepareWebSources() throws {
|
||||
try execute("""
|
||||
CREATE TABLE IF NOT EXISTS message_web_sources (
|
||||
message TEXT PRIMARY KEY REFERENCES messages(id) ON DELETE CASCADE,
|
||||
sources TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS message_documents (
|
||||
message TEXT PRIMARY KEY REFERENCES messages(id) ON DELETE CASCADE,
|
||||
names TEXT NOT NULL
|
||||
);
|
||||
|
||||
""")
|
||||
}
|
||||
|
||||
private func preparePromptLibrary() throws {
|
||||
@@ -198,16 +213,48 @@ final class Store {
|
||||
}
|
||||
|
||||
func save(_ message: Message, in chat: UUID) throws {
|
||||
try mutate("INSERT INTO messages(id,chat,role,content,thinking,backend) VALUES(?,?,?,?,?,?)",
|
||||
[message.id.uuidString, chat.uuidString, message.role.rawValue, message.content, message.thinking, message.backend])
|
||||
func insertMessage() throws {
|
||||
try mutate("INSERT INTO messages(id,chat,role,content,thinking,backend) VALUES(?,?,?,?,?,?)",
|
||||
[message.id.uuidString, chat.uuidString, message.role.rawValue, message.content, message.thinking, message.backend])
|
||||
}
|
||||
|
||||
guard !message.webSources.isEmpty || !message.documentNames.isEmpty else { try insertMessage(); return }
|
||||
let encoder = JSONEncoder()
|
||||
try execute("SAVEPOINT save_message_sources")
|
||||
do {
|
||||
try insertMessage()
|
||||
if !message.webSources.isEmpty {
|
||||
let sources = try encoder.encode(message.webSources)
|
||||
try mutate("INSERT INTO message_web_sources(message,sources) VALUES(?,?)",
|
||||
[message.id.uuidString, String(decoding: sources, as: UTF8.self)])
|
||||
}
|
||||
if !message.documentNames.isEmpty {
|
||||
let names = try encoder.encode(message.documentNames)
|
||||
try mutate("INSERT INTO message_documents(message,names) VALUES(?,?)",
|
||||
[message.id.uuidString, String(decoding: names, as: UTF8.self)])
|
||||
}
|
||||
try execute("RELEASE save_message_sources")
|
||||
} catch {
|
||||
try? execute("ROLLBACK TO save_message_sources")
|
||||
try? execute("RELEASE save_message_sources")
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
func messages(in chat: UUID) throws -> [Message] {
|
||||
try query("SELECT id,role,content,thinking,backend FROM messages WHERE chat=? ORDER BY rowid",
|
||||
[chat.uuidString]) { row in
|
||||
try query("""
|
||||
SELECT m.id,m.role,m.content,m.thinking,m.backend,w.sources,d.names
|
||||
FROM messages m
|
||||
LEFT JOIN message_web_sources w ON w.message=m.id
|
||||
LEFT JOIN message_documents d ON d.message=m.id
|
||||
WHERE m.chat=? ORDER BY m.rowid
|
||||
""", [chat.uuidString]) { row in
|
||||
guard let role = Role(rawValue: string(row, 1)) else { throw StudioError("Invalid stored message role.") }
|
||||
let decoder = JSONDecoder()
|
||||
let sources = (try? decoder.decode([WebReplySource].self, from: Data(string(row, 5).utf8))) ?? []
|
||||
let documents = (try? decoder.decode([String].self, from: Data(string(row, 6).utf8))) ?? []
|
||||
return Message(id: try uuid(row, 0), role: role, content: string(row, 2),
|
||||
thinking: string(row, 3), backend: string(row, 4))
|
||||
thinking: string(row, 3), backend: string(row, 4), webSources: sources, documentNames: documents)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +286,15 @@ final class Store {
|
||||
}
|
||||
}
|
||||
|
||||
func openingExcerpts(in chat: UUID) throws -> [DocumentExcerpt] {
|
||||
try query("""
|
||||
SELECT d.title, substr((SELECT i.content FROM document_index i WHERE i.document=d.id
|
||||
ORDER BY i.rowid LIMIT 1),1,1600)
|
||||
FROM documents d WHERE d.chat=? ORDER BY d.rowid DESC LIMIT 4
|
||||
""", [chat.uuidString]) { DocumentExcerpt(title: string($0, 0), text: string($0, 1)) }
|
||||
.filter { !$0.text.isEmpty }
|
||||
}
|
||||
|
||||
func addDocument(title: String, chunks: [String], vectors: [DocumentVector?] = [], in chat: UUID) throws {
|
||||
let id = UUID().uuidString
|
||||
try execute("BEGIN IMMEDIATE")
|
||||
@@ -397,6 +453,7 @@ final class Store {
|
||||
let finish = sqlite3_backup_finish(backup)
|
||||
guard step == SQLITE_DONE, finish == SQLITE_OK else { throw failure }
|
||||
try preparePromptLibrary()
|
||||
try prepareWebSources()
|
||||
}
|
||||
|
||||
private func execute(_ sql: String) throws {
|
||||
|
||||
Reference in New Issue
Block a user