核心特性
V2 清单解析
解析并加载带 13 个标准错误码的 V2 协议清单。 与服务商无关的配置,零硬编码逻辑。
韧性模式
内置 RetryPolicy、CircuitBreaker、RateLimiter 与 Backpressure。 PreflightChecker 提供统一请求门控。
模型路由
ModelManager 配合 CostBasedSelector、QualityBasedSelector 与 FallbackChain。 按成本与质量智能选型。
多模态支持
SttClient(语音转文字)、TtsClient(文字转语音)、RerankerClient(文档重排)。 完整多模态内容块支持。
MCP 工具桥接
将 MCP 工具桥接到 AI-Protocol 格式。以统一工具调用接口无缝集成 MCP 服务器。
批处理与插件
BatchExecutor 支持可配置并发的并行处理。插件系统通过钩子拦截请求 / 响应。
统一、简洁的 API
同一套代码适用于全部 37+ 服务商。只需更换模型标识符 —the protocol manifest handles everything else.
- 1 使用模型 ID 创建客户端
- 2 用流畅 API 构建聊天请求
- 3 执行或流式获取响应
import { AiClient, Message } from '@ailib-official/ai-lib-ts';
const client = await AiClient.new('openai/gpt-4o');
const response = await client
.chat([
Message.system('You are helpful.'),
Message.user('Hello!'),
])
.temperature(0.7)
.execute();
console.log(response.content); 内部架构
协议驱动架构:类型安全执行、默认可选韧性、完全可扩展。
模块概览
client/ + errors/
AiClient、ChatRequestBuilder、ChatResponse、CallStats、CancelToken、 CancellableStream,以及统一错误码。
protocol/
ProtocolLoader(本地 / fetch / GitHub)、V2 清单定义、JSON Schema 校验器、 与服务商无关的模型类型。
pipeline/
Decoder(SSE、JSON Lines)、Selector(JSONPath)、Accumulator、FanOut、EventMapper (协议驱动)。
resilience/
RetryPolicy、CircuitBreaker、RateLimiter、Backpressure、PreflightChecker、 SignalsSnapshot、FallbackChain。
routing/
ModelManager、QualityBasedSelector、CostBasedSelector、FallbackChain,用于智能选型。
mcp/
McpBridge 与 McpTool 定义,用于无缝集成 Model Context Protocol 服务器。
multimodal/
SttClient、TtsClient、RerankerClient,完整支持多模态输入与专用模型。
plugins/ + batch/
PluginRegistry、Hooks。BatchExecutor 支持带并发上限的批量执行。