new features
This commit is contained in:
+65
-3
@@ -86,6 +86,10 @@ final class Store {
|
||||
|
||||
private func prepareMessageSources() throws {
|
||||
try execute("""
|
||||
CREATE TABLE IF NOT EXISTS message_performance (
|
||||
message TEXT PRIMARY KEY REFERENCES messages(id) ON DELETE CASCADE,
|
||||
measurement TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS message_web_sources (
|
||||
message TEXT PRIMARY KEY REFERENCES messages(id) ON DELETE CASCADE,
|
||||
sources TEXT NOT NULL
|
||||
@@ -245,7 +249,7 @@ final class Store {
|
||||
[message.id.uuidString, chat.uuidString, message.role.rawValue, message.content, message.thinking, message.backend])
|
||||
}
|
||||
|
||||
guard !message.webSources.isEmpty || !message.documentNames.isEmpty || !message.documentCitations.isEmpty else {
|
||||
guard !message.webSources.isEmpty || !message.documentNames.isEmpty || !message.documentCitations.isEmpty || message.performance != nil else {
|
||||
try insertMessage()
|
||||
return
|
||||
}
|
||||
@@ -268,6 +272,11 @@ final class Store {
|
||||
try mutate("INSERT INTO message_document_citations(message,citations) VALUES(?,?)",
|
||||
[message.id.uuidString, String(decoding: citations, as: UTF8.self)])
|
||||
}
|
||||
if let timing = message.performance {
|
||||
let data = try encoder.encode(timing)
|
||||
try mutate("INSERT INTO message_performance(message,measurement) VALUES(?,?)",
|
||||
[message.id.uuidString, String(decoding: data, as: UTF8.self)])
|
||||
}
|
||||
try execute("RELEASE save_message_sources")
|
||||
} catch {
|
||||
try? execute("ROLLBACK TO save_message_sources")
|
||||
@@ -278,11 +287,12 @@ final class Store {
|
||||
|
||||
func messages(in chat: UUID) throws -> [Message] {
|
||||
try query("""
|
||||
SELECT m.id,m.role,m.content,m.thinking,m.backend,w.sources,d.names,c.citations
|
||||
SELECT m.id,m.role,m.content,m.thinking,m.backend,w.sources,d.names,c.citations,p.measurement
|
||||
FROM messages m
|
||||
LEFT JOIN message_web_sources w ON w.message=m.id
|
||||
LEFT JOIN message_documents d ON d.message=m.id
|
||||
LEFT JOIN message_document_citations c ON c.message=m.id
|
||||
LEFT JOIN message_performance p ON p.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.") }
|
||||
@@ -291,10 +301,62 @@ final class Store {
|
||||
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), webSources: sources, documentNames: documents,
|
||||
documentCitations: decodeArray(string(row, 7)))
|
||||
documentCitations: decodeArray(string(row, 7)),
|
||||
performance: try? decoder.decode(GenerationMeasurement.self, from: Data(string(row, 8).utf8)))
|
||||
}
|
||||
}
|
||||
|
||||
func exportChat(_ chat: UUID, to url: URL) throws {
|
||||
guard let title = try query("SELECT title FROM chats WHERE id=?", [chat.uuidString], map: {
|
||||
string($0, 0)
|
||||
}).first else {
|
||||
throw StudioError("This conversation is no longer available.")
|
||||
}
|
||||
|
||||
func inline(_ text: String) -> String {
|
||||
let punctuation: Set<Character> = ["\\", "`", "*", "_", "[", "]", "<", ">", "#", "&", "~"]
|
||||
return text.components(separatedBy: .newlines).joined(separator: " ").map {
|
||||
punctuation.contains($0) ? "\\\($0)" : String($0)
|
||||
}.joined()
|
||||
}
|
||||
|
||||
func quote(_ text: String) -> String {
|
||||
text.components(separatedBy: "\n").map { "> " + $0 }.joined(separator: "\n")
|
||||
}
|
||||
|
||||
var sections = ["# \(inline(title))"]
|
||||
for message in try messages(in: chat) {
|
||||
switch message.role {
|
||||
case .user: sections.append("## You")
|
||||
case .assistant: sections.append("## Assistant")
|
||||
case .system: sections.append("## System")
|
||||
}
|
||||
if !message.backend.isEmpty { sections.append("*\(inline(message.backend))*") }
|
||||
if !message.content.isEmpty { sections.append(message.content) }
|
||||
if !message.thinking.isEmpty {
|
||||
sections.append("### Thinking\n\n" + quote(message.thinking))
|
||||
}
|
||||
if !message.documentNames.isEmpty {
|
||||
sections.append("### Attachments\n\n" + message.documentNames.map { "- " + inline($0) }.joined(separator: "\n"))
|
||||
}
|
||||
if !message.documentCitations.isEmpty {
|
||||
sections.append("### Document Sources")
|
||||
for source in message.documentCitations {
|
||||
sections.append("**\\[D\(source.number)\\] \(inline(source.title))**\n\n" + quote(source.excerpt))
|
||||
}
|
||||
}
|
||||
let webLinks = message.webSources.compactMap { source -> String? in
|
||||
guard let url = try? WebClipCapture.url(source.url.absoluteString) else { return nil }
|
||||
let label = source.title.isEmpty ? url.absoluteString : source.title
|
||||
return "- [\(inline(label))](<\(url.absoluteString)>)"
|
||||
}
|
||||
if !webLinks.isEmpty {
|
||||
sections.append("### Web Sources\n\n" + webLinks.joined(separator: "\n"))
|
||||
}
|
||||
}
|
||||
try (sections.joined(separator: "\n\n") + "\n").write(to: url, atomically: true, encoding: .utf8)
|
||||
}
|
||||
|
||||
func memory(matching prompt: String, in chat: UUID, excluding: Set<UUID> = []) throws -> [String] {
|
||||
let terms = Array(Set(prompt.lowercased().split { !$0.isLetter && !$0.isNumber }
|
||||
.filter { $0.count >= 4 }.prefix(12).map(String.init))).sorted()
|
||||
|
||||
Reference in New Issue
Block a user