Skip to content

Commit ccca262

Browse files
authored
Merge pull request #244 from iceljc/features/refine-dialog-meta-data
structure dialog meta data
2 parents 1286f35 + 0df8f14 commit ccca262

File tree

4 files changed

+94
-17
lines changed

4 files changed

+94
-17
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,27 @@ public class Conversation
2525

2626
public class DialogElement
2727
{
28-
public string MetaData { get; set; }
28+
public DialogMeta MetaData { get; set; }
2929
public string Content { get; set; }
3030

3131
public DialogElement()
3232
{
3333

3434
}
3535

36-
public DialogElement(string meta, string content)
36+
public DialogElement(DialogMeta meta, string content)
3737
{
3838
MetaData = meta;
3939
Content = content;
4040
}
4141
}
42+
43+
public class DialogMeta
44+
{
45+
public string Role { get; set; }
46+
public string AgentId { get; set; }
47+
public string MessageId { get; set; }
48+
public string? FunctionName { get; set; }
49+
public string? SenderId { get; set; }
50+
public DateTime CreateTime { get; set; }
51+
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ public void Append(string conversationId, RoleDialogModel dialog)
2424

2525
if (dialog.Role == AgentRole.Function)
2626
{
27-
// var args = dialog.FunctionArgs.RemoveNewLine();
28-
var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.FunctionName}";
27+
var meta = new DialogMeta
28+
{
29+
Role = dialog.Role,
30+
AgentId = agentId,
31+
MessageId = dialog.MessageId,
32+
FunctionName = dialog.FunctionName,
33+
CreateTime = dialog.CreatedAt
34+
};
35+
2936
var content = dialog.Content.RemoveNewLine();
3037
if (string.IsNullOrEmpty(content))
3138
{
@@ -35,7 +42,15 @@ public void Append(string conversationId, RoleDialogModel dialog)
3542
}
3643
else
3744
{
38-
var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.SenderId}";
45+
var meta = new DialogMeta
46+
{
47+
Role = dialog.Role,
48+
AgentId = agentId,
49+
MessageId = dialog.MessageId,
50+
SenderId = dialog.SenderId,
51+
CreateTime = dialog.CreatedAt
52+
};
53+
3954
var content = dialog.Content.RemoveNewLine();
4055
if (string.IsNullOrEmpty(content))
4156
{
@@ -58,13 +73,12 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
5873
{
5974
var meta = dialog.MetaData;
6075
var content = dialog.Content;
61-
var blocks = meta.Split('|');
62-
var createdAt = DateTime.Parse(blocks[0]);
63-
var role = blocks[1];
64-
var currentAgentId = blocks[2];
65-
var messageId = blocks[3];
66-
var senderId = role == AgentRole.Function ? currentAgentId : blocks[4];
67-
var function = role == AgentRole.Function ? blocks[4] : null;
76+
var role = meta.Role;
77+
var currentAgentId = meta.AgentId;
78+
var messageId = meta.MessageId;
79+
var function = role == AgentRole.Function ? meta.FunctionName : null;
80+
var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId;
81+
var createdAt = meta.CreateTime;
6882

6983
results.Add(new RoleDialogModel(role, content)
7084
{

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,9 +1037,18 @@ private List<DialogElement> CollectDialogElements(string dialogDir)
10371037
{
10381038
for (int i = 0; i < rawDialogs.Count(); i += 2)
10391039
{
1040-
var meta = rawDialogs[i];
1040+
var blocks = rawDialogs[i].Split("|");
10411041
var content = rawDialogs[i + 1];
10421042
var trimmed = content.Substring(4);
1043+
var meta = new DialogMeta
1044+
{
1045+
Role = blocks[1],
1046+
AgentId = blocks[2],
1047+
MessageId = blocks[3],
1048+
FunctionName = blocks[1] == AgentRole.Function ? blocks[4] : null,
1049+
SenderId = blocks[1] == AgentRole.Function ? null : blocks[4],
1050+
CreateTime = DateTime.Parse(blocks[0])
1051+
};
10431052
dialogs.Add(new DialogElement(meta, trimmed));
10441053
}
10451054
}
@@ -1053,7 +1062,10 @@ private List<string> ParseDialogElements(List<DialogElement> dialogs)
10531062

10541063
foreach (var element in dialogs)
10551064
{
1056-
dialogTexts.Add(element.MetaData);
1065+
var meta = element.MetaData;
1066+
var source = meta.FunctionName ?? meta.SenderId;
1067+
var metaStr = $"{meta.CreateTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{source}";
1068+
dialogTexts.Add(metaStr);
10571069
var content = $" - {element.Content}";
10581070
dialogTexts.Add(content);
10591071
}

src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace BotSharp.Plugin.MongoStorage.Models;
44

55
public class DialogMongoElement
66
{
7-
public string MetaData { get; set; }
7+
public DialogMetaMongoElement MetaData { get; set; }
88
public string Content { get; set; }
99

1010
public DialogMongoElement()
@@ -16,7 +16,7 @@ public static DialogMongoElement ToMongoElement(DialogElement dialog)
1616
{
1717
return new DialogMongoElement
1818
{
19-
MetaData = dialog.MetaData,
19+
MetaData = DialogMetaMongoElement.ToMongoElement(dialog.MetaData),
2020
Content = dialog.Content
2121
};
2222
}
@@ -25,8 +25,49 @@ public static DialogElement ToDomainElement(DialogMongoElement dialog)
2525
{
2626
return new DialogElement
2727
{
28-
MetaData = dialog.MetaData,
28+
MetaData = DialogMetaMongoElement.ToDomainElement(dialog.MetaData),
2929
Content = dialog.Content
3030
};
3131
}
3232
}
33+
34+
public class DialogMetaMongoElement
35+
{
36+
public string Role { get; set; }
37+
public string AgentId { get; set; }
38+
public string MessageId { get; set; }
39+
public string? FunctionName { get; set; }
40+
public string? SenderId { get; set; }
41+
public DateTime CreateTime { get; set; }
42+
43+
public DialogMetaMongoElement()
44+
{
45+
46+
}
47+
48+
public static DialogMeta ToDomainElement(DialogMetaMongoElement meta)
49+
{
50+
return new DialogMeta
51+
{
52+
Role = meta.Role,
53+
AgentId = meta.AgentId,
54+
MessageId = meta.MessageId,
55+
FunctionName = meta.FunctionName,
56+
SenderId = meta.SenderId,
57+
CreateTime = meta.CreateTime,
58+
};
59+
}
60+
61+
public static DialogMetaMongoElement ToMongoElement(DialogMeta meta)
62+
{
63+
return new DialogMetaMongoElement
64+
{
65+
Role = meta.Role,
66+
AgentId = meta.AgentId,
67+
MessageId = meta.MessageId,
68+
FunctionName = meta.FunctionName,
69+
SenderId = meta.SenderId,
70+
CreateTime = meta.CreateTime,
71+
};
72+
}
73+
}

0 commit comments

Comments
 (0)