Skip to content

Commit 1a1919f

Browse files
committed
Merge branch 'master' of https://github.com/SciSharp/BotSharp
2 parents 26ffb04 + 5dabd8f commit 1a1919f

8 files changed

Lines changed: 485 additions & 0 deletions

File tree

src/Plugins/BotSharp.Plugin.Membase/Controllers/MembaseController.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,150 @@ public async Task<IActionResult> DeleteEdge(string graphId, string edgeId)
448448
new { message = "An error occurred while deleting the edge.", error = ex.Message });
449449
}
450450
}
451+
452+
/// <summary>
453+
/// Simulate a PGT definition
454+
/// </summary>
455+
/// <param name="graphId">The graph identifier</param>
456+
/// <param name="definitionId">The PGT definition identifier</param>
457+
/// <param name="request">The simulation request containing start node and options</param>
458+
/// <returns>Simulation result with trace log and visited nodes</returns>
459+
#if DEBUG
460+
[AllowAnonymous]
461+
#endif
462+
[HttpPost("/membase/{graphId}/pgt-definitions/{definitionId}/simulate")]
463+
[ProducesResponseType(StatusCodes.Status200OK)]
464+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
465+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
466+
public async Task<IActionResult> SimulatePgtDefinition(string graphId, string definitionId, [FromBody] PgtSimulationRequest request)
467+
{
468+
if (string.IsNullOrWhiteSpace(graphId))
469+
{
470+
return BadRequest("Graph ID cannot be empty.");
471+
}
472+
473+
if (string.IsNullOrWhiteSpace(definitionId))
474+
{
475+
return BadRequest("Definition ID cannot be empty.");
476+
}
477+
478+
if (string.IsNullOrWhiteSpace(request?.StartId))
479+
{
480+
return BadRequest("Start ID cannot be empty.");
481+
}
482+
483+
try
484+
{
485+
request.Options ??= new();
486+
if (string.IsNullOrEmpty(request.Options.RunId))
487+
{
488+
request.Options.RunId = $"sim-{Guid.NewGuid()}";
489+
}
490+
491+
var result = await _membaseApi.SimulatePgtDefinitionAsync(graphId, definitionId, request);
492+
result.RunId = request.Options.RunId;
493+
return Ok(result);
494+
}
495+
catch (Exception ex)
496+
{
497+
return StatusCode(
498+
StatusCodes.Status500InternalServerError,
499+
new { message = "An error occurred while simulating the PGT definition.", error = ex.Message });
500+
}
501+
}
502+
503+
/// <summary>
504+
/// Traverse a PGT definition
505+
/// </summary>
506+
/// <param name="graphId">The graph identifier</param>
507+
/// <param name="definitionId">The PGT definition identifier</param>
508+
/// <param name="request">The traversal request containing start node and options</param>
509+
/// <returns>Traversal result with context, visited nodes, and trace log</returns>
510+
#if DEBUG
511+
[AllowAnonymous]
512+
#endif
513+
[HttpPost("/membase/{graphId}/pgt-definitions/{definitionId}/traverse")]
514+
[ProducesResponseType(StatusCodes.Status200OK)]
515+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
516+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
517+
public async Task<IActionResult> TraversePgtDefinition(string graphId, string definitionId, [FromBody] PgtTraversalRequest request)
518+
{
519+
if (string.IsNullOrWhiteSpace(graphId))
520+
{
521+
return BadRequest("Graph ID cannot be empty.");
522+
}
523+
524+
if (string.IsNullOrWhiteSpace(definitionId))
525+
{
526+
return BadRequest("Definition ID cannot be empty.");
527+
}
528+
529+
if (string.IsNullOrWhiteSpace(request?.StartId))
530+
{
531+
return BadRequest("Start ID cannot be empty.");
532+
}
533+
534+
try
535+
{
536+
request.Options ??= new();
537+
if (string.IsNullOrEmpty(request.Options.RunId))
538+
{
539+
request.Options.RunId = $"trv-{Guid.NewGuid()}";
540+
}
541+
542+
var result = await _membaseApi.TraversePgtDefinitionAsync(graphId, definitionId, request);
543+
result.RunId = request.Options.RunId;
544+
return Ok(result);
545+
}
546+
catch (Exception ex)
547+
{
548+
return StatusCode(
549+
StatusCodes.Status500InternalServerError,
550+
new { message = "An error occurred while traversing the PGT definition.", error = ex.Message });
551+
}
552+
}
553+
554+
/// <summary>
555+
/// Validate a PGT definition
556+
/// </summary>
557+
/// <param name="graphId">The graph identifier</param>
558+
/// <param name="definitionId">The PGT definition identifier</param>
559+
/// <param name="request">The validation request containing start node and options</param>
560+
/// <returns>Validation result with valid flag, errors, warnings, and stats</returns>
561+
#if DEBUG
562+
[AllowAnonymous]
563+
#endif
564+
[HttpPost("/membase/{graphId}/pgt-definitions/{definitionId}/validate")]
565+
[ProducesResponseType(StatusCodes.Status200OK)]
566+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
567+
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
568+
public async Task<IActionResult> ValidatePgtDefinition(string graphId, string definitionId, [FromBody] PgtValidationRequest request)
569+
{
570+
if (string.IsNullOrWhiteSpace(graphId))
571+
{
572+
return BadRequest("Graph ID cannot be empty.");
573+
}
574+
575+
if (string.IsNullOrWhiteSpace(definitionId))
576+
{
577+
return BadRequest("Definition ID cannot be empty.");
578+
}
579+
580+
if (string.IsNullOrWhiteSpace(request?.StartId))
581+
{
582+
return BadRequest("Start ID cannot be empty.");
583+
}
584+
585+
try
586+
{
587+
var result = await _membaseApi.ValidatePgtDefinitionAsync(graphId, definitionId, request);
588+
return Ok(result);
589+
}
590+
catch (Exception ex)
591+
{
592+
return StatusCode(
593+
StatusCodes.Status500InternalServerError,
594+
new { message = "An error occurred while validating the PGT definition.", error = ex.Message });
595+
}
596+
}
451597
}

src/Plugins/BotSharp.Plugin.Membase/Interfaces/IMembaseApi.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,15 @@ public interface IMembaseApi
4545
[Delete("/graph/{graphId}/edge/{edgeId}")]
4646
Task DeleteEdgeAsync(string graphId, string edgeId);
4747
#endregion
48+
49+
#region PGT
50+
[Post("/graph/{graphId}/pgt-definitions/{definitionId}/simulate")]
51+
Task<PgtSimulationResponse> SimulatePgtDefinitionAsync(string graphId, string definitionId, [Body] PgtSimulationRequest request);
52+
53+
[Post("/graph/{graphId}/pgt-definitions/{definitionId}/traverse")]
54+
Task<PgtTraversalResponse> TraversePgtDefinitionAsync(string graphId, string definitionId, [Body] PgtTraversalRequest request);
55+
56+
[Post("/graph/{graphId}/pgt-definitions/{definitionId}/validate")]
57+
Task<PgtValidationResponse> ValidatePgtDefinitionAsync(string graphId, string definitionId, [Body] PgtValidationRequest request);
58+
#endregion
4859
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Plugin.Membase.Models;
4+
5+
public class PgtSimulationRequest
6+
{
7+
public string StartId { get; set; } = string.Empty;
8+
9+
[JsonPropertyName("options")]
10+
public PgtSimulationOptions? Options { get; set; }
11+
}
12+
13+
public class PgtSimulationOptions
14+
{
15+
[JsonPropertyName("max_depth")]
16+
public int? MaxDepth { get; set; }
17+
18+
[JsonPropertyName("timeout_ms")]
19+
public int? TimeoutMs { get; set; }
20+
21+
[JsonPropertyName("strategy")]
22+
public string? Strategy { get; set; }
23+
24+
[JsonPropertyName("initial_context")]
25+
public Dictionary<string, object>? InitialContext { get; set; }
26+
27+
[JsonPropertyName("stream")]
28+
public bool Stream { get; set; }
29+
30+
[JsonPropertyName("run_id")]
31+
public string? RunId { get; set; }
32+
33+
[JsonPropertyName("persist_run")]
34+
public bool PersistRun { get; set; }
35+
36+
[JsonPropertyName("debug")]
37+
public bool Debug { get; set; }
38+
39+
[JsonPropertyName("pause_on")]
40+
public string[]? PauseOn { get; set; }
41+
42+
[JsonPropertyName("debug_idle_timeout_ms")]
43+
public int? DebugIdleTimeoutMs { get; set; }
44+
45+
[JsonPropertyName("node_execute_hooks")]
46+
public Dictionary<string, object>? NodeExecuteHooks { get; set; }
47+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Plugin.Membase.Models;
4+
5+
public class PgtTraversalRequest
6+
{
7+
public string StartId { get; set; } = string.Empty;
8+
9+
[JsonPropertyName("options")]
10+
public PgtTraversalOptions? Options { get; set; }
11+
}
12+
13+
public class PgtTraversalOptions
14+
{
15+
[JsonPropertyName("max_depth")]
16+
public int? MaxDepth { get; set; }
17+
18+
[JsonPropertyName("max_subgraph_nesting")]
19+
public int? MaxSubGrapNesting { get; set; }
20+
21+
[JsonPropertyName("max_visits_per_node")]
22+
public int? MaxVisitsPerNode { get; set; }
23+
24+
[JsonPropertyName("strategy")]
25+
public string? Strategy { get; set; }
26+
27+
[JsonPropertyName("timeout_ms")]
28+
public int? TimeoutMs { get; set; }
29+
30+
[JsonPropertyName("node_validation_hooks")]
31+
public Dictionary<string, object>? NodeValidationHooks { get; set; }
32+
33+
[JsonPropertyName("edge_validation_hooks")]
34+
public Dictionary<string, object>? EdgeValidationHooks { get; set; }
35+
36+
[JsonPropertyName("edge_evaluate_hooks")]
37+
public Dictionary<string, object>? EdgeEvaluateHooks { get; set; }
38+
39+
[JsonPropertyName("node_execute_hooks")]
40+
public Dictionary<string, object>? NodeExecuteHooks { get; set; }
41+
42+
[JsonPropertyName("traits")]
43+
public Dictionary<string, object>? Traits { get; set; }
44+
45+
[JsonPropertyName("interfaces")]
46+
public Dictionary<string, object>? Interfaces { get; set; }
47+
48+
[JsonPropertyName("actors")]
49+
public Dictionary<string, object>? Actors { get; set; }
50+
51+
[JsonPropertyName("initial_context")]
52+
public Dictionary<string, object>? InitialContext { get; set; }
53+
54+
[JsonPropertyName("environment")]
55+
public Dictionary<string, object>? Environment { get; set; }
56+
57+
[JsonPropertyName("functions")]
58+
public Dictionary<string, object>? Functions { get; set; }
59+
60+
[JsonPropertyName("record_trace")]
61+
public bool RecordTrace { get; set; }
62+
63+
[JsonPropertyName("stream")]
64+
public bool Stream { get; set; }
65+
66+
[JsonPropertyName("persist_run")]
67+
public bool PersistRun { get; set; }
68+
69+
[JsonPropertyName("debug")]
70+
public bool Debug { get; set; }
71+
72+
[JsonPropertyName("pause_on")]
73+
public string[]? PauseOn { get; set; }
74+
75+
[JsonPropertyName("debug_idle_timeout_ms")]
76+
public int? DebugIdleTimeoutMs { get; set; }
77+
78+
[JsonPropertyName("run_id")]
79+
public string? RunId { get; set; }
80+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Plugin.Membase.Models;
4+
5+
public class PgtValidationRequest
6+
{
7+
public string StartId { get; set; } = string.Empty;
8+
9+
[JsonPropertyName("options")]
10+
public PgtValidationOptions? Options { get; set; }
11+
}
12+
13+
public class PgtValidationOptions
14+
{
15+
[JsonPropertyName("max_depth")]
16+
public int? MaxDepth { get; set; }
17+
18+
[JsonPropertyName("max_nodes")]
19+
public int? MaxNodes { get; set; }
20+
21+
[JsonPropertyName("allow_cycles")]
22+
public bool? AllowCycles { get; set; }
23+
24+
[JsonPropertyName("target_node_ids")]
25+
public string[]? TargetNodeIds { get; set; }
26+
27+
[JsonPropertyName("edge_types")]
28+
public string[]? EdgeTypes { get; set; }
29+
30+
[JsonPropertyName("node_validation_hooks")]
31+
public Dictionary<string, object>? NodeValidationHooks { get; set; }
32+
33+
[JsonPropertyName("edge_validation_hooks")]
34+
public Dictionary<string, object>? EdgeValidationHooks { get; set; }
35+
36+
[JsonPropertyName("traits")]
37+
public Dictionary<string, object>? Traits { get; set; }
38+
39+
[JsonPropertyName("interfaces")]
40+
public Dictionary<string, object>? Interfaces { get; set; }
41+
42+
[JsonPropertyName("actors")]
43+
public Dictionary<string, object>? Actors { get; set; }
44+
45+
[JsonPropertyName("initial_context")]
46+
public Dictionary<string, object>? InitialContext { get; set; }
47+
}

0 commit comments

Comments
 (0)