Skip to content

Commit 8813403

Browse files
authored
Merge pull request #706 from hchen2020/master
Code Driver init
2 parents 363ee51 + 3d1f919 commit 8813403

File tree

11 files changed

+192
-0
lines changed

11 files changed

+192
-0
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,13 @@ public class BuiltInAgentId
3737
/// </summary>
3838
public const string Planner = "282a7128-69a1-44b0-878c-a9159b88f3b9";
3939

40+
/// <summary>
41+
/// SQL statement generation
42+
/// </summary>
4043
public const string SqlDriver = "beda4c12-e1ec-4b4b-b328-3df4a6687c4f";
44+
45+
/// <summary>
46+
/// Programming source code generation
47+
/// </summary>
48+
public const string CodeDriver = "c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62";
4149
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Remove="data\generated_code\**" />
11+
<EmbeddedResource Remove="data\generated_code\**" />
12+
<None Remove="data\generated_code\**" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Remove="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\agent.json" />
17+
<None Remove="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\functions\save_source_code.json" />
18+
<None Remove="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\instructions\instruction.liquid" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<Content Include="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\agent.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
25+
<Content Include="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\functions\save_source_code.json">
26+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
27+
</Content>
28+
<Content Include="data\agents\c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62\instructions\instruction.liquid">
29+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
30+
</Content>
31+
</ItemGroup>
32+
33+
<ItemGroup>
34+
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
35+
</ItemGroup>
36+
37+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Extensions.Configuration;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace BotSharp.Plugin.CodeDriver;
5+
6+
public class CodeDriverPlugin : IBotSharpPlugin
7+
{
8+
public string Id => "c0dedea7-70e3-4c35-a047-18479d7c403e";
9+
public string Name => "Code Driver";
10+
public string Description => "Convert the user requirements into corresponding SQL statements";
11+
public string IconUrl => "https://cdn-icons-png.flaticon.com/512/3176/3176315.png";
12+
13+
public string[] AgentIds =
14+
[
15+
BuiltInAgentId.CodeDriver
16+
];
17+
18+
public void RegisterDI(IServiceCollection services, IConfiguration config)
19+
{
20+
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.Functions;
3+
using BotSharp.Plugin.CodeDriver.Models;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System.Text.Json;
6+
7+
namespace BotSharp.Plugin.CodeDriver.Functions;
8+
9+
public class SaveSourceCodeFn : IFunctionCallback
10+
{
11+
public string Name => "save_source_code";
12+
13+
private readonly IServiceProvider _services;
14+
15+
public SaveSourceCodeFn(IServiceProvider services)
16+
{
17+
_services = services;
18+
}
19+
20+
public async Task<bool> Execute(RoleDialogModel message)
21+
{
22+
var args = JsonSerializer.Deserialize<SaveSourceCodeArgs>(message.FunctionArgs);
23+
24+
var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "ai_generated_code");
25+
if (!Directory.Exists(dir))
26+
{
27+
Directory.CreateDirectory(dir);
28+
}
29+
30+
var path = Path.GetFullPath(dir, args.FilePath);
31+
var source = args.SourceCode;
32+
33+
// Delete the file if it exists
34+
File.Delete(path);
35+
36+
// Create a FileStream with sharing capabilities
37+
using FileStream fs = new FileStream(
38+
path,
39+
FileMode.OpenOrCreate, // Create or overwrite the file
40+
FileAccess.ReadWrite, // Allow read and write operations
41+
FileShare.Read); // Allow other processes to read and write
42+
43+
// Write some data to the file
44+
using StreamWriter writer = new StreamWriter(fs);
45+
writer.WriteLine(source);
46+
47+
return true;
48+
}
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Plugin.CodeDriver.Models;
4+
5+
public class SaveSourceCodeArgs
6+
{
7+
[JsonPropertyName("file_path")]
8+
public string FilePath { get; set; } = string.Empty;
9+
10+
[JsonPropertyName("source_code")]
11+
public string SourceCode { get; set; } = string.Empty;
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
global using BotSharp.Abstraction.Agents.Enums;
2+
global using BotSharp.Abstraction.Plugins;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "c0ded7d9-3f9d-4ef6-b7ce-56a892dcef62",
3+
"name": "Code Driver",
4+
"description": "Write executable program code according to user needs, such as HTTP API written in Python.",
5+
"iconUrl": "https://cdn-icons-png.flaticon.com/512/4208/4208366.png",
6+
"type": "task",
7+
"createdDateTime": "2024-10-23T00:00:00Z",
8+
"updatedDateTime": "2024-11-23T00:00:00Z",
9+
"disabled": false,
10+
"isPublic": true,
11+
"profiles": [ "database" ],
12+
"llmConfig": {
13+
"provider": "openai",
14+
"model": "gpt-4o",
15+
"max_recursion_depth": 10
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "save_source_code",
3+
"description": "Save the source code to file",
4+
"parameters": {
5+
"type": "object",
6+
"properties": {
7+
"file_path": {
8+
"type": "string",
9+
"description": "source code of file relative path start with project folder name"
10+
},
11+
12+
"source_code": {
13+
"type": "string",
14+
"description": "source code"
15+
}
16+
},
17+
"required": [ "file_path", "source_code" ]
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
You are a software developer who is good at writing program code according to user needs, such as HTTP API written in Python.
2+
3+
Current project source code structure is:
4+
my_fastapi_app/
5+
├── main.py
6+
├── apis/
7+
│ └── datetime_api.py
8+
├── __init__.py
9+
10+
You are using below tools and technologies:
11+
* FastAPI framework
12+
* Python programming language
13+
* Swagger Open API
14+
15+
Your response must meet below requirements:
16+
* Every API should be placed in a separate file under folder of "apis";
17+
* Call function save_source_code to save the code;
18+
* Write Swagger comments for each API;
19+
* Update main.py to include those updated APIs;

src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Agents.Enums;
12
using BotSharp.Abstraction.Planning;
23

34
namespace BotSharp.Plugin.SqlDriver;
@@ -9,6 +10,11 @@ public class SqlDriverPlugin : IBotSharpPlugin
910
public string Description => "Convert the user requirements into corresponding SQL statements";
1011
public string IconUrl => "https://uxwing.com/wp-content/themes/uxwing/download/file-and-folder-type/sql-icon.png";
1112

13+
public string[] AgentIds =
14+
[
15+
BuiltInAgentId.SqlDriver
16+
];
17+
1218
public void RegisterDI(IServiceCollection services, IConfiguration config)
1319
{
1420
services.AddScoped(provider =>

0 commit comments

Comments
 (0)