Skip to content

Commit 4206764

Browse files
committed
code_style: move command to get file changes for AI to SourceGit.Commands.GetFileChangeForAI
Signed-off-by: leo <longshuang@msn.cn>
1 parent 3a50b05 commit 4206764

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/AI/ChatTools.cs

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,32 @@ public static async Task<ToolChatMessage> Process(ChatToolCall call, Action<stri
3636
{
3737
using var doc = JsonDocument.Parse(call.FunctionArguments);
3838

39-
switch (call.FunctionName)
39+
if (call.FunctionName.Equals(Tool_GetDetailChangesInFile.FunctionName))
4040
{
41-
case nameof(GetDetailChangesInFile):
42-
{
43-
var hasRepo = doc.RootElement.TryGetProperty("repo", out var repoPath);
44-
var hasFile = doc.RootElement.TryGetProperty("file", out var filePath);
45-
var hasOriginalFile = doc.RootElement.TryGetProperty("originalFile", out var originalFilePath);
46-
if (!hasRepo)
47-
throw new ArgumentException("repo", "The repo argument is required");
48-
if (!hasFile)
49-
throw new ArgumentException("file", "The file argument is required");
41+
var hasRepo = doc.RootElement.TryGetProperty("repo", out var repoPath);
42+
var hasFile = doc.RootElement.TryGetProperty("file", out var filePath);
43+
var hasOriginalFile = doc.RootElement.TryGetProperty("originalFile", out var originalFilePath);
44+
if (!hasRepo)
45+
throw new ArgumentException("repo", "The repo argument is required");
46+
if (!hasFile)
47+
throw new ArgumentException("file", "The file argument is required");
5048

51-
output?.Invoke($"Read changes in file: {filePath.GetString()}");
49+
output?.Invoke($"Read changes in file: {filePath.GetString()}");
5250

53-
var toolResult = await ChatTools.GetDetailChangesInFile(
54-
repoPath.GetString(),
55-
filePath.GetString(),
56-
hasOriginalFile ? originalFilePath.GetString() : string.Empty);
57-
return new ToolChatMessage(call.Id, toolResult);
58-
}
59-
default:
60-
throw new NotSupportedException($"The tool {call.FunctionName} is not supported");
51+
var toolResult = await ChatTools.GetDetailChangesInFile(
52+
repoPath.GetString(),
53+
filePath.GetString(),
54+
hasOriginalFile ? originalFilePath.GetString() : string.Empty);
55+
return new ToolChatMessage(call.Id, toolResult);
6156
}
57+
58+
throw new NotSupportedException($"The tool {call.FunctionName} is not supported");
6259
}
6360

6461
private static async Task<string> GetDetailChangesInFile(string repo, string file, string originalFile)
6562
{
66-
var rs = await new GetDiffContentCommand(repo, file, originalFile).ReadAsync();
63+
var rs = await new Commands.GetFileChangeForAI(repo, file, originalFile).ReadAsync();
6764
return rs.IsSuccess ? rs.StdOut : string.Empty;
6865
}
69-
70-
private class GetDiffContentCommand : Commands.Command
71-
{
72-
public GetDiffContentCommand(string repo, string file, string originalFile)
73-
{
74-
WorkingDirectory = repo;
75-
Context = repo;
76-
77-
var builder = new StringBuilder();
78-
builder.Append("diff --no-color --no-ext-diff --diff-algorithm=minimal --cached -- ");
79-
if (!string.IsNullOrEmpty(originalFile) && !file.Equals(originalFile, StringComparison.Ordinal))
80-
builder.Append(originalFile.Quoted()).Append(' ');
81-
builder.Append(file.Quoted());
82-
83-
Args = builder.ToString();
84-
}
85-
86-
public async Task<Result> ReadAsync()
87-
{
88-
return await ReadToEndAsync().ConfigureAwait(false);
89-
}
90-
}
9166
}
9267
}

src/Commands/GetFileChangeForAI.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
5+
namespace SourceGit.Commands
6+
{
7+
public class GetFileChangeForAI : Command
8+
{
9+
public GetFileChangeForAI(string repo, string file, string originalFile)
10+
{
11+
WorkingDirectory = repo;
12+
Context = repo;
13+
14+
var builder = new StringBuilder();
15+
builder.Append("diff --no-color --no-ext-diff --diff-algorithm=minimal --cached -- ");
16+
if (!string.IsNullOrEmpty(originalFile) && !file.Equals(originalFile, StringComparison.Ordinal))
17+
builder.Append(originalFile.Quoted()).Append(' ');
18+
builder.Append(file.Quoted());
19+
20+
Args = builder.ToString();
21+
}
22+
23+
public async Task<Result> ReadAsync()
24+
{
25+
return await ReadToEndAsync().ConfigureAwait(false);
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)