Bitzsoft.Integrations.AI.Conversation 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package Bitzsoft.Integrations.AI.Conversation --version 1.0.6
                    
NuGet\Install-Package Bitzsoft.Integrations.AI.Conversation -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.AI.Conversation" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.AI.Conversation" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.AI.Conversation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.AI.Conversation --version 1.0.6
                    
#r "nuget: Bitzsoft.Integrations.AI.Conversation, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.AI.Conversation@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.AI.Conversation&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.AI.Conversation&version=1.0.6
                    
Install as a Cake Tool

Bitzsoft.Integrations.AI.Conversation

面向企业多租户 AI 对话应用的零 ORM 依赖、零业务词汇会话协议与运行时编排包。统一封装富消息分片与确定性 JSON 落盘预算、前后端流式事件帧协议、推理模型采样防踩坑选项工厂、跨任意分块的思考标签拆分与五维多轮 Token 去重累加投影器、两阶段上下文预算器、非阻断数字事实对账器、幂等请求四态台账与会话互斥租约协调器以及无锁 19 位单调递增主键生成器。

版本与兼容性

dotnet add package Bitzsoft.Integrations.AI.Conversation
  • 目标框架:net8.0;net10.0;
  • 项目依赖:Bitzsoft.Integrations.AI.Governance、Bitzsoft.Integrations.AI.Skills;
  • 第三方依赖:仅复用 Microsoft.Extensions.AI.Abstractions、DI、Options、Logging,零新增第三方 NuGet 包;
  • 不依赖 SqlSugar、EntityFrameworkCore、StackExchange.Redis 或 ASP.NET Core Web 框架(HTTP 写出独立位于 Bitzsoft.Integrations.AI.AspNetCore)。

核心模块与设计边界

模块 核心类型 解决的工程问题
A. 富消息分片与落盘预算 AIMessagePart、AIMessagePartTypes、AIMessagePartSerializer 覆盖 text、reasoning、tool_call、tool_result、data、file、approval_required、citation、warning 九类标准分片;向下兼容 BitzOrcas(tool-call/source)与 BitzOrcasVNext(tool_call/citation)历史载荷,按 MaxTotalChars/MaxPartChars/MaxArrayItems 确定性裁剪并保持合法 JSON 且置 Truncated = true
B. 统一流式事件帧 AIStreamFrame、AIStreamFrameTypes 统一 start、reasoning、tool_call、tool_result、data、file、approval_required、citation、warning、message、done、error 十二种 camelCase 事件帧
C. 模型对话选项工厂 AIChatOptionsFactory、AIChatSessionRequest 推理模型(DeepSeek R1、o1/o3/o4-mini、QwQ 等)强制清空 Temperature = null 与 TopP = null,通过 AdditionalProperties["reasoning_effort"] 注入思考强度,并原样保留 AIConcurrentTurnToolCollection 实时列表引用以支持同轮渐进挂载技能工具
D. 流式与非流式投影器 AIChatResponseProjector、AIChatProjectorOptions、AIUsageSnapshot 内置跨任意字符边界 <think>...</think> 状态机(含单字符切块与 EOF 未闭合兜底)、五维多轮去重累加器(Input/Output/Total/Reasoning/CachedInput)、approval_required 回执最高优先级提取、结构化表格/指标/附件帧分流、max_iterations_reached 与 ungrounded_numbers 告警帧注入
E. 两阶段上下文预算器 AIConversationContextBudgeter、DefaultTokenEstimator 区分中日韩宽字符与拉丁代码符号保守估算 Token;固定保护系统提示词、最新用户提问与最近 MinRecentTurnsToKeep 轮,先将较早历史工具大 JSON 压缩为紧凑摘要,仍超预算再整轮淘汰最早历史避免孤立工具调用对
F. 非阻断数字事实对账 AINumericGroundingChecker、AINumericGroundingResult 提取回复中的金额、百分比与四位以上业务数值(自动排除日期、单据编号、条款章节号与 < 10 短数),与当轮业务工具输出执行千分位及 万/亿 单位等价对账,永不阻断输出
G. 幂等请求台账与会话租约 IAIRequestLedgerStore、AIMessageRequestCoordinator 计算长度前缀防碰撞的确定性 sha256: 请求指纹,编排 Acquired / InProgress / CompletedReplay / PayloadConflict 四态状态机、基于 UpdatedAt ?? CreatedAt 心跳的僵尸请求 (Status, Version) 单赢家接管及会话互斥租约
H. 单调递增主键生成器 AISequentialIdGenerator 无锁 CAS 生成定长 19 位十进制正整数文本主键(恒在 [10^18, long.MaxValue] 区间,绝不产出 "0"),消除雪花或随机 ID 跨节点时钟微偏导致的历史消息乱序
I. 千人千面工作台四维上下文联动 IAIWorkbenchSurfaceResolver、DefaultAIWorkbenchSurfaceResolver、AISkillWorkbenchBridge 按「RBAC + 数据分级硬门禁 → 岗位角色蓝图 → 四维上下文联动(Mode × BoundEntity × StateTags × AccessMode)→ 用户个人偏好(置顶/隐藏/排序)」生成千人千面工作台模式与快捷技能胶囊快照,支持 {{EntityId}}/{{DisplayNo}}/{{Title}} 模板渲染与无绑定实体时 WorkspaceFastTrack 自动安全降级
J. 四级对话风格偏好级联 AIPreferenceResolver、AIPreferencePromptBuilder、AIConversationPreferenceProfile 按「租户默认 → 岗位角色蓝图 → 用户个人偏好 → 会话单轮临时覆盖」四级合并详略密度、语气基调、排版结构与引用标注风格,清洗自定义指令并生成确定性中文系统提示词附录

双宿主流式事件帧对照表

统一帧类型 (AIStreamFrameTypes) BitzOrcas 存量前端语义 BitzOrcasVNext 前端语义 关键载荷字段 (camelCase)
start 会话握手 流开始元数据 conversationId, requestId, modelId, replayed
reasoning 深度思考增量 思考链分片 content
tool_call 工具调用开始 工具调用事件 callId, toolName, toolArguments
tool_result 工具返回摘要 工具结果事件 callId, toolName, structuredData, isError
approval_required 高风险写工具待审批卡片 待办确认动作 actionId, toolName, structuredData
data / file / citation 报表表格、指标卡、导出文件、引用来源 结构化视图分片 structuredData, fileId, fileName, mediaType, url, title
warning 非阻断治理提示 运行时告警提示 warningCode, content, structuredData
message 助手可见正文增量 文本增量 content
done 正常收敛帧 结束统计帧 conversationId, requestId, modelId, inputTokenCount, outputTokenCount, totalTokenCount, reasoningTokenCount, cachedInputTokenCount, timeToFirstTokenMs, latencyMs, replayed
error 终端脱敏错误帧 异常终止帧 errorCode, errorMessage, traceId

IAIRequestLedgerStore 双 ORM(SqlSugar / EF Core)实现走查

建议在宿主请求台账表建立 (TenantId, SessionId, RequestId) 唯一索引,并在会话租约表建立 (TenantId, SessionId) 主键或唯一索引(或直接使用 Redis SET NX PX + Lua 释放):

  1. 原子插入或回查(CreateOrGetExistingAsync):直接尝试插入 Version = 1, Status = Pending 新行;若命中唯一索引冲突异常,立即按 (TenantId, SessionId, RequestId) 回查既有行并返回 new AIRequestLedgerCreateResult(CreatedNew: false, existing)。
  2. 双条件乐观并发跃迁(TryTransitionAsync):
    • SqlSugar 宿主:使用 db.CopyNew().Updateable<SysAiRequestLedger>() 并在 WHERE 子句严格限定 TenantId == tenantId && SessionId == sessionId && RequestId == requestId && Status == (int)expectedStatus && Version == expectedVersion,同时将 Version 更新为 expectedVersion + 1、UpdatedAt 更新为 transition.TransitionedAt,仅当受影响行数 == 1 时返回 true;
    • EF Core 宿主:查询侧统一加 .AsNoTracking(),更新侧使用 ExecuteUpdateAsync 在单条 SQL 内匹配 Status == expectedStatus && Version == expectedVersion 并原子递增 Version。
  3. 契约测试基类验收:宿主测试项目直接继承 Bitzsoft.Integrations.AI.Conversation.Testing.AIRequestLedgerStoreContractTestsBase 调用 RunAllContractAssertionsAsync(),一行代码自动验证唯一键幂等、多租户与跨会话隔离、16 路并发抢占单赢家、心跳续期版本保护、会话租约令牌防伪释放与过期自动接管。

完整最小示例

以下示例演示单调 19 位主键生成、推理模型安全选项构建、跨块 <think> 流式投影与数字事实对账以及幂等请求抢占与重放:

using Bitzsoft.Integrations.AI.Conversation;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddBitzsoftAIConversation();
services.AddInMemoryBitzsoftAIConversationStores();

await using var provider = services.BuildServiceProvider(
    new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });

using var scope = provider.CreateScope();
var idGenerator = scope.ServiceProvider.GetRequiredService<AISequentialIdGenerator>();
var coordinator = scope.ServiceProvider.GetRequiredService<IAIMessageRequestCoordinator>();
var projector = scope.ServiceProvider.GetRequiredService<AIChatResponseProjector>();

// 1. 构建推理模型选项:自动清空不兼容的 Temperature / TopP 并注入 reasoning_effort。
ChatOptions chatOptions = AIChatOptionsFactory.Create(new AIChatSessionRequest
{
    ModelId = "deepseek-reasoner",
    IsReasoningModel = true,
    ReasoningEffort = "high",
    Temperature = 0.7f,
    TopP = 0.9f,
});

// 2. 计算确定性请求指纹并声明幂等执行权。
string fingerprint = coordinator.ComputeFingerprint(new AIRequestFingerprintInput(
    TenantId: "tenant-demo",
    UserId: "user-01",
    SessionId: "sess-100",
    Prompt: "汇总本月已审结账单金额"));

var claim = await coordinator.TryClaimOrGetExistingAsync(new AIRequestClaimRequest(
    TenantId: "tenant-demo",
    UserId: "user-01",
    SessionId: "sess-100",
    RequestId: "req-20261107-01",
    RequestFingerprint: fingerprint,
    UserMessageId: idGenerator.NextId()));

if (claim.Outcome == AIRequestClaimOutcome.Acquired && claim.Entry is not null)
{
    // 3. 投影跨分块 <think> 流式更新并执行工具结果数字事实对账。
    static async IAsyncEnumerable<ChatResponseUpdate> MockStreamAsync()
    {
        yield return new ChatResponseUpdate(ChatRole.Assistant, [new FunctionCallContent("call-1", "query_bill_summary")]);
        yield return new ChatResponseUpdate(ChatRole.Tool, [new FunctionResultContent("call-1", """{"totalAmount":1200000}""")]);
        yield return new ChatResponseUpdate(ChatRole.Assistant, "<thi");
        yield return new ChatResponseUpdate(ChatRole.Assistant, "nk>核对账单金额 1200000 元</think>本月已审结总额为 120万元。");
        await Task.CompletedTask;
    }

    await foreach (AIStreamFrame frame in projector.ProjectStreamAsync(
        MockStreamAsync(),
        conversationId: "sess-100",
        requestId: "req-20261107-01",
        modelId: chatOptions.ModelId))
    {
        if (frame.Type == AIStreamFrameTypes.Done)
        {
            string? partsJson = AIMessagePartSerializer.Serialize(projector.Parts);
            await coordinator.MarkCompletedAsync(new AIRequestCompletionUpdate(
                TenantId: "tenant-demo",
                SessionId: "sess-100",
                RequestId: "req-20261107-01",
                ExpectedVersion: claim.Entry.Version,
                AssistantMessageId: idGenerator.NextId(),
                ResponseText: projector.PersistenceContent,
                PartsJson: partsJson,
                Usage: projector.Usage));
        }
    }
}

Console.WriteLine($"正文输出:{projector.AssistantText},思考参数已清理:{chatOptions.Temperature is null}");
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Bitzsoft.Integrations.AI.Conversation:

Package Downloads
Bitzsoft.Integrations.AgentFramework

Microsoft Agent Framework 集成 — Provider-neutral Agent、BitzAgentBuilder、会话持久化桥接、技能与上下文供给、长任务续传、HITL 审批与迭代循环编排

Bitzsoft.Integrations.AI.AspNetCore

ASP.NET Core AI 基础设施包 — 提供零缓冲 SSE/NDJSON 流式写出器(HITL/DryRun/Continuation/FinOps)、源生成 RFC 9457 ProblemDetails 异常映射、HttpContext 身份绑定、多模态与后台任务端点辅助,以及标注 AOT 边界的 MVC 只读工具反射发现与构建器

Bitzsoft.Integrations.AI.Testing

Bitzsoft AI 与 MCP 测试支持包 — 无测试框架锁定的存储端口契约测试基类、进程内测试替身、受控测试时钟与 FDE 黄金评估运行器

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 0 11/14/2026
1.0.8 69 11/12/2026
1.0.7 88 11/10/2026
1.0.6 92 11/7/2026
1.0.5 113 11/7/2026