# @antseed/api-adapter 中文文档

规范地址：https://npmcn.com/package/%40antseed/api-adapter/

版本：0\.1\.57
npm 元数据更新时间：2026-11-04T06:29:19.766Z
中文译文更新时间：2026-11-06T07:15:10.534Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40antseed/api-adapter
原文对照：https://npmcn.com/package/%40antseed/api-adapter/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：406 kB，51 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- 首次发布：2026-03-14T20:30:03.671Z
- 最新版本发布：2026-11-04T06:29:19.579Z
- 近 30 天下载量：9984（2026-10-06 至 2026-11-04，npm 官方下载统计）
- 已发布版本数：58（近 12 个月 58 个）

## 中文简介

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

## 安装

```sh
npm install @antseed/api-adapter
```

也可以使用：`pnpm add @antseed/api-adapter`、`yarn add @antseed/api-adapter`、`bun add @antseed/api-adapter`

## README

# @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
```

## 使用量

### 检测来件请求的内容

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

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

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

```ts
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 }
```

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

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

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

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

### 逐步调整流式响应

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

```ts
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 参考

### 协议检测和路由

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

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

### 请求

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

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

### 回应

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

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

### 流线

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

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

### 类型

```ts
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 是什么？

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 和提交记录判断。

