Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ exports[`IPC message snapshots ServerMessage types history_response serializes t
"timestamp": 1700000001,
},
],
"sessionId": "sess-history-001",
"type": "history_response",
}
`;
Expand Down
1 change: 1 addition & 0 deletions assistant/src/__tests__/ipc-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ const serverMessages: Record<ServerMessageType, ServerMessage> = {
},
history_response: {
type: 'history_response',
sessionId: 'sess-history-001',
messages: [
{ role: 'user', text: 'Hello', timestamp: 1700000000 },
{ role: 'assistant', text: 'Hi there!', timestamp: 1700000001 },
Expand Down
2 changes: 1 addition & 1 deletion assistant/src/daemon/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ function handleHistoryRequest(
timestamp: m.timestamp,
...(m.toolCalls.length > 0 ? { toolCalls: m.toolCalls } : {}),
}));
ctx.send(socket, { type: 'history_response', messages: historyMessages });
ctx.send(socket, { type: 'history_response', sessionId: msg.sessionId, messages: historyMessages });
}

export function mergeToolResults(messages: ParsedHistoryMessage[]): ParsedHistoryMessage[] {
Expand Down
1 change: 1 addition & 0 deletions assistant/src/daemon/ipc-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ export interface HistoryResponseToolCall {

export interface HistoryResponse {
type: 'history_response';
sessionId: string;
messages: Array<{
role: string;
text: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,15 @@ final class ChatViewModel: ObservableObject {
}

/// Populate messages from history data returned by the daemon.
/// Only replaces messages if the user hasn't sent any new messages yet,
/// preventing a late history_response from overwriting live conversation.
func populateFromHistory(_ historyMessages: [HistoryResponseMessage.HistoryMessageItem]) {
let hasUserSentMessages = messages.contains { $0.role == .user }
if hasUserSentMessages {
isHistoryLoaded = true
return
Comment thread
siddseethepalli marked this conversation as resolved.
}

var chatMessages: [ChatMessage] = []
for item in historyMessages {
let role: ChatRole = item.role == "assistant" ? .assistant : .user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ final class ThreadManager: ObservableObject {
private var viewModelCancellable: AnyCancellable?
private var connectionCancellable: AnyCancellable?

/// Tracks which thread is currently awaiting a history_response from the daemon.
private var pendingHistoryThreadId: UUID?
/// Maps session IDs to thread IDs for in-flight history_request messages,
/// so rapid tab switches don't cause history from one thread to land in another.
private var pendingHistoryBySessionId: [String: UUID] = [:]

/// Called when an inline confirmation response should dismiss the floating panel.
var confirmationDismissHandler: ((String) -> Void)?
Expand Down Expand Up @@ -175,23 +176,22 @@ final class ThreadManager: ObservableObject {
private func loadHistoryForActiveThreadIfNeeded() {
guard let activeThreadId else { return }
guard let thread = threads.first(where: { $0.id == activeThreadId }) else { return }
guard thread.sessionId != nil else { return }
guard let sessionId = thread.sessionId else { return }
guard let viewModel = chatViewModels[activeThreadId] else { return }
guard !viewModel.isHistoryLoaded else { return }

pendingHistoryThreadId = activeThreadId
pendingHistoryBySessionId[sessionId] = activeThreadId

do {
try daemonClient.sendHistoryRequest(sessionId: thread.sessionId!)
try daemonClient.sendHistoryRequest(sessionId: sessionId)
} catch {
log.error("Failed to send history_request: \(error.localizedDescription)")
pendingHistoryThreadId = nil
pendingHistoryBySessionId.removeValue(forKey: sessionId)
}
}

private func handleHistoryResponse(_ response: HistoryResponseMessage) {
guard let threadId = pendingHistoryThreadId else { return }
pendingHistoryThreadId = nil
guard let threadId = pendingHistoryBySessionId.removeValue(forKey: response.sessionId) else { return }

guard let viewModel = chatViewModels[threadId] else { return }
viewModel.populateFromHistory(response.messages)
Expand Down
1 change: 1 addition & 0 deletions clients/macos/vellum-assistant/IPC/IPCMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ struct SessionListResponseMessage: Decodable, Sendable {
/// Response containing message history for a session.
/// Wire type: `"history_response"`
struct HistoryResponseMessage: Decodable, Sendable {
let sessionId: String
struct HistoryToolCallItem: Decodable, Sendable {
let name: String
let input: [String: AnyCodable]
Expand Down
Loading