-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Chats.BE.Controllers.Chats.Messages.Dtos; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chats.BE.Controllers.Chats.Chats.Dtos; | ||
|
||
public record SseEndMessage | ||
{ | ||
[JsonPropertyName("requestMessage")] | ||
public required MessageDto? RequestMessage { get; init; } | ||
|
||
[JsonPropertyName("responseMessage")] | ||
public required MessageDto ResponseMessage { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Chats.BE.Controllers.Chats.Chats.Dtos; | ||
|
||
public enum SseResponseKind | ||
{ | ||
StopId = 0, | ||
Segment = 1, | ||
Error = 2, | ||
End = 3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,95 @@ | ||
using System.Text.Json.Serialization; | ||
using Chats.BE.Controllers.Chats.Messages.Dtos; | ||
using Chats.BE.DB; | ||
using Chats.BE.Services.ChatServices; | ||
using Chats.BE.Services.FileServices; | ||
using Chats.BE.Services.UrlEncryption; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Chats.BE.Controllers.Chats.Chats.Dtos; | ||
|
||
public record SseResponseLine | ||
public record SseResponseLine<T> | ||
{ | ||
[JsonPropertyName("result")] | ||
public required string Result { get; init; } | ||
[JsonPropertyName("r")] | ||
public required T Result { get; init; } | ||
|
||
[JsonPropertyName("success")] | ||
public required bool Success { get; init; } | ||
[JsonPropertyName("k")] | ||
public required SseResponseKind Kind { get; init; } | ||
} | ||
|
||
public static class SseResponseLine | ||
{ | ||
public static SseResponseLine<string> CreateSegment(string segment) | ||
{ | ||
return new SseResponseLine<string> | ||
{ | ||
Result = segment, | ||
Kind = SseResponseKind.Segment, | ||
}; | ||
} | ||
|
||
public static SseResponseLine<string> CreateError(string error) | ||
{ | ||
return new SseResponseLine<string> | ||
{ | ||
Result = error, | ||
Kind = SseResponseKind.Error, | ||
}; | ||
} | ||
|
||
public static SseResponseLine<SseEndMessage> CreateEnd( | ||
Message? userMessage, | ||
Message assistantMessage, | ||
IUrlEncryptionService urlEncryptionService, | ||
FileUrlProvider fup) | ||
{ | ||
ChatMessageTemp? userMessageTemp = userMessage == null ? null : new ChatMessageTemp() | ||
{ | ||
Content = [.. userMessage.MessageContents], | ||
CreatedAt = userMessage.CreatedAt, | ||
Id = userMessage.Id, | ||
ParentId = userMessage.ParentId, | ||
Role = (DBChatRole)userMessage.ChatRoleId, | ||
Usage = null, | ||
}; | ||
ChatMessageTemp assistantMessageTemp = new() | ||
{ | ||
Content = [.. assistantMessage.MessageContents], | ||
CreatedAt = assistantMessage.CreatedAt, | ||
Id = assistantMessage.Id, | ||
ParentId = assistantMessage.ParentId, | ||
Role = (DBChatRole)assistantMessage.ChatRoleId, | ||
Usage = assistantMessage.Usage == null ? null : new ChatMessageTempUsage() | ||
{ | ||
Duration = assistantMessage.Usage.TotalDurationMs - assistantMessage.Usage.PreprocessDurationMs, | ||
FirstTokenLatency = assistantMessage.Usage.FirstResponseDurationMs, | ||
InputPrice = assistantMessage.Usage.InputCost, | ||
InputTokens = assistantMessage.Usage.InputTokens, | ||
ModelId = assistantMessage.Usage.UserModel.ModelId, | ||
ModelName = assistantMessage.Usage.UserModel.Model.Name, | ||
OutputPrice = assistantMessage.Usage.OutputCost, | ||
OutputTokens = assistantMessage.Usage.OutputTokens, | ||
ReasoningTokens = assistantMessage.Usage.ReasoningTokens, | ||
}, | ||
}; | ||
MessageDto? userMessageDto = userMessageTemp?.ToDto(urlEncryptionService, fup); | ||
MessageDto assistantMessageDto = assistantMessageTemp.ToDto(urlEncryptionService, fup); | ||
return new SseResponseLine<SseEndMessage> | ||
{ | ||
Result = new SseEndMessage | ||
{ | ||
RequestMessage = userMessageDto, | ||
ResponseMessage = assistantMessageDto | ||
}, | ||
Kind = SseResponseKind.End, | ||
}; | ||
} | ||
|
||
public static SseResponseLine<string> CreateStopId(string stopId) | ||
{ | ||
return new SseResponseLine<string> | ||
{ | ||
Result = stopId, | ||
Kind = SseResponseKind.StopId, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.