Fix tool call persistence in chat

This commit is contained in:
Siddharth Ganesan
2026-03-09 14:00:02 -07:00
parent a091149da4
commit cf9cc0377d
4 changed files with 48 additions and 5 deletions

View File

@@ -184,12 +184,15 @@ export async function POST(req: NextRequest) {
timestamp: new Date().toISOString(),
}
const assistantMessage = {
const assistantMessage: Record<string, unknown> = {
id: crypto.randomUUID(),
role: 'assistant' as const,
content: result.content,
timestamp: new Date().toISOString(),
}
if (result.toolCalls.length > 0) {
assistantMessage.toolCalls = result.toolCalls
}
const updatedMessages = [...conversationHistory, userMessage, assistantMessage]

View File

@@ -8,6 +8,7 @@ import {
type TaskChatHistory,
type TaskStoredContentBlock,
type TaskStoredMessage,
type TaskStoredToolCall,
taskKeys,
useChatHistory,
} from '@/hooks/queries/tasks'
@@ -59,12 +60,28 @@ function mapStoredBlock(block: TaskStoredContentBlock): ContentBlock {
name: block.toolCall.name ?? 'unknown',
status: STATE_TO_STATUS[block.toolCall.state ?? ''] ?? 'success',
displayTitle: block.toolCall.display?.text,
result: block.toolCall.result,
}
}
return mapped
}
function mapStoredToolCall(tc: TaskStoredToolCall): ContentBlock {
return {
type: 'tool_call',
toolCall: {
id: tc.id,
name: tc.name,
status: (STATE_TO_STATUS[tc.status] ?? 'success') as ToolCallStatus,
result:
tc.result != null
? { success: tc.status === 'success', output: tc.result, error: tc.error }
: undefined,
},
}
}
function mapStoredMessage(msg: TaskStoredMessage): ChatMessage {
const mapped: ChatMessage = {
id: msg.id,
@@ -72,7 +89,9 @@ function mapStoredMessage(msg: TaskStoredMessage): ChatMessage {
content: msg.content,
}
if (Array.isArray(msg.contentBlocks) && msg.contentBlocks.length > 0) {
if (Array.isArray(msg.toolCalls) && msg.toolCalls.length > 0) {
mapped.contentBlocks = msg.toolCalls.map(mapStoredToolCall)
} else if (Array.isArray(msg.contentBlocks) && msg.contentBlocks.length > 0) {
mapped.contentBlocks = msg.contentBlocks.map(mapStoredBlock)
}
@@ -277,7 +296,13 @@ export function useChat(workspaceId: string, initialChatId?: string): UseChatRet
if (!id) break
const idx = toolMap.get(id)
if (idx !== undefined && blocks[idx].toolCall) {
blocks[idx].toolCall!.status = parsed.success ? 'success' : 'error'
const tc = blocks[idx].toolCall!
tc.status = parsed.success ? 'success' : 'error'
tc.result = {
success: !!parsed.success,
output: parsed.result ?? getPayloadData(parsed)?.result,
error: (parsed.error ?? getPayloadData(parsed)?.error) as string | undefined,
}
flush()
}

View File

@@ -5,6 +5,7 @@ export interface ToolCallInfo {
name: string
status: ToolCallStatus
displayTitle?: string
result?: { success: boolean; output?: unknown; error?: string }
}
export type ContentBlockType = 'text' | 'tool_call' | 'subagent'
@@ -49,7 +50,8 @@ export interface SSEPayloadData {
agent?: string
arguments?: Record<string, unknown>
input?: Record<string, unknown>
result?: Record<string, unknown>
result?: unknown
error?: string
}
export interface SSEPayload {
@@ -61,9 +63,9 @@ export interface SSEPayload {
toolName?: string
ui?: SSEPayloadUI
success?: boolean
result?: unknown
error?: string
subagent?: string
result?: Record<string, unknown>
}
export type MothershipResourceType = 'table' | 'file' | 'workflow'

View File

@@ -13,10 +13,21 @@ export interface TaskChatHistory {
activeStreamId: string | null
}
export interface TaskStoredToolCall {
id: string
name: string
status: string
params?: Record<string, unknown>
result?: unknown
error?: string
durationMs?: number
}
export interface TaskStoredMessage {
id: string
role: 'user' | 'assistant'
content: string
toolCalls?: TaskStoredToolCall[]
contentBlocks?: TaskStoredContentBlock[]
}
@@ -27,6 +38,8 @@ export interface TaskStoredContentBlock {
id?: string
name?: string
state?: string
params?: Record<string, unknown>
result?: { success: boolean; output?: unknown; error?: string }
display?: { text?: string }
} | null
}