@@ -4,10 +4,103 @@ namespace BotSharp.Plugin.Membase.Models;
44
55public class PgtTraversalRequest
66{
7+ [ JsonPropertyName ( "startId" ) ]
78 public string StartId { get ; set ; } = string . Empty ;
89
910 [ JsonPropertyName ( "options" ) ]
1011 public PgtTraversalOptions ? Options { get ; set ; }
12+
13+ /// <summary>
14+ /// Builds a traverse request from a fetched <see cref="PgtDefinition"/>,
15+ /// merging caller overrides on top of stored config values.
16+ /// </summary>
17+ public static PgtTraversalRequest FromDefinition (
18+ PgtDefinition definition ,
19+ string ? runId = null ,
20+ Dictionary < string , object ? > ? environmentOverrides = null ,
21+ Dictionary < string , object ? > ? initialContextOverrides = null ,
22+ bool stream = false ,
23+ bool debug = false ,
24+ string [ ] ? pauseOn = null ,
25+ int ? debugIdleTimeoutMs = null )
26+ {
27+ var cfg = definition . Config ;
28+
29+ return new PgtTraversalRequest
30+ {
31+ StartId = cfg . StartId ?? string . Empty ,
32+ Options = new PgtTraversalOptions
33+ {
34+ MaxDepth = cfg . MaxDepth ,
35+ MaxVisitsPerNode = cfg . MaxVisitsPerNode ,
36+ TimeoutMs = cfg . TimeoutMs ,
37+ MaxSubGrapNesting = cfg . MaxSubgraphNesting ,
38+ Strategy = cfg . Strategy ,
39+ RecordTrace = cfg . RecordTrace ,
40+ PersistRun = cfg . PersistRun ,
41+ RunId = runId ,
42+ Stream = stream ,
43+ Debug = debug ,
44+ PauseOn = pauseOn ,
45+ DebugIdleTimeoutMs = debugIdleTimeoutMs ,
46+ Actors = ParseActorsJson ( cfg . ActorsJson ) ,
47+ Environment = MergeJsonDict ( cfg . EnvironmentJson , environmentOverrides ) ,
48+ InitialContext = MergeJsonDict ( cfg . InitialContextJson , initialContextOverrides ) ,
49+ } ,
50+ } ;
51+ }
52+
53+ private static Dictionary < string , object > ? ParseActorsJson ( string ? actorsJson )
54+ {
55+ if ( string . IsNullOrWhiteSpace ( actorsJson ) )
56+ return null ;
57+
58+ var array = JsonSerializer . Deserialize < JsonElement [ ] > ( actorsJson ) ;
59+ if ( array is null || array . Length == 0 )
60+ return null ;
61+
62+ var dict = new Dictionary < string , object > ( StringComparer . Ordinal ) ;
63+ foreach ( var element in array )
64+ {
65+ if ( ! element . TryGetProperty ( "actor_id" , out var idProp ) )
66+ continue ;
67+
68+ var actorId = idProp . GetString ( ) ?? string . Empty ;
69+ dict [ actorId ] = JsonSerializer . Deserialize < object > ( element . GetRawText ( ) ) ?? element ;
70+ }
71+
72+ return dict . Count > 0 ? dict : null ;
73+ }
74+
75+ private static Dictionary < string , object > ? MergeJsonDict (
76+ string ? existingJson ,
77+ Dictionary < string , object ? > ? overrides )
78+ {
79+ var merged = new Dictionary < string , object > ( StringComparer . Ordinal ) ;
80+
81+ if ( ! string . IsNullOrWhiteSpace ( existingJson ) )
82+ {
83+ var existing = JsonSerializer . Deserialize < Dictionary < string , object > > ( existingJson ) ;
84+ if ( existing is not null )
85+ {
86+ foreach ( var kv in existing )
87+ merged [ kv . Key ] = kv . Value ;
88+ }
89+ }
90+
91+ if ( overrides is not null && overrides . Count > 0 )
92+ {
93+ foreach ( var kv in overrides )
94+ {
95+ if ( kv . Value is null )
96+ merged . Remove ( kv . Key ) ;
97+ else
98+ merged [ kv . Key ] = kv . Value ;
99+ }
100+ }
101+
102+ return merged . Count > 0 ? merged : null ;
103+ }
11104}
12105
13106public class PgtTraversalOptions
0 commit comments