Skip to content

Commit 0a475db

Browse files
committed
Enhance agent management with editing and deletion confirmation features
1 parent 5241fa9 commit 0a475db

File tree

4 files changed

+175
-46
lines changed

4 files changed

+175
-46
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
4-
<Version>0.1.3.0</Version>
4+
<Version>0.1.4.0</Version>
55
<AssemblyVersion>$(Version)</AssemblyVersion>
66
<FileVersion>$(Version)</FileVersion>
77
<InformationalVersion>$(Version)</InformationalVersion>

SemanticCode/Pages/AgentsManagementView.axaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@
6363
Content="刷新"
6464
Command="{Binding RefreshCommand}"
6565
Margin="0,0,8,0"/>
66-
67-
<Button Grid.Column="2"
68-
Content="添加Agent"
69-
Command="{Binding AddAgentCommand}"/>
7066
</Grid>
7167

7268
<ListBox ItemsSource="{Binding Agents}"
@@ -76,6 +72,17 @@
7672
<ListBox.ItemTemplate>
7773
<DataTemplate x:DataType="models:AgentModel">
7874
<Grid Margin="0,8,0,8">
75+
<Grid.ContextMenu>
76+
<ContextMenu>
77+
<MenuItem Header="用编辑器打开"
78+
79+
Command="{Binding $parent[UserControl].DataContext.EditAgentInEditorCommand}"
80+
CommandParameter="{Binding}"/>
81+
<MenuItem Header="删除"
82+
Command="{Binding $parent[UserControl].DataContext.DeleteAgentCommand}"
83+
CommandParameter="{Binding}"/>
84+
</ContextMenu>
85+
</Grid.ContextMenu>
7986
<Grid.RowDefinitions>
8087
<RowDefinition Height="Auto"/>
8188
<RowDefinition Height="Auto"/>
@@ -104,7 +111,8 @@
104111
CornerRadius="8"
105112
Padding="16"
106113
Margin="16,0,0,0">
107-
<StackPanel Spacing="16">
114+
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
115+
<StackPanel Spacing="16">
108116
<TextBlock Text="Agent详情"
109117
FontSize="18"
110118
FontWeight="SemiBold"/>
@@ -225,6 +233,7 @@
225233
VerticalAlignment="Center"
226234
IsVisible="{Binding SelectedAgent, Converter={x:Static ObjectConverters.IsNull}}"/>
227235
</StackPanel>
236+
</ScrollViewer>
228237
</Border>
229238
</Grid>
230239
</Grid>

SemanticCode/Services/AgentFileParser.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Text.RegularExpressions;
56

67
namespace SemanticCode.Services;
@@ -25,22 +26,24 @@ public class AgentInfo
2526
try
2627
{
2728
var content = File.ReadAllText(filePath);
28-
var frontMatter = ExtractFrontMatter(content);
29+
var parsedData = ExtractFrontMatter(content);
2930

30-
if (!frontMatter.ContainsKey("name"))
31+
if (!parsedData.ContainsKey("name"))
3132
{
3233
return null;
3334
}
3435

3536
var agentInfo = new AgentInfo
3637
{
37-
Name = frontMatter["name"],
38-
Description = frontMatter.ContainsKey("description") ? frontMatter["description"] : string.Empty,
39-
Color = frontMatter.ContainsKey("color") ? frontMatter["color"] : "default",
38+
Name = parsedData["name"],
39+
Description = parsedData.ContainsKey("description") ? parsedData["description"] : string.Empty,
40+
Color = parsedData.ContainsKey("color") ? parsedData["color"] : "default",
4041
FileName = Path.GetFileName(filePath),
4142
FilePath = filePath,
4243
Content = content,
43-
FrontMatter = frontMatter
44+
PreContent = parsedData.ContainsKey("_precontent") ? parsedData["_precontent"] : string.Empty,
45+
MainContent = parsedData.ContainsKey("_maincontent") ? parsedData["_maincontent"] : string.Empty,
46+
FrontMatter = parsedData.Where(kvp => !kvp.Key.StartsWith("_")).ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
4447
};
4548

4649
return agentInfo;
@@ -144,28 +147,39 @@ public bool SaveAgentFile(AgentInfo agentInfo)
144147
{
145148
try
146149
{
147-
var frontMatter = new List<string>
150+
var contentParts = new List<string>();
151+
152+
// 添加前置内容(如果有)
153+
if (!string.IsNullOrEmpty(agentInfo.PreContent))
148154
{
149-
"---",
150-
$"name: {agentInfo.Name}",
151-
$"description: {agentInfo.Description}",
152-
$"color: {agentInfo.Color}"
153-
};
155+
contentParts.Add(agentInfo.PreContent);
156+
}
157+
158+
// 添加 front matter
159+
contentParts.Add("---");
160+
contentParts.Add($"name: {agentInfo.Name}");
161+
contentParts.Add($"description: {agentInfo.Description}");
162+
contentParts.Add($"color: {agentInfo.Color}");
154163

155164
// 添加其他 front matter 属性
156165
foreach (var kvp in agentInfo.FrontMatter)
157166
{
158167
if (kvp.Key != "name" && kvp.Key != "description" && kvp.Key != "color")
159168
{
160-
frontMatter.Add($"{kvp.Key}: {kvp.Value}");
169+
contentParts.Add($"{kvp.Key}: {kvp.Value}");
161170
}
162171
}
163172

164-
frontMatter.Add("---");
165-
frontMatter.Add("");
166-
frontMatter.Add(agentInfo.Content);
173+
contentParts.Add("---");
174+
175+
// 添加主要内容(如果有)
176+
if (!string.IsNullOrEmpty(agentInfo.MainContent))
177+
{
178+
contentParts.Add("");
179+
contentParts.Add(agentInfo.MainContent);
180+
}
167181

168-
File.WriteAllText(agentInfo.FilePath, string.Join("\n", frontMatter));
182+
File.WriteAllText(agentInfo.FilePath, string.Join("\n", contentParts));
169183
return true;
170184
}
171185
catch (Exception ex)

SemanticCode/ViewModels/AgentsManagementViewModel.cs

Lines changed: 129 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
using System;
22
using System.Collections.ObjectModel;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Reactive;
67
using System.Threading.Tasks;
8+
using Avalonia;
9+
using Avalonia.Controls;
10+
using Avalonia.Controls.ApplicationLifetimes;
11+
using Avalonia.Layout;
12+
using Avalonia.Media;
713
using ReactiveUI;
814
using SemanticCode.Models;
915
using SemanticCode.Services;
@@ -28,6 +34,7 @@ public AgentModel? SelectedAgent
2834

2935
public ReactiveCommand<Unit, Unit> AddAgentCommand { get; }
3036
public ReactiveCommand<AgentModel, Unit> EditAgentCommand { get; }
37+
public ReactiveCommand<AgentModel, Unit> EditAgentInEditorCommand { get; }
3138
public ReactiveCommand<AgentModel, Unit> DeleteAgentCommand { get; }
3239
public ReactiveCommand<Unit, Unit> RefreshCommand { get; }
3340

@@ -37,6 +44,7 @@ public AgentsManagementViewModel()
3744

3845
AddAgentCommand = ReactiveCommand.Create(AddAgent);
3946
EditAgentCommand = ReactiveCommand.Create<AgentModel>(EditAgent);
47+
EditAgentInEditorCommand = ReactiveCommand.Create<AgentModel>(EditAgentInEditor);
4048
DeleteAgentCommand = ReactiveCommand.Create<AgentModel>(DeleteAgent);
4149
RefreshCommand = ReactiveCommand.Create(LoadAgents);
4250

@@ -115,10 +123,37 @@ private void EditAgent(AgentModel agent)
115123
}
116124
}
117125

118-
private void DeleteAgent(AgentModel agent)
126+
private void EditAgentInEditor(AgentModel agent)
127+
{
128+
if (agent != null && !string.IsNullOrEmpty(agent.FilePath) && File.Exists(agent.FilePath))
129+
{
130+
try
131+
{
132+
Process.Start(new ProcessStartInfo
133+
{
134+
FileName = agent.FilePath,
135+
UseShellExecute = true
136+
});
137+
System.Diagnostics.Debug.WriteLine($"Opened agent file in editor: {agent.FilePath}");
138+
}
139+
catch (Exception ex)
140+
{
141+
System.Diagnostics.Debug.WriteLine($"Failed to open agent file in editor: {ex.Message}");
142+
}
143+
}
144+
}
145+
146+
private async void DeleteAgent(AgentModel agent)
119147
{
120148
if (agent != null && !string.IsNullOrEmpty(agent.FileName))
121149
{
150+
// 显示确认对话框
151+
var result = await ShowDeleteConfirmationDialog(agent);
152+
if (result != true)
153+
{
154+
return;
155+
}
156+
122157
if (_directoryService.DeleteAgent(agent.FileName))
123158
{
124159
Agents.Remove(agent);
@@ -136,6 +171,99 @@ private void DeleteAgent(AgentModel agent)
136171
}
137172
}
138173

174+
private async Task<bool> ShowDeleteConfirmationDialog(AgentModel agent)
175+
{
176+
try
177+
{
178+
var mainWindow = Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop
179+
? desktop.MainWindow
180+
: null;
181+
182+
if (mainWindow == null)
183+
return false;
184+
185+
var dialog = new Window
186+
{
187+
Title = "确认删除",
188+
Width = 400,
189+
Height = 200,
190+
WindowStartupLocation = WindowStartupLocation.CenterOwner,
191+
CanResize = false
192+
};
193+
194+
var panel = new StackPanel
195+
{
196+
Margin = new Thickness(20),
197+
Spacing = 20
198+
};
199+
200+
panel.Children.Add(new TextBlock
201+
{
202+
Text = $"确定要删除Agent \"{agent.Name}\" 吗?",
203+
FontSize = 16,
204+
TextWrapping = TextWrapping.Wrap
205+
});
206+
207+
panel.Children.Add(new TextBlock
208+
{
209+
Text = "此操作无法撤销。",
210+
FontSize = 12,
211+
Foreground = Brushes.Gray
212+
});
213+
214+
var buttonPanel = new StackPanel
215+
{
216+
Orientation = Orientation.Horizontal,
217+
HorizontalAlignment = HorizontalAlignment.Right,
218+
Spacing = 10
219+
};
220+
221+
var cancelButton = new Button
222+
{
223+
Content = "取消",
224+
Width = 80,
225+
Height = 32
226+
};
227+
228+
var deleteButton = new Button
229+
{
230+
Content = "删除",
231+
Width = 80,
232+
Height = 32,
233+
Background = Brushes.Red,
234+
Foreground = Brushes.White
235+
};
236+
237+
bool? result = null;
238+
239+
cancelButton.Click += (s, e) =>
240+
{
241+
result = false;
242+
dialog.Close();
243+
};
244+
245+
deleteButton.Click += (s, e) =>
246+
{
247+
result = true;
248+
dialog.Close();
249+
};
250+
251+
buttonPanel.Children.Add(cancelButton);
252+
buttonPanel.Children.Add(deleteButton);
253+
panel.Children.Add(buttonPanel);
254+
255+
dialog.Content = panel;
256+
257+
await dialog.ShowDialog(mainWindow);
258+
return result == true;
259+
}
260+
catch (Exception ex)
261+
{
262+
System.Diagnostics.Debug.WriteLine($"Error showing delete confirmation dialog: {ex.Message}");
263+
return false;
264+
}
265+
}
266+
139267
private void SetupFileWatcher()
140268
{
141269
try
@@ -153,8 +281,6 @@ private void SetupFileWatcher()
153281
};
154282

155283
_fileWatcher.Changed += OnFileChanged;
156-
_fileWatcher.Created += OnFileCreated;
157-
_fileWatcher.Deleted += OnFileDeleted;
158284
_fileWatcher.Renamed += OnFileRenamed;
159285

160286
System.Diagnostics.Debug.WriteLine($"File watcher setup for: {agentsDirectory}");
@@ -175,24 +301,6 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
175301
}, TaskScheduler.FromCurrentSynchronizationContext());
176302
}
177303

178-
private void OnFileCreated(object sender, FileSystemEventArgs e)
179-
{
180-
System.Diagnostics.Debug.WriteLine($"File created: {e.Name}");
181-
Task.Delay(500).ContinueWith(_ =>
182-
{
183-
LoadAgents();
184-
}, TaskScheduler.FromCurrentSynchronizationContext());
185-
}
186-
187-
private void OnFileDeleted(object sender, FileSystemEventArgs e)
188-
{
189-
System.Diagnostics.Debug.WriteLine($"File deleted: {e.Name}");
190-
Task.Delay(500).ContinueWith(_ =>
191-
{
192-
LoadAgents();
193-
}, TaskScheduler.FromCurrentSynchronizationContext());
194-
}
195-
196304
private void OnFileRenamed(object sender, RenamedEventArgs e)
197305
{
198306
System.Diagnostics.Debug.WriteLine($"File renamed: {e.OldName} -> {e.Name}");
@@ -207,8 +315,6 @@ public void Dispose()
207315
if (_fileWatcher != null)
208316
{
209317
_fileWatcher.Changed -= OnFileChanged;
210-
_fileWatcher.Created -= OnFileCreated;
211-
_fileWatcher.Deleted -= OnFileDeleted;
212318
_fileWatcher.Renamed -= OnFileRenamed;
213319
_fileWatcher.Dispose();
214320
_fileWatcher = null;

0 commit comments

Comments
 (0)