ai-protocol-adapters 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 AI Protocol Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # @ai-protocol/adapters
2
+
3
+ [![npm version](https://badge.fury.io/js/@ai-protocol%2Fadapters.svg)](https://badge.fury.io/js/@ai-protocol%2Fadapters)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)
6
+
7
+ > 🔄 **Universal AI Protocol Converter** - 在 OpenAI 和 Anthropic API 格式之间进行无缝转换,支持流式响应和完整的 TypeScript 类型安全。
8
+
9
+ ## ✨ 特性
10
+
11
+ - 🔄 **双向转换**: OpenAI ↔ Anthropic 协议完全兼容
12
+ - 🌊 **流式支持**: 完整的 SSE (Server-Sent Events) 流式响应处理
13
+ - 🧠 **思维链支持**: 专门优化处理 GLM-4.5 等思维链模型的 thinking + content 双模式
14
+ - 🛡️ **类型安全**: 100% TypeScript 覆盖,完整的类型定义
15
+ - 🚀 **零配置**: 开箱即用,无需复杂配置
16
+ - 🔧 **高度可移植**: 适用于 Node.js 和现代浏览器环境
17
+ - 📦 **轻量化**: < 500KB 未压缩,支持 Tree-shaking
18
+
19
+ ## 📦 安装
20
+
21
+ ```bash
22
+ npm install @ai-protocol/adapters
23
+ ```
24
+
25
+ ```bash
26
+ yarn add @ai-protocol/adapters
27
+ ```
28
+
29
+ ```bash
30
+ pnpm add @ai-protocol/adapters
31
+ ```
32
+
33
+ ## 🚀 快速开始
34
+
35
+ ### 基础使用
36
+
37
+ ```typescript
38
+ import { StreamingProtocolAdapter } from '@ai-protocol/adapters'
39
+
40
+ // 创建适配器实例
41
+ const adapter = new StreamingProtocolAdapter({
42
+ debugMode: true, // 开发时启用调试模式
43
+ validateInput: true // 启用输入验证
44
+ })
45
+
46
+ // Anthropic 格式请求
47
+ const anthropicRequest = {
48
+ model: 'claude-3-sonnet-20240229',
49
+ messages: [
50
+ { role: 'user', content: 'Hello, how are you?' }
51
+ ],
52
+ stream: true,
53
+ max_tokens: 100
54
+ }
55
+
56
+ // 转换为 OpenAI 格式
57
+ const { openaiRequest, metadata } = adapter.convertAnthropicToOpenAI(anthropicRequest)
58
+
59
+ console.log('OpenAI格式:', openaiRequest)
60
+ console.log('元数据:', metadata) // { hasImages: false, requiresVisionHeaders: false }
61
+ ```
62
+
63
+ ### 流式响应转换
64
+
65
+ ```typescript
66
+ // 假设你有 OpenAI 的流式响应数据
67
+ const openaiStreamData = `data: {"choices":[{"delta":{"content":"Hello"}}]}
68
+ data: {"choices":[{"delta":{"content":" world"}}]}
69
+ data: [DONE]`
70
+
71
+ // 转换为 Anthropic SSE 格式
72
+ const result = adapter.convertOpenAIStreamToAnthropic(openaiStreamData, anthropicRequest)
73
+
74
+ if (result.success) {
75
+ console.log('Anthropic SSE:', result.anthropicSSE)
76
+ console.log('标准格式:', result.anthropicStandardResponse)
77
+ } else {
78
+ console.error('转换失败:', result.error)
79
+ }
80
+ ```
81
+
82
+ ### 支持思维链模型 (GLM-4.5)
83
+
84
+ ```typescript
85
+ const glmRequest = {
86
+ model: 'glm-4.5',
87
+ messages: [
88
+ { role: 'user', content: '请解释量子计算的基本原理' }
89
+ ],
90
+ stream: true,
91
+ max_tokens: 500
92
+ }
93
+
94
+ const { openaiRequest } = adapter.convertAnthropicToOpenAI(glmRequest)
95
+
96
+ // 适配器会自动处理 thinking + content 双模式:
97
+ // 1. reasoning_content (思维过程) -> <thinking>...</thinking>
98
+ // 2. content (最终回答) -> 独立的内容块
99
+ ```
100
+
101
+ ## 🔧 高级配置
102
+
103
+ ### 自定义日志器
104
+
105
+ ```typescript
106
+ import { setGlobalLogger, StreamingProtocolAdapter } from '@ai-protocol/adapters'
107
+
108
+ // 使用自定义日志器
109
+ setGlobalLogger({
110
+ info: (msg, ...args) => console.log(`[INFO] ${msg}`, ...args),
111
+ warn: (msg, ...args) => console.warn(`[WARN] ${msg}`, ...args),
112
+ error: (msg, ...args) => console.error(`[ERROR] ${msg}`, ...args),
113
+ debug: (msg, ...args) => console.debug(`[DEBUG] ${msg}`, ...args)
114
+ })
115
+
116
+ const adapter = new StreamingProtocolAdapter({
117
+ debugMode: true,
118
+ validateInput: true,
119
+ autoHeal: true // 启用自动修复功能
120
+ })
121
+ ```
122
+
123
+ ### 完整配置选项
124
+
125
+ ```typescript
126
+ const adapter = new StreamingProtocolAdapter({
127
+ debugMode: false, // 调试模式
128
+ validateInput: false, // 输入验证
129
+ validateOutput: false, // 输出验证
130
+ autoHeal: false, // 自动修复
131
+ timeout: 30000, // 超时时间 (ms)
132
+ retries: 3, // 重试次数
133
+ bufferSize: 1024, // 缓冲区大小
134
+ logger: customLogger // 自定义日志器
135
+ })
136
+ ```
137
+
138
+ ## 🔍 支持的模型
139
+
140
+ ### OpenAI 兼容模型
141
+ - GPT-4, GPT-3.5-turbo 等官方模型
142
+ - GLM-4.5 (支持思维链)
143
+ - Kimi-K2
144
+ - DeepSeek 系列
145
+ - Qwen 系列
146
+
147
+ ### Anthropic 模型
148
+ - Claude-3 系列 (Opus, Sonnet, Haiku)
149
+ - Claude-3.5 系列
150
+
151
+ ## 📊 性能特性
152
+
153
+ - **转换延迟**: < 5ms (单个消息)
154
+ - **内存占用**: ~2MB (运行时状态)
155
+ - **流式吞吐**: >1000 events/秒
156
+ - **包大小**: ~400KB (未压缩), ~80KB (Gzip)
157
+
158
+ ## 🛠️ 开发
159
+
160
+ ```bash
161
+ # 克隆仓库
162
+ git clone https://github.com/your-org/ai-protocol-adapters
163
+ cd ai-protocol-adapters
164
+
165
+ # 安装依赖
166
+ npm install
167
+
168
+ # 构建
169
+ npm run build
170
+
171
+ # 运行测试
172
+ npm test
173
+
174
+ # 监视模式开发
175
+ npm run dev
176
+ ```
177
+
178
+ ## 🔗 相关项目
179
+
180
+ 基于此项目的协议适配器被成功应用于:
181
+ - [copilot-api](https://github.com/your-org/copilot-api) - AI API 统一网关
182
+ - 更多集成项目...
183
+
184
+ ## 📄 许可证
185
+
186
+ MIT License - 详见 [LICENSE](LICENSE) 文件
187
+
188
+ ## 🤝 贡献
189
+
190
+ 欢迎贡献代码!请阅读我们的贡献指南:
191
+
192
+ 1. Fork 本项目
193
+ 2. 创建特性分支 (`git checkout -b feature/amazing-feature`)
194
+ 3. 提交更改 (`git commit -m 'Add some amazing feature'`)
195
+ 4. 推送到分支 (`git push origin feature/amazing-feature`)
196
+ 5. 开启 Pull Request
197
+
198
+ ## 🆘 支持
199
+
200
+ - 📖 [文档](https://docs.example.com/ai-protocol-adapters)
201
+ - 🐛 [Issues](https://github.com/your-org/ai-protocol-adapters/issues)
202
+ - 💬 [Discussions](https://github.com/your-org/ai-protocol-adapters/discussions)
203
+
204
+ ---
205
+
206
+ <div align="center">
207
+ Made with ❤️ by the AI Protocol Team
208
+ </div>