@classicicn/codex-transfer 0.2.0

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) 2026 cbq
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,205 @@
1
+ # codex-transfer
2
+
3
+ Responses API ↔ Chat Completions translation bridge for Codex CLI (TypeScript implementation)
4
+
5
+ ## Overview
6
+
7
+ A lightweight proxy that translates the OpenAI **Responses API** (used by Codex CLI) into the **Chat Completions API**, letting Codex work with any OpenAI-compatible provider — DeepSeek, Kimi, Qwen, Mistral, Groq, xAI, OpenRouter, and more.
8
+
9
+ ```
10
+ Codex CLI (Responses API) → codex-transfer → DeepSeek (Chat Completions API)
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```bash
16
+ # Install from npm
17
+ npm install -g @classicicn/codex-transfer
18
+
19
+ # Run
20
+ codex-transfer -k
21
+ ```
22
+
23
+ ## CLI Options
24
+
25
+ ```
26
+ codex-transfer [options]
27
+
28
+ Options:
29
+ -p, --port PORT Listen port (default: 4444)
30
+ -u, --upstream URL Upstream Chat Completions base URL
31
+ --api-key KEY API key for upstream
32
+ -m, --model MODEL Force override model name (highest priority)
33
+ -c, --config PATH Path to config file (JSON)
34
+ -k, --insecure Skip TLS certificate verification
35
+ -d, --daemon Run in background, logs to logs/ directory
36
+ -h, --help Show this help
37
+ ```
38
+
39
+ ## Configuration
40
+
41
+ Priority: CLI args > environment variables > config file > defaults
42
+
43
+ ### Config File
44
+
45
+ Create a JSON config file at one of these locations:
46
+ - `./codex-transfer.json` (current directory)
47
+ - `~/.codex-transfer/config.json` (user home)
48
+ - Custom path via `--config` or `CODEX_TRANSFER_CONFIG`
49
+
50
+ ```json
51
+ {
52
+ "port": 4446,
53
+ "upstream": "https://api.deepseek.com/v1",
54
+ "apiKey": "sk-your-key-here",
55
+ "insecure": false,
56
+ "modelMap": {
57
+ "*": "deepseek-v4-pro"
58
+ }
59
+ }
60
+ ```
61
+
62
+ ### Model Name Mapping
63
+
64
+ Codex CLI may send non-standard model names (e.g. `codex-auto-review`) that the upstream provider doesn't recognize. Use `modelMap` to translate them:
65
+
66
+ ```json
67
+ {
68
+ "modelMap": {
69
+ "*": "deepseek-v4-pro",
70
+ "codex-auto-review": "deepseek-v4-pro"
71
+ }
72
+ }
73
+ ```
74
+
75
+ Lookup order: exact key match → wildcard `"*"` → original name (passthrough).
76
+
77
+ Or use `--model` CLI flag to force-override all model names:
78
+
79
+ ```bash
80
+ codex-transfer --model deepseek-v4-pro -k
81
+ ```
82
+
83
+ ### Environment Variables
84
+
85
+ | Variable | Default | Description |
86
+ |----------|---------|-------------|
87
+ | `CODEX_TRANSFER_PORT` | `4444` | Listen port |
88
+ | `CODEX_TRANSFER_UPSTREAM` | `https://openrouter.ai/api/v1` | Upstream Chat Completions base URL |
89
+ | `CODEX_TRANSFER_API_KEY` | _(empty)_ | API key forwarded to upstream |
90
+ | `CODEX_TRANSFER_CONFIG` | _(auto)_ | Path to config file |
91
+ | `CODEX_TRANSFER_INSECURE` | `false` | Skip TLS certificate verification |
92
+
93
+ ## Usage
94
+
95
+ ### Method 1: Direct execution
96
+
97
+ ```bash
98
+ node dist/codex-transfer.mjs -k -p 4446 -u https://api.deepseek.com/v1
99
+ ```
100
+
101
+ ### Method 2: npm link (global command)
102
+
103
+ ```bash
104
+ npm link
105
+ # Then run directly
106
+ codex-transfer -k
107
+ ```
108
+
109
+ ### Method 3: npx
110
+
111
+ ```bash
112
+ npx @classicicn/codex-transfer -k
113
+ ```
114
+
115
+ ### Method 4: Background (daemon) mode
116
+
117
+ ```bash
118
+ # Start as background process (logs → config_dir/logs/)
119
+ node dist/codex-transfer.mjs -d -k
120
+
121
+ # Output:
122
+ # codex-transfer started in background (PID: 12345)
123
+ # Log file: ~/.codex-transfer/logs/codex-transfer.log
124
+ # PID file: ~/.codex-transfer/logs/codex-transfer.pid
125
+ # Stop: kill $(cat ~/.codex-transfer/logs/codex-transfer.pid)
126
+
127
+ # View logs
128
+ tail -f ~/.codex-transfer/logs/codex-transfer.log
129
+
130
+ # Stop
131
+ kill $(cat ~/.codex-transfer/logs/codex-transfer.pid)
132
+ ```
133
+
134
+ ### Method 5: As a library (from source only)
135
+
136
+ > **Note:** The npm package contains only the CLI bundle. To use as a library, clone the repo and import from source.
137
+
138
+ ```typescript
139
+ import { createTransfer } from "./src/server.js";
140
+
141
+ const { app, port } = createTransfer({
142
+ configPath: "./codex-transfer.json",
143
+ port: 4446,
144
+ upstream: "https://api.deepseek.com/v1",
145
+ apiKey: "sk-...",
146
+ disableTlsVerify: true,
147
+ });
148
+ ```
149
+
150
+ ## Codex Configuration
151
+
152
+ Add to `~/.codex/config.toml`:
153
+
154
+ ```toml
155
+ model = "deepseek-v4-pro"
156
+ model_provider = "deepseek-transfer"
157
+
158
+ [model_providers.deepseek-transfer]
159
+ name = "DeepSeek"
160
+ base_url = "http://127.0.0.1:4446/v1"
161
+ wire_api = "responses"
162
+ ```
163
+
164
+ ## Supported Providers
165
+
166
+ | Provider | Base URL |
167
+ |----------|----------|
168
+ | DeepSeek | `https://api.deepseek.com/v1` |
169
+ | Kimi (Moonshot) | `https://api.moonshot.cn/v1` |
170
+ | Qwen | `https://dashscope.aliyuncs.com/compatible-mode/v1` |
171
+ | Mistral | `https://api.mistral.ai/v1` |
172
+ | Groq | `https://api.groq.com/openai/v1` |
173
+ | xAI | `https://api.x.ai/v1` |
174
+ | OpenRouter | `https://openrouter.ai/api/v1` |
175
+
176
+ ## Features
177
+
178
+ - **Single-file bundle** — `dist/codex-transfer.mjs` has zero runtime dependencies
179
+ - **Streaming** — full SSE streaming with correct event sequencing
180
+ - **Tool calls** — accumulates streaming deltas and emits structured function_call items
181
+ - **Parallel tool calls** — consecutive function_call input items merged into one assistant message
182
+ - **Tool call message ordering** — automatically reorders messages to ensure `assistant(tool_calls)` is immediately followed by matching `tool` messages (required by DeepSeek and other strict providers)
183
+ - **Model name mapping** — maps non-standard Codex model names (e.g. `codex-auto-review`) to upstream provider models via `modelMap` config or `--model` flag
184
+ - **Reasoning models** — preserves `reasoning_content` across turns (DeepSeek, kimi-k2.6)
185
+ - **Model catalog** — proxies `/v1/models` from the upstream provider
186
+ - **Health check** — `GET /health` diagnostic endpoint
187
+ - **TLS skip** — supports corporate proxy / self-signed certificate scenarios
188
+ - **Daemon mode** — `--daemon` runs in background with logs to `logs/` directory next to config file
189
+
190
+ ## Project Structure
191
+
192
+ | File | Description |
193
+ |------|-------------|
194
+ | `src/types.ts` | Responses/Chat Completions API type definitions |
195
+ | `src/config.ts` | Configuration loading (file + env vars) |
196
+ | `src/session.ts` | Session store and reasoning content cache |
197
+ | `src/translate.ts` | Request/response translation logic |
198
+ | `src/stream.ts` | SSE stream translation |
199
+ | `src/server.ts` | HTTP server (Hono) |
200
+ | `src/cli.ts` | CLI entry point |
201
+ | `build.mjs` | esbuild bundler script |
202
+
203
+ ## License
204
+
205
+ MIT
@@ -0,0 +1,205 @@
1
+ # codex-transfer
2
+
3
+ Responses API ↔ Chat Completions 转换桥(TypeScript 实现)
4
+
5
+ ## 概述
6
+
7
+ 一个轻量级代理,用于将 OpenAI **Responses API**(Codex CLI 使用)转换为 **Chat Completions API**,让 Codex 能够与任何 OpenAI 兼容的提供商配合使用——DeepSeek、Kimi、Qwen、Mistral、Groq、xAI、OpenRouter 等。
8
+
9
+ ```
10
+ Codex CLI (Responses API) → codex-transfer → DeepSeek (Chat Completions API)
11
+ ```
12
+
13
+ ## 快速开始
14
+
15
+ ```bash
16
+ # 从 npm 安装
17
+ npm install -g @classicicn/codex-transfer
18
+
19
+ # 启动
20
+ codex-transfer -k
21
+ ```
22
+
23
+ ## 命令行参数
24
+
25
+ ```
26
+ codex-transfer [options]
27
+
28
+ Options:
29
+ -p, --port PORT 监听端口(默认 4444)
30
+ -u, --upstream URL 上游 Chat Completions 地址
31
+ --api-key KEY 上游 API 密钥
32
+ -m, --model MODEL 强制覆盖模型名(最高优先级)
33
+ -c, --config PATH 配置文件路径(JSON)
34
+ -k, --insecure 跳过 TLS 证书验证
35
+ -d, --daemon 后台运行,日志输出到 logs/ 目录
36
+ -h, --help 显示帮助
37
+ ```
38
+
39
+ ## 配置
40
+
41
+ 配置优先级:CLI 参数 > 环境变量 > 配置文件 > 默认值
42
+
43
+ ### 配置文件
44
+
45
+ 在以下位置之一创建 JSON 配置文件:
46
+ - `./codex-transfer.json`(当前目录)
47
+ - `~/.codex-transfer/config.json`(用户主目录)
48
+ - 通过 `--config` 或 `CODEX_TRANSFER_CONFIG` 指定
49
+
50
+ ```json
51
+ {
52
+ "port": 4446,
53
+ "upstream": "https://api.deepseek.com/v1",
54
+ "apiKey": "sk-your-key-here",
55
+ "insecure": false,
56
+ "modelMap": {
57
+ "*": "deepseek-v4-pro"
58
+ }
59
+ }
60
+ ```
61
+
62
+ ### 模型名映射
63
+
64
+ Codex CLI 可能发送上游不识别的模型名(如 `codex-auto-review`)。使用 `modelMap` 进行转换:
65
+
66
+ ```json
67
+ {
68
+ "modelMap": {
69
+ "*": "deepseek-v4-pro",
70
+ "codex-auto-review": "deepseek-v4-pro"
71
+ }
72
+ }
73
+ ```
74
+
75
+ 查找顺序:精确匹配 key → 通配符 `"*"` → 原始模型名(直接透传)。
76
+
77
+ 也可使用 `--model` CLI 参数强制覆盖所有模型名:
78
+
79
+ ```bash
80
+ codex-transfer --model deepseek-v4-pro -k
81
+ ```
82
+
83
+ ### 环境变量
84
+
85
+ | 变量名 | 默认值 | 说明 |
86
+ |--------|--------|------|
87
+ | `CODEX_TRANSFER_PORT` | `4444` | 监听端口 |
88
+ | `CODEX_TRANSFER_UPSTREAM` | `https://openrouter.ai/api/v1` | 上游 Chat Completions 地址 |
89
+ | `CODEX_TRANSFER_API_KEY` | _(空)_ | 上游 API 密钥 |
90
+ | `CODEX_TRANSFER_CONFIG` | _(自动)_ | 配置文件路径 |
91
+ | `CODEX_TRANSFER_INSECURE` | `false` | 跳过 TLS 验证 |
92
+
93
+ ## 使用方式
94
+
95
+ ### 方式一:直接运行打包产物
96
+
97
+ ```bash
98
+ node dist/codex-transfer.mjs -k -p 4446 -u https://api.deepseek.com/v1
99
+ ```
100
+
101
+ ### 方式二:npm link(全局命令)
102
+
103
+ ```bash
104
+ npm link
105
+ # 之后可直接执行
106
+ codex-transfer -k
107
+ ```
108
+
109
+ ### 方式三:npx
110
+
111
+ ```bash
112
+ npx @classicicn/codex-transfer -k
113
+ ```
114
+
115
+ ### 方式四:后台运行
116
+
117
+ ```bash
118
+ # 启动后台进程(日志输出到配置文件同级 logs/ 目录)
119
+ node dist/codex-transfer.mjs -d -k
120
+
121
+ # 输出示例:
122
+ # codex-transfer started in background (PID: 12345)
123
+ # Log file: ~/.codex-transfer/logs/codex-transfer.log
124
+ # PID file: ~/.codex-transfer/logs/codex-transfer.pid
125
+ # Stop: kill $(cat ~/.codex-transfer/logs/codex-transfer.pid)
126
+
127
+ # 查看日志
128
+ tail -f ~/.codex-transfer/logs/codex-transfer.log
129
+
130
+ # 停止
131
+ kill $(cat ~/.codex-transfer/logs/codex-transfer.pid)
132
+ ```
133
+
134
+ ### 方式五:作为库使用(仅限源码)
135
+
136
+ > **注意:** npm 包仅包含 CLI 打包产物。如需作为库使用,请 clone 仓库后从源码导入。
137
+
138
+ ```typescript
139
+ import { createTransfer } from "./src/server.js";
140
+
141
+ const { app, port } = createTransfer({
142
+ configPath: "./codex-transfer.json",
143
+ port: 4446,
144
+ upstream: "https://api.deepseek.com/v1",
145
+ apiKey: "sk-...",
146
+ disableTlsVerify: true,
147
+ });
148
+ ```
149
+
150
+ ## Codex 配置
151
+
152
+ 在 `~/.codex/config.toml` 中添加:
153
+
154
+ ```toml
155
+ model = "deepseek-v4-pro"
156
+ model_provider = "deepseek-transfer"
157
+
158
+ [model_providers.deepseek-transfer]
159
+ name = "DeepSeek"
160
+ base_url = "http://127.0.0.1:4446/v1"
161
+ wire_api = "responses"
162
+ ```
163
+
164
+ ## 支持的提供商
165
+
166
+ | 提供商 | 基础 URL |
167
+ |--------|----------|
168
+ | DeepSeek | `https://api.deepseek.com/v1` |
169
+ | Kimi (Moonshot) | `https://api.moonshot.cn/v1` |
170
+ | Qwen | `https://dashscope.aliyuncs.com/compatible-mode/v1` |
171
+ | Mistral | `https://api.mistral.ai/v1` |
172
+ | Groq | `https://api.groq.com/openai/v1` |
173
+ | xAI | `https://api.x.ai/v1` |
174
+ | OpenRouter | `https://openrouter.ai/api/v1` |
175
+
176
+ ## 功能特性
177
+
178
+ - **单文件打包** — `dist/codex-transfer.mjs` 无外部依赖,拷贝即用
179
+ - **流式传输** — 完整的 SSE 流式传输,正确的事件排序
180
+ - **工具调用** — 累积流式增量并发出结构化的 function_call 项目
181
+ - **并行工具调用** — 连续的 function_call 输入项目合并为单个 assistant 消息
182
+ - **工具调用消息排序** — 自动重排消息,确保 `assistant(tool_calls)` 后紧跟对应的 `tool` 消息(DeepSeek 等严格提供商要求)
183
+ - **模型名映射** — 将 Codex 非标准模型名(如 `codex-auto-review`)映射到上游提供商模型,支持 `modelMap` 配置或 `--model` 参数
184
+ - **推理模型** — 跨轮次保留 `reasoning_content`(DeepSeek、kimi-k2.6)
185
+ - **模型目录** — 代理上游的 `/v1/models` 端点
186
+ - **健康检查** — `GET /health` 诊断上游连接状态
187
+ - **TLS 跳过** — 支持企业代理/自签名证书场景
188
+ - **后台运行** — `--daemon` 后台运行,日志输出到配置文件同级 `logs/` 目录
189
+
190
+ ## 项目架构
191
+
192
+ | 文件 | 说明 |
193
+ |------|------|
194
+ | `src/types.ts` | Responses/Chat Completions API 类型定义 |
195
+ | `src/config.ts` | 配置加载(配置文件 + 环境变量) |
196
+ | `src/session.ts` | 会话存储与推理内容缓存 |
197
+ | `src/translate.ts` | 请求/响应转换逻辑 |
198
+ | `src/stream.ts` | SSE 流式转换 |
199
+ | `src/server.ts` | HTTP 服务(Hono) |
200
+ | `src/cli.ts` | CLI 入口 |
201
+ | `build.mjs` | esbuild 打包脚本 |
202
+
203
+ ## 许可证
204
+
205
+ MIT