Skip to content

Commit 0713f48

Browse files
author
Theodore Li
committed
Handle compaction event type
1 parent a521c56 commit 0713f48

5 files changed

Lines changed: 1126 additions & 0 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ export function useChat(
527527
const toolArgsMap = new Map<string, Record<string, unknown>>()
528528
const clientExecutionStarted = new Set<string>()
529529
let activeSubagent: string | undefined
530+
let activeCompactionId: string | undefined
530531
let runningText = ''
531532
let lastContentSource: 'main' | 'subagent' | null = null
532533
let streamRequestId: string | undefined
@@ -853,6 +854,44 @@ export function useChat(
853854
}
854855
break
855856
}
857+
case 'context_compaction_start': {
858+
const compactionId = `compaction_${Date.now()}`
859+
activeCompactionId = compactionId
860+
toolMap.set(compactionId, blocks.length)
861+
blocks.push({
862+
type: 'tool_call',
863+
toolCall: {
864+
id: compactionId,
865+
name: 'context_compaction',
866+
status: 'executing',
867+
displayTitle: 'Compacting context...',
868+
},
869+
})
870+
flush()
871+
break
872+
}
873+
case 'context_compaction': {
874+
const compactionId = activeCompactionId || `compaction_${Date.now()}`
875+
activeCompactionId = undefined
876+
const idx = toolMap.get(compactionId)
877+
if (idx !== undefined && blocks[idx]?.toolCall) {
878+
blocks[idx].toolCall!.status = 'success'
879+
blocks[idx].toolCall!.displayTitle = 'Compacted context'
880+
} else {
881+
toolMap.set(compactionId, blocks.length)
882+
blocks.push({
883+
type: 'tool_call',
884+
toolCall: {
885+
id: compactionId,
886+
name: 'context_compaction',
887+
status: 'success',
888+
displayTitle: 'Compacted context',
889+
},
890+
})
891+
}
892+
flush()
893+
break
894+
}
856895
case 'tool_error': {
857896
const id = parsed.toolCallId || getPayloadData(parsed)?.id
858897
if (!id) break

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export type SSEEventType =
4949
| 'structured_result' // structured result from a tool call
5050
| 'subagent_result' // result from a subagent
5151
| 'done' // end of the chat
52+
| 'context_compaction_start' // context compaction started
53+
| 'context_compaction' // conversation context was compacted
5254
| 'error' // error in the chat
5355
| 'start' // start of the chat
5456

@@ -94,6 +96,7 @@ export type MothershipToolName =
9496
| 'edit'
9597
| 'fast_edit'
9698
| 'open_resource'
99+
| 'context_compaction'
97100

98101
/**
99102
* Subagent identifiers dispatched via `subagent_start` SSE events.
@@ -349,6 +352,11 @@ export const TOOL_UI_METADATA: Partial<Record<MothershipToolName, ToolUIMetadata
349352
phaseLabel: 'Resource',
350353
phase: 'resource',
351354
},
355+
context_compaction: {
356+
title: 'Compacted context',
357+
phaseLabel: 'Context',
358+
phase: 'management',
359+
},
352360
}
353361

354362
export interface SSEPayloadUI {

0 commit comments

Comments
 (0)