Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace BotSharp.Abstraction.Knowledges;

/// <summary>
/// Graph-based semantic knowledge service that supports complex relationships and connections between entities.
/// This service allows for executing Cypher queries to traverse and analyze graph data structures.
/// </summary>
public interface ICypherGraphService
{
Task<GraphQueryResult> Execute(string graphId, string query, Dictionary<string, object>? args = null);

Task<GraphNode> MergeNode(string graphId, GraphNode node);

Task<bool> DeleteNode(string graphId, string nodeId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace BotSharp.Abstraction.Knowledges.Models;

public class GraphQueryResult
{
public string[] Columns { get; set; } = [];
public Dictionary<string, object?>[] Items { get; set; } = [];
}

public class GraphNode
{
public string Id { get; set; } = string.Empty;

public List<string> Labels { get; set; } = new();

public object Properties { get; set; } = new();

public DateTime Time { get; set; } = DateTime.UtcNow;

public override string ToString()
{
var labelsString = Labels.Count > 0 ? string.Join(", ", Labels) : "No Labels";
return $"Node ({labelsString}: {Id})";
}
}
9 changes: 2 additions & 7 deletions src/Plugins/BotSharp.Plugin.Membase/MembasePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using BotSharp.Abstraction.Plugins;
using BotSharp.Abstraction.Settings;
using BotSharp.Plugin.Membase.Services;
using BotSharp.Plugin.Membase.Settings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Refit;
using System.Threading.Tasks;

namespace BotSharp.Plugin.Membase;

Expand All @@ -29,5 +22,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
Task.FromResult($"Bearer {dbSettings.ApiKey}")
})
.ConfigureHttpClient(c => c.BaseAddress = new Uri(dbSettings.Host));

services.AddScoped<ICypherGraphService, MembaseService>();
}
}
17 changes: 17 additions & 0 deletions src/Plugins/BotSharp.Plugin.Membase/Models/CypherQueryRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BotSharp.Plugin.Membase.Models;

public class CypherQueryRequest
{
public string Query { get; set; } = string.Empty;

public Dictionary<string, object> Parameters { get; set; } = [];

public bool IncludeExecutionPlan { get; set; } = false;

/// <summary>
/// Whether to profile the query execution.
/// </summary>
public bool Profile { get; set; } = false;

public int? TimeoutMs { get; set; }
}
17 changes: 17 additions & 0 deletions src/Plugins/BotSharp.Plugin.Membase/Models/CypherQueryResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BotSharp.Plugin.Membase.Models;

public class CypherQueryResponse
{
public string[] Columns { get; set; } = [];
public Dictionary<string, object?>[] Data { get; set; } = [];

public CypherNotification[] Notifications { get; set; } = [];
public int RowCount { get; set; }
}

public class CypherNotification
{
public string Code { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion src/Plugins/BotSharp.Plugin.Membase/Models/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Node

public List<string> Labels { get; set; } = new();

public Dictionary<string, object> Properties { get; set; } = new();
public object Properties { get; set; } = new();

public DateTime Time { get; set; } = DateTime.UtcNow;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ public class NodeCreationModel
{
public string? Id { get; set; }
public string[]? Labels { get; set; }
public Dictionary<string, object>? Properties { get; set; }
public object? Properties { get; set; }
public DateTime? Time { get; set; }

public Node ToNode()
{
return new Node
{
Id = Id,
Labels = Labels?.ToList() ?? new List<string>(),
Properties = Properties ?? new Dictionary<string, object>(),
Time = DateTime.UtcNow
Properties = Properties ?? new(),
Time = Time ?? DateTime.UtcNow
};
}
}
11 changes: 7 additions & 4 deletions src/Plugins/BotSharp.Plugin.Membase/Models/NodeUpdateModel.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Text.Json;

namespace BotSharp.Plugin.Membase.Models;

public class NodeUpdateModel
{
public string Id { get; set; } = null!;
public string[]? Labels { get; set; }
public Dictionary<string, object>? Properties { get; set; }
public object? Properties { get; set; }
public DateTime? Time { get; set; }

public Node ToNode()
{
return new Node
{
Id = Id,
Labels = Labels?.ToList() ?? new List<string>(),
Properties = Properties ?? new Dictionary<string, object>(),
Time = DateTime.UtcNow
Labels = Labels?.ToList() ?? [],
Properties = Properties ?? new(),
Time = Time ?? DateTime.UtcNow
};
}
}
18 changes: 14 additions & 4 deletions src/Plugins/BotSharp.Plugin.Membase/Services/IMembaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@

namespace BotSharp.Plugin.Membase.Services;

/// <summary>
/// Membase REST API interface
/// https://membase.dev/graph-api-reference
/// </summary>
public interface IMembaseApi
{
[Post("/cypher/execute?graphId={graphId}")]
Task<CypherQueryResponse> CypherQueryAsync(string graphId, CypherQueryRequest request);

[Post("/graph/{graphId}/node")]
Task CreateNode(string graphId, [Body] NodeCreationModel node);
Task<Node> CreateNodeAsync(string graphId, [Body] NodeCreationModel node);

[Get("/graph/{graphId}/node/{nodeId}")]
Task<Node> GetNode(string graphId, string nodeId);
Task<Node> GetNodeAsync(string graphId, string nodeId);

[Put("/graph/{graphId}/node/{nodeId}")]
Task<Node> UpdateNode(string graphId, string nodeId, [Body] NodeUpdateModel node);
Task<Node> UpdateNodeAsync(string graphId, string nodeId, [Body] NodeUpdateModel node);

[Put("/graph/{graphId}/node/{nodeId}/merge")]
Task<Node> MergeNodeAsync(string graphId, string nodeId, [Body] NodeUpdateModel node);

[Delete("/graph/{graphId}/node/{nodeId}")]
Task<IActionResult> DeleteNode(string graphId, string nodeId);
Task<IActionResult> DeleteNodeAsync(string graphId, string nodeId);
}
49 changes: 48 additions & 1 deletion src/Plugins/BotSharp.Plugin.Membase/Services/MembaseService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
using BotSharp.Abstraction.Knowledges;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Plugin.Membase.Models;
using System.Threading.Tasks;

namespace BotSharp.Plugin.Membase.Services;

public class MembaseService
public class MembaseService : ICypherGraphService
{
private readonly IServiceProvider _services;
private readonly IMembaseApi _membase;

public MembaseService(IServiceProvider services, IMembaseApi membase)
{
_services = services;
_membase = membase;
}

public async Task<GraphQueryResult> Execute(string graphId, string query, Dictionary<string, object>? args = null)
{
var response = await _membase.CypherQueryAsync(graphId, new CypherQueryRequest
{
Query = query,
Parameters = args ?? []
});

return new GraphQueryResult
{
Columns = response.Columns,
Items = response.Data
};
}

public async Task<GraphNode> MergeNode(string graphId, GraphNode node)
{
var newNode = await _membase.MergeNodeAsync(graphId, node.Id, new NodeUpdateModel
{
Id = node.Id,
Labels = [.. node.Labels],
Properties = node.Properties,
Time = node.Time
});

return node;
}

public async Task<bool> DeleteNode(string graphId, string nodeId)
{
await _membase.DeleteNodeAsync(graphId, nodeId);
return true;
}
}
8 changes: 8 additions & 0 deletions src/Plugins/BotSharp.Plugin.Membase/Using.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;

global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;

global using BotSharp.Abstraction.Users;
global using BotSharp.Abstraction.Knowledges;
global using BotSharp.Abstraction.Plugins;
global using BotSharp.Abstraction.Settings;
global using BotSharp.Plugin.Membase.Services;
global using BotSharp.Plugin.Membase.Settings;
Loading