Skip to content

Commit c5103c8

Browse files
authored
Merge branch 'develop' into localization/traditional-chinese
2 parents 1c6bc7f + c9911e3 commit c5103c8

58 files changed

Lines changed: 2045 additions & 1843 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎TRANSLATION.md‎

Lines changed: 147 additions & 42 deletions
Large diffs are not rendered by default.

‎src/AI/ExtraHeadersPolicy.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.ClientModel.Primitives;
3+
using System.Collections.Generic;
4+
using System.Threading.Tasks;
5+
6+
namespace SourceGit.AI
7+
{
8+
public class ExtraHeadersPolicy : PipelinePolicy
9+
{
10+
public ExtraHeadersPolicy(string headers)
11+
{
12+
_headers = headers;
13+
}
14+
15+
public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
16+
{
17+
Apply(message);
18+
ProcessNext(message, pipeline, currentIndex);
19+
}
20+
21+
public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
22+
{
23+
Apply(message);
24+
await ProcessNextAsync(message, pipeline, currentIndex);
25+
}
26+
27+
private void Apply(PipelineMessage message)
28+
{
29+
var lines = _headers.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
30+
31+
foreach (var line in lines)
32+
{
33+
var idx = line.IndexOf(':');
34+
if (idx < 0)
35+
continue;
36+
37+
var key = line.Substring(0, idx).Trim();
38+
var value = line.Substring(idx + 1).Trim();
39+
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
40+
continue;
41+
42+
message.Request.Headers.Add(key, value);
43+
}
44+
}
45+
46+
private readonly string _headers;
47+
}
48+
}

‎src/AI/Service.cs‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.ClientModel;
3+
using System.ClientModel.Primitives;
34
using System.Collections.Generic;
45
using System.Text.Json.Serialization;
6+
57
using Azure.AI.OpenAI;
68
using CommunityToolkit.Mvvm.ComponentModel;
79
using OpenAI;
@@ -66,6 +68,12 @@ public string AdditionalPrompt
6668
set;
6769
} = string.Empty;
6870

71+
public string ExtraHeaders
72+
{
73+
get;
74+
set;
75+
} = string.Empty;
76+
6977
public void FetchAvailableModels()
7078
{
7179
if (!_autoFetchAvailableModels)
@@ -92,9 +100,21 @@ public ChatClient GetChatClient()
92100
private OpenAIClient GetOpenAIClient()
93101
{
94102
var credential = new ApiKeyCredential(ReadApiKeyFromEnv ? Environment.GetEnvironmentVariable(ApiKey) : ApiKey);
95-
return Server.Contains("openai.azure.com/", StringComparison.Ordinal)
96-
? new AzureOpenAIClient(new Uri(Server), credential)
97-
: new OpenAIClient(credential, new() { Endpoint = new Uri(Server) });
103+
104+
if (Server.Contains("openai.azure.com/", StringComparison.Ordinal))
105+
{
106+
if (string.IsNullOrEmpty(ExtraHeaders))
107+
return new AzureOpenAIClient(new Uri(Server), credential);
108+
109+
var azureOptions = new AzureOpenAIClientOptions();
110+
azureOptions.AddPolicy(new ExtraHeadersPolicy(ExtraHeaders), PipelinePosition.PerCall);
111+
return new AzureOpenAIClient(new Uri(Server), credential, azureOptions);
112+
}
113+
114+
var options = new OpenAIClientOptions() { Endpoint = new Uri(Server) };
115+
if (!string.IsNullOrEmpty(ExtraHeaders))
116+
options.AddPolicy(new ExtraHeadersPolicy(ExtraHeaders), PipelinePosition.PerCall);
117+
return new OpenAIClient(credential, options);
98118
}
99119

100120
private string _name = string.Empty;

‎src/Commands/Blame.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Blame(string repo, string file, string revision, bool ignoreWhitespace)
3737
ParseLine(line);
3838

3939
if (_result.IsBinary)
40-
break;
40+
return _result;
4141
}
4242

4343
if (_needUnifyCommitSHA)
@@ -49,6 +49,9 @@ public Blame(string repo, string file, string revision, bool ignoreWhitespace)
4949
}
5050
}
5151

52+
if (_content.Length > 0)
53+
_content.Length--; // Remove the last newline character
54+
5255
_result.Content = _content.ToString();
5356
return _result;
5457
}
@@ -66,9 +69,8 @@ private void ParseLine(string line)
6669
if (!match.Success)
6770
return;
6871

69-
if (_content.Length > 0)
70-
_content.Append('\n');
71-
_content.Append(match.Groups[5].Value);
72+
// Force using `\n` as line-ending
73+
_content.Append(match.Groups[5].Value).Append('\n');
7274

7375
var commit = match.Groups[1].Value;
7476
var file = match.Groups[2].Value.Trim();

‎src/Commands/QuerySubmoduleRevision.cs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public QuerySubmoduleRevision(string repo, string revision)
2828

2929
var rs = await ReadToEndAsync().ConfigureAwait(false);
3030
if (!rs.IsSuccess || string.IsNullOrEmpty(rs.StdOut))
31-
return null;
31+
return new Models.RevisionSubmodule(_revision);
3232

3333
var commit = new Models.Commit();
3434
var lines = rs.StdOut.Split('\0');
3535
if (lines.Length < 9)
36-
return null;
36+
return new Models.RevisionSubmodule(_revision);
3737

3838
commit.SHA = lines[0];
3939
if (!string.IsNullOrEmpty(lines[1]))
@@ -46,19 +46,14 @@ public QuerySubmoduleRevision(string repo, string revision)
4646
commit.CommitterTime = ulong.Parse(lines[6]);
4747
commit.Subject = lines[7];
4848

49-
var message = new Models.CommitFullMessage() { Message = lines[8].TrimEnd() };
49+
var message = lines[8].TrimEnd();
5050
var uncommittedChangesCount = 0;
5151
if (_hasUncommittedChange)
5252
uncommittedChangesCount = await new CountLocalChanges(WorkingDirectory, true)
5353
.GetResultAsync()
5454
.ConfigureAwait(false);
5555

56-
return new Models.RevisionSubmodule()
57-
{
58-
Commit = commit,
59-
FullMessage = message,
60-
UncommittedChanges = uncommittedChangesCount
61-
};
56+
return new Models.RevisionSubmodule(commit, message, uncommittedChangesCount);
6257
}
6358

6459
private bool _hasUncommittedChange = false;

‎src/Converters/IntConverters.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ public static class IntConverters
2525

2626
public static readonly FuncValueConverter<int, string> ToBadgeWithLimit =
2727
new(v => v > 999 ? "999+" : v.ToString());
28+
29+
public static readonly FuncValueConverter<int, bool> IsMaxCountOfStatistics =
30+
new(v => v >= 200000);
2831
}
2932
}

‎src/Models/RevisionFile.cs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,18 @@ public class RevisionSubmodule
5151
public Commit Commit { get; set; } = null;
5252
public CommitFullMessage FullMessage { get; set; } = null;
5353
public int UncommittedChanges { get; set; } = 0;
54+
55+
public RevisionSubmodule(string hash)
56+
{
57+
Commit = new Commit() { SHA = hash };
58+
FullMessage = new CommitFullMessage() { Message = string.Empty };
59+
}
60+
61+
public RevisionSubmodule(Commit commit, string message, int uncommittedChanges)
62+
{
63+
Commit = commit;
64+
FullMessage = new CommitFullMessage() { Message = message };
65+
UncommittedChanges = uncommittedChanges;
66+
}
5467
}
5568
}

0 commit comments

Comments
 (0)