Skip to content

Commit fda4f5a

Browse files
author
Jicheng Lu
committed
add pgt simulate
1 parent e1de5c5 commit fda4f5a

4 files changed

Lines changed: 178 additions & 0 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,51 @@ 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+
request.Options.RunId = $"sim-{Guid.NewGuid()}";
487+
var result = await _membaseApi.SimulatePgtDefinitionAsync(graphId, definitionId, request);
488+
result.RunId = request.Options.RunId;
489+
return Ok(result);
490+
}
491+
catch (Exception ex)
492+
{
493+
return StatusCode(
494+
StatusCodes.Status500InternalServerError,
495+
new { message = "An error occurred while simulating the PGT definition.", error = ex.Message });
496+
}
497+
}
451498
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@ 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+
#endregion
4853
}
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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.Plugin.Membase.Models;
4+
5+
public class PgtSimulationResponse
6+
{
7+
public string[] Columns { get; set; } = [];
8+
public PgtSimulationDataItem[] Data { get; set; } = [];
9+
10+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
11+
public PgtSimulationStatistics? Statistics { get; set; }
12+
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
14+
public object? ExecutionPlan { get; set; }
15+
public CypherNotification[] Notifications { get; set; } = [];
16+
public int RowCount { get; set; }
17+
18+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
19+
public DateTime? ExecutedAt { get; set; }
20+
21+
public string RunId { get; set; }
22+
}
23+
24+
public class PgtSimulationDataItem
25+
{
26+
[JsonPropertyName("final_context")]
27+
public Dictionary<string, object>? FinalContext { get; set; }
28+
29+
[JsonPropertyName("visited")]
30+
public string[] Visited { get; set; } = [];
31+
32+
[JsonPropertyName("trace_log")]
33+
public PgtTraceLogEntry[] TraceLog { get; set; } = [];
34+
35+
[JsonPropertyName("halted")]
36+
public string Halted { get; set; } = string.Empty;
37+
38+
[JsonPropertyName("halt_reason")]
39+
public string HaltReason { get; set; } = string.Empty;
40+
}
41+
42+
public class PgtTraceLogEntry
43+
{
44+
public string Event { get; set; } = string.Empty;
45+
46+
[JsonPropertyName("node_id")]
47+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
48+
public string? NodeId { get; set; }
49+
50+
[JsonPropertyName("edge_id")]
51+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
52+
public string? EdgeId { get; set; }
53+
54+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
55+
public string? Source { get; set; }
56+
57+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
58+
public string? Target { get; set; }
59+
60+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
61+
public string? Type { get; set; }
62+
63+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
64+
public bool? Allowed { get; set; }
65+
66+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
67+
public int? Depth { get; set; }
68+
69+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
70+
public Dictionary<string, object>? Output { get; set; }
71+
72+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
73+
public Dictionary<string, object>? Input { get; set; }
74+
}
75+
76+
public class PgtSimulationStatistics
77+
{
78+
public long ExecutionTimeMs { get; set; }
79+
}

0 commit comments

Comments
 (0)