System Full View
系统全貌:从 UE5 到 Core
LiteRT-LM 的价值不在于单一的推理,而在于这套针对虚幻引擎长对话场景深度优化的增量架构。
三层渐进式架构 (Progressive Architecture)
graph TD
subgraph UE5[虚幻业务层 - UmgMcp Provider]
Agent[Agent Object]
Tools[Tool Definitions]
end
subgraph UnrealApi[集成抽象层 - LiteRtLmUnrealApi]
Subsystem[Global Subsystem]
Sync[Incremental Sync Engine]
Norm[Role Normalizer]
end
subgraph DLL[隔离驱动层 - litert_lm_wrapper.dll]
ABI[ABI Firewall]
Worker[Background Inference Thread]
end
subgraph Core[推理核心 - LiteRT Core]
GPU[GPU Inference]
KV[Persistent KV Cache]
end
Agent --> Subsystem
Subsystem --> Sync
Sync --> Norm
Norm --> ABI
ABI --> Worker
Worker --> GPU
GPU --- KV
style DLL fill:#1E293B,stroke:#3B82F6,stroke-width:2px
style Core fill:#000,stroke:#8B5CF6,stroke-width:2px
业务集成
处理 FabServer 消息模型,将游戏内的 NPC 逻辑映射为 AI 请求。
集成抽象
实现增量消息同步。维护 Session 与 Agent 的一对一持久映射关系。
物理隔离
通过 ABI 防火墙彻底解决符号冲突。后台线程驱动 WaitUntilDone 心跳。
增量同步模型 (Incremental Sync)
sequenceDiagram
participant UE as UE5 Subsystem
participant DLL as Wrapper Session
Note over UE,DLL: Round 1: 初次对话
UE->>DLL: Append(Msg 1-3)
UE->>DLL: RunInference()
Note right of DLL: 显存存入 KV Cache
Note over UE,DLL: Round 2: 追问
UE->>UE: 检测已同步 = 3
UE->>DLL: 仅同步新消息 (Msg 4)
UE->>DLL: RunInference()
Note right of DLL: 命中缓存,$O(\Delta N)$ 计算
物理 KV Cache 持久性
在 LiteRtLmUnrealApi.cpp 中,系统通过 SessionMsgCountMap 精确跟踪每个 Agent 已同步的消息索引。切换 Agent 就像在 GPU 显存中切换指针,实现了零开销的多 Agent 交互。
"这是将首字延迟 (TTFT) 从秒级降低到毫秒级的核心黑科技。"
角色归一化逻辑 (Normalization)
由于底层模型(如 Gemma-2)不原生支持 system 或 tool 角色,UnrealApi 层执行了复杂的协议降级:
System Message
Merged into User
Tool Result
Structured User Msg
代码片段 (Normalization logic)
if (Role == "tool") {
NewMsg->SetStringField("role", "user");
NewMsg->SetStringField("content",
"[Tool Result] (id: " + id + ")\n" + Content);
}
线程协同序列 (Inference Driving)
sequenceDiagram
participant GT as Game Thread (UE5)
participant BT as Background Worker
participant DLL as Wrapper DLL (C)
participant GPU as GPU Queue
GT->>BT: AsyncTask: RunInference
activate BT
BT->>DLL: LiteRtLm_RunInference()
DLL->>GPU: Submit Task
loop Driving Loop
BT->>DLL: WaitUntilDone(blocking)
GPU-->>DLL: Output Token
DLL-->>GT: Callback (AsyncTask/GameThread)
GT->>GT: Update STextBlock UI
end
BT->>GT: Signal Completion
deactivate BT
GameThread: UI & Logic
Worker: WaitUntilDone (Engine Heartbeat)
GPU: Heavy Lifting