@antseed/api-adapter

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

HTTP-level format translation between LLM API protocols (Anthropic Messages, OpenAI Chat Completions, OpenAI Responses)

原始使用文档
文档目录15 节
  1. Supported Protocols
  2. Transform Matrix
  3. Usage
  4. Detect what protocol an incoming request speaks
  5. Select a target protocol given what a provider supports
  6. Transform a request before forwarding to a provider
  7. Transform a non-streaming response back to the original pro…
  8. Adapt a streaming response incrementally
  9. API Reference
  10. Protocol detection & routing
  11. Requests
  12. Responses
  13. Streaming
  14. Types
  15. File Structure

@antseed/api-adapter

HTTP-level format translation between LLM API protocols. Converts requests and responses between Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses — without any network calls.

Supported Protocols

ProtocolIdentifier
Anthropic Messagesanthropic-messages
OpenAI Chat Completionsopenai-chat-completions
OpenAI Responsesopenai-responses
OpenAI Completions (legacy)openai-completions

Transform Matrix

Request transforms use a small internal canonical request model so shared fields, messages, tools, and tool results are normalized once and rendered to the target protocol. Response success payloads use the same canonical response shape where possible. Streaming adapters use the same hub pattern with protocol-specific stream normalizers and renderers.

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

Usage

Detect what protocol an incoming request speaks

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

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

Select a target protocol given what a provider supports

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 }

Transform a request before forwarding to a provider

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',
});

Transform a non-streaming response back to the original protocol

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',
});

Adapt a streaming response incrementally

For streaming responses, create an adapter once per request and feed it chunks as they arrive.

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

The same pattern applies for any supported stream transform between Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses.

API Reference

Protocol detection & routing

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

inferProviderDefaultServiceApiProtocols maps well-known provider names ('anthropic', 'claude-code', 'claude-oauth', 'openai', 'local-llm') to their default protocols.

Requests

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

from and to currently support anthropic-messages, openai-chat-completions, and openai-responses. Unsupported protocols return null.

Responses

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

from and to currently support anthropic-messages, openai-chat-completions, and openai-responses. Unsupported protocols return null. When streamRequested is true, a successful non-stream provider response is rendered as target-protocol SSE.

Streaming

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

Returns a per-request adapter for streaming response chunks, or null for unsupported or same-protocol paths.

Types

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[];
}

File Structure

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 是什么?

HTTP-level format translation between LLM API protocols (Anthropic Messages, OpenAI Chat Completions, OpenAI Responses)

如何安装 @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 和提交记录判断。