Skip to content

Commit eda61bb

Browse files
committed
Simplify CreateMemoryTool sql
1 parent 3273393 commit eda61bb

File tree

1 file changed

+14
-46
lines changed

1 file changed

+14
-46
lines changed

src/KnowledgeBaseServer/Tools/CreateMemoryTool.cs

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,28 @@ public static string Handle(
3131
using var connection = connectionString.CreateConnection();
3232
using var transaction = connection.BeginTransaction();
3333

34-
var topicId = connection.QuerySingleOrDefault<Guid>(
34+
var newMemoryNodeId = connection.QuerySingle<Guid>(
3535
sql: """
36-
select id
37-
from topics
38-
where name = @Name
39-
""",
40-
new { Name = topic }
41-
);
42-
if (topicId == Guid.Empty)
43-
{
44-
topicId = Guid.CreateVersion7();
45-
_ = connection.Execute(
46-
sql: """
47-
insert into topics (id, created, name) values
48-
(@Id, @Created, @Name)
49-
""",
50-
new
51-
{
52-
Id = topicId,
53-
Created = now,
54-
Name = topic,
55-
},
56-
transaction
57-
);
58-
}
36+
insert into topics (id, created, name) values
37+
(@TopicId, @Now, @Topic)
38+
on conflict (name) do nothing;
5939
60-
var createdNode = new
61-
{
62-
Id = Guid.CreateVersion7(),
63-
Created = now,
64-
TopicId = topicId,
65-
Content = memory,
66-
Context = context,
67-
Importance = importance,
68-
};
69-
_ = connection.Execute(
70-
sql: """
7140
insert into memory_nodes (id, created, topic_id, content, context, importance) values
72-
(@Id, @Created, @TopicId, @Content, @Context, @Importance)
73-
""",
74-
createdNode,
75-
transaction
76-
);
41+
(@MemoryNodeId, @Now, (select id from topics where name = @Topic), @Content, @Context, @Importance)
42+
returning id;
7743
78-
_ = connection.Execute(
79-
sql: """
8044
insert into memory_search (memory_node_id, memory_content, memory_context) values
81-
(@MemoryId, @Content, @Context)
45+
(@MemoryNodeId, @Content, @Context);
8246
""",
8347
new
8448
{
85-
MemoryId = createdNode.Id,
49+
TopicId = Guid.CreateVersion7(),
50+
Now = now,
51+
Topic = topic,
52+
MemoryNodeId = Guid.CreateVersion7(),
8653
Content = memory,
8754
Context = context,
55+
Importance = importance,
8856
},
8957
transaction
9058
);
@@ -93,7 +61,7 @@ insert into memory_search (memory_node_id, memory_content, memory_context) value
9361
{
9462
try
9563
{
96-
_ = connection.ConnectMemoriesInternal(transaction, sourceMemoryNodeId.Value, [createdNode.Id], now);
64+
_ = connection.ConnectMemoriesInternal(transaction, sourceMemoryNodeId.Value, [newMemoryNodeId], now);
9765
}
9866
catch (SqliteException ex) when (ex.IsForeignKeyConstraintViolation())
9967
{
@@ -103,6 +71,6 @@ insert into memory_search (memory_node_id, memory_content, memory_context) value
10371

10472
transaction.Commit();
10573

106-
return AppJsonSerializer.Serialize(new CreatedMemoryDto(createdNode.Id, createdNode.Content));
74+
return AppJsonSerializer.Serialize(new CreatedMemoryDto(newMemoryNodeId, memory));
10775
}
10876
}

0 commit comments

Comments
 (0)