forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoRuleGraph.cs
More file actions
321 lines (279 loc) · 10.6 KB
/
DemoRuleGraph.cs
File metadata and controls
321 lines (279 loc) · 10.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
using BotSharp.Abstraction.Rules;
using BotSharp.Abstraction.Rules.Models;
using BotSharp.Abstraction.Rules.Options;
using BotSharp.Abstraction.Utilities;
namespace BotSharp.Plugin.Membase.Services;
public class DemoRuleGraph : IRuleFlow<RuleGraph>
{
private readonly IServiceProvider _services;
private readonly ILogger<DemoRuleGraph> _logger;
public DemoRuleGraph(
IServiceProvider services,
ILogger<DemoRuleGraph> logger)
{
_services = services;
_logger = logger;
}
public string Name => "Demo";
public async Task<RuleConfigModel> GetTopologyConfigAsync(RuleFlowConfigOptions? options = null)
{
var settings = _services.GetRequiredService<MembaseSettings>();
var apiKey = settings.ApiKey;
var projectId = settings.ProjectId;
var topologyName = Name;
if (!string.IsNullOrEmpty(options?.TopologyName))
{
topologyName = options.TopologyName;
}
var foundInstance = settings.GraphInstances?.FirstOrDefault(x => x.Name.IsEqualTo(topologyName));
var graphId = foundInstance?.Id ?? string.Empty;
var query = Uri.EscapeDataString("MATCH (a)-[r]->(b) WITH a, r, b WHERE a.agent = $agent AND a.trigger = $trigger AND b.agent = $agent AND b.trigger = $trigger RETURN a, r, b LIMIT 100");
return new RuleConfigModel
{
TopologyId = graphId,
TopologyName = foundInstance?.Name,
CustomParameters = JsonDocument.Parse(JsonSerializer.Serialize(new
{
htmlTag = "iframe",
appendParameterName = "parameters",
url = $"https://console.membase.dev/query-editor/{projectId}?graphId={graphId}&query={query}&token={apiKey}"
}))
};
}
public async Task<RuleGraph?> GetTopologyAsync(string id, RuleFlowLoadOptions? options = null)
{
if (string.IsNullOrEmpty(id))
{
#if DEBUG
return GetDefaultGraph();
#else
return null;
#endif
}
var query = options?.Query ?? string.Empty;
if (string.IsNullOrEmpty(query))
{
query = $"""
MATCH (a)-[r]->(b)
WITH a, r, b
WHERE a.agent = $agent AND a.trigger = $trigger AND b.agent = $agent AND b.trigger = $trigger
RETURN a, r, b
LIMIT 100
""";
}
var args = new Dictionary<string, object>();
if (options?.Parameters != null)
{
foreach (var param in options.Parameters!)
{
if (param.Key == null || param.Value == null)
{
continue;
}
args[param.Key] = param.Value;
}
}
try
{
var graphDb = _services.GetServices<IGraphDb>().First(x => x.Provider.IsEqualTo("membase"));
var result = await graphDb.ExecuteQueryAsync(query, options: new()
{
GraphId = id,
Arguments = args
});
if (result == null)
{
return null;
}
var graph = BuildGraph(result);
return graph;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error when loading graph (id: {GraphId})", id);
return null;
}
}
private RuleGraph BuildGraph(GraphQueryResult result)
{
var graph = RuleGraph.Init();
if (result.Values.IsNullOrEmpty())
{
return graph;
}
foreach (var item in result.Values)
{
// Try to deserialize nodes and edge from the dictionary
if (!item.TryGetValue<JsonElement>("a", out var sourceNodeElement) ||
!item.TryGetValue<JsonElement>("b", out var targetNodeElement) ||
!item.TryGetValue<JsonElement>("r", out var edgeElement))
{
continue;
}
// Parse source node
var sourceNodeId = sourceNodeElement.GetProperty("id").GetString();
var sourceNodeLabels = sourceNodeElement.TryGetProperty("labels", out var sLabels)
? sLabels.EnumerateArray().Select(x => x.GetString() ?? "").ToList()
: [];
var sourceNodeProps = sourceNodeElement.TryGetProperty("properties", out var sProps)
? sProps
: default;
var sourceNodeWeight = sourceNodeElement.TryGetProperty("weight", out var sNodeWeight) && sNodeWeight.ValueKind == JsonValueKind.Number
? sNodeWeight.GetDouble()
: 1.0;
// Parse target node
var targetNodeId = targetNodeElement.GetProperty("id").GetString();
var targetNodeLabels = targetNodeElement.TryGetProperty("labels", out var tLabels)
? tLabels.EnumerateArray().Select(x => x.GetString() ?? "").ToList()
: [];
var targetNodeProps = targetNodeElement.TryGetProperty("properties", out var tProps)
? tProps
: default;
var targetNodeWeight = targetNodeElement.TryGetProperty("weight", out var tNodeWeight) && tNodeWeight.ValueKind == JsonValueKind.Number
? tNodeWeight.GetDouble()
: 1.0;
// Parse edge
var edgeId = edgeElement.GetProperty("id").GetString();
var edgeProps = edgeElement.TryGetProperty("properties", out var eProps)
? eProps
: default;
var edgeWeight = edgeElement.TryGetProperty("weight", out var eWeight) && eWeight.ValueKind == JsonValueKind.Number
? eWeight.GetDouble()
: 1.0;
// Create source node
var sourceNode = new RuleNode()
{
Id = sourceNodeId ?? Guid.NewGuid().ToString(),
Labels = sourceNodeLabels,
Weight = sourceNodeWeight,
Name = GetGraphItemAttribute(sourceNodeProps, key: "name", defaultValue: "node"),
Type = GetGraphItemAttribute(sourceNodeProps, key: "type", defaultValue: "action"),
Purpose = GetGraphItemAttribute(sourceNodeProps, key: "purpose", defaultValue: ""),
Description = GetGraphItemAttribute(sourceNodeProps, key: "description", defaultValue: ""),
Config = GetConfig(sourceNodeProps)
};
// Create target node
var targetNode = new RuleNode()
{
Id = targetNodeId ?? Guid.NewGuid().ToString(),
Labels = targetNodeLabels,
Weight = targetNodeWeight,
Name = GetGraphItemAttribute(targetNodeProps, key: "name", defaultValue: "node"),
Type = GetGraphItemAttribute(targetNodeProps, key: "type", defaultValue: "action"),
Purpose = GetGraphItemAttribute(targetNodeProps, key: "purpose", defaultValue: ""),
Description = GetGraphItemAttribute(targetNodeProps, key: "description", defaultValue: ""),
Config = GetConfig(targetNodeProps)
};
// Create edge payload
var edgePayload = new EdgeItemPayload()
{
Id = edgeId ?? Guid.NewGuid().ToString(),
Name = GetGraphItemAttribute(edgeProps, key: "name", defaultValue: "edge"),
Type = GetGraphItemAttribute(edgeProps, key: "type", defaultValue: "next"),
Purpose = GetGraphItemAttribute(edgeProps, key: "purpose", defaultValue: ""),
Description = GetGraphItemAttribute(edgeProps, key: "description", defaultValue: ""),
Weight = edgeWeight,
Config = GetConfig(edgeProps)
};
// Add edge to graph
graph.AddEdge(sourceNode, targetNode, edgePayload);
}
return graph;
}
private string GetGraphItemAttribute(JsonElement? properties, string key, string defaultValue)
{
if (properties == null || properties.Value.ValueKind == JsonValueKind.Undefined)
{
return defaultValue;
}
if (properties.Value.TryGetProperty(key, out var name) && name.ValueKind == JsonValueKind.String)
{
return name.GetString() ?? defaultValue;
}
return defaultValue;
}
private Dictionary<string, string?> GetConfig(JsonElement? properties)
{
var config = new Dictionary<string, string?>();
if (properties == null || properties.Value.ValueKind == JsonValueKind.Undefined)
{
return config;
}
// Convert all properties to config dictionary
foreach (var prop in properties.Value.EnumerateObject())
{
config[prop.Name] = prop.Value.ConvertToString();
}
return config;
}
private RuleGraph GetDefaultGraph()
{
var graph = RuleGraph.Init();
var root = new RuleNode
{
Name = "start",
Type = "start",
};
var end = new RuleNode
{
Name = "end",
Type = "end",
};
var node1 = new RuleNode
{
Name = "http_request",
Type = "action",
Config = new()
{
["http_method"] = "GET",
["http_url"] = "https://dummy.restapiexample.com/api/v1/employees"
}
};
var node2 = new RuleNode
{
Name = "http_request",
Type = "action",
Config = new()
{
["http_method"] = "GET",
["http_url"] = "https://dummy.restapiexample.com/api/v1/employee/1"
}
};
var node3 = new RuleNode
{
Name = "http_request",
Type = "action",
Config = new()
{
["http_method"] = "GET",
["http_url"] = "https://dummy.restapiexample.com/api/v1/employee/2"
}
};
graph.AddEdge(root, node1, payload: new()
{
Name = "edge",
Type = "next"
});
graph.AddEdge(node1, node2, payload: new()
{
Name = "edge",
Type = "next"
});
graph.AddEdge(node1, node3, payload: new()
{
Name = "edge",
Type = "next"
});
graph.AddEdge(node2, node3, payload: new()
{
Name = "edge",
Type = "next"
});
graph.AddEdge(node3, end, payload: new()
{
Name = "edge",
Type = "next"
});
return graph;
}
}