@@ -13,6 +13,8 @@ import {
1313import { getCopilotModel } from '@/lib/copilot/config'
1414import { TITLE_GENERATION_SYSTEM_PROMPT , TITLE_GENERATION_USER_PROMPT } from '@/lib/copilot/prompts'
1515import { getBlocksAndToolsTool } from '@/lib/copilot/tools/server-tools/blocks/get-blocks-and-tools'
16+ import { getEnvironmentVariablesTool } from '@/lib/copilot/tools/server-tools/user/get-environment-variables'
17+ import { getOAuthCredentialsTool } from '@/lib/copilot/tools/server-tools/user/get-oauth-credentials'
1618import { env } from '@/lib/env'
1719import { createLogger } from '@/lib/logs/console/logger'
1820import { SIM_AGENT_API_URL_DEFAULT } from '@/lib/sim-agent'
@@ -90,6 +92,7 @@ const ChatMessageSchema = z.object({
9092 fileAttachments : z . array ( FileAttachmentSchema ) . optional ( ) ,
9193 provider : z . string ( ) . optional ( ) . default ( 'openai' ) ,
9294 conversationId : z . string ( ) . optional ( ) ,
95+ userWorkflow : z . string ( ) . optional ( ) ,
9396} )
9497
9598/**
@@ -207,6 +210,7 @@ export async function POST(req: NextRequest) {
207210 fileAttachments,
208211 provider,
209212 conversationId,
213+ userWorkflow,
210214 } = ChatMessageSchema . parse ( body )
211215
212216 // Derive request origin for downstream service
@@ -244,6 +248,7 @@ export async function POST(req: NextRequest) {
244248 depth,
245249 prefetch,
246250 origin : requestOrigin ,
251+ hasUserWorkflow : ! ! userWorkflow ,
247252 } )
248253
249254 // Handle chat context
@@ -414,17 +419,54 @@ export async function POST(req: NextRequest) {
414419 let prefetchResults : Record < string , any > | undefined
415420 if ( effectivePrefetch === true ) {
416421 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- } )
422+ const [ blocksAndToolsResp , envVarsResp , oauthResp ] = await Promise . all ( [
423+ getBlocksAndToolsTool . execute ( { } ) ,
424+ getEnvironmentVariablesTool . execute ( { userId : authenticatedUserId , workflowId } ) ,
425+ getOAuthCredentialsTool . execute ( { userId : authenticatedUserId } ) ,
426+ ] )
427+
428+ prefetchResults = { }
429+
430+ if ( blocksAndToolsResp . success ) {
431+ prefetchResults . get_blocks_and_tools = blocksAndToolsResp . data
423432 } else {
424433 logger . warn ( `[${ tracker . requestId } ] Failed to prefetch get_blocks_and_tools` , {
425- error : prefetchResp . error ,
434+ error : blocksAndToolsResp . error ,
435+ } )
436+ }
437+
438+ if ( envVarsResp . success ) {
439+ prefetchResults . get_environment_variables = envVarsResp . data
440+ } else {
441+ logger . warn ( `[${ tracker . requestId } ] Failed to prefetch get_environment_variables` , {
442+ error : envVarsResp . error ,
443+ } )
444+ }
445+
446+ if ( oauthResp . success ) {
447+ prefetchResults . get_oauth_credentials = oauthResp . data
448+ } else {
449+ logger . warn ( `[${ tracker . requestId } ] Failed to prefetch get_oauth_credentials` , {
450+ error : oauthResp . error ,
451+ } )
452+ }
453+
454+ if ( userWorkflow && typeof userWorkflow === 'string' && userWorkflow . trim ( ) . length > 0 ) {
455+ prefetchResults . get_user_workflow = userWorkflow
456+ const uwLength = userWorkflow . length
457+ const uwPreview = userWorkflow . substring ( 0 , 10000 )
458+ logger . info ( `[${ tracker . requestId } ] Included client-provided userWorkflow in prefetch` , {
459+ length : uwLength ,
460+ preview : `${ uwPreview } ${ uwLength > 10000 ? '...' : '' } ` ,
426461 } )
427462 }
463+
464+ logger . info ( `[${ tracker . requestId } ] Prepared prefetchResults for streaming payload` , {
465+ hasBlocksAndTools : ! ! prefetchResults . get_blocks_and_tools ,
466+ hasEnvVars : ! ! prefetchResults . get_environment_variables ,
467+ hasOAuthCreds : ! ! prefetchResults . get_oauth_credentials ,
468+ hasUserWorkflow : ! ! prefetchResults . get_user_workflow ,
469+ } )
428470 } catch ( e ) {
429471 logger . error ( `[${ tracker . requestId } ] Error while preparing prefetchResults` , e )
430472 }
0 commit comments