-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSSEMessage.cs
More file actions
269 lines (234 loc) · 6 KB
/
SSEMessage.cs
File metadata and controls
269 lines (234 loc) · 6 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
using System.Text.Json.Serialization;
namespace SQLAgent.Hosting.Dto;
// ============================================
// SSE 消息类型定义(新架构:类似 ChatGPT/Claude)
// ============================================
/// <summary>
/// SSE 事件类型
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum SSEEventType
{
delta, // 增量文本(流式输出)
block, // 内容块(SQL、数据、图表等)
done, // 完成
error // 错误
}
/// <summary>
/// SSE 消息基类
/// </summary>
public abstract class SSEMessage
{
/// <summary>
/// 事件类型
/// </summary>
[JsonPropertyName("type")]
public abstract SSEEventType Type { get; }
}
/// <summary>
/// 增量文本消息(流式文本输出)
/// </summary>
public class DeltaMessage : SSEMessage
{
[JsonPropertyName("type")]
public override SSEEventType Type => SSEEventType.delta;
/// <summary>
/// 增量文本内容
/// </summary>
[JsonPropertyName("delta")]
public string Delta { get; set; } = string.Empty;
}
/// <summary>
/// 内容块消息
/// </summary>
public class BlockMessage : SSEMessage
{
[JsonPropertyName("type")]
public override SSEEventType Type => SSEEventType.block;
/// <summary>
/// 内容块
/// </summary>
[JsonPropertyName("block")]
public ContentBlock Block { get; set; } = new SqlBlock();
}
/// <summary>
/// 错误消息
/// </summary>
public class ErrorMessage : SSEMessage
{
[JsonPropertyName("type")]
public override SSEEventType Type => SSEEventType.error;
/// <summary>
/// 错误代码
/// </summary>
[JsonPropertyName("code")]
public string Code { get; set; } = string.Empty;
/// <summary>
/// 错误消息
/// </summary>
[JsonPropertyName("message")]
public string Message { get; set; } = string.Empty;
/// <summary>
/// 详细信息
/// </summary>
[JsonPropertyName("details")]
public string? Details { get; set; }
}
/// <summary>
/// 完成消息
/// </summary>
public class DoneMessage : SSEMessage
{
[JsonPropertyName("type")]
public override SSEEventType Type => SSEEventType.done;
/// <summary>
/// 执行耗时(毫秒)
/// </summary>
[JsonPropertyName("elapsedMs")]
public long ElapsedMs { get; set; }
}
// ============================================
// 内容块类型定义
// ============================================
/// <summary>
/// 内容块类型
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ContentBlockType
{
Sql, // SQL 代码
Data, // 数据表格
Chart, // 图表
Error // 错误
}
/// <summary>
/// 内容块基类
/// </summary>
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(SqlBlock), "sql")]
[JsonDerivedType(typeof(DataBlock), "data")]
[JsonDerivedType(typeof(ChartBlock), "chart")]
[JsonDerivedType(typeof(ErrorBlock), "error")]
public abstract class ContentBlock
{
/// <summary>
/// 块ID
/// </summary>
[JsonPropertyName("id")]
public string Id { get; set; } = Guid.NewGuid().ToString();
}
/// <summary>
/// SQL 代码块
/// </summary>
public class SqlBlock : ContentBlock
{
/// <summary>
/// SQL 语句
/// </summary>
[JsonPropertyName("sql")]
public string Sql { get; set; } = string.Empty;
/// <summary>
/// 涉及的表
/// </summary>
[JsonPropertyName("tables")]
public string[] Tables { get; set; } = Array.Empty<string>();
/// <summary>
/// SQL 方言
/// </summary>
[JsonPropertyName("dialect")]
public string? Dialect { get; set; }
}
/// <summary>
/// 数据表格块
/// </summary>
public class DataBlock : ContentBlock
{
/// <summary>
/// 列名
/// </summary>
[JsonPropertyName("columns")]
public string[] Columns { get; set; } = Array.Empty<string>();
/// <summary>
/// 数据行
/// </summary>
[JsonPropertyName("rows")]
public object[][] Rows { get; set; } = Array.Empty<object[]>();
/// <summary>
/// 总行数
/// </summary>
[JsonPropertyName("totalRows")]
public int TotalRows { get; set; }
}
/// <summary>
/// 图表块
/// </summary>
public class ChartBlock : ContentBlock
{
/// <summary>
/// 图表类型
/// </summary>
[JsonPropertyName("chartType")]
public string ChartType { get; set; } = "bar";
/// <summary>
/// ECharts option 配置 JSON 字符串
/// </summary>
[JsonPropertyName("echartsOption")]
public string? EchartsOption { get; set; }
/// <summary>
/// 图表配置(兼容旧版)
/// </summary>
[JsonPropertyName("config")]
public ChartConfig Config { get; set; } = new();
/// <summary>
/// 图表数据(兼容旧版)
/// </summary>
[JsonPropertyName("data")]
public object Data { get; set; } = new { };
}
/// <summary>
/// 图表配置
/// </summary>
public class ChartConfig
{
/// <summary>
/// X轴字段
/// </summary>
[JsonPropertyName("xAxis")]
public string? XAxis { get; set; }
/// <summary>
/// Y轴字段
/// </summary>
[JsonPropertyName("yAxis")]
public string[]? YAxis { get; set; }
/// <summary>
/// 标题
/// </summary>
[JsonPropertyName("title")]
public string? Title { get; set; }
/// <summary>
/// 显示图例
/// </summary>
[JsonPropertyName("showLegend")]
public bool ShowLegend { get; set; } = true;
}
/// <summary>
/// 错误块
/// </summary>
public class ErrorBlock : ContentBlock
{
/// <summary>
/// 错误代码
/// </summary>
[JsonPropertyName("code")]
public string Code { get; set; } = string.Empty;
/// <summary>
/// 错误消息
/// </summary>
[JsonPropertyName("message")]
public string Message { get; set; } = string.Empty;
/// <summary>
/// 详细信息
/// </summary>
[JsonPropertyName("details")]
public string? Details { get; set; }
}