diff --git a/apps/sim/app/api/copilot/chat/route.ts b/apps/sim/app/api/copilot/chat/route.ts index 314d256433..ddf89ad1b2 100644 --- a/apps/sim/app/api/copilot/chat/route.ts +++ b/apps/sim/app/api/copilot/chat/route.ts @@ -238,7 +238,6 @@ export async function POST(req: NextRequest) { mode, model: selectedModel, provider, - conversationHistory, contexts: agentContexts, fileAttachments, commands, @@ -277,7 +276,7 @@ export async function POST(req: NextRequest) { streamId: userMessageIdToUse, chatId: actualChatId, currentChat, - conversationHistory, + isNewChat: conversationHistory.length === 0, message, titleModel: selectedModel, titleProvider: provider, diff --git a/apps/sim/app/api/mothership/chat/route.ts b/apps/sim/app/api/mothership/chat/route.ts index 603cadaf5e..d1f016964c 100644 --- a/apps/sim/app/api/mothership/chat/route.ts +++ b/apps/sim/app/api/mothership/chat/route.ts @@ -152,7 +152,6 @@ export async function POST(req: NextRequest) { userMessageId, mode: 'agent', model: '', - conversationHistory, contexts: agentContexts, fileAttachments, chatId: actualChatId, @@ -169,7 +168,7 @@ export async function POST(req: NextRequest) { streamId: userMessageId, chatId: actualChatId, currentChat, - conversationHistory, + isNewChat: conversationHistory.length === 0, message, titleModel: 'claude-opus-4-5', requestId: tracker.requestId, diff --git a/apps/sim/lib/copilot/chat-payload.ts b/apps/sim/lib/copilot/chat-payload.ts index 13f2e2e2fa..1157ea6cea 100644 --- a/apps/sim/lib/copilot/chat-payload.ts +++ b/apps/sim/lib/copilot/chat-payload.ts @@ -18,7 +18,6 @@ export interface BuildPayloadParams { mode: string model: string provider?: string - conversationHistory?: unknown[] contexts?: Array<{ type: string; content: string }> fileAttachments?: Array<{ id: string; key: string; size: number; [key: string]: unknown }> commands?: string[] @@ -102,7 +101,6 @@ export async function buildCopilotRequestPayload( commands, chatId, prefetch, - conversationHistory, implicitFeedback, } = params @@ -157,9 +155,6 @@ export async function buildCopilotRequestPayload( messageId: userMessageId, ...(contexts && contexts.length > 0 ? { context: contexts } : {}), ...(chatId ? { chatId } : {}), - ...(Array.isArray(conversationHistory) && conversationHistory.length > 0 - ? { conversationHistory } - : {}), ...(typeof prefetch === 'boolean' ? { prefetch } : {}), ...(implicitFeedback ? { implicitFeedback } : {}), ...(processedFileContents.length > 0 ? { fileAttachments: processedFileContents } : {}), diff --git a/apps/sim/lib/copilot/chat-streaming.ts b/apps/sim/lib/copilot/chat-streaming.ts index 883d9d2bff..cf45f7bed5 100644 --- a/apps/sim/lib/copilot/chat-streaming.ts +++ b/apps/sim/lib/copilot/chat-streaming.ts @@ -68,7 +68,7 @@ export interface StreamingOrchestrationParams { streamId: string chatId?: string currentChat: any - conversationHistory: unknown[] + isNewChat: boolean message: string titleModel: string titleProvider?: string @@ -83,7 +83,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS streamId, chatId, currentChat, - conversationHistory, + isNewChat, message, titleModel, titleProvider, @@ -93,6 +93,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS let eventWriter: ReturnType | null = null let clientDisconnected = false + const abortController = new AbortController() return new ReadableStream({ async start(controller) { @@ -137,7 +138,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS await pushEvent({ type: 'chat_id', chatId }) } - if (chatId && !currentChat?.title && conversationHistory.length === 0) { + if (chatId && !currentChat?.title && isNewChat) { requestChatTitle({ message, model: titleModel, provider: titleProvider }) .then(async (title) => { if (title) { @@ -153,6 +154,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS try { await orchestrateCopilotStream(requestPayload, { ...orchestrateOptions, + abortSignal: abortController.signal, onEvent: async (event) => { await pushEvent(event) }, @@ -161,6 +163,12 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS await eventWriter.close() await setStreamMeta(streamId, { status: 'complete', userId }) } catch (error) { + if (clientDisconnected || abortController.signal.aborted) { + logger.info(`[${requestId}] Stream aborted by client disconnect`) + await eventWriter.close().catch(() => {}) + await setStreamMeta(streamId, { status: 'complete', userId }) + return + } logger.error(`[${requestId}] Orchestration error:`, error) await eventWriter.close() await setStreamMeta(streamId, { @@ -180,6 +188,7 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS }, async cancel() { clientDisconnected = true + abortController.abort() if (eventWriter) { await eventWriter.flush() }