@@ -12,6 +12,7 @@ import {
1212} from '@/lib/copilot/auth'
1313import { getCopilotModel } from '@/lib/copilot/config'
1414import { TITLE_GENERATION_SYSTEM_PROMPT , TITLE_GENERATION_USER_PROMPT } from '@/lib/copilot/prompts'
15+ import { getBlocksAndToolsTool } from '@/lib/copilot/tools/server-tools/blocks/get-blocks-and-tools'
1516import { env } from '@/lib/env'
1617import { createLogger } from '@/lib/logs/console/logger'
1718import { SIM_AGENT_API_URL_DEFAULT } from '@/lib/sim-agent'
@@ -409,6 +410,26 @@ export async function POST(req: NextRequest) {
409410 [ ...messages ] . reverse ( ) . find ( ( m ) => m ?. role === 'user' ) || messages [ messages . length - 1 ]
410411 const messagesForAgent = effectiveConversationId ? [ latestUserMessage ] : messages
411412
413+ // Prepare optional prefetch results
414+ let prefetchResults : Record < string , any > | undefined
415+ if ( effectivePrefetch === true ) {
416+ try {
417+ const prefetchResp = await getBlocksAndToolsTool . execute ( { } )
418+ if ( prefetchResp . success ) {
419+ prefetchResults = { get_blocks_and_tools : prefetchResp . data }
420+ logger . info ( `[${ tracker . requestId } ] Prepared prefetchResults for streaming payload` , {
421+ hasBlocksAndTools : ! ! prefetchResp . data ,
422+ } )
423+ } else {
424+ logger . warn ( `[${ tracker . requestId } ] Failed to prefetch get_blocks_and_tools` , {
425+ error : prefetchResp . error ,
426+ } )
427+ }
428+ } catch ( e ) {
429+ logger . error ( `[${ tracker . requestId } ] Error while preparing prefetchResults` , e )
430+ }
431+ }
432+
412433 const requestPayload = {
413434 messages : messagesForAgent ,
414435 workflowId,
@@ -422,6 +443,7 @@ export async function POST(req: NextRequest) {
422443 ...( typeof effectivePrefetch === 'boolean' ? { prefetch : effectivePrefetch } : { } ) ,
423444 ...( session ?. user ?. name && { userName : session . user . name } ) ,
424445 ...( requestOrigin ? { origin : requestOrigin } : { } ) ,
446+ ...( prefetchResults ? { prefetchResults } : { } ) ,
425447 }
426448
427449 // Log the payload being sent to the streaming endpoint
@@ -438,10 +460,6 @@ export async function POST(req: NextRequest) {
438460 messagesCount : requestPayload . messages . length ,
439461 ...( requestOrigin ? { origin : requestOrigin } : { } ) ,
440462 } )
441- // Full payload as JSON string
442- logger . info (
443- `[${ tracker . requestId } ] Full streaming payload: ${ JSON . stringify ( requestPayload ) } `
444- )
445463 } catch ( e ) {
446464 logger . warn ( `[${ tracker . requestId } ] Failed to log payload preview for streaming endpoint` , e )
447465 }
@@ -622,6 +640,15 @@ export async function POST(req: NextRequest) {
622640 if ( event . data ?. id ) {
623641 announcedToolCallIds . add ( event . data . id )
624642 }
643+ if ( event . data ?. name === 'get_user_workflow' ) {
644+ logger . info (
645+ `[${ tracker . requestId } ] get_user_workflow tool call received in stream; client will execute locally and post to /api/copilot/methods` ,
646+ {
647+ toolCallId : event . data ?. id ,
648+ hasArgs : ! ! event . data ?. arguments ,
649+ }
650+ )
651+ }
625652 }
626653 break
627654
0 commit comments