Skip to content

Commit 2e8e578

Browse files
committed
Streaming fix -- need to test more
1 parent 8afa184 commit 2e8e578

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ export async function POST(req: NextRequest) {
238238
mode,
239239
model: selectedModel,
240240
provider,
241-
conversationHistory,
242241
contexts: agentContexts,
243242
fileAttachments,
244243
commands,
@@ -277,7 +276,7 @@ export async function POST(req: NextRequest) {
277276
streamId: userMessageIdToUse,
278277
chatId: actualChatId,
279278
currentChat,
280-
conversationHistory,
279+
isNewChat: conversationHistory.length === 0,
281280
message,
282281
titleModel: selectedModel,
283282
titleProvider: provider,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ export async function POST(req: NextRequest) {
152152
userMessageId,
153153
mode: 'agent',
154154
model: '',
155-
conversationHistory,
156155
contexts: agentContexts,
157156
fileAttachments,
158157
chatId: actualChatId,
@@ -169,7 +168,7 @@ export async function POST(req: NextRequest) {
169168
streamId: userMessageId,
170169
chatId: actualChatId,
171170
currentChat,
172-
conversationHistory,
171+
isNewChat: conversationHistory.length === 0,
173172
message,
174173
titleModel: 'claude-opus-4-5',
175174
requestId: tracker.requestId,

apps/sim/lib/copilot/chat-payload.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export interface BuildPayloadParams {
1818
mode: string
1919
model: string
2020
provider?: string
21-
conversationHistory?: unknown[]
2221
contexts?: Array<{ type: string; content: string }>
2322
fileAttachments?: Array<{ id: string; key: string; size: number; [key: string]: unknown }>
2423
commands?: string[]
@@ -102,7 +101,6 @@ export async function buildCopilotRequestPayload(
102101
commands,
103102
chatId,
104103
prefetch,
105-
conversationHistory,
106104
implicitFeedback,
107105
} = params
108106

@@ -157,9 +155,6 @@ export async function buildCopilotRequestPayload(
157155
messageId: userMessageId,
158156
...(contexts && contexts.length > 0 ? { context: contexts } : {}),
159157
...(chatId ? { chatId } : {}),
160-
...(Array.isArray(conversationHistory) && conversationHistory.length > 0
161-
? { conversationHistory }
162-
: {}),
163158
...(typeof prefetch === 'boolean' ? { prefetch } : {}),
164159
...(implicitFeedback ? { implicitFeedback } : {}),
165160
...(processedFileContents.length > 0 ? { fileAttachments: processedFileContents } : {}),

apps/sim/lib/copilot/chat-streaming.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export interface StreamingOrchestrationParams {
6868
streamId: string
6969
chatId?: string
7070
currentChat: any
71-
conversationHistory: unknown[]
71+
isNewChat: boolean
7272
message: string
7373
titleModel: string
7474
titleProvider?: string
@@ -83,7 +83,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
8383
streamId,
8484
chatId,
8585
currentChat,
86-
conversationHistory,
86+
isNewChat,
8787
message,
8888
titleModel,
8989
titleProvider,
@@ -93,6 +93,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
9393

9494
let eventWriter: ReturnType<typeof createStreamEventWriter> | null = null
9595
let clientDisconnected = false
96+
const abortController = new AbortController()
9697

9798
return new ReadableStream({
9899
async start(controller) {
@@ -137,7 +138,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
137138
await pushEvent({ type: 'chat_id', chatId })
138139
}
139140

140-
if (chatId && !currentChat?.title && conversationHistory.length === 0) {
141+
if (chatId && !currentChat?.title && isNewChat) {
141142
requestChatTitle({ message, model: titleModel, provider: titleProvider })
142143
.then(async (title) => {
143144
if (title) {
@@ -153,6 +154,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
153154
try {
154155
await orchestrateCopilotStream(requestPayload, {
155156
...orchestrateOptions,
157+
abortSignal: abortController.signal,
156158
onEvent: async (event) => {
157159
await pushEvent(event)
158160
},
@@ -161,6 +163,12 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
161163
await eventWriter.close()
162164
await setStreamMeta(streamId, { status: 'complete', userId })
163165
} catch (error) {
166+
if (clientDisconnected || abortController.signal.aborted) {
167+
logger.info(`[${requestId}] Stream aborted by client disconnect`)
168+
await eventWriter.close().catch(() => {})
169+
await setStreamMeta(streamId, { status: 'complete', userId })
170+
return
171+
}
164172
logger.error(`[${requestId}] Orchestration error:`, error)
165173
await eventWriter.close()
166174
await setStreamMeta(streamId, {
@@ -180,6 +188,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
180188
},
181189
async cancel() {
182190
clientDisconnected = true
191+
abortController.abort()
183192
if (eventWriter) {
184193
await eventWriter.flush()
185194
}

0 commit comments

Comments
 (0)