-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatSegment.cs
More file actions
126 lines (104 loc) · 4.02 KB
/
ChatSegment.cs
File metadata and controls
126 lines (104 loc) · 4.02 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
using System.Collections.Generic;
using System.Buffers;
namespace ProjectVG.Application.Models.Chat
{
public record ChatSegment
{
public string Content { get; init; } = string.Empty;
public int Order { get; init; }
public string? Emotion { get; init; }
public List<string>? Actions { get; init; }
public byte[]? AudioData { get; init; }
public string? AudioContentType { get; init; }
public float? AudioLength { get; init; }
// 스트림 기반 음성 데이터 처리를 위한 새로운 프로퍼티
public IMemoryOwner<byte>? AudioMemoryOwner { get; init; }
public int AudioDataSize { get; init; }
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();
/// <summary>
/// 메모리 효율적인 방식으로 음성 데이터에 접근합니다
/// </summary>
public ReadOnlySpan<byte> GetAudioSpan()
{
if (AudioMemoryOwner != null && AudioDataSize > 0)
{
return AudioMemoryOwner.Memory.Span.Slice(0, AudioDataSize);
}
if (AudioData != null)
{
return new ReadOnlySpan<byte>(AudioData);
}
return ReadOnlySpan<byte>.Empty;
}
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);
}
// Method to add audio data (returns new record instance)
public ChatSegment WithAudioData(byte[] audioData, string audioContentType, float audioLength)
{
return this with
{
AudioData = audioData,
AudioContentType = audioContentType,
AudioLength = audioLength
};
}
/// <summary>
/// 메모리 효율적인 방식으로 음성 데이터를 추가합니다 (LOH 방지)
/// </summary>
public ChatSegment WithAudioMemory(IMemoryOwner<byte> audioMemoryOwner, int audioDataSize, string audioContentType, float audioLength)
{
return this with
{
AudioMemoryOwner = audioMemoryOwner,
AudioDataSize = audioDataSize,
AudioContentType = audioContentType,
AudioLength = audioLength,
// 기존 AudioData는 null로 설정하여 중복 저장 방지
AudioData = null
};
}
/// <summary>
/// 음성 데이터를 배열로 변환합니다 (필요한 경우에만 사용)
/// </summary>
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;
}
/// <summary>
/// 리소스 해제 (IMemoryOwner 해제)
/// </summary>
public void Dispose()
{
AudioMemoryOwner?.Dispose();
}
}
}