namespace OASystem.API.OAMethodLib.TokenHubAI.Models;
///
/// 流式响应片段
///
public class StreamChunk
{
// ============ 内容 ============
///
/// 内容片段
///
public string? Content { get; set; }
///
/// 增量消息对象
///
public ChatMessage? Delta { get; set; }
// ============ 状态 ============
///
/// 是否结束
///
public bool IsDone { get; set; }
///
/// 完成原因
///
///
/// - stop:正常结束
/// - length:达到最大长度
/// - content_filter:内容被过滤
/// - tool_calls:工具调用
///
public string? FinishReason { get; set; }
///
/// 使用的模型
///
public string? Model { get; set; }
///
/// 响应 ID
///
public string? Id { get; set; }
///
/// 索引
///
public int Index { get; set; }
// ============ 工具调用 ============
///
/// 工具调用列表(流式)
///
public List? ToolCalls { get; set; }
// ============ 统计 ============
///
/// Token 使用统计(仅在结束时返回)
///
public TokenUsage? Usage { get; set; }
// ============ 工厂方法 ============
///
/// 创建内容片段
///
public static StreamChunk ContentChunk(string content)
{
return new StreamChunk
{
Content = content,
IsDone = false
};
}
///
/// 创建结束标记
///
public static StreamChunk Done(string? finishReason = "stop")
{
return new StreamChunk
{
IsDone = true,
FinishReason = finishReason
};
}
///
/// 创建错误片段
///
public static StreamChunk Error(string errorMessage)
{
return new StreamChunk
{
IsDone = true,
Content = $"[错误: {errorMessage}]"
};
}
///
/// 判断是否包含内容
///
public bool HasContent => !string.IsNullOrEmpty(Content) || Delta?.Content != null;
///
/// 获取完整内容(累积)
///
public string GetFullContent()
{
if (!string.IsNullOrEmpty(Content))
return Content;
return Delta?.Content ?? string.Empty;
}
///
/// 判断是否正常结束
///
public bool IsNormalEnd => IsDone && FinishReason is "stop" or null;
}