feat: 实现ChatGPT Codex路由器的核心功能

- 添加完整的项目基础结构,包括配置、类型定义和常量
- 实现OAuth认证流程和令牌管理
- 开发请求转换和响应处理逻辑
- 添加SSE流处理和ChatCompletions API转换
- 实现模型映射和提示指令系统
- 包含Docker部署配置和快速启动文档
- 添加自动登录功能和测试脚本
This commit is contained in:
mars
2026-01-07 10:51:54 +08:00
commit 0dd6fe2c7d
45 changed files with 8286 additions and 0 deletions

107
test-codex-raw.js Normal file
View File

@@ -0,0 +1,107 @@
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"),
);
console.log("Fetching from Codex API...");
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: "Say hello in one word",
},
],
},
],
stream: true,
store: false,
reasoning: { effort: "medium", summary: "auto" },
instructions: instructions,
}),
},
);
console.log("Status:", response.status);
if (response.status !== 200) {
const errorText = await response.text();
console.error("Error:", errorText);
process.exit(1);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let chunks = 0;
console.log("\n=== Raw SSE Chunks ===\n");
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(`\n--- Chunk ${chunks} ---`);
console.log(`Type: ${parsed.type}`);
console.log(`Has delta: ${!!parsed.delta}`);
if (parsed.delta) {
console.log(`Delta:`, JSON.stringify(parsed.delta, null, 2));
}
console.log(`Full chunk keys:`, Object.keys(parsed));
if (parsed.item) {
console.log(
`Item type: ${parsed.item.type}, role: ${parsed.item.role}`,
);
}
if (chunks >= 20) {
console.log("\n=== Stopping after 20 chunks ===");
process.exit(0);
}
} catch (e) {
console.log(`\nChunk ${chunks} (raw):`, data.substring(0, 200));
}
}
}
}
}
console.log("\n=== End of stream ===");