Skip to content

Commit effbe44

Browse files
authored
Add some more logging entries and support agent settings (#254)
- Add a few more logging entries at the `Debug` level - Add support for the agent setting, initially with 2 properties only: `logging` and `telemetry`, both are boolean values. - By default, `logging` and `telemetry` are both `true`, meaning both are enabled. - A new config file will be created if it doesn't exist, and the default values will be used.
1 parent 4670645 commit effbe44

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

shell/agents/Microsoft.Azure.Agent/AzureAgent.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class AzureAgent : ILLMAgent
1717

1818
internal ArgumentPlaceholder ArgPlaceholder { set; get; }
1919

20-
private const string SettingFileName = "az.agent.json";
20+
private const string SettingFileName = "az.config.json";
2121
private const string LoggingFileName = "log..txt";
2222
private const string InstructionPrompt = """
2323
NOTE: follow the below instructions when generating responses that include Azure CLI commands with placeholders:
@@ -37,6 +37,7 @@ 7. DO NOT include the placeholder summary when the commands contains no placehol
3737

3838
private int _turnsLeft;
3939
private CopilotResponse _copilotResponse;
40+
private AgentSetting _setting;
4041

4142
private readonly string _instructions;
4243
private readonly StringBuilder _buffer;
@@ -84,17 +85,30 @@ public void Dispose()
8485

8586
public void Initialize(AgentConfig config)
8687
{
87-
_turnsLeft = int.MaxValue;
8888
SettingFile = Path.Combine(config.ConfigurationRoot, SettingFileName);
8989

90-
string logFile = Path.Combine(config.ConfigurationRoot, LoggingFileName);
91-
Log.Logger = new LoggerConfiguration()
92-
.WriteTo.Async(a => a.File(
93-
path: logFile,
94-
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
95-
rollingInterval: RollingInterval.Day))
96-
.CreateLogger();
97-
Log.Information("Azure agent initialized.");
90+
_turnsLeft = int.MaxValue;
91+
_setting = AgentSetting.LoadFromFile(SettingFile);
92+
93+
if (_setting is null)
94+
{
95+
// Use default setting and create a setting file with the default settings.
96+
_setting = AgentSetting.Default;
97+
AgentSetting.NewSettingFile(SettingFile);
98+
}
99+
100+
if (_setting.Logging)
101+
{
102+
string logFile = Path.Combine(config.ConfigurationRoot, LoggingFileName);
103+
Log.Logger = new LoggerConfiguration()
104+
.MinimumLevel.Debug()
105+
.WriteTo.Async(a => a.File(
106+
path: logFile,
107+
outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
108+
rollingInterval: RollingInterval.Day))
109+
.CreateLogger();
110+
Log.Information("Azure agent initialized.");
111+
}
98112
}
99113

100114
public IEnumerable<CommandBase> GetCommands() => [new ReplaceCommand(this)];

shell/agents/Microsoft.Azure.Agent/AzureCopilotReceiver.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ internal static async Task<AzureCopilotReceiver> CreateAsync(string streamUrl)
4343

4444
private async Task ProcessActivities()
4545
{
46+
Log.Debug("[AzureCopilotReceiver] Receiver is up and running.");
47+
4648
while (_webSocket.State is WebSocketState.Open)
4749
{
4850
string closingMessage = null;
@@ -55,18 +57,18 @@ private async Task ProcessActivities()
5557
{
5658
closingMessage = "Close message received";
5759
_activityQueue.Add(new CopilotActivity { Error = new ConnectionDroppedException("The server websocket is closing. Connection dropped.") });
60+
Log.Information("[AzureCopilotReceiver] Web socket closed by server.");
5861
}
5962
}
6063
catch (OperationCanceledException)
6164
{
6265
// Close the web socket before the thread is going away.
6366
closingMessage = "Client closing";
64-
Log.Error("[AzureCopilotReceiver] Receiver thread cancelled, which means the instance was disposed.");
67+
Log.Information("[AzureCopilotReceiver] Receiver was cancelled and disposed.");
6568
}
6669

6770
if (closingMessage is not null)
6871
{
69-
Log.Error("[AzureCopilotReceiver] Sending web socket closing request, message: '{0}'", closingMessage);
7072
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, closingMessage, CancellationToken.None);
7173
_activityQueue.CompleteAdding();
7274
break;

shell/agents/Microsoft.Azure.Agent/ChatSession.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text.Json.Nodes;
66

77
using AIShell.Abstraction;
8+
using Serilog;
89

910
namespace Microsoft.Azure.Agent;
1011

@@ -145,6 +146,8 @@ private async Task StartConversationAsync(IHost host, CancellationToken cancella
145146
_expireOn = DateTime.UtcNow.AddSeconds(spl.ExpiresIn);
146147
_copilotReceiver = await AzureCopilotReceiver.CreateAsync(_streamUrl);
147148

149+
Log.Debug("[ChatSession] Conversation started. Id: {0}", _conversationId);
150+
148151
while (true)
149152
{
150153
CopilotActivity activity = _copilotReceiver.Take(cancellationToken);

shell/agents/Microsoft.Azure.Agent/DataRetriever.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ private ArgumentInfo CreateArgInfo(ArgumentPair pair)
433433
// Handle non-AzCLI command.
434434
if (pair.Parameter is null)
435435
{
436+
Log.Debug("[DataRetriever] Non-AzCLI command: '{0}'", pair.Command);
436437
return new ArgumentInfo(item.Name, item.Desc, dataType);
437438
}
438439

@@ -481,6 +482,8 @@ private List<string> GetArgValues(ArgumentPair pair)
481482
string commandLine = $"{pair.Command} {pair.Parameter} ";
482483
string tempFile = Path.GetTempFileName();
483484

485+
Log.Debug("[DataRetriever] Perform tab completion for '{0}'", commandLine);
486+
484487
try
485488
{
486489
using var process = new Process()

shell/agents/Microsoft.Azure.Agent/Schema.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
1+
using System.Text;
12
using System.Text.Json;
23
using System.Text.Json.Nodes;
34
using System.Text.Json.Serialization;
45
using AIShell.Abstraction;
56

67
namespace Microsoft.Azure.Agent;
78

9+
internal class AgentSetting
10+
{
11+
public bool Logging { get; set; }
12+
public bool Telemetry { get; set; }
13+
14+
public AgentSetting()
15+
{
16+
// Enable logging and telemetry by default.
17+
Logging = true;
18+
Telemetry = true;
19+
}
20+
21+
internal static AgentSetting Default => new();
22+
23+
internal static AgentSetting LoadFromFile(string path)
24+
{
25+
FileInfo file = new(path);
26+
if (file.Exists)
27+
{
28+
try
29+
{
30+
using var stream = file.OpenRead();
31+
return JsonSerializer.Deserialize<AgentSetting>(stream, Utils.JsonOptions);
32+
}
33+
catch (Exception e)
34+
{
35+
throw new InvalidDataException($"Parsing settings from '{path}' failed with the following error: {e.Message}", e);
36+
}
37+
}
38+
39+
return null;
40+
}
41+
42+
internal static void NewSettingFile(string path)
43+
{
44+
const string content = """
45+
{
46+
"logging": true,
47+
"telemetry": true
48+
}
49+
""";
50+
File.WriteAllText(path, content, Encoding.UTF8);
51+
}
52+
}
53+
854
internal class TokenPayload
955
{
1056
public string ConversationId { get; set; }

0 commit comments

Comments
 (0)