@@ -8,15 +8,12 @@ import { getSession } from '@/lib/auth'
88import { getAccessibleCopilotChat , resolveOrCreateChat } from '@/lib/copilot/chat-lifecycle'
99import { buildCopilotRequestPayload } from '@/lib/copilot/chat-payload'
1010import {
11- acquirePendingChatStream ,
1211 createSSEStream ,
13- releasePendingChatStream ,
1412 requestChatTitle ,
1513 SSE_RESPONSE_HEADERS ,
1614} from '@/lib/copilot/chat-streaming'
1715import { COPILOT_REQUEST_MODES } from '@/lib/copilot/models'
1816import { orchestrateCopilotStream } from '@/lib/copilot/orchestrator'
19- import { getStreamMeta , readStreamEvents } from '@/lib/copilot/orchestrator/stream/buffer'
2017import type { OrchestratorResult } from '@/lib/copilot/orchestrator/types'
2118import { resolveActiveResourceContext } from '@/lib/copilot/process-contents'
2219import {
@@ -110,9 +107,6 @@ const ChatMessageSchema = z.object({
110107export async function POST ( req : NextRequest ) {
111108 const tracker = createRequestTracker ( )
112109 let actualChatId : string | undefined
113- let pendingChatStreamAcquired = false
114- let pendingChatStreamHandedOff = false
115- let pendingChatStreamID : string | undefined
116110
117111 try {
118112 // Get session to access user information including name
@@ -340,21 +334,6 @@ export async function POST(req: NextRequest) {
340334 } )
341335 } catch { }
342336
343- if ( stream && actualChatId ) {
344- const acquired = await acquirePendingChatStream ( actualChatId , userMessageIdToUse )
345- if ( ! acquired ) {
346- return NextResponse . json (
347- {
348- error :
349- 'A response is already in progress for this chat. Wait for it to finish or use Stop.' ,
350- } ,
351- { status : 409 }
352- )
353- }
354- pendingChatStreamAcquired = true
355- pendingChatStreamID = userMessageIdToUse
356- }
357-
358337 if ( actualChatId ) {
359338 const userMsg = {
360339 id : userMessageIdToUse ,
@@ -401,7 +380,6 @@ export async function POST(req: NextRequest) {
401380 titleProvider : provider ,
402381 requestId : tracker . requestId ,
403382 workspaceId : resolvedWorkspaceId ,
404- pendingChatStreamAlreadyRegistered : Boolean ( actualChatId && stream ) ,
405383 orchestrateOptions : {
406384 userId : authenticatedUserId ,
407385 workflowId,
@@ -489,7 +467,6 @@ export async function POST(req: NextRequest) {
489467 } ,
490468 } ,
491469 } )
492- pendingChatStreamHandedOff = true
493470
494471 return new Response ( sseStream , { headers : SSE_RESPONSE_HEADERS } )
495472 }
@@ -595,14 +572,6 @@ export async function POST(req: NextRequest) {
595572 } ,
596573 } )
597574 } catch ( error ) {
598- if (
599- actualChatId &&
600- pendingChatStreamAcquired &&
601- ! pendingChatStreamHandedOff &&
602- pendingChatStreamID
603- ) {
604- await releasePendingChatStream ( actualChatId , pendingChatStreamID ) . catch ( ( ) => { } )
605- }
606575 const duration = tracker . getDuration ( )
607576
608577 if ( error instanceof z . ZodError ) {
@@ -649,30 +618,6 @@ export async function GET(req: NextRequest) {
649618 return NextResponse . json ( { success : false , error : 'Chat not found' } , { status : 404 } )
650619 }
651620
652- let streamSnapshot : {
653- events : Array < { eventId : number ; streamId : string ; event : Record < string , unknown > } >
654- status : string
655- } | null = null
656-
657- if ( chat . conversationId ) {
658- try {
659- const [ meta , events ] = await Promise . all ( [
660- getStreamMeta ( chat . conversationId ) ,
661- readStreamEvents ( chat . conversationId , 0 ) ,
662- ] )
663- streamSnapshot = {
664- events : events || [ ] ,
665- status : meta ?. status || 'unknown' ,
666- }
667- } catch ( err ) {
668- logger . warn ( 'Failed to read stream snapshot for chat' , {
669- chatId,
670- conversationId : chat . conversationId ,
671- error : err instanceof Error ? err . message : String ( err ) ,
672- } )
673- }
674- }
675-
676621 const transformedChat = {
677622 id : chat . id ,
678623 title : chat . title ,
@@ -681,11 +626,10 @@ export async function GET(req: NextRequest) {
681626 messageCount : Array . isArray ( chat . messages ) ? chat . messages . length : 0 ,
682627 planArtifact : chat . planArtifact || null ,
683628 config : chat . config || null ,
684- conversationId : chat . conversationId || null ,
629+ activeStreamId : chat . conversationId || null ,
685630 resources : Array . isArray ( chat . resources ) ? chat . resources : [ ] ,
686631 createdAt : chat . createdAt ,
687632 updatedAt : chat . updatedAt ,
688- ...( streamSnapshot ? { streamSnapshot } : { } ) ,
689633 }
690634
691635 logger . info ( `Retrieved chat ${ chatId } ` )
0 commit comments