forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
133 lines (110 loc) · 5.83 KB
/
Copy pathProgram.cs
File metadata and controls
133 lines (110 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.AGUI;
using Microsoft.Agents.AI.AGUI.Shared;
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Options;
using OpenAI;
using ProverbsAgent.Models;
using ProverbsAgent.Services;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Add(CanvasAgentSerializerContext.Default);
});
// Register AGUI services
builder.Services.AddAGUI();
var app = builder.Build();
// Create the agent factory and map the AG-UI agent endpoint
var jsonOptions = app.Services.GetRequiredService<IOptions<JsonOptions>>();
var agentFactory = new CanvasAgentFactory(builder.Configuration, jsonOptions.Value.SerializerOptions);
app.MapAGUI("/", agentFactory.CreateCanvasAgent());
app.Run();
// =================
// Agent Factory
// =================
public class CanvasAgentFactory
{
private readonly IConfiguration _configuration;
private readonly AgentState _state;
private readonly OpenAIClient _openAiClient;
private readonly System.Text.Json.JsonSerializerOptions _jsonSerializerOptions;
public CanvasAgentFactory(IConfiguration configuration, System.Text.Json.JsonSerializerOptions jsonSerializerOptions)
{
_configuration = configuration;
_jsonSerializerOptions = jsonSerializerOptions;
// Initialize with a default board matching frontend initialState
_state = new AgentState
{
Boards = new List<Board>
{
new Board
{
Id = "board001",
Name = "My First Board",
Tasks = new List<KanbanTask>()
}
},
ActiveBoardId = "board001",
LastAction = ""
};
var openAiKey = _configuration["OpenAIKey"]
?? throw new InvalidOperationException("OpenAIKey not found in configuration. Run: dotnet user-secrets set OpenAIKey \"YOUR_OPENAI_API_KEY\"");
_openAiClient = new OpenAIClient(openAiKey);
}
public AIAgent CreateCanvasAgent()
{
// Create Kanban service with shared state
var kanbanService = new KanbanService(_state);
var chatClient = _openAiClient.GetChatClient("gpt-4o-mini").AsIChatClient();
var chatClientAgent = chatClient.CreateAIAgent(
name: "my_agent",
instructions: @"A helpful assistant managing Kanban boards and tasks.
You have tools to manage boards and tasks:
- State: get_state (call this after modifications to see current state)
- Board tools: create_board, delete_board, rename_board, switch_board
- Task tools: create_task, update_task_field, add_task_tag, remove_task_tag, move_task_to_status, delete_task
Each task has title, subtitle, description, tags[], and status.
Tasks flow through 4 statuses: new → in_progress → review → completed.
IMPORTANT: After creating or modifying boards/tasks, call get_state to retrieve the current state.",
tools: [
AIFunctionFactory.Create(kanbanService.GetState, new AIFunctionFactoryOptions { Name = "get_state", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.CreateBoard, new AIFunctionFactoryOptions { Name = "create_board", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.DeleteBoard, new AIFunctionFactoryOptions { Name = "delete_board", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.SwitchBoard, new AIFunctionFactoryOptions { Name = "switch_board", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.RenameBoard, new AIFunctionFactoryOptions { Name = "rename_board", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.CreateTask, new AIFunctionFactoryOptions { Name = "create_task", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.UpdateTaskField, new AIFunctionFactoryOptions { Name = "update_task_field", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.AddTaskTag, new AIFunctionFactoryOptions { Name = "add_task_tag", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.RemoveTaskTag, new AIFunctionFactoryOptions { Name = "remove_task_tag", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.MoveTaskToStatus, new AIFunctionFactoryOptions { Name = "move_task_to_status", SerializerOptions = _jsonSerializerOptions }),
AIFunctionFactory.Create(kanbanService.DeleteTask, new AIFunctionFactoryOptions { Name = "delete_task", SerializerOptions = _jsonSerializerOptions })
]
);
// Wrap with SharedStateAgent for AG-UI state synchronization
return new SharedStateAgent(chatClientAgent, _jsonSerializerOptions, _state);
}
}
public partial class Program { }
// =================
// State Snapshot
// =================
public class AgentStateSnapshot
{
[JsonPropertyName("boards")]
public List<Board> Boards { get; set; } = new();
[JsonPropertyName("activeBoardId")]
public string ActiveBoardId { get; set; } = string.Empty;
[JsonPropertyName("lastAction")]
public string? LastAction { get; set; }
}
// =================
// Serializer Context
// =================
[JsonSerializable(typeof(AgentStateSnapshot))]
[JsonSerializable(typeof(Board))]
[JsonSerializable(typeof(KanbanTask))]
[JsonSerializable(typeof(AgentState))]
internal sealed partial class CanvasAgentSerializerContext : JsonSerializerContext;