Skip to content

Commit

Permalink
[All] AiRecord (#670)
Browse files Browse the repository at this point in the history
Co-authored-by: DarkRRb <177549718+DarkRRb@users.noreply.github.com>
  • Loading branch information
Decrabbityyy and DarkRRb authored Nov 3, 2024
1 parent ddee04b commit cdf493e
Show file tree
Hide file tree
Showing 19 changed files with 530 additions and 19 deletions.
32 changes: 32 additions & 0 deletions Lagrange.Core/Common/Entity/AiCharacter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Lagrange.Core.Common.Entity;

[Serializable]
public class AiCharacter
{
public string VoiceId { get; set; }

public string CharacterName { get; set; }

public string CharacterVoiceUrl { get; set; }

public AiCharacter(string voiceId, string characterName, string characterVoiceUrl)
{
VoiceId = voiceId;
CharacterName = characterName;
CharacterVoiceUrl = characterVoiceUrl;
}
}

[Serializable]
public class AiCharacterList
{
public string Type { get; set; }

public List<AiCharacter> Characters { get; set; }

public AiCharacterList(string type, List<AiCharacter> characters)
{
Type = type;
Characters = characters;
}
}
29 changes: 20 additions & 9 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ public static Task<BotGroupClockInResult> GroupClockIn(this BotContext bot, uint
public static Task<bool> MarkAsRead(this BotContext bot, MessageChain targetChain)
{
uint timestamp = (uint)(targetChain.Time - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
return bot.ContextCollection.Business.OperationLogic.MarkAsRead(targetChain.GroupUin ?? 0, targetChain.Uid, targetChain.Sequence, timestamp);
return bot.ContextCollection.Business.OperationLogic.MarkAsRead(targetChain.GroupUin ?? 0, targetChain.Uid,
targetChain.Sequence, timestamp);
}

public static Task<bool> UploadFriendFile(this BotContext bot, uint targetUin, FileEntity fileEntity)
Expand Down Expand Up @@ -241,7 +242,7 @@ public static Task<MessageResult> FriendShake(this BotContext bot, uint friendUi

public static Task<List<string>?> FetchMarketFaceKey(this BotContext bot, List<string> faceIds)
=> bot.ContextCollection.Business.OperationLogic.FetchMarketFaceKey(faceIds);

/// <summary>
/// Set the avatar of the bot itself
/// </summary>
Expand All @@ -252,22 +253,32 @@ public static Task<bool> SetAvatar(this BotContext bot, ImageEntity avatar)

public static Task<bool> FetchSuperFaceId(this BotContext bot, uint id)
=> bot.ContextCollection.Business.OperationLogic.FetchSuperFaceId(id);

public static Task<SysFaceEntry?> FetchFaceEntity(this BotContext bot, uint id)
=> bot.ContextCollection.Business.OperationLogic.FetchFaceEntity(id);

public static Task<bool> GroupJoinEmojiChain(this BotContext bot, uint groupUin, uint emojiId, uint targetMessageSeq)
=> bot.ContextCollection.Business.OperationLogic.GroupJoinEmojiChain(groupUin, emojiId, targetMessageSeq);

public static Task<bool> FriendJoinEmojiChain(this BotContext bot, uint friendUin, uint emojiId, uint targetMessageSeq)
public static Task<bool> FriendJoinEmojiChain(this BotContext bot, uint friendUin, uint emojiId,
uint targetMessageSeq)
=> bot.ContextCollection.Business.OperationLogic.FriendJoinEmojiChain(friendUin, emojiId, targetMessageSeq);

public static Task<string> UploadImage(this BotContext bot, ImageEntity entity)
=> bot.ContextCollection.Business.OperationLogic.UploadImage(entity);

public static Task<ImageOcrResult?> OcrImage(this BotContext bot, string url)
=> bot.ContextCollection.Business.OperationLogic.ImageOcr(url);

public static Task<ImageOcrResult?> OcrImage(this BotContext bot, ImageEntity entity)
=> bot.ContextCollection.Business.OperationLogic.ImageOcr(entity);
}

public static Task<(int Code, string ErrMsg, string? Url)> GetGroupGenerateAiRecordUrl(this BotContext bot, uint groupUin, string character, string text)
=> bot.ContextCollection.Business.OperationLogic.GetGroupGenerateAiRecordUrl(groupUin, character, text);

public static Task<(int Code, string ErrMsg, RecordEntity? RecordEntity)> GetGroupGenerateAiRecord(this BotContext bot, uint groupUin, string character, string text)
=> bot.ContextCollection.Business.OperationLogic.GetGroupGenerateAiRecord(groupUin, character, text);

public static Task<(int Code, string ErrMsg, List<AiCharacterList>? Result)> GetAiCharacters(this BotContext bot, uint chatType, uint groupUin = 42)
=> bot.ContextCollection.Business.OperationLogic.GetAiCharacters(chatType, groupUin);
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public async Task<List<IBotFSEntry>> FetchGroupFSList(uint groupUin, string targ
if (((GroupFSListEvent)events[0]).IsEnd) break;
startIndex += 20;
}

return entries;
}

Expand Down Expand Up @@ -350,7 +351,8 @@ public async Task<bool> RecallFriendMessage(MessageChain chain)

foreach (var result in resolved)
{
var uins = await Task.WhenAll(ResolveUid(result.InvitorMemberUid), ResolveUid(result.TargetMemberUid), ResolveUid(result.OperatorUid));
var uins = await Task.WhenAll(ResolveUid(result.InvitorMemberUid), ResolveUid(result.TargetMemberUid),
ResolveUid(result.OperatorUid));
uint invitorUin = uins[0];
uint targetUin = uins[1];
uint operatorUin = uins[2];
Expand Down Expand Up @@ -664,7 +666,8 @@ public async Task<bool> SetAvatar(ImageEntity avatar)

var ticket = ((HighwayUrlEvent)highwayUrlResults[0]).SigSession;
var md5 = avatar.ImageStream.Value.Md5().UnHex();
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, md5, Array.Empty<byte>());
return await Collection.Highway.UploadSrcByStreamAsync(90, avatar.ImageStream.Value, ticket, md5,
Array.Empty<byte>());
}

public async Task<bool> GroupSetAvatar(uint groupUin, ImageEntity avatar)
Expand Down Expand Up @@ -697,18 +700,20 @@ public async Task<bool> GroupSetAvatar(uint groupUin, ImageEntity avatar)
var ret = (FetchGroupAtAllRemainEvent)results[0];
return (ret.RemainAtAllCountForUin, ret.RemainAtAllCountForGroup);
}

public async Task<bool> FetchSuperFaceId(uint id) => await Collection.Business.CachingLogic.GetCachedIsSuperFaceId(id);

public async Task<SysFaceEntry?> FetchFaceEntity(uint id) => await Collection.Business.CachingLogic.GetCachedFaceEntity(id);

public async Task<bool> FetchSuperFaceId(uint id) =>
await Collection.Business.CachingLogic.GetCachedIsSuperFaceId(id);

public async Task<SysFaceEntry?> FetchFaceEntity(uint id) =>
await Collection.Business.CachingLogic.GetCachedFaceEntity(id);

public async Task<bool> GroupJoinEmojiChain(uint groupUin, uint emojiId, uint targetMessageSeq)
{
var groupJoinEmojiChainEvent = GroupJoinEmojiChainEvent.Create(targetMessageSeq, emojiId, groupUin);
var results = await Collection.Business.SendEvent(groupJoinEmojiChainEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}

public async Task<bool> FriendJoinEmojiChain(uint friendUin, uint emojiId, uint targetMessageSeq)
{
string? friendUid = await Collection.Business.CachingLogic.ResolveUid(null, friendUin);
Expand All @@ -717,7 +722,59 @@ public async Task<bool> FriendJoinEmojiChain(uint friendUin, uint emojiId, uint
var results = await Collection.Business.SendEvent(friendJoinEmojiChainEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}


public async Task<(int Code, string ErrMsg, string? Url)> GetGroupGenerateAiRecordUrl(uint groupUin, string character, string text)
{
var (code, errMsg, record) = await GetGroupGenerateAiRecord(groupUin, character, text);
if (code != 0)
return (code, errMsg, null);

var recordGroupDownloadEvent = RecordGroupDownloadEvent.Create(groupUin, record!.MsgInfo!);
var @event = await Collection.Business.SendEvent(recordGroupDownloadEvent);
if (@event.Count == 0) return (-1, "running event missing!", null);

var finalResult = (RecordGroupDownloadEvent)@event[0];
return finalResult.ResultCode == 0
? (finalResult.ResultCode, string.Empty, finalResult.AudioUrl)
: (finalResult.ResultCode, "Failed to get group ai record", null);
}

public async Task<(int Code, string ErrMsg, RecordEntity? Record)> GetGroupGenerateAiRecord(uint groupUin, string character, string text)
{
var groupAiRecordEvent = GroupAiRecordEvent.Create(groupUin, character, text);
while (true)
{
var results = await Collection.Business.SendEvent(groupAiRecordEvent);
if (results.Count == 0) return (-1, "running event missing!", null);
var aiRecordResult = (GroupAiRecordEvent)results[0];
if (aiRecordResult.ResultCode != 0)
return (aiRecordResult.ResultCode, aiRecordResult.ErrorMessage, null);
if (aiRecordResult.RecordInfo is not null)
{
var index = aiRecordResult.RecordInfo!.MsgInfoBody[0].Index;
return (aiRecordResult.ResultCode, string.Empty, new RecordEntity(index.FileUuid, index.Info.FileName, index.Info.FileHash.UnHex())
{
AudioLength = (int)index.Info.Time,
FileSha1 = index.Info.FileSha1,
MsgInfo = aiRecordResult.RecordInfo
});
}
}

}

public async Task<(int Code, string ErrMsg, List<AiCharacterList>? Result)> GetAiCharacters(uint chatType, uint groupUin)
{
var fetchAiRecordListEvent = FetchAiCharacterListEvent.Create(chatType, groupUin);

var results = await Collection.Business.SendEvent(fetchAiRecordListEvent);
if (results.Count == 0) return (-1, "Event missing!", null);

var result = (FetchAiCharacterListEvent)results[0];

return (result.ResultCode, result.ErrorMessage, result.AiCharacters);
}

public async Task<string> UploadImage(ImageEntity image)
{
await Collection.Highway.ManualUploadEntity(image);
Expand All @@ -728,14 +785,14 @@ public async Task<string> UploadImage(ImageEntity image)
var ret = (ImageDownloadEvent)result[0];
return ret.ImageUrl;
}

public async Task<ImageOcrResult?> ImageOcr(string imageUrl)
{
var imageOcrEvent = ImageOcrEvent.Create(imageUrl);
var results = await Collection.Business.SendEvent(imageOcrEvent);
return results.Count != 0 ? ((ImageOcrEvent)results[0]).ImageOcrResult : null;
}

public async Task<ImageOcrResult?> ImageOcr(ImageEntity image)
{
var imageUrl = await UploadImage(image);
Expand Down
32 changes: 32 additions & 0 deletions Lagrange.Core/Internal/Event/Action/FetchAiCharacterListEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Lagrange.Core.Common.Entity;

namespace Lagrange.Core.Internal.Event.Action;

internal class FetchAiCharacterListEvent : ProtocolEvent
{
public uint GroupUin { get; set; }

public uint ChatType { get; set; }

public List<AiCharacterList>? AiCharacters { get; set; }

public string ErrorMessage { get; set; }=string.Empty;

private FetchAiCharacterListEvent(uint chatType, uint groupUin = 42) : base(true)
{
ChatType = chatType;
GroupUin = groupUin;
AiCharacters = new List<AiCharacterList>();
}

private FetchAiCharacterListEvent(int resultCode, List<AiCharacterList>? aiCharacters,string errMsg) : base(resultCode)
{
AiCharacters = aiCharacters;
ErrorMessage = errMsg;
}

public static FetchAiCharacterListEvent Create(uint chatType, uint groupUin) => new(chatType, groupUin);

public static FetchAiCharacterListEvent Result(int resultCode, List<AiCharacterList>? aiCharacters,string errMsg) =>
new(resultCode, aiCharacters, errMsg);
}
38 changes: 38 additions & 0 deletions Lagrange.Core/Internal/Event/Action/GroupAiRecordEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;
using Lagrange.Core.Message.Entity;

namespace Lagrange.Core.Internal.Event.Action;

internal class GroupAiRecordEvent : ProtocolEvent
{
public uint GroupUin { get; set; }
public string Character { get; set; }= string.Empty;
public string Text { get; set; }= string.Empty;

public MsgInfo? RecordInfo { get; set; }
public string ErrorMessage { get; set; }= string.Empty;

private GroupAiRecordEvent(uint groupUin, string character, string text) : base(0)
{
GroupUin = groupUin;
Character = character;
Text = text;
}

private GroupAiRecordEvent(int resultCode, MsgInfo? msgInfo) : base(resultCode)
{
RecordInfo = msgInfo;
}

private GroupAiRecordEvent(int resultCode, string errMsg) : base(resultCode)
{
ErrorMessage = errMsg;
}

public static GroupAiRecordEvent Create(uint groupUin, string character, string text) =>
new(groupUin, character, text);

public static GroupAiRecordEvent Result(int resultCode, MsgInfo? msgInfo) => new(resultCode, msgInfo);

public static GroupAiRecordEvent Result(int resultCode, string errMessage) => new(resultCode, errMessage);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

#pragma warning disable CS8618
[ProtoContract]
[OidbSvcTrpcTcp(0x929B, 0)]
internal class OidbSvcTrpcTcp0x929B_0
{
[ProtoMember(1)] public uint GroupCode { get; set; }

[ProtoMember(2)] public string VoiceId { get; set; }

[ProtoMember(3)] public string Text { get; set; }

[ProtoMember(4)] public uint ChatType { get; set; } = 1; //1 voice,2 song

[ProtoMember(5)] private OidbSvcTrpcTcp0x929B_0ClientMsgInfo ClientMsgInfo { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x929B_0ClientMsgInfo
{
[ProtoMember(1)] private uint MsgRandom { get; set; } = 233;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

[ProtoContract]
[OidbSvcTrpcTcp(0x929C, 0)]
internal class OidbSvcTrpcTcp0x929C_0
{
[ProtoMember(1)] public uint Group { get; set; }

[ProtoMember(2)] public uint ChatType { get; set; } = 1; //1 voice,2 song
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

[ProtoContract]
[OidbSvcTrpcTcp(0x929D, 0)]
internal class OidbSvcTrpcTcp0x929D_0
{
[ProtoMember(1)] public uint Group { get; set; }

[ProtoMember(2)] public uint ChatType { get; set; } //1 voice,2 song
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Response;

[ProtoContract]
internal class OidbSvcTrpcTcp0x929B_0Response
{
[ProtoMember(1)] uint Field1 { get; set; } //1 complete ,2 wait

[ProtoMember(2)] uint? Field2 { get; set; } //319

[ProtoMember(3)] uint Field3 { get; set; } //20

[ProtoMember(4)] public MsgInfo? MsgInfo { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Response;
#pragma warning disable CS8618
[ProtoContract]
internal class OidbSvcTrpcTcp0x929C_0Response
{
[ProtoMember(1)] public List<OidbSvcTrpcTcp0x929C_0ResponseProperty> Property { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x929C_0ResponseProperty
{
[ProtoMember(1)] public string CharacterId { get; set; }

[ProtoMember(2)] public string CharacterName { get; set; }

[ProtoMember(3)] public string CharacterVoiceUrl { get; set; }
}
Loading

0 comments on commit cdf493e

Please sign in to comment.