add chat flow for search results
This commit is contained in:
@@ -87,6 +87,7 @@ final class SybilViewModel {
|
||||
var isLoadingCollections = false
|
||||
var isLoadingSelection = false
|
||||
var isSending = false
|
||||
var isCreatingSearchChat = false
|
||||
var errorMessage: String?
|
||||
|
||||
var composer = ""
|
||||
@@ -202,20 +203,20 @@ final class SybilViewModel {
|
||||
}
|
||||
|
||||
var displayedMessages: [Message] {
|
||||
let canonical = selectedChat?.messages ?? []
|
||||
let canonical = displayableMessages(selectedChat?.messages ?? [])
|
||||
guard let pending = pendingChatState else {
|
||||
return canonical
|
||||
}
|
||||
|
||||
if let pendingID = pending.chatID {
|
||||
if case let .chat(selectedID) = selectedItem, selectedID == pendingID {
|
||||
return pending.messages
|
||||
return displayableMessages(pending.messages)
|
||||
}
|
||||
return canonical
|
||||
}
|
||||
|
||||
if draftKind == .chat {
|
||||
return pending.messages
|
||||
return displayableMessages(pending.messages)
|
||||
}
|
||||
|
||||
return canonical
|
||||
@@ -473,6 +474,36 @@ final class SybilViewModel {
|
||||
isSending = false
|
||||
}
|
||||
|
||||
func startChatFromSelectedSearch() async {
|
||||
guard let search = selectedSearch, !isCreatingSearchChat, !isSending else {
|
||||
return
|
||||
}
|
||||
|
||||
isCreatingSearchChat = true
|
||||
errorMessage = nil
|
||||
|
||||
do {
|
||||
let client = try client()
|
||||
let chat = try await client.createChatFromSearch(searchID: search.id)
|
||||
draftKind = nil
|
||||
pendingChatState = nil
|
||||
composer = ""
|
||||
|
||||
chats.removeAll(where: { $0.id == chat.id })
|
||||
chats.insert(chat, at: 0)
|
||||
|
||||
selectedItem = .chat(chat.id)
|
||||
selectedSearch = nil
|
||||
|
||||
await refreshCollections(preferredSelection: .chat(chat.id))
|
||||
} catch {
|
||||
errorMessage = normalizeAPIError(error)
|
||||
SybilLog.error(SybilLog.ui, "Create chat from search failed", error: error)
|
||||
}
|
||||
|
||||
isCreatingSearchChat = false
|
||||
}
|
||||
|
||||
private func loadInitialData(using client: SybilAPIClient) async {
|
||||
isLoadingCollections = true
|
||||
errorMessage = nil
|
||||
@@ -974,6 +1005,10 @@ final class SybilViewModel {
|
||||
}
|
||||
}
|
||||
|
||||
private func displayableMessages(_ messages: [Message]) -> [Message] {
|
||||
messages.filter { $0.role != .system }
|
||||
}
|
||||
|
||||
private func chatTitle(title: String?, messages: [Message]?) -> String {
|
||||
if let title = title?.trimmingCharacters(in: .whitespacesAndNewlines), !title.isEmpty {
|
||||
return title
|
||||
|
||||
Reference in New Issue
Block a user