Files
chatgpt-codex-router/test-direct-response.js
mars 0dd6fe2c7d feat: 实现ChatGPT Codex路由器的核心功能
- 添加完整的项目基础结构,包括配置、类型定义和常量
- 实现OAuth认证流程和令牌管理
- 开发请求转换和响应处理逻辑
- 添加SSE流处理和ChatCompletions API转换
- 实现模型映射和提示指令系统
- 包含Docker部署配置和快速启动文档
- 添加自动登录功能和测试脚本
2026-01-07 10:51:54 +08:00

75 lines
2.0 KiB
JavaScript

import { getAccessToken } from './dist/auth/token-refresh.js';
import { getAccountId } from './dist/auth/token-refresh.js';
const accessToken = await getAccessToken();
const accountId = await getAccountId();
if (!accessToken || !accountId) {
console.error('No token or account ID');
process.exit(1);
}
const instructions = await import('./dist/prompts/index.js').then(m => m.getCodexInstructions('gpt-5.1'));
const response = await fetch('https://chatgpt.com/backend-api/codex/responses', {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'openai-account-id': accountId,
'openai-beta': 'responses=2',
'openai-originator': 'codex_cli_rs',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-5.1',
input: [
{
type: 'message',
role: 'user',
content: [
{
type: 'input_text',
text: 'Hello, say hi in one word'
}
]
}
],
stream: true,
store: false,
reasoning: { effort: 'medium', summary: 'auto' },
instructions: instructions
})
});
console.log('Status:', response.status);
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let chunks = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
chunks++;
const data = line.substring(6);
if (data.trim() !== '[DONE]') {
try {
const parsed = JSON.parse(data);
console.log(`Chunk ${chunks}: type="${parsed.type}", delta=`, parsed.delta ? JSON.stringify(parsed.delta).substring(0, 100) : 'N/A');
if (chunks >= 10) process.exit(0);
} catch (e) {
console.log(`Raw chunk ${chunks}:`, data.substring(0, 100));
}
}
}
}
}