mirror of
https://github.com/simstudioai/sim.git
synced 2026-10-25 09:34:44 +08:00
Streaming fix -- need to test more
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 } : {}),
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user