Streaming fix -- need to test more

This commit is contained in:
Siddharth Ganesan
2026-03-10 11:01:14 -07:00
parent 8afa184c64
commit 2e8e578ede
4 changed files with 14 additions and 12 deletions
+1 -2
View File
@@ -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,
+1 -2
View File
@@ -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,
-5
View File
@@ -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 } : {}),
+12 -3
View File
@@ -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<typeof createStreamEventWriter> | 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()
}