feat: 实现ChatGPT Codex路由器的核心功能
- 添加完整的项目基础结构,包括配置、类型定义和常量 - 实现OAuth认证流程和令牌管理 - 开发请求转换和响应处理逻辑 - 添加SSE流处理和ChatCompletions API转换 - 实现模型映射和提示指令系统 - 包含Docker部署配置和快速启动文档 - 添加自动登录功能和测试脚本
This commit is contained in:
42
src/response/sse-parser.ts
Normal file
42
src/response/sse-parser.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { SSEEventData, SSEChunk } from "../types.js";
|
||||
import { logDebug, logError } from "../logger.js";
|
||||
|
||||
export function parseSseStream(sseText: string): SSEEventData | null {
|
||||
const lines = sseText.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("data: ")) {
|
||||
try {
|
||||
const data = JSON.parse(line.substring(6)) as SSEEventData;
|
||||
|
||||
if (data.type === "response.done" || data.type === "response.completed") {
|
||||
logDebug(null, "Found response.done event in SSE stream");
|
||||
return data;
|
||||
}
|
||||
} catch (error) {
|
||||
logError(null, `Failed to parse SSE event: ${line}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logError(null, "No response.done event found in SSE stream");
|
||||
return null;
|
||||
}
|
||||
|
||||
export function parseSseChunks(sseText: string): SSEChunk[] {
|
||||
const chunks: SSEChunk[] = [];
|
||||
const lines = sseText.split("\n");
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("data: ")) {
|
||||
try {
|
||||
const data = JSON.parse(line.substring(6));
|
||||
chunks.push(data as SSEChunk);
|
||||
} catch (error) {
|
||||
logError(null, `Failed to parse SSE chunk: ${line}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
Reference in New Issue
Block a user