@@ -11,7 +11,7 @@ namespace OpenAI.ChatGpt;
11
11
public class ChatGPT : IDisposable
12
12
{
13
13
private readonly string _userId ;
14
- private readonly IChatHistoryStorage _chatHistoryStorage ;
14
+ private readonly IChatHistoryStorage _storage ;
15
15
private readonly ITimeProvider _clock ;
16
16
private readonly ChatGPTConfig ? _config ;
17
17
private readonly OpenAiClient _client ;
@@ -29,7 +29,7 @@ public ChatGPT(
29
29
{
30
30
_client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
31
31
_userId = userId ?? throw new ArgumentNullException ( nameof ( userId ) ) ;
32
- _chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException ( nameof ( chatHistoryStorage ) ) ;
32
+ _storage = chatHistoryStorage ?? throw new ArgumentNullException ( nameof ( chatHistoryStorage ) ) ;
33
33
_clock = clock ?? throw new ArgumentNullException ( nameof ( clock ) ) ;
34
34
_config = config ;
35
35
}
@@ -44,7 +44,7 @@ public ChatGPT(
44
44
ChatGPTConfig ? config )
45
45
{
46
46
_client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
47
- _chatHistoryStorage = chatHistoryStorage ?? throw new ArgumentNullException ( nameof ( chatHistoryStorage ) ) ;
47
+ _storage = chatHistoryStorage ?? throw new ArgumentNullException ( nameof ( chatHistoryStorage ) ) ;
48
48
_clock = clock ?? throw new ArgumentNullException ( nameof ( clock ) ) ;
49
49
_userId = Guid . Empty . ToString ( ) ;
50
50
_config = config ;
@@ -71,12 +71,10 @@ public void Dispose()
71
71
}
72
72
73
73
/// <summary> Continues the last topic or starts a new one.</summary>
74
- public async Task < Chat > ContinueOrStartNewTopic (
75
- DateTimeOffset ? createdAt = null ,
76
- CancellationToken cancellationToken = default )
74
+ public async Task < Chat > ContinueOrStartNewTopic ( CancellationToken cancellationToken = default )
77
75
{
78
76
if ( _currentChat is not null ) return _currentChat ;
79
- var topic = await _chatHistoryStorage . GetMostRecentTopicOrNull ( _userId , cancellationToken ) ;
77
+ var topic = await _storage . GetMostRecentTopicOrNull ( _userId , cancellationToken ) ;
80
78
return topic is null
81
79
? await StartNewTopic ( cancellationToken : cancellationToken )
82
80
: await SetTopic ( topic , cancellationToken ) ;
@@ -91,13 +89,13 @@ public async Task<Chat> StartNewTopic(
91
89
CancellationToken cancellationToken = default )
92
90
{
93
91
config = ChatGPTConfig . CombineOrDefault ( _config , config ) ;
94
- var topic = new Topic ( _chatHistoryStorage . NewTopicId ( ) , _userId , name , _clock . GetCurrentTime ( ) , config ) ;
95
- await _chatHistoryStorage . AddTopic ( topic , cancellationToken ) ;
92
+ var topic = new Topic ( _storage . NewTopicId ( ) , _userId , name , _clock . GetCurrentTime ( ) , config ) ;
93
+ await _storage . AddTopic ( topic , cancellationToken ) ;
96
94
initialDialog ??= config . GetInitialDialogOrNull ( ) ;
97
95
if ( initialDialog is not null )
98
96
{
99
97
var messages = ConvertToPersistentMessages ( initialDialog , topic ) ;
100
- await _chatHistoryStorage . SaveMessages ( _userId , topic . Id , messages , cancellationToken ) ;
98
+ await _storage . SaveMessages ( _userId , topic . Id , messages , cancellationToken ) ;
101
99
}
102
100
103
101
_currentChat = CreateChat ( topic , initialDialog is null , clearOnDisposal : clearOnDisposal ) ;
@@ -108,13 +106,13 @@ private IEnumerable<PersistentChatMessage> ConvertToPersistentMessages(ChatCompl
108
106
{
109
107
return dialog . GetMessages ( )
110
108
. Select ( m => new PersistentChatMessage (
111
- _chatHistoryStorage . NewMessageId ( ) , _userId , topic . Id , _clock . GetCurrentTime ( ) , m )
109
+ _storage . NewMessageId ( ) , _userId , topic . Id , _clock . GetCurrentTime ( ) , m )
112
110
) ;
113
111
}
114
112
115
113
public async Task < Chat > SetTopic ( Guid topicId , CancellationToken cancellationToken = default )
116
114
{
117
- var topic = await _chatHistoryStorage . GetTopic ( _userId , topicId , cancellationToken ) ;
115
+ var topic = await _storage . GetTopic ( _userId , topicId , cancellationToken ) ;
118
116
if ( topic is null )
119
117
{
120
118
throw new ArgumentException ( $ "Chat with id { topicId } not found for user { _userId } ") ;
@@ -132,12 +130,12 @@ private Task<Chat> SetTopic(Topic topic, CancellationToken cancellationToken = d
132
130
private Chat CreateChat ( Topic topic , bool isNew , bool clearOnDisposal )
133
131
{
134
132
if ( topic == null ) throw new ArgumentNullException ( nameof ( topic ) ) ;
135
- return new Chat ( _chatHistoryStorage , _clock , _client , _userId , topic , isNew , clearOnDisposal ) ;
133
+ return new Chat ( _storage , _clock , _client , _userId , topic , isNew , clearOnDisposal ) ;
136
134
}
137
135
138
136
public async Task < IReadOnlyList < Topic > > GetTopics ( CancellationToken cancellationToken = default )
139
137
{
140
- var chats = await _chatHistoryStorage . GetTopics ( _userId , cancellationToken ) ;
138
+ var chats = await _storage . GetTopics ( _userId , cancellationToken ) ;
141
139
return chats . ToList ( ) ;
142
140
}
143
141
0 commit comments