-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatProcessResultMessage.cs
More file actions
112 lines (93 loc) · 3.77 KB
/
ChatProcessResultMessage.cs
File metadata and controls
112 lines (93 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.Text.Json.Serialization;
using System.Buffers;
using System.Buffers.Text;
namespace ProjectVG.Application.Models.Chat
{
public record ChatProcessResultMessage
{
[JsonPropertyName("request_id")]
public string? RequestId { get; init; }
[JsonPropertyName("type")]
public string Type { get; init; } = "chat";
[JsonPropertyName("text")]
public string? Text { get; init; }
[JsonPropertyName("emotion")]
public string? Emotion { get; init; }
[JsonPropertyName("actions")]
public string[]? Actions { get; init; }
[JsonPropertyName("audio_data")]
public string? AudioData { get; init; }
[JsonPropertyName("audio_format")]
public string? AudioFormat { get; init; }
[JsonPropertyName("audio_length")]
public float? AudioLength { get; init; }
[JsonPropertyName("timestamp")]
public DateTime Timestamp { get; init; } = DateTime.UtcNow;
[JsonPropertyName("order")]
public int Order { get; init; }
[JsonPropertyName("credits_used")]
public decimal? CreditsUsed { get; init; }
[JsonPropertyName("credits_remaining")]
public decimal? CreditsRemaining { get; init; }
public static ChatProcessResultMessage FromSegment(ChatSegment segment, string? requestId = null)
{
var audioData = segment.HasAudio ? ConvertToBase64Optimized(segment.GetAudioSpan()) : null;
var audioFormat = segment.HasAudio ? segment.AudioContentType ?? "wav" : null;
return new ChatProcessResultMessage
{
RequestId = requestId,
Type = "chat",
Text = segment.Content,
Emotion = segment.Emotion,
Actions = segment.Actions?.ToArray(),
AudioData = audioData,
AudioFormat = audioFormat,
AudioLength = segment.AudioLength,
Order = segment.Order,
Timestamp = DateTime.UtcNow
};
}
public ChatProcessResultMessage WithAudioData(byte[]? audioBytes)
{
if (audioBytes != null && audioBytes.Length > 0)
{
return this with { AudioData = ConvertToBase64Optimized(new ReadOnlySpan<byte>(audioBytes)) };
}
else
{
return this with { AudioData = null };
}
}
/// <summary>
/// ArrayPool을 사용한 메모리 효율적인 Base64 인코딩 (LOH 방지)
/// </summary>
private static string? ConvertToBase64Optimized(ReadOnlySpan<byte> data)
{
if (data.IsEmpty) return null;
var arrayPool = ArrayPool<byte>.Shared;
var base64Length = Base64.GetMaxEncodedToUtf8Length(data.Length);
var buffer = arrayPool.Rent(base64Length);
try
{
if (Base64.EncodeToUtf8(data, buffer, out _, out var bytesWritten) == OperationStatus.Done)
{
// UTF8 바이트를 문자열로 변환
return System.Text.Encoding.UTF8.GetString(buffer, 0, bytesWritten);
}
else
{
// 폴백: 기존 방법 사용
return Convert.ToBase64String(data);
}
}
finally
{
arrayPool.Return(buffer);
}
}
public ChatProcessResultMessage WithCreditInfo(decimal? creditsUsed, decimal? creditsRemaining)
{
return this with { CreditsUsed = creditsUsed, CreditsRemaining = creditsRemaining };
}
}
}