forked from syncfusion/blazor-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextToSequence.razor
More file actions
321 lines (299 loc) · 13.3 KB
/
TextToSequence.razor
File metadata and controls
321 lines (299 loc) · 13.3 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
@page "/ai-diagram/text-to-sequence-diagram"
@page "/diagram/ai-text-to-sequence-diagram"
@using Syncfusion.Blazor.Diagram
@using Syncfusion.Blazor.Diagram.SymbolPalette
@using System.Collections.ObjectModel
@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Blazor.Spinner
@using Syncfusion.Blazor.Popups
@using Syncfusion.Blazor.SplitButtons
@using Syncfusion.Blazor.Inputs
@using System.Text.RegularExpressions
@namespace TextToSequenceDiagram
@using BlazorDemos.Service
@using BlazorDemos.Model
@inject AzureAIService ChatGptService
@implements IDisposable
@inject IJSRuntime JS
@*Hidden:Lines*@
@inherits SampleBaseComponent
@inject NavigationManager NavigationManager
@*End:Hidden*@
<SampleDescription>
<p>
This sample demonstrates how a dynamic sequence diagram is generated using the SfDiagram Component, powered by AI assistance. The process begins with user input, which is analyzed to identify key participants, messages, interaction fragments, and activation regions. These elements are then arranged in a sequential timeline to illustrate how components interact over time. The result is a visual representation that effectively documents and clarifies complex system processes and interactions.
</p>
<p>
Explore our <strong>Smart AI demos</strong> with limited AI token usage directly in your browser. To dive deeper and try out these features locally using your own API key, check out our
<a href="https://github.com/syncfusion/smart-ai-samples/tree/master/blazor" target="_blank" aria-label="Navigate to the Syncfusion Smart AI Samples GitHub repository">
<strong>Syncfusion Smart AI Samples</strong>
</a> on GitHub.
</p>
</SampleDescription>
<ActionDescription>
<p>
This sample demonstrates how a user submits a request to generate a sequence diagram using an AI Prompt Processor. The processor forwards this request to the OpenAI API, which returns interaction data representing participants, messages, and activation regions. This data is parsed and arranged into a sequence diagram layout, visually depicting the flow of interactions and thought processes over time. Finally, the diagram is rendered for the user to explore the dynamic sequence diagram or system interactions.
</p>
</ActionDescription>
@*Hidden:Lines*@
<AINotification></AINotification>
@*End:Hidden*@
@*Hidden:Lines*@
<AIToastNotification></AIToastNotification>
@*End:Hidden*@
<div class="col-lg-12 control-section">
<div class="content-wrapper">
<div class="control-section" style="height: calc(100% - 350px); width: 100%">
<div style="border: 2px solid #ccc;">
<SequenceDiagramToolbar @ref="ToolbarRef"></SequenceDiagramToolbar>
<div class="diagram-area">
<SfDiagramComponent ID="diagram-area" @ref="Diagram" Height="700px" @bind-Model="@umlModel" NodeCreating="@OnNodeCreating" SelectionSettings="@selectionSettings" @bind-InteractionController="@DiagramTool">
<RulerSettings>
<HorizontalRuler>
</HorizontalRuler>
<VerticalRuler>
</VerticalRuler>
</RulerSettings>
<SnapSettings>
<HorizontalGridLines LineColor="#e0e0e0 " LineIntervals="@GridLineIntervals">
</HorizontalGridLines>
<VerticalGridLines LineColor="#e0e0e0" LineIntervals="@GridLineIntervals">
</VerticalGridLines>
</SnapSettings>
<PageSettings MultiplePage="true">
</PageSettings>
<SfSpinner @ref="@SpinnerRef" Label="Generating diagram...." Type="@SpinnerType.Bootstrap"> </SfSpinner>
</SfDiagramComponent>
</div>
</div>
<div style="display: none;visibility: hidden;">
<SfUploader @ref="@uploadFiles" ID="UploadFiles" ShowFileList="false" AllowedExtensions="@ExtensionType">
<UploaderEvents OnUploadStart="@OnUploadFileSelected"></UploaderEvents>
<UploaderAsyncSettings SaveUrl="https://blazor.syncfusion.com/services/production/api/FileUploader/Save"
RemoveUrl="https://blazor.syncfusion.com/services/production/api/FileUploader/Remove"></UploaderAsyncSettings>
</SfUploader>
</div>
</div>
</div>
</div>
<SequenceDiagramOpenAI @ref="DiagramOpenAIRef"></SequenceDiagramOpenAI>
@code{
DiagramSelectionSettings selectionSettings = new DiagramSelectionSettings();
public SfDiagramComponent? Diagram;
public bool IsDiagramLoading = false;
public bool InitialRendering = true;
public SfSpinner? SpinnerRef;
public SequenceDiagramOpenAI? DiagramOpenAIRef;
public SequenceDiagramToolbar? ToolbarRef;
public string ExtensionType = ".json";
public bool IsGeneratingFromAI = false;
public DiagramInteractions DiagramTool = DiagramInteractions.Default;
// Defines interval values for GridLines
public double[] GridLineIntervals { get; set; } = new double[] { };
//Reference to uploder
SfUploader? uploadFiles;
#pragma warning disable CS0414
bool CanUpdateStyle = false;
#pragma warning restore CS0414
private double newWidth = 0;
[Inject]
protected IJSRuntime? jsRuntime { get; set; }
#pragma warning restore CS8618
private void OnNodeCreating(IDiagramObject obj){
Node? node=obj as Node;
if (node!.ID!.Contains("1_Fragment"))
{
node.Style!.StrokeColor = "#0067C0";
}
foreach (ShapeAnnotation annotation in node.Annotations!)
{
if (node.ID.Contains("Transaction") || node.ID.Contains("FraudDetectionSystem"))
{
annotation.Style!.Color = "white";
}
if (!node.ID.Contains("User")){
UpdateNodeWidth(node, annotation.Content!);
}
if (node.Width < newWidth)
{
node.Width = newWidth;
}
}
}
private void UpdateNodeWidth(Node node, string text)
{
if (node == null) return;
if (text == null) text = string.Empty;
double fontSize = 18.0;
double charWidthEstimate = fontSize * 0.5; // Adjust multiplier based on font
double estimatedTextWidth = text.Length * charWidthEstimate;
double measuredWidth = estimatedTextWidth + 20; // Padding
double calculateWidth = Math.Max(100, measuredWidth);
newWidth = calculateWidth;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await base.OnAfterRenderAsync(firstRender);
if (firstRender)
{
if (DiagramOpenAIRef != null)
DiagramOpenAIRef.Parent = this;
if (ToolbarRef != null)
ToolbarRef.Parent = this;
}
}
DiagramModel? umlModel;
protected override void OnInitialized()
{
selectionSettings.Constraints = SelectorConstraints.All & ~SelectorConstraints.Rotate & ~SelectorConstraints.ResizeAll;
umlModel = new UmlSequenceDiagramModel()
{
SpaceBetweenParticipants = 300,
Participants = new List<UmlSequenceParticipant>()
{
new UmlSequenceParticipant()
{
ID = "User",
Content= "User",
IsActor= true,
},
new UmlSequenceParticipant()
{
ID = "Transaction",
Content= "Transaction",
ActivationBoxes= new List<UmlSequenceActivationBox>()
{
new UmlSequenceActivationBox() { ID = "act1", StartMessageID= "1", EndMessageID= "4" }
}
},
new UmlSequenceParticipant()
{
ID = "FraudDetectionSystem",
Content= "FraudDetectionSystem",
ActivationBoxes= new List<UmlSequenceActivationBox>()
{
new UmlSequenceActivationBox() { ID = "act2", StartMessageID= "2", EndMessageID= "3" },
new UmlSequenceActivationBox() { ID = "act3", StartMessageID= "5", EndMessageID= "6" }
}
},
},
Messages = new List<UmlSequenceMessage>()
{
new UmlSequenceMessage()
{
ID = "1",
Content = "Initiate Transaction",
FromParticipantID = "User",
ToParticipantID = "Transaction",
MessageType = UmlSequenceMessageType.Synchronous
},
new UmlSequenceMessage()
{
ID = "2",
Content = "Send Transaction Data",
FromParticipantID = "Transaction",
ToParticipantID = "FraudDetectionSystem",
MessageType = UmlSequenceMessageType.Synchronous
},
new UmlSequenceMessage()
{
ID = "3",
Content = "Validate Transaction",
FromParticipantID = "FraudDetectionSystem",
ToParticipantID = "Transaction",
MessageType = UmlSequenceMessageType.Reply
},
new UmlSequenceMessage()
{
ID = "4",
Content = "Transaction Approved",
FromParticipantID = "Transaction",
ToParticipantID = "User",
MessageType = UmlSequenceMessageType.Asynchronous
},
new UmlSequenceMessage()
{
ID = "5",
Content = "Flag Transaction",
FromParticipantID = "Transaction",
ToParticipantID = "FraudDetectionSystem",
MessageType = UmlSequenceMessageType.Synchronous
},
new UmlSequenceMessage()
{
ID = "6",
Content = "Fraud Detected",
FromParticipantID = "FraudDetectionSystem",
ToParticipantID = "User",
MessageType = UmlSequenceMessageType.Reply
},
new UmlSequenceMessage()
{
ID = "7",
Content = "Cancel Transaction",
FromParticipantID = "User",
ToParticipantID = "Transaction",
MessageType = UmlSequenceMessageType.Synchronous
},
new UmlSequenceMessage()
{
ID = "8",
Content = "Complete Transaction",
FromParticipantID = "User",
ToParticipantID = "Transaction",
MessageType = UmlSequenceMessageType.Synchronous
},
},
Fragments = new List<UmlSequenceFragment>()
{
new UmlSequenceFragment()
{
ID = "1",
FragmentType = UmlSequenceFragmentType.Alternative,
Conditions = new List<UmlSequenceFragmentCondition>()
{
new UmlSequenceFragmentCondition()
{
Content = "Fraud Detected",
MessageIds = new List<string>(){"5", "6", "7"},
},
new UmlSequenceFragmentCondition()
{
Content = "No Fraud Detected",
MessageIds = new List<string>(){"8"},
}
}
}
}
};
}
public async Task OnUploadFileSelected(UploadingEventArgs args)
{
if (args.FileData!.Type == "json")
{
IsDiagramLoading = true; newWidth = 0;
string json = await BlazorDemos.Model.FileUtil.LoadFile(jsRuntime!, args.FileData);
json = json.Replace(System.Environment.NewLine, string.Empty);
await Diagram!.LoadDiagramAsync(json.ToString());
await uploadFiles!.ClearAllAsync();
IsDiagramLoading = false;
}
}
public void StateChanged()
{
StateHasChanged();
}
public void Dispose()
{
if (umlModel != null)
{
umlModel = null;
}
if (GridLineIntervals != null)
{
Array.Clear(GridLineIntervals, 0, GridLineIntervals.Length);
}
}
}