@antseed/api-adapter

中文文档
0.1.57发布于 2026/11/04TypeScriptES Module近 30 天下载 9984

LLM API 协议之间的 HTTP 级格式翻译( Anthropic Messages, OpenAI 聊天补全, OpenAI 响应)

中文使用文档
文档目录15 节
  1. 支持的议定书
  2. 变形矩阵
  3. 使用量
  4. 检测来件请求的内容
  5. 选择提供者支持的目标协议
  6. 在向提供者转发前转换请求
  7. 将非流式响应转换回原协议
  8. 逐步调整流式响应
  9. API 参考
  10. 协议检测和路由
  11. 请求
  12. 回应
  13. 流线
  14. 类型
  15. 文件结构

@antseed/ api-适应器

HTTP级格式翻译介于LLM API协议之间. 在Anthropic Messages,OpenAI Chat Professions,和OpenAI Response之间转换请求和响应——没有任何网络呼叫.

支持的议定书

议定书标识符
Anthropic 信件anthropic-messages
OpenAI 聊天补全openai-chat-completions
OpenAI 响应openai-responses
OpenAI 补全( 遗产)openai-completions

变形矩阵

请求转换使用一个共享的小型内立体请求模式 字段、信息、工具和工具结果实现一次正常化并提供给 目标协议。 反应成功有效载荷使用相同的犬类反应 如果可能的话,形状。 流出适配器使用相同的枢纽模式 协议特定的流常态器和渲染器.

anthropic-messages  ⟷  openai-chat-completions
openai-responses    ⟷  openai-chat-completions
anthropic-messages  ⟷  openai-responses

使用量

检测来件请求的内容

import { detectRequestServiceApiProtocol } from '@antseed/api-adapter';

const protocol = detectRequestServiceApiProtocol(request);
// → 'anthropic-messages' | 'openai-chat-completions' | 'openai-responses' | null

选择提供者支持的目标协议

import { selectTargetProtocolForRequest } from '@antseed/api-adapter';

const selection = selectTargetProtocolForRequest('anthropic-messages', ['openai-chat-completions']);
// → { targetProtocol: 'openai-chat-completions', requiresTransform: true }

const passthrough = selectTargetProtocolForRequest('anthropic-messages', ['anthropic-messages']);
// → { targetProtocol: 'anthropic-messages', requiresTransform: false }

const reverse = selectTargetProtocolForRequest('openai-responses', ['anthropic-messages']);
// → { targetProtocol: 'anthropic-messages', requiresTransform: true }

在向提供者转发前转换请求

import {
  transformRequest,
} from '@antseed/api-adapter';

// Buyer sent an Anthropic request; provider only speaks OpenAI Chat
const result = transformRequest(incomingRequest, {
  from: 'anthropic-messages',
  to: 'openai-chat-completions',
});
if (result) {
  const { request, streamRequested, requestedModel } = result;
  // forward `request` to the provider
}

// Buyer sent an Anthropic request; provider only speaks OpenAI Responses
const responsesResult = transformRequest(incomingRequest, {
  from: 'anthropic-messages',
  to: 'openai-responses',
});

将非流式响应转换回原协议

import {
  transformResponse,
} from '@antseed/api-adapter';

// Provider returned an OpenAI Chat response; buyer expects Anthropic
const adapted = transformResponse(providerResponse, {
  from: 'openai-chat-completions',
  to: 'anthropic-messages',
  streamRequested: false,
  fallbackModel: 'claude-sonnet',
});

// Provider returned an OpenAI Responses response; buyer expects Anthropic
const adaptedFromResponses = transformResponse(providerResponse, {
  from: 'openai-responses',
  to: 'anthropic-messages',
  streamRequested: false,
  fallbackModel: 'claude-sonnet',
});

逐步调整流式响应

对于流出响应,每个请求创建一次适配器,并在它们到达时进行分块喂食.

import { createStreamingAdapter } from '@antseed/api-adapter';

const adapter = createStreamingAdapter({
  from: 'openai-chat-completions',
  to: 'anthropic-messages',
  fallbackModel: 'claude-sonnet',
});
if (!adapter) throw new Error('Unsupported stream transform');

// On first response headers:
const startResponse = adapter.adaptStart(providerResponse);
// startResponse.headers['content-type'] === 'text/event-stream'

// On each incoming SSE chunk:
const outChunks = adapter.adaptChunk(incomingChunk);
// outChunks is an array of SerializedHttpResponseChunk in Anthropic SSE format

相同的模式适用于Anthropic之间任何支持的流变 消息, OpenAI 聊天补全, 和 OpenAI 响应.

API 参考

协议检测和路由

detectRequestServiceApiProtocol(request): ServiceApiProtocol | null
inferProviderDefaultServiceApiProtocols(providerName): ServiceApiProtocol[]
selectTargetProtocolForRequest(requestProtocol, supportedProtocols): TargetProtocolSelection | null

inferProviderDefaultServiceApiProtocols 知名供应商名称地图('anthropic', (中文(简体) ), 'claude-code', (中文(简体) ), 'claude-oauth', (中文(简体) ), 'openai', (中文(简体) ), 'local-llm') 用于它们的默认协议.

请求

transformRequest(request, { from, to }): ServiceApiRequestTransformResult | null

from 财务报告和审定财务报表 to 当前支持 anthropic-messages, (中文(简体) ), openai-chat-completions,以及 openai-responses返回不支持的协议 null。 。 。 .

回应

transformResponse(response, { from, to, streamRequested, fallbackModel }): SerializedHttpResponse | null

from 财务报告和审定财务报表 to 当前支持 anthropic-messages, (中文(简体) ), openai-chat-completions,以及 openai-responses返回不支持的协议 null时 streamRequested 以目标规范SSE的形式提供成功的非流源响应.

流线

createStreamingAdapter({ from, to, fallbackModel }): StreamingResponseAdapter | null

返回用于流化响应块的每个请求适配器,或 null 用于不支持或同为protocol路径.

类型

interface SerializedHttpRequest {
  requestId: string;
  method: string;
  path: string;
  headers: Record<string, string>;
  body: Uint8Array;
}

interface SerializedHttpResponse {
  requestId: string;
  statusCode: number;
  headers: Record<string, string>;
  body: Uint8Array;
}

interface SerializedHttpResponseChunk {
  requestId: string;
  data: Uint8Array;
  done: boolean;
}

interface StreamingResponseAdapter {
  adaptStart(response: SerializedHttpResponse): SerializedHttpResponse;
  adaptChunk(chunk: SerializedHttpResponseChunk): SerializedHttpResponseChunk[];
}

文件结构

src/
  canonical.ts        Internal normalized request/response shape + protocol renderers
  request-transform.ts Generic request transform entry point
  utils.ts            Shared helpers: encode/decode, SSE parsing, toStringContent
  detect.ts           Protocol detection and target selection
  anthropic.ts        Anthropic Messages ↔ OpenAI Chat transforms + streaming adapter
  openai-responses.ts OpenAI Responses ↔ OpenAI Chat transforms + streaming adapter
  types.ts            Shared types (SerializedHttpRequest/Response, ServiceApiProtocol)
  index.ts            Public re-exports

版本与发布记录

@antseed/api-adapter 共发布 58 个版本,首次发布于 2026/03/15;过去 12 个月发布了 58 个版本。

版本发布日期距今
0.1.57latest2026/11/042 天前
0.1.562026/11/042 天前
0.1.552026/10/306 天前
0.1.542026/10/307 天前
0.1.532026/10/1621 天前
0.1.522026/10/1621 天前

关于 @antseed/api-adapter 的常见问题

@antseed/api-adapter 是什么?

LLM API 协议之间的 HTTP 级格式翻译( Anthropic Messages, OpenAI 聊天补全, OpenAI 响应)

如何安装 @antseed/api-adapter?

在项目目录运行 npm install @antseed/api-adapter,或使用 pnpm add / yarn add / bun add。

@antseed/api-adapter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@antseed/api-adapter 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@antseed/api-adapter 可以商用吗?

@antseed/api-adapter 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@antseed/api-adapter 的下载量有多少?

根据 npm 官方下载统计,@antseed/api-adapter 在 2026/10/06 – 2026/11/04 这 30 天内被下载 9,984 次,日均约 333 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@antseed/api-adapter 还在维护吗?

最新版本 0.1.57 发布于 2026/11/04(2 天前);至今共发布 58 个版本,其中过去 12 个月发布了 58 个;首次发布于 2026/03/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。