@@ -237,13 +237,20 @@ function extractBlockOutput(logs: any[], blockId: string, path?: string) {
237237}
238238
239239/**
240- * Executes a workflow for a chat and extracts the specified output.
241- * This function contains the same logic as the internal chat panel.
240+ * Executes a workflow for a chat request and returns the formatted output.
241+ *
242+ * When workflows reference <start.response.input>, they receive a structured JSON
243+ * containing both the message and conversationId for maintaining chat context.
244+ *
245+ * @param chatId - Chat deployment identifier
246+ * @param message - User's chat message
247+ * @param conversationId - Optional ID for maintaining conversation context
248+ * @returns Workflow execution result formatted for the chat interface
242249 */
243- export async function executeWorkflowForChat ( chatId : string , message : string ) {
250+ export async function executeWorkflowForChat ( chatId : string , message : string , conversationId ?: string ) {
244251 const requestId = crypto . randomUUID ( ) . slice ( 0 , 8 )
245252
246- logger . debug ( `[${ requestId } ] Executing workflow for chat: ${ chatId } ` )
253+ logger . debug ( `[${ requestId } ] Executing workflow for chat: ${ chatId } ${ conversationId ? `, conversationId: ${ conversationId } ` : '' } ` )
247254
248255 // Find the chat deployment
249256 const deploymentResult = await db
@@ -418,7 +425,7 @@ export async function executeWorkflowForChat(chatId: string, message: string) {
418425 workflow : serializedWorkflow ,
419426 currentBlockStates : processedBlockStates ,
420427 envVarValues : decryptedEnvVars ,
421- workflowInput : { input : message } ,
428+ workflowInput : { input : message , conversationId } ,
422429 workflowVariables,
423430 contextExtensions : {
424431 // Always request streaming – the executor will downgrade gracefully if unsupported
@@ -490,10 +497,22 @@ export async function executeWorkflowForChat(chatId: string, message: string) {
490497 totalDuration,
491498 }
492499
500+ // Add conversationId to metadata if available
501+ if ( conversationId ) {
502+ if ( ! enrichedResult . metadata ) {
503+ enrichedResult . metadata = {
504+ duration : totalDuration ,
505+ } ;
506+ }
507+ ( enrichedResult . metadata as any ) . conversationId = conversationId ;
508+ }
509+
493510 const executionId = uuidv4 ( )
494511 await persistExecutionLogs ( workflowId , executionId , enrichedResult , 'chat' )
495512 logger . debug (
496- `[${ requestId } ] Persisted execution logs for streaming chat with ID: ${ executionId } `
513+ `[${ requestId } ] Persisted execution logs for streaming chat with ID: ${ executionId } ${
514+ conversationId ? `, conversationId: ${ conversationId } ` : ''
515+ } `
497516 )
498517
499518 // Update user stats for successful streaming chat execution
@@ -562,6 +581,11 @@ export async function executeWorkflowForChat(chatId: string, message: string) {
562581 ...( result . metadata || { } ) ,
563582 source : 'chat' ,
564583 }
584+
585+ // Add conversationId to metadata if available
586+ if ( conversationId ) {
587+ ( result as any ) . metadata . conversationId = conversationId
588+ }
565589 }
566590
567591 // Update user stats to increment totalChatExecutions if the execution was successful
@@ -606,13 +630,26 @@ export async function executeWorkflowForChat(chatId: string, message: string) {
606630 totalDuration,
607631 }
608632
633+ // Add conversation ID to metadata if available
634+ if ( conversationId ) {
635+ if ( ! enrichedResult . metadata ) {
636+ enrichedResult . metadata = {
637+ duration : totalDuration ,
638+ } ;
639+ }
640+ ( enrichedResult . metadata as any ) . conversationId = conversationId ;
641+ }
642+
609643 // Generate a unique execution ID for this chat interaction
610644 const executionId = uuidv4 ( )
611645
612646 // Persist the logs with 'chat' trigger type
613647 await persistExecutionLogs ( workflowId , executionId , enrichedResult , 'chat' )
614648
615- logger . debug ( `[${ requestId } ] Persisted execution logs for chat with ID: ${ executionId } ` )
649+ logger . debug (
650+ `[${ requestId } ] Persisted execution logs for chat with ID: ${ executionId } ${
651+ conversationId ? `, conversationId: ${ conversationId } ` : ''
652+ } `)
616653 } catch ( error ) {
617654 // Don't fail the chat response if logging fails
618655 logger . error ( `[${ requestId } ] Failed to persist chat execution logs:` , error )
0 commit comments