-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatSegment.cs
More file actions
171 lines (142 loc) · 5.89 KB
/
ChatSegment.cs
File metadata and controls
171 lines (142 loc) · 5.89 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Collections.Generic;
using System.Buffers;
namespace ProjectVG.Application.Models.Chat
{
public sealed class ChatSegment : IDisposable
{
public string Content { get; private set; } = string.Empty;
public int Order { get; private set; }
public string? Emotion { get; private set; }
public List<string>? Actions { get; private set; }
public byte[]? AudioData { get; private set; }
public string? AudioContentType { get; private set; }
public float? AudioLength { get; private set; }
// LOH 방지를 위한 ArrayPool 기반 메모리 관리
internal IMemoryOwner<byte>? AudioMemoryOwner { get; private set; }
internal int AudioDataSize { get; private set; }
private bool _disposed;
public bool HasContent => !string.IsNullOrEmpty(Content);
public bool HasAudio => (AudioData != null && AudioData.Length > 0) || (AudioMemoryOwner != null && AudioDataSize > 0);
public bool IsEmpty => !HasContent && !HasActions;
public bool HasEmotion => !string.IsNullOrEmpty(Emotion);
public bool HasActions => Actions != null && Actions.Any();
public ReadOnlySpan<byte> GetAudioSpan()
{
if (AudioMemoryOwner != null && AudioDataSize > 0)
{
var memory = AudioMemoryOwner.Memory;
var safeSize = Math.Min(AudioDataSize, memory.Length);
return memory.Span.Slice(0, safeSize);
}
if (AudioData != null)
{
return new ReadOnlySpan<byte>(AudioData);
}
return ReadOnlySpan<byte>.Empty;
}
private ChatSegment() { }
public static ChatSegment Create(string content, string? emotion = null, List<string>? actions = null, int order = 0)
{
return new ChatSegment
{
Content = content,
Emotion = emotion,
Actions = actions,
Order = order
};
}
public static ChatSegment CreateText(string content, string? emotion = null, int order = 0)
{
return Create(content, emotion, null, order);
}
public static ChatSegment CreateAction(string action, int order = 0)
{
return Create("", null, new List<string> { action }, order);
}
public ChatSegment WithAudioData(byte[] audioData, string audioContentType, float audioLength)
{
return new ChatSegment
{
Content = this.Content,
Order = this.Order,
Emotion = this.Emotion,
Actions = this.Actions,
AudioData = audioData,
AudioContentType = audioContentType,
AudioLength = audioLength
};
}
// 주의: 원본 인스턴스의 AudioMemoryOwner 해제됨
public ChatSegment WithAudioMemory(IMemoryOwner<byte> audioMemoryOwner, int audioDataSize, string audioContentType, float audioLength)
{
if (audioMemoryOwner is null)
throw new ArgumentNullException(nameof(audioMemoryOwner));
if (audioDataSize < 0 || audioDataSize > audioMemoryOwner.Memory.Length)
throw new ArgumentOutOfRangeException(
nameof(audioDataSize),
audioDataSize,
$"audioDataSize는 0 이상 {audioMemoryOwner.Memory.Length} 이하여야 합니다.");
// 기존 소유자 해제 및 상태 정리
this.AudioMemoryOwner?.Dispose();
this.AudioMemoryOwner = null;
this.AudioDataSize = 0;
return new ChatSegment
{
Content = this.Content,
Order = this.Order,
Emotion = this.Emotion,
Actions = this.Actions,
AudioMemoryOwner = audioMemoryOwner,
AudioDataSize = audioDataSize,
AudioContentType = audioContentType,
AudioLength = audioLength,
AudioData = null
};
}
/// <summary>
/// 오디오 메모리를 부착한 새 인스턴스 생성 (원본 불변)
/// </summary>
public ChatSegment AttachAudioMemory(IMemoryOwner<byte> audioMemoryOwner, int audioDataSize, string audioContentType, float audioLength)
{
if (audioMemoryOwner is null)
throw new ArgumentNullException(nameof(audioMemoryOwner));
if (audioDataSize < 0 || audioDataSize > audioMemoryOwner.Memory.Length)
throw new ArgumentOutOfRangeException(
nameof(audioDataSize),
audioDataSize,
$"audioDataSize는 0 이상 {audioMemoryOwner.Memory.Length} 이하여야 합니다.");
return new ChatSegment
{
Content = this.Content,
Order = this.Order,
Emotion = this.Emotion,
Actions = this.Actions,
AudioMemoryOwner = audioMemoryOwner,
AudioDataSize = audioDataSize,
AudioContentType = audioContentType,
AudioLength = audioLength,
AudioData = null
};
}
// 필요시만 사용 - LOH 위험 있음
public byte[]? GetAudioDataAsArray()
{
if (AudioData != null)
{
return AudioData;
}
if (AudioMemoryOwner != null && AudioDataSize > 0)
{
var span = AudioMemoryOwner.Memory.Span.Slice(0, AudioDataSize);
return span.ToArray();
}
return null;
}
public void Dispose()
{
if (_disposed) return;
AudioMemoryOwner?.Dispose();
_disposed = true;
}
}
}