Skip to content

Commit 484863e

Browse files
committed
Refactor and clean up the code
1 parent 7ce8052 commit 484863e

File tree

2 files changed

+78
-54
lines changed

2 files changed

+78
-54
lines changed

shell/agents/AIShell.PhiSilica.Agent/AIShell.PhiSilica.Agent.csproj

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFramework>net8.0-windows10.0.26100.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
8-
<Platforms>AnyCPU</Platforms>
9-
<WindowsPackageType>None</WindowsPackageType>
10-
<EnableDynamicLoading>true</EnableDynamicLoading>
11-
<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
12-
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
13-
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
14-
<EnableMsixTooling>true</EnableMsixTooling>
15-
3+
<OutputType>Library</OutputType>
4+
<TargetFramework>net8.0-windows10.0.26100.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<NoWarn>CS8305</NoWarn>
7+
<Platforms>AnyCPU</Platforms>
8+
<WindowsPackageType>None</WindowsPackageType>
9+
<EnableDynamicLoading>true</EnableDynamicLoading>
10+
<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
11+
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
12+
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
13+
<EnableMsixTooling>true</EnableMsixTooling>
1614
</PropertyGroup>
1715

18-
1916
<ItemGroup>
2017
<ProjectReference Include="..\..\AIShell.Abstraction\AIShell.Abstraction.csproj">
2118
<!-- Disable copying AIShell.Abstraction.dll to output folder -->
@@ -25,7 +22,6 @@
2522
</ProjectReference>
2623
</ItemGroup>
2724

28-
2925
<ItemGroup>
3026
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.8.250410001-experimental1" />
3127
</ItemGroup>
Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,97 @@
1-
2-
using Microsoft.Windows.AI.Generative;
3-
41
using AIShell.Abstraction;
52
using Microsoft.Windows.AI;
3+
using Microsoft.Windows.AI.Generative;
64

7-
namespace AIShell.PhiSilica.Agent
5+
namespace AIShell.PhiSilica.Agent;
6+
7+
public sealed partial class PhiSilicaAgent : ILLMAgent
88
{
9-
public class PhiSilicaAgent : ILLMAgent
10-
{
11-
public string Name => "PhiSilica";
9+
private readonly Task _initTask;
10+
private LanguageModel _model;
1211

13-
public string Description => "This is the Phi Silica agent, an offline local agent on Copilot+ PCs";
12+
public string Name => "PhiSilica";
13+
public string Description => "This is the Phi Silica agent, an offline local agent on Copilot+ PCs";
14+
public string SettingFile => null;
1415

15-
public string SettingFile => null;
16+
public IEnumerable<CommandBase> GetCommands() => null;
17+
public bool CanAcceptFeedback(UserAction action) => false;
18+
public Task RefreshChatAsync(IShell shell, bool force) => Task.CompletedTask;
19+
public void OnUserAction(UserActionPayload actionPayload) { }
20+
public void Initialize(AgentConfig config) { }
21+
public void Dispose() { }
1622

17-
public bool CanAcceptFeedback(UserAction action) => false;
23+
public PhiSilicaAgent()
24+
{
25+
// Start the initialization for AI feature and model on a background thread.
26+
_initTask = Task.Run(InitFeatureAndModelAsync);
27+
}
1828

29+
private async Task InitFeatureAndModelAsync()
30+
{
31+
AIFeatureReadyState state = LanguageModel.GetReadyState();
32+
if (state is AIFeatureReadyState.NotSupportedOnCurrentSystem)
33+
{
34+
throw new PlatformNotSupportedException("The Phi Silica feature is not supported on current system.");
35+
}
1936

20-
public async Task<bool> ChatAsync(string input, IShell shell)
37+
if (state is AIFeatureReadyState.DisabledByUser)
2138
{
22-
IHost host = shell.Host;
23-
if (LanguageModel.GetReadyState() == AIFeatureReadyState.EnsureNeeded)
24-
{
25-
var op = await LanguageModel.EnsureReadyAsync();
39+
throw new PlatformNotSupportedException("The Phi Silica feature is currently disabled.");
40+
}
2641

42+
if (state is AIFeatureReadyState.EnsureNeeded)
43+
{
44+
// Initialize the WinRT runtime.
45+
AIFeatureReadyResult result = await LanguageModel.EnsureReadyAsync();
46+
// Do not proceed if it failed to get the feature ready.
47+
if (result.Status is not AIFeatureReadyResultState.Success)
48+
{
49+
throw new InvalidOperationException(result.ErrorDisplayText, result.Error);
2750
}
51+
}
2852

29-
var languageModel = await LanguageModel.CreateAsync();
53+
_model = await LanguageModel.CreateAsync();
54+
}
3055

31-
var results = await languageModel.GenerateResponseAsync(input);
56+
public async Task<bool> ChatAsync(string input, IShell shell)
57+
{
58+
IHost host = shell.Host;
3259

33-
if (results != null && !string.IsNullOrEmpty(results.Text))
60+
try
61+
{
62+
// Wait for the init task to finish. Once it's finished, calling this again is a non-op.
63+
await _initTask;
64+
}
65+
catch (Exception e)
66+
{
67+
host.WriteErrorLine(e.Message);
68+
if (e is InvalidOperationException && e.InnerException is not null)
3469
{
35-
host.RenderFullResponse(results.Text);
70+
host.WriteErrorLine(e.InnerException.StackTrace);
3671
}
37-
else
72+
else if (e is not PlatformNotSupportedException)
3873
{
39-
host.WriteErrorLine("No response received from the language model.");
74+
// Show stack trace for non-PNS exception.
75+
host.WriteErrorLine(e.StackTrace);
4076
}
41-
//host.RenderFullResponse("Goodbye World");
4277

43-
return true;
78+
return false;
4479
}
4580

46-
public void Dispose()
47-
{
48-
49-
}
81+
var result = await host.RunWithSpinnerAsync(
82+
status: "Thinking ...",
83+
func: async () => await _model.GenerateResponseAsync(input)
84+
);
5085

51-
public IEnumerable<CommandBase> GetCommands() => null;
52-
53-
public void Initialize(AgentConfig config)
86+
if (result is not null && !string.IsNullOrEmpty(result.Text))
5487
{
55-
88+
host.RenderFullResponse(result.Text);
5689
}
57-
58-
public void OnUserAction(UserActionPayload actionPayload)
90+
else
5991
{
60-
92+
host.WriteErrorLine("No response received from the language model.");
6193
}
6294

63-
public Task RefreshChatAsync(IShell shell, bool force)
64-
{
65-
return Task.CompletedTask;
66-
}
95+
return true;
6796
}
68-
6997
}

0 commit comments

Comments
 (0)