* 解析一行中可能包含的多个 JSON 对象 * Codex 有时会在同一行输出多个 JSON: {"type":"error",...} {"type":"turn.failed",...}
(line: string)
| 124 | const lines = this.buffer.split('\n'); |
| 125 | this.buffer = lines.pop() || ''; |
| 126 | |
| 127 | for (const line of lines) { |
| 128 | const trimmed = line.trim(); |
| 129 | if (!trimmed) continue; |
| 130 | |
| 131 | // 收集非 JSON 行(如 ERROR 日志),用于异常退出时展示 |
| 132 | if (!trimmed.startsWith('{')) { |
| 133 | this.nonJsonLines.push(stripAnsiSequences(trimmed)); |
| 134 | if (DEBUG_PARSER) { |
| 135 | console.log('[CodexParser] Skipping non-JSON line:', trimmed.substring(0, 100)); |
| 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | this.parseJsonSegments(trimmed); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * 解析一行中可能包含的多个 JSON 对象 |
| 146 | * Codex 有时会在同一行输出多个 JSON: {"type":"error",...} {"type":"turn.failed",...} |
| 147 | */ |
| 148 | private parseJsonSegments(line: string): void { |
| 149 | // 尝试直接解析整行 |
| 150 | try { |
| 151 | const event: CodexEvent = JSON.parse(line); |
no test coverage detected