Skip to content
2026-09-29 04:20610 字AIOpenClawLLM流式接口提供商

LLM Client 大语言模型客户端 ​

源码路径:src/llm/(~40 文件)

LLM Client 是 OpenClaw 的 AI 模型接入层。将 Anthropic、OpenAI、Google 等提供商统一到一致的 StreamFunction 接口。

通俗类比:LLM Stream 是「电话线」 ​

OpenClaw 支持 40+ 个 AI 模型提供商,每个 API 格式不同。LLM Stream 层通过统一接口 + 多 Provider 适配,让上层代码只写一次就能和所有模型通信。

关键:返回 AsyncIterable<AssistantMessageEvent>——不是等 AI 全部回复完再返回,而是边生成边返回(流式)。

核心职责 ​

职责说明
API 统一抽象KnownApi 枚举统一 8 种 API 家族
模型注册表管理所有可用模型,支持认证状态检测
流式协议AssistantMessageEventStream 标准化事件流
使用统计统一 Usage 统计(token 数 + 成本)
提供者注册懒加载注册内置提供者
OAuth 支持OAuth 认证流程支持

KnownApi 枚举 ​

typescript
type KnownApi =
  | "openai-completions"
  | "mistral-conversations"
  | "openai-responses"
  | "azure-openai-responses"
  | "openai-chatgpt-responses"
  | "anthropic-messages"
  | "bedrock-converse-stream"
  | "google-generative-ai"
  | "google-vertex";

流式事件协议 ​

typescript
type AssistantMessageEvent =
  | { type: "start"; partial: AssistantMessage }
  | { type: "text_delta"; contentIndex: number; delta: string }
  | { type: "toolcall_start"; contentIndex: number }
  | { type: "done"; reason: StopReason; message: AssistantMessage }
  | { type: "error"; reason: StopReason; error: AssistantMessage };

事件流设计的好处:

  • 用户实时看到 AI 在打字
  • 工具调用可以边生成边执行

使用统计 ​

typescript
interface Usage {
  input: number;      // 输入 token
  output: number;     // 输出 token
  cacheRead: number;  // 缓存读取
  cacheWrite: number; // 缓存写入
  cost: { input; output; cacheRead; cacheWrite; total };
}

内置提供者 ​

提供商API 家族
Anthropicanthropic-messages
OpenAI Completionsopenai-completions
OpenAI Responsesopenai-responses
Azure OpenAIazure-openai-responses
Googlegoogle-generative-ai
Google Vertexgoogle-vertex
Mistralmistral-conversations
Cloudflare—

懒加载注册 ​

typescript
// register-builtins.ts
{
  "anthropic-messages": () => import("./anthropic.js"),
  "openai-completions": () => import("./openai-completions.js"),
  // ... 首次调用时才加载
}

每个提供者导出 stream() 和 streamSimple() 函数。

流包装器 ​

包装器用途
anthropic-cache-control-payload.tsAnthropic 缓存控制
google.tsGoogle 流适配
openai.tsOpenAI 流适配
reasoning-effort-utils.ts推理力度工具

设计模式 ​

模式应用
策略模式每个 API 家族对应一个 stream 策略
注册表模式ModelRegistry / ApiRegistry
懒加载模式提供者模块延迟加载
事件流AssistantMessageEventStream 标准化事件
适配器stream-wrappers 适配各提供者差异

依赖关系 ​

  • 上游:Agent Engine(chatCompletion 调用)
  • 下游:40+ 模型提供商 API
  • 横切:Secrets(API Key 管理)、Model Catalog(模型目录)

每一篇文章,都是时间的标本