@@ -106,20 +106,30 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
106106 try {
107107 ChatCompletionService chatCompletionService = kernel .getService (ChatCompletionService .class , arguments );
108108
109- PromptExecutionSettings executionSettings = invocationContext .getPromptExecutionSettings () != null
109+ PromptExecutionSettings executionSettings = invocationContext != null && invocationContext .getPromptExecutionSettings () != null
110110 ? invocationContext .getPromptExecutionSettings ()
111111 : kernelArguments .getExecutionSettings ().get (chatCompletionService .getServiceId ());
112112
113- final InvocationContext updatedInvocationContext = InvocationContext .builder ()
113+ ToolCallBehavior toolCallBehavior = invocationContext != null
114+ ? invocationContext .getToolCallBehavior ()
115+ : ToolCallBehavior .allowAllKernelFunctions (false );
116+
117+ // Build base invocation context
118+ InvocationContext .Builder builder = InvocationContext .builder ()
114119 .withPromptExecutionSettings (executionSettings )
115- .withToolCallBehavior (invocationContext .getToolCallBehavior ())
116- .withTelemetry (invocationContext .getTelemetry ())
117- .withContextVariableConverter (invocationContext .getContextVariableTypes ())
118- .withKernelHooks (invocationContext .getKernelHooks ())
119- .withReturnMode (InvocationReturnMode .FULL_HISTORY )
120- .build ();
121-
122- return formatInstructionsAsync (kernel , arguments , updatedInvocationContext ).flatMap (
120+ .withToolCallBehavior (toolCallBehavior )
121+ .withReturnMode (InvocationReturnMode .FULL_HISTORY );
122+
123+ if (invocationContext != null ) {
124+ builder = builder
125+ .withTelemetry (invocationContext .getTelemetry ())
126+ .withContextVariableConverter (invocationContext .getContextVariableTypes ())
127+ .withKernelHooks (invocationContext .getKernelHooks ());
128+ }
129+
130+ InvocationContext agentInvocationContext = builder .build ();
131+
132+ return formatInstructionsAsync (kernel , arguments , agentInvocationContext ).flatMap (
123133 instructions -> {
124134 // Create a new chat history with the instructions
125135 ChatHistory chat = new ChatHistory (
@@ -137,7 +147,7 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
137147 chat .addAll (history );
138148 int previousHistorySize = chat .getMessages ().size ();
139149
140- return chatCompletionService .getChatMessageContentsAsync (chat , kernel , updatedInvocationContext )
150+ return chatCompletionService .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
141151 .map (chatMessageContents -> {
142152 return chatMessageContents .subList (
143153 previousHistorySize ,
0 commit comments