update conversation record

This commit is contained in:
Jicheng Lu 2023-09-03 14:06:40 -05:00
parent ba96bd81c0
commit be5f9f247a
6 changed files with 70 additions and 71 deletions

View file

@ -15,9 +15,6 @@ public class ConversationRecord : RecordBase
[MaxLength(64)]
public string Title { get; set; } = string.Empty;
public string Dialog { get; set; } = string.Empty;
public string State { get; set; } = string.Empty;
[Required]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;

View file

@ -1,7 +1,9 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations.Settings;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Repositories.Records;
using MongoDB.Bson;
using System.IO;
namespace BotSharp.Core.Conversations.Services;
@ -54,16 +56,33 @@ public partial class ConversationService : IConversationService
public async Task<Conversation> NewConversation(Conversation sess)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var foundUserId = user?.Id ?? _user.Id;
var record = ConversationRecord.FromConversation(sess);
record.Id = sess.Id.IfNullOrEmptyAs(ObjectId.GenerateNewId().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(foundUserId);
record.Title = "New Conversation";
db.Transaction<IBotSharpTable>(delegate
//db.Transaction<IBotSharpTable>(delegate
//{
// db.Add<IBotSharpTable>(record);
//});
var dir = Path.Combine(dbSettings.FileRepository, conversationSettings.DataDir, record.Id);
if (!Directory.Exists(dir))
{
db.Add<IBotSharpTable>(record);
});
Directory.CreateDirectory(dir);
}
var path = Path.Combine(dir, "conversation.json");
File.WriteAllText(path, JsonSerializer.Serialize(record, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
}));
_storage.InitStorage(record.Id);

View file

@ -117,16 +117,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
public void CleanState()
{
//File.Delete(_file);
var conversation = _db.Conversation.FirstOrDefault(x => x.Id == _conversationId);
if (conversation != null)
{
conversation.State = string.Empty;
_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(conversation);
});
}
File.Delete(_file);
}
private string GetStorageFile(string conversationId)
@ -145,32 +136,32 @@ public class ConversationStateService : IConversationStateService, IDisposable
return stateFile;
}
private string GetConversationState(string conversationId)
{
var conversation = _db.Conversation.FirstOrDefault(x => x.Id == conversationId);
if (conversation == null)
{
var user = _db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var record = new ConversationRecord()
{
Id = ObjectId.GenerateNewId().ToString(),
//AgentId = _agentSettings.RouterId,
UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(),
Title = "New Conversation",
Dialog = string.Empty,
State = string.Empty
};
//private string GetConversationState(string conversationId)
//{
// var conversation = _db.Conversation.FirstOrDefault(x => x.Id == conversationId);
// if (conversation == null)
// {
// var user = _db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
// var record = new ConversationRecord()
// {
// Id = ObjectId.GenerateNewId().ToString(),
// //AgentId = _agentSettings.RouterId,
// UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(),
// Title = "New Conversation",
// Dialog = string.Empty,
// State = string.Empty
// };
_db.Transaction<IBotSharpTable>(delegate
{
_db.Add<IBotSharpTable>(record);
});
// _db.Transaction<IBotSharpTable>(delegate
// {
// _db.Add<IBotSharpTable>(record);
// });
conversation = _db.Conversation.FirstOrDefault(x => x.Id == record.Id);
}
// conversation = _db.Conversation.FirstOrDefault(x => x.Id == record.Id);
// }
return conversation.State ?? string.Empty;
}
// return conversation.State ?? string.Empty;
//}
public string GetState(string name)
{

View file

@ -127,29 +127,29 @@ public class ConversationStorage : IConversationStorage
return Path.Combine(dir, "dialogs.txt");
}
private string GetConversationDialogs(string conversationId)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var conversation = db.Conversation.FirstOrDefault(x => x.Id == conversationId);
if (conversation == null)
{
var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
var record = new ConversationRecord()
{
Id = ObjectId.GenerateNewId().ToString(),
//AgentId = _agentSettings.RouterId,
UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(),
Title = "New Conversation"
};
//private string GetConversationDialogs(string conversationId)
//{
// var db = _services.GetRequiredService<IBotSharpRepository>();
// var conversation = db.Conversation.FirstOrDefault(x => x.Id == conversationId);
// if (conversation == null)
// {
// var user = db.User.FirstOrDefault(x => x.ExternalId == _user.Id);
// var record = new ConversationRecord()
// {
// Id = ObjectId.GenerateNewId().ToString(),
// //AgentId = _agentSettings.RouterId,
// UserId = user?.Id ?? ObjectId.GenerateNewId().ToString(),
// Title = "New Conversation"
// };
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IBotSharpTable>(record);
});
// db.Transaction<IBotSharpTable>(delegate
// {
// db.Add<IBotSharpTable>(record);
// });
conversation = db.Conversation.FirstOrDefault(x => x.Id == record.Id);
}
// conversation = db.Conversation.FirstOrDefault(x => x.Id == record.Id);
// }
return conversation.Dialog ?? string.Empty;
}
// return conversation.Dialog ?? string.Empty;
//}
}

View file

@ -5,8 +5,6 @@ public class ConversationCollection : MongoBase
public Guid AgentId { get; set; }
public Guid UserId { get; set; }
public string Title { get; set; }
public string Dialog { get; set; }
public string State { get; set; }
public DateTime CreatedTime { get; set; }
public DateTime UpdatedTime { get; set; }

View file

@ -114,8 +114,6 @@ public class MongoRepository : IBotSharpRepository
AgentId = x.AgentId.ToString(),
UserId = x.UserId.ToString(),
Title = x.Title,
Dialog = x.Dialog,
State = x.State,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
@ -212,8 +210,6 @@ public class MongoRepository : IBotSharpRepository
AgentId = Guid.Parse(x.AgentId),
UserId = Guid.Parse(x.UserId),
Title = x.Title,
Dialog = x.Dialog,
State = x.State,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
@ -225,8 +221,6 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.AgentId, conversation.AgentId)
.Set(x => x.UserId, conversation.UserId)
.Set(x => x.Title, conversation.Title)
.Set(x => x.Dialog, conversation.Dialog)
.Set(x => x.State, conversation.State)
.Set(x => x.CreatedTime, conversation.CreatedTime)
.Set(x => x.UpdatedTime, conversation.UpdatedTime);
_dc.Conversations.UpdateOne(filter, update, _options);