Skip to content

Commit 08ad250

Browse files
authored
Merge branch 'main' into users/shlomiyosef/converter-use-porp-name-and-ignore
2 parents b083807 + 47f317d commit 08ad250

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+290
-223
lines changed

src/libraries/Builder/Microsoft.Agents.Builder/State/TurnState.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public TurnState(IStorage storage, params IAgentState[] agentStates)
5757
public PrivateConversationState Private => GetScope<PrivateConversationState>();
5858
public TempState Temp => GetScope<TempState>();
5959

60-
public bool HasValue(string path)
60+
public virtual bool HasValue(string path)
6161
{
6262
var (scope, property) = GetScopeAndPath(path);
6363
return GetScope(scope).HasValue(property);
@@ -69,13 +69,13 @@ public T GetValue<T>(string name, Func<T> defaultValueFactory = null)
6969
return GetScope(scope).GetValue(property, defaultValueFactory);
7070
}
7171

72-
public void SetValue(string path, object value)
72+
public virtual void SetValue(string path, object value)
7373
{
7474
var (scope, property) = GetScopeAndPath(path);
7575
GetScope(scope).SetValue(property, value);
7676
}
7777

78-
public void DeleteValue(string path)
78+
public virtual void DeleteValue(string path)
7979
{
8080
var (scope, property) = GetScopeAndPath(path);
8181
GetScope(scope).DeleteValue(property);

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/AI.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ namespace Microsoft.Agents.Extensions.Teams.AI
1515
/// </summary>
1616
/// <remarks>
1717
/// The AI system is responsible for generating plans, moderating input and output, and
18-
/// generating prompts. It can be used free standing or routed to by the <see cref="Application{TState}"/> object.
18+
/// generating prompts. It can be used free standing or routed to by the Application{TState} object.
1919
/// </remarks>
20-
/// <typeparam name="TState">Optional. Type of the turn state.</typeparam>
20+
//// <typeparam name="TState">Optional. Type of the turn state.</typeparam>
2121
public class AISystem
2222
{
23-
private readonly IActionCollection<ITurnState> _actions;
23+
private readonly ActionCollection<ITurnState> _actions;
2424

2525
/// <summary>
26-
/// Creates an instance of the <see cref="AI{TState}"/> class.
26+
/// Creates an instance of the AISystem{TState} class.
2727
/// </summary>
2828
/// <param name="options">The options to configure.</param>
2929
/// <param name="loggerFactory">Optional. The logger factory to use.</param>
@@ -114,7 +114,7 @@ public AISystem RegisterAction(string name, IActionHandler<ITurnState> handler)
114114
/// Registers the default handler for a named action.
115115
/// </summary>
116116
/// <remarks>
117-
/// Default handlers can be replaced by calling the <see cref="RegisterAction(string, IActionHandler{TState})"/> method with the same name.
117+
/// Default handlers can be replaced by calling the RegisterAction(string, ActionHandler{TState}) method with the same name.
118118
/// </remarks>
119119
/// <param name="name">The name of the action.</param>
120120
/// <param name="handler">The action handler function.</param>
@@ -341,7 +341,9 @@ await _actions[AIConstants.TooManyStepsActionName]
341341
}
342342
}
343343

344+
#pragma warning disable CA1822 // Mark members as static
344345
private void _SetTempStateValues(ITurnState turnState, ITurnContext turnContext)
346+
#pragma warning restore CA1822 // Mark members as static
345347
{
346348
TempState? tempState = turnState.Temp;
347349

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionAttribute.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
using System;
3+
24
namespace Microsoft.Agents.Extensions.Teams.AI.Action
35
{
46
/// <summary>

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionCollection.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Microsoft.Agents.Builder.State;
22
using Microsoft.Agents.Extensions.Teams.AI.State;
3+
using System;
4+
using System.Collections.Generic;
35
using System.Runtime.CompilerServices;
46

57
namespace Microsoft.Agents.Extensions.Teams.AI.Action
@@ -18,11 +20,11 @@ public ActionEntry<TState> this[string actionName]
1820
{
1921
get
2022
{
21-
if (!_actions.ContainsKey(actionName))
23+
if (!_actions.TryGetValue(actionName, out ActionEntry<TState>? actionValue))
2224
{
2325
throw new ArgumentException($"`{actionName}` action does not exist");
2426
}
25-
return _actions[actionName];
27+
return actionValue;
2628
}
2729
}
2830

@@ -47,7 +49,7 @@ public bool ContainsAction(string actionName)
4749

4850
public bool TryGetAction(string actionName, out ActionEntry<TState> actionEntry)
4951
{
50-
return _actions.TryGetValue(actionName, out actionEntry);
52+
return _actions.TryGetValue(actionName, out actionEntry!);
5153
}
5254
}
5355
}

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionHandler.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using System.Reflection;
22
using Microsoft.Agents.Builder.State;
33
using Microsoft.Agents.Builder;
4+
using System;
5+
using System.Threading.Tasks;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading;
49

510
namespace Microsoft.Agents.Extensions.Teams.AI.Action
611
{
@@ -37,7 +42,7 @@ internal ActionHandler(MethodInfo method, object containerInstance)
3742
foreach (ParameterInfo parameter in parameters)
3843
{
3944
IEnumerable<ActionParameterAttribute> parameterAttributes = parameter.GetCustomAttributes(typeof(ActionParameterAttribute), true).Cast<ActionParameterAttribute>();
40-
ActionParameterAttribute parameterAttribute = parameterAttributes.FirstOrDefault();
45+
ActionParameterAttribute? parameterAttribute = parameterAttributes.FirstOrDefault();
4146
if (parameterAttribute == null)
4247
{
4348
parameterTypes.Add(Tuple.Create(ActionParameterType.Unknown, parameter.ParameterType));
@@ -92,18 +97,18 @@ public async Task<string> PerformActionAsync(ITurnContext turnContext, TState tu
9297

9398
try
9499
{
95-
object result = _method.Invoke(_containerInstance, parameters.ToArray());
100+
object? result = _method.Invoke(_containerInstance, parameters.ToArray());
96101
if (_returnType == typeof(string))
97102
{
98-
return (string)result;
103+
return (string)result!;
99104
}
100105
else if (_returnType == typeof(Task<string>))
101106
{
102-
return await ((Task<string>)result).ConfigureAwait(false);
107+
return await ((Task<string>)result!).ConfigureAwait(false);
103108
}
104109
else if (_returnType == typeof(ValueTask<string>))
105110
{
106-
return await ((ValueTask<string>)result).ConfigureAwait(false);
111+
return await ((ValueTask<string>)result!).ConfigureAwait(false);
107112
}
108113
else
109114
{

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionNameAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Microsoft.Agents.Extensions.Teams.AI.Action
1+
using System;
2+
3+
namespace Microsoft.Agents.Extensions.Teams.AI.Action
24
{
35
/// <summary>
46
/// Attribute to represent the action name parameter of an action method.

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionParameterAttribute.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
namespace Microsoft.Agents.Extensions.Teams.AI.Action
1+
using System;
2+
3+
namespace Microsoft.Agents.Extensions.Teams.AI.Action
24
{
35
/// <summary>
46
/// Attribute that marks a method parameter as an action parameter.
57
/// </summary>
8+
#pragma warning disable CA1018 // Mark attributes with AttributeUsageAttribute
69
public class ActionParameterAttribute : Attribute
10+
#pragma warning restore CA1018 // Mark attributes with AttributeUsageAttribute
711
{
812
/// <summary>
913
/// The attribute type that represents what the action parameter is.

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/ActionParametersAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Microsoft.Agents.Extensions.Teams.AI.Action
1+
using System;
2+
3+
namespace Microsoft.Agents.Extensions.Teams.AI.Action
24
{
35
/// <summary>
46
/// Attribute to represent the action parameters of an action method.

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Action/DefaultActions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using Microsoft.Agents.Extensions.Teams.AI.Planners;
1+
#pragma warning disable CA1822 // Mark members as static
2+
using Microsoft.Agents.Builder;
3+
using Microsoft.Agents.Builder.State;
4+
using Microsoft.Agents.Core.Models;
25
using Microsoft.Agents.Extensions.Teams.AI.Exceptions;
3-
using Microsoft.Agents.Extensions.Teams.AI.State;
6+
using Microsoft.Agents.Extensions.Teams.AI.Planners;
47
using Microsoft.Agents.Extensions.Teams.AI.Utilities;
58
using Microsoft.Extensions.Logging;
6-
using Microsoft.Agents.Builder;
79
using Microsoft.Extensions.Logging.Abstractions;
8-
using Microsoft.Agents.Extensions.Teams.AI.Models;
9-
using Microsoft.Agents.Builder.State;
10-
using Microsoft.Agents.Core.Models;
1110

1211
namespace Microsoft.Agents.Extensions.Teams.AI.Action
1312
{
@@ -21,7 +20,7 @@ public DefaultActions(bool enableFeedbackLoop = false, string feedbackLoopType =
2120
{
2221
_feedbackLoopType = feedbackLoopType;
2322
_enableFeedbackLoop = enableFeedbackLoop;
24-
_logger = loggerFactory is null ? NullLogger.Instance : loggerFactory.CreateLogger(typeof(DefaultActions<TState>));
23+
_logger = loggerFactory is null ? NullLogger.Instance : loggerFactory.CreateLogger<DefaultActions<TState>>();
2524
}
2625

2726
[Action(AIConstants.UnknownActionName, isDefault: true)]
@@ -184,3 +183,4 @@ public Task<string> TooManyStepsAction([ActionParameters] TooManyStepsParameters
184183
}
185184
}
186185
}
186+
#pragma warning restore CA1822 // Mark members as static

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Augmentations/MonologueAugmentation.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace Microsoft.Agents.Extensions.Teams.AI.Augmentations
2020
/// </summary>
2121
public class InnerMonologue
2222
{
23+
private static readonly string[] _requiredInnerMonologue = ["thoughts", "action"];
24+
2325
/// <summary>
2426
/// Thoughts
2527
/// </summary>
@@ -48,10 +50,12 @@ public InnerMonologue(InnerMonologueThoughts thoughts, InnerMonologueAction acti
4850
/// </summary>
4951
public class InnerMonologueThoughts
5052
{
51-
/// <summary>
52-
/// The LLM's current thought.
53-
/// </summary>
54-
[JsonPropertyName("thought")]
53+
private static readonly string[] _requiredInnerMonologueThoughts = ["thought", "reasoning"];
54+
55+
/// <summary>
56+
/// The LLM's current thought.
57+
/// </summary>
58+
[JsonPropertyName("thought")]
5559
public string Thought { get; set; }
5660

5761
/// <summary>
@@ -93,7 +97,7 @@ public static JsonSchema Schema()
9397
.Description("The LLM's reasoning for the current thought.")
9498
)
9599
)
96-
.Required(new string[] { "thought", "reasoning" })
100+
.Required(_requiredInnerMonologueThoughts)
97101
.Build();
98102
}
99103
}
@@ -103,6 +107,8 @@ public static JsonSchema Schema()
103107
/// </summary>
104108
public class InnerMonologueAction
105109
{
110+
private readonly static string[] _requiredInnerMonologueAction = ["name", "parameters"];
111+
106112
/// <summary>
107113
/// Name of the action to perform.
108114
/// </summary>
@@ -149,7 +155,7 @@ public static JsonSchema Schema()
149155
.Description("Parameters for the action.")
150156
)
151157
)
152-
.Required(new string[] { "name" })
158+
.Required(_requiredInnerMonologueAction)
153159
.Build();
154160
}
155161
}
@@ -172,7 +178,7 @@ public static JsonSchema Schema()
172178
InnerMonologueAction.Schema()
173179
)
174180
)
175-
.Required(new string[] { "thoughts", "action" })
181+
.Required(_requiredInnerMonologue)
176182
.Build();
177183
}
178184
}
@@ -188,14 +194,25 @@ public class MonologueAugmentation : IAugmentation
188194
private const string _missingActionFeedback = "The JSON returned had errors. Apply these fixes:\nadd the \"action\" property to \"instance\"";
189195
private const string _sayRedirectFeedback = "The JSON returned was missing an action. Return a valid JSON object that contains your thoughts and uses the SAY action.";
190196

197+
private static readonly string[] _requiredMonologueAugmentation = ["text"];
198+
private static readonly string[] _defaultSectionAppend = new string[]
199+
{
200+
"Return a JSON object with your thoughts and the next action to perform.",
201+
"Only respond with the JSON format below and base your plan on the actions above.",
202+
"If you're not sure what to do, you can always say something by returning a SAY action.",
203+
"If you're told your JSON response has errors, do your best to fix them.",
204+
"Response Format:",
205+
"{\"thoughts\":{\"thought\":\"<your current thought>\",\"reasoning\":\"<self reflect on why you made this decision>\"},\"action\":{\"name\":\"<action name>\",\"parameters\":{\"<name>\":\"<value>\"}}}"
206+
};
207+
191208
/// <summary>
192209
/// Creates an instance of `MonologueAugmentation`
193210
/// </summary>
194211
/// <param name="actions">Actions</param>
195212
public MonologueAugmentation(List<ChatCompletionAction> actions)
196213
{
197214
List<ChatCompletionAction> _actions = new(actions);
198-
215+
199216
_actions.Add(new("SAY")
200217
{
201218
Description = "use to ask the user a question or say something",
@@ -209,20 +226,11 @@ public MonologueAugmentation(List<ChatCompletionAction> actions)
209226
.Description("text to say or question to ask")
210227
)
211228
)
212-
.Required(new string[] { "text" })
229+
.Required(_requiredMonologueAugmentation)
213230
.Build()
214231
});
215232

216-
this._section = new(_actions, string.Join("\n", new string[]
217-
{
218-
"Return a JSON object with your thoughts and the next action to perform.",
219-
"Only respond with the JSON format below and base your plan on the actions above.",
220-
"If you're not sure what to do, you can always say something by returning a SAY action.",
221-
"If you're told your JSON response has errors, do your best to fix them.",
222-
"Response Format:",
223-
"{\"thoughts\":{\"thought\":\"<your current thought>\",\"reasoning\":\"<self reflect on why you made this decision>\"},\"action\":{\"name\":\"<action name>\",\"parameters\":{\"<name>\":\"<value>\"}}}"
224-
}));
225-
233+
this._section = new(_actions, string.Join("\n", _defaultSectionAppend));
226234
this._monologueValidator = new(InnerMonologue.Schema(), "No valid JSON objects were found in the response. Return a valid JSON object with your thoughts and the next action to perform.");
227235
this._actionValidator = new(_actions, true);
228236
}
@@ -257,7 +265,9 @@ public MonologueAugmentation(List<ChatCompletionAction> actions)
257265

258266
if (value != null)
259267
{
268+
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
260269
text = value.ToString();
270+
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
261271
}
262272
}
263273

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Augmentations/SequenceAugmentation.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ public class SequenceAugmentation : IAugmentation
2020
private readonly ActionAugmentationSection _section;
2121
private readonly JsonResponseValidator _planValidator;
2222
private readonly ActionResponseValidator _actionValidator;
23+
private readonly string[] defaultSection =
24+
[
25+
"Use the actions above to create a plan in the following JSON format:",
26+
"{\"type\":\"plan\",\"commands\":[{\"type\":\"DO\",\"action\":\"<name>\",\"parameters\":{\"<name>\":<value>}},{\"type\":\"SAY\",\"response\":\"<response>\"}]}"
27+
];
2328

2429
/// <summary>
2530
/// Creates an instance of `SequenceAugmentation`
2631
/// </summary>
2732
/// <param name="actions"></param>
2833
public SequenceAugmentation(List<ChatCompletionAction> actions)
2934
{
30-
this._section = new(actions, string.Join("\n", new string[]
31-
{
32-
"Use the actions above to create a plan in the following JSON format:",
33-
"{\"type\":\"plan\",\"commands\":[{\"type\":\"DO\",\"action\":\"<name>\",\"parameters\":{\"<name>\":<value>}},{\"type\":\"SAY\",\"response\":\"<response>\"}]}"
34-
}));
35+
this._section = new(actions, string.Join("\n", defaultSection));
3536

3637
this._planValidator = new(Plan.Schema(), "Return a JSON object that uses the SAY command to say what you're thinking.");
3738
this._actionValidator = new(actions, true);
@@ -115,7 +116,7 @@ public async Task<Validation> ValidateResponseAsync(ITurnContext context, ITurnS
115116
}
116117
else if (command.Type == "SAY")
117118
{
118-
valid = await this._ValidateSayCommandAsync(command, cancellationToken);
119+
valid = await ValidateSayCommandAsync(command, cancellationToken);
119120

120121
if (valid != null)
121122
{
@@ -177,7 +178,7 @@ public async Task<Validation> ValidateResponseAsync(ITurnContext context, ITurnS
177178
return null;
178179
}
179180

180-
private async Task<Validation?> _ValidateSayCommandAsync(IPredictedCommand command, CancellationToken cancellationToken = default)
181+
private static async Task<Validation?> ValidateSayCommandAsync(IPredictedCommand command, CancellationToken cancellationToken = default)
181182
{
182183
PredictedSayCommand? sayCommand = command as PredictedSayCommand;
183184

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Augmentations/ToolsAugmentation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ToolsAugmentation : IAugmentation
2727
{
2828
List<IPredictedCommand> commands = new();
2929

30-
if (response.Message != null && response.Message.ActionCalls != null && response.Message.ActionCalls.Count() > 0)
30+
if (response.Message != null && response.Message.ActionCalls != null && response.Message.ActionCalls.Count > 0)
3131
{
3232
IList<ActionCall> actionCalls = response.Message.ActionCalls;
3333

src/libraries/Extensions/Microsoft.Agents.Extensions.Teams.AI/AI/Clients/LLMClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public async Task<PromptResponse> CompletePromptAsync(
270270

271271
// End the stream and remove pointer from memory
272272
// - We're not listening for the response received event because we can't await the completion of events.
273-
await streamer.EndStreamAsync();
273+
await streamer.EndStreamAsync(cancellationToken);
274274
memory.DeleteValue("temp.streamer");
275275
}
276276
}

0 commit comments

Comments
 (0)