@@ -106,20 +106,30 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
106
106
try {
107
107
ChatCompletionService chatCompletionService = kernel .getService (ChatCompletionService .class , arguments );
108
108
109
- PromptExecutionSettings executionSettings = invocationContext .getPromptExecutionSettings () != null
109
+ PromptExecutionSettings executionSettings = invocationContext != null && invocationContext .getPromptExecutionSettings () != null
110
110
? invocationContext .getPromptExecutionSettings ()
111
111
: kernelArguments .getExecutionSettings ().get (chatCompletionService .getServiceId ());
112
112
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 ()
114
119
.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 (
123
133
instructions -> {
124
134
// Create a new chat history with the instructions
125
135
ChatHistory chat = new ChatHistory (
@@ -137,7 +147,7 @@ private Mono<List<ChatMessageContent<?>>> internalInvokeAsync(
137
147
chat .addAll (history );
138
148
int previousHistorySize = chat .getMessages ().size ();
139
149
140
- return chatCompletionService .getChatMessageContentsAsync (chat , kernel , updatedInvocationContext )
150
+ return chatCompletionService .getChatMessageContentsAsync (chat , kernel , agentInvocationContext )
141
151
.map (chatMessageContents -> {
142
152
return chatMessageContents .subList (
143
153
previousHistorySize ,
0 commit comments