Skip to content

Commit cf9cc03

Browse files
committed
Fix tool call persistence in chat
1 parent a091149 commit cf9cc03

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
lines changed

apps/sim/app/api/mothership/chat/route.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,15 @@ export async function POST(req: NextRequest) {
184184
timestamp: new Date().toISOString(),
185185
}
186186

187-
const assistantMessage = {
187+
const assistantMessage: Record<string, unknown> = {
188188
id: crypto.randomUUID(),
189189
role: 'assistant' as const,
190190
content: result.content,
191191
timestamp: new Date().toISOString(),
192192
}
193+
if (result.toolCalls.length > 0) {
194+
assistantMessage.toolCalls = result.toolCalls
195+
}
193196

194197
const updatedMessages = [...conversationHistory, userMessage, assistantMessage]
195198

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type TaskChatHistory,
99
type TaskStoredContentBlock,
1010
type TaskStoredMessage,
11+
type TaskStoredToolCall,
1112
taskKeys,
1213
useChatHistory,
1314
} from '@/hooks/queries/tasks'
@@ -59,20 +60,38 @@ function mapStoredBlock(block: TaskStoredContentBlock): ContentBlock {
5960
name: block.toolCall.name ?? 'unknown',
6061
status: STATE_TO_STATUS[block.toolCall.state ?? ''] ?? 'success',
6162
displayTitle: block.toolCall.display?.text,
63+
result: block.toolCall.result,
6264
}
6365
}
6466

6567
return mapped
6668
}
6769

70+
function mapStoredToolCall(tc: TaskStoredToolCall): ContentBlock {
71+
return {
72+
type: 'tool_call',
73+
toolCall: {
74+
id: tc.id,
75+
name: tc.name,
76+
status: (STATE_TO_STATUS[tc.status] ?? 'success') as ToolCallStatus,
77+
result:
78+
tc.result != null
79+
? { success: tc.status === 'success', output: tc.result, error: tc.error }
80+
: undefined,
81+
},
82+
}
83+
}
84+
6885
function mapStoredMessage(msg: TaskStoredMessage): ChatMessage {
6986
const mapped: ChatMessage = {
7087
id: msg.id,
7188
role: msg.role,
7289
content: msg.content,
7390
}
7491

75-
if (Array.isArray(msg.contentBlocks) && msg.contentBlocks.length > 0) {
92+
if (Array.isArray(msg.toolCalls) && msg.toolCalls.length > 0) {
93+
mapped.contentBlocks = msg.toolCalls.map(mapStoredToolCall)
94+
} else if (Array.isArray(msg.contentBlocks) && msg.contentBlocks.length > 0) {
7695
mapped.contentBlocks = msg.contentBlocks.map(mapStoredBlock)
7796
}
7897

@@ -277,7 +296,13 @@ export function useChat(workspaceId: string, initialChatId?: string): UseChatRet
277296
if (!id) break
278297
const idx = toolMap.get(id)
279298
if (idx !== undefined && blocks[idx].toolCall) {
280-
blocks[idx].toolCall!.status = parsed.success ? 'success' : 'error'
299+
const tc = blocks[idx].toolCall!
300+
tc.status = parsed.success ? 'success' : 'error'
301+
tc.result = {
302+
success: !!parsed.success,
303+
output: parsed.result ?? getPayloadData(parsed)?.result,
304+
error: (parsed.error ?? getPayloadData(parsed)?.error) as string | undefined,
305+
}
281306
flush()
282307
}
283308

apps/sim/app/workspace/[workspaceId]/home/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface ToolCallInfo {
55
name: string
66
status: ToolCallStatus
77
displayTitle?: string
8+
result?: { success: boolean; output?: unknown; error?: string }
89
}
910

1011
export type ContentBlockType = 'text' | 'tool_call' | 'subagent'
@@ -49,7 +50,8 @@ export interface SSEPayloadData {
4950
agent?: string
5051
arguments?: Record<string, unknown>
5152
input?: Record<string, unknown>
52-
result?: Record<string, unknown>
53+
result?: unknown
54+
error?: string
5355
}
5456

5557
export interface SSEPayload {
@@ -61,9 +63,9 @@ export interface SSEPayload {
6163
toolName?: string
6264
ui?: SSEPayloadUI
6365
success?: boolean
66+
result?: unknown
6467
error?: string
6568
subagent?: string
66-
result?: Record<string, unknown>
6769
}
6870

6971
export type MothershipResourceType = 'table' | 'file' | 'workflow'

apps/sim/hooks/queries/tasks.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@ export interface TaskChatHistory {
1313
activeStreamId: string | null
1414
}
1515

16+
export interface TaskStoredToolCall {
17+
id: string
18+
name: string
19+
status: string
20+
params?: Record<string, unknown>
21+
result?: unknown
22+
error?: string
23+
durationMs?: number
24+
}
25+
1626
export interface TaskStoredMessage {
1727
id: string
1828
role: 'user' | 'assistant'
1929
content: string
30+
toolCalls?: TaskStoredToolCall[]
2031
contentBlocks?: TaskStoredContentBlock[]
2132
}
2233

@@ -27,6 +38,8 @@ export interface TaskStoredContentBlock {
2738
id?: string
2839
name?: string
2940
state?: string
41+
params?: Record<string, unknown>
42+
result?: { success: boolean; output?: unknown; error?: string }
3043
display?: { text?: string }
3144
} | null
3245
}

0 commit comments

Comments
 (0)