Skip to content

Commit 3d20e71

Browse files
committed
修复回复带At导致的问题
1 parent 7f9e81e commit 3d20e71

4 files changed

Lines changed: 67 additions & 22 deletions

File tree

src/HuaJiBot.NET.Adapter.OneBot/OneBotApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private async Task<TR> SendAsync<T, TR>(string action, T data)
3434
var req = new ActionRequest<T>(action, data, id);
3535
var str = JsonConvert.SerializeObject(req);
3636

37-
#if DEBUGs
37+
#if DEBUG
3838
Console.WriteLine("Sending: " + str);
3939
#endif
4040
var tcs = new TaskCompletionSource<JToken>();

src/HuaJiBot.NET.Adapter.Satori/Protocol/SatoriApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task<TData> HttpPostAsync<TData>(string selfId, string endpoint, JO
7878
var response = await _http.SendAsync(request);
7979
#if DEBUG
8080
var text = await response.Content.ReadAsStringAsync();
81-
Console.WriteLine(text);
81+
Console.WriteLine("HttpPostAsync: " + text);
8282
var data = JsonSerializer.Deserialize<TData>(text, JsonOptions);
8383
#else
8484
var data = await response.Content.ReadFromJsonAsync<TData>(JsonOptions);

src/HuaJiBot.NET.Plugin.AIChat/PluginMain.cs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using HuaJiBot.NET.Logger;
55
using Microsoft.Extensions.Logging;
66
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Converters;
78
using OpenAI;
89
using OpenAI.Chat;
910
using OpenAI.Models;
@@ -13,11 +14,25 @@ namespace HuaJiBot.NET.Plugin.AIChat;
1314

1415
public class PluginConfig : ConfigBase
1516
{
17+
public string SystemPrompt = "你是一个有用的AI助手";
18+
public ModelConfig Model = new();
19+
}
20+
21+
[JsonConverter(typeof(StringEnumConverter))]
22+
public enum ModelProvider
23+
{
24+
// ReSharper disable once InconsistentNaming
25+
OpenAI,
26+
Google,
27+
}
28+
29+
public class ModelConfig
30+
{
31+
public ModelProvider Provider = ModelProvider.OpenAI;
1632
public string Endpoint = "";
33+
public string Id = "";
1734
public string ApiKey = "";
18-
public string Model = "huihui_ai/qwen2.5-1m-abliterated:14b";
19-
public string SystemPrompt = "你是一个有用的AI助手";
20-
public bool OpenAILogging = false;
35+
public bool Logging = false;
2136
}
2237

2338
public class PluginMain : PluginBase, IPluginWithConfig<PluginConfig>
@@ -32,34 +47,34 @@ private OpenAIClient Client
3247
{
3348
if (
3449
_client is null //首次获取
35-
|| _clientApiKey != Config.ApiKey
36-
|| _clientModel != Config.Model //模型设置有变动
50+
|| _clientApiKey != Config.Model.ApiKey
51+
|| _clientModel != Config.Model.Id //模型设置有变动
3752
)
3853
{
3954
_client = new OpenAIClient(
4055
new ApiKeyCredential(
41-
string.IsNullOrEmpty(Config.ApiKey) ? "null" : Config.ApiKey
56+
string.IsNullOrEmpty(Config.Model.ApiKey) ? "null" : Config.Model.ApiKey
4257
),
4358
new OpenAIClientOptions
4459
{
45-
Endpoint = new Uri(Config.Endpoint),
60+
Endpoint = new Uri(Config.Model.Endpoint),
4661
ClientLoggingOptions = new()
4762
{
48-
EnableLogging = Config.OpenAILogging,
63+
EnableLogging = Config.Model.Logging,
4964
LoggerFactory = LoggerFactory.Create(logger =>
5065
{
5166
logger.AddProvider(new PluginLoggerProvider(this));
5267
}),
5368
},
5469
}
5570
);
56-
_clientApiKey = Config.ApiKey;
57-
_clientModel = Config.Model;
71+
_clientApiKey = Config.Model.ApiKey;
72+
_clientModel = Config.Model.Id;
5873
}
5974
return _client;
6075
}
6176
}
62-
private ChatClient ChatClient => Client.GetChatClient(Config.Model);
77+
private ChatClient ChatClient => Client.GetChatClient(Config.Model.Id);
6378
private OpenAIModelClient ModelClient => Client.GetOpenAIModelClient();
6479

6580
private MessageHistory _history = null!;
@@ -72,8 +87,7 @@ protected override void Initialize()
7287
Task.Run(async () =>
7388
{
7489
var models = await ModelClient.GetModelsAsync();
75-
var s = new StringBuilder("模型列表:");
76-
foreach (var model in models.Value) { }
90+
Info("模型列表:" + string.Join(", ", models.Value.Select(x => x.Id)));
7791
});
7892
}
7993

@@ -168,7 +182,7 @@ await InvokeLlmMessage(
168182
reader = e.CommandReader;
169183
if (reader.Reply(out var data))
170184
{
171-
_ = reader.Input(out var text);
185+
_ = reader.Input(out var text, true);
172186
text ??= "";
173187
SortedList<DateTime, GroupMessage> messageList = [];
174188
void PrependMessage(GroupMessage message)

src/HuaJiBot.NET/Commands/CommandReader.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using System.Text;
23
using static HuaJiBot.NET.Commands.CommonCommandReader;
34

45
namespace HuaJiBot.NET.Commands;
@@ -69,7 +70,10 @@ public abstract record ReaderEntity
6970
public static implicit operator ReaderEntity(string text) => new ReaderText(text);
7071
}
7172

72-
public record ReaderText(string Text) : ReaderEntity;
73+
public record ReaderText(string Text) : ReaderEntity
74+
{
75+
public override string ToString() => Text;
76+
};
7377

7478
public record ReaderAt(string AtTarget) : ReaderEntity
7579
{
@@ -83,6 +87,9 @@ public ReaderAt(string atTarget, string? atText)
8387
}
8488

8589
public string AtText { get; init; } = $"@{AtTarget}";
90+
91+
public override string ToString() =>
92+
AtText.StartsWith("@") ? AtText.TrimEnd() + " " : "@" + AtText.TrimEnd() + " ";
8693
}
8794

8895
public record ReaderReply(ReplyInfo Data) : ReaderEntity;
@@ -94,7 +101,11 @@ private abstract record MatchResult
94101

95102
private record MatchText(string Text) : MatchResult;
96103

97-
private record MatchAt(string AtTarget, string AtText) : MatchResult;
104+
private record MatchAt(string AtTarget, string AtText) : MatchResult
105+
{
106+
public override string ToString() =>
107+
AtText.StartsWith("@") ? AtText.TrimEnd() + " " : "@" + AtText.TrimEnd() + " ";
108+
};
98109

99110
private record MatchReply(ReplyInfo Data) : MatchResult;
100111

@@ -108,8 +119,20 @@ public void Reset()
108119
_seq = BuildReadSeq().GetEnumerator();
109120
IEnumerable<MatchResult> BuildReadSeq()
110121
{
122+
StringBuilder? lastTextBuffer = null;
123+
bool previousIsReply = false; //上一个是回复
111124
foreach (var s in Msg)
112125
{
126+
if (previousIsReply && s is ReaderAt)
127+
{ //上一个是回复,则忽略本次的At
128+
continue;
129+
}
130+
previousIsReply = false;
131+
if (_lastOne)
132+
{
133+
(lastTextBuffer ??= new StringBuilder()).Append(s); //返回剩下的整个文本,加到buffer
134+
break;
135+
}
113136
switch (s)
114137
{
115138
case ReaderText { Text: var _text }:
@@ -119,9 +142,11 @@ IEnumerable<MatchResult> BuildReadSeq()
119142
{
120143
if (_lastOne) //如果是最后一个参数
121144
{
122-
yield return text; //返回剩下的整个文本
123-
//todo 处理并合并剩下的elements
124-
yield break; //结束
145+
(lastTextBuffer ??= new StringBuilder()).Append(text); //返回剩下的整个文本,加到buffer
146+
// yield return text; //返回剩下的整个文本
147+
////todo 处理并合并剩下的elements
148+
//yield break; //结束
149+
break; //结束
125150
}
126151
else if (
127152
//当前有期望的文本,如匹配枚举
@@ -184,18 +209,24 @@ and var end //找结束的匹配引号
184209
}
185210
break;
186211
}
187-
case ReaderAt { AtTarget: var atTarget, AtText: var atText }:
212+
case ReaderAt { AtTarget: var atTarget, AtText: var atText } at:
188213
{
189214
yield return new MatchAt(atTarget, atText);
190215
break;
191216
}
192217
case ReaderReply { Data: var data }:
193218
{
194219
yield return new MatchReply(data);
220+
previousIsReply = true;
195221
break;
196222
}
197223
}
198224
}
225+
226+
if (lastTextBuffer is { } anyLastText)
227+
{ //如果有剩下的文本buffer
228+
yield return new MatchText(anyLastText.ToString()); //返回剩下的整个文本
229+
}
199230
}
200231
}
201232

0 commit comments

Comments
 (0)