Skip to content

Commit 7507271

Browse files
authored
Add opinionated lib for (mostly?) single-file agents (#42)
* Add opinionated lib for (mostly?) single-file agents Weaving (you know, the actor behind the agent? ;-)) provides an opinionated library and default set of package dependencies, default imports and helpers to make creating "dotnet run" file-based AI agents a breeze. The sample showcases how trivial the combination of M.E.AI, Devlooped.Extension.AI and Weaving make the authoring of this single file AI agents.
1 parent 83545da commit 7507271

File tree

13 files changed

+258
-0
lines changed

13 files changed

+258
-0
lines changed

Extensions.AI.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AI.Tests", "src\AI.Tests\AI
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-meai", "src\dotnet-meai\dotnet-meai.csproj", "{58FC11CF-7D61-48B9-B95F-0EAA889D6DED}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weaving", "src\Weaving\Weaving.csproj", "{0DA390D2-2953-4CB6-8939-02E142B11733}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "src\Samples\Samples.csproj", "{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -55,8 +59,35 @@ Global
5559
{58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x64.Build.0 = Release|Any CPU
5660
{58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x86.ActiveCfg = Release|Any CPU
5761
{58FC11CF-7D61-48B9-B95F-0EAA889D6DED}.Release|x86.Build.0 = Release|Any CPU
62+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x64.ActiveCfg = Debug|Any CPU
65+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x64.Build.0 = Debug|Any CPU
66+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x86.ActiveCfg = Debug|Any CPU
67+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Debug|x86.Build.0 = Debug|Any CPU
68+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x64.ActiveCfg = Release|Any CPU
71+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x64.Build.0 = Release|Any CPU
72+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x86.ActiveCfg = Release|Any CPU
73+
{0DA390D2-2953-4CB6-8939-02E142B11733}.Release|x86.Build.0 = Release|Any CPU
74+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x64.ActiveCfg = Debug|Any CPU
77+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x64.Build.0 = Debug|Any CPU
78+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x86.ActiveCfg = Debug|Any CPU
79+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Debug|x86.Build.0 = Debug|Any CPU
80+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
81+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|Any CPU.Build.0 = Release|Any CPU
82+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x64.ActiveCfg = Release|Any CPU
83+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x64.Build.0 = Release|Any CPU
84+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x86.ActiveCfg = Release|Any CPU
85+
{4B78F0E3-E03B-4283-AB0B-B1D76CAEF1BC}.Release|x86.Build.0 = Release|Any CPU
5886
EndGlobalSection
5987
GlobalSection(SolutionProperties) = preSolution
6088
HideSolutionNode = FALSE
6189
EndGlobalSection
90+
GlobalSection(ExtensibilityGlobals) = postSolution
91+
SolutionGuid = {CCA59B79-037D-4847-871D-385CD5E8BB6F}
92+
EndGlobalSection
6293
EndGlobal

src/Samples/Program.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Sample X.AI client usage with .NET
2+
var messages = new Chat()
3+
{
4+
{ "system", "You are a highly intelligent AI assistant." },
5+
{ "user", "What is 101*3?" },
6+
};
7+
8+
var grok = new GrokClient(Env.Get("XAI_API_KEY")!);
9+
10+
var options = new GrokChatOptions
11+
{
12+
ModelId = "grok-3-mini", // or "grok-3-mini-fast"
13+
Temperature = 0.7f,
14+
ReasoningEffort = ReasoningEffort.High, // or GrokReasoningEffort.Low
15+
Search = GrokSearch.Auto, // or GrokSearch.On or GrokSearch.Off
16+
};
17+
18+
var response = await grok.GetResponseAsync(messages, options);
19+
20+
AnsiConsole.MarkupLine($":robot: {response.Text}");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Samples": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"DOTNET_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}

src/Samples/Samples.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<LangVersion>Preview</LangVersion>
7+
<ImplicitUsings>true</ImplicitUsings>
8+
</PropertyGroup>
9+
10+
<Import Project="..\Weaving\Weaving.props" />
11+
12+
<ItemGroup>
13+
<PackageReference Include="Azure.Core" Version="1.44.1" />
14+
<PackageReference Include="DotNetConfig.Configuration" Version="1.2.0" />
15+
<PackageReference Include="System.ClientModel" Version="1.4.2" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\Weaving\Weaving.csproj" />
20+
</ItemGroup>
21+
22+
<Import Project="..\Weaving\Weaving.targets" />
23+
24+
</Project>

src/Samples/appsettings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"System": "Warning",
7+
"Azure": "Debug",
8+
"Azure.Core": "Debug",
9+
"Azure.AI.OpenAI": "Debug",
10+
"Devlooped.Extensions.AI": "Debug"
11+
}
12+
}
13+
}

src/Weaving/ConsoleInitializer.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
namespace Weaving;
6+
7+
class ConsoleInitializer
8+
{
9+
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
10+
[ModuleInitializer]
11+
#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
12+
public static void Init()
13+
{
14+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
15+
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
16+
}
17+
}

src/Weaving/Env.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace Weaving;
6+
7+
/// <summary>
8+
/// Allows retrieving configuration settings for the app environment using the
9+
/// default configuration system provided by <see cref="Host.CreateApplicationBuilder()"/>
10+
/// </summary>
11+
public static class Env
12+
{
13+
static readonly IConfigurationManager configuration =
14+
Host.CreateApplicationBuilder(Environment.GetCommandLineArgs()).Configuration;
15+
16+
/// <summary>
17+
/// Gets a (possibly null) configuration value for the given key.
18+
/// </summary>
19+
/// <param name="key">The key to retrieve, following .NET configuration system conventions.</param>
20+
public static string? Get(string key) => configuration[key];
21+
22+
/// <summary>
23+
/// Gets a configuration value for the given key, returning a default value if the key is not found.
24+
/// </summary>
25+
/// <param name="key">The key to retrieve, following .NET configuration system conventions.</param>
26+
/// <param name="defaultValue">Default value to return if the key is not found.</param>
27+
/// <returns>Never returns null if <paramref name="defaultValue"/> is not <see langword="null"/>.</returns>
28+
[return: NotNullIfNotNull(nameof(defaultValue))]
29+
public static string? Get(string key, string defaultValue) => configuration[key] ?? defaultValue;
30+
}

src/Weaving/HostExtensions.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.Extensions.AI;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
6+
namespace Weaving;
7+
8+
/// <summary>
9+
/// Extensions for configuring the host application builder and accessing services.
10+
/// </summary>
11+
public static class HostExtensions
12+
{
13+
extension<TBuilder>(TBuilder builder) where TBuilder : IHostApplicationBuilder
14+
{
15+
/// <summary>
16+
/// Configures the host configuration.
17+
/// </summary>
18+
public TBuilder Configure(Action<IConfigurationManager> configure)
19+
{
20+
configure?.Invoke(builder.Configuration);
21+
return builder;
22+
}
23+
24+
/// <summary>
25+
/// Configures services in the host.
26+
/// </summary>
27+
public TBuilder ConfigureServices(Action<IServiceCollection> configure)
28+
{
29+
configure?.Invoke(builder.Services);
30+
return builder;
31+
}
32+
}
33+
34+
extension(IHost app)
35+
{
36+
/// <summary>
37+
/// Gest the host app configuration;
38+
/// </summary>
39+
public IConfiguration Configuration => app.Services.GetRequiredService<IConfiguration>();
40+
41+
/// <summary>
42+
/// Gets the default AI client for the app.
43+
/// </summary>
44+
public IChatClient ChatClient => app.Services.GetRequiredService<IChatClient>();
45+
}
46+
}

src/Weaving/Visibility.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
namespace System
2+
{
3+
public partial class Throw { }
4+
}

src/Weaving/Weaving.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<LangVersion>Preview</LangVersion>
7+
<PackageId>Weaving</PackageId>
8+
<RootNamespace>Weaving</RootNamespace>
9+
<Description>Run AI-powered C# files with the power of Microsoft.Extensions.AI and Devlooped.Extensions.AI</Description>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\AI\AI.csproj" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="Weaving.props" PackFolder="build" />
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)