@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