forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationStateService.cs
More file actions
461 lines (400 loc) · 15.6 KB
/
ConversationStateService.cs
File metadata and controls
461 lines (400 loc) · 15.6 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*****************************************************************************
Copyright 2024 Written by Jicheng Lu. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.SideCar;
namespace BotSharp.Core.Conversations.Services;
/// <summary>
/// Maintain the conversation state
/// </summary>
public class ConversationStateService : IConversationStateService
{
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private readonly IBotSharpRepository _db;
private readonly IRoutingContext _routingContext;
private readonly IConversationSideCar? _sidecar;
private string _conversationId;
/// <summary>
/// States in the current round of conversation
/// </summary>
private ConversationState _curStates;
/// <summary>
/// States in the previous rounds of conversation
/// </summary>
private ConversationState _historyStates;
private bool _isReadOnly;
public ConversationStateService(
IServiceProvider services,
IBotSharpRepository db,
IRoutingContext routingContext,
ILogger<ConversationStateService> logger)
{
_services = services;
_db = db;
_routingContext = routingContext;
_logger = logger;
_curStates = new ConversationState();
_historyStates = new ConversationState();
_sidecar = services.GetService<IConversationSideCar>();
}
public string GetConversationId() => _conversationId;
/// <summary>
/// Set conversation state
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="isNeedVersion">whether the state is related to message or not</param>
/// <returns></returns>
public IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true,
int activeRounds = -1, string valueType = StateDataType.String, string source = StateSource.User, bool readOnly = false)
{
if (value == null)
{
return this;
}
var options = _services.GetRequiredService<BotSharpOptions>();
var defaultRound = -1;
var preValue = string.Empty;
var currentValue = value.ConvertToString(options.JsonSerializerOptions);
var curActive = true;
StateKeyValue? pair = null;
StateValue? prevLeafNode = null;
var curActiveRounds = activeRounds > 0 ? activeRounds : defaultRound;
if (ContainsState(name) && _curStates.TryGetValue(name, out pair))
{
prevLeafNode = pair?.Values?.LastOrDefault();
preValue = prevLeafNode?.Data ?? string.Empty;
}
_logger.LogDebug($"[STATE] {name} = {value}");
var isNoChange = ContainsState(name)
&& preValue == currentValue
&& prevLeafNode?.ActiveRounds == curActiveRounds
&& curActiveRounds == defaultRound
&& prevLeafNode?.Source == source
&& prevLeafNode?.DataType == valueType
&& prevLeafNode?.Active == curActive
&& pair?.Readonly == readOnly;
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
if (!ContainsState(name) || preValue != currentValue || prevLeafNode?.ActiveRounds != curActiveRounds)
{
foreach (var hook in hooks)
{
hook.OnStateChanged(new StateChangeModel
{
ConversationId = _conversationId,
MessageId = _routingContext.MessageId,
Name = name,
BeforeValue = preValue,
BeforeActiveRounds = prevLeafNode?.ActiveRounds,
AfterValue = currentValue,
AfterActiveRounds = curActiveRounds,
DataType = valueType,
Source = source,
Readonly = readOnly
}).ConfigureAwait(false).GetAwaiter().GetResult();
}
}
var newPair = new StateKeyValue
{
Key = name,
Versioning = isNeedVersion,
Readonly = readOnly
};
var newValue = new StateValue
{
Data = currentValue,
MessageId = _routingContext.MessageId,
Active = curActive,
ActiveRounds = curActiveRounds,
DataType = valueType,
Source = source,
UpdateTime = DateTime.UtcNow,
};
if (!isNeedVersion || !_curStates.ContainsKey(name))
{
newPair.Values = new List<StateValue> { newValue };
_curStates[name] = newPair;
}
else if (!isNoChange)
{
_curStates[name].Values.Add(newValue);
}
return this;
}
public async Task<Dictionary<string, string>> Load(string conversationId, bool isReadOnly = false)
{
_isReadOnly = isReadOnly;
_conversationId = conversationId;
Reset();
var endNodes = new Dictionary<string, string>();
if (_sidecar?.IsEnabled == true)
{
return endNodes;
}
_historyStates = await _db.GetConversationStates(conversationId);
if (_historyStates.IsNullOrEmpty())
{
return endNodes;
}
var curMsgId = _routingContext.MessageId;
var dialogs = await _db.GetConversationDialogs(conversationId);
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User)
.GroupBy(x => x.MetaData?.MessageId)
.Select(g => g.First())
.OrderBy(x => x.MetaData?.CreatedTime)
.ToList();
var curMsgIndex = userDialogs.FindIndex(x => !string.IsNullOrEmpty(curMsgId) && x.MetaData?.MessageId == curMsgId);
curMsgIndex = curMsgIndex < 0 ? userDialogs.Count() : curMsgIndex;
foreach (var state in _historyStates)
{
var key = state.Key;
var value = state.Value;
var leafNode = value?.Values?.LastOrDefault();
if (leafNode == null) continue;
_curStates[key] = new StateKeyValue
{
Key = key,
Versioning = value.Versioning,
Readonly = value.Readonly,
Values = new List<StateValue> { leafNode }
};
if (!leafNode.Active) continue;
// Handle state active rounds
if (leafNode.ActiveRounds > 0)
{
var stateMsgIndex = userDialogs.FindIndex(x => !string.IsNullOrEmpty(x.MetaData?.MessageId) && x.MetaData.MessageId == leafNode.MessageId);
if (stateMsgIndex >= 0 && curMsgIndex - stateMsgIndex >= leafNode.ActiveRounds)
{
_curStates[key].Values.Add(new StateValue
{
Data = leafNode.Data,
MessageId = curMsgId,
Active = false,
ActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
UpdateTime = DateTime.UtcNow
});
continue;
}
}
var data = leafNode.Data ?? string.Empty;
endNodes[state.Key] = data;
_logger.LogDebug($"[STATE] {key} : {data}");
}
_logger.LogInformation($"Loaded conversation states: {conversationId}");
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
foreach (var hook in hooks)
{
await hook.OnStateLoaded(_curStates);
}
return endNodes;
}
public async Task Save()
{
if (_conversationId == null || _sidecar?.IsEnabled == true || _isReadOnly)
{
return;
}
var states = new List<StateKeyValue>();
foreach (var pair in _curStates)
{
var key = pair.Key;
var curValue = pair.Value;
if (!_historyStates.TryGetValue(key, out var historyValue)
|| historyValue == null
|| historyValue.Values.IsNullOrEmpty()
|| !curValue.Versioning)
{
states.Add(curValue);
}
else
{
var historyValues = historyValue.Values.Take(historyValue.Values.Count - 1).ToList();
var newValues = historyValues.Concat(curValue.Values).ToList();
var updatedNode = new StateKeyValue
{
Key = pair.Key,
Versioning = curValue.Versioning,
Readonly = curValue.Readonly,
Values = newValues
};
states.Add(updatedNode);
}
}
await _db.UpdateConversationStates(_conversationId, states);
_logger.LogInformation($"Saved states of conversation {_conversationId}");
}
public bool RemoveState(string name)
{
if (!ContainsState(name)) return false;
var value = _curStates[name];
var leafNode = value?.Values?.LastOrDefault();
if (value == null || !value.Versioning || leafNode == null) return false;
_curStates[name].Values.Add(new StateValue
{
Data = leafNode.Data,
MessageId = _routingContext.MessageId,
Active = false,
ActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
UpdateTime = DateTime.UtcNow
});
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
foreach (var hook in hooks)
{
hook.OnStateChanged(new StateChangeModel
{
ConversationId = _conversationId,
MessageId = _routingContext.MessageId,
Name = name,
BeforeValue = leafNode.Data,
BeforeActiveRounds = leafNode.ActiveRounds,
AfterValue = null,
AfterActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
Readonly = value.Readonly
}).ConfigureAwait(false).GetAwaiter().GetResult();
}
return true;
}
public void CleanStates(params string[] excludedStates)
{
var curMsgId = _routingContext.MessageId;
var utcNow = DateTime.UtcNow;
foreach (var key in _curStates.Keys)
{
// skip state
if (excludedStates.Contains(key))
{
continue;
}
var value = _curStates[key];
if (value == null || !value.Versioning || value.Values.IsNullOrEmpty()) continue;
var leafNode = value.Values.LastOrDefault();
if (leafNode == null || !leafNode.Active) continue;
value.Values.Add(new StateValue
{
Data = leafNode.Data,
MessageId = curMsgId,
Active = false,
ActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
UpdateTime = utcNow
});
}
}
public Dictionary<string, string> GetStates()
{
var endNodes = new Dictionary<string, string>();
foreach (var state in _curStates)
{
var value = state.Value?.Values?.LastOrDefault();
if (value == null || !value.Active) continue;
endNodes[state.Key] = value.Data ?? string.Empty;
}
return endNodes;
}
public string GetState(string name, string defaultValue = "")
{
if (!_curStates.ContainsKey(name) || _curStates[name].Values.IsNullOrEmpty() || !_curStates[name].Values.Last().Active)
{
return defaultValue;
}
return _curStates[name].Values.Last().Data;
}
public void Dispose()
{
Save().ConfigureAwait(false).GetAwaiter().GetResult();
}
public bool ContainsState(string name)
{
return _curStates.ContainsKey(name)
&& !_curStates[name].Values.IsNullOrEmpty()
&& _curStates[name].Values.LastOrDefault()?.Active == true
&& !string.IsNullOrEmpty(_curStates[name].Values.Last().Data);
}
public void SaveStateByArgs(JsonDocument args)
{
if (args == null)
{
return;
}
if (args.RootElement is JsonElement root)
{
foreach (JsonProperty property in root.EnumerateObject())
{
var propertyValue = property.Value;
var stateValue = propertyValue.ToString();
if (!string.IsNullOrEmpty(stateValue))
{
if (propertyValue.ValueKind == JsonValueKind.True ||
propertyValue.ValueKind == JsonValueKind.False)
{
stateValue = stateValue?.ToLower();
}
if (CheckArgType(property.Name, stateValue))
{
SetState(property.Name, stateValue, source: StateSource.Application);
}
}
}
}
}
private bool CheckArgType(string name, string value)
{
// Defensive: Ensure AgentParameterTypes is not null or empty and values are not null
if (AgentService.AgentParameterTypes.IsNullOrEmpty())
return true;
if (!AgentService.AgentParameterTypes.TryGetValue(_routingContext.GetCurrentAgentId(), out var agentTypes))
return true;
if (agentTypes.IsNullOrEmpty())
return true;
var found = agentTypes.FirstOrDefault(t => t.Key == name);
if (found.Key != null)
{
return found.Value switch
{
"boolean" => bool.TryParse(value, out _),
"number" => long.TryParse(value, out _),
_ => true,
};
}
return true;
}
public ConversationState GetCurrentState()
{
var values = _curStates.Values.ToList();
var copy = JsonSerializer.Deserialize<List<StateKeyValue>>(JsonSerializer.Serialize(values));
return new ConversationState(copy ?? []);
}
public void SetCurrentState(ConversationState state)
{
var values = state.Values.ToList();
var copy = JsonSerializer.Deserialize<List<StateKeyValue>>(JsonSerializer.Serialize(values));
_curStates = new ConversationState(copy ?? []);
}
public void ResetCurrentState()
{
_curStates.Clear();
}
private void Reset()
{
_curStates.Clear();
_historyStates.Clear();
}
}