Initial commit: OpenAI Codex reverse proxy with Node.js + Express
This commit is contained in:
92
src/index.js
Normal file
92
src/index.js
Normal file
@@ -0,0 +1,92 @@
|
||||
import express from 'express';
|
||||
import dotenv from 'dotenv';
|
||||
import fs from 'fs/promises';
|
||||
import TokenManager from './tokenManager.js';
|
||||
import ProxyHandler from './proxyHandler.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const TOKEN_FILE = process.env.TOKEN_FILE || './token.json';
|
||||
const MODELS_FILE = process.env.MODELS_FILE || './models.json';
|
||||
|
||||
// 中间件
|
||||
app.use(express.json());
|
||||
|
||||
// 初始化 Token 管理器和代理处理器
|
||||
const tokenManager = new TokenManager(TOKEN_FILE);
|
||||
const proxyHandler = new ProxyHandler(tokenManager);
|
||||
|
||||
// 加载模型列表
|
||||
let modelsList = [];
|
||||
try {
|
||||
const modelsData = await fs.readFile(MODELS_FILE, 'utf-8');
|
||||
modelsList = JSON.parse(modelsData);
|
||||
console.log(`✓ 加载了 ${modelsList.length} 个模型`);
|
||||
} catch (err) {
|
||||
console.warn('⚠ 无法加载模型列表,使用默认列表');
|
||||
modelsList = [
|
||||
{ id: 'gpt-5.3-codex', object: 'model', created: 1770307200, owned_by: 'openai' },
|
||||
{ id: 'gpt-5.2-codex', object: 'model', created: 1765440000, owned_by: 'openai' }
|
||||
];
|
||||
}
|
||||
|
||||
// 启动时加载 token
|
||||
await tokenManager.loadToken().catch(err => {
|
||||
console.error('❌ 启动失败:', err.message);
|
||||
console.error('请确保 token.json 文件存在且格式正确');
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// 健康检查
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
token: tokenManager.getTokenInfo()
|
||||
});
|
||||
});
|
||||
|
||||
// OpenAI 兼容的聊天完成接口
|
||||
app.post('/v1/chat/completions', async (req, res) => {
|
||||
const isStream = req.body.stream === true;
|
||||
|
||||
if (isStream) {
|
||||
await proxyHandler.handleStreamRequest(req, res);
|
||||
} else {
|
||||
await proxyHandler.handleNonStreamRequest(req, res);
|
||||
}
|
||||
});
|
||||
|
||||
// 模型列表接口
|
||||
app.get('/v1/models', (req, res) => {
|
||||
res.json({
|
||||
object: 'list',
|
||||
data: modelsList
|
||||
});
|
||||
});
|
||||
|
||||
// 错误处理
|
||||
app.use((err, req, res, next) => {
|
||||
console.error('服务器错误:', err);
|
||||
res.status(500).json({
|
||||
error: {
|
||||
message: err.message || '内部服务器错误',
|
||||
type: 'server_error'
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
app.listen(PORT, () => {
|
||||
console.log('=================================');
|
||||
console.log('🚀 GPT2API Node 服务已启动');
|
||||
console.log(`📡 监听端口: ${PORT}`);
|
||||
console.log(`👤 账户: ${tokenManager.getTokenInfo().email || tokenManager.getTokenInfo().account_id}`);
|
||||
console.log(`⏰ Token 过期时间: ${tokenManager.getTokenInfo().expired}`);
|
||||
console.log('=================================');
|
||||
console.log(`\n接口地址:`);
|
||||
console.log(` - 聊天: POST http://localhost:${PORT}/v1/chat/completions`);
|
||||
console.log(` - 模型: GET http://localhost:${PORT}/v1/models`);
|
||||
console.log(` - 健康: GET http://localhost:${PORT}/health\n`);
|
||||
});
|
||||
423
src/proxyHandler.js
Normal file
423
src/proxyHandler.js
Normal file
@@ -0,0 +1,423 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const CODEX_BASE_URL = 'https://chatgpt.com/backend-api/codex';
|
||||
const CODEX_CLIENT_VERSION = '0.101.0';
|
||||
const CODEX_USER_AGENT = 'codex_cli_rs/0.101.0 (Mac OS 26.0.1; arm64) Apple_Terminal/464';
|
||||
|
||||
/**
|
||||
* 代理处理器
|
||||
*/
|
||||
class ProxyHandler {
|
||||
constructor(tokenManager) {
|
||||
this.tokenManager = tokenManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成会话 ID
|
||||
*/
|
||||
generateSessionId() {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 OpenAI 格式请求到 Codex 格式
|
||||
*/
|
||||
transformRequest(openaiRequest) {
|
||||
const { model, messages, stream = true, stream_options, ...rest } = openaiRequest;
|
||||
|
||||
// 提取 system 消息作为 instructions
|
||||
let instructions = '';
|
||||
const userMessages = [];
|
||||
|
||||
for (const msg of messages) {
|
||||
if (msg.role === 'system') {
|
||||
// system 消息转为 instructions
|
||||
const content = Array.isArray(msg.content)
|
||||
? msg.content.map(c => c.text || c).join('\n')
|
||||
: msg.content;
|
||||
instructions += (instructions ? '\n' : '') + content;
|
||||
} else {
|
||||
// 其他消息保留
|
||||
userMessages.push(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// 转换消息格式
|
||||
const input = userMessages.map(msg => {
|
||||
const contentType = msg.role === 'assistant' ? 'output_text' : 'input_text';
|
||||
|
||||
return {
|
||||
type: 'message',
|
||||
role: msg.role,
|
||||
content: Array.isArray(msg.content)
|
||||
? msg.content.map(c => {
|
||||
// 处理不同类型的内容
|
||||
if (c.type === 'text') {
|
||||
return { type: contentType, text: c.text || c };
|
||||
} else if (c.type === 'image_url') {
|
||||
// OpenAI 的 image_url 转换为 Codex 的 input_image
|
||||
return {
|
||||
type: 'input_image',
|
||||
image_url: c.image_url?.url || c.image_url
|
||||
};
|
||||
} else {
|
||||
return c;
|
||||
}
|
||||
})
|
||||
: [{ type: contentType, text: msg.content }]
|
||||
};
|
||||
});
|
||||
|
||||
// 移除 Codex 不支持的参数
|
||||
const codexRequest = {
|
||||
model: model || 'gpt-5.3-codex',
|
||||
input,
|
||||
instructions: instructions || '',
|
||||
stream,
|
||||
store: false // 必须设置为 false
|
||||
};
|
||||
|
||||
// 只保留 Codex 支持的参数
|
||||
if (rest.temperature !== undefined) codexRequest.temperature = rest.temperature;
|
||||
if (rest.max_tokens !== undefined) codexRequest.max_tokens = rest.max_tokens;
|
||||
if (rest.top_p !== undefined) codexRequest.top_p = rest.top_p;
|
||||
|
||||
return codexRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 Codex 响应到 OpenAI 格式
|
||||
*/
|
||||
transformResponse(codexResponse, model, isStream = false, state = {}) {
|
||||
if (isStream) {
|
||||
// 流式响应处理
|
||||
const line = codexResponse.toString().trim();
|
||||
|
||||
if (!line.startsWith('data:')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = line.slice(5).trim();
|
||||
|
||||
if (data === '[DONE]') {
|
||||
return 'data: [DONE]\n\n';
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
|
||||
// 保存响应 ID 和创建时间
|
||||
if (parsed.type === 'response.created') {
|
||||
state.responseId = parsed.response?.id;
|
||||
state.createdAt = parsed.response?.created_at || Math.floor(Date.now() / 1000);
|
||||
state.model = parsed.response?.model || model;
|
||||
return null;
|
||||
}
|
||||
|
||||
const responseId = state.responseId || 'chatcmpl-' + Date.now();
|
||||
const createdAt = state.createdAt || Math.floor(Date.now() / 1000);
|
||||
const modelName = state.model || model;
|
||||
|
||||
// 处理不同类型的事件 - 根据原项目的实现
|
||||
if (parsed.type === 'response.output_text.delta') {
|
||||
// 文本增量更新
|
||||
return `data: ${JSON.stringify({
|
||||
id: responseId,
|
||||
object: 'chat.completion.chunk',
|
||||
created: createdAt,
|
||||
model: modelName,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: { role: 'assistant', content: parsed.delta || '' },
|
||||
finish_reason: null
|
||||
}]
|
||||
})}\n\n`;
|
||||
} else if (parsed.type === 'response.reasoning_summary_text.delta') {
|
||||
// 推理内容增量
|
||||
return `data: ${JSON.stringify({
|
||||
id: responseId,
|
||||
object: 'chat.completion.chunk',
|
||||
created: createdAt,
|
||||
model: modelName,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: { role: 'assistant', reasoning_content: parsed.delta || '' },
|
||||
finish_reason: null
|
||||
}]
|
||||
})}\n\n`;
|
||||
} else if (parsed.type === 'response.completed') {
|
||||
// 提取使用信息
|
||||
const usage = parsed.response?.usage || {};
|
||||
return `data: ${JSON.stringify({
|
||||
id: parsed.response_id || 'chatcmpl-' + Date.now(),
|
||||
object: 'chat.completion.chunk',
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {},
|
||||
finish_reason: 'stop'
|
||||
}],
|
||||
usage: {
|
||||
prompt_tokens: usage.input_tokens || 0,
|
||||
completion_tokens: usage.output_tokens || 0,
|
||||
total_tokens: usage.total_tokens || 0
|
||||
}
|
||||
})}\n\n`;
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略 JSON 解析错误,可能是不完整的数据
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
} else {
|
||||
// 非流式响应处理
|
||||
try {
|
||||
const parsed = typeof codexResponse === 'string'
|
||||
? JSON.parse(codexResponse)
|
||||
: codexResponse;
|
||||
|
||||
const response = parsed.response || {};
|
||||
const output = response.output || [];
|
||||
|
||||
// 提取消息内容
|
||||
let content = '';
|
||||
for (const item of output) {
|
||||
if (item.type === 'message' && item.content) {
|
||||
for (const part of item.content) {
|
||||
if (part.type === 'output_text') {
|
||||
content += part.text || '';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const usage = response.usage || {};
|
||||
|
||||
return {
|
||||
id: response.id || 'chatcmpl-' + Date.now(),
|
||||
object: 'chat.completion',
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: content
|
||||
},
|
||||
finish_reason: 'stop'
|
||||
}],
|
||||
usage: {
|
||||
prompt_tokens: usage.input_tokens || 0,
|
||||
completion_tokens: usage.output_tokens || 0,
|
||||
total_tokens: usage.total_tokens || 0
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
throw new Error(`转换响应失败: ${e.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理流式请求
|
||||
*/
|
||||
async handleStreamRequest(req, res) {
|
||||
try {
|
||||
const openaiRequest = req.body;
|
||||
console.log('收到请求:', JSON.stringify(openaiRequest, null, 2));
|
||||
|
||||
const codexRequest = this.transformRequest(openaiRequest);
|
||||
console.log('转换后的 Codex 请求:', JSON.stringify(codexRequest, null, 2));
|
||||
|
||||
const accessToken = await this.tokenManager.getValidToken();
|
||||
|
||||
// 设置响应头
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
|
||||
const response = await axios.post(
|
||||
`${CODEX_BASE_URL}/responses`,
|
||||
codexRequest,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': CODEX_USER_AGENT,
|
||||
'Version': CODEX_CLIENT_VERSION,
|
||||
'Openai-Beta': 'responses=experimental',
|
||||
'Session_id': this.generateSessionId(),
|
||||
'Accept': 'text/event-stream'
|
||||
},
|
||||
responseType: 'stream',
|
||||
timeout: 300000 // 5 分钟超时
|
||||
}
|
||||
);
|
||||
|
||||
// 处理流式响应
|
||||
let buffer = '';
|
||||
const state = {}; // 用于保存响应 ID 和创建时间
|
||||
|
||||
response.data.on('data', (chunk) => {
|
||||
buffer += chunk.toString();
|
||||
const lines = buffer.split('\n');
|
||||
|
||||
// 保留最后一行(可能不完整)
|
||||
buffer = lines.pop() || '';
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim()) {
|
||||
const transformed = this.transformResponse(line, openaiRequest.model, true, state);
|
||||
if (transformed) {
|
||||
res.write(transformed);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
response.data.on('end', () => {
|
||||
// 处理缓冲区中剩余的数据
|
||||
if (buffer.trim()) {
|
||||
const transformed = this.transformResponse(buffer, openaiRequest.model, true, state);
|
||||
if (transformed) {
|
||||
res.write(transformed);
|
||||
}
|
||||
}
|
||||
res.write('data: [DONE]\n\n');
|
||||
res.end();
|
||||
});
|
||||
|
||||
response.data.on('error', (error) => {
|
||||
console.error('流式响应错误:', error.message);
|
||||
res.end();
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('代理请求失败:', error.message);
|
||||
if (error.response) {
|
||||
console.error('响应状态:', error.response.status);
|
||||
console.error('响应头:', error.response.headers);
|
||||
|
||||
// 尝试读取响应数据
|
||||
if (error.response.data) {
|
||||
if (typeof error.response.data === 'string') {
|
||||
console.error('响应数据:', error.response.data);
|
||||
} else if (error.response.data.on) {
|
||||
// 如果是流,尝试读取
|
||||
let data = '';
|
||||
error.response.data.on('data', chunk => {
|
||||
data += chunk.toString();
|
||||
});
|
||||
error.response.data.on('end', () => {
|
||||
console.error('响应数据:', data);
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
console.error('响应数据:', JSON.stringify(error.response.data));
|
||||
} catch (e) {
|
||||
console.error('响应数据类型:', typeof error.response.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!res.headersSent) {
|
||||
res.status(error.response?.status || 500).json({
|
||||
error: {
|
||||
message: error.response?.data?.error?.message || error.message,
|
||||
type: 'proxy_error',
|
||||
code: error.response?.status || 500
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理非流式请求
|
||||
*/
|
||||
async handleNonStreamRequest(req, res) {
|
||||
try {
|
||||
const openaiRequest = req.body;
|
||||
console.log('收到请求:', JSON.stringify(openaiRequest, null, 2));
|
||||
|
||||
const codexRequest = this.transformRequest({ ...openaiRequest, stream: false });
|
||||
console.log('转换后的 Codex 请求:', JSON.stringify(codexRequest, null, 2));
|
||||
|
||||
const accessToken = await this.tokenManager.getValidToken();
|
||||
|
||||
const response = await axios.post(
|
||||
`${CODEX_BASE_URL}/responses`,
|
||||
codexRequest,
|
||||
{
|
||||
headers: {
|
||||
'Authorization': `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': CODEX_USER_AGENT,
|
||||
'Version': CODEX_CLIENT_VERSION,
|
||||
'Openai-Beta': 'responses=experimental',
|
||||
'Session_id': this.generateSessionId()
|
||||
},
|
||||
timeout: 300000
|
||||
}
|
||||
);
|
||||
|
||||
// 处理响应数据 - 查找 response.completed 事件
|
||||
let finalResponse = null;
|
||||
const lines = response.data.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim().startsWith('data:')) {
|
||||
const data = line.slice(5).trim();
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
if (parsed.type === 'response.completed') {
|
||||
finalResponse = parsed;
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!finalResponse) {
|
||||
throw new Error('未收到完整响应');
|
||||
}
|
||||
|
||||
// 转换为 OpenAI 格式
|
||||
const transformed = this.transformNonStreamResponse(finalResponse, openaiRequest.model);
|
||||
res.json(transformed);
|
||||
|
||||
} catch (error) {
|
||||
console.error('代理请求失败:', error.message);
|
||||
if (error.response) {
|
||||
console.error('响应状态:', error.response.status);
|
||||
console.error('响应头:', error.response.headers);
|
||||
try {
|
||||
console.error('响应数据:', typeof error.response.data === 'string'
|
||||
? error.response.data
|
||||
: JSON.stringify(error.response.data));
|
||||
} catch (e) {
|
||||
console.error('响应数据无法序列化');
|
||||
}
|
||||
}
|
||||
|
||||
res.status(error.response?.status || 500).json({
|
||||
error: {
|
||||
message: error.response?.data?.error?.message || error.message,
|
||||
type: 'proxy_error',
|
||||
code: error.response?.status || 500
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default ProxyHandler;
|
||||
132
src/tokenManager.js
Normal file
132
src/tokenManager.js
Normal file
@@ -0,0 +1,132 @@
|
||||
import fs from 'fs/promises';
|
||||
import axios from 'axios';
|
||||
|
||||
// OpenAI OAuth 配置
|
||||
const TOKEN_URL = 'https://auth.openai.com/oauth/token';
|
||||
const CLIENT_ID = 'app_EMoamEEZ73f0CkXaXp7hrann';
|
||||
|
||||
/**
|
||||
* Token 管理器
|
||||
*/
|
||||
class TokenManager {
|
||||
constructor(tokenFilePath) {
|
||||
this.tokenFilePath = tokenFilePath;
|
||||
this.tokenData = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件加载 token
|
||||
*/
|
||||
async loadToken() {
|
||||
try {
|
||||
const data = await fs.readFile(this.tokenFilePath, 'utf-8');
|
||||
this.tokenData = JSON.parse(data);
|
||||
console.log(`✓ Token 加载成功: ${this.tokenData.email || this.tokenData.account_id}`);
|
||||
return this.tokenData;
|
||||
} catch (error) {
|
||||
throw new Error(`加载 token 文件失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存 token 到文件
|
||||
*/
|
||||
async saveToken(tokenData) {
|
||||
try {
|
||||
this.tokenData = tokenData;
|
||||
await fs.writeFile(this.tokenFilePath, JSON.stringify(tokenData, null, 2), 'utf-8');
|
||||
console.log('✓ Token 已保存到文件');
|
||||
} catch (error) {
|
||||
console.error(`保存 token 文件失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 token 是否过期
|
||||
*/
|
||||
isTokenExpired() {
|
||||
if (!this.tokenData || !this.tokenData.expired) {
|
||||
return true;
|
||||
}
|
||||
const expireTime = new Date(this.tokenData.expired);
|
||||
const now = new Date();
|
||||
// 提前 5 分钟刷新
|
||||
return expireTime.getTime() - now.getTime() < 5 * 60 * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新 access token
|
||||
*/
|
||||
async refreshToken() {
|
||||
if (!this.tokenData || !this.tokenData.refresh_token) {
|
||||
throw new Error('没有可用的 refresh_token');
|
||||
}
|
||||
|
||||
console.log('正在刷新 token...');
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
client_id: CLIENT_ID,
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: this.tokenData.refresh_token,
|
||||
scope: 'openid profile email'
|
||||
});
|
||||
|
||||
const response = await axios.post(TOKEN_URL, params.toString(), {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
const { access_token, refresh_token, id_token, expires_in } = response.data;
|
||||
|
||||
// 更新 token 数据
|
||||
const newTokenData = {
|
||||
...this.tokenData,
|
||||
access_token,
|
||||
refresh_token: refresh_token || this.tokenData.refresh_token,
|
||||
id_token: id_token || this.tokenData.id_token,
|
||||
expired: new Date(Date.now() + expires_in * 1000).toISOString(),
|
||||
last_refresh: new Date().toISOString()
|
||||
};
|
||||
|
||||
await this.saveToken(newTokenData);
|
||||
console.log('✓ Token 刷新成功');
|
||||
|
||||
return newTokenData;
|
||||
} catch (error) {
|
||||
const errorMsg = error.response?.data || error.message;
|
||||
throw new Error(`Token 刷新失败: ${JSON.stringify(errorMsg)}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取有效的 access token(自动刷新)
|
||||
*/
|
||||
async getValidToken() {
|
||||
if (!this.tokenData) {
|
||||
await this.loadToken();
|
||||
}
|
||||
|
||||
if (this.isTokenExpired()) {
|
||||
await this.refreshToken();
|
||||
}
|
||||
|
||||
return this.tokenData.access_token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 token 信息
|
||||
*/
|
||||
getTokenInfo() {
|
||||
return {
|
||||
email: this.tokenData?.email,
|
||||
account_id: this.tokenData?.account_id,
|
||||
expired: this.tokenData?.expired,
|
||||
type: this.tokenData?.type
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default TokenManager;
|
||||
Reference in New Issue
Block a user