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
35 changes: 31 additions & 4 deletions assistant/src/daemon/handlers/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,8 @@ export function handleHistoryRequest(
timestamp: m.timestamp,
...(truncatedToolCalls.length > 0 ? { toolCalls: truncatedToolCalls, toolCallsBeforeText: m.toolCallsBeforeText } : {}),
...(attachments ? { attachments } : {}),
...(m.textSegments.length > 0 ? { textSegments: m.textSegments } : {}),
...(m.contentOrder.length > 0 ? { contentOrder: m.contentOrder } : {}),
...(!wasTruncated && m.textSegments.length > 0 ? { textSegments: m.textSegments } : {}),
...(!wasTruncated && m.contentOrder.length > 0 ? { contentOrder: m.contentOrder } : {}),
Comment thread
ashleeradka marked this conversation as resolved.
...(filteredSurfaces.length > 0 ? { surfaces: filteredSurfaces } : {}),
...(m.subagentNotification ? { subagentNotification: m.subagentNotification } : {}),
...(wasTruncated ? { wasTruncated: true } : {}),
Expand Down Expand Up @@ -927,8 +927,35 @@ export function handleMessageContentRequest(
const content = JSON.parse(dbMessage.content);
const rendered = renderHistoryContent(content);
text = rendered.text || undefined;
if (rendered.toolCalls.length > 0) {
toolCalls = rendered.toolCalls.map((tc) => ({
let mergedToolCalls = rendered.toolCalls;

// Handle legacy conversations where tool_result blocks are stored in the
// following user message rather than inline with the assistant message.
// This mirrors the mergeToolResults logic used by handleHistoryRequest.
if (dbMessage.role === 'assistant' && mergedToolCalls.some((tc) => tc.result === undefined)) {
const nextMsg = conversationStore.getNextMessage(msg.sessionId, dbMessage.createdAt);
if (nextMsg && nextMsg.role === 'user') {
try {
const nextContent = JSON.parse(nextMsg.content);
const nextRendered = renderHistoryContent(nextContent);
if (nextRendered.text.trim() === '' && nextRendered.toolCalls.length > 0) {
for (const resultEntry of nextRendered.toolCalls) {
const unresolved = mergedToolCalls.find((tc) => tc.result === undefined);
if (unresolved) {
unresolved.result = resultEntry.result;
unresolved.isError = resultEntry.isError;
if (resultEntry.imageData) unresolved.imageData = resultEntry.imageData;
}
}
}
} catch {
// Next message isn't valid JSON — skip merging
}
}
}

if (mergedToolCalls.length > 0) {
toolCalls = mergedToolCalls.map((tc) => ({
name: tc.name,
input: tc.input,
...(tc.result !== undefined ? { result: tc.result } : {}),
Expand Down
21 changes: 20 additions & 1 deletion assistant/src/memory/conversation-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { and, asc, count, desc, eq, inArray, isNull, lt, lte, ne, sql } from 'drizzle-orm';
import { and, asc, count, desc, eq, gt, inArray, isNull, lt, lte, ne, sql } from 'drizzle-orm';
import { v4 as uuid } from 'uuid';
import { z } from 'zod';

Expand Down Expand Up @@ -338,6 +338,25 @@ export function getMessageById(messageId: string, conversationId?: string): Mess
return row ? parseMessage(row) : null;
}

/**
* Get the next message in a conversation after a given message (by timestamp).
* Used for legacy tool_result merging in the rehydrate endpoint.
*/
export function getNextMessage(conversationId: string, afterTimestamp: number): MessageRow | null {
const db = getDb();
const row = db
.select()
.from(messages)
.where(and(
eq(messages.conversationId, conversationId),
gt(messages.createdAt, afterTimestamp),
Comment thread
ashleeradka marked this conversation as resolved.
))
.orderBy(asc(messages.createdAt))
.limit(1)
.get();
return row ? parseMessage(row) : null;
}

export interface PaginatedMessagesResult {
messages: MessageRow[];
/** Whether older messages exist beyond the returned page. */
Expand Down