Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions shell/AIShell.Abstraction/ILLMAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ public interface ILLMAgent : IDisposable
void Initialize(AgentConfig config);

/// <summary>
/// Refresh the current chat by starting a new chat session.
/// Refresh the current chat or force starting a new chat session.
/// This method allows an agent to reset chat states, interact with user for authentication, print welcome message, and more.
/// </summary>
/// <param name="shell">The interface for interacting with the shell.</param>
Task RefreshChatAsync(IShell shell);
/// <param name="force">Whether or not to force creating a new chat session.</param>
Task RefreshChatAsync(IShell shell, bool force);

/// <summary>
/// Initiates a chat with the AI, using the provided input and shell.
Expand Down
2 changes: 1 addition & 1 deletion shell/AIShell.Kernel/Command/RefreshCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ private void RefreshAction()
var shell = (Shell)Shell;
shell.ShowBanner();
shell.ShowLandingPage();
shell.ActiveAgent.Impl.RefreshChatAsync(Shell).GetAwaiter().GetResult();
shell.ActiveAgent.Impl.RefreshChatAsync(Shell, force: true).GetAwaiter().GetResult();
}
}
4 changes: 2 additions & 2 deletions shell/AIShell.Kernel/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ internal async Task RunREPLAsync()
if (_shouldRefresh)
{
_shouldRefresh = false;
await agent?.Impl.RefreshChatAsync(this);
await agent?.Impl.RefreshChatAsync(this, force: false);
}

if (Regenerate)
Expand Down Expand Up @@ -670,7 +670,7 @@ internal async Task RunOnceAsync(string prompt)

try
{
await _activeAgent.Impl.RefreshChatAsync(this);
await _activeAgent.Impl.RefreshChatAsync(this, force: false);
await _activeAgent.Impl.ChatAsync(prompt, this);
}
catch (OperationCanceledException)
Expand Down
2 changes: 1 addition & 1 deletion shell/agents/AIShell.Azure.Agent/AzCLI/AzCLIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void OnUserAction(UserActionPayload actionPayload)
});
}

public Task RefreshChatAsync(IShell shell)
public Task RefreshChatAsync(IShell shell, bool force)
{
// Reset the history so the subsequent chat can start fresh.
_chatService.ChatHistory.Clear();
Expand Down
2 changes: 1 addition & 1 deletion shell/agents/AIShell.Azure.Agent/AzPS/AzPSAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void OnUserAction(UserActionPayload actionPayload)
});
}

public Task RefreshChatAsync(IShell shell)
public Task RefreshChatAsync(IShell shell, bool force)
{
// Reset the history so the subsequent chat can start fresh.
_chatService.ChatHistory.Clear();
Expand Down
2 changes: 1 addition & 1 deletion shell/agents/AIShell.Interpreter.Agent/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Initialize(AgentConfig config)
}

/// <inheritdoc/>
public Task RefreshChatAsync(IShell shell)
public Task RefreshChatAsync(IShell shell, bool force)
{
// Reload the setting file if needed.
ReloadSettings();
Expand Down
2 changes: 1 addition & 1 deletion shell/agents/AIShell.Ollama.Agent/OllamaAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void OnUserAction(UserActionPayload actionPayload) {}
/// Refresh the current chat by starting a new chat session.
/// This method allows an agent to reset chat states, interact with user for authentication, print welcome message, and more.
/// </summary>
public Task RefreshChatAsync(IShell shell) => Task.CompletedTask;
public Task RefreshChatAsync(IShell shell, bool force) => Task.CompletedTask;

/// <summary>
/// Main chat function that takes the users input and passes it to the LLM and renders it.
Expand Down
2 changes: 1 addition & 1 deletion shell/agents/AIShell.OpenAI.Agent/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void Initialize(AgentConfig config)
public void OnUserAction(UserActionPayload actionPayload) {}

/// <inheritdoc/>
public Task RefreshChatAsync(IShell shell)
public Task RefreshChatAsync(IShell shell, bool force)
{
// Reload the setting file if needed.
ReloadSettings();
Expand Down
36 changes: 33 additions & 3 deletions shell/agents/Microsoft.Azure.Agent/AzureAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text;

using AIShell.Abstraction;
using Azure.Identity;
using Serilog;

namespace Microsoft.Azure.Agent;
Expand Down Expand Up @@ -115,10 +116,39 @@ public void Initialize(AgentConfig config)
public bool CanAcceptFeedback(UserAction action) => false;
public void OnUserAction(UserActionPayload actionPayload) {}

public async Task RefreshChatAsync(IShell shell)
public async Task RefreshChatAsync(IShell shell, bool force)
{
// Refresh the chat session.
await _chatSession.RefreshAsync(shell.Host, shell.CancellationToken);
IHost host = shell.Host;
CancellationToken cancellationToken = shell.CancellationToken;

try
{
string welcome = await host.RunWithSpinnerAsync(
status: "Initializing ...",
spinnerKind: SpinnerKind.Processing,
func: async context => await _chatSession.RefreshAsync(context, force, cancellationToken)
).ConfigureAwait(false);

if (!string.IsNullOrEmpty(welcome))
{
host.WriteLine(welcome);
}
}
catch (OperationCanceledException)
{
host.WriteErrorLine("Operation cancelled. Please run '/refresh' to start a new conversation.");
}
catch (CredentialUnavailableException)
{
host.WriteErrorLine($"Failed to start a chat session: Access token not available.");
host.WriteErrorLine($"The '{Name}' agent depends on the Azure CLI credential to acquire access token. Please run 'az login' from a command-line shell to setup account.");
}
catch (Exception e)
{
host.WriteErrorLine($"Failed to start a chat session: {e.Message}\n{e.StackTrace}")
.WriteErrorLine()
.WriteErrorLine("Please try '/refresh' to start a new conversation.");
}
}

public async Task<bool> ChatAsync(string input, IShell shell)
Expand Down
Loading