Skip to content

Commit 7aac61a

Browse files
committed
Update prompts
1 parent 345dc6f commit 7aac61a

File tree

14 files changed

+100
-39
lines changed

14 files changed

+100
-39
lines changed

samples/FizzBuzz.Generator.Basic/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
using Wolder.Core;
77
using Wolder.Core.Workspace;
88
using Wolder.CSharp;
9-
using Wolder.CSharp.Actions;
9+
using Wolder.CSharp.CodeActions;
10+
using Wolder.CSharp.ProjectActions;
1011

1112
var builder = Host.CreateApplicationBuilder();
1213
builder.Logging.AddConsole();

samples/FizzBuzz.Generator.OpenAI/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Wolder.Core;
22
using Wolder.CSharp;
3-
using Wolder.CSharp.Actions;
43
using Wolder.CSharp.OpenAI;
54
using Wolder.CSharp.OpenAI.Actions;
65
using Microsoft.Extensions.DependencyInjection;
@@ -9,6 +8,7 @@
98
using Wolder.CommandLine;
109
using Wolder.CommandLine.Actions;
1110
using Wolder.Core.Workspace;
11+
using Wolder.CSharp.CodeActions;
1212

1313
var builder = Host.CreateApplicationBuilder();
1414
builder.Logging.AddConsole();

samples/TodoList.Blazor.Generator/Program.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
using Wolder.CommandLine.Actions;
33
using Wolder.Core;
44
using Wolder.CSharp;
5-
using Wolder.CSharp.Actions;
65
using Wolder.CSharp.OpenAI;
76
using Wolder.CSharp.OpenAI.Actions;
87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.Hosting;
109
using Microsoft.Extensions.Logging;
1110
using Wolder.Core.Workspace;
11+
using Wolder.CSharp.CodeActions;
12+
using Wolder.CSharp.ProjectActions;
1213

1314
var builder = Host.CreateApplicationBuilder();
1415
builder.Logging.AddConsole();
@@ -27,6 +28,7 @@ await host.Services.GetRequiredService<GeneratorWorkspaceBuilder>()
2728
class GenerateTodoListApp(
2829
CommandLineActions commandLine,
2930
CSharpActions csharp,
31+
CSharpProjectActions cSharpProject,
3032
CSharpGenerator csharpGenerator) : IVoidAction
3133
{
3234
public async Task InvokeAsync()
@@ -71,7 +73,11 @@ Make sure the services are registered before the app container is built.
7173
"Services",
7274
"ITodoService",
7375
"""
74-
An interface for a service that provides CRUD actions for todo list items. Getting all items should return a list.
76+
An interface for a service that provides these actions for todo list items.
77+
- Add item
78+
- Remove item
79+
- Update item
80+
- Get all items: should return a list
7581
""")
7682
{
7783
ContextMemoryItems = [todoItem]

src/Wolder.CSharp.OpenAI/Actions/GenerateBlazorComponent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class GenerateBlazorComponent(
3030
"written to a `.razor` file. Write terse but helpful comments to explain the code and its structure. " +
3131
"Add any usings for items used from the context. Ensure all comments use razor style comments @* comment *@. " +
3232
"It is crucial that the `@rendermode InteractiveServer` directive is included at the top of an interactive component/page " +
33-
"so that it works correctly in an interactive server scenario.";
33+
"so that it works correctly in an interactive server scenario. Nullable references are enabled.";
3434

3535
public async Task<FileMemoryItem> InvokeAsync()
3636
{

src/Wolder.CSharp.OpenAI/Actions/GenerateClass.cs

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
using System.Text.RegularExpressions;
1+
using Microsoft.Extensions.Logging;
2+
using System.Text.RegularExpressions;
23
using Wolder.Core.Assistants;
34
using Wolder.Core.Files;
45
using Wolder.CSharp.Compilation;
5-
using Microsoft.Extensions.Logging;
6-
using Wolder.CommandLine;
7-
using Wolder.CommandLine.Actions;
86
using Wolder.Core.Workspace;
9-
using Wolder.CSharp.Actions;
107

118
namespace Wolder.CSharp.OpenAI.Actions;
129

@@ -25,6 +22,10 @@ public class GenerateClass(
2522
GenerateClassParameters parameters)
2623
: IAction<GenerateClassParameters, FileMemoryItem>
2724
{
25+
public const string CSharpPrompt =
26+
"You are a helpful assistant that writes C# code to complete any task specified by me. " +
27+
"Your output will be directly written to a file where it will be compiled as part of a " +
28+
"larger C# project. Nullable references are enabled.";
2829
public async Task<FileMemoryItem> InvokeAsync()
2930
{
3031
var (project, classNamespace, className, behaviorPrompt) = parameters;
@@ -36,12 +37,13 @@ public async Task<FileMemoryItem> InvokeAsync()
3637

3738
var tree = sourceFiles.GetDirectoryTree();
3839
var context = $$"""
40+
3941
Directory Tree:
4042
{{tree}}
4143
""";
4244
if (parameters.ContextMemoryItems.Any())
4345
{
44-
context = "\nThe items may also provide helpful context:\n" +
46+
context = "\nThese items may also provide helpful context:\n" +
4547
string.Join("\n", parameters.ContextMemoryItems
4648
.Select(i => $"File: {i.RelativePath}\n{i.Content}" ));
4749
}
@@ -50,12 +52,13 @@ public async Task<FileMemoryItem> InvokeAsync()
5052
? ""
5153
: $".{classNamespace}";
5254
var response = await assistant.CompletePromptAsync($"""
53-
You are a C# code generator. Output only C#, your output will be directly written to a `.cs` file.
54-
Write terse but helpful explanatory comments.
55+
{CSharpPrompt}
5556
{context}
5657
5758
Create a class named `{className}` with namespace `{project.BaseNamespace}{namespaceEnd}` with the following behavior:
5859
{behaviorPrompt}
60+
61+
Begin Output:
5962
""");
6063

6164
var classMemoryItem = await SanitizeAndWriteClassAsync(response);
@@ -88,14 +91,18 @@ Write terse but helpful explanatory comments.
8891
var diagnosticMessages = lastResult.Output.Errors;
8992
var messagesText = string.Join(Environment.NewLine, diagnosticMessages);
9093
var response = await assistant.CompletePromptAsync($"""
91-
You are a helpful assistant that writes C# code to complete any task specified by me. Your output will be directly written to a file where it will be compiled as part of a larger C# project.
94+
{CSharpPrompt}
9295
{context}
9396
94-
Given the following compilation diagnostic messages transform the following file to resolve the messages:
97+
Given the following errors:
9598
{messagesText}
9699
97-
File Content:
100+
Transform the following file to resolve the errors:
101+
```
98102
{lastFile.Content}
103+
```
104+
105+
Begin Output:
99106
""");
100107

101108
classMemoryItem = await SanitizeAndWriteClassAsync(response);

src/Wolder.CSharp.OpenAI/Actions/TransformClass.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public async Task<FileMemoryItem> InvokeAsync()
2222
var (projectRef, filePath, behaviorPrompt) = parameters;
2323
var content = await sourceFiles.ReadFileAsync(filePath);
2424
var response = await assistant.CompletePromptAsync($"""
25-
You are a C# code generator. Output only C#, your output will be directly written to a `.cs` file.
26-
Write terse but helpful comments. Any explanation must be written as C# comments.
25+
{GenerateClass.CSharpPrompt}
2726
2827
Using the code from this file:
2928
File: {filePath}
@@ -33,6 +32,8 @@ Write terse but helpful comments. Any explanation must be written as C# comments
3332
3433
Update the code with the following behavior:
3534
{behaviorPrompt}
35+
36+
Begin Output:
3637
""");
3738

3839
logger.LogInformation(response);

src/Wolder.CSharp/Actions/CreateProject.cs

-16
This file was deleted.

src/Wolder.CSharp/CSharpActions.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Wolder.Core.Workspace;
2-
using Wolder.CSharp.Actions;
2+
using Wolder.CSharp.CodeActions;
3+
using Wolder.CSharp.ProjectActions;
34

45
namespace Wolder.CSharp;
56

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Wolder.Core.Workspace;
2+
using Wolder.CSharp.ProjectActions;
3+
4+
namespace Wolder.CSharp;
5+
6+
[GenerateActionCall<SetNullability>]
7+
[GenerateActionCall<CompileProject>]
8+
public partial class CSharpProjectActions
9+
{
10+
}

src/Wolder.CSharp/Actions/CreateClass.cs renamed to src/Wolder.CSharp/CodeActions/CreateClass.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Wolder.Core.Files;
22
using Wolder.Core.Workspace;
33

4-
namespace Wolder.CSharp.Actions;
4+
namespace Wolder.CSharp.CodeActions;
55

66
public record CreateClassParameters(DotNetProjectReference Project, string ClassName, string ClassContent);
77

src/Wolder.CSharp/Actions/CreateSdkGlobal.cs renamed to src/Wolder.CSharp/CodeActions/CreateSdkGlobal.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Wolder.Core.Files;
22
using Wolder.Core.Workspace;
33

4-
namespace Wolder.CSharp.Actions;
4+
namespace Wolder.CSharp.CodeActions;
55

66
public enum DotNetSdkVersion
77
{

src/Wolder.CSharp/Actions/CompileProject.cs renamed to src/Wolder.CSharp/ProjectActions/CompileProject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Wolder.Core.Workspace;
66
using Wolder.CSharp.Compilation;
77

8-
namespace Wolder.CSharp.Actions;
8+
namespace Wolder.CSharp.ProjectActions;
99

1010
public record CompileProjectParameters(DotNetProjectReference Project);
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Build.Construction;
2+
using Wolder.Core.Files;
3+
using Wolder.Core.Workspace;
4+
5+
namespace Wolder.CSharp.ProjectActions;
6+
7+
public enum ProjectNullability
8+
{
9+
Enable,
10+
Disable
11+
}
12+
13+
public record SetNullabilityParameters(DotNetProjectReference Project, ProjectNullability Nullability);
14+
15+
public class SetNullability(
16+
ISourceFiles sourceFiles,
17+
SetNullabilityParameters parameters)
18+
: IVoidAction<SetNullabilityParameters>
19+
{
20+
public Task InvokeAsync()
21+
{
22+
var csprojPath = sourceFiles.GetAbsolutePath(parameters.Project.RelativeFilePath);
23+
var projectRoot = ProjectRootElement.Open(csprojPath);
24+
25+
var propertyGroup = projectRoot.PropertyGroups
26+
.FirstOrDefault(x => x.Properties.Any(p => p.Name == "Nullable"))
27+
?? projectRoot.PropertyGroups
28+
.FirstOrDefault(x => x.Properties.Any(p => p.Name == "TargetFramework"));
29+
30+
var nullability = parameters.Nullability switch
31+
{
32+
ProjectNullability.Enable => "enable",
33+
ProjectNullability.Disable => "disable",
34+
_ => throw new InvalidOperationException("Unknown nullability")
35+
};
36+
37+
if (propertyGroup != null)
38+
{
39+
propertyGroup.AddProperty("Nullable", nullability);
40+
}
41+
else
42+
{
43+
propertyGroup = projectRoot.AddPropertyGroup();
44+
propertyGroup.AddProperty("Nullable", nullability);
45+
}
46+
47+
projectRoot.Save();
48+
49+
return Task.CompletedTask;
50+
}
51+
}

src/Wolder.CSharp/ServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Wolder.Core;
2-
using Wolder.CSharp.Actions;
32
using Wolder.CSharp.Compilation;
43
using Microsoft.Extensions.DependencyInjection;
54
using Wolder.Core.Workspace;
@@ -12,6 +11,7 @@ public static GeneratorWorkspaceBuilder AddCSharpActions(this GeneratorWorkspace
1211
{
1312
builder.Services.AddScoped<DotNetProjectFactory>();
1413
builder.AddActions<CSharpActions>();
14+
builder.AddActions<CSharpProjectActions>();
1515

1616
return builder;
1717
}

0 commit comments

Comments
 (0)